[clang] b8cc85b - [clang][Interp] Limit lambda capture lazy visting to actual captures

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 7 04:44:54 PDT 2024


Author: Timm Bäder
Date: 2024-06-07T13:29:23+02:00
New Revision: b8cc85b318c0dd89e4dd69e3691ffcad5e401885

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

LOG: [clang][Interp] Limit lambda capture lazy visting to actual captures

Check this by looking at the VarDecl.

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 8dc71b2527f3b..ff2b51e3fb6fa 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -3895,12 +3895,13 @@ bool ByteCodeExprGen<Emitter>::visitDeclRef(const ValueDecl *D, const Expr *E) {
       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);
+             DRE && DRE->refersToEnclosingVariableOrCapture()) {
+    if (const auto *VD = dyn_cast<VarDecl>(D); VD && VD->isInitCapture()) {
+      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 71e7077550b28..0eb12643b1b7f 100644
--- a/clang/test/AST/Interp/lambda.cpp
+++ b/clang/test/AST/Interp/lambda.cpp
@@ -267,3 +267,16 @@ namespace CaptureDefaults {
 
 constexpr auto t4 = ([x=42]() consteval { return x; }());
 static_assert(t4 == 42, "");
+
+namespace InvalidCapture {
+
+  int &f(int *p);
+  char &f(...);
+  void g() {
+    int n = -1;   // both-note {{declared here}}
+    [=] {
+      int arr[n]; // both-warning {{variable length arrays in C++ are a Clang extension}} \
+                     both-note {{read of non-const variable 'n' is not allowed in a constant expression}}
+    } ();
+  }
+}


        


More information about the cfe-commits mailing list