Skip to content

Commit 4121445

Browse files
committed
[clang-format] Fix BeforeHash indent of comments above PPDirective
Fixes #56326. Differential Revision: https://reviews.llvm.org/D132097
1 parent 4a51b0c commit 4121445

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2696,15 +2696,18 @@ void TokenAnnotator::setCommentLineLevels(
26962696
NextNonCommentLine->First->NewlinesBefore <= 1 &&
26972697
NextNonCommentLine->First->OriginalColumn ==
26982698
Line->First->OriginalColumn) {
2699+
const bool PPDirectiveOrImportStmt =
2700+
NextNonCommentLine->Type == LT_PreprocessorDirective ||
2701+
NextNonCommentLine->Type == LT_ImportStatement;
2702+
if (PPDirectiveOrImportStmt)
2703+
Line->Type = LT_CommentAbovePPDirective;
26992704
// Align comments for preprocessor lines with the # in column 0 if
27002705
// preprocessor lines are not indented. Otherwise, align with the next
27012706
// line.
2702-
Line->Level =
2703-
(Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash &&
2704-
(NextNonCommentLine->Type == LT_PreprocessorDirective ||
2705-
NextNonCommentLine->Type == LT_ImportStatement))
2706-
? 0
2707-
: NextNonCommentLine->Level;
2707+
Line->Level = Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash &&
2708+
PPDirectiveOrImportStmt
2709+
? 0
2710+
: NextNonCommentLine->Level;
27082711
} else {
27092712
NextNonCommentLine = Line->First->isNot(tok::r_brace) ? Line : nullptr;
27102713
}

clang/lib/Format/TokenAnnotator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ enum LineType {
3131
LT_PreprocessorDirective,
3232
LT_VirtualFunctionDecl,
3333
LT_ArrayOfStructInitializer,
34+
LT_CommentAbovePPDirective,
3435
};
3536

3637
class AnnotatedLine {

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ class LevelIndentTracker {
6060
// Update the indent level cache size so that we can rely on it
6161
// having the right size in adjustToUnmodifiedline.
6262
skipLine(Line, /*UnknownIndent=*/true);
63-
if (Line.InPPDirective) {
63+
if (Line.InPPDirective ||
64+
(Style.IndentPPDirectives == FormatStyle::PPDIS_BeforeHash &&
65+
Line.Type == LT_CommentAbovePPDirective)) {
6466
unsigned IndentWidth =
6567
(Style.PPIndentWidth >= 0) ? Style.PPIndentWidth : Style.IndentWidth;
6668
Indent = Line.Level * IndentWidth + AdditionalIndent;

clang/unittests/Format/FormatTest.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4954,6 +4954,25 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
49544954
" int y = 0;\n"
49554955
"}\n",
49564956
style);
4957+
verifyFormat("#if 1\n"
4958+
" // some comments\n"
4959+
" // another\n"
4960+
" #define foo 1\n"
4961+
"// not a define comment\n"
4962+
"void bar() {\n"
4963+
" // comment\n"
4964+
" int y = 0;\n"
4965+
"}",
4966+
"#if 1\n"
4967+
"// some comments\n"
4968+
"// another\n"
4969+
"#define foo 1\n"
4970+
"// not a define comment\n"
4971+
"void bar() {\n"
4972+
" // comment\n"
4973+
" int y = 0;\n"
4974+
"}",
4975+
style);
49574976
}
49584977

49594978
TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {

0 commit comments

Comments
 (0)