[clang] f00f2b3 - [clang-format] Fix a bug in removing braces for the LLVM style

via cfe-commits cfe-commits at lists.llvm.org
Sun Aug 28 14:26:28 PDT 2022


Author: owenca
Date: 2022-08-28T14:22:31-07:00
New Revision: f00f2b3e8d406c503902117669297b99c40207b6

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

LOG: [clang-format] Fix a bug in removing braces for the LLVM style

When an l_brace is wrapped and the line above it ends with a
comment, the annotator adds ColumnLimit to the TotalLength of the
l_brace, so the actual column position of the l_brace must be
adjusted accordingly.

Fixes #57376.

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 7518fd36d2534..03c6565598a7a 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -815,6 +815,10 @@ bool UnwrappedLineParser::mightFitOnOneLine(
   auto Length = LastToken->TotalLength;
   if (OpeningBrace) {
     assert(OpeningBrace != Tokens.front().Tok);
+    if (auto Prev = OpeningBrace->Previous;
+        Prev && Prev->TotalLength + ColumnLimit == OpeningBrace->TotalLength) {
+      Length -= ColumnLimit;
+    }
     Length -= OpeningBrace->TokenText.size() + 1;
   }
 

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 6abe155c388da..257be01c3dae2 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -25815,6 +25815,14 @@ TEST_F(FormatTest, RemoveBraces) {
                "}",
                Style);
 
+  verifyFormat("if (a) // comment\n"
+               "  b = 1;",
+               "if (a) // comment\n"
+               "{\n"
+               "  b = 1;\n"
+               "}",
+               Style);
+
   verifyFormat("if (a) {\n"
                "Label:\n"
                "}",


        


More information about the cfe-commits mailing list