Add CompilerInstance::createPCHExternalASTSource.

llvm-svn: 87097
This commit is contained in:
Daniel Dunbar 2009-11-13 08:21:10 +00:00
parent adf6c242a0
commit 599313ef94
3 changed files with 55 additions and 46 deletions

View File

@ -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 <cassert>
@ -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);
/// }
};

View File

@ -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<ExternalASTSource> 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<PCHReader> 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;
}

View File

@ -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<PCHReader> 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<ExternalASTSource> 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(),