Lines Matching +full:module +full:- +full:instance
1 //===--- CompilerInstance.cpp ---------------------------------------------===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
45 #include "llvm/Config/llvm-config.h"
85 (TheASTReader && TheASTReader->isGlobalIndexUnavailable() &&
108 // Create the target instance.
121 TO->Triple = llvm::Triple::normalize(getFrontendOpts().AuxTriple);
123 TO->CPU = *getFrontendOpts().AuxTargetCPU;
125 TO->FeaturesAsWritten = *getFrontendOpts().AuxTargetFeatures;
126 TO->HostTriple = getTarget().getTriple().str();
206 assert(ModuleCache.get() == &Reader->getModuleManager().getModuleCache() &&
226 MDC->addFile(Name);
239 MDC->addFile(PCHInclude);
245 llvm::sys::path::native(PCHDir->getName(), DirNative);
254 Dir->path(), FileMgr, CI.getModuleCache(),
258 MDC->addFile(Dir->path());
279 MDC->addFile(E.VPath, E.RPath);
289 if (DiagOpts->DiagnosticLogFile != "-") {
292 DiagOpts->DiagnosticLogFile, EC,
296 << DiagOpts->DiagnosticLogFile << EC.message();
298 FileOS->SetUnbuffered();
308 Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags);
349 // implementing -verify.
351 Diags->setClient(Client, ShouldOwnClient);
352 } else if (Opts->getFormat() == DiagnosticOptions::SARIF) {
353 Diags->setClient(new SARIFDiagnosticPrinter(llvm::errs(), Opts));
355 Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts));
357 // Chain in -verify checker, if requested.
358 if (Opts->VerifyDiagnostics)
359 Diags->setClient(new VerifyDiagnosticConsumer(*Diags));
361 // Chain in -diagnostic-log-file dumper, if requested.
362 if (!Opts->DiagnosticLogFile.empty())
365 if (!Opts->DiagnosticSerializationFile.empty())
367 Opts->DiagnosticSerializationFile);
380 VFS = FileMgr ? &FileMgr->getVirtualFileSystem()
404 FileMgr.getVirtualFileRef(RB.first, RB.second->getBufferSize(), 0);
411 SourceMgr.overrideFileContents(FromFile, RB.second->getMemBufferRef());
428 FileMgr.getVirtualFile(RF.first, ToFile->getSize(), 0);
455 PP = std::make_shared<Preprocessor>(Invocation->getPreprocessorOptsPtr(),
461 PP->Initialize(getTarget(), getAuxTarget());
464 PP->createPreprocessingRecord();
467 InitializeFileRemapping(PP->getDiagnostics(), PP->getSourceManager(),
468 PP->getFileManager(), PPOpts);
477 const llvm::Triple *HeaderSearchTriple = &PP->getTargetInfo().getTriple();
478 if (PP->getTargetInfo().getTriple().getOS() == llvm::Triple::CUDA &&
479 PP->getAuxTargetInfo())
480 HeaderSearchTriple = &PP->getAuxTargetInfo()->getTriple();
482 ApplyHeaderSearchOptions(PP->getHeaderSearchInfo(), getHeaderSearchOpts(),
483 PP->getLangOpts(), *HeaderSearchTriple);
485 PP->setPreprocessedOutput(getPreprocessorOutputOpts().ShowCPP);
487 if (PP->getLangOpts().Modules && PP->getLangOpts().ImplicitModules) {
489 PP->getHeaderSearchInfo().setModuleHash(ModuleHash);
490 PP->getHeaderSearchInfo().setModuleCachePath(
502 // If we don't have a collector, but we are collecting module dependencies,
503 // then we're the top level compiler instance and need to create one.
509 // If there is a module dep collector, register with other dep collectors
513 collectHeaderMaps(PP->getHeaderSearchInfo(), ModuleDepCollector);
519 Listener->attachToPreprocessor(*PP);
526 if (OutputPath == "-")
541 // Set up the module path, including the hash for the module-creation options.
555 Context->InitBuiltinTypes(getTarget(), getAuxTarget());
562 // Helper to recursively read the module names for all modules we're adding.
563 // We mark these as known and redirect any attempt to load that module to
572 // Keep the module name as a string for now. It's not safe to create a new
587 if (Module *M = PP.getHeaderSearchInfo().getModuleMap().findModule(
589 M->HasIncompatibleModuleFile = true;
591 // Mark module as available if the only reason it was unavailable
593 SmallVector<Module *, 2> Stack;
596 Module *Current = Stack.pop_back_val();
597 if (Current->IsUnimportable) continue;
598 Current->IsAvailable = true;
599 auto SubmodulesRange = Current->submodules();
644 // eagerly-deserialized declarations may use it.
647 Reader->setDeserializationListener(
652 Listener->attachToASTReader(*Reader);
659 switch (Reader->ReadAST(Path,
667 PP.setPredefines(Reader->getSuggestedPredefines());
725 new llvm::TimerGroup("frontend", "Clang front-end time report"));
727 new llvm::Timer("frontend", "Clang front-end timer",
741 // Set up the creation routine for code-completion.
751 TheSema->APINotes.setSwiftVersion(getAPINotesOpts().SwiftVersion);
755 TheSema->addExternalSource(ExternalSemaSrc.get());
756 ExternalSemaSrc->InitializeSema(*TheSema);
759 // If we're building a module and are supposed to load API notes,
762 (void)TheSema->APINotes.loadCurrentModuleAPINotes(
777 consumeError(OF.File->discard());
786 if (OF.File->TmpName.empty()) {
787 consumeError(OF.File->discard());
791 llvm::Error E = OF.File->keep(OF.Filename);
796 << OF.File->TmpName << OF.Filename << std::move(E);
798 llvm::sys::fs::remove(OF.File->TmpName);
802 for (auto &Module : BuiltModules)
803 llvm::sys::fs::remove(Module.second);
814 if (InFile == "-" || Extension.empty()) {
815 OutputPath = "-";
854 // If '-working-directory' was passed, the output filename should be
857 if (OutputPath != "-" && !llvm::sys::path::is_absolute(OutputPath)) {
862 FileMgr->FixupRelativePath(*AbsPath);
870 if (OutputPath == "-")
882 // things like '-o /dev/null'
892 // Insert -%%%%%%%% before the extension (if any), and because some tools
898 TempPath += "-%%%%%%%%";
909 ExpectedFile.takeError(), [&](const llvm::ECError &E) -> llvm::Error {
931 OS.reset(new llvm::raw_fd_ostream(Temp->FD, /*shouldClose=*/false));
932 OSFile = Temp->TmpName;
949 // Add the output file -- but don't try to remove "-", since this means we are
951 OutputFiles.emplace_back(((OutputPath != "-") ? OutputPath : "").str(),
954 if (!Binary || OS->supportsSeeking())
988 auto FileOrErr = InputFile == "-"
993 if (InputFile != "-")
1008 // High-Level Operations
1012 assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
1013 assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
1033 // rewriter project will change target built-in bool type from its default.
1039 OS << "clang -cc1 version " CLANG_VERSION_STRING << " based upon LLVM "
1093 return !getDiagnostics().getClient()->getNumErrors();
1104 unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings();
1105 unsigned NumErrors = getDiagnostics().getClient()->getNumErrors();
1139 if (P->getActionType() == PluginASTAction::ReplaceAction) {
1159 /// Compile a module file for the given module, using the options
1160 /// provided by the importing compiler instance. Returns true if the module
1170 llvm::TimeTraceScope TimeScope("Module Compile", ModuleName);
1172 // Never compile a module that's already finalized - this would cause the
1173 // existing module to be freed, causing crashes if it is later referenced
1181 // Construct a compiler invocation for creating this module.
1185 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
1187 // For any options that aren't intended to affect how a module is built,
1189 Invocation->resetNonModularOptions();
1191 // Remove any macro definitions that are explicitly ignored by the module.
1192 // They aren't supposed to affect how the module is built anyway.
1193 HeaderSearchOptions &HSOpts = Invocation->getHeaderSearchOpts();
1201 // If the original compiler invocation had -fmodule-name, pass it through.
1202 Invocation->getLangOpts().ModuleName =
1205 // Note the name of the module we're building.
1206 Invocation->getLangOpts().CurrentModule = std::string(ModuleName);
1208 // If there is a module map file, build the module using the module map.
1209 // Set up the inputs/outputs so that we build the module from its umbrella
1211 FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
1217 // Force implicitly-built modules to hash the content of the module file.
1224 DiagnosticOptions &DiagOpts = Invocation->getDiagnosticOpts();
1228 Invocation->getModuleHash() && "Module hash mismatch!");
1230 // Construct a compiler instance that will be used to actually create the
1231 // module. Since we're sharing an in-memory module cache,
1233 // buffers to prevent use-after-frees.
1234 CompilerInstance Instance(ImportingInstance.getPCHContainerOperations(),
1237 Instance.setInvocation(std::move(Invocation));
1239 Instance.createDiagnostics(new ForwardingDiagnosticConsumer(
1244 Instance.getDiagnostics().setSuppressSystemWarnings(false);
1247 Instance.setFileManager(&ImportingInstance.getFileManager());
1249 Instance.createFileManager(&ImportingInstance.getVirtualFileSystem());
1251 Instance.createSourceManager(Instance.getFileManager());
1252 SourceManager &SourceMgr = Instance.getSourceManager();
1254 // Note that this module is part of the module build stack, so that we
1255 // can detect cycles in the module graph.
1261 // Make sure that the failed-module structure has been allocated in
1262 // the importing instance, and propagate the pointer to the newly-created
1263 // instance.
1266 Instance.setFailedModulesSet(ImportingInstance.getFailedModulesSetPtr());
1268 // If we're collecting module dependencies, we need to share a collector
1269 // between all of the module CompilerInstances. Other than that, we don't
1270 // want to produce any dependency output from the module build.
1271 Instance.setModuleDepCollector(ImportingInstance.getModuleDepCollector());
1278 PreBuildStep(Instance);
1280 // Execute the action to actually build the module in-place. Use a separate
1285 Instance.ExecuteAction(Action);
1289 PostBuildStep(Instance);
1297 ImportingInstance.getFileManager().AddStats(Instance.getFileManager());
1302 Instance.setSema(nullptr);
1303 Instance.setASTConsumer(nullptr);
1305 // Delete any remaining temporary files related to Instance.
1306 Instance.clearOutputFiles(/*EraseFiles=*/true);
1311 return !Instance.getDiagnostics().hasErrorOccurred() ||
1312 Instance.getFrontendOpts().AllowPCMWithCompilerErrors;
1320 llvm::sys::path::append(PublicFilename, "module.map");
1321 else if (Filename == "module.private.modulemap")
1322 llvm::sys::path::append(PublicFilename, "module.modulemap");
1328 /// Compile a module file for the given module in a separate compiler instance,
1329 /// using the options provided by the importing compiler instance. Returns true
1330 /// if the module was built without errors.
1332 SourceLocation ImportLoc, Module *Module,
1337 // Get or create the module map that we'll use to build this module.
1342 if (FileID ModuleMapFID = ModMap.getContainingModuleMapFileID(Module);
1344 // We want to use the top-level module map. If we don't, the compiling
1345 // instance may think the containing module map is a top-level one, while
1346 // the importing instance knows it's included from a parent module map via
1356 assert(ModuleMapFile && "Top-level module map with no FileID");
1358 // Canonicalize compilation to start with the public module map. This is
1359 // vital for submodules declarations in the private module maps to be
1360 // correctly parsed when depending on a top level module in the public one.
1365 StringRef ModuleMapFilePath = ModuleMapFile->getNameAsRequested();
1367 // Use the module map where this module resides.
1369 ImportingInstance, ImportLoc, Module->getTopLevelModuleName(),
1370 FrontendInputFile(ModuleMapFilePath, IK, +Module->IsSystem),
1371 ModMap.getModuleMapFileForUniquing(Module)->getName(), ModuleFileName);
1374 // transporting the module's directory to the module map parser. We should
1377 SmallString<128> FakeModuleMapFile(Module->Directory->getName());
1382 Module->print(OS);
1386 ImportingInstance, ImportLoc, Module->getTopLevelModuleName(),
1387 FrontendInputFile(FakeModuleMapFile, IK, +Module->IsSystem),
1388 ModMap.getModuleMapFileForUniquing(Module)->getName(),
1390 [&](CompilerInstance &Instance) {
1393 FileEntryRef ModuleMapFile = Instance.getFileManager().getVirtualFileRef(
1395 Instance.getSourceManager().overrideFileContents(
1400 // We've rebuilt a module. If we're allowed to generate or update the global
1401 // module index, record that fact in the importing compiler instance.
1409 /// Read the AST right after compiling the module.
1413 Module *Module, StringRef ModuleFileName,
1421 // Try to read the module file, now that we've compiled it.
1423 ImportingInstance.getASTReader()->ReadAST(
1429 // The caller wants to handle out-of-date failures.
1438 << Module->Name << SourceRange(ImportLoc, ModuleNameLoc);
1443 /// Compile a module in a separate compiler instance and read the AST,
1444 /// returning true if the module compiles without errors.
1448 Module *Module,
1450 if (!compileModule(ImportingInstance, ModuleNameLoc, Module,
1454 << Module->Name << SourceRange(ImportLoc, ModuleNameLoc);
1459 Module, ModuleFileName,
1463 /// Compile a module in a separate compiler instance and read the AST,
1464 /// returning true if the module compiles without errors, using a lock manager
1465 /// to avoid building the same module in multiple compiler instances.
1468 /// multiple instances will compete to create the same module. On timeout,
1473 SourceLocation ModuleNameLoc, Module *Module, StringRef ModuleFileName) {
1477 << ModuleFileName << Module->Name;
1489 // performance. Fallback to building the module in case of any lock
1492 << Module->Name << Locked.getErrorMessage();
1497 // We're responsible for building the module ourselves.
1499 ModuleNameLoc, Module, ModuleFileName);
1505 // Someone else is responsible for building the module. Wait for them to
1517 << Module->Name;
1523 // Read the module that was just written by someone else.
1526 Module, ModuleFileName, &OutOfDate))
1531 // The module may be out of date in the presence of file system races,
1537 /// Compile a module in a separate compiler instance and read the AST,
1538 /// returning true if the module compiles without errors, potentially using a
1539 /// lock manager to avoid building the same module in multiple compiler
1544 Module *Module, StringRef ModuleFileName) {
1549 ModuleNameLoc, Module,
1552 ModuleNameLoc, Module,
1559 Module *Mod, SourceLocation ImportLoc) {
1565 if (!Id->hadMacroDefinition())
1571 for (auto *MD = LatestLocalMD; MD; MD = MD->getPrevious()) {
1573 FileID FID = SourceMgr.getFileID(MD->getLocation());
1577 CmdLineDefinition = DMD->getMacroInfo();
1588 << true << ConfigMacro << Mod->getFullModuleName();
1589 auto LatestDef = LatestLocalMD->getDefinition();
1599 << false << ConfigMacro << Mod->getFullModuleName();
1600 PP.Diag(CurrentDefinition->getDefinitionLoc(),
1603 } else if (!CurrentDefinition->isIdenticalTo(*CmdLineDefinition, PP,
1607 << false << ConfigMacro << Mod->getFullModuleName();
1608 PP.Diag(CurrentDefinition->getDefinitionLoc(),
1614 static void checkConfigMacros(Preprocessor &PP, Module *M,
1616 clang::Module *TopModule = M->getTopLevelModule();
1617 for (const StringRef ConMacro : TopModule->ConfigMacros) {
1628 /// Prune the module cache of modules that haven't been accessed in
1651 if (CurrentTime - TimeStampModTime <= time_t(HSOpts.ModuleCachePruneInterval))
1656 // notice at the same time that the timestamp is out-of-date.
1659 // Walk the entire module cache, looking for unused module files and module
1667 if (!llvm::sys::fs::is_directory(Dir->path()))
1671 for (llvm::sys::fs::directory_iterator File(Dir->path(), EC), FileEnd;
1673 // We only care about module and global module index files.
1674 StringRef Extension = llvm::sys::path::extension(File->path());
1676 llvm::sys::path::filename(File->path()) != "modules.idx")
1681 if (llvm::sys::fs::status(File->path(), StatBuf))
1686 if (CurrentTime - FileAccessTime <=
1692 llvm::sys::fs::remove(File->path());
1695 std::string TimpestampFilename = File->path() + ".timestamp";
1701 if (llvm::sys::fs::directory_iterator(Dir->path(), EC) ==
1703 llvm::sys::fs::remove(Dir->path());
1715 // building a module, check whether we need to prune the module cache.
1743 TheASTReader->setDeserializationListener(
1750 TheASTReader->InitializeSema(getSema());
1752 TheASTReader->StartTranslationUnit(&getASTConsumer());
1755 Listener->attachToASTReader(*TheASTReader);
1770 // If -Wmodule-file-config-mismatch is mapped as an error or worse, allow the
1782 // Try to load the module file.
1783 switch (TheASTReader->ReadAST(
1788 // We successfully loaded the module file; remember the set of provided
1794 // Ignore unusable module files.
1816 /// Select a source for loading the named module and compute the filename to
1819 Module *M, StringRef ModuleName, std::string &ModuleFilename,
1822 assert(ModuleFilename.empty() && "Already has a module source?");
1824 // Check to see if the module has been built as part of this compilation
1825 // via a module build pragma.
1828 ModuleFilename = BuiltModuleIt->second;
1832 // Try to load the module from the prebuilt module path.
1843 // Try to load the module from the module cache.
1855 // Search for a module with the given name.
1856 HeaderSearch &HS = PP->getHeaderSearchInfo();
1857 Module *M =
1861 // immediately before potentially building a module in case this module
1867 // Select the source and filename for loading the named module.
1872 // We can't find a module, error out here.
1878 if (M && M->HasIncompatibleModuleFile) {
1879 // We tried and failed to load a module file for this module. Fall
1893 // Time how long it takes to load the module.
1899 llvm::TimeTraceScope TimeScope("Module Load", ModuleName);
1901 // Try to load the module file. If we are not trying to load from the
1902 // module cache, we don't know how to rebuild modules.
1909 switch (getASTReader()->ReadAST(ModuleFilename,
1920 "missing module, but file loaded from cache");
1922 // A prebuilt module is indexed as a ModuleFile; the Module does not exist
1926 // Check whether M refers to the file in the prebuilt module path.
1927 if (M && M->getASTFile())
1928 if (auto ModuleFile = FileMgr->getFile(ModuleFilename))
1929 if (*ModuleFile == M->getASTFile())
1965 // We don't know the desired configuration for this module and don't
1966 // necessarily even have a module map. Since ReadAST already produces
1971 // The module file is missing or out-of-date. Build it.
1972 assert(M && "missing module, but trying to compile for cache");
1974 // Check whether there is a cycle in the module graph.
1978 if (Pos->first == ModuleName)
1985 CyclePath += Pos->first;
1986 CyclePath += " -> ";
1995 // Check whether we have already attempted to build this module (but failed).
1996 if (FailedModules && FailedModules->hasAlreadyFailed(ModuleName)) {
2008 FailedModules->addFailed(ModuleName);
2012 // Okay, we've rebuilt and now loaded the module.
2019 Module::NameVisibilityKind Visibility,
2022 StringRef ModuleName = Path[0].first->getName();
2026 // This one-element cache is important to eliminate redundant diagnostics
2029 // Make the named module visible.
2031 TheASTReader->makeModuleVisible(LastModuleImportResult, Visibility,
2036 // If we don't already have information on this module, load the module now.
2037 Module *Module = nullptr;
2041 Module = *MaybeModule;
2042 // Config macros are already checked before building a module, but they need
2045 if (Module)
2046 checkConfigMacros(getPreprocessor(), Module, ImportLoc);
2048 // This is the module we're building.
2049 Module = PP->getHeaderSearchInfo().lookupModule(
2059 MM.cacheModuleLoad(*Path[0].first, Module);
2067 Module = Result;
2068 MM.cacheModuleLoad(*Path[0].first, Module);
2071 // If we never found the module, fail. Otherwise, verify the module and link
2073 if (!Module)
2076 // Verify that the rest of the module path actually corresponds to
2080 StringRef Name = Path[I].first->getName();
2081 clang::Module *Sub = Module->findSubmodule(Name);
2087 if (!Sub && Name == "Private" && Module == Module->getTopLevelModule()) {
2088 SmallString<128> PrivateModule(Module->Name);
2092 auto &II = PP->getIdentifierTable().get(
2093 PrivateModule, PP->getIdentifierInfo(Module->Name)->getTokenID());
2097 // If there is a modulemap module or prebuilt module, load it.
2098 if (PP->getHeaderSearchInfo().lookupModule(PrivateModule, ImportLoc, true,
2101 PP->getHeaderSearchInfo()) != MS_ModuleNotFound)
2105 PP->markClangModuleAsAffecting(Module);
2110 << Path[I].first << Module->getFullModuleName() << PrivateModule
2114 getDiagnostics().Report(Sub->DefinitionLoc,
2121 // Attempt to perform typo correction to find a module name that works.
2125 for (class Module *SubModule : Module->submodules()) {
2127 Name.edit_distance(SubModule->Name,
2135 Best.push_back(SubModule->Name);
2142 << Path[I].first << Module->getFullModuleName() << Best[0]
2143 << SourceRange(Path[0].second, Path[I - 1].second)
2147 Sub = Module->findSubmodule(Best[0]);
2155 << Path[I].first << Module->getFullModuleName()
2156 << SourceRange(Path[0].second, Path[I - 1].second);
2160 Module = Sub;
2163 // Make the named module visible, if it's not already part of the module
2166 if (!Module->IsFromModuleFile && !MapPrivateSubModToTopLevel) {
2171 // FIXME: Should we detect this at module load time? It seems fairly
2174 << Module->getFullModuleName()
2177 return ModuleLoadResult(Module, ModuleLoadResult::MissingExpected);
2180 // Check whether this module is available.
2182 *Module, getDiagnostics())) {
2190 TheASTReader->makeModuleVisible(Module, Visibility, ImportLoc);
2193 // Resolve any remaining module using export_as for this one.
2197 .resolveLinkAsDependencies(Module->getTopLevelModule());
2200 LastModuleImportResult = ModuleLoadResult(Module);
2227 InputKind(getLanguageFromOptions(Invocation->getLangOpts()),
2248 // Build the module, inheriting any modules that we've built locally.
2256 void CompilerInstance::makeModuleVisible(Module *Mod,
2257 Module::NameVisibilityKind Visibility,
2264 TheASTReader->makeModuleVisible(Mod, Visibility, ImportLoc);
2273 // Can't do anything if we don't have the module manager.
2278 TheASTReader->loadGlobalIndex();
2279 GlobalModuleIndex *GlobalIndex = TheASTReader->getGlobalIndex();
2295 TheASTReader->resetForReload();
2296 TheASTReader->loadGlobalIndex();
2297 GlobalIndex = TheASTReader->getGlobalIndex();
2306 Module *TheModule = I->second;
2307 OptionalFileEntryRef Entry = TheModule->getASTFile();
2311 getPreprocessor().getIdentifierInfo(TheModule->Name), TriggerLoc));
2313 // Load a module as hidden. This also adds it to the global index.
2314 loadModule(TheModule->DefinitionLoc, Path, Module::Hidden, false);
2326 TheASTReader->resetForReload();
2327 TheASTReader->loadGlobalIndex();
2328 GlobalIndex = TheASTReader->getGlobalIndex();
2335 // Check global module index for missing imports.
2339 // Look for the symbol in non-imported modules, but only if an error
2342 // Load global module index, or retrieve a previously loaded one.
2351 // Note that this only finds top-level modules.
2352 // We'll let diagnoseTypo find the actual declaration module.
2353 if (GlobalIndex->lookupIdentifier(Name, FoundModules))