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/Sema/ParseAST.cpp b/lib/Sema/ParseAST.cpp
index 364b072..fcdc27b 100644
--- a/lib/Sema/ParseAST.cpp
+++ b/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 @@
   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 @@
   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) {