Author: Timm Bäder
Date: 2024-06-15T07:36:43+02:00
New Revision: 7b6447a1574a1b852dfd8bff57ad4c48548c0102

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

LOG: [clang][Interp] Fix calling lambdas with explicit instance pointers...

...via a function pointer.

Added: 
    

Modified: 
    clang/lib/AST/Interp/Function.h
    clang/lib/AST/Interp/Interp.h
    clang/test/AST/Interp/lambda.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/Function.h b/clang/lib/AST/Interp/Function.h
index 0be4564e1e9ec..92bcd96927912 100644
--- a/clang/lib/AST/Interp/Function.h
+++ b/clang/lib/AST/Interp/Function.h
@@ -196,6 +196,12 @@ class Function final {
     return ArgSize - (align(primSize(PT_Ptr)) * (hasThisPointer() + hasRVO()));
   }
 
+  bool isThisPointerExplicit() const {
+    if (const auto *MD = dyn_cast<CXXMethodDecl>(F))
+      return MD->isExplicitObjectMemberFunction();
+    return false;
+  }
+
   unsigned getParamOffset(unsigned ParamIndex) const {
     return ParamOffsets[ParamIndex];
   }

diff  --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h
index 784e138e1467d..1ce92798150c5 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -2442,6 +2442,11 @@ inline bool CallPtr(InterpState &S, CodePtr OpPC, 
uint32_t ArgSize,
   assert(ArgSize >= F->getWrittenArgSize());
   uint32_t VarArgSize = ArgSize - F->getWrittenArgSize();
 
+  // We need to do this explicitly here since we don't have the necessary
+  // information to do it automatically.
+  if (F->isThisPointerExplicit())
+    VarArgSize -= align(primSize(PT_Ptr));
+
   if (F->isVirtual())
     return CallVirt(S, OpPC, F, VarArgSize);
 

diff  --git a/clang/test/AST/Interp/lambda.cpp 
b/clang/test/AST/Interp/lambda.cpp
index 0eb12643b1b7f..c4fce283ec45a 100644
--- a/clang/test/AST/Interp/lambda.cpp
+++ b/clang/test/AST/Interp/lambda.cpp
@@ -280,3 +280,14 @@ namespace InvalidCapture {
     } ();
   }
 }
+
+namespace ExplicitInstancePointer {
+  struct C {
+      constexpr C(auto) { }
+  };
+  void foo() {
+      constexpr auto b = [](this C) { return 1; };
+      constexpr int (*fp)(C) = b;
+      static_assert(fp(1) == 1, "");
+  }
+}


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to