Try to plug some memory leaks...

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
diff --git a/lib/AST/TranslationUnit.cpp b/lib/AST/TranslationUnit.cpp
index 5c043ef..9d77586 100644
--- a/lib/AST/TranslationUnit.cpp
+++ b/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 @@
        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