[cfe-commits] r92867 - in /cfe/trunk: include/clang/AST/Expr.h lib/AST/Expr.cpp lib/Frontend/PCHReaderStmt.cpp lib/Sema/SemaInit.cpp

Douglas Gregor dgregor at apple.com
Wed Jan 6 15:17:19 PST 2010


Author: dgregor
Date: Wed Jan  6 17:17:19 2010
New Revision: 92867

URL: http://llvm.org/viewvc/llvm-project?rev=92867&view=rev
Log:
Move the allocation of designators in DesignatedInitExpr to the
ASTContext. Fixes <rdar://problem/7495428>.

Modified:
    cfe/trunk/include/clang/AST/Expr.h
    cfe/trunk/lib/AST/Expr.cpp
    cfe/trunk/lib/Frontend/PCHReaderStmt.cpp
    cfe/trunk/lib/Sema/SemaInit.cpp

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

==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Wed Jan  6 17:17:19 2010
@@ -2557,7 +2557,7 @@
   unsigned NumSubExprs : 16;
 
 
-  DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
+  DesignatedInitExpr(ASTContext &C, QualType Ty, unsigned NumDesignators,
                      const Designator *Designators,
                      SourceLocation EqualOrColonLoc, bool GNUSyntax,
                      Expr **IndexExprs, unsigned NumIndexExprs,
@@ -2570,6 +2570,8 @@
 protected:
   virtual void DoDestroy(ASTContext &C);
 
+  void DestroyDesignators(ASTContext &C);
+  
 public:
   /// A field designator, e.g., ".x".
   struct FieldDesignator {
@@ -2737,7 +2739,8 @@
 
   Designator *getDesignator(unsigned Idx) { return &designators_begin()[Idx]; }
 
-  void setDesignators(const Designator *Desigs, unsigned NumDesigs);
+  void setDesignators(ASTContext &C, const Designator *Desigs, 
+                      unsigned NumDesigs);
 
   Expr *getArrayIndex(const Designator& D);
   Expr *getArrayRangeStart(const Designator& D);
@@ -2784,7 +2787,7 @@
 
   /// \brief Replaces the designator at index @p Idx with the series
   /// of designators in [First, Last).
-  void ExpandDesignator(unsigned Idx, const Designator *First,
+  void ExpandDesignator(ASTContext &C, unsigned Idx, const Designator *First,
                         const Designator *Last);
 
   virtual SourceRange getSourceRange() const;

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

==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Wed Jan  6 17:17:19 2010
@@ -2154,7 +2154,8 @@
     return getField()->getIdentifier();
 }
 
-DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
+DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty, 
+                                       unsigned NumDesignators,
                                        const Designator *Designators,
                                        SourceLocation EqualOrColonLoc,
                                        bool GNUSyntax,
@@ -2165,7 +2166,7 @@
          Init->isTypeDependent(), Init->isValueDependent()),
     EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
     NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
-  this->Designators = new Designator[NumDesignators];
+  this->Designators = new (C) Designator[NumDesignators];
 
   // Record the initializer itself.
   child_iterator Child = child_begin();
@@ -2210,7 +2211,7 @@
                            bool UsesColonSyntax, Expr *Init) {
   void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
                          sizeof(Stmt *) * (NumIndexExprs + 1), 8);
-  return new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
+  return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
                                       ColonOrEqualLoc, UsesColonSyntax,
                                       IndexExprs, NumIndexExprs, Init);
 }
@@ -2222,12 +2223,12 @@
   return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
 }
 
-void DesignatedInitExpr::setDesignators(const Designator *Desigs,
+void DesignatedInitExpr::setDesignators(ASTContext &C,
+                                        const Designator *Desigs,
                                         unsigned NumDesigs) {
-  if (Designators)
-    delete [] Designators;
+  DestroyDesignators(C);
 
-  Designators = new Designator[NumDesigs];
+  Designators = new (C) Designator[NumDesigs];
   NumDesignators = NumDesigs;
   for (unsigned I = 0; I != NumDesigs; ++I)
     Designators[I] = Desigs[I];
@@ -2276,7 +2277,7 @@
 
 /// \brief Replaces the designator at index @p Idx with the series
 /// of designators in [First, Last).
-void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
+void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
                                           const Designator *First,
                                           const Designator *Last) {
   unsigned NumNewDesignators = Last - First;
@@ -2292,21 +2293,28 @@
   }
 
   Designator *NewDesignators
-    = new Designator[NumDesignators - 1 + NumNewDesignators];
+    = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
   std::copy(Designators, Designators + Idx, NewDesignators);
   std::copy(First, Last, NewDesignators + Idx);
   std::copy(Designators + Idx + 1, Designators + NumDesignators,
             NewDesignators + Idx + NumNewDesignators);
-  delete [] Designators;
+  DestroyDesignators(C);
   Designators = NewDesignators;
   NumDesignators = NumDesignators - 1 + NumNewDesignators;
 }
 
 void DesignatedInitExpr::DoDestroy(ASTContext &C) {
-  delete [] Designators;
+  DestroyDesignators(C);
   Expr::DoDestroy(C);
 }
 
+void DesignatedInitExpr::DestroyDesignators(ASTContext &C) {
+  for (unsigned I = 0; I != NumDesignators; ++I)
+    Designators[I].~Designator();
+  C.Deallocate(Designators);
+  Designators = 0;
+}
+
 ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
                              Expr **exprs, unsigned nexprs,
                              SourceLocation rparenloc)

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

==============================================================================
--- cfe/trunk/lib/Frontend/PCHReaderStmt.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReaderStmt.cpp Wed Jan  6 17:17:19 2010
@@ -616,7 +616,8 @@
     }
     }
   }
-  E->setDesignators(Designators.data(), Designators.size());
+  E->setDesignators(*Reader.getContext(), 
+                    Designators.data(), Designators.size());
 
   return NumSubExprs;
 }

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaInit.cpp (original)
+++ cfe/trunk/lib/Sema/SemaInit.cpp Wed Jan  6 17:17:19 2010
@@ -1136,7 +1136,7 @@
   // Expand the current designator into the set of replacement
   // designators, so we have a full subobject path down to where the
   // member of the anonymous struct/union is actually stored.
-  DIE->ExpandDesignator(DesigIdx, &Replacements[0],
+  DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0],
                         &Replacements[0] + Replacements.size());
 
   // Update FieldIter/FieldIndex;





More information about the cfe-commits mailing list