Skip to content

Commit 82f1f5e

Browse files
authored
[clang-format] Correctly identify include guards (#137112)
Fix #136814
1 parent 4652b69 commit 82f1f5e

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,7 @@ void UnwrappedLineParser::parsePPDefine() {
11561156
return;
11571157
}
11581158

1159+
bool MaybeIncludeGuard = false;
11591160
if (IncludeGuard == IG_IfNdefed &&
11601161
IncludeGuardToken->TokenText == FormatTok->TokenText) {
11611162
IncludeGuard = IG_Defined;
@@ -1166,6 +1167,7 @@ void UnwrappedLineParser::parsePPDefine() {
11661167
break;
11671168
}
11681169
}
1170+
MaybeIncludeGuard = IncludeGuard == IG_Defined;
11691171
}
11701172

11711173
// In the context of a define, even keywords should be treated as normal
@@ -1176,6 +1178,11 @@ void UnwrappedLineParser::parsePPDefine() {
11761178
FormatTok->Tok.setKind(tok::identifier);
11771179
FormatTok->Tok.setIdentifierInfo(Keywords.kw_internal_ident_after_define);
11781180
nextToken();
1181+
1182+
// IncludeGuard can't have a non-empty macro definition.
1183+
if (MaybeIncludeGuard && !eof())
1184+
IncludeGuard = IG_Rejected;
1185+
11791186
if (FormatTok->Tok.getKind() == tok::l_paren &&
11801187
!FormatTok->hasWhitespaceBefore()) {
11811188
parseParens();

clang/unittests/Format/FormatTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6492,6 +6492,18 @@ TEST_F(FormatTest, IndentPreprocessorDirectives) {
64926492
"#if 1\n"
64936493
"#endif",
64946494
Style);
6495+
6496+
verifyFormat("#ifndef ABCDE\n"
6497+
" #define ABCDE 0\n"
6498+
"#endif\n"
6499+
"\n"
6500+
"#define FGHIJK",
6501+
"#ifndef ABCDE\n"
6502+
"#define ABCDE 0\n"
6503+
"#endif\n"
6504+
"\n"
6505+
"#define FGHIJK",
6506+
Style);
64956507
}
64966508

64976509
TEST_F(FormatTest, FormatAlignInsidePreprocessorElseBlock) {

0 commit comments

Comments
 (0)