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.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
** do not even output a warning. This fails of course for
** 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;
const Type* RType; /* Real type used for struct parameters */
const Type* ReturnType; /* Return type */
int StmtFlags; /* Flow control flags for the function compound */
/* Remember this function descriptor used for definition */
GetFuncDesc (Func->Type)->FuncDef = D;
@@ -627,10 +628,12 @@ void NewFunc (SymEntry* Func, FuncDesc* D)
CurrentFunc->TopLevelSP = StackPtr;
/* Now process statements in this block checking for unreachable code */
StatementBlock (0);
StmtFlags = StatementBlock (0);
/* 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,
** let it always return zero. Otherwise output a warning.
*/

View File

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

View File

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

View File

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