]> granicus.if.org Git - clang/commitdiff
Patch fixes miscompile with non-trivial copy constructors and
authorFariborz Jahanian <fjahanian@apple.com>
Fri, 22 Oct 2010 21:01:02 +0000 (21:01 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Fri, 22 Oct 2010 21:01:02 +0000 (21:01 +0000)
statement expressions, //rdar: //8540501

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

lib/CodeGen/CGExprAgg.cpp
lib/CodeGen/CGExprCXX.cpp
lib/CodeGen/CGValue.h
test/CodeGenCXX/stmtexpr-copy-init.cpp [new file with mode: 0644]

index 31ba3373f08d0b53e3b96873a9dabddab8d85e72..99b46d41a812cc8f157670b2a82b2c92f2af128a 100644 (file)
@@ -147,6 +147,23 @@ public:
 /// represents a value lvalue, this method emits the address of the lvalue,
 /// then loads the result into DestPtr.
 void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
+  if (CGF.getContext().getLangOptions().CPlusPlus) {
+    if (const CXXConstructExpr *CE = Dest.getCtorExpr()) {
+      // Perform copy initialization of Src into Dest.
+      const CXXConstructorDecl *CD = CE->getConstructor();
+      CXXCtorType Type = 
+        (CE->getConstructionKind() == CXXConstructExpr::CK_Complete) 
+        ? Ctor_Complete : Ctor_Base;
+      bool ForVirtualBase = 
+        CE->getConstructionKind() == CXXConstructExpr::CK_VirtualBase;
+      // Call the constructor.
+      const Stmt * S = dyn_cast<Stmt>(E);
+      clang::ConstExprIterator BegExp(&S);
+      CGF.EmitCXXConstructorCall(CD, Type, ForVirtualBase, Dest.getAddr(),
+                                 BegExp, (BegExp+1));
+      return;
+    }
+  }
   LValue LV = CGF.EmitLValue(E);
   EmitFinalDestCopy(E, LV);
 }
@@ -204,7 +221,6 @@ void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore) {
         CGF.CGM.getLangOptions().CPlusPlus ||
         (IgnoreResult && Ignore))
       return;
-
     // If the source is volatile, we must read from it; to do that, we need
     // some place to put it.
     Dest = CGF.CreateAggTemp(E->getType(), "agg.tmp");
@@ -222,6 +238,7 @@ void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore) {
                                                       SizeVal);
     return;
   }
+  
   // If the result of the assignment is used, copy the LHS there also.
   // FIXME: Pass VolatileDest as well.  I think we also need to merge volatile
   // from the source as well, as we can't eliminate it if either operand
index 2a88d339978a881c0180547c8e2e2391411ab259..3cebfe7f883b5f9918cbef6963617e62d244f4a3 100644 (file)
@@ -299,6 +299,7 @@ CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E,
     assert(getContext().hasSameUnqualifiedType(E->getType(),
                                                E->getArg(0)->getType()));
     if (E->getArg(0)->isTemporaryObject(getContext(), CD->getParent())) {
+      Dest.setCtorExpr(const_cast<CXXConstructExpr*>(E));
       EmitAggExpr(E->getArg(0), Dest);
       return;
     }
index a000b223311ea31f74a45f01285dfe2aeeb56eb4..fd82be75580b690cf3f8882b6920ec4f8d298224 100644 (file)
@@ -26,6 +26,7 @@ namespace llvm {
 namespace clang {
   class ObjCPropertyRefExpr;
   class ObjCImplicitSetterGetterRefExpr;
+  class CXXConstructExpr;
 
 namespace CodeGen {
   class CGBitFieldInfo;
@@ -335,6 +336,7 @@ public:
 class AggValueSlot {
   /// The address.
   llvm::Value *Addr;
+  CXXConstructExpr *CtorExpr;
   
   // Associated flags.
   bool VolatileFlag : 1;
@@ -347,6 +349,7 @@ public:
   static AggValueSlot ignored() {
     AggValueSlot AV;
     AV.Addr = 0;
+    AV.CtorExpr = 0;
     AV.VolatileFlag = AV.LifetimeFlag = AV.RequiresGCollection = 0;
     return AV;
   }
@@ -364,6 +367,7 @@ public:
                               bool RequiresGCollection=false) {
     AggValueSlot AV;
     AV.Addr = Addr;
+    AV.CtorExpr = 0;
     AV.VolatileFlag = Volatile;
     AV.LifetimeFlag = LifetimeExternallyManaged;
     AV.RequiresGCollection = RequiresGCollection;
@@ -375,7 +379,10 @@ public:
     return forAddr(LV.getAddress(), LV.isVolatileQualified(),
                    LifetimeExternallyManaged, RequiresGCollection);
   }
-
+  
+  void setCtorExpr(CXXConstructExpr *E) { CtorExpr = E; }
+  CXXConstructExpr *getCtorExpr() const { return CtorExpr; }
+  
   bool isLifetimeExternallyManaged() const {
     return LifetimeFlag;
   }
diff --git a/test/CodeGenCXX/stmtexpr-copy-init.cpp b/test/CodeGenCXX/stmtexpr-copy-init.cpp
new file mode 100644 (file)
index 0000000..8a460e3
--- /dev/null
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -emit-llvm -o - %s
+// rdar //8540501
+
+struct A
+{
+  int i;
+  A (int j) : i(j) {}
+  A (const A &j) : i(j.i) {}
+  A& operator= (const A &j) { i = j.i; return *this; }
+};
+
+A foo(int j)
+{
+  return ({ j ? A(1) : A(0); });
+}
+
+int main()
+{
+  return foo(1).i-1;
+}
+
+void foo2()
+{
+  A b = ({ A a(1); a; });
+}
+