r294304 - clang-format: [JS] exclaim preceding regex literals.

Martin Probst via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 7 06:08:03 PST 2017


Author: mprobst
Date: Tue Feb  7 08:08:03 2017
New Revision: 294304

URL: http://llvm.org/viewvc/llvm-project?rev=294304&view=rev
Log:
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

Modified:
    cfe/trunk/lib/Format/FormatTokenLexer.cpp
    cfe/trunk/unittests/Format/FormatTestJS.cpp

Modified: cfe/trunk/lib/Format/FormatTokenLexer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/FormatTokenLexer.cpp?rev=294304&r1=294303&r2=294304&view=diff
==============================================================================
--- cfe/trunk/lib/Format/FormatTokenLexer.cpp (original)
+++ cfe/trunk/lib/Format/FormatTokenLexer.cpp Tue Feb  7 08:08:03 2017
@@ -157,7 +157,9 @@ bool FormatTokenLexer::canPrecedeRegexLi
   // 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

Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=294304&r1=294303&r2=294304&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Tue Feb  7 08:08:03 2017
@@ -941,6 +941,7 @@ TEST_F(FormatTestJS, RegexLiteralClassif
   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);");




More information about the cfe-commits mailing list