r249534 - Fix crash in codegen on casting to `bool &`.

Alexey Bataev via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 7 03:22:09 PDT 2015


Author: abataev
Date: Wed Oct  7 05:22:08 2015
New Revision: 249534

URL: http://llvm.org/viewvc/llvm-project?rev=249534&view=rev
Log:
Fix crash in codegen on casting to `bool &`.
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

Added:
    cfe/trunk/test/CodeGenCXX/cast-to-ref-bool.cpp   (with props)
Modified:
    cfe/trunk/lib/CodeGen/CGExprScalar.cpp

Modified: cfe/trunk/lib/CodeGen/CGExprScalar.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprScalar.cpp?rev=249534&r1=249533&r2=249534&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprScalar.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprScalar.cpp Wed Oct  7 05:22:08 2015
@@ -1383,7 +1383,7 @@ Value *ScalarExprEmitter::VisitCastExpr(
   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());
   }

Added: cfe/trunk/test/CodeGenCXX/cast-to-ref-bool.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/cast-to-ref-bool.cpp?rev=249534&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenCXX/cast-to-ref-bool.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/cast-to-ref-bool.cpp Wed Oct  7 05:22:08 2015
@@ -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;
+}

Propchange: cfe/trunk/test/CodeGenCXX/cast-to-ref-bool.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/CodeGenCXX/cast-to-ref-bool.cpp
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Rev URL

Propchange: cfe/trunk/test/CodeGenCXX/cast-to-ref-bool.cpp
------------------------------------------------------------------------------
    svn:mime-type = text/plain




More information about the cfe-commits mailing list