Complete unreachable checking for switch statements.

This commit is contained in:
Kugel Fuhr
2025-07-26 09:03:27 +02:00
parent 70c1bd5e3c
commit 8f4a4040d6
7 changed files with 239 additions and 62 deletions

View File

@@ -17,7 +17,7 @@ static int f2(void)
}
/* Unreachable */
a = 2;
return a;
return a;
}
static int f3(void)

View File

@@ -44,7 +44,7 @@ static int f3(void)
case 2: return a+1;
default: return a+2;
}
/* Unreachable but no warning */
/* Unreachable */
return a;
}
@@ -58,11 +58,71 @@ static int f4(void)
default: return a+2;
} while (1);
}
/* Unreachable but no warning */
/* Unreachable */
return a;
}
static int f5(void)
{
do {
switch (a) {
case 1: return a;
case 2: ++a; continue;
default: return 1;
}
} while (0);
/* Unreachable */
return 2;
}
static int f6(void)
{
do {
L: switch (a) {
case 1: return a;
case 2: ++a; goto L;
default: return 1;
}
} while (0);
/* Unreachable but no warning because of "goto" */
return 2;
}
static int f7(void)
{
do {
switch (a) {
/* Unreachable */
a = 3;
case 1: return a;
case 2: ++a; continue;
default: return 1;
}
} while (0);
/* Unreachable but no warning because of weird switch */
return 2;
}
static void duff(char* to, char* from, unsigned count)
{
unsigned n = (count+7)/8;
switch (count%8) {
case 0: do { *to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while (--n>0);
}
}
int main(void)
{
return f1() + f2() + f3() + f4();
char x[11];
char y[11];
duff(x, y, 11);
return f1() + f2() + f3() + f4() + f5() + f6() + f7();
}

View File

@@ -1 +1,5 @@
flow-switch-01.c:7: Warning: Unreachable code
flow-switch-01.c:48: Warning: Unreachable code
flow-switch-01.c:62: Warning: Unreachable code
flow-switch-01.c:75: Warning: Unreachable code
flow-switch-01.c:96: Warning: Unreachable code

View File

@@ -5,8 +5,11 @@ goto.c:38: Warning: Unreachable code
goto.c:59: Warning: Unreachable code
goto.c:80: Warning: Unreachable code
goto.c:97: Warning: Variable 'a' is defined but never used
goto.c:100: Warning: Unreachable code
goto.c:117: Warning: Variable 'a' is defined but never used
goto.c:120: Warning: Unreachable code
goto.c:137: Warning: Variable 'a' is defined but never used
goto.c:140: Warning: Unreachable code
goto.c:159: Warning: Goto at line 23 to label l8 jumps into a block with initialization of an object that has automatic storage duration
goto.c:159: Warning: Goto at line 44 to label l8 jumps into a block with initialization of an object that has automatic storage duration
goto.c:159: Warning: Goto at line 65 to label l8 jumps into a block with initialization of an object that has automatic storage duration
@@ -42,6 +45,7 @@ goto.c:221: Warning: Goto at line 109 to label lb jumps into a block with initia
goto.c:221: Warning: Goto at line 129 to label lb jumps into a block with initialization of an object that has automatic storage duration
goto.c:221: Warning: Goto at line 149 to label lb jumps into a block with initialization of an object that has automatic storage duration
goto.c:221: Warning: Goto at line 170 to label lb jumps into a block with initialization of an object that has automatic storage duration
goto.c:223: Warning: Unreachable code
goto.c:231: Warning: Goto at line 231 to label la jumps into a block with initialization of an object that has automatic storage duration
goto.c:241: Warning: Goto at line 27 to label lc jumps into a block with initialization of an object that has automatic storage duration
goto.c:241: Warning: Goto at line 48 to label lc jumps into a block with initialization of an object that has automatic storage duration
@@ -50,6 +54,7 @@ goto.c:241: Warning: Goto at line 90 to label lc jumps into a block with initial
goto.c:241: Warning: Goto at line 110 to label lc jumps into a block with initialization of an object that has automatic storage duration
goto.c:241: Warning: Goto at line 130 to label lc jumps into a block with initialization of an object that has automatic storage duration
goto.c:241: Warning: Goto at line 150 to label lc jumps into a block with initialization of an object that has automatic storage duration
goto.c:243: Warning: Unreachable code
goto.c:250: Warning: Goto at line 250 to label l9 jumps into a block with initialization of an object that has automatic storage duration
goto.c:251: Warning: Goto at line 251 to label la jumps into a block with initialization of an object that has automatic storage duration
goto.c:252: Warning: Goto at line 252 to label lb jumps into a block with initialization of an object that has automatic storage duration
@@ -123,6 +128,7 @@ goto.c:325: Warning: Goto at line 217 to label lg jumps into a block with initia
goto.c:325: Warning: Goto at line 237 to label lg jumps into a block with initialization of an object that has automatic storage duration
goto.c:325: Warning: Goto at line 257 to label lg jumps into a block with initialization of an object that has automatic storage duration
goto.c:325: Warning: Goto at line 279 to label lg jumps into a block with initialization of an object that has automatic storage duration
goto.c:327: Warning: Unreachable code
goto.c:333: Warning: Goto at line 333 to label l8 jumps into a block with initialization of an object that has automatic storage duration
goto.c:334: Warning: Goto at line 334 to label l9 jumps into a block with initialization of an object that has automatic storage duration
goto.c:335: Warning: Goto at line 335 to label la jumps into a block with initialization of an object that has automatic storage duration
@@ -141,6 +147,7 @@ goto.c:345: Warning: Goto at line 197 to label lh jumps into a block with initia
goto.c:345: Warning: Goto at line 218 to label lh jumps into a block with initialization of an object that has automatic storage duration
goto.c:345: Warning: Goto at line 238 to label lh jumps into a block with initialization of an object that has automatic storage duration
goto.c:345: Warning: Goto at line 258 to label lh jumps into a block with initialization of an object that has automatic storage duration
goto.c:347: Warning: Unreachable code
goto.c:353: Warning: Goto at line 353 to label l8 jumps into a block with initialization of an object that has automatic storage duration
goto.c:354: Warning: Goto at line 354 to label l9 jumps into a block with initialization of an object that has automatic storage duration
goto.c:355: Warning: Goto at line 355 to label la jumps into a block with initialization of an object that has automatic storage duration
@@ -149,6 +156,7 @@ goto.c:357: Warning: Goto at line 357 to label lc jumps into a block with initia
goto.c:359: Warning: Goto at line 359 to label le jumps into a block with initialization of an object that has automatic storage duration
goto.c:360: Warning: Goto at line 360 to label lf jumps into a block with initialization of an object that has automatic storage duration
goto.c:361: Warning: Goto at line 361 to label lg jumps into a block with initialization of an object that has automatic storage duration
goto.c:367: Warning: Unreachable code
goto.c:373: Warning: Goto at line 373 to label l8 jumps into a block with initialization of an object that has automatic storage duration
goto.c:374: Warning: Goto at line 374 to label l9 jumps into a block with initialization of an object that has automatic storage duration
goto.c:375: Warning: Goto at line 375 to label la jumps into a block with initialization of an object that has automatic storage duration