[PATCH] D132905: [clang-format] Fix a bug in inserting braces at trailing comments

Owen Pan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 29 18:33:20 PDT 2022


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

If the style wraps control statement braces, the opening braces should be inserted after the trailing comments if present.

Fixes https://github.com/llvm/llvm-project/issues/57419.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D132905

Files:
  clang/lib/Format/Format.cpp
  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
@@ -25201,6 +25201,13 @@
                "  f();",
                Style);
 
+  verifyFormat("if (a) { //\n"
+               "  b = 1;\n"
+               "}",
+               "if (a) //\n"
+               "  b = 1;",
+               Style);
+
   verifyFormat("if (a) { // comment\n"
                "  // comment\n"
                "  f();\n"
@@ -25266,6 +25273,19 @@
                "if (a + b > c)\n"
                "  f();",
                Style);
+
+  Style.BreakBeforeBraces = FormatStyle::BS_Custom;
+  Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
+  // TODO: Delete the following line after #57421 is fixed.
+  Style.BraceWrapping.AfterFunction = true;
+
+  verifyFormat("if (a) //\n"
+               "{\n"
+               "  b = 1;\n"
+               "}",
+               "if (a) //\n"
+               "  b = 1;",
+               Style);
 }
 
 TEST_F(FormatTest, RemoveBraces) {
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -2606,7 +2606,10 @@
 
   if (Style.InsertBraces && !Line->InPPDirective && !Line->Tokens.empty() &&
       PreprocessorDirectives.empty()) {
-    Tok = getLastNonComment(*Line);
+    assert(!Line->Tokens.empty());
+    Tok = Style.BraceWrapping.AfterControlStatement == FormatStyle::BWACS_Never
+              ? getLastNonComment(*Line)
+              : Line->Tokens.back().Tok;
     assert(Tok);
     if (Tok->BraceCount < 0) {
       assert(Tok->BraceCount == -1);
Index: clang/lib/Format/Format.cpp
===================================================================
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -1869,7 +1869,7 @@
         std::string Brace;
         if (Token->BraceCount < 0) {
           assert(Token->BraceCount == -1);
-          Brace = '{';
+          Brace = "\n{";
         } else {
           Brace = '\n' + std::string(Token->BraceCount, '}');
         }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132905.456514.patch
Type: text/x-patch
Size: 2229 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220830/8dffcdae/attachment.bin>


More information about the cfe-commits mailing list