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