1 1.2.2.2 cjep /* $NetBSD: expr_range.c,v 1.2.2.2 2021/05/31 22:15:24 cjep Exp $ */ 2 1.2.2.2 cjep # 3 "expr_range.c" 3 1.2.2.2 cjep 4 1.2.2.2 cjep /* 5 1.2.2.2 cjep * In a switch statement that has (expr & constant) as the controlling 6 1.2.2.2 cjep * expression, complain if one of the case branches is unreachable because 7 1.2.2.2 cjep * the case label does can never match the controlling expression. 8 1.2.2.2 cjep * 9 1.2.2.2 cjep * GCC 10 does not complain about the unreachable branch. It knows that the 10 1.2.2.2 cjep * branch is unreachable though since it doesn't generate any code for it. 11 1.2.2.2 cjep * GCC once had the option -Wunreachable-code, but that option was made a 12 1.2.2.2 cjep * no-op in 2011. 13 1.2.2.2 cjep * 14 1.2.2.2 cjep * Clang 10 does not complain about this either, and just like GCC it doesn't 15 1.2.2.2 cjep * generate any code for this branch. The code for tracking an expression's 16 1.2.2.2 cjep * possible values may be related to RangeConstraintManager, just guessing. 17 1.2.2.2 cjep */ 18 1.2.2.2 cjep 19 1.2.2.2 cjep /* lint1-extra-flags: -chap */ 20 1.2.2.2 cjep 21 1.2.2.2 cjep void println(const char *); 22 1.2.2.2 cjep 23 1.2.2.2 cjep void 24 1.2.2.2 cjep example(unsigned x) 25 1.2.2.2 cjep { 26 1.2.2.2 cjep switch (x & 6) { 27 1.2.2.2 cjep case 0: 28 1.2.2.2 cjep println("0 is reachable"); 29 1.2.2.2 cjep break; 30 1.2.2.2 cjep case 1: /* expect: statement not reached */ 31 1.2.2.2 cjep println("1 is not reachable"); 32 1.2.2.2 cjep break; 33 1.2.2.2 cjep case 2: 34 1.2.2.2 cjep println("2 is reachable"); 35 1.2.2.2 cjep break; 36 1.2.2.2 cjep case 6: 37 1.2.2.2 cjep println("6 is reachable"); 38 1.2.2.2 cjep break; 39 1.2.2.2 cjep case 7: /* expect: statement not reached */ 40 1.2.2.2 cjep println("7 is not reachable"); 41 1.2.2.2 cjep break; 42 1.2.2.2 cjep } 43 1.2.2.2 cjep } 44