Merge pull request #2265 from acqn/C99Main

[cc65] Enabled implicit "return 0" in C99 standard main function in default cc65 mode
This commit is contained in:
Bob Andrews 2023-11-28 14:27:55 +01:00 committed by GitHub
commit ad0b778008
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 35 deletions

View File

@ -518,11 +518,21 @@ void NewFunc (SymEntry* Func, FuncDesc* D)
Error ("'main' cannot be declared as __fastcall__");
}
/* If cc65 extensions aren't enabled, don't allow a main function that
** doesn't return an int.
*/
if (IS_Get (&Standard) != STD_CC65 && ReturnType[0].C != T_INT) {
Error ("'main' must always return an int");
/* Check return type */
if (GetUnqualRawTypeCode (ReturnType) == T_INT) {
/* Determine if this is a main function in a C99 environment that
** returns an int.
*/
if (IS_Get (&Standard) >= STD_C99) {
C99MainFunc = 1;
}
} else {
/* If cc65 extensions aren't enabled, don't allow a main function
** that doesn't return an int.
*/
if (IS_Get (&Standard) != STD_CC65) {
Error ("'main' must always return an int");
}
}
/* Add a forced import of a symbol that is contained in the startup
@ -540,14 +550,6 @@ void NewFunc (SymEntry* Func, FuncDesc* D)
/* The start-up code doesn't fast-call main(). */
Func->Type->C |= T_QUAL_CDECL;
}
/* Determine if this is a main function in a C99 environment that
** returns an int.
*/
if (GetUnqualRawTypeCode (ReturnType) == T_INT &&
IS_Get (&Standard) == STD_C99) {
C99MainFunc = 1;
}
}
/* Allocate code and data segments for this function */
@ -652,21 +654,16 @@ void NewFunc (SymEntry* Func, FuncDesc* D)
AnyStatement (0);
}
/* 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) && !C99MainFunc && IS_Get (&WarnReturnType)) {
Warning ("Control reaches end of non-void function [-Wreturn-type]");
}
/* 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);
/* Check if this function is missing a return value */
if (!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.
*/
if (C99MainFunc) {
g_getimmed (CF_INT | CF_CONST, 0, 0);
} else if (IS_Get (&WarnReturnType)) {
Warning ("Control reaches end of non-void function [-Wreturn-type]");
}
}
/* Output the function exit code label */

View File

@ -13,9 +13,9 @@
and then "make" again to confirm
*/
int main(int argc, char* argv[])
short main(int argc, char* argv[])
{
printf("%02x", 0x42);
n = 0; /* produce an error */
/* another error */
printf("%02x", 0x42); /* produce an error */
n = 0; /* produce an error */
/* produce a warning */
}

View File

@ -20,5 +20,4 @@ int main(int argc, char* argv[])
{
printf("%02x", 0x42);
/* produce a warning */
return 0;
}

View File

@ -1,2 +1,2 @@
custom-reference.c:24: Warning: Parameter 'argc' is never used
custom-reference.c:24: Warning: Parameter 'argv' is never used
custom-reference.c:23: Warning: Parameter 'argc' is never used
custom-reference.c:23: Warning: Parameter 'argv' is never used