Merge lp:~hjd/widelands/more-warnings into lp:widelands

Proposed by Hans Joachim Desserud
Status: Merged
Merged at revision: 6523
Proposed branch: lp:~hjd/widelands/more-warnings
Merge into: lp:widelands
Diff against target: 144 lines (+28/-26)
6 files modified
CMakeLists.txt (+1/-1)
src/editor/editorinteractive.cc (+4/-3)
src/minizip/unzip.cc (+3/-0)
src/scripting/pluto.cc (+2/-0)
src/ui_basic/messagebox.cc (+14/-14)
src/ui_fsmenu/options.cc (+4/-8)
To merge this branch: bzr merge lp:~hjd/widelands/more-warnings
Reviewer Review Type Date Requested Status
SirVer Approve
Nicolai Hähnle Approve
Review via email: mp+148949@code.launchpad.net

Description of the change

Adding another batch of additional warnings. Mainly finds a couple of issues reported by -Wswitch-enum and -Wswitch-default, as well as a couple of others which doesn't seem to hurt.

As mentioned, this includes -Wswitch-enum which has been silenced in several places, since some places will trigger warnings on 200+ SDL_* enums not used. I hope this won't ignore any important warnings, though adding 200 different elements just to have them there doesn't seem ideal either.

Some notes:
The two methods touched in src/ui_fsmenu/options.cc look identical to me, but I haven't investigated thoroughly.
Had to add explicit brackets to a conditional in editorinteractive.cc, otherwise it would fail to compile claiming it found an else without an if.

To post a comment you must log in.
Revision history for this message
Nicolai Hähnle (nha) wrote :

Thank you for doing this. I am a bit worried about the discussion in the linked bug reports that compilers ignore enabled warnings if the encounter some warning option that they don't know about. Isn't there some way to make this more robust, in case the supported compiler options change at some point? Of course, this may be something for a separate merge and doesn't necessarily need to be addressed here.

The explicit brackets are a good thing.

I oppose -Wswitch-enum. The appropriate way to indicate our intention in those switch(SDL_keysym) cases is by adding a default: label, and that should therefore be sufficient to stop the compiler from complaining. According to the gcc documentation, -Wswitch-enum complains even when a default: label exists, and we should therefore use -Wswitch rather than -Wswitch-enum. Note that, according to the documentation, -Wswitch is part of -Wall.

Other than that, I like your changes.

review: Needs Fixing
Revision history for this message
SirVer (sirver) wrote :

@nicolai: If you have a suggestion of warning detection besides compiling something with the warning enabled (which is quite painful indeed with cmake), let it here.

I agree that we should only get a switch warning when both conditions are true

a) there is no default: label
b) not all cases of an enum are handled in the switch

if this is given by -Wswitch, we should not use switch-enum. hjd, could you double check this please? Maybe also what clang does.

Revision history for this message
Hans Joachim Desserud (hjd) wrote :
Download full text (3.7 KiB)

Nicolai: You are of course right, thanks for pointing that out. I didn't know it would include missing enums even when a default case is present, which sounds a tad bit overzealous even to me. I checked both the g++ man page and a few sample warnings reported and can confirm this is indeed how the option behaved. (I wasn't able to find any place documenting Clang's warning options in detail, but every source agree they want to be compatible with GCC, so I would assume they have the same behaviour). I've now backed it out again, though keeping the style changes I did.

SirVer: From g++ manpage on -Wswitch: "Warn whenever a "switch" statement has an index of enumerated type and lacks a "case" for one or more of the named codes of that enumeration. (The presence of a "default" label prevents this warning.)" which is essentially just rephrasing your conditions.

However, that makes me wonder about -Wswitch-default, which will always trigger if the switch lacks a default case, even if all enumerations are present. Of course, often you don't need a default case when you have defined behaviour for all the enumerations. So at the moment, I can't really see a valid use case it would warn about, which we wouldn't already see from -Wswitch. Do you think I should drop that as well?

As for the "ignore warnings", see also the comments for the other merge requests. The discussion/explanation of this is unfortunately scattered all over the place. :/

However, notice that they are divided into three main groups; basic, additional and gcc. The basic group consists of "-Wall -Wextra" which will be check and applied separately. This means that even if some of the additional warnings arn't supported, those two will always be included. On top of that you migth get a bunch of other additional warnings assuming your compiler supports it, and in the end another small set only supported by GCC. So the only thing you will normally lose out on with an older compiler is the additional and GCC-specific ones, as the minimal set consisting of "-Wall -Wextra" will be there. Naturally I would like to run as many as possible, but historically we haven't run the additional ones at all, so I don't consider it a major flaw. I hope this calms your worries.

