[clang] 6cd9633 - [clang-format] Handle comments below r_brace in RemoveBracesLLVM

via cfe-commits cfe-commits at lists.llvm.org
Thu May 12 16:51:31 PDT 2022


Author: owenca
Date: 2022-05-12T16:51:18-07:00
New Revision: 6cd9633c1da5d2867306217af59bcc4b589bab02

URL: https://github.com/llvm/llvm-project/commit/6cd9633c1da5d2867306217af59bcc4b589bab02
DIFF: https://github.com/llvm/llvm-project/commit/6cd9633c1da5d2867306217af59bcc4b589bab02.diff

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

If a closing brace is followed by a non-trailing comment, the
newline before the closing brace must also be removed.

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index afa87a8bafe31..10945f455120e 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1898,8 +1898,9 @@ class BracesRemover : public TokenAnalyzer {
         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 =

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 2a764b68b9129..8ac59dc4a5131 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -25348,6 +25348,20 @@ TEST_F(FormatTest, RemoveBraces) {
                "}",
                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"
                "}",


        


More information about the cfe-commits mailing list