Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Frontend/CompilerInstance.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- CompilerInstance.cpp ---------------------------------------------===//
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
#include "clang/Frontend/CompilerInstance.h"
10
#include "clang/AST/ASTConsumer.h"
11
#include "clang/AST/ASTContext.h"
12
#include "clang/AST/Decl.h"
13
#include "clang/Basic/CharInfo.h"
14
#include "clang/Basic/Diagnostic.h"
15
#include "clang/Basic/FileManager.h"
16
#include "clang/Basic/LangStandard.h"
17
#include "clang/Basic/SourceManager.h"
18
#include "clang/Basic/Stack.h"
19
#include "clang/Basic/TargetInfo.h"
20
#include "clang/Basic/Version.h"
21
#include "clang/Config/config.h"
22
#include "clang/Frontend/ChainedDiagnosticConsumer.h"
23
#include "clang/Frontend/FrontendAction.h"
24
#include "clang/Frontend/FrontendActions.h"
25
#include "clang/Frontend/FrontendDiagnostic.h"
26
#include "clang/Frontend/FrontendPluginRegistry.h"
27
#include "clang/Frontend/LogDiagnosticPrinter.h"
28
#include "clang/Frontend/SerializedDiagnosticPrinter.h"
29
#include "clang/Frontend/TextDiagnosticPrinter.h"
30
#include "clang/Frontend/Utils.h"
31
#include "clang/Frontend/VerifyDiagnosticConsumer.h"
32
#include "clang/Lex/HeaderSearch.h"
33
#include "clang/Lex/Preprocessor.h"
34
#include "clang/Lex/PreprocessorOptions.h"
35
#include "clang/Sema/CodeCompleteConsumer.h"
36
#include "clang/Sema/Sema.h"
37
#include "clang/Serialization/ASTReader.h"
38
#include "clang/Serialization/GlobalModuleIndex.h"
39
#include "clang/Serialization/InMemoryModuleCache.h"
40
#include "llvm/ADT/ScopeExit.h"
41
#include "llvm/ADT/Statistic.h"
42
#include "llvm/Support/BuryPointer.h"
43
#include "llvm/Support/CrashRecoveryContext.h"
44
#include "llvm/Support/Errc.h"
45
#include "llvm/Support/FileSystem.h"
46
#include "llvm/Support/Host.h"
47
#include "llvm/Support/LockFileManager.h"
48
#include "llvm/Support/MemoryBuffer.h"
49
#include "llvm/Support/Path.h"
50
#include "llvm/Support/Program.h"
51
#include "llvm/Support/Signals.h"
52
#include "llvm/Support/TimeProfiler.h"
53
#include "llvm/Support/Timer.h"
54
#include "llvm/Support/raw_ostream.h"
55
#include <time.h>
56
#include <utility>
57
58
using namespace clang;
59
60
CompilerInstance::CompilerInstance(
61
    std::shared_ptr<PCHContainerOperations> PCHContainerOps,
62
    InMemoryModuleCache *SharedModuleCache)
63
    : ModuleLoader(/* BuildingModule = */ SharedModuleCache),
64
      Invocation(new CompilerInvocation()),
65
      ModuleCache(SharedModuleCache ? SharedModuleCache
66
                                    : new InMemoryModuleCache),
67
91.2k
      ThePCHContainerOperations(std::move(PCHContainerOps)) {}
68
69
85.0k
CompilerInstance::~CompilerInstance() {
70
85.0k
  assert(OutputFiles.empty() && "Still output files in flight?");
71
85.0k
}
72
73
void CompilerInstance::setInvocation(
74
33.8k
    std::shared_ptr<CompilerInvocation> Value) {
75
33.8k
  Invocation = std::move(Value);
76
33.8k
}
77
78
73.4k
bool CompilerInstance::shouldBuildGlobalModuleIndex() const {
79
73.4k
  return (BuildGlobalModuleIndex ||
80
73.4k
          
(72.7k
TheASTReader72.7k
&&
TheASTReader->isGlobalIndexUnavailable()11.0k
&&
81
72.7k
           
getFrontendOpts().GenerateGlobalModuleIndex654
)) &&
82
73.4k
         
!DisableGeneratingGlobalModuleIndex934
;
83
73.4k
}
84
85
13.8k
void CompilerInstance::setDiagnostics(DiagnosticsEngine *Value) {
86
13.8k
  Diagnostics = Value;
87
13.8k
}
88
89
2
void CompilerInstance::setVerboseOutputStream(raw_ostream &Value) {
90
2
  OwnedVerboseOutputStream.reset();
91
2
  VerboseOutputStream = &Value;
92
2
}
93
94
2
void CompilerInstance::setVerboseOutputStream(std::unique_ptr<raw_ostream> Value) {
95
2
  OwnedVerboseOutputStream.swap(Value);
96
2
  VerboseOutputStream = OwnedVerboseOutputStream.get();
97
2
}
98
99
91.1k
void CompilerInstance::setTarget(TargetInfo *Value) { Target = Value; }
100
143
void CompilerInstance::setAuxTarget(TargetInfo *Value) { AuxTarget = Value; }
101
102
73.6k
bool CompilerInstance::createTarget() {
103
  // Create the target instance.
104
73.6k
  setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(),
105
73.6k
                                         getInvocation().TargetOpts));
106
73.6k
  if (!hasTarget())
107
102
    return false;
108
109
  // Check whether AuxTarget exists, if not, then create TargetInfo for the
110
  // other side of CUDA/OpenMP/SYCL compilation.
111
73.5k
  if (!getAuxTarget() &&
112
73.5k
      (getLangOpts().CUDA || 
getLangOpts().OpenMPIsDevice73.1k
||
113
73.5k
       
getLangOpts().SYCLIsDevice72.4k
) &&
114
73.5k
      
!getFrontendOpts().AuxTriple.empty()1.15k
) {
115
143
    auto TO = std::make_shared<TargetOptions>();
116
143
    TO->Triple = llvm::Triple::normalize(getFrontendOpts().AuxTriple);
117
143
    if (getFrontendOpts().AuxTargetCPU)
118
24
      TO->CPU = getFrontendOpts().AuxTargetCPU.value();
119
143
    if (getFrontendOpts().AuxTargetFeatures)
120
1
      TO->FeaturesAsWritten = getFrontendOpts().AuxTargetFeatures.value();
121
143
    TO->HostTriple = getTarget().getTriple().str();
122
143
    setAuxTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), TO));
123
143
  }
124
125
73.5k
  if (!getTarget().hasStrictFP() && 
!getLangOpts().ExpStrictFP6.28k
) {
126
6.26k
    if (getLangOpts().RoundingMath) {
127
2
      getDiagnostics().Report(diag::warn_fe_backend_unsupported_fp_rounding);
128
2
      getLangOpts().RoundingMath = false;
129
2
    }
130
6.26k
    auto FPExc = getLangOpts().getFPExceptionMode();
131
6.26k
    if (FPExc != LangOptions::FPE_Default && 
FPExc != LangOptions::FPE_Ignore4
) {
132
2
      getDiagnostics().Report(diag::warn_fe_backend_unsupported_fp_exceptions);
133
2
      getLangOpts().setFPExceptionMode(LangOptions::FPE_Ignore);
134
2
    }
135
    // FIXME: can we disable FEnvAccess?
136
6.26k
  }
137
138
  // We should do it here because target knows nothing about
139
  // language options when it's being created.
140
73.5k
  if (getLangOpts().OpenCL &&
141
73.5k
      
!getTarget().validateOpenCLTarget(getLangOpts(), getDiagnostics())930
)
142
16
    return false;
143
144
  // Inform the target of the language options.
145
  // FIXME: We shouldn't need to do this, the target should be immutable once
146
  // created. This complexity should be lifted elsewhere.
147
73.5k
  getTarget().adjust(getDiagnostics(), getLangOpts());
148
149
  // Adjust target options based on codegen options.
150
73.5k
  getTarget().adjustTargetOptions(getCodeGenOpts(), getTargetOpts());
151
152
73.5k
  if (auto *Aux = getAuxTarget())
153
143
    getTarget().setAuxTarget(Aux);
154
155
73.5k
  return true;
156
73.5k
}
157
158
25
llvm::vfs::FileSystem &CompilerInstance::getVirtualFileSystem() const {
159
25
  return getFileManager().getVirtualFileSystem();
160
25
}
161
162
40.8k
void CompilerInstance::setFileManager(FileManager *Value) {
163
40.8k
  FileMgr = Value;
164
40.8k
}
165
166
21.7k
void CompilerInstance::setSourceManager(SourceManager *Value) {
167
21.7k
  SourceMgr = Value;
168
21.7k
}
169
170
126
void CompilerInstance::setPreprocessor(std::shared_ptr<Preprocessor> Value) {
171
126
  PP = std::move(Value);
172
126
}
173
174
155k
void CompilerInstance::setASTContext(ASTContext *Value) {
175
155k
  Context = Value;
176
177
155k
  if (Context && 
Consumer88.4k
)
178
0
    getASTConsumer().Initialize(getASTContext());
179
155k
}
180
181
98.9k
void CompilerInstance::setSema(Sema *S) {
182
98.9k
  TheSema.reset(S);
183
98.9k
}
184
185
155k
void CompilerInstance::setASTConsumer(std::unique_ptr<ASTConsumer> Value) {
186
155k
  Consumer = std::move(Value);
187
188
155k
  if (Context && 
Consumer88.3k
)
189
88.2k
    getASTConsumer().Initialize(getASTContext());
190
155k
}
191
192
1.21k
void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) {
193
1.21k
  CompletionConsumer.reset(Value);
194
1.21k
}
195
196
15.3k
std::unique_ptr<Sema> CompilerInstance::takeSema() {
197
15.3k
  return std::move(TheSema);
198
15.3k
}
199
200
29.8k
IntrusiveRefCntPtr<ASTReader> CompilerInstance::getASTReader() const {
201
29.8k
  return TheASTReader;
202
29.8k
}
203
51
void CompilerInstance::setASTReader(IntrusiveRefCntPtr<ASTReader> Reader) {
204
51
  assert(ModuleCache.get() == &Reader->getModuleManager().getModuleCache() &&
205
51
         "Expected ASTReader to use the same PCM cache");
206
0
  TheASTReader = std::move(Reader);
207
51
}
208
209
std::shared_ptr<ModuleDependencyCollector>
210
1.79k
CompilerInstance::getModuleDepCollector() const {
211
1.79k
  return ModuleDepCollector;
212
1.79k
}
213
214
void CompilerInstance::setModuleDepCollector(
215
1.80k
    std::shared_ptr<ModuleDependencyCollector> Collector) {
216
1.80k
  ModuleDepCollector = std::move(Collector);
217
1.80k
}
218
219
static void collectHeaderMaps(const HeaderSearch &HS,
220
46
                              std::shared_ptr<ModuleDependencyCollector> MDC) {
221
46
  SmallVector<std::string, 4> HeaderMapFileNames;
222
46
  HS.getHeaderMapFileNames(HeaderMapFileNames);
223
46
  for (auto &Name : HeaderMapFileNames)
224
1
    MDC->addFile(Name);
225
46
}
226
227
static void collectIncludePCH(CompilerInstance &CI,
228
46
                              std::shared_ptr<ModuleDependencyCollector> MDC) {
229
46
  const PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
230
46
  if (PPOpts.ImplicitPCHInclude.empty())
231
45
    return;
232
233
1
  StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
234
1
  FileManager &FileMgr = CI.getFileManager();
235
1
  auto PCHDir = FileMgr.getOptionalDirectoryRef(PCHInclude);
236
1
  if (!PCHDir) {
237
1
    MDC->addFile(PCHInclude);
238
1
    return;
239
1
  }
240
241
0
  std::error_code EC;
242
0
  SmallString<128> DirNative;
243
0
  llvm::sys::path::native(PCHDir->getName(), DirNative);
244
0
  llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
245
0
  SimpleASTReaderListener Validator(CI.getPreprocessor());
246
0
  for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd;
247
0
       Dir != DirEnd && !EC; Dir.increment(EC)) {
248
    // Check whether this is an AST file. ASTReader::isAcceptableASTFile is not
249
    // used here since we're not interested in validating the PCH at this time,
250
    // but only to check whether this is a file containing an AST.
251
0
    if (!ASTReader::readASTFileControlBlock(
252
0
            Dir->path(), FileMgr, CI.getPCHContainerReader(),
253
0
            /*FindModuleFileExtensions=*/false, Validator,
254
0
            /*ValidateDiagnosticOptions=*/false))
255
0
      MDC->addFile(Dir->path());
256
0
  }
257
0
}
258
259
static void collectVFSEntries(CompilerInstance &CI,
260
46
                              std::shared_ptr<ModuleDependencyCollector> MDC) {
261
46
  if (CI.getHeaderSearchOpts().VFSOverlayFiles.empty())
262
43
    return;
263
264
  // Collect all VFS found.
265
3
  SmallVector<llvm::vfs::YAMLVFSEntry, 16> VFSEntries;
266
3
  for (const std::string &VFSFile : CI.getHeaderSearchOpts().VFSOverlayFiles) {
267
3
    llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buffer =
268
3
        llvm::MemoryBuffer::getFile(VFSFile);
269
3
    if (!Buffer)
270
0
      return;
271
3
    llvm::vfs::collectVFSFromYAML(std::move(Buffer.get()),
272
3
                                  /*DiagHandler*/ nullptr, VFSFile, VFSEntries);
273
3
  }
274
275
3
  for (auto &E : VFSEntries)
276
2
    MDC->addFile(E.VPath, E.RPath);
277
3
}
278
279
// Diagnostics
280
static void SetUpDiagnosticLog(DiagnosticOptions *DiagOpts,
281
                               const CodeGenOptions *CodeGenOpts,
282
11
                               DiagnosticsEngine &Diags) {
283
11
  std::error_code EC;
284
11
  std::unique_ptr<raw_ostream> StreamOwner;
285
11
  raw_ostream *OS = &llvm::errs();
286
11
  if (DiagOpts->DiagnosticLogFile != "-") {
287
    // Create the output stream.
288
2
    auto FileOS = std::make_unique<llvm::raw_fd_ostream>(
289
2
        DiagOpts->DiagnosticLogFile, EC,
290
2
        llvm::sys::fs::OF_Append | llvm::sys::fs::OF_TextWithCRLF);
291
2
    if (EC) {
292
0
      Diags.Report(diag::warn_fe_cc_log_diagnostics_failure)
293
0
          << DiagOpts->DiagnosticLogFile << EC.message();
294
2
    } else {
295
2
      FileOS->SetUnbuffered();
296
2
      OS = FileOS.get();
297
2
      StreamOwner = std::move(FileOS);
298
2
    }
299
2
  }
300
301
  // Chain in the diagnostic client which will log the diagnostics.
302
11
  auto Logger = std::make_unique<LogDiagnosticPrinter>(*OS, DiagOpts,
303
11
                                                        std::move(StreamOwner));
304
11
  if (CodeGenOpts)
305
10
    Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags);
