[cfe-dev] False positive for -Wunreachable-code

Abramo Bagnara abramo.bagnara at bugseng.com
Tue Oct 30 02:34:36 PDT 2012


Il 28/10/2012 08:12, Abramo Bagnara ha scritto:
> $ cat p.c
> #include <stdio.h>
> 
> enum e { a, b = 4 } x = 3;
> 
> void g(int v) {
>   printf("%d\n", v);
> }
> 
> int main(int argc, char **argv) {
>   switch (x) {
>   case a:
>     g(0);
>     break;
>   case b:
>     g(1);
>     break;
>   default:
>     g(2);
>     break;
>   }
> }
> $ _clang -Wunreachable-code -Wcovered-switch-default -O2 p.c
> p.c:17:3: warning: default label in switch which covers all enumeration
> values
>       [-Wcovered-switch-default]
>   default:
>   ^
> p.c:18:7: warning: will never be executed [-Wunreachable-code]
>     g(2);
>       ^
> $ ./a.out
> 2
> 
> Of course -Wcovered-switch-default warning is a perfectly true positive.
> 
> My reading of the standard is that nothing prevent an enum to have a
> value different from listed enum constants if this value is compatible
> with enum range (and code generation seems to agree on that).

I've attached the patch for review.

The fixed testcase shows well why to hide warnings about undefined
behaviour in code actually generated is a bad thing.


-- 
Abramo Bagnara

BUGSENG srl - http://bugseng.com
mailto:abramo.bagnara at bugseng.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ReachableDefault.patch
Type: text/x-patch
Size: 1877 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20121030/2f3ca9d6/attachment.bin>


More information about the cfe-dev mailing list