25 #include "llvm/ADT/StringExtras.h" 26 #include "llvm/ADT/StringSet.h" 27 #include "llvm/Config/llvm-config.h" 28 #include "llvm/Support/CrashRecoveryContext.h" 29 #include "llvm/Support/FileSystem.h" 30 #include "llvm/Support/Mutex.h" 31 #include "llvm/Support/MutexGuard.h" 32 #include "llvm/Support/Process.h" 36 using namespace clang;
40 StringRef getInMemoryPreamblePath() {
41 #if defined(LLVM_ON_UNIX) 42 return "/__clang_tmp/___clang_inmemory_preamble___";
43 #elif defined(LLVM_ON_WIN32) 44 return "C:\\__clang_tmp\\___clang_inmemory_preamble___";
46 #warning "Unknown platform. Defaulting to UNIX-style paths for in-memory PCHs" 47 return "/__clang_tmp/___clang_inmemory_preamble___";
52 createVFSOverlayForPreamblePCH(StringRef PCHFilename,
53 std::unique_ptr<llvm::MemoryBuffer>
PCHBuffer,
59 PCHFS->addFile(PCHFilename, 0, std::move(PCHBuffer));
62 Overlay->pushOverlay(PCHFS);
67 class TemporaryFiles {
70 static TemporaryFiles &getInstance();
74 TemporaryFiles() =
default;
76 TemporaryFiles(
const TemporaryFiles &) =
delete;
82 void addFile(StringRef File);
85 void removeFile(StringRef File);
88 llvm::sys::SmartMutex<false> Mutex;
89 llvm::StringSet<> Files;
92 TemporaryFiles &TemporaryFiles::getInstance() {
93 static TemporaryFiles Instance;
97 TemporaryFiles::~TemporaryFiles() {
98 llvm::MutexGuard Guard(Mutex);
99 for (
const auto &File : Files)
100 llvm::sys::fs::remove(File.getKey());
103 void TemporaryFiles::addFile(StringRef File) {
104 llvm::MutexGuard Guard(Mutex);
105 auto IsInserted = Files.insert(File).second;
107 assert(IsInserted &&
"File has already been added");
110 void TemporaryFiles::removeFile(StringRef File) {
111 llvm::MutexGuard Guard(Mutex);
112 auto WasPresent = Files.erase(File);
114 assert(WasPresent &&
"File was not tracked");
115 llvm::sys::fs::remove(File);
120 PrecompilePreambleAction(std::string *InMemStorage,
122 : InMemStorage(InMemStorage), Callbacks(Callbacks) {}
125 StringRef InFile)
override;
127 bool hasEmittedPreamblePCH()
const {
return HasEmittedPreamblePCH; }
129 void setEmittedPreamblePCH(
ASTWriter &Writer) {
130 this->HasEmittedPreamblePCH =
true;
131 Callbacks.AfterPCHEmitted(Writer);
134 bool shouldEraseOutputFiles()
override {
return !hasEmittedPreamblePCH(); }
135 bool hasCodeCompletionSupport()
const override {
return false; }
136 bool hasASTFileSupport()
const override {
return false; }
140 friend class PrecompilePreambleConsumer;
142 bool HasEmittedPreamblePCH =
false;
143 std::string *InMemStorage;
147 class PrecompilePreambleConsumer :
public PCHGenerator {
149 PrecompilePreambleConsumer(PrecompilePreambleAction &Action,
151 std::unique_ptr<raw_ostream> Out)
155 Action(Action), Out(
std::move(Out)) {}
158 Action.Callbacks.HandleTopLevelDecl(DG);
162 void HandleTranslationUnit(
ASTContext &Ctx)
override {
164 if (!hasEmittedPCH())
173 getPCH() = std::move(Empty);
175 Action.setEmittedPreamblePCH(getWriter());
179 PrecompilePreambleAction &Action;
180 std::unique_ptr<raw_ostream> Out;
183 std::unique_ptr<ASTConsumer>
190 std::unique_ptr<llvm::raw_ostream> OS;
192 OS = llvm::make_unique<llvm::raw_string_ostream>(*InMemStorage);
194 std::string OutputFile;
203 return llvm::make_unique<PrecompilePreambleConsumer>(
207 template <
class T>
bool moveOnNoError(llvm::ErrorOr<T> Val,
T &Output) {
210 Output = std::move(*Val);
217 llvm::MemoryBuffer *Buffer,
226 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
bool StoreInMemory,
228 assert(VFS &&
"VFS is null");
233 auto PreambleInvocation = std::make_shared<CompilerInvocation>(Invocation);
236 PreambleInvocation->getPreprocessorOpts();
239 if (!StoreInMemory) {
242 llvm::ErrorOr<PrecompiledPreamble::TempPCHFile> PreamblePCHFile =
243 PrecompiledPreamble::TempPCHFile::CreateNewPreamblePCHFile();
244 if (!PreamblePCHFile)
246 TempFile = std::move(*PreamblePCHFile);
249 PCHStorage Storage = StoreInMemory ? PCHStorage(InMemoryPreamble())
250 : PCHStorage(std::move(*TempFile));
254 std::vector<char> PreambleBytes(MainFileBuffer->getBufferStart(),
255 MainFileBuffer->getBufferStart() +
261 FrontendOpts.OutputFile = StoreInMemory ? getInMemoryPreamblePath()
262 : Storage.asFile().getFilePath();
269 std::unique_ptr<CompilerInstance> Clang(
273 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> CICleanup(
276 Clang->setInvocation(std::move(PreambleInvocation));
277 Clang->setDiagnostics(&Diagnostics);
281 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
282 if (!Clang->hasTarget())
289 Clang->getTarget().adjust(Clang->getLangOpts());
291 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
292 "Invocation must have exactly one source file!");
293 assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() ==
295 "FIXME: AST inputs not yet supported here!");
296 assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() !=
298 "IR inputs not support here!");
310 Clang->setFileManager(
new FileManager(Clang->getFileSystemOpts(), VFS));
313 Clang->setSourceManager(
316 auto PreambleDepCollector = std::make_shared<DependencyCollector>();
317 Clang->addDependencyCollector(PreambleDepCollector);
320 StringRef MainFilePath = FrontendOpts.Inputs[0].getFile();
321 auto PreambleInputBuffer = llvm::MemoryBuffer::getMemBufferCopy(
322 MainFileBuffer->getBuffer().slice(0, Bounds.
Size), MainFilePath);
325 PreprocessorOpts.
addRemappedFile(MainFilePath, PreambleInputBuffer.get());
330 PreambleInputBuffer.release());
333 std::unique_ptr<PrecompilePreambleAction> Act;
334 Act.reset(
new PrecompilePreambleAction(
335 StoreInMemory ? &Storage.asMemory().Data :
nullptr, Callbacks));
337 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0]))
340 std::unique_ptr<PPCallbacks> DelegatedPPCallbacks =
342 if (DelegatedPPCallbacks)
343 Clang->getPreprocessor().addPPCallbacks(std::move(DelegatedPPCallbacks));
350 Act->EndSourceFile();
352 if (!Act->hasEmittedPreamblePCH())
357 llvm::StringMap<PrecompiledPreamble::PreambleFileHash> FilesInPreamble;
360 for (
auto &
Filename : PreambleDepCollector->getDependencies()) {
365 FilesInPreamble[File->
getName()] =
366 PrecompiledPreamble::PreambleFileHash::createForFile(File->
getSize(),
370 FilesInPreamble[File->
getName()] =
371 PrecompiledPreamble::PreambleFileHash::createForMemoryBuffer(Buffer);
376 PreambleEndsAtStartOfLine,
377 std::move(FilesInPreamble));
381 return PreambleBounds(PreambleBytes.size(), PreambleEndsAtStartOfLine);
385 switch (Storage.getKind()) {
386 case PCHStorage::Kind::Empty:
387 assert(
false &&
"Calling getSize() on invalid PrecompiledPreamble. " 388 "Was it std::moved?");
390 case PCHStorage::Kind::InMemory:
391 return Storage.asMemory().Data.size();
392 case PCHStorage::Kind::TempFile: {
394 if (llvm::sys::fs::file_size(Storage.asFile().getFilePath(),
Result))
398 "file size did not fit into size_t");
402 llvm_unreachable(
"Unhandled storage kind");
406 const llvm::MemoryBuffer *MainFileBuffer,
411 Bounds.
Size <= MainFileBuffer->getBufferSize() &&
412 "Buffer is too large. Bounds were calculated from a different buffer?");
414 auto PreambleInvocation = std::make_shared<CompilerInvocation>(Invocation);
416 PreambleInvocation->getPreprocessorOpts();
425 if (PreambleBytes.size() != Bounds.
Size ||
427 memcmp(PreambleBytes.data(), MainFileBuffer->getBufferStart(),
436 std::map<llvm::sys::fs::UniqueID, PreambleFileHash> OverriddenFiles;
437 for (
const auto &R : PreprocessorOpts.RemappedFiles) {
439 if (!moveOnNoError(VFS->
status(R.second), Status)) {
445 OverriddenFiles[Status.
getUniqueID()] = PreambleFileHash::createForFile(
449 for (
const auto &RB : PreprocessorOpts.RemappedFileBuffers) {
451 if (!moveOnNoError(VFS->
status(RB.first), Status))
455 PreambleFileHash::createForMemoryBuffer(RB.second);
459 for (
const auto &F : FilesInPreamble) {
461 if (!moveOnNoError(VFS->
status(F.first()), Status)) {
466 std::map<llvm::sys::fs::UniqueID, PreambleFileHash>::iterator Overridden =
468 if (Overridden != OverriddenFiles.end()) {
471 if (Overridden->second != F.second)
477 if (Status.
getSize() != uint64_t(F.second.Size) ||
487 llvm::MemoryBuffer *MainFileBuffer)
const {
488 assert(VFS &&
"VFS must not be null");
494 PreprocessorOpts.addRemappedFile(MainFilePath, MainFileBuffer);
497 PreprocessorOpts.PrecompiledPreambleBytes.first = PreambleBytes.size();
498 PreprocessorOpts.PrecompiledPreambleBytes.second = PreambleEndsAtStartOfLine;
499 PreprocessorOpts.DisablePCHValidation =
true;
501 setupPreambleStorage(Storage, PreprocessorOpts, VFS);
505 PCHStorage Storage, std::vector<char> PreambleBytes,
506 bool PreambleEndsAtStartOfLine,
507 llvm::StringMap<PreambleFileHash> FilesInPreamble)
508 : Storage(std::move(Storage)), FilesInPreamble(std::move(FilesInPreamble)),
509 PreambleBytes(std::move(PreambleBytes)),
510 PreambleEndsAtStartOfLine(PreambleEndsAtStartOfLine) {
511 assert(this->Storage.getKind() != PCHStorage::Kind::Empty);
514 llvm::ErrorOr<PrecompiledPreamble::TempPCHFile>
515 PrecompiledPreamble::TempPCHFile::CreateNewPreamblePCHFile() {
519 const char *TmpFile = ::getenv(
"CINDEXTEST_PREAMBLE_FILE");
521 return TempPCHFile::createFromCustomPath(TmpFile);
522 return TempPCHFile::createInSystemTempDir(
"preamble",
"pch");
525 llvm::ErrorOr<PrecompiledPreamble::TempPCHFile>
526 PrecompiledPreamble::TempPCHFile::createInSystemTempDir(
const Twine &Prefix,
533 auto EC = llvm::sys::fs::createTemporaryFile(Prefix, Suffix, FD, File);
537 llvm::sys::Process::SafelyCloseFileDescriptor(FD);
538 return TempPCHFile(std::move(File).str());
541 llvm::ErrorOr<PrecompiledPreamble::TempPCHFile>
542 PrecompiledPreamble::TempPCHFile::createFromCustomPath(
const Twine &Path) {
543 return TempPCHFile(Path.str());
546 PrecompiledPreamble::TempPCHFile::TempPCHFile(std::string FilePath)
547 : FilePath(std::move(FilePath)) {
548 TemporaryFiles::getInstance().addFile(*this->FilePath);
551 PrecompiledPreamble::TempPCHFile::TempPCHFile(TempPCHFile &&Other) {
552 FilePath = std::move(Other.FilePath);
553 Other.FilePath =
None;
556 PrecompiledPreamble::TempPCHFile &PrecompiledPreamble::TempPCHFile::
557 operator=(TempPCHFile &&Other) {
558 RemoveFileIfPresent();
560 FilePath = std::move(Other.FilePath);
561 Other.FilePath =
None;
565 PrecompiledPreamble::TempPCHFile::~TempPCHFile() { RemoveFileIfPresent(); }
567 void PrecompiledPreamble::TempPCHFile::RemoveFileIfPresent() {
569 TemporaryFiles::getInstance().removeFile(*FilePath);
574 llvm::StringRef PrecompiledPreamble::TempPCHFile::getFilePath()
const {
575 assert(FilePath &&
"TempPCHFile doesn't have a FilePath. Had it been moved?");
579 PrecompiledPreamble::PCHStorage::PCHStorage(TempPCHFile File)
580 : StorageKind(Kind::TempFile) {
581 new (&asFile()) TempPCHFile(std::move(File));
584 PrecompiledPreamble::PCHStorage::PCHStorage(InMemoryPreamble Memory)
585 : StorageKind(Kind::InMemory) {
586 new (&asMemory()) InMemoryPreamble(std::move(Memory));
589 PrecompiledPreamble::PCHStorage::PCHStorage(PCHStorage &&Other) : PCHStorage() {
590 *
this = std::move(Other);
593 PrecompiledPreamble::PCHStorage &PrecompiledPreamble::PCHStorage::
594 operator=(PCHStorage &&Other) {
597 StorageKind = Other.StorageKind;
598 switch (StorageKind) {
603 new (&asFile()) TempPCHFile(std::move(Other.asFile()));
606 new (&asMemory()) InMemoryPreamble(std::move(Other.asMemory()));
614 PrecompiledPreamble::PCHStorage::~PCHStorage() { destroy(); }
616 PrecompiledPreamble::PCHStorage::Kind
621 PrecompiledPreamble::TempPCHFile &PrecompiledPreamble::PCHStorage::asFile() {
622 assert(
getKind() == Kind::TempFile);
623 return *
reinterpret_cast<TempPCHFile *
>(Storage.buffer);
626 const PrecompiledPreamble::TempPCHFile &
627 PrecompiledPreamble::PCHStorage::asFile()
const {
628 return const_cast<PCHStorage *
>(
this)->asFile();
631 PrecompiledPreamble::InMemoryPreamble &
632 PrecompiledPreamble::PCHStorage::asMemory() {
633 assert(
getKind() == Kind::InMemory);
634 return *
reinterpret_cast<InMemoryPreamble *
>(Storage.buffer);
637 const PrecompiledPreamble::InMemoryPreamble &
638 PrecompiledPreamble::PCHStorage::asMemory()
const {
639 return const_cast<PCHStorage *
>(
this)->asMemory();
642 void PrecompiledPreamble::PCHStorage::destroy() {
643 switch (StorageKind) {
647 asFile().~TempPCHFile();
650 asMemory().~InMemoryPreamble();
655 void PrecompiledPreamble::PCHStorage::setEmpty() {
657 StorageKind = Kind::Empty;
660 PrecompiledPreamble::PreambleFileHash
661 PrecompiledPreamble::PreambleFileHash::createForFile(off_t Size,
665 Result.ModTime = ModTime;
670 PrecompiledPreamble::PreambleFileHash
671 PrecompiledPreamble::PreambleFileHash::createForMemoryBuffer(
672 const llvm::MemoryBuffer *Buffer) {
674 Result.Size = Buffer->getBufferSize();
678 MD5Ctx.update(Buffer->getBuffer().data());
679 MD5Ctx.final(Result.MD5);
684 void PrecompiledPreamble::setupPreambleStorage(
687 if (Storage.getKind() == PCHStorage::Kind::TempFile) {
688 const TempPCHFile &PCHFile = Storage.asFile();
693 auto PCHPath = PCHFile.getFilePath();
694 if (VFS == RealFS || VFS->exists(PCHPath))
696 auto Buf = RealFS->getBufferForFile(PCHPath);
706 VFS = createVFSOverlayForPreamblePCH(PCHPath, std::move(*Buf), VFS);
708 assert(Storage.getKind() == PCHStorage::Kind::InMemory);
711 StringRef PCHPath = getInMemoryPreamblePath();
714 auto Buf = llvm::MemoryBuffer::getMemBuffer(Storage.asMemory().Data);
715 VFS = createVFSOverlayForPreamblePCH(PCHPath, std::move(Buf), VFS);
732 return "build-preamble.error";
736 switch (static_cast<BuildPreambleError>(condition)) {
738 return "Preamble is empty";
740 return "Could not create temporary file for PCH";
742 return "CreateTargetInfo() return null";
744 return "Could not create VFS Overlay";
746 return "BeginSourceFile() return an error";
748 return "Could not emit PCH";
750 llvm_unreachable(
"unexpected BuildPreambleError");
std::size_t getSize() const
Returns the size, in bytes, that preamble takes on disk or in memory.
Describes the bounds (start, size) of the preamble and a flag required by PreprocessorOptions::Precom...
Implements support for file system lookup, file system caching, and directory search management...
__SIZE_TYPE__ size_t
The unsigned integer type of the result of the sizeof operator.
time_t getModificationTime() const
IntrusiveRefCntPtr< FileSystem > getRealFileSystem()
Gets an vfs::FileSystem for the 'real' file system, as seen by the operating system.
The translation unit is a prefix to a translation unit, and is not complete.
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
void addRemappedFile(StringRef From, StringRef To)
void AddImplicitPreamble(CompilerInvocation &CI, IntrusiveRefCntPtr< vfs::FileSystem > &VFS, llvm::MemoryBuffer *MainFileBuffer) const
Changes options inside CI to use PCH from this preamble.
The virtual file system interface.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
void HandleTranslationUnit(ASTContext &Ctx) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
static llvm::ErrorOr< PrecompiledPreamble > Build(const CompilerInvocation &Invocation, const llvm::MemoryBuffer *MainFileBuffer, PreambleBounds Bounds, DiagnosticsEngine &Diagnostics, IntrusiveRefCntPtr< vfs::FileSystem > VFS, std::shared_ptr< PCHContainerOperations > PCHContainerOps, bool StoreInMemory, PreambleCallbacks &Callbacks)
Try to build PrecompiledPreamble for Invocation.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
An in-memory file system.
unsigned RelocatablePCH
When generating PCH files, instruct the AST writer to create relocatable PCH files.
static bool ComputeASTConsumerArguments(CompilerInstance &CI, std::string &Sysroot)
Compute the AST consumer arguments that will be used to create the PCHGenerator instance returned by ...
A file system that allows overlaying one AbstractFileSystem on top of another.
virtual llvm::ErrorOr< Status > status(const Twine &Path)=0
Get the status of the entry at Path, if one exists.
std::error_code make_error_code(BuildPreambleError Error)
FrontendOptions & getFrontendOpts()
Concrete class used by the front-end to report problems and issues.
bool RetainRemappedFileBuffers
Whether the compiler instance should retain (i.e., not free) the buffers associated with remapped fil...
The result of a status operation.
void Reset()
Reset the state of the diagnostic object to its initial configuration.
FrontendOptions & getFrontendOpts()
bool PreambleEndsAtStartOfLine
Whether the preamble ends at the start of a new line.
A set of callbacks to gather useful information while building a preamble.
const char * name() const noexcept override
PreambleBounds getBounds() const
PreambleBounds used to build the preamble.
bool CanReuse(const CompilerInvocation &Invocation, const llvm::MemoryBuffer *MainFileBuffer, PreambleBounds Bounds, vfs::FileSystem *VFS) const
Check whether PrecompiledPreamble can be reused for the new contents(MainFileBuffer) of the main file...
static PreambleBounds ComputePreamble(StringRef Buffer, const LangOptions &LangOpts, unsigned MaxLines=0)
Compute the preamble of the given file.
const FileEntry * getFileEntryForID(FileID FID) const
Returns the FileEntry record for the provided FileID.
const FunctionProtoType * T
IntrusiveRefCntPtr< vfs::FileSystem > createVFSFromCompilerInvocation(const CompilerInvocation &CI, DiagnosticsEngine &Diags)
static TargetInfo * CreateTargetInfo(DiagnosticsEngine &Diags, const std::shared_ptr< TargetOptions > &Opts)
Construct a target for the given options.
bool GeneratePreamble
True indicates that a preamble is being generated.
PreambleBounds ComputePreambleBounds(const LangOptions &LangOpts, llvm::MemoryBuffer *Buffer, unsigned MaxLines)
Runs lexer to compute suggested preamble bounds.
The result type of a method or function.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
StringRef getName() const
std::string ImplicitPCHInclude
The implicit PCH included at the start of the translation unit, or empty.
std::vector< FrontendInputFile > Inputs
The input files and their types.
Cached information about one file (either on disk or in the virtual file system). ...
llvm::sys::TimePoint getLastModificationTime() const
void ProcessWarningOptions(DiagnosticsEngine &Diags, const DiagnosticOptions &Opts, bool ReportDiags=true)
ProcessWarningOptions - Initialize the diagnostic client and process the warning options specified on...
Abstract base class to use for AST consumer-based frontend actions.
virtual void BeforeExecute(CompilerInstance &CI)
Called before FrontendAction::BeginSourceFile.
virtual void AfterPCHEmitted(ASTWriter &Writer)
Called after PCH has been emitted.
llvm::MemoryBuffer * getMemoryBufferForFile(const FileEntry *File, bool *Invalid=nullptr)
Retrieve the memory buffer associated with the given file.
PrecompiledPreamble(PrecompiledPreamble &&)=default
std::string message(int condition) const override
Dataflow Directional Tag Classes.
A class holding a PCH and all information to check whether it is valid to reuse the PCH for the subse...
PreprocessorOptions & getPreprocessorOpts()
FileID getMainFileID() const
Returns the FileID of the main source file.
Helper class for holding the data necessary to invoke the compiler.
Defines the virtual file system interface vfs::FileSystem.
FrontendOptions - Options for controlling the behavior of the frontend.
virtual void AfterExecute(CompilerInstance &CI)
Called after FrontendAction::Execute(), but before FrontendAction::EndSourceFile().
unsigned Size
Size of the preamble in bytes.
llvm::sys::fs::UniqueID getUniqueID() const
char __ovld __cnfn max(char x, char y)
Returns y if x < y, otherwise it returns x.
Generate pre-compiled header.
Preprocessor & getPreprocessor() const
Return the current preprocessor.
TranslationUnitKind
Describes the kind of translation unit being processed.
Writes an AST file containing the contents of a translation unit.
std::pair< unsigned, bool > PrecompiledPreambleBytes
If non-zero, the implicit PCH include is actually a precompiled preamble that covers this number of b...
Defines the clang::TargetInfo interface.
static Decl::Kind getKind(const Decl *D)
An abstract superclass that describes a custom extension to the module/precompiled header file format...
AST and semantic-analysis consumer that generates a precompiled header from the parsed source code...
static std::unique_ptr< llvm::raw_pwrite_stream > CreateOutputFile(CompilerInstance &CI, StringRef InFile, std::string &OutputFile)
Creates file to write the PCH into and returns a stream to write it into.
virtual void HandleTopLevelDecl(DeclGroupRef DG)
Called for each TopLevelDecl.
This class handles loading and caching of source files into memory.
virtual std::unique_ptr< PPCallbacks > createPPCallbacks()
Creates wrapper class for PPCallbacks so we can also process information about includes that are insi...
Engages in a tight little dance with the lexer to efficiently preprocess tokens.