From f13284d3f82edfdf699f512c89b9a2067d88b7b7 Mon Sep 17 00:00:00 2001 From: Kugel Fuhr <98353208+kugelfuhr@users.noreply.github.com> Date: Thu, 17 Jul 2025 15:41:17 +0200 Subject: [PATCH] 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. --- src/cc65/error.c | 46 ++++++++++++++++++++++----------------------- src/cc65/stmt.c | 5 +++++ src/cc65/testexpr.c | 10 +++------- src/cc65/testexpr.h | 5 ++--- 4 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/cc65/error.c b/src/cc65/error.c index 97e651890..a5cf4e3e8 100644 --- a/src/cc65/error.c +++ b/src/cc65/error.c @@ -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"); } } diff --git a/src/cc65/stmt.c b/src/cc65/stmt.c index 51a8a6966..20618d8bb 100644 --- a/src/cc65/stmt.c +++ b/src/cc65/stmt.c @@ -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); diff --git a/src/cc65/testexpr.c b/src/cc65/testexpr.c index ced563d01..85355f07e 100644 --- a/src/cc65/testexpr.c +++ b/src/cc65/testexpr.c @@ -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; diff --git a/src/cc65/testexpr.h b/src/cc65/testexpr.h index 84b957af2..8563838eb 100644 --- a/src/cc65/testexpr.h +++ b/src/cc65/testexpr.h @@ -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. */