Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Frontend/PrecompiledPreamble.cpp
Line
Count
Source (jump to first uncovered line)
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
13
#include "clang/Frontend/PrecompiledPreamble.h"
14
#include "clang/AST/DeclObjC.h"
15
#include "clang/Basic/TargetInfo.h"
16
#include "clang/Frontend/CompilerInstance.h"
17
#include "clang/Frontend/CompilerInvocation.h"
18
#include "clang/Frontend/FrontendActions.h"
19
#include "clang/Frontend/FrontendOptions.h"
20
#include "clang/Lex/Lexer.h"
21
#include "clang/Lex/Preprocessor.h"
22
#include "clang/Lex/PreprocessorOptions.h"
23
#include "clang/Serialization/ASTWriter.h"
24
#include "llvm/ADT/StringExtras.h"
25
#include "llvm/ADT/StringSet.h"
26
#include "llvm/Config/llvm-config.h"
27
#include "llvm/Support/CrashRecoveryContext.h"
28
#include "llvm/Support/FileSystem.h"
29
#include "llvm/Support/Mutex.h"
30
#include "llvm/Support/MutexGuard.h"
31
#include "llvm/Support/Process.h"
32
#include "llvm/Support/VirtualFileSystem.h"
33
#include <limits>
34
#include <utility>
35
36
using namespace clang;
37
38
namespace {
39
40
0
StringRef getInMemoryPreamblePath() {
41
0
#if defined(LLVM_ON_UNIX)
42
0
  return "/__clang_tmp/___clang_inmemory_preamble___";
43
#elif defined(_WIN32)
44
  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
51
IntrusiveRefCntPtr<llvm::vfs::FileSystem>
52
createVFSOverlayForPreamblePCH(StringRef PCHFilename,
53
                               std::unique_ptr<llvm::MemoryBuffer> PCHBuffer,
54
12
                               IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
55
12
  // We want only the PCH file from the real filesystem to be available,
56
12
  // so we create an in-memory VFS with just that and overlay it on top.
57
12
  IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> PCHFS(
58
12
      new llvm::vfs::InMemoryFileSystem());
59
12
  PCHFS->addFile(PCHFilename, 0, std::move(PCHBuffer));
60
12
  IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> Overlay(
61
12
      new llvm::vfs::OverlayFileSystem(VFS));
62
12
  Overlay->pushOverlay(PCHFS);
63
12
  return Overlay;
64
12
}
65
66
class PreambleDependencyCollector : public DependencyCollector {
67
public:
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
289
  bool needSystemDependencies() override { return true; }
74
};
75
76
/// Keeps a track of files to be deleted in destructor.
77
class TemporaryFiles {
78
public:
79
  // A static instance to be used by all clients.
80
  static TemporaryFiles &getInstance();
81
82
private:
83
  // Disallow constructing the class directly.
84
81
  TemporaryFiles() = default;
85
  // Disallow copy.
86
  TemporaryFiles(const TemporaryFiles &) = delete;
87
88
public:
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
97
private:
98
  llvm::sys::SmartMutex<false> Mutex;
99
  llvm::StringSet<> Files;
100
};
101
102
175
TemporaryFiles &TemporaryFiles::getInstance() {
103
175
  static TemporaryFiles Instance;
104
175
  return Instance;
105
175
}
106
107
81
TemporaryFiles::~TemporaryFiles() {
108
81
  llvm::MutexGuard Guard(Mutex);
109
81
  for (const auto &File : Files)
110
1
    llvm::sys::fs::remove(File.getKey());
111
81
}
112
113
88
void TemporaryFiles::addFile(StringRef File) {
114
88
  llvm::MutexGuard Guard(Mutex);
115
88
  auto IsInserted = Files.insert(File).second;
116
88
  (void)IsInserted;
117
88
  assert(IsInserted && "File has already been added");
118
88
}
119
120
87
void TemporaryFiles::removeFile(StringRef File) {
121
87
  llvm::MutexGuard Guard(Mutex);
122
87
  auto WasPresent = Files.erase(File);
123
87
  (void)WasPresent;
124
87
  assert(WasPresent && "File was not tracked");
125
87
  llvm::sys::fs::remove(File);
126
87
}
127
128
class PrecompilePreambleAction : public ASTFrontendAction {
129
public:
130
  PrecompilePreambleAction(std::string *InMemStorage,
131
                           PreambleCallbacks &Callbacks)
132
88
      : InMemStorage(InMemStorage), Callbacks(Callbacks) {}
133
134
  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
135
                                                 StringRef InFile) override;
136
137
176
  bool hasEmittedPreamblePCH() const { return HasEmittedPreamblePCH; }
138
139
88
  void setEmittedPreamblePCH(ASTWriter &Writer) {
140
88
    this->HasEmittedPreamblePCH = true;
141
88
    Callbacks.AfterPCHEmitted(Writer);
142
88
  }
143
144
88
  bool shouldEraseOutputFiles() override { return !hasEmittedPreamblePCH(); }
145
88
  bool hasCodeCompletionSupport() const override { return false; }
146
0
  bool hasASTFileSupport() const override { return false; }
147
176
  TranslationUnitKind getTranslationUnitKind() override { return TU_Prefix; }
148
149
private:
150
  friend class PrecompilePreambleConsumer;
151
152
  bool HasEmittedPreamblePCH = false;
153
  std::string *InMemStorage;
154
  PreambleCallbacks &Callbacks;
155
};
156
157
class PrecompilePreambleConsumer : public PCHGenerator {
158
public:
159
  PrecompilePreambleConsumer(PrecompilePreambleAction &Action,
160
                             const Preprocessor &PP,
161
                             InMemoryModuleCache &ModuleCache,
162
                             StringRef isysroot,
163
                             std::unique_ptr<raw_ostream> Out)
164
      : PCHGenerator(PP, ModuleCache, "", isysroot,
165
                     std::make_shared<PCHBuffer>(),
166
                     ArrayRef<std::shared_ptr<ModuleFileExtension>>(),
167
                     /*AllowASTWithErrors=*/true),
168
88
        Action(Action), Out(std::move(Out)) {}
169
170
85
  bool HandleTopLevelDecl(DeclGroupRef DG) override {
171
85
    Action.Callbacks.HandleTopLevelDecl(DG);
172
85
    return true;
173
85
  }
174
175
88
  void HandleTranslationUnit(ASTContext &Ctx) override {
176
88
    PCHGenerator::HandleTranslationUnit(Ctx);
177
88
    if (!hasEmittedPCH())
178
0
      return;
179
88
180
88
    // Write the generated bitstream to "Out".
181
88
    *Out << getPCH();
182
88
    // Make sure it hits disk now.
183
88
    Out->flush();
184
88
    // Free the buffer.
185
88
    llvm::SmallVector<char, 0> Empty;
186
88
    getPCH() = std::move(Empty);
187
88
188
88
    Action.setEmittedPreamblePCH(getWriter());
189
88
  }
190
191
private:
192
  PrecompilePreambleAction &Action;
193
  std::unique_ptr<raw_ostream> Out;
194
};
195
196
std::unique_ptr<ASTConsumer>
197
PrecompilePreambleAction::CreateASTConsumer(CompilerInstance &CI,
198
88
                                            StringRef InFile) {
199
88
  std::string Sysroot;
200
88
  if (!GeneratePCHAction::ComputeASTConsumerArguments(CI, Sysroot))
201
0
    return nullptr;
202
88
203
88
  std::unique_ptr<llvm::raw_ostream> OS;
204
88
  if (InMemStorage) {
205
0
    OS = llvm::make_unique<llvm::raw_string_ostream>(*InMemStorage);
206
88
  } else {
207
88
    std::string OutputFile;
208
88
    OS = GeneratePCHAction::CreateOutputFile(CI, InFile, OutputFile);
209
88
  }
210
88
  if (!OS)
211
0
    return nullptr;
212
88
213
88
  if (!CI.getFrontendOpts().RelocatablePCH)
214
88
    Sysroot.clear();
215
88
216
88
  return llvm::make_unique<PrecompilePreambleConsumer>(
217
88
      *this, CI.getPreprocessor(), CI.getModuleCache(), Sysroot, std::move(OS));
218
88
}
219
220
464
template <class T> bool moveOnNoError(llvm::ErrorOr<T> Val, T &Output) {
221
464
  if (!Val)
222
2
    return false;
223
462
  Output = std::move(*Val);
224
462
  return true;
225
462
}
226
227
} // namespace
228
229
PreambleBounds clang::ComputePreambleBounds(const LangOptions &LangOpts,
230
                                            llvm::MemoryBuffer *Buffer,
231
499
                                            unsigned MaxLines) {
232
499
  return Lexer::ComputePreamble(Buffer->getBuffer(), LangOpts, MaxLines);
233
499
}
234
235
llvm::ErrorOr<PrecompiledPreamble> PrecompiledPreamble::Build(
236
    const CompilerInvocation &Invocation,
237
    const llvm::MemoryBuffer *MainFileBuffer, PreambleBounds Bounds,
238
    DiagnosticsEngine &Diagnostics,
239
    IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
240
    std::shared_ptr<PCHContainerOperations> PCHContainerOps, bool StoreInMemory,
241
88
    PreambleCallbacks &Callbacks) {
242
88
  assert(VFS && "VFS is null");
243
88
244
88
  auto PreambleInvocation = std::make_shared<CompilerInvocation>(Invocation);
245
88
  FrontendOptions &FrontendOpts = PreambleInvocation->getFrontendOpts();
246
88
  PreprocessorOptions &PreprocessorOpts =
247
88
      PreambleInvocation->getPreprocessorOpts();
248
88
249
88
  llvm::Optional<TempPCHFile> TempFile;
250
88
  if (!StoreInMemory) {
251
88
    // Create a temporary file for the precompiled preamble. In rare
252
88
    // circumstances, this can fail.
253
88
    llvm::ErrorOr<PrecompiledPreamble::TempPCHFile> PreamblePCHFile =
254
88
        PrecompiledPreamble::TempPCHFile::CreateNewPreamblePCHFile();
255
88
    if (!PreamblePCHFile)
256
0
      return BuildPreambleError::CouldntCreateTempFile;
257
88
    TempFile = std::move(*PreamblePCHFile);
258
88
  }
259
88
260
88
  PCHStorage Storage = StoreInMemory ? 
PCHStorage(InMemoryPreamble())0
261
88
                                     : PCHStorage(std::move(*TempFile));
262
88
263
88
  // Save the preamble text for later; we'll need to compare against it for
264
88
  // subsequent reparses.
265
88
  std::vector<char> PreambleBytes(MainFileBuffer->getBufferStart(),
266
88
                                  MainFileBuffer->getBufferStart() +
267
88
                                      Bounds.Size);
268
88
  bool PreambleEndsAtStartOfLine = Bounds.PreambleEndsAtStartOfLine;
269
88
270
88
  // Tell the compiler invocation to generate a temporary precompiled header.
271
88
  FrontendOpts.ProgramAction = frontend::GeneratePCH;
272
88
  FrontendOpts.OutputFile = StoreInMemory ? 
getInMemoryPreamblePath()0
273
88
                                          : Storage.asFile().getFilePath();
274
88
  PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
275
88
  PreprocessorOpts.PrecompiledPreambleBytes.second = false;
276
88
  // Inform preprocessor to record conditional stack when building the preamble.
277
88
  PreprocessorOpts.GeneratePreamble = true;
278
88
279
88
  // Create the compiler instance to use for building the precompiled preamble.
280
88
  std::unique_ptr<CompilerInstance> Clang(
281
88
      new CompilerInstance(std::move(PCHContainerOps)));
282
88
283
88
  // Recover resources if we crash before exiting this method.
284
88
  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> CICleanup(
285
88
      Clang.get());
286
88
287
88
  Clang->setInvocation(std::move(PreambleInvocation));
288
88
  Clang->setDiagnostics(&Diagnostics);
289
88
290
88
  // Create the target instance.
291
88
  Clang->setTarget(TargetInfo::CreateTargetInfo(
292
88
      Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
293
88
  if (!Clang->hasTarget())
294
0
    return BuildPreambleError::CouldntCreateTargetInfo;
295
88
296
88
  // Inform the target of the language options.
297
88
  //
298
88
  // FIXME: We shouldn't need to do this, the target should be immutable once
299
88
  // created. This complexity should be lifted elsewhere.
300
88
  Clang->getTarget().adjust(Clang->getLangOpts());
301
88
302
88
  if (Clang->getFrontendOpts().Inputs.size() != 1 ||
303
88
      Clang->getFrontendOpts().Inputs[0].getKind().getFormat() !=
304
88
          InputKind::Source ||
305
88
      Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() ==
306
88
          InputKind::LLVM_IR) {
307
0
    return BuildPreambleError::BadInputs;
308
0
  }
309
88
310
88
  // Clear out old caches and data.
311
88
  Diagnostics.Reset();
312
88
  ProcessWarningOptions(Diagnostics, Clang->getDiagnosticOpts());
313
88
314
88
  VFS =
315
88
      createVFSFromCompilerInvocation(Clang->getInvocation(), Diagnostics, VFS);
316
88
317
88
  // Create a file manager object to provide access to and cache the filesystem.
318
88
  Clang->setFileManager(new FileManager(Clang->getFileSystemOpts(), VFS));
319
88
320
88
  // Create the source manager.
321
88
  Clang->setSourceManager(
322
88
      new SourceManager(Diagnostics, Clang->getFileManager()));
323
88
324
88
  auto PreambleDepCollector = std::make_shared<PreambleDependencyCollector>();
325
88
  Clang->addDependencyCollector(PreambleDepCollector);
326
88
327
88
  // Remap the main source file to the preamble buffer.
328
88
  StringRef MainFilePath = FrontendOpts.Inputs[0].getFile();
329
88
  auto PreambleInputBuffer = llvm::MemoryBuffer::getMemBufferCopy(
330
88
      MainFileBuffer->getBuffer().slice(0, Bounds.Size), MainFilePath);
331
88
  if (PreprocessorOpts.RetainRemappedFileBuffers) {
332
88
    // MainFileBuffer will be deleted by unique_ptr after leaving the method.
333
88
    PreprocessorOpts.addRemappedFile(MainFilePath, PreambleInputBuffer.get());
334
88
  } else {
335
0
    // In that case, remapped buffer will be deleted by CompilerInstance on
336
0
    // BeginSourceFile, so we call release() to avoid double deletion.
337
0
    PreprocessorOpts.addRemappedFile(MainFilePath,
338
0
                                     PreambleInputBuffer.release());
339
0
  }
340
88
341
88
  std::unique_ptr<PrecompilePreambleAction> Act;
342
88
  Act.reset(new PrecompilePreambleAction(
343
88
      StoreInMemory ? 
&Storage.asMemory().Data0
: nullptr, Callbacks));
344
88
  Callbacks.BeforeExecute(*Clang);
345
88
  if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0]))
