]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] Skip comments when applying the regex-literal heuristic
authorDaniel Jasper <djasper@google.com>
Thu, 2 Jul 2015 14:14:04 +0000 (14:14 +0000)
committerDaniel Jasper <djasper@google.com>
Thu, 2 Jul 2015 14:14:04 +0000 (14:14 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@241264 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 6e2012eb0fd86c1de62cc8180b8bbf9981854766..f97e1fbc6d778b854b82ca4ca970f94b39640a4e 100644 (file)
@@ -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) {
index 40a2a1ffe6360b447509bf4854d2cb34c9900fe3..ef5901e8d69c319e1cd455de8278208d59239d2d 100644 (file)
@@ -636,6 +636,8 @@ TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) {
   verifyFormat("var regex =\n"
                "    /\"/;",
                getGoogleJSStyleWithColumns(15));
+  verifyFormat("var regex =  //\n"
+               "    /a/;");
 }
 
 TEST_F(FormatTestJS, RegexLiteralModifiers) {