[cfe-commits] r87095 - in /cfe/trunk: include/clang/Frontend/CompilerInstance.h lib/Frontend/CompilerInstance.cpp tools/clang-cc/clang-cc.cpp

Daniel Dunbar daniel at zuster.org
Fri Nov 13 00:20:47 PST 2009


Author: ddunbar
Date: Fri Nov 13 02:20:47 2009
New Revision: 87095

URL: http://llvm.org/viewvc/llvm-project?rev=87095&view=rev
Log:
Add ASTContext to CompilerInstance.

Modified:
    cfe/trunk/include/clang/Frontend/CompilerInstance.h
    cfe/trunk/lib/Frontend/CompilerInstance.cpp
    cfe/trunk/tools/clang-cc/clang-cc.cpp

Modified: cfe/trunk/include/clang/Frontend/CompilerInstance.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/CompilerInstance.h?rev=87095&r1=87094&r2=87095&view=diff

==============================================================================
--- cfe/trunk/include/clang/Frontend/CompilerInstance.h (original)
+++ cfe/trunk/include/clang/Frontend/CompilerInstance.h Fri Nov 13 02:20:47 2009
@@ -19,6 +19,7 @@
 }
 
 namespace clang {
+class ASTContext;
 class Diagnostic;
 class DiagnosticClient;
 class Preprocessor;
@@ -70,6 +71,9 @@
   /// The preprocessor.
   llvm::OwningPtr<Preprocessor> PP;
 
+  /// The AST context.
+  llvm::OwningPtr<ASTContext> Context;
+
 public:
   /// Create a new compiler instance with the given LLVM context, optionally
   /// taking ownership of it.
@@ -265,6 +269,23 @@
   void setPreprocessor(Preprocessor *Value) { PP.reset(Value); }
 
   /// }
+  /// @name ASTContext
+  /// {
+
+  ASTContext &getASTContext() const {
+    assert(Context && "Compiler instance has no AST context!");
+    return *Context;
+  }
+
+  /// takeASTContext - Remove the current AST context and give ownership to the
+  /// caller.
+  ASTContext *takeASTContext() { return Context.take(); }
+
+  /// setASTContext - Replace the current AST context; the compiler instance
+  /// takes ownership of \arg Value.
+  void setASTContext(ASTContext *Value) { Context.reset(Value); }
+
+  /// }
   /// @name Construction Utility Methods
   /// {
 
@@ -280,6 +301,10 @@
   /// when the diagnostic options indicate that the compiler should output
   /// logging information.
   ///
+  /// Note that this creates an unowned DiagnosticClient, if using directly the
+  /// caller is responsible for releaseing the returned Diagnostic's client
+  /// eventually.
+  ///
   /// \return The new object on success, or null on failure.
   static Diagnostic *createDiagnostics(const DiagnosticOptions &Opts,
                                        int Argc, char **Argv);
@@ -307,6 +332,9 @@
                                           const TargetInfo &,
                                           SourceManager &, FileManager &);
 
+  /// Create the AST context.
+  void createASTContext();
+
   /// }
 };
 

Modified: cfe/trunk/lib/Frontend/CompilerInstance.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInstance.cpp?rev=87095&r1=87094&r2=87095&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInstance.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInstance.cpp Fri Nov 13 02:20:47 2009
@@ -8,6 +8,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/Frontend/CompilerInstance.h"
+#include "clang/AST/ASTContext.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/SourceManager.h"
@@ -152,3 +153,14 @@
 
   return PP;
 }
+
+// ASTContext
+
+void CompilerInstance::createASTContext() {
+  Preprocessor &PP = getPreprocessor();
+  Context.reset(new ASTContext(getLangOpts(), PP.getSourceManager(),
+                               getTarget(), PP.getIdentifierTable(),
+                               PP.getSelectorTable(), PP.getBuiltinInfo(),
+                               /*FreeMemory=*/ !getFrontendOpts().DisableFree,
+                               /*size_reserve=*/ 0));
+}

Modified: cfe/trunk/tools/clang-cc/clang-cc.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-cc/clang-cc.cpp?rev=87095&r1=87094&r2=87095&view=diff

==============================================================================
--- cfe/trunk/tools/clang-cc/clang-cc.cpp (original)
+++ cfe/trunk/tools/clang-cc/clang-cc.cpp Fri Nov 13 02:20:47 2009
@@ -509,30 +509,23 @@
     }
   }
 
-  llvm::OwningPtr<ASTContext> ContextOwner;
   llvm::OwningPtr<ExternalASTSource> Source;
   const std::string &ImplicitPCHInclude =
     CI.getPreprocessorOpts().getImplicitPCHInclude();
   if (Consumer) {
-    ContextOwner.reset(new ASTContext(PP.getLangOptions(),
-                                      PP.getSourceManager(),
-                                      PP.getTargetInfo(),
-                                      PP.getIdentifierTable(),
-                                      PP.getSelectorTable(),
-                                      PP.getBuiltinInfo(),
-                                      /* FreeMemory = */ !FEOpts.DisableFree,
-                                      /* size_reserve = */0));
+    // Create the ASTContext.
+    CI.createASTContext();
 
     if (!ImplicitPCHInclude.empty()) {
       Source.reset(ReadPCHFile(ImplicitPCHInclude,
                                CI.getHeaderSearchOpts().Sysroot, PP,
-                               *ContextOwner));
+                               CI.getASTContext()));
       if (!Source)
         return;
 
       // Attach the PCH reader to the AST context as an external AST source, so
       // that declarations will be deserialized from the PCH file as needed.
-      ContextOwner->setExternalSource(Source);
+      CI.getASTContext().setExternalSource(Source);
     } else {
       // Initialize builtin info when not using PCH.
       PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
@@ -566,7 +559,7 @@
     }
 
     // Run the AST consumer action.
-    ParseAST(PP, Consumer.get(), *ContextOwner.get(), FEOpts.ShowStats,
+    ParseAST(PP, Consumer.get(), CI.getASTContext(), FEOpts.ShowStats,
              CompleteTranslationUnit,
              CreateCodeCompleter, CreateCodeCompleterData);
   } else {
@@ -656,10 +649,10 @@
   // perform actions in its destructor which require the context.
   if (FEOpts.DisableFree) {
     Consumer.take();
-    ContextOwner.take();
+    CI.takeASTContext();
   } else {
     Consumer.reset();
-    ContextOwner.reset();
+    CI.setASTContext(0);
   }
 
   if (CI.getDiagnosticOpts().VerifyDiagnostics)





More information about the cfe-commits mailing list