306
11
  if (Diags.ownsClient()) {
307
10
    Diags.setClient(
308
10
        new ChainedDiagnosticConsumer(Diags.takeClient(), std::move(Logger)));
309
10
  } else {
310
1
    Diags.setClient(
311
1
        new ChainedDiagnosticConsumer(Diags.getClient(), std::move(Logger)));
312
1
  }
313
11
}
314
315
static void SetupSerializedDiagnostics(DiagnosticOptions *DiagOpts,
316
                                       DiagnosticsEngine &Diags,
317
25
                                       StringRef OutputFile) {
318
25
  auto SerializedConsumer =
319
25
      clang::serialized_diags::create(OutputFile, DiagOpts);
320
321
25
  if (Diags.ownsClient()) {
322
25
    Diags.setClient(new ChainedDiagnosticConsumer(
323
25
        Diags.takeClient(), std::move(SerializedConsumer)));
324
25
  } else {
325
0
    Diags.setClient(new ChainedDiagnosticConsumer(
326
0
        Diags.getClient(), std::move(SerializedConsumer)));
327
0
  }
328
25
}
329
330
void CompilerInstance::createDiagnostics(DiagnosticConsumer *Client,
331
77.4k
                                         bool ShouldOwnClient) {
332
77.4k
  Diagnostics = createDiagnostics(&getDiagnosticOpts(), Client,
333
77.4k
                                  ShouldOwnClient, &getCodeGenOpts());
334
77.4k
}
335
336
IntrusiveRefCntPtr<DiagnosticsEngine>
337
CompilerInstance::createDiagnostics(DiagnosticOptions *Opts,
338
                                    DiagnosticConsumer *Client,
339
                                    bool ShouldOwnClient,
340
112k
                                    const CodeGenOptions *CodeGenOpts) {
341
112k
  IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
342
112k
  IntrusiveRefCntPtr<DiagnosticsEngine>
343
112k
      Diags(new DiagnosticsEngine(DiagID, Opts));
344
345
  // Create the diagnostic client for reporting errors or for
346
  // implementing -verify.
347
112k
  if (Client) {
348
29.1k
    Diags->setClient(Client, ShouldOwnClient);
349
29.1k
  } else
350
83.6k
    Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts));
351
352
  // Chain in -verify checker, if requested.
353
112k
  if (Opts->VerifyDiagnostics)
354
18.1k
    Diags->setClient(new VerifyDiagnosticConsumer(*Diags));
355
356
  // Chain in -diagnostic-log-file dumper, if requested.
357
112k
  if (!Opts->DiagnosticLogFile.empty())
358
11
    SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags);
359
360
112k
  if (!Opts->DiagnosticSerializationFile.empty())
361
25
    SetupSerializedDiagnostics(Opts, *Diags,
362
25
                               Opts->DiagnosticSerializationFile);
363
364
  // Configure our handling of diagnostics.
365
112k
  ProcessWarningOptions(*Diags, *Opts);
366
367
112k
  return Diags;
368
112k
}
369
370
// File Manager
371
372
FileManager *CompilerInstance::createFileManager(
373
75.3k
    IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
374
75.3k
  if (!VFS)
375
57.3k
    VFS = FileMgr ? 
&FileMgr->getVirtualFileSystem()15.7k
376
57.3k
                  : createVFSFromCompilerInvocation(getInvocation(),
377
41.5k
                                                    getDiagnostics());
378
75.3k
  assert(VFS && "FileManager has no VFS?");
379
0
  FileMgr = new FileManager(getFileSystemOpts(), std::move(VFS));
380
75.3k
  return FileMgr.get();
381
75.3k
}
382
383
// Source Manager
384
385
78.5k
void CompilerInstance::createSourceManager(FileManager &FileMgr) {
386
78.5k
  SourceMgr = new SourceManager(getDiagnostics(), FileMgr);
387
78.5k
}
388
389
// Initialize the remapping of files to alternative contents, e.g.,
390
// those specified through other files.
391
static void InitializeFileRemapping(DiagnosticsEngine &Diags,
392
                                    SourceManager &SourceMgr,
393
                                    FileManager &FileMgr,
394
90.8k
                                    const PreprocessorOptions &InitOpts) {
395
  // Remap files in the source manager (with buffers).
396
90.8k
  for (const auto &RB : InitOpts.RemappedFileBuffers) {
397
    // Create the file entry for the file that we're mapping from.
398
4.21k
    const FileEntry *FromFile =
399
4.21k
        FileMgr.getVirtualFile(RB.first, RB.second->getBufferSize(), 0);
400
4.21k
    if (!FromFile) {
401
0
      Diags.Report(diag::err_fe_remap_missing_from_file) << RB.first;
402
0
      if (!InitOpts.RetainRemappedFileBuffers)
403
0
        delete RB.second;
404
0
      continue;
405
0
    }
406
407
    // Override the contents of the "from" file with the contents of the
408
    // "to" file. If the caller owns the buffers, then pass a MemoryBufferRef;
409
    // otherwise, pass as a std::unique_ptr<MemoryBuffer> to transfer ownership
410
    // to the SourceManager.
411
4.21k
    if (InitOpts.RetainRemappedFileBuffers)
412
567
      SourceMgr.overrideFileContents(FromFile, RB.second->getMemBufferRef());
413
3.64k
    else
414
3.64k
      SourceMgr.overrideFileContents(
415
3.64k
          FromFile, std::unique_ptr<llvm::MemoryBuffer>(
416
3.64k
                        const_cast<llvm::MemoryBuffer *>(RB.second)));
417
4.21k
  }
418
419
  // Remap files in the source manager (with other files).
420
90.8k
  for (const auto &RF : InitOpts.RemappedFiles) {
421
    // Find the file that we're mapping to.
422
18
    auto ToFile = FileMgr.getFile(RF.second);
423
18
    if (!ToFile) {
424
0
      Diags.Report(diag::err_fe_remap_missing_to_file) << RF.first << RF.second;
425
0
      continue;
426
0
    }
427
428
    // Create the file entry for the file that we're mapping from.
429
18
    const FileEntry *FromFile =
430
18
        FileMgr.getVirtualFile(RF.first, (*ToFile)->getSize(), 0);
431
18
    if (!FromFile) {
432
0
      Diags.Report(diag::err_fe_remap_missing_from_file) << RF.first;
433
0
      continue;
434
0
    }
435
436
    // Override the contents of the "from" file with the contents of
437
    // the "to" file.
438
18
    SourceMgr.overrideFileContents(FromFile, *ToFile);
439
18
  }
440
441
90.8k
  SourceMgr.setOverridenFilesKeepOriginalName(
442
90.8k
      InitOpts.RemappedFilesKeepOriginalName);
443
90.8k
}
444
445
// Preprocessor
446
447
90.8k
void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) {
448
90.8k
  const PreprocessorOptions &PPOpts = getPreprocessorOpts();
449
450
  // The AST reader holds a reference to the old preprocessor (if any).
451
90.8k
  TheASTReader.reset();
452
453
  // Create the Preprocessor.
454
90.8k
  HeaderSearch *HeaderInfo =
455
90.8k
      new HeaderSearch(getHeaderSearchOptsPtr(), getSourceManager(),
456
90.8k
                       getDiagnostics(), getLangOpts(), &getTarget());
457
90.8k
  PP = std::make_shared<Preprocessor>(Invocation->getPreprocessorOptsPtr(),
458
90.8k
                                      getDiagnostics(), getLangOpts(),
459
90.8k
                                      getSourceManager(), *HeaderInfo, *this,
460
90.8k
                                      /*IdentifierInfoLookup=*/nullptr,
461
90.8k
                                      /*OwnsHeaderSearch=*/true, TUKind);
462
90.8k
  getTarget().adjust(getDiagnostics(), getLangOpts());
463
90.8k
  PP->Initialize(getTarget(), getAuxTarget());
464
465
90.8k
  if (PPOpts.DetailedRecord)
466
1.99k
    PP->createPreprocessingRecord();
467
468
  // Apply remappings to the source manager.
469
90.8k
  InitializeFileRemapping(PP->getDiagnostics(), PP->getSourceManager(),
470
90.8k
                          PP->getFileManager(), PPOpts);
471
472
  // Predefine macros and configure the preprocessor.
473
90.8k
  InitializePreprocessor(*PP, PPOpts, getPCHContainerReader(),
474
90.8k
                         getFrontendOpts());
475
476
  // Initialize the header search object.  In CUDA compilations, we use the aux
477
  // triple (the host triple) to initialize our header search, since we need to
478
  // find the host headers in order to compile the CUDA code.
479
90.8k
  const llvm::Triple *HeaderSearchTriple = &PP->getTargetInfo().getTriple();
480
90.8k
  if (PP->getTargetInfo().getTriple().getOS() == llvm::Triple::CUDA &&
481
90.8k
      
PP->getAuxTargetInfo()65
)
482
18
    HeaderSearchTriple = &PP->getAuxTargetInfo()->getTriple();
483
484
90.8k
  ApplyHeaderSearchOptions(PP->getHeaderSearchInfo(), getHeaderSearchOpts(),
485
90.8k
                           PP->getLangOpts(), *HeaderSearchTriple);
486
487
90.8k
  PP->setPreprocessedOutput(getPreprocessorOutputOpts().ShowCPP);
488
489
90.8k
  if (PP->getLangOpts().Modules && 
PP->getLangOpts().ImplicitModules10.4k
) {
490
7.30k
    std::string ModuleHash = getInvocation().getModuleHash();
491
7.30k
    PP->getHeaderSearchInfo().setModuleHash(ModuleHash);
492
7.30k
    PP->getHeaderSearchInfo().setModuleCachePath(
493
7.30k
        getSpecificModuleCachePath(ModuleHash));
494
7.30k
  }
495
496
  // Handle generating dependencies, if requested.
497
90.8k
  const DependencyOutputOptions &DepOpts = getDependencyOutputOpts();
498
90.8k
  if (!DepOpts.OutputFile.empty())
499
3.39k
    addDependencyCollector(std::make_shared<DependencyFileGenerator>(DepOpts));
500
90.8k
  if (!DepOpts.DOTOutputFile.empty())
501
0
    AttachDependencyGraphGen(*PP, DepOpts.DOTOutputFile,
502
0
                             getHeaderSearchOpts().Sysroot);
503
504
  // If we don't have a collector, but we are collecting module dependencies,
505
  // then we're the top level compiler instance and need to create one.
506
90.8k
  if (!ModuleDepCollector && 
!DepOpts.ModuleDependencyOutputDir.empty()90.8k
) {
507
17
    ModuleDepCollector = std::make_shared<ModuleDependencyCollector>(
508
17
        DepOpts.ModuleDependencyOutputDir);
509
17
  }
510
511
  // If there is a module dep collector, register with other dep collectors
512
  // and also (a) collect header maps and (b) TODO: input vfs overlay files.
513
90.8k
  if (ModuleDepCollector) {
514
46
    addDependencyCollector(ModuleDepCollector);
515
46
    collectHeaderMaps(PP->getHeaderSearchInfo(), ModuleDepCollector);
516
46
    collectIncludePCH(*this, ModuleDepCollector);
517
46
    collectVFSEntries(*this, ModuleDepCollector);
518
46
  }
519
520
90.8k
  for (auto &Listener : DependencyCollectors)
521
3.72k
    Listener->attachToPreprocessor(*PP);
522
523
  // Handle generating header include information, if requested.
524
90.8k
  if (DepOpts.ShowHeaderIncludes)
525
4
    AttachHeaderIncludeGen(*PP, DepOpts);
526
90.8k
  if (!DepOpts.HeaderIncludeOutputFile.empty()) {
527
6
    StringRef OutputPath = DepOpts.HeaderIncludeOutputFile;
528
6
    if (OutputPath == "-")
529
6
      OutputPath = "";
530
6
    AttachHeaderIncludeGen(*PP, DepOpts,
531
6
                           /*ShowAllHeaders=*/true, OutputPath,
532
6
                           /*ShowDepth=*/false);
533
6
  }
534
535
90.8k
  if (DepOpts.ShowIncludesDest != ShowIncludesDestination::None) {
536
10
    AttachHeaderIncludeGen(*PP, DepOpts,
537
10
                           /*ShowAllHeaders=*/true, /*OutputPath=*/"",
538
10
                           /*ShowDepth=*/true, /*MSStyle=*/true);
539
10
  }
540
90.8k
}
541
542
11.0k
std::string CompilerInstance::getSpecificModuleCachePath(StringRef ModuleHash) {
543
  // Set up the module path, including the hash for the module-creation options.
544
11.0k
  SmallString<256> SpecificModuleCache(getHeaderSearchOpts().ModuleCachePath);
545
11.0k
  if (!SpecificModuleCache.empty() && 
!getHeaderSearchOpts().DisableModuleHash6.08k
)
546
5.82k
    llvm::sys::path::append(SpecificModuleCache, ModuleHash);
547
11.0k
  return std::string(SpecificModuleCache.str());
548
11.0k
}
549
550
// ASTContext
551
552
88.2k
void CompilerInstance::createASTContext() {
553
88.2k
  Preprocessor &PP = getPreprocessor();
554
88.2k
  auto *Context = new ASTContext(getLangOpts(), PP.getSourceManager(),
555
88.2k
                                 PP.getIdentifierTable(), PP.getSelectorTable(),
556
88.2k
                                 PP.getBuiltinInfo(), PP.TUKind);
557
88.2k
  Context->InitBuiltinTypes(getTarget(), getAuxTarget());
558
88.2k
  setASTContext(Context);
559
88.2k
}
560
561
// ExternalASTSource
562
563
namespace {
564
// Helper to recursively read the module names for all modules we're adding.
565
// We mark these as known and redirect any attempt to load that module to
566
// the files we were handed.
567
struct ReadModuleNames : ASTReaderListener {
568
  Preprocessor &PP;
569
  llvm::SmallVector<std::string, 8> LoadedModules;
570
571
4.23k
  ReadModuleNames(Preprocessor &PP) : PP(PP) {}
572
573
1.48k
  void ReadModuleName(StringRef ModuleName) override {
574
    // Keep the module name as a string for now. It's not safe to create a new
575
    // IdentifierInfo from an ASTReader callback.
576
1.48k
    LoadedModules.push_back(ModuleName.str());
577
1.48k
  }
578
579
4.19k
  void registerAll() {
580
4.19k
    ModuleMap &MM = PP.getHeaderSearchInfo().getModuleMap();
581
4.19k
    for (const std::string &LoadedModule : LoadedModules)
582
1.45k
      MM.cacheModuleLoad(*PP.getIdentifierInfo(LoadedModule),
583
1.45k
                         MM.findModule(LoadedModule));
584
4.19k
    LoadedModules.clear();
585
4.19k
  }
586
587
34
  void markAllUnavailable() {
588
34
    for (const std::string &LoadedModule : LoadedModules) {
589
16
      if (Module *M = PP.getHeaderSearchInfo().getModuleMap().findModule(
590
16
              LoadedModule)) {
591
13
        M->HasIncompatibleModuleFile = true;
592
593
        // Mark module as available if the only reason it was unavailable
594
        // was missing headers.
595
13
        SmallVector<Module *, 2> Stack;
596
13
        Stack.push_back(M);
597
28
        while (!Stack.empty()) {
598
15
          Module *Current = Stack.pop_back_val();
599
15
          if (Current->IsUnimportable) 
continue0
;
600
15
          Current->IsAvailable = true;
601
15
          Stack.insert(Stack.end(),
602
15
                       Current->submodule_begin(), Current->submodule_end());
603
15
        }
604
13
      }
605
16
    }
606
34
    LoadedModules.clear();
607
34
  }
608
};
609
} // namespace
610
611
void CompilerInstance::createPCHExternalASTSource(
612
    StringRef Path, DisableValidationForModuleKind DisableValidation,
613
    bool AllowPCHWithCompilerErrors, void *DeserializationListener,
614
3.70k
    bool OwnDeserializationListener) {
615
3.70k
  bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
616
3.70k
  TheASTReader = createPCHExternalASTSource(
617
3.70k
      Path, getHeaderSearchOpts().Sysroot, DisableValidation,
618
3.70k
      AllowPCHWithCompilerErrors, getPreprocessor(), getModuleCache(),
619
3.70k
      getASTContext(), getPCHContainerReader(),
620
3.70k
      getFrontendOpts().ModuleFileExtensions, DependencyCollectors,
621
3.70k
      DeserializationListener, OwnDeserializationListener, Preamble,
622
3.70k
      getFrontendOpts().UseGlobalModuleIndex);
623
3.70k
}
624
625
IntrusiveRefCntPtr<ASTReader> CompilerInstance::createPCHExternalASTSource(
626
    StringRef Path, StringRef Sysroot,
627
    DisableValidationForModuleKind DisableValidation,
628
    bool AllowPCHWithCompilerErrors, Preprocessor &PP,
629
    InMemoryModuleCache &ModuleCache, ASTContext &Context,
630
    const PCHContainerReader &PCHContainerRdr,
631
    ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
632
    ArrayRef<std::shared_ptr<DependencyCollector>> DependencyCollectors,
633
    void *DeserializationListener, bool OwnDeserializationListener,
634
3.70k
    bool Preamble, bool UseGlobalModuleIndex) {
635
3.70k
  HeaderSearchOptions &HSOpts = PP.getHeaderSearchInfo().getHeaderSearchOpts();
636
637
3.70k
  IntrusiveRefCntPtr<ASTReader> Reader(new ASTReader(
638
3.70k
      PP, ModuleCache, &Context, PCHContainerRdr, Extensions,
639
3.70k
      Sysroot.empty() ? 
""0
: Sysroot.data(), DisableValidation,
640
3.70k
      AllowPCHWithCompilerErrors, /*AllowConfigurationMismatch*/ false,
641
3.70k
      HSOpts.ModulesValidateSystemHeaders, HSOpts.ValidateASTInputFilesContent,
642
3.70k
      UseGlobalModuleIndex));
643
644
  // We need the external source to be set up before we read the AST, because
645
  // eagerly-deserialized declarations may use it.
646
3.70k
  Context.setExternalSource(Reader.get());
647
648
3.70k
  Reader->setDeserializationListener(
649
3.70k
      static_cast<ASTDeserializationListener *>(DeserializationListener),
650
3.70k
      /*TakeOwnership=*/OwnDeserializationListener);
651
652
3.70k
  for (auto &Listener : DependencyCollectors)
653
28
    Listener->attachToASTReader(*Reader);
654
655
3.70k
  auto Listener = std::make_unique<ReadModuleNames>(PP);
656
3.70k
  auto &ListenerRef = *Listener;
657
3.70k
  ASTReader::ListenerScope ReadModuleNamesListener(*Reader,
658
3.70k
                                                   std::move(Listener));
659
660
3.70k
  switch (Reader->ReadAST(Path,
661
3.70k
                          Preamble ? 
serialization::MK_Preamble339
662
3.70k
                                   : 
serialization::MK_PCH3.36k
,
663
3.70k
                          SourceLocation(),
664
3.70k
                          ASTReader::ARR_None)) {
665
3.67k
  case ASTReader::Success:
666
    // Set the predefines buffer as suggested by the PCH reader. Typically, the
667
    // predefines buffer will be empty.
668
3.67k
    PP.setPredefines(Reader->getSuggestedPredefines());
669
3.67k
    ListenerRef.registerAll();
670
3.67k
    return Reader;
671
672
2
  case ASTReader::Failure:
673
    // Unrecoverable failure: don't even try to process the input file.
674
2
    break;
675
676
0
  case ASTReader::Missing:
677
11
  case ASTReader::OutOfDate:
678
11
  case ASTReader::VersionMismatch:
679
25
  case ASTReader::ConfigurationMismatch:
680
26
  case ASTReader::HadErrors:
681
    // No suitable PCH file could be found. Return an error.
682
26
    break;
683
3.70k
  }
684
685
28
  ListenerRef.markAllUnavailable();
686
28
  Context.setExternalSource(nullptr);
687
28
  return nullptr;
688
3.70k
}
689
690
// Code Completion
691
692
static bool EnableCodeCompletion(Preprocessor &PP,
693
                                 StringRef Filename,
694
                                 unsigned Line,
695
1.21k
                                 unsigned Column) {
696
  // Tell the source manager to chop off the given file at a specific
697
  // line and column.
698
1.21k
  auto Entry = PP.getFileManager().getFile(Filename);
699
1.21k
  if (!Entry) {
700
0
    PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
701
0
      << Filename;
702
0
    return true;
703
0
  }
704
705
  // Truncate the named file at the given line/column.
706
1.21k
  PP.SetCodeCompletionPoint(*Entry, Line, Column);
707
1.21k
  return false;
708
1.21k
}
709
710
1.21k
void CompilerInstance::createCodeCompletionConsumer() {
711
1.21k
  const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt;
712
1.21k
  if (!CompletionConsumer) {
713
325
    setCodeCompletionConsumer(createCodeCompletionConsumer(
714
325
        getPreprocessor(), Loc.FileName, Loc.Line, Loc.Column,
715
325
        getFrontendOpts().CodeCompleteOpts, llvm::outs()));
716
325
    return;
717
889
  } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName,
718
889
                                  Loc.Line, Loc.Column)) {
719
0
    setCodeCompletionConsumer(nullptr);
720
0
    return;
721
0
  }
