Skip to content

Commit

Permalink
[clang-format] Fix a bug in aligning comments above PPDirective (llvm…
Browse files Browse the repository at this point in the history
  • Loading branch information
owenca authored and zahiraam committed Nov 20, 2023
1 parent 96ceb62 commit 3c071a4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
11 changes: 11 additions & 0 deletions clang/unittests/Format/FormatTestComments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 3c071a4

Please sign in to comment.