1
0
mirror of https://github.com/cc65/cc65.git synced 2024-11-19 06:31:31 +00:00

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
This commit is contained in:
uz 2011-08-04 17:18:06 +00:00
parent 2f75733e43
commit d1efe1af90
2 changed files with 27 additions and 5 deletions

View File

@ -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));

View File

@ -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 */