]> granicus.if.org Git - clang/commitdiff
The type of a compound literal expression is not necessarily the same as the
authorJohn McCall <rjmccall@apple.com>
Tue, 19 Jan 2010 22:33:45 +0000 (22:33 +0000)
committerJohn McCall <rjmccall@apple.com>
Tue, 19 Jan 2010 22:33:45 +0000 (22:33 +0000)
type which was syntactically written.  Fixes PR 6080.

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

include/clang/AST/Expr.h
lib/Frontend/RewriteObjC.cpp
lib/Sema/SemaExpr.cpp
lib/Sema/TreeTransform.h
test/Sema/compound-literal.c

index 2852c19744256a4abf7a30989ffd9054e51d98b0..252781767169607cf49c84a0b3eb6107afda1740 100644 (file)
@@ -1462,14 +1462,17 @@ class CompoundLiteralExpr : public Expr {
   /// compound literal like "(int){4}".  This can be null if this is a
   /// synthesized compound expression.
   SourceLocation LParenLoc;
+
+  /// The type as written.  This can be an incomplete array type, in
+  /// which case the actual expression type will be different.
   TypeSourceInfo *TInfo;
   Stmt *Init;
   bool FileScope;
 public:
   // FIXME: Can compound literals be value-dependent?
   CompoundLiteralExpr(SourceLocation lparenloc, TypeSourceInfo *tinfo,
-                      Expr *init, bool fileScope)
-    : Expr(CompoundLiteralExprClass, tinfo->getType(),
+                      QualType T, Expr *init, bool fileScope)
+    : Expr(CompoundLiteralExprClass, T,
            tinfo->getType()->isDependentType(), false),
       LParenLoc(lparenloc), TInfo(tinfo), Init(init), FileScope(fileScope) {}
 
index 2e101f402b7b591d4acc06635f719a2a79e192c5..c0ad799c5a966836a096f89ff4095245c3a72c1c 100644 (file)
@@ -2516,8 +2516,8 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) {
                                              SourceLocation());
         TypeSourceInfo *superTInfo
           = Context->getTrivialTypeSourceInfo(superType);
-        SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(),
-                                                     superTInfo, ILE, false);
+        SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo,
+                                                     superType, ILE, false);
         // struct objc_super *
         SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf,
                                  Context->getPointerType(SuperRep->getType()),
@@ -2601,8 +2601,8 @@ Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) {
                                              SourceLocation());
         TypeSourceInfo *superTInfo
           = Context->getTrivialTypeSourceInfo(superType);
-        SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(),
-                                                     superTInfo, ILE, false);
+        SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo,
+                                                     superType, ILE, false);
       }
       MsgExprs.push_back(SuperRep);
     } else {
index f8f3e05f56e5722ad18b2ff29acbfa373574abe0..8c740dd1dbf6bfd0e183b0a2ea7111af7ae7a2de 100644 (file)
@@ -3707,7 +3707,7 @@ Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo,
 
   Result.release();
   
-  return Owned(new (Context) CompoundLiteralExpr(LParenLoc, TInfo,
+  return Owned(new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType,
                                                  literalExpr, isFileScope));
 }
 
@@ -4718,7 +4718,7 @@ static void ConstructTransparentUnion(ASTContext &C, Expr *&E,
   // Build a compound literal constructing a value of the transparent
   // union type from this initializer list.
   TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType);
-  E = new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo,
+  E = new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType,
                                   Initializer, false);
 }
 
index 587c93d1ad5fc5753aa9b6ccc7497eee318a1292..b2102afdfc42f4a4b2a92fef66a358f630f23a2f 100644 (file)
@@ -3867,6 +3867,10 @@ TreeTransform<Derived>::TransformCompoundLiteralExpr(CompoundLiteralExpr *E) {
       Init.get() == E->getInitializer())
     return SemaRef.Owned(E->Retain());
 
+  // Note: the expression type doesn't necessarily match the
+  // type-as-written, but that's okay, because it should always be
+  // derivable from the initializer.
+
   return getDerived().RebuildCompoundLiteralExpr(E->getLParenLoc(), NewT,
                                    /*FIXME:*/E->getInitializer()->getLocEnd(),
                                                  move(Init));
index a650d12de97385a0beea0f89e12c67aaa051322b..0c8ddd42d0c88eda274e64c5de775b2958bfface 100644 (file)
@@ -31,3 +31,6 @@ void IncompleteFunc(unsigned x) {
   (void){1,2,3}; // -expected-error {{variable has incomplete type}}
   (void(void)) { 0 }; // -expected-error{{illegal initializer type 'void (void)'}}
 }
+
+// PR6080
+int array[(sizeof(int[3]) == sizeof( (int[]) {0,1,2} )) ? 1 : -1];