Record if a compound literal expression is @ file scope. This allows us to implement C99 6.5.2.5p6. This could have been done without modifying the AST (by checking the decl type and passing the info down to isContextExpr), however we concluded this is more desirable.

Bug/patch by Eli Friedman!



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@45966 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/AST/Expr.cpp b/AST/Expr.cpp
index 986f0ad..e2aa6ca 100644
--- a/AST/Expr.cpp
+++ b/AST/Expr.cpp
@@ -420,6 +420,8 @@
     return cast<ParenExpr>(this)->getSubExpr()->hasStaticStorage();
   case ImplicitCastExprClass:
     return cast<ImplicitCastExpr>(this)->getSubExpr()->hasStaticStorage();
+  case CompoundLiteralExprClass:
+    return cast<CompoundLiteralExpr>(this)->isFileScope();
   case DeclRefExprClass: {
     const Decl *D = cast<DeclRefExpr>(this)->getDecl();
     if (const VarDecl *VD = dyn_cast<VarDecl>(D))
diff --git a/AST/StmtSerialization.cpp b/AST/StmtSerialization.cpp
index 2dbb125..f0a1a38 100644
--- a/AST/StmtSerialization.cpp
+++ b/AST/StmtSerialization.cpp
@@ -392,13 +392,15 @@
   S.Emit(getType());
   S.Emit(getLParenLoc());
   S.EmitOwnedPtr(Init);
+  S.EmitBool(isFileScope());
 }
 
 CompoundLiteralExpr* CompoundLiteralExpr::CreateImpl(Deserializer& D) {
   QualType Q = QualType::ReadVal(D);
   SourceLocation L = SourceLocation::ReadVal(D);
   Expr* Init = D.ReadOwnedPtr<Expr>();
-  return new CompoundLiteralExpr(L, Q, Init);
+  bool fileScope = D.ReadBool();
+  return new CompoundLiteralExpr(L, Q, Init, fileScope);
 }
 
 void CompoundStmt::EmitImpl(Serializer& S) const {
diff --git a/Driver/RewriteTest.cpp b/Driver/RewriteTest.cpp
index 6f6bfc2..120ffce 100644
--- a/Driver/RewriteTest.cpp
+++ b/Driver/RewriteTest.cpp
@@ -1510,7 +1510,7 @@
   InitListExpr *ILE = new InitListExpr(SourceLocation(), 
                                        &InitExprs[0], InitExprs.size(), 
                                        SourceLocation());
-  CompoundLiteralExpr *StrRep = new CompoundLiteralExpr(CFConstantStrType, ILE);
+  CompoundLiteralExpr *StrRep = new CompoundLiteralExpr(CFConstantStrType, ILE, false);
   // struct NSConstantString *
   expType = Context->getPointerType(StrRep->getType());
   Unop = new UnaryOperator(StrRep, UnaryOperator::AddrOf, expType, 
@@ -1643,7 +1643,7 @@
                                            &InitExprs[0], InitExprs.size(), 
                                            SourceLocation());
       CompoundLiteralExpr *SuperRep = new CompoundLiteralExpr(SourceLocation(),
-                                                              superType, ILE);
+                                                              superType, ILE, false);
       // struct objc_super *
       Expr *Unop = new UnaryOperator(SuperRep, UnaryOperator::AddrOf,
                                Context->getPointerType(SuperRep->getType()), 
@@ -1696,7 +1696,7 @@
                                            &InitExprs[0], InitExprs.size(), 
                                            SourceLocation());
       CompoundLiteralExpr *SuperRep = new CompoundLiteralExpr(SourceLocation(),
-                                                              superType, ILE);
+                                                              superType, ILE, false);
       // struct objc_super *
       Expr *Unop = new UnaryOperator(SuperRep, UnaryOperator::AddrOf,
                                Context->getPointerType(SuperRep->getType()), 
diff --git a/Sema/SemaExpr.cpp b/Sema/SemaExpr.cpp
index ced6953..9f5e8e6 100644
--- a/Sema/SemaExpr.cpp
+++ b/Sema/SemaExpr.cpp
@@ -673,12 +673,13 @@
   // FIXME: add more semantic analysis (C99 6.5.2.5).
   if (CheckInitializerTypes(literalExpr, literalType))
     return true;
-    
-  if (!CurFunctionDecl && !CurMethodDecl) { // 6.5.2.5p3
+
+  bool isFileScope = !CurFunctionDecl && !CurMethodDecl;
+  if (isFileScope) { // 6.5.2.5p3
     if (CheckForConstantInitializer(literalExpr, literalType))
       return true;
   }
-  return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr);
+  return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr, isFileScope);
 }
 
 Action::ExprResult Sema::
@@ -1963,7 +1964,7 @@
   
   // Otherwise, create a compound literal expression as the base, and
   // iteratively process the offsetof designators.
-  Expr *Res = new CompoundLiteralExpr(SourceLocation(), ArgTy, 0);
+  Expr *Res = new CompoundLiteralExpr(SourceLocation(), ArgTy, 0, false);
   
   // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a
   // GCC extension, diagnose them.
diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h
index da18d09..9c34a7c 100644
--- a/include/clang/AST/Expr.h
+++ b/include/clang/AST/Expr.h
@@ -728,12 +728,15 @@
   /// synthesized compound expression.
   SourceLocation LParenLoc;
   Expr *Init;
+  bool FileScope;
 public:
-  CompoundLiteralExpr(SourceLocation lparenloc, QualType ty, Expr *init) : 
-    Expr(CompoundLiteralExprClass, ty), LParenLoc(lparenloc), Init(init) {}
+  CompoundLiteralExpr(SourceLocation lparenloc, QualType ty, Expr *init, bool fileScope) : 
+    Expr(CompoundLiteralExprClass, ty), LParenLoc(lparenloc), Init(init), FileScope(fileScope) {}
   
   const Expr *getInitializer() const { return Init; }
   Expr *getInitializer() { return Init; }
+
+  bool isFileScope() const { return FileScope; }
   
   SourceLocation getLParenLoc() const { return LParenLoc; }
   
diff --git a/test/Sema/compound-literal.c b/test/Sema/compound-literal.c
index 12ee9f9..1a03945 100644
--- a/test/Sema/compound-literal.c
+++ b/test/Sema/compound-literal.c
@@ -11,6 +11,9 @@
 static int *p2 = (int []){2,x}; // -expected-error {{initializer element is not constant}}
 static int *p3 = (int []){2,"x"}; // -expected-warning {{incompatible pointer to integer conversion initializing 'char *', expected 'int'}}
 
+typedef struct Test {int a;int b;} Test;
+static Test* ll = &(Test) {0,0};
+
 extern void fooFunc(struct foo *pfoo);
 
 int main(int argc, char **argv) {