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