]> granicus.if.org Git - clang/commitdiff
Attempt to fix PR3709: when converting from an integer to a pointer,
authorEli Friedman <eli.friedman@gmail.com>
Wed, 4 Mar 2009 04:02:35 +0000 (04:02 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Wed, 4 Mar 2009 04:02:35 +0000 (04:02 +0000)
first extend the integer to the correct width.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@66009 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGExprScalar.cpp
test/CodeGen/PR3709-int-to-pointer-sign.c [new file with mode: 0644]

index 0b8cc535bf02dd4a05c6b745a17068096faafac2..38cc3eec5dc760338df473a046f6c3875fe571d8 100644 (file)
@@ -403,7 +403,14 @@ Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
     if (isa<llvm::PointerType>(Src->getType()))
       return Builder.CreateBitCast(Src, DstTy, "conv");
     assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
-    return Builder.CreateIntToPtr(Src, DstTy, "conv");
+    // First, convert to the correct width so that we control the kind of
+    // extension.
+    const llvm::Type *MiddleTy = llvm::IntegerType::get(CGF.LLVMPointerWidth);
+    bool InputSigned = SrcType->isSignedIntegerType();
+    llvm::Value* IntResult =
+        Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv");
+    // Then, cast to pointer.
+    return Builder.CreateIntToPtr(IntResult, DstTy, "conv");
   }
   
   if (isa<llvm::PointerType>(Src->getType())) {
diff --git a/test/CodeGen/PR3709-int-to-pointer-sign.c b/test/CodeGen/PR3709-int-to-pointer-sign.c
new file mode 100644 (file)
index 0000000..85944a3
--- /dev/null
@@ -0,0 +1,5 @@
+// RUN: clang -emit-llvm %s -o - -O1 -triple=x86_64-gnu-linux | grep "i64 -1"
+
+// PR3709
+long long a() { return (long long)(int*)-1;}
+