Repository navigation
[clang][Modules] Respect -fno-cxx-modules as a driver flag - #150349
Conversation
The mentioned flag is 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 as accompanying it This poses a problem where c++20 modules is enabled implicitly given the language standard regardless of other module settings. This patch resolves this issue by checking for the presense unconditionally & passing it down to cc1 when applicable.
|
@llvm/pr-subscribers-clang-driver @llvm/pr-subscribers-clang Author: Cyndy Ishida (cyndyishida) ChangesThe mentioned flag is already both a cc1 & driver flag; however, whether it is respected was tied to either:
This poses a consistency problem where 2 Files Affected:
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 7d0c142ecd061..9d882dbfd0c65 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -3881,17 +3881,17 @@ static bool RenderModulesOptions(Compilation &C, const Driver &D,
const ArgList &Args, const InputInfo &Input,
const InputInfo &Output, bool HaveStd20,
ArgStringList &CmdArgs) {
- bool IsCXX = types::isCXX(Input.getType());
- bool HaveStdCXXModules = IsCXX && HaveStd20;
+ const bool IsCXX = types::isCXX(Input.getType());
+ const bool HaveStdCXXModules = IsCXX && HaveStd20;
bool HaveModules = HaveStdCXXModules;
// -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.
+ const bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules,
+ options::OPT_fno_cxx_modules, true);
bool HaveClangModules = false;
if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) {
- bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules,
- options::OPT_fno_cxx_modules, true);
if (AllowedInCXX || !IsCXX) {
CmdArgs.push_back("-fmodules");
HaveClangModules = true;
@@ -3900,6 +3900,9 @@ static bool RenderModulesOptions(Compilation &C, const Driver &D,
HaveModules |= HaveClangModules;
+ if (HaveModules && !AllowedInCXX)
+ CmdArgs.push_back("-fno-cxx-modules");
+
// -fmodule-maps enables implicit reading of module map files. By default,
// this is enabled if we are using Clang's flavor of precompiled modules.
if (Args.hasFlag(options::OPT_fimplicit_module_maps,
diff --git a/clang/test/Driver/modules.mm b/clang/test/Driver/modules.mm
index d1536c73a1f3b..f0b06697a37ce 100644
--- a/clang/test/Driver/modules.mm
+++ b/clang/test/Driver/modules.mm
@@ -3,6 +3,9 @@
// RUN: %clang -fmodules -fno-cxx-modules -### %s 2>&1 | FileCheck -check-prefix=CHECK-NO-MODULES %s
// CHECK-NO-MODULES-NOT: -fmodules
+// RUN: %clang -std=c++20 -fno-cxx-modules -### %s 2>&1 | FileCheck -check-prefix=CHECK-NO-CPP-20-MODULES %s
+// CHECK-NO-CPP-20-MODULES: -fno-cxx-modules
+
// RUN: %clang -fmodules -### %s 2>&1 | FileCheck -check-prefix=CHECK-HAS-MODULES %s
// RUN: %clang -fmodules -fno-cxx-modules -fcxx-modules -### %s 2>&1 | FileCheck -check-prefix=CHECK-HAS-MODULES %s
// CHECK-HAS-MODULES: -fmodules
|
|
This PR would also resolve: #57432 , but I'm not sure if its still relevant for the affected client code |
|
The bad side of If we want, I think we can introduce a new language options for C++20 modules only. If it enabled, the C++20 modules are enabled. If it is disabled, the C++20 modules will be disabled. |
Thanks for offering this suggestion, but I'm not sure if that's necessary yet. If the lack of distinction actively causes adoption issues for either clang modules or c++20 modules, then I'd say yes, but I'm not aware of this. In an ideal world, I'd hope these two definitions of modules can be enabled & intermixed without issues, especially if header units end up being represented with modulemaps (or something like it). FWIW, I actually noticed the issue this PR fixes when trying to clean up some of the Darwin-toolchain compiler divergence around this area. |
Bigcheese
left a comment
There was a problem hiding this comment.
This looks good to me. I think having -fno-cxx-modules mean no modules (Clang or standard) when in C++ mode makes sense, and matches how we've been using it.
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.
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.
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)
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.
Newer versions of the macOS SDK, have introduced code like: ```cpp if defined(__has_feature) && __has_feature(modules) define USE_CLANG_TYPES 1 else define USE_CLANG_TYPES 0 endif if USE_CLANG_TYPES include <sys/_types/_ptrdiff_t.h> include <sys/_types/_size_t.h> include <sys/_types/_va_list.h> include <sys/_types/_wchar_t.h> endif ``` which is currently causing compile failures due to undeclared types, which manifest when C++ modules are enabled. Note that the usage of "modules" in LLVM is can be a bit ambiguous, see: llvm/llvm-project#55891. For now, explcitly disable cxx modules using `-fno-cxx-modules`. This resolves the include/compilation issues. Related discussion: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116827 llvm/llvm-project#150349 llvm/llvm-project#57432
Newer versions of the macOS SDK, have introduced code like: ```cpp if defined(__has_feature) && __has_feature(modules) define USE_CLANG_TYPES 1 else define USE_CLANG_TYPES 0 endif if USE_CLANG_TYPES include <sys/_types/_ptrdiff_t.h> include <sys/_types/_size_t.h> include <sys/_types/_va_list.h> include <sys/_types/_wchar_t.h> endif ``` which is currently causing compile failures due to undeclared types, which manifest when C++ modules are enabled. Note that the usage of "modules" in LLVM is can be a bit ambiguous, see: llvm/llvm-project#55891. For now, explcitly disable cxx modules using `-fno-cxx-modules`. This resolves the include/compilation issues. Related discussion: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116827 llvm/llvm-project#150349 llvm/llvm-project#57432
Newer versions of the macOS SDK, have introduced code like: ```cpp if defined(__has_feature) && __has_feature(modules) define USE_CLANG_TYPES 1 else define USE_CLANG_TYPES 0 endif if USE_CLANG_TYPES include <sys/_types/_ptrdiff_t.h> include <sys/_types/_size_t.h> include <sys/_types/_va_list.h> include <sys/_types/_wchar_t.h> endif ``` which is currently causing compile failures due to undeclared types, which manifest when C++ modules are enabled. Note that the usage of "modules" in LLVM is can be a bit ambiguous, see: llvm/llvm-project#55891. For now, explcitly disable cxx modules using `-fno-cxx-modules`. This resolves the include/compilation issues. Related discussion: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116827 llvm/llvm-project#150349 llvm/llvm-project#57432
Newer versions of the macOS SDK, have introduced code like: ```cpp if defined(__has_feature) && __has_feature(modules) define USE_CLANG_TYPES 1 else define USE_CLANG_TYPES 0 endif if USE_CLANG_TYPES include <sys/_types/_ptrdiff_t.h> include <sys/_types/_size_t.h> include <sys/_types/_va_list.h> include <sys/_types/_wchar_t.h> endif ``` which is currently causing compile failures due to undeclared types, which manifest when C++ modules are enabled. Note that the usage of "modules" in LLVM is can be a bit ambiguous, see: llvm/llvm-project#55891. For now, explcitly disable cxx modules using `-fno-cxx-modules`. This resolves the include/compilation issues. Related discussion: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116827 llvm/llvm-project#150349 llvm/llvm-project#57432
Newer versions of the macOS SDK, have introduced code like: ```cpp if defined(__has_feature) && __has_feature(modules) define USE_CLANG_TYPES 1 else define USE_CLANG_TYPES 0 endif if USE_CLANG_TYPES include <sys/_types/_ptrdiff_t.h> include <sys/_types/_size_t.h> include <sys/_types/_va_list.h> include <sys/_types/_wchar_t.h> endif ``` which is currently causing compile failures due to undeclared types, which manifest when C++ modules are enabled. Note that the usage of "modules" in LLVM is can be a bit ambiguous, see: llvm/llvm-project#55891. For now, explcitly disable cxx modules using `-fno-cxx-modules`. This resolves the include/compilation issues. Related discussion: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116827 llvm/llvm-project#150349 llvm/llvm-project#57432 Backport of bitcoin/bitcoin#34036.
The mentioned flag is already both a cc1 & driver flag; however, whether it is respected was tied to either:
Xclang)-fmodulesaccompanying itThis poses a consistency problem where
std=c++20enables 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.