722
1.21k
}
723
724
17
void CompilerInstance::createFrontendTimer() {
725
17
  FrontendTimerGroup.reset(
726
17
      new llvm::TimerGroup("frontend", "Clang front-end time report"));
727
17
  FrontendTimer.reset(
728
17
      new llvm::Timer("frontend", "Clang front-end timer",
729
17
                      *FrontendTimerGroup));
730
17
}
731
732
CodeCompleteConsumer *
733
CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
734
                                               StringRef Filename,
735
                                               unsigned Line,
736
                                               unsigned Column,
737
                                               const CodeCompleteOptions &Opts,
738
325
                                               raw_ostream &OS) {
739
325
  if (EnableCodeCompletion(PP, Filename, Line, Column))
740
0
    return nullptr;
741
742
  // Set up the creation routine for code-completion.
743
325
  return new PrintingCodeCompleteConsumer(Opts, OS);
744
325
}
745
746
void CompilerInstance::createSema(TranslationUnitKind TUKind,
747
72.2k
                                  CodeCompleteConsumer *CompletionConsumer) {
748
72.2k
  TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(),
749
72.2k
                         TUKind, CompletionConsumer));
750
  // Attach the external sema source if there is any.
751
72.2k
  if (ExternalSemaSrc) {
752
1
    TheSema->addExternalSource(ExternalSemaSrc.get());
753
1
    ExternalSemaSrc->InitializeSema(*TheSema);
754
1
  }
755
72.2k
}
756
757
// Output Files
758
759
73.5k
void CompilerInstance::clearOutputFiles(bool EraseFiles) {
760
  // The ASTConsumer can own streams that write to the output files.
761
73.5k
  assert(!hasASTConsumer() && "ASTConsumer should be reset");
762
  // Ignore errors that occur when trying to discard the temp file.
763
28.8k
  for (OutputFile &OF : OutputFiles) {
764
28.8k
    if (EraseFiles) {
765
336
      if (OF.File)
766
114
        consumeError(OF.File->discard());
767
336
      if (!OF.Filename.empty())
768
145
        llvm::sys::fs::remove(OF.Filename);
769
336
      continue;
770
336
    }
771
772
28.5k
    if (!OF.File)
773
18.1k
      continue;
774
775
10.3k
    if (OF.File->TmpName.empty()) {
776
0
      consumeError(OF.File->discard());
777
0
      continue;
778
0
    }
779
780
    // If '-working-directory' was passed, the output filename should be
781
    // relative to that.
782
10.3k
    SmallString<128> NewOutFile(OF.Filename);
783
10.3k
    FileMgr->FixupRelativePath(NewOutFile);
784
785
10.3k
    llvm::Error E = OF.File->keep(NewOutFile);
786
10.3k
    if (!E)
787
10.3k
      continue;
788
789
0
    getDiagnostics().Report(diag::err_unable_to_rename_temp)
790
0
        << OF.File->TmpName << OF.Filename << std::move(E);
791
792
0
    llvm::sys::fs::remove(OF.File->TmpName);
793
0
  }
794
73.5k
  OutputFiles.clear();
795
73.5k
  if (DeleteBuiltModules) {
796
73.4k
    for (auto &Module : BuiltModules)
797
74
      llvm::sys::fs::remove(Module.second);
798
73.4k
    BuiltModules.clear();
799
73.4k
  }
800
73.5k
}
801
802
std::unique_ptr<raw_pwrite_stream> CompilerInstance::createDefaultOutputFile(
803
    bool Binary, StringRef InFile, StringRef Extension, bool RemoveFileOnSignal,
804
28.8k
    bool CreateMissingDirectories, bool ForceUseTemporary) {
805
28.8k
  StringRef OutputPath = getFrontendOpts().OutputFile;
806
28.8k
  Optional<SmallString<128>> PathStorage;
807
28.8k
  if (OutputPath.empty()) {
808
1.43k
    if (InFile == "-" || 
Extension.empty()975
) {
809
1.43k
      OutputPath = "-";
810
1.43k
    } else {
811
5
      PathStorage.emplace(InFile);
812
5
      llvm::sys::path::replace_extension(*PathStorage, Extension);
813
5
      OutputPath = *PathStorage;
814
5
    }
815
1.43k
  }
816
817
28.8k
  return createOutputFile(OutputPath, Binary, RemoveFileOnSignal,
818
28.8k
                          getFrontendOpts().UseTemporary || 
ForceUseTemporary0
,
819
28.8k
                          CreateMissingDirectories);
820
28.8k
}
821
822
9
std::unique_ptr<raw_pwrite_stream> CompilerInstance::createNullOutputFile() {
823
9
  return std::make_unique<llvm::raw_null_ostream>();
824
9
}
825
826
std::unique_ptr<raw_pwrite_stream>
827
CompilerInstance::createOutputFile(StringRef OutputPath, bool Binary,
828
                                   bool RemoveFileOnSignal, bool UseTemporary,
829
28.8k
                                   bool CreateMissingDirectories) {
830
28.8k
  Expected<std::unique_ptr<raw_pwrite_stream>> OS =
831
28.8k
      createOutputFileImpl(OutputPath, Binary, RemoveFileOnSignal, UseTemporary,
832
28.8k
                           CreateMissingDirectories);
833
28.8k
  if (OS)
834
28.8k
    return std::move(*OS);
835
3
  getDiagnostics().Report(diag::err_fe_unable_to_open_output)
836
3
      << OutputPath << errorToErrorCode(OS.takeError()).message();
837
3
  return nullptr;
838
28.8k
}
839
840
Expected<std::unique_ptr<llvm::raw_pwrite_stream>>
841
CompilerInstance::createOutputFileImpl(StringRef OutputPath, bool Binary,
842
                                       bool RemoveFileOnSignal,
843
                                       bool UseTemporary,
844
28.8k
                                       bool CreateMissingDirectories) {
845
28.8k
  assert((!CreateMissingDirectories || UseTemporary) &&
846
28.8k
         "CreateMissingDirectories is only allowed when using temporary files");
847
848
0
  std::unique_ptr<llvm::raw_fd_ostream> OS;
849
28.8k
  Optional<StringRef> OSFile;
850
851
28.8k
  if (UseTemporary) {
852
28.8k
    if (OutputPath == "-")
853
17.7k
      UseTemporary = false;
854
11.0k
    else {
855
11.0k
      llvm::sys::fs::file_status Status;
856
11.0k
      llvm::sys::fs::status(OutputPath, Status);
857
11.0k
      if (llvm::sys::fs::exists(Status)) {
858
        // Fail early if we can't write to the final destination.
859
3.81k
        if (!llvm::sys::fs::can_write(OutputPath))
860
0
          return llvm::errorCodeToError(
861
0
              make_error_code(llvm::errc::operation_not_permitted));
862
863
        // Don't use a temporary if the output is a special file. This handles
864
        // things like '-o /dev/null'
865
3.81k
        if (!llvm::sys::fs::is_regular_file(Status))
866
573
          UseTemporary = false;
867
3.81k
      }
868
11.0k
    }
869
28.8k
  }
870
871
28.8k
  Optional<llvm::sys::fs::TempFile> Temp;
872
28.8k
  if (UseTemporary) {
873
    // Create a temporary file.
874
    // Insert -%%%%%%%% before the extension (if any), and because some tools
875
    // (noticeable, clang's own GlobalModuleIndex.cpp) glob for build
876
    // artifacts, also append .tmp.
877
10.5k
    StringRef OutputExtension = llvm::sys::path::extension(OutputPath);
878
10.5k
    SmallString<128> TempPath =
879
10.5k
        StringRef(OutputPath).drop_back(OutputExtension.size());
880
10.5k
    TempPath += "-%%%%%%%%";
881
10.5k
    TempPath += OutputExtension;
882
10.5k
    TempPath += ".tmp";
883
10.5k
    Expected<llvm::sys::fs::TempFile> ExpectedFile =
884
10.5k
        llvm::sys::fs::TempFile::create(
885
10.5k
            TempPath, llvm::sys::fs::all_read | llvm::sys::fs::all_write,
886
10.5k
            Binary ? 
llvm::sys::fs::OF_None9.75k
:
llvm::sys::fs::OF_Text762
);
887
888
10.5k
    llvm::Error E = handleErrors(
889
10.5k
        ExpectedFile.takeError(), [&](const llvm::ECError &E) -> llvm::Error {
890
69
          std::error_code EC = E.convertToErrorCode();
891
69
          if (CreateMissingDirectories &&
892
69
              
EC == llvm::errc::no_such_file_or_directory66
) {
893
66
            StringRef Parent = llvm::sys::path::parent_path(OutputPath);
894
66
            EC = llvm::sys::fs::create_directories(Parent);
895
66
            if (!EC) {
896
66
              ExpectedFile = llvm::sys::fs::TempFile::create(TempPath);
897
66
              if (!ExpectedFile)
898
0
                return llvm::errorCodeToError(
899
0
                    llvm::errc::no_such_file_or_directory);
900
66
            }
901
66
          }
902
69
          return llvm::errorCodeToError(EC);
903
69
        });
904
905
10.5k
    if (E) {
906
3
      consumeError(std::move(E));
907
10.5k
    } else {
908
10.5k
      Temp = std::move(ExpectedFile.get());
909
10.5k
      OS.reset(new llvm::raw_fd_ostream(Temp->FD, /*shouldClose=*/false));
910
10.5k
      OSFile = Temp->TmpName;
911
10.5k
    }
912
    // If we failed to create the temporary, fallback to writing to the file
913
    // directly. This handles the corner case where we cannot write to the
914
    // directory, but can write to the file.
915
10.5k
  }
916
917
28.8k
  if (!OS) {
918
18.3k
    OSFile = OutputPath;
919
18.3k
    std::error_code EC;
920
18.3k
    OS.reset(new llvm::raw_fd_ostream(
921
18.3k
        *OSFile, EC,
922
18.3k
        (Binary ? 
llvm::sys::fs::OF_None138
:
llvm::sys::fs::OF_TextWithCRLF18.2k
)));
923
18.3k
    if (EC)
924
3
      return llvm::errorCodeToError(EC);
925
18.3k
  }
926
927
  // Add the output file -- but don't try to remove "-", since this means we are
928
  // using stdin.
929
28.8k
  OutputFiles.emplace_back(((OutputPath != "-") ? 
OutputPath11.0k
:
""17.7k
).str(),
930
28.8k
                           std::move(Temp));
931
932
28.8k
  if (!Binary || 
OS->supportsSeeking()9.88k
)
933
28.8k
    return std::move(OS);
934
935
73
  return std::make_unique<llvm::buffer_unique_ostream>(std::move(OS));
936
28.8k
}
937
938
// Initialization Utilities
939
940
74.9k
bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input){
941
74.9k
  return InitializeSourceManager(Input, getDiagnostics(), getFileManager(),
942
74.9k
                                 getSourceManager());
943
74.9k
}
944
945
// static
946
bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input,
947
                                               DiagnosticsEngine &Diags,
