diff options
Diffstat (limited to 'contrib/llvm/tools/clang/lib/Frontend/CompilerInstance.cpp')
-rw-r--r-- | contrib/llvm/tools/clang/lib/Frontend/CompilerInstance.cpp | 254 |
1 files changed, 126 insertions, 128 deletions
diff --git a/contrib/llvm/tools/clang/lib/Frontend/CompilerInstance.cpp b/contrib/llvm/tools/clang/lib/Frontend/CompilerInstance.cpp index c33b150..3edcf5d 100644 --- a/contrib/llvm/tools/clang/lib/Frontend/CompilerInstance.cpp +++ b/contrib/llvm/tools/clang/lib/Frontend/CompilerInstance.cpp @@ -78,9 +78,8 @@ void CompilerInstance::setDiagnostics(DiagnosticsEngine *Value) { Diagnostics = Value; } -void CompilerInstance::setTarget(TargetInfo *Value) { - Target = Value; -} +void CompilerInstance::setTarget(TargetInfo *Value) { Target = Value; } +void CompilerInstance::setAuxTarget(TargetInfo *Value) { AuxTarget = Value; } void CompilerInstance::setFileManager(FileManager *Value) { FileMgr = Value; @@ -96,7 +95,12 @@ void CompilerInstance::setSourceManager(SourceManager *Value) { void CompilerInstance::setPreprocessor(Preprocessor *Value) { PP = Value; } -void CompilerInstance::setASTContext(ASTContext *Value) { Context = Value; } +void CompilerInstance::setASTContext(ASTContext *Value) { + Context = Value; + + if (Context && Consumer) + getASTConsumer().Initialize(getASTContext()); +} void CompilerInstance::setSema(Sema *S) { TheSema.reset(S); @@ -104,6 +108,9 @@ void CompilerInstance::setSema(Sema *S) { void CompilerInstance::setASTConsumer(std::unique_ptr<ASTConsumer> Value) { Consumer = std::move(Value); + + if (Context && Consumer) + getASTConsumer().Initialize(getASTContext()); } void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) { @@ -148,7 +155,6 @@ static void SetUpDiagnosticLog(DiagnosticOptions *DiagOpts, << DiagOpts->DiagnosticLogFile << EC.message(); } else { FileOS->SetUnbuffered(); - FileOS->SetUseAtomicWrites(true); OS = FileOS.get(); StreamOwner = std::move(FileOS); } @@ -304,7 +310,7 @@ void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) { PP = new Preprocessor(&getPreprocessorOpts(), getDiagnostics(), getLangOpts(), getSourceManager(), *HeaderInfo, *this, PTHMgr, /*OwnsHeaderSearch=*/true, TUKind); - PP->Initialize(getTarget()); + PP->Initialize(getTarget(), getAuxTarget()); // Note that this is different then passing PTHMgr to Preprocessor's ctor. // That argument is used as the IdentifierInfoLookup argument to @@ -331,7 +337,7 @@ void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) { PP->setPreprocessedOutput(getPreprocessorOutputOpts().ShowCPP); - if (PP->getLangOpts().Modules) + if (PP->getLangOpts().Modules && PP->getLangOpts().ImplicitModules) PP->getHeaderSearchInfo().setModuleCachePath(getSpecificModuleCachePath()); // Handle generating dependencies, if requested. @@ -354,17 +360,19 @@ void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) { // Handle generating header include information, if requested. if (DepOpts.ShowHeaderIncludes) - AttachHeaderIncludeGen(*PP); + AttachHeaderIncludeGen(*PP, DepOpts.ExtraDeps); if (!DepOpts.HeaderIncludeOutputFile.empty()) { StringRef OutputPath = DepOpts.HeaderIncludeOutputFile; if (OutputPath == "-") OutputPath = ""; - AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/true, OutputPath, + AttachHeaderIncludeGen(*PP, DepOpts.ExtraDeps, + /*ShowAllHeaders=*/true, OutputPath, /*ShowDepth=*/false); } if (DepOpts.PrintShowIncludes) { - AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/false, /*OutputPath=*/"", + AttachHeaderIncludeGen(*PP, DepOpts.ExtraDeps, + /*ShowAllHeaders=*/false, /*OutputPath=*/"", /*ShowDepth=*/true, /*MSStyle=*/true); } } @@ -372,9 +380,8 @@ void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) { std::string CompilerInstance::getSpecificModuleCachePath() { // Set up the module path, including the hash for the // module-creation options. - SmallString<256> SpecificModuleCache( - getHeaderSearchOpts().ModuleCachePath); - if (!getHeaderSearchOpts().DisableModuleHash) + SmallString<256> SpecificModuleCache(getHeaderSearchOpts().ModuleCachePath); + if (!SpecificModuleCache.empty() && !getHeaderSearchOpts().DisableModuleHash) llvm::sys::path::append(SpecificModuleCache, getInvocation().getModuleHash()); return SpecificModuleCache.str(); @@ -384,10 +391,11 @@ std::string CompilerInstance::getSpecificModuleCachePath() { void CompilerInstance::createASTContext() { Preprocessor &PP = getPreprocessor(); - Context = new ASTContext(getLangOpts(), PP.getSourceManager(), - PP.getIdentifierTable(), PP.getSelectorTable(), - PP.getBuiltinInfo()); - Context->InitBuiltinTypes(getTarget()); + auto *Context = new ASTContext(getLangOpts(), PP.getSourceManager(), + PP.getIdentifierTable(), PP.getSelectorTable(), + PP.getBuiltinInfo()); + Context->InitBuiltinTypes(getTarget(), getAuxTarget()); + setASTContext(Context); } // ExternalASTSource @@ -399,7 +407,9 @@ void CompilerInstance::createPCHExternalASTSource( ModuleManager = createPCHExternalASTSource( Path, getHeaderSearchOpts().Sysroot, DisablePCHValidation, AllowPCHWithCompilerErrors, getPreprocessor(), getASTContext(), - getPCHContainerReader(), DeserializationListener, + getPCHContainerReader(), + getFrontendOpts().ModuleFileExtensions, + DeserializationListener, OwnDeserializationListener, Preamble, getFrontendOpts().UseGlobalModuleIndex); } @@ -408,15 +418,16 @@ IntrusiveRefCntPtr<ASTReader> CompilerInstance::createPCHExternalASTSource( StringRef Path, StringRef Sysroot, bool DisablePCHValidation, bool AllowPCHWithCompilerErrors, Preprocessor &PP, ASTContext &Context, const PCHContainerReader &PCHContainerRdr, + ArrayRef<IntrusiveRefCntPtr<ModuleFileExtension>> Extensions, void *DeserializationListener, bool OwnDeserializationListener, bool Preamble, bool UseGlobalModuleIndex) { HeaderSearchOptions &HSOpts = PP.getHeaderSearchInfo().getHeaderSearchOpts(); IntrusiveRefCntPtr<ASTReader> Reader(new ASTReader( - PP, Context, PCHContainerRdr, Sysroot.empty() ? "" : Sysroot.data(), - DisablePCHValidation, AllowPCHWithCompilerErrors, - /*AllowConfigurationMismatch*/ false, HSOpts.ModulesValidateSystemHeaders, - UseGlobalModuleIndex)); + PP, Context, PCHContainerRdr, Extensions, + Sysroot.empty() ? "" : Sysroot.data(), DisablePCHValidation, + AllowPCHWithCompilerErrors, /*AllowConfigurationMismatch*/ false, + HSOpts.ModulesValidateSystemHeaders, UseGlobalModuleIndex)); // We need the external source to be set up before we read the AST, because // eagerly-deserialized declarations may use it. @@ -631,8 +642,10 @@ std::unique_ptr<llvm::raw_pwrite_stream> CompilerInstance::createOutputFile( llvm::sys::fs::status(OutputPath, Status); if (llvm::sys::fs::exists(Status)) { // Fail early if we can't write to the final destination. - if (!llvm::sys::fs::can_write(OutputPath)) + if (!llvm::sys::fs::can_write(OutputPath)) { + Error = make_error_code(llvm::errc::operation_not_permitted); return nullptr; + } // Don't use a temporary if the output is a special file. This handles // things like '-o /dev/null' @@ -715,7 +728,7 @@ bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input, if (Input.isBuffer()) { SourceMgr.setMainFileID(SourceMgr.createFileID( std::unique_ptr<llvm::MemoryBuffer>(Input.getBuffer()), Kind)); - assert(!SourceMgr.getMainFileID().isInvalid() && + assert(SourceMgr.getMainFileID().isValid() && "Couldn't establish MainFileID!"); return true; } @@ -766,7 +779,7 @@ bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input, SourceMgr.overrideFileContents(File, std::move(SB)); } - assert(!SourceMgr.getMainFileID().isInvalid() && + assert(SourceMgr.getMainFileID().isValid() && "Couldn't establish MainFileID!"); return true; } @@ -788,6 +801,13 @@ bool CompilerInstance::ExecuteAction(FrontendAction &Act) { if (!hasTarget()) return false; + // Create TargetInfo for the other side of CUDA compilation. + if (getLangOpts().CUDA && !getFrontendOpts().AuxTriple.empty()) { + std::shared_ptr<TargetOptions> TO(new TargetOptions); + TO->Triple = getFrontendOpts().AuxTriple; + setAuxTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), TO)); + } + // Inform the target of the language options. // // FIXME: We shouldn't need to do this, the target should be immutable once @@ -810,13 +830,13 @@ bool CompilerInstance::ExecuteAction(FrontendAction &Act) { if (getFrontendOpts().ShowStats) llvm::EnableStatistics(); - for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) { + for (const FrontendInputFile &FIF : getFrontendOpts().Inputs) { // Reset the ID tables if we are reusing the SourceManager and parsing // regular files. if (hasSourceManager() && !Act.isModelParsingAction()) getSourceManager().clearIDTables(); - if (Act.BeginSourceFile(*this, getFrontendOpts().Inputs[i])) { + if (Act.BeginSourceFile(*this, FIF)) { Act.Execute(); Act.EndSourceFile(); } @@ -912,6 +932,7 @@ static bool compileModuleImpl(CompilerInstance &ImportingInstance, FrontendOpts.OutputFile = ModuleFileName.str(); FrontendOpts.DisableFree = false; FrontendOpts.GenerateGlobalModuleIndex = false; + FrontendOpts.BuildingImplicitModule = true; FrontendOpts.Inputs.clear(); InputKind IK = getSourceInputKindFromOptions(*Invocation->getLangOpts()); @@ -945,8 +966,10 @@ static bool compileModuleImpl(CompilerInstance &ImportingInstance, FullSourceLoc(ImportLoc, ImportingInstance.getSourceManager())); // If we're collecting module dependencies, we need to share a collector - // between all of the module CompilerInstances. + // between all of the module CompilerInstances. Other than that, we don't + // want to produce any dependency output from the module build. Instance.setModuleDepCollector(ImportingInstance.getModuleDepCollector()); + Invocation->getDependencyOutputOpts() = DependencyOutputOptions(); // Get or create the module map that we'll use to build this module. std::string InferredModuleMapContent; @@ -1151,6 +1174,7 @@ static void pruneModuleCache(const HeaderSearchOptions &HSOpts) { struct stat StatBuf; llvm::SmallString<128> TimestampFile; TimestampFile = HSOpts.ModuleCachePath; + assert(!TimestampFile.empty()); llvm::sys::path::append(TimestampFile, "modules.timestamp"); // Try to stat() the timestamp file. @@ -1229,8 +1253,8 @@ void CompilerInstance::createModuleManager() { // If we're implicitly building modules but not currently recursively // building a module, check whether we need to prune the module cache. - if (getLangOpts().ImplicitModules && - getSourceManager().getModuleBuildStack().empty() && + if (getSourceManager().getModuleBuildStack().empty() && + !getPreprocessor().getHeaderSearchInfo().getModuleCachePath().empty() && getHeaderSearchOpts().ModuleCachePruneInterval > 0 && getHeaderSearchOpts().ModuleCachePruneAfter > 0) { pruneModuleCache(getHeaderSearchOpts()); @@ -1244,7 +1268,8 @@ void CompilerInstance::createModuleManager() { ReadTimer = llvm::make_unique<llvm::Timer>("Reading modules", *FrontendTimerGroup); ModuleManager = new ASTReader( - getPreprocessor(), *Context, getPCHContainerReader(), + getPreprocessor(), getASTContext(), getPCHContainerReader(), + getFrontendOpts().ModuleFileExtensions, Sysroot.empty() ? "" : Sysroot.c_str(), PPOpts.DisablePCHValidation, /*AllowASTWithCompilerErrors=*/false, /*AllowConfigurationMismatch=*/false, @@ -1262,6 +1287,13 @@ void CompilerInstance::createModuleManager() { ModuleManager->InitializeSema(getSema()); if (hasASTConsumer()) ModuleManager->StartTranslationUnit(&getASTConsumer()); + + if (TheDependencyFileGenerator) + TheDependencyFileGenerator->AttachToASTReader(*ModuleManager); + if (ModuleDepCollector) + ModuleDepCollector->attachToASTReader(*ModuleManager); + for (auto &Listener : DependencyCollectors) + Listener->attachToASTReader(*ModuleManager); } } @@ -1276,87 +1308,68 @@ bool CompilerInstance::loadModuleFile(StringRef FileName) { // the files we were handed. struct ReadModuleNames : ASTReaderListener { CompilerInstance &CI; - std::vector<StringRef> ModuleFileStack; - std::vector<StringRef> ModuleNameStack; - bool Failed; - bool TopFileIsModule; - - ReadModuleNames(CompilerInstance &CI) - : CI(CI), Failed(false), TopFileIsModule(false) {} + llvm::SmallVector<IdentifierInfo*, 8> LoadedModules; - bool needsImportVisitation() const override { return true; } + ReadModuleNames(CompilerInstance &CI) : CI(CI) {} - void visitImport(StringRef FileName) override { - if (!CI.ExplicitlyLoadedModuleFiles.insert(FileName).second) { - if (ModuleFileStack.size() == 0) - TopFileIsModule = true; - return; - } + void ReadModuleName(StringRef ModuleName) override { + LoadedModules.push_back( + CI.getPreprocessor().getIdentifierInfo(ModuleName)); + } - ModuleFileStack.push_back(FileName); - ModuleNameStack.push_back(StringRef()); - if (ASTReader::readASTFileControlBlock(FileName, CI.getFileManager(), - CI.getPCHContainerReader(), - *this)) { - CI.getDiagnostics().Report( - SourceLocation(), CI.getFileManager().getBufferForFile(FileName) - ? diag::err_module_file_invalid - : diag::err_module_file_not_found) - << FileName; - for (int I = ModuleFileStack.size() - 2; I >= 0; --I) - CI.getDiagnostics().Report(SourceLocation(), - diag::note_module_file_imported_by) - << ModuleFileStack[I] - << !ModuleNameStack[I].empty() << ModuleNameStack[I]; - Failed = true; + void registerAll() { + for (auto *II : LoadedModules) { + CI.KnownModules[II] = CI.getPreprocessor() + .getHeaderSearchInfo() + .getModuleMap() + .findModule(II->getName()); } - ModuleNameStack.pop_back(); - ModuleFileStack.pop_back(); + LoadedModules.clear(); } - void ReadModuleName(StringRef ModuleName) override { - if (ModuleFileStack.size() == 1) - TopFileIsModule = true; - ModuleNameStack.back() = ModuleName; - - auto &ModuleFile = CI.ModuleFileOverrides[ModuleName]; - if (!ModuleFile.empty() && - CI.getFileManager().getFile(ModuleFile) != - CI.getFileManager().getFile(ModuleFileStack.back())) - CI.getDiagnostics().Report(SourceLocation(), - diag::err_conflicting_module_files) - << ModuleName << ModuleFile << ModuleFileStack.back(); - ModuleFile = ModuleFileStack.back(); + void markAllUnavailable() { + for (auto *II : LoadedModules) { + if (Module *M = CI.getPreprocessor() + .getHeaderSearchInfo() + .getModuleMap() + .findModule(II->getName())) + M->HasIncompatibleModuleFile = true; + } + LoadedModules.clear(); } - } RMN(*this); + }; // If we don't already have an ASTReader, create one now. if (!ModuleManager) createModuleManager(); - // Tell the module manager about this module file. - if (getModuleManager()->getModuleManager().addKnownModuleFile(FileName)) { - getDiagnostics().Report(SourceLocation(), diag::err_module_file_not_found) - << FileName; - return false; - } + auto Listener = llvm::make_unique<ReadModuleNames>(*this); + auto &ListenerRef = *Listener; + ASTReader::ListenerScope ReadModuleNamesListener(*ModuleManager, + std::move(Listener)); - // Build our mapping of module names to module files from this file - // and its imports. - RMN.visitImport(FileName); + // Try to load the module file. + switch (ModuleManager->ReadAST(FileName, serialization::MK_ExplicitModule, + SourceLocation(), + ASTReader::ARR_ConfigurationMismatch)) { + case ASTReader::Success: + // We successfully loaded the module file; remember the set of provided + // modules so that we don't try to load implicit modules for them. + ListenerRef.registerAll(); + return true; - if (RMN.Failed) - return false; + case ASTReader::ConfigurationMismatch: + // Ignore unusable module files. + getDiagnostics().Report(SourceLocation(), diag::warn_module_config_mismatch) + << FileName; + // All modules provided by any files we tried and failed to load are now + // unavailable; includes of those modules should now be handled textually. + ListenerRef.markAllUnavailable(); + return true; - // If we never found a module name for the top file, then it's not a module, - // it's a PCH or preamble or something. - if (!RMN.TopFileIsModule) { - getDiagnostics().Report(SourceLocation(), diag::err_module_file_not_module) - << FileName; + default: return false; } - - return true; } ModuleLoadResult @@ -1371,7 +1384,7 @@ CompilerInstance::loadModule(SourceLocation ImportLoc, // If we've already handled this import, just return the cached result. // This one-element cache is important to eliminate redundant diagnostics // when both the preprocessor and parser see the same import declaration. - if (!ImportLoc.isInvalid() && LastModuleImportLoc == ImportLoc) { + if (ImportLoc.isValid() && LastModuleImportLoc == ImportLoc) { // Make the named module visible. if (LastModuleImportResult && ModuleName != getLangOpts().CurrentModule && ModuleName != getLangOpts().ImplementationOfModule) @@ -1404,56 +1417,40 @@ CompilerInstance::loadModule(SourceLocation ImportLoc, return ModuleLoadResult(); } - auto Override = ModuleFileOverrides.find(ModuleName); - bool Explicit = Override != ModuleFileOverrides.end(); - if (!Explicit && !getLangOpts().ImplicitModules) { + std::string ModuleFileName = + PP->getHeaderSearchInfo().getModuleFileName(Module); + if (ModuleFileName.empty()) { + if (Module->HasIncompatibleModuleFile) { + // We tried and failed to load a module file for this module. Fall + // back to textual inclusion for its headers. + return ModuleLoadResult(nullptr, /*missingExpected*/true); + } + getDiagnostics().Report(ModuleNameLoc, diag::err_module_build_disabled) << ModuleName; ModuleBuildFailed = true; return ModuleLoadResult(); } - std::string ModuleFileName = - Explicit ? Override->second - : PP->getHeaderSearchInfo().getModuleFileName(Module); - // If we don't already have an ASTReader, create one now. if (!ModuleManager) createModuleManager(); - if (TheDependencyFileGenerator) - TheDependencyFileGenerator->AttachToASTReader(*ModuleManager); - - if (ModuleDepCollector) - ModuleDepCollector->attachToASTReader(*ModuleManager); - - for (auto &Listener : DependencyCollectors) - Listener->attachToASTReader(*ModuleManager); - llvm::Timer Timer; if (FrontendTimerGroup) Timer.init("Loading " + ModuleFileName, *FrontendTimerGroup); llvm::TimeRegion TimeLoading(FrontendTimerGroup ? &Timer : nullptr); // Try to load the module file. - unsigned ARRFlags = - Explicit ? 0 : ASTReader::ARR_OutOfDate | ASTReader::ARR_Missing; + unsigned ARRFlags = ASTReader::ARR_OutOfDate | ASTReader::ARR_Missing; switch (ModuleManager->ReadAST(ModuleFileName, - Explicit ? serialization::MK_ExplicitModule - : serialization::MK_ImplicitModule, + serialization::MK_ImplicitModule, ImportLoc, ARRFlags)) { case ASTReader::Success: break; case ASTReader::OutOfDate: case ASTReader::Missing: { - if (Explicit) { - // ReadAST has already complained for us. - ModuleLoader::HadFatalFailure = true; - KnownModules[Path[0].first] = nullptr; - return ModuleLoadResult(); - } - // The module file is missing or out-of-date. Build it. assert(Module && "missing module file"); // Check whether there is a cycle in the module graph. @@ -1508,7 +1505,7 @@ CompilerInstance::loadModule(SourceLocation ImportLoc, case ASTReader::ConfigurationMismatch: case ASTReader::HadErrors: ModuleLoader::HadFatalFailure = true; - // FIXME: The ASTReader will already have complained, but can we showhorn + // FIXME: The ASTReader will already have complained, but can we shoehorn // that diagnostic information into a more useful form? KnownModules[Path[0].first] = nullptr; return ModuleLoadResult(); @@ -1652,6 +1649,8 @@ void CompilerInstance::makeModuleVisible(Module *Mod, GlobalModuleIndex *CompilerInstance::loadGlobalModuleIndex( SourceLocation TriggerLoc) { + if (getPreprocessor().getHeaderSearchInfo().getModuleCachePath().empty()) + return nullptr; if (!ModuleManager) createModuleManager(); // Can't do anything if we don't have the module manager. @@ -1685,11 +1684,10 @@ GlobalModuleIndex *CompilerInstance::loadGlobalModuleIndex( if (!Entry) { SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path; Path.push_back(std::make_pair( - getPreprocessor().getIdentifierInfo(TheModule->Name), TriggerLoc)); + getPreprocessor().getIdentifierInfo(TheModule->Name), TriggerLoc)); std::reverse(Path.begin(), Path.end()); - // Load a module as hidden. This also adds it to the global index. - loadModule(TheModule->DefinitionLoc, Path, - Module::Hidden, false); + // Load a module as hidden. This also adds it to the global index. + loadModule(TheModule->DefinitionLoc, Path, Module::Hidden, false); RecreateIndex = true; } } |