[PATCH] D125451: [clang-format] Handle comments below r_brace in RemoveBracesLLVM

Owen Pan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu May 12 16:51:40 PDT 2022


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG6cd9633c1da5: [clang-format] Handle comments below r_brace in RemoveBracesLLVM (authored by owenpan).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D125451

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


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -25348,6 +25348,20 @@
                "}",
                Style);
 
+  verifyFormat("if (a)\n"
+               "  foo();\n"
+               "// comment\n"
+               "else\n"
+               "  bar();",
+               "if (a) {\n"
+               "  foo();\n"
+               "}\n"
+               "// comment\n"
+               "else {\n"
+               "  bar();\n"
+               "}",
+               Style);
+
   verifyFormat("if (a) {\n"
                "Label:\n"
                "}",
Index: clang/lib/Format/Format.cpp
===================================================================
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -1898,8 +1898,9 @@
         assert(Token->isOneOf(tok::l_brace, tok::r_brace));
         assert(Token->Next || Token == Line->Last);
         const auto Start =
-            Token == Line->Last || (Token->Next->is(tok::kw_else) &&
-                                    Token->Next->NewlinesBefore > 0)
+            Token == Line->Last ||
+                    (Token->Next->isOneOf(tok::kw_else, tok::comment) &&
+                     Token->Next->NewlinesBefore > 0)
                 ? Token->WhitespaceRange.getBegin()
                 : Token->Tok.getLocation();
         const auto Range =


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125451.429103.patch
Type: text/x-patch
Size: 1462 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220512/9a40e52b/attachment.bin>


More information about the cfe-commits mailing list