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/Stmt.cpp b/lib/AST/Stmt.cpp
index ffc6c2e..993dda7 100644
--- a/lib/AST/Stmt.cpp
+++ b/lib/AST/Stmt.cpp
@@ -42,9 +42,14 @@
   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() {