346
0
    return BuildPreambleError::BeginSourceFileFailed;
347
88
348
88
  std::unique_ptr<PPCallbacks> DelegatedPPCallbacks =
349
88
      Callbacks.createPPCallbacks();
350
88
  if (DelegatedPPCallbacks)
351
88
    Clang->getPreprocessor().addPPCallbacks(std::move(DelegatedPPCallbacks));
352
88
  if (auto CommentHandler = Callbacks.getCommentHandler())
353
0
    Clang->getPreprocessor().addCommentHandler(CommentHandler);
354
88
355
88
  if (llvm::Error Err = Act->Execute())
356
0
    return errorToErrorCode(std::move(Err));
357
88
358
88
  // Run the callbacks.
359
88
  Callbacks.AfterExecute(*Clang);
360
88
361
88
  Act->EndSourceFile();
362
88
363
88
  if (!Act->hasEmittedPreamblePCH())
364
0
    return BuildPreambleError::CouldntEmitPCH;
365
88
366
88
  // Keep track of all of the files that the source manager knows about,
367
88
  // so we can verify whether they have changed or not.
368
88
  llvm::StringMap<PrecompiledPreamble::PreambleFileHash> FilesInPreamble;
369
88
370
88
  SourceManager &SourceMgr = Clang->getSourceManager();
