1
0
mirror of https://github.com/cc65/cc65.git synced 2024-10-01 15:54:59 +00:00

Error message for negative array sizes.

Restrucured struct decl parsing code.


git-svn-id: svn://svn.cc65.org/cc65/trunk@1914 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz 2003-02-01 12:39:12 +00:00
parent 7a1a7745eb
commit 88cb1a9fd8

View File

@ -6,9 +6,9 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 1998-2002 Ullrich von Bassewitz */ /* (C) 1998-2003 Ullrich von Bassewitz */
/* Wacholderweg 14 */ /* Römerstrasse 52 */
/* D-70597 Stuttgart */ /* D-70794 Filderstadt */
/* EMail: uz@musoftware.de */ /* EMail: uz@musoftware.de */
/* */ /* */
/* */ /* */
@ -236,7 +236,7 @@ static void ParseEnumDecl (void)
/* Add an entry to the symbol table */ /* Add an entry to the symbol table */
AddConstSym (Ident, type_int, SC_ENUM, EnumVal++); AddConstSym (Ident, type_int, SC_ENUM, EnumVal++);
/* Check for end of definition */ /* Check for end of definition */
if (CurTok.Tok != TOK_COMMA) if (CurTok.Tok != TOK_COMMA)
break; break;
@ -251,7 +251,8 @@ static SymEntry* ParseStructDecl (const char* Name, type StructType)
/* Parse a struct/union declaration. */ /* Parse a struct/union declaration. */
{ {
unsigned Size; unsigned StructSize;
unsigned FieldSize;
unsigned Offs; unsigned Offs;
SymTable* FieldTab; SymTable* FieldTab;
SymEntry* Entry; SymEntry* Entry;
@ -282,7 +283,7 @@ static SymEntry* ParseStructDecl (const char* Name, type StructType)
EnterStructLevel (); EnterStructLevel ();
/* Parse struct fields */ /* Parse struct fields */
Size = 0; StructSize = 0;
while (CurTok.Tok != TOK_RCURLY) { while (CurTok.Tok != TOK_RCURLY) {
/* Get the type of the entry */ /* Get the type of the entry */
@ -298,18 +299,20 @@ static SymEntry* ParseStructDecl (const char* Name, type StructType)
ParseDecl (&Spec, &Decl, 0); ParseDecl (&Spec, &Decl, 0);
/* Get the offset of this field */ /* Get the offset of this field */
Offs = (StructType == T_STRUCT)? Size : 0; Offs = (StructType == T_STRUCT)? StructSize : 0;
/* Add a field entry to the table */ /* Add a field entry to the table */
AddLocalSym (Decl.Ident, Decl.Type, SC_STRUCTFIELD, Offs); AddLocalSym (Decl.Ident, Decl.Type, SC_STRUCTFIELD, Offs);
/* Calculate offset of next field/size of the union */ /* Calculate offset of next field/size of the union */
Offs = CheckedSizeOf (Decl.Type); FieldSize = CheckedSizeOf (Decl.Type);
if (StructType == T_STRUCT) { if (StructType == T_STRUCT) {
Size += Offs; /* It's a struct */
StructSize += FieldSize;
} else { } else {
if (Offs > Size) { /* It's a union */
Size = Offs; if (FieldSize > StructSize) {
StructSize = FieldSize;
} }
} }
@ -328,7 +331,7 @@ static SymEntry* ParseStructDecl (const char* Name, type StructType)
LeaveStructLevel (); LeaveStructLevel ();
/* Make a real entry from the forward decl and return it */ /* Make a real entry from the forward decl and return it */
return AddStructSym (Name, Size, FieldTab); return AddStructSym (Name, StructSize, FieldTab);
} }
@ -874,6 +877,14 @@ static void Decl (const DeclSpec* Spec, Declaration* D, unsigned Mode)
if (CurTok.Tok != TOK_RBRACK) { if (CurTok.Tok != TOK_RBRACK) {
ExprDesc lval; ExprDesc lval;
ConstExpr (&lval); ConstExpr (&lval);
if (lval.ConstVal < 0) {
if (D->Ident[0] != '\0') {
Error ("Size of array `%s' is negative", D->Ident);
} else {
Error ("Size of array is negative");
}
lval.ConstVal = 1;
}
Size = lval.ConstVal; Size = lval.ConstVal;
} }
ConsumeRBrack (); ConsumeRBrack ();