[clang] cddb9ce - [clang-format] Don't over-indent comment below unbraced body (#95354)

via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 14 20:49:16 PDT 2024


Author: Owen Pan
Date: 2024-06-14T20:49:13-07:00
New Revision: cddb9ce04e68d876de895d39d60b6af34d569012

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

LOG: [clang-format] Don't over-indent comment below unbraced body (#95354)

Fixes #45002.

Added: 
    

Modified: 
    clang/lib/Format/UnwrappedLineParser.cpp
    clang/lib/Format/UnwrappedLineParser.h
    clang/unittests/Format/FormatTestComments.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 08387d2e08ee0..899e68eadf70e 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -112,6 +112,7 @@ class ScopedLineState {
     Parser.Line->PPLevel = PreBlockLine->PPLevel;
     Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
     Parser.Line->InMacroBody = PreBlockLine->InMacroBody;
+    Parser.Line->UnbracedBodyLevel = PreBlockLine->UnbracedBodyLevel;
   }
 
   ~ScopedLineState() {
@@ -2708,7 +2709,9 @@ void UnwrappedLineParser::parseUnbracedBody(bool CheckEOF) {
 
   addUnwrappedLine();
   ++Line->Level;
+  ++Line->UnbracedBodyLevel;
   parseStructuralElement();
+  --Line->UnbracedBodyLevel;
 
   if (Tok) {
     assert(!Line->InPPDirective);
@@ -4836,6 +4839,8 @@ void UnwrappedLineParser::readToken(int LevelDifference) {
           PPBranchLevel > 0) {
         Line->Level += PPBranchLevel;
       }
+      assert(Line->Level >= Line->UnbracedBodyLevel);
+      Line->Level -= Line->UnbracedBodyLevel;
       flushComments(isOnNewLine(*FormatTok));
       parsePPDirective();
       PreviousWasComment = FormatTok->is(tok::comment);

diff  --git a/clang/lib/Format/UnwrappedLineParser.h b/clang/lib/Format/UnwrappedLineParser.h
index d7963a4211bb9..d5eeb3d57149c 100644
--- a/clang/lib/Format/UnwrappedLineParser.h
+++ b/clang/lib/Format/UnwrappedLineParser.h
@@ -49,6 +49,9 @@ struct UnwrappedLine {
   /// Whether it is part of a macro body.
   bool InMacroBody = false;
 
+  /// Nesting level of unbraced body of a control statement.
+  unsigned UnbracedBodyLevel = 0;
+
   bool MustBeDeclaration = false;
 
   /// Whether the parser has seen \c decltype(auto) in this line.

diff  --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp
index d2baace6a7d80..3e75707a9faec 100644
--- a/clang/unittests/Format/FormatTestComments.cpp
+++ b/clang/unittests/Format/FormatTestComments.cpp
@@ -1087,6 +1087,36 @@ TEST_F(FormatTestComments, KeepsLevelOfCommentBeforePPDirective) {
                Style);
 }
 
+TEST_F(FormatTestComments, CommentsBetweenUnbracedBodyAndPPDirective) {
+  verifyFormat("{\n"
+               "  if (a)\n"
+               "    f(); // comment\n"
+               "#define A\n"
+               "}");
+
+  verifyFormat("{\n"
+               "  while (a)\n"
+               "    f();\n"
+               "// comment\n"
+               "#define A\n"
+               "}");
+
+  verifyNoChange("{\n"
+                 "  if (a)\n"
+                 "    f();\n"
+                 "  // comment\n"
+                 "#define A\n"
+                 "}");
+
+  verifyNoChange("{\n"
+                 "  while (a)\n"
+                 "    if (b)\n"
+                 "      f();\n"
+                 "  // comment\n"
+                 "#define A\n"
+                 "}");
+}
+
 TEST_F(FormatTestComments, SplitsLongLinesInComments) {
   // FIXME: Do we need to fix up the "  */" at the end?
   // It doesn't look like any of our current logic triggers this.


        


More information about the cfe-commits mailing list