From 599313ef94fb801b55c67ece2b7ae657890af3b5 Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Fri, 13 Nov 2009 08:21:10 +0000 Subject: [PATCH] Add CompilerInstance::createPCHExternalASTSource. llvm-svn: 87097 --- .../include/clang/Frontend/CompilerInstance.h | 16 ++++++- clang/lib/Frontend/CompilerInstance.cpp | 38 +++++++++++++++ clang/tools/clang-cc/clang-cc.cpp | 47 +------------------ 3 files changed, 55 insertions(+), 46 deletions(-) diff --git a/clang/include/clang/Frontend/CompilerInstance.h b/clang/include/clang/Frontend/CompilerInstance.h index 8c638206b6da..2e19822fa0e7 100644 --- a/clang/include/clang/Frontend/CompilerInstance.h +++ b/clang/include/clang/Frontend/CompilerInstance.h @@ -11,6 +11,7 @@ #define LLVM_CLANG_FRONTEND_COMPILERINSTANCE_H_ #include "clang/Frontend/CompilerInvocation.h" +#include "llvm/ADT/StringRef.h" #include "llvm/ADT/OwningPtr.h" #include @@ -22,8 +23,10 @@ namespace clang { class ASTContext; class Diagnostic; class DiagnosticClient; -class Preprocessor; +class ExternalASTSource; class FileManager; +class Preprocessor; +class Source; class SourceManager; class TargetInfo; @@ -349,6 +352,17 @@ public: /// Create the AST context. void createASTContext(); + /// Create an external AST source to read a PCH file and attach it to the AST + /// context. + void createPCHExternalASTSource(llvm::StringRef Path); + + /// Create an external AST source to read a PCH file. + /// + /// \return - The new object on success, or null on failure. + static ExternalASTSource * + createPCHExternalASTSource(llvm::StringRef Path, const std::string &Sysroot, + Preprocessor &PP, ASTContext &Context); + /// } }; diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp index 0829ce32cc3c..e7c3611f0fd1 100644 --- a/clang/lib/Frontend/CompilerInstance.cpp +++ b/clang/lib/Frontend/CompilerInstance.cpp @@ -17,6 +17,7 @@ #include "clang/Lex/Preprocessor.h" #include "clang/Lex/PTHManager.h" #include "clang/Frontend/ChainedDiagnosticClient.h" +#include "clang/Frontend/PCHReader.h" #include "clang/Frontend/TextDiagnosticBuffer.h" #include "clang/Frontend/TextDiagnosticPrinter.h" #include "clang/Frontend/Utils.h" @@ -164,3 +165,40 @@ void CompilerInstance::createASTContext() { /*FreeMemory=*/ !getFrontendOpts().DisableFree, /*size_reserve=*/ 0)); } + +// ExternalASTSource + +void CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path) { + llvm::OwningPtr Source; + Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot, + getPreprocessor(), getASTContext())); + getASTContext().setExternalSource(Source); +} + +ExternalASTSource * +CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path, + const std::string &Sysroot, + Preprocessor &PP, + ASTContext &Context) { + llvm::OwningPtr Reader; + Reader.reset(new PCHReader(PP, &Context, + Sysroot.empty() ? 0 : Sysroot.c_str())); + + switch (Reader->ReadPCH(Path)) { + case PCHReader::Success: + // Set the predefines buffer as suggested by the PCH reader. Typically, the + // predefines buffer will be empty. + PP.setPredefines(Reader->getSuggestedPredefines()); + return Reader.take(); + + case PCHReader::Failure: + // Unrecoverable failure: don't even try to process the input file. + break; + + case PCHReader::IgnorePCH: + // No suitable PCH file could be found. Return an error. + break; + } + + return 0; +} diff --git a/clang/tools/clang-cc/clang-cc.cpp b/clang/tools/clang-cc/clang-cc.cpp index 764db6f503d5..b186f20ed53c 100644 --- a/clang/tools/clang-cc/clang-cc.cpp +++ b/clang/tools/clang-cc/clang-cc.cpp @@ -33,7 +33,6 @@ #include "clang/Frontend/DependencyOutputOptions.h" #include "clang/Frontend/FixItRewriter.h" #include "clang/Frontend/FrontendDiagnostic.h" -#include "clang/Frontend/PCHReader.h" #include "clang/Frontend/PathDiagnosticClients.h" #include "clang/Frontend/PreprocessorOptions.h" #include "clang/Frontend/PreprocessorOutputOptions.h" @@ -388,41 +387,6 @@ static ASTConsumer *CreateConsumerAction(CompilerInstance &CI, } } -/// ReadPCHFile - Load a PCH file from disk, and initialize the preprocessor for -/// reading from the PCH file. -/// -/// \return The AST source, or null on failure. -static ExternalASTSource *ReadPCHFile(llvm::StringRef Path, - const std::string Sysroot, - Preprocessor &PP, - ASTContext &Context) { - // If the user specified -isysroot, it will be used for relocatable PCH files. - const char *isysrootPCH = Sysroot.c_str(); - if (isysrootPCH[0] == '\0') - isysrootPCH = 0; - - llvm::OwningPtr Reader; - Reader.reset(new PCHReader(PP, &Context, isysrootPCH)); - - switch (Reader->ReadPCH(Path)) { - case PCHReader::Success: - // Set the predefines buffer as suggested by the PCH reader. Typically, the - // predefines buffer will be empty. - PP.setPredefines(Reader->getSuggestedPredefines()); - return Reader.take(); - - case PCHReader::Failure: - // Unrecoverable failure: don't even try to process the input file. - break; - - case PCHReader::IgnorePCH: - // No suitable PCH file could be found. Return an error. - break; - } - - return 0; -} - /// ProcessInputFile - Process a single input file with the specified state. static void ProcessInputFile(CompilerInstance &CI, const std::string &InFile, ProgActions PA) { @@ -509,7 +473,6 @@ static void ProcessInputFile(CompilerInstance &CI, const std::string &InFile, } } - llvm::OwningPtr Source; const std::string &ImplicitPCHInclude = CI.getPreprocessorOpts().getImplicitPCHInclude(); if (Consumer) { @@ -517,15 +480,9 @@ static void ProcessInputFile(CompilerInstance &CI, const std::string &InFile, CI.createASTContext(); if (!ImplicitPCHInclude.empty()) { - Source.reset(ReadPCHFile(ImplicitPCHInclude, - CI.getHeaderSearchOpts().Sysroot, PP, - CI.getASTContext())); - if (!Source) + CI.createPCHExternalASTSource(ImplicitPCHInclude); + if (!CI.getASTContext().getExternalSource()) 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. - CI.getASTContext().setExternalSource(Source); } else { // Initialize builtin info when not using PCH. PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),