[llvm-branch-commits] [clang] f8575ff - [clang-format] Fix a bug in aligning comments above PPDirective (#72791)

Tobias Hieta via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Nov 20 04:18:38 PST 2023


Author: Owen Pan
Date: 2023-11-20T13:18:01+01:00
New Revision: f8575ff46f89e88c3e3a7f560bb2da77206f83b7

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

LOG: [clang-format] Fix a bug in aligning comments above PPDirective (#72791)

Fixed #72785.

(cherry picked from commit 5860d248a780aaef860db3d54184b49fc758c3c1)

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 4a1fc08455e5e3b..c52fe12e7ffde78 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3065,8 +3065,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 && !NextNonCommentLine->First->Finalized &&
-        Line->isComment() && NextNonCommentLine->First->NewlinesBefore <= 1 &&
+    if (NextNonCommentLine && NextNonCommentLine->First->NewlinesBefore < 2 &&
+        Line->isComment() && !isClangFormatOff(Line->First->TokenText) &&
         NextNonCommentLine->First->OriginalColumn ==
             Line->First->OriginalColumn) {
       const bool PPDirectiveOrImportStmt =

diff  --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp
index 2406fd64624df75..2b59b611c3e4fa0 100644
--- a/clang/unittests/Format/FormatTestComments.cpp
+++ b/clang/unittests/Format/FormatTestComments.cpp
@@ -1069,6 +1069,17 @@ TEST_F(FormatTestComments, KeepsLevelOfCommentBeforePPDirective) {
                        "  // clang-format on\n"
                        "}");
   verifyNoChange(Code);
+
+  auto Style = getLLVMStyle();
+  Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
+  verifyFormat("#ifdef FOO\n"
+               "  // Foo\n"
+               "  #define Foo foo\n"
+               "#else\n"
+               "  // Bar\n"
+               "  #define Bar bar\n"
+               "#endif",
+               Style);
 }
 
 TEST_F(FormatTestComments, SplitsLongLinesInComments) {


        


More information about the llvm-branch-commits mailing list