Skip to content

Commit a728f21

Browse files
committed
[Driver] [C++20] [Modules] Fix --precompile with -fmodule-output
Close #159780
1 parent 4d7694a commit a728f21

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4051,15 +4051,24 @@ static bool RenderModulesOptions(Compilation &C, const Driver &D,
40514051
// module fragment.
40524052
CmdArgs.push_back("-fskip-odr-check-in-gmf");
40534053

4054-
if (!Args.hasArg(options::OPT_fno_modules_reduced_bmi) &&
4055-
(Input.getType() == driver::types::TY_CXXModule ||
4056-
Input.getType() == driver::types::TY_PP_CXXModule) &&
4057-
!Args.hasArg(options::OPT__precompile)) {
4058-
CmdArgs.push_back("-fmodules-reduced-bmi");
4054+
if (Input.getType() == driver::types::TY_CXXModule ||
4055+
Input.getType() == driver::types::TY_PP_CXXModule) {
4056+
if (!Args.hasArg(options::OPT_fno_modules_reduced_bmi))
4057+
CmdArgs.push_back("-fmodules-reduced-bmi");
40594058

40604059
if (Args.hasArg(options::OPT_fmodule_output_EQ))
40614060
Args.AddLastArg(CmdArgs, options::OPT_fmodule_output_EQ);
4062-
else
4061+
else if (!Args.hasArg(options::OPT__precompile) ||
4062+
Args.hasArg(options::OPT_fmodule_output))
4063+
// If --precompile is specified, we will always generate a module file if
4064+
// we're compiling an importable module unit. This is fine even if the
4065+
// compilation process won't reach the point of generating the module file
4066+
// (e.g., in the preprocessing mode), since the attached flag
4067+
// '-fmodule-output' is useless.
4068+
//
4069+
// But if '--precompile' is specified, it might be annoying to always
4070+
// generate the module file as '--precompile' will generate the module
4071+
// file anyway.
40634072
CmdArgs.push_back(Args.MakeArgString(
40644073
"-fmodule-output=" +
40654074
getCXX20NamedModuleOutputPath(Args, Input.getBaseInput())));

clang/test/Driver/modules.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// RUN: %clang -std=c++2a -x c++-module --precompile %t/foo.cpp -o %t/foo.pcm -v 2>&1 | FileCheck %s --check-prefix=CHECK-PRECOMPILE
88
// RUN: %clang -std=gnu++2a -x c++-module --precompile %t/foo.cpp -o %t/foo-gnu.pcm -v 2>&1 | FileCheck %s --check-prefix=CHECK-PRECOMPILE
99
//
10-
// CHECK-PRECOMPILE: -cc1 {{.*}} -emit-module-interface
10+
// CHECK-PRECOMPILE: -cc1 {{.*}} -emit-module-interface
1111
// CHECK-PRECOMPILE-SAME: -o {{.*}}.pcm
1212
// CHECK-PRECOMPILE-SAME: -x c++
1313
// CHECK-PRECOMPILE-SAME: foo.cpp
@@ -41,6 +41,12 @@
4141
// RUN: cp %t/foo.cpp %t/foo.cppm
4242
// RUN: %clang -std=c++2a --precompile %t/foo.cppm -o %t/foo.pcm -v 2>&1 | FileCheck %s --check-prefix=CHECK-PRECOMPILE
4343

44+
// RUN: %clang -std=c++20 -x c++-module %t/foo.cpp --precompile -o %t/foo.pcm -### 2>&1 | FileCheck %s --check-prefix=CHECK-PRECOMPILE-WO-MODULE-OUTPUT
45+
// CHECK-PRECOMPILE-WO-MODULE-OUTPUT-NOT: -fmodule-output=
46+
47+
// RUN: %clang -std=c++20 -x c++-module %t/foo.cpp --precompile -fmodule-output=%t/foo.reduced.pcm -o %t/foo.pcm -### 2>&1 | FileCheck %s --check-prefix=CHECK-PRECOMPILE-WT-MODULE-OUTPUT
48+
// CHECK-PRECOMPILE-WT-MODULE-OUTPUT: -fmodule-output=
49+
4450
//--- foo.cpp
4551
export module foo;
4652

0 commit comments

Comments
 (0)