948
                                               FileManager &FileMgr,
949
74.9k
                                               SourceManager &SourceMgr) {
950
74.9k
  SrcMgr::CharacteristicKind Kind =
951
74.9k
      Input.getKind().getFormat() == InputKind::ModuleMap
952
74.9k
          ? 
Input.isSystem()2.06k
?
SrcMgr::C_System_ModuleMap353
953
2.06k
                             : 
SrcMgr::C_User_ModuleMap1.70k
954
74.9k
          : 
Input.isSystem()72.9k
?
SrcMgr::C_System0
:
SrcMgr::C_User72.9k
;
955
956
74.9k
  if (Input.isBuffer()) {
957
26
    SourceMgr.setMainFileID(SourceMgr.createFileID(Input.getBuffer(), Kind));
958
26
    assert(SourceMgr.getMainFileID().isValid() &&
959
26
           "Couldn't establish MainFileID!");
960
0
    return true;
961
26
  }
962
963
74.9k
  StringRef InputFile = Input.getFile();
964
965
  // Figure out where to get and map in the main file.
966
74.9k
  auto FileOrErr = InputFile == "-"
967
74.9k
                       ? 
FileMgr.getSTDIN()655
968
74.9k
                       : 
FileMgr.getFileRef(InputFile, /*OpenFile=*/true)74.2k
;
969
74.9k
  if (!FileOrErr) {
970
    // FIXME: include the error in the diagnostic even when it's not stdin.
971
4
    auto EC = llvm::errorToErrorCode(FileOrErr.takeError());
972
4
    if (InputFile != "-")
973
4
      Diags.Report(diag::err_fe_error_reading) << InputFile;
974
0
    else
975
0
      Diags.Report(diag::err_fe_error_reading_stdin) << EC.message();
976
4
    return false;
977
4
  }
978
979
74.9k
  SourceMgr.setMainFileID(
980
74.9k
      SourceMgr.createFileID(*FileOrErr, SourceLocation(), Kind));
981
982
74.9k
  assert(SourceMgr.getMainFileID().isValid() &&
983
74.9k
         "Couldn't establish MainFileID!");
984
0
  return true;
985
74.9k
}
986
987
// High-Level Operations
988
989
63.5k
bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
990
63.5k
  assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
991
0
  assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
992
0
  assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
993
994
  // Mark this point as the bottom of the stack if we don't have somewhere
995
  // better. We generally expect frontend actions to be invoked with (nearly)
996
  // DesiredStackSpace available.
997
0
  noteBottomOfStack();
998
999
63.5k
  auto FinishDiagnosticClient = llvm::make_scope_exit([&]() {
1000
    // Notify the diagnostic client that all files were processed.
1001
63.5k
    getDiagnosticClient().finish();
1002
63.5k
  });
1003
1004
63.5k
  raw_ostream &OS = getVerboseOutputStream();
1005
1006
63.5k
  if (!Act.PrepareToExecute(*this))
1007
1
    return false;
1008
1009
63.5k
  if (!createTarget())
1010
117
    return false;
1011
1012
  // rewriter project will change target built-in bool type from its default.
1013
63.4k
  if (getFrontendOpts().ProgramAction == frontend::RewriteObjC)
1014
164
    getTarget().noSignedCharForObjCBool();
1015
1016
  // Validate/process some options.
1017
63.4k
  if (getHeaderSearchOpts().Verbose)
1018
68
    OS << "clang -cc1 version " CLANG_VERSION_STRING
1019
68
       << " based upon " << BACKEND_PACKAGE_STRING
1020
68
       << " default target " << llvm::sys::getDefaultTargetTriple() << "\n";
1021
1022
63.4k
  if (getCodeGenOpts().TimePasses)
1023
17
    createFrontendTimer();
1024
1025
63.4k
  if (getFrontendOpts().ShowStats || 
!getFrontendOpts().StatsFile.empty()63.3k
)
1026
7
    llvm::EnableStatistics(false);
1027
1028
63.4k
  for (const FrontendInputFile &FIF : getFrontendOpts().Inputs) {
1029
    // Reset the ID tables if we are reusing the SourceManager and parsing
1030
    // regular files.
1031
63.4k
    if (hasSourceManager() && 
!Act.isModelParsingAction()22.0k
)
1032
22.0k
      getSourceManager().clearIDTables();
1033
1034
63.4k
    if (Act.BeginSourceFile(*this, FIF)) {
1035
63.3k
      if (llvm::Error Err = Act.Execute()) {
1036
0
        consumeError(std::move(Err)); // FIXME this drops errors on the floor.
1037
0
      }
1038
63.3k
      Act.EndSourceFile();
1039
63.3k
    }
1040
63.4k
  }
1041
1042
63.4k
  if (getDiagnosticOpts().ShowCarets) {
1043
    // We can have multiple diagnostics sharing one diagnostic client.
1044
    // Get the total number of warnings/errors from the client.
1045
63.1k
    unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings();
1046
63.1k
    unsigned NumErrors = getDiagnostics().getClient()->getNumErrors();
1047
1048
63.1k
    if (NumWarnings)
1049
3.10k
      OS << NumWarnings << " warning" << (NumWarnings == 1 ? 
""1.90k
:
"s"1.19k
);
1050
63.1k
    if (NumWarnings && 
NumErrors3.10k
)
1051
113
      OS << " and ";
1052
63.1k
    if (NumErrors)
1053
751
      OS << NumErrors << " error" << (NumErrors == 1 ? 
""362
:
"s"389
);
1054
63.1k
    if (NumWarnings || 
NumErrors60.0k
) {
1055
3.74k
      OS << " generated";
1056
3.74k
      if (getLangOpts().CUDA) {
1057
8
        if (!getLangOpts().CUDAIsDevice) {
1058
3
          OS << " when compiling for host";
1059
5
        } else {
1060
5
          OS << " when compiling for " << getTargetOpts().CPU;
1061
5
        }
1062
8
      }
1063
3.74k
      OS << ".\n";
1064
3.74k
    }
1065
63.1k
  }
1066
1067
63.4k
  if (getFrontendOpts().ShowStats) {
1068
5
    if (hasFileManager()) {
1069
5
      getFileManager().PrintStats();
1070
5
      OS << '\n';
1071
5
    }
1072
5
    llvm::PrintStatistics(OS);
1073
5
  }
1074
63.4k
  StringRef StatsFile = getFrontendOpts().StatsFile;
1075
63.4k
  if (!StatsFile.empty()) {
1076
2
    std::error_code EC;
1077
2
    auto StatS = std::make_unique<llvm::raw_fd_ostream>(
1078
2
        StatsFile, EC, llvm::sys::fs::OF_TextWithCRLF);
1079
2
    if (EC) {
1080
1
      getDiagnostics().Report(diag::warn_fe_unable_to_open_stats_file)
1081
1
          << StatsFile << EC.message();
1082
1
    } else {
1083
1
      llvm::PrintStatisticsJSON(*StatS);
1084
1
    }
1085
2
  }
1086
1087
63.4k
  return !getDiagnostics().getClient()->getNumErrors();
