Skip to content

Commit f8575ff

Browse files
owencatru
authored andcommitted
[clang-format] Fix a bug in aligning comments above PPDirective (#72791)
Fixed #72785. (cherry picked from commit 5860d24)
1 parent a71237b commit f8575ff

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

Diff for: clang/lib/Format/TokenAnnotator.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -3065,8 +3065,8 @@ void TokenAnnotator::setCommentLineLevels(
30653065

30663066
// If the comment is currently aligned with the line immediately following
30673067
// it, that's probably intentional and we should keep it.
3068-
if (NextNonCommentLine && !NextNonCommentLine->First->Finalized &&
3069-
Line->isComment() && NextNonCommentLine->First->NewlinesBefore <= 1 &&
3068+
if (NextNonCommentLine && NextNonCommentLine->First->NewlinesBefore < 2 &&
3069+
Line->isComment() && !isClangFormatOff(Line->First->TokenText) &&
30703070
NextNonCommentLine->First->OriginalColumn ==
30713071
Line->First->OriginalColumn) {
30723072
const bool PPDirectiveOrImportStmt =

Diff for: clang/unittests/Format/FormatTestComments.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,17 @@ TEST_F(FormatTestComments, KeepsLevelOfCommentBeforePPDirective) {
10691069
" // clang-format on\n"
10701070
"}");
10711071
verifyNoChange(Code);
1072+
1073+
auto Style = getLLVMStyle();
1074+
Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
1075+
verifyFormat("#ifdef FOO\n"
1076+
" // Foo\n"
1077+
" #define Foo foo\n"
1078+
"#else\n"
1079+
" // Bar\n"
1080+
" #define Bar bar\n"
1081+
"#endif",
1082+
Style);
10721083
}
10731084

10741085
TEST_F(FormatTestComments, SplitsLongLinesInComments) {

0 commit comments

Comments
 (0)