]> granicus.if.org Git - clang/commitdiff
Record if a compound literal expression is @ file scope. This allows us to implement...
authorSteve Naroff <snaroff@apple.com>
Mon, 14 Jan 2008 18:19:28 +0000 (18:19 +0000)
committerSteve Naroff <snaroff@apple.com>
Mon, 14 Jan 2008 18:19:28 +0000 (18:19 +0000)
Bug/patch by Eli Friedman!

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

AST/Expr.cpp
AST/StmtSerialization.cpp
Driver/RewriteTest.cpp
Sema/SemaExpr.cpp
include/clang/AST/Expr.h
test/Sema/compound-literal.c

index 986f0ad95f6bffd6135bde6a8911c25ceb99d76f..e2aa6ca405586d9d86dc145464a5eeaa37fa428a 100644 (file)
@@ -420,6 +420,8 @@ bool Expr::hasStaticStorage() const {
     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))
index 2dbb125f903b6bfc5da70c1122eca89a398abd2c..f0a1a385c6558d6e095a9816ecc3c765f99e3cb4 100644 (file)
@@ -392,13 +392,15 @@ void CompoundLiteralExpr::EmitImpl(Serializer& S) const {
   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 {
index 6f6bfc23546d7e980c7f55cec56c8f56dcacf6f1..120ffce6851384a443b196fbac596b793635dfa7 100644 (file)
@@ -1510,7 +1510,7 @@ Stmt *RewriteTest::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) {
   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 @@ Stmt *RewriteTest::SynthMessageExpr(ObjCMessageExpr *Exp) {
                                            &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 @@ Stmt *RewriteTest::SynthMessageExpr(ObjCMessageExpr *Exp) {
                                            &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()), 
index ced6953c7d09c0365dd1e4b1a17e8d2238757901..9f5e8e68b89266d5552ec0aaff1d7ad60f4d2b3f 100644 (file)
@@ -673,12 +673,13 @@ ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
   // 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 @@ Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
   
   // 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.
index da18d09dd1f6c362700d3ea232403973579e4ef6..9c34a7cb2ad1ea0e3fad6e2b3f331fdcfa776c61 100644 (file)
@@ -728,12 +728,15 @@ class CompoundLiteralExpr : public Expr {
   /// 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; }
   
index 12ee9f9300ac32d105cce4ca11278b714f252ccb..1a0394578a9fcd4e3f420c33e2db92bb008b1c21 100644 (file)
@@ -11,6 +11,9 @@ static int x = (int){1}; // -expected-error {{initializer element is not constan
 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) {