[cfe-commits] r93752 - in /cfe/trunk: include/clang/AST/Expr.h lib/Frontend/PCHReaderStmt.cpp lib/Frontend/PCHWriterStmt.cpp lib/Frontend/RewriteObjC.cpp lib/Sema/Sema.h lib/Sema/SemaExpr.cpp lib/Sema/TreeTransform.h

John McCall rjmccall at apple.com
Mon Jan 18 11:35:47 PST 2010


Author: rjmccall
Date: Mon Jan 18 13:35:47 2010
New Revision: 93752

URL: http://llvm.org/viewvc/llvm-project?rev=93752&view=rev
Log:
Preserve type source information in compound literal expressions.
Patch by Enea Zaffanella!


Modified:
    cfe/trunk/include/clang/AST/Expr.h
    cfe/trunk/lib/Frontend/PCHReaderStmt.cpp
    cfe/trunk/lib/Frontend/PCHWriterStmt.cpp
    cfe/trunk/lib/Frontend/RewriteObjC.cpp
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/lib/Sema/TreeTransform.h

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

==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Mon Jan 18 13:35:47 2010
@@ -1462,14 +1462,16 @@
   /// compound literal like "(int){4}".  This can be null if this is a
   /// synthesized compound expression.
   SourceLocation LParenLoc;
+  TypeSourceInfo *TInfo;
   Stmt *Init;
   bool FileScope;
 public:
   // FIXME: Can compound literals be value-dependent?
-  CompoundLiteralExpr(SourceLocation lparenloc, QualType ty, Expr *init,
-                      bool fileScope)
-    : Expr(CompoundLiteralExprClass, ty, ty->isDependentType(), false),
-      LParenLoc(lparenloc), Init(init), FileScope(fileScope) {}
+  CompoundLiteralExpr(SourceLocation lparenloc, TypeSourceInfo *tinfo,
+                      Expr *init, bool fileScope)
+    : Expr(CompoundLiteralExprClass, tinfo->getType(),
+           tinfo->getType()->isDependentType(), false),
+      LParenLoc(lparenloc), TInfo(tinfo), Init(init), FileScope(fileScope) {}
 
   /// \brief Construct an empty compound literal.
   explicit CompoundLiteralExpr(EmptyShell Empty)
@@ -1485,6 +1487,9 @@
   SourceLocation getLParenLoc() const { return LParenLoc; }
   void setLParenLoc(SourceLocation L) { LParenLoc = L; }
 
+  TypeSourceInfo *getTypeSourceInfo() const { return TInfo; }
+  void setTypeSourceInfo(TypeSourceInfo* tinfo) { TInfo = tinfo; }
+
   virtual SourceRange getSourceRange() const {
     // FIXME: Init should never be null.
     if (!Init)

Modified: cfe/trunk/lib/Frontend/PCHReaderStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHReaderStmt.cpp?rev=93752&r1=93751&r2=93752&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/PCHReaderStmt.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReaderStmt.cpp Mon Jan 18 13:35:47 2010
@@ -533,6 +533,7 @@
 unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
   VisitExpr(E);
   E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
+  E->setTypeSourceInfo(Reader.GetTypeSourceInfo(Record, Idx));
   E->setInitializer(cast<Expr>(StmtStack.back()));
   E->setFileScope(Record[Idx++]);
   return 1;

Modified: cfe/trunk/lib/Frontend/PCHWriterStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHWriterStmt.cpp?rev=93752&r1=93751&r2=93752&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/PCHWriterStmt.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHWriterStmt.cpp Mon Jan 18 13:35:47 2010
@@ -496,6 +496,7 @@
 void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
   VisitExpr(E);
   Writer.AddSourceLocation(E->getLParenLoc(), Record);
+  Writer.AddTypeSourceInfo(E->getTypeSourceInfo(), Record);
   Writer.WriteSubStmt(E->getInitializer());
   Record.push_back(E->isFileScope());
   Code = pch::EXPR_COMPOUND_LITERAL;

Modified: cfe/trunk/lib/Frontend/RewriteObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/RewriteObjC.cpp?rev=93752&r1=93751&r2=93752&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/RewriteObjC.cpp (original)
+++ cfe/trunk/lib/Frontend/RewriteObjC.cpp Mon Jan 18 13:35:47 2010
@@ -2514,8 +2514,10 @@
         InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(),
                                              &InitExprs[0], InitExprs.size(),
                                              SourceLocation());
