Skip to content

Commit 6cf4470

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. (cherry picked from commit 5ebdfe3)
1 parent 98a3ca2 commit 6cf4470

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
@@ -3923,17 +3923,17 @@ static bool RenderModulesOptions(Compilation &C, const Driver &D,
39233923
const ArgList &Args, const InputInfo &Input,
39243924
const InputInfo &Output, bool HaveStd20,
39253925
ArgStringList &CmdArgs) {
3926-
bool IsCXX = types::isCXX(Input.getType());
3927-
bool HaveStdCXXModules = IsCXX && HaveStd20;
3926+
const bool IsCXX = types::isCXX(Input.getType());
3927+
const bool HaveStdCXXModules = IsCXX && HaveStd20;
39283928
bool HaveModules = HaveStdCXXModules;
39293929

39303930
// -fmodules enables the use of precompiled modules (off by default).
39313931
// Users can pass -fno-cxx-modules to turn off modules support for
39323932
// C++/Objective-C++ programs.
3933+
const bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules,
3934+
options::OPT_fno_cxx_modules, true);
39333935
bool HaveClangModules = false;
39343936
if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) {
3935-
bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules,
3936-
options::OPT_fno_cxx_modules, true);
39373937
if (AllowedInCXX || !IsCXX) {
39383938
CmdArgs.push_back("-fmodules");
39393939
HaveClangModules = true;
@@ -3942,6 +3942,9 @@ static bool RenderModulesOptions(Compilation &C, const Driver &D,
39423942

39433943
HaveModules |= HaveClangModules;
39443944

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