Splitting them into sections like this might not be ideal and long-term it would of course be nice to check and apply the warnings individually, however I think it is sufficient for now. I haven't had time to get a good look at the cmake docs, so I don't know if it supports some basic looping to go through a list/array of warnings. Other approaches:
A) check and apply each warning manually. Discarded since it would generate a long list with mostly duplicated code checking different warnings, especially since I would like feedback on which warnings weren't supported/used.
B) introduce som hierarchy checking for compiler name/versions, adding more warnings for higher version numbers. First of all I will rather do feature detection than attempt to sniff for specific names and version numbers, because that strikes me as easier to keep track of. (This is probably because feature detection is the preferred way for web development...

Read more...

Revision history for this message
Nicolai Hähnle (nha) wrote :

Thank you for the change! The style changes are fine.

As for -Wswitch-default, I see what you mean. I have to admit that I'm somewhat torn on the matter. There are basically two arguments, in different directions:

1. Forcing a default: label seems ridiculous if you cover all cases of an enum. This is doubly true because it might actually mask a bug if the enum is later extended. On the other hand, those enums where all cases are treated explicitly most of the time are not really subject to change.

2. Forcing a default: label is an exercise in defensive programming in the sense that it might happen that an enum variable is assigned a value that does not actually appear in the enum. This is of course bad style, but forcing default: labels, and making it a practice to guard them with assertions or even exceptions might flush out such bugs in a more obvious way.

Right now, I lean slightly towards keeping the -Wswitch-default, but it's only a slight preference. In any case, I'm happy with the changes.

review: Approve
Revision history for this message
SirVer (sirver) wrote :

lgtm as is. Thanks for working on this!

review: Approve
Revision history for this message
Hans Joachim Desserud (hjd) wrote :

Nicolai: Yes, I've tried to weigh the arguments against each other too. I'll keep it in for now, but if we see a lot of false postives in the future, we can of course reconsider and potentially silence it again.

Thanks both, for looking over the changes. Any particular reason why this hasn't been pushed to trunk now that it is approved. Is it implied that I can merge and push it myself? :)

Revision history for this message
SirVer (sirver) wrote :

I am quite pressed on time currently, that's why I didn't merge this. And of course - you can do it yourself: two approvals are plenty - besides, the review process should not limit in any way, so just merge when you feel that your work is ready. With review, the code will just be better in general imho.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'CMakeLists.txt'
2--- CMakeLists.txt 2013-01-27 14:13:03 +0000
3+++ CMakeLists.txt 2013-02-21 19:55:42 +0000
4@@ -158,7 +158,7 @@
5 set (PARAMETER_COMPILERFLAG_OLDSTYLECAST_EXTENDED "-Wold-style-cast -isystem ${Boost_INCLUDE_DIR}")
6 set (PARAMETER_COMPILERFLAG_OLDSTYLECAST "-Wold-style-cast")
7 set (PARAMETER_COMPILERFLAG_GENERICWARNINGS "-Wall -Wextra")
8-set (PARAMETER_COMPILERFLAG_EXTRAWARNINGS "-Wshadow -Wsign-promo -Wundef -Wunused -Wformat -Wformat-nonliteral -Wformat-security")
9+set (PARAMETER_COMPILERFLAG_EXTRAWARNINGS "-Wformat -Wformat-nonliteral -Wformat-security -Wformat-y2k -Winit-self -Winvalid-pch -Wmissing-include-dirs -Woverlength-strings -Wpacked -Wpointer-arith -Wredundant-decls -Wshadow -Wsign-promo -Wswitch-default -Wundef -Wunused -Wunused-macros")
10 set (PARAMETER_COMPILERFLAG_GCCWARNINGS "-Wlogical-op -Wsync-nand -Wtrampolines")
11 set (PARAMETER_COMPILERFLAG_STRICT "-Werror -Wno-error=old-style-cast -Wno-error=deprecated-declarations -fdiagnostics-show-option")
12 IF (CMAKE_BUILD_TYPE STREQUAL "Debug")
13
14=== modified file 'src/editor/editorinteractive.cc'
15--- src/editor/editorinteractive.cc 2013-02-10 20:07:27 +0000
16+++ src/editor/editorinteractive.cc 2013-02-21 19:55:42 +0000
17@@ -333,8 +333,9 @@
18 bool Editor_Interactive::handle_key(bool const down, SDL_keysym const code) {
19 bool handled = Interactive_Base::handle_key(down, code);
20
21- if (down)
22+ if (down) {
23 // only on down events
24+
25 switch (code.sym) {
26 // Sel radius
27 case SDLK_1:
28@@ -463,7 +464,7 @@
29 break;
30
31 }
32- else
33+ } else {
34 // key up events
35 switch (code.sym) {
36 case SDLK_LSHIFT:
37@@ -478,7 +479,7 @@
38 default:
39 break;
40 }
41-
42+ }
43 return handled;
44 }
45
46
47=== modified file 'src/minizip/unzip.cc'
48--- src/minizip/unzip.cc 2013-01-06 11:02:56 +0000
49+++ src/minizip/unzip.cc 2013-02-21 19:55:42 +0000
50@@ -1,6 +1,9 @@
51 /* Don't want to modify the minizip/zlib sources, so lets silence the warnings */
52 #include "compile_diagnostics.h"
53 GCC_DIAG_OFF("-Wold-style-cast")
54+GCC_DIAG_OFF("-Wswitch-default")
55+GCC_DIAG_OFF("-Wunused-macros")
56+CLANG_DIAG_OFF("-Wunused-macros")
57
58 /*
59 ================================================================================
60
61=== modified file 'src/scripting/pluto.cc'
62--- src/scripting/pluto.cc 2013-02-10 17:39:24 +0000
63+++ src/scripting/pluto.cc 2013-02-21 19:55:42 +0000
64@@ -30,7 +30,9 @@
65 // Widelands: silence warnings about unused variables, usually because they
66 //are only used in conditional asserts
67 #include "compile_diagnostics.h"
68+GCC_DIAG_OFF("-Wunused-macros")
69 GCC_DIAG_OFF("-Wunused-variable")
70+CLANG_DIAG_OFF("-Wunused-macros")
71 CLANG_DIAG_OFF("-Wunused-variable")
72
73 // Forward declarated from lua_impl.h. So we do not need to include it
74
75=== modified file 'src/ui_basic/messagebox.cc'
76--- src/ui_basic/messagebox.cc 2013-02-09 23:36:30 +0000
77+++ src/ui_basic/messagebox.cc 2013-02-21 19:55:42 +0000
78@@ -143,22 +143,22 @@
79
80 bool WLMessageBox::handle_key(bool down, SDL_keysym code)
81 {
82- if (!down)
83+ if (!down) {
84 return false;
85+ }
86
87- switch (code.sym)
88- {
89- case SDLK_KP_ENTER:
90- case SDLK_RETURN:
91- pressedYes();
92- pressedOk();
93- return true;
94- case SDLK_ESCAPE:
95- pressedNo();
96- pressedOk();
97- return true;
98- default:
99- return false;
100+ switch (code.sym) {
101+ case SDLK_KP_ENTER:
102+ case SDLK_RETURN:
103+ pressedYes();
104+ pressedOk();
105+ return true;
106+ case SDLK_ESCAPE:
107+ pressedNo();
108+ pressedOk();
109+ return true;
110+ default:
111+ return false;
112 }
113
114 return UI::Panel::handle_key(down, code);
115
116=== modified file 'src/ui_fsmenu/options.cc'
117--- src/ui_fsmenu/options.cc 2013-02-09 23:36:30 +0000
118+++ src/ui_fsmenu/options.cc 2013-02-21 19:55:42 +0000
119@@ -355,10 +355,8 @@
120
121 bool Fullscreen_Menu_Options::handle_key(bool down, SDL_keysym code)
122 {
123- if (down)
124- {
125- switch (code.sym)
126- {
127+ if (down) {
128+ switch (code.sym) {
129 case SDLK_KP_ENTER:
130 case SDLK_RETURN:
131 end_modal(static_cast<int32_t>(om_ok));
132@@ -593,10 +591,8 @@
133
134 bool Fullscreen_Menu_Advanced_Options::handle_key(bool down, SDL_keysym code)
135 {
136- if (down)
137- {
138- switch (code.sym)
139- {
140+ if (down) {
141+ switch (code.sym) {
142 case SDLK_KP_ENTER:
143 case SDLK_RETURN:
144 end_modal(static_cast<int32_t>(om_ok));

Subscribers

People subscribed via source and target branches

to status/vote changes: