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