1088
63.5k
}
1089
1090
41.4k
void CompilerInstance::LoadRequestedPlugins() {
1091
  // Load any requested plugins.
1092
41.4k
  for (const std::string &Path : getFrontendOpts().Plugins) {
1093
0
    std::string Error;
1094
0
    if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
1095
0
      getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
1096
0
          << Path << Error;
1097
0
  }
1098
1099
  // Check if any of the loaded plugins replaces the main AST action
1100
41.4k
  for (const FrontendPluginRegistry::entry &Plugin :
1101
41.4k
       FrontendPluginRegistry::entries()) {
1102
0
    std::unique_ptr<PluginASTAction> P(Plugin.instantiate());
1103
0
    if (P->getActionType() == PluginASTAction::ReplaceAction) {
1104
0
      getFrontendOpts().ProgramAction = clang::frontend::PluginAction;
1105
0
      getFrontendOpts().ActionName = Plugin.getName().str();
1106
0
      break;
1107
0
    }
1108
0
  }
1109
41.4k
}
1110
1111
/// Determine the appropriate source input kind based on language
1112
/// options.
1113
1.79k
static Language getLanguageFromOptions(const LangOptions &LangOpts) {
1114
1.79k
  if (LangOpts.OpenCL)
1115
6
    return Language::OpenCL;
1116
1.79k
  if (LangOpts.CUDA)
1117
0
    return Language::CUDA;
1118
1.79k
  if (LangOpts.ObjC)
1119
947
    return LangOpts.CPlusPlus ? 
Language::ObjCXX234
:
Language::ObjC713
;
1120
843
  return LangOpts.CPlusPlus ? 
Language::CXX700
:
Language::C143
;
1121
1.79k
}
1122
1123
/// Compile a module file for the given module, using the options
1124
/// provided by the importing compiler instance. Returns true if the module
1125
/// was built without errors.
1126
static bool
1127
compileModuleImpl(CompilerInstance &ImportingInstance, SourceLocation ImportLoc,
1128
                  StringRef ModuleName, FrontendInputFile Input,
1129
                  StringRef OriginalModuleMapFile, StringRef ModuleFileName,
1130
                  llvm::function_ref<void(CompilerInstance &)> PreBuildStep =
1131
1.62k
                      [](CompilerInstance &) {},
1132
                  llvm::function_ref<void(CompilerInstance &)> PostBuildStep =
1133
1.79k
                      [](CompilerInstance &) 
{}1.71k
) {
1134
1.79k
  llvm::TimeTraceScope TimeScope("Module Compile", ModuleName);
1135
1136
  // Never compile a module that's already finalized - this would cause the
1137
  // existing module to be freed, causing crashes if it is later referenced
1138
1.79k
  if (ImportingInstance.getModuleCache().isPCMFinal(ModuleFileName)) {
1139
2
    ImportingInstance.getDiagnostics().Report(
1140
2
        ImportLoc, diag::err_module_rebuild_finalized)
1141
2
        << ModuleName;
1142
2
    return false;
1143
2
  }
1144
1145
  // Construct a compiler invocation for creating this module.
1146
1.79k
  auto Invocation =
1147
1.79k
      std::make_shared<CompilerInvocation>(ImportingInstance.getInvocation());
1148
1149
1.79k
  PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
1150
1151
  // For any options that aren't intended to affect how a module is built,
1152
  // reset them to their default values.
1153
1.79k
  Invocation->getLangOpts()->resetNonModularOptions();
1154
1.79k
  PPOpts.resetNonModularOptions();
1155
1156
  // Remove any macro definitions that are explicitly ignored by the module.
1157
  // They aren't supposed to affect how the module is built anyway.
1158
1.79k
  HeaderSearchOptions &HSOpts = Invocation->getHeaderSearchOpts();
1159
1.79k
  llvm::erase_if(PPOpts.Macros,
1160
1.79k
                 [&HSOpts](const std::pair<std::string, bool> &def) {
1161
1.58k
                   StringRef MacroDef = def.first;
1162
1.58k
                   return HSOpts.ModulesIgnoreMacros.contains(
1163
1.58k
                       llvm::CachedHashString(MacroDef.split('=').first));
1164
1.58k
                 });
1165
1166
  // If the original compiler invocation had -fmodule-name, pass it through.
1167
1.79k
  Invocation->getLangOpts()->ModuleName =
1168
1.79k
      ImportingInstance.getInvocation().getLangOpts()->ModuleName;
1169
1170
  // Note the name of the module we're building.
1171
1.79k
  Invocation->getLangOpts()->CurrentModule = std::string(ModuleName);
1172
1173
  // Make sure that the failed-module structure has been allocated in
1174
  // the importing instance, and propagate the pointer to the newly-created
1175
  // instance.
1176
1.79k
  PreprocessorOptions &ImportingPPOpts
1177
1.79k
    = ImportingInstance.getInvocation().getPreprocessorOpts();
1178
1.79k
  if (!ImportingPPOpts.FailedModules)
1179
883
    ImportingPPOpts.FailedModules =
1180
883
        std::make_shared<PreprocessorOptions::FailedModulesSet>();
1181
1.79k
  PPOpts.FailedModules = ImportingPPOpts.FailedModules;
1182
1183
  // If there is a module map file, build the module using the module map.
1184
  // Set up the inputs/outputs so that we build the module from its umbrella
1185
  // header.
1186
1.79k
  FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
1187
1.79k
  FrontendOpts.OutputFile = ModuleFileName.str();
1188
1.79k
  FrontendOpts.DisableFree = false;
1189
1.79k
  FrontendOpts.GenerateGlobalModuleIndex = false;
1190
1.79k
  FrontendOpts.BuildingImplicitModule = true;
1191
1.79k
  FrontendOpts.OriginalModuleMap = std::string(OriginalModuleMapFile);
1192
  // Force implicitly-built modules to hash the content of the module file.
1193
1.79k
  HSOpts.ModulesHashContent = true;
1194
1.79k
  FrontendOpts.Inputs = {Input};
1195
1196
  // Don't free the remapped file buffers; they are owned by our caller.
1197
1.79k
  PPOpts.RetainRemappedFileBuffers = true;
1198
1199
1.79k
  Invocation->getDiagnosticOpts().VerifyDiagnostics = 0;
1200
1.79k
  assert(ImportingInstance.getInvocation().getModuleHash() ==
1201
1.79k
         Invocation->getModuleHash() && "Module hash mismatch!");
1202
1203
  // Construct a compiler instance that will be used to actually create the
1204
  // module.  Since we're sharing an in-memory module cache,
1205
  // CompilerInstance::CompilerInstance is responsible for finalizing the
1206
  // buffers to prevent use-after-frees.
1207
0
  CompilerInstance Instance(ImportingInstance.getPCHContainerOperations(),
1208
1.79k
                            &ImportingInstance.getModuleCache());
1209
1.79k
  auto &Inv = *Invocation;
1210
1.79k
  Instance.setInvocation(std::move(Invocation));
1211
1212
1.79k
  Instance.createDiagnostics(new ForwardingDiagnosticConsumer(
1213
1.79k
                                   ImportingInstance.getDiagnosticClient()),
1214
1.79k
                             /*ShouldOwnClient=*/true);
1215
1216
  // Note that this module is part of the module build stack, so that we
1217
  // can detect cycles in the module graph.
1218
1.79k
  Instance.setFileManager(&ImportingInstance.getFileManager());
1219
1.79k
  Instance.createSourceManager(Instance.getFileManager());
1220
1.79k
  SourceManager &SourceMgr = Instance.getSourceManager();
1221
1.79k
  SourceMgr.setModuleBuildStack(
1222
1.79k
    ImportingInstance.getSourceManager().getModuleBuildStack());
1223
1.79k
  SourceMgr.pushModuleBuildStack(ModuleName,
1224
1.79k
    FullSourceLoc(ImportLoc, ImportingInstance.getSourceManager()));
1225
1226
  // If we're collecting module dependencies, we need to share a collector
1227
  // between all of the module CompilerInstances. Other than that, we don't
1228
  // want to produce any dependency output from the module build.
1229
1.79k
  Instance.setModuleDepCollector(ImportingInstance.getModuleDepCollector());
1230
1.79k
  Inv.getDependencyOutputOpts() = DependencyOutputOptions();
1231
1232
1.79k
  ImportingInstance.getDiagnostics().Report(ImportLoc,
1233
1.79k
                                            diag::remark_module_build)
1234
1.79k
    << ModuleName << ModuleFileName;
1235
1236
1.79k
  PreBuildStep(Instance);
1237
1238
  // Execute the action to actually build the module in-place. Use a separate
1239
  // thread so that we get a stack large enough.
1240
1.79k
  bool Crashed = !llvm::CrashRecoveryContext().RunSafelyOnThread(
1241
1.79k
      [&]() {
1242
1.79k
        GenerateModuleFromModuleMapAction Action;
1243
1.79k
        Instance.ExecuteAction(Action);
1244
1.79k
      },
1245
1.79k
      DesiredStackSize);
1246
1247
1.79k
  PostBuildStep(Instance);
1248
1249
1.79k
  ImportingInstance.getDiagnostics().Report(ImportLoc,
1250
1.79k
                                            diag::remark_module_build_done)
1251
1.79k
    << ModuleName;
1252
1253
1.79k
  if (Crashed) {
1254
    // Clear the ASTConsumer if it hasn't been already, in case it owns streams
1255
    // that must be closed before clearing output files.
1256
2
    Instance.setSema(nullptr);
1257
2
    Instance.setASTConsumer(nullptr);
1258
1259
    // Delete any remaining temporary files related to Instance.
1260
2
    Instance.clearOutputFiles(/*EraseFiles=*/true);
1261
2
  }
1262
1263
  // If \p AllowPCMWithCompilerErrors is set return 'success' even if errors
1264
  // occurred.
1265
1.79k
  return !Instance.getDiagnostics().hasErrorOccurred() ||
1266
1.79k
         
Instance.getFrontendOpts().AllowPCMWithCompilerErrors59
;
1267
1.79k
}
1268
1269
static const FileEntry *getPublicModuleMap(const FileEntry *File,
1270
1.62k
                                           FileManager &FileMgr) {
1271
1.62k
  StringRef Filename = llvm::sys::path::filename(File->getName());
1272
1.62k
  SmallString<128> PublicFilename(File->getDir()->getName());
1273
1.62k
  if (Filename == "module_private.map")
1274
1
    llvm::sys::path::append(PublicFilename, "module.map");
1275
1.62k
  else if (Filename == "module.private.modulemap")
1276
11
    llvm::sys::path::append(PublicFilename, "module.modulemap");
1277
1.61k
  else
1278
1.61k
    return nullptr;
1279
12
  if (auto FE = FileMgr.getFile(PublicFilename))
1280
11
    return *FE;
1281
1
  return nullptr;
1282
12
}
1283
1284
/// Compile a module file for the given module in a separate compiler instance,
1285
/// using the options provided by the importing compiler instance. Returns true
1286
/// if the module was built without errors.
1287
static bool compileModule(CompilerInstance &ImportingInstance,
1288
                          SourceLocation ImportLoc, Module *Module,
1289
1.72k
                          StringRef ModuleFileName) {
1290
1.72k
  InputKind IK(getLanguageFromOptions(ImportingInstance.getLangOpts()),
1291
1.72k
               InputKind::ModuleMap);
1292
1293
  // Get or create the module map that we'll use to build this module.
1294
1.72k
  ModuleMap &ModMap
1295
1.72k
    = ImportingInstance.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1296
1.72k
  bool Result;
1297
1.72k
  if (const FileEntry *ModuleMapFile =
1298
1.72k
          ModMap.getContainingModuleMapFile(Module)) {
1299
    // Canonicalize compilation to start with the public module map. This is
1300
    // vital for submodules declarations in the private module maps to be
1301
    // correctly parsed when depending on a top level module in the public one.
1302
1.62k
    if (const FileEntry *PublicMMFile = getPublicModuleMap(
1303
1.62k
            ModuleMapFile, ImportingInstance.getFileManager()))
1304
11
      ModuleMapFile = PublicMMFile;
1305
1306
    // Use the module map where this module resides.
1307
1.62k
    Result = compileModuleImpl(
1308
1.62k
        ImportingInstance, ImportLoc, Module->getTopLevelModuleName(),
1309
1.62k
        FrontendInputFile(ModuleMapFile->getName(), IK, +Module->IsSystem),
1310
1.62k
        ModMap.getModuleMapFileForUniquing(Module)->getName(),
1311
1.62k
        ModuleFileName);
1312
1.62k
  } else {
1313
    // FIXME: We only need to fake up an input file here as a way of
1314
    // transporting the module's directory to the module map parser. We should
1315
    // be able to do that more directly, and parse from a memory buffer without
1316
    // inventing this file.
1317
99
    SmallString<128> FakeModuleMapFile(Module->Directory->getName());
1318
99
    llvm::sys::path::append(FakeModuleMapFile, "__inferred_module.map");
1319
1320
99
    std::string InferredModuleMapContent;
1321
99
    llvm::raw_string_ostream OS(InferredModuleMapContent);
1322
99
    Module->print(OS);
1323
99
    OS.flush();
1324
1325
99
    Result = compileModuleImpl(
1326
99
        ImportingInstance, ImportLoc, Module->getTopLevelModuleName(),
1327
99
        FrontendInputFile(FakeModuleMapFile, IK, +Module->IsSystem),
1328
99
        ModMap.getModuleMapFileForUniquing(Module)->getName(),
1329
99
        ModuleFileName,
1330
99
        [&](CompilerInstance &Instance) {
1331
99
      std::unique_ptr<llvm::MemoryBuffer> ModuleMapBuffer =
1332
99
          llvm::MemoryBuffer::getMemBuffer(InferredModuleMapContent);
1333
99
      ModuleMapFile = Instance.getFileManager().getVirtualFile(
1334
99
          FakeModuleMapFile, InferredModuleMapContent.size(), 0);
1335
99
      Instance.getSourceManager().overrideFileContents(
1336
99
          ModuleMapFile, std::move(ModuleMapBuffer));
1337
99
    });
1338
99
  }
1339
1340
  // We've rebuilt a module. If we're allowed to generate or update the global
1341
  // module index, record that fact in the importing compiler instance.
1342
1.72k
  if (ImportingInstance.getFrontendOpts().GenerateGlobalModuleIndex) {
1343
1.12k
    ImportingInstance.setBuildGlobalModuleIndex(true);
1344
1.12k
  }
1345
1346
1.72k
  return Result;
1347
1.72k
}
1348
1349
/// Read the AST right after compiling the module.
1350
static bool readASTAfterCompileModule(CompilerInstance &ImportingInstance,
1351
                                      SourceLocation ImportLoc,
1352
                                      SourceLocation ModuleNameLoc,
1353
                                      Module *Module, StringRef ModuleFileName,
1354
1.68k
                                      bool *OutOfDate) {
1355
1.68k
  DiagnosticsEngine &Diags = ImportingInstance.getDiagnostics();
1356
1357
1.68k
  unsigned ModuleLoadCapabilities = ASTReader::ARR_Missing;
1358
1.68k
  if (OutOfDate)
1359
6
    ModuleLoadCapabilities |= ASTReader::ARR_OutOfDate;
1360
1361
  // Try to read the module file, now that we've compiled it.
1362
1.68k
  ASTReader::ASTReadResult ReadResult =
1363
1.68k
      ImportingInstance.getASTReader()->ReadAST(
1364
1.68k
          ModuleFileName, serialization::MK_ImplicitModule, ImportLoc,
1365
1.68k
          ModuleLoadCapabilities);
1366
1.68k
  if (ReadResult == ASTReader::Success)
1367
1.68k
    return true;
1368
1369
  // The caller wants to handle out-of-date failures.
1370
3
  if (OutOfDate && 
ReadResult == ASTReader::OutOfDate0
) {
1371
0
    *OutOfDate = true;
1372
0
    return false;
1373
0
  }
1374
1375
  // The ASTReader didn't diagnose the error, so conservatively report it.
1376
3
  if (ReadResult == ASTReader::Missing || 
!Diags.hasErrorOccurred()1
)
1377
2
    Diags.Report(ModuleNameLoc, diag::err_module_not_built)
1378
2
      << Module->Name << SourceRange(ImportLoc, ModuleNameLoc);
1379
1380
3
  return false;
1381
3
}
1382
1383
/// Compile a module in a separate compiler instance and read the AST,
1384
/// returning true if the module compiles without errors.
1385
static bool compileModuleAndReadASTImpl(CompilerInstance &ImportingInstance,
1386
                                        SourceLocation ImportLoc,
1387
                                        SourceLocation ModuleNameLoc,
1388
                                        Module *Module,
1389
1.72k
                                        StringRef ModuleFileName) {
1390
1.72k
  if (!compileModule(ImportingInstance, ModuleNameLoc, Module,
1391
1.72k
                     ModuleFileName)) {
1392
42
    ImportingInstance.getDiagnostics().Report(ModuleNameLoc,
1393
42
                                              diag::err_module_not_built)
1394
42
        << Module->Name << SourceRange(ImportLoc, ModuleNameLoc);
1395
42
    return false;
1396
42
  }
1397
1398
1.67k
  return readASTAfterCompileModule(ImportingInstance, ImportLoc, ModuleNameLoc,
1399
1.67k
                                   Module, ModuleFileName,
1400
1.67k
                                   /*OutOfDate=*/nullptr);
1401
1.72k
}
1402
1403
/// Compile a module in a separate compiler instance and read the AST,
1404
/// returning true if the module compiles without errors, using a lock manager
1405
/// to avoid building the same module in multiple compiler instances.
1406
///
1407
/// Uses a lock file manager and exponential backoff to reduce the chances that
1408
/// multiple instances will compete to create the same module.  On timeout,
1409
/// deletes the lock file in order to avoid deadlock from crashing processes or
1410
/// bugs in the lock file manager.
1411
static bool compileModuleAndReadASTBehindLock(
1412
    CompilerInstance &ImportingInstance, SourceLocation ImportLoc,
1413
1.72k
    SourceLocation ModuleNameLoc, Module *Module, StringRef ModuleFileName) {
1414
1.72k
  DiagnosticsEngine &Diags = ImportingInstance.getDiagnostics();
1415
1416
1.72k
  Diags.Report(ModuleNameLoc, diag::remark_module_lock)
1417
1.72k
      << ModuleFileName << Module->Name;
1418
1419
  // FIXME: have LockFileManager return an error_code so that we can
1420
  // avoid the mkdir when the directory already exists.
1421
1.72k
  StringRef Dir = llvm::sys::path::parent_path(ModuleFileName);
1422
1.72k
  llvm::sys::fs::create_directories(Dir);
1423
1424
1.72k
  while (
true1.72k
) {
1425
1.72k
    llvm::LockFileManager Locked(ModuleFileName);
1426
1.72k
    switch (Locked) {
1427
0
    case llvm::LockFileManager::LFS_Error:
1428
      // ModuleCache takes care of correctness and locks are only necessary for
1429
      // performance. Fallback to building the module in case of any lock
1430
      // related errors.
1431
0
      Diags.Report(ModuleNameLoc, diag::remark_module_lock_failure)
1432
0
          << Module->Name << Locked.getErrorMessage();
1433
      // Clear out any potential leftover.
1434
0
      Locked.unsafeRemoveLockFile();
1435
0
      LLVM_FALLTHROUGH;
1436
1.71k
    case llvm::LockFileManager::LFS_Owned:
1437
      // We're responsible for building the module ourselves.
1438
1.71k
      return compileModuleAndReadASTImpl(ImportingInstance, ImportLoc,
1439
1.71k
                                         ModuleNameLoc, Module, ModuleFileName);
1440
1441
6
    case llvm::LockFileManager::LFS_Shared:
1442
6
      break; // The interesting case.
1443
1.72k
    }
1444
1445
    // Someone else is responsible for building the module. Wait for them to
1446
    // finish.
1447
6
    switch (Locked.waitForUnlock()) {
1448
6
    case llvm::LockFileManager::Res_Success:
1449
6
      break; // The interesting case.
1450
0
    case llvm::LockFileManager::Res_OwnerDied:
1451
0
      continue; // try again to get the lock.
1452
0
    case llvm::LockFileManager::Res_Timeout:
1453
      // Since ModuleCache takes care of correctness, we try waiting for
1454
      // another process to complete the build so clang does not do it done
1455
      // twice. If case of timeout, build it ourselves.
1456
0
      Diags.Report(ModuleNameLoc, diag::remark_module_lock_timeout)
1457
0
          << Module->Name;
1458
      // Clear the lock file so that future invocations can make progress.
1459
0
      Locked.unsafeRemoveLockFile();
1460
0
      continue;
1461
6
    }
1462
1463
    // Read the module that was just written by someone else.
1464
6
    bool OutOfDate = false;
1465
6
    if (readASTAfterCompileModule(ImportingInstance, ImportLoc, ModuleNameLoc,
1466
6
                                  Module, ModuleFileName, &OutOfDate))
1467
6
      return true;
1468
0
    if (!OutOfDate)
1469
0
      return false;
1470
1471
    // The module may be out of date in the presence of file system races,
1472
    // or if one of its imports depends on header search paths that are not
1473
    // consistent with this ImportingInstance.  Try again...
1474
0
  }
1475
1.72k
}
1476
1477
/// Compile a module in a separate compiler instance and read the AST,
1478
/// returning true if the module compiles without errors, potentially using a
1479
/// lock manager to avoid building the same module in multiple compiler
1480
/// instances.
1481
static bool compileModuleAndReadAST(CompilerInstance &ImportingInstance,
1482
                                    SourceLocation ImportLoc,
1483
                                    SourceLocation ModuleNameLoc,
1484
1.72k
                                    Module *Module, StringRef ModuleFileName) {
1485
1.72k
  return ImportingInstance.getInvocation()
1486
1.72k
                 .getFrontendOpts()
1487
1.72k
                 .BuildingImplicitModuleUsesLock
1488
1.72k
             ? compileModuleAndReadASTBehindLock(ImportingInstance, ImportLoc,
1489
1.72k
                                                 ModuleNameLoc, Module,
1490
1.72k
                                                 ModuleFileName)
1491
1.72k
             : compileModuleAndReadASTImpl(ImportingInstance, ImportLoc,
1492
2
                                           ModuleNameLoc, Module,
1493
2
                                           ModuleFileName);
1494
1.72k
}
1495
1496
/// Diagnose differences between the current definition of the given
1497
/// configuration macro and the definition provided on the command line.
1498
static void checkConfigMacro(Preprocessor &PP, StringRef ConfigMacro,
1499
10
                             Module *Mod, SourceLocation ImportLoc) {
1500
10
  IdentifierInfo *Id = PP.getIdentifierInfo(ConfigMacro);
1501
10
  SourceManager &SourceMgr = PP.getSourceManager();
1502
1503
  // If this identifier has never had a macro definition, then it could
1504
  // not have changed.
1505
10
  if (!Id->hadMacroDefinition())
1506
4
    return;
1507
6
  auto *LatestLocalMD = PP.getLocalMacroDirectiveHistory(Id);
1508
1509
  // Find the macro definition from the command line.
1510
6
  MacroInfo *CmdLineDefinition = nullptr;
1511
18
  for (auto *MD = LatestLocalMD; MD; 
MD = MD->getPrevious()12
) {
1512
    // We only care about the predefines buffer.
1513
17
    FileID FID = SourceMgr.getFileID(MD->getLocation());
1514
17
    if (FID.isInvalid() || FID != PP.getPredefinesFileID())
1515
12
      continue;
1516
5
    if (auto *DMD = dyn_cast<DefMacroDirective>(MD))
1517
5
      CmdLineDefinition = DMD->getMacroInfo();
1518
5
    break;
1519
17
  }
1520
1521
6
  auto *CurrentDefinition = PP.getMacroInfo(Id);
1522
6
  if (CurrentDefinition == CmdLineDefinition) {
1523
    // Macro matches. Nothing to do.
1524
5
  } else if (!CurrentDefinition) {
1525
    // This macro was defined on the command line, then #undef'd later.
1526
    // Complain.
1527
1
    PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
1528
1
      << true << ConfigMacro << Mod->getFullModuleName();
1529
1
    auto LatestDef = LatestLocalMD->getDefinition();
1530
1
    assert(LatestDef.isUndefined() &&
1531
1
           "predefined macro went away with no #undef?");
1532
0
    PP.Diag(LatestDef.getUndefLocation(), diag::note_module_def_undef_here)
1533
1
      << true;
1534
1
    return;
1535
4
  } else if (!CmdLineDefinition) {
1536
    // There was no definition for this macro in the predefines buffer,
1537
    // but there was a local definition. Complain.
1538
1
    PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
1539
1
      << false << ConfigMacro << Mod->getFullModuleName();
1540
1
    PP.Diag(CurrentDefinition->getDefinitionLoc(),
1541
1
            diag::note_module_def_undef_here)
1542
1
      << false;
1543
3
  } else if (!CurrentDefinition->isIdenticalTo(*CmdLineDefinition, PP,
1544
3
                                               /*Syntactically=*/true)) {
1545
    // The macro definitions differ.
1546
1
    PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
1547
1
      << false << ConfigMacro << Mod->getFullModuleName();
1548
1
    PP.Diag(CurrentDefinition->getDefinitionLoc(),
1549
1
            diag::note_module_def_undef_here)
1550
1
      << false;
1551
1
  }
1552
6
}
1553
1554
/// Write a new timestamp file with the given path.
1555
675
static void writeTimestampFile(StringRef TimestampFile) {
1556
675
  std::error_code EC;
1557
675
  llvm::raw_fd_ostream Out(TimestampFile.str(), EC, llvm::sys::fs::OF_None);
1558
675
}
1559
1560
/// Prune the module cache of modules that haven't been accessed in
1561
/// a long time.
1562
4.08k
static void pruneModuleCache(const HeaderSearchOptions &HSOpts) {
1563
4.08k
  llvm::sys::fs::file_status StatBuf;
1564
4.08k
  llvm::SmallString<128> TimestampFile;
1565
4.08k
  TimestampFile = HSOpts.ModuleCachePath;
1566
4.08k
  assert(!TimestampFile.empty());
1567
0
  llvm::sys::path::append(TimestampFile, "modules.timestamp");
1568
1569
  // Try to stat() the timestamp file.
1570
4.08k
  if (std::error_code EC = llvm::sys::fs::status(TimestampFile, StatBuf)) {
1571
    // If the timestamp file wasn't there, create one now.
1572
677
    if (EC == std::errc::no_such_file_or_directory) {
1573
674
      writeTimestampFile(TimestampFile);
1574
674
    }
1575
677
    return;
1576
677
  }
1577
1578
  // Check whether the time stamp is older than our pruning interval.
1579
  // If not, do nothing.
1580
3.40k
  time_t TimeStampModTime =
1581
3.40k
      llvm::sys::toTimeT(StatBuf.getLastModificationTime());
1582
3.40k
  time_t CurrentTime = time(nullptr);
1583
3.40k
  if (CurrentTime - TimeStampModTime <= time_t(HSOpts.ModuleCachePruneInterval))
1584
3.41k
    return;
1585
1586
  // Write a new timestamp file so that nobody else attempts to prune.
1587
  // There is a benign race condition here, if two Clang instances happen to
1588
  // notice at the same time that the timestamp is out-of-date.
1589
18.4E
  writeTimestampFile(TimestampFile);
1590
1591
  // Walk the entire module cache, looking for unused module files and module
1592
  // indices.
1593
18.4E
  std::error_code EC;
1594
18.4E
  SmallString<128> ModuleCachePathNative;
1595
18.4E
  llvm::sys::path::native(HSOpts.ModuleCachePath, ModuleCachePathNative);
1596
18.4E
  for (llvm::sys::fs::directory_iterator Dir(ModuleCachePathNative, EC), DirEnd;
1597
18.4E
       
Dir != DirEnd0
&&
!EC5
;
Dir.increment(EC)5
) {
1598
    // If we don't have a directory, there's nothing to look into.
1599
5
    if (!llvm::sys::fs::is_directory(Dir->path()))
1600
2
      continue;
1601
1602
    // Walk all of the files within this directory.
1603
3
    for (llvm::sys::fs::directory_iterator File(Dir->path(), EC), FileEnd;
1604
11
         File != FileEnd && 
!EC8
;
File.increment(EC)8
) {
1605
      // We only care about module and global module index files.
1606
8
      StringRef Extension = llvm::sys::path::extension(File->path());
1607
8
      if (Extension != ".pcm" && 
Extension != ".timestamp"3
&&
1608
8
          
llvm::sys::path::filename(File->path()) != "modules.idx"3
)
1609
0
        continue;
1610
1611
      // Look at this file. If we can't stat it, there's nothing interesting
1612
      // there.
1613
8
      if (llvm::sys::fs::status(File->path(), StatBuf))
1614
0
        continue;
1615
1616
      // If the file has been used recently enough, leave it there.
1617
8
      time_t FileAccessTime = llvm::sys::toTimeT(StatBuf.getLastAccessedTime());
1618
8
      if (CurrentTime - FileAccessTime <=
1619
8
              time_t(HSOpts.ModuleCachePruneAfter)) {
1620
7
        continue;
1621
7
      }
1622
1623
      // Remove the file.
1624
1
      llvm::sys::fs::remove(File->path());
1625
1626
      // Remove the timestamp file.
1627
1
      std::string TimpestampFilename = File->path() + ".timestamp";
1628
1
      llvm::sys::fs::remove(TimpestampFilename);
1629
1
    }
1630
1631
    // If we removed all of the files in the directory, remove the directory
1632
    // itself.
1633
3
    if (llvm::sys::fs::directory_iterator(Dir->path(), EC) ==
1634
3
            llvm::sys::fs::directory_iterator() && 
!EC0
)
1635
0
      llvm::sys::fs::remove(Dir->path());
1636
3
  }
1637
18.4E
}
1638
1639
11.8k
void CompilerInstance::createASTReader() {
1640
11.8k
  if (TheASTReader)
1641
1.52k
    return;
1642
1643
10.2k
  if (!hasASTContext())
1644
61
    createASTContext();
1645
1646
  // If we're implicitly building modules but not currently recursively
1647
  // building a module, check whether we need to prune the module cache.
1648
10.2k
  if (getSourceManager().getModuleBuildStack().empty() &&
1649
10.2k
      
!getPreprocessor().getHeaderSearchInfo().getModuleCachePath().empty()8.25k
&&
1650
10.2k
      
getHeaderSearchOpts().ModuleCachePruneInterval > 04.08k
&&
1651
10.2k
      
getHeaderSearchOpts().ModuleCachePruneAfter > 04.09k
) {
1652
4.08k
    pruneModuleCache(getHeaderSearchOpts());
1653
4.08k
  }
1654
1655
10.2k
  HeaderSearchOptions &HSOpts = getHeaderSearchOpts();
1656
10.2k
  std::string Sysroot = HSOpts.Sysroot;
1657
10.2k
  const PreprocessorOptions &PPOpts = getPreprocessorOpts();
1658
10.2k
  const FrontendOptions &FEOpts = getFrontendOpts();
1659
10.2k
  std::unique_ptr<llvm::Timer> ReadTimer;
1660
1661
10.2k
  if (FrontendTimerGroup)
1662
12
    ReadTimer = std::make_unique<llvm::Timer>("reading_modules",
1663
12
                                                "Reading modules",
1664
12
                                                *FrontendTimerGroup);
1665
10.2k
  TheASTReader = new ASTReader(
1666
10.2k
      getPreprocessor(), getModuleCache(), &getASTContext(),
1667
10.2k
      getPCHContainerReader(), getFrontendOpts().ModuleFileExtensions,
1668
10.2k
      Sysroot.empty() ? 
""0
: Sysroot.c_str(),
1669
10.2k
      PPOpts.DisablePCHOrModuleValidation,
1670
10.2k
      /*AllowASTWithCompilerErrors=*/FEOpts.AllowPCMWithCompilerErrors,
1671
10.2k
      /*AllowConfigurationMismatch=*/false, HSOpts.ModulesValidateSystemHeaders,
1672
10.2k
      HSOpts.ValidateASTInputFilesContent,
1673
10.2k
      getFrontendOpts().UseGlobalModuleIndex, std::move(ReadTimer));
1674
10.2k
  if (hasASTConsumer()) {
1675
803
    TheASTReader->setDeserializationListener(
1676
803
        getASTConsumer().GetASTDeserializationListener());
1677
803
    getASTContext().setASTMutationListener(
1678
803
      getASTConsumer().GetASTMutationListener());
1679
803
  }
1680
10.2k
  getASTContext().setExternalSource(TheASTReader);
1681
10.2k
  if (hasSema())
1682
803
    TheASTReader->InitializeSema(getSema());
1683
10.2k
  if (hasASTConsumer())
1684
803
    TheASTReader->StartTranslationUnit(&getASTConsumer());
1685
1686
10.2k
  for (auto &Listener : DependencyCollectors)
1687
1.00k
    Listener->attachToASTReader(*TheASTReader);
1688
10.2k
}
1689
1690
533
bool CompilerInstance::loadModuleFile(StringRef FileName) {
1691
533
  llvm::Timer Timer;
1692
533
  if (FrontendTimerGroup)
1693
0
    Timer.init("preloading." + FileName.str(), "Preloading " + FileName.str(),
1694
0
               *FrontendTimerGroup);
1695
533
  llvm::TimeRegion TimeLoading(FrontendTimerGroup ? 
&Timer0
: nullptr);
1696
1697
  // If we don't already have an ASTReader, create one now.
1698
533
  if (!TheASTReader)
1699
18
    createASTReader();
1700
1701
  // If -Wmodule-file-config-mismatch is mapped as an error or worse, allow the
1702
  // ASTReader to diagnose it, since it can produce better errors that we can.
1703
533
  bool ConfigMismatchIsRecoverable =
1704
533
      getDiagnostics().getDiagnosticLevel(diag::warn_module_config_mismatch,
1705
533
                                          SourceLocation())
1706
533
        <= DiagnosticsEngine::Warning;
1707
1708
533
  auto Listener = std::make_unique<ReadModuleNames>(*PP);
1709
533
  auto &ListenerRef = *Listener;
1710
533
  ASTReader::ListenerScope ReadModuleNamesListener(*TheASTReader,
1711
533
                                                   std::move(Listener));
1712
1713
  // Try to load the module file.
1714
533
  switch (TheASTReader->ReadAST(
1715
533
      FileName, serialization::MK_ExplicitModule, SourceLocation(),
1716
533
      ConfigMismatchIsRecoverable ? 
ASTReader::ARR_ConfigurationMismatch3
:
0530
)) {
1717
516
  case ASTReader::Success:
1718
    // We successfully loaded the module file; remember the set of provided
1719
    // modules so that we don't try to load implicit modules for them.
1720
516
    ListenerRef.registerAll();
1721
516
    return true;
1722
1723
6
  case ASTReader::ConfigurationMismatch:
1724
    // Ignore unusable module files.
1725
6
    getDiagnostics().Report(SourceLocation(), diag::warn_module_config_mismatch)
1726
6
        << FileName;
1727
    // All modules provided by any files we tried and failed to load are now
1728
    // unavailable; includes of those modules should now be handled textually.
1729
6
    ListenerRef.markAllUnavailable();
1730
6
    return true;
1731
1732
11
  default:
1733
11
    return false;
1734
533
  }
1735
533
}
1736
1737
namespace {
1738
enum ModuleSource {
1739
  MS_ModuleNotFound,
1740
  MS_ModuleCache,
1741
  MS_PrebuiltModulePath,
1742
  MS_ModuleBuildPragma
1743
};
1744
} // end namespace
1745
1746
/// Select a source for loading the named module and compute the filename to
1747
/// load it from.
1748
static ModuleSource selectModuleSource(
1749
    Module *M, StringRef ModuleName, std::string &ModuleFilename,
1750
    const std::map<std::string, std::string, std::less<>> &BuiltModules,
1751
4.58k
    HeaderSearch &HS) {
1752
4.58k
  assert(ModuleFilename.empty() && "Already has a module source?");
1753
1754
  // Check to see if the module has been built as part of this compilation
1755
  // via a module build pragma.
1756
0
  auto BuiltModuleIt = BuiltModules.find(ModuleName);
1757
4.58k
  if (BuiltModuleIt != BuiltModules.end()) {
1758
84
    ModuleFilename = BuiltModuleIt->second;
1759
84
    return MS_ModuleBuildPragma;
1760
84
  }
1761
1762
  // Try to load the module from the prebuilt module path.
1763
4.49k
  const HeaderSearchOptions &HSOpts = HS.getHeaderSearchOpts();
1764
4.49k
  if (!HSOpts.PrebuiltModuleFiles.empty() ||
1765
4.49k
      
!HSOpts.PrebuiltModulePaths.empty()4.47k
) {
1766
88
    ModuleFilename = HS.getPrebuiltModuleFileName(ModuleName);
1767
88
    if (HSOpts.EnablePrebuiltImplicitModules && 
ModuleFilename.empty()4
)
1768
3
      ModuleFilename = HS.getPrebuiltImplicitModuleFileName(M);
1769
88
    if (!ModuleFilename.empty())
1770
82
      return MS_PrebuiltModulePath;
1771
88
  }
1772
1773
  // Try to load the module from the module cache.
1774
4.41k
  if (M) {
1775
4.38k
    ModuleFilename = HS.getCachedModuleFileName(M);
1776
4.38k
    return MS_ModuleCache;
1777
4.38k
  }
1778
1779
29
  return MS_ModuleNotFound;
1780
4.41k
}
1781
1782
ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST(
1783
    StringRef ModuleName, SourceLocation ImportLoc,
1784
4.58k
    SourceLocation ModuleNameLoc, bool IsInclusionDirective) {
1785
  // Search for a module with the given name.
1786
4.58k
  HeaderSearch &HS = PP->getHeaderSearchInfo();
1787
4.58k
  Module *M =
1788
4.58k
      HS.lookupModule(ModuleName, ImportLoc, true, !IsInclusionDirective);
1789
1790
  // Select the source and filename for loading the named module.
1791
4.58k
  std::string ModuleFilename;
1792
4.58k
  ModuleSource Source =
1793
4.58k
      selectModuleSource(M, ModuleName, ModuleFilename, BuiltModules, HS);
1794
4.58k
  if (Source == MS_ModuleNotFound) {
1795
    // We can't find a module, error out here.
1796
24
    getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_found)
1797
24
        << ModuleName << SourceRange(ImportLoc, ModuleNameLoc);
1798
24
    return nullptr;
1799
24
  }
