[cfe-commits] r72690 - in /cfe/trunk: lib/CodeGen/CGExpr.cpp test/CodeGen/2009-06-01-addrofknr.c

Eli Friedman eli.friedman at gmail.com
Mon Jun 1 03:04:27 PDT 2009


Author: efriedma
Date: Mon Jun  1 05:04:20 2009
New Revision: 72690

URL: http://llvm.org/viewvc/llvm-project?rev=72690&view=rev
Log:
PR4289: Make sure "&func" has the right LLVM type when "func" is a 
K&R-style definition.


Added:
    cfe/trunk/test/CodeGen/2009-06-01-addrofknr.c
Modified:
    cfe/trunk/lib/CodeGen/CGExpr.cpp

Modified: cfe/trunk/lib/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExpr.cpp?rev=72690&r1=72689&r2=72690&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExpr.cpp Mon Jun  1 05:04:20 2009
@@ -711,8 +711,20 @@
       LV.SetGlobalObjCRef(LV, true);
     return LV;
   } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) {
-    return LValue::MakeAddr(CGM.GetAddrOfFunction(GlobalDecl(FD)),
-                            E->getType().getCVRQualifiers(),
+    llvm::Value* V = CGM.GetAddrOfFunction(GlobalDecl(FD));
+    if (!FD->hasPrototype()) {
+      if (const FunctionProtoType *Proto =
+              FD->getType()->getAsFunctionProtoType()) {
+        // Ugly case: for a K&R-style definition, the type of the definition
+        // isn't the same as the type of a use.  Correct for this with a
+        // bitcast.
+        QualType NoProtoType =
+            getContext().getFunctionNoProtoType(Proto->getResultType());
+        NoProtoType = getContext().getPointerType(NoProtoType);
+        V = Builder.CreateBitCast(V, ConvertType(NoProtoType), "tmp");
+      }
+    }
+    return LValue::MakeAddr(V, E->getType().getCVRQualifiers(),
                             getContext().getObjCGCAttrKind(E->getType()));
   }
   else if (const ImplicitParamDecl *IPD =

Added: cfe/trunk/test/CodeGen/2009-06-01-addrofknr.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/2009-06-01-addrofknr.c?rev=72690&view=auto

==============================================================================
--- cfe/trunk/test/CodeGen/2009-06-01-addrofknr.c (added)
+++ cfe/trunk/test/CodeGen/2009-06-01-addrofknr.c Mon Jun  1 05:04:20 2009
@@ -0,0 +1,21 @@
+// RUN: clang-cc %s -o %t -emit-llvm -verify
+// PR4289
+
+struct funcptr {
+    int (*func)();
+};
+
+static int func(f)
+        void *f;
+{
+}
+
+int
+main(int argc, char *argv[])
+{
+        struct funcptr fp;
+
+        fp.func = &func;
+        fp.func = func;
+}
+





More information about the cfe-commits mailing list