From: Daniel Jasper Date: Thu, 2 Jul 2015 14:14:04 +0000 (+0000) Subject: clang-format: [JS] Skip comments when applying the regex-literal heuristic X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=33f8df6cdd6bf7ecd1a40230faba2c699b4cf4fd;p=clang clang-format: [JS] Skip comments when applying the regex-literal heuristic git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@241264 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 6e2012eb0f..f97e1fbc6d 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -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) { diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index 40a2a1ffe6..ef5901e8d6 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -636,6 +636,8 @@ TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) { verifyFormat("var regex =\n" " /\"/;", getGoogleJSStyleWithColumns(15)); + verifyFormat("var regex = //\n" + " /a/;"); } TEST_F(FormatTestJS, RegexLiteralModifiers) {