]> granicus.if.org Git - clang/commitdiff
Try to plug some memory leaks...
authorTed Kremenek <kremenek@apple.com>
Tue, 20 May 2008 00:43:19 +0000 (00:43 +0000)
committerTed Kremenek <kremenek@apple.com>
Tue, 20 May 2008 00:43:19 +0000 (00:43 +0000)
1) Sema::ParseAST now constructs a TranslationUnit object to own the top-level Decls, which releases the top-level Decls upon exiting ParseAST.

2) Bug fix: TranslationUnit::~TranslationUnit handles the case where a Decl is added more than once as a top-level Decl.

3) Decl::Destroy is now a virtual method, obviating the need for a special dispatch based on DeclKind.

3) FunctionDecl::Destroy now releases its Body using its Destroy method.

4) Added Stmt::Destroy and Stmt::DestroyChildren, which recursively delete the child ASTs of a Stmt and call their dstors.  We may need to special case dstor/Destroy methods for particular Stmt subclasses that own other dynamically allocated objects besides AST nodes.

5) REGRESSION: We temporarily are not deallocating attributes; a FIXME is provided.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@51286 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/Decl.h
include/clang/AST/DeclBase.h
include/clang/AST/DeclObjC.h
include/clang/AST/Stmt.h
lib/AST/Decl.cpp
lib/AST/Stmt.cpp
lib/AST/TranslationUnit.cpp
lib/Sema/ParseAST.cpp

index f346be80eadaa3f502ab7afbb839a5ba3a50d10d..af981d9a67dbd468a335e18511d5ceaab71aa629 100644 (file)
@@ -403,11 +403,13 @@ private:
       IsInline(isInline), IsImplicit(0), PreviousDeclaration(0) {}
 
   virtual ~FunctionDecl();
+  virtual void Destroy(ASTContext& C);
+
 public:
   static FunctionDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L,
                               IdentifierInfo *Id, QualType T, 
                               StorageClass S = None, bool isInline = false, 
-                              ScopedDecl *PrevDecl = 0);
+                              ScopedDecl *PrevDecl = 0);  
   
   /// getBody - Retrieve the body (definition) of the function. The
   /// function body might be in any of the (re-)declarations of this
@@ -488,7 +490,6 @@ protected:
   static FunctionDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
   
   friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
-  friend void Decl::Destroy(ASTContext& C) const;
 };
 
 
@@ -540,7 +541,8 @@ protected:
                    IdentifierInfo *Id, QualType T, Expr *E,
                    const llvm::APSInt &V, ScopedDecl *PrevDecl)
     : ValueDecl(EnumConstant, DC, L, Id, T, PrevDecl), Init(E), Val(V) {}
-  ~EnumConstantDecl() {}
+
+  virtual ~EnumConstantDecl() {}
 public:
 
   static EnumConstantDecl *Create(ASTContext &C, EnumDecl *DC,
@@ -569,7 +571,6 @@ protected:
   static EnumConstantDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
   
   friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
-  friend void Decl::Destroy(ASTContext& C) const;
 };
 
 
@@ -600,7 +601,8 @@ class TypedefDecl : public TypeDecl {
   TypedefDecl(DeclContext *DC, SourceLocation L,
               IdentifierInfo *Id, QualType T, ScopedDecl *PD) 
     : TypeDecl(Typedef, DC, L, Id, PD), UnderlyingType(T) {}
-  ~TypedefDecl() {}
+
+  virtual ~TypedefDecl() {}
 public:
   
   static TypedefDecl *Create(ASTContext &C, DeclContext *DC,
@@ -622,7 +624,7 @@ protected:
   static TypedefDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
   
   friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
-  friend void Decl::Destroy(ASTContext& C) const;
+  
 };
 
 
index f8ef8d41de605bd9c542ee99a2b0d9b9e8f8d08f..210f1691502ca9694cba9844ab587c4b2cfcfb33 100644 (file)
@@ -185,7 +185,7 @@ public:
   static Decl* Create(llvm::Deserializer& D, ASTContext& C);
 
   /// Destroy - Call destructors and release memory.
-  void Destroy(ASTContext& C) const;
+  virtual void Destroy(ASTContext& C);
 
 protected:
   /// EmitImpl - Provides the subclass-specific serialization logic for
index 1255c179256f1aa72f0f1fb8d4cd12cc0a896e63..1e82b5c56df6b5c5ea23c9d208c27f681a4552a4 100644 (file)
@@ -184,8 +184,6 @@ public:
   // Implement isa/cast/dyncast/etc.
   static bool classof(const Decl *D) { return D->getKind() == ObjCMethod; }
   static bool classof(const ObjCMethodDecl *D) { return true; }
-
-  friend void Decl::Destroy(ASTContext& C) const;
 };
   
 /// ObjCInterfaceDecl - Represents an ObjC class declaration. For example:
index dac69a16103373bcb8dfc30398676d7575780964..8b29617fa0aa9155c54d4d3a70db22d3c3b169ff 100644 (file)
@@ -54,13 +54,15 @@ private:
 protected:
   /// DestroyChildren - Invoked by destructors of subclasses of Stmt to
   ///  recursively release child AST nodes.
-  void DestroyChildren();
+  void DestroyChildren(ASTContext& Ctx);
   
 public:
   Stmt(StmtClass SC) : sClass(SC) { 
     if (Stmt::CollectingStats()) Stmt::addStmtClass(SC);
   }
   virtual ~Stmt() {}
+  
+  virtual void Destroy(ASTContext& Ctx);
 
   StmtClass getStmtClass() const { return sClass; }
   const char *getStmtClassName() const;
index 1a7eecac32ad2a0be7024049529988ffeb969e0e..a831c4ff9a8a9e8bc0be996015edda16f9f0691f 100644 (file)
@@ -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) {
index ffc6c2e62a8567c5b0bfd9929bdfd8c8ece09a53..993dda71c98c576329323513ef38b4caa81dd048 100644 (file)
@@ -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() {
index 5c043efeb31753e7671ffcc4f77cf0f9fdca6732..9d7758648d527a7625b13de0e4337fbc0d0065e1 100644 (file)
@@ -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
index 364b07291006d877ca6af22e8d74a5159ccc7854..fcdc27b10aeb0396471b5df9e65a92bae96ffbbe 100644 (file)
@@ -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) {