summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Probst <martin@probst.io>2017-02-07 14:08:03 +0000
committerMartin Probst <martin@probst.io>2017-02-07 14:08:03 +0000
commit16282993b77e1d2bda3d7fb94a855af081311563 (patch)
tree2f481503223696faf50b568c5386f8c4eb2cd11b
parentce43bdb3d06a519731682e2727c26c7f986fe541 (diff)
downloadbcm5719-llvm-16282993b77e1d2bda3d7fb94a855af081311563.tar.gz
bcm5719-llvm-16282993b77e1d2bda3d7fb94a855af081311563.zip
clang-format: [JS] exclaim preceding regex literals.
Summary: Regex detection would incorrectly classify a trailing `!` operator (nullability cast) followed by a `/` as the start of a regular expression literal. This fixes code such as: var foo = x()! / 10; Which would previously parse a regexp all the way to the end of the source file (or next `/`). Reviewers: djasper Subscribers: cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D29634 llvm-svn: 294304
-rw-r--r--clang/lib/Format/FormatTokenLexer.cpp4
-rw-r--r--clang/unittests/Format/FormatTestJS.cpp1
2 files changed, 4 insertions, 1 deletions
diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp
index 46a32a917dd..5bd1168587f 100644
--- a/clang/lib/Format/FormatTokenLexer.cpp
+++ b/clang/lib/Format/FormatTokenLexer.cpp
@@ -157,7 +157,9 @@ bool FormatTokenLexer::canPrecedeRegexLiteral(FormatToken *Prev) {
// postfix unary operators. If the '++' is followed by a non-operand
// introducing token, the slash here is the operand and not the start of a
// regex.
- if (Prev->isOneOf(tok::plusplus, tok::minusminus))
+ // `!` is an unary prefix operator, but also a post-fix operator that casts
+ // away nullability, so the same check applies.
+ if (Prev->isOneOf(tok::plusplus, tok::minusminus, tok::exclaim))
return (Tokens.size() < 3 || precedesOperand(Tokens[Tokens.size() - 3]));
// The previous token must introduce an operand location where regex
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index b64d13ad509..61acce3a977 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -941,6 +941,7 @@ TEST_F(FormatTestJS, RegexLiteralClassification) {
verifyFormat("var x = a ? /abc/ : /abc/;");
verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}");
verifyFormat("var x = !/abc/.test(y);");
+ verifyFormat("var x = foo()! / 10;");
verifyFormat("var x = a && /abc/.test(y);");
verifyFormat("var x = a || /abc/.test(y);");
verifyFormat("var x = a + /abc/.search(y);");
OpenPOWER on IntegriCloud