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