[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 16:19:00 PST 2022


owenpan updated this revision to Diff 411256.
owenpan added a comment.

- Simplified the code that backtracks to the token which the right braces are to be inserted after.
- Added a missing newline in the new testcase.


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

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)\n"
+               "  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
@@ -2336,10 +2336,9 @@
     assert(!Line->InPPDirective);
     Tok = nullptr;
     for (const auto &L : llvm::reverse(*CurrentLines)) {
-      if (!L.InPPDirective) {
-        Tok = getLastNonComment(L);
-        if (Tok)
-          break;
+      if (!L.InPPDirective && getLastNonComment(L)) {
+        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.411256.patch
Type: text/x-patch
Size: 1606 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220225/77f76c73/attachment-0001.bin>


More information about the cfe-commits mailing list