[clang] [clang-format] Fix a bug in aligning comments above PPDirective (PR #72791)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 19 03:45:14 PST 2023
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/72791
Fixed #72785.
>From 0aadd3c1e458f1137c4d45bc14dfae7db9b0446f Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Sun, 19 Nov 2023 03:41:58 -0800
Subject: [PATCH] [clang-format] Fix a bug in aligning comments above
PPDirective
Fixed #72785.
---
clang/lib/Format/TokenAnnotator.cpp | 4 ++--
clang/unittests/Format/FormatTestComments.cpp | 11 +++++++++++
2 files changed, 13 insertions(+), 2 deletions(-)
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) {
More information about the cfe-commits
mailing list