[clang] [clang][Lex] Do not look up files for __has_include in unevaluated operands (PR #247849)
Brad Smith via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 1 19:05:21 PST 2026
https://github.com/brad0 updated https://github.com/llvm/llvm-project/pull/247849
>From 4ae57c7b7cbf87799f56e4f3d5c1619da022b77e Mon Sep 17 00:00:00 2001
From: Brad Smith <brad at comstyle.com>
Date: Tue, 1 Dec 2026 18:30:00 -0500
Subject: [PATCH] [clang][Lex] Do not look up files for __has_include in
unevaluated operands
Clang expands __has_include, __has_include_next and __has_embed while
lexing the #if expression, before the evaluator knows whether the
operand is used. So the file is looked up, and ends up in the
dependency output, even on the right-hand side of '0 &&'. Clang's own
tgmath.h has
#if defined(__APPLE__) && __STDC_HOSTED__ && __has_include_next(<tgmath.h>)
so on Linux /usr/include/tgmath.h is listed by -M even though it is
never read.
GCC does not do the lookup when it is skipping evaluation. Do the same:
track whether the operand being lexed is evaluated and skip the lookup
when it is not.
Fixes #120891
Co-Authored-By: Claude Opus 5.5 <noreply at anthropic.com>
---
clang/include/clang/Lex/Preprocessor.h | 12 ++++
clang/lib/Lex/PPExpressions.cpp | 30 ++++++++-
clang/lib/Lex/PPMacroExpansion.cpp | 13 ++++
.../dependency-gen-has-include-unevaluated.c | 62 +++++++++++++++++++
4 files changed, 114 insertions(+), 3 deletions(-)
create mode 100644 clang/test/Frontend/dependency-gen-has-include-unevaluated.c
diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h
index 8d15ed7e8afd85..ec124493d9e3c8 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -302,6 +302,10 @@ class Preprocessor {
/// True if we are currently preprocessing a #if or #elif directive
bool ParsingIfOrElifDirective;
+ /// True if we are lexing an operand of a #if or #elif expression whose
+ /// value is not used, such as the right-hand side of '0 &&'.
+ bool InUnevaluatedPPOperand = false;
+
/// True if we are pre-expanding macro arguments.
bool InMacroArgPreExpansion;
@@ -1312,6 +1316,14 @@ class Preprocessor {
return ParsingIfOrElifDirective;
}
+ /// True if we are lexing an operand of a #if or #elif expression whose
+ /// value is not used. __has_include, __has_include_next and __has_embed
+ /// do not look up the file there.
+ bool isInUnevaluatedPPOperand() const { return InUnevaluatedPPOperand; }
+ void setInUnevaluatedPPOperand(bool Unevaluated) {
+ InUnevaluatedPPOperand = Unevaluated;
+ }
+
/// Control whether the preprocessor retains comments in output.
void SetCommentRetentionState(bool KeepComments, bool KeepMacroComments) {
this->KeepComments = KeepComments | KeepMacroComments;
diff --git a/clang/lib/Lex/PPExpressions.cpp b/clang/lib/Lex/PPExpressions.cpp
index 8ccfe50d20493a..ea54a5ec5103ad 100644
--- a/clang/lib/Lex/PPExpressions.cpp
+++ b/clang/lib/Lex/PPExpressions.cpp
@@ -71,6 +71,21 @@ class PPValue {
void setEnd(SourceLocation L) { Range.setEnd(L); }
};
+/// Marks the tokens lexed while it is alive as part of an operand whose value
+/// is used or not. Like GCC, __has_include, __has_include_next and
+/// __has_embed do not look up the file in an operand whose value is not used.
+class UnevaluatedOperandRAII {
+ Preprocessor &PP;
+ bool OldUnevaluated;
+
+public:
+ UnevaluatedOperandRAII(Preprocessor &PP, bool Unevaluated)
+ : PP(PP), OldUnevaluated(PP.isInUnevaluatedPPOperand()) {
+ PP.setInUnevaluatedPPOperand(Unevaluated);
+ }
+ ~UnevaluatedOperandRAII() { PP.setInUnevaluatedPPOperand(OldUnevaluated); }
+};
+
} // end anonymous namespace
static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
@@ -237,6 +252,9 @@ static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
Result.setIdentifier(nullptr);
+ // The tokens lexed below belong to this value.
+ UnevaluatedOperandRAII Unevaluated(PP, !ValueLive);
+
if (PeekTok.is(tok::code_completion)) {
if (PP.getCodeCompletionHandler())
PP.getCodeCompletionHandler()->CodeCompletePreprocessorExpression();
@@ -644,7 +662,10 @@ static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
// Consume the operator, remembering the operator's location for reporting.
SourceLocation OpLoc = PeekTok.getLocation();
- PP.LexNonComment(PeekTok);
+ {
+ UnevaluatedOperandRAII Unevaluated(PP, !RHSIsLive);
+ PP.LexNonComment(PeekTok);
+ }
PPValue RHS(LHS.getBitWidth());
// Parse the RHS of the operator.
@@ -837,11 +858,14 @@ static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
PP.Diag(OpLoc, diag::note_matching) << tok::question;
return true;
}
+ bool AfterColonLive = ValueLive && LHS.Val == 0;
// Consume the :.
- PP.LexNonComment(PeekTok);
+ {
+ UnevaluatedOperandRAII Unevaluated(PP, !AfterColonLive);
+ PP.LexNonComment(PeekTok);
+ }
// Evaluate the value after the :.
- bool AfterColonLive = ValueLive && LHS.Val == 0;
PPValue AfterColonVal(LHS.getBitWidth());
DefinedTracker DT;
if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp
index bce83b27298f88..adfc25b12bbca3 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -1220,6 +1220,13 @@ static bool EvaluateHasIncludeCommon(Token &Tok, IdentifierInfo *II,
if (Filename.empty())
return false;
+ // Like GCC, do not look up the file in an operand whose value is not used,
+ // such as the right-hand side of '0 &&'. Besides saving the file system
+ // access, this keeps a file that is never used out of the dependency
+ // output.
+ if (PP.isInUnevaluatedPPOperand())
+ return false;
+
// Passing this to LookupFile forces header search to check whether the found
// file belongs to a module. Skipping that check could incorrectly mark
// modular header as textual, causing issues down the line.
@@ -1303,6 +1310,12 @@ EmbedResult Preprocessor::EvaluateHasEmbed(Token &Tok, IdentifierInfo *II) {
this->GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
// If GetIncludeFilenameSpelling set the start ptr to null, there was an
// error.
+
+ // Like GCC, do not look up the resource in an operand whose value is not
+ // used, such as the right-hand side of '0 &&'.
+ if (isInUnevaluatedPPOperand())
+ return EmbedResult::NotFound;
+
OptionalFileEntryRef MaybeFileEntry =
this->LookupEmbedFile(Filename, isAngled, false);
if (Callbacks) {
diff --git a/clang/test/Frontend/dependency-gen-has-include-unevaluated.c b/clang/test/Frontend/dependency-gen-has-include-unevaluated.c
new file mode 100644
index 00000000000000..cf4b4c7b87d31b
--- /dev/null
+++ b/clang/test/Frontend/dependency-gen-has-include-unevaluated.c
@@ -0,0 +1,62 @@
+// Check that __has_include, __has_include_next and __has_embed do not look up
+// the file in an operand whose value is not used, like GCC, so the file does
+// not end up in the dependency output.
+
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+// RUN: %clang -std=c23 -fsyntax-only -MD -MF %t/deps -MT out \
+// RUN: -I %t/inc -I %t/next --embed-dir=%t/embed %t/main.c
+// RUN: FileCheck --input-file=%t/deps --implicit-check-not=skipped %s
+
+// CHECK: out:
+// CHECK: main.c
+// CHECK: inc{{[/\\]}}live-or.h
+// CHECK: inc{{[/\\]}}live-cond.h
+// CHECK: embed{{[/\\]}}live-embed.bin
+// CHECK: inc{{[/\\]}}next.h
+// CHECK: next{{[/\\]}}live-next.h
+
+//--- main.c
+#if 0 && __has_include("skipped-and.h")
+#endif
+#if 1 || __has_include("skipped-or.h")
+#endif
+#if 0 ? __has_include("skipped-true.h") : 0
+#endif
+#if 1 ? 0 : __has_include("skipped-false.h")
+#endif
+#if 0 && (1 + !__has_include("skipped-nested.h"))
+#endif
+#if 0 && __has_embed(<skipped-embed.bin>)
+#endif
+
+#if !(0 && 1 || __has_include("live-or.h"))
+#error live-or.h not found
+#endif
+#if !(1 ? __has_include("live-cond.h") : 0)
+#error live-cond.h not found
+#endif
+#if __has_embed(<live-embed.bin>) == __STDC_EMBED_NOT_FOUND__
+#error live-embed.bin not found
+#endif
+
+#include "next.h"
+
+//--- inc/next.h
+#if 0 && __has_include_next(<skipped-next.h>)
+#endif
+#if !__has_include_next(<live-next.h>)
+#error live-next.h not found
+#endif
+
+//--- inc/skipped-and.h
+//--- inc/skipped-or.h
+//--- inc/skipped-true.h
+//--- inc/skipped-false.h
+//--- inc/skipped-nested.h
+//--- inc/live-or.h
+//--- inc/live-cond.h
+//--- next/skipped-next.h
+//--- next/live-next.h
+//--- embed/skipped-embed.bin
+//--- embed/live-embed.bin
More information about the cfe-commits
mailing list