]> granicus.if.org Git - clang/commitdiff
Patch by Alexei Svitkine: Refactor Sema::ParseAST API to allow clients to pass as...
authorTed Kremenek <kremenek@apple.com>
Wed, 28 Jan 2009 04:29:29 +0000 (04:29 +0000)
committerTed Kremenek <kremenek@apple.com>
Wed, 28 Jan 2009 04:29:29 +0000 (04:29 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@63175 91177308-0d34-0410-b5e6-96231b3b80d8

Driver/clang.cpp
include/clang/Sema/ParseAST.h
lib/Sema/ParseAST.cpp

index 51f4a5d1d8300fa4712ec009a0cb72312f35a1bf..bc548e4e8b8496dfacf811a5e4150bb20acda571 100644 (file)
@@ -1418,9 +1418,20 @@ static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF,
     ClearSourceMgr = true;
     break;
   }
-  
-  if (Consumer)
-    ParseAST(PP, Consumer.get(), Stats, !DisableFree);
+
+  if (Consumer) {
+    TranslationUnit *TU = 0;
+    if (DisableFree) {
+      ASTContext *Context = new ASTContext(PP.getLangOptions(),
+                                           PP.getSourceManager(),
+                                           PP.getTargetInfo(),
+                                           PP.getIdentifierTable(),
+                                           PP.getSelectorTable(),
+                                           /* FreeMemory = */ false);
+      TU = new TranslationUnit(*Context);
+    }
+    ParseAST(PP, Consumer.get(), TU, Stats);
+  }
 
   if (VerifyDiagnostics)
     if (CheckDiagnostics(PP))
index 91d7f1aa36b6048536c23415b7e75d634c4d2ee1..b18e5a672959216c199606053c106b70275d4a8a 100644 (file)
 namespace clang {
   class Preprocessor;
   class ASTConsumer;
-  
+  class TranslationUnit;
+
   /// 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.
+  /// \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, 
-                bool PrintStats = false, bool FreeMemory = true);
+                TranslationUnit *TU = 0,
+                bool PrintStats = false);
 
 }  // end namespace clang
 
index 67af285315016dd5374b7c70a698012c6503fc6a..6923476aeba0d25f0d1decb53811869e2d535519 100644 (file)
@@ -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;      
-  }
 }