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___";
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);
73 bool needSystemDependencies()
override {
return true; }
77 class TemporaryFiles {
80 static TemporaryFiles &getInstance();
84 TemporaryFiles() =
default;
86 TemporaryFiles(
const TemporaryFiles &) =
delete;
92 void addFile(StringRef File);
95 void removeFile(StringRef File);
98 llvm::sys::SmartMutex<false> Mutex;
99 llvm::StringSet<> Files;
102 TemporaryFiles &TemporaryFiles::getInstance() {
103 static TemporaryFiles Instance;
107 TemporaryFiles::~TemporaryFiles() {
108 llvm::MutexGuard Guard(Mutex);
109 for (
const auto &File : Files)
110 llvm::sys::fs::remove(File.getKey());
113 void TemporaryFiles::addFile(StringRef File) {
114 llvm::MutexGuard Guard(Mutex);
115 auto IsInserted = Files.insert(File).second;
117 assert(IsInserted &&
"File has already been added");
120 void TemporaryFiles::removeFile(StringRef File) {
121 llvm::MutexGuard Guard(Mutex);
122 auto WasPresent = Files.erase(File);
124 assert(WasPresent &&
"File was not tracked");
125 llvm::sys::fs::remove(File);
130 PrecompilePreambleAction(std::string *InMemStorage,
132 : InMemStorage(InMemStorage), Callbacks(Callbacks) {}
135 StringRef InFile)
override;
137 bool hasEmittedPreamblePCH()
const {
return HasEmittedPreamblePCH; }
139 void setEmittedPreamblePCH(
ASTWriter &Writer) {
140 this->HasEmittedPreamblePCH =
true;
141 Callbacks.AfterPCHEmitted(Writer);
144 bool shouldEraseOutputFiles()
override {
return !hasEmittedPreamblePCH(); }
145 bool hasCodeCompletionSupport()
const override {
return false; }
146 bool hasASTFileSupport()
const override {
return false; }
150 friend class PrecompilePreambleConsumer;
152 bool HasEmittedPreamblePCH =
false;
153 std::string *InMemStorage;
157 class PrecompilePreambleConsumer :
public PCHGenerator {
159 PrecompilePreambleConsumer(PrecompilePreambleAction &Action,
161 std::unique_ptr<raw_ostream> Out)
165 Action(Action), Out(
std::move(Out)) {}
168 Action.Callbacks.HandleTopLevelDecl(DG);
172 void HandleTranslationUnit(
ASTContext &Ctx)
override {
174 if (!hasEmittedPCH())
183 getPCH() = std::move(Empty);
185 Action.setEmittedPreamblePCH(getWriter());
189 PrecompilePreambleAction &Action;
190 std::unique_ptr<raw_ostream> Out;
193 std::unique_ptr<ASTConsumer>
200 std::unique_ptr<llvm::raw_ostream> OS;
202 OS = llvm::make_unique<llvm::raw_string_ostream>(*InMemStorage);
204 std::string OutputFile;
213 return llvm::make_unique<PrecompilePreambleConsumer>(
217 template <
class T>
bool moveOnNoError(llvm::ErrorOr<T> Val, T &Output) {
220 Output = std::move(*Val);
227 llvm::MemoryBuffer *Buffer,
236 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
bool StoreInMemory,
238 assert(VFS &&
"VFS is null");
243 auto PreambleInvocation = std::make_shared<CompilerInvocation>(Invocation);
246 PreambleInvocation->getPreprocessorOpts();
249 if (!StoreInMemory) {
252 llvm::ErrorOr<PrecompiledPreamble::TempPCHFile> PreamblePCHFile =
253 PrecompiledPreamble::TempPCHFile::CreateNewPreamblePCHFile();
254 if (!PreamblePCHFile)
256 TempFile = std::move(*PreamblePCHFile);
259 PCHStorage Storage = StoreInMemory ? PCHStorage(InMemoryPreamble())
260 : PCHStorage(std::move(*TempFile));
264 std::vector<char> PreambleBytes(MainFileBuffer->getBufferStart(),
265 MainFileBuffer->getBufferStart() +
271 FrontendOpts.OutputFile = StoreInMemory ? getInMemoryPreamblePath()
272 : Storage.asFile().getFilePath();
279 std::unique_ptr<CompilerInstance> Clang(
283 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> CICleanup(
286 Clang->setInvocation(std::move(PreambleInvocation));
287 Clang->setDiagnostics(&Diagnostics);
291 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
292 if (!Clang->hasTarget())
299 Clang->getTarget().adjust(Clang->getLangOpts());
301 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
302 "Invocation must have exactly one source file!");
303 assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() ==
305 "FIXME: AST inputs not yet supported here!");
306 assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() !=
308 "IR inputs not support here!");
318 Clang->setFileManager(
new FileManager(Clang->getFileSystemOpts(), VFS));
321 Clang->setSourceManager(
324 auto PreambleDepCollector = std::make_shared<PreambleDependencyCollector>();
325 Clang->addDependencyCollector(PreambleDepCollector);
328 StringRef MainFilePath = FrontendOpts.Inputs[0].getFile();
329 auto PreambleInputBuffer = llvm::MemoryBuffer::getMemBufferCopy(
330 MainFileBuffer->getBuffer().slice(0, Bounds.
Size), MainFilePath);
333 PreprocessorOpts.
addRemappedFile(MainFilePath, PreambleInputBuffer.get());
338 PreambleInputBuffer.release());
341 std::unique_ptr<PrecompilePreambleAction> Act;
342 Act.reset(
new PrecompilePreambleAction(
343 StoreInMemory ? &Storage.asMemory().Data :
nullptr, Callbacks));
345 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0]))
348 std::unique_ptr<PPCallbacks> DelegatedPPCallbacks =
350 if (DelegatedPPCallbacks)
351 Clang->getPreprocessor().addPPCallbacks(std::move(DelegatedPPCallbacks));
358 Act->EndSourceFile();
360 if (!Act->hasEmittedPreamblePCH())
365 llvm::StringMap<PrecompiledPreamble::PreambleFileHash> FilesInPreamble;
368 for (
auto &
Filename : PreambleDepCollector->getDependencies()) {
373 FilesInPreamble[File->
getName()] =
374 PrecompiledPreamble::PreambleFileHash::createForFile(File->
getSize(),
378 FilesInPreamble[File->
getName()] =
379 PrecompiledPreamble::PreambleFileHash::createForMemoryBuffer(Buffer);
384 PreambleEndsAtStartOfLine,
385 std::move(FilesInPreamble));
389 return PreambleBounds(PreambleBytes.size(), PreambleEndsAtStartOfLine);
393 switch (Storage.getKind()) {
394 case PCHStorage::Kind::Empty:
395 assert(
false &&
"Calling getSize() on invalid PrecompiledPreamble. " 396 "Was it std::moved?");
398 case PCHStorage::Kind::InMemory:
399 return Storage.asMemory().Data.size();
400 case PCHStorage::Kind::TempFile: {
402 if (llvm::sys::fs::file_size(Storage.asFile().getFilePath(),
Result))
406 "file size did not fit into size_t");
410 llvm_unreachable(
"Unhandled storage kind");
414 const llvm::MemoryBuffer *MainFileBuffer,
419 Bounds.
Size <= MainFileBuffer->getBufferSize() &&
420 "Buffer is too large. Bounds were calculated from a different buffer?");
422 auto PreambleInvocation = std::make_shared<CompilerInvocation>(Invocation);
424 PreambleInvocation->getPreprocessorOpts();
433 if (PreambleBytes.size() != Bounds.
Size ||
435 memcmp(PreambleBytes.data(), MainFileBuffer->getBufferStart(),
444 std::map<llvm::sys::fs::UniqueID, PreambleFileHash> OverriddenFiles;
445 for (
const auto &R : PreprocessorOpts.RemappedFiles) {
453 OverriddenFiles[Status.
getUniqueID()] = PreambleFileHash::createForFile(
457 for (
const auto &RB : PreprocessorOpts.RemappedFileBuffers) {
463 PreambleFileHash::createForMemoryBuffer(RB.second);
467 for (
const auto &F : FilesInPreamble) {
474 std::map<llvm::sys::fs::UniqueID, PreambleFileHash>::iterator Overridden =
476 if (Overridden != OverriddenFiles.end()) {
479 if (Overridden->second != F.second)
485 if (Status.
getSize() != uint64_t(F.second.Size) ||
495 llvm::MemoryBuffer *MainFileBuffer)
const {
496 PreambleBounds Bounds(PreambleBytes.size(), PreambleEndsAtStartOfLine);
497 configurePreamble(Bounds, CI, VFS, MainFileBuffer);
502 llvm::MemoryBuffer *MainFileBuffer)
const {
504 configurePreamble(Bounds, CI, VFS, MainFileBuffer);
508 PCHStorage Storage, std::vector<char> PreambleBytes,
509 bool PreambleEndsAtStartOfLine,
510 llvm::StringMap<PreambleFileHash> FilesInPreamble)
511 : Storage(std::move(Storage)), FilesInPreamble(std::move(FilesInPreamble)),
512 PreambleBytes(std::move(PreambleBytes)),
513 PreambleEndsAtStartOfLine(PreambleEndsAtStartOfLine) {
514 assert(this->Storage.getKind() != PCHStorage::Kind::Empty);
517 llvm::ErrorOr<PrecompiledPreamble::TempPCHFile>
518 PrecompiledPreamble::TempPCHFile::CreateNewPreamblePCHFile() {
522 const char *TmpFile = ::getenv(
"CINDEXTEST_PREAMBLE_FILE");
524 return TempPCHFile::createFromCustomPath(TmpFile);
525 return TempPCHFile::createInSystemTempDir(
"preamble",
"pch");
528 llvm::ErrorOr<PrecompiledPreamble::TempPCHFile>
529 PrecompiledPreamble::TempPCHFile::createInSystemTempDir(
const Twine &Prefix,
536 auto EC = llvm::sys::fs::createTemporaryFile(Prefix, Suffix, FD, File);
540 llvm::sys::Process::SafelyCloseFileDescriptor(FD);
541 return TempPCHFile(std::move(File).str());
544 llvm::ErrorOr<PrecompiledPreamble::TempPCHFile>
545 PrecompiledPreamble::TempPCHFile::createFromCustomPath(
const Twine &Path) {
546 return TempPCHFile(Path.str());
549 PrecompiledPreamble::TempPCHFile::TempPCHFile(std::string FilePath)
550 : FilePath(std::move(FilePath)) {
551 TemporaryFiles::getInstance().addFile(*this->FilePath);
554 PrecompiledPreamble::TempPCHFile::TempPCHFile(TempPCHFile &&Other) {
555 FilePath = std::move(Other.FilePath);
556 Other.FilePath =
None;
559 PrecompiledPreamble::TempPCHFile &PrecompiledPreamble::TempPCHFile::
560 operator=(TempPCHFile &&Other) {
561 RemoveFileIfPresent();
563 FilePath = std::move(Other.FilePath);
564 Other.FilePath =
None;
568 PrecompiledPreamble::TempPCHFile::~TempPCHFile() { RemoveFileIfPresent(); }
570 void PrecompiledPreamble::TempPCHFile::RemoveFileIfPresent() {
572 TemporaryFiles::getInstance().removeFile(*FilePath);
577 llvm::StringRef PrecompiledPreamble::TempPCHFile::getFilePath()
const {
578 assert(FilePath &&
"TempPCHFile doesn't have a FilePath. Had it been moved?");
582 PrecompiledPreamble::PCHStorage::PCHStorage(TempPCHFile File)
583 : StorageKind(Kind::TempFile) {
584 new (&asFile()) TempPCHFile(std::move(File));
587 PrecompiledPreamble::PCHStorage::PCHStorage(InMemoryPreamble Memory)
588 : StorageKind(Kind::InMemory) {
589 new (&asMemory()) InMemoryPreamble(std::move(Memory));
592 PrecompiledPreamble::PCHStorage::PCHStorage(PCHStorage &&Other) : PCHStorage() {
593 *
this = std::move(Other);
596 PrecompiledPreamble::PCHStorage &PrecompiledPreamble::PCHStorage::
597 operator=(PCHStorage &&Other) {
600 StorageKind = Other.StorageKind;
601 switch (StorageKind) {
606 new (&asFile()) TempPCHFile(std::move(Other.asFile()));
609 new (&asMemory()) InMemoryPreamble(std::move(Other.asMemory()));
617 PrecompiledPreamble::PCHStorage::~PCHStorage() { destroy(); }
619 PrecompiledPreamble::PCHStorage::Kind
624 PrecompiledPreamble::TempPCHFile &PrecompiledPreamble::PCHStorage::asFile() {
625 assert(
getKind() == Kind::TempFile);
626 return *
reinterpret_cast<TempPCHFile *
>(Storage.buffer);
629 const PrecompiledPreamble::TempPCHFile &
630 PrecompiledPreamble::PCHStorage::asFile()
const {
631 return const_cast<PCHStorage *
>(
this)->asFile();
634 PrecompiledPreamble::InMemoryPreamble &
635 PrecompiledPreamble::PCHStorage::asMemory() {
636 assert(
getKind() == Kind::InMemory);
637 return *
reinterpret_cast<InMemoryPreamble *
>(Storage.buffer);
640 const PrecompiledPreamble::InMemoryPreamble &
641 PrecompiledPreamble::PCHStorage::asMemory()
const {
642 return const_cast<PCHStorage *
>(
this)->asMemory();
645 void PrecompiledPreamble::PCHStorage::destroy() {
646 switch (StorageKind) {
650 asFile().~TempPCHFile();
653 asMemory().~InMemoryPreamble();
658 void PrecompiledPreamble::PCHStorage::setEmpty() {
660 StorageKind = Kind::Empty;
663 PrecompiledPreamble::PreambleFileHash
664 PrecompiledPreamble::PreambleFileHash::createForFile(off_t Size,
668 Result.ModTime = ModTime;
673 PrecompiledPreamble::PreambleFileHash
674 PrecompiledPreamble::PreambleFileHash::createForMemoryBuffer(
675 const llvm::MemoryBuffer *Buffer) {
677 Result.Size = Buffer->getBufferSize();
681 MD5Ctx.update(Buffer->getBuffer().data());
682 MD5Ctx.final(Result.MD5);
687 void PrecompiledPreamble::configurePreamble(
690 llvm::MemoryBuffer *MainFileBuffer)
const {
697 PreprocessorOpts.addRemappedFile(MainFilePath, MainFileBuffer);
700 PreprocessorOpts.PrecompiledPreambleBytes.first = Bounds.
Size;
701 PreprocessorOpts.PrecompiledPreambleBytes.second =
703 PreprocessorOpts.DisablePCHValidation =
true;
705 setupPreambleStorage(Storage, PreprocessorOpts, VFS);
708 void PrecompiledPreamble::setupPreambleStorage(
711 if (Storage.getKind() == PCHStorage::Kind::TempFile) {
712 const TempPCHFile &PCHFile = Storage.asFile();
717 auto PCHPath = PCHFile.getFilePath();
718 if (VFS == RealFS || VFS->exists(PCHPath))
720 auto Buf = RealFS->getBufferForFile(PCHPath);
730 VFS = createVFSOverlayForPreamblePCH(PCHPath, std::move(*Buf), VFS);
732 assert(Storage.getKind() == PCHStorage::Kind::InMemory);
735 StringRef PCHPath = getInMemoryPreamblePath();
738 auto Buf = llvm::MemoryBuffer::getMemBuffer(Storage.asMemory().Data);
739 VFS = createVFSOverlayForPreamblePCH(PCHPath, std::move(Buf), VFS);
756 return "build-preamble.error";
760 switch (static_cast<BuildPreambleError>(condition)) {
762 return "Preamble is empty";
764 return "Could not create temporary file for PCH";
766 return "CreateTargetInfo() return null";
768 return "BeginSourceFile() return an error";
770 return "Could not emit PCH";
772 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
An interface for collecting the dependencies of a compilation.
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.
static bool moveOnNoError(llvm::ErrorOr< T > Val, T &Output)
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.
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().
void OverridePreamble(CompilerInvocation &CI, IntrusiveRefCntPtr< vfs::FileSystem > &VFS, llvm::MemoryBuffer *MainFileBuffer) const
Configure CI to use this preamble.
unsigned Size
Size of the preamble in bytes.
llvm::sys::fs::UniqueID getUniqueID() const
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.
__DEVICE__ int max(int __a, int __b)
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.
LangOptions * getLangOpts()
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.