Skip to content

Commit 5ebdfe3

Browse files
authored
[clang][Modules] Respect -fno-cxx-modules as a driver flag (#150349)
The mentioned flag is already both a cc1 & driver flag; however, whether it is respected was tied to either: 1. Whether it was passed as a cc1 option (`Xclang`) 2. or `-fmodules` accompanying it This poses a consistency problem where `std=c++20` enables the modules feature, independent of other module settings. This patch resolves this issue by checking for the presence unconditionally & passing it down to cc1 when applicable.
1 parent 3e1392f commit 5ebdfe3

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3881,17 +3881,17 @@ static bool RenderModulesOptions(Compilation &C, const Driver &D,
38813881
const ArgList &Args, const InputInfo &Input,
38823882
const InputInfo &Output, bool HaveStd20,
38833883
ArgStringList &CmdArgs) {
3884-
bool IsCXX = types::isCXX(Input.getType());
3885-
bool HaveStdCXXModules = IsCXX && HaveStd20;
3884+
const bool IsCXX = types::isCXX(Input.getType());
3885+
const bool HaveStdCXXModules = IsCXX && HaveStd20;
38863886
bool HaveModules = HaveStdCXXModules;
38873887

38883888
// -fmodules enables the use of precompiled modules (off by default).
38893889
// Users can pass -fno-cxx-modules to turn off modules support for
38903890
// C++/Objective-C++ programs.
3891+
const bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules,
3892+
options::OPT_fno_cxx_modules, true);
38913893
bool HaveClangModules = false;
38923894
if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) {
3893-
bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules,
3894-
options::OPT_fno_cxx_modules, true);
38953895
if (AllowedInCXX || !IsCXX) {
38963896
CmdArgs.push_back("-fmodules");
38973897
HaveClangModules = true;
@@ -3900,6 +3900,9 @@ static bool RenderModulesOptions(Compilation &C, const Driver &D,
39003900

39013901
HaveModules |= HaveClangModules;
39023902

3903+
if (HaveModules && !AllowedInCXX)
3904+
CmdArgs.push_back("-fno-cxx-modules");
3905+
39033906
// -fmodule-maps enables implicit reading of module map files. By default,
39043907
// this is enabled if we are using Clang's flavor of precompiled modules.
39053908
if (Args.hasFlag(options::OPT_fimplicit_module_maps,

clang/test/Driver/modules.mm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// RUN: %clang -fmodules -fno-cxx-modules -### %s 2>&1 | FileCheck -check-prefix=CHECK-NO-MODULES %s
44
// CHECK-NO-MODULES-NOT: -fmodules
55

6+
// RUN: %clang -std=c++20 -fno-cxx-modules -### %s 2>&1 | FileCheck -check-prefix=CHECK-NO-CPP-20-MODULES %s
7+
// CHECK-NO-CPP-20-MODULES: -fno-cxx-modules
8+
69
// RUN: %clang -fmodules -### %s 2>&1 | FileCheck -check-prefix=CHECK-HAS-MODULES %s
710
// RUN: %clang -fmodules -fno-cxx-modules -fcxx-modules -### %s 2>&1 | FileCheck -check-prefix=CHECK-HAS-MODULES %s
811
// CHECK-HAS-MODULES: -fmodules

0 commit comments

Comments
 (0)