From 7e80e55b6d12138024d44557ed878fa3c50c6fda Mon Sep 17 00:00:00 2001 From: acqn Date: Sat, 13 Jan 2024 16:40:44 +0800 Subject: [PATCH 1/5] Added a warning on implicit int in typedefs. --- src/cc65/declare.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/cc65/declare.c b/src/cc65/declare.c index f93305f01..7f1d8b948 100644 --- a/src/cc65/declare.c +++ b/src/cc65/declare.c @@ -2388,16 +2388,26 @@ int ParseDecl (DeclSpec* Spec, Declarator* D, declmode_t Mode) GetFuncDesc (D->Type)->Flags |= FD_OLDSTYLE_INTRET; } - /* For anything that is not a function or typedef, check for an implicit - ** int declaration. + /* For anything that is not a function, check for an implicit int + ** declaration. */ - if (!IsTypeFunc (D->Type) && - (D->StorageClass & SC_TYPEMASK) != SC_TYPEDEF) { - /* If the standard was not set explicitly to C89, print a warning - ** for variables with implicit int type. - */ - if (IS_Get (&Standard) >= STD_C99) { - Warning ("Implicit 'int' is an obsolete feature"); + if (!IsTypeFunc (D->Type) && IsRankInt (D->Type)) { + if ((D->StorageClass & SC_TYPEMASK) != SC_TYPEDEF) { + /* If the standard was not set explicitly to C89, print a warning + ** for variables with implicit int type. + */ + if (IS_Get (&Standard) >= STD_C99) { + Warning ("Implicit 'int' is an obsolete feature"); + } + } else { + /* If the standard was not set explicitly to C89, print a warning + ** for typedefs with implicit int type. + */ + if (IS_Get (&Standard) >= STD_C99) { + Warning ("Type defaults to 'int' in typedef of '%s'", + D->Ident); + Note ("Implicit 'int' is an obsolete feature"); + } } } } From de3087a7e9b7590ab8122d547e8ae9ac0d191b21 Mon Sep 17 00:00:00 2001 From: acqn Date: Sun, 14 Jan 2024 00:19:11 +0800 Subject: [PATCH 2/5] Removed the extra "unused parameter" warning when the parameter had an duplicated identifier error. --- src/cc65/symtab.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cc65/symtab.c b/src/cc65/symtab.c index a76e60450..d3544bfc4 100644 --- a/src/cc65/symtab.c +++ b/src/cc65/symtab.c @@ -170,7 +170,8 @@ static void CheckSymTable (SymTable* Tab) if (SymIsDef (Entry) && !SymIsRef (Entry) && !SymHasAttr (Entry, atUnused)) { if (Flags & SC_PARAM) { - if (IS_Get (&WarnUnusedParam)) { + if (IS_Get (&WarnUnusedParam) && + !IsAnonName (Entry->Name)) { Warning ("Parameter '%s' is never used", Entry->Name); } } else if ((Flags & SC_TYPEMASK) == SC_FUNC) { From afdf398a0b628704b23166cde21d986b23d168b6 Mon Sep 17 00:00:00 2001 From: acqn Date: Mon, 15 Jan 2024 23:56:11 +0800 Subject: [PATCH 3/5] Fixed repeated diagnosis when reading EOF in certain cases. --- src/cc65/declare.c | 6 +++--- src/cc65/locals.c | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/cc65/declare.c b/src/cc65/declare.c index 7f1d8b948..c3c1160a6 100644 --- a/src/cc65/declare.c +++ b/src/cc65/declare.c @@ -997,7 +997,7 @@ static SymEntry* ParseUnionSpec (const char* Name, unsigned* DSFlags) /* Parse union fields */ UnionSize = 0; - while (CurTok.Tok != TOK_RCURLY) { + while (CurTok.Tok != TOK_RCURLY && CurTok.Tok != TOK_CEOF) { /* Get the type of the entry */ DeclSpec Spec; @@ -1217,7 +1217,7 @@ static SymEntry* ParseStructSpec (const char* Name, unsigned* DSFlags) FlexibleMember = 0; StructSize = 0; BitOffs = 0; - while (CurTok.Tok != TOK_RCURLY) { + while (CurTok.Tok != TOK_RCURLY && CurTok.Tok != TOK_CEOF) { /* Get the type of the entry */ DeclSpec Spec; @@ -1814,7 +1814,7 @@ static void ParseOldStyleParamDeclList (FuncDesc* F attribute ((unused))) } /* An optional list of type specifications follows */ - while (CurTok.Tok != TOK_LCURLY) { + while (CurTok.Tok != TOK_LCURLY && CurTok.Tok != TOK_CEOF) { DeclSpec Spec; int NeedClean; diff --git a/src/cc65/locals.c b/src/cc65/locals.c index 28e263bb8..8bf7aa1d2 100644 --- a/src/cc65/locals.c +++ b/src/cc65/locals.c @@ -551,7 +551,9 @@ void DeclareLocals (void) /* A place to store info about potential initializations of auto variables */ CollAppend (&CurrentFunc->LocalsBlockStack, 0); - /* Loop until we don't find any more variables */ + /* Loop until we don't find any more variables. EOF is handled in the loop + ** as well. + */ while (1) { DeclSpec Spec; int NeedClean; From 07e349c517a4702c8c2af26064691dcf05c5e580 Mon Sep 17 00:00:00 2001 From: acqn Date: Mon, 15 Jan 2024 23:56:39 +0800 Subject: [PATCH 4/5] Skipped anonymous tag names in diagnosis on empty structs/unions. --- src/cc65/symtab.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/cc65/symtab.c b/src/cc65/symtab.c index d3544bfc4..8ae49cdf3 100644 --- a/src/cc65/symtab.c +++ b/src/cc65/symtab.c @@ -955,7 +955,13 @@ SymEntry* AddStructSym (const char* Name, unsigned Flags, unsigned Size, SymTabl TagEntry = 0; } else if (Size == 0) { /* Empty struct is not supported now */ - Error ("Empty %s type '%s' is not supported", SCType == SC_STRUCT ? "struct" : "union", Name); + if (!IsAnonName (Name)) { + Error ("Empty %s type '%s' is not supported", + SCType == SC_STRUCT ? "struct" : "union", Name); + } else { + Error ("Empty %s type is not supported", + SCType == SC_STRUCT ? "struct" : "union"); + } TagEntry = 0; } } From e9bd9330c0b09f646a52e36c6cbf198015887d2f Mon Sep 17 00:00:00 2001 From: acqn Date: Mon, 15 Jan 2024 23:56:42 +0800 Subject: [PATCH 5/5] Added warning on some code patterns of faulty attempt to declare anonymous structs/unions. Removed unnecessary warning on tagless enum/struct/unions that would be invisible out of a function declaration. --- src/cc65/datatype.c | 18 ++++++++++++ src/cc65/datatype.h | 6 ++++ src/cc65/declare.c | 68 ++++++++++++++++++++------------------------- 3 files changed, 54 insertions(+), 38 deletions(-) diff --git a/src/cc65/datatype.c b/src/cc65/datatype.c index 9c82e6773..4d6cb25a5 100644 --- a/src/cc65/datatype.c +++ b/src/cc65/datatype.c @@ -989,6 +989,24 @@ int IsIncompleteESUType (const Type* T) +int IsAnonESUType (const Type* T) +/* Return true if this is an anonymous ESU type */ +{ + SymEntry* TagSym = GetESUTagSym (T); + + return TagSym != 0 && SymHasAnonName (TagSym); +} + + + +int IsAnonStructClass (const Type* T) +/* Return true if this is an anonymous struct or union type */ +{ + return IsClassStruct (T) && IsAnonESUType (T); +} + + + int IsPassByRefType (const Type* T) /* Return true if this is a large struct/union type that doesn't fit in the ** primary. This returns false for the void value extension type since it is diff --git a/src/cc65/datatype.h b/src/cc65/datatype.h index dbe0eedaa..8446fb914 100644 --- a/src/cc65/datatype.h +++ b/src/cc65/datatype.h @@ -792,6 +792,12 @@ int IsESUType (const Type* T); int IsIncompleteESUType (const Type* T); /* Return true if this is an incomplete ESU type */ +int IsAnonESUType (const Type* T); +/* Return true if this is an anonymous ESU type */ + +int IsAnonStructClass (const Type* T); +/* Return true if this is an anonymous struct or union type */ + int IsPassByRefType (const Type* T); /* Return true if this is a large struct/union type that doesn't fit in the ** primary. This returns false for the void value extension type since it is diff --git a/src/cc65/declare.c b/src/cc65/declare.c index c3c1160a6..20e2e6879 100644 --- a/src/cc65/declare.c +++ b/src/cc65/declare.c @@ -1020,7 +1020,8 @@ static SymEntry* ParseUnionSpec (const char* Name, unsigned* DSFlags) ParseTypeSpec (&Spec, TS_DEFAULT_TYPE_NONE); /* Check if this is only a type declaration */ - if (CurTok.Tok == TOK_SEMI && (Spec.Flags & DS_EXTRA_TYPE) == 0) { + if (CurTok.Tok == TOK_SEMI && + !(IS_Get (&Standard) >= STD_CC65 && IsAnonStructClass (Spec.Type))) { CheckEmptyDecl (&Spec); NextToken (); continue; @@ -1061,22 +1062,12 @@ static SymEntry* ParseUnionSpec (const char* Name, unsigned* DSFlags) /* In cc65 mode, we allow anonymous structs/unions within ** a union. */ - SymEntry* TagEntry; - if (IS_Get (&Standard) >= STD_CC65 && - IsClassStruct (Decl.Type) && - (TagEntry = GetESUTagSym (Decl.Type)) && - SymHasAnonName (TagEntry)) { - /* This is an anonymous struct or union */ - AnonFieldName (Decl.Ident, GetBasicTypeName (Decl.Type), UnionTagEntry->V.S.ACount); + AnonFieldName (Decl.Ident, GetBasicTypeName (Decl.Type), UnionTagEntry->V.S.ACount); - /* Ignore CVR qualifiers */ - if (IsQualConst (Decl.Type) || IsQualVolatile (Decl.Type) || IsQualRestrict (Decl.Type)) { - Warning ("Anonymous %s qualifiers are ignored", GetBasicTypeName (Decl.Type)); - Decl.Type[0].C &= ~T_QUAL_CVR; - } - } else { - /* Invalid member */ - goto NextMember; + /* Ignore CVR qualifiers */ + if (IsQualConst (Decl.Type) || IsQualVolatile (Decl.Type) || IsQualRestrict (Decl.Type)) { + Warning ("Anonymous %s qualifiers are ignored", GetBasicTypeName (Decl.Type)); + Decl.Type[0].C &= ~T_QUAL_CVR; } } else if (FieldWidth > 0) { /* A bit-field without a name will get an anonymous one */ @@ -1240,7 +1231,8 @@ static SymEntry* ParseStructSpec (const char* Name, unsigned* DSFlags) ParseTypeSpec (&Spec, TS_DEFAULT_TYPE_NONE); /* Check if this is only a type declaration */ - if (CurTok.Tok == TOK_SEMI && (Spec.Flags & DS_EXTRA_TYPE) == 0) { + if (CurTok.Tok == TOK_SEMI && + !(IS_Get (&Standard) >= STD_CC65 && IsAnonStructClass (Spec.Type))) { CheckEmptyDecl (&Spec); NextToken (); continue; @@ -1308,22 +1300,12 @@ static SymEntry* ParseStructSpec (const char* Name, unsigned* DSFlags) /* In cc65 mode, we allow anonymous structs/unions within ** a struct. */ - SymEntry* TagEntry; - if (IS_Get (&Standard) >= STD_CC65 && - IsClassStruct (Decl.Type) && - (TagEntry = GetESUTagSym (Decl.Type)) && - SymHasAnonName (TagEntry)) { - /* This is an anonymous struct or union */ - AnonFieldName (Decl.Ident, GetBasicTypeName (Decl.Type), StructTagEntry->V.S.ACount); + AnonFieldName (Decl.Ident, GetBasicTypeName (Decl.Type), StructTagEntry->V.S.ACount); - /* Ignore CVR qualifiers */ - if (IsQualConst (Decl.Type) || IsQualVolatile (Decl.Type) || IsQualRestrict (Decl.Type)) { - Warning ("Anonymous %s qualifiers are ignored", GetBasicTypeName (Decl.Type)); - Decl.Type[0].C &= ~T_QUAL_CVR; - } - } else { - /* Invalid member */ - goto NextMember; + /* Ignore CVR qualifiers */ + if (IsQualConst (Decl.Type) || IsQualVolatile (Decl.Type) || IsQualRestrict (Decl.Type)) { + Warning ("Anonymous %s qualifiers are ignored", GetBasicTypeName (Decl.Type)); + Decl.Type[0].C &= ~T_QUAL_CVR; } } else if (FieldWidth > 0) { /* A bit-field without a name will get an anonymous one */ @@ -1854,7 +1836,7 @@ static void ParseOldStyleParamDeclList (FuncDesc* F attribute ((unused))) } /* Warn about new local type declaration */ - if ((Spec.Flags & DS_NEW_TYPE_DECL) != 0) { + if ((Spec.Flags & DS_NEW_TYPE_DECL) != 0 && !IsAnonESUType (Spec.Type)) { Warning ("'%s' will be invisible out of this function", GetFullTypeName (Spec.Type)); } @@ -1957,7 +1939,7 @@ static void ParseAnsiParamList (FuncDesc* F) } /* Warn about new local type declaration */ - if ((Spec.Flags & DS_NEW_TYPE_DECL) != 0) { + if ((Spec.Flags & DS_NEW_TYPE_DECL) != 0 && !IsAnonESUType (Spec.Type)) { Warning ("'%s' will be invisible out of this function", GetFullTypeName (Spec.Type)); } @@ -2508,10 +2490,20 @@ void CheckEmptyDecl (const DeclSpec* Spec) if ((Spec->Flags & DS_TYPE_MASK) == DS_NONE) { /* No declaration at all */ } else if ((Spec->Flags & DS_EXTRA_TYPE) == 0) { - Warning ("Declaration does not declare anything"); - } else if (IsClassStruct (Spec->Type) && - !IsIncompleteESUType (Spec->Type) && - SymHasAnonName (GetESUTagSym (Spec->Type))) { + /* Empty declaration of basic types */ + Warning ("Useless declaration"); + } else if (IsAnonStructClass (Spec->Type)) { + /* This could be that the user made a wrong attempt to declare an + ** anonymous struct/union field outside a struct/union. + */ Warning ("Unnamed %s that defines no instances", GetBasicTypeName (Spec->Type)); + } else if (GetLexicalLevel () == LEX_LEVEL_STRUCT) { + /* This could be that the user made a wrong attempt to declare an + ** anonymous struct/union field inside a struct/union. Perhaps just + ** paranoid since it is not so uncommon to do forward declarations. + */ + if (!IsTypeEnum (Spec->Type) || ((Spec->Flags & DS_NEW_TYPE_DEF) == 0)) { + Warning ("Declaration defines no instances"); + } } }