From c1bbec85a87c9a929b1814a0b316ed28e6e224af Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Fri, 25 Jan 2013 00:45:27 +0000 Subject: [PATCH] Rename the -cc1 option "-generate-module-index" to "-fmodules-global-index" and expand its behavior to include both the use and generation of the global module index. llvm-svn: 173404 --- clang/include/clang/Driver/CC1Options.td | 2 +- clang/include/clang/Frontend/CompilerInstance.h | 10 ++++++--- clang/include/clang/Frontend/FrontendOptions.h | 11 ++++++---- clang/include/clang/Serialization/ASTReader.h | 12 +++++++++- clang/lib/Frontend/CompilerInstance.cpp | 29 ++++++++++++++++++------- clang/lib/Frontend/CompilerInvocation.cpp | 3 ++- clang/lib/Frontend/FrontendAction.cpp | 7 +++--- clang/lib/Serialization/ASTReader.cpp | 3 ++- clang/test/Modules/global_index.m | 8 ++++++- 9 files changed, 61 insertions(+), 24 deletions(-) diff --git a/clang/include/clang/Driver/CC1Options.td b/clang/include/clang/Driver/CC1Options.td index 77fa82a..f9a2558 100644 --- a/clang/include/clang/Driver/CC1Options.td +++ b/clang/include/clang/Driver/CC1Options.td @@ -289,7 +289,7 @@ def ast_dump_filter : Separate<["-"], "ast-dump-filter">, HelpText<"Use with -ast-dump or -ast-print to dump/print only AST declaration" " nodes having a certain substring in a qualified name. Use" " -ast-list to list all filterable declaration node names.">; -def generate_module_index : Flag<["-"], "generate-module-index">, +def fmodules_global_index : Flag<["-"], "fmodules-global-index">, HelpText<"Automatically generate or update the global module index">; let Group = Action_Group in { diff --git a/clang/include/clang/Frontend/CompilerInstance.h b/clang/include/clang/Frontend/CompilerInstance.h index d6b3afa..a17e1c8 100644 --- a/clang/include/clang/Frontend/CompilerInstance.h +++ b/clang/include/clang/Frontend/CompilerInstance.h @@ -117,6 +117,9 @@ class CompilerInstance : public ModuleLoader { /// have finished with this translation unit. bool BuildGlobalModuleIndex; + /// \brief One or more modules failed to build. + bool ModuleBuildFailed; + /// \brief Holds information about the output file. /// /// If TempFilename is not empty we must rename it to Filename at the end. @@ -191,8 +194,8 @@ public: void setInvocation(CompilerInvocation *Value); /// \brief Indicates whether we should (re)build the global module index. - bool getBuildGlobalModuleIndex() const { return BuildGlobalModuleIndex; } - + bool shouldBuildGlobalModuleIndex() const; + /// \brief Set the flag indicating whether we should (re)build the global /// module index. void setBuildGlobalModuleIndex(bool Build) { @@ -549,7 +552,8 @@ public: bool DisablePCHValidation, bool AllowPCHWithCompilerErrors, Preprocessor &PP, ASTContext &Context, - void *DeserializationListener, bool Preamble); + void *DeserializationListener, bool Preamble, + bool UseGlobalModuleIndex); /// Create a code completion consumer using the invocation; note that this /// will cause the source manager to truncate the input source file at the diff --git a/clang/include/clang/Frontend/FrontendOptions.h b/clang/include/clang/Frontend/FrontendOptions.h index bb6da34..3816211 100644 --- a/clang/include/clang/Frontend/FrontendOptions.h +++ b/clang/include/clang/Frontend/FrontendOptions.h @@ -137,9 +137,11 @@ public: /// speed up parsing in cases you do /// not need them (e.g. with code /// completion). - unsigned GenerateModuleIndex : 1; ///< Whether to auto-generate a - ///< global module index when a new - ///< module has been built. + unsigned UseGlobalModuleIndex : 1; ///< Whether we can use the + ///< global module index if available. + unsigned GenerateGlobalModuleIndex : 1; ///< Whether we can generate the + ///< global module index if needed. + CodeCompleteOptions CodeCompleteOpts; enum { @@ -211,7 +213,8 @@ public: ShowStats(false), ShowTimers(false), ShowVersion(false), FixWhatYouCan(false), FixOnlyWarnings(false), FixAndRecompile(false), FixToTemporaries(false), ARCMTMigrateEmitARCErrors(false), - SkipFunctionBodies(false), GenerateModuleIndex(false), + SkipFunctionBodies(false), UseGlobalModuleIndex(false), + GenerateGlobalModuleIndex(false), ARCMTAction(ARCMT_None), ObjCMTAction(ObjCMT_None), ProgramAction(frontend::ParseSyntaxOnly) {} diff --git a/clang/include/clang/Serialization/ASTReader.h b/clang/include/clang/Serialization/ASTReader.h index cbd523b..5d80e0d 100644 --- a/clang/include/clang/Serialization/ASTReader.h +++ b/clang/include/clang/Serialization/ASTReader.h @@ -692,6 +692,12 @@ private: /// \brief Whether to accept an AST file with compiler errors. bool AllowASTWithCompilerErrors; + /// \brief Whether we are allowed to use the global module index. + bool UseGlobalIndex; + + /// \brief Whether we have tried loading the global module index yet. + bool TriedLoadingGlobalIndex; + /// \brief The current "generation" of the module file import stack, which /// indicates how many separate module file load operations have occurred. unsigned CurrentGeneration; @@ -1081,9 +1087,13 @@ public: /// \param AllowASTWithCompilerErrors If true, the AST reader will accept an /// AST file the was created out of an AST with compiler errors, /// otherwise it will reject it. + /// + /// \param UseGlobalIndex If true, the AST reader will try to load and use + /// the global module index. ASTReader(Preprocessor &PP, ASTContext &Context, StringRef isysroot = "", bool DisableValidation = false, - bool AllowASTWithCompilerErrors = false); + bool AllowASTWithCompilerErrors = false, + bool UseGlobalIndex = true); ~ASTReader(); diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp index 07c0248..0e60c39 100644 --- a/clang/lib/Frontend/CompilerInstance.cpp +++ b/clang/lib/Frontend/CompilerInstance.cpp @@ -49,7 +49,7 @@ using namespace clang; CompilerInstance::CompilerInstance() : Invocation(new CompilerInvocation()), ModuleManager(0), - BuildGlobalModuleIndex(false) { + BuildGlobalModuleIndex(false), ModuleBuildFailed(false) { } CompilerInstance::~CompilerInstance() { @@ -60,6 +60,10 @@ void CompilerInstance::setInvocation(CompilerInvocation *Value) { Invocation = Value; } +bool CompilerInstance::shouldBuildGlobalModuleIndex() const { + return BuildGlobalModuleIndex && !ModuleBuildFailed; +} + void CompilerInstance::setDiagnostics(DiagnosticsEngine *Value) { Diagnostics = Value; } @@ -290,7 +294,8 @@ void CompilerInstance::createPCHExternalASTSource(StringRef Path, AllowPCHWithCompilerErrors, getPreprocessor(), getASTContext(), DeserializationListener, - Preamble)); + Preamble, + getFrontendOpts().UseGlobalModuleIndex)); ModuleManager = static_cast(Source.get()); getASTContext().setExternalSource(Source); } @@ -303,12 +308,14 @@ CompilerInstance::createPCHExternalASTSource(StringRef Path, Preprocessor &PP, ASTContext &Context, void *DeserializationListener, - bool Preamble) { + bool Preamble, + bool UseGlobalModuleIndex) { OwningPtr Reader; Reader.reset(new ASTReader(PP, Context, Sysroot.empty() ? "" : Sysroot.c_str(), DisablePCHValidation, - AllowPCHWithCompilerErrors)); + AllowPCHWithCompilerErrors, + UseGlobalModuleIndex)); Reader->setDeserializationListener( static_cast(DeserializationListener)); @@ -786,7 +793,7 @@ static void compileModule(CompilerInstance &ImportingInstance, FrontendOptions &FrontendOpts = Invocation->getFrontendOpts(); FrontendOpts.OutputFile = ModuleFileName.str(); FrontendOpts.DisableFree = false; - FrontendOpts.GenerateModuleIndex = false; + FrontendOpts.GenerateGlobalModuleIndex = false; FrontendOpts.Inputs.clear(); InputKind IK = getSourceInputKindFromOptions(*Invocation->getLangOpts()); @@ -863,7 +870,7 @@ static void compileModule(CompilerInstance &ImportingInstance, // We've rebuilt a module. If we're allowed to generate or update the global // module index, record that fact in the importing compiler instance. - if (ImportingInstance.getFrontendOpts().GenerateModuleIndex) { + if (ImportingInstance.getFrontendOpts().GenerateGlobalModuleIndex) { ImportingInstance.setBuildGlobalModuleIndex(true); } } @@ -953,7 +960,7 @@ CompilerInstance::loadModule(SourceLocation ImportLoc, getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_built) << ModuleName << SourceRange(ImportLoc, ModuleNameLoc); - + ModuleBuildFailed = true; return ModuleLoadResult(); } @@ -971,6 +978,7 @@ CompilerInstance::loadModule(SourceLocation ImportLoc, : diag::err_module_not_found) << ModuleName << SourceRange(ImportLoc, ModuleNameLoc); + ModuleBuildFailed = true; return ModuleLoadResult(); } @@ -983,7 +991,9 @@ CompilerInstance::loadModule(SourceLocation ImportLoc, const PreprocessorOptions &PPOpts = getPreprocessorOpts(); ModuleManager = new ASTReader(getPreprocessor(), *Context, Sysroot.empty() ? "" : Sysroot.c_str(), - PPOpts.DisablePCHValidation); + PPOpts.DisablePCHValidation, + /*AllowASTWithCompilerErrors=*/false, + getFrontendOpts().UseGlobalModuleIndex); if (hasASTConsumer()) { ModuleManager->setDeserializationListener( getASTConsumer().GetASTDeserializationListener()); @@ -1024,6 +1034,7 @@ CompilerInstance::loadModule(SourceLocation ImportLoc, getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_built) << ModuleName << SourceRange(ImportLoc, ModuleNameLoc); + ModuleBuildFailed = true; return ModuleLoadResult(); } @@ -1039,6 +1050,7 @@ CompilerInstance::loadModule(SourceLocation ImportLoc, if (getPreprocessorOpts().FailedModules) getPreprocessorOpts().FailedModules->addFailed(ModuleName); KnownModules[Path[0].first] = 0; + ModuleBuildFailed = true; return ModuleLoadResult(); } @@ -1057,6 +1069,7 @@ CompilerInstance::loadModule(SourceLocation ImportLoc, case ASTReader::Failure: // Already complained, but note now that we failed. KnownModules[Path[0].first] = 0; + ModuleBuildFailed = true; return ModuleLoadResult(); } diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 9c94dfe..e135ad4 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -689,7 +689,8 @@ static InputKind ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args, Opts.FixAndRecompile = Args.hasArg(OPT_fixit_recompile); Opts.FixToTemporaries = Args.hasArg(OPT_fixit_to_temp); Opts.ASTDumpFilter = Args.getLastArgValue(OPT_ast_dump_filter); - Opts.GenerateModuleIndex = Args.hasArg(OPT_generate_module_index); + Opts.UseGlobalModuleIndex = Args.hasArg(OPT_fmodules_global_index); + Opts.GenerateGlobalModuleIndex = Opts.UseGlobalModuleIndex; Opts.CodeCompleteOpts.IncludeMacros = Args.hasArg(OPT_code_completion_macros); diff --git a/clang/lib/Frontend/FrontendAction.cpp b/clang/lib/Frontend/FrontendAction.cpp index 4b642b3..05064fb 100644 --- a/clang/lib/Frontend/FrontendAction.cpp +++ b/clang/lib/Frontend/FrontendAction.cpp @@ -382,10 +382,9 @@ bool FrontendAction::Execute() { else ExecuteAction(); // If we are supposed to rebuild the global module index, do so now unless - // an error occurred. - if (CI.getBuildGlobalModuleIndex() && CI.hasFileManager() && - CI.hasPreprocessor() && - (!CI.hasDiagnostics() || !CI.getDiagnostics().hasErrorOccurred())) { + // there were any module-build failures. + if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() && + CI.hasPreprocessor()) { GlobalModuleIndex::writeIndex( CI.getFileManager(), CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath()); diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp index c8bca73..e24fddf 100644 --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -6941,13 +6941,14 @@ void ASTReader::FinishedDeserializing() { ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context, StringRef isysroot, bool DisableValidation, - bool AllowASTWithCompilerErrors) + bool AllowASTWithCompilerErrors, bool UseGlobalIndex) : Listener(new PCHValidator(PP, *this)), DeserializationListener(0), SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context), Consumer(0), ModuleMgr(PP.getFileManager()), isysroot(isysroot), DisableValidation(DisableValidation), AllowASTWithCompilerErrors(AllowASTWithCompilerErrors), + UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false), CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts), NumSLocEntriesRead(0), TotalNumSLocEntries(0), NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0), diff --git a/clang/test/Modules/global_index.m b/clang/test/Modules/global_index.m index 423e111..f183b1f 100644 --- a/clang/test/Modules/global_index.m +++ b/clang/test/Modules/global_index.m @@ -1,7 +1,13 @@ // RUN: rm -rf %t -// RUN: %clang_cc1 -Wauto-import -fmodule-cache-path %t -fdisable-module-hash -fmodules -generate-module-index -F %S/Inputs %s -verify +// RUN: %clang_cc1 -Wauto-import -fmodule-cache-path %t -fdisable-module-hash -fmodules -fmodules-global-index -F %S/Inputs %s -verify // RUN: ls %t|grep modules.idx +// RUN: %clang_cc1 -Wauto-import -fmodule-cache-path %t -fdisable-module-hash -fmodules -fmodules-global-index -F %S/Inputs %s -verify +// REQUIRES: shell // expected-no-diagnostics @import DependsOnModule; +@import Module; +int *get_sub() { + return Module_Sub; +} -- 2.7.4