Skip to content

Commit 72c29fa

Browse files
committed
[C++20] [Modules] [Driver] Emit unused argument warning if we use '-fmodule-output' with non-module input
We required the file name of an 'importable module unit' should end with .cppm (or .ccm, .cxxm, .c++m). But the driver can accept '-fmodule-output' for files with normal suffixes (e.g., .cpp). This is somewhat inconsistency. In this patch, we only claim the option `-fmodule-output` is used if the type of the input file is modules related. Then now the compiler will emit 'unused argument' warnings if the input file is not modules related.
1 parent 4dd103e commit 72c29fa

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,9 @@ Improvements to Clang's diagnostics
328328
- New ``-Wformat-signedness`` diagnostic that warn if the format string requires an
329329
unsigned argument and the argument is signed and vice versa.
330330

331+
- Clang now emits ``unused argument`` warning when the -fmodule-output flag is used
332+
with an input that is not of type c++-module.
333+
331334
Improvements to Clang's time-trace
332335
----------------------------------
333336

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4045,9 +4045,18 @@ static bool RenderModulesOptions(Compilation &C, const Driver &D,
40454045
// module fragment.
40464046
CmdArgs.push_back("-fskip-odr-check-in-gmf");
40474047

4048-
// Claim `-fmodule-output` and `-fmodule-output=` to avoid unused warnings.
4049-
Args.ClaimAllArgs(options::OPT_fmodule_output);
4050-
Args.ClaimAllArgs(options::OPT_fmodule_output_EQ);
4048+
// We need to include the case the input file is a module file here.
4049+
// Since the default compilation model for C++ module interface unit will
4050+
// create temporary module file and compile the temporary module file
4051+
// to get the object file. Then the `-fmodule-output` flag will be
4052+
// brought to the second compilation process. So we have to claim it for
4053+
// the case too.
4054+
if (Input.getType() == driver::types::TY_CXXModule ||
4055+
Input.getType() == driver::types::TY_PP_CXXModule ||
4056+
Input.getType() == driver::types::TY_ModuleFile) {
4057+
Args.ClaimAllArgs(options::OPT_fmodule_output);
4058+
Args.ClaimAllArgs(options::OPT_fmodule_output_EQ);
4059+
}
40514060

40524061
return HaveModules;
40534062
}

clang/test/Driver/module-output.cppm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
// RUN: %clang -std=c++20 %t/Hello.cppm -fmodule-output=%t/Hello.pcm -fmodule-output -c -fsyntax-only \
3434
// RUN: -### 2>&1 | FileCheck %t/Hello.cppm --check-prefix=CHECK-NOT-USED
3535

36+
// Test that we can emit a warning if the type of the input file is not a module interface unit.
37+
// RUN: %clang -std=c++20 %t/a.cpp -fmodule-output -c -o %t/a.o -### 2>&1 | FileCheck %t/a.cpp
38+
3639
//--- Hello.cppm
3740
export module Hello;
3841

@@ -55,3 +58,8 @@ export module AnotherModule;
5558
// CHECK: "-emit-obj" {{.*}}"-main-file-name" "Hello.cppm" {{.*}}"-o" "{{.*}}/Hello-{{.*}}.o" "-x" "pcm" "{{.*}}/Hello.pcm"
5659
// CHECK: "-emit-module-interface" {{.*}}"-main-file-name" "AnotherModule.cppm" {{.*}}"-o" "{{.*}}/AnotherModule.pcm" "-x" "c++" "{{.*}}/AnotherModule.cppm"
5760
// CHECK: "-emit-obj" {{.*}}"-main-file-name" "AnotherModule.cppm" {{.*}}"-o" "{{.*}}/AnotherModule-{{.*}}.o" "-x" "pcm" "{{.*}}/AnotherModule.pcm"
61+
62+
//--- a.cpp
63+
export module a;
64+
65+
// CHECK: warning: argument unused during compilation: '-fmodule-output'

0 commit comments

Comments
 (0)