From a544710cd43ba9f7729a613c58729f146e792a8e Mon Sep 17 00:00:00 2001 From: Chuanqi Xu Date: Mon, 30 May 2022 10:08:39 +0800 Subject: [PATCH] [Driver] Enable to use C++20 standalne by -fcxx-modules 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 --- clang-tools-extra/test/pp-trace/pp-trace-modules.cpp | 2 +- clang/include/clang/Driver/Options.td | 2 +- clang/lib/Driver/ToolChains/Clang.cpp | 6 ++++++ clang/test/Driver/modules.cpp | 11 +++++++++++ clang/test/Modules/cxx-modules.cppm | 7 +++++++ 5 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 clang/test/Modules/cxx-modules.cppm diff --git a/clang-tools-extra/test/pp-trace/pp-trace-modules.cpp b/clang-tools-extra/test/pp-trace/pp-trace-modules.cpp index 32d6c48..028c967 100644 --- a/clang-tools-extra/test/pp-trace/pp-trace-modules.cpp +++ b/clang-tools-extra/test/pp-trace/pp-trace-modules.cpp @@ -1,5 +1,5 @@ // RUN: rm -rf %t -// RUN: pp-trace -callbacks '*,-FileChanged,-MacroDefined' %s -- -x objective-c++ -undef -target x86_64 -std=c++11 -fmodules -fcxx-modules -fmodules-cache-path=%t -I%S -I%S/Input | FileCheck --strict-whitespace %s +// RUN: pp-trace -callbacks '*,-FileChanged,-MacroDefined' %s -- -x objective-c++ -undef -target x86_64 -std=c++11 -fmodules -fmodules-cache-path=%t -I%S -I%S/Input | FileCheck --strict-whitespace %s // CHECK: --- diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index e19baac..0343e48 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -1410,7 +1410,7 @@ defm async_exceptions: BoolFOption<"async-exceptions", PosFlag, NegFlag>; defm cxx_modules : BoolFOption<"cxx-modules", LangOpts<"CPlusPlusModules">, Default, - NegFlag, PosFlag, + NegFlag, PosFlag, BothFlags<[NoXarchOption], " modules for C++">>, ShouldParseIf; def fdebug_pass_arguments : Flag<["-"], "fdebug-pass-arguments">, Group; diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 936682e..5106c0e 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -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, diff --git a/clang/test/Driver/modules.cpp b/clang/test/Driver/modules.cpp index 87b6cc6..bac9f71 100644 --- a/clang/test/Driver/modules.cpp +++ b/clang/test/Driver/modules.cpp @@ -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" diff --git a/clang/test/Modules/cxx-modules.cppm b/clang/test/Modules/cxx-modules.cppm new file mode 100644 index 0000000..be17f85 --- /dev/null +++ b/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; -- 2.7.4