[clang] [clang-format] Correctly identify include guards (PR #137112)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 23 19:50:41 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Owen Pan (owenca)
<details>
<summary>Changes</summary>
Fix 136814
---
Full diff: https://github.com/llvm/llvm-project/pull/137112.diff
2 Files Affected:
- (modified) clang/lib/Format/UnwrappedLineParser.cpp (+7)
- (modified) clang/unittests/Format/FormatTest.cpp (+12)
``````````diff
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index df58e5ef4d6a3..565dcfce4ec18 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1156,6 +1156,7 @@ void UnwrappedLineParser::parsePPDefine() {
return;
}
+ bool MaybeIncludeGuard = false;
if (IncludeGuard == IG_IfNdefed &&
IncludeGuardToken->TokenText == FormatTok->TokenText) {
IncludeGuard = IG_Defined;
@@ -1166,6 +1167,7 @@ void UnwrappedLineParser::parsePPDefine() {
break;
}
}
+ MaybeIncludeGuard = IncludeGuard == IG_Defined;
}
// In the context of a define, even keywords should be treated as normal
@@ -1176,6 +1178,11 @@ void UnwrappedLineParser::parsePPDefine() {
FormatTok->Tok.setKind(tok::identifier);
FormatTok->Tok.setIdentifierInfo(Keywords.kw_internal_ident_after_define);
nextToken();
+
+ // IncludeGuard can't have a non-empty macro definition.
+ if (MaybeIncludeGuard && !eof())
+ IncludeGuard = IG_Rejected;
+
if (FormatTok->Tok.getKind() == tok::l_paren &&
!FormatTok->hasWhitespaceBefore()) {
parseParens();
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index f1b3b7dd8c0c3..dbfeae2a44697 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -6492,6 +6492,18 @@ TEST_F(FormatTest, IndentPreprocessorDirectives) {
"#if 1\n"
"#endif",
Style);
+
+ verifyFormat("#ifndef ABCDE\n"
+ " #define ABCDE 0\n"
+ "#endif\n"
+ "\n"
+ "#define FGHIJK",
+ "#ifndef ABCDE\n"
+ "#define ABCDE 0\n"
+ "#endif\n"
+ "\n"
+ "#define FGHIJK",
+ Style);
}
TEST_F(FormatTest, FormatAlignInsidePreprocessorElseBlock) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/137112
More information about the cfe-commits
mailing list