diff options
author | Daniel Dunbar <daniel@zuster.org> | 2009-11-13 08:20:47 +0000 |
---|---|---|
committer | Daniel Dunbar <daniel@zuster.org> | 2009-11-13 08:20:47 +0000 |
commit | 5eb810024dc8a1d12d5f066c02c978f07c4fcb00 (patch) | |
tree | 2ed8a314e87a0e312408af4deef30d973abae917 | |
parent | fc7ac8f0b9ffd83b9e7329926e9e184586b49138 (diff) |
Add ASTContext to CompilerInstance.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@87095 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/clang/Frontend/CompilerInstance.h | 28 | ||||
-rw-r--r-- | lib/Frontend/CompilerInstance.cpp | 12 | ||||
-rw-r--r-- | tools/clang-cc/clang-cc.cpp | 21 |
3 files changed, 47 insertions, 14 deletions
diff --git a/include/clang/Frontend/CompilerInstance.h b/include/clang/Frontend/CompilerInstance.h index b288236768..0cc1911c0c 100644 --- a/include/clang/Frontend/CompilerInstance.h +++ b/include/clang/Frontend/CompilerInstance.h @@ -19,6 +19,7 @@ class LLVMContext; } namespace clang { +class ASTContext; class Diagnostic; class DiagnosticClient; class Preprocessor; @@ -70,6 +71,9 @@ class CompilerInstance { /// 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 @@ public: 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 @@ public: /// 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 @@ public: const TargetInfo &, SourceManager &, FileManager &); + /// Create the AST context. + void createASTContext(); + /// } }; diff --git a/lib/Frontend/CompilerInstance.cpp b/lib/Frontend/CompilerInstance.cpp index e1ae63cf37..0829ce32cc 100644 --- a/lib/Frontend/CompilerInstance.cpp +++ b/lib/Frontend/CompilerInstance.cpp @@ -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 @@ CompilerInstance::createPreprocessor(Diagnostic &Diags, 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)); +} diff --git a/tools/clang-cc/clang-cc.cpp b/tools/clang-cc/clang-cc.cpp index a1b731c9f2..0393c7301d 100644 --- a/tools/clang-cc/clang-cc.cpp +++ b/tools/clang-cc/clang-cc.cpp @@ -509,30 +509,23 @@ static void ProcessInputFile(CompilerInstance &CI, const std::string &InFile, } } - 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 @@ static void ProcessInputFile(CompilerInstance &CI, const std::string &InFile, } // 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 @@ static void ProcessInputFile(CompilerInstance &CI, const std::string &InFile, // 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) |