10b57cec5SDimitry Andric //===--- CompilerInstance.cpp ---------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "clang/Frontend/CompilerInstance.h"
100b57cec5SDimitry Andric #include "clang/AST/ASTConsumer.h"
110b57cec5SDimitry Andric #include "clang/AST/ASTContext.h"
120b57cec5SDimitry Andric #include "clang/AST/Decl.h"
130b57cec5SDimitry Andric #include "clang/Basic/CharInfo.h"
140b57cec5SDimitry Andric #include "clang/Basic/Diagnostic.h"
15bdd1243dSDimitry Andric #include "clang/Basic/DiagnosticOptions.h"
160b57cec5SDimitry Andric #include "clang/Basic/FileManager.h"
17a7dea167SDimitry Andric #include "clang/Basic/LangStandard.h"
180b57cec5SDimitry Andric #include "clang/Basic/SourceManager.h"
190b57cec5SDimitry Andric #include "clang/Basic/Stack.h"
200b57cec5SDimitry Andric #include "clang/Basic/TargetInfo.h"
210b57cec5SDimitry Andric #include "clang/Basic/Version.h"
220b57cec5SDimitry Andric #include "clang/Config/config.h"
230b57cec5SDimitry Andric #include "clang/Frontend/ChainedDiagnosticConsumer.h"
240b57cec5SDimitry Andric #include "clang/Frontend/FrontendAction.h"
250b57cec5SDimitry Andric #include "clang/Frontend/FrontendActions.h"
260b57cec5SDimitry Andric #include "clang/Frontend/FrontendDiagnostic.h"
27349cc55cSDimitry Andric #include "clang/Frontend/FrontendPluginRegistry.h"
280b57cec5SDimitry Andric #include "clang/Frontend/LogDiagnosticPrinter.h"
29bdd1243dSDimitry Andric #include "clang/Frontend/SARIFDiagnosticPrinter.h"
300b57cec5SDimitry Andric #include "clang/Frontend/SerializedDiagnosticPrinter.h"
310b57cec5SDimitry Andric #include "clang/Frontend/TextDiagnosticPrinter.h"
320b57cec5SDimitry Andric #include "clang/Frontend/Utils.h"
330b57cec5SDimitry Andric #include "clang/Frontend/VerifyDiagnosticConsumer.h"
340b57cec5SDimitry Andric #include "clang/Lex/HeaderSearch.h"
350b57cec5SDimitry Andric #include "clang/Lex/Preprocessor.h"
360b57cec5SDimitry Andric #include "clang/Lex/PreprocessorOptions.h"
370b57cec5SDimitry Andric #include "clang/Sema/CodeCompleteConsumer.h"
380b57cec5SDimitry Andric #include "clang/Sema/Sema.h"
390b57cec5SDimitry Andric #include "clang/Serialization/ASTReader.h"
400b57cec5SDimitry Andric #include "clang/Serialization/GlobalModuleIndex.h"
410b57cec5SDimitry Andric #include "clang/Serialization/InMemoryModuleCache.h"
425f757f3fSDimitry Andric #include "llvm/ADT/STLExtras.h"
4304eeddc0SDimitry Andric #include "llvm/ADT/ScopeExit.h"
440b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
45bdd1243dSDimitry Andric #include "llvm/Config/llvm-config.h"
460b57cec5SDimitry Andric #include "llvm/Support/BuryPointer.h"
470b57cec5SDimitry Andric #include "llvm/Support/CrashRecoveryContext.h"
480b57cec5SDimitry Andric #include "llvm/Support/Errc.h"
490b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
500b57cec5SDimitry Andric #include "llvm/Support/LockFileManager.h"
510b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
520b57cec5SDimitry Andric #include "llvm/Support/Path.h"
530b57cec5SDimitry Andric #include "llvm/Support/Program.h"
540b57cec5SDimitry Andric #include "llvm/Support/Signals.h"
550b57cec5SDimitry Andric #include "llvm/Support/TimeProfiler.h"
560b57cec5SDimitry Andric #include "llvm/Support/Timer.h"
570b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
5806c3fb27SDimitry Andric #include "llvm/TargetParser/Host.h"
59bdd1243dSDimitry Andric #include <optional>
600b57cec5SDimitry Andric #include <time.h>
610b57cec5SDimitry Andric #include <utility>
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric using namespace clang;
640b57cec5SDimitry Andric 
CompilerInstance(std::shared_ptr<PCHContainerOperations> PCHContainerOps,InMemoryModuleCache * SharedModuleCache)650b57cec5SDimitry Andric CompilerInstance::CompilerInstance(
660b57cec5SDimitry Andric     std::shared_ptr<PCHContainerOperations> PCHContainerOps,
670b57cec5SDimitry Andric     InMemoryModuleCache *SharedModuleCache)
680b57cec5SDimitry Andric     : ModuleLoader(/* BuildingModule = */ SharedModuleCache),
690b57cec5SDimitry Andric       Invocation(new CompilerInvocation()),
700b57cec5SDimitry Andric       ModuleCache(SharedModuleCache ? SharedModuleCache
710b57cec5SDimitry Andric                                     : new InMemoryModuleCache),
720b57cec5SDimitry Andric       ThePCHContainerOperations(std::move(PCHContainerOps)) {}
730b57cec5SDimitry Andric 
~CompilerInstance()740b57cec5SDimitry Andric CompilerInstance::~CompilerInstance() {
750b57cec5SDimitry Andric   assert(OutputFiles.empty() && "Still output files in flight?");
760b57cec5SDimitry Andric }
770b57cec5SDimitry Andric 
setInvocation(std::shared_ptr<CompilerInvocation> Value)780b57cec5SDimitry Andric void CompilerInstance::setInvocation(
790b57cec5SDimitry Andric     std::shared_ptr<CompilerInvocation> Value) {
800b57cec5SDimitry Andric   Invocation = std::move(Value);
810b57cec5SDimitry Andric }
820b57cec5SDimitry Andric 
shouldBuildGlobalModuleIndex() const830b57cec5SDimitry Andric bool CompilerInstance::shouldBuildGlobalModuleIndex() const {
840b57cec5SDimitry Andric   return (BuildGlobalModuleIndex ||
85480093f4SDimitry Andric           (TheASTReader && TheASTReader->isGlobalIndexUnavailable() &&
860b57cec5SDimitry Andric            getFrontendOpts().GenerateGlobalModuleIndex)) &&
87fe6060f1SDimitry Andric          !DisableGeneratingGlobalModuleIndex;
880b57cec5SDimitry Andric }
890b57cec5SDimitry Andric 
setDiagnostics(DiagnosticsEngine * Value)900b57cec5SDimitry Andric void CompilerInstance::setDiagnostics(DiagnosticsEngine *Value) {
910b57cec5SDimitry Andric   Diagnostics = Value;
920b57cec5SDimitry Andric }
930b57cec5SDimitry Andric 
setVerboseOutputStream(raw_ostream & Value)94a7dea167SDimitry Andric void CompilerInstance::setVerboseOutputStream(raw_ostream &Value) {
95e8d8bef9SDimitry Andric   OwnedVerboseOutputStream.reset();
96a7dea167SDimitry Andric   VerboseOutputStream = &Value;
97a7dea167SDimitry Andric }
98a7dea167SDimitry Andric 
setVerboseOutputStream(std::unique_ptr<raw_ostream> Value)99a7dea167SDimitry Andric void CompilerInstance::setVerboseOutputStream(std::unique_ptr<raw_ostream> Value) {
100a7dea167SDimitry Andric   OwnedVerboseOutputStream.swap(Value);
101a7dea167SDimitry Andric   VerboseOutputStream = OwnedVerboseOutputStream.get();
102a7dea167SDimitry Andric }
103a7dea167SDimitry Andric 
setTarget(TargetInfo * Value)1040b57cec5SDimitry Andric void CompilerInstance::setTarget(TargetInfo *Value) { Target = Value; }
setAuxTarget(TargetInfo * Value)1050b57cec5SDimitry Andric void CompilerInstance::setAuxTarget(TargetInfo *Value) { AuxTarget = Value; }
1060b57cec5SDimitry Andric 
createTarget()107fe6060f1SDimitry Andric bool CompilerInstance::createTarget() {
108fe6060f1SDimitry Andric   // Create the target instance.
109fe6060f1SDimitry Andric   setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(),
110fe6060f1SDimitry Andric                                          getInvocation().TargetOpts));
111fe6060f1SDimitry Andric   if (!hasTarget())
112fe6060f1SDimitry Andric     return false;
113fe6060f1SDimitry Andric 
114fe6060f1SDimitry Andric   // Check whether AuxTarget exists, if not, then create TargetInfo for the
115fe6060f1SDimitry Andric   // other side of CUDA/OpenMP/SYCL compilation.
116fe6060f1SDimitry Andric   if (!getAuxTarget() &&
11706c3fb27SDimitry Andric       (getLangOpts().CUDA || getLangOpts().OpenMPIsTargetDevice ||
118fe6060f1SDimitry Andric        getLangOpts().SYCLIsDevice) &&
119fe6060f1SDimitry Andric       !getFrontendOpts().AuxTriple.empty()) {
120fe6060f1SDimitry Andric     auto TO = std::make_shared<TargetOptions>();
121fe6060f1SDimitry Andric     TO->Triple = llvm::Triple::normalize(getFrontendOpts().AuxTriple);
122fe6060f1SDimitry Andric     if (getFrontendOpts().AuxTargetCPU)
123bdd1243dSDimitry Andric       TO->CPU = *getFrontendOpts().AuxTargetCPU;
124fe6060f1SDimitry Andric     if (getFrontendOpts().AuxTargetFeatures)
125bdd1243dSDimitry Andric       TO->FeaturesAsWritten = *getFrontendOpts().AuxTargetFeatures;
126fe6060f1SDimitry Andric     TO->HostTriple = getTarget().getTriple().str();
127fe6060f1SDimitry Andric     setAuxTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), TO));
128fe6060f1SDimitry Andric   }
129fe6060f1SDimitry Andric 
130fe6060f1SDimitry Andric   if (!getTarget().hasStrictFP() && !getLangOpts().ExpStrictFP) {
13181ad6265SDimitry Andric     if (getLangOpts().RoundingMath) {
132fe6060f1SDimitry Andric       getDiagnostics().Report(diag::warn_fe_backend_unsupported_fp_rounding);
13381ad6265SDimitry Andric       getLangOpts().RoundingMath = false;
134fe6060f1SDimitry Andric     }
13581ad6265SDimitry Andric     auto FPExc = getLangOpts().getFPExceptionMode();
13681ad6265SDimitry Andric     if (FPExc != LangOptions::FPE_Default && FPExc != LangOptions::FPE_Ignore) {
137fe6060f1SDimitry Andric       getDiagnostics().Report(diag::warn_fe_backend_unsupported_fp_exceptions);
138fe6060f1SDimitry Andric       getLangOpts().setFPExceptionMode(LangOptions::FPE_Ignore);
139fe6060f1SDimitry Andric     }
140fe6060f1SDimitry Andric     // FIXME: can we disable FEnvAccess?
141fe6060f1SDimitry Andric   }
142fe6060f1SDimitry Andric 
143fe6060f1SDimitry Andric   // We should do it here because target knows nothing about
144fe6060f1SDimitry Andric   // language options when it's being created.
145fe6060f1SDimitry Andric   if (getLangOpts().OpenCL &&
146fe6060f1SDimitry Andric       !getTarget().validateOpenCLTarget(getLangOpts(), getDiagnostics()))
147fe6060f1SDimitry Andric     return false;
148fe6060f1SDimitry Andric 
149fe6060f1SDimitry Andric   // Inform the target of the language options.
150fe6060f1SDimitry Andric   // FIXME: We shouldn't need to do this, the target should be immutable once
151fe6060f1SDimitry Andric   // created. This complexity should be lifted elsewhere.
152fe6060f1SDimitry Andric   getTarget().adjust(getDiagnostics(), getLangOpts());
153fe6060f1SDimitry Andric 
154fe6060f1SDimitry Andric   if (auto *Aux = getAuxTarget())
155fe6060f1SDimitry Andric     getTarget().setAuxTarget(Aux);
156fe6060f1SDimitry Andric 
157fe6060f1SDimitry Andric   return true;
158fe6060f1SDimitry Andric }
159fe6060f1SDimitry Andric 
getVirtualFileSystem() const1605ffd83dbSDimitry Andric llvm::vfs::FileSystem &CompilerInstance::getVirtualFileSystem() const {
1615ffd83dbSDimitry Andric   return getFileManager().getVirtualFileSystem();
1625ffd83dbSDimitry Andric }
1635ffd83dbSDimitry Andric 
setFileManager(FileManager * Value)1640b57cec5SDimitry Andric void CompilerInstance::setFileManager(FileManager *Value) {
1650b57cec5SDimitry Andric   FileMgr = Value;
1660b57cec5SDimitry Andric }
1670b57cec5SDimitry Andric 
setSourceManager(SourceManager * Value)1680b57cec5SDimitry Andric void CompilerInstance::setSourceManager(SourceManager *Value) {
1690b57cec5SDimitry Andric   SourceMgr = Value;
1700b57cec5SDimitry Andric }
1710b57cec5SDimitry Andric 
setPreprocessor(std::shared_ptr<Preprocessor> Value)1720b57cec5SDimitry Andric void CompilerInstance::setPreprocessor(std::shared_ptr<Preprocessor> Value) {
1730b57cec5SDimitry Andric   PP = std::move(Value);
1740b57cec5SDimitry Andric }
1750b57cec5SDimitry Andric 
setASTContext(ASTContext * Value)1760b57cec5SDimitry Andric void CompilerInstance::setASTContext(ASTContext *Value) {
1770b57cec5SDimitry Andric   Context = Value;
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric   if (Context && Consumer)
1800b57cec5SDimitry Andric     getASTConsumer().Initialize(getASTContext());
1810b57cec5SDimitry Andric }
1820b57cec5SDimitry Andric 
setSema(Sema * S)1830b57cec5SDimitry Andric void CompilerInstance::setSema(Sema *S) {
1840b57cec5SDimitry Andric   TheSema.reset(S);
1850b57cec5SDimitry Andric }
1860b57cec5SDimitry Andric 
setASTConsumer(std::unique_ptr<ASTConsumer> Value)1870b57cec5SDimitry Andric void CompilerInstance::setASTConsumer(std::unique_ptr<ASTConsumer> Value) {
1880b57cec5SDimitry Andric   Consumer = std::move(Value);
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric   if (Context && Consumer)
1910b57cec5SDimitry Andric     getASTConsumer().Initialize(getASTContext());
1920b57cec5SDimitry Andric }
1930b57cec5SDimitry Andric 
setCodeCompletionConsumer(CodeCompleteConsumer * Value)1940b57cec5SDimitry Andric void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) {
1950b57cec5SDimitry Andric   CompletionConsumer.reset(Value);
1960b57cec5SDimitry Andric }
1970b57cec5SDimitry Andric 
takeSema()1980b57cec5SDimitry Andric std::unique_ptr<Sema> CompilerInstance::takeSema() {
1990b57cec5SDimitry Andric   return std::move(TheSema);
2000b57cec5SDimitry Andric }
2010b57cec5SDimitry Andric 
getASTReader() const202480093f4SDimitry Andric IntrusiveRefCntPtr<ASTReader> CompilerInstance::getASTReader() const {
203480093f4SDimitry Andric   return TheASTReader;
2040b57cec5SDimitry Andric }
setASTReader(IntrusiveRefCntPtr<ASTReader> Reader)2055ffd83dbSDimitry Andric void CompilerInstance::setASTReader(IntrusiveRefCntPtr<ASTReader> Reader) {
2060b57cec5SDimitry Andric   assert(ModuleCache.get() == &Reader->getModuleManager().getModuleCache() &&
2070b57cec5SDimitry Andric          "Expected ASTReader to use the same PCM cache");
208480093f4SDimitry Andric   TheASTReader = std::move(Reader);
2090b57cec5SDimitry Andric }
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric std::shared_ptr<ModuleDependencyCollector>
getModuleDepCollector() const2120b57cec5SDimitry Andric CompilerInstance::getModuleDepCollector() const {
2130b57cec5SDimitry Andric   return ModuleDepCollector;
2140b57cec5SDimitry Andric }
2150b57cec5SDimitry Andric 
setModuleDepCollector(std::shared_ptr<ModuleDependencyCollector> Collector)2160b57cec5SDimitry Andric void CompilerInstance::setModuleDepCollector(
2170b57cec5SDimitry Andric     std::shared_ptr<ModuleDependencyCollector> Collector) {
2180b57cec5SDimitry Andric   ModuleDepCollector = std::move(Collector);
2190b57cec5SDimitry Andric }
2200b57cec5SDimitry Andric 
collectHeaderMaps(const HeaderSearch & HS,std::shared_ptr<ModuleDependencyCollector> MDC)2210b57cec5SDimitry Andric static void collectHeaderMaps(const HeaderSearch &HS,
2220b57cec5SDimitry Andric                               std::shared_ptr<ModuleDependencyCollector> MDC) {
2230b57cec5SDimitry Andric   SmallVector<std::string, 4> HeaderMapFileNames;
2240b57cec5SDimitry Andric   HS.getHeaderMapFileNames(HeaderMapFileNames);
2250b57cec5SDimitry Andric   for (auto &Name : HeaderMapFileNames)
2260b57cec5SDimitry Andric     MDC->addFile(Name);
2270b57cec5SDimitry Andric }
2280b57cec5SDimitry Andric 
collectIncludePCH(CompilerInstance & CI,std::shared_ptr<ModuleDependencyCollector> MDC)2290b57cec5SDimitry Andric static void collectIncludePCH(CompilerInstance &CI,
2300b57cec5SDimitry Andric                               std::shared_ptr<ModuleDependencyCollector> MDC) {
2310b57cec5SDimitry Andric   const PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
2320b57cec5SDimitry Andric   if (PPOpts.ImplicitPCHInclude.empty())
2330b57cec5SDimitry Andric     return;
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric   StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
2360b57cec5SDimitry Andric   FileManager &FileMgr = CI.getFileManager();
23781ad6265SDimitry Andric   auto PCHDir = FileMgr.getOptionalDirectoryRef(PCHInclude);
2380b57cec5SDimitry Andric   if (!PCHDir) {
2390b57cec5SDimitry Andric     MDC->addFile(PCHInclude);
2400b57cec5SDimitry Andric     return;
2410b57cec5SDimitry Andric   }
2420b57cec5SDimitry Andric 
2430b57cec5SDimitry Andric   std::error_code EC;
2440b57cec5SDimitry Andric   SmallString<128> DirNative;
24581ad6265SDimitry Andric   llvm::sys::path::native(PCHDir->getName(), DirNative);
2460b57cec5SDimitry Andric   llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
2470b57cec5SDimitry Andric   SimpleASTReaderListener Validator(CI.getPreprocessor());
2480b57cec5SDimitry Andric   for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd;
2490b57cec5SDimitry Andric        Dir != DirEnd && !EC; Dir.increment(EC)) {
2500b57cec5SDimitry Andric     // Check whether this is an AST file. ASTReader::isAcceptableASTFile is not
2510b57cec5SDimitry Andric     // used here since we're not interested in validating the PCH at this time,
2520b57cec5SDimitry Andric     // but only to check whether this is a file containing an AST.
2530b57cec5SDimitry Andric     if (!ASTReader::readASTFileControlBlock(
254bdd1243dSDimitry Andric             Dir->path(), FileMgr, CI.getModuleCache(),
255bdd1243dSDimitry Andric             CI.getPCHContainerReader(),
2560b57cec5SDimitry Andric             /*FindModuleFileExtensions=*/false, Validator,
2570b57cec5SDimitry Andric             /*ValidateDiagnosticOptions=*/false))
2580b57cec5SDimitry Andric       MDC->addFile(Dir->path());
2590b57cec5SDimitry Andric   }
2600b57cec5SDimitry Andric }
2610b57cec5SDimitry Andric 
collectVFSEntries(CompilerInstance & CI,std::shared_ptr<ModuleDependencyCollector> MDC)2620b57cec5SDimitry Andric static void collectVFSEntries(CompilerInstance &CI,
2630b57cec5SDimitry Andric                               std::shared_ptr<ModuleDependencyCollector> MDC) {
2640b57cec5SDimitry Andric   if (CI.getHeaderSearchOpts().VFSOverlayFiles.empty())
2650b57cec5SDimitry Andric     return;
2660b57cec5SDimitry Andric 
2670b57cec5SDimitry Andric   // Collect all VFS found.
2680b57cec5SDimitry Andric   SmallVector<llvm::vfs::YAMLVFSEntry, 16> VFSEntries;
2690b57cec5SDimitry Andric   for (const std::string &VFSFile : CI.getHeaderSearchOpts().VFSOverlayFiles) {
2700b57cec5SDimitry Andric     llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buffer =
2710b57cec5SDimitry Andric         llvm::MemoryBuffer::getFile(VFSFile);
2720b57cec5SDimitry Andric     if (!Buffer)
2730b57cec5SDimitry Andric       return;
2740b57cec5SDimitry Andric     llvm::vfs::collectVFSFromYAML(std::move(Buffer.get()),
2750b57cec5SDimitry Andric                                   /*DiagHandler*/ nullptr, VFSFile, VFSEntries);
2760b57cec5SDimitry Andric   }
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric   for (auto &E : VFSEntries)
2790b57cec5SDimitry Andric     MDC->addFile(E.VPath, E.RPath);
2800b57cec5SDimitry Andric }
2810b57cec5SDimitry Andric 
2820b57cec5SDimitry Andric // Diagnostics
SetUpDiagnosticLog(DiagnosticOptions * DiagOpts,const CodeGenOptions * CodeGenOpts,DiagnosticsEngine & Diags)2830b57cec5SDimitry Andric static void SetUpDiagnosticLog(DiagnosticOptions *DiagOpts,
2840b57cec5SDimitry Andric                                const CodeGenOptions *CodeGenOpts,
2850b57cec5SDimitry Andric                                DiagnosticsEngine &Diags) {
2860b57cec5SDimitry Andric   std::error_code EC;
2870b57cec5SDimitry Andric   std::unique_ptr<raw_ostream> StreamOwner;
2880b57cec5SDimitry Andric   raw_ostream *OS = &llvm::errs();
2890b57cec5SDimitry Andric   if (DiagOpts->DiagnosticLogFile != "-") {
2900b57cec5SDimitry Andric     // Create the output stream.
291a7dea167SDimitry Andric     auto FileOS = std::make_unique<llvm::raw_fd_ostream>(
2920b57cec5SDimitry Andric         DiagOpts->DiagnosticLogFile, EC,
293fe6060f1SDimitry Andric         llvm::sys::fs::OF_Append | llvm::sys::fs::OF_TextWithCRLF);
2940b57cec5SDimitry Andric     if (EC) {
2950b57cec5SDimitry Andric       Diags.Report(diag::warn_fe_cc_log_diagnostics_failure)
2960b57cec5SDimitry Andric           << DiagOpts->DiagnosticLogFile << EC.message();
2970b57cec5SDimitry Andric     } else {
2980b57cec5SDimitry Andric       FileOS->SetUnbuffered();
2990b57cec5SDimitry Andric       OS = FileOS.get();
3000b57cec5SDimitry Andric       StreamOwner = std::move(FileOS);
3010b57cec5SDimitry Andric     }
3020b57cec5SDimitry Andric   }
3030b57cec5SDimitry Andric 
3040b57cec5SDimitry Andric   // Chain in the diagnostic client which will log the diagnostics.
305a7dea167SDimitry Andric   auto Logger = std::make_unique<LogDiagnosticPrinter>(*OS, DiagOpts,
3060b57cec5SDimitry Andric                                                         std::move(StreamOwner));
3070b57cec5SDimitry Andric   if (CodeGenOpts)
3080b57cec5SDimitry Andric     Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags);
3090b57cec5SDimitry Andric   if (Diags.ownsClient()) {
3100b57cec5SDimitry Andric     Diags.setClient(
3110b57cec5SDimitry Andric         new ChainedDiagnosticConsumer(Diags.takeClient(), std::move(Logger)));
3120b57cec5SDimitry Andric   } else {
3130b57cec5SDimitry Andric     Diags.setClient(
3140b57cec5SDimitry Andric         new ChainedDiagnosticConsumer(Diags.getClient(), std::move(Logger)));
3150b57cec5SDimitry Andric   }
3160b57cec5SDimitry Andric }
3170b57cec5SDimitry Andric 
SetupSerializedDiagnostics(DiagnosticOptions * DiagOpts,DiagnosticsEngine & Diags,StringRef OutputFile)3180b57cec5SDimitry Andric static void SetupSerializedDiagnostics(DiagnosticOptions *DiagOpts,
3190b57cec5SDimitry Andric                                        DiagnosticsEngine &Diags,
3200b57cec5SDimitry Andric                                        StringRef OutputFile) {
3210b57cec5SDimitry Andric   auto SerializedConsumer =
3220b57cec5SDimitry Andric       clang::serialized_diags::create(OutputFile, DiagOpts);
3230b57cec5SDimitry Andric 
3240b57cec5SDimitry Andric   if (Diags.ownsClient()) {
3250b57cec5SDimitry Andric     Diags.setClient(new ChainedDiagnosticConsumer(
3260b57cec5SDimitry Andric         Diags.takeClient(), std::move(SerializedConsumer)));
3270b57cec5SDimitry Andric   } else {
3280b57cec5SDimitry Andric     Diags.setClient(new ChainedDiagnosticConsumer(
3290b57cec5SDimitry Andric         Diags.getClient(), std::move(SerializedConsumer)));
3300b57cec5SDimitry Andric   }
3310b57cec5SDimitry Andric }
3320b57cec5SDimitry Andric 
createDiagnostics(DiagnosticConsumer * Client,bool ShouldOwnClient)3330b57cec5SDimitry Andric void CompilerInstance::createDiagnostics(DiagnosticConsumer *Client,
3340b57cec5SDimitry Andric                                          bool ShouldOwnClient) {
3350b57cec5SDimitry Andric   Diagnostics = createDiagnostics(&getDiagnosticOpts(), Client,
3360b57cec5SDimitry Andric                                   ShouldOwnClient, &getCodeGenOpts());
3370b57cec5SDimitry Andric }
3380b57cec5SDimitry Andric 
3390b57cec5SDimitry Andric IntrusiveRefCntPtr<DiagnosticsEngine>
createDiagnostics(DiagnosticOptions * Opts,DiagnosticConsumer * Client,bool ShouldOwnClient,const CodeGenOptions * CodeGenOpts)3400b57cec5SDimitry Andric CompilerInstance::createDiagnostics(DiagnosticOptions *Opts,
3410b57cec5SDimitry Andric                                     DiagnosticConsumer *Client,
3420b57cec5SDimitry Andric                                     bool ShouldOwnClient,
3430b57cec5SDimitry Andric                                     const CodeGenOptions *CodeGenOpts) {
3440b57cec5SDimitry Andric   IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
3450b57cec5SDimitry Andric   IntrusiveRefCntPtr<DiagnosticsEngine>
3460b57cec5SDimitry Andric       Diags(new DiagnosticsEngine(DiagID, Opts));
3470b57cec5SDimitry Andric 
3480b57cec5SDimitry Andric   // Create the diagnostic client for reporting errors or for
3490b57cec5SDimitry Andric   // implementing -verify.
3500b57cec5SDimitry Andric   if (Client) {
3510b57cec5SDimitry Andric     Diags->setClient(Client, ShouldOwnClient);
352bdd1243dSDimitry Andric   } else if (Opts->getFormat() == DiagnosticOptions::SARIF) {
353bdd1243dSDimitry Andric     Diags->setClient(new SARIFDiagnosticPrinter(llvm::errs(), Opts));
3540b57cec5SDimitry Andric   } else
3550b57cec5SDimitry Andric     Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts));
3560b57cec5SDimitry Andric 
3570b57cec5SDimitry Andric   // Chain in -verify checker, if requested.
3580b57cec5SDimitry Andric   if (Opts->VerifyDiagnostics)
3590b57cec5SDimitry Andric     Diags->setClient(new VerifyDiagnosticConsumer(*Diags));
3600b57cec5SDimitry Andric 
3610b57cec5SDimitry Andric   // Chain in -diagnostic-log-file dumper, if requested.
3620b57cec5SDimitry Andric   if (!Opts->DiagnosticLogFile.empty())
3630b57cec5SDimitry Andric     SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags);
3640b57cec5SDimitry Andric 
3650b57cec5SDimitry Andric   if (!Opts->DiagnosticSerializationFile.empty())
3660b57cec5SDimitry Andric     SetupSerializedDiagnostics(Opts, *Diags,
3670b57cec5SDimitry Andric                                Opts->DiagnosticSerializationFile);
3680b57cec5SDimitry Andric 
3690b57cec5SDimitry Andric   // Configure our handling of diagnostics.
3700b57cec5SDimitry Andric   ProcessWarningOptions(*Diags, *Opts);
3710b57cec5SDimitry Andric 
3720b57cec5SDimitry Andric   return Diags;
3730b57cec5SDimitry Andric }
3740b57cec5SDimitry Andric 
3750b57cec5SDimitry Andric // File Manager
3760b57cec5SDimitry Andric 
createFileManager(IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS)3770b57cec5SDimitry Andric FileManager *CompilerInstance::createFileManager(
3780b57cec5SDimitry Andric     IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
3790b57cec5SDimitry Andric   if (!VFS)
3800b57cec5SDimitry Andric     VFS = FileMgr ? &FileMgr->getVirtualFileSystem()
3810b57cec5SDimitry Andric                   : createVFSFromCompilerInvocation(getInvocation(),
3820b57cec5SDimitry Andric                                                     getDiagnostics());
3830b57cec5SDimitry Andric   assert(VFS && "FileManager has no VFS?");
3840b57cec5SDimitry Andric   FileMgr = new FileManager(getFileSystemOpts(), std::move(VFS));
3850b57cec5SDimitry Andric   return FileMgr.get();
3860b57cec5SDimitry Andric }
3870b57cec5SDimitry Andric 
3880b57cec5SDimitry Andric // Source Manager
3890b57cec5SDimitry Andric 
createSourceManager(FileManager & FileMgr)3900b57cec5SDimitry Andric void CompilerInstance::createSourceManager(FileManager &FileMgr) {
3910b57cec5SDimitry Andric   SourceMgr = new SourceManager(getDiagnostics(), FileMgr);
3920b57cec5SDimitry Andric }
3930b57cec5SDimitry Andric 
3940b57cec5SDimitry Andric // Initialize the remapping of files to alternative contents, e.g.,
3950b57cec5SDimitry Andric // those specified through other files.
InitializeFileRemapping(DiagnosticsEngine & Diags,SourceManager & SourceMgr,FileManager & FileMgr,const PreprocessorOptions & InitOpts)3960b57cec5SDimitry Andric static void InitializeFileRemapping(DiagnosticsEngine &Diags,
3970b57cec5SDimitry Andric                                     SourceManager &SourceMgr,
3980b57cec5SDimitry Andric                                     FileManager &FileMgr,
3990b57cec5SDimitry Andric                                     const PreprocessorOptions &InitOpts) {
4000b57cec5SDimitry Andric   // Remap files in the source manager (with buffers).
4010b57cec5SDimitry Andric   for (const auto &RB : InitOpts.RemappedFileBuffers) {
4020b57cec5SDimitry Andric     // Create the file entry for the file that we're mapping from.
4035f757f3fSDimitry Andric     FileEntryRef FromFile =
4045f757f3fSDimitry Andric         FileMgr.getVirtualFileRef(RB.first, RB.second->getBufferSize(), 0);
4050b57cec5SDimitry Andric 
406e8d8bef9SDimitry Andric     // Override the contents of the "from" file with the contents of the
407e8d8bef9SDimitry Andric     // "to" file. If the caller owns the buffers, then pass a MemoryBufferRef;
408e8d8bef9SDimitry Andric     // otherwise, pass as a std::unique_ptr<MemoryBuffer> to transfer ownership
409e8d8bef9SDimitry Andric     // to the SourceManager.
410e8d8bef9SDimitry Andric     if (InitOpts.RetainRemappedFileBuffers)
411e8d8bef9SDimitry Andric       SourceMgr.overrideFileContents(FromFile, RB.second->getMemBufferRef());
412e8d8bef9SDimitry Andric     else
413e8d8bef9SDimitry Andric       SourceMgr.overrideFileContents(
414e8d8bef9SDimitry Andric           FromFile, std::unique_ptr<llvm::MemoryBuffer>(
415e8d8bef9SDimitry Andric                         const_cast<llvm::MemoryBuffer *>(RB.second)));
4160b57cec5SDimitry Andric   }
4170b57cec5SDimitry Andric 
4180b57cec5SDimitry Andric   // Remap files in the source manager (with other files).
4190b57cec5SDimitry Andric   for (const auto &RF : InitOpts.RemappedFiles) {
4200b57cec5SDimitry Andric     // Find the file that we're mapping to.
421bdd1243dSDimitry Andric     OptionalFileEntryRef ToFile = FileMgr.getOptionalFileRef(RF.second);
4220b57cec5SDimitry Andric     if (!ToFile) {
4230b57cec5SDimitry Andric       Diags.Report(diag::err_fe_remap_missing_to_file) << RF.first << RF.second;
4240b57cec5SDimitry Andric       continue;
4250b57cec5SDimitry Andric     }
4260b57cec5SDimitry Andric 
4270b57cec5SDimitry Andric     // Create the file entry for the file that we're mapping from.
4280b57cec5SDimitry Andric     const FileEntry *FromFile =
429bdd1243dSDimitry Andric         FileMgr.getVirtualFile(RF.first, ToFile->getSize(), 0);
4300b57cec5SDimitry Andric     if (!FromFile) {
4310b57cec5SDimitry Andric       Diags.Report(diag::err_fe_remap_missing_from_file) << RF.first;
4320b57cec5SDimitry Andric       continue;
4330b57cec5SDimitry Andric     }
4340b57cec5SDimitry Andric 
4350b57cec5SDimitry Andric     // Override the contents of the "from" file with the contents of
4360b57cec5SDimitry Andric     // the "to" file.
437a7dea167SDimitry Andric     SourceMgr.overrideFileContents(FromFile, *ToFile);
4380b57cec5SDimitry Andric   }
4390b57cec5SDimitry Andric 
4400b57cec5SDimitry Andric   SourceMgr.setOverridenFilesKeepOriginalName(
4410b57cec5SDimitry Andric       InitOpts.RemappedFilesKeepOriginalName);
4420b57cec5SDimitry Andric }
4430b57cec5SDimitry Andric 
4440b57cec5SDimitry Andric // Preprocessor
4450b57cec5SDimitry Andric 
createPreprocessor(TranslationUnitKind TUKind)4460b57cec5SDimitry Andric void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) {
4470b57cec5SDimitry Andric   const PreprocessorOptions &PPOpts = getPreprocessorOpts();
4480b57cec5SDimitry Andric 
4495ffd83dbSDimitry Andric   // The AST reader holds a reference to the old preprocessor (if any).
450480093f4SDimitry Andric   TheASTReader.reset();
4510b57cec5SDimitry Andric 
4520b57cec5SDimitry Andric   // Create the Preprocessor.
4530b57cec5SDimitry Andric   HeaderSearch *HeaderInfo =
4540b57cec5SDimitry Andric       new HeaderSearch(getHeaderSearchOptsPtr(), getSourceManager(),
4550b57cec5SDimitry Andric                        getDiagnostics(), getLangOpts(), &getTarget());
4560b57cec5SDimitry Andric   PP = std::make_shared<Preprocessor>(Invocation->getPreprocessorOptsPtr(),
4570b57cec5SDimitry Andric                                       getDiagnostics(), getLangOpts(),
4580b57cec5SDimitry Andric                                       getSourceManager(), *HeaderInfo, *this,
4590b57cec5SDimitry Andric                                       /*IdentifierInfoLookup=*/nullptr,
4600b57cec5SDimitry Andric                                       /*OwnsHeaderSearch=*/true, TUKind);
461fe6060f1SDimitry Andric   getTarget().adjust(getDiagnostics(), getLangOpts());
4620b57cec5SDimitry Andric   PP->Initialize(getTarget(), getAuxTarget());
4630b57cec5SDimitry Andric 
4640b57cec5SDimitry Andric   if (PPOpts.DetailedRecord)
4650b57cec5SDimitry Andric     PP->createPreprocessingRecord();
4660b57cec5SDimitry Andric 
4670b57cec5SDimitry Andric   // Apply remappings to the source manager.
4680b57cec5SDimitry Andric   InitializeFileRemapping(PP->getDiagnostics(), PP->getSourceManager(),
4690b57cec5SDimitry Andric                           PP->getFileManager(), PPOpts);
4700b57cec5SDimitry Andric 
4710b57cec5SDimitry Andric   // Predefine macros and configure the preprocessor.
4720b57cec5SDimitry Andric   InitializePreprocessor(*PP, PPOpts, getPCHContainerReader(),
4737a6dacacSDimitry Andric                          getFrontendOpts(), getCodeGenOpts());
4740b57cec5SDimitry Andric 
4750b57cec5SDimitry Andric   // Initialize the header search object.  In CUDA compilations, we use the aux
4760b57cec5SDimitry Andric   // triple (the host triple) to initialize our header search, since we need to
4770b57cec5SDimitry Andric   // find the host headers in order to compile the CUDA code.
4780b57cec5SDimitry Andric   const llvm::Triple *HeaderSearchTriple = &PP->getTargetInfo().getTriple();
4790b57cec5SDimitry Andric   if (PP->getTargetInfo().getTriple().getOS() == llvm::Triple::CUDA &&
4800b57cec5SDimitry Andric       PP->getAuxTargetInfo())
4810b57cec5SDimitry Andric     HeaderSearchTriple = &PP->getAuxTargetInfo()->getTriple();
4820b57cec5SDimitry Andric 
4830b57cec5SDimitry Andric   ApplyHeaderSearchOptions(PP->getHeaderSearchInfo(), getHeaderSearchOpts(),
4840b57cec5SDimitry Andric                            PP->getLangOpts(), *HeaderSearchTriple);
4850b57cec5SDimitry Andric 
4860b57cec5SDimitry Andric   PP->setPreprocessedOutput(getPreprocessorOutputOpts().ShowCPP);
4870b57cec5SDimitry Andric 
488e8d8bef9SDimitry Andric   if (PP->getLangOpts().Modules && PP->getLangOpts().ImplicitModules) {
489e8d8bef9SDimitry Andric     std::string ModuleHash = getInvocation().getModuleHash();
490e8d8bef9SDimitry Andric     PP->getHeaderSearchInfo().setModuleHash(ModuleHash);
491e8d8bef9SDimitry Andric     PP->getHeaderSearchInfo().setModuleCachePath(
492e8d8bef9SDimitry Andric         getSpecificModuleCachePath(ModuleHash));
493e8d8bef9SDimitry Andric   }
4940b57cec5SDimitry Andric 
4950b57cec5SDimitry Andric   // Handle generating dependencies, if requested.
4960b57cec5SDimitry Andric   const DependencyOutputOptions &DepOpts = getDependencyOutputOpts();
4970b57cec5SDimitry Andric   if (!DepOpts.OutputFile.empty())
4980b57cec5SDimitry Andric     addDependencyCollector(std::make_shared<DependencyFileGenerator>(DepOpts));
4990b57cec5SDimitry Andric   if (!DepOpts.DOTOutputFile.empty())
5000b57cec5SDimitry Andric     AttachDependencyGraphGen(*PP, DepOpts.DOTOutputFile,
5010b57cec5SDimitry Andric                              getHeaderSearchOpts().Sysroot);
5020b57cec5SDimitry Andric 
5030b57cec5SDimitry Andric   // If we don't have a collector, but we are collecting module dependencies,
5040b57cec5SDimitry Andric   // then we're the top level compiler instance and need to create one.
5050b57cec5SDimitry Andric   if (!ModuleDepCollector && !DepOpts.ModuleDependencyOutputDir.empty()) {
5060b57cec5SDimitry Andric     ModuleDepCollector = std::make_shared<ModuleDependencyCollector>(
5070b57cec5SDimitry Andric         DepOpts.ModuleDependencyOutputDir);
5080b57cec5SDimitry Andric   }
5090b57cec5SDimitry Andric 
5100b57cec5SDimitry Andric   // If there is a module dep collector, register with other dep collectors
5110b57cec5SDimitry Andric   // and also (a) collect header maps and (b) TODO: input vfs overlay files.
5120b57cec5SDimitry Andric   if (ModuleDepCollector) {
5130b57cec5SDimitry Andric     addDependencyCollector(ModuleDepCollector);
5140b57cec5SDimitry Andric     collectHeaderMaps(PP->getHeaderSearchInfo(), ModuleDepCollector);
5150b57cec5SDimitry Andric     collectIncludePCH(*this, ModuleDepCollector);
5160b57cec5SDimitry Andric     collectVFSEntries(*this, ModuleDepCollector);
5170b57cec5SDimitry Andric   }
5180b57cec5SDimitry Andric 
5190b57cec5SDimitry Andric   for (auto &Listener : DependencyCollectors)
5200b57cec5SDimitry Andric     Listener->attachToPreprocessor(*PP);
5210b57cec5SDimitry Andric 
5220b57cec5SDimitry Andric   // Handle generating header include information, if requested.
5230b57cec5SDimitry Andric   if (DepOpts.ShowHeaderIncludes)
5240b57cec5SDimitry Andric     AttachHeaderIncludeGen(*PP, DepOpts);
5250b57cec5SDimitry Andric   if (!DepOpts.HeaderIncludeOutputFile.empty()) {
5260b57cec5SDimitry Andric     StringRef OutputPath = DepOpts.HeaderIncludeOutputFile;
5270b57cec5SDimitry Andric     if (OutputPath == "-")
5280b57cec5SDimitry Andric       OutputPath = "";
5290b57cec5SDimitry Andric     AttachHeaderIncludeGen(*PP, DepOpts,
5300b57cec5SDimitry Andric                            /*ShowAllHeaders=*/true, OutputPath,
5310b57cec5SDimitry Andric                            /*ShowDepth=*/false);
5320b57cec5SDimitry Andric   }
5330b57cec5SDimitry Andric 
5340b57cec5SDimitry Andric   if (DepOpts.ShowIncludesDest != ShowIncludesDestination::None) {
5350b57cec5SDimitry Andric     AttachHeaderIncludeGen(*PP, DepOpts,
5360b57cec5SDimitry Andric                            /*ShowAllHeaders=*/true, /*OutputPath=*/"",
5370b57cec5SDimitry Andric                            /*ShowDepth=*/true, /*MSStyle=*/true);
5380b57cec5SDimitry Andric   }
5390b57cec5SDimitry Andric }
5400b57cec5SDimitry Andric 
getSpecificModuleCachePath(StringRef ModuleHash)541e8d8bef9SDimitry Andric std::string CompilerInstance::getSpecificModuleCachePath(StringRef ModuleHash) {
542e8d8bef9SDimitry Andric   // Set up the module path, including the hash for the module-creation options.
5430b57cec5SDimitry Andric   SmallString<256> SpecificModuleCache(getHeaderSearchOpts().ModuleCachePath);
5440b57cec5SDimitry Andric   if (!SpecificModuleCache.empty() && !getHeaderSearchOpts().DisableModuleHash)
545e8d8bef9SDimitry Andric     llvm::sys::path::append(SpecificModuleCache, ModuleHash);
5467a6dacacSDimitry Andric   return std::string(SpecificModuleCache);
5470b57cec5SDimitry Andric }
5480b57cec5SDimitry Andric 
5490b57cec5SDimitry Andric // ASTContext
5500b57cec5SDimitry Andric 
createASTContext()5510b57cec5SDimitry Andric void CompilerInstance::createASTContext() {
5520b57cec5SDimitry Andric   Preprocessor &PP = getPreprocessor();
5530b57cec5SDimitry Andric   auto *Context = new ASTContext(getLangOpts(), PP.getSourceManager(),
5540b57cec5SDimitry Andric                                  PP.getIdentifierTable(), PP.getSelectorTable(),
555fe6060f1SDimitry Andric                                  PP.getBuiltinInfo(), PP.TUKind);
5560b57cec5SDimitry Andric   Context->InitBuiltinTypes(getTarget(), getAuxTarget());
5570b57cec5SDimitry Andric   setASTContext(Context);
5580b57cec5SDimitry Andric }
5590b57cec5SDimitry Andric 
5600b57cec5SDimitry Andric // ExternalASTSource
5610b57cec5SDimitry Andric 
562349cc55cSDimitry Andric namespace {
563349cc55cSDimitry Andric // Helper to recursively read the module names for all modules we're adding.
564349cc55cSDimitry Andric // We mark these as known and redirect any attempt to load that module to
565349cc55cSDimitry Andric // the files we were handed.
566349cc55cSDimitry Andric struct ReadModuleNames : ASTReaderListener {
567349cc55cSDimitry Andric   Preprocessor &PP;
568349cc55cSDimitry Andric   llvm::SmallVector<std::string, 8> LoadedModules;
569349cc55cSDimitry Andric 
ReadModuleNames__anonacda6b810111::ReadModuleNames570349cc55cSDimitry Andric   ReadModuleNames(Preprocessor &PP) : PP(PP) {}
571349cc55cSDimitry Andric 
ReadModuleName__anonacda6b810111::ReadModuleNames572349cc55cSDimitry Andric   void ReadModuleName(StringRef ModuleName) override {
573349cc55cSDimitry Andric     // Keep the module name as a string for now. It's not safe to create a new
574349cc55cSDimitry Andric     // IdentifierInfo from an ASTReader callback.
575349cc55cSDimitry Andric     LoadedModules.push_back(ModuleName.str());
576349cc55cSDimitry Andric   }
577349cc55cSDimitry Andric 
registerAll__anonacda6b810111::ReadModuleNames578349cc55cSDimitry Andric   void registerAll() {
579349cc55cSDimitry Andric     ModuleMap &MM = PP.getHeaderSearchInfo().getModuleMap();
580349cc55cSDimitry Andric     for (const std::string &LoadedModule : LoadedModules)
581349cc55cSDimitry Andric       MM.cacheModuleLoad(*PP.getIdentifierInfo(LoadedModule),
582349cc55cSDimitry Andric                          MM.findModule(LoadedModule));
583349cc55cSDimitry Andric     LoadedModules.clear();
584349cc55cSDimitry Andric   }
585349cc55cSDimitry Andric 
markAllUnavailable__anonacda6b810111::ReadModuleNames586349cc55cSDimitry Andric   void markAllUnavailable() {
587349cc55cSDimitry Andric     for (const std::string &LoadedModule : LoadedModules) {
588349cc55cSDimitry Andric       if (Module *M = PP.getHeaderSearchInfo().getModuleMap().findModule(
589349cc55cSDimitry Andric               LoadedModule)) {
590349cc55cSDimitry Andric         M->HasIncompatibleModuleFile = true;
591349cc55cSDimitry Andric 
592349cc55cSDimitry Andric         // Mark module as available if the only reason it was unavailable
593349cc55cSDimitry Andric         // was missing headers.
594349cc55cSDimitry Andric         SmallVector<Module *, 2> Stack;
595349cc55cSDimitry Andric         Stack.push_back(M);
596349cc55cSDimitry Andric         while (!Stack.empty()) {
597349cc55cSDimitry Andric           Module *Current = Stack.pop_back_val();
598349cc55cSDimitry Andric           if (Current->IsUnimportable) continue;
599349cc55cSDimitry Andric           Current->IsAvailable = true;
60006c3fb27SDimitry Andric           auto SubmodulesRange = Current->submodules();
60106c3fb27SDimitry Andric           Stack.insert(Stack.end(), SubmodulesRange.begin(),
60206c3fb27SDimitry Andric                        SubmodulesRange.end());
603349cc55cSDimitry Andric         }
604349cc55cSDimitry Andric       }
605349cc55cSDimitry Andric     }
606349cc55cSDimitry Andric     LoadedModules.clear();
607349cc55cSDimitry Andric   }
608349cc55cSDimitry Andric };
609349cc55cSDimitry Andric } // namespace
610349cc55cSDimitry Andric 
createPCHExternalASTSource(StringRef Path,DisableValidationForModuleKind DisableValidation,bool AllowPCHWithCompilerErrors,void * DeserializationListener,bool OwnDeserializationListener)6110b57cec5SDimitry Andric void CompilerInstance::createPCHExternalASTSource(
612e8d8bef9SDimitry Andric     StringRef Path, DisableValidationForModuleKind DisableValidation,
613e8d8bef9SDimitry Andric     bool AllowPCHWithCompilerErrors, void *DeserializationListener,
614e8d8bef9SDimitry Andric     bool OwnDeserializationListener) {
6150b57cec5SDimitry Andric   bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
616480093f4SDimitry Andric   TheASTReader = createPCHExternalASTSource(
617e8d8bef9SDimitry Andric       Path, getHeaderSearchOpts().Sysroot, DisableValidation,
6180b57cec5SDimitry Andric       AllowPCHWithCompilerErrors, getPreprocessor(), getModuleCache(),
6190b57cec5SDimitry Andric       getASTContext(), getPCHContainerReader(),
6200b57cec5SDimitry Andric       getFrontendOpts().ModuleFileExtensions, DependencyCollectors,
6210b57cec5SDimitry Andric       DeserializationListener, OwnDeserializationListener, Preamble,
6220b57cec5SDimitry Andric       getFrontendOpts().UseGlobalModuleIndex);
6230b57cec5SDimitry Andric }
6240b57cec5SDimitry Andric 
createPCHExternalASTSource(StringRef Path,StringRef Sysroot,DisableValidationForModuleKind DisableValidation,bool AllowPCHWithCompilerErrors,Preprocessor & PP,InMemoryModuleCache & ModuleCache,ASTContext & Context,const PCHContainerReader & PCHContainerRdr,ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,ArrayRef<std::shared_ptr<DependencyCollector>> DependencyCollectors,void * DeserializationListener,bool OwnDeserializationListener,bool Preamble,bool UseGlobalModuleIndex)6250b57cec5SDimitry Andric IntrusiveRefCntPtr<ASTReader> CompilerInstance::createPCHExternalASTSource(
626e8d8bef9SDimitry Andric     StringRef Path, StringRef Sysroot,
627e8d8bef9SDimitry Andric     DisableValidationForModuleKind DisableValidation,
6280b57cec5SDimitry Andric     bool AllowPCHWithCompilerErrors, Preprocessor &PP,
6290b57cec5SDimitry Andric     InMemoryModuleCache &ModuleCache, ASTContext &Context,
6300b57cec5SDimitry Andric     const PCHContainerReader &PCHContainerRdr,
6310b57cec5SDimitry Andric     ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
6320b57cec5SDimitry Andric     ArrayRef<std::shared_ptr<DependencyCollector>> DependencyCollectors,
6330b57cec5SDimitry Andric     void *DeserializationListener, bool OwnDeserializationListener,
6340b57cec5SDimitry Andric     bool Preamble, bool UseGlobalModuleIndex) {
6350b57cec5SDimitry Andric   HeaderSearchOptions &HSOpts = PP.getHeaderSearchInfo().getHeaderSearchOpts();
6360b57cec5SDimitry Andric 
6370b57cec5SDimitry Andric   IntrusiveRefCntPtr<ASTReader> Reader(new ASTReader(
6380b57cec5SDimitry Andric       PP, ModuleCache, &Context, PCHContainerRdr, Extensions,
639e8d8bef9SDimitry Andric       Sysroot.empty() ? "" : Sysroot.data(), DisableValidation,
6400b57cec5SDimitry Andric       AllowPCHWithCompilerErrors, /*AllowConfigurationMismatch*/ false,
641a7dea167SDimitry Andric       HSOpts.ModulesValidateSystemHeaders, HSOpts.ValidateASTInputFilesContent,
642a7dea167SDimitry Andric       UseGlobalModuleIndex));
6430b57cec5SDimitry Andric 
6440b57cec5SDimitry Andric   // We need the external source to be set up before we read the AST, because
6450b57cec5SDimitry Andric   // eagerly-deserialized declarations may use it.
6460b57cec5SDimitry Andric   Context.setExternalSource(Reader.get());
6470b57cec5SDimitry Andric 
6480b57cec5SDimitry Andric   Reader->setDeserializationListener(
6490b57cec5SDimitry Andric       static_cast<ASTDeserializationListener *>(DeserializationListener),
6500b57cec5SDimitry Andric       /*TakeOwnership=*/OwnDeserializationListener);
6510b57cec5SDimitry Andric 
6520b57cec5SDimitry Andric   for (auto &Listener : DependencyCollectors)
6530b57cec5SDimitry Andric     Listener->attachToASTReader(*Reader);
6540b57cec5SDimitry Andric 
655349cc55cSDimitry Andric   auto Listener = std::make_unique<ReadModuleNames>(PP);
656349cc55cSDimitry Andric   auto &ListenerRef = *Listener;
657349cc55cSDimitry Andric   ASTReader::ListenerScope ReadModuleNamesListener(*Reader,
658349cc55cSDimitry Andric                                                    std::move(Listener));
659349cc55cSDimitry Andric 
6600b57cec5SDimitry Andric   switch (Reader->ReadAST(Path,
6610b57cec5SDimitry Andric                           Preamble ? serialization::MK_Preamble
6620b57cec5SDimitry Andric                                    : serialization::MK_PCH,
6630b57cec5SDimitry Andric                           SourceLocation(),
6640b57cec5SDimitry Andric                           ASTReader::ARR_None)) {
6650b57cec5SDimitry Andric   case ASTReader::Success:
6660b57cec5SDimitry Andric     // Set the predefines buffer as suggested by the PCH reader. Typically, the
6670b57cec5SDimitry Andric     // predefines buffer will be empty.
6680b57cec5SDimitry Andric     PP.setPredefines(Reader->getSuggestedPredefines());
669349cc55cSDimitry Andric     ListenerRef.registerAll();
6700b57cec5SDimitry Andric     return Reader;
6710b57cec5SDimitry Andric 
6720b57cec5SDimitry Andric   case ASTReader::Failure:
6730b57cec5SDimitry Andric     // Unrecoverable failure: don't even try to process the input file.
6740b57cec5SDimitry Andric     break;
6750b57cec5SDimitry Andric 
6760b57cec5SDimitry Andric   case ASTReader::Missing:
6770b57cec5SDimitry Andric   case ASTReader::OutOfDate:
6780b57cec5SDimitry Andric   case ASTReader::VersionMismatch:
6790b57cec5SDimitry Andric   case ASTReader::ConfigurationMismatch:
6800b57cec5SDimitry Andric   case ASTReader::HadErrors:
6810b57cec5SDimitry Andric     // No suitable PCH file could be found. Return an error.
6820b57cec5SDimitry Andric     break;
6830b57cec5SDimitry Andric   }
6840b57cec5SDimitry Andric 
685349cc55cSDimitry Andric   ListenerRef.markAllUnavailable();
6860b57cec5SDimitry Andric   Context.setExternalSource(nullptr);
6870b57cec5SDimitry Andric   return nullptr;
6880b57cec5SDimitry Andric }
6890b57cec5SDimitry Andric 
6900b57cec5SDimitry Andric // Code Completion
6910b57cec5SDimitry Andric 
EnableCodeCompletion(Preprocessor & PP,StringRef Filename,unsigned Line,unsigned Column)6920b57cec5SDimitry Andric static bool EnableCodeCompletion(Preprocessor &PP,
6930b57cec5SDimitry Andric                                  StringRef Filename,
6940b57cec5SDimitry Andric                                  unsigned Line,
6950b57cec5SDimitry Andric                                  unsigned Column) {
6960b57cec5SDimitry Andric   // Tell the source manager to chop off the given file at a specific
6970b57cec5SDimitry Andric   // line and column.
6985f757f3fSDimitry Andric   auto Entry = PP.getFileManager().getOptionalFileRef(Filename);
6990b57cec5SDimitry Andric   if (!Entry) {
7000b57cec5SDimitry Andric     PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
7010b57cec5SDimitry Andric       << Filename;
7020b57cec5SDimitry Andric     return true;
7030b57cec5SDimitry Andric   }
7040b57cec5SDimitry Andric 
7050b57cec5SDimitry Andric   // Truncate the named file at the given line/column.
706a7dea167SDimitry Andric   PP.SetCodeCompletionPoint(*Entry, Line, Column);
7070b57cec5SDimitry Andric   return false;
7080b57cec5SDimitry Andric }
7090b57cec5SDimitry Andric 
createCodeCompletionConsumer()7100b57cec5SDimitry Andric void CompilerInstance::createCodeCompletionConsumer() {
7110b57cec5SDimitry Andric   const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt;
7120b57cec5SDimitry Andric   if (!CompletionConsumer) {
71381ad6265SDimitry Andric     setCodeCompletionConsumer(createCodeCompletionConsumer(
71481ad6265SDimitry Andric         getPreprocessor(), Loc.FileName, Loc.Line, Loc.Column,
71581ad6265SDimitry Andric         getFrontendOpts().CodeCompleteOpts, llvm::outs()));
7160b57cec5SDimitry Andric     return;
7170b57cec5SDimitry Andric   } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName,
7180b57cec5SDimitry Andric                                   Loc.Line, Loc.Column)) {
7190b57cec5SDimitry Andric     setCodeCompletionConsumer(nullptr);
7200b57cec5SDimitry Andric     return;
7210b57cec5SDimitry Andric   }
7220b57cec5SDimitry Andric }
7230b57cec5SDimitry Andric 
createFrontendTimer()7240b57cec5SDimitry Andric void CompilerInstance::createFrontendTimer() {
7250b57cec5SDimitry Andric   FrontendTimerGroup.reset(
7260b57cec5SDimitry Andric       new llvm::TimerGroup("frontend", "Clang front-end time report"));
7270b57cec5SDimitry Andric   FrontendTimer.reset(
7280b57cec5SDimitry Andric       new llvm::Timer("frontend", "Clang front-end timer",
7290b57cec5SDimitry Andric                       *FrontendTimerGroup));
7300b57cec5SDimitry Andric }
7310b57cec5SDimitry Andric 
7320b57cec5SDimitry Andric CodeCompleteConsumer *
createCodeCompletionConsumer(Preprocessor & PP,StringRef Filename,unsigned Line,unsigned Column,const CodeCompleteOptions & Opts,raw_ostream & OS)7330b57cec5SDimitry Andric CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
7340b57cec5SDimitry Andric                                                StringRef Filename,
7350b57cec5SDimitry Andric                                                unsigned Line,
7360b57cec5SDimitry Andric                                                unsigned Column,
7370b57cec5SDimitry Andric                                                const CodeCompleteOptions &Opts,
7380b57cec5SDimitry Andric                                                raw_ostream &OS) {
7390b57cec5SDimitry Andric   if (EnableCodeCompletion(PP, Filename, Line, Column))
7400b57cec5SDimitry Andric     return nullptr;
7410b57cec5SDimitry Andric 
7420b57cec5SDimitry Andric   // Set up the creation routine for code-completion.
7430b57cec5SDimitry Andric   return new PrintingCodeCompleteConsumer(Opts, OS);
7440b57cec5SDimitry Andric }
7450b57cec5SDimitry Andric 
createSema(TranslationUnitKind TUKind,CodeCompleteConsumer * CompletionConsumer)7460b57cec5SDimitry Andric void CompilerInstance::createSema(TranslationUnitKind TUKind,
7470b57cec5SDimitry Andric                                   CodeCompleteConsumer *CompletionConsumer) {
7480b57cec5SDimitry Andric   TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(),
7490b57cec5SDimitry Andric                          TUKind, CompletionConsumer));
7505f757f3fSDimitry Andric 
7515f757f3fSDimitry Andric   // Set up API notes.
7525f757f3fSDimitry Andric   TheSema->APINotes.setSwiftVersion(getAPINotesOpts().SwiftVersion);
7535f757f3fSDimitry Andric 
7540b57cec5SDimitry Andric   // Attach the external sema source if there is any.
7550b57cec5SDimitry Andric   if (ExternalSemaSrc) {
7560b57cec5SDimitry Andric     TheSema->addExternalSource(ExternalSemaSrc.get());
7570b57cec5SDimitry Andric     ExternalSemaSrc->InitializeSema(*TheSema);
7580b57cec5SDimitry Andric   }
7595f757f3fSDimitry Andric 
7605f757f3fSDimitry Andric   // If we're building a module and are supposed to load API notes,
7615f757f3fSDimitry Andric   // notify the API notes manager.
7625f757f3fSDimitry Andric   if (auto *currentModule = getPreprocessor().getCurrentModule()) {
7635f757f3fSDimitry Andric     (void)TheSema->APINotes.loadCurrentModuleAPINotes(
7645f757f3fSDimitry Andric         currentModule, getLangOpts().APINotesModules,
7655f757f3fSDimitry Andric         getAPINotesOpts().ModuleSearchPaths);
7665f757f3fSDimitry Andric   }
7670b57cec5SDimitry Andric }
7680b57cec5SDimitry Andric 
7690b57cec5SDimitry Andric // Output Files
7700b57cec5SDimitry Andric 
clearOutputFiles(bool EraseFiles)7710b57cec5SDimitry Andric void CompilerInstance::clearOutputFiles(bool EraseFiles) {
772753f127fSDimitry Andric   // The ASTConsumer can own streams that write to the output files.
773753f127fSDimitry Andric   assert(!hasASTConsumer() && "ASTConsumer should be reset");
774fe6060f1SDimitry Andric   // Ignore errors that occur when trying to discard the temp file.
7750b57cec5SDimitry Andric   for (OutputFile &OF : OutputFiles) {
7760b57cec5SDimitry Andric     if (EraseFiles) {
777fe6060f1SDimitry Andric       if (OF.File)
778fe6060f1SDimitry Andric         consumeError(OF.File->discard());
779e8d8bef9SDimitry Andric       if (!OF.Filename.empty())
780e8d8bef9SDimitry Andric         llvm::sys::fs::remove(OF.Filename);
781e8d8bef9SDimitry Andric       continue;
782e8d8bef9SDimitry Andric     }
783e8d8bef9SDimitry Andric 
784fe6060f1SDimitry Andric     if (!OF.File)
785e8d8bef9SDimitry Andric       continue;
7860b57cec5SDimitry Andric 
787fe6060f1SDimitry Andric     if (OF.File->TmpName.empty()) {
788fe6060f1SDimitry Andric       consumeError(OF.File->discard());
789fe6060f1SDimitry Andric       continue;
790fe6060f1SDimitry Andric     }
791fe6060f1SDimitry Andric 
792bdd1243dSDimitry Andric     llvm::Error E = OF.File->keep(OF.Filename);
793fe6060f1SDimitry Andric     if (!E)
794fe6060f1SDimitry Andric       continue;
795fe6060f1SDimitry Andric 
796fe6060f1SDimitry Andric     getDiagnostics().Report(diag::err_unable_to_rename_temp)
797fe6060f1SDimitry Andric         << OF.File->TmpName << OF.Filename << std::move(E);
798fe6060f1SDimitry Andric 
799fe6060f1SDimitry Andric     llvm::sys::fs::remove(OF.File->TmpName);
8000b57cec5SDimitry Andric   }
8010b57cec5SDimitry Andric   OutputFiles.clear();
8020b57cec5SDimitry Andric   if (DeleteBuiltModules) {
8030b57cec5SDimitry Andric     for (auto &Module : BuiltModules)
8040b57cec5SDimitry Andric       llvm::sys::fs::remove(Module.second);
8050b57cec5SDimitry Andric     BuiltModules.clear();
8060b57cec5SDimitry Andric   }
8070b57cec5SDimitry Andric }
8080b57cec5SDimitry Andric 
createDefaultOutputFile(bool Binary,StringRef InFile,StringRef Extension,bool RemoveFileOnSignal,bool CreateMissingDirectories,bool ForceUseTemporary)809fe6060f1SDimitry Andric std::unique_ptr<raw_pwrite_stream> CompilerInstance::createDefaultOutputFile(
810fe6060f1SDimitry Andric     bool Binary, StringRef InFile, StringRef Extension, bool RemoveFileOnSignal,
811fe6060f1SDimitry Andric     bool CreateMissingDirectories, bool ForceUseTemporary) {
812e8d8bef9SDimitry Andric   StringRef OutputPath = getFrontendOpts().OutputFile;
813bdd1243dSDimitry Andric   std::optional<SmallString<128>> PathStorage;
814e8d8bef9SDimitry Andric   if (OutputPath.empty()) {
815e8d8bef9SDimitry Andric     if (InFile == "-" || Extension.empty()) {
816e8d8bef9SDimitry Andric       OutputPath = "-";
817e8d8bef9SDimitry Andric     } else {
818e8d8bef9SDimitry Andric       PathStorage.emplace(InFile);
819e8d8bef9SDimitry Andric       llvm::sys::path::replace_extension(*PathStorage, Extension);
820e8d8bef9SDimitry Andric       OutputPath = *PathStorage;
821e8d8bef9SDimitry Andric     }
822e8d8bef9SDimitry Andric   }
823e8d8bef9SDimitry Andric 
824e8d8bef9SDimitry Andric   return createOutputFile(OutputPath, Binary, RemoveFileOnSignal,
825fe6060f1SDimitry Andric                           getFrontendOpts().UseTemporary || ForceUseTemporary,
826e8d8bef9SDimitry Andric                           CreateMissingDirectories);
8270b57cec5SDimitry Andric }
8280b57cec5SDimitry Andric 
createNullOutputFile()8290b57cec5SDimitry Andric std::unique_ptr<raw_pwrite_stream> CompilerInstance::createNullOutputFile() {
830a7dea167SDimitry Andric   return std::make_unique<llvm::raw_null_ostream>();
8310b57cec5SDimitry Andric }
8320b57cec5SDimitry Andric 
8330b57cec5SDimitry Andric std::unique_ptr<raw_pwrite_stream>
createOutputFile(StringRef OutputPath,bool Binary,bool RemoveFileOnSignal,bool UseTemporary,bool CreateMissingDirectories)8340b57cec5SDimitry Andric CompilerInstance::createOutputFile(StringRef OutputPath, bool Binary,
835e8d8bef9SDimitry Andric                                    bool RemoveFileOnSignal, bool UseTemporary,
8360b57cec5SDimitry Andric                                    bool CreateMissingDirectories) {
837e8d8bef9SDimitry Andric   Expected<std::unique_ptr<raw_pwrite_stream>> OS =
838e8d8bef9SDimitry Andric       createOutputFileImpl(OutputPath, Binary, RemoveFileOnSignal, UseTemporary,
839e8d8bef9SDimitry Andric                            CreateMissingDirectories);
840e8d8bef9SDimitry Andric   if (OS)
841e8d8bef9SDimitry Andric     return std::move(*OS);
842e8d8bef9SDimitry Andric   getDiagnostics().Report(diag::err_fe_unable_to_open_output)
843e8d8bef9SDimitry Andric       << OutputPath << errorToErrorCode(OS.takeError()).message();
8440b57cec5SDimitry Andric   return nullptr;
8450b57cec5SDimitry Andric }
8460b57cec5SDimitry Andric 
847e8d8bef9SDimitry Andric Expected<std::unique_ptr<llvm::raw_pwrite_stream>>
createOutputFileImpl(StringRef OutputPath,bool Binary,bool RemoveFileOnSignal,bool UseTemporary,bool CreateMissingDirectories)848e8d8bef9SDimitry Andric CompilerInstance::createOutputFileImpl(StringRef OutputPath, bool Binary,
849e8d8bef9SDimitry Andric                                        bool RemoveFileOnSignal,
850e8d8bef9SDimitry Andric                                        bool UseTemporary,
851e8d8bef9SDimitry Andric                                        bool CreateMissingDirectories) {
8520b57cec5SDimitry Andric   assert((!CreateMissingDirectories || UseTemporary) &&
8530b57cec5SDimitry Andric          "CreateMissingDirectories is only allowed when using temporary files");
8540b57cec5SDimitry Andric 
855bdd1243dSDimitry Andric   // If '-working-directory' was passed, the output filename should be
856bdd1243dSDimitry Andric   // relative to that.
857bdd1243dSDimitry Andric   std::optional<SmallString<128>> AbsPath;
858bdd1243dSDimitry Andric   if (OutputPath != "-" && !llvm::sys::path::is_absolute(OutputPath)) {
85906c3fb27SDimitry Andric     assert(hasFileManager() &&
86006c3fb27SDimitry Andric            "File Manager is required to fix up relative path.\n");
86106c3fb27SDimitry Andric 
862bdd1243dSDimitry Andric     AbsPath.emplace(OutputPath);
863bdd1243dSDimitry Andric     FileMgr->FixupRelativePath(*AbsPath);
864bdd1243dSDimitry Andric     OutputPath = *AbsPath;
865bdd1243dSDimitry Andric   }
866bdd1243dSDimitry Andric 
8670b57cec5SDimitry Andric   std::unique_ptr<llvm::raw_fd_ostream> OS;
868bdd1243dSDimitry Andric   std::optional<StringRef> OSFile;
8690b57cec5SDimitry Andric 
8700b57cec5SDimitry Andric   if (UseTemporary) {
871e8d8bef9SDimitry Andric     if (OutputPath == "-")
8720b57cec5SDimitry Andric       UseTemporary = false;
8730b57cec5SDimitry Andric     else {
8740b57cec5SDimitry Andric       llvm::sys::fs::file_status Status;
8750b57cec5SDimitry Andric       llvm::sys::fs::status(OutputPath, Status);
8760b57cec5SDimitry Andric       if (llvm::sys::fs::exists(Status)) {
8770b57cec5SDimitry Andric         // Fail early if we can't write to the final destination.
878e8d8bef9SDimitry Andric         if (!llvm::sys::fs::can_write(OutputPath))
879e8d8bef9SDimitry Andric           return llvm::errorCodeToError(
880e8d8bef9SDimitry Andric               make_error_code(llvm::errc::operation_not_permitted));
8810b57cec5SDimitry Andric 
8820b57cec5SDimitry Andric         // Don't use a temporary if the output is a special file. This handles
8830b57cec5SDimitry Andric         // things like '-o /dev/null'
8840b57cec5SDimitry Andric         if (!llvm::sys::fs::is_regular_file(Status))
8850b57cec5SDimitry Andric           UseTemporary = false;
8860b57cec5SDimitry Andric       }
8870b57cec5SDimitry Andric     }
8880b57cec5SDimitry Andric   }
8890b57cec5SDimitry Andric 
890bdd1243dSDimitry Andric   std::optional<llvm::sys::fs::TempFile> Temp;
8910b57cec5SDimitry Andric   if (UseTemporary) {
8920b57cec5SDimitry Andric     // Create a temporary file.
8930b57cec5SDimitry Andric     // Insert -%%%%%%%% before the extension (if any), and because some tools
8940b57cec5SDimitry Andric     // (noticeable, clang's own GlobalModuleIndex.cpp) glob for build
8950b57cec5SDimitry Andric     // artifacts, also append .tmp.
896e8d8bef9SDimitry Andric     StringRef OutputExtension = llvm::sys::path::extension(OutputPath);
8970b57cec5SDimitry Andric     SmallString<128> TempPath =
898e8d8bef9SDimitry Andric         StringRef(OutputPath).drop_back(OutputExtension.size());
8990b57cec5SDimitry Andric     TempPath += "-%%%%%%%%";
9000b57cec5SDimitry Andric     TempPath += OutputExtension;
9010b57cec5SDimitry Andric     TempPath += ".tmp";
90206c3fb27SDimitry Andric     llvm::sys::fs::OpenFlags BinaryFlags =
90306c3fb27SDimitry Andric         Binary ? llvm::sys::fs::OF_None : llvm::sys::fs::OF_Text;
904fe6060f1SDimitry Andric     Expected<llvm::sys::fs::TempFile> ExpectedFile =
905fe6060f1SDimitry Andric         llvm::sys::fs::TempFile::create(
906fe6060f1SDimitry Andric             TempPath, llvm::sys::fs::all_read | llvm::sys::fs::all_write,
90706c3fb27SDimitry Andric             BinaryFlags);
9080b57cec5SDimitry Andric 
909fe6060f1SDimitry Andric     llvm::Error E = handleErrors(
910fe6060f1SDimitry Andric         ExpectedFile.takeError(), [&](const llvm::ECError &E) -> llvm::Error {
911fe6060f1SDimitry Andric           std::error_code EC = E.convertToErrorCode();
9120b57cec5SDimitry Andric           if (CreateMissingDirectories &&
9130b57cec5SDimitry Andric               EC == llvm::errc::no_such_file_or_directory) {
9140b57cec5SDimitry Andric             StringRef Parent = llvm::sys::path::parent_path(OutputPath);
9150b57cec5SDimitry Andric             EC = llvm::sys::fs::create_directories(Parent);
9160b57cec5SDimitry Andric             if (!EC) {
91706c3fb27SDimitry Andric               ExpectedFile = llvm::sys::fs::TempFile::create(
91806c3fb27SDimitry Andric                   TempPath, llvm::sys::fs::all_read | llvm::sys::fs::all_write,
91906c3fb27SDimitry Andric                   BinaryFlags);
920fe6060f1SDimitry Andric               if (!ExpectedFile)
921fe6060f1SDimitry Andric                 return llvm::errorCodeToError(
922fe6060f1SDimitry Andric                     llvm::errc::no_such_file_or_directory);
9230b57cec5SDimitry Andric             }
9240b57cec5SDimitry Andric           }
925fe6060f1SDimitry Andric           return llvm::errorCodeToError(EC);
926fe6060f1SDimitry Andric         });
9270b57cec5SDimitry Andric 
928fe6060f1SDimitry Andric     if (E) {
929fe6060f1SDimitry Andric       consumeError(std::move(E));
930fe6060f1SDimitry Andric     } else {
931fe6060f1SDimitry Andric       Temp = std::move(ExpectedFile.get());
932fe6060f1SDimitry Andric       OS.reset(new llvm::raw_fd_ostream(Temp->FD, /*shouldClose=*/false));
933fe6060f1SDimitry Andric       OSFile = Temp->TmpName;
9340b57cec5SDimitry Andric     }
9350b57cec5SDimitry Andric     // If we failed to create the temporary, fallback to writing to the file
9360b57cec5SDimitry Andric     // directly. This handles the corner case where we cannot write to the
9370b57cec5SDimitry Andric     // directory, but can write to the file.
9380b57cec5SDimitry Andric   }
9390b57cec5SDimitry Andric 
9400b57cec5SDimitry Andric   if (!OS) {
941e8d8bef9SDimitry Andric     OSFile = OutputPath;
942e8d8bef9SDimitry Andric     std::error_code EC;
9430b57cec5SDimitry Andric     OS.reset(new llvm::raw_fd_ostream(
944e8d8bef9SDimitry Andric         *OSFile, EC,
945fe6060f1SDimitry Andric         (Binary ? llvm::sys::fs::OF_None : llvm::sys::fs::OF_TextWithCRLF)));
946e8d8bef9SDimitry Andric     if (EC)
947e8d8bef9SDimitry Andric       return llvm::errorCodeToError(EC);
9480b57cec5SDimitry Andric   }
9490b57cec5SDimitry Andric 
950e8d8bef9SDimitry Andric   // Add the output file -- but don't try to remove "-", since this means we are
951e8d8bef9SDimitry Andric   // using stdin.
952e8d8bef9SDimitry Andric   OutputFiles.emplace_back(((OutputPath != "-") ? OutputPath : "").str(),
953fe6060f1SDimitry Andric                            std::move(Temp));
9540b57cec5SDimitry Andric 
9550b57cec5SDimitry Andric   if (!Binary || OS->supportsSeeking())
9560b57cec5SDimitry Andric     return std::move(OS);
9570b57cec5SDimitry Andric 
958e8d8bef9SDimitry Andric   return std::make_unique<llvm::buffer_unique_ostream>(std::move(OS));
9590b57cec5SDimitry Andric }
9600b57cec5SDimitry Andric 
9610b57cec5SDimitry Andric // Initialization Utilities
9620b57cec5SDimitry Andric 
InitializeSourceManager(const FrontendInputFile & Input)9630b57cec5SDimitry Andric bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input){
9645ffd83dbSDimitry Andric   return InitializeSourceManager(Input, getDiagnostics(), getFileManager(),
9655ffd83dbSDimitry Andric                                  getSourceManager());
9660b57cec5SDimitry Andric }
9670b57cec5SDimitry Andric 
9680b57cec5SDimitry Andric // static
InitializeSourceManager(const FrontendInputFile & Input,DiagnosticsEngine & Diags,FileManager & FileMgr,SourceManager & SourceMgr)9695ffd83dbSDimitry Andric bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input,
9705ffd83dbSDimitry Andric                                                DiagnosticsEngine &Diags,
9715ffd83dbSDimitry Andric                                                FileManager &FileMgr,
9725ffd83dbSDimitry Andric                                                SourceManager &SourceMgr) {
9730b57cec5SDimitry Andric   SrcMgr::CharacteristicKind Kind =
9740b57cec5SDimitry Andric       Input.getKind().getFormat() == InputKind::ModuleMap
9750b57cec5SDimitry Andric           ? Input.isSystem() ? SrcMgr::C_System_ModuleMap
9760b57cec5SDimitry Andric                              : SrcMgr::C_User_ModuleMap
9770b57cec5SDimitry Andric           : Input.isSystem() ? SrcMgr::C_System : SrcMgr::C_User;
9780b57cec5SDimitry Andric 
9790b57cec5SDimitry Andric   if (Input.isBuffer()) {
980e8d8bef9SDimitry Andric     SourceMgr.setMainFileID(SourceMgr.createFileID(Input.getBuffer(), Kind));
9810b57cec5SDimitry Andric     assert(SourceMgr.getMainFileID().isValid() &&
9820b57cec5SDimitry Andric            "Couldn't establish MainFileID!");
9830b57cec5SDimitry Andric     return true;
9840b57cec5SDimitry Andric   }
9850b57cec5SDimitry Andric 
9860b57cec5SDimitry Andric   StringRef InputFile = Input.getFile();
9870b57cec5SDimitry Andric 
9880b57cec5SDimitry Andric   // Figure out where to get and map in the main file.
989e8d8bef9SDimitry Andric   auto FileOrErr = InputFile == "-"
990e8d8bef9SDimitry Andric                        ? FileMgr.getSTDIN()
991e8d8bef9SDimitry Andric                        : FileMgr.getFileRef(InputFile, /*OpenFile=*/true);
992a7dea167SDimitry Andric   if (!FileOrErr) {
993e8d8bef9SDimitry Andric     auto EC = llvm::errorToErrorCode(FileOrErr.takeError());
994e8d8bef9SDimitry Andric     if (InputFile != "-")
99506c3fb27SDimitry Andric       Diags.Report(diag::err_fe_error_reading) << InputFile << EC.message();
996e8d8bef9SDimitry Andric     else
9970b57cec5SDimitry Andric       Diags.Report(diag::err_fe_error_reading_stdin) << EC.message();
9980b57cec5SDimitry Andric     return false;
9990b57cec5SDimitry Andric   }
10000b57cec5SDimitry Andric 
10010b57cec5SDimitry Andric   SourceMgr.setMainFileID(
1002e8d8bef9SDimitry Andric       SourceMgr.createFileID(*FileOrErr, SourceLocation(), Kind));
10030b57cec5SDimitry Andric 
10040b57cec5SDimitry Andric   assert(SourceMgr.getMainFileID().isValid() &&
10050b57cec5SDimitry Andric          "Couldn't establish MainFileID!");
10060b57cec5SDimitry Andric   return true;
10070b57cec5SDimitry Andric }
10080b57cec5SDimitry Andric 
10090b57cec5SDimitry Andric // High-Level Operations
10100b57cec5SDimitry Andric 
ExecuteAction(FrontendAction & Act)10110b57cec5SDimitry Andric bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
10120b57cec5SDimitry Andric   assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
10130b57cec5SDimitry Andric   assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
10140b57cec5SDimitry Andric   assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
10150b57cec5SDimitry Andric 
1016a7dea167SDimitry Andric   // Mark this point as the bottom of the stack if we don't have somewhere
1017a7dea167SDimitry Andric   // better. We generally expect frontend actions to be invoked with (nearly)
1018a7dea167SDimitry Andric   // DesiredStackSpace available.
1019a7dea167SDimitry Andric   noteBottomOfStack();
1020a7dea167SDimitry Andric 
102104eeddc0SDimitry Andric   auto FinishDiagnosticClient = llvm::make_scope_exit([&]() {
102204eeddc0SDimitry Andric     // Notify the diagnostic client that all files were processed.
102304eeddc0SDimitry Andric     getDiagnosticClient().finish();
102404eeddc0SDimitry Andric   });
102504eeddc0SDimitry Andric 
1026a7dea167SDimitry Andric   raw_ostream &OS = getVerboseOutputStream();
10270b57cec5SDimitry Andric 
10280b57cec5SDimitry Andric   if (!Act.PrepareToExecute(*this))
10290b57cec5SDimitry Andric     return false;
10300b57cec5SDimitry Andric 
1031fe6060f1SDimitry Andric   if (!createTarget())
10320b57cec5SDimitry Andric     return false;
10330b57cec5SDimitry Andric 
10340b57cec5SDimitry Andric   // rewriter project will change target built-in bool type from its default.
10350b57cec5SDimitry Andric   if (getFrontendOpts().ProgramAction == frontend::RewriteObjC)
10360b57cec5SDimitry Andric     getTarget().noSignedCharForObjCBool();
10370b57cec5SDimitry Andric 
10380b57cec5SDimitry Andric   // Validate/process some options.
10390b57cec5SDimitry Andric   if (getHeaderSearchOpts().Verbose)
1040bdd1243dSDimitry Andric     OS << "clang -cc1 version " CLANG_VERSION_STRING << " based upon LLVM "
1041bdd1243dSDimitry Andric        << LLVM_VERSION_STRING << " default target "
1042bdd1243dSDimitry Andric        << llvm::sys::getDefaultTargetTriple() << "\n";
10430b57cec5SDimitry Andric 
1044e8d8bef9SDimitry Andric   if (getCodeGenOpts().TimePasses)
10450b57cec5SDimitry Andric     createFrontendTimer();
10460b57cec5SDimitry Andric 
10470b57cec5SDimitry Andric   if (getFrontendOpts().ShowStats || !getFrontendOpts().StatsFile.empty())
10480b57cec5SDimitry Andric     llvm::EnableStatistics(false);
10490b57cec5SDimitry Andric 
10500b57cec5SDimitry Andric   for (const FrontendInputFile &FIF : getFrontendOpts().Inputs) {
10510b57cec5SDimitry Andric     // Reset the ID tables if we are reusing the SourceManager and parsing
10520b57cec5SDimitry Andric     // regular files.
10530b57cec5SDimitry Andric     if (hasSourceManager() && !Act.isModelParsingAction())
10540b57cec5SDimitry Andric       getSourceManager().clearIDTables();
10550b57cec5SDimitry Andric 
10560b57cec5SDimitry Andric     if (Act.BeginSourceFile(*this, FIF)) {
10570b57cec5SDimitry Andric       if (llvm::Error Err = Act.Execute()) {
10580b57cec5SDimitry Andric         consumeError(std::move(Err)); // FIXME this drops errors on the floor.
10590b57cec5SDimitry Andric       }
10600b57cec5SDimitry Andric       Act.EndSourceFile();
10610b57cec5SDimitry Andric     }
10620b57cec5SDimitry Andric   }
10630b57cec5SDimitry Andric 
10640b57cec5SDimitry Andric   if (getDiagnosticOpts().ShowCarets) {
10650b57cec5SDimitry Andric     // We can have multiple diagnostics sharing one diagnostic client.
10660b57cec5SDimitry Andric     // Get the total number of warnings/errors from the client.
10670b57cec5SDimitry Andric     unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings();
10680b57cec5SDimitry Andric     unsigned NumErrors = getDiagnostics().getClient()->getNumErrors();
10690b57cec5SDimitry Andric 
10700b57cec5SDimitry Andric     if (NumWarnings)
10710b57cec5SDimitry Andric       OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s");
10720b57cec5SDimitry Andric     if (NumWarnings && NumErrors)
10730b57cec5SDimitry Andric       OS << " and ";
10740b57cec5SDimitry Andric     if (NumErrors)
10750b57cec5SDimitry Andric       OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s");
10760b57cec5SDimitry Andric     if (NumWarnings || NumErrors) {
10770b57cec5SDimitry Andric       OS << " generated";
10780b57cec5SDimitry Andric       if (getLangOpts().CUDA) {
10790b57cec5SDimitry Andric         if (!getLangOpts().CUDAIsDevice) {
10800b57cec5SDimitry Andric           OS << " when compiling for host";
10810b57cec5SDimitry Andric         } else {
10820b57cec5SDimitry Andric           OS << " when compiling for " << getTargetOpts().CPU;
10830b57cec5SDimitry Andric         }
10840b57cec5SDimitry Andric       }
10850b57cec5SDimitry Andric       OS << ".\n";
10860b57cec5SDimitry Andric     }
10870b57cec5SDimitry Andric   }
10880b57cec5SDimitry Andric 
10890b57cec5SDimitry Andric   if (getFrontendOpts().ShowStats) {
10900b57cec5SDimitry Andric     if (hasFileManager()) {
10910b57cec5SDimitry Andric       getFileManager().PrintStats();
10920b57cec5SDimitry Andric       OS << '\n';
10930b57cec5SDimitry Andric     }
10940b57cec5SDimitry Andric     llvm::PrintStatistics(OS);
10950b57cec5SDimitry Andric   }
10960b57cec5SDimitry Andric   StringRef StatsFile = getFrontendOpts().StatsFile;
10970b57cec5SDimitry Andric   if (!StatsFile.empty()) {
109806c3fb27SDimitry Andric     llvm::sys::fs::OpenFlags FileFlags = llvm::sys::fs::OF_TextWithCRLF;
109906c3fb27SDimitry Andric     if (getFrontendOpts().AppendStats)
110006c3fb27SDimitry Andric       FileFlags |= llvm::sys::fs::OF_Append;
11010b57cec5SDimitry Andric     std::error_code EC;
110206c3fb27SDimitry Andric     auto StatS =
110306c3fb27SDimitry Andric         std::make_unique<llvm::raw_fd_ostream>(StatsFile, EC, FileFlags);
11040b57cec5SDimitry Andric     if (EC) {
11050b57cec5SDimitry Andric       getDiagnostics().Report(diag::warn_fe_unable_to_open_stats_file)
11060b57cec5SDimitry Andric           << StatsFile << EC.message();
11070b57cec5SDimitry Andric     } else {
11080b57cec5SDimitry Andric       llvm::PrintStatisticsJSON(*StatS);
11090b57cec5SDimitry Andric     }
11100b57cec5SDimitry Andric   }
11110b57cec5SDimitry Andric 
11120b57cec5SDimitry Andric   return !getDiagnostics().getClient()->getNumErrors();
11130b57cec5SDimitry Andric }
11140b57cec5SDimitry Andric 
LoadRequestedPlugins()1115349cc55cSDimitry Andric void CompilerInstance::LoadRequestedPlugins() {
1116349cc55cSDimitry Andric   // Load any requested plugins.
1117349cc55cSDimitry Andric   for (const std::string &Path : getFrontendOpts().Plugins) {
1118349cc55cSDimitry Andric     std::string Error;
1119349cc55cSDimitry Andric     if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
1120349cc55cSDimitry Andric       getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
1121349cc55cSDimitry Andric           << Path << Error;
1122349cc55cSDimitry Andric   }
1123349cc55cSDimitry Andric 
1124349cc55cSDimitry Andric   // Check if any of the loaded plugins replaces the main AST action
1125349cc55cSDimitry Andric   for (const FrontendPluginRegistry::entry &Plugin :
1126349cc55cSDimitry Andric        FrontendPluginRegistry::entries()) {
1127349cc55cSDimitry Andric     std::unique_ptr<PluginASTAction> P(Plugin.instantiate());
1128349cc55cSDimitry Andric     if (P->getActionType() == PluginASTAction::ReplaceAction) {
1129349cc55cSDimitry Andric       getFrontendOpts().ProgramAction = clang::frontend::PluginAction;
1130349cc55cSDimitry Andric       getFrontendOpts().ActionName = Plugin.getName().str();
1131349cc55cSDimitry Andric       break;
1132349cc55cSDimitry Andric     }
1133349cc55cSDimitry Andric   }
1134349cc55cSDimitry Andric }
1135349cc55cSDimitry Andric 
11360b57cec5SDimitry Andric /// Determine the appropriate source input kind based on language
11370b57cec5SDimitry Andric /// options.
getLanguageFromOptions(const LangOptions & LangOpts)1138a7dea167SDimitry Andric static Language getLanguageFromOptions(const LangOptions &LangOpts) {
11390b57cec5SDimitry Andric   if (LangOpts.OpenCL)
1140a7dea167SDimitry Andric     return Language::OpenCL;
11410b57cec5SDimitry Andric   if (LangOpts.CUDA)
1142a7dea167SDimitry Andric     return Language::CUDA;
11430b57cec5SDimitry Andric   if (LangOpts.ObjC)
1144a7dea167SDimitry Andric     return LangOpts.CPlusPlus ? Language::ObjCXX : Language::ObjC;
1145a7dea167SDimitry Andric   return LangOpts.CPlusPlus ? Language::CXX : Language::C;
11460b57cec5SDimitry Andric }
11470b57cec5SDimitry Andric 
11480b57cec5SDimitry Andric /// Compile a module file for the given module, using the options
11490b57cec5SDimitry Andric /// provided by the importing compiler instance. Returns true if the module
11500b57cec5SDimitry Andric /// was built without errors.
11510b57cec5SDimitry Andric static bool
compileModuleImpl(CompilerInstance & ImportingInstance,SourceLocation ImportLoc,StringRef ModuleName,FrontendInputFile Input,StringRef OriginalModuleMapFile,StringRef ModuleFileName,llvm::function_ref<void (CompilerInstance &)> PreBuildStep=[](CompilerInstance &){},llvm::function_ref<void (CompilerInstance &)> PostBuildStep=[](CompilerInstance &){})11520b57cec5SDimitry Andric compileModuleImpl(CompilerInstance &ImportingInstance, SourceLocation ImportLoc,
11530b57cec5SDimitry Andric                   StringRef ModuleName, FrontendInputFile Input,
11540b57cec5SDimitry Andric                   StringRef OriginalModuleMapFile, StringRef ModuleFileName,
11550b57cec5SDimitry Andric                   llvm::function_ref<void(CompilerInstance &)> PreBuildStep =
11560b57cec5SDimitry Andric                       [](CompilerInstance &) {},
11570b57cec5SDimitry Andric                   llvm::function_ref<void(CompilerInstance &)> PostBuildStep =
__anonacda6b810502(CompilerInstance &) 11580b57cec5SDimitry Andric                       [](CompilerInstance &) {}) {
11590b57cec5SDimitry Andric   llvm::TimeTraceScope TimeScope("Module Compile", ModuleName);
11600b57cec5SDimitry Andric 
1161fe6060f1SDimitry Andric   // Never compile a module that's already finalized - this would cause the
1162fe6060f1SDimitry Andric   // existing module to be freed, causing crashes if it is later referenced
1163fe6060f1SDimitry Andric   if (ImportingInstance.getModuleCache().isPCMFinal(ModuleFileName)) {
1164fe6060f1SDimitry Andric     ImportingInstance.getDiagnostics().Report(
1165fe6060f1SDimitry Andric         ImportLoc, diag::err_module_rebuild_finalized)
1166fe6060f1SDimitry Andric         << ModuleName;
1167fe6060f1SDimitry Andric     return false;
1168fe6060f1SDimitry Andric   }
1169fe6060f1SDimitry Andric 
11700b57cec5SDimitry Andric   // Construct a compiler invocation for creating this module.
11710b57cec5SDimitry Andric   auto Invocation =
11720b57cec5SDimitry Andric       std::make_shared<CompilerInvocation>(ImportingInstance.getInvocation());
11730b57cec5SDimitry Andric 
11740b57cec5SDimitry Andric   PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
11750b57cec5SDimitry Andric 
11760b57cec5SDimitry Andric   // For any options that aren't intended to affect how a module is built,
11770b57cec5SDimitry Andric   // reset them to their default values.
1178bdd1243dSDimitry Andric   Invocation->resetNonModularOptions();
11790b57cec5SDimitry Andric 
11800b57cec5SDimitry Andric   // Remove any macro definitions that are explicitly ignored by the module.
11810b57cec5SDimitry Andric   // They aren't supposed to affect how the module is built anyway.
11820b57cec5SDimitry Andric   HeaderSearchOptions &HSOpts = Invocation->getHeaderSearchOpts();
11830eae32dcSDimitry Andric   llvm::erase_if(PPOpts.Macros,
__anonacda6b810602(const std::pair<std::string, bool> &def) 11840eae32dcSDimitry Andric                  [&HSOpts](const std::pair<std::string, bool> &def) {
11850b57cec5SDimitry Andric                    StringRef MacroDef = def.first;
11860eae32dcSDimitry Andric                    return HSOpts.ModulesIgnoreMacros.contains(
11870eae32dcSDimitry Andric                        llvm::CachedHashString(MacroDef.split('=').first));
1188349cc55cSDimitry Andric                  });
11890b57cec5SDimitry Andric 
11900b57cec5SDimitry Andric   // If the original compiler invocation had -fmodule-name, pass it through.
11915f757f3fSDimitry Andric   Invocation->getLangOpts().ModuleName =
11925f757f3fSDimitry Andric       ImportingInstance.getInvocation().getLangOpts().ModuleName;
11930b57cec5SDimitry Andric 
11940b57cec5SDimitry Andric   // Note the name of the module we're building.
11955f757f3fSDimitry Andric   Invocation->getLangOpts().CurrentModule = std::string(ModuleName);
11960b57cec5SDimitry Andric 
11970b57cec5SDimitry Andric   // Make sure that the failed-module structure has been allocated in
11980b57cec5SDimitry Andric   // the importing instance, and propagate the pointer to the newly-created
11990b57cec5SDimitry Andric   // instance.
12000b57cec5SDimitry Andric   PreprocessorOptions &ImportingPPOpts
12010b57cec5SDimitry Andric     = ImportingInstance.getInvocation().getPreprocessorOpts();
12020b57cec5SDimitry Andric   if (!ImportingPPOpts.FailedModules)
12030b57cec5SDimitry Andric     ImportingPPOpts.FailedModules =
12040b57cec5SDimitry Andric         std::make_shared<PreprocessorOptions::FailedModulesSet>();
12050b57cec5SDimitry Andric   PPOpts.FailedModules = ImportingPPOpts.FailedModules;
12060b57cec5SDimitry Andric 
12070b57cec5SDimitry Andric   // If there is a module map file, build the module using the module map.
12080b57cec5SDimitry Andric   // Set up the inputs/outputs so that we build the module from its umbrella
12090b57cec5SDimitry Andric   // header.
12100b57cec5SDimitry Andric   FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
12110b57cec5SDimitry Andric   FrontendOpts.OutputFile = ModuleFileName.str();
12120b57cec5SDimitry Andric   FrontendOpts.DisableFree = false;
12130b57cec5SDimitry Andric   FrontendOpts.GenerateGlobalModuleIndex = false;
12140b57cec5SDimitry Andric   FrontendOpts.BuildingImplicitModule = true;
12155ffd83dbSDimitry Andric   FrontendOpts.OriginalModuleMap = std::string(OriginalModuleMapFile);
12160b57cec5SDimitry Andric   // Force implicitly-built modules to hash the content of the module file.
12170b57cec5SDimitry Andric   HSOpts.ModulesHashContent = true;
12180b57cec5SDimitry Andric   FrontendOpts.Inputs = {Input};
12190b57cec5SDimitry Andric 
12200b57cec5SDimitry Andric   // Don't free the remapped file buffers; they are owned by our caller.
12210b57cec5SDimitry Andric   PPOpts.RetainRemappedFileBuffers = true;
12220b57cec5SDimitry Andric 
12235f757f3fSDimitry Andric   DiagnosticOptions &DiagOpts = Invocation->getDiagnosticOpts();
12245f757f3fSDimitry Andric 
12255f757f3fSDimitry Andric   DiagOpts.VerifyDiagnostics = 0;
12260b57cec5SDimitry Andric   assert(ImportingInstance.getInvocation().getModuleHash() ==
12270b57cec5SDimitry Andric          Invocation->getModuleHash() && "Module hash mismatch!");
12280b57cec5SDimitry Andric 
12290b57cec5SDimitry Andric   // Construct a compiler instance that will be used to actually create the
12300b57cec5SDimitry Andric   // module.  Since we're sharing an in-memory module cache,
12310b57cec5SDimitry Andric   // CompilerInstance::CompilerInstance is responsible for finalizing the
12320b57cec5SDimitry Andric   // buffers to prevent use-after-frees.
12330b57cec5SDimitry Andric   CompilerInstance Instance(ImportingInstance.getPCHContainerOperations(),
12340b57cec5SDimitry Andric                             &ImportingInstance.getModuleCache());
12350b57cec5SDimitry Andric   auto &Inv = *Invocation;
12360b57cec5SDimitry Andric   Instance.setInvocation(std::move(Invocation));
12370b57cec5SDimitry Andric 
12380b57cec5SDimitry Andric   Instance.createDiagnostics(new ForwardingDiagnosticConsumer(
12390b57cec5SDimitry Andric                                    ImportingInstance.getDiagnosticClient()),
12400b57cec5SDimitry Andric                              /*ShouldOwnClient=*/true);
12410b57cec5SDimitry Andric 
12425f757f3fSDimitry Andric   if (llvm::is_contained(DiagOpts.SystemHeaderWarningsModules, ModuleName))
12435f757f3fSDimitry Andric     Instance.getDiagnostics().setSuppressSystemWarnings(false);
12445f757f3fSDimitry Andric 
1245bdd1243dSDimitry Andric   if (FrontendOpts.ModulesShareFileManager) {
12460b57cec5SDimitry Andric     Instance.setFileManager(&ImportingInstance.getFileManager());
1247bdd1243dSDimitry Andric   } else {
1248bdd1243dSDimitry Andric     Instance.createFileManager(&ImportingInstance.getVirtualFileSystem());
1249bdd1243dSDimitry Andric   }
12500b57cec5SDimitry Andric   Instance.createSourceManager(Instance.getFileManager());
12510b57cec5SDimitry Andric   SourceManager &SourceMgr = Instance.getSourceManager();
1252bdd1243dSDimitry Andric 
1253bdd1243dSDimitry Andric   // Note that this module is part of the module build stack, so that we
1254bdd1243dSDimitry Andric   // can detect cycles in the module graph.
12550b57cec5SDimitry Andric   SourceMgr.setModuleBuildStack(
12560b57cec5SDimitry Andric     ImportingInstance.getSourceManager().getModuleBuildStack());
12570b57cec5SDimitry Andric   SourceMgr.pushModuleBuildStack(ModuleName,
12580b57cec5SDimitry Andric     FullSourceLoc(ImportLoc, ImportingInstance.getSourceManager()));
12590b57cec5SDimitry Andric 
12600b57cec5SDimitry Andric   // If we're collecting module dependencies, we need to share a collector
12610b57cec5SDimitry Andric   // between all of the module CompilerInstances. Other than that, we don't
12620b57cec5SDimitry Andric   // want to produce any dependency output from the module build.
12630b57cec5SDimitry Andric   Instance.setModuleDepCollector(ImportingInstance.getModuleDepCollector());
12640b57cec5SDimitry Andric   Inv.getDependencyOutputOpts() = DependencyOutputOptions();
12650b57cec5SDimitry Andric 
12660b57cec5SDimitry Andric   ImportingInstance.getDiagnostics().Report(ImportLoc,
12670b57cec5SDimitry Andric                                             diag::remark_module_build)
12680b57cec5SDimitry Andric     << ModuleName << ModuleFileName;
12690b57cec5SDimitry Andric 
12700b57cec5SDimitry Andric   PreBuildStep(Instance);
12710b57cec5SDimitry Andric 
12720b57cec5SDimitry Andric   // Execute the action to actually build the module in-place. Use a separate
12730b57cec5SDimitry Andric   // thread so that we get a stack large enough.
1274753f127fSDimitry Andric   bool Crashed = !llvm::CrashRecoveryContext().RunSafelyOnThread(
__anonacda6b810702() 12750b57cec5SDimitry Andric       [&]() {
12760b57cec5SDimitry Andric         GenerateModuleFromModuleMapAction Action;
12770b57cec5SDimitry Andric         Instance.ExecuteAction(Action);
12780b57cec5SDimitry Andric       },
12790b57cec5SDimitry Andric       DesiredStackSize);
12800b57cec5SDimitry Andric 
12810b57cec5SDimitry Andric   PostBuildStep(Instance);
12820b57cec5SDimitry Andric 
12830b57cec5SDimitry Andric   ImportingInstance.getDiagnostics().Report(ImportLoc,
12840b57cec5SDimitry Andric                                             diag::remark_module_build_done)
12850b57cec5SDimitry Andric     << ModuleName;
12860b57cec5SDimitry Andric 
1287753f127fSDimitry Andric   if (Crashed) {
1288753f127fSDimitry Andric     // Clear the ASTConsumer if it hasn't been already, in case it owns streams
1289753f127fSDimitry Andric     // that must be closed before clearing output files.
1290753f127fSDimitry Andric     Instance.setSema(nullptr);
1291753f127fSDimitry Andric     Instance.setASTConsumer(nullptr);
1292753f127fSDimitry Andric 
1293753f127fSDimitry Andric     // Delete any remaining temporary files related to Instance.
12940b57cec5SDimitry Andric     Instance.clearOutputFiles(/*EraseFiles=*/true);
1295753f127fSDimitry Andric   }
12960b57cec5SDimitry Andric 
1297fe6060f1SDimitry Andric   // If \p AllowPCMWithCompilerErrors is set return 'success' even if errors
1298fe6060f1SDimitry Andric   // occurred.
1299fe6060f1SDimitry Andric   return !Instance.getDiagnostics().hasErrorOccurred() ||
1300fe6060f1SDimitry Andric          Instance.getFrontendOpts().AllowPCMWithCompilerErrors;
13010b57cec5SDimitry Andric }
13020b57cec5SDimitry Andric 
getPublicModuleMap(FileEntryRef File,FileManager & FileMgr)1303bdd1243dSDimitry Andric static OptionalFileEntryRef getPublicModuleMap(FileEntryRef File,
13040b57cec5SDimitry Andric                                                FileManager &FileMgr) {
1305bdd1243dSDimitry Andric   StringRef Filename = llvm::sys::path::filename(File.getName());
1306bdd1243dSDimitry Andric   SmallString<128> PublicFilename(File.getDir().getName());
13070b57cec5SDimitry Andric   if (Filename == "module_private.map")
13080b57cec5SDimitry Andric     llvm::sys::path::append(PublicFilename, "module.map");
13090b57cec5SDimitry Andric   else if (Filename == "module.private.modulemap")
13100b57cec5SDimitry Andric     llvm::sys::path::append(PublicFilename, "module.modulemap");
13110b57cec5SDimitry Andric   else
1312bdd1243dSDimitry Andric     return std::nullopt;
1313bdd1243dSDimitry Andric   return FileMgr.getOptionalFileRef(PublicFilename);
13140b57cec5SDimitry Andric }
13150b57cec5SDimitry Andric 
1316480093f4SDimitry Andric /// Compile a module file for the given module in a separate compiler instance,
1317480093f4SDimitry Andric /// using the options provided by the importing compiler instance. Returns true
1318480093f4SDimitry Andric /// if the module was built without errors.
compileModule(CompilerInstance & ImportingInstance,SourceLocation ImportLoc,Module * Module,StringRef ModuleFileName)1319480093f4SDimitry Andric static bool compileModule(CompilerInstance &ImportingInstance,
1320480093f4SDimitry Andric                           SourceLocation ImportLoc, Module *Module,
13210b57cec5SDimitry Andric                           StringRef ModuleFileName) {
13220b57cec5SDimitry Andric   InputKind IK(getLanguageFromOptions(ImportingInstance.getLangOpts()),
13230b57cec5SDimitry Andric                InputKind::ModuleMap);
13240b57cec5SDimitry Andric 
13250b57cec5SDimitry Andric   // Get or create the module map that we'll use to build this module.
13260b57cec5SDimitry Andric   ModuleMap &ModMap
13270b57cec5SDimitry Andric     = ImportingInstance.getPreprocessor().getHeaderSearchInfo().getModuleMap();
13280b57cec5SDimitry Andric   bool Result;
1329bdd1243dSDimitry Andric   if (OptionalFileEntryRef ModuleMapFile =
13300b57cec5SDimitry Andric           ModMap.getContainingModuleMapFile(Module)) {
13310b57cec5SDimitry Andric     // Canonicalize compilation to start with the public module map. This is
13320b57cec5SDimitry Andric     // vital for submodules declarations in the private module maps to be
13330b57cec5SDimitry Andric     // correctly parsed when depending on a top level module in the public one.
1334bdd1243dSDimitry Andric     if (OptionalFileEntryRef PublicMMFile = getPublicModuleMap(
1335bdd1243dSDimitry Andric             *ModuleMapFile, ImportingInstance.getFileManager()))
13360b57cec5SDimitry Andric       ModuleMapFile = PublicMMFile;
13370b57cec5SDimitry Andric 
1338bdd1243dSDimitry Andric     StringRef ModuleMapFilePath = ModuleMapFile->getNameAsRequested();
1339bdd1243dSDimitry Andric 
13400b57cec5SDimitry Andric     // Use the module map where this module resides.
13410b57cec5SDimitry Andric     Result = compileModuleImpl(
13420b57cec5SDimitry Andric         ImportingInstance, ImportLoc, Module->getTopLevelModuleName(),
1343bdd1243dSDimitry Andric         FrontendInputFile(ModuleMapFilePath, IK, +Module->IsSystem),
1344bdd1243dSDimitry Andric         ModMap.getModuleMapFileForUniquing(Module)->getName(), ModuleFileName);
13450b57cec5SDimitry Andric   } else {
13460b57cec5SDimitry Andric     // FIXME: We only need to fake up an input file here as a way of
13470b57cec5SDimitry Andric     // transporting the module's directory to the module map parser. We should
13480b57cec5SDimitry Andric     // be able to do that more directly, and parse from a memory buffer without
13490b57cec5SDimitry Andric     // inventing this file.
13500b57cec5SDimitry Andric     SmallString<128> FakeModuleMapFile(Module->Directory->getName());
13510b57cec5SDimitry Andric     llvm::sys::path::append(FakeModuleMapFile, "__inferred_module.map");
13520b57cec5SDimitry Andric 
13530b57cec5SDimitry Andric     std::string InferredModuleMapContent;
13540b57cec5SDimitry Andric     llvm::raw_string_ostream OS(InferredModuleMapContent);
13550b57cec5SDimitry Andric     Module->print(OS);
13560b57cec5SDimitry Andric     OS.flush();
13570b57cec5SDimitry Andric 
13580b57cec5SDimitry Andric     Result = compileModuleImpl(
13590b57cec5SDimitry Andric         ImportingInstance, ImportLoc, Module->getTopLevelModuleName(),
13600b57cec5SDimitry Andric         FrontendInputFile(FakeModuleMapFile, IK, +Module->IsSystem),
13610b57cec5SDimitry Andric         ModMap.getModuleMapFileForUniquing(Module)->getName(),
13620b57cec5SDimitry Andric         ModuleFileName,
13630b57cec5SDimitry Andric         [&](CompilerInstance &Instance) {
13640b57cec5SDimitry Andric       std::unique_ptr<llvm::MemoryBuffer> ModuleMapBuffer =
13650b57cec5SDimitry Andric           llvm::MemoryBuffer::getMemBuffer(InferredModuleMapContent);
13665f757f3fSDimitry Andric       FileEntryRef ModuleMapFile = Instance.getFileManager().getVirtualFileRef(
13670b57cec5SDimitry Andric           FakeModuleMapFile, InferredModuleMapContent.size(), 0);
13680b57cec5SDimitry Andric       Instance.getSourceManager().overrideFileContents(
13690b57cec5SDimitry Andric           ModuleMapFile, std::move(ModuleMapBuffer));
13700b57cec5SDimitry Andric     });
13710b57cec5SDimitry Andric   }
13720b57cec5SDimitry Andric 
13730b57cec5SDimitry Andric   // We've rebuilt a module. If we're allowed to generate or update the global
13740b57cec5SDimitry Andric   // module index, record that fact in the importing compiler instance.
13750b57cec5SDimitry Andric   if (ImportingInstance.getFrontendOpts().GenerateGlobalModuleIndex) {
13760b57cec5SDimitry Andric     ImportingInstance.setBuildGlobalModuleIndex(true);
13770b57cec5SDimitry Andric   }
13780b57cec5SDimitry Andric 
13790b57cec5SDimitry Andric   return Result;
13800b57cec5SDimitry Andric }
13810b57cec5SDimitry Andric 
1382349cc55cSDimitry Andric /// Read the AST right after compiling the module.
readASTAfterCompileModule(CompilerInstance & ImportingInstance,SourceLocation ImportLoc,SourceLocation ModuleNameLoc,Module * Module,StringRef ModuleFileName,bool * OutOfDate)1383349cc55cSDimitry Andric static bool readASTAfterCompileModule(CompilerInstance &ImportingInstance,
1384349cc55cSDimitry Andric                                       SourceLocation ImportLoc,
1385349cc55cSDimitry Andric                                       SourceLocation ModuleNameLoc,
1386349cc55cSDimitry Andric                                       Module *Module, StringRef ModuleFileName,
1387349cc55cSDimitry Andric                                       bool *OutOfDate) {
1388349cc55cSDimitry Andric   DiagnosticsEngine &Diags = ImportingInstance.getDiagnostics();
1389349cc55cSDimitry Andric 
1390349cc55cSDimitry Andric   unsigned ModuleLoadCapabilities = ASTReader::ARR_Missing;
1391349cc55cSDimitry Andric   if (OutOfDate)
1392349cc55cSDimitry Andric     ModuleLoadCapabilities |= ASTReader::ARR_OutOfDate;
1393349cc55cSDimitry Andric 
1394349cc55cSDimitry Andric   // Try to read the module file, now that we've compiled it.
1395349cc55cSDimitry Andric   ASTReader::ASTReadResult ReadResult =
1396349cc55cSDimitry Andric       ImportingInstance.getASTReader()->ReadAST(
1397349cc55cSDimitry Andric           ModuleFileName, serialization::MK_ImplicitModule, ImportLoc,
1398349cc55cSDimitry Andric           ModuleLoadCapabilities);
1399349cc55cSDimitry Andric   if (ReadResult == ASTReader::Success)
1400349cc55cSDimitry Andric     return true;
1401349cc55cSDimitry Andric 
1402349cc55cSDimitry Andric   // The caller wants to handle out-of-date failures.
1403349cc55cSDimitry Andric   if (OutOfDate && ReadResult == ASTReader::OutOfDate) {
1404349cc55cSDimitry Andric     *OutOfDate = true;
1405349cc55cSDimitry Andric     return false;
1406349cc55cSDimitry Andric   }
1407349cc55cSDimitry Andric 
1408349cc55cSDimitry Andric   // The ASTReader didn't diagnose the error, so conservatively report it.
1409349cc55cSDimitry Andric   if (ReadResult == ASTReader::Missing || !Diags.hasErrorOccurred())
1410349cc55cSDimitry Andric     Diags.Report(ModuleNameLoc, diag::err_module_not_built)
1411349cc55cSDimitry Andric       << Module->Name << SourceRange(ImportLoc, ModuleNameLoc);
1412349cc55cSDimitry Andric 
1413349cc55cSDimitry Andric   return false;
1414349cc55cSDimitry Andric }
1415349cc55cSDimitry Andric 
1416480093f4SDimitry Andric /// Compile a module in a separate compiler instance and read the AST,
1417480093f4SDimitry Andric /// returning true if the module compiles without errors.
compileModuleAndReadASTImpl(CompilerInstance & ImportingInstance,SourceLocation ImportLoc,SourceLocation ModuleNameLoc,Module * Module,StringRef ModuleFileName)1418349cc55cSDimitry Andric static bool compileModuleAndReadASTImpl(CompilerInstance &ImportingInstance,
1419349cc55cSDimitry Andric                                         SourceLocation ImportLoc,
1420349cc55cSDimitry Andric                                         SourceLocation ModuleNameLoc,
1421349cc55cSDimitry Andric                                         Module *Module,
1422349cc55cSDimitry Andric                                         StringRef ModuleFileName) {
1423349cc55cSDimitry Andric   if (!compileModule(ImportingInstance, ModuleNameLoc, Module,
1424349cc55cSDimitry Andric                      ModuleFileName)) {
1425349cc55cSDimitry Andric     ImportingInstance.getDiagnostics().Report(ModuleNameLoc,
1426349cc55cSDimitry Andric                                               diag::err_module_not_built)
1427349cc55cSDimitry Andric         << Module->Name << SourceRange(ImportLoc, ModuleNameLoc);
1428349cc55cSDimitry Andric     return false;
1429349cc55cSDimitry Andric   }
1430349cc55cSDimitry Andric 
1431349cc55cSDimitry Andric   return readASTAfterCompileModule(ImportingInstance, ImportLoc, ModuleNameLoc,
1432349cc55cSDimitry Andric                                    Module, ModuleFileName,
1433349cc55cSDimitry Andric                                    /*OutOfDate=*/nullptr);
1434349cc55cSDimitry Andric }
1435349cc55cSDimitry Andric 
1436349cc55cSDimitry Andric /// Compile a module in a separate compiler instance and read the AST,
1437349cc55cSDimitry Andric /// returning true if the module compiles without errors, using a lock manager
1438349cc55cSDimitry Andric /// to avoid building the same module in multiple compiler instances.
1439480093f4SDimitry Andric ///
1440480093f4SDimitry Andric /// Uses a lock file manager and exponential backoff to reduce the chances that
1441480093f4SDimitry Andric /// multiple instances will compete to create the same module.  On timeout,
1442480093f4SDimitry Andric /// deletes the lock file in order to avoid deadlock from crashing processes or
1443480093f4SDimitry Andric /// bugs in the lock file manager.
compileModuleAndReadASTBehindLock(CompilerInstance & ImportingInstance,SourceLocation ImportLoc,SourceLocation ModuleNameLoc,Module * Module,StringRef ModuleFileName)1444349cc55cSDimitry Andric static bool compileModuleAndReadASTBehindLock(
1445349cc55cSDimitry Andric     CompilerInstance &ImportingInstance, SourceLocation ImportLoc,
1446349cc55cSDimitry Andric     SourceLocation ModuleNameLoc, Module *Module, StringRef ModuleFileName) {
14470b57cec5SDimitry Andric   DiagnosticsEngine &Diags = ImportingInstance.getDiagnostics();
14480b57cec5SDimitry Andric 
1449349cc55cSDimitry Andric   Diags.Report(ModuleNameLoc, diag::remark_module_lock)
1450349cc55cSDimitry Andric       << ModuleFileName << Module->Name;
14510b57cec5SDimitry Andric 
14520b57cec5SDimitry Andric   // FIXME: have LockFileManager return an error_code so that we can
14530b57cec5SDimitry Andric   // avoid the mkdir when the directory already exists.
14540b57cec5SDimitry Andric   StringRef Dir = llvm::sys::path::parent_path(ModuleFileName);
14550b57cec5SDimitry Andric   llvm::sys::fs::create_directories(Dir);
14560b57cec5SDimitry Andric 
145704eeddc0SDimitry Andric   while (true) {
14580b57cec5SDimitry Andric     llvm::LockFileManager Locked(ModuleFileName);
14590b57cec5SDimitry Andric     switch (Locked) {
14600b57cec5SDimitry Andric     case llvm::LockFileManager::LFS_Error:
14610b57cec5SDimitry Andric       // ModuleCache takes care of correctness and locks are only necessary for
14620b57cec5SDimitry Andric       // performance. Fallback to building the module in case of any lock
14630b57cec5SDimitry Andric       // related errors.
14640b57cec5SDimitry Andric       Diags.Report(ModuleNameLoc, diag::remark_module_lock_failure)
14650b57cec5SDimitry Andric           << Module->Name << Locked.getErrorMessage();
14660b57cec5SDimitry Andric       // Clear out any potential leftover.
14670b57cec5SDimitry Andric       Locked.unsafeRemoveLockFile();
1468bdd1243dSDimitry Andric       [[fallthrough]];
14690b57cec5SDimitry Andric     case llvm::LockFileManager::LFS_Owned:
14700b57cec5SDimitry Andric       // We're responsible for building the module ourselves.
1471349cc55cSDimitry Andric       return compileModuleAndReadASTImpl(ImportingInstance, ImportLoc,
1472349cc55cSDimitry Andric                                          ModuleNameLoc, Module, ModuleFileName);
14730b57cec5SDimitry Andric 
14740b57cec5SDimitry Andric     case llvm::LockFileManager::LFS_Shared:
1475349cc55cSDimitry Andric       break; // The interesting case.
1476349cc55cSDimitry Andric     }
1477349cc55cSDimitry Andric 
14780b57cec5SDimitry Andric     // Someone else is responsible for building the module. Wait for them to
14790b57cec5SDimitry Andric     // finish.
14800b57cec5SDimitry Andric     switch (Locked.waitForUnlock()) {
14810b57cec5SDimitry Andric     case llvm::LockFileManager::Res_Success:
1482349cc55cSDimitry Andric       break; // The interesting case.
14830b57cec5SDimitry Andric     case llvm::LockFileManager::Res_OwnerDied:
14840b57cec5SDimitry Andric       continue; // try again to get the lock.
14850b57cec5SDimitry Andric     case llvm::LockFileManager::Res_Timeout:
14860b57cec5SDimitry Andric       // Since ModuleCache takes care of correctness, we try waiting for
14870b57cec5SDimitry Andric       // another process to complete the build so clang does not do it done
14880b57cec5SDimitry Andric       // twice. If case of timeout, build it ourselves.
14890b57cec5SDimitry Andric       Diags.Report(ModuleNameLoc, diag::remark_module_lock_timeout)
14900b57cec5SDimitry Andric           << Module->Name;
14910b57cec5SDimitry Andric       // Clear the lock file so that future invocations can make progress.
14920b57cec5SDimitry Andric       Locked.unsafeRemoveLockFile();
14930b57cec5SDimitry Andric       continue;
14940b57cec5SDimitry Andric     }
14950b57cec5SDimitry Andric 
1496349cc55cSDimitry Andric     // Read the module that was just written by someone else.
1497349cc55cSDimitry Andric     bool OutOfDate = false;
1498349cc55cSDimitry Andric     if (readASTAfterCompileModule(ImportingInstance, ImportLoc, ModuleNameLoc,
1499349cc55cSDimitry Andric                                   Module, ModuleFileName, &OutOfDate))
1500349cc55cSDimitry Andric       return true;
1501349cc55cSDimitry Andric     if (!OutOfDate)
1502349cc55cSDimitry Andric       return false;
15030b57cec5SDimitry Andric 
15040b57cec5SDimitry Andric     // The module may be out of date in the presence of file system races,
15050b57cec5SDimitry Andric     // or if one of its imports depends on header search paths that are not
15060b57cec5SDimitry Andric     // consistent with this ImportingInstance.  Try again...
15070b57cec5SDimitry Andric   }
15080b57cec5SDimitry Andric }
1509349cc55cSDimitry Andric 
1510349cc55cSDimitry Andric /// Compile a module in a separate compiler instance and read the AST,
1511349cc55cSDimitry Andric /// returning true if the module compiles without errors, potentially using a
1512349cc55cSDimitry Andric /// lock manager to avoid building the same module in multiple compiler
1513349cc55cSDimitry Andric /// instances.
compileModuleAndReadAST(CompilerInstance & ImportingInstance,SourceLocation ImportLoc,SourceLocation ModuleNameLoc,Module * Module,StringRef ModuleFileName)1514349cc55cSDimitry Andric static bool compileModuleAndReadAST(CompilerInstance &ImportingInstance,
1515349cc55cSDimitry Andric                                     SourceLocation ImportLoc,
1516349cc55cSDimitry Andric                                     SourceLocation ModuleNameLoc,
1517349cc55cSDimitry Andric                                     Module *Module, StringRef ModuleFileName) {
1518349cc55cSDimitry Andric   return ImportingInstance.getInvocation()
1519349cc55cSDimitry Andric                  .getFrontendOpts()
1520349cc55cSDimitry Andric                  .BuildingImplicitModuleUsesLock
1521349cc55cSDimitry Andric              ? compileModuleAndReadASTBehindLock(ImportingInstance, ImportLoc,
1522349cc55cSDimitry Andric                                                  ModuleNameLoc, Module,
1523349cc55cSDimitry Andric                                                  ModuleFileName)
1524349cc55cSDimitry Andric              : compileModuleAndReadASTImpl(ImportingInstance, ImportLoc,
1525349cc55cSDimitry Andric                                            ModuleNameLoc, Module,
1526349cc55cSDimitry Andric                                            ModuleFileName);
15270b57cec5SDimitry Andric }
15280b57cec5SDimitry Andric 
15290b57cec5SDimitry Andric /// Diagnose differences between the current definition of the given
15300b57cec5SDimitry Andric /// configuration macro and the definition provided on the command line.
checkConfigMacro(Preprocessor & PP,StringRef ConfigMacro,Module * Mod,SourceLocation ImportLoc)15310b57cec5SDimitry Andric static void checkConfigMacro(Preprocessor &PP, StringRef ConfigMacro,
15320b57cec5SDimitry Andric                              Module *Mod, SourceLocation ImportLoc) {
15330b57cec5SDimitry Andric   IdentifierInfo *Id = PP.getIdentifierInfo(ConfigMacro);
15340b57cec5SDimitry Andric   SourceManager &SourceMgr = PP.getSourceManager();
15350b57cec5SDimitry Andric 
15360b57cec5SDimitry Andric   // If this identifier has never had a macro definition, then it could
15370b57cec5SDimitry Andric   // not have changed.
15380b57cec5SDimitry Andric   if (!Id->hadMacroDefinition())
15390b57cec5SDimitry Andric     return;
15400b57cec5SDimitry Andric   auto *LatestLocalMD = PP.getLocalMacroDirectiveHistory(Id);
15410b57cec5SDimitry Andric 
15420b57cec5SDimitry Andric   // Find the macro definition from the command line.
15430b57cec5SDimitry Andric   MacroInfo *CmdLineDefinition = nullptr;
15440b57cec5SDimitry Andric   for (auto *MD = LatestLocalMD; MD; MD = MD->getPrevious()) {
15450b57cec5SDimitry Andric     // We only care about the predefines buffer.
15460b57cec5SDimitry Andric     FileID FID = SourceMgr.getFileID(MD->getLocation());
15470b57cec5SDimitry Andric     if (FID.isInvalid() || FID != PP.getPredefinesFileID())
15480b57cec5SDimitry Andric       continue;
15490b57cec5SDimitry Andric     if (auto *DMD = dyn_cast<DefMacroDirective>(MD))
15500b57cec5SDimitry Andric       CmdLineDefinition = DMD->getMacroInfo();
15510b57cec5SDimitry Andric     break;
15520b57cec5SDimitry Andric   }
15530b57cec5SDimitry Andric 
15540b57cec5SDimitry Andric   auto *CurrentDefinition = PP.getMacroInfo(Id);
15550b57cec5SDimitry Andric   if (CurrentDefinition == CmdLineDefinition) {
15560b57cec5SDimitry Andric     // Macro matches. Nothing to do.
15570b57cec5SDimitry Andric   } else if (!CurrentDefinition) {
15580b57cec5SDimitry Andric     // This macro was defined on the command line, then #undef'd later.
15590b57cec5SDimitry Andric     // Complain.
15600b57cec5SDimitry Andric     PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
15610b57cec5SDimitry Andric       << true << ConfigMacro << Mod->getFullModuleName();
15620b57cec5SDimitry Andric     auto LatestDef = LatestLocalMD->getDefinition();
15630b57cec5SDimitry Andric     assert(LatestDef.isUndefined() &&
15640b57cec5SDimitry Andric            "predefined macro went away with no #undef?");
15650b57cec5SDimitry Andric     PP.Diag(LatestDef.getUndefLocation(), diag::note_module_def_undef_here)
15660b57cec5SDimitry Andric       << true;
15670b57cec5SDimitry Andric     return;
15680b57cec5SDimitry Andric   } else if (!CmdLineDefinition) {
15690b57cec5SDimitry Andric     // There was no definition for this macro in the predefines buffer,
15700b57cec5SDimitry Andric     // but there was a local definition. Complain.
15710b57cec5SDimitry Andric     PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
15720b57cec5SDimitry Andric       << false << ConfigMacro << Mod->getFullModuleName();
15730b57cec5SDimitry Andric     PP.Diag(CurrentDefinition->getDefinitionLoc(),
15740b57cec5SDimitry Andric             diag::note_module_def_undef_here)
15750b57cec5SDimitry Andric       << false;
15760b57cec5SDimitry Andric   } else if (!CurrentDefinition->isIdenticalTo(*CmdLineDefinition, PP,
15770b57cec5SDimitry Andric                                                /*Syntactically=*/true)) {
15780b57cec5SDimitry Andric     // The macro definitions differ.
15790b57cec5SDimitry Andric     PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
15800b57cec5SDimitry Andric       << false << ConfigMacro << Mod->getFullModuleName();
15810b57cec5SDimitry Andric     PP.Diag(CurrentDefinition->getDefinitionLoc(),
15820b57cec5SDimitry Andric             diag::note_module_def_undef_here)
15830b57cec5SDimitry Andric       << false;
15840b57cec5SDimitry Andric   }
15850b57cec5SDimitry Andric }
15860b57cec5SDimitry Andric 
15870b57cec5SDimitry Andric /// Write a new timestamp file with the given path.
writeTimestampFile(StringRef TimestampFile)15880b57cec5SDimitry Andric static void writeTimestampFile(StringRef TimestampFile) {
15890b57cec5SDimitry Andric   std::error_code EC;
1590a7dea167SDimitry Andric   llvm::raw_fd_ostream Out(TimestampFile.str(), EC, llvm::sys::fs::OF_None);
15910b57cec5SDimitry Andric }
15920b57cec5SDimitry Andric 
15930b57cec5SDimitry Andric /// Prune the module cache of modules that haven't been accessed in
15940b57cec5SDimitry Andric /// a long time.
pruneModuleCache(const HeaderSearchOptions & HSOpts)15950b57cec5SDimitry Andric static void pruneModuleCache(const HeaderSearchOptions &HSOpts) {
1596a7dea167SDimitry Andric   llvm::sys::fs::file_status StatBuf;
15970b57cec5SDimitry Andric   llvm::SmallString<128> TimestampFile;
15980b57cec5SDimitry Andric   TimestampFile = HSOpts.ModuleCachePath;
15990b57cec5SDimitry Andric   assert(!TimestampFile.empty());
16000b57cec5SDimitry Andric   llvm::sys::path::append(TimestampFile, "modules.timestamp");
16010b57cec5SDimitry Andric 
16020b57cec5SDimitry Andric   // Try to stat() the timestamp file.
1603a7dea167SDimitry Andric   if (std::error_code EC = llvm::sys::fs::status(TimestampFile, StatBuf)) {
16040b57cec5SDimitry Andric     // If the timestamp file wasn't there, create one now.
1605a7dea167SDimitry Andric     if (EC == std::errc::no_such_file_or_directory) {
16060b57cec5SDimitry Andric       writeTimestampFile(TimestampFile);
16070b57cec5SDimitry Andric     }
16080b57cec5SDimitry Andric     return;
16090b57cec5SDimitry Andric   }
16100b57cec5SDimitry Andric 
16110b57cec5SDimitry Andric   // Check whether the time stamp is older than our pruning interval.
16120b57cec5SDimitry Andric   // If not, do nothing.
1613a7dea167SDimitry Andric   time_t TimeStampModTime =
1614a7dea167SDimitry Andric       llvm::sys::toTimeT(StatBuf.getLastModificationTime());
16150b57cec5SDimitry Andric   time_t CurrentTime = time(nullptr);
16160b57cec5SDimitry Andric   if (CurrentTime - TimeStampModTime <= time_t(HSOpts.ModuleCachePruneInterval))
16170b57cec5SDimitry Andric     return;
16180b57cec5SDimitry Andric 
16190b57cec5SDimitry Andric   // Write a new timestamp file so that nobody else attempts to prune.
16200b57cec5SDimitry Andric   // There is a benign race condition here, if two Clang instances happen to
16210b57cec5SDimitry Andric   // notice at the same time that the timestamp is out-of-date.
16220b57cec5SDimitry Andric   writeTimestampFile(TimestampFile);
16230b57cec5SDimitry Andric 
16240b57cec5SDimitry Andric   // Walk the entire module cache, looking for unused module files and module
16250b57cec5SDimitry Andric   // indices.
16260b57cec5SDimitry Andric   std::error_code EC;
16270b57cec5SDimitry Andric   SmallString<128> ModuleCachePathNative;
16280b57cec5SDimitry Andric   llvm::sys::path::native(HSOpts.ModuleCachePath, ModuleCachePathNative);
16290b57cec5SDimitry Andric   for (llvm::sys::fs::directory_iterator Dir(ModuleCachePathNative, EC), DirEnd;
16300b57cec5SDimitry Andric        Dir != DirEnd && !EC; Dir.increment(EC)) {
16310b57cec5SDimitry Andric     // If we don't have a directory, there's nothing to look into.
16320b57cec5SDimitry Andric     if (!llvm::sys::fs::is_directory(Dir->path()))
16330b57cec5SDimitry Andric       continue;
16340b57cec5SDimitry Andric 
16350b57cec5SDimitry Andric     // Walk all of the files within this directory.
16360b57cec5SDimitry Andric     for (llvm::sys::fs::directory_iterator File(Dir->path(), EC), FileEnd;
16370b57cec5SDimitry Andric          File != FileEnd && !EC; File.increment(EC)) {
16380b57cec5SDimitry Andric       // We only care about module and global module index files.
16390b57cec5SDimitry Andric       StringRef Extension = llvm::sys::path::extension(File->path());
16400b57cec5SDimitry Andric       if (Extension != ".pcm" && Extension != ".timestamp" &&
16410b57cec5SDimitry Andric           llvm::sys::path::filename(File->path()) != "modules.idx")
16420b57cec5SDimitry Andric         continue;
16430b57cec5SDimitry Andric 
16440b57cec5SDimitry Andric       // Look at this file. If we can't stat it, there's nothing interesting
16450b57cec5SDimitry Andric       // there.
1646a7dea167SDimitry Andric       if (llvm::sys::fs::status(File->path(), StatBuf))
16470b57cec5SDimitry Andric         continue;
16480b57cec5SDimitry Andric 
16490b57cec5SDimitry Andric       // If the file has been used recently enough, leave it there.
1650a7dea167SDimitry Andric       time_t FileAccessTime = llvm::sys::toTimeT(StatBuf.getLastAccessedTime());
16510b57cec5SDimitry Andric       if (CurrentTime - FileAccessTime <=
16520b57cec5SDimitry Andric               time_t(HSOpts.ModuleCachePruneAfter)) {
16530b57cec5SDimitry Andric         continue;
16540b57cec5SDimitry Andric       }
16550b57cec5SDimitry Andric 
16560b57cec5SDimitry Andric       // Remove the file.
16570b57cec5SDimitry Andric       llvm::sys::fs::remove(File->path());
16580b57cec5SDimitry Andric 
16590b57cec5SDimitry Andric       // Remove the timestamp file.
16600b57cec5SDimitry Andric       std::string TimpestampFilename = File->path() + ".timestamp";
16610b57cec5SDimitry Andric       llvm::sys::fs::remove(TimpestampFilename);
16620b57cec5SDimitry Andric     }
16630b57cec5SDimitry Andric 
16640b57cec5SDimitry Andric     // If we removed all of the files in the directory, remove the directory
16650b57cec5SDimitry Andric     // itself.
16660b57cec5SDimitry Andric     if (llvm::sys::fs::directory_iterator(Dir->path(), EC) ==
16670b57cec5SDimitry Andric             llvm::sys::fs::directory_iterator() && !EC)
16680b57cec5SDimitry Andric       llvm::sys::fs::remove(Dir->path());
16690b57cec5SDimitry Andric   }
16700b57cec5SDimitry Andric }
16710b57cec5SDimitry Andric 
createASTReader()1672480093f4SDimitry Andric void CompilerInstance::createASTReader() {
1673480093f4SDimitry Andric   if (TheASTReader)
1674480093f4SDimitry Andric     return;
1675480093f4SDimitry Andric 
16760b57cec5SDimitry Andric   if (!hasASTContext())
16770b57cec5SDimitry Andric     createASTContext();
16780b57cec5SDimitry Andric 
16790b57cec5SDimitry Andric   // If we're implicitly building modules but not currently recursively
16800b57cec5SDimitry Andric   // building a module, check whether we need to prune the module cache.
16810b57cec5SDimitry Andric   if (getSourceManager().getModuleBuildStack().empty() &&
16820b57cec5SDimitry Andric       !getPreprocessor().getHeaderSearchInfo().getModuleCachePath().empty() &&
16830b57cec5SDimitry Andric       getHeaderSearchOpts().ModuleCachePruneInterval > 0 &&
16840b57cec5SDimitry Andric       getHeaderSearchOpts().ModuleCachePruneAfter > 0) {
16850b57cec5SDimitry Andric     pruneModuleCache(getHeaderSearchOpts());
16860b57cec5SDimitry Andric   }
16870b57cec5SDimitry Andric 
16880b57cec5SDimitry Andric   HeaderSearchOptions &HSOpts = getHeaderSearchOpts();
16890b57cec5SDimitry Andric   std::string Sysroot = HSOpts.Sysroot;
16900b57cec5SDimitry Andric   const PreprocessorOptions &PPOpts = getPreprocessorOpts();
1691e8d8bef9SDimitry Andric   const FrontendOptions &FEOpts = getFrontendOpts();
16920b57cec5SDimitry Andric   std::unique_ptr<llvm::Timer> ReadTimer;
1693e8d8bef9SDimitry Andric 
16940b57cec5SDimitry Andric   if (FrontendTimerGroup)
1695a7dea167SDimitry Andric     ReadTimer = std::make_unique<llvm::Timer>("reading_modules",
16960b57cec5SDimitry Andric                                                 "Reading modules",
16970b57cec5SDimitry Andric                                                 *FrontendTimerGroup);
1698480093f4SDimitry Andric   TheASTReader = new ASTReader(
16990b57cec5SDimitry Andric       getPreprocessor(), getModuleCache(), &getASTContext(),
17000b57cec5SDimitry Andric       getPCHContainerReader(), getFrontendOpts().ModuleFileExtensions,
1701e8d8bef9SDimitry Andric       Sysroot.empty() ? "" : Sysroot.c_str(),
1702e8d8bef9SDimitry Andric       PPOpts.DisablePCHOrModuleValidation,
1703e8d8bef9SDimitry Andric       /*AllowASTWithCompilerErrors=*/FEOpts.AllowPCMWithCompilerErrors,
1704480093f4SDimitry Andric       /*AllowConfigurationMismatch=*/false, HSOpts.ModulesValidateSystemHeaders,
1705a7dea167SDimitry Andric       HSOpts.ValidateASTInputFilesContent,
17060b57cec5SDimitry Andric       getFrontendOpts().UseGlobalModuleIndex, std::move(ReadTimer));
17070b57cec5SDimitry Andric   if (hasASTConsumer()) {
1708480093f4SDimitry Andric     TheASTReader->setDeserializationListener(
17090b57cec5SDimitry Andric         getASTConsumer().GetASTDeserializationListener());
17100b57cec5SDimitry Andric     getASTContext().setASTMutationListener(
17110b57cec5SDimitry Andric       getASTConsumer().GetASTMutationListener());
17120b57cec5SDimitry Andric   }
1713480093f4SDimitry Andric   getASTContext().setExternalSource(TheASTReader);
17140b57cec5SDimitry Andric   if (hasSema())
1715480093f4SDimitry Andric     TheASTReader->InitializeSema(getSema());
17160b57cec5SDimitry Andric   if (hasASTConsumer())
1717480093f4SDimitry Andric     TheASTReader->StartTranslationUnit(&getASTConsumer());
17180b57cec5SDimitry Andric 
17190b57cec5SDimitry Andric   for (auto &Listener : DependencyCollectors)
1720480093f4SDimitry Andric     Listener->attachToASTReader(*TheASTReader);
17210b57cec5SDimitry Andric }
17220b57cec5SDimitry Andric 
loadModuleFile(StringRef FileName,serialization::ModuleFile * & LoadedModuleFile)17235f757f3fSDimitry Andric bool CompilerInstance::loadModuleFile(
17245f757f3fSDimitry Andric     StringRef FileName, serialization::ModuleFile *&LoadedModuleFile) {
17250b57cec5SDimitry Andric   llvm::Timer Timer;
17260b57cec5SDimitry Andric   if (FrontendTimerGroup)
17270b57cec5SDimitry Andric     Timer.init("preloading." + FileName.str(), "Preloading " + FileName.str(),
17280b57cec5SDimitry Andric                *FrontendTimerGroup);
17290b57cec5SDimitry Andric   llvm::TimeRegion TimeLoading(FrontendTimerGroup ? &Timer : nullptr);
17300b57cec5SDimitry Andric 
17310b57cec5SDimitry Andric   // If we don't already have an ASTReader, create one now.
1732480093f4SDimitry Andric   if (!TheASTReader)
1733480093f4SDimitry Andric     createASTReader();
17340b57cec5SDimitry Andric 
17350b57cec5SDimitry Andric   // If -Wmodule-file-config-mismatch is mapped as an error or worse, allow the
17360b57cec5SDimitry Andric   // ASTReader to diagnose it, since it can produce better errors that we can.
17370b57cec5SDimitry Andric   bool ConfigMismatchIsRecoverable =
17380b57cec5SDimitry Andric       getDiagnostics().getDiagnosticLevel(diag::warn_module_config_mismatch,
17390b57cec5SDimitry Andric                                           SourceLocation())
17400b57cec5SDimitry Andric         <= DiagnosticsEngine::Warning;
17410b57cec5SDimitry Andric 
1742349cc55cSDimitry Andric   auto Listener = std::make_unique<ReadModuleNames>(*PP);
17430b57cec5SDimitry Andric   auto &ListenerRef = *Listener;
1744480093f4SDimitry Andric   ASTReader::ListenerScope ReadModuleNamesListener(*TheASTReader,
17450b57cec5SDimitry Andric                                                    std::move(Listener));
17460b57cec5SDimitry Andric 
17470b57cec5SDimitry Andric   // Try to load the module file.
1748480093f4SDimitry Andric   switch (TheASTReader->ReadAST(
17490b57cec5SDimitry Andric       FileName, serialization::MK_ExplicitModule, SourceLocation(),
17505f757f3fSDimitry Andric       ConfigMismatchIsRecoverable ? ASTReader::ARR_ConfigurationMismatch : 0,
17515f757f3fSDimitry Andric       &LoadedModuleFile)) {
17520b57cec5SDimitry Andric   case ASTReader::Success:
17530b57cec5SDimitry Andric     // We successfully loaded the module file; remember the set of provided
17540b57cec5SDimitry Andric     // modules so that we don't try to load implicit modules for them.
17550b57cec5SDimitry Andric     ListenerRef.registerAll();
17560b57cec5SDimitry Andric     return true;
17570b57cec5SDimitry Andric 
17580b57cec5SDimitry Andric   case ASTReader::ConfigurationMismatch:
17590b57cec5SDimitry Andric     // Ignore unusable module files.
17600b57cec5SDimitry Andric     getDiagnostics().Report(SourceLocation(), diag::warn_module_config_mismatch)
17610b57cec5SDimitry Andric         << FileName;
17620b57cec5SDimitry Andric     // All modules provided by any files we tried and failed to load are now
17630b57cec5SDimitry Andric     // unavailable; includes of those modules should now be handled textually.
17640b57cec5SDimitry Andric     ListenerRef.markAllUnavailable();
17650b57cec5SDimitry Andric     return true;
17660b57cec5SDimitry Andric 
17670b57cec5SDimitry Andric   default:
17680b57cec5SDimitry Andric     return false;
17690b57cec5SDimitry Andric   }
17700b57cec5SDimitry Andric }
17710b57cec5SDimitry Andric 
1772480093f4SDimitry Andric namespace {
17730b57cec5SDimitry Andric enum ModuleSource {
1774480093f4SDimitry Andric   MS_ModuleNotFound,
1775480093f4SDimitry Andric   MS_ModuleCache,
1776480093f4SDimitry Andric   MS_PrebuiltModulePath,
1777480093f4SDimitry Andric   MS_ModuleBuildPragma
1778480093f4SDimitry Andric };
1779480093f4SDimitry Andric } // end namespace
1780480093f4SDimitry Andric 
1781480093f4SDimitry Andric /// Select a source for loading the named module and compute the filename to
1782480093f4SDimitry Andric /// load it from.
selectModuleSource(Module * M,StringRef ModuleName,std::string & ModuleFilename,const std::map<std::string,std::string,std::less<>> & BuiltModules,HeaderSearch & HS)17835ffd83dbSDimitry Andric static ModuleSource selectModuleSource(
17845ffd83dbSDimitry Andric     Module *M, StringRef ModuleName, std::string &ModuleFilename,
17855ffd83dbSDimitry Andric     const std::map<std::string, std::string, std::less<>> &BuiltModules,
1786480093f4SDimitry Andric     HeaderSearch &HS) {
1787480093f4SDimitry Andric   assert(ModuleFilename.empty() && "Already has a module source?");
17880b57cec5SDimitry Andric 
17890b57cec5SDimitry Andric   // Check to see if the module has been built as part of this compilation
17900b57cec5SDimitry Andric   // via a module build pragma.
17910b57cec5SDimitry Andric   auto BuiltModuleIt = BuiltModules.find(ModuleName);
17920b57cec5SDimitry Andric   if (BuiltModuleIt != BuiltModules.end()) {
1793480093f4SDimitry Andric     ModuleFilename = BuiltModuleIt->second;
1794480093f4SDimitry Andric     return MS_ModuleBuildPragma;
17950b57cec5SDimitry Andric   }
17960b57cec5SDimitry Andric 
17970b57cec5SDimitry Andric   // Try to load the module from the prebuilt module path.
1798480093f4SDimitry Andric   const HeaderSearchOptions &HSOpts = HS.getHeaderSearchOpts();
1799480093f4SDimitry Andric   if (!HSOpts.PrebuiltModuleFiles.empty() ||
1800480093f4SDimitry Andric       !HSOpts.PrebuiltModulePaths.empty()) {
1801480093f4SDimitry Andric     ModuleFilename = HS.getPrebuiltModuleFileName(ModuleName);
1802e8d8bef9SDimitry Andric     if (HSOpts.EnablePrebuiltImplicitModules && ModuleFilename.empty())
1803e8d8bef9SDimitry Andric       ModuleFilename = HS.getPrebuiltImplicitModuleFileName(M);
1804480093f4SDimitry Andric     if (!ModuleFilename.empty())
1805480093f4SDimitry Andric       return MS_PrebuiltModulePath;
18060b57cec5SDimitry Andric   }
18070b57cec5SDimitry Andric 
18080b57cec5SDimitry Andric   // Try to load the module from the module cache.
1809480093f4SDimitry Andric   if (M) {
1810480093f4SDimitry Andric     ModuleFilename = HS.getCachedModuleFileName(M);
1811480093f4SDimitry Andric     return MS_ModuleCache;
18120b57cec5SDimitry Andric   }
18130b57cec5SDimitry Andric 
1814480093f4SDimitry Andric   return MS_ModuleNotFound;
1815480093f4SDimitry Andric }
1816480093f4SDimitry Andric 
findOrCompileModuleAndReadAST(StringRef ModuleName,SourceLocation ImportLoc,SourceLocation ModuleNameLoc,bool IsInclusionDirective)1817480093f4SDimitry Andric ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST(
1818480093f4SDimitry Andric     StringRef ModuleName, SourceLocation ImportLoc,
1819480093f4SDimitry Andric     SourceLocation ModuleNameLoc, bool IsInclusionDirective) {
1820480093f4SDimitry Andric   // Search for a module with the given name.
1821480093f4SDimitry Andric   HeaderSearch &HS = PP->getHeaderSearchInfo();
1822349cc55cSDimitry Andric   Module *M =
1823349cc55cSDimitry Andric       HS.lookupModule(ModuleName, ImportLoc, true, !IsInclusionDirective);
1824480093f4SDimitry Andric 
1825480093f4SDimitry Andric   // Select the source and filename for loading the named module.
1826480093f4SDimitry Andric   std::string ModuleFilename;
1827480093f4SDimitry Andric   ModuleSource Source =
1828480093f4SDimitry Andric       selectModuleSource(M, ModuleName, ModuleFilename, BuiltModules, HS);
1829480093f4SDimitry Andric   if (Source == MS_ModuleNotFound) {
18300b57cec5SDimitry Andric     // We can't find a module, error out here.
18310b57cec5SDimitry Andric     getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_found)
18320b57cec5SDimitry Andric         << ModuleName << SourceRange(ImportLoc, ModuleNameLoc);
1833fe6060f1SDimitry Andric     return nullptr;
18340b57cec5SDimitry Andric   }
1835480093f4SDimitry Andric   if (ModuleFilename.empty()) {
1836480093f4SDimitry Andric     if (M && M->HasIncompatibleModuleFile) {
18370b57cec5SDimitry Andric       // We tried and failed to load a module file for this module. Fall
18380b57cec5SDimitry Andric       // back to textual inclusion for its headers.
18390b57cec5SDimitry Andric       return ModuleLoadResult::ConfigMismatch;
18400b57cec5SDimitry Andric     }
18410b57cec5SDimitry Andric 
18420b57cec5SDimitry Andric     getDiagnostics().Report(ModuleNameLoc, diag::err_module_build_disabled)
18430b57cec5SDimitry Andric         << ModuleName;
1844fe6060f1SDimitry Andric     return nullptr;
18450b57cec5SDimitry Andric   }
18460b57cec5SDimitry Andric 
1847480093f4SDimitry Andric   // Create an ASTReader on demand.
1848480093f4SDimitry Andric   if (!getASTReader())
1849480093f4SDimitry Andric     createASTReader();
18500b57cec5SDimitry Andric 
1851480093f4SDimitry Andric   // Time how long it takes to load the module.
18520b57cec5SDimitry Andric   llvm::Timer Timer;
18530b57cec5SDimitry Andric   if (FrontendTimerGroup)
1854480093f4SDimitry Andric     Timer.init("loading." + ModuleFilename, "Loading " + ModuleFilename,
18550b57cec5SDimitry Andric                *FrontendTimerGroup);
18560b57cec5SDimitry Andric   llvm::TimeRegion TimeLoading(FrontendTimerGroup ? &Timer : nullptr);
18570b57cec5SDimitry Andric   llvm::TimeTraceScope TimeScope("Module Load", ModuleName);
18580b57cec5SDimitry Andric 
18590b57cec5SDimitry Andric   // Try to load the module file. If we are not trying to load from the
18600b57cec5SDimitry Andric   // module cache, we don't know how to rebuild modules.
1861480093f4SDimitry Andric   unsigned ARRFlags = Source == MS_ModuleCache
1862fe6060f1SDimitry Andric                           ? ASTReader::ARR_OutOfDate | ASTReader::ARR_Missing |
1863fe6060f1SDimitry Andric                                 ASTReader::ARR_TreatModuleWithErrorsAsOutOfDate
1864480093f4SDimitry Andric                           : Source == MS_PrebuiltModulePath
1865480093f4SDimitry Andric                                 ? 0
1866480093f4SDimitry Andric                                 : ASTReader::ARR_ConfigurationMismatch;
1867480093f4SDimitry Andric   switch (getASTReader()->ReadAST(ModuleFilename,
1868480093f4SDimitry Andric                                   Source == MS_PrebuiltModulePath
18690b57cec5SDimitry Andric                                       ? serialization::MK_PrebuiltModule
1870480093f4SDimitry Andric                                       : Source == MS_ModuleBuildPragma
18710b57cec5SDimitry Andric                                             ? serialization::MK_ExplicitModule
18720b57cec5SDimitry Andric                                             : serialization::MK_ImplicitModule,
18730b57cec5SDimitry Andric                                   ImportLoc, ARRFlags)) {
18740b57cec5SDimitry Andric   case ASTReader::Success: {
1875480093f4SDimitry Andric     if (M)
1876480093f4SDimitry Andric       return M;
1877480093f4SDimitry Andric     assert(Source != MS_ModuleCache &&
1878480093f4SDimitry Andric            "missing module, but file loaded from cache");
1879480093f4SDimitry Andric 
1880480093f4SDimitry Andric     // A prebuilt module is indexed as a ModuleFile; the Module does not exist
1881480093f4SDimitry Andric     // until the first call to ReadAST.  Look it up now.
1882349cc55cSDimitry Andric     M = HS.lookupModule(ModuleName, ImportLoc, true, !IsInclusionDirective);
1883480093f4SDimitry Andric 
1884480093f4SDimitry Andric     // Check whether M refers to the file in the prebuilt module path.
1885480093f4SDimitry Andric     if (M && M->getASTFile())
1886480093f4SDimitry Andric       if (auto ModuleFile = FileMgr->getFile(ModuleFilename))
1887480093f4SDimitry Andric         if (*ModuleFile == M->getASTFile())
1888480093f4SDimitry Andric           return M;
1889480093f4SDimitry Andric 
18900b57cec5SDimitry Andric     getDiagnostics().Report(ModuleNameLoc, diag::err_module_prebuilt)
18910b57cec5SDimitry Andric         << ModuleName;
18920b57cec5SDimitry Andric     return ModuleLoadResult();
18930b57cec5SDimitry Andric   }
18940b57cec5SDimitry Andric 
18950b57cec5SDimitry Andric   case ASTReader::OutOfDate:
1896480093f4SDimitry Andric   case ASTReader::Missing:
1897480093f4SDimitry Andric     // The most interesting case.
1898480093f4SDimitry Andric     break;
1899480093f4SDimitry Andric 
1900480093f4SDimitry Andric   case ASTReader::ConfigurationMismatch:
1901480093f4SDimitry Andric     if (Source == MS_PrebuiltModulePath)
1902480093f4SDimitry Andric       // FIXME: We shouldn't be setting HadFatalFailure below if we only
1903480093f4SDimitry Andric       // produce a warning here!
1904480093f4SDimitry Andric       getDiagnostics().Report(SourceLocation(),
1905480093f4SDimitry Andric                               diag::warn_module_config_mismatch)
1906480093f4SDimitry Andric           << ModuleFilename;
1907480093f4SDimitry Andric     // Fall through to error out.
1908bdd1243dSDimitry Andric     [[fallthrough]];
1909480093f4SDimitry Andric   case ASTReader::VersionMismatch:
1910480093f4SDimitry Andric   case ASTReader::HadErrors:
1911480093f4SDimitry Andric     ModuleLoader::HadFatalFailure = true;
1912480093f4SDimitry Andric     // FIXME: The ASTReader will already have complained, but can we shoehorn
1913480093f4SDimitry Andric     // that diagnostic information into a more useful form?
1914480093f4SDimitry Andric     return ModuleLoadResult();
1915480093f4SDimitry Andric 
1916480093f4SDimitry Andric   case ASTReader::Failure:
1917480093f4SDimitry Andric     ModuleLoader::HadFatalFailure = true;
1918480093f4SDimitry Andric     return ModuleLoadResult();
1919480093f4SDimitry Andric   }
1920480093f4SDimitry Andric 
1921480093f4SDimitry Andric   // ReadAST returned Missing or OutOfDate.
1922480093f4SDimitry Andric   if (Source != MS_ModuleCache) {
19230b57cec5SDimitry Andric     // We don't know the desired configuration for this module and don't
19240b57cec5SDimitry Andric     // necessarily even have a module map. Since ReadAST already produces
19250b57cec5SDimitry Andric     // diagnostics for these two cases, we simply error out here.
19260b57cec5SDimitry Andric     return ModuleLoadResult();
19270b57cec5SDimitry Andric   }
19280b57cec5SDimitry Andric 
19290b57cec5SDimitry Andric   // The module file is missing or out-of-date. Build it.
1930480093f4SDimitry Andric   assert(M && "missing module, but trying to compile for cache");
1931480093f4SDimitry Andric 
19320b57cec5SDimitry Andric   // Check whether there is a cycle in the module graph.
19330b57cec5SDimitry Andric   ModuleBuildStack ModPath = getSourceManager().getModuleBuildStack();
19340b57cec5SDimitry Andric   ModuleBuildStack::iterator Pos = ModPath.begin(), PosEnd = ModPath.end();
19350b57cec5SDimitry Andric   for (; Pos != PosEnd; ++Pos) {
19360b57cec5SDimitry Andric     if (Pos->first == ModuleName)
19370b57cec5SDimitry Andric       break;
19380b57cec5SDimitry Andric   }
19390b57cec5SDimitry Andric 
19400b57cec5SDimitry Andric   if (Pos != PosEnd) {
19410b57cec5SDimitry Andric     SmallString<256> CyclePath;
19420b57cec5SDimitry Andric     for (; Pos != PosEnd; ++Pos) {
19430b57cec5SDimitry Andric       CyclePath += Pos->first;
19440b57cec5SDimitry Andric       CyclePath += " -> ";
19450b57cec5SDimitry Andric     }
19460b57cec5SDimitry Andric     CyclePath += ModuleName;
19470b57cec5SDimitry Andric 
19480b57cec5SDimitry Andric     getDiagnostics().Report(ModuleNameLoc, diag::err_module_cycle)
19490b57cec5SDimitry Andric         << ModuleName << CyclePath;
1950fe6060f1SDimitry Andric     return nullptr;
19510b57cec5SDimitry Andric   }
19520b57cec5SDimitry Andric 
19530b57cec5SDimitry Andric   // Check whether we have already attempted to build this module (but
19540b57cec5SDimitry Andric   // failed).
19550b57cec5SDimitry Andric   if (getPreprocessorOpts().FailedModules &&
19560b57cec5SDimitry Andric       getPreprocessorOpts().FailedModules->hasAlreadyFailed(ModuleName)) {
19570b57cec5SDimitry Andric     getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_built)
1958480093f4SDimitry Andric         << ModuleName << SourceRange(ImportLoc, ModuleNameLoc);
1959fe6060f1SDimitry Andric     return nullptr;
19600b57cec5SDimitry Andric   }
19610b57cec5SDimitry Andric 
1962480093f4SDimitry Andric   // Try to compile and then read the AST.
1963480093f4SDimitry Andric   if (!compileModuleAndReadAST(*this, ImportLoc, ModuleNameLoc, M,
1964480093f4SDimitry Andric                                ModuleFilename)) {
19650b57cec5SDimitry Andric     assert(getDiagnostics().hasErrorOccurred() &&
1966480093f4SDimitry Andric            "undiagnosed error in compileModuleAndReadAST");
19670b57cec5SDimitry Andric     if (getPreprocessorOpts().FailedModules)
19680b57cec5SDimitry Andric       getPreprocessorOpts().FailedModules->addFailed(ModuleName);
1969fe6060f1SDimitry Andric     return nullptr;
19700b57cec5SDimitry Andric   }
19710b57cec5SDimitry Andric 
19720b57cec5SDimitry Andric   // Okay, we've rebuilt and now loaded the module.
1973480093f4SDimitry Andric   return M;
19740b57cec5SDimitry Andric }
19750b57cec5SDimitry Andric 
1976480093f4SDimitry Andric ModuleLoadResult
loadModule(SourceLocation ImportLoc,ModuleIdPath Path,Module::NameVisibilityKind Visibility,bool IsInclusionDirective)1977480093f4SDimitry Andric CompilerInstance::loadModule(SourceLocation ImportLoc,
1978480093f4SDimitry Andric                              ModuleIdPath Path,
1979480093f4SDimitry Andric                              Module::NameVisibilityKind Visibility,
1980480093f4SDimitry Andric                              bool IsInclusionDirective) {
1981480093f4SDimitry Andric   // Determine what file we're searching from.
1982480093f4SDimitry Andric   StringRef ModuleName = Path[0].first->getName();
1983480093f4SDimitry Andric   SourceLocation ModuleNameLoc = Path[0].second;
19840b57cec5SDimitry Andric 
1985480093f4SDimitry Andric   // If we've already handled this import, just return the cached result.
1986480093f4SDimitry Andric   // This one-element cache is important to eliminate redundant diagnostics
1987480093f4SDimitry Andric   // when both the preprocessor and parser see the same import declaration.
1988480093f4SDimitry Andric   if (ImportLoc.isValid() && LastModuleImportLoc == ImportLoc) {
1989480093f4SDimitry Andric     // Make the named module visible.
1990480093f4SDimitry Andric     if (LastModuleImportResult && ModuleName != getLangOpts().CurrentModule)
1991480093f4SDimitry Andric       TheASTReader->makeModuleVisible(LastModuleImportResult, Visibility,
1992480093f4SDimitry Andric                                       ImportLoc);
1993480093f4SDimitry Andric     return LastModuleImportResult;
19940b57cec5SDimitry Andric   }
19950b57cec5SDimitry Andric 
1996480093f4SDimitry Andric   // If we don't already have information on this module, load the module now.
1997480093f4SDimitry Andric   Module *Module = nullptr;
1998480093f4SDimitry Andric   ModuleMap &MM = getPreprocessor().getHeaderSearchInfo().getModuleMap();
1999480093f4SDimitry Andric   if (auto MaybeModule = MM.getCachedModuleLoad(*Path[0].first)) {
2000480093f4SDimitry Andric     // Use the cached result, which may be nullptr.
2001480093f4SDimitry Andric     Module = *MaybeModule;
2002480093f4SDimitry Andric   } else if (ModuleName == getLangOpts().CurrentModule) {
2003480093f4SDimitry Andric     // This is the module we're building.
2004480093f4SDimitry Andric     Module = PP->getHeaderSearchInfo().lookupModule(
2005349cc55cSDimitry Andric         ModuleName, ImportLoc, /*AllowSearch*/ true,
2006480093f4SDimitry Andric         /*AllowExtraModuleMapSearch*/ !IsInclusionDirective);
20071ac55f4cSDimitry Andric 
2008480093f4SDimitry Andric     MM.cacheModuleLoad(*Path[0].first, Module);
2009480093f4SDimitry Andric   } else {
2010480093f4SDimitry Andric     ModuleLoadResult Result = findOrCompileModuleAndReadAST(
2011480093f4SDimitry Andric         ModuleName, ImportLoc, ModuleNameLoc, IsInclusionDirective);
2012480093f4SDimitry Andric     if (!Result.isNormal())
2013480093f4SDimitry Andric       return Result;
2014fe6060f1SDimitry Andric     if (!Result)
2015fe6060f1SDimitry Andric       DisableGeneratingGlobalModuleIndex = true;
2016480093f4SDimitry Andric     Module = Result;
2017480093f4SDimitry Andric     MM.cacheModuleLoad(*Path[0].first, Module);
20180b57cec5SDimitry Andric   }
20190b57cec5SDimitry Andric 
2020480093f4SDimitry Andric   // If we never found the module, fail.  Otherwise, verify the module and link
2021480093f4SDimitry Andric   // it up.
20220b57cec5SDimitry Andric   if (!Module)
20230b57cec5SDimitry Andric     return ModuleLoadResult();
20240b57cec5SDimitry Andric 
20250b57cec5SDimitry Andric   // Verify that the rest of the module path actually corresponds to
20260b57cec5SDimitry Andric   // a submodule.
20270b57cec5SDimitry Andric   bool MapPrivateSubModToTopLevel = false;
20280b57cec5SDimitry Andric   for (unsigned I = 1, N = Path.size(); I != N; ++I) {
20290b57cec5SDimitry Andric     StringRef Name = Path[I].first->getName();
20300b57cec5SDimitry Andric     clang::Module *Sub = Module->findSubmodule(Name);
20310b57cec5SDimitry Andric 
20320b57cec5SDimitry Andric     // If the user is requesting Foo.Private and it doesn't exist, try to
20330b57cec5SDimitry Andric     // match Foo_Private and emit a warning asking for the user to write
20340b57cec5SDimitry Andric     // @import Foo_Private instead. FIXME: remove this when existing clients
20350b57cec5SDimitry Andric     // migrate off of Foo.Private syntax.
2036bdd1243dSDimitry Andric     if (!Sub && Name == "Private" && Module == Module->getTopLevelModule()) {
20370b57cec5SDimitry Andric       SmallString<128> PrivateModule(Module->Name);
20380b57cec5SDimitry Andric       PrivateModule.append("_Private");
20390b57cec5SDimitry Andric 
20400b57cec5SDimitry Andric       SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> PrivPath;
20410b57cec5SDimitry Andric       auto &II = PP->getIdentifierTable().get(
20420b57cec5SDimitry Andric           PrivateModule, PP->getIdentifierInfo(Module->Name)->getTokenID());
20430b57cec5SDimitry Andric       PrivPath.push_back(std::make_pair(&II, Path[0].second));
20440b57cec5SDimitry Andric 
204506c3fb27SDimitry Andric       std::string FileName;
204606c3fb27SDimitry Andric       // If there is a modulemap module or prebuilt module, load it.
2047349cc55cSDimitry Andric       if (PP->getHeaderSearchInfo().lookupModule(PrivateModule, ImportLoc, true,
204806c3fb27SDimitry Andric                                                  !IsInclusionDirective) ||
204906c3fb27SDimitry Andric           selectModuleSource(nullptr, PrivateModule, FileName, BuiltModules,
205006c3fb27SDimitry Andric                              PP->getHeaderSearchInfo()) != MS_ModuleNotFound)
2051349cc55cSDimitry Andric         Sub = loadModule(ImportLoc, PrivPath, Visibility, IsInclusionDirective);
20520b57cec5SDimitry Andric       if (Sub) {
20530b57cec5SDimitry Andric         MapPrivateSubModToTopLevel = true;
2054bdd1243dSDimitry Andric         PP->markClangModuleAsAffecting(Module);
20550b57cec5SDimitry Andric         if (!getDiagnostics().isIgnored(
20560b57cec5SDimitry Andric                 diag::warn_no_priv_submodule_use_toplevel, ImportLoc)) {
20570b57cec5SDimitry Andric           getDiagnostics().Report(Path[I].second,
20580b57cec5SDimitry Andric                                   diag::warn_no_priv_submodule_use_toplevel)
20590b57cec5SDimitry Andric               << Path[I].first << Module->getFullModuleName() << PrivateModule
20600b57cec5SDimitry Andric               << SourceRange(Path[0].second, Path[I].second)
20610b57cec5SDimitry Andric               << FixItHint::CreateReplacement(SourceRange(Path[0].second),
20620b57cec5SDimitry Andric                                               PrivateModule);
20630b57cec5SDimitry Andric           getDiagnostics().Report(Sub->DefinitionLoc,
20640b57cec5SDimitry Andric                                   diag::note_private_top_level_defined);
20650b57cec5SDimitry Andric         }
20660b57cec5SDimitry Andric       }
20670b57cec5SDimitry Andric     }
20680b57cec5SDimitry Andric 
20690b57cec5SDimitry Andric     if (!Sub) {
20700b57cec5SDimitry Andric       // Attempt to perform typo correction to find a module name that works.
20710b57cec5SDimitry Andric       SmallVector<StringRef, 2> Best;
20720b57cec5SDimitry Andric       unsigned BestEditDistance = (std::numeric_limits<unsigned>::max)();
20730b57cec5SDimitry Andric 
2074349cc55cSDimitry Andric       for (class Module *SubModule : Module->submodules()) {
2075349cc55cSDimitry Andric         unsigned ED =
2076349cc55cSDimitry Andric             Name.edit_distance(SubModule->Name,
2077349cc55cSDimitry Andric                                /*AllowReplacements=*/true, BestEditDistance);
20780b57cec5SDimitry Andric         if (ED <= BestEditDistance) {
20790b57cec5SDimitry Andric           if (ED < BestEditDistance) {
20800b57cec5SDimitry Andric             Best.clear();
20810b57cec5SDimitry Andric             BestEditDistance = ED;
20820b57cec5SDimitry Andric           }
20830b57cec5SDimitry Andric 
2084349cc55cSDimitry Andric           Best.push_back(SubModule->Name);
20850b57cec5SDimitry Andric         }
20860b57cec5SDimitry Andric       }
20870b57cec5SDimitry Andric 
20880b57cec5SDimitry Andric       // If there was a clear winner, user it.
20890b57cec5SDimitry Andric       if (Best.size() == 1) {
2090349cc55cSDimitry Andric         getDiagnostics().Report(Path[I].second, diag::err_no_submodule_suggest)
20910b57cec5SDimitry Andric             << Path[I].first << Module->getFullModuleName() << Best[0]
20920b57cec5SDimitry Andric             << SourceRange(Path[0].second, Path[I - 1].second)
20930b57cec5SDimitry Andric             << FixItHint::CreateReplacement(SourceRange(Path[I].second),
20940b57cec5SDimitry Andric                                             Best[0]);
20950b57cec5SDimitry Andric 
20960b57cec5SDimitry Andric         Sub = Module->findSubmodule(Best[0]);
20970b57cec5SDimitry Andric       }
20980b57cec5SDimitry Andric     }
20990b57cec5SDimitry Andric 
21000b57cec5SDimitry Andric     if (!Sub) {
21010b57cec5SDimitry Andric       // No submodule by this name. Complain, and don't look for further
21020b57cec5SDimitry Andric       // submodules.
21030b57cec5SDimitry Andric       getDiagnostics().Report(Path[I].second, diag::err_no_submodule)
21040b57cec5SDimitry Andric           << Path[I].first << Module->getFullModuleName()
21050b57cec5SDimitry Andric           << SourceRange(Path[0].second, Path[I - 1].second);
21060b57cec5SDimitry Andric       break;
21070b57cec5SDimitry Andric     }
21080b57cec5SDimitry Andric 
21090b57cec5SDimitry Andric     Module = Sub;
21100b57cec5SDimitry Andric   }
21110b57cec5SDimitry Andric 
21120b57cec5SDimitry Andric   // Make the named module visible, if it's not already part of the module
21130b57cec5SDimitry Andric   // we are parsing.
21140b57cec5SDimitry Andric   if (ModuleName != getLangOpts().CurrentModule) {
21150b57cec5SDimitry Andric     if (!Module->IsFromModuleFile && !MapPrivateSubModToTopLevel) {
21160b57cec5SDimitry Andric       // We have an umbrella header or directory that doesn't actually include
21170b57cec5SDimitry Andric       // all of the headers within the directory it covers. Complain about
21180b57cec5SDimitry Andric       // this missing submodule and recover by forgetting that we ever saw
21190b57cec5SDimitry Andric       // this submodule.
21200b57cec5SDimitry Andric       // FIXME: Should we detect this at module load time? It seems fairly
21210b57cec5SDimitry Andric       // expensive (and rare).
21220b57cec5SDimitry Andric       getDiagnostics().Report(ImportLoc, diag::warn_missing_submodule)
21230b57cec5SDimitry Andric         << Module->getFullModuleName()
21240b57cec5SDimitry Andric         << SourceRange(Path.front().second, Path.back().second);
21250b57cec5SDimitry Andric 
2126bdd1243dSDimitry Andric       return ModuleLoadResult(Module, ModuleLoadResult::MissingExpected);
21270b57cec5SDimitry Andric     }
21280b57cec5SDimitry Andric 
21290b57cec5SDimitry Andric     // Check whether this module is available.
21300b57cec5SDimitry Andric     if (Preprocessor::checkModuleIsAvailable(getLangOpts(), getTarget(),
21315f757f3fSDimitry Andric                                              *Module, getDiagnostics())) {
21320b57cec5SDimitry Andric       getDiagnostics().Report(ImportLoc, diag::note_module_import_here)
21330b57cec5SDimitry Andric         << SourceRange(Path.front().second, Path.back().second);
21340b57cec5SDimitry Andric       LastModuleImportLoc = ImportLoc;
21350b57cec5SDimitry Andric       LastModuleImportResult = ModuleLoadResult();
21360b57cec5SDimitry Andric       return ModuleLoadResult();
21370b57cec5SDimitry Andric     }
21380b57cec5SDimitry Andric 
2139480093f4SDimitry Andric     TheASTReader->makeModuleVisible(Module, Visibility, ImportLoc);
21400b57cec5SDimitry Andric   }
21410b57cec5SDimitry Andric 
21420b57cec5SDimitry Andric   // Check for any configuration macros that have changed.
21430b57cec5SDimitry Andric   clang::Module *TopModule = Module->getTopLevelModule();
21440b57cec5SDimitry Andric   for (unsigned I = 0, N = TopModule->ConfigMacros.size(); I != N; ++I) {
21450b57cec5SDimitry Andric     checkConfigMacro(getPreprocessor(), TopModule->ConfigMacros[I],
21460b57cec5SDimitry Andric                      Module, ImportLoc);
21470b57cec5SDimitry Andric   }
21480b57cec5SDimitry Andric 
21490b57cec5SDimitry Andric   // Resolve any remaining module using export_as for this one.
21500b57cec5SDimitry Andric   getPreprocessor()
21510b57cec5SDimitry Andric       .getHeaderSearchInfo()
21520b57cec5SDimitry Andric       .getModuleMap()
21530b57cec5SDimitry Andric       .resolveLinkAsDependencies(TopModule);
21540b57cec5SDimitry Andric 
21550b57cec5SDimitry Andric   LastModuleImportLoc = ImportLoc;
21560b57cec5SDimitry Andric   LastModuleImportResult = ModuleLoadResult(Module);
21570b57cec5SDimitry Andric   return LastModuleImportResult;
21580b57cec5SDimitry Andric }
21590b57cec5SDimitry Andric 
createModuleFromSource(SourceLocation ImportLoc,StringRef ModuleName,StringRef Source)2160480093f4SDimitry Andric void CompilerInstance::createModuleFromSource(SourceLocation ImportLoc,
21610b57cec5SDimitry Andric                                               StringRef ModuleName,
21620b57cec5SDimitry Andric                                               StringRef Source) {
21630b57cec5SDimitry Andric   // Avoid creating filenames with special characters.
21640b57cec5SDimitry Andric   SmallString<128> CleanModuleName(ModuleName);
21650b57cec5SDimitry Andric   for (auto &C : CleanModuleName)
21660b57cec5SDimitry Andric     if (!isAlphanumeric(C))
21670b57cec5SDimitry Andric       C = '_';
21680b57cec5SDimitry Andric 
21690b57cec5SDimitry Andric   // FIXME: Using a randomized filename here means that our intermediate .pcm
21700b57cec5SDimitry Andric   // output is nondeterministic (as .pcm files refer to each other by name).
21710b57cec5SDimitry Andric   // Can this affect the output in any way?
21720b57cec5SDimitry Andric   SmallString<128> ModuleFileName;
21730b57cec5SDimitry Andric   if (std::error_code EC = llvm::sys::fs::createTemporaryFile(
21740b57cec5SDimitry Andric           CleanModuleName, "pcm", ModuleFileName)) {
21750b57cec5SDimitry Andric     getDiagnostics().Report(ImportLoc, diag::err_fe_unable_to_open_output)
21760b57cec5SDimitry Andric         << ModuleFileName << EC.message();
21770b57cec5SDimitry Andric     return;
21780b57cec5SDimitry Andric   }
21790b57cec5SDimitry Andric   std::string ModuleMapFileName = (CleanModuleName + ".map").str();
21800b57cec5SDimitry Andric 
21810b57cec5SDimitry Andric   FrontendInputFile Input(
21820b57cec5SDimitry Andric       ModuleMapFileName,
21835f757f3fSDimitry Andric       InputKind(getLanguageFromOptions(Invocation->getLangOpts()),
21840b57cec5SDimitry Andric                 InputKind::ModuleMap, /*Preprocessed*/true));
21850b57cec5SDimitry Andric 
21860b57cec5SDimitry Andric   std::string NullTerminatedSource(Source.str());
21870b57cec5SDimitry Andric 
21880b57cec5SDimitry Andric   auto PreBuildStep = [&](CompilerInstance &Other) {
21890b57cec5SDimitry Andric     // Create a virtual file containing our desired source.
21900b57cec5SDimitry Andric     // FIXME: We shouldn't need to do this.
21915f757f3fSDimitry Andric     FileEntryRef ModuleMapFile = Other.getFileManager().getVirtualFileRef(
21920b57cec5SDimitry Andric         ModuleMapFileName, NullTerminatedSource.size(), 0);
21930b57cec5SDimitry Andric     Other.getSourceManager().overrideFileContents(
2194349cc55cSDimitry Andric         ModuleMapFile, llvm::MemoryBuffer::getMemBuffer(NullTerminatedSource));
21950b57cec5SDimitry Andric 
21960b57cec5SDimitry Andric     Other.BuiltModules = std::move(BuiltModules);
21970b57cec5SDimitry Andric     Other.DeleteBuiltModules = false;
21980b57cec5SDimitry Andric   };
21990b57cec5SDimitry Andric 
22000b57cec5SDimitry Andric   auto PostBuildStep = [this](CompilerInstance &Other) {
22010b57cec5SDimitry Andric     BuiltModules = std::move(Other.BuiltModules);
22020b57cec5SDimitry Andric   };
22030b57cec5SDimitry Andric 
22040b57cec5SDimitry Andric   // Build the module, inheriting any modules that we've built locally.
22050b57cec5SDimitry Andric   if (compileModuleImpl(*this, ImportLoc, ModuleName, Input, StringRef(),
22060b57cec5SDimitry Andric                         ModuleFileName, PreBuildStep, PostBuildStep)) {
22077a6dacacSDimitry Andric     BuiltModules[std::string(ModuleName)] = std::string(ModuleFileName);
22080b57cec5SDimitry Andric     llvm::sys::RemoveFileOnSignal(ModuleFileName);
22090b57cec5SDimitry Andric   }
22100b57cec5SDimitry Andric }
22110b57cec5SDimitry Andric 
makeModuleVisible(Module * Mod,Module::NameVisibilityKind Visibility,SourceLocation ImportLoc)22120b57cec5SDimitry Andric void CompilerInstance::makeModuleVisible(Module *Mod,
22130b57cec5SDimitry Andric                                          Module::NameVisibilityKind Visibility,
22140b57cec5SDimitry Andric                                          SourceLocation ImportLoc) {
2215480093f4SDimitry Andric   if (!TheASTReader)
2216480093f4SDimitry Andric     createASTReader();
2217480093f4SDimitry Andric   if (!TheASTReader)
22180b57cec5SDimitry Andric     return;
22190b57cec5SDimitry Andric 
2220480093f4SDimitry Andric   TheASTReader->makeModuleVisible(Mod, Visibility, ImportLoc);
22210b57cec5SDimitry Andric }
22220b57cec5SDimitry Andric 
loadGlobalModuleIndex(SourceLocation TriggerLoc)22230b57cec5SDimitry Andric GlobalModuleIndex *CompilerInstance::loadGlobalModuleIndex(
22240b57cec5SDimitry Andric     SourceLocation TriggerLoc) {
22250b57cec5SDimitry Andric   if (getPreprocessor().getHeaderSearchInfo().getModuleCachePath().empty())
22260b57cec5SDimitry Andric     return nullptr;
2227480093f4SDimitry Andric   if (!TheASTReader)
2228480093f4SDimitry Andric     createASTReader();
22290b57cec5SDimitry Andric   // Can't do anything if we don't have the module manager.
2230480093f4SDimitry Andric   if (!TheASTReader)
22310b57cec5SDimitry Andric     return nullptr;
22320b57cec5SDimitry Andric   // Get an existing global index.  This loads it if not already
22330b57cec5SDimitry Andric   // loaded.
2234480093f4SDimitry Andric   TheASTReader->loadGlobalIndex();
2235480093f4SDimitry Andric   GlobalModuleIndex *GlobalIndex = TheASTReader->getGlobalIndex();
22360b57cec5SDimitry Andric   // If the global index doesn't exist, create it.
22370b57cec5SDimitry Andric   if (!GlobalIndex && shouldBuildGlobalModuleIndex() && hasFileManager() &&
22380b57cec5SDimitry Andric       hasPreprocessor()) {
22390b57cec5SDimitry Andric     llvm::sys::fs::create_directories(
22400b57cec5SDimitry Andric       getPreprocessor().getHeaderSearchInfo().getModuleCachePath());
22410b57cec5SDimitry Andric     if (llvm::Error Err = GlobalModuleIndex::writeIndex(
22420b57cec5SDimitry Andric             getFileManager(), getPCHContainerReader(),
22430b57cec5SDimitry Andric             getPreprocessor().getHeaderSearchInfo().getModuleCachePath())) {
22440b57cec5SDimitry Andric       // FIXME this drops the error on the floor. This code is only used for
22450b57cec5SDimitry Andric       // typo correction and drops more than just this one source of errors
22460b57cec5SDimitry Andric       // (such as the directory creation failure above). It should handle the
22470b57cec5SDimitry Andric       // error.
22480b57cec5SDimitry Andric       consumeError(std::move(Err));
22490b57cec5SDimitry Andric       return nullptr;
22500b57cec5SDimitry Andric     }
2251480093f4SDimitry Andric     TheASTReader->resetForReload();
2252480093f4SDimitry Andric     TheASTReader->loadGlobalIndex();
2253480093f4SDimitry Andric     GlobalIndex = TheASTReader->getGlobalIndex();
22540b57cec5SDimitry Andric   }
22550b57cec5SDimitry Andric   // For finding modules needing to be imported for fixit messages,
22560b57cec5SDimitry Andric   // we need to make the global index cover all modules, so we do that here.
22570b57cec5SDimitry Andric   if (!HaveFullGlobalModuleIndex && GlobalIndex && !buildingModule()) {
22580b57cec5SDimitry Andric     ModuleMap &MMap = getPreprocessor().getHeaderSearchInfo().getModuleMap();
22590b57cec5SDimitry Andric     bool RecreateIndex = false;
22600b57cec5SDimitry Andric     for (ModuleMap::module_iterator I = MMap.module_begin(),
22610b57cec5SDimitry Andric         E = MMap.module_end(); I != E; ++I) {
22620b57cec5SDimitry Andric       Module *TheModule = I->second;
22635f757f3fSDimitry Andric       OptionalFileEntryRef Entry = TheModule->getASTFile();
22640b57cec5SDimitry Andric       if (!Entry) {
22650b57cec5SDimitry Andric         SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
22660b57cec5SDimitry Andric         Path.push_back(std::make_pair(
22670b57cec5SDimitry Andric             getPreprocessor().getIdentifierInfo(TheModule->Name), TriggerLoc));
22680b57cec5SDimitry Andric         std::reverse(Path.begin(), Path.end());
22690b57cec5SDimitry Andric         // Load a module as hidden.  This also adds it to the global index.
22700b57cec5SDimitry Andric         loadModule(TheModule->DefinitionLoc, Path, Module::Hidden, false);
22710b57cec5SDimitry Andric         RecreateIndex = true;
22720b57cec5SDimitry Andric       }
22730b57cec5SDimitry Andric     }
22740b57cec5SDimitry Andric     if (RecreateIndex) {
22750b57cec5SDimitry Andric       if (llvm::Error Err = GlobalModuleIndex::writeIndex(
22760b57cec5SDimitry Andric               getFileManager(), getPCHContainerReader(),
22770b57cec5SDimitry Andric               getPreprocessor().getHeaderSearchInfo().getModuleCachePath())) {
22780b57cec5SDimitry Andric         // FIXME As above, this drops the error on the floor.
22790b57cec5SDimitry Andric         consumeError(std::move(Err));
22800b57cec5SDimitry Andric         return nullptr;
22810b57cec5SDimitry Andric       }
2282480093f4SDimitry Andric       TheASTReader->resetForReload();
2283480093f4SDimitry Andric       TheASTReader->loadGlobalIndex();
2284480093f4SDimitry Andric       GlobalIndex = TheASTReader->getGlobalIndex();
22850b57cec5SDimitry Andric     }
22860b57cec5SDimitry Andric     HaveFullGlobalModuleIndex = true;
22870b57cec5SDimitry Andric   }
22880b57cec5SDimitry Andric   return GlobalIndex;
22890b57cec5SDimitry Andric }
22900b57cec5SDimitry Andric 
22910b57cec5SDimitry Andric // Check global module index for missing imports.
22920b57cec5SDimitry Andric bool
lookupMissingImports(StringRef Name,SourceLocation TriggerLoc)22930b57cec5SDimitry Andric CompilerInstance::lookupMissingImports(StringRef Name,
22940b57cec5SDimitry Andric                                        SourceLocation TriggerLoc) {
22950b57cec5SDimitry Andric   // Look for the symbol in non-imported modules, but only if an error
22960b57cec5SDimitry Andric   // actually occurred.
22970b57cec5SDimitry Andric   if (!buildingModule()) {
22980b57cec5SDimitry Andric     // Load global module index, or retrieve a previously loaded one.
22990b57cec5SDimitry Andric     GlobalModuleIndex *GlobalIndex = loadGlobalModuleIndex(
23000b57cec5SDimitry Andric       TriggerLoc);
23010b57cec5SDimitry Andric 
23020b57cec5SDimitry Andric     // Only if we have a global index.
23030b57cec5SDimitry Andric     if (GlobalIndex) {
23040b57cec5SDimitry Andric       GlobalModuleIndex::HitSet FoundModules;
23050b57cec5SDimitry Andric 
23060b57cec5SDimitry Andric       // Find the modules that reference the identifier.
23070b57cec5SDimitry Andric       // Note that this only finds top-level modules.
23080b57cec5SDimitry Andric       // We'll let diagnoseTypo find the actual declaration module.
23090b57cec5SDimitry Andric       if (GlobalIndex->lookupIdentifier(Name, FoundModules))
23100b57cec5SDimitry Andric         return true;
23110b57cec5SDimitry Andric     }
23120b57cec5SDimitry Andric   }
23130b57cec5SDimitry Andric 
23140b57cec5SDimitry Andric   return false;
23150b57cec5SDimitry Andric }
resetAndLeakSema()23160b57cec5SDimitry Andric void CompilerInstance::resetAndLeakSema() { llvm::BuryPointer(takeSema()); }
23170b57cec5SDimitry Andric 
setExternalSemaSource(IntrusiveRefCntPtr<ExternalSemaSource> ESS)23180b57cec5SDimitry Andric void CompilerInstance::setExternalSemaSource(
23190b57cec5SDimitry Andric     IntrusiveRefCntPtr<ExternalSemaSource> ESS) {
23200b57cec5SDimitry Andric   ExternalSemaSrc = std::move(ESS);
23210b57cec5SDimitry Andric }
2322