]> granicus.if.org Git - clang/commitdiff
IRgen: Fix a horrible bug in pointer to bool conversion, which we were treating
authorDaniel Dunbar <daniel@zuster.org>
Wed, 25 Aug 2010 03:32:38 +0000 (03:32 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Wed, 25 Aug 2010 03:32:38 +0000 (03:32 +0000)
as a truncation not a comparison to null.

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

lib/CodeGen/CGExprScalar.cpp
test/CodeGen/_Bool-conversion.c [new file with mode: 0644]

index 49deb7b365a8cc0957b8664be09c48476b437912..6901f9b924fbe17711a0c91dca121adc79db0213 100644 (file)
@@ -1017,18 +1017,23 @@ Value *ScalarExprEmitter::EmitCastExpr(CastExpr *CE) {
 
   case CastExpr::CK_IntegralToPointer: {
     Value *Src = Visit(const_cast<Expr*>(E));
-    
+
     // First, convert to the correct width so that we control the kind of
     // extension.
     const llvm::Type *MiddleTy = CGF.IntPtrTy;
     bool InputSigned = E->getType()->isSignedIntegerType();
     llvm::Value* IntResult =
       Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv");
-    
+
     return Builder.CreateIntToPtr(IntResult, ConvertType(DestTy));
   }
   case CastExpr::CK_PointerToIntegral: {
     Value *Src = Visit(const_cast<Expr*>(E));
+
+    // Handle conversion to bool correctly.
+    if (DestTy->isBooleanType())
+      return EmitScalarConversion(Visit(E), E->getType(), DestTy);
+
     return Builder.CreatePtrToInt(Src, ConvertType(DestTy));
   }
   case CastExpr::CK_ToVoid: {
diff --git a/test/CodeGen/_Bool-conversion.c b/test/CodeGen/_Bool-conversion.c
new file mode 100644 (file)
index 0000000..fce7ada
--- /dev/null
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -triple i386 -emit-llvm -O2 -o - %s | FileCheck %s
+
+// CHECK: define i32 @f0()
+// CHECK:  ret i32 1
+// CHECK: }
+
+static _Bool f0_0(void *a0) { return (_Bool) a0; }
+int f0() { return f0_0((void*) 0x2); }