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:[email protected]
Index: lib/Analysis/CFG.cpp
===================================================================
--- lib/Analysis/CFG.cpp	(revisione 167008)
+++ lib/Analysis/CFG.cpp	(copia locale)
@@ -2605,8 +2605,7 @@
   // following the switch body.  Moreover, take into account if all the
   // cases of a switch are covered (e.g., switching on an enum value).
   addSuccessor(SwitchTerminatedBlock,
-               switchExclusivelyCovered || Terminator->isAllEnumCasesCovered()
-               ? 0 : DefaultCaseBlock);
+               switchExclusivelyCovered ? 0 : DefaultCaseBlock);
 
   // Add the terminator and condition in the switch block.
   SwitchTerminatedBlock->setTerminator(Terminator);
Index: test/SemaCXX/array-bounds.cpp
===================================================================
--- test/SemaCXX/array-bounds.cpp	(revisione 167008)
+++ test/SemaCXX/array-bounds.cpp	(copia locale)
@@ -171,17 +171,17 @@
 }
 
 // Test that if all the values of an enum covered, that the 'default' branch
-// is unreachable.
+// is still reachable.
 enum Values { A, B, C, D };
 void test_all_enums_covered(enum Values v) {
-  int x[2];
+  int x[2]; // expected-note {{declared here}}
   switch (v) {
   case A: return;
   case B: return;
   case C: return;
   case D: return;
   }
-  x[2] = 0; // no-warning
+  x[2] = 0; // expected-warning {{array index 2 is past the end of the array}}
 }
 
 namespace tailpad {
Index: test/Sema/warn-unreachable.c
===================================================================
--- test/Sema/warn-unreachable.c	(revisione 167008)
+++ test/Sema/warn-unreachable.c	(copia locale)
@@ -107,11 +107,11 @@
     case C3:
       return 1;
     default: {
-      int i = 0; // expected-warning{{will never be executed}}
+      int i = 0;
       ++i;
       return i;
     }
-  }  
+  }
 }
 
 // Handle unreachable code triggered by macro expansions.
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to