Coverage Report

Created: 2019-07-24 05:18

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