1800
4.55k
  if (ModuleFilename.empty()) {
1801
13
    if (M && M->HasIncompatibleModuleFile) {
1802
      // We tried and failed to load a module file for this module. Fall
1803
      // back to textual inclusion for its headers.
1804
8
      return ModuleLoadResult::ConfigMismatch;
1805
8
    }
1806
1807
5
    getDiagnostics().Report(ModuleNameLoc, diag::err_module_build_disabled)
1808
5
        << ModuleName;
1809
5
    return nullptr;
1810
13
  }
1811
1812
  // Create an ASTReader on demand.
1813
4.54k
  if (!getASTReader())
1814
38
    createASTReader();
1815
1816
  // Time how long it takes to load the module.
1817
4.54k
  llvm::Timer Timer;
1818
4.54k
  if (FrontendTimerGroup)
1819
8
    Timer.init("loading." + ModuleFilename, "Loading " + ModuleFilename,
1820
8
               *FrontendTimerGroup);
1821
4.54k
  llvm::TimeRegion TimeLoading(FrontendTimerGroup ? 
&Timer8
:
nullptr4.53k
);
1822
4.54k
  llvm::TimeTraceScope TimeScope("Module Load", ModuleName);
1823
1824
  // Try to load the module file. If we are not trying to load from the
1825
  // module cache, we don't know how to rebuild modules.
