[cfe-commits] r45966 - in /cfe/trunk: AST/Expr.cpp AST/StmtSerialization.cpp Driver/RewriteTest.cpp Sema/SemaExpr.cpp include/clang/AST/Expr.h test/Sema/compound-literal.c

Steve Naroff snaroff at apple.com
Mon Jan 14 10:19:28 PST 2008


Author: snaroff
Date: Mon Jan 14 12:19:28 2008
New Revision: 45966

URL: http://llvm.org/viewvc/llvm-project?rev=45966&view=rev
Log:

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!


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

Modified: cfe/trunk/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/Expr.cpp?rev=45966&r1=45965&r2=45966&view=diff

==============================================================================
--- cfe/trunk/AST/Expr.cpp (original)
+++ cfe/trunk/AST/Expr.cpp Mon Jan 14 12:19:28 2008
@@ -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))

Modified: cfe/trunk/AST/StmtSerialization.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/StmtSerialization.cpp?rev=45966&r1=45965&r2=45966&view=diff

==============================================================================
--- cfe/trunk/AST/StmtSerialization.cpp (original)
+++ cfe/trunk/AST/StmtSerialization.cpp Mon Jan 14 12:19:28 2008
@@ -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 {

Modified: cfe/trunk/Driver/RewriteTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/RewriteTest.cpp?rev=45966&r1=45965&r2=45966&view=diff

==============================================================================
--- cfe/trunk/Driver/RewriteTest.cpp (original)
+++ cfe/trunk/Driver/RewriteTest.cpp Mon Jan 14 12:19:28 2008
@@ -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()), 

Modified: cfe/trunk/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaExpr.cpp?rev=45966&r1=45965&r2=45966&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/Sema/SemaExpr.cpp Mon Jan 14 12:19:28 2008
@@ -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.

Modified: cfe/trunk/include/clang/AST/Expr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Expr.h?rev=45966&r1=45965&r2=45966&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Mon Jan 14 12:19:28 2008
@@ -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; }
   

Modified: cfe/trunk/test/Sema/compound-literal.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/compound-literal.c?rev=45966&r1=45965&r2=45966&view=diff

==============================================================================
--- cfe/trunk/test/Sema/compound-literal.c (original)
+++ cfe/trunk/test/Sema/compound-literal.c Mon Jan 14 12:19:28 2008
@@ -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) {





More information about the cfe-commits mailing list