r281856 - clang-format: [JS] ASI insertion after boolean literals.

Martin Probst via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 18 10:21:53 PDT 2016


Author: mprobst
Date: Sun Sep 18 12:21:52 2016
New Revision: 281856

URL: http://llvm.org/viewvc/llvm-project?rev=281856&view=rev
Log:
clang-format: [JS] ASI insertion after boolean literals.

Summary:
Before when a semicolon was missing after a boolean literal:
    a = true
    return 1;

clang-format would parse this as one line and format as:
    a = true return 1;

It turns out that C++ does not consider `true` and `false` to be literals, we
have to check for that explicitly.

Reviewers: djasper

Subscribers: klimek, cfe-commits

Differential Revision: https://reviews.llvm.org/D24574

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

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=281856&r1=281855&r2=281856&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Sun Sep 18 12:21:52 2016
@@ -681,7 +681,9 @@ static bool mustBeJSIdent(const Addition
 
 static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords,
                                  const FormatToken *FormatTok) {
-  return FormatTok->Tok.isLiteral() || mustBeJSIdent(Keywords, FormatTok);
+  return FormatTok->Tok.isLiteral() ||
+         FormatTok->isOneOf(tok::kw_true, tok::kw_false) ||
+         mustBeJSIdent(Keywords, FormatTok);
 }
 
 // isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement

Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=281856&r1=281855&r2=281856&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Sun Sep 18 12:21:52 2016
@@ -774,6 +774,18 @@ TEST_F(FormatTestJS, AutomaticSemicolonI
                                       "String");
   verifyFormat("function f(@Foo bar) {}", "function f(@Foo\n"
                                           "  bar) {}");
+  verifyFormat("a = true\n"
+               "return 1",
+               "a = true\n"
+               "  return   1");
+  verifyFormat("a = 's'\n"
+               "return 1",
+               "a = 's'\n"
+               "  return   1");
+  verifyFormat("a = null\n"
+               "return 1",
+               "a = null\n"
+               "  return   1");
 }
 
 TEST_F(FormatTestJS, ClosureStyleCasts) {




More information about the cfe-commits mailing list