371
244
  for (auto &Filename : PreambleDepCollector->getDependencies()) {
372
244
    const FileEntry *File = Clang->getFileManager().getFile(Filename);
373
244
    if (!File || 
File == SourceMgr.getFileEntryForID(SourceMgr.getMainFileID())243
)
374
89
      continue;
375
155
    if (time_t ModTime = File->getModificationTime()) {
376
147
      FilesInPreamble[File->getName()] =
377
147
          PrecompiledPreamble::PreambleFileHash::createForFile(File->getSize(),
378
147
                                                               ModTime);
379
147
    } else {
380
8
      const llvm::MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File);
381
8
      FilesInPreamble[File->getName()] =
382
8
          PrecompiledPreamble::PreambleFileHash::createForMemoryBuffer(Buffer);
383
8
    }
384
155
  }
385
88
386
88
  return PrecompiledPreamble(std::move(Storage), std::move(PreambleBytes),
387
88
                             PreambleEndsAtStartOfLine,
388
88
                             std::move(FilesInPreamble));
389
88
}
390
391
539
PreambleBounds PrecompiledPreamble::getBounds() const {
392
539
  return PreambleBounds(PreambleBytes.size(), PreambleEndsAtStartOfLine);
393
539
}
394
395
0
std::size_t PrecompiledPreamble::getSize() const {
396
0
  switch (Storage.getKind()) {
397
0
  case PCHStorage::Kind::Empty:
398
0
    assert(false && "Calling getSize() on invalid PrecompiledPreamble. "
399
0
                    "Was it std::moved?");
400
0
    return 0;
401
0
  case PCHStorage::Kind::InMemory:
402
0
    return Storage.asMemory().Data.size();
403
0
  case PCHStorage::Kind::TempFile: {
404
0
    uint64_t Result;
405
0
    if (llvm::sys::fs::file_size(Storage.asFile().getFilePath(), Result))
406
0
      return 0;
407
0
408
0
    assert(Result <= std::numeric_limits<std::size_t>::max() &&
409
0
           "file size did not fit into size_t");
410
0
    return Result;
411
0
  }
412
0
  }
413
0
  llvm_unreachable("Unhandled storage kind");
414
0
}
415
416
bool PrecompiledPreamble::CanReuse(const CompilerInvocation &Invocation,
417
                                   const llvm::MemoryBuffer *MainFileBuffer,
418
                                   PreambleBounds Bounds,
419
251
                                   llvm::vfs::FileSystem *VFS) const {
420
251
421
251
  assert(
422
251
      Bounds.Size <= MainFileBuffer->getBufferSize() &&
423
251
      "Buffer is too large. Bounds were calculated from a different buffer?");
424
251
425
251
  auto PreambleInvocation = std::make_shared<CompilerInvocation>(Invocation);
426
251
  PreprocessorOptions &PreprocessorOpts =
427
251
      PreambleInvocation->getPreprocessorOpts();
428
251
429
251
  // We've previously computed a preamble. Check whether we have the same
430
251
  // preamble now that we did before, and that there's enough space in
431
251
  // the main-file buffer within the precompiled preamble to fit the
432
251
  // new main file.
433
251
  if (PreambleBytes.size() != Bounds.Size ||
434
251
      
PreambleEndsAtStartOfLine != Bounds.PreambleEndsAtStartOfLine245
||
435
251
      !std::equal(PreambleBytes.begin(), PreambleBytes.end(),
436
245
                  MainFileBuffer->getBuffer().begin()))
437
6
    return false;
438
245
  // The preamble has not changed. We may be able to re-use the precompiled
439
245
  // preamble.
440
245
441
245
  // Check that none of the files used by the preamble have changed.
442
245
  // First, make a record of those files that have been overridden via
443
245
  // remapping or unsaved_files.
444
245
  std::map<llvm::sys::fs::UniqueID, PreambleFileHash> OverriddenFiles;
445
245
  for (const auto &R : PreprocessorOpts.RemappedFiles) {
446
0
    llvm::vfs::Status Status;
447
0
    if (!moveOnNoError(VFS->status(R.second), Status)) {
448
0
      // If we can't stat the file we're remapping to, assume that something
449
0
      // horrible happened.
450
0
      return false;
451
0
    }
452
0
453
0
    OverriddenFiles[Status.getUniqueID()] = PreambleFileHash::createForFile(
454
0
        Status.getSize(), llvm::sys::toTimeT(Status.getLastModificationTime()));
455
0
  }
456
245
457
245
  // OverridenFileBuffers tracks only the files not found in VFS.
458
245
  llvm::StringMap<PreambleFileHash> OverridenFileBuffers;
459
245
  for (const auto &RB : PreprocessorOpts.RemappedFileBuffers) {
460
6
    const PrecompiledPreamble::PreambleFileHash PreambleHash =
461
6
        PreambleFileHash::createForMemoryBuffer(RB.second);
462
6
    llvm::vfs::Status Status;
463
6
    if (moveOnNoError(VFS->status(RB.first), Status))
464
4
      OverriddenFiles[Status.getUniqueID()] = PreambleHash;
465
2
    else
466
2
      OverridenFileBuffers[RB.first] = PreambleHash;
467
6
  }
468
245
469
245
  // Check whether anything has changed.
470
460
  for (const auto &F : FilesInPreamble) {
471
460
    auto OverridenFileBuffer = OverridenFileBuffers.find(F.first());
472
460
    if (OverridenFileBuffer != OverridenFileBuffers.end()) {
473
2
      // The file's buffer was remapped and the file was not found in VFS.
474
2
      // Check whether it matches up with the previous mapping.
475
2
      if (OverridenFileBuffer->second != F.second)
476
0
        return false;
477
2
      continue;
478
2
    }
479
458
480
458
    llvm::vfs::Status Status;
481
458
    if (!moveOnNoError(VFS->status(F.first()), Status)) {
482
0
      // If the file's buffer is not remapped and we can't stat it,
483
0
      // assume that something horrible happened.
484
0
      return false;
485
0
    }
486
458
487
458
    std::map<llvm::sys::fs::UniqueID, PreambleFileHash>::iterator Overridden =
488
458
        OverriddenFiles.find(Status.getUniqueID());
489
458
    if (Overridden != OverriddenFiles.end()) {
490
4
      // This file was remapped; check whether the newly-mapped file
491
4
      // matches up with the previous mapping.
492
4
      if (Overridden->second != F.second)
493
2
        return false;
494
2
      continue;
495
2
    }
496
454
497
454
    // Neither the file's buffer nor the file itself was remapped;
498
454
    // check whether it has changed on disk.
499
454
    if (Status.getSize() != uint64_t(F.second.Size) ||
500
454
        llvm::sys::toTimeT(Status.getLastModificationTime()) !=
501
452
            F.second.ModTime)
502
2
      return false;
503
454
  }
504
245
  
return true241
;
505
245
}
506
507
void PrecompiledPreamble::AddImplicitPreamble(
508
    CompilerInvocation &CI, IntrusiveRefCntPtr<llvm::vfs::FileSystem> &VFS,
509
329
    llvm::MemoryBuffer *MainFileBuffer) const {
510
329
  PreambleBounds Bounds(PreambleBytes.size(), PreambleEndsAtStartOfLine);
511
329
  configurePreamble(Bounds, CI, VFS, MainFileBuffer);
512
329
}
513
514
void PrecompiledPreamble::OverridePreamble(
515
    CompilerInvocation &CI, IntrusiveRefCntPtr<llvm::vfs::FileSystem> &VFS,
516
0
    llvm::MemoryBuffer *MainFileBuffer) const {
517
0
  auto Bounds = ComputePreambleBounds(*CI.getLangOpts(), MainFileBuffer, 0);
518
0
  configurePreamble(Bounds, CI, VFS, MainFileBuffer);
519
0
}
520
521
PrecompiledPreamble::PrecompiledPreamble(
522
    PCHStorage Storage, std::vector<char> PreambleBytes,
523
    bool PreambleEndsAtStartOfLine,
524
    llvm::StringMap<PreambleFileHash> FilesInPreamble)
