[clang] 293e4da - [clang-format] Correctly indent comment above finalized PPDirective

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 14 13:58:48 PDT 2023


Author: Owen Pan
Date: 2023-04-14T13:58:38-07:00
New Revision: 293e4da32b1d823e63b2614e626bcd22649a8a15

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

LOG: [clang-format] Correctly indent comment above finalized PPDirective

Fixes #62107.

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

Added: 
    

Modified: 
    clang/lib/Format/TokenAnnotator.cpp
    clang/unittests/Format/FormatTestComments.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 526eb24135261..9060bf76a7f52 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3025,8 +3025,8 @@ void TokenAnnotator::setCommentLineLevels(
 
     // If the comment is currently aligned with the line immediately following
     // it, that's probably intentional and we should keep it.
-    if (NextNonCommentLine && Line->isComment() &&
-        NextNonCommentLine->First->NewlinesBefore <= 1 &&
+    if (NextNonCommentLine && !NextNonCommentLine->First->Finalized &&
+        Line->isComment() && NextNonCommentLine->First->NewlinesBefore <= 1 &&
         NextNonCommentLine->First->OriginalColumn ==
             Line->First->OriginalColumn) {
       const bool PPDirectiveOrImportStmt =

diff  --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp
index 03d093bd58913..60e045059be86 100644
--- a/clang/unittests/Format/FormatTestComments.cpp
+++ b/clang/unittests/Format/FormatTestComments.cpp
@@ -1059,6 +1059,13 @@ TEST_F(FormatTestComments, KeepsLevelOfCommentBeforePPDirective) {
                    "#endif\n"
                    "  }\n"
                    "}"));
+
+  const StringRef Code("void func() {\n"
+                       "  // clang-format off\n"
+                       "  #define KV(value) #value, value\n"
+                       "  // clang-format on\n"
+                       "}");
+  EXPECT_EQ(Code, format(Code));
 }
 
 TEST_F(FormatTestComments, SplitsLongLinesInComments) {


        


More information about the cfe-commits mailing list