Merge pull request #2808 from kugelfuhr/kugelfuhr/flowanalysis

Simple flow analysis to find unreachable code
This commit is contained in:
Bob Andrews
2025-07-26 00:10:21 +02:00
committed by GitHub
28 changed files with 1051 additions and 152 deletions

View File

@@ -152,6 +152,36 @@ $(WORKDIR)/bug2655.$1.$2.prg: bug2655.c $(ISEQUAL) | $(WORKDIR)
$(CC65) -t sim$2 -$1 -o $$@ $$< $(NULLOUT) 2>$(WORKDIR)/bug2655.$1.$2.out
$(ISEQUAL) $(WORKDIR)/bug2655.$1.$2.out bug2655.ref
$(WORKDIR)/flow-do-01.$1.$2.prg: flow-do-01.c $(ISEQUAL) | $(WORKDIR)
$(if $(QUIET),echo misc/flow-do-01.$1.$2.prg)
$(CC65) -t sim$2 -$1 -o $$@ $$< $(NULLOUT) 2>$(WORKDIR)/flow-do-01.$1.$2.out
$(ISEQUAL) $(WORKDIR)/flow-do-01.$1.$2.out flow-do-01.ref
$(WORKDIR)/flow-if-01.$1.$2.prg: flow-if-01.c $(ISEQUAL) | $(WORKDIR)
$(if $(QUIET),echo misc/flow-if-01.$1.$2.prg)
$(CC65) -t sim$2 -$1 -o $$@ $$< $(NULLOUT) 2>$(WORKDIR)/flow-if-01.$1.$2.out
$(ISEQUAL) $(WORKDIR)/flow-if-01.$1.$2.out flow-if-01.ref
$(WORKDIR)/flow-if-02.$1.$2.prg: flow-if-02.c $(ISEQUAL) | $(WORKDIR)
$(if $(QUIET),echo misc/flow-if-02.$1.$2.prg)
$(CC65) -t sim$2 -$1 -o $$@ $$< $(NULLOUT) 2>$(WORKDIR)/flow-if-02.$1.$2.out
$(ISEQUAL) $(WORKDIR)/flow-if-02.$1.$2.out flow-if-02.ref
$(WORKDIR)/flow-switch-01.$1.$2.prg: flow-switch-01.c $(ISEQUAL) | $(WORKDIR)
$(if $(QUIET),echo misc/flow-switch-01.$1.$2.prg)
$(CC65) -t sim$2 -$1 -o $$@ $$< $(NULLOUT) 2>$(WORKDIR)/flow-switch-01.$1.$2.out
$(ISEQUAL) $(WORKDIR)/flow-switch-01.$1.$2.out flow-switch-01.ref
$(WORKDIR)/flow-while-01.$1.$2.prg: flow-while-01.c $(ISEQUAL) | $(WORKDIR)
$(if $(QUIET),echo misc/flow-while-01.$1.$2.prg)
$(CC65) -t sim$2 -$1 -o $$@ $$< $(NULLOUT) 2>$(WORKDIR)/flow-while-01.$1.$2.out
$(ISEQUAL) $(WORKDIR)/flow-while-01.$1.$2.out flow-while-01.ref
$(WORKDIR)/flow-while-02.$1.$2.prg: flow-while-02.c $(ISEQUAL) | $(WORKDIR)
$(if $(QUIET),echo misc/flow-while-02.$1.$2.prg)
$(CC65) -t sim$2 -$1 -o $$@ $$< $(NULLOUT) 2>$(WORKDIR)/flow-while-02.$1.$2.out
$(ISEQUAL) $(WORKDIR)/flow-while-02.$1.$2.out flow-while-02.ref
$(WORKDIR)/limits.$1.$2.prg: limits.c $(ISEQUAL) | $(WORKDIR)
$(if $(QUIET),echo misc/limits.$1.$2.prg)
$(CC65) -t sim$2 -$1 -o $$(@:.prg=.s) $$< $(NULLOUT) $(CATERR)

View File

@@ -1,2 +1,2 @@
bug2655.c:5: Warning: Unreachable code
bug2655.c:12: Warning: Unreachable code
bug2655.c:6: Warning: Unreachable code
bug2655.c:13: Warning: Unreachable code