525
    : Storage(std::move(Storage)), FilesInPreamble(std::move(FilesInPreamble)),
526
      PreambleBytes(std::move(PreambleBytes)),
527
88
      PreambleEndsAtStartOfLine(PreambleEndsAtStartOfLine) {
528
88
  assert(this->Storage.getKind() != PCHStorage::Kind::Empty);
529
88
}
530
531
llvm::ErrorOr<PrecompiledPreamble::TempPCHFile>
532
88
PrecompiledPreamble::TempPCHFile::CreateNewPreamblePCHFile() {
533
88
  // FIXME: This is a hack so that we can override the preamble file during
534
88
  // crash-recovery testing, which is the only case where the preamble files
535
88
  // are not necessarily cleaned up.
536
88
  const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE");
537
88
  if (TmpFile)
538
2
    return TempPCHFile::createFromCustomPath(TmpFile);
539
86
  return TempPCHFile::createInSystemTempDir("preamble", "pch");
540
86
}
541
542
llvm::ErrorOr<PrecompiledPreamble::TempPCHFile>
543
PrecompiledPreamble::TempPCHFile::createInSystemTempDir(const Twine &Prefix,
544
86
                                                        StringRef Suffix) {
545
86
  llvm::SmallString<64> File;
546
86
  // Using a version of createTemporaryFile with a file descriptor guarantees
547
86
  // that we would never get a race condition in a multi-threaded setting
548
86
  // (i.e., multiple threads getting the same temporary path).
549
86
  int FD;
550
86
  auto EC = llvm::sys::fs::createTemporaryFile(Prefix, Suffix, FD, File);
551
86
  if (EC)
552
0
    return EC;
553
86
  // We only needed to make sure the file exists, close the file right away.
554
86
  llvm::sys::Process::SafelyCloseFileDescriptor(FD);
555
86
  return TempPCHFile(std::move(File).str());
556
86
}
557
558
llvm::ErrorOr<PrecompiledPreamble::TempPCHFile>
559
2
PrecompiledPreamble::TempPCHFile::createFromCustomPath(const Twine &Path) {
560
2
  return TempPCHFile(Path.str());
561
2
}
562
563
PrecompiledPreamble::TempPCHFile::TempPCHFile(std::string FilePath)
564
88
    : FilePath(std::move(FilePath)) {
565
88
  TemporaryFiles::getInstance().addFile(*this->FilePath);
566
88
}
567
568
704
PrecompiledPreamble::TempPCHFile::TempPCHFile(TempPCHFile &&Other) {
569
704
  FilePath = std::move(Other.FilePath);
570
704
  Other.FilePath = None;
571
704
}
572
573
PrecompiledPreamble::TempPCHFile &PrecompiledPreamble::TempPCHFile::
574
0
operator=(TempPCHFile &&Other) {
575
0
  RemoveFileIfPresent();
576
0
577
0
  FilePath = std::move(Other.FilePath);
578
0
  Other.FilePath = None;
579
0
  return *this;
580
0
}
581
582
791
PrecompiledPreamble::TempPCHFile::~TempPCHFile() { RemoveFileIfPresent(); }
583
584
791
void PrecompiledPreamble::TempPCHFile::RemoveFileIfPresent() {
585
791
  if (FilePath) {
586
87
    TemporaryFiles::getInstance().removeFile(*FilePath);
587
87
    FilePath = None;
588
87
  }
589
791
}
590
591
746
llvm::StringRef PrecompiledPreamble::TempPCHFile::getFilePath() const {
592
746
  assert(FilePath && "TempPCHFile doesn't have a FilePath. Had it been moved?");
593
746
  return *FilePath;
594
746
}
595
596
PrecompiledPreamble::PCHStorage::PCHStorage(TempPCHFile File)
597
88
    : StorageKind(Kind::TempFile) {
598
88
  new (&asFile()) TempPCHFile(std::move(File));
599
88
}
600
601
PrecompiledPreamble::PCHStorage::PCHStorage(InMemoryPreamble Memory)
602
0
    : StorageKind(Kind::InMemory) {
603
0
  new (&asMemory()) InMemoryPreamble(std::move(Memory));
604
0
}
605
606
352
PrecompiledPreamble::PCHStorage::PCHStorage(PCHStorage &&Other) : PCHStorage() {
607
352
  *this = std::move(Other);
608
352
}
Unexecuted instantiation: clang::PrecompiledPreamble::PCHStorage::PCHStorage(clang::PrecompiledPreamble::PCHStorage&&)
clang::PrecompiledPreamble::PCHStorage::PCHStorage(clang::PrecompiledPreamble::PCHStorage&&)
Line
Count
Source
606
352
PrecompiledPreamble::PCHStorage::PCHStorage(PCHStorage &&Other) : PCHStorage() {
607
352
  *this = std::move(Other);
608
352
}
609
610
PrecompiledPreamble::PCHStorage &PrecompiledPreamble::PCHStorage::
611
352
operator=(PCHStorage &&Other) {
612
352
  destroy();
613
352
614
352
  StorageKind = Other.StorageKind;
615
352
  switch (StorageKind) {
616
352
  case Kind::Empty:
617
0
    // do nothing;
618
0
    break;
619
352
  case Kind::TempFile:
620
352
    new (&asFile()) TempPCHFile(std::move(Other.asFile()));
621
352
    break;
622
352
  case Kind::InMemory:
623
0
    new (&asMemory()) InMemoryPreamble(std::move(Other.asMemory()));
624
0
    break;
625
352
  }
626
352
627
352
  Other.setEmpty();
628
352
  return *this;
629
352
}
630
631
439
PrecompiledPreamble::PCHStorage::~PCHStorage() { destroy(); }
632
633
PrecompiledPreamble::PCHStorage::Kind
634
329
PrecompiledPreamble::PCHStorage::getKind() const {
635
329
  return StorageKind;
636
329
}
637
638
1.64k
PrecompiledPreamble::TempPCHFile &PrecompiledPreamble::PCHStorage::asFile() {
639
1.64k
  assert(getKind() == Kind::TempFile);
640
1.64k
  return *reinterpret_cast<TempPCHFile *>(Storage.buffer);
641
1.64k
}
642
643
const PrecompiledPreamble::TempPCHFile &
644
329
PrecompiledPreamble::PCHStorage::asFile() const {
645
329
  return const_cast<PCHStorage *>(this)->asFile();
646
329
}
647
648
PrecompiledPreamble::InMemoryPreamble &
649
0
PrecompiledPreamble::PCHStorage::asMemory() {
650
0
  assert(getKind() == Kind::InMemory);
651
0
  return *reinterpret_cast<InMemoryPreamble *>(Storage.buffer);
652
0
}
653
654
const PrecompiledPreamble::InMemoryPreamble &
655
0
PrecompiledPreamble::PCHStorage::asMemory() const {
656
0
  return const_cast<PCHStorage *>(this)->asMemory();
657
0
}
658
659
1.14k
void PrecompiledPreamble::PCHStorage::destroy() {
660
1.14k
  switch (StorageKind) {
661
1.14k
  case Kind::Empty:
662
704
    return;
663
1.14k
  case Kind::TempFile:
664
439
    asFile().~TempPCHFile();
665
439
    return;
666
1.14k
  case Kind::InMemory:
667
0
    asMemory().~InMemoryPreamble();
668
0
    return;
669
1.14k
  }
670
1.14k
}
671
672
352
void PrecompiledPreamble::PCHStorage::setEmpty() {
673
352
  destroy();
674
352
  StorageKind = Kind::Empty;
675
352
}
676
677
PrecompiledPreamble::PreambleFileHash
678
PrecompiledPreamble::PreambleFileHash::createForFile(off_t Size,
679
147
                                                     time_t ModTime) {
680
147
  PreambleFileHash Result;
681
147
  Result.Size = Size;
682
147
  Result.ModTime = ModTime;
683
147
  Result.MD5 = {};
684
147
  return Result;
685
147
}
686
687
PrecompiledPreamble::PreambleFileHash
688
PrecompiledPreamble::PreambleFileHash::createForMemoryBuffer(
689
14
    const llvm::MemoryBuffer *Buffer) {
690
14
  PreambleFileHash Result;
691
14
  Result.Size = Buffer->getBufferSize();
692
14
  Result.ModTime = 0;
693
14
694
14
  llvm::MD5 MD5Ctx;
695
14
  MD5Ctx.update(Buffer->getBuffer().data());
696
14
  MD5Ctx.final(Result.MD5);
697
14
698
14
  return Result;
699
14
}
700
701
void PrecompiledPreamble::configurePreamble(
702
    PreambleBounds Bounds, CompilerInvocation &CI,
703
    IntrusiveRefCntPtr<llvm::vfs::FileSystem> &VFS,
704
329
    llvm::MemoryBuffer *MainFileBuffer) const {
705
329
  assert(VFS);
706
329
707
329
  auto &PreprocessorOpts = CI.getPreprocessorOpts();
708
329
709
329
  // Remap main file to point to MainFileBuffer.
710
329
  auto MainFilePath = CI.getFrontendOpts().Inputs[0].getFile();
711
329
  PreprocessorOpts.addRemappedFile(MainFilePath, MainFileBuffer);
712
329
713
329
  // Configure ImpicitPCHInclude.
714
329
  PreprocessorOpts.PrecompiledPreambleBytes.first = Bounds.Size;
715
329
  PreprocessorOpts.PrecompiledPreambleBytes.second =
716
329
      Bounds.PreambleEndsAtStartOfLine;
717
329
  PreprocessorOpts.DisablePCHValidation = true;
718
329
719
329
  setupPreambleStorage(Storage, PreprocessorOpts, VFS);
720
329
}
721
722
void PrecompiledPreamble::setupPreambleStorage(
723
    const PCHStorage &Storage, PreprocessorOptions &PreprocessorOpts,
724
329
    IntrusiveRefCntPtr<llvm::vfs::FileSystem> &VFS) {
725
329
  if (Storage.getKind() == PCHStorage::Kind::TempFile) {
726
329
    const TempPCHFile &PCHFile = Storage.asFile();
727
329
    PreprocessorOpts.ImplicitPCHInclude = PCHFile.getFilePath();
728
329
729
329
    // Make sure we can access the PCH file even if we're using a VFS
730
329
    IntrusiveRefCntPtr<llvm::vfs::FileSystem> RealFS =
731
329
        llvm::vfs::getRealFileSystem();
732
329
    auto PCHPath = PCHFile.getFilePath();
733
329
    if (VFS == RealFS || 
VFS->exists(PCHPath)12
)
734
317
      return;
735
12
    auto Buf = RealFS->getBufferForFile(PCHPath);
736
12
    if (!Buf) {
737
0
      // We can't read the file even from RealFS, this is clearly an error,
738
0
      // but we'll just leave the current VFS as is and let clang's code
739
0
      // figure out what to do with missing PCH.
740
0
      return;
741
0
    }
742
12
743
12
    // We have a slight inconsistency here -- we're using the VFS to
744
12
    // read files, but the PCH was generated in the real file system.
745
12
    VFS = createVFSOverlayForPreamblePCH(PCHPath, std::move(*Buf), VFS);
746
12
  } else {
747
0
    assert(Storage.getKind() == PCHStorage::Kind::InMemory);
748
0
    // For in-memory preamble, we have to provide a VFS overlay that makes it
749
0
    // accessible.
750
0
    StringRef PCHPath = getInMemoryPreamblePath();
751
0
    PreprocessorOpts.ImplicitPCHInclude = PCHPath;
752
0
753
0
    auto Buf = llvm::MemoryBuffer::getMemBuffer(Storage.asMemory().Data);
754
0
    VFS = createVFSOverlayForPreamblePCH(PCHPath, std::move(Buf), VFS);
755
0
  }
756
329
}
757
758
88
void PreambleCallbacks::BeforeExecute(CompilerInstance &CI) {}
759
88
void PreambleCallbacks::AfterExecute(CompilerInstance &CI) {}
760
0
void PreambleCallbacks::AfterPCHEmitted(ASTWriter &Writer) {}
761
0
void PreambleCallbacks::HandleTopLevelDecl(DeclGroupRef DG) {}
762
0
std::unique_ptr<PPCallbacks> PreambleCallbacks::createPPCallbacks() {
763
0
  return nullptr;
764
0
}
765
88
CommentHandler *PreambleCallbacks::getCommentHandler() { return nullptr; }
766
767
static llvm::ManagedStatic<BuildPreambleErrorCategory> BuildPreambleErrCategory;
768
769
0
std::error_code clang::make_error_code(BuildPreambleError Error) {
770
0
  return std::error_code(static_cast<int>(Error), *BuildPreambleErrCategory);
771
0
}
772
773
0
const char *BuildPreambleErrorCategory::name() const noexcept {
774
0
  return "build-preamble.error";
775
0
}
776
777
0
std::string BuildPreambleErrorCategory::message(int condition) const {
778
0
  switch (static_cast<BuildPreambleError>(condition)) {
779
0
  case BuildPreambleError::CouldntCreateTempFile:
780
0
    return "Could not create temporary file for PCH";
781
0
  case BuildPreambleError::CouldntCreateTargetInfo:
782
0
    return "CreateTargetInfo() return null";
783
0
  case BuildPreambleError::BeginSourceFileFailed:
784
0
    return "BeginSourceFile() return an error";
785
0
  case BuildPreambleError::CouldntEmitPCH:
786
0
    return "Could not emit PCH";
787
0
  case BuildPreambleError::BadInputs:
788
0
    return "Command line arguments must contain exactly one source file";
789
0
  }
790
0
  llvm_unreachable("unexpected BuildPreambleError");
791
0
}