[PATCH] D120503: [clang-format] Handle trailing comment for InsertBraces

Owen Pan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 24 10:29:33 PST 2022


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

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120503

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
@@ -24481,6 +24481,13 @@
                "  f();",
                Style);
 
+  verifyFormat("if (a) {\n"
+               "  f(); // comment\n"
+               "}",
+               "if (a)"
+               "  f(); // comment",
+               Style);
+
   verifyFormat("if (a) {\n"
                "  f();\n"
                "}\n"
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -2338,8 +2338,10 @@
     for (const auto &L : llvm::reverse(*CurrentLines)) {
       if (!L.InPPDirective) {
         Tok = getLastNonComment(L);
-        if (Tok)
+        if (Tok) {
+          Tok = L.Tokens.back().Tok;
           break;
+        }
       }
     }
     assert(Tok);
Index: clang/lib/Format/Format.cpp
===================================================================
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -1855,7 +1855,7 @@
           assert(Token->BraceCount == -1);
           Brace = '{';
         } else {
-          Brace = std::string(Token->BraceCount, '}');
+          Brace = '\n' + std::string(Token->BraceCount, '}');
         }
         Token->BraceCount = 0;
         const auto Start = Token->Tok.getEndLoc();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D120503.411175.patch
Type: text/x-patch
Size: 1511 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220224/d5586f4c/attachment.bin>


More information about the cfe-commits mailing list