clang  14.0.0git
PrecompiledPreamble.cpp
Go to the documentation of this file.
1 //===--- PrecompiledPreamble.cpp - Build precompiled preambles --*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Helper class to build precompiled preamble.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "clang/AST/DeclObjC.h"
17 #include "clang/Basic/TargetInfo.h"
22 #include "clang/Lex/HeaderSearch.h"
23 #include "clang/Lex/Lexer.h"
24 #include "clang/Lex/Preprocessor.h"
27 #include "llvm/ADT/SmallString.h"
28 #include "llvm/ADT/StringExtras.h"
29 #include "llvm/ADT/StringSet.h"
30 #include "llvm/ADT/iterator_range.h"
31 #include "llvm/Config/llvm-config.h"
32 #include "llvm/Support/CrashRecoveryContext.h"
33 #include "llvm/Support/FileSystem.h"
34 #include "llvm/Support/Path.h"
35 #include "llvm/Support/Process.h"
36 #include "llvm/Support/VirtualFileSystem.h"
37 #include <limits>
38 #include <mutex>
39 #include <utility>
40 
41 using namespace clang;
42 
43 namespace {
44 
45 StringRef getInMemoryPreamblePath() {
46 #if defined(LLVM_ON_UNIX)
47  return "/__clang_tmp/___clang_inmemory_preamble___";
48 #elif defined(_WIN32)
49  return "C:\\__clang_tmp\\___clang_inmemory_preamble___";
50 #else
51 #warning "Unknown platform. Defaulting to UNIX-style paths for in-memory PCHs"
52  return "/__clang_tmp/___clang_inmemory_preamble___";
53 #endif
54 }
55 
57 createVFSOverlayForPreamblePCH(StringRef PCHFilename,
58  std::unique_ptr<llvm::MemoryBuffer> PCHBuffer,
60  // We want only the PCH file from the real filesystem to be available,
61  // so we create an in-memory VFS with just that and overlay it on top.
63  new llvm::vfs::InMemoryFileSystem());
64  PCHFS->addFile(PCHFilename, 0, std::move(PCHBuffer));
66  new llvm::vfs::OverlayFileSystem(VFS));
67  Overlay->pushOverlay(PCHFS);
68  return Overlay;
69 }
70 
71 class PreambleDependencyCollector : public DependencyCollector {
72 public:
73  // We want to collect all dependencies for correctness. Avoiding the real
74  // system dependencies (e.g. stl from /usr/lib) would probably be a good idea,
75  // but there is no way to distinguish between those and the ones that can be
76  // spuriously added by '-isystem' (e.g. to suppress warnings from those
77  // headers).
78  bool needSystemDependencies() override { return true; }
79 };
80 
81 // Collects files whose existence would invalidate the preamble.
82 // Collecting *all* of these would make validating it too slow though, so we
83 // just find all the candidates for 'file not found' diagnostics.
84 //
85 // A caveat that may be significant for generated files: we'll omit files under
86 // search path entries whose roots don't exist when the preamble is built.
87 // These are pruned by InitHeaderSearch and so we don't see the search path.
88 // It would be nice to include them but we don't want to duplicate all the rest
89 // of the InitHeaderSearch logic to reconstruct them.
90 class MissingFileCollector : public PPCallbacks {
91  llvm::StringSet<> &Out;
92  const HeaderSearch &Search;
93  const SourceManager &SM;
94 
95 public:
96  MissingFileCollector(llvm::StringSet<> &Out, const HeaderSearch &Search,
97  const SourceManager &SM)
98  : Out(Out), Search(Search), SM(SM) {}
99 
100  void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
101  StringRef FileName, bool IsAngled,
102  CharSourceRange FilenameRange, const FileEntry *File,
103  StringRef SearchPath, StringRef RelativePath,
104  const Module *Imported,
105  SrcMgr::CharacteristicKind FileType) override {
106  // File is null if it wasn't found.
107  // (We have some false negatives if PP recovered e.g. <foo> -> "foo")
108  if (File != nullptr)
109  return;
110 
111  // If it's a rare absolute include, we know the full path already.
112  if (llvm::sys::path::is_absolute(FileName)) {
113  Out.insert(FileName);
114  return;
115  }
116 
117  // Reconstruct the filenames that would satisfy this directive...
119  auto NotFoundRelativeTo = [&](const DirectoryEntry *DE) {
120  Buf = DE->getName();
121  llvm::sys::path::append(Buf, FileName);
122  llvm::sys::path::remove_dots(Buf, /*remove_dot_dot=*/true);
123  Out.insert(Buf);
124  };
125  // ...relative to the including file.
126  if (!IsAngled) {
127  if (const FileEntry *IncludingFile =
128  SM.getFileEntryForID(SM.getFileID(IncludeTok.getLocation())))
129  if (IncludingFile->getDir())
130  NotFoundRelativeTo(IncludingFile->getDir());
131  }
132  // ...relative to the search paths.
133  for (const auto &Dir : llvm::make_range(
134  IsAngled ? Search.angled_dir_begin() : Search.search_dir_begin(),
135  Search.search_dir_end())) {
136  // No support for frameworks or header maps yet.
137  if (Dir.isNormalDir())
138  NotFoundRelativeTo(Dir.getDir());
139  }
140  }
141 };
142 
143 /// Keeps a track of files to be deleted in destructor.
144 class TemporaryFiles {
145 public:
146  // A static instance to be used by all clients.
147  static TemporaryFiles &getInstance();
148 
149 private:
150  // Disallow constructing the class directly.
151  TemporaryFiles() = default;
152  // Disallow copy.
153  TemporaryFiles(const TemporaryFiles &) = delete;
154 
155 public:
156  ~TemporaryFiles();
157 
158  /// Adds \p File to a set of tracked files.
159  void addFile(StringRef File);
160 
161  /// Remove \p File from disk and from the set of tracked files.
162  void removeFile(StringRef File);
163 
164 private:
165  std::mutex Mutex;
166  llvm::StringSet<> Files;
167 };
168 
169 TemporaryFiles &TemporaryFiles::getInstance() {
170  static TemporaryFiles Instance;
171  return Instance;
172 }
173 
174 TemporaryFiles::~TemporaryFiles() {
175  std::lock_guard<std::mutex> Guard(Mutex);
176  for (const auto &File : Files)
177  llvm::sys::fs::remove(File.getKey());
178 }
179 
180 void TemporaryFiles::addFile(StringRef File) {
181  std::lock_guard<std::mutex> Guard(Mutex);
182  auto IsInserted = Files.insert(File).second;
183  (void)IsInserted;
184  assert(IsInserted && "File has already been added");
185 }
186 
187 void TemporaryFiles::removeFile(StringRef File) {
188  std::lock_guard<std::mutex> Guard(Mutex);
189  auto WasPresent = Files.erase(File);
190  (void)WasPresent;
191  assert(WasPresent && "File was not tracked");
192  llvm::sys::fs::remove(File);
193 }
194 
195 class PrecompilePreambleAction : public ASTFrontendAction {
196 public:
197  PrecompilePreambleAction(std::string *InMemStorage,
198  PreambleCallbacks &Callbacks)
199  : InMemStorage(InMemStorage), Callbacks(Callbacks) {}
200 
201  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
202  StringRef InFile) override;
203 
204  bool hasEmittedPreamblePCH() const { return HasEmittedPreamblePCH; }
205 
206  void setEmittedPreamblePCH(ASTWriter &Writer) {
207  this->HasEmittedPreamblePCH = true;
208  Callbacks.AfterPCHEmitted(Writer);
209  }
210 
211  bool BeginSourceFileAction(CompilerInstance &CI) override {
212  assert(CI.getLangOpts().CompilingPCH);
214  }
215 
216  bool shouldEraseOutputFiles() override { return !hasEmittedPreamblePCH(); }
217  bool hasCodeCompletionSupport() const override { return false; }
218  bool hasASTFileSupport() const override { return false; }
219  TranslationUnitKind getTranslationUnitKind() override { return TU_Prefix; }
220 
221 private:
222  friend class PrecompilePreambleConsumer;
223 
224  bool HasEmittedPreamblePCH = false;
225  std::string *InMemStorage;
226  PreambleCallbacks &Callbacks;
227 };
228 
229 class PrecompilePreambleConsumer : public PCHGenerator {
230 public:
231  PrecompilePreambleConsumer(PrecompilePreambleAction &Action,
232  const Preprocessor &PP,
233  InMemoryModuleCache &ModuleCache,
234  StringRef isysroot,
235  std::unique_ptr<raw_ostream> Out)
236  : PCHGenerator(PP, ModuleCache, "", isysroot,
237  std::make_shared<PCHBuffer>(),
238  ArrayRef<std::shared_ptr<ModuleFileExtension>>(),
239  /*AllowASTWithErrors=*/true),
240  Action(Action), Out(std::move(Out)) {}
241 
242  bool HandleTopLevelDecl(DeclGroupRef DG) override {
243  Action.Callbacks.HandleTopLevelDecl(DG);
244  return true;
245  }
246 
247  void HandleTranslationUnit(ASTContext &Ctx) override {
249  if (!hasEmittedPCH())
250  return;
251 
252  // Write the generated bitstream to "Out".
253  *Out << getPCH();
254  // Make sure it hits disk now.
255  Out->flush();
256  // Free the buffer.
258  getPCH() = std::move(Empty);
259 
260  Action.setEmittedPreamblePCH(getWriter());
261  }
262 
263  bool shouldSkipFunctionBody(Decl *D) override {
264  return Action.Callbacks.shouldSkipFunctionBody(D);
265  }
266 
267 private:
268  PrecompilePreambleAction &Action;
269  std::unique_ptr<raw_ostream> Out;
270 };
271 
272 std::unique_ptr<ASTConsumer>
273 PrecompilePreambleAction::CreateASTConsumer(CompilerInstance &CI,
274  StringRef InFile) {
275  std::string Sysroot;
277  return nullptr;
278 
279  std::unique_ptr<llvm::raw_ostream> OS;
280  if (InMemStorage) {
281  OS = std::make_unique<llvm::raw_string_ostream>(*InMemStorage);
282  } else {
283  std::string OutputFile;
284  OS = GeneratePCHAction::CreateOutputFile(CI, InFile, OutputFile);
285  }
286  if (!OS)
287  return nullptr;
288 
290  Sysroot.clear();
291 
292  return std::make_unique<PrecompilePreambleConsumer>(
293  *this, CI.getPreprocessor(), CI.getModuleCache(), Sysroot, std::move(OS));
294 }
295 
296 template <class T> bool moveOnNoError(llvm::ErrorOr<T> Val, T &Output) {
297  if (!Val)
298  return false;
299  Output = std::move(*Val);
300  return true;
301 }
302 
303 } // namespace
304 
306  const llvm::MemoryBufferRef &Buffer,
307  unsigned MaxLines) {
308  return Lexer::ComputePreamble(Buffer.getBuffer(), LangOpts, MaxLines);
309 }
310 
311 llvm::ErrorOr<PrecompiledPreamble> PrecompiledPreamble::Build(
312  const CompilerInvocation &Invocation,
313  const llvm::MemoryBuffer *MainFileBuffer, PreambleBounds Bounds,
314  DiagnosticsEngine &Diagnostics,
316  std::shared_ptr<PCHContainerOperations> PCHContainerOps, bool StoreInMemory,
317  PreambleCallbacks &Callbacks) {
318  assert(VFS && "VFS is null");
319 
320  auto PreambleInvocation = std::make_shared<CompilerInvocation>(Invocation);
321  FrontendOptions &FrontendOpts = PreambleInvocation->getFrontendOpts();
322  PreprocessorOptions &PreprocessorOpts =
323  PreambleInvocation->getPreprocessorOpts();
324 
326  if (!StoreInMemory) {
327  // Create a temporary file for the precompiled preamble. In rare
328  // circumstances, this can fail.
329  llvm::ErrorOr<PrecompiledPreamble::TempPCHFile> PreamblePCHFile =
330  PrecompiledPreamble::TempPCHFile::CreateNewPreamblePCHFile();
331  if (!PreamblePCHFile)
333  TempFile = std::move(*PreamblePCHFile);
334  }
335 
336  PCHStorage Storage = StoreInMemory ? PCHStorage(InMemoryPreamble())
337  : PCHStorage(std::move(*TempFile));
338 
339  // Save the preamble text for later; we'll need to compare against it for
340  // subsequent reparses.
341  std::vector<char> PreambleBytes(MainFileBuffer->getBufferStart(),
342  MainFileBuffer->getBufferStart() +
343  Bounds.Size);
344  bool PreambleEndsAtStartOfLine = Bounds.PreambleEndsAtStartOfLine;
345 
346  // Tell the compiler invocation to generate a temporary precompiled header.
347  FrontendOpts.ProgramAction = frontend::GeneratePCH;
348  FrontendOpts.OutputFile =
349  std::string(StoreInMemory ? getInMemoryPreamblePath()
350  : Storage.asFile().getFilePath());
351  PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
352  PreprocessorOpts.PrecompiledPreambleBytes.second = false;
353  // Inform preprocessor to record conditional stack when building the preamble.
354  PreprocessorOpts.GeneratePreamble = true;
355 
356  // Create the compiler instance to use for building the precompiled preamble.
357  std::unique_ptr<CompilerInstance> Clang(
358  new CompilerInstance(std::move(PCHContainerOps)));
359 
360  // Recover resources if we crash before exiting this method.
361  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> CICleanup(
362  Clang.get());
363 
364  Clang->setInvocation(std::move(PreambleInvocation));
365  Clang->setDiagnostics(&Diagnostics);
366 
367  // Create the target instance.
368  if (!Clang->createTarget())
370 
371  if (Clang->getFrontendOpts().Inputs.size() != 1 ||
372  Clang->getFrontendOpts().Inputs[0].getKind().getFormat() !=
374  Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() ==
377  }
378 
379  // Clear out old caches and data.
380  Diagnostics.Reset();
381  ProcessWarningOptions(Diagnostics, Clang->getDiagnosticOpts());
382 
383  VFS =
384  createVFSFromCompilerInvocation(Clang->getInvocation(), Diagnostics, VFS);
385 
386  // Create a file manager object to provide access to and cache the filesystem.
387  Clang->setFileManager(new FileManager(Clang->getFileSystemOpts(), VFS));
388 
389  // Create the source manager.
390  Clang->setSourceManager(
391  new SourceManager(Diagnostics, Clang->getFileManager()));
392 
393  auto PreambleDepCollector = std::make_shared<PreambleDependencyCollector>();
394  Clang->addDependencyCollector(PreambleDepCollector);
395 
396  Clang->getLangOpts().CompilingPCH = true;
397 
398  // Remap the main source file to the preamble buffer.
399  StringRef MainFilePath = FrontendOpts.Inputs[0].getFile();
400  auto PreambleInputBuffer = llvm::MemoryBuffer::getMemBufferCopy(
401  MainFileBuffer->getBuffer().slice(0, Bounds.Size), MainFilePath);
402  if (PreprocessorOpts.RetainRemappedFileBuffers) {
403  // MainFileBuffer will be deleted by unique_ptr after leaving the method.
404  PreprocessorOpts.addRemappedFile(MainFilePath, PreambleInputBuffer.get());
405  } else {
406  // In that case, remapped buffer will be deleted by CompilerInstance on
407  // BeginSourceFile, so we call release() to avoid double deletion.
408  PreprocessorOpts.addRemappedFile(MainFilePath,
409  PreambleInputBuffer.release());
410  }
411 
412  std::unique_ptr<PrecompilePreambleAction> Act;
413  Act.reset(new PrecompilePreambleAction(
414  StoreInMemory ? &Storage.asMemory().Data : nullptr, Callbacks));
415  Callbacks.BeforeExecute(*Clang);
416  if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0]))
418 
419  std::unique_ptr<PPCallbacks> DelegatedPPCallbacks =
420  Callbacks.createPPCallbacks();
421  if (DelegatedPPCallbacks)
422  Clang->getPreprocessor().addPPCallbacks(std::move(DelegatedPPCallbacks));
423  if (auto CommentHandler = Callbacks.getCommentHandler())
424  Clang->getPreprocessor().addCommentHandler(CommentHandler);
425  llvm::StringSet<> MissingFiles;
426  Clang->getPreprocessor().addPPCallbacks(
427  std::make_unique<MissingFileCollector>(
428  MissingFiles, Clang->getPreprocessor().getHeaderSearchInfo(),
429  Clang->getSourceManager()));
430 
431  if (llvm::Error Err = Act->Execute())
432  return errorToErrorCode(std::move(Err));
433 
434  // Run the callbacks.
435  Callbacks.AfterExecute(*Clang);
436 
437  Act->EndSourceFile();
438 
439  if (!Act->hasEmittedPreamblePCH())
441 
442  // Keep track of all of the files that the source manager knows about,
443  // so we can verify whether they have changed or not.
444  llvm::StringMap<PrecompiledPreamble::PreambleFileHash> FilesInPreamble;
445 
446  SourceManager &SourceMgr = Clang->getSourceManager();
447  for (auto &Filename : PreambleDepCollector->getDependencies()) {
448  auto FileOrErr = Clang->getFileManager().getFile(Filename);
449  if (!FileOrErr ||
450  *FileOrErr == SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()))
451  continue;
452  auto File = *FileOrErr;
453  if (time_t ModTime = File->getModificationTime()) {
454  FilesInPreamble[File->getName()] =
455  PrecompiledPreamble::PreambleFileHash::createForFile(File->getSize(),
456  ModTime);
457  } else {
458  llvm::MemoryBufferRef Buffer =
460  FilesInPreamble[File->getName()] =
461  PrecompiledPreamble::PreambleFileHash::createForMemoryBuffer(Buffer);
462  }
463  }
464 
465  return PrecompiledPreamble(
466  std::move(Storage), std::move(PreambleBytes), PreambleEndsAtStartOfLine,
467  std::move(FilesInPreamble), std::move(MissingFiles));
468 }
469 
471  return PreambleBounds(PreambleBytes.size(), PreambleEndsAtStartOfLine);
472 }
473 
475  switch (Storage.getKind()) {
476  case PCHStorage::Kind::Empty:
477  assert(false && "Calling getSize() on invalid PrecompiledPreamble. "
478  "Was it std::moved?");
479  return 0;
480  case PCHStorage::Kind::InMemory:
481  return Storage.asMemory().Data.size();
482  case PCHStorage::Kind::TempFile: {
483  uint64_t Result;
484  if (llvm::sys::fs::file_size(Storage.asFile().getFilePath(), Result))
485  return 0;
486 
487  assert(Result <= std::numeric_limits<std::size_t>::max() &&
488  "file size did not fit into size_t");
489  return Result;
490  }
491  }
492  llvm_unreachable("Unhandled storage kind");
493 }
494 
496  const llvm::MemoryBufferRef &MainFileBuffer,
497  PreambleBounds Bounds,
498  llvm::vfs::FileSystem &VFS) const {
499 
500  assert(
501  Bounds.Size <= MainFileBuffer.getBufferSize() &&
502  "Buffer is too large. Bounds were calculated from a different buffer?");
503 
504  auto PreambleInvocation = std::make_shared<CompilerInvocation>(Invocation);
505  PreprocessorOptions &PreprocessorOpts =
506  PreambleInvocation->getPreprocessorOpts();
507 
508  // We've previously computed a preamble. Check whether we have the same
509  // preamble now that we did before, and that there's enough space in
510  // the main-file buffer within the precompiled preamble to fit the
511  // new main file.
512  if (PreambleBytes.size() != Bounds.Size ||
513  PreambleEndsAtStartOfLine != Bounds.PreambleEndsAtStartOfLine ||
514  !std::equal(PreambleBytes.begin(), PreambleBytes.end(),
515  MainFileBuffer.getBuffer().begin()))
516  return false;
517  // The preamble has not changed. We may be able to re-use the precompiled
518  // preamble.
519 
520  // Check that none of the files used by the preamble have changed.
521  // First, make a record of those files that have been overridden via
522  // remapping or unsaved_files.
523  std::map<llvm::sys::fs::UniqueID, PreambleFileHash> OverriddenFiles;
524  llvm::StringSet<> OverriddenAbsPaths; // Either by buffers or files.
525  for (const auto &R : PreprocessorOpts.RemappedFiles) {
526  llvm::vfs::Status Status;
527  if (!moveOnNoError(VFS.status(R.second), Status)) {
528  // If we can't stat the file we're remapping to, assume that something
529  // horrible happened.
530  return false;
531  }
532  // If a mapped file was previously missing, then it has changed.
533  llvm::SmallString<128> MappedPath(R.first);
534  if (!VFS.makeAbsolute(MappedPath))
535  OverriddenAbsPaths.insert(MappedPath);
536 
537  OverriddenFiles[Status.getUniqueID()] = PreambleFileHash::createForFile(
538  Status.getSize(), llvm::sys::toTimeT(Status.getLastModificationTime()));
539  }
540 
541  // OverridenFileBuffers tracks only the files not found in VFS.
542  llvm::StringMap<PreambleFileHash> OverridenFileBuffers;
543  for (const auto &RB : PreprocessorOpts.RemappedFileBuffers) {
544  const PrecompiledPreamble::PreambleFileHash PreambleHash =
545  PreambleFileHash::createForMemoryBuffer(RB.second->getMemBufferRef());
546  llvm::vfs::Status Status;
547  if (moveOnNoError(VFS.status(RB.first), Status))
548  OverriddenFiles[Status.getUniqueID()] = PreambleHash;
549  else
550  OverridenFileBuffers[RB.first] = PreambleHash;
551 
552  llvm::SmallString<128> MappedPath(RB.first);
553  if (!VFS.makeAbsolute(MappedPath))
554  OverriddenAbsPaths.insert(MappedPath);
555  }
556 
557  // Check whether anything has changed.
558  for (const auto &F : FilesInPreamble) {
559  auto OverridenFileBuffer = OverridenFileBuffers.find(F.first());
560  if (OverridenFileBuffer != OverridenFileBuffers.end()) {
561  // The file's buffer was remapped and the file was not found in VFS.
562  // Check whether it matches up with the previous mapping.
563  if (OverridenFileBuffer->second != F.second)
564  return false;
565  continue;
566  }
567 
568  llvm::vfs::Status Status;
569  if (!moveOnNoError(VFS.status(F.first()), Status)) {
570  // If the file's buffer is not remapped and we can't stat it,
571  // assume that something horrible happened.
572  return false;
573  }
574 
575  std::map<llvm::sys::fs::UniqueID, PreambleFileHash>::iterator Overridden =
576  OverriddenFiles.find(Status.getUniqueID());
577  if (Overridden != OverriddenFiles.end()) {
578  // This file was remapped; check whether the newly-mapped file
579  // matches up with the previous mapping.
580  if (Overridden->second != F.second)
581  return false;
582  continue;
583  }
584 
585  // Neither the file's buffer nor the file itself was remapped;
586  // check whether it has changed on disk.
587  if (Status.getSize() != uint64_t(F.second.Size) ||
588  llvm::sys::toTimeT(Status.getLastModificationTime()) !=
589  F.second.ModTime)
590  return false;
591  }
592  for (const auto &F : MissingFiles) {
593  // A missing file may be "provided" by an override buffer or file.
594  if (OverriddenAbsPaths.count(F.getKey()))
595  return false;
596  // If a file previously recorded as missing exists as a regular file, then
597  // consider the preamble out-of-date.
598  if (auto Status = VFS.status(F.getKey())) {
599  if (Status->isRegularFile())
600  return false;
601  }
602  }
603  return true;
604 }
605 
608  llvm::MemoryBuffer *MainFileBuffer) const {
609  PreambleBounds Bounds(PreambleBytes.size(), PreambleEndsAtStartOfLine);
610  configurePreamble(Bounds, CI, VFS, MainFileBuffer);
611 }
612 
615  llvm::MemoryBuffer *MainFileBuffer) const {
616  auto Bounds = ComputePreambleBounds(*CI.getLangOpts(), *MainFileBuffer, 0);
617  configurePreamble(Bounds, CI, VFS, MainFileBuffer);
618 }
619 
621  PCHStorage Storage, std::vector<char> PreambleBytes,
622  bool PreambleEndsAtStartOfLine,
623  llvm::StringMap<PreambleFileHash> FilesInPreamble,
624  llvm::StringSet<> MissingFiles)
625  : Storage(std::move(Storage)), FilesInPreamble(std::move(FilesInPreamble)),
626  MissingFiles(std::move(MissingFiles)),
627  PreambleBytes(std::move(PreambleBytes)),
628  PreambleEndsAtStartOfLine(PreambleEndsAtStartOfLine) {
629  assert(this->Storage.getKind() != PCHStorage::Kind::Empty);
630 }
631 
632 llvm::ErrorOr<PrecompiledPreamble::TempPCHFile>
633 PrecompiledPreamble::TempPCHFile::CreateNewPreamblePCHFile() {
634  // FIXME: This is a hack so that we can override the preamble file during
635  // crash-recovery testing, which is the only case where the preamble files
636  // are not necessarily cleaned up.
637  if (const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE"))
638  return TempPCHFile(TmpFile);
639 
641  // Using a version of createTemporaryFile with a file descriptor guarantees
642  // that we would never get a race condition in a multi-threaded setting
643  // (i.e., multiple threads getting the same temporary path).
644  int FD;
645  auto EC = llvm::sys::fs::createTemporaryFile("preamble", "pch", FD, File);
646  if (EC)
647  return EC;
648  // We only needed to make sure the file exists, close the file right away.
649  llvm::sys::Process::SafelyCloseFileDescriptor(FD);
650  return TempPCHFile(std::string(std::move(File).str()));
651 }
652 
653 PrecompiledPreamble::TempPCHFile::TempPCHFile(std::string FilePath)
654  : FilePath(std::move(FilePath)) {
655  TemporaryFiles::getInstance().addFile(*this->FilePath);
656 }
657 
658 PrecompiledPreamble::TempPCHFile::TempPCHFile(TempPCHFile &&Other) {
659  FilePath = std::move(Other.FilePath);
660  Other.FilePath = None;
661 }
662 
663 PrecompiledPreamble::TempPCHFile &PrecompiledPreamble::TempPCHFile::
664 operator=(TempPCHFile &&Other) {
665  RemoveFileIfPresent();
666 
667  FilePath = std::move(Other.FilePath);
668  Other.FilePath = None;
669  return *this;
670 }
671 
672 PrecompiledPreamble::TempPCHFile::~TempPCHFile() { RemoveFileIfPresent(); }
673 
674 void PrecompiledPreamble::TempPCHFile::RemoveFileIfPresent() {
675  if (FilePath) {
676  TemporaryFiles::getInstance().removeFile(*FilePath);
677  FilePath = None;
678  }
679 }
680 
681 llvm::StringRef PrecompiledPreamble::TempPCHFile::getFilePath() const {
682  assert(FilePath && "TempPCHFile doesn't have a FilePath. Had it been moved?");
683  return *FilePath;
684 }
685 
686 PrecompiledPreamble::PCHStorage::PCHStorage(TempPCHFile File)
687  : StorageKind(Kind::TempFile) {
688  new (&asFile()) TempPCHFile(std::move(File));
689 }
690 
691 PrecompiledPreamble::PCHStorage::PCHStorage(InMemoryPreamble Memory)
692  : StorageKind(Kind::InMemory) {
693  new (&asMemory()) InMemoryPreamble(std::move(Memory));
694 }
695 
696 PrecompiledPreamble::PCHStorage::PCHStorage(PCHStorage &&Other) : PCHStorage() {
697  *this = std::move(Other);
698 }
699 
700 PrecompiledPreamble::PCHStorage &PrecompiledPreamble::PCHStorage::
701 operator=(PCHStorage &&Other) {
702  destroy();
703 
704  StorageKind = Other.StorageKind;
705  switch (StorageKind) {
706  case Kind::Empty:
707  // do nothing;
708  break;
709  case Kind::TempFile:
710  new (&asFile()) TempPCHFile(std::move(Other.asFile()));
711  break;
712  case Kind::InMemory:
713  new (&asMemory()) InMemoryPreamble(std::move(Other.asMemory()));
714  break;
715  }
716 
717  Other.setEmpty();
718  return *this;
719 }
720 
721 PrecompiledPreamble::PCHStorage::~PCHStorage() { destroy(); }
722 
723 PrecompiledPreamble::PCHStorage::Kind
725  return StorageKind;
726 }
727 
728 PrecompiledPreamble::TempPCHFile &PrecompiledPreamble::PCHStorage::asFile() {
729  assert(getKind() == Kind::TempFile);
730  return *reinterpret_cast<TempPCHFile *>(&Storage);
731 }
732 
733 const PrecompiledPreamble::TempPCHFile &
734 PrecompiledPreamble::PCHStorage::asFile() const {
735  return const_cast<PCHStorage *>(this)->asFile();
736 }
737 
738 PrecompiledPreamble::InMemoryPreamble &
739 PrecompiledPreamble::PCHStorage::asMemory() {
740  assert(getKind() == Kind::InMemory);
741  return *reinterpret_cast<InMemoryPreamble *>(&Storage);
742 }
743 
744 const PrecompiledPreamble::InMemoryPreamble &
745 PrecompiledPreamble::PCHStorage::asMemory() const {
746  return const_cast<PCHStorage *>(this)->asMemory();
747 }
748 
749 void PrecompiledPreamble::PCHStorage::destroy() {
750  switch (StorageKind) {
751  case Kind::Empty:
752  return;
753  case Kind::TempFile:
754  asFile().~TempPCHFile();
755  return;
756  case Kind::InMemory:
757  asMemory().~InMemoryPreamble();
758  return;
759  }
760 }
761 
762 void PrecompiledPreamble::PCHStorage::setEmpty() {
763  destroy();
764  StorageKind = Kind::Empty;
765 }
766 
767 PrecompiledPreamble::PreambleFileHash
768 PrecompiledPreamble::PreambleFileHash::createForFile(off_t Size,
769  time_t ModTime) {
770  PreambleFileHash Result;
771  Result.Size = Size;
772  Result.ModTime = ModTime;
773  Result.MD5 = {};
774  return Result;
775 }
776 
777 PrecompiledPreamble::PreambleFileHash
778 PrecompiledPreamble::PreambleFileHash::createForMemoryBuffer(
779  const llvm::MemoryBufferRef &Buffer) {
780  PreambleFileHash Result;
781  Result.Size = Buffer.getBufferSize();
782  Result.ModTime = 0;
783 
784  llvm::MD5 MD5Ctx;
785  MD5Ctx.update(Buffer.getBuffer().data());
786  MD5Ctx.final(Result.MD5);
787 
788  return Result;
789 }
790 
791 void PrecompiledPreamble::configurePreamble(
794  llvm::MemoryBuffer *MainFileBuffer) const {
795  assert(VFS);
796 
797  auto &PreprocessorOpts = CI.getPreprocessorOpts();
798 
799  // Remap main file to point to MainFileBuffer.
800  auto MainFilePath = CI.getFrontendOpts().Inputs[0].getFile();
801  PreprocessorOpts.addRemappedFile(MainFilePath, MainFileBuffer);
802 
803  // Configure ImpicitPCHInclude.
804  PreprocessorOpts.PrecompiledPreambleBytes.first = Bounds.Size;
805  PreprocessorOpts.PrecompiledPreambleBytes.second =
807  PreprocessorOpts.DisablePCHOrModuleValidation =
809 
810  setupPreambleStorage(Storage, PreprocessorOpts, VFS);
811 }
812 
813 void PrecompiledPreamble::setupPreambleStorage(
814  const PCHStorage &Storage, PreprocessorOptions &PreprocessorOpts,
816  if (Storage.getKind() == PCHStorage::Kind::TempFile) {
817  const TempPCHFile &PCHFile = Storage.asFile();
818  PreprocessorOpts.ImplicitPCHInclude = std::string(PCHFile.getFilePath());
819 
820  // Make sure we can access the PCH file even if we're using a VFS
822  llvm::vfs::getRealFileSystem();
823  auto PCHPath = PCHFile.getFilePath();
824  if (VFS == RealFS || VFS->exists(PCHPath))
825  return;
826  auto Buf = RealFS->getBufferForFile(PCHPath);
827  if (!Buf) {
828  // We can't read the file even from RealFS, this is clearly an error,
829  // but we'll just leave the current VFS as is and let clang's code
830  // figure out what to do with missing PCH.
831  return;
832  }
833 
834  // We have a slight inconsistency here -- we're using the VFS to
835  // read files, but the PCH was generated in the real file system.
836  VFS = createVFSOverlayForPreamblePCH(PCHPath, std::move(*Buf), VFS);
837  } else {
838  assert(Storage.getKind() == PCHStorage::Kind::InMemory);
839  // For in-memory preamble, we have to provide a VFS overlay that makes it
840  // accessible.
841  StringRef PCHPath = getInMemoryPreamblePath();
842  PreprocessorOpts.ImplicitPCHInclude = std::string(PCHPath);
843 
844  auto Buf = llvm::MemoryBuffer::getMemBuffer(Storage.asMemory().Data);
845  VFS = createVFSOverlayForPreamblePCH(PCHPath, std::move(Buf), VFS);
846  }
847 }
848 
853 std::unique_ptr<PPCallbacks> PreambleCallbacks::createPPCallbacks() {
854  return nullptr;
855 }
857 
858 static llvm::ManagedStatic<BuildPreambleErrorCategory> BuildPreambleErrCategory;
859 
861  return std::error_code(static_cast<int>(Error), *BuildPreambleErrCategory);
862 }
863 
864 const char *BuildPreambleErrorCategory::name() const noexcept {
865  return "build-preamble.error";
866 }
867 
869  switch (static_cast<BuildPreambleError>(condition)) {
871  return "Could not create temporary file for PCH";
873  return "CreateTargetInfo() return null";
875  return "BeginSourceFile() return an error";
877  return "Could not emit PCH";
879  return "Command line arguments must contain exactly one source file";
880  }
881  llvm_unreachable("unexpected BuildPreambleError");
882 }
clang::DiagnosticsEngine::Reset
void Reset()
Reset the state of the diagnostic object to its initial configuration.
Definition: Diagnostic.cpp:133
max
__DEVICE__ int max(int __a, int __b)
Definition: __clang_cuda_math.h:196
clang::PreambleCallbacks
A set of callbacks to gather useful information while building a preamble.
Definition: PrecompiledPreamble.h:273
clang::DeclaratorContext::File
@ File
Error
llvm::Error Error
Definition: ByteCodeEmitter.cpp:20
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::PreprocessorOptions
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
Definition: PreprocessorOptions.h:64
clang::ProcessWarningOptions
void ProcessWarningOptions(DiagnosticsEngine &Diags, const DiagnosticOptions &Opts, bool ReportDiags=true)
ProcessWarningOptions - Initialize the diagnostic client and process the warning options specified on...
Definition: Warnings.cpp:44
clang::FrontendOptions::ProgramAction
frontend::ActionKind ProgramAction
The frontend action to perform.
Definition: FrontendOptions.h:405
llvm::SmallVector< char, 0 >
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::PPCallbacks
This interface provides a way to observe the actions of the preprocessor as it does its thing.
Definition: PPCallbacks.h:35
TargetInfo.h
clang::HeaderSearch::angled_dir_begin
search_dir_iterator angled_dir_begin() const
Definition: HeaderSearch.h:773
clang::CompilerInstance::getLangOpts
LangOptions & getLangOpts()
Definition: CompilerInstance.h:301
Filename
StringRef Filename
Definition: Format.cpp:2333
clang::PreambleCallbacks::createPPCallbacks
virtual std::unique_ptr< PPCallbacks > createPPCallbacks()
Creates wrapper class for PPCallbacks so we can also process information about includes that are insi...
Definition: PrecompiledPreamble.cpp:853
clang::DiagnosticsEngine
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:191
clang::PreambleCallbacks::HandleTopLevelDecl
virtual void HandleTopLevelDecl(DeclGroupRef DG)
Called for each TopLevelDecl.
Definition: PrecompiledPreamble.cpp:852
clang::ModuleFileExtension
An abstract superclass that describes a custom extension to the module/precompiled header file format...
Definition: ModuleFileExtension.h:66
BuildPreambleErrCategory
static llvm::ManagedStatic< BuildPreambleErrorCategory > BuildPreambleErrCategory
Definition: PrecompiledPreamble.cpp:858
llvm::Optional
Definition: LLVM.h:40
clang::SrcMgr::CharacteristicKind
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
Definition: SourceManager.h:78
clang::HeaderSearch::search_dir_begin
search_dir_iterator search_dir_begin() const
Definition: HeaderSearch.h:761
ASTWriter.h
clang::make_error_code
std::error_code make_error_code(BuildPreambleError Error)
Definition: PrecompiledPreamble.cpp:860
clang::Token
Token - This structure provides full information about a lexed token.
Definition: Token.h:34
clang::CommentHandler
Abstract base class that describes a handler that will receive source ranges for each of the comments...
Definition: Preprocessor.h:2490
clang::InputKind::Source
@ Source
Definition: FrontendOptions.h:154
CompilerInvocation.h
clang::PrecompiledPreamble::getSize
std::size_t getSize() const
Returns the size, in bytes, that preamble takes on disk or in memory.
Definition: PrecompiledPreamble.cpp:474
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:626
clang::FrontendOptions::Inputs
SmallVector< FrontendInputFile, 0 > Inputs
The input files and their types.
Definition: FrontendOptions.h:386
Preprocessor.h
clang::HeaderSearch
Encapsulates the information needed to find the file referenced by a #include or #include_next,...
Definition: HeaderSearch.h:169
clang::TU_Prefix
@ TU_Prefix
The translation unit is a prefix to a translation unit, and is not complete.
Definition: LangOptions.h:709
clang::PrecompiledPreamble::getBounds
PreambleBounds getBounds() const
PreambleBounds used to build the preamble.
Definition: PrecompiledPreamble.cpp:470
clang::frontend::GeneratePCH
@ GeneratePCH
Generate pre-compiled header.
Definition: FrontendOptions.h:91
DeclObjC.h
clang::BuildPreambleErrorCategory::message
std::string message(int condition) const override
Definition: PrecompiledPreamble.cpp:868
clang::GeneratePCHAction::CreateOutputFile
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.
Definition: FrontendActions.cpp:158
size_t
__SIZE_TYPE__ size_t
The unsigned integer type of the result of the sizeof operator.
Definition: opencl-c-base.h:102
clang::Module
Describes a module or submodule.
Definition: Module.h:96
clang::CompilerInvocationValueBase::getFrontendOpts
FrontendOptions & getFrontendOpts()
Definition: CompilerInvocation.h:176
FrontendOptions.h
clang::FrontendOptions::OutputFile
std::string OutputFile
The output file, if any.
Definition: FrontendOptions.h:393
clang::DirectoryEntry
Cached information about one directory (either on disk or in the virtual file system).
Definition: DirectoryEntry.h:33
clang::XRayInstrKind::None
constexpr XRayInstrMask None
Definition: XRayInstr.h:38
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:212
clang::SourceManager::getMainFileID
FileID getMainFileID() const
Returns the FileID of the main source file.
Definition: SourceManager.h:840
clang::FrontendOptions
FrontendOptions - Options for controlling the behavior of the frontend.
Definition: FrontendOptions.h:228
PrecompiledPreamble.h
clang::PrecompiledPreamble::AddImplicitPreamble
void AddImplicitPreamble(CompilerInvocation &CI, IntrusiveRefCntPtr< llvm::vfs::FileSystem > &VFS, llvm::MemoryBuffer *MainFileBuffer) const
Changes options inside CI to use PCH from this preamble.
Definition: PrecompiledPreamble.cpp:606
llvm::SmallString
Definition: LLVM.h:37
clang::CompilerInvocationRefBase::getLangOpts
LangOptions * getLangOpts()
Definition: CompilerInvocation.h:104
clang::PreambleBounds
Describes the bounds (start, size) of the preamble and a flag required by PreprocessorOptions::Precom...
Definition: Lexer.h:58
clang::PreambleBounds::Size
unsigned Size
Size of the preamble in bytes.
Definition: Lexer.h:60
clang::CompilerInstance::getPreprocessor
Preprocessor & getPreprocessor() const
Return the current preprocessor.
Definition: CompilerInstance.h:444
clang::PreambleCallbacks::getCommentHandler
virtual CommentHandler * getCommentHandler()
The returned CommentHandler will be added to the preprocessor if not null.
Definition: PrecompiledPreamble.cpp:856
clang::ASTWriter
Writes an AST file containing the contents of a translation unit.
Definition: ASTWriter.h:99
clang::DependencyCollector
An interface for collecting the dependencies of a compilation.
Definition: Utils.h:77
clang::PrecompiledPreamble::OverridePreamble
void OverridePreamble(CompilerInvocation &CI, IntrusiveRefCntPtr< llvm::vfs::FileSystem > &VFS, llvm::MemoryBuffer *MainFileBuffer) const
Configure CI to use this preamble.
Definition: PrecompiledPreamble.cpp:613
clang::CompilerInstance
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Definition: CompilerInstance.h:74
getKind
static Decl::Kind getKind(const Decl *D)
Definition: DeclBase.cpp:998
clang::FileEntry
Cached information about one file (either on disk or in the virtual file system).
Definition: FileEntry.h:331
clang::DisableValidationForModuleKind::PCH
@ PCH
Disable validation for a precompiled header and the modules it depends on.
clang::BuildPreambleErrorCategory::name
const char * name() const noexcept override
Definition: PrecompiledPreamble.cpp:864
clang::CompilerInstance::getModuleCache
InMemoryModuleCache & getModuleCache() const
Definition: CompilerInstance.h:819
clang::PreambleCallbacks::AfterExecute
virtual void AfterExecute(CompilerInstance &CI)
Called after FrontendAction::Execute(), but before FrontendAction::EndSourceFile().
Definition: PrecompiledPreamble.cpp:850
clang::transformer::remove
ASTEdit remove(RangeSelector S)
Removes the source selected by S.
Definition: RewriteRule.cpp:146
clang::BuildPreambleError::BadInputs
@ BadInputs
clang::CompilerInvocationRefBase::getPreprocessorOpts
PreprocessorOptions & getPreprocessorOpts()
Definition: CompilerInvocation.h:126
moveOnNoError
static bool moveOnNoError(llvm::ErrorOr< T > Val, T &Output)
Definition: ASTUnit.cpp:149
clang::PreprocessorOptions::GeneratePreamble
bool GeneratePreamble
True indicates that a preamble is being generated.
Definition: PreprocessorOptions.h:132
clang::InclusionDirective
Record the location of an inclusion directive, such as an #include or #import statement.
Definition: PreprocessingRecord.h:208
clang::PreprocessorOptions::RetainRemappedFileBuffers
bool RetainRemappedFileBuffers
Whether the compiler instance should retain (i.e., not free) the buffers associated with remapped fil...
Definition: PreprocessorOptions.h:170
clang::GeneratePCHAction::ComputeASTConsumerArguments
static bool ComputeASTConsumerArguments(CompilerInstance &CI, std::string &Sysroot)
Compute the AST consumer arguments that will be used to create the PCHGenerator instance returned by ...
Definition: FrontendActions.cpp:146
clang::syntax::NodeRole::Size
@ Size
LangStandard.h
clang::BuildPreambleError::BeginSourceFileFailed
@ BeginSourceFileFailed
llvm::ArrayRef
Definition: LLVM.h:34
clang::Language::LLVM_IR
@ LLVM_IR
LLVM IR: we accept this so that we can run the optimizer on it, and compile it to assembly or object ...
Lexer.h
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:89
clang::PreprocessorOptions::RemappedFileBuffers
std::vector< std::pair< std::string, llvm::MemoryBuffer * > > RemappedFileBuffers
The set of file-to-buffer remappings, which take existing files on the system (the first part of each...
Definition: PreprocessorOptions.h:162
clang::BuildPreambleError
BuildPreambleError
Definition: PrecompiledPreamble.h:304
HeaderSearch.h
clang::DeclGroupRef
Definition: DeclGroup.h:51
clang::PreambleCallbacks::AfterPCHEmitted
virtual void AfterPCHEmitted(ASTWriter &Writer)
Called after PCH has been emitted.
Definition: PrecompiledPreamble.cpp:851
clang::CharSourceRange
Represents a character-granular source range.
Definition: SourceLocation.h:255
clang::PreprocessorOptions::PrecompiledPreambleBytes
std::pair< unsigned, bool > PrecompiledPreambleBytes
If non-zero, the implicit PCH include is actually a precompiled preamble that covers this number of b...
Definition: PreprocessorOptions.h:125
FrontendActions.h
clang::PCHBuffer
Definition: PCHContainerOperations.h:29
clang::PCHGenerator::HandleTranslationUnit
void HandleTranslationUnit(ASTContext &Ctx) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
Definition: GeneratePCH.cpp:41
clang::createVFSFromCompilerInvocation
IntrusiveRefCntPtr< llvm::vfs::FileSystem > createVFSFromCompilerInvocation(const CompilerInvocation &CI, DiagnosticsEngine &Diags)
Definition: CompilerInvocation.cpp:4659
clang::Token::getLocation
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file.
Definition: Token.h:126
clang::HeaderSearch::search_dir_end
search_dir_iterator search_dir_end() const
Definition: HeaderSearch.h:762
clang::PreprocessorOptions::RemappedFiles
std::vector< std::pair< std::string, std::string > > RemappedFiles
The set of file remappings, which take existing files on the system (the first part of each pair) and...
Definition: PreprocessorOptions.h:157
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:58
clang::ObjCPropertyAttribute::Kind
Kind
Definition: DeclObjCCommon.h:22
std
Definition: Format.h:4034
clang::BuildPreambleError::CouldntEmitPCH
@ CouldntEmitPCH
clang::TranslationUnitKind
TranslationUnitKind
Describes the kind of translation unit being processed.
Definition: LangOptions.h:703
clang::SourceManager::getMemoryBufferForFileOrFake
llvm::MemoryBufferRef getMemoryBufferForFileOrFake(const FileEntry *File)
Retrieve the memory buffer associated with the given file.
Definition: SourceManager.h:934
clang
Definition: CalledOnceCheck.h:17
clang::FileManager
Implements support for file system lookup, file system caching, and directory search management.
Definition: FileManager.h:53
clang::PreambleBounds::PreambleEndsAtStartOfLine
bool PreambleEndsAtStartOfLine
Whether the preamble ends at the start of a new line.
Definition: Lexer.h:66
clang::BuildPreambleError::CouldntCreateTargetInfo
@ CouldntCreateTargetInfo
clang::CompilerInvocation
Helper class for holding the data necessary to invoke the compiler.
Definition: CompilerInvocation.h:193
clang::PrecompiledPreamble::PrecompiledPreamble
PrecompiledPreamble(PrecompiledPreamble &&)=default
clang::SourceManager::getFileEntryForID
const FileEntry * getFileEntryForID(FileID FID) const
Returns the FileEntry record for the provided FileID.
Definition: SourceManager.h:1030
clang::InMemoryModuleCache
In-memory cache for modules.
Definition: InMemoryModuleCache.h:30
clang::FrontendAction::BeginSourceFileAction
virtual bool BeginSourceFileAction(CompilerInstance &CI)
Callback at the start of processing a single input.
Definition: FrontendAction.h:85
clang::PrecompiledPreamble::CanReuse
bool CanReuse(const CompilerInvocation &Invocation, const llvm::MemoryBufferRef &MainFileBuffer, PreambleBounds Bounds, llvm::vfs::FileSystem &VFS) const
Check whether PrecompiledPreamble can be reused for the new contents(MainFileBuffer) of the main file...
Definition: PrecompiledPreamble.cpp:495
clang::SourceManager::getFileManager
FileManager & getFileManager() const
Definition: SourceManager.h:806
clang::FileManager::getFile
llvm::ErrorOr< const FileEntry * > getFile(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Lookup, cache, and verify the specified file (real or virtual).
Definition: FileManager.cpp:196
CompilerInstance.h
clang::PCHGenerator
AST and semantic-analysis consumer that generates a precompiled header from the parsed source code.
Definition: ASTWriter.h:765
clang::CompilerInstance::getFrontendOpts
FrontendOptions & getFrontendOpts()
Definition: CompilerInstance.h:284
clang::FrontendOptions::RelocatablePCH
unsigned RelocatablePCH
When generating PCH files, instruct the AST writer to create relocatable PCH files.
Definition: FrontendOptions.h:235
clang::Lexer::ComputePreamble
static PreambleBounds ComputePreamble(StringRef Buffer, const LangOptions &LangOpts, unsigned MaxLines=0)
Compute the preamble of the given file.
Definition: Lexer.cpp:585
clang::ASTFrontendAction
Abstract base class to use for AST consumer-based frontend actions.
Definition: FrontendAction.h:243
clang::PreambleCallbacks::BeforeExecute
virtual void BeforeExecute(CompilerInstance &CI)
Called before FrontendAction::BeginSourceFile.
Definition: PrecompiledPreamble.cpp:849
FileManager.h
true
#define true
Definition: stdbool.h:16
clang::ComputePreambleBounds
PreambleBounds ComputePreambleBounds(const LangOptions &LangOpts, const llvm::MemoryBufferRef &Buffer, unsigned MaxLines)
Runs lexer to compute suggested preamble bounds.
Definition: PrecompiledPreamble.cpp:305
clang::PreprocessorOptions::addRemappedFile
void addRemappedFile(StringRef From, StringRef To)
Definition: PreprocessorOptions.h:226
PreprocessorOptions.h
clang::Preprocessor
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:130
SM
#define SM(sm)
Definition: Cuda.cpp:78
clang::PrecompiledPreamble::Build
static llvm::ErrorOr< 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)
Try to build PrecompiledPreamble for Invocation.
Definition: PrecompiledPreamble.cpp:311
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem >
clang::PreprocessorOptions::ImplicitPCHInclude
std::string ImplicitPCHInclude
The implicit PCH included at the start of the translation unit, or empty.
Definition: PreprocessorOptions.h:96
clang::ento::ObjKind::OS
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
clang::BuildPreambleError::CouldntCreateTempFile
@ CouldntCreateTempFile