-        SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superType, ILE,
-                                           false);
+        TypeSourceInfo *superTInfo
+          = Context->getTrivialTypeSourceInfo(superType);
+        SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(),
+                                                     superTInfo, ILE, false);
         // struct objc_super *
         SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf,
                                  Context->getPointerType(SuperRep->getType()),
@@ -2597,7 +2599,10 @@
         InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(),
                                              &InitExprs[0], InitExprs.size(),
                                              SourceLocation());
-        SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superType, ILE, false);
+        TypeSourceInfo *superTInfo
+          = Context->getTrivialTypeSourceInfo(superType);
+        SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(),
+                                                     superTInfo, ILE, false);
       }
       MsgExprs.push_back(SuperRep);
     } else {

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=93752&r1=93751&r2=93752&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Mon Jan 18 13:35:47 2010
@@ -1666,13 +1666,18 @@
   OwningExprResult MaybeConvertParenListExprToParenExpr(Scope *S, ExprArg ME);
   OwningExprResult ActOnCastOfParenListExpr(Scope *S, SourceLocation LParenLoc,
                                             SourceLocation RParenLoc, ExprArg E,
-                                            QualType Ty);
+                                            TypeSourceInfo *TInfo);
 
   virtual OwningExprResult ActOnCompoundLiteral(SourceLocation LParenLoc,
                                                 TypeTy *Ty,
                                                 SourceLocation RParenLoc,
                                                 ExprArg Op);
 
+  OwningExprResult BuildCompoundLiteralExpr(SourceLocation LParenLoc,
+                                            TypeSourceInfo *TInfo,
+                                            SourceLocation RParenLoc,
+                                            ExprArg InitExpr);
+
   virtual OwningExprResult ActOnInitList(SourceLocation LParenLoc,
                                          MultiExprArg InitList,
                                          SourceLocation RParenLoc);

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Mon Jan 18 13:35:47 2010
@@ -3663,11 +3663,21 @@
 Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
                            SourceLocation RParenLoc, ExprArg InitExpr) {
   assert((Ty != 0) && "ActOnCompoundLiteral(): missing type");
-  
-  QualType literalType = GetTypeFromParser(Ty);  
-  
   // FIXME: put back this assert when initializers are worked out.
   //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression");
+
+  TypeSourceInfo *TInfo;
+  QualType literalType = GetTypeFromParser(Ty, &TInfo);
+  if (!TInfo)
+    TInfo = Context.getTrivialTypeSourceInfo(literalType);
+
+  return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, move(InitExpr));
+}
+
+Action::OwningExprResult
+Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo,
+                               SourceLocation RParenLoc, ExprArg InitExpr) {
+  QualType literalType = TInfo->getType();
   Expr *literalExpr = static_cast<Expr*>(InitExpr.get());
 
   if (literalType->isArrayType()) {
@@ -3703,8 +3713,7 @@
 
   Result.release();
   
-  // FIXME: Store the TInfo to preserve type information better.
-  return Owned(new (Context) CompoundLiteralExpr(LParenLoc, literalType,
+  return Owned(new (Context) CompoundLiteralExpr(LParenLoc, TInfo,
                                                  literalExpr, isFileScope));
 }
 
@@ -3912,13 +3921,13 @@
   TypeSourceInfo *castTInfo;
   QualType castType = GetTypeFromParser(Ty, &castTInfo);
   if (!castTInfo)
-    castTInfo = Context.getTrivialTypeSourceInfo(castType, SourceLocation());
+    castTInfo = Context.getTrivialTypeSourceInfo(castType);
 
   // If the Expr being casted is a ParenListExpr, handle it specially.
-  // FIXME: preserve type source info.
   Expr *castExpr = (Expr *)Op.get();
   if (isa<ParenListExpr>(castExpr))
-    return ActOnCastOfParenListExpr(S, LParenLoc, RParenLoc, move(Op),castType);
+    return ActOnCastOfParenListExpr(S, LParenLoc, RParenLoc, move(Op),
+                                    castTInfo);
 
   return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, move(Op));
 }
@@ -3973,8 +3982,9 @@
 Action::OwningExprResult
 Sema::ActOnCastOfParenListExpr(Scope *S, SourceLocation LParenLoc,
                                SourceLocation RParenLoc, ExprArg Op,
-                               QualType Ty) {
+                               TypeSourceInfo *TInfo) {
   ParenListExpr *PE = (ParenListExpr *)Op.get();
+  QualType Ty = TInfo->getType();
 
   // If this is an altivec initializer, '(' type ')' '(' init, ..., init ')'
   // then handle it as such.
@@ -3994,13 +4004,12 @@
     InitListExpr *E = new (Context) InitListExpr(LParenLoc, &initExprs[0],
                                                  initExprs.size(), RParenLoc);
     E->setType(Ty);
-    return ActOnCompoundLiteral(LParenLoc, Ty.getAsOpaquePtr(), RParenLoc,
-                                Owned(E));
+    return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, Owned(E));
   } else {
     // This is not an AltiVec-style cast, so turn the ParenListExpr into a
     // sequence of BinOp comma operators.
     Op = MaybeConvertParenListExprToParenExpr(S, move(Op));
-    return ActOnCastExpr(S, LParenLoc, Ty.getAsOpaquePtr(), RParenLoc,move(Op));
+    return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, move(Op));
   }
 }
 
