Skip to content

Commit 0b1d353

Browse files
authored
[clang-format] Fix a bug in SkipMacroDefinitionBody (llvm#154787)
Fixes llvm#154683
1 parent 52ed03d commit 0b1d353

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,34 +1182,43 @@ void UnwrappedLineParser::parsePPDefine() {
11821182
if (MaybeIncludeGuard && !eof())
11831183
IncludeGuard = IG_Rejected;
11841184

1185-
if (FormatTok->Tok.getKind() == tok::l_paren &&
1186-
!FormatTok->hasWhitespaceBefore()) {
1185+
if (FormatTok->is(tok::l_paren) && !FormatTok->hasWhitespaceBefore())
11871186
parseParens();
1188-
}
11891187
if (Style.IndentPPDirectives != FormatStyle::PPDIS_None)
11901188
Line->Level += PPBranchLevel + 1;
11911189
addUnwrappedLine();
11921190
++Line->Level;
11931191

11941192
Line->PPLevel = PPBranchLevel + (IncludeGuard == IG_Defined ? 0 : 1);
11951193
assert((int)Line->PPLevel >= 0);
1194+
1195+
if (eof())
1196+
return;
1197+
11961198
Line->InMacroBody = true;
11971199

1198-
if (Style.SkipMacroDefinitionBody) {
1199-
while (!eof()) {
1200-
FormatTok->Finalized = true;
1201-
FormatTok = Tokens->getNextToken();
1202-
}
1203-
addUnwrappedLine();
1200+
if (!Style.SkipMacroDefinitionBody) {
1201+
// Errors during a preprocessor directive can only affect the layout of the
1202+
// preprocessor directive, and thus we ignore them. An alternative approach
1203+
// would be to use the same approach we use on the file level (no
1204+
// re-indentation if there was a structural error) within the macro
1205+
// definition.
1206+
parseFile();
12041207
return;
12051208
}
12061209

1207-
// Errors during a preprocessor directive can only affect the layout of the
1208-
// preprocessor directive, and thus we ignore them. An alternative approach
1209-
// would be to use the same approach we use on the file level (no
1210-
// re-indentation if there was a structural error) within the macro
1211-
// definition.
1212-
parseFile();
1210+
if (auto *Prev = Tokens->getPreviousToken(); Prev->is(tok::comment) &&
1211+
Prev->NewlinesBefore > 0 &&
1212+
!Prev->HasUnescapedNewline) {
1213+
Prev->Finalized = true;
1214+
}
1215+
1216+
do {
1217+
FormatTok->Finalized = true;
1218+
FormatTok = Tokens->getNextToken();
1219+
} while (!eof());
1220+
1221+
addUnwrappedLine();
12131222
}
12141223

12151224
void UnwrappedLineParser::parsePPPragma() {

clang/unittests/Format/FormatTest.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26022,6 +26022,10 @@ TEST_F(FormatTest, SkipMacroDefinitionBody) {
2602226022
" A a \\\n "
2602326023
" A a",
2602426024
Style);
26025+
verifyNoChange("#define MY_MACRO \\\n"
26026+
" /* comment */ \\\n"
26027+
" 1",
26028+
Style);
2602526029
}
2602626030

2602726031
TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {

0 commit comments

Comments
 (0)