Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-format] Fix a bug in aligning comments above PPDirective #72791

Merged
merged 1 commit into from
Nov 19, 2023

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Nov 19, 2023

Fixed #72785.

@llvmbot
Copy link
Member

llvmbot commented Nov 19, 2023

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

Fixed #72785.


Full diff: https://github.com/llvm/llvm-project/pull/72791.diff

2 Files Affected:

  • (modified) clang/lib/Format/TokenAnnotator.cpp (+2-2)
  • (modified) clang/unittests/Format/FormatTestComments.cpp (+11)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 1d376cd8b5794dd..d1b3045ab63a87a 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3123,8 +3123,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 967ffa32db79c75..9770d5090703c77 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) {

@owenca owenca merged commit 5860d24 into llvm:main Nov 19, 2023
4 checks passed
@owenca owenca deleted the 72785 branch November 19, 2023 22:59
sr-tream pushed a commit to sr-tream/llvm-project that referenced this pull request Nov 20, 2023
tru pushed a commit that referenced this pull request Nov 20, 2023
zahiraam pushed a commit to zahiraam/llvm-project that referenced this pull request Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang-format] Regression in clang-format version 17
3 participants