Move the warning about unreachable code out of the Test() function and into

the callers. This has the effect that the location for the warning is much
more precise than before.
This commit is contained in:
Kugel Fuhr
2025-07-17 15:41:17 +02:00
parent 93b94d314a
commit f13284d3f8
4 changed files with 33 additions and 33 deletions

View File

@@ -163,6 +163,11 @@ static int IfStatement (void)
Label1 = GetLocalLabel (); Label1 = GetLocalLabel ();
TestResult = TestInParens (Label1, 0); TestResult = TestInParens (Label1, 0);
/* Output a warning if the condition is always false */
if (TestResult == TESTEXPR_FALSE) {
UnreachableCodeWarning ();
}
/* Parse the if body */ /* Parse the if body */
StmtFlags = AnyStatement (0, 0); StmtFlags = AnyStatement (0, 0);

View File

@@ -53,7 +53,7 @@
unsigned Test (unsigned Label, int Invert) unsigned Test (unsigned Label, int Invert)
/* Evaluate a boolean test expression and jump depending on the result of /* Evaluate a boolean test expression and jump depending on the result of
** the test and on Invert. The function returns one of the TESTEXPR_xx codes ** the test and on Invert. The function returns one of the TESTEXPR_xx codes
** defined above. If the jump is always true, a warning is output. ** defined above.
*/ */
{ {
ExprDesc Expr; ExprDesc Expr;
@@ -74,10 +74,7 @@ unsigned Test (unsigned Label, int Invert)
Result = (Expr.IVal != 0) ? TESTEXPR_TRUE : TESTEXPR_FALSE; Result = (Expr.IVal != 0) ? TESTEXPR_TRUE : TESTEXPR_FALSE;
/* Constant rvalue */ /* Constant rvalue */
if (!Invert && Expr.IVal == 0) { if ((!Invert && Expr.IVal == 0) || (Invert && Expr.IVal != 0)) {
g_jump (Label);
UnreachableCodeWarning ();
} else if (Invert && Expr.IVal != 0) {
g_jump (Label); g_jump (Label);
} }
@@ -125,8 +122,7 @@ unsigned Test (unsigned Label, int Invert)
unsigned TestInParens (unsigned Label, int Invert) unsigned TestInParens (unsigned Label, int Invert)
/* Evaluate a boolean test expression in parenthesis and jump depending on /* Evaluate a boolean test expression in parenthesis and jump depending on
** the result of the test * and on Invert. The function returns one of the ** the result of the test * and on Invert. The function returns one of the
** TESTEXPR_xx codes defined above. If the jump is always true, a warning is ** TESTEXPR_xx codes defined above.
** output.
*/ */
{ {
unsigned Result; unsigned Result;

View File

@@ -59,14 +59,13 @@
unsigned Test (unsigned Label, int Invert); unsigned Test (unsigned Label, int Invert);
/* Evaluate a boolean test expression and jump depending on the result of /* Evaluate a boolean test expression and jump depending on the result of
** the test and on Invert. The function returns one of the TESTEXPR_xx codes ** the test and on Invert. The function returns one of the TESTEXPR_xx codes
** defined above. If the jump is always true, a warning is output. ** defined above.
*/ */
unsigned TestInParens (unsigned Label, int Invert); unsigned TestInParens (unsigned Label, int Invert);
/* Evaluate a boolean test expression in parenthesis and jump depending on /* Evaluate a boolean test expression in parenthesis and jump depending on
** the result of the test * and on Invert. The function returns one of the ** the result of the test * and on Invert. The function returns one of the
** TESTEXPR_xx codes defined above. If the jump is always true, a warning is ** TESTEXPR_xx codes defined above.
** output.
*/ */