From 03e8bdc07e8ec067a3a518727b5bad4507504ecf Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Wed, 6 Jan 2010 23:17:19 +0000 Subject: [PATCH] Move the allocation of designators in DesignatedInitExpr to the ASTContext. Fixes . llvm-svn: 92867 --- clang/include/clang/AST/Expr.h | 9 ++++++--- clang/lib/AST/Expr.cpp | 30 ++++++++++++++++++---------- clang/lib/Frontend/PCHReaderStmt.cpp | 3 ++- clang/lib/Sema/SemaInit.cpp | 2 +- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h index 98ecda9b8358..cfbae9fb2cbf 100644 --- a/clang/include/clang/AST/Expr.h +++ b/clang/include/clang/AST/Expr.h @@ -2557,7 +2557,7 @@ private: 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 @@ private: protected: virtual void DoDestroy(ASTContext &C); + void DestroyDesignators(ASTContext &C); + public: /// A field designator, e.g., ".x". struct FieldDesignator { @@ -2737,7 +2739,8 @@ public: 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 @@ public: /// \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; diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp index 04a6abca87c9..4c3046bed10b 100644 --- a/clang/lib/AST/Expr.cpp +++ b/clang/lib/AST/Expr.cpp @@ -2154,7 +2154,8 @@ IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() { 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 @@ DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators, 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 @@ DesignatedInitExpr::Create(ASTContext &C, Designator *Designators, 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 @@ DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C, 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 @@ Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) { /// \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 @@ void DesignatedInitExpr::ExpandDesignator(unsigned Idx, } 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) diff --git a/clang/lib/Frontend/PCHReaderStmt.cpp b/clang/lib/Frontend/PCHReaderStmt.cpp index c108f549ab2a..138f1e141b09 100644 --- a/clang/lib/Frontend/PCHReaderStmt.cpp +++ b/clang/lib/Frontend/PCHReaderStmt.cpp @@ -616,7 +616,8 @@ unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) { } } } - E->setDesignators(Designators.data(), Designators.size()); + E->setDesignators(*Reader.getContext(), + Designators.data(), Designators.size()); return NumSubExprs; } diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp index 3ef51561ffe2..54d94f86ceca 100644 --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -1136,7 +1136,7 @@ static void ExpandAnonymousFieldDesignator(Sema &SemaRef, // 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;