[clang] 6ab7307 - [clang-format] Fix invalid-code-generation by RemoveBracesLLVM

via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 14 15:20:05 PDT 2022


Author: owenca
Date: 2022-07-14T15:19:57-07:00
New Revision: 6ab7307177c336d7ca5910fc434c012f35245fd1

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

LOG: [clang-format] Fix invalid-code-generation by RemoveBracesLLVM

When removing an r_brace that is the first token of an annotated line, if the
line above ends with a line comment, clang-format generates invalid code by
merging the tokens after the r_brace into the line comment.

Fixes #56488.

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

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 d13907faca43f..2659fa2af1a7f 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1895,26 +1895,31 @@ class BracesRemover : public TokenAnalyzer {
   void removeBraces(SmallVectorImpl<AnnotatedLine *> &Lines,
                     tooling::Replacements &Result) {
     const auto &SourceMgr = Env.getSourceManager();
+    bool EndsWithComment = false;
     for (AnnotatedLine *Line : Lines) {
       removeBraces(Line->Children, Result);
-      if (!Line->Affected)
-        continue;
-      for (FormatToken *Token = Line->First; Token && !Token->Finalized;
-           Token = Token->Next) {
-        if (!Token->Optional)
-          continue;
-        assert(Token->isOneOf(tok::l_brace, tok::r_brace));
-        assert(Token->Next || Token == Line->Last);
-        const auto Start =
-            Token == Line->Last ||
-                    (Token->Next->isOneOf(tok::kw_else, tok::comment) &&
-                     Token->Next->NewlinesBefore > 0)
-                ? Token->WhitespaceRange.getBegin()
-                : Token->Tok.getLocation();
-        const auto Range =
-            CharSourceRange::getCharRange(Start, Token->Tok.getEndLoc());
-        cantFail(Result.add(tooling::Replacement(SourceMgr, Range, "")));
+      if (Line->Affected) {
+        for (FormatToken *Token = Line->First; Token && !Token->Finalized;
+             Token = Token->Next) {
+          if (!Token->Optional)
+            continue;
+          assert(Token->isOneOf(tok::l_brace, tok::r_brace));
+          assert(Token->Previous || Token == Line->First);
+          const FormatToken *Next = Token->Next;
+          assert(Next || Token == Line->Last);
+          const auto Start =
+              (!Token->Previous && EndsWithComment) ||
+                      (Next && !(Next->isOneOf(tok::kw_else, tok::comment) &&
+                                 Next->NewlinesBefore > 0))
+                  ? Token->Tok.getLocation()
+                  : Token->WhitespaceRange.getBegin();
+          const auto Range =
+              CharSourceRange::getCharRange(Start, Token->Tok.getEndLoc());
+          cantFail(Result.add(tooling::Replacement(SourceMgr, Range, "")));
+        }
       }
+      assert(Line->Last);
+      EndsWithComment = Line->Last->is(tok::comment);
     }
   }
 };

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index e506d0dbf3a5e..ba07584d661ae 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -25741,6 +25741,17 @@ TEST_F(FormatTest, RemoveBraces) {
                "}",
                Style);
 
+  verifyFormat("if (a) {\n"
+               "  if (b)\n"
+               "    c = 1; // comment\n"
+               "}",
+               "if (a) {\n"
+               "  if (b) {\n"
+               "    c = 1; // comment\n"
+               "  }\n"
+               "}",
+               Style);
+
   verifyFormat("if (a) {\n"
                "Label:\n"
                "}",


        


More information about the cfe-commits mailing list