@@ -4714,8 +4723,9 @@
 
   // Build a compound literal constructing a value of the transparent
   // union type from this initializer list.
-  E = new (C) CompoundLiteralExpr(SourceLocation(), UnionType, Initializer,
-                                  false);
+  TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType);
+  E = new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo,
+                                  Initializer, false);
 }
 
 Sema::AssignConvertType

Modified: cfe/trunk/lib/Sema/TreeTransform.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/TreeTransform.h?rev=93752&r1=93751&r2=93752&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/TreeTransform.h (original)
+++ cfe/trunk/lib/Sema/TreeTransform.h Mon Jan 18 13:35:47 2010
@@ -1027,11 +1027,11 @@
   /// By default, performs semantic analysis to build the new expression.
   /// Subclasses may override this routine to provide different behavior.
   OwningExprResult RebuildCompoundLiteralExpr(SourceLocation LParenLoc,
-                                              QualType T,
+                                              TypeSourceInfo *TInfo,
                                               SourceLocation RParenLoc,
                                               ExprArg Init) {
-    return getSema().ActOnCompoundLiteral(LParenLoc, T.getAsOpaquePtr(),
-                                          RParenLoc, move(Init));
+    return getSema().BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc,
+                                              move(Init));
   }
 
   /// \brief Build a new extended vector element access expression.
@@ -3853,28 +3853,21 @@
 template<typename Derived>
 Sema::OwningExprResult
 TreeTransform<Derived>::TransformCompoundLiteralExpr(CompoundLiteralExpr *E) {
-  QualType T;
-  {
-    // FIXME: Source location isn't quite accurate.
-    SourceLocation FakeTypeLoc
-      = SemaRef.PP.getLocForEndOfToken(E->getLParenLoc());
-    TemporaryBase Rebase(*this, FakeTypeLoc, DeclarationName());
-
-    T = getDerived().TransformType(E->getType());
-    if (T.isNull())
-      return SemaRef.ExprError();
-  }
+  TypeSourceInfo *OldT = E->getTypeSourceInfo();
+  TypeSourceInfo *NewT = getDerived().TransformType(OldT);
+  if (!NewT)
+    return SemaRef.ExprError();
 
   OwningExprResult Init = getDerived().TransformExpr(E->getInitializer());
   if (Init.isInvalid())
     return SemaRef.ExprError();
 
   if (!getDerived().AlwaysRebuild() &&
-      T == E->getType() &&
+      OldT == NewT &&
       Init.get() == E->getInitializer())
     return SemaRef.Owned(E->Retain());
 
-  return getDerived().RebuildCompoundLiteralExpr(E->getLParenLoc(), T,
+  return getDerived().RebuildCompoundLiteralExpr(E->getLParenLoc(), NewT,
                                    /*FIXME:*/E->getInitializer()->getLocEnd(),
                                                  move(Init));
 }





More information about the cfe-commits mailing list