87
test/misc/flow-do-01.c Normal file
View File

@@ -0,0 +1,87 @@
int a;
static int f1(void)
{
do {
return a;
} while (a == 2);
/* Unreachable */
a = 2;
return a;
}
static int f2(void)
{
do {
return a;
} while (1);
/* Unreachable */
a = 2;
return a;
}
static int f3(void)
{
do {
return a;
} while (0);
/* Unreachable */
a = 2;
return a;
}
static int f4(void)
{
do {
continue; /* Turns do/while into an endless loop */
} while (0);
/* Unreachable */
a = 2;
return a;
}
static int f5(void)
{
do {
if (a == 2) {
break;
}
return a;
} while (0);
/* Reachable */
a = 2;
return a;
}
static int f6(void)
{
do {
if (a == 2) {
break;
}
continue;
} while (0);
/* Reachable */
a = 2;
return a;
}
static int f7(void)
{
do {
if (a == 2) {
return a;
} else {
continue;
}
} while (1);
/* Unreachable */
a = 2;
return a;
}
int main(void)
{
return f1() + f2() + f3() + f4() + f5() + f6() + f7();
}

5
test/misc/flow-do-01.ref Normal file
View File

@@ -0,0 +1,5 @@
flow-do-01.c:9: Warning: Unreachable code
flow-do-01.c:19: Warning: Unreachable code
flow-do-01.c:29: Warning: Unreachable code
flow-do-01.c:39: Warning: Unreachable code
flow-do-01.c:79: Warning: Unreachable code

62
test/misc/flow-if-01.c Normal file
View File

@@ -0,0 +1,62 @@
int a, b;
static int f1(void)
{
if (a == 1) {
return 1;
}
/* Reachable */
a = 2;
return a;
}
static int f2(void)
{
if (a == 1) {
a = 2;
} else {
return 1;
}
/* Reachable */
return a;
}
static int f3(void)
{
if (a == 1) {
return 1;
} else {
a = 2;
}
/* Reachable */
return a;
}
static int f4(void)
{
if (a == 1) {
return 1;
} else {
return 0;
}
/* Unreachable */
a = 2;
}
static int f5(void)
{
if (1) {
return 1;
} else {
/* Unreachable */
return 0;
}
/* Unreachable */
a = 2;
}
int main(void)
{
return f1() + f2() + f3() + f4() + f5();
}

3
test/misc/flow-if-01.ref Normal file
View File

@@ -0,0 +1,3 @@
flow-if-01.c:43: Warning: Unreachable code
flow-if-01.c:52: Warning: Unreachable code
flow-if-01.c:55: Warning: Unreachable code

49
test/misc/flow-if-02.c Normal file
View File

@@ -0,0 +1,49 @@
int a, b;
static int f1(void)
{
if (0) {
/* Unreachable but no warning */
} else {
a = 2;
}
return a;
}
static int f2(void)
{
if (0) {
/* Unreachable */
a = 1;
} else {
a = 2;
}
return a;
}
static int f3(void)
{
if (1) {
a = 2;
} else {
/* Unreachable but no warning */
}
return a;
}
static int f4(void)
{
if (1) {
a = 2;
} else {
/* Unreachable */
a = 1;
}
return a;
}
int main(void)
{
return f1() + f2() + f3() + f4();
}

2
test/misc/flow-if-02.ref Normal file
View File

@@ -0,0 +1,2 @@
flow-if-02.c:17: Warning: Unreachable code
flow-if-02.c:40: Warning: Unreachable code

View File

@@ -0,0 +1,68 @@
int a;
static int f1(void)
{
switch (a) {
/* Unreachable */
a = 3;
case 1:
a = 2;
break;
case 2:
a = 1;
break;
default:
a = 0;
break;
}
/* Reachable */
return a;
}
static int f2(void)
{
switch (a) {
/* Reachable */
L: a = 3;
case 1:
goto L;
case 2:
a = 1;
break;
default:
a = 0;
break;
}
/* Reachable */
return a;
}
static int f3(void)
{
switch (a) {
case 1: return a;
case 2: return a+1;
default: return a+2;
}
/* Unreachable but no warning */
return a;
}
static int f4(void)
{
switch (a) {
/* No warning */
do {
case 1: ++a; continue;
case 2: return a+1;
default: return a+2;
} while (1);
}
/* Unreachable but no warning */
return a;
}
int main(void)
{
return f1() + f2() + f3() + f4();
}

