]> granicus.if.org Git - clang/commitdiff
Fix crash in codegen on casting to `bool &`.
authorAlexey Bataev <a.bataev@hotmail.com>
Wed, 7 Oct 2015 10:22:08 +0000 (10:22 +0000)
committerAlexey Bataev <a.bataev@hotmail.com>
Wed, 7 Oct 2015 10:22:08 +0000 (10:22 +0000)
Currently codegen crashes trying to emit casting to bool &. It happens because bool type is converted to i1 and later then lvalue for reference is converted to i1*. But when codegen tries to load this lvalue it crashes trying to load value from this i1*.

Differential Revision: http://reviews.llvm.org/D13325

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

lib/CodeGen/CGExprScalar.cpp
test/CodeGenCXX/cast-to-ref-bool.cpp [new file with mode: 0644]

index 44a8646c1199824058af1a9aa53a54bbaf083e66..6a40e4ed86f682078b603a40dc77ac9096022027 100644 (file)
@@ -1383,7 +1383,7 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
   case CK_LValueBitCast:
   case CK_ObjCObjectLValueCast: {
     Address Addr = EmitLValue(E).getAddress();
-    Addr = Builder.CreateElementBitCast(Addr, ConvertType(DestTy));
+    Addr = Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(DestTy));
     LValue LV = CGF.MakeAddrLValue(Addr, DestTy);
     return EmitLoadOfLValue(LV, CE->getExprLoc());
   }
diff --git a/test/CodeGenCXX/cast-to-ref-bool.cpp b/test/CodeGenCXX/cast-to-ref-bool.cpp
new file mode 100644 (file)
index 0000000..1064720
--- /dev/null
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o - %s | FileCheck %s
+
+// CHECK-LABEL: main
+int main(int argc, char **argv) {
+  // CHECK: load i8, i8* %
+  // CHECK-NEXT: trunc i8 %{{.+}} to i1
+  bool b = (bool &)argv[argc][1];
+  return b;
+}