Home | History | Annotate | Line # | Download | only in lint1
expr_range.c revision 1.1
      1 /*	$NetBSD: expr_range.c,v 1.1 2021/05/14 21:14:55 rillig Exp $	*/
      2 # 3 "expr_range.c"
      3 
      4 /*
      5  * Test whether lint can detect switch branches that are impossible to reach.
      6  * As of 2021-05-14, it cannot.  To do this, it would need to keep track of
      7  * the possible values of each variable or expression.  To do this reliably,
      8  * it would also need accurate control flow analysis, which as of 2021-05-14
      9  * works only for functions without 'goto'.
     10  *
     11  * GCC 10 does not complain the unreachable branch.  It knows that the branch
     12  * is unreachable though since it doesn't generate any code for it.  GCC once
     13  * had the option -Wunreachable-code, but that option was made a no-op in
     14  * 2011.
     15  *
     16  * Clang 10 does not complain about this either, and just like GCC it doesn't
     17  * generate any code for this branch.  The code for tracking an expression's
     18  * possible values may be related to RangeConstraintManager, just guessing.
     19  *
     20  * There should be at least one static analysis tool that warns about this.
     21  */
     22 
     23 /* lint1-extra-flags: -chap */
     24 
     25 void
     26 example(unsigned x)
     27 {
     28 	switch (x & 6) {
     29 	case 1:
     30 		/* This branch is unreachable. */
     31 		printf("one\n");
     32 		break;
     33 	case 2:
     34 		printf("two\n");
     35 		break;
     36 	}
     37 }
     38