View File

@@ -0,0 +1 @@
flow-switch-01.c:7: Warning: Unreachable code

70
test/misc/flow-while-01.c Normal file
View File

@@ -0,0 +1,70 @@
int a;
static int f1(void)
{
while (1) {
++a;
}
/* Unreachable */
a = 2;
return a;
}
static int f2(void)
{
while (1) {
++a;
if (a == 5) break;
}
/* Reachable */
a = 2;
return a;
}
static int f3(void)
{
while (1) {
++a;
return a;
}
/* Unreachable */
a = 2;
}
static int f4(void)
{
while (0) {
/* Unreachable */
++a;
return a;
}
/* Reachable */
a = 2;
return 0;
}
static int f5(void)
{
while (1) {
++a;
if (a == 4) goto L;
return a;
}
/* Reachable via L */
L: a = 2;
}
static int f6(void)
{
while (0) {
/* Unreachable but no warning */
}
a = 2;
return a;
}
int main(void)
{
return f1() + f2() + f3() + f4() + f5() + f6();
}

View File

@@ -0,0 +1,3 @@
flow-while-01.c:9: Warning: Unreachable code
flow-while-01.c:31: Warning: Unreachable code
flow-while-01.c:38: Warning: Unreachable code

74
test/misc/flow-while-02.c Normal file
View File

@@ -0,0 +1,74 @@
int a;
static int f1(void)
{
while (1) {
while (0) {
/* Unreachable */
++a;
}
}
/* Unreachable */
a = 2;
return a;
}
static int f2(void)
{
while (1) {
do {
return a;
} while (0);
/* Unreachable */
break;
}
/* Unreachable but no warning */
a = 2;
return a;
}
static int f3(void)
{
do {
while (1) {
break;
}
} while (1);
/* Unreachable */
a = 2;
return a;
}
static int f4(void)
{
do {
while (1) {
return a;
}
} while (0);
/* Unreachable */
a = 2;
return a;
}
static int f5(void)
{
do {
do {
if (a == 2) {
return a;
} else {
continue;
}
} while (0);
} while (0);
/* Unreachable */
a = 2;
return a;
}
int main(void)
{
return f1()/ + f2() + f3() + f4() + f5();
}

View File

@@ -0,0 +1,6 @@
flow-while-02.c:8: Warning: Unreachable code
flow-while-02.c:12: Warning: Unreachable code
flow-while-02.c:23: Warning: Unreachable code
flow-while-02.c:38: Warning: Unreachable code
flow-while-02.c:50: Warning: Unreachable code
flow-while-02.c:66: Warning: Unreachable code

View File

