From: George Burgess IV Date: Tue, 14 Feb 2017 05:37:36 +0000 (+0000) Subject: Add a destruct-on-exit function to ASTContext. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0b0a12c5693701cc54458d3c3981850547a9840e;p=clang Add a destruct-on-exit function to ASTContext. It looks like the only use of AddDeallocation is to indirectly call the destructors of objects. In one case I found (TypeAliasTemplateDecl::Common), the destructor is a nop, so registering it to run later seems pointless. All of the other *::Common types have non-trivial dtors, so deleting the useless AddDeallocation felt somewhat fragile. Happy to kill it + turn the is_trivial_dtor check into a static_assert if people think that'd be better. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@295029 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/ASTContext.h b/include/clang/AST/ASTContext.h index 6811281570..4366b79c1c 100644 --- a/include/clang/AST/ASTContext.h +++ b/include/clang/AST/ASTContext.h @@ -66,6 +66,7 @@ #include #include #include +#include #include #include @@ -2487,6 +2488,16 @@ public: /// when it is called. void AddDeallocation(void (*Callback)(void*), void *Data); + /// If T isn't trivially destructible, calls AddDeallocation to register it + /// for destruction. + template + void addDestruction(T *Ptr) { + if (!std::is_trivially_destructible::value) { + auto DestroyPtr = [](void *V) { static_cast(V)->~T(); }; + AddDeallocation(DestroyPtr, Ptr); + } + } + GVALinkage GetGVALinkageForFunction(const FunctionDecl *FD) const; GVALinkage GetGVALinkageForVariable(const VarDecl *VD); diff --git a/include/clang/AST/DeclTemplate.h b/include/clang/AST/DeclTemplate.h index 6f81fcb4ad..e83ec3cfcf 100644 --- a/include/clang/AST/DeclTemplate.h +++ b/include/clang/AST/DeclTemplate.h @@ -937,8 +937,6 @@ SpecEntryTraits { /// Declaration of a template function. class FunctionTemplateDecl : public RedeclarableTemplateDecl { - static void DeallocateCommon(void *Ptr); - protected: /// \brief Data that is common to all of the declarations of a given /// function template. @@ -2035,8 +2033,6 @@ public: /// Declaration of a class template. class ClassTemplateDecl : public RedeclarableTemplateDecl { - static void DeallocateCommon(void *Ptr); - protected: /// \brief Data that is common to all of the declarations of a given /// class template. @@ -2330,8 +2326,6 @@ public: /// template \ using V = std::map>; /// \endcode class TypeAliasTemplateDecl : public RedeclarableTemplateDecl { - static void DeallocateCommon(void *Ptr); - protected: typedef CommonBase Common; @@ -2856,8 +2850,6 @@ public: /// Declaration of a variable template. class VarTemplateDecl : public RedeclarableTemplateDecl { - static void DeallocateCommon(void *Ptr); - protected: /// \brief Data that is common to all of the declarations of a given /// variable template. diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp index 81f08787d5..2927199506 100644 --- a/lib/AST/Decl.cpp +++ b/lib/AST/Decl.cpp @@ -2143,13 +2143,6 @@ APValue *VarDecl::evaluateValue() const { return evaluateValue(Notes); } -namespace { -// Destroy an APValue that was allocated in an ASTContext. -void DestroyAPValue(void* UntypedValue) { - static_cast(UntypedValue)->~APValue(); -} -} // namespace - APValue *VarDecl::evaluateValue( SmallVectorImpl &Notes) const { EvaluatedStmt *Eval = ensureEvaluatedStmt(); @@ -2181,7 +2174,7 @@ APValue *VarDecl::evaluateValue( if (!Result) Eval->Evaluated = APValue(); else if (Eval->Evaluated.needsCleanup()) - getASTContext().AddDeallocation(DestroyAPValue, &Eval->Evaluated); + getASTContext().addDestruction(&Eval->Evaluated); Eval->IsEvaluating = false; Eval->WasEvaluated = true; diff --git a/lib/AST/DeclTemplate.cpp b/lib/AST/DeclTemplate.cpp index 22f6e18d56..00a6739478 100644 --- a/lib/AST/DeclTemplate.cpp +++ b/lib/AST/DeclTemplate.cpp @@ -208,10 +208,6 @@ void RedeclarableTemplateDecl::addSpecializationImpl( // FunctionTemplateDecl Implementation //===----------------------------------------------------------------------===// -void FunctionTemplateDecl::DeallocateCommon(void *Ptr) { - static_cast(Ptr)->~Common(); -} - FunctionTemplateDecl *FunctionTemplateDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, @@ -231,7 +227,7 @@ FunctionTemplateDecl *FunctionTemplateDecl::CreateDeserialized(ASTContext &C, RedeclarableTemplateDecl::CommonBase * FunctionTemplateDecl::newCommon(ASTContext &C) const { Common *CommonPtr = new (C) Common; - C.AddDeallocation(DeallocateCommon, CommonPtr); + C.addDestruction(CommonPtr); return CommonPtr; } @@ -288,10 +284,6 @@ ArrayRef FunctionTemplateDecl::getInjectedTemplateArgs() { // ClassTemplateDecl Implementation //===----------------------------------------------------------------------===// -void ClassTemplateDecl::DeallocateCommon(void *Ptr) { - static_cast(Ptr)->~Common(); -} - ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, @@ -348,7 +340,7 @@ ClassTemplateDecl::getPartialSpecializations() { RedeclarableTemplateDecl::CommonBase * ClassTemplateDecl::newCommon(ASTContext &C) const { Common *CommonPtr = new (C) Common; - C.AddDeallocation(DeallocateCommon, CommonPtr); + C.addDestruction(CommonPtr); return CommonPtr; } @@ -888,13 +880,10 @@ TypeAliasTemplateDecl *TypeAliasTemplateDecl::CreateDeserialized(ASTContext &C, DeclarationName(), nullptr, nullptr); } -void TypeAliasTemplateDecl::DeallocateCommon(void *Ptr) { - static_cast(Ptr)->~Common(); -} RedeclarableTemplateDecl::CommonBase * TypeAliasTemplateDecl::newCommon(ASTContext &C) const { Common *CommonPtr = new (C) Common; - C.AddDeallocation(DeallocateCommon, CommonPtr); + C.addDestruction(CommonPtr); return CommonPtr; } @@ -915,10 +904,6 @@ ClassScopeFunctionSpecializationDecl::CreateDeserialized(ASTContext &C, // VarTemplateDecl Implementation //===----------------------------------------------------------------------===// -void VarTemplateDecl::DeallocateCommon(void *Ptr) { - static_cast(Ptr)->~Common(); -} - VarTemplateDecl *VarTemplateDecl::getDefinition() { VarTemplateDecl *CurD = this; while (CurD) { @@ -974,7 +959,7 @@ VarTemplateDecl::getPartialSpecializations() { RedeclarableTemplateDecl::CommonBase * VarTemplateDecl::newCommon(ASTContext &C) const { Common *CommonPtr = new (C) Common; - C.AddDeallocation(DeallocateCommon, CommonPtr); + C.addDestruction(CommonPtr); return CommonPtr; }