[clang] 3a31eae - [clang][Interp] Fix refers_to_enclosing_variable_or_capture DREs
Timm Bäder via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 7 02:49:09 PDT 2024
Author: Timm Bäder
Date: 2024-06-07T11:48:44+02:00
New Revision: 3a31eaeac8482fa5e242ee00cd4e77b203db539e
URL: https://github.com/llvm/llvm-project/commit/3a31eaeac8482fa5e242ee00cd4e77b203db539e
DIFF: https://github.com/llvm/llvm-project/commit/3a31eaeac8482fa5e242ee00cd4e77b203db539e.diff
LOG: [clang][Interp] Fix refers_to_enclosing_variable_or_capture DREs
They do not count into lambda captures, so visit them lazily.
Added:
Modified:
clang/lib/AST/Interp/ByteCodeExprGen.cpp
clang/test/AST/Interp/lambda.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index 1d0c36d0bf09f..8dc71b2527f3b 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -3894,6 +3894,13 @@ bool ByteCodeExprGen<Emitter>::visitDeclRef(const ValueDecl *D, const Expr *E) {
if (IsPtr)
return this->emitGetThisFieldPtr(Offset, E);
return this->emitGetPtrThisField(Offset, E);
+ } else if (const auto *DRE = dyn_cast<DeclRefExpr>(E);
+ DRE && DRE->refersToEnclosingVariableOrCapture() &&
+ isa<VarDecl>(D)) {
+ if (!this->visitVarDecl(cast<VarDecl>(D)))
+ return false;
+ // Retry.
+ return this->visitDeclRef(D, E);
}
// Try to lazily visit (or emit dummy pointers for) declarations
diff --git a/clang/test/AST/Interp/lambda.cpp b/clang/test/AST/Interp/lambda.cpp
index 77e035ce25470..71e7077550b28 100644
--- a/clang/test/AST/Interp/lambda.cpp
+++ b/clang/test/AST/Interp/lambda.cpp
@@ -264,3 +264,6 @@ namespace CaptureDefaults {
};
static_assert(f2() == 3, "");
}
+
+constexpr auto t4 = ([x=42]() consteval { return x; }());
+static_assert(t4 == 42, "");
More information about the cfe-commits
mailing list