Skip to content

Commit 5860d24

Browse files
authored
[clang-format] Fix a bug in aligning comments above PPDirective (#72791)
Fixed #72785.
1 parent e16a834 commit 5860d24

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3123,8 +3123,8 @@ void TokenAnnotator::setCommentLineLevels(
31233123

31243124
// If the comment is currently aligned with the line immediately following
31253125
// it, that's probably intentional and we should keep it.
3126-
if (NextNonCommentLine && !NextNonCommentLine->First->Finalized &&
3127-
Line->isComment() && NextNonCommentLine->First->NewlinesBefore <= 1 &&
3126+
if (NextNonCommentLine && NextNonCommentLine->First->NewlinesBefore < 2 &&
3127+
Line->isComment() && !isClangFormatOff(Line->First->TokenText) &&
31283128
NextNonCommentLine->First->OriginalColumn ==
31293129
Line->First->OriginalColumn) {
31303130
const bool PPDirectiveOrImportStmt =

clang/unittests/Format/FormatTestComments.cpp

Lines changed: 11 additions & 0 deletions
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)