[clang] fd22785 - [Lex] Consider a PCH header-guarded even with #endif truncated

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 20 05:37:24 PDT 2021


Author: Sam McCall
Date: 2021-07-20T14:25:36+02:00
New Revision: fd2278505482bd9db21423b0620be6afe04feb60

URL: https://github.com/llvm/llvm-project/commit/fd2278505482bd9db21423b0620be6afe04feb60
DIFF: https://github.com/llvm/llvm-project/commit/fd2278505482bd9db21423b0620be6afe04feb60.diff

LOG: [Lex] Consider a PCH header-guarded even with #endif truncated

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.

Differential Revision: https://reviews.llvm.org/D106204

Added: 
    

Modified: 
    clang-tools-extra/clangd/unittests/ParsedASTTests.cpp
    clang/lib/Lex/Lexer.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/unittests/ParsedASTTests.cpp b/clang-tools-extra/clangd/unittests/ParsedASTTests.cpp
index 365dbc581c201..ef0199a11ed90 100644
--- a/clang-tools-extra/clangd/unittests/ParsedASTTests.cpp
+++ b/clang-tools-extra/clangd/unittests/ParsedASTTests.cpp
@@ -667,7 +667,7 @@ TEST(ParsedASTTest, HeaderGuards) {
   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()));
@@ -768,7 +768,7 @@ TEST(ParsedASTTest, HeaderGuardsSelfInclude) {
   )cpp";
   AST = TU.build();
   EXPECT_THAT(*AST.getDiagnostics(), IsEmpty());
-  EXPECT_FALSE(mainIsGuarded(AST)); // FIXME: true
+  EXPECT_TRUE(mainIsGuarded(AST));
 
   // Guarded too late...
   TU.Code = R"cpp(
@@ -858,7 +858,7 @@ TEST(ParsedASTTest, HeaderGuardsImplIface) {
   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);

diff  --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp
index 2cc4cae533d07..ac0489e58407f 100644
--- a/clang/lib/Lex/Lexer.cpp
+++ b/clang/lib/Lex/Lexer.cpp
@@ -2793,6 +2793,11 @@ bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
 
   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();
   }
 


        


More information about the cfe-commits mailing list