Skip to content

Commit 72027ec

Browse files
committed
[clang][Modules] Respect -fno-cxx-modules as a driver flag (llvm#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 bd45852 commit 72027ec

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
@@ -3946,17 +3946,17 @@ static bool RenderModulesOptions(Compilation &C, const Driver &D,
39463946
const ArgList &Args, const InputInfo &Input,
39473947
const InputInfo &Output, bool HaveStd20,
39483948
ArgStringList &CmdArgs) {
3949-
bool IsCXX = types::isCXX(Input.getType());
3950-
bool HaveStdCXXModules = IsCXX && HaveStd20;
3949+
const bool IsCXX = types::isCXX(Input.getType());
3950+
const bool HaveStdCXXModules = IsCXX && HaveStd20;
39513951
bool HaveModules = HaveStdCXXModules;
39523952

39533953
// -fmodules enables the use of precompiled modules (off by default).
39543954
// Users can pass -fno-cxx-modules to turn off modules support for
39553955
// C++/Objective-C++ programs.
3956+
const bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules,
3957+
options::OPT_fno_cxx_modules, true);
39563958
bool HaveClangModules = false;
39573959
if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) {
3958-
bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules,
3959-
options::OPT_fno_cxx_modules, true);
39603960
if (AllowedInCXX || !IsCXX) {
39613961
CmdArgs.push_back("-fmodules");
39623962
HaveClangModules = true;
@@ -3965,6 +3965,9 @@ static bool RenderModulesOptions(Compilation &C, const Driver &D,
39653965

39663966
HaveModules |= HaveClangModules;
39673967

3968+
if (HaveModules && !AllowedInCXX)
3969+
CmdArgs.push_back("-fno-cxx-modules");
3970+
39683971
// -fmodule-maps enables implicit reading of module map files. By default,
39693972
// this is enabled if we are using Clang's flavor of precompiled modules.
39703973
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)