clangDriver is the library implementing the compiler driver forClang. It utilitized LLVMOption to process command line options. Asoptions are processed when required, as opposed to use a largeswitch, Clang gets the ability to detect unused options.
When an option is checked with APIs such as hasArg
andgetLastArg
, its "claimed" bit is set. 1
2
3
4
5
6
7
8
9template<typename ...OptSpecifiers>
Arg *getLastArg(OptSpecifiers ...Ids) const {
Arg *Res = nullptr;
for (Arg *A : filtered(Ids...)) {
Res = A;
Res->claim();
}
return Res;
}
After all options are processed, Clang reports a-Wunused-command-line-argument
diagnostic for eachunclaimed option. There are multiple possible messages, butargument unused during compilation
is the most common one.1
2
3
4
5
6
7
8% clang -c -ftime-trace a.s
clang: warning: argument unused during compilation: '-ftime-trace' [-Wunused-command-line-argument]
% clang -c a.c -la
clang: warning: -la: 'linker' input unused [-Wunused-command-line-argument]
% clang -c -ftime-trace -Werror a.s
clang: error: argument unused during compilation: '-ftime-trace' [-Werror,-Wunused-command-line-argument]
% clang -c -ftime-trace -Werror=unused-command-line-argument a.s
clang: error: argument unused during compilation: '-ftime-trace' [-Werror,-Wunused-command-line-argument]
There are many heuristics to enhance the desirability of-Wunused-command-line-argument
, which can be rathersubjective. For instance, options that are relevant only duringcompilation do not result in -Wunused-command-line-argument
diagnostics when linking is performed. 1
% clang -faddrsig -fpic -march=generic a.o
There is a tension between-Wunused-command-line-argument
and default options. Let'sconsider a scenario where we specify--rtlib=compiler-rt --unwindlib=libunwind
inCFLAGS
and CXXFLAGS
to utilize compiler-rt andLLVM libunwind. ClangDriver claims --rtlib
and--unwindlib
in the following code snippet:1
2
3
4
5
6
7
8if (!Args.hasArg(options::OPT_nostdlib, options::OPT_r)) {
if (!Args.hasArg(options::OPT_nodefaultlibs)) {
// Handle --rtlib and --unwindlib.
}
if (!Args.hasArg(options::OPT_nostartfiles)) {
// Handle --rtlib. Append clang_rt.crtend.o or GCC style crtend{,S,T}.o
}
}
However, if a build target employs -nostdlib
or-nodefaultlibs
, options such as --rtlib
,--unwindlib
, and many other linker options (e.g.-static-libstdc++
and -pthread
) will not beclaimed, resulting in unused argument diagnostics: 1
2
3
4
5% clang --rtlib=compiler-rt --unwindlib=libunwind -stdlib=libstdc++ -static-libstdc++ -pthread -nostdlib a.o
clang: warning: argument unused during compilation: '--rtlib=compiler-rt' [-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '--unwindlib=libunwind' [-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '-static-libstdc++' [-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '-pthread' [-Wunused-command-line-argument]
While some options like -stdlib=
do not trigger adiagnostic, this seems more like a happenstance rather than a deliberatedesign choice.
To suppress the diagnostics, we can utilitize --start-no-unused-arguments
and --end-no-unused-arguments
(Clang 14) like thefollowing: 1
% clang --start-no-unused-arguments --rtlib=compiler-rt --unwindlib=libunwind -stdlib=libstdc++ -static-libstdc++ -pthread --end-no-unused-arguments -nostdlib a.o
There is also a heavy hammer -Qunused-arguments
tosuppress -Wunused-command-line-argument
diagnosticsregardless of options' positions: 1
% clang -Qunused-arguments --rtlib=compiler-rt --unwindlib=libunwind -stdlib=libstdc++ -static-libstdc++ -pthread -nostdlib a.o
Conveniently, options specified in a Clang configuration file areautomatically claimed.