Coverage Report

Created: 2020-02-25 14:32

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