xref: /netbsd-src/tests/usr.bin/xlint/lint1/expr_range.c (revision 6937eff333b197fb14840f0e58df0f7a0edfd51a)
1 /*	$NetBSD: expr_range.c,v 1.6 2024/11/13 04:32:49 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 -X 351 */
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:
31 		/* expect-1: warning: 'case' statement not reached [193] */
32 		println("1 is not reachable");
33 		break;
34 	case 2:
35 		println("2 is reachable");
36 		break;
37 	case 6:
38 		println("6 is reachable");
39 		break;
40 	case 7:
41 		/* expect-1: warning: 'case' statement not reached [193] */
42 		println("7 is not reachable");
43 		break;
44 	}
45 }
46