[clang] 45bd85e - [clang][Interp] Fix casting function pointers to integers

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 29 07:42:01 PDT 2024


Author: Timm Bäder
Date: 2024-04-29T16:33:21+02:00
New Revision: 45bd85e4815254a4528cc337447fd6a8eb6fd583

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

LOG: [clang][Interp] Fix casting function pointers to integers

Added: 
    

Modified: 
    clang/lib/AST/Interp/ByteCodeExprGen.cpp
    clang/test/AST/Interp/c.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index accaea0a84424c..3ceccfdebfca66 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -212,6 +212,13 @@ bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) {
     if (!this->visit(SubExpr))
       return false;
 
+    // If SubExpr doesn't result in a pointer, make it one.
+    if (PrimType FromT = classifyPrim(SubExpr->getType()); FromT != PT_Ptr) {
+      assert(isPtrType(FromT));
+      if (!this->emitDecayPtr(FromT, PT_Ptr, CE))
+        return false;
+    }
+
     PrimType T = classifyPrim(CE->getType());
     if (T == PT_IntAP)
       return this->emitCastPointerIntegralAP(Ctx.getBitWidth(CE->getType()),

diff  --git a/clang/test/AST/Interp/c.c b/clang/test/AST/Interp/c.c
index a5951158ed0e0d..207da5fe812608 100644
--- a/clang/test/AST/Interp/c.c
+++ b/clang/test/AST/Interp/c.c
@@ -263,3 +263,10 @@ const int *p = &b;
 const __int128 K = (__int128)(int*)0;
 const unsigned __int128 KU = (unsigned __int128)(int*)0;
 #endif
+
+
+int test3(void) {
+  int a[2];
+  a[0] = test3; // all-error {{incompatible pointer to integer conversion assigning to 'int' from 'int (void)'}}
+  return 0;
+}


        


More information about the cfe-commits mailing list