[clang-format] Update removed brace's next token's WhitespaceRange
authorowenca <owenpiano@gmail.com>
Sun, 18 Sep 2022 20:32:05 +0000 (13:32 -0700)
committerowenca <owenpiano@gmail.com>
Mon, 19 Sep 2022 22:02:08 +0000 (15:02 -0700)
Fixes #57803.

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

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

index 58d5fc1..025e1aa 100644 (file)
@@ -1920,10 +1920,13 @@ private:
         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, "")));
index 828948e..8999e59 100644 (file)
@@ -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"