1826
4.54k
  unsigned ARRFlags = Source == MS_ModuleCache
1827
4.54k
                          ? ASTReader::ARR_OutOfDate | ASTReader::ARR_Missing |
1828
4.38k
                                ASTReader::ARR_TreatModuleWithErrorsAsOutOfDate
1829
4.54k
                          : 
Source == MS_PrebuiltModulePath162
1830
162
                                ? 
082
1831
162
                                : 
ASTReader::ARR_ConfigurationMismatch80
;
1832
4.54k
  switch (getASTReader()->ReadAST(ModuleFilename,
1833
4.54k
                                  Source == MS_PrebuiltModulePath
1834
4.54k
                                      ? 
serialization::MK_PrebuiltModule82
1835
4.54k
                                      : 
Source == MS_ModuleBuildPragma4.46k
1836
4.46k
                                            ? 
serialization::MK_ExplicitModule84
1837
4.46k
                                            : 
serialization::MK_ImplicitModule4.37k
,
1838
4.54k
                                  ImportLoc, ARRFlags)) {
1839
2.80k
  case ASTReader::Success: {
1840
2.80k
    if (M)
1841
2.65k
      return M;
1842
149
    assert(Source != MS_ModuleCache &&
1843
149
           "missing module, but file loaded from cache");
1844
1845
    // A prebuilt module is indexed as a ModuleFile; the Module does not exist
1846
    // until the first call to ReadAST.  Look it up now.
1847
0
    M = HS.lookupModule(ModuleName, ImportLoc, true, !IsInclusionDirective);
1848
1849
    // Check whether M refers to the file in the prebuilt module path.
1850
149
    if (M && M->getASTFile())
1851
149
      if (auto ModuleFile = FileMgr->getFile(ModuleFilename))
1852
149
        if (*ModuleFile == M->getASTFile())
1853
149
          return M;
1854
1855
0
    getDiagnostics().Report(ModuleNameLoc, diag::err_module_prebuilt)
1856
0
        << ModuleName;
1857
0
    return ModuleLoadResult();
1858
149
  }
1859
1860
80
  case ASTReader::OutOfDate:
1861
1.73k
  case ASTReader::Missing:
1862
    // The most interesting case.
1863
1.73k
    break;
1864
1865
4
  case ASTReader::ConfigurationMismatch:
1866
4
    if (Source == MS_PrebuiltModulePath)
1867
      // FIXME: We shouldn't be setting HadFatalFailure below if we only
1868
      // produce a warning here!
1869
2
      getDiagnostics().Report(SourceLocation(),
1870
2
                              diag::warn_module_config_mismatch)
1871
2
          << ModuleFilename;
1872
    // Fall through to error out.
1873
4
    LLVM_FALLTHROUGH;
1874
4
  case ASTReader::VersionMismatch:
1875
6
  case ASTReader::HadErrors:
1876
6
    ModuleLoader::HadFatalFailure = true;
1877
    // FIXME: The ASTReader will already have complained, but can we shoehorn
1878
    // that diagnostic information into a more useful form?
1879
6
    return ModuleLoadResult();
1880
1881
3
  case ASTReader::Failure:
1882
3
    ModuleLoader::HadFatalFailure = true;
1883
3
    return ModuleLoadResult();
1884
4.54k
  }
1885
1886
  // ReadAST returned Missing or OutOfDate.
1887
1.73k
  if (Source != MS_ModuleCache) {
1888
    // We don't know the desired configuration for this module and don't
1889
    // necessarily even have a module map. Since ReadAST already produces
1890
    // diagnostics for these two cases, we simply error out here.
1891
0
    return ModuleLoadResult();
1892
0
  }
1893
1894
  // The module file is missing or out-of-date. Build it.
1895
1.73k
  assert(M && "missing module, but trying to compile for cache");
1896
1897
  // Check whether there is a cycle in the module graph.
1898
0
  ModuleBuildStack ModPath = getSourceManager().getModuleBuildStack();
1899
1.73k
  ModuleBuildStack::iterator Pos = ModPath.begin(), PosEnd = ModPath.end();
1900
2.55k
  for (; Pos != PosEnd; 
++Pos822
) {
1901
825
    if (Pos->first == ModuleName)
1902
3
      break;
1903
825
  }
1904
1905
1.73k
  if (Pos != PosEnd) {
1906
3
    SmallString<256> CyclePath;
1907
9
    for (; Pos != PosEnd; 
++Pos6
) {
1908
6
      CyclePath += Pos->first;
1909
6
      CyclePath += " -> ";
1910
6
    }
1911
3
    CyclePath += ModuleName;
1912
1913
3
    getDiagnostics().Report(ModuleNameLoc, diag::err_module_cycle)
1914
3
        << ModuleName << CyclePath;
1915
3
    return nullptr;
1916
3
  }
1917
1918
  // Check whether we have already attempted to build this module (but
1919
  // failed).
1920
1.73k
  if (getPreprocessorOpts().FailedModules &&
1921
1.73k
      
getPreprocessorOpts().FailedModules->hasAlreadyFailed(ModuleName)878
) {
1922
3
    getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_built)
1923
3
        << ModuleName << SourceRange(ImportLoc, ModuleNameLoc);
1924
3
    return nullptr;
1925
3
  }
1926
1927
  // Try to compile and then read the AST.
1928
1.72k
  if (!compileModuleAndReadAST(*this, ImportLoc, ModuleNameLoc, M,
1929
1.72k
                               ModuleFilename)) {
1930
45
    assert(getDiagnostics().hasErrorOccurred() &&
1931
45
           "undiagnosed error in compileModuleAndReadAST");
1932
45
    if (getPreprocessorOpts().FailedModules)
1933
43
      getPreprocessorOpts().FailedModules->addFailed(ModuleName);
1934
45
    return nullptr;
1935
45
  }
1936
1937
  // Okay, we've rebuilt and now loaded the module.
1938
1.68k
  return M;
