[clang] 7349084 - [clang-format] Update removed brace's next token's WhitespaceRange

via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 19 15:02:27 PDT 2022


Author: owenca
Date: 2022-09-19T15:02:08-07:00
New Revision: 7349084e7afc55fc734922a9facd76316f2977b1

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

LOG: [clang-format] Update removed brace's next token's WhitespaceRange

Fixes #57803.

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 58d5fc1f8c865..025e1aa977247 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1920,10 +1920,13 @@ class BracesRemover : public TokenAnalyzer {
         assert(Next || Token == Line->Last);
         if (!Next && NextLine)
           Next = NextLine->First;
-        const auto Start =
-            Next && Next->NewlinesBefore == 0 && Next->isNot(tok::eof)
-                ? Token->Tok.getLocation()
-                : Token->WhitespaceRange.getBegin();
+        SourceLocation Start;
+        if (Next && Next->NewlinesBefore == 0 && Next->isNot(tok::eof)) {
+          Start = Token->Tok.getLocation();
+          Next->WhitespaceRange = Token->WhitespaceRange;
+        } else {
+          Start = Token->WhitespaceRange.getBegin();
+        }
         const auto Range =
             CharSourceRange::getCharRange(Start, Token->Tok.getEndLoc());
         cantFail(Result.add(tooling::Replacement(SourceMgr, Range, "")));

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 828948e8f5772..8999e590612e3 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -26006,6 +26006,17 @@ TEST_F(FormatTest, RemoveBraces) {
                "}",
                Style);
 
+  verifyFormat("while (0)\n"
+               "  if (a)\n"
+               "    return b;\n"
+               "return a;",
+               "while (0) {\n"
+               "  if (a) {\n"
+               "    return b;\n"
+               "}}\n"
+               "return a;",
+               Style);
+
   Style.ColumnLimit = 65;
   verifyFormat("if (condition) {\n"
                "  ff(Indices,\n"


        


More information about the cfe-commits mailing list