blob: 239e0dd974aa9b5cd61f211f6a19804c710ccb1d [file] [log] [blame]
Ilya Biryukov200b3282017-06-21 10:24:58 +00001//===--- PrecompiledPreamble.cpp - Build precompiled preambles --*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Helper class to build precompiled preamble.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/PrecompiledPreamble.h"
15#include "clang/AST/DeclObjC.h"
16#include "clang/Basic/TargetInfo.h"
17#include "clang/Basic/VirtualFileSystem.h"
18#include "clang/Frontend/CompilerInstance.h"
19#include "clang/Frontend/CompilerInvocation.h"
20#include "clang/Frontend/FrontendActions.h"
21#include "clang/Frontend/FrontendOptions.h"
22#include "clang/Lex/Lexer.h"
23#include "clang/Lex/PreprocessorOptions.h"
24#include "clang/Serialization/ASTWriter.h"
25#include "llvm/ADT/StringExtras.h"
26#include "llvm/ADT/StringSet.h"
Ilya Biryukovdd9ea752017-11-17 10:09:02 +000027#include "llvm/Config/llvm-config.h"
Ilya Biryukov200b3282017-06-21 10:24:58 +000028#include "llvm/Support/CrashRecoveryContext.h"
29#include "llvm/Support/FileSystem.h"
30#include "llvm/Support/Mutex.h"
31#include "llvm/Support/MutexGuard.h"
Ilya Biryukovb88de412017-08-10 16:10:40 +000032#include "llvm/Support/Process.h"
Ilya Biryukov923e3382017-12-21 14:04:39 +000033#include <limits>
Ilya Biryukov417085a2017-11-16 16:25:01 +000034#include <utility>
35
Ilya Biryukov200b3282017-06-21 10:24:58 +000036using namespace clang;
37
38namespace {
39
Ilya Biryukov417085a2017-11-16 16:25:01 +000040StringRef getInMemoryPreamblePath() {
41#if defined(LLVM_ON_UNIX)
42 return "/__clang_tmp/___clang_inmemory_preamble___";
Nico Weber1865df42018-04-27 19:11:14 +000043#elif defined(_WIN32)
Ilya Biryukov417085a2017-11-16 16:25:01 +000044 return "C:\\__clang_tmp\\___clang_inmemory_preamble___";
45#else
46#warning "Unknown platform. Defaulting to UNIX-style paths for in-memory PCHs"
47 return "/__clang_tmp/___clang_inmemory_preamble___";
48#endif
49}
50
51IntrusiveRefCntPtr<vfs::FileSystem>
52createVFSOverlayForPreamblePCH(StringRef PCHFilename,
53 std::unique_ptr<llvm::MemoryBuffer> PCHBuffer,
54 IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
55 // We want only the PCH file from the real filesystem to be available,
56 // so we create an in-memory VFS with just that and overlay it on top.
57 IntrusiveRefCntPtr<vfs::InMemoryFileSystem> PCHFS(
58 new vfs::InMemoryFileSystem());
59 PCHFS->addFile(PCHFilename, 0, std::move(PCHBuffer));
60 IntrusiveRefCntPtr<vfs::OverlayFileSystem> Overlay(
61 new vfs::OverlayFileSystem(VFS));
62 Overlay->pushOverlay(PCHFS);
63 return Overlay;
64}
65
Ilya Biryukove5801b02018-07-09 09:07:01 +000066class PreambleDependencyCollector : public DependencyCollector {
67public:
68 // We want to collect all dependencies for correctness. Avoiding the real
69 // system dependencies (e.g. stl from /usr/lib) would probably be a good idea,
70 // but there is no way to distinguish between those and the ones that can be
71 // spuriously added by '-isystem' (e.g. to suppress warnings from those
72 // headers).
73 bool needSystemDependencies() override { return true; }
74};
75
Ilya Biryukov200b3282017-06-21 10:24:58 +000076/// Keeps a track of files to be deleted in destructor.
77class TemporaryFiles {
78public:
79 // A static instance to be used by all clients.
80 static TemporaryFiles &getInstance();
81
82private:
83 // Disallow constructing the class directly.
84 TemporaryFiles() = default;
85 // Disallow copy.
86 TemporaryFiles(const TemporaryFiles &) = delete;
87
88public:
89 ~TemporaryFiles();
90
91 /// Adds \p File to a set of tracked files.
92 void addFile(StringRef File);
93
94 /// Remove \p File from disk and from the set of tracked files.
95 void removeFile(StringRef File);
96
97private:
98 llvm::sys::SmartMutex<false> Mutex;
99 llvm::StringSet<> Files;
100};
101
102TemporaryFiles &TemporaryFiles::getInstance() {
103 static TemporaryFiles Instance;
104 return Instance;
105}
106
107TemporaryFiles::~TemporaryFiles() {
108 llvm::MutexGuard Guard(Mutex);
109 for (const auto &File : Files)
110 llvm::sys::fs::remove(File.getKey());
111}
112
113void TemporaryFiles::addFile(StringRef File) {
114 llvm::MutexGuard Guard(Mutex);
115 auto IsInserted = Files.insert(File).second;
Haojian Wu53dd6442017-06-21 11:26:58 +0000116 (void)IsInserted;
Ilya Biryukov200b3282017-06-21 10:24:58 +0000117 assert(IsInserted && "File has already been added");
118}
119
120void TemporaryFiles::removeFile(StringRef File) {
121 llvm::MutexGuard Guard(Mutex);
122 auto WasPresent = Files.erase(File);
Haojian Wu53dd6442017-06-21 11:26:58 +0000123 (void)WasPresent;
Ilya Biryukov200b3282017-06-21 10:24:58 +0000124 assert(WasPresent && "File was not tracked");
125 llvm::sys::fs::remove(File);
126}
127
Ilya Biryukov200b3282017-06-21 10:24:58 +0000128class PrecompilePreambleAction : public ASTFrontendAction {
129public:
Ilya Biryukov417085a2017-11-16 16:25:01 +0000130 PrecompilePreambleAction(std::string *InMemStorage,
131 PreambleCallbacks &Callbacks)
132 : InMemStorage(InMemStorage), Callbacks(Callbacks) {}
Ilya Biryukov200b3282017-06-21 10:24:58 +0000133
134 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
135 StringRef InFile) override;
136
137 bool hasEmittedPreamblePCH() const { return HasEmittedPreamblePCH; }
138
139 void setEmittedPreamblePCH(ASTWriter &Writer) {
140 this->HasEmittedPreamblePCH = true;
141 Callbacks.AfterPCHEmitted(Writer);
142 }
143
144 bool shouldEraseOutputFiles() override { return !hasEmittedPreamblePCH(); }
145 bool hasCodeCompletionSupport() const override { return false; }
146 bool hasASTFileSupport() const override { return false; }
147 TranslationUnitKind getTranslationUnitKind() override { return TU_Prefix; }
148
149private:
150 friend class PrecompilePreambleConsumer;
151
152 bool HasEmittedPreamblePCH = false;
Ilya Biryukov417085a2017-11-16 16:25:01 +0000153 std::string *InMemStorage;
Ilya Biryukov200b3282017-06-21 10:24:58 +0000154 PreambleCallbacks &Callbacks;
155};
156
157class PrecompilePreambleConsumer : public PCHGenerator {
158public:
159 PrecompilePreambleConsumer(PrecompilePreambleAction &Action,
160 const Preprocessor &PP, StringRef isysroot,
161 std::unique_ptr<raw_ostream> Out)
162 : PCHGenerator(PP, "", isysroot, std::make_shared<PCHBuffer>(),
163 ArrayRef<std::shared_ptr<ModuleFileExtension>>(),
164 /*AllowASTWithErrors=*/true),
165 Action(Action), Out(std::move(Out)) {}
166
167 bool HandleTopLevelDecl(DeclGroupRef DG) override {
168 Action.Callbacks.HandleTopLevelDecl(DG);
169 return true;
170 }
171
172 void HandleTranslationUnit(ASTContext &Ctx) override {
173 PCHGenerator::HandleTranslationUnit(Ctx);
174 if (!hasEmittedPCH())
175 return;
176
177 // Write the generated bitstream to "Out".
178 *Out << getPCH();
179 // Make sure it hits disk now.
180 Out->flush();
181 // Free the buffer.
182 llvm::SmallVector<char, 0> Empty;
183 getPCH() = std::move(Empty);
184
185 Action.setEmittedPreamblePCH(getWriter());
186 }
187
188private:
189 PrecompilePreambleAction &Action;
190 std::unique_ptr<raw_ostream> Out;
191};
192
193std::unique_ptr<ASTConsumer>
194PrecompilePreambleAction::CreateASTConsumer(CompilerInstance &CI,
Ilya Biryukov200b3282017-06-21 10:24:58 +0000195 StringRef InFile) {
196 std::string Sysroot;
Ilya Biryukov417085a2017-11-16 16:25:01 +0000197 if (!GeneratePCHAction::ComputeASTConsumerArguments(CI, Sysroot))
198 return nullptr;
199
200 std::unique_ptr<llvm::raw_ostream> OS;
201 if (InMemStorage) {
202 OS = llvm::make_unique<llvm::raw_string_ostream>(*InMemStorage);
203 } else {
204 std::string OutputFile;
205 OS = GeneratePCHAction::CreateOutputFile(CI, InFile, OutputFile);
206 }
Ilya Biryukov200b3282017-06-21 10:24:58 +0000207 if (!OS)
208 return nullptr;
209
210 if (!CI.getFrontendOpts().RelocatablePCH)
211 Sysroot.clear();
212
Ilya Biryukov200b3282017-06-21 10:24:58 +0000213 return llvm::make_unique<PrecompilePreambleConsumer>(
214 *this, CI.getPreprocessor(), Sysroot, std::move(OS));
215}
216
217template <class T> bool moveOnNoError(llvm::ErrorOr<T> Val, T &Output) {
218 if (!Val)
219 return false;
220 Output = std::move(*Val);
221 return true;
222}
223
224} // namespace
225
226PreambleBounds clang::ComputePreambleBounds(const LangOptions &LangOpts,
227 llvm::MemoryBuffer *Buffer,
228 unsigned MaxLines) {
Cameron Desrochers84fd0642017-09-20 19:03:37 +0000229 return Lexer::ComputePreamble(Buffer->getBuffer(), LangOpts, MaxLines);
Ilya Biryukov200b3282017-06-21 10:24:58 +0000230}
231
232llvm::ErrorOr<PrecompiledPreamble> PrecompiledPreamble::Build(
233 const CompilerInvocation &Invocation,
234 const llvm::MemoryBuffer *MainFileBuffer, PreambleBounds Bounds,
235 DiagnosticsEngine &Diagnostics, IntrusiveRefCntPtr<vfs::FileSystem> VFS,
Ilya Biryukov417085a2017-11-16 16:25:01 +0000236 std::shared_ptr<PCHContainerOperations> PCHContainerOps, bool StoreInMemory,
Ilya Biryukov200b3282017-06-21 10:24:58 +0000237 PreambleCallbacks &Callbacks) {
238 assert(VFS && "VFS is null");
239
Ilya Biryukov200b3282017-06-21 10:24:58 +0000240 auto PreambleInvocation = std::make_shared<CompilerInvocation>(Invocation);
241 FrontendOptions &FrontendOpts = PreambleInvocation->getFrontendOpts();
242 PreprocessorOptions &PreprocessorOpts =
243 PreambleInvocation->getPreprocessorOpts();
244
Ilya Biryukov417085a2017-11-16 16:25:01 +0000245 llvm::Optional<TempPCHFile> TempFile;
246 if (!StoreInMemory) {
247 // Create a temporary file for the precompiled preamble. In rare
248 // circumstances, this can fail.
249 llvm::ErrorOr<PrecompiledPreamble::TempPCHFile> PreamblePCHFile =
250 PrecompiledPreamble::TempPCHFile::CreateNewPreamblePCHFile();
251 if (!PreamblePCHFile)
252 return BuildPreambleError::CouldntCreateTempFile;
253 TempFile = std::move(*PreamblePCHFile);
254 }
255
256 PCHStorage Storage = StoreInMemory ? PCHStorage(InMemoryPreamble())
257 : PCHStorage(std::move(*TempFile));
Ilya Biryukov200b3282017-06-21 10:24:58 +0000258
259 // Save the preamble text for later; we'll need to compare against it for
260 // subsequent reparses.
261 std::vector<char> PreambleBytes(MainFileBuffer->getBufferStart(),
262 MainFileBuffer->getBufferStart() +
263 Bounds.Size);
264 bool PreambleEndsAtStartOfLine = Bounds.PreambleEndsAtStartOfLine;
265
266 // Tell the compiler invocation to generate a temporary precompiled header.
267 FrontendOpts.ProgramAction = frontend::GeneratePCH;
Ilya Biryukov417085a2017-11-16 16:25:01 +0000268 FrontendOpts.OutputFile = StoreInMemory ? getInMemoryPreamblePath()
269 : Storage.asFile().getFilePath();
Ilya Biryukov200b3282017-06-21 10:24:58 +0000270 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
271 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
Ilya Biryukoveb1ec872017-10-09 16:52:12 +0000272 // Inform preprocessor to record conditional stack when building the preamble.
273 PreprocessorOpts.GeneratePreamble = true;
Ilya Biryukov200b3282017-06-21 10:24:58 +0000274
275 // Create the compiler instance to use for building the precompiled preamble.
276 std::unique_ptr<CompilerInstance> Clang(
277 new CompilerInstance(std::move(PCHContainerOps)));
278
279 // Recover resources if we crash before exiting this method.
280 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> CICleanup(
281 Clang.get());
282
283 Clang->setInvocation(std::move(PreambleInvocation));
284 Clang->setDiagnostics(&Diagnostics);
285
286 // Create the target instance.
287 Clang->setTarget(TargetInfo::CreateTargetInfo(
288 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
289 if (!Clang->hasTarget())
290 return BuildPreambleError::CouldntCreateTargetInfo;
291
292 // Inform the target of the language options.
293 //
294 // FIXME: We shouldn't need to do this, the target should be immutable once
295 // created. This complexity should be lifted elsewhere.
296 Clang->getTarget().adjust(Clang->getLangOpts());
297
298 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
299 "Invocation must have exactly one source file!");
300 assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() ==
301 InputKind::Source &&
302 "FIXME: AST inputs not yet supported here!");
303 assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() !=
304 InputKind::LLVM_IR &&
305 "IR inputs not support here!");
306
307 // Clear out old caches and data.
308 Diagnostics.Reset();
309 ProcessWarningOptions(Diagnostics, Clang->getDiagnosticOpts());
310
311 VFS =
312 createVFSFromCompilerInvocation(Clang->getInvocation(), Diagnostics, VFS);
Ilya Biryukov200b3282017-06-21 10:24:58 +0000313
314 // Create a file manager object to provide access to and cache the filesystem.
315 Clang->setFileManager(new FileManager(Clang->getFileSystemOpts(), VFS));
316
317 // Create the source manager.
318 Clang->setSourceManager(
319 new SourceManager(Diagnostics, Clang->getFileManager()));
320
Ilya Biryukove5801b02018-07-09 09:07:01 +0000321 auto PreambleDepCollector = std::make_shared<PreambleDependencyCollector>();
Ilya Biryukov200b3282017-06-21 10:24:58 +0000322 Clang->addDependencyCollector(PreambleDepCollector);
323
324 // Remap the main source file to the preamble buffer.
325 StringRef MainFilePath = FrontendOpts.Inputs[0].getFile();
326 auto PreambleInputBuffer = llvm::MemoryBuffer::getMemBufferCopy(
327 MainFileBuffer->getBuffer().slice(0, Bounds.Size), MainFilePath);
328 if (PreprocessorOpts.RetainRemappedFileBuffers) {
329 // MainFileBuffer will be deleted by unique_ptr after leaving the method.
330 PreprocessorOpts.addRemappedFile(MainFilePath, PreambleInputBuffer.get());
331 } else {
332 // In that case, remapped buffer will be deleted by CompilerInstance on
333 // BeginSourceFile, so we call release() to avoid double deletion.
334 PreprocessorOpts.addRemappedFile(MainFilePath,
335 PreambleInputBuffer.release());
336 }
337
338 std::unique_ptr<PrecompilePreambleAction> Act;
Ilya Biryukov417085a2017-11-16 16:25:01 +0000339 Act.reset(new PrecompilePreambleAction(
340 StoreInMemory ? &Storage.asMemory().Data : nullptr, Callbacks));
Ilya Biryukov1f8647d2017-12-20 16:48:56 +0000341 Callbacks.BeforeExecute(*Clang);
Ilya Biryukov200b3282017-06-21 10:24:58 +0000342 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0]))
343 return BuildPreambleError::BeginSourceFileFailed;
344
Ilya Biryukov41e90bc2017-12-15 11:27:51 +0000345 std::unique_ptr<PPCallbacks> DelegatedPPCallbacks =
346 Callbacks.createPPCallbacks();
347 if (DelegatedPPCallbacks)
348 Clang->getPreprocessor().addPPCallbacks(std::move(DelegatedPPCallbacks));
349
Ilya Biryukov200b3282017-06-21 10:24:58 +0000350 Act->Execute();
351
352 // Run the callbacks.
353 Callbacks.AfterExecute(*Clang);
354
355 Act->EndSourceFile();
356
357 if (!Act->hasEmittedPreamblePCH())
358 return BuildPreambleError::CouldntEmitPCH;
359
360 // Keep track of all of the files that the source manager knows about,
361 // so we can verify whether they have changed or not.
362 llvm::StringMap<PrecompiledPreamble::PreambleFileHash> FilesInPreamble;
363
364 SourceManager &SourceMgr = Clang->getSourceManager();
365 for (auto &Filename : PreambleDepCollector->getDependencies()) {
366 const FileEntry *File = Clang->getFileManager().getFile(Filename);
367 if (!File || File == SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()))
368 continue;
369 if (time_t ModTime = File->getModificationTime()) {
370 FilesInPreamble[File->getName()] =
371 PrecompiledPreamble::PreambleFileHash::createForFile(File->getSize(),
372 ModTime);
373 } else {
374 llvm::MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File);
375 FilesInPreamble[File->getName()] =
376 PrecompiledPreamble::PreambleFileHash::createForMemoryBuffer(Buffer);
377 }
378 }
379
Ilya Biryukov417085a2017-11-16 16:25:01 +0000380 return PrecompiledPreamble(std::move(Storage), std::move(PreambleBytes),
381 PreambleEndsAtStartOfLine,
382 std::move(FilesInPreamble));
Ilya Biryukov200b3282017-06-21 10:24:58 +0000383}
384
385PreambleBounds PrecompiledPreamble::getBounds() const {
386 return PreambleBounds(PreambleBytes.size(), PreambleEndsAtStartOfLine);
387}
388
Ilya Biryukov923e3382017-12-21 14:04:39 +0000389std::size_t PrecompiledPreamble::getSize() const {
390 switch (Storage.getKind()) {
391 case PCHStorage::Kind::Empty:
392 assert(false && "Calling getSize() on invalid PrecompiledPreamble. "
393 "Was it std::moved?");
394 return 0;
395 case PCHStorage::Kind::InMemory:
396 return Storage.asMemory().Data.size();
397 case PCHStorage::Kind::TempFile: {
398 uint64_t Result;
399 if (llvm::sys::fs::file_size(Storage.asFile().getFilePath(), Result))
400 return 0;
401
402 assert(Result <= std::numeric_limits<std::size_t>::max() &&
403 "file size did not fit into size_t");
404 return Result;
405 }
406 }
407 llvm_unreachable("Unhandled storage kind");
408}
409
Ilya Biryukov200b3282017-06-21 10:24:58 +0000410bool PrecompiledPreamble::CanReuse(const CompilerInvocation &Invocation,
411 const llvm::MemoryBuffer *MainFileBuffer,
412 PreambleBounds Bounds,
413 vfs::FileSystem *VFS) const {
414
415 assert(
416 Bounds.Size <= MainFileBuffer->getBufferSize() &&
417 "Buffer is too large. Bounds were calculated from a different buffer?");
418
419 auto PreambleInvocation = std::make_shared<CompilerInvocation>(Invocation);
420 PreprocessorOptions &PreprocessorOpts =
421 PreambleInvocation->getPreprocessorOpts();
422
Ilya Biryukov200b3282017-06-21 10:24:58 +0000423 // We've previously computed a preamble. Check whether we have the same
424 // preamble now that we did before, and that there's enough space in
425 // the main-file buffer within the precompiled preamble to fit the
426 // new main file.
427 if (PreambleBytes.size() != Bounds.Size ||
428 PreambleEndsAtStartOfLine != Bounds.PreambleEndsAtStartOfLine ||
Haojian Wua3b34572018-08-22 12:34:04 +0000429 !std::equal(PreambleBytes.begin(), PreambleBytes.end(),
430 MainFileBuffer->getBuffer().begin()))
Ilya Biryukov200b3282017-06-21 10:24:58 +0000431 return false;
432 // The preamble has not changed. We may be able to re-use the precompiled
433 // preamble.
434
435 // Check that none of the files used by the preamble have changed.
436 // First, make a record of those files that have been overridden via
437 // remapping or unsaved_files.
438 std::map<llvm::sys::fs::UniqueID, PreambleFileHash> OverriddenFiles;
439 for (const auto &R : PreprocessorOpts.RemappedFiles) {
440 vfs::Status Status;
441 if (!moveOnNoError(VFS->status(R.second), Status)) {
442 // If we can't stat the file we're remapping to, assume that something
443 // horrible happened.
444 return false;
445 }
446
447 OverriddenFiles[Status.getUniqueID()] = PreambleFileHash::createForFile(
448 Status.getSize(), llvm::sys::toTimeT(Status.getLastModificationTime()));
449 }
450
451 for (const auto &RB : PreprocessorOpts.RemappedFileBuffers) {
452 vfs::Status Status;
453 if (!moveOnNoError(VFS->status(RB.first), Status))
454 return false;
455
456 OverriddenFiles[Status.getUniqueID()] =
457 PreambleFileHash::createForMemoryBuffer(RB.second);
458 }
459
460 // Check whether anything has changed.
461 for (const auto &F : FilesInPreamble) {
462 vfs::Status Status;
463 if (!moveOnNoError(VFS->status(F.first()), Status)) {
464 // If we can't stat the file, assume that something horrible happened.
465 return false;
466 }
467
468 std::map<llvm::sys::fs::UniqueID, PreambleFileHash>::iterator Overridden =
469 OverriddenFiles.find(Status.getUniqueID());
470 if (Overridden != OverriddenFiles.end()) {
471 // This file was remapped; check whether the newly-mapped file
472 // matches up with the previous mapping.
473 if (Overridden->second != F.second)
474 return false;
475 continue;
476 }
477
478 // The file was not remapped; check whether it has changed on disk.
479 if (Status.getSize() != uint64_t(F.second.Size) ||
480 llvm::sys::toTimeT(Status.getLastModificationTime()) !=
481 F.second.ModTime)
482 return false;
483 }
484 return true;
485}
486
487void PrecompiledPreamble::AddImplicitPreamble(
Ilya Biryukov417085a2017-11-16 16:25:01 +0000488 CompilerInvocation &CI, IntrusiveRefCntPtr<vfs::FileSystem> &VFS,
489 llvm::MemoryBuffer *MainFileBuffer) const {
Ilya Biryukov4a8f7532018-01-18 15:16:53 +0000490 PreambleBounds Bounds(PreambleBytes.size(), PreambleEndsAtStartOfLine);
491 configurePreamble(Bounds, CI, VFS, MainFileBuffer);
492}
Ilya Biryukov200b3282017-06-21 10:24:58 +0000493
Ilya Biryukov4a8f7532018-01-18 15:16:53 +0000494void PrecompiledPreamble::OverridePreamble(
495 CompilerInvocation &CI, IntrusiveRefCntPtr<vfs::FileSystem> &VFS,
496 llvm::MemoryBuffer *MainFileBuffer) const {
497 auto Bounds = ComputePreambleBounds(*CI.getLangOpts(), MainFileBuffer, 0);
498 configurePreamble(Bounds, CI, VFS, MainFileBuffer);
Ilya Biryukov200b3282017-06-21 10:24:58 +0000499}
500
501PrecompiledPreamble::PrecompiledPreamble(
Ilya Biryukov417085a2017-11-16 16:25:01 +0000502 PCHStorage Storage, std::vector<char> PreambleBytes,
Ilya Biryukov200b3282017-06-21 10:24:58 +0000503 bool PreambleEndsAtStartOfLine,
504 llvm::StringMap<PreambleFileHash> FilesInPreamble)
Ilya Biryukov417085a2017-11-16 16:25:01 +0000505 : Storage(std::move(Storage)), FilesInPreamble(std::move(FilesInPreamble)),
Ilya Biryukov200b3282017-06-21 10:24:58 +0000506 PreambleBytes(std::move(PreambleBytes)),
Ilya Biryukov417085a2017-11-16 16:25:01 +0000507 PreambleEndsAtStartOfLine(PreambleEndsAtStartOfLine) {
508 assert(this->Storage.getKind() != PCHStorage::Kind::Empty);
509}
Ilya Biryukov200b3282017-06-21 10:24:58 +0000510
511llvm::ErrorOr<PrecompiledPreamble::TempPCHFile>
512PrecompiledPreamble::TempPCHFile::CreateNewPreamblePCHFile() {
513 // FIXME: This is a hack so that we can override the preamble file during
514 // crash-recovery testing, which is the only case where the preamble files
515 // are not necessarily cleaned up.
516 const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE");
517 if (TmpFile)
518 return TempPCHFile::createFromCustomPath(TmpFile);
519 return TempPCHFile::createInSystemTempDir("preamble", "pch");
520}
521
522llvm::ErrorOr<PrecompiledPreamble::TempPCHFile>
523PrecompiledPreamble::TempPCHFile::createInSystemTempDir(const Twine &Prefix,
524 StringRef Suffix) {
525 llvm::SmallString<64> File;
Ilya Biryukovb88de412017-08-10 16:10:40 +0000526 // Using a version of createTemporaryFile with a file descriptor guarantees
Ilya Biryukov923e3382017-12-21 14:04:39 +0000527 // that we would never get a race condition in a multi-threaded setting
528 // (i.e., multiple threads getting the same temporary path).
Ilya Biryukovb88de412017-08-10 16:10:40 +0000529 int FD;
Ilya Biryukov417085a2017-11-16 16:25:01 +0000530 auto EC = llvm::sys::fs::createTemporaryFile(Prefix, Suffix, FD, File);
Ilya Biryukov200b3282017-06-21 10:24:58 +0000531 if (EC)
532 return EC;
Ilya Biryukovb88de412017-08-10 16:10:40 +0000533 // We only needed to make sure the file exists, close the file right away.
534 llvm::sys::Process::SafelyCloseFileDescriptor(FD);
Ilya Biryukov200b3282017-06-21 10:24:58 +0000535 return TempPCHFile(std::move(File).str());
536}
537
538llvm::ErrorOr<PrecompiledPreamble::TempPCHFile>
539PrecompiledPreamble::TempPCHFile::createFromCustomPath(const Twine &Path) {
540 return TempPCHFile(Path.str());
541}
542
543PrecompiledPreamble::TempPCHFile::TempPCHFile(std::string FilePath)
544 : FilePath(std::move(FilePath)) {
545 TemporaryFiles::getInstance().addFile(*this->FilePath);
546}
547
548PrecompiledPreamble::TempPCHFile::TempPCHFile(TempPCHFile &&Other) {
549 FilePath = std::move(Other.FilePath);
550 Other.FilePath = None;
551}
552
553PrecompiledPreamble::TempPCHFile &PrecompiledPreamble::TempPCHFile::
554operator=(TempPCHFile &&Other) {
555 RemoveFileIfPresent();
556
557 FilePath = std::move(Other.FilePath);
558 Other.FilePath = None;
559 return *this;
560}
561
562PrecompiledPreamble::TempPCHFile::~TempPCHFile() { RemoveFileIfPresent(); }
563
564void PrecompiledPreamble::TempPCHFile::RemoveFileIfPresent() {
565 if (FilePath) {
566 TemporaryFiles::getInstance().removeFile(*FilePath);
567 FilePath = None;
568 }
569}
570
571llvm::StringRef PrecompiledPreamble::TempPCHFile::getFilePath() const {
572 assert(FilePath && "TempPCHFile doesn't have a FilePath. Had it been moved?");
573 return *FilePath;
574}
575
Ilya Biryukov417085a2017-11-16 16:25:01 +0000576PrecompiledPreamble::PCHStorage::PCHStorage(TempPCHFile File)
577 : StorageKind(Kind::TempFile) {
578 new (&asFile()) TempPCHFile(std::move(File));
579}
580
581PrecompiledPreamble::PCHStorage::PCHStorage(InMemoryPreamble Memory)
582 : StorageKind(Kind::InMemory) {
583 new (&asMemory()) InMemoryPreamble(std::move(Memory));
584}
585
586PrecompiledPreamble::PCHStorage::PCHStorage(PCHStorage &&Other) : PCHStorage() {
587 *this = std::move(Other);
588}
589
590PrecompiledPreamble::PCHStorage &PrecompiledPreamble::PCHStorage::
591operator=(PCHStorage &&Other) {
592 destroy();
593
594 StorageKind = Other.StorageKind;
595 switch (StorageKind) {
596 case Kind::Empty:
597 // do nothing;
598 break;
599 case Kind::TempFile:
600 new (&asFile()) TempPCHFile(std::move(Other.asFile()));
601 break;
602 case Kind::InMemory:
603 new (&asMemory()) InMemoryPreamble(std::move(Other.asMemory()));
604 break;
605 }
606
607 Other.setEmpty();
608 return *this;
609}
610
611PrecompiledPreamble::PCHStorage::~PCHStorage() { destroy(); }
612
613PrecompiledPreamble::PCHStorage::Kind
614PrecompiledPreamble::PCHStorage::getKind() const {
615 return StorageKind;
616}
617
618PrecompiledPreamble::TempPCHFile &PrecompiledPreamble::PCHStorage::asFile() {
619 assert(getKind() == Kind::TempFile);
620 return *reinterpret_cast<TempPCHFile *>(Storage.buffer);
621}
622
623const PrecompiledPreamble::TempPCHFile &
624PrecompiledPreamble::PCHStorage::asFile() const {
625 return const_cast<PCHStorage *>(this)->asFile();
626}
627
628PrecompiledPreamble::InMemoryPreamble &
629PrecompiledPreamble::PCHStorage::asMemory() {
630 assert(getKind() == Kind::InMemory);
631 return *reinterpret_cast<InMemoryPreamble *>(Storage.buffer);
632}
633
634const PrecompiledPreamble::InMemoryPreamble &
635PrecompiledPreamble::PCHStorage::asMemory() const {
636 return const_cast<PCHStorage *>(this)->asMemory();
637}
638
639void PrecompiledPreamble::PCHStorage::destroy() {
640 switch (StorageKind) {
641 case Kind::Empty:
642 return;
643 case Kind::TempFile:
644 asFile().~TempPCHFile();
645 return;
646 case Kind::InMemory:
647 asMemory().~InMemoryPreamble();
648 return;
649 }
650}
651
652void PrecompiledPreamble::PCHStorage::setEmpty() {
653 destroy();
654 StorageKind = Kind::Empty;
655}
656
Ilya Biryukov200b3282017-06-21 10:24:58 +0000657PrecompiledPreamble::PreambleFileHash
658PrecompiledPreamble::PreambleFileHash::createForFile(off_t Size,
659 time_t ModTime) {
660 PreambleFileHash Result;
661 Result.Size = Size;
662 Result.ModTime = ModTime;
663 Result.MD5 = {};
664 return Result;
665}
666
667PrecompiledPreamble::PreambleFileHash
668PrecompiledPreamble::PreambleFileHash::createForMemoryBuffer(
669 const llvm::MemoryBuffer *Buffer) {
670 PreambleFileHash Result;
671 Result.Size = Buffer->getBufferSize();
672 Result.ModTime = 0;
673
674 llvm::MD5 MD5Ctx;
675 MD5Ctx.update(Buffer->getBuffer().data());
676 MD5Ctx.final(Result.MD5);
677
678 return Result;
679}
680
Ilya Biryukov4a8f7532018-01-18 15:16:53 +0000681void PrecompiledPreamble::configurePreamble(
682 PreambleBounds Bounds, CompilerInvocation &CI,
683 IntrusiveRefCntPtr<vfs::FileSystem> &VFS,
684 llvm::MemoryBuffer *MainFileBuffer) const {
685 assert(VFS);
686
687 auto &PreprocessorOpts = CI.getPreprocessorOpts();
688
689 // Remap main file to point to MainFileBuffer.
690 auto MainFilePath = CI.getFrontendOpts().Inputs[0].getFile();
691 PreprocessorOpts.addRemappedFile(MainFilePath, MainFileBuffer);
692
693 // Configure ImpicitPCHInclude.
694 PreprocessorOpts.PrecompiledPreambleBytes.first = Bounds.Size;
695 PreprocessorOpts.PrecompiledPreambleBytes.second =
696 Bounds.PreambleEndsAtStartOfLine;
697 PreprocessorOpts.DisablePCHValidation = true;
698
699 setupPreambleStorage(Storage, PreprocessorOpts, VFS);
700}
701
Ilya Biryukov417085a2017-11-16 16:25:01 +0000702void PrecompiledPreamble::setupPreambleStorage(
703 const PCHStorage &Storage, PreprocessorOptions &PreprocessorOpts,
704 IntrusiveRefCntPtr<vfs::FileSystem> &VFS) {
705 if (Storage.getKind() == PCHStorage::Kind::TempFile) {
706 const TempPCHFile &PCHFile = Storage.asFile();
707 PreprocessorOpts.ImplicitPCHInclude = PCHFile.getFilePath();
708
709 // Make sure we can access the PCH file even if we're using a VFS
710 IntrusiveRefCntPtr<vfs::FileSystem> RealFS = vfs::getRealFileSystem();
711 auto PCHPath = PCHFile.getFilePath();
712 if (VFS == RealFS || VFS->exists(PCHPath))
713 return;
714 auto Buf = RealFS->getBufferForFile(PCHPath);
715 if (!Buf) {
716 // We can't read the file even from RealFS, this is clearly an error,
717 // but we'll just leave the current VFS as is and let clang's code
718 // figure out what to do with missing PCH.
719 return;
720 }
721
722 // We have a slight inconsistency here -- we're using the VFS to
723 // read files, but the PCH was generated in the real file system.
724 VFS = createVFSOverlayForPreamblePCH(PCHPath, std::move(*Buf), VFS);
725 } else {
726 assert(Storage.getKind() == PCHStorage::Kind::InMemory);
727 // For in-memory preamble, we have to provide a VFS overlay that makes it
728 // accessible.
729 StringRef PCHPath = getInMemoryPreamblePath();
730 PreprocessorOpts.ImplicitPCHInclude = PCHPath;
731
Ilya Biryukov8318f61b2017-11-24 13:12:38 +0000732 auto Buf = llvm::MemoryBuffer::getMemBuffer(Storage.asMemory().Data);
Ilya Biryukov417085a2017-11-16 16:25:01 +0000733 VFS = createVFSOverlayForPreamblePCH(PCHPath, std::move(Buf), VFS);
734 }
735}
736
Ilya Biryukov1f8647d2017-12-20 16:48:56 +0000737void PreambleCallbacks::BeforeExecute(CompilerInstance &CI) {}
Ilya Biryukov200b3282017-06-21 10:24:58 +0000738void PreambleCallbacks::AfterExecute(CompilerInstance &CI) {}
739void PreambleCallbacks::AfterPCHEmitted(ASTWriter &Writer) {}
740void PreambleCallbacks::HandleTopLevelDecl(DeclGroupRef DG) {}
Ilya Biryukov41e90bc2017-12-15 11:27:51 +0000741std::unique_ptr<PPCallbacks> PreambleCallbacks::createPPCallbacks() {
742 return nullptr;
743}
Ilya Biryukov200b3282017-06-21 10:24:58 +0000744
745std::error_code clang::make_error_code(BuildPreambleError Error) {
746 return std::error_code(static_cast<int>(Error), BuildPreambleErrorCategory());
747}
748
749const char *BuildPreambleErrorCategory::name() const noexcept {
750 return "build-preamble.error";
751}
752
753std::string BuildPreambleErrorCategory::message(int condition) const {
754 switch (static_cast<BuildPreambleError>(condition)) {
Ilya Biryukov200b3282017-06-21 10:24:58 +0000755 case BuildPreambleError::CouldntCreateTempFile:
756 return "Could not create temporary file for PCH";
757 case BuildPreambleError::CouldntCreateTargetInfo:
758 return "CreateTargetInfo() return null";
Ilya Biryukov200b3282017-06-21 10:24:58 +0000759 case BuildPreambleError::BeginSourceFileFailed:
760 return "BeginSourceFile() return an error";
761 case BuildPreambleError::CouldntEmitPCH:
762 return "Could not emit PCH";
763 }
764 llvm_unreachable("unexpected BuildPreambleError");
765}