From bbfd68dd5288c435cfd6aef1264263e5f856958d Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Wed, 23 Dec 2009 21:13:52 +0000 Subject: [PATCH] Tidy up ~ASTContext a bit by turning orphan compound statements into for loops. Also do not manually free the Type objects when the 'FreeMemory' flag is set, as they will be deallocated when the BumpPtrAllocator is destroyed. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@92047 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/AST/ASTContext.cpp | 51 +++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp index 0e36a0ad1c..bba169938b 100644 --- a/lib/AST/ASTContext.cpp +++ b/lib/AST/ASTContext.cpp @@ -56,44 +56,43 @@ ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, } ASTContext::~ASTContext() { - // Deallocate all the types. - while (!Types.empty()) { - Types.back()->Destroy(*this); - Types.pop_back(); - } + if (FreeMemory) { + // Deallocate all the types. + while (!Types.empty()) { + Types.back()->Destroy(*this); + Types.pop_back(); + } - { - llvm::FoldingSet::iterator - I = ExtQualNodes.begin(), E = ExtQualNodes.end(); - while (I != E) + for (llvm::FoldingSet::iterator + I = ExtQualNodes.begin(), E = ExtQualNodes.end(); I != E; ) { + // Increment in loop to prevent using deallocated memory. Deallocate(&*I++); - } - - { - llvm::DenseMap::iterator - I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); - while (I != E) { - ASTRecordLayout *R = const_cast((I++)->second); - delete R; } } - { - llvm::DenseMap::iterator - I = ObjCLayouts.begin(), E = ObjCLayouts.end(); - while (I != E) { - ASTRecordLayout *R = const_cast((I++)->second); - delete R; - } + for (llvm::DenseMap::iterator + I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { + // Increment in loop to prevent using deallocated memory. + ASTRecordLayout *R = const_cast((I++)->second); + delete R; + } + + for (llvm::DenseMap::iterator + I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) { + // Increment in loop to prevent using deallocated memory. + ASTRecordLayout *R = const_cast((I++)->second); + delete R; } // Destroy nested-name-specifiers. for (llvm::FoldingSet::iterator NNS = NestedNameSpecifiers.begin(), NNSEnd = NestedNameSpecifiers.end(); - NNS != NNSEnd; - /* Increment in loop */) + NNS != NNSEnd; ) { + // Increment in loop to prevent using deallocated memory. (*NNS++).Destroy(*this); + } if (GlobalNestedNameSpecifier) GlobalNestedNameSpecifier->Destroy(*this);