Coverage Report

Created: 2020-09-15 12:33

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