From d1efe1af90813a8c536fd539b62f7af5b0da1130 Mon Sep 17 00:00:00 2001 From: uz Date: Thu, 4 Aug 2011 17:18:06 +0000 Subject: [PATCH] Adjust code to C99 regarding the main function: Not returning anything in a main function with an int return type is identical to returning zero. git-svn-id: svn://svn.cc65.org/cc65/trunk@5118 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/cc65/function.c | 29 ++++++++++++++++++++++++----- src/cc65/function.h | 3 +++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/cc65/function.c b/src/cc65/function.c index 8944d1007..0f3229084 100644 --- a/src/cc65/function.c +++ b/src/cc65/function.c @@ -359,7 +359,7 @@ static void F_RestoreRegVars (Function* F) } - + /*****************************************************************************/ /* code */ /*****************************************************************************/ @@ -369,7 +369,8 @@ static void F_RestoreRegVars (Function* F) void NewFunc (SymEntry* Func) /* Parse argument declarations and function body. */ { - SymEntry* Param; + int C99MainFunc = 0;/* Flag for C99 main function returning int */ + SymEntry* Param; /* Get the function descriptor from the function entry */ FuncDesc* D = Func->V.F.Func; @@ -434,6 +435,14 @@ void NewFunc (SymEntry* Func) if (D->ParamCount > 0 || (D->Flags & FD_VARIADIC) != 0) { g_importmainargs (); } + + /* Determine if this is a main function in a C99 environment that + * returns an int. + */ + if (IsTypeInt (F_GetReturnType (CurrentFunc)) && + IS_Get (&Standard) == STD_C99) { + C99MainFunc = 1; + } } /* Allocate code and data segments for this function */ @@ -517,13 +526,23 @@ void NewFunc (SymEntry* Func) Statement (0); } - /* If this is not a void function, output a warning if we didn't see a - * return statement. + /* If this is not a void function, and not the main function in a C99 + * environment returning int, output a warning if we didn't see a return + * statement. */ - if (!F_HasVoidReturn (CurrentFunc) && !F_HasReturn (CurrentFunc)) { + if (!F_HasVoidReturn (CurrentFunc) && !F_HasReturn (CurrentFunc) && !C99MainFunc) { Warning ("Control reaches end of non-void function"); } + /* If this is the main function in a C99 environment returning an int, let + * it always return zero. Note: Actual return statements jump to the return + * label defined below. + * The code is removed by the optimizer if unused. + */ + if (C99MainFunc) { + g_getimmed (CF_INT | CF_CONST, 0, 0); + } + /* Output the function exit code label */ g_defcodelabel (F_GetRetLab (CurrentFunc)); diff --git a/src/cc65/function.h b/src/cc65/function.h index de9fdc123..36dcb0528 100644 --- a/src/cc65/function.h +++ b/src/cc65/function.h @@ -79,6 +79,9 @@ void F_ReturnFound (Function* F); int F_HasReturn (const Function* F); /* Return true if the function contains a return statement*/ +int F_IsMainFunc (const Function* F); +/* Return true if this is the main function */ + int F_IsVariadic (const Function* F); /* Return true if this is a variadic function */ -- 2.39.5