sammccall created this revision. sammccall added a reviewer: kadircet. Herald added subscribers: usaxena95, arphaman. sammccall requested review of this revision. Herald added projects: clang, clang-tools-extra. Herald added a subscriber: cfe-commits.
This seems to be a more useful behavior for tools that use preambles. I believe it doesn't affect real compiles: the PCH is only included once when used, and recursive inclusion of the main-file *within* the PCH isn't supported in any case. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D106204 Files: clang-tools-extra/clangd/unittests/ParsedASTTests.cpp clang/lib/Lex/Lexer.cpp Index: clang/lib/Lex/Lexer.cpp =================================================================== --- clang/lib/Lex/Lexer.cpp +++ clang/lib/Lex/Lexer.cpp @@ -2793,6 +2793,11 @@ if (PP->isRecordingPreamble() && PP->isInPrimaryFile()) { PP->setRecordedPreambleConditionalStack(ConditionalStack); + // If the preamble cuts off the end of a header guard, consider it guarded. + // The guard is valid for the preamble content itself, and for tools the + // most useful answer is "yes, this file has a header guard". + if (!ConditionalStack.empty()) + MIOpt.ExitTopLevelConditional(); ConditionalStack.clear(); } Index: clang-tools-extra/clangd/unittests/ParsedASTTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/ParsedASTTests.cpp +++ clang-tools-extra/clangd/unittests/ParsedASTTests.cpp @@ -667,7 +667,7 @@ EXPECT_FALSE(mainIsGuarded(TU.build())); TU.Code = guard(";"); - EXPECT_FALSE(mainIsGuarded(TU.build())); // FIXME: true + EXPECT_TRUE(mainIsGuarded(TU.build())); TU.Code = once(";"); EXPECT_TRUE(mainIsGuarded(TU.build())); @@ -751,7 +751,7 @@ )cpp"; AST = TU.build(); EXPECT_THAT(*AST.getDiagnostics(), IsEmpty()); - EXPECT_FALSE(mainIsGuarded(AST)); // FIXME: true + EXPECT_TRUE(mainIsGuarded(AST)); } // Tests how we handle common idioms for splitting a header-only library @@ -785,7 +785,7 @@ TU.AdditionalFiles = {{"impl.h", Implementation}}; auto AST = TU.build(); EXPECT_THAT(*AST.getDiagnostics(), IsEmpty()); - EXPECT_FALSE(mainIsGuarded(AST)); // FIXME: true + EXPECT_TRUE(mainIsGuarded(AST)); // Slightly harder: the `#pragma once` is part of the preamble, and we // need to transfer it to the main file's HeaderFileInfo. TU.Code = once(Interface);
Index: clang/lib/Lex/Lexer.cpp =================================================================== --- clang/lib/Lex/Lexer.cpp +++ clang/lib/Lex/Lexer.cpp @@ -2793,6 +2793,11 @@ if (PP->isRecordingPreamble() && PP->isInPrimaryFile()) { PP->setRecordedPreambleConditionalStack(ConditionalStack); + // If the preamble cuts off the end of a header guard, consider it guarded. + // The guard is valid for the preamble content itself, and for tools the + // most useful answer is "yes, this file has a header guard". + if (!ConditionalStack.empty()) + MIOpt.ExitTopLevelConditional(); ConditionalStack.clear(); } Index: clang-tools-extra/clangd/unittests/ParsedASTTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/ParsedASTTests.cpp +++ clang-tools-extra/clangd/unittests/ParsedASTTests.cpp @@ -667,7 +667,7 @@ EXPECT_FALSE(mainIsGuarded(TU.build())); TU.Code = guard(";"); - EXPECT_FALSE(mainIsGuarded(TU.build())); // FIXME: true + EXPECT_TRUE(mainIsGuarded(TU.build())); TU.Code = once(";"); EXPECT_TRUE(mainIsGuarded(TU.build())); @@ -751,7 +751,7 @@ )cpp"; AST = TU.build(); EXPECT_THAT(*AST.getDiagnostics(), IsEmpty()); - EXPECT_FALSE(mainIsGuarded(AST)); // FIXME: true + EXPECT_TRUE(mainIsGuarded(AST)); } // Tests how we handle common idioms for splitting a header-only library @@ -785,7 +785,7 @@ TU.AdditionalFiles = {{"impl.h", Implementation}}; auto AST = TU.build(); EXPECT_THAT(*AST.getDiagnostics(), IsEmpty()); - EXPECT_FALSE(mainIsGuarded(AST)); // FIXME: true + EXPECT_TRUE(mainIsGuarded(AST)); // Slightly harder: the `#pragma once` is part of the preamble, and we // need to transfer it to the main file's HeaderFileInfo. TU.Code = once(Interface);
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits