Coverage Report

Created: 2021-09-21 08:58

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