Index: docs/Modules.rst =================================================================== --- docs/Modules.rst +++ docs/Modules.rst @@ -207,6 +207,9 @@ ``-fmodules-search-all`` If a symbol is not found, search modules referenced in the current module maps but not imported for symbols, so the error message can reference the module by name. Note that if the global module index has not been built before, this might take some time as it needs to build all the modules. Note that this option doesn't apply in module builds, to avoid the recursion. +``-fno-modules-implicit-maps`` + Suppresses the implicit search for files called ``module.modulemap`` and similar. Instead, module files need to be explicitly specified via ``-fmodule-map-file`` or transitively used. + Module Semantics ================ Index: include/clang/Basic/LangOptions.def =================================================================== --- include/clang/Basic/LangOptions.def +++ include/clang/Basic/LangOptions.def @@ -126,6 +126,7 @@ LANGOPT(ModulesSearchAll , 1, 1, "search even non-imported modules to find unresolved references") LANGOPT(ModulesStrictDeclUse, 1, 0, "require declaration of module uses and all headers to be in modules") LANGOPT(ModulesErrorRecovery, 1, 1, "automatically import modules as needed when performing error recovery") +BENIGN_LANGOPT(ModulesImplicitMaps, 1, 1, "use files called module.modulemap implicitly as module maps") COMPATIBLE_LANGOPT(Optimize , 1, 0, "__OPTIMIZE__ predefined macro") COMPATIBLE_LANGOPT(OptimizeSize , 1, 0, "__OPTIMIZE_SIZE__ predefined macro") LANGOPT(Static , 1, 0, "__STATIC__ predefined macro (as opposed to __DYNAMIC__)") Index: include/clang/Driver/Options.td =================================================================== --- include/clang/Driver/Options.td +++ include/clang/Driver/Options.td @@ -675,6 +675,12 @@ HelpText<"Like -fmodules-decluse but requires all headers to be in modules">; def fno_modules_search_all : Flag <["-"], "fno-modules-search-all">, Group, Flags<[DriverOption, CC1Option]>; +def fmodules_implicit_maps : + Flag <["-"], "fmodules-implicit-maps">, + Group, Flags<[DriverOption, CC1Option]>; +def fno_modules_implicit_maps : + Flag <["-"], "fno-modules-implicit-maps">, + Group, Flags<[DriverOption, CC1Option]>; def fretain_comments_from_system_headers : Flag<["-"], "fretain-comments-from-system-headers">, Group, Flags<[CC1Option]>; def fmudflapth : Flag<["-"], "fmudflapth">, Group; Index: include/clang/Lex/HeaderSearch.h =================================================================== --- include/clang/Lex/HeaderSearch.h +++ include/clang/Lex/HeaderSearch.h @@ -252,7 +252,7 @@ unsigned NumMultiIncludeFileOptzn; unsigned NumFrameworkLookups, NumSubFrameworkLookups; - bool EnabledModules; + const LangOptions &LangOpts; // HeaderSearch doesn't support default or copy construction. HeaderSearch(const HeaderSearch&) LLVM_DELETED_FUNCTION; @@ -479,7 +479,7 @@ const HeaderMap *CreateHeaderMap(const FileEntry *FE); /// Returns true if modules are enabled. - bool enabledModules() const { return EnabledModules; } + bool enabledModules() const { return LangOpts.Modules; } /// \brief Retrieve the name of the module file that should be used to /// load the given module. Index: lib/Frontend/CompilerInvocation.cpp =================================================================== --- lib/Frontend/CompilerInvocation.cpp +++ lib/Frontend/CompilerInvocation.cpp @@ -1462,6 +1462,9 @@ !Args.hasArg(OPT_fno_modules_search_all) && Args.hasArg(OPT_fmodules_search_all); Opts.ModulesErrorRecovery = !Args.hasArg(OPT_fno_modules_error_recovery); + Opts.ModulesImplicitMaps = + Args.hasFlag(OPT_fmodules_implicit_maps, OPT_fno_modules_implicit_maps, + Opts.ModulesImplicitMaps); Opts.CharIsSigned = Opts.OpenCL || !Args.hasArg(OPT_fno_signed_char); Opts.WChar = Opts.CPlusPlus && !Args.hasArg(OPT_fno_wchar); Opts.ShortWChar = Args.hasFlag(OPT_fshort_wchar, OPT_fno_short_wchar, false); Index: lib/Lex/HeaderSearch.cpp =================================================================== --- lib/Lex/HeaderSearch.cpp +++ lib/Lex/HeaderSearch.cpp @@ -50,7 +50,8 @@ const LangOptions &LangOpts, const TargetInfo *Target) : HSOpts(HSOpts), Diags(Diags), FileMgr(SourceMgr.getFileManager()), - FrameworkMap(64), ModMap(SourceMgr, Diags, LangOpts, Target, *this) { + FrameworkMap(64), ModMap(SourceMgr, Diags, LangOpts, Target, *this), + LangOpts(LangOpts) { AngledDirIdx = 0; SystemDirIdx = 0; NoCurDirSearch = false; @@ -60,8 +61,6 @@ NumIncluded = 0; NumMultiIncludeFileOptzn = 0; NumFrameworkLookups = NumSubFrameworkLookups = 0; - - EnabledModules = LangOpts.Modules; } HeaderSearch::~HeaderSearch() { @@ -149,7 +148,7 @@ Module *HeaderSearch::lookupModule(StringRef ModuleName, bool AllowSearch) { // Look in the module map to determine if there is a module by this name. Module *Module = ModMap.findModule(ModuleName); - if (Module || !AllowSearch) + if (Module || !AllowSearch || !LangOpts.ModulesImplicitMaps) return Module; // Look through the various header search paths to load any available module @@ -1062,7 +1061,7 @@ bool HeaderSearch::hasModuleMap(StringRef FileName, const DirectoryEntry *Root, bool IsSystem) { - if (!enabledModules()) + if (!enabledModules() || !LangOpts.ModulesImplicitMaps) return false; SmallVector FixUpDirectories; @@ -1170,6 +1169,8 @@ const FileEntry * HeaderSearch::lookupModuleMapFile(const DirectoryEntry *Dir, bool IsFramework) { + if (!LangOpts.ModulesImplicitMaps) + return nullptr; // For frameworks, the preferred spelling is Modules/module.modulemap, but // module.map at the framework root is also accepted. SmallString<128> ModuleMapFileName(Dir->getName()); @@ -1190,12 +1191,12 @@ bool IsSystem) { if (Module *Module = ModMap.findModule(Name)) return Module; - + // Try to load a module map file. switch (loadModuleMapFile(Dir, IsSystem, /*IsFramework*/true)) { case LMM_InvalidModuleMap: break; - + case LMM_AlreadyLoaded: case LMM_NoDirectory: return nullptr; @@ -1206,7 +1207,10 @@ // Try to infer a module map from the framework directory. - return ModMap.inferFrameworkModule(Name, Dir, IsSystem, /*Parent=*/nullptr); + if (LangOpts.ModulesImplicitMaps) + return ModMap.inferFrameworkModule(Name, Dir, IsSystem, /*Parent=*/nullptr); + + return nullptr; } @@ -1242,45 +1246,49 @@ void HeaderSearch::collectAllModules(SmallVectorImpl &Modules) { Modules.clear(); - - // Load module maps for each of the header search directories. - for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) { - bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory(); - if (SearchDirs[Idx].isFramework()) { - std::error_code EC; - SmallString<128> DirNative; - llvm::sys::path::native(SearchDirs[Idx].getFrameworkDir()->getName(), - DirNative); - - // Search each of the ".framework" directories to load them as modules. - for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd; - Dir != DirEnd && !EC; Dir.increment(EC)) { - if (llvm::sys::path::extension(Dir->path()) != ".framework") - continue; - - const DirectoryEntry *FrameworkDir = FileMgr.getDirectory(Dir->path()); - if (!FrameworkDir) - continue; - - // Load this framework module. - loadFrameworkModule(llvm::sys::path::stem(Dir->path()), FrameworkDir, - IsSystem); + + if (LangOpts.ModulesImplicitMaps) { + // Load module maps for each of the header search directories. + for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) { + bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory(); + if (SearchDirs[Idx].isFramework()) { + std::error_code EC; + SmallString<128> DirNative; + llvm::sys::path::native(SearchDirs[Idx].getFrameworkDir()->getName(), + DirNative); + + // Search each of the ".framework" directories to load them as modules. + for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd; + Dir != DirEnd && !EC; Dir.increment(EC)) { + if (llvm::sys::path::extension(Dir->path()) != ".framework") + continue; + + const DirectoryEntry *FrameworkDir = + FileMgr.getDirectory(Dir->path()); + if (!FrameworkDir) + continue; + + // Load this framework module. + loadFrameworkModule(llvm::sys::path::stem(Dir->path()), FrameworkDir, + IsSystem); + } + continue; } - continue; + + // FIXME: Deal with header maps. + if (SearchDirs[Idx].isHeaderMap()) + continue; + + // Try to load a module map file for the search directory. + loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem, + /*IsFramework*/ false); + + // Try to load module map files for immediate subdirectories of this + // search directory. + loadSubdirectoryModuleMaps(SearchDirs[Idx]); } - - // FIXME: Deal with header maps. - if (SearchDirs[Idx].isHeaderMap()) - continue; - - // Try to load a module map file for the search directory. - loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem, /*IsFramework*/false); - - // Try to load module map files for immediate subdirectories of this search - // directory. - loadSubdirectoryModuleMaps(SearchDirs[Idx]); } - + // Populate the list of modules. for (ModuleMap::module_iterator M = ModMap.module_begin(), MEnd = ModMap.module_end(); @@ -1290,6 +1298,9 @@ } void HeaderSearch::loadTopLevelSystemModules() { + if (!LangOpts.ModulesImplicitMaps) + return; + // Load module maps for each of the header search directories. for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) { // We only care about normal header directories. @@ -1305,6 +1316,9 @@ } void HeaderSearch::loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir) { + assert(LangOpts.ModulesImplicitMaps && + "Should not be loading subdirectory module maps"); + if (SearchDir.haveSearchedAllModuleMaps()) return; Index: test/Modules/no-implicit-maps.cpp =================================================================== --- /dev/null +++ test/Modules/no-implicit-maps.cpp @@ -0,0 +1,3 @@ +// RUN: rm -rf %t +// RUN: %clang_cc1 -x objective-c -fno-modules-implicit-maps -fmodules-cache-path=%t -fmodules -I %S/Inputs/private %s -verify +@import libPrivate1; // expected-error {{not found}}