Skip to content

Commit

Permalink
[Driver] Enable to use C++20 standalne by -fcxx-modules
Browse files Browse the repository at this point in the history
This patch allows user to use C++20 module by -fcxx-modules. Previously,
we could only use it under -std=c++20. Given that user could use C++20
coroutine standalonel by -fcoroutines-ts. It makes sense to offer an
option to use C++20 modules without enabling C++20.

Reviewed By: iains, MaskRay

Differential Revision: https://reviews.llvm.org/D120540
  • Loading branch information
ChuanqiXu9 committed May 30, 2022
1 parent ef256ed commit 99eca83
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clang/include/clang/Driver/Options.td
Expand Up @@ -1410,7 +1410,7 @@ defm async_exceptions: BoolFOption<"async-exceptions",
PosFlag<SetTrue, [CC1Option], "Enable EH Asynchronous exceptions">, NegFlag<SetFalse>>;
defm cxx_modules : BoolFOption<"cxx-modules",
LangOpts<"CPlusPlusModules">, Default<cpp20.KeyPath>,
NegFlag<SetFalse, [CC1Option], "Disable">, PosFlag<SetTrue, [], "Enable">,
NegFlag<SetFalse, [CC1Option], "Disable">, PosFlag<SetTrue, [CC1Option], "Enable">,
BothFlags<[NoXarchOption], " modules for C++">>,
ShouldParseIf<cplusplus.KeyPath>;
def fdebug_pass_arguments : Flag<["-"], "fdebug-pass-arguments">, Group<f_Group>;
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Driver/ToolChains/Clang.cpp
Expand Up @@ -3627,6 +3627,12 @@ static void RenderModulesOptions(Compilation &C, const Driver &D,
HaveModules = true;
}

if (Args.hasFlag(options::OPT_fcxx_modules, options::OPT_fno_cxx_modules,
false)) {
CmdArgs.push_back("-fcxx-modules");
HaveModules = true;
}

// -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,
Expand Down
11 changes: 11 additions & 0 deletions clang/test/Driver/modules.cpp
Expand Up @@ -73,3 +73,14 @@ import "foo.h";
// CHECK-HEADER-UNIT-USE: BAR;
FOO;
#endif

// Check the independent use of -fcxx-modules
//
// RUN: %clang++ -fcxx-modules -std=c++17 -### %s 2>&1 | FileCheck %s --check-prefix=CHECK-CXX17-MODULES
// CHECK-CXX17-MODULES: "-fcxx-modules"
// RUN: %clang++ -fcxx-modules -std=c++14 -### %s 2>&1 | FileCheck %s --check-prefix=CHECK-CXX14-MODULES
// CHECK-CXX14-MODULES: "-fcxx-modules"
// RUN: %clang++ -fcxx-modules -std=c++11 -### %s 2>&1 | FileCheck %s --check-prefix=CHECK-CXX11-MODULES
// CHECK-CXX11-MODULES: "-fcxx-modules"
// RUN: %clang++ -fcxx-modules -std=c++03 -### %s 2>&1 | FileCheck %s --check-prefix=CHECK-CXX03-MODULES
// CHECK-CXX03-MODULES: "-fcxx-modules"
7 changes: 7 additions & 0 deletions clang/test/Modules/cxx-modules.cppm
@@ -0,0 +1,7 @@
// This tests that we could use C++20 modules standalone.
// RUN: %clang -std=c++03 -fcxx-modules -fsyntax-only -Xclang -verify %s
// RUN: %clang -std=c++11 -fcxx-modules -fsyntax-only -Xclang -verify %s
// RUN: %clang -std=c++14 -fcxx-modules -fsyntax-only -Xclang -verify %s
// RUN: %clang -std=c++17 -fcxx-modules -fsyntax-only -Xclang -verify %s
// expected-no-diagnostics
export module M;

0 comments on commit 99eca83

Please sign in to comment.