[cfe-commits] r62857 - in /cfe/trunk: lib/CodeGen/CGExprScalar.cpp test/CodeGen/pointer-arithmetic.c

Daniel Dunbar daniel at zuster.org
Fri Jan 23 10:51:10 PST 2009


Author: ddunbar
Date: Fri Jan 23 12:51:09 2009
New Revision: 62857

URL: http://llvm.org/viewvc/llvm-project?rev=62857&view=rev
Log:
Handle pointer arithmetic on function pointers.
 - <rdar://problem/6518844> Clang-generated bitcode crashes LLVM while compiling function pointer addition expression

Modified:
    cfe/trunk/lib/CodeGen/CGExprScalar.cpp
    cfe/trunk/test/CodeGen/pointer-arithmetic.c

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprScalar.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprScalar.cpp Fri Jan 23 12:51:09 2009
@@ -872,11 +872,14 @@
   // FIXME: What about a pointer to a VLA?
   Value *Ptr, *Idx;
   Expr *IdxExp;
-  if (isa<llvm::PointerType>(Ops.LHS->getType())) {  // pointer + int
+  const PointerType *PT;
+  if ((PT = Ops.E->getLHS()->getType()->getAsPointerType())) {
     Ptr = Ops.LHS;
     Idx = Ops.RHS;
     IdxExp = Ops.E->getRHS();
   } else {                                           // int + pointer
+    PT = Ops.E->getRHS()->getType()->getAsPointerType();
+    assert(PT && "Invalid add expr");
     Ptr = Ops.RHS;
     Idx = Ops.LHS;
     IdxExp = Ops.E->getLHS();
@@ -892,6 +895,17 @@
     else
       Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
   }
+
+  // Explicitly handle GNU void* and function pointer arithmetic
+  // extensions. The GNU void* casts amount to no-ops since our void*
+  // type is i8*, but this is future proof.
+  const QualType ElementType = PT->getPointeeType();
+  if (ElementType->isVoidType() || ElementType->isFunctionType()) {
+    const llvm::Type *i8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
+    Value *Casted = Builder.CreateBitCast(Ptr, i8Ty);
+    Value *Res = Builder.CreateGEP(Casted, Idx, "sub.ptr");
+    return Builder.CreateBitCast(Res, Ptr->getType());
+  } 
   
   return Builder.CreateGEP(Ptr, Idx, "add.ptr");
 }
@@ -900,6 +914,8 @@
   if (!isa<llvm::PointerType>(Ops.LHS->getType()))
     return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub");
 
+  const QualType LHSType = Ops.E->getLHS()->getType();
+  const QualType LHSElementType = LHSType->getAsPointerType()->getPointeeType();
   if (!isa<llvm::PointerType>(Ops.RHS->getType())) {
     // pointer - int
     Value *Idx = Ops.RHS;
@@ -916,16 +932,23 @@
     Idx = Builder.CreateNeg(Idx, "sub.ptr.neg");
     
     // FIXME: The pointer could point to a VLA.
-    // The GNU void* - int case is automatically handled here because
-    // our LLVM type for void* is i8*.
+
+    // Explicitly handle GNU void* and function pointer arithmetic
+    // extensions. The GNU void* casts amount to no-ops since our
+    // void* type is i8*, but this is future proof.
+    if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) {
+      const llvm::Type *i8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
+      Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty);
+      Value *Res = Builder.CreateGEP(LHSCasted, Idx, "sub.ptr");
+      return Builder.CreateBitCast(Res, Ops.LHS->getType());
+    } 
+      
     return Builder.CreateGEP(Ops.LHS, Idx, "sub.ptr");
   } else {
     // pointer - pointer
     Value *LHS = Ops.LHS;
     Value *RHS = Ops.RHS;
   
-    const QualType LHSType = Ops.E->getLHS()->getType();
-    const QualType LHSElementType = LHSType->getAsPointerType()->getPointeeType();
     uint64_t ElementSize;
 
     // Handle GCC extension for pointer arithmetic on void* types.

Modified: cfe/trunk/test/CodeGen/pointer-arithmetic.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/pointer-arithmetic.c?rev=62857&r1=62856&r2=62857&view=diff

==============================================================================
--- cfe/trunk/test/CodeGen/pointer-arithmetic.c (original)
+++ cfe/trunk/test/CodeGen/pointer-arithmetic.c Fri Jan 23 12:51:09 2009
@@ -1,7 +1,22 @@
-// RUN: clang -emit-llvm %s -o %t
+// RUN: clang -S %s -o %t
 
 typedef int Int;
 
-int test1(int *a, Int *b) { return a - b; }
+int f0(int *a, Int *b) { return a - b; }
 
-int test2(const char *a, char *b) { return b - a; }
+int f1(const char *a, char *b) { return b - a; }
+
+// GNU extensions
+typedef void (*FP)(void);
+void *f2(void *a, int b) { return a + b; }
+void *f2_1(void *a, int b) { return (a += b); }
+void *f3(int a, void *b) { return a + b; }
+void *f3_1(int a, void *b) { return (a += b); }
+void *f4(void *a, int b) { return a - b; }
+void *f4_1(void *a, int b) { return (a -= b); }
+FP f5(FP a, int b) { return a + b; }
+FP f5_1(FP a, int b) { return (a += b); }
+FP f6(int a, FP b) { return a + b; }
+FP f6_1(int a, FP b) { return (a += b); }
+FP f7(FP a, int b) { return a - b; }
+FP f7_1(FP a, int b) { return (a -= b); }





More information about the cfe-commits mailing list