https://github.com/7se7en72025 created https://github.com/llvm/llvm-project/pull/246854
A top-level conditional that looks like a header guard can end before the rest of the file. clang-format leaves the guard candidate active in this case, so the next top-level macro tries to use a negative preprocessor indentation level. Resolve the candidate at its closing directive. Keep it as a guard if only comments and end of file follow. Reject it when the file contains more source. I reproduced the original assertion on Windows LLVM 23.1.6. All 397 tests in FormatTest passed with the patch reapplied to that build and the preprocessor-level check enabled. Fixes #245441. Assisted-by: OpenAI Codex. Draft for contributor review. >From 3689537fb146a7882789f30aaa0d11bb680d54e3 Mon Sep 17 00:00:00 2001 From: 7se7en72025 <[email protected]> Date: Sat, 28 Nov 2026 19:43:34 +0530 Subject: [PATCH] [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; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