@@ -1,4 +1,9 @@
goto.c:3: Warning: Unreachable code
goto.c:8: Warning: Goto at line 8 to label start jumps into a block with initialization of an object that has automatic storage duration
goto.c:17: Warning: Unreachable code
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:117: Warning: Variable 'a' is defined but never used
goto.c:137: Warning: Variable 'a' is defined but never used
@@ -9,6 +14,7 @@ goto.c:159: Warning: Goto at line 86 to label l8 jumps into a block with initial
goto.c:159: Warning: Goto at line 106 to label l8 jumps into a block with initialization of an object that has automatic storage duration
goto.c:159: Warning: Goto at line 126 to label l8 jumps into a block with initialization of an object that has automatic storage duration
goto.c:159: Warning: Goto at line 146 to label l8 jumps into a block with initialization of an object that has automatic storage duration
goto.c:161: Warning: Unreachable code
goto.c:180: Warning: Goto at line 24 to label l9 jumps into a block with initialization of an object that has automatic storage duration
goto.c:180: Warning: Goto at line 45 to label l9 jumps into a block with initialization of an object that has automatic storage duration
goto.c:180: Warning: Goto at line 66 to label l9 jumps into a block with initialization of an object that has automatic storage duration
@@ -17,6 +23,7 @@ goto.c:180: Warning: Goto at line 107 to label l9 jumps into a block with initia
goto.c:180: Warning: Goto at line 127 to label l9 jumps into a block with initialization of an object that has automatic storage duration
goto.c:180: Warning: Goto at line 147 to label l9 jumps into a block with initialization of an object that has automatic storage duration
goto.c:180: Warning: Goto at line 168 to label l9 jumps into a block with initialization of an object that has automatic storage duration
goto.c:182: Warning: Unreachable code
goto.c:201: Warning: Goto at line 25 to label la jumps into a block with initialization of an object that has automatic storage duration
goto.c:201: Warning: Goto at line 46 to label la jumps into a block with initialization of an object that has automatic storage duration
goto.c:201: Warning: Goto at line 67 to label la jumps into a block with initialization of an object that has automatic storage duration
@@ -26,6 +33,7 @@ goto.c:201: Warning: Goto at line 128 to label la jumps into a block with initia
goto.c:201: Warning: Goto at line 148 to label la jumps into a block with initialization of an object that has automatic storage duration
goto.c:201: Warning: Goto at line 169 to label la jumps into a block with initialization of an object that has automatic storage duration
goto.c:201: Warning: Goto at line 190 to label la jumps into a block with initialization of an object that has automatic storage duration
goto.c:203: Warning: Unreachable code
goto.c:221: Warning: Goto at line 26 to label lb jumps into a block with initialization of an object that has automatic storage duration
goto.c:221: Warning: Goto at line 47 to label lb jumps into a block with initialization of an object that has automatic storage duration
goto.c:221: Warning: Goto at line 68 to label lb jumps into a block with initialization of an object that has automatic storage duration
@@ -57,6 +65,7 @@ goto.c:263: Warning: Goto at line 193 to label ld jumps into a block with initia
goto.c:263: Warning: Goto at line 214 to label ld jumps into a block with initialization of an object that has automatic storage duration
goto.c:263: Warning: Goto at line 234 to label ld jumps into a block with initialization of an object that has automatic storage duration
goto.c:263: Warning: Goto at line 254 to label ld jumps into a block with initialization of an object that has automatic storage duration
goto.c:265: Warning: Unreachable code
goto.c:271: Warning: Goto at line 271 to label l8 jumps into a block with initialization of an object that has automatic storage duration
goto.c:272: Warning: Goto at line 272 to label l9 jumps into a block with initialization of an object that has automatic storage duration
goto.c:273: Warning: Goto at line 273 to label la jumps into a block with initialization of an object that has automatic storage duration
@@ -75,6 +84,7 @@ goto.c:284: Warning: Goto at line 215 to label le jumps into a block with initia
goto.c:284: Warning: Goto at line 235 to label le jumps into a block with initialization of an object that has automatic storage duration
goto.c:284: Warning: Goto at line 255 to label le jumps into a block with initialization of an object that has automatic storage duration
goto.c:284: Warning: Goto at line 277 to label le jumps into a block with initialization of an object that has automatic storage duration
goto.c:286: Warning: Unreachable code
goto.c:292: Warning: Goto at line 292 to label l8 jumps into a block with initialization of an object that has automatic storage duration
goto.c:293: Warning: Goto at line 293 to label l9 jumps into a block with initialization of an object that has automatic storage duration
goto.c:294: Warning: Goto at line 294 to label la jumps into a block with initialization of an object that has automatic storage duration
@@ -94,6 +104,7 @@ goto.c:305: Warning: Goto at line 236 to label lf jumps into a block with initia
goto.c:305: Warning: Goto at line 256 to label lf jumps into a block with initialization of an object that has automatic storage duration
goto.c:305: Warning: Goto at line 278 to label lf jumps into a block with initialization of an object that has automatic storage duration
goto.c:305: Warning: Goto at line 299 to label lf jumps into a block with initialization of an object that has automatic storage duration
goto.c:307: Warning: Unreachable code
goto.c:313: Warning: Goto at line 313 to label l8 jumps into a block with initialization of an object that has automatic storage duration
goto.c:314: Warning: Goto at line 314 to label l9 jumps into a block with initialization of an object that has automatic storage duration
goto.c:315: Warning: Goto at line 315 to label la jumps into a block with initialization of an object that has automatic storage duration