https://github.com/7se7en72025 updated https://github.com/llvm/llvm-project/pull/246854
>From 3689537fb146a7882789f30aaa0d11bb680d54e3 Mon Sep 17 00:00:00 2001 From: 7se7en72025 <[email protected]> Date: Sat, 28 Nov 2026 19:43:34 +0530 Subject: [PATCH 1/2] [clang-format] Reject include guard candidates before later code Assisted-by: OpenAI Codex --- clang/lib/Format/UnwrappedLineParser.cpp | 13 ++++++------- clang/unittests/Format/FormatTest.cpp | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 28f240553cd63c..c30a20fd89a314 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -5318,14 +5318,13 @@ void UnwrappedLineParser::readToken(int LevelDifference) { PreviousWasComment = FormatTok->is(tok::comment); FirstNonCommentOnLine = IsFirstNonCommentOnLine( FirstNonCommentOnLine, *FormatTok, PreviousWasComment); - // If the #endif of a potential include guard is the last thing in the - // file, then we found an include guard. + // An include guard must enclose the whole file. if (IsEndIf && PP.IncludeGuard == IG_Defined && PP.BranchLevel == -1 && - getIncludeGuardState(Style.IndentPPDirectives) == IG_Inited && - (eof() || - (PreviousWasComment && - Tokens->peekNextToken(/*SkipComment=*/true)->is(tok::eof)))) { - PP.IncludeGuard = IG_Found; + getIncludeGuardState(Style.IndentPPDirectives) == IG_Inited) { + const bool EndsFile = + eof() || (PreviousWasComment && + Tokens->peekNextToken(/*SkipComment=*/true)->is(tok::eof)); + PP.IncludeGuard = EndsFile ? IG_Found : IG_Rejected; } } diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index be4936e9a8d95e..a2268831efec42 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -6542,6 +6542,20 @@ TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { getLLVMStyleWithColumns(11)); } +TEST_F(FormatTest, RejectsIncludeGuardFollowedByAnotherDirective) { + auto Style = getLLVMStyle(); + Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; + verifyFormat("#ifndef HEADER_H\n" + " #define HEADER_H\n" + "#endif\n" + "#define OTHER", + "#ifndef HEADER_H\n" + "#define HEADER_H\n" + "#endif\n" + "#define OTHER", + Style); +} + TEST_F(FormatTest, IndentPreprocessorDirectives) { FormatStyle Style = getLLVMStyleWithColumns(40); Style.IndentPPDirectives = FormatStyle::PPDIS_None; >From f7b0a5255b3e63e5582a2439bce2d9d211babba8 Mon Sep 17 00:00:00 2001 From: 7se7en72025 <[email protected]> Date: Sat, 28 Nov 2026 23:39:14 +0530 Subject: [PATCH 2/2] [clang-format] Match project formatting for guard check --- clang/lib/Format/UnwrappedLineParser.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index c30a20fd89a314..9614602ac8aab5 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -5322,8 +5322,9 @@ void UnwrappedLineParser::readToken(int LevelDifference) { if (IsEndIf && PP.IncludeGuard == IG_Defined && PP.BranchLevel == -1 && getIncludeGuardState(Style.IndentPPDirectives) == IG_Inited) { const bool EndsFile = - eof() || (PreviousWasComment && - Tokens->peekNextToken(/*SkipComment=*/true)->is(tok::eof)); + eof() || + (PreviousWasComment && + Tokens->peekNextToken(/*SkipComment=*/true)->is(tok::eof)); PP.IncludeGuard = EndsFile ? IG_Found : IG_Rejected; } } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
