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