<br><br><div class="gmail_quote">On Sun, Oct 28, 2012 at 12:08 PM, Abramo Bagnara <span dir="ltr"><<a href="mailto:abramo.bagnara@bugseng.com" target="_blank">abramo.bagnara@bugseng.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Il 28/10/2012 11:47, Matthieu Monrocq ha scritto:<br>
><br>
><br>
> On Sun, Oct 28, 2012 at 8:12 AM, Abramo Bagnara<br>
> <<a href="mailto:abramo.bagnara@bugseng.com">abramo.bagnara@bugseng.com</a> <mailto:<a href="mailto:abramo.bagnara@bugseng.com">abramo.bagnara@bugseng.com</a>>> wrote:<br>
><br>
>     $ cat p.c<br>
>     #include <stdio.h><br>
><br>
>     enum e { a, b = 4 } x = 3;<br>
><br>
>     void g(int v) {<br>
>       printf("%d\n", v);<br>
>     }<br>
><br>
>     int main(int argc, char **argv) {<br>
>       switch (x) {<br>
>       case a:<br>
>         g(0);<br>
>         break;<br>
>       case b:<br>
>         g(1);<br>
>         break;<br>
>       default:<br>
>         g(2);<br>
>         break;<br>
>       }<br>
>     }<br>
>     $ _clang -Wunreachable-code -Wcovered-switch-default -O2 p.c<br>
>     p.c:17:3: warning: default label in switch which covers all enumeration<br>
>     values<br>
>           [-Wcovered-switch-default]<br>
>       default:<br>
>       ^<br>
>     p.c:18:7: warning: will never be executed [-Wunreachable-code]<br>
>         g(2);<br>
>           ^<br>
>     $ ./a.out<br>
>     2<br>
><br>
>     Of course -Wcovered-switch-default warning is a perfectly true positive.<br>
><br>
>     My reading of the standard is that nothing prevent an enum to have a<br>
>     value different from listed enum constants if this value is compatible<br>
>     with enum range (and code generation seems to agree on that).<br>
><br>
>     Are there any objection to file a bug report?<br>
><br>
><br>
> Well, I would object on the basis that for all "regular" uses of the<br>
> switch/enum combination: ie, the enum values are only the enumerators<br>
> and the switch does cover all enumerators; then this warning is<br>
> perfectly valid.<br>
><br>
> Therefore I would rather say that people who use enums for bit-masks and<br>
> thus have variables of the enum type with values other than the<br>
> enumerators (which is fine) should either not be using a switch on this<br>
> enum OR simply disable this warning. Maybe casting `x` to `int` such as<br>
> `switch(int(x))` would also work.<br>
><br>
> How do you propose to distinguish the two widely different usages of enum ?<br>
><br>
> I personally would be rather disappointed to see<br>
> -Wcovered-switch-default disappear since at both the company I work on<br>
> and in my pet programs we only ever use enum for its enumerators and<br>
> thus this warning is very useful.<br>
<br>
There is a misunderstanding here:<br>
<br>
- I think that -Wcovered-switch-default is very useful and the warning<br>
show above is a *TRUE* positive, so there is nothing to fix there<br>
<br>
- my point is that -Wunreachable-code is showing a false negative for<br>
the example above and should be fixed<br>
<span class="HOEnZb"><font color="#888888"><br></font></span></blockquote><div> </div><div>I am afraid I did not expressed myself clearly either: if we accept that the switch is perfectly covered (and thus warn that the default is not needed), then it falls out that the code in the default branch is unreachable.<br>
</div></div><br>I am fine to remove the `-Wunreachable` warning in the case were the `-Wcovered-switch-default` one has already been emitted, they are redundant.<br><br>The real question is: when Clang does not warn about the switch already being covered, should it warn about the code being unreachable ?<br>
<br>What do you think of a note suggesting a cast to the underlying integral type of `x` such as `switch(int(x))` added to `-Wunreachable` to show how to silence it ? (and making it so that it does silence it)<br><br>-- Matthieu<br>