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

@@ -425,31 +425,31 @@ void UnreachableCodeWarning (void)
{
if (IS_Get (&WarnUnreachableCode)) {
LineInfo* LI;
LineInfo* LI;
/* Add special handling for compound statements if the current token
** is from the source. Doing this here is a bit hacky but unfortunately
** there's no better place.
*/
if (CurTok.LI && NextTok.LI) {
if (CurTok.Tok == TOK_LCURLY) {
/* Do not point to the compoung 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.
*/
if (NextTok.Tok == TOK_RCURLY) {
return;
}
LI = NextTok.LI;
} else {
LI = CurTok.LI;
}
} else {
LI = GetCurLineInfo ();
}
/* Add special handling for compound statements if the current token
** is from the source. Doing this here is a bit hacky but unfortunately
** there's no better place.
*/
if (CurTok.LI && NextTok.LI) {
if (CurTok.Tok == TOK_LCURLY) {
/* Do not point to the compoung 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.
*/
if (NextTok.Tok == TOK_RCURLY) {
return;
}
LI = NextTok.LI;
} else {
LI = CurTok.LI;
}
} else {
LI = GetCurLineInfo ();
}
/* Now output the warning */
/* Now output the warning */
LIWarning (EC_PARSER, LI, "Unreachable code");
}
}

View File

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

View File

@@ -53,7 +53,7 @@
unsigned Test (unsigned Label, int Invert)
/* 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
** defined above. If the jump is always true, a warning is output.
** defined above.
*/
{
ExprDesc Expr;
@@ -74,10 +74,7 @@ unsigned Test (unsigned Label, int Invert)
Result = (Expr.IVal != 0) ? TESTEXPR_TRUE : TESTEXPR_FALSE;
/* Constant rvalue */
if (!Invert && Expr.IVal == 0) {
g_jump (Label);
UnreachableCodeWarning ();
} else if (Invert && Expr.IVal != 0) {
if ((!Invert && Expr.IVal == 0) || (Invert && Expr.IVal != 0)) {
g_jump (Label);
}
@@ -125,8 +122,7 @@ unsigned Test (unsigned Label, int Invert)
unsigned TestInParens (unsigned Label, int Invert)
/* 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
** TESTEXPR_xx codes defined above. If the jump is always true, a warning is
** output.
** TESTEXPR_xx codes defined above.
*/
{
unsigned Result;

View File

@@ -59,14 +59,13 @@
unsigned Test (unsigned Label, int Invert);
/* 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
** defined above. If the jump is always true, a warning is output.
** defined above.
*/
unsigned TestInParens (unsigned Label, int Invert);
/* 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
** TESTEXPR_xx codes defined above. If the jump is always true, a warning is
** output.
** TESTEXPR_xx codes defined above.
*/