diff options
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/AST/Decl.cpp | 54 | ||||
-rw-r--r-- | clang/lib/AST/Stmt.cpp | 9 | ||||
-rw-r--r-- | clang/lib/AST/TranslationUnit.cpp | 13 | ||||
-rw-r--r-- | clang/lib/Sema/ParseAST.cpp | 11 |
4 files changed, 39 insertions, 48 deletions
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index 1a7eecac32a..a831c4ff9a8 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -16,6 +16,8 @@ #include "clang/AST/Attr.h" #include "clang/Basic/IdentifierTable.h" #include "llvm/ADT/DenseMap.h" + +#include <stdio.h> using namespace clang; //===----------------------------------------------------------------------===// @@ -314,8 +316,10 @@ Decl::~Decl() { DeclAttrMapTy::iterator it = DeclAttrs->find(this); assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!"); - delete it->second; + // FIXME: Properly release attributes. + // delete it->second; DeclAttrs->erase(it); + if (DeclAttrs->empty()) { delete DeclAttrs; DeclAttrs = 0; @@ -366,47 +370,11 @@ void Decl::swapAttrs(Decl *RHS) { } -#define CASE(KIND) \ - case KIND: \ - static_cast<KIND##Decl *>(const_cast<Decl *>(this))->~KIND##Decl(); \ - break - -void Decl::Destroy(ASTContext& C) const { - switch (getKind()) { - CASE(TranslationUnit); - CASE(Field); - CASE(ObjCIvar); - CASE(ObjCCategory); - CASE(ObjCCategoryImpl); - CASE(ObjCImplementation); - CASE(ObjCProtocol); - CASE(ObjCProperty); - CASE(Namespace); - CASE(Typedef); - CASE(Enum); - CASE(EnumConstant); - CASE(Function); - CASE(Var); - CASE(ParmVar); - CASE(ObjCInterface); - CASE(ObjCCompatibleAlias); - CASE(ObjCMethod); - CASE(ObjCClass); - CASE(ObjCForwardProtocol); - CASE(LinkageSpec); - - case Struct: case Union: case Class: - static_cast<RecordDecl *>(const_cast<Decl *>(this))->~RecordDecl(); - break; - - default: assert(0 && "Unknown decl kind!"); - } - +void Decl::Destroy(ASTContext& C) { + this->~Decl(); C.getAllocator().Deallocate((void *)this); } -#undef CASE - //===----------------------------------------------------------------------===// // DeclContext Implementation //===----------------------------------------------------------------------===// @@ -442,10 +410,14 @@ const char *NamedDecl::getName() const { FunctionDecl::~FunctionDecl() { delete[] ParamInfo; - delete Body; - delete PreviousDeclaration; } +void FunctionDecl::Destroy(ASTContext& C) { + if (Body) Body->Destroy(C); + Decl::Destroy(C); +} + + Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) { if (FD->Body) { diff --git a/clang/lib/AST/Stmt.cpp b/clang/lib/AST/Stmt.cpp index ffc6c2e62a8..993dda71c98 100644 --- a/clang/lib/AST/Stmt.cpp +++ b/clang/lib/AST/Stmt.cpp @@ -42,9 +42,14 @@ const char *Stmt::getStmtClassName() const { return getStmtInfoTableEntry(sClass).Name; } -void Stmt::DestroyChildren() { +void Stmt::DestroyChildren(ASTContext& C) { for (child_iterator I = child_begin(), E = child_end(); I !=E; ++I) - delete *I; // Handles the case when *I == NULL. + if (Stmt* Child = *I) Child->Destroy(C); +} + +void Stmt::Destroy(ASTContext& C) { + DestroyChildren(C); + this->~Stmt(); } void Stmt::PrintStats() { diff --git a/clang/lib/AST/TranslationUnit.cpp b/clang/lib/AST/TranslationUnit.cpp index 5c043efeb31..9d7758648d5 100644 --- a/clang/lib/AST/TranslationUnit.cpp +++ b/clang/lib/AST/TranslationUnit.cpp @@ -20,9 +20,9 @@ #include "llvm/Bitcode/Deserialize.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/System/Path.h" -#include "llvm/ADT/OwningPtr.h" -#include <stdio.h> +#include "llvm/ADT/OwningPtr.h" +#include "llvm/ADT/DenseSet.h" using namespace clang; @@ -31,8 +31,15 @@ enum { BasicMetadataBlock = 1, DeclsBlock = 3 }; TranslationUnit::~TranslationUnit() { - for (iterator I=begin(), E=end(); I!=E; ++I) + + llvm::DenseSet<Decl*> Killed; + + for (iterator I=begin(), E=end(); I!=E; ++I) { + if (Killed.count(*I)) continue; + + Killed.insert(*I); (*I)->Destroy(*Context); + } if (OwnsMetaData && Context) { // The ASTContext object has the sole references to the IdentifierTable diff --git a/clang/lib/Sema/ParseAST.cpp b/clang/lib/Sema/ParseAST.cpp index 364b0729100..fcdc27b10ae 100644 --- a/clang/lib/Sema/ParseAST.cpp +++ b/clang/lib/Sema/ParseAST.cpp @@ -14,6 +14,7 @@ #include "clang/Sema/ParseAST.h" #include "clang/AST/ASTContext.h" #include "clang/AST/ASTConsumer.h" +#include "clang/AST/TranslationUnit.h" #include "Sema.h" #include "clang/Parse/Action.h" #include "clang/Parse/Parser.h" @@ -36,6 +37,8 @@ void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, bool PrintStats) { ASTContext Context(PP.getSourceManager(), PP.getTargetInfo(), PP.getIdentifierTable(), PP.getSelectorTable()); + TranslationUnit TU(Context, PP.getLangOptions()); + Parser P(PP, *new Sema(PP, Context, *Consumer)); PP.EnterMainSourceFile(); @@ -45,12 +48,16 @@ void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, bool PrintStats) { Consumer->Initialize(Context); Parser::DeclTy *ADecl; + while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file. // If we got a null return and something *was* parsed, ignore it. This // is due to a top-level semicolon, an action override, or a parse error // skipping something. - if (ADecl) - Consumer->HandleTopLevelDecl(static_cast<Decl*>(ADecl)); + if (ADecl) { + Decl* D = static_cast<Decl*>(ADecl); + TU.AddTopLevelDecl(D); // TranslationUnit now owns the Decl. + Consumer->HandleTopLevelDecl(D); + } }; if (PrintStats) { |