r241264 - clang-format: [JS] Skip comments when applying the regex-literal heuristic

Daniel Jasper djasper at google.com
Thu Jul 2 07:14:04 PDT 2015


Author: djasper
Date: Thu Jul  2 09:14:04 2015
New Revision: 241264

URL: http://llvm.org/viewvc/llvm-project?rev=241264&view=rev
Log:
clang-format: [JS] Skip comments when applying the regex-literal heuristic

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

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=241264&r1=241263&r2=241264&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Thu Jul  2 09:14:04 2015
@@ -783,12 +783,16 @@ private:
     unsigned TokenCount = 0;
     for (auto I = Tokens.rbegin() + 1, E = Tokens.rend(); I != E; ++I) {
       ++TokenCount;
+      auto Prev = I + 1;
+      while (Prev != E && Prev[0]->is(tok::comment))
+        ++Prev;
       if (I[0]->isOneOf(tok::slash, tok::slashequal) &&
-          (I + 1 == E ||
-           ((I[1]->isOneOf(tok::l_paren, tok::semi, tok::l_brace, tok::r_brace,
-                           tok::exclaim, tok::l_square, tok::colon, tok::comma,
-                           tok::question, tok::kw_return) ||
-             I[1]->isBinaryOperator())))) {
+          (Prev == E ||
+           ((Prev[0]->isOneOf(tok::l_paren, tok::semi, tok::l_brace,
+                              tok::r_brace, tok::exclaim, tok::l_square,
+                              tok::colon, tok::comma, tok::question,
+                              tok::kw_return) ||
+             Prev[0]->isBinaryOperator())))) {
         unsigned LastColumn = Tokens.back()->OriginalColumn;
         SourceLocation Loc = Tokens.back()->Tok.getLocation();
         if (MightEndWithEscapedSlash) {

Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=241264&r1=241263&r2=241264&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Thu Jul  2 09:14:04 2015
@@ -636,6 +636,8 @@ TEST_F(FormatTestJS, RegexLiteralSpecial
   verifyFormat("var regex =\n"
                "    /\"/;",
                getGoogleJSStyleWithColumns(15));
+  verifyFormat("var regex =  //\n"
+               "    /a/;");
 }
 
 TEST_F(FormatTestJS, RegexLiteralModifiers) {





More information about the cfe-commits mailing list