1939
1.72k
}
1940
1941
ModuleLoadResult
1942
CompilerInstance::loadModule(SourceLocation ImportLoc,
1943
                             ModuleIdPath Path,
1944
                             Module::NameVisibilityKind Visibility,
1945
21.8k
                             bool IsInclusionDirective) {
1946
  // Determine what file we're searching from.
1947
21.8k
  StringRef ModuleName = Path[0].first->getName();
1948
21.8k
  SourceLocation ModuleNameLoc = Path[0].second;
1949
1950
  // If we've already handled this import, just return the cached result.
1951
  // This one-element cache is important to eliminate redundant diagnostics
1952
  // when both the preprocessor and parser see the same import declaration.
1953
21.8k
  if (ImportLoc.isValid() && 
LastModuleImportLoc == ImportLoc21.8k
) {
1954
    // Make the named module visible.
1955
1.89k
    if (LastModuleImportResult && 
ModuleName != getLangOpts().CurrentModule1.87k
)
1956
1.87k
      TheASTReader->makeModuleVisible(LastModuleImportResult, Visibility,
1957
1.87k
                                      ImportLoc);
1958
1.89k
    return LastModuleImportResult;
1959
1.89k
  }
1960
1961
  // If we don't already have information on this module, load the module now.
1962
19.9k
  Module *Module = nullptr;
1963
19.9k
  ModuleMap &MM = getPreprocessor().getHeaderSearchInfo().getModuleMap();
1964
19.9k
  if (auto MaybeModule = MM.getCachedModuleLoad(*Path[0].first)) {
1965
    // Use the cached result, which may be nullptr.
1966
15.3k
    Module = *MaybeModule;
1967
15.3k
  } else 
if (4.60k
ModuleName == getLangOpts().CurrentModule4.60k
) {
1968
    // This is the module we're building.
1969
16
    Module = PP->getHeaderSearchInfo().lookupModule(
1970
16
        ModuleName, ImportLoc, /*AllowSearch*/ true,
1971
16
        /*AllowExtraModuleMapSearch*/ !IsInclusionDirective);
1972
    /// FIXME: perhaps we should (a) look for a module using the module name
1973
    //  to file map (PrebuiltModuleFiles) and (b) diagnose if still not found?
1974
    //if (Module == nullptr) {
1975
    //  getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_found)
1976
    //    << ModuleName;
1977
    //  DisableGeneratingGlobalModuleIndex = true;
1978
    //  return ModuleLoadResult();
1979
    //}
1980
16
    MM.cacheModuleLoad(*Path[0].first, Module);
1981
4.58k
  } else {
1982
4.58k
    ModuleLoadResult Result = findOrCompileModuleAndReadAST(
1983
4.58k
        ModuleName, ImportLoc, ModuleNameLoc, IsInclusionDirective);
1984
4.58k
    if (!Result.isNormal())
1985
8
      return Result;
1986
4.57k
    if (!Result)
1987
89
      DisableGeneratingGlobalModuleIndex = true;
1988
4.57k
    Module = Result;
1989
4.57k
    MM.cacheModuleLoad(*Path[0].first, Module);
1990
4.57k
  }
1991
1992
  // If we never found the module, fail.  Otherwise, verify the module and link
1993
  // it up.
1994
19.9k
  if (!Module)
1995
153
    return ModuleLoadResult();
1996
1997
  // Verify that the rest of the module path actually corresponds to
1998
  // a submodule.
1999
19.7k
  bool MapPrivateSubModToTopLevel = false;
2000
29.6k
  for (unsigned I = 1, N = Path.size(); I != N; 
++I9.84k
) {
2001
9.84k
    StringRef Name = Path[I].first->getName();
2002
9.84k
    clang::Module *Sub = Module->findSubmodule(Name);
2003
2004
    // If the user is requesting Foo.Private and it doesn't exist, try to
2005
    // match Foo_Private and emit a warning asking for the user to write
2006
    // @import Foo_Private instead. FIXME: remove this when existing clients
2007
    // migrate off of Foo.Private syntax.
2008
9.84k
    if (!Sub && 
PP->getLangOpts().ImplicitModules11
&&
Name == "Private"11
&&
2009
9.84k
        
Module == Module->getTopLevelModule()3
) {
2010
3
      SmallString<128> PrivateModule(Module->Name);
2011
3
      PrivateModule.append("_Private");
2012
2013
3
      SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> PrivPath;
2014
3
      auto &II = PP->getIdentifierTable().get(
2015
3
          PrivateModule, PP->getIdentifierInfo(Module->Name)->getTokenID());
2016
3
      PrivPath.push_back(std::make_pair(&II, Path[0].second));
2017
2018
3
      if (PP->getHeaderSearchInfo().lookupModule(PrivateModule, ImportLoc, true,
2019
3
                                                 !IsInclusionDirective))
2020
2
        Sub = loadModule(ImportLoc, PrivPath, Visibility, IsInclusionDirective);
2021
3
      if (Sub) {
2022
2
        MapPrivateSubModToTopLevel = true;
2023
2
        if (!getDiagnostics().isIgnored(
2024
2
                diag::warn_no_priv_submodule_use_toplevel, ImportLoc)) {
2025
1
          getDiagnostics().Report(Path[I].second,
2026
1
                                  diag::warn_no_priv_submodule_use_toplevel)
2027
1
              << Path[I].first << Module->getFullModuleName() << PrivateModule
2028
1
              << SourceRange(Path[0].second, Path[I].second)
2029
1
              << FixItHint::CreateReplacement(SourceRange(Path[0].second),
2030
1
                                              PrivateModule);
2031
1
          getDiagnostics().Report(Sub->DefinitionLoc,
2032
1
                                  diag::note_private_top_level_defined);
2033
1
        }
2034
2
      }
2035
3
    }
2036
2037
9.84k
    if (!Sub) {
2038
      // Attempt to perform typo correction to find a module name that works.
2039
9
      SmallVector<StringRef, 2> Best;
2040
9
      unsigned BestEditDistance = (std::numeric_limits<unsigned>::max)();
2041
2042
10
      for (class Module *SubModule : Module->submodules()) {
2043
10
        unsigned ED =
2044
10
            Name.edit_distance(SubModule->Name,
2045
10
                               /*AllowReplacements=*/true, BestEditDistance);
2046
10
        if (ED <= BestEditDistance) {
2047
8
          if (ED < BestEditDistance) {
2048
6
            Best.clear();
2049
6
            BestEditDistance = ED;
2050
6
          }
2051
2052
8
          Best.push_back(SubModule->Name);
2053
8
        }
2054
10
      }
2055
2056
      // If there was a clear winner, user it.
2057
9
      if (Best.size() == 1) {
2058
2
        getDiagnostics().Report(Path[I].second, diag::err_no_submodule_suggest)
2059
2
            << Path[I].first << Module->getFullModuleName() << Best[0]
2060
2
            << SourceRange(Path[0].second, Path[I - 1].second)
2061
2
            << FixItHint::CreateReplacement(SourceRange(Path[I].second),
2062
2
                                            Best[0]);
2063
2064
2
        Sub = Module->findSubmodule(Best[0]);
2065
2
      }
2066
9
    }
2067
2068
9.84k
    if (!Sub) {
2069
      // No submodule by this name. Complain, and don't look for further
2070
      // submodules.
2071
7
      getDiagnostics().Report(Path[I].second, diag::err_no_submodule)
2072
7
          << Path[I].first << Module->getFullModuleName()
2073
7
          << SourceRange(Path[0].second, Path[I - 1].second);
2074
7
      break;
2075
7
    }
2076
2077
9.84k
    Module = Sub;
2078
9.84k
  }
2079
2080
  // Make the named module visible, if it's not already part of the module
2081
  // we are parsing.
2082
19.7k
  if (ModuleName != getLangOpts().CurrentModule) {
2083
19.7k
    if (!Module->IsFromModuleFile && 
!MapPrivateSubModToTopLevel4
) {
2084
      // We have an umbrella header or directory that doesn't actually include
2085
      // all of the headers within the directory it covers. Complain about
2086
      // this missing submodule and recover by forgetting that we ever saw
2087
      // this submodule.
2088
      // FIXME: Should we detect this at module load time? It seems fairly
2089
      // expensive (and rare).
2090
4
      getDiagnostics().Report(ImportLoc, diag::warn_missing_submodule)
2091
4
        << Module->getFullModuleName()
2092
4
        << SourceRange(Path.front().second, Path.back().second);
2093
2094
4
      return ModuleLoadResult::MissingExpected;
2095
4
    }
2096
2097
    // Check whether this module is available.
2098
19.7k
    if (Preprocessor::checkModuleIsAvailable(getLangOpts(), getTarget(),
2099
19.7k
                                             getDiagnostics(), Module)) {
2100
16
      getDiagnostics().Report(ImportLoc, diag::note_module_import_here)
2101
16
        << SourceRange(Path.front().second, Path.back().second);
2102
16
      LastModuleImportLoc = ImportLoc;
2103
16
      LastModuleImportResult = ModuleLoadResult();
2104
16
      return ModuleLoadResult();
2105
16
    }
2106
2107
19.7k
    TheASTReader->makeModuleVisible(Module, Visibility, ImportLoc);
2108
19.7k
  }
2109
2110
  // Check for any configuration macros that have changed.
2111
19.7k
  clang::Module *TopModule = Module->getTopLevelModule();
2112
19.7k
  for (unsigned I = 0, N = TopModule->ConfigMacros.size(); I != N; 
++I10
) {
2113
10
    checkConfigMacro(getPreprocessor(), TopModule->ConfigMacros[I],
2114
10
                     Module, ImportLoc);
2115
10
  }
2116
2117
  // Resolve any remaining module using export_as for this one.
2118
19.7k
  getPreprocessor()
2119
19.7k
      .getHeaderSearchInfo()
2120
19.7k
      .getModuleMap()
2121
19.7k
      .resolveLinkAsDependencies(TopModule);
2122
2123
19.7k
  LastModuleImportLoc = ImportLoc;
2124
19.7k
  LastModuleImportResult = ModuleLoadResult(Module);
2125
19.7k
  return LastModuleImportResult;
2126
19.7k
}
2127
2128
void CompilerInstance::createModuleFromSource(SourceLocation ImportLoc,
2129
                                              StringRef ModuleName,
2130
75
                                              StringRef Source) {
2131
  // Avoid creating filenames with special characters.
2132
75
  SmallString<128> CleanModuleName(ModuleName);
2133
75
  for (auto &C : CleanModuleName)
2134
213
    if (!isAlphanumeric(C))
2135
10
      C = '_';
2136
2137
  // FIXME: Using a randomized filename here means that our intermediate .pcm
2138
  // output is nondeterministic (as .pcm files refer to each other by name).
2139
  // Can this affect the output in any way?
2140
75
  SmallString<128> ModuleFileName;
2141
75
  if (std::error_code EC = llvm::sys::fs::createTemporaryFile(
2142
75
          CleanModuleName, "pcm", ModuleFileName)) {
2143
0
    getDiagnostics().Report(ImportLoc, diag::err_fe_unable_to_open_output)
2144
0
        << ModuleFileName << EC.message();
2145
0
    return;
2146
0
  }
2147
75
  std::string ModuleMapFileName = (CleanModuleName + ".map").str();
2148
2149
75
  FrontendInputFile Input(
2150
75
      ModuleMapFileName,
2151
75
      InputKind(getLanguageFromOptions(*Invocation->getLangOpts()),
2152
75
                InputKind::ModuleMap, /*Preprocessed*/true));
2153
2154
75
  std::string NullTerminatedSource(Source.str());
2155
2156
75
  auto PreBuildStep = [&](CompilerInstance &Other) {
2157
    // Create a virtual file containing our desired source.
2158
    // FIXME: We shouldn't need to do this.
2159
75
    const FileEntry *ModuleMapFile = Other.getFileManager().getVirtualFile(
2160
75
        ModuleMapFileName, NullTerminatedSource.size(), 0);
2161
75
    Other.getSourceManager().overrideFileContents(
2162
75
        ModuleMapFile, llvm::MemoryBuffer::getMemBuffer(NullTerminatedSource));
2163
2164
75
    Other.BuiltModules = std::move(BuiltModules);
2165
75
    Other.DeleteBuiltModules = false;
2166
75
  };
2167
2168
75
  auto PostBuildStep = [this](CompilerInstance &Other) {
2169
75
    BuiltModules = std::move(Other.BuiltModules);
2170
75
  };
2171
2172
  // Build the module, inheriting any modules that we've built locally.
2173
75
  if (compileModuleImpl(*this, ImportLoc, ModuleName, Input, StringRef(),
2174
75
                        ModuleFileName, PreBuildStep, PostBuildStep)) {
2175
74
    BuiltModules[std::string(ModuleName)] = std::string(ModuleFileName.str());
2176
74
    llvm::sys::RemoveFileOnSignal(ModuleFileName);
2177
74
  }
2178
75
}
2179
2180
void CompilerInstance::makeModuleVisible(Module *Mod,
2181
                                         Module::NameVisibilityKind Visibility,
2182
142k
                                         SourceLocation ImportLoc) {
2183
142k
  if (!TheASTReader)
2184
14
    createASTReader();
2185
142k
  if (!TheASTReader)
2186
0
    return;
2187
2188
142k
  TheASTReader->makeModuleVisible(Mod, Visibility, ImportLoc);
2189
142k
}
2190
2191
GlobalModuleIndex *CompilerInstance::loadGlobalModuleIndex(
2192
1
    SourceLocation TriggerLoc) {
2193
1
  if (getPreprocessor().getHeaderSearchInfo().getModuleCachePath().empty())
2194
0
    return nullptr;
2195
1
  if (!TheASTReader)
2196
0
    createASTReader();
2197
  // Can't do anything if we don't have the module manager.
2198
1
  if (!TheASTReader)
2199
0
    return nullptr;
2200
  // Get an existing global index.  This loads it if not already
2201
  // loaded.
2202
1
  TheASTReader->loadGlobalIndex();
2203
1
  GlobalModuleIndex *GlobalIndex = TheASTReader->getGlobalIndex();
2204
  // If the global index doesn't exist, create it.
2205
1
  if (!GlobalIndex && shouldBuildGlobalModuleIndex() && hasFileManager() &&
2206
1
      hasPreprocessor()) {
2207
1
    llvm::sys::fs::create_directories(
2208
1
      getPreprocessor().getHeaderSearchInfo().getModuleCachePath());
2209
1
    if (llvm::Error Err = GlobalModuleIndex::writeIndex(
2210
1
            getFileManager(), getPCHContainerReader(),
2211
1
            getPreprocessor().getHeaderSearchInfo().getModuleCachePath())) {
2212
      // FIXME this drops the error on the floor. This code is only used for
2213
      // typo correction and drops more than just this one source of errors
2214
      // (such as the directory creation failure above). It should handle the
2215
      // error.
2216
0
      consumeError(std::move(Err));
2217
0
      return nullptr;
2218
0
    }
2219
1
    TheASTReader->resetForReload();
2220
1
    TheASTReader->loadGlobalIndex();
2221
1
    GlobalIndex = TheASTReader->getGlobalIndex();
2222
1
  }
2223
  // For finding modules needing to be imported for fixit messages,
2224
  // we need to make the global index cover all modules, so we do that here.
2225
1
  if (!HaveFullGlobalModuleIndex && GlobalIndex && !buildingModule()) {
2226
1
    ModuleMap &MMap = getPreprocessor().getHeaderSearchInfo().getModuleMap();
2227
1
    bool RecreateIndex = false;
2228
1
    for (ModuleMap::module_iterator I = MMap.module_begin(),
2229
3
        E = MMap.module_end(); I != E; 
++I2
) {
2230
2
      Module *TheModule = I->second;
2231
2
      const FileEntry *Entry = TheModule->getASTFile();
2232
2
      if (!Entry) {
2233
1
        SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
2234
1
        Path.push_back(std::make_pair(
2235
1
            getPreprocessor().getIdentifierInfo(TheModule->Name), TriggerLoc));
2236
1
        std::reverse(Path.begin(), Path.end());
2237
        // Load a module as hidden.  This also adds it to the global index.
2238
1
        loadModule(TheModule->DefinitionLoc, Path, Module::Hidden, false);
2239
1
        RecreateIndex = true;
2240
1
      }
2241
2
    }
2242
1
    if (RecreateIndex) {
2243
1
      if (llvm::Error Err = GlobalModuleIndex::writeIndex(
2244
1
              getFileManager(), getPCHContainerReader(),
2245
1
              getPreprocessor().getHeaderSearchInfo().getModuleCachePath())) {
2246
        // FIXME As above, this drops the error on the floor.
2247
0
        consumeError(std::move(Err));
2248
0
        return nullptr;
2249
0
      }
2250
1
      TheASTReader->resetForReload();
2251
1
      TheASTReader->loadGlobalIndex();
2252
1
      GlobalIndex = TheASTReader->getGlobalIndex();
2253
1
    }
2254
1
    HaveFullGlobalModuleIndex = true;
2255
1
  }
2256
1
  return GlobalIndex;
2257
1
}
2258
2259
// Check global module index for missing imports.
2260
bool
2261
CompilerInstance::lookupMissingImports(StringRef Name,
2262
19
                                       SourceLocation TriggerLoc) {
2263
  // Look for the symbol in non-imported modules, but only if an error
2264
  // actually occurred.
2265
19
  if (!buildingModule()) {
2266
    // Load global module index, or retrieve a previously loaded one.
2267
1
    GlobalModuleIndex *GlobalIndex = loadGlobalModuleIndex(
2268
1
      TriggerLoc);
2269
2270
    // Only if we have a global index.
2271
1
    if (GlobalIndex) {
2272
1
      GlobalModuleIndex::HitSet FoundModules;
2273
2274
      // Find the modules that reference the identifier.
2275
      // Note that this only finds top-level modules.
2276
      // We'll let diagnoseTypo find the actual declaration module.
2277
1
      if (GlobalIndex->lookupIdentifier(Name, FoundModules))
2278
1
        return true;
2279
1
    }
2280
1
  }
2281
2282
18
  return false;
2283
19
}
2284
6.08k
void CompilerInstance::resetAndLeakSema() { llvm::BuryPointer(takeSema()); }
2285
2286
void CompilerInstance::setExternalSemaSource(
2287
1
    IntrusiveRefCntPtr<ExternalSemaSource> ESS) {
2288
1
  ExternalSemaSrc = std::move(ESS);
2289
1
}