]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] exclaim preceding regex literals.
authorMartin Probst <martin@probst.io>
Tue, 7 Feb 2017 14:08:03 +0000 (14:08 +0000)
committerMartin Probst <martin@probst.io>
Tue, 7 Feb 2017 14:08:03 +0000 (14:08 +0000)
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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@294304 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/FormatTokenLexer.cpp
unittests/Format/FormatTestJS.cpp

index 46a32a917dd93911632c507d434e50c74b0d8f5c..5bd1168587f3ff1a1c2937ad2341106d5fe402a2 100644 (file)
@@ -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
index b64d13ad50993d8b6007e55a6274d44c31166ed3..61acce3a9774b0235b761350bf9d3732e9ad1544 100644 (file)
@@ -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);");