aboutsummaryrefslogtreecommitdiff
path: root/lib/Sema/ParseAST.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Sema/ParseAST.cpp')
-rw-r--r--lib/Sema/ParseAST.cpp40
1 files changed, 22 insertions, 18 deletions
diff --git a/lib/Sema/ParseAST.cpp b/lib/Sema/ParseAST.cpp
index 67af285315..6923476aeb 100644
--- a/lib/Sema/ParseAST.cpp
+++ b/lib/Sema/ParseAST.cpp
@@ -11,6 +11,7 @@
//
//===----------------------------------------------------------------------===//
+#include <llvm/ADT/OwningPtr.h>
#include "clang/Sema/ParseAST.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/Stmt.h"
@@ -26,23 +27,31 @@ using namespace clang;
/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
/// the file is parsed.
///
-/// \param FreeMemory If false, the memory used for AST elements is
-/// not released.
-void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
- bool PrintStats, bool FreeMemory) {
+/// \param TU If 0, then memory used for AST elements will be allocated only
+/// for the duration of the ParseAST() call. In this case, the client should
+/// not access any AST elements after ParseAST() returns.
+void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
+ TranslationUnit *TU, bool PrintStats) {
// Collect global stats on Decls/Stmts (until we have a module streamer).
if (PrintStats) {
Decl::CollectingStats(true);
Stmt::CollectingStats(true);
}
-
- ASTContext *Context =
- new ASTContext(PP.getLangOptions(), PP.getSourceManager(),
- PP.getTargetInfo(),
- PP.getIdentifierTable(), PP.getSelectorTable(),
- FreeMemory);
- TranslationUnit *TU = new TranslationUnit(*Context);
- Sema S(PP, *Context, *Consumer);
+
+ llvm::OwningPtr<ASTContext> ContextOwner;
+ llvm::OwningPtr<TranslationUnit> TranslationUnitOwner;
+ if (TU == 0) {
+ ASTContext *Context = new ASTContext(PP.getLangOptions(),
+ PP.getSourceManager(),
+ PP.getTargetInfo(),
+ PP.getIdentifierTable(),
+ PP.getSelectorTable());
+ ContextOwner.reset(Context);
+ TU = new TranslationUnit(*Context);
+ TranslationUnitOwner.reset(TU);
+ }
+
+ Sema S(PP, TU->getContext(), *Consumer);
Parser P(PP, S);
PP.EnterMainSourceFile();
@@ -68,7 +77,7 @@ void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
if (PrintStats) {
fprintf(stderr, "\nSTATISTICS:\n");
P.getActions().PrintStats();
- Context->PrintStats();
+ TU->getContext().PrintStats();
Decl::PrintStats();
Stmt::PrintStats();
Consumer->PrintStats();
@@ -76,9 +85,4 @@ void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
Decl::CollectingStats(false);
Stmt::CollectingStats(false);
}
-
- if (FreeMemory) {
- delete TU;
- delete Context;
- }
}