Made C's sizeof operator work with initialized void variables.

Added regression tests that check cc65's handling of void variables.
This commit is contained in:
Greg King
2017-03-12 12:55:31 -04:00
parent a780df1fe1
commit 750a527100
7 changed files with 147 additions and 62 deletions

View File

@@ -389,7 +389,10 @@ unsigned SizeOf (const Type* T)
switch (UnqualifiedType (T->C)) {
case T_VOID:
return 0; /* Assume voids have size zero */
/* A void variable is a cc65 extension.
** Get its size (in bytes).
*/
return T->A.U;
/* Beware: There's a chance that this triggers problems in other parts
of the compiler. The solution is to fix the callers, because calling
@@ -438,7 +441,7 @@ unsigned SizeOf (const Type* T)
/* Array with unspecified size */
return 0;
} else {
return T->A.L * SizeOf (T + 1);
return T->A.U * SizeOf (T + 1);
}
default:

View File

@@ -891,6 +891,7 @@ static void ParseTypeSpec (DeclSpec* D, long Default, TypeCode Qualifiers)
case TOK_VOID:
NextToken ();
D->Type[0].C = T_VOID;
D->Type[0].A.U = 0;
D->Type[1].C = T_END;
break;
@@ -2114,7 +2115,7 @@ NextMember:
static unsigned ParseVoidInit (void)
static unsigned ParseVoidInit (Type* T)
/* Parse an initialization of a void variable (special cc65 extension).
** Return the number of bytes initialized.
*/
@@ -2181,6 +2182,9 @@ static unsigned ParseVoidInit (void)
/* Closing brace */
ConsumeRCurly ();
/* Number of bytes determined by initializer */
T->A.U = Size;
/* Return the number of bytes initialized */
return Size;
}
@@ -2216,8 +2220,8 @@ static unsigned ParseInitInternal (Type* T, int AllowFlexibleMembers)
case T_VOID:
if (IS_Get (&Standard) == STD_CC65) {
/* Special cc65 extension in non ANSI mode */
return ParseVoidInit ();
/* Special cc65 extension in non-ANSI mode */
return ParseVoidInit (T);
}
/* FALLTHROUGH */