#include "clang/Frontend/PrecompiledPreamble.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/LangStandard.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/CompilerInvocation.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Frontend/FrontendOptions.h"
#include "clang/Lex/HeaderSearch.h"
#include "clang/Lex/Lexer.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/PreprocessorOptions.h"
#include "clang/Serialization/ASTWriter.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/Support/CrashRecoveryContext.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/VirtualFileSystem.h"
#include <limits>
#include <mutex>
#include <utility>
using namespace clang;
namespace {
StringRef getInMemoryPreamblePath() {
#if defined(LLVM_ON_UNIX)
return "/__clang_tmp/___clang_inmemory_preamble___";
#elif defined(_WIN32)
return "C:\\__clang_tmp\\___clang_inmemory_preamble___";
#else
#warning "Unknown platform. Defaulting to UNIX-style paths for in-memory PCHs"
return "/__clang_tmp/___clang_inmemory_preamble___";
#endif
}
IntrusiveRefCntPtr<llvm::vfs::FileSystem>
createVFSOverlayForPreamblePCH(StringRef PCHFilename,
std::unique_ptr<llvm::MemoryBuffer> PCHBuffer,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> PCHFS(
new llvm::vfs::InMemoryFileSystem());
PCHFS->addFile(PCHFilename, 0, std::move(PCHBuffer));
IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> Overlay(
new llvm::vfs::OverlayFileSystem(VFS));
Overlay->pushOverlay(PCHFS);
return Overlay;
}
class PreambleDependencyCollector : public DependencyCollector {
public:
bool needSystemDependencies() override { return true; }
};
class MissingFileCollector : public PPCallbacks {
llvm::StringSet<> &Out;
const HeaderSearch &Search;
const SourceManager &SM;
public:
MissingFileCollector(llvm::StringSet<> &Out, const HeaderSearch &Search,
const SourceManager &SM)
: Out(Out), Search(Search), SM(SM) {}
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
CharSourceRange FilenameRange,
Optional<FileEntryRef> File, StringRef SearchPath,
StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override {
if (File)
return;
if (llvm::sys::path::is_absolute(FileName)) {
Out.insert(FileName);
return;
}
llvm::SmallString<256> Buf;
auto NotFoundRelativeTo = [&](const DirectoryEntry *DE) {
Buf = DE->getName();
llvm::sys::path::append(Buf, FileName);
llvm::sys::path::remove_dots(Buf, true);
Out.insert(Buf);
};
if (!IsAngled) {
if (const FileEntry *IncludingFile =
SM.getFileEntryForID(SM.getFileID(IncludeTok.getLocation())))
if (IncludingFile->getDir())
NotFoundRelativeTo(IncludingFile->getDir());
}
for (const auto &Dir : llvm::make_range(
IsAngled ? Search.angled_dir_begin() : Search.search_dir_begin(),
Search.search_dir_end())) {
if (Dir.isNormalDir())
NotFoundRelativeTo(Dir.getDir());
}
}
};
class TemporaryFiles {
public:
static TemporaryFiles &getInstance();
private:
TemporaryFiles() = default;
TemporaryFiles(const TemporaryFiles &) = delete;
public:
~TemporaryFiles();
void addFile(StringRef File);
void removeFile(StringRef File);
private:
std::mutex Mutex;
llvm::StringSet<> Files;
};
TemporaryFiles &TemporaryFiles::getInstance() {
static TemporaryFiles Instance;
return Instance;
}
TemporaryFiles::~TemporaryFiles() {
std::lock_guard<std::mutex> Guard(Mutex);
for (const auto &File : Files)
llvm::sys::fs::remove(File.getKey());
}
void TemporaryFiles::addFile(StringRef File) {
std::lock_guard<std::mutex> Guard(Mutex);
auto IsInserted = Files.insert(File).second;
(void)IsInserted;
assert(IsInserted && "File has already been added");
}
void TemporaryFiles::removeFile(StringRef File) {
std::lock_guard<std::mutex> Guard(Mutex);
auto WasPresent = Files.erase(File);
(void)WasPresent;
assert(WasPresent && "File was not tracked");
llvm::sys::fs::remove(File);
}
class TempPCHFile {
public:
static std::unique_ptr<TempPCHFile> create() {
if (const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE"))
return std::unique_ptr<TempPCHFile>(new TempPCHFile(TmpFile));
llvm::SmallString<64> File;
int FD;
if (auto EC =
llvm::sys::fs::createTemporaryFile("preamble", "pch", FD, File))
return nullptr;
llvm::sys::Process::SafelyCloseFileDescriptor(FD);
return std::unique_ptr<TempPCHFile>(new TempPCHFile(File.str().str()));
}
TempPCHFile &operator=(const TempPCHFile &) = delete;
TempPCHFile(const TempPCHFile &) = delete;
~TempPCHFile() { TemporaryFiles::getInstance().removeFile(FilePath); };
llvm::StringRef getFilePath() const { return FilePath; };
private:
TempPCHFile(std::string FilePath) : FilePath(std::move(FilePath)) {
TemporaryFiles::getInstance().addFile(this->FilePath);
}
std::string FilePath;
};
class PrecompilePreambleAction : public ASTFrontendAction {
public:
PrecompilePreambleAction(std::shared_ptr<PCHBuffer> Buffer, bool WritePCHFile,
PreambleCallbacks &Callbacks)
: Buffer(std::move(Buffer)), WritePCHFile(WritePCHFile),
Callbacks(Callbacks) {}
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
StringRef InFile) override;
bool hasEmittedPreamblePCH() const { return HasEmittedPreamblePCH; }
void setEmittedPreamblePCH(ASTWriter &Writer) {
if (FileOS) {
*FileOS << Buffer->Data;
FileOS.reset();
}
this->HasEmittedPreamblePCH = true;
Callbacks.AfterPCHEmitted(Writer);
}
bool BeginSourceFileAction(CompilerInstance &CI) override {
assert(CI.getLangOpts().CompilingPCH);
return ASTFrontendAction::BeginSourceFileAction(CI);
}
bool shouldEraseOutputFiles() override { return !hasEmittedPreamblePCH(); }
bool hasCodeCompletionSupport() const override { return false; }
bool hasASTFileSupport() const override { return false; }
TranslationUnitKind getTranslationUnitKind() override { return TU_Prefix; }
private:
friend class PrecompilePreambleConsumer;
bool HasEmittedPreamblePCH = false;
std::shared_ptr<PCHBuffer> Buffer;
bool WritePCHFile; std::unique_ptr<llvm::raw_pwrite_stream> FileOS; PreambleCallbacks &Callbacks;
};
class PrecompilePreambleConsumer : public PCHGenerator {
public:
PrecompilePreambleConsumer(PrecompilePreambleAction &Action,
const Preprocessor &PP,
InMemoryModuleCache &ModuleCache,
StringRef isysroot,
std::shared_ptr<PCHBuffer> Buffer)
: PCHGenerator(PP, ModuleCache, "", isysroot, std::move(Buffer),
ArrayRef<std::shared_ptr<ModuleFileExtension>>(),
true),
Action(Action) {}
bool HandleTopLevelDecl(DeclGroupRef DG) override {
Action.Callbacks.HandleTopLevelDecl(DG);
return true;
}
void HandleTranslationUnit(ASTContext &Ctx) override {
PCHGenerator::HandleTranslationUnit(Ctx);
if (!hasEmittedPCH())
return;
Action.setEmittedPreamblePCH(getWriter());
}
bool shouldSkipFunctionBody(Decl *D) override {
return Action.Callbacks.shouldSkipFunctionBody(D);
}
private:
PrecompilePreambleAction &Action;
};
std::unique_ptr<ASTConsumer>
PrecompilePreambleAction::CreateASTConsumer(CompilerInstance &CI,
StringRef InFile) {
std::string Sysroot;
if (!GeneratePCHAction::ComputeASTConsumerArguments(CI, Sysroot))
return nullptr;
if (WritePCHFile) {
std::string OutputFile; FileOS = GeneratePCHAction::CreateOutputFile(CI, InFile, OutputFile);
if (!FileOS)
return nullptr;
}
if (!CI.getFrontendOpts().RelocatablePCH)
Sysroot.clear();
return std::make_unique<PrecompilePreambleConsumer>(
*this, CI.getPreprocessor(), CI.getModuleCache(), Sysroot, Buffer);
}
template <class T> bool moveOnNoError(llvm::ErrorOr<T> Val, T &Output) {
if (!Val)
return false;
Output = std::move(*Val);
return true;
}
}
PreambleBounds clang::ComputePreambleBounds(const LangOptions &LangOpts,
const llvm::MemoryBufferRef &Buffer,
unsigned MaxLines) {
return Lexer::ComputePreamble(Buffer.getBuffer(), LangOpts, MaxLines);
}
class PrecompiledPreamble::PCHStorage {
public:
static std::unique_ptr<PCHStorage> file(std::unique_ptr<TempPCHFile> File) {
assert(File);
std::unique_ptr<PCHStorage> S(new PCHStorage());
S->File = std::move(File);
return S;
}
static std::unique_ptr<PCHStorage> inMemory(std::shared_ptr<PCHBuffer> Buf) {
std::unique_ptr<PCHStorage> S(new PCHStorage());
S->Memory = std::move(Buf);
return S;
}
enum class Kind { InMemory, TempFile };
Kind getKind() const {
if (Memory)
return Kind::InMemory;
if (File)
return Kind::TempFile;
llvm_unreachable("Neither Memory nor File?");
}
llvm::StringRef filePath() const {
assert(getKind() == Kind::TempFile);
return File->getFilePath();
}
llvm::StringRef memoryContents() const {
assert(getKind() == Kind::InMemory);
return StringRef(Memory->Data.data(), Memory->Data.size());
}
void shrink() {
if (!Memory)
return;
Memory->Data = decltype(Memory->Data)(Memory->Data);
}
private:
PCHStorage() = default;
PCHStorage(const PCHStorage &) = delete;
PCHStorage &operator=(const PCHStorage &) = delete;
std::shared_ptr<PCHBuffer> Memory;
std::unique_ptr<TempPCHFile> File;
};
PrecompiledPreamble::~PrecompiledPreamble() = default;
PrecompiledPreamble::PrecompiledPreamble(PrecompiledPreamble &&) = default;
PrecompiledPreamble &
PrecompiledPreamble::operator=(PrecompiledPreamble &&) = default;
llvm::ErrorOr<PrecompiledPreamble> PrecompiledPreamble::Build(
const CompilerInvocation &Invocation,
const llvm::MemoryBuffer *MainFileBuffer, PreambleBounds Bounds,
DiagnosticsEngine &Diagnostics,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
std::shared_ptr<PCHContainerOperations> PCHContainerOps, bool StoreInMemory,
PreambleCallbacks &Callbacks) {
assert(VFS && "VFS is null");
auto PreambleInvocation = std::make_shared<CompilerInvocation>(Invocation);
FrontendOptions &FrontendOpts = PreambleInvocation->getFrontendOpts();
PreprocessorOptions &PreprocessorOpts =
PreambleInvocation->getPreprocessorOpts();
std::shared_ptr<PCHBuffer> Buffer = std::make_shared<PCHBuffer>();
std::unique_ptr<PCHStorage> Storage;
if (StoreInMemory) {
Storage = PCHStorage::inMemory(Buffer);
} else {
std::unique_ptr<TempPCHFile> PreamblePCHFile = TempPCHFile::create();
if (!PreamblePCHFile)
return BuildPreambleError::CouldntCreateTempFile;
Storage = PCHStorage::file(std::move(PreamblePCHFile));
}
std::vector<char> PreambleBytes(MainFileBuffer->getBufferStart(),
MainFileBuffer->getBufferStart() +
Bounds.Size);
bool PreambleEndsAtStartOfLine = Bounds.PreambleEndsAtStartOfLine;
FrontendOpts.ProgramAction = frontend::GeneratePCH;
FrontendOpts.OutputFile = std::string(
StoreInMemory ? getInMemoryPreamblePath() : Storage->filePath());
PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
PreprocessorOpts.PrecompiledPreambleBytes.second = false;
PreprocessorOpts.GeneratePreamble = true;
std::unique_ptr<CompilerInstance> Clang(
new CompilerInstance(std::move(PCHContainerOps)));
llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> CICleanup(
Clang.get());
Clang->setInvocation(std::move(PreambleInvocation));
Clang->setDiagnostics(&Diagnostics);
if (!Clang->createTarget())
return BuildPreambleError::CouldntCreateTargetInfo;
if (Clang->getFrontendOpts().Inputs.size() != 1 ||
Clang->getFrontendOpts().Inputs[0].getKind().getFormat() !=
InputKind::Source ||
Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() ==
Language::LLVM_IR) {
return BuildPreambleError::BadInputs;
}
Diagnostics.Reset();
ProcessWarningOptions(Diagnostics, Clang->getDiagnosticOpts());
VFS =
createVFSFromCompilerInvocation(Clang->getInvocation(), Diagnostics, VFS);
Clang->setFileManager(new FileManager(Clang->getFileSystemOpts(), VFS));
Clang->setSourceManager(
new SourceManager(Diagnostics, Clang->getFileManager()));
auto PreambleDepCollector = std::make_shared<PreambleDependencyCollector>();
Clang->addDependencyCollector(PreambleDepCollector);
Clang->getLangOpts().CompilingPCH = true;
StringRef MainFilePath = FrontendOpts.Inputs[0].getFile();
auto PreambleInputBuffer = llvm::MemoryBuffer::getMemBufferCopy(
MainFileBuffer->getBuffer().slice(0, Bounds.Size), MainFilePath);
if (PreprocessorOpts.RetainRemappedFileBuffers) {
PreprocessorOpts.addRemappedFile(MainFilePath, PreambleInputBuffer.get());
} else {
PreprocessorOpts.addRemappedFile(MainFilePath,
PreambleInputBuffer.release());
}
auto Act = std::make_unique<PrecompilePreambleAction>(
std::move(Buffer),
Storage->getKind() == PCHStorage::Kind::TempFile,
Callbacks);
if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0]))
return BuildPreambleError::BeginSourceFileFailed;
Callbacks.BeforeExecute(*Clang);
std::unique_ptr<PPCallbacks> DelegatedPPCallbacks =
Callbacks.createPPCallbacks();
if (DelegatedPPCallbacks)
Clang->getPreprocessor().addPPCallbacks(std::move(DelegatedPPCallbacks));
if (auto CommentHandler = Callbacks.getCommentHandler())
Clang->getPreprocessor().addCommentHandler(CommentHandler);
llvm::StringSet<> MissingFiles;
Clang->getPreprocessor().addPPCallbacks(
std::make_unique<MissingFileCollector>(
MissingFiles, Clang->getPreprocessor().getHeaderSearchInfo(),
Clang->getSourceManager()));
if (llvm::Error Err = Act->Execute())
return errorToErrorCode(std::move(Err));
Callbacks.AfterExecute(*Clang);
Act->EndSourceFile();
if (!Act->hasEmittedPreamblePCH())
return BuildPreambleError::CouldntEmitPCH;
Act.reset();
llvm::StringMap<PrecompiledPreamble::PreambleFileHash> FilesInPreamble;
SourceManager &SourceMgr = Clang->getSourceManager();
for (auto &Filename : PreambleDepCollector->getDependencies()) {
auto FileOrErr = Clang->getFileManager().getFile(Filename);
if (!FileOrErr ||
*FileOrErr == SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()))
continue;
auto File = *FileOrErr;
if (time_t ModTime = File->getModificationTime()) {
FilesInPreamble[File->getName()] =
PrecompiledPreamble::PreambleFileHash::createForFile(File->getSize(),
ModTime);
} else {
llvm::MemoryBufferRef Buffer =
SourceMgr.getMemoryBufferForFileOrFake(File);
FilesInPreamble[File->getName()] =
PrecompiledPreamble::PreambleFileHash::createForMemoryBuffer(Buffer);
}
}
CICleanup.unregister();
Clang.reset();
Storage->shrink();
return PrecompiledPreamble(
std::move(Storage), std::move(PreambleBytes), PreambleEndsAtStartOfLine,
std::move(FilesInPreamble), std::move(MissingFiles));
}
PreambleBounds PrecompiledPreamble::getBounds() const {
return PreambleBounds(PreambleBytes.size(), PreambleEndsAtStartOfLine);
}
std::size_t PrecompiledPreamble::getSize() const {
switch (Storage->getKind()) {
case PCHStorage::Kind::InMemory:
return Storage->memoryContents().size();
case PCHStorage::Kind::TempFile: {
uint64_t Result;
if (llvm::sys::fs::file_size(Storage->filePath(), Result))
return 0;
assert(Result <= std::numeric_limits<std::size_t>::max() &&
"file size did not fit into size_t");
return Result;
}
}
llvm_unreachable("Unhandled storage kind");
}
bool PrecompiledPreamble::CanReuse(const CompilerInvocation &Invocation,
const llvm::MemoryBufferRef &MainFileBuffer,
PreambleBounds Bounds,
llvm::vfs::FileSystem &VFS) const {
assert(
Bounds.Size <= MainFileBuffer.getBufferSize() &&
"Buffer is too large. Bounds were calculated from a different buffer?");
auto PreambleInvocation = std::make_shared<CompilerInvocation>(Invocation);
PreprocessorOptions &PreprocessorOpts =
PreambleInvocation->getPreprocessorOpts();
if (PreambleBytes.size() != Bounds.Size ||
PreambleEndsAtStartOfLine != Bounds.PreambleEndsAtStartOfLine ||
!std::equal(PreambleBytes.begin(), PreambleBytes.end(),
MainFileBuffer.getBuffer().begin()))
return false;
std::map<llvm::sys::fs::UniqueID, PreambleFileHash> OverriddenFiles;
llvm::StringSet<> OverriddenAbsPaths; for (const auto &R : PreprocessorOpts.RemappedFiles) {
llvm::vfs::Status Status;
if (!moveOnNoError(VFS.status(R.second), Status)) {
return false;
}
llvm::SmallString<128> MappedPath(R.first);
if (!VFS.makeAbsolute(MappedPath))
OverriddenAbsPaths.insert(MappedPath);
OverriddenFiles[Status.getUniqueID()] = PreambleFileHash::createForFile(
Status.getSize(), llvm::sys::toTimeT(Status.getLastModificationTime()));
}
llvm::StringMap<PreambleFileHash> OverridenFileBuffers;
for (const auto &RB : PreprocessorOpts.RemappedFileBuffers) {
const PrecompiledPreamble::PreambleFileHash PreambleHash =
PreambleFileHash::createForMemoryBuffer(RB.second->getMemBufferRef());
llvm::vfs::Status Status;
if (moveOnNoError(VFS.status(RB.first), Status))
OverriddenFiles[Status.getUniqueID()] = PreambleHash;
else
OverridenFileBuffers[RB.first] = PreambleHash;
llvm::SmallString<128> MappedPath(RB.first);
if (!VFS.makeAbsolute(MappedPath))
OverriddenAbsPaths.insert(MappedPath);
}
for (const auto &F : FilesInPreamble) {
auto OverridenFileBuffer = OverridenFileBuffers.find(F.first());
if (OverridenFileBuffer != OverridenFileBuffers.end()) {
if (OverridenFileBuffer->second != F.second)
return false;
continue;
}
llvm::vfs::Status Status;
if (!moveOnNoError(VFS.status(F.first()), Status)) {
return false;
}
std::map<llvm::sys::fs::UniqueID, PreambleFileHash>::iterator Overridden =
OverriddenFiles.find(Status.getUniqueID());
if (Overridden != OverriddenFiles.end()) {
if (Overridden->second != F.second)
return false;
continue;
}
if (Status.getSize() != uint64_t(F.second.Size) ||
llvm::sys::toTimeT(Status.getLastModificationTime()) !=
F.second.ModTime)
return false;
}
for (const auto &F : MissingFiles) {
if (OverriddenAbsPaths.count(F.getKey()))
return false;
if (auto Status = VFS.status(F.getKey())) {
if (Status->isRegularFile())
return false;
}
}
return true;
}
void PrecompiledPreamble::AddImplicitPreamble(
CompilerInvocation &CI, IntrusiveRefCntPtr<llvm::vfs::FileSystem> &VFS,
llvm::MemoryBuffer *MainFileBuffer) const {
PreambleBounds Bounds(PreambleBytes.size(), PreambleEndsAtStartOfLine);
configurePreamble(Bounds, CI, VFS, MainFileBuffer);
}
void PrecompiledPreamble::OverridePreamble(
CompilerInvocation &CI, IntrusiveRefCntPtr<llvm::vfs::FileSystem> &VFS,
llvm::MemoryBuffer *MainFileBuffer) const {
auto Bounds = ComputePreambleBounds(*CI.getLangOpts(), *MainFileBuffer, 0);
configurePreamble(Bounds, CI, VFS, MainFileBuffer);
}
PrecompiledPreamble::PrecompiledPreamble(
std::unique_ptr<PCHStorage> Storage, std::vector<char> PreambleBytes,
bool PreambleEndsAtStartOfLine,
llvm::StringMap<PreambleFileHash> FilesInPreamble,
llvm::StringSet<> MissingFiles)
: Storage(std::move(Storage)), FilesInPreamble(std::move(FilesInPreamble)),
MissingFiles(std::move(MissingFiles)),
PreambleBytes(std::move(PreambleBytes)),
PreambleEndsAtStartOfLine(PreambleEndsAtStartOfLine) {
assert(this->Storage != nullptr);
}
PrecompiledPreamble::PreambleFileHash
PrecompiledPreamble::PreambleFileHash::createForFile(off_t Size,
time_t ModTime) {
PreambleFileHash Result;
Result.Size = Size;
Result.ModTime = ModTime;
Result.MD5 = {};
return Result;
}
PrecompiledPreamble::PreambleFileHash
PrecompiledPreamble::PreambleFileHash::createForMemoryBuffer(
const llvm::MemoryBufferRef &Buffer) {
PreambleFileHash Result;
Result.Size = Buffer.getBufferSize();
Result.ModTime = 0;
llvm::MD5 MD5Ctx;
MD5Ctx.update(Buffer.getBuffer().data());
MD5Ctx.final(Result.MD5);
return Result;
}
void PrecompiledPreamble::configurePreamble(
PreambleBounds Bounds, CompilerInvocation &CI,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> &VFS,
llvm::MemoryBuffer *MainFileBuffer) const {
assert(VFS);
auto &PreprocessorOpts = CI.getPreprocessorOpts();
auto MainFilePath = CI.getFrontendOpts().Inputs[0].getFile();
PreprocessorOpts.addRemappedFile(MainFilePath, MainFileBuffer);
PreprocessorOpts.PrecompiledPreambleBytes.first = Bounds.Size;
PreprocessorOpts.PrecompiledPreambleBytes.second =
Bounds.PreambleEndsAtStartOfLine;
PreprocessorOpts.DisablePCHOrModuleValidation =
DisableValidationForModuleKind::PCH;
PreprocessorOpts.UsePredefines = false;
setupPreambleStorage(*Storage, PreprocessorOpts, VFS);
}
void PrecompiledPreamble::setupPreambleStorage(
const PCHStorage &Storage, PreprocessorOptions &PreprocessorOpts,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> &VFS) {
if (Storage.getKind() == PCHStorage::Kind::TempFile) {
llvm::StringRef PCHPath = Storage.filePath();
PreprocessorOpts.ImplicitPCHInclude = PCHPath.str();
IntrusiveRefCntPtr<llvm::vfs::FileSystem> RealFS =
llvm::vfs::getRealFileSystem();
if (VFS == RealFS || VFS->exists(PCHPath))
return;
auto Buf = RealFS->getBufferForFile(PCHPath);
if (!Buf) {
return;
}
VFS = createVFSOverlayForPreamblePCH(PCHPath, std::move(*Buf), VFS);
} else {
assert(Storage.getKind() == PCHStorage::Kind::InMemory);
StringRef PCHPath = getInMemoryPreamblePath();
PreprocessorOpts.ImplicitPCHInclude = std::string(PCHPath);
auto Buf = llvm::MemoryBuffer::getMemBuffer(
Storage.memoryContents(), PCHPath, false);
VFS = createVFSOverlayForPreamblePCH(PCHPath, std::move(Buf), VFS);
}
}
void PreambleCallbacks::BeforeExecute(CompilerInstance &CI) {}
void PreambleCallbacks::AfterExecute(CompilerInstance &CI) {}
void PreambleCallbacks::AfterPCHEmitted(ASTWriter &Writer) {}
void PreambleCallbacks::HandleTopLevelDecl(DeclGroupRef DG) {}
std::unique_ptr<PPCallbacks> PreambleCallbacks::createPPCallbacks() {
return nullptr;
}
CommentHandler *PreambleCallbacks::getCommentHandler() { return nullptr; }
static llvm::ManagedStatic<BuildPreambleErrorCategory> BuildPreambleErrCategory;
std::error_code clang::make_error_code(BuildPreambleError Error) {
return std::error_code(static_cast<int>(Error), *BuildPreambleErrCategory);
}
const char *BuildPreambleErrorCategory::name() const noexcept {
return "build-preamble.error";
}
std::string BuildPreambleErrorCategory::message(int condition) const {
switch (static_cast<BuildPreambleError>(condition)) {
case BuildPreambleError::CouldntCreateTempFile:
return "Could not create temporary file for PCH";
case BuildPreambleError::CouldntCreateTargetInfo:
return "CreateTargetInfo() return null";
case BuildPreambleError::BeginSourceFileFailed:
return "BeginSourceFile() return an error";
case BuildPreambleError::CouldntEmitPCH:
return "Could not emit PCH";
case BuildPreambleError::BadInputs:
return "Command line arguments must contain exactly one source file";
}
llvm_unreachable("unexpected BuildPreambleError");
}