[PATCH] D13325: Fix crash in codegen on casting to `bool &`.

Alexey Bataev via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 30 20:35:30 PDT 2015


ABataev created this revision.
ABataev added a reviewer: rjmccall.
ABataev added a subscriber: cfe-commits.

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*`.

http://reviews.llvm.org/D13325

Files:
  lib/CodeGen/CGExprScalar.cpp
  test/CodeGenCXX/cast-to-ref-bool.cpp

Index: lib/CodeGen/CGExprScalar.cpp
===================================================================
--- lib/CodeGen/CGExprScalar.cpp
+++ lib/CodeGen/CGExprScalar.cpp
@@ -1369,7 +1369,7 @@
   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());
   }
Index: test/CodeGenCXX/cast-to-ref-bool.cpp
===================================================================
--- test/CodeGenCXX/cast-to-ref-bool.cpp
+++ test/CodeGenCXX/cast-to-ref-bool.cpp
@@ -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;
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D13325.36179.patch
Type: text/x-patch
Size: 1028 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20151001/561d913e/attachment.bin>


More information about the cfe-commits mailing list