Hi,

The attached patch fixes the compound-literals-in-C++ extension to always use
static storage duration for file-scope compound literals, as per GCC's
documentation for this extension. Currently we only give them static storage
duration if the initializer in which they appear is a constant.

OK to commit?

Thanks,
Richard
Index: test/CodeGenCXX/compound-literals.cpp
===================================================================
--- test/CodeGenCXX/compound-literals.cpp	(revision 145005)
+++ test/CodeGenCXX/compound-literals.cpp	(working copy)
@@ -37,3 +37,8 @@
   // 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
Index: lib/CodeGen/CGExprConstant.cpp
===================================================================
--- lib/CodeGen/CGExprConstant.cpp	(revision 145005)
+++ lib/CodeGen/CGExprConstant.cpp	(working copy)
@@ -1087,6 +1087,12 @@
   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: lib/CodeGen/CodeGenModule.h
===================================================================
--- lib/CodeGen/CodeGenModule.h	(revision 145005)
+++ lib/CodeGen/CodeGenModule.h	(working copy)
@@ -599,6 +599,10 @@
   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: lib/CodeGen/CGExpr.cpp
===================================================================
--- lib/CodeGen/CGExpr.cpp	(revision 145005)
+++ lib/CodeGen/CGExpr.cpp	(working copy)
@@ -1947,6 +1947,11 @@
 }
 
 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());
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to