]> granicus.if.org Git - clang/commitdiff
Use static storage duration for file-scope compound literals, even when they
authorRichard Smith <richard-llvm@metafoo.co.uk>
Tue, 22 Nov 2011 22:48:32 +0000 (22:48 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Tue, 22 Nov 2011 22:48:32 +0000 (22:48 +0000)
appear in non-constant initializers in C++.

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

lib/CodeGen/CGExpr.cpp
lib/CodeGen/CGExprConstant.cpp
lib/CodeGen/CodeGenModule.h
test/CodeGenCXX/compound-literals.cpp

index a35bb5f7118f0ba3b315c423e949edc9b0d22bed..e4f81b7a3f4c5640b7025cee31af57111b919e82 100644 (file)
@@ -1947,6 +1947,11 @@ CodeGenFunction::EmitLValueForFieldInitialization(llvm::Value *BaseValue,
 }
 
 LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr *E){
+  if (E->isFileScope()) {
+    llvm::Value *GlobalPtr = CGM.GetAddrOfConstantCompoundLiteral(E);
+    return MakeAddrLValue(GlobalPtr, E->getType());
+  }
+
   llvm::Value *DeclPtr = CreateMemTemp(E->getType(), ".compoundliteral");
   const Expr *InitExpr = E->getInitializer();
   LValue Result = MakeAddrLValue(DeclPtr, E->getType());
index 7485c71d7db6250c3db94b5a566151920153c4b5..5d436619d75b42298f881e7bebb16b58ba2a2c77 100644 (file)
@@ -1087,6 +1087,12 @@ llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E,
   return C;
 }
 
+llvm::Constant *
+CodeGenModule::GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *E) {
+  assert(E->isFileScope() && "not a file-scope compound literal expr");
+  return ConstExprEmitter(*this, 0).EmitLValue(E);
+}
+
 static uint64_t getFieldOffset(ASTContext &C, const FieldDecl *field) {
   const ASTRecordLayout &layout = C.getASTRecordLayout(field->getParent());
   return layout.getFieldOffset(field->getFieldIndex());
index 7b82819af3db93143346136a8a40411cbc967ee7..4c31737b29621a9b346917708ab32f2d8032b702 100644 (file)
@@ -599,6 +599,10 @@ public:
   llvm::Constant *GetAddrOfConstantCString(const std::string &str,
                                            const char *GlobalName=0,
                                            unsigned Alignment=1);
+
+  /// GetAddrOfConstantCompoundLiteral - Returns a pointer to a constant global
+  /// variable for the given file-scope compound literal expression.
+  llvm::Constant *GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr*E);
   
   /// \brief Retrieve the record type that describes the state of an
   /// Objective-C fast enumeration loop (for..in).
index f520ff99516556f9e69a595b9ca02b08c8fa96be..aa9ae3cacfd2233ad44dda86aceb27e1d58d10a7 100644 (file)
@@ -37,3 +37,8 @@ int g() {
   // CHECK-NEXT: ret i32 [[A0]]
   return v[0];
 }
+
+struct Z { int i[3]; };
+int *p = (Z){ {1, 2, 3} }.i;
+// CHECK: define {{.*}}__cxx_global_var_init()
+// CHECK: store i32* getelementptr inbounds (%struct.Z* @.compoundliteral, i32 0, i32 0, i32 0), i32** @p, align 8