warning/error issues

Hello,

I began to experiment with clang to build an existing C++ (Qt-based) project. I wanted to make warnings into errors in order to ensure that the code is solid. However, there are several issues:

1) Clang is said to be designed to have the same command-line interface as gcc, but the options for warnings and errors are different. The gcc command -Wall is -Werror in clang, and so on.

2) The user documentation states that specific warnings can be made into errors or not errors by using the -Werror=foo and -Wno-error=foo options. However, no list of accepted values for foo is given.

3) Finally, gcc/clang both warn on unused variables and parameters. This often undesired, especially in C++ where, due to polymorphism some implementations of a method may not need certain parameters. When clang produces an error for unused parameters (-Werror enabled), it reports the reason is -Wunused-parameter. However, introducing -Wno-error=unused-parameter has no effect, despite documentation to the contrary. The gcc-style -Wno-unused-parameter is equally ineffective. So too is replacing unused-parameter with simply unused, which, at least according to gcc documentation, subsumes unused variables and parameters alike.

Whether these observations represent choice or oversight, I would like to know how to make all warnings errors except for unused variables and parameters.

Thank you.

Sincerely,
Eric Levy

Hello,

I began to experiment with clang to build an existing C++ (Qt-based)
project. I wanted to make warnings into errors in order to ensure that the
code is solid. However, there are several issues:

1) Clang is said to be designed to have the same command-line interface as
gcc, but the options for warnings and errors are different. The gcc
command -Wall is -Werror in clang, and so on.

Actually Clang and GCC both have -Wall and -Werror and they do (roughly)
the same thing in each. -Wall is the "common likely-good warnings" and
-Werror is "whatever warnings are on, make them errors". The specific set
of things in -Wall differs somewhat between Clang and GCC since there's no
specific definition, but roughly speaking those flags do the same thing.

2) The user documentation states that specific warnings can be made into
errors or not errors by using the -Werror=foo and -Wno-error=foo options.
However, no list of accepted values for foo is given.

This is a flaw in Clang's documentation - we basically don't document our
flags, so far as I know. You guess them, or you look at the implementation,
or try -Weverything (turns on all warnings) and see what warnings fire and
what their flags are called (we print the flag name by default).

3) Finally, gcc/clang both warn on unused variables and parameters. This
often undesired, especially in C++ where, due to polymorphism some
implementations of a method may not need certain parameters.

I don't believe Clang warns on unused parameters by default, if at all. If
it does so on virtual functions that's probably a bug to be fixed.

When clang produces an error for unused parameters (-Werror enabled), it
reports the reason is -Wunused-parameter. However, introducing
-Wno-error=unused-parameter has no effect, despite documentation to the
contrary. The gcc-style -Wno-unused-parameter is equally ineffective.

Could you show examples of this?

-Wunused-parameter isn't under -Wall, so you must've passed it explicitly I
assume (or perhaps via -Wextra)?

All these options seem to work quite well for me with Clang:

$ clang++-tot unused.cpp -Wextra
unused.cpp:2:17: warning: unused parameter 'i' [-Wunused-parameter]
  void func(int i) {
                ^
1 warning generated.
$ clang++-tot unused.cpp -Wextra -Werror
unused.cpp:2:17: error: unused parameter 'i' [-Werror,-Wunused-parameter]
  void func(int i) {
                ^
1 error generated.
$ clang++-tot unused.cpp -Wextra -Werror -Wno-error=unused-parameter
unused.cpp:2:17: warning: unused parameter 'i' [-Wunused-parameter]
  void func(int i) {
                ^
1 warning generated.
$ clang++-tot unused.cpp -Wextra -Werror -Wno-unused-parameter
$

I stand corrected on the -Werror/-Wall issue.

With respect to unused parameters, I do have -Wall enabled, however, the
documentation states I can override it on particular types of warnings
using -Wno-error=foo (this part is definitely different from gcc), however,
I have been unsuccessful in doing so.

I demonstrated that in my example:

$ clang++-tot unused.cpp -Wextra -Werror -Wno-error=unused-parameter
unused.cpp:2:17: warning: unused parameter 'i' [-Wunused-parameter]
  void func(int i) {
                ^
1 warning generated.

demoting the unused-parameter warning back from an error to a warning,
worked for me. You do need to use the exact warning name, not a warning
group (eg: -Wno-error=unused or -Wno-error=all won't work).

This is just a matter of ordering:

$ clang+±tot unused.cpp -Werror -Wno-error=unused-parameter -Wunused-parameter
unused.cpp:2:17: error: unused parameter ‘i’ [-Werror,-Wunused-parameter]
void func(int i) {
^
1 error generated.

$ clang+±tot unused.cpp -Wextra -Werror -Wno-error=unused-parameter
unused.cpp:2:17: warning: unused parameter ‘i’ [-Wunused-parameter]
void func(int i) {
^
1 warning generated.

Which differs from GCC, which warns in both cases. This is arguably (not sure if the Clang behavior is intentional) a bug in Clang. Feel free to file it at llvm.org/bugs - in the mean time you can workaround it by putting the -Werror related flags last.