Do not output a warning about a missing "return" in a function if the function

exit is unreachable.
This commit is contained in:
Kugel Fuhr
2025-07-17 16:12:48 +02:00
parent ed54e9b168
commit 6d45a94127
6 changed files with 17 additions and 14 deletions

View File

@@ -433,7 +433,7 @@ void UnreachableCodeWarning (void)
*/ */
if (CurTok.LI && NextTok.LI) { if (CurTok.LI && NextTok.LI) {
if (CurTok.Tok == TOK_LCURLY) { if (CurTok.Tok == TOK_LCURLY) {
/* Do not point to the compoung statement but to the first /* Do not point to the compound statement but to the first
** statement within it. If the compound statement is empty ** statement within it. If the compound statement is empty
** do not even output a warning. This fails of course for ** do not even output a warning. This fails of course for
** nested compounds but will do the right thing in most cases. ** nested compounds but will do the right thing in most cases.

View File

@@ -453,6 +453,7 @@ void NewFunc (SymEntry* Func, FuncDesc* D)
SymEntry* Param; SymEntry* Param;
const Type* RType; /* Real type used for struct parameters */ const Type* RType; /* Real type used for struct parameters */
const Type* ReturnType; /* Return type */ const Type* ReturnType; /* Return type */
int StmtFlags; /* Flow control flags for the function compound */
/* Remember this function descriptor used for definition */ /* Remember this function descriptor used for definition */
GetFuncDesc (Func->Type)->FuncDef = D; GetFuncDesc (Func->Type)->FuncDef = D;
@@ -627,10 +628,12 @@ void NewFunc (SymEntry* Func, FuncDesc* D)
CurrentFunc->TopLevelSP = StackPtr; CurrentFunc->TopLevelSP = StackPtr;
/* Now process statements in this block checking for unreachable code */ /* Now process statements in this block checking for unreachable code */
StatementBlock (0); StmtFlags = StatementBlock (0);
/* Check if this function is missing a return value */ /* Check if this function is missing a return value */
if (!F_HasVoidReturn (CurrentFunc) && !F_HasReturn (CurrentFunc)) { if (!SF_Unreach (StmtFlags) &&
!F_HasVoidReturn (CurrentFunc) &&
!F_HasReturn (CurrentFunc)) {
/* If this is the main function in a C99 environment returning an int, /* If this is the main function in a C99 environment returning an int,
** let it always return zero. Otherwise output a warning. ** let it always return zero. Otherwise output a warning.
*/ */

View File

@@ -43,7 +43,7 @@ static int f4(void)
static int f5(void) static int f5(void)
{ {
do { do {
if (a == 2) { if (a == 2) {
break; break;
} }
return a; return a;
@@ -56,7 +56,7 @@ static int f5(void)
static int f6(void) static int f6(void)
{ {
do { do {
if (a == 2) { if (a == 2) {
break; break;
} }
continue; continue;
@@ -69,11 +69,11 @@ static int f6(void)
static int f7(void) static int f7(void)
{ {
do { do {
if (a == 2) { if (a == 2) {
return a; return a;
} else { } else {
continue; continue;
} }
} while (1); } while (1);
/* Unreachable */ /* Unreachable */
a = 2; a = 2;

View File

@@ -14,7 +14,7 @@ static int f2(void)
{ {
if (0) { if (0) {
/* Unreachable */ /* Unreachable */
a = 1; a = 1;
} else { } else {
a = 2; a = 2;
} }
@@ -37,7 +37,7 @@ static int f4(void)
a = 2; a = 2;
} else { } else {
/* Unreachable */ /* Unreachable */
a = 1; a = 1;
} }
return a; return a;
} }

View File

@@ -47,7 +47,7 @@ static int f5(void)
{ {
while (1) { while (1) {
++a; ++a;
if (a == 4) goto L; if (a == 4) goto L;
return a; return a;
} }
/* Reachable via L */ /* Reachable via L */
@@ -57,7 +57,7 @@ L: a = 2;
static int f6(void) static int f6(void)
{ {
while (0) { while (0) {
/* Unreachable but no warning */ /* Unreachable but no warning */
} }
a = 2; a = 2;
return a; return a;