[cfe-commits] r133235 - in /cfe/trunk: lib/CodeGen/CGExprAgg.cpp lib/Sema/SemaExpr.cpp test/CodeGenCXX/compound-literals.cpp

Douglas Gregor dgregor at apple.com
Thu Jun 16 21:59:12 PDT 2011


Author: dgregor
Date: Thu Jun 16 23:59:12 2011
New Revision: 133235

URL: http://llvm.org/viewvc/llvm-project?rev=133235&view=rev
Log:
Implement proper support for generating code for compound literals in
C++, which means:
  - binding the temporary as needed in Sema, so that we generate the
  appropriate call to the destructor, and
  - emitting the compound literal into the appropriate location for
  the aggregate, rather than trying to emit it as a temporary and
  memcpy() it.

Fixes PR10138 / <rdar://problem/9615901>.


Added:
    cfe/trunk/test/CodeGenCXX/compound-literals.cpp   (with props)
Modified:
    cfe/trunk/lib/CodeGen/CGExprAgg.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp

Modified: cfe/trunk/lib/CodeGen/CGExprAgg.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprAgg.cpp?rev=133235&r1=133234&r2=133235&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprAgg.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprAgg.cpp Thu Jun 16 23:59:12 2011
@@ -91,9 +91,7 @@
   void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
   void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
   void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
-  void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
-    EmitAggLoadOfLValue(E);
-  }
+  void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
   void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
     EmitAggLoadOfLValue(E);
   }
@@ -247,6 +245,13 @@
   EmitFinalDestCopy(e, CGF.getOpaqueLValueMapping(e));
 }
 
+void
+AggExprEmitter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
+  AggValueSlot Slot = EnsureSlot(E->getType());
+  CGF.EmitAggExpr(E->getInitializer(), Slot);
+}
+
+
 void AggExprEmitter::VisitCastExpr(CastExpr *E) {
   switch (E->getCastKind()) {
   case CK_Dynamic: {

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=133235&r1=133234&r2=133235&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Thu Jun 16 23:59:12 2011
@@ -5309,8 +5309,9 @@
   // In C, compound literals are l-values for some reason.
   ExprValueKind VK = getLangOptions().CPlusPlus ? VK_RValue : VK_LValue;
 
-  return Owned(new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType,
-                                                 VK, literalExpr, isFileScope));
+  return MaybeBindToTemporary(
+           new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType,
+                                             VK, literalExpr, isFileScope));
 }
 
 ExprResult

Added: cfe/trunk/test/CodeGenCXX/compound-literals.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/compound-literals.cpp?rev=133235&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenCXX/compound-literals.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/compound-literals.cpp Thu Jun 16 23:59:12 2011
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -emit-llvm -o - %s | FileCheck %s
+
+struct X {
+  X();
+  X(const X&);
+  X(const char*);
+  ~X();
+};
+
+struct Y { 
+  int i;
+  X x;
+};
+
+// CHECK: define i32 @_Z1fv()
+int f() {
+  // CHECK: [[LVALUE:%[a-z0-9.]+]] = alloca
+  // CHECK-NEXT: [[I:%[a-z0-9]+]] = getelementptr inbounds {{.*}}* [[LVALUE]], i32 0, i32 0
+  // CHECK-NEXT: store i32 17, i32* [[I]]
+  // CHECK-NEXT: [[X:%[a-z0-9]+]] = getelementptr inbounds {{.*}} [[LVALUE]], i32 0, i32 1
+  // CHECK-NEXT: call void @_ZN1XC1EPKc({{.*}}[[X]]
+  // CHECK-NEXT: [[I:%[a-z0-9]+]] = getelementptr inbounds {{.*}} [[LVALUE]], i32 0, i32 0
+  // CHECK-NEXT: [[RESULT:%[a-z0-9]+]] = load i32*
+  // CHECK-NEXT: call void @_ZN1YD1Ev
+  // CHECK-NEXT: ret i32 [[RESULT]]
+  return ((Y){17, "seventeen"}).i;
+}

Propchange: cfe/trunk/test/CodeGenCXX/compound-literals.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/CodeGenCXX/compound-literals.cpp
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/test/CodeGenCXX/compound-literals.cpp
------------------------------------------------------------------------------
    svn:mime-type = text/plain





More information about the cfe-commits mailing list