[PATCH] D114583: [clang-format] Adjust braced list detection

S. B. Tam via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 25 09:23:25 PST 2021


cpplearner updated this revision to Diff 389822.
cpplearner added a comment.

Fixed tests.

Since in JavaScript, the thing after `extends` must be an expression, and the thing after `implements` must be an object type, I decided to parse both as braced lists without going through `tryToParseBracedList`.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D114583/new/

https://reviews.llvm.org/D114583

Files:
  clang/lib/Format/UnwrappedLineParser.cpp
  clang/unittests/Format/FormatTest.cpp


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -11731,6 +11731,15 @@
                "  f(v);\n"
                "}");
 
+  verifyFormat("void foo() {\n"
+               "  { // asdf\n"
+               "    { int a; }\n"
+               "  }\n"
+               "  {\n"
+               "    { int b; }\n"
+               "  }\n"
+               "}");
+
   // Long lists should be formatted in columns even if they are nested.
   verifyFormat(
       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -513,17 +513,14 @@
           // BlockKind later if we parse a braced list (where all blocks
           // inside are by default braced lists), or when we explicitly detect
           // blocks (for example while parsing lambdas).
-          // FIXME: Some of these do not apply to JS, e.g. "} {" can never be a
-          // braced list in JS.
           ProbablyBracedList =
               (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::ellipsis) ||
-              (NextTok->is(tok::identifier) &&
+                               tok::r_paren, tok::r_square, tok::ellipsis) ||
+              (NextTok->isOneOf(tok::l_brace, tok::identifier) &&
                !PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)) ||
               (NextTok->is(tok::semi) &&
                (!ExpectClassBody || LBraceStack.size() != 1)) ||
@@ -2761,7 +2758,7 @@
       //     class Foo implements {bar: number} { }
       nextToken();
       if (FormatTok->is(tok::l_brace)) {
-        tryToParseBracedList();
+        parseBracedList();
         continue;
       }
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114583.389822.patch
Type: text/x-patch
Size: 2311 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211125/4027135b/attachment.bin>


More information about the cfe-commits mailing list