Index: lib/Driver/Tools.cpp =================================================================== --- lib/Driver/Tools.cpp +++ lib/Driver/Tools.cpp @@ -5615,11 +5615,26 @@ CmdArgs.push_back("-fcoroutines-ts"); } + // If modules is not asked for explicitly enable it exclusively + // for the Clang builtins in applicable cases. + bool HaveBuiltinModules = !Args.hasArg(options::OPT_fno_implicit_modules) && + !Args.hasArg(options::OPT_fmodules) && + !Args.hasArg(options::OPT_include) && + !isa(JA) && + ((JA.getType() == types::TY_LLVM_BC) || + (JA.getType() == types::TY_LLVM_IR) || + (JA.getType() == types::TY_LTO_BC) || + (JA.getType() == types::TY_LTO_IR) || + (JA.getType() == types::TY_PP_Asm) || + (JA.getType() == types::TY_Object) || + (JA.getType() == types::TY_AST)); + // -fmodules enables the use of precompiled modules (off by default). // Users can pass -fno-cxx-modules to turn off modules support for // C++/Objective-C++ programs. bool HaveClangModules = false; - if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) { + if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, + HaveBuiltinModules)) { bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules, options::OPT_fno_cxx_modules, true); if (AllowedInCXX || !types::isCXX(InputType)) { @@ -5635,9 +5650,11 @@ } // -fmodule-maps enables implicit reading of module map files. By default, - // this is enabled if we are using Clang's flavor of precompiled modules. + // this is enabled if Clang's flavor of precompiled modules is explicitly + // enabled. if (Args.hasFlag(options::OPT_fimplicit_module_maps, - options::OPT_fno_implicit_module_maps, HaveClangModules)) { + options::OPT_fno_implicit_module_maps, + HaveClangModules && !HaveBuiltinModules)) { CmdArgs.push_back("-fimplicit-module-maps"); } @@ -5701,7 +5718,8 @@ // -fbuiltin-module-map can be used to load the clang // builtin headers modulemap file. - if (Args.hasArg(options::OPT_fbuiltin_module_map)) { + if (Args.hasArg(options::OPT_fbuiltin_module_map) || + (HaveClangModules && HaveBuiltinModules)) { SmallString<128> BuiltinModuleMap(getToolChain().getDriver().ResourceDir); llvm::sys::path::append(BuiltinModuleMap, "include"); llvm::sys::path::append(BuiltinModuleMap, "module.modulemap"); Index: test/Driver/modules.mm =================================================================== --- test/Driver/modules.mm +++ test/Driver/modules.mm @@ -1,5 +1,8 @@ -// RUN: %clang -### %s 2>&1 | FileCheck -check-prefix=CHECK-NO-MODULES %s -// RUN: %clang -fcxx-modules -### %s 2>&1 | FileCheck -check-prefix=CHECK-NO-MODULES %s +// RUN: %clang -### %s 2>&1 | FileCheck -check-prefix=CHECK-DEFAULT-MODULES %s +// CHECK-DEFAULT-MODULES: -fmodules + +// RUN: %clang -fno-modules -### %s 2>&1 | FileCheck -check-prefix=CHECK-NO-MODULES %s +// RUN: %clang -fno-cxx-modules -### %s 2>&1 | FileCheck -check-prefix=CHECK-NO-MODULES %s // RUN: %clang -fmodules -fno-cxx-modules -### %s 2>&1 | FileCheck -check-prefix=CHECK-NO-MODULES %s // CHECK-NO-MODULES-NOT: -fmodules