diff --git a/clang/Driver/clang.cpp b/clang/Driver/clang.cpp index ae7ec879ddab..060fbf3f6945 100644 --- a/clang/Driver/clang.cpp +++ b/clang/Driver/clang.cpp @@ -54,13 +54,17 @@ using namespace clang; // Global options. //===----------------------------------------------------------------------===// -bool HadErrors = false; +static bool HadErrors = false; static llvm::cl::opt Verbose("v", llvm::cl::desc("Enable verbose output")); static llvm::cl::opt Stats("print-stats", llvm::cl::desc("Print performance metrics and statistics")); +static llvm::cl::opt +DisableFree("disable-free", + llvm::cl::desc("Disable freeing of memory on exit"), + llvm::cl::init(false)); enum ProgActions { RewriteObjC, // ObjC->C Rewriter. @@ -1187,7 +1191,7 @@ static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF, if (VerifyDiagnostics) exit(CheckASTConsumer(PP, Consumer.get())); - ParseAST(PP, Consumer.get(), Stats); + ParseAST(PP, Consumer.get(), Stats, !DisableFree); } else { if (VerifyDiagnostics) exit(CheckDiagnostics(PP)); diff --git a/clang/include/clang/Sema/ParseAST.h b/clang/include/clang/Sema/ParseAST.h index 907372ddef4d..60249ede8d89 100644 --- a/clang/include/clang/Sema/ParseAST.h +++ b/clang/include/clang/Sema/ParseAST.h @@ -21,7 +21,11 @@ namespace clang { /// ParseAST - Parse the entire file specified, notifying the ASTConsumer as /// the file is parsed. This takes ownership of the ASTConsumer and /// ultimately deletes it. - void ParseAST(Preprocessor &pp, ASTConsumer *C, bool PrintStats = false); + /// + /// \param FreeMemory If false, the memory used for AST elements is + /// not released. + void ParseAST(Preprocessor &pp, ASTConsumer *C, + bool PrintStats = false, bool FreeMemory = true); } // end namespace clang diff --git a/clang/lib/Sema/ParseAST.cpp b/clang/lib/Sema/ParseAST.cpp index fc3509682728..567cfc27630d 100644 --- a/clang/lib/Sema/ParseAST.cpp +++ b/clang/lib/Sema/ParseAST.cpp @@ -26,26 +26,30 @@ using namespace clang; /// ParseAST - Parse the entire file specified, notifying the ASTConsumer as /// the file is parsed. This takes ownership of the ASTConsumer and /// ultimately deletes it. -void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, bool PrintStats) { +/// +/// \param FreeMemory If false, the memory used for AST elements is +/// not released. +void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, + bool PrintStats, bool FreeMemory) { // Collect global stats on Decls/Stmts (until we have a module streamer). if (PrintStats) { Decl::CollectingStats(true); Stmt::CollectingStats(true); } - ASTContext Context(PP.getLangOptions(), PP.getSourceManager(), - PP.getTargetInfo(), - PP.getIdentifierTable(), PP.getSelectorTable()); - - TranslationUnit TU(Context); - Sema S(PP, Context, *Consumer); + ASTContext *Context = + new ASTContext(PP.getLangOptions(), PP.getSourceManager(), + PP.getTargetInfo(), + PP.getIdentifierTable(), PP.getSelectorTable()); + TranslationUnit *TU = new TranslationUnit(*Context); + Sema S(PP, *Context, *Consumer); Parser P(PP, S); PP.EnterMainSourceFile(); // Initialize the parser. P.Initialize(); - Consumer->InitializeTU(TU); + Consumer->InitializeTU(*TU); Parser::DeclTy *ADecl; @@ -55,17 +59,17 @@ void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, bool PrintStats) { // skipping something. if (ADecl) { Decl* D = static_cast(ADecl); - TU.AddTopLevelDecl(D); // TranslationUnit now owns the Decl. + TU->AddTopLevelDecl(D); // TranslationUnit now owns the Decl. Consumer->HandleTopLevelDecl(D); } }; - Consumer->HandleTranslationUnit(TU); + Consumer->HandleTranslationUnit(*TU); if (PrintStats) { fprintf(stderr, "\nSTATISTICS:\n"); P.getActions().PrintStats(); - Context.PrintStats(); + Context->PrintStats(); Decl::PrintStats(); Stmt::PrintStats(); Consumer->PrintStats(); @@ -73,4 +77,9 @@ void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, bool PrintStats) { Decl::CollectingStats(false); Stmt::CollectingStats(false); } + + if (FreeMemory) { + delete TU; + delete Context; + } }