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