From 6d45a9412767eb7850ef5891b5732474bab37b4e Mon Sep 17 00:00:00 2001 From: Kugel Fuhr <98353208+kugelfuhr@users.noreply.github.com> Date: Thu, 17 Jul 2025 16:12:48 +0200 Subject: [PATCH] Do not output a warning about a missing "return" in a function if the function exit is unreachable. --- src/cc65/error.c | 2 +- src/cc65/function.c | 7 +++++-- test/misc/Makefile | 4 ++-- test/misc/flow-do-01.c | 8 ++++---- test/misc/flow-if-02.c | 4 ++-- test/misc/flow-while-01.c | 6 +++--- 6 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/cc65/error.c b/src/cc65/error.c index a5cf4e3e8..ebd3a55c6 100644 --- a/src/cc65/error.c +++ b/src/cc65/error.c @@ -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. diff --git a/src/cc65/function.c b/src/cc65/function.c index 0e040d61d..c14372d43 100644 --- a/src/cc65/function.c +++ b/src/cc65/function.c @@ -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. */ diff --git a/test/misc/Makefile b/test/misc/Makefile index 3e8b0c157..a595d82a9 100644 --- a/test/misc/Makefile +++ b/test/misc/Makefile @@ -156,7 +156,7 @@ $(WORKDIR)/flow-do-01.$1.$2.prg: flow-do-01.c $(ISEQUAL) | $(WORKDIR) $(if $(QUIET),echo misc/flow-do-01.$1.$2.prg) $(CC65) -t sim$2 -$1 -o $$@ $$< $(NULLOUT) 2>$(WORKDIR)/flow-do-01.$1.$2.out $(ISEQUAL) $(WORKDIR)/flow-do-01.$1.$2.out flow-do-01.ref - + $(WORKDIR)/flow-if-01.$1.$2.prg: flow-if-01.c $(ISEQUAL) | $(WORKDIR) $(if $(QUIET),echo misc/flow-if-01.$1.$2.prg) $(CC65) -t sim$2 -$1 -o $$@ $$< $(NULLOUT) 2>$(WORKDIR)/flow-if-01.$1.$2.out @@ -182,7 +182,7 @@ $(WORKDIR)/flow-while-02.$1.$2.prg: flow-while-02.c $(ISEQUAL) | $(WORKDIR) $(CC65) -t sim$2 -$1 -o $$@ $$< $(NULLOUT) 2>$(WORKDIR)/flow-while-02.$1.$2.out $(ISEQUAL) $(WORKDIR)/flow-while-02.$1.$2.out flow-while-02.ref -$(WORKDIR)/limits.$1.$2.prg: limits.c $(ISEQUAL) | $(WORKDIR) +$(WORKDIR)/limits.$1.$2.prg: limits.c $(ISEQUAL) | $(WORKDIR) $(if $(QUIET),echo misc/limits.$1.$2.prg) $(CC65) -t sim$2 -$1 -o $$(@:.prg=.s) $$< $(NULLOUT) $(CATERR) $(CA65) -t sim$2 -o $$(@:.prg=.o) $$(@:.prg=.s) $(NULLERR) diff --git a/test/misc/flow-do-01.c b/test/misc/flow-do-01.c index 7d4cb86e2..d1997311f 100644 --- a/test/misc/flow-do-01.c +++ b/test/misc/flow-do-01.c @@ -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; diff --git a/test/misc/flow-if-02.c b/test/misc/flow-if-02.c index 6e30d9d5f..ff60e5ce0 100644 --- a/test/misc/flow-if-02.c +++ b/test/misc/flow-if-02.c @@ -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; } diff --git a/test/misc/flow-while-01.c b/test/misc/flow-while-01.c index 5ba4f8d9f..91d1454a9 100644 --- a/test/misc/flow-while-01.c +++ b/test/misc/flow-while-01.c @@ -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 */ @@ -56,8 +56,8 @@ L: a = 2; static int f6(void) { - while (0) { - /* Unreachable but no warning */ + while (0) { + /* Unreachable but no warning */ } a = 2; return a;