r302658 - clang-format: refine calculating brace types.

Martin Probst via cfe-commits cfe-commits at lists.llvm.org
Wed May 10 06:53:29 PDT 2017


Author: mprobst
Date: Wed May 10 08:53:29 2017
New Revision: 302658

URL: http://llvm.org/viewvc/llvm-project?rev=302658&view=rev
Log:
clang-format: refine calculating brace types.

Summary:
For C++ code, opening parenthesis following a } indicate a braced init. For JavaScript and other languages, this is an invalid syntactical construct, unless the closing parenthesis belongs to a function - in which situation its a BK_Block.

This fixes indenting IIFEs following top level functions:

    function foo() {}
    (function() { codeHere(); }());

clang-format used to collapse these lines together.

Subscribers: klimek

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

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=302658&r1=302657&r2=302658&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Wed May 10 08:53:29 2017
@@ -368,9 +368,10 @@ void UnwrappedLineParser::calculateBrace
               (Style.Language == FormatStyle::LK_JavaScript &&
                NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
                                 Keywords.kw_as)) ||
+              (Style.isCpp() && NextTok->is(tok::l_paren)) ||
               NextTok->isOneOf(tok::comma, tok::period, tok::colon,
                                tok::r_paren, tok::r_square, tok::l_brace,
-                               tok::l_square, tok::l_paren, tok::ellipsis) ||
+                               tok::l_square, tok::ellipsis) ||
               (NextTok->is(tok::identifier) &&
                !PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)) ||
               (NextTok->is(tok::semi) &&

Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=302658&r1=302657&r2=302658&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Wed May 10 08:53:29 2017
@@ -470,6 +470,16 @@ TEST_F(FormatTestJS, FormatsFreestanding
                "  inner2(a, b);\n"
                "}");
   verifyFormat("function f() {}");
+  verifyFormat("function aFunction() {}\n"
+               "(function f() {\n"
+               "  var x = 1;\n"
+               "}());\n");
+  // Known issue: this should wrap after {}, but calculateBraceTypes
+  // misclassifies the first braces as a BK_BracedInit.
+  verifyFormat("function aFunction(){} {\n"
+               "  let x = 1;\n"
+               "  console.log(x);\n"
+               "}\n");
 }
 
 TEST_F(FormatTestJS, GeneratorFunctions) {




More information about the cfe-commits mailing list