owenpan created this revision.
owenpan added reviewers: curdeius, HazardyKnusperkeks, MyDeveloperDay.
Herald added a project: All.
owenpan requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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


Repository:
  rG LLVM Github Monorepo

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 =


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 =
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to