]> granicus.if.org Git - clang/commitdiff
simplify ParseAST by sucking -disable-free handling logic up into
authorChris Lattner <sabre@nondot.org>
Sat, 28 Mar 2009 01:37:17 +0000 (01:37 +0000)
committerChris Lattner <sabre@nondot.org>
Sat, 28 Mar 2009 01:37:17 +0000 (01:37 +0000)
clang.cpp

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67890 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Sema/ParseAST.h
lib/Sema/ParseAST.cpp
tools/clang-cc/clang.cpp

index b18e5a672959216c199606053c106b70275d4a8a..b41c2da1d41d85c96e69e0ccb816ec71b05ed87a 100644 (file)
@@ -20,14 +20,10 @@ namespace clang {
   class TranslationUnit;
 
   /// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
-  /// the file is parsed.
+  /// the file is parsed.  This inserts the parsed decls into TU.
   ///
-  /// \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 ParseAST(Preprocessor &pp, ASTConsumer *C, 
-                TranslationUnit *TU = 0,
-                bool PrintStats = false);
+                TranslationUnit &TU, bool PrintStats = false);
 
 }  // end namespace clang
 
index 6923476aeba0d25f0d1decb53811869e2d535519..6b40d7fd185d4f0713692bb8c60da50b6e9a4bd7 100644 (file)
@@ -25,40 +25,24 @@ using namespace clang;
 //===----------------------------------------------------------------------===//
 
 /// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
-/// the file is parsed.
+/// the file is parsed.  This inserts the parsed decls into TU.
 ///
-/// \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) {
+                     TranslationUnit &TU, bool PrintStats) {
   // Collect global stats on Decls/Stmts (until we have a module streamer).
   if (PrintStats) {
     Decl::CollectingStats(true);
     Stmt::CollectingStats(true);
   }
 
-  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);
+  Sema S(PP, TU.getContext(), *Consumer);
   Parser P(PP, S);
   PP.EnterMainSourceFile();
     
   // Initialize the parser.
   P.Initialize();
   
-  Consumer->InitializeTU(*TU);
+  Consumer->InitializeTU(TU);
   
   Parser::DeclTy *ADecl;
   
@@ -72,12 +56,12 @@ void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
     }
   };
   
-  Consumer->HandleTranslationUnit(*TU);
+  Consumer->HandleTranslationUnit(TU);
 
   if (PrintStats) {
     fprintf(stderr, "\nSTATISTICS:\n");
     P.getActions().PrintStats();
-    TU->getContext().PrintStats();
+    TU.getContext().PrintStats();
     Decl::PrintStats();
     Stmt::PrintStats();
     Consumer->PrintStats();
index 5d3f68dd8ef8bbbdd44c158ddd633c380556a5de..3b2afa91a613b40c0c8e87d506f11c92ad1ee226 100644 (file)
@@ -1476,17 +1476,26 @@ static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF,
   }
 
   if (Consumer) {
-    TranslationUnit *TU = 0;
+    llvm::OwningPtr<ASTContext> ContextOwner;
+    llvm::OwningPtr<TranslationUnit> TranslationUnitOwner;
+
+    ContextOwner.reset(new ASTContext(PP.getLangOptions(),
+                                      PP.getSourceManager(),
+                                      PP.getTargetInfo(),
+                                      PP.getIdentifierTable(),
+                                      PP.getSelectorTable(),
+                                      /* FreeMemory = */ !DisableFree));
+    TranslationUnitOwner.reset(new TranslationUnit(*ContextOwner.get()));
+    
+    
+    ParseAST(PP, Consumer.get(), *TranslationUnitOwner.get(), Stats);
+    
+    // If in -disable-free mode, don't deallocate these when they go out of
+    // scope.
     if (DisableFree) {
-      ASTContext *Context = new ASTContext(PP.getLangOptions(),
-                                           PP.getSourceManager(),
-                                           PP.getTargetInfo(),
-                                           PP.getIdentifierTable(),
-                                           PP.getSelectorTable(),
-                                           /* FreeMemory = */ false);
-      TU = new TranslationUnit(*Context);
+      ContextOwner.take();
+      TranslationUnitOwner.take();
     }
-    ParseAST(PP, Consumer.get(), TU, Stats);
   }
 
   if (VerifyDiagnostics)