From 8cdffc1944d743455310fae7dac7470229106759 Mon Sep 17 00:00:00 2001 From: acqn Date: Mon, 3 Aug 2020 01:36:19 +0800 Subject: [PATCH] No storage for unsuccessfully parsed variables. --- src/cc65/compile.c | 6 ++++++ src/cc65/declare.c | 11 +++++++++-- src/cc65/locals.c | 21 +++++++++++---------- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/cc65/compile.c b/src/cc65/compile.c index a28c3e848..ef7dea149 100644 --- a/src/cc65/compile.c +++ b/src/cc65/compile.c @@ -148,6 +148,11 @@ static void Parse (void) break; } + if ((Decl.StorageClass & SC_ALIAS) == SC_ALIAS) { + /* Failed parsing */ + goto SkipOneDecl; + } + /* Check if we must reserve storage for the variable. We do this, ** ** - if it is not a typedef or function, @@ -276,6 +281,7 @@ static void Parse (void) } +SkipOneDecl: /* Check for end of declaration list */ if (CurTok.Tok == TOK_COMMA) { NextToken (); diff --git a/src/cc65/declare.c b/src/cc65/declare.c index 5f5fdeac4..7b868380d 100644 --- a/src/cc65/declare.c +++ b/src/cc65/declare.c @@ -1814,6 +1814,9 @@ Type* ParseType (Type* T) void ParseDecl (const DeclSpec* Spec, Declaration* D, declmode_t Mode) /* Parse a variable, type or function declaration */ { + /* Used to check if we have any errors during parsing this */ + unsigned PrevErrorCount = ErrorCount; + /* Initialize the Declaration struct */ InitDeclaration (D); @@ -1891,8 +1894,8 @@ void ParseDecl (const DeclSpec* Spec, Declaration* D, declmode_t Mode) } } - /* Check the size of the generated type */ if (!IsTypeFunc (D->Type) && !IsTypeVoid (D->Type)) { + /* Check the size of the generated type */ unsigned Size = SizeOf (D->Type); if (Size >= 0x10000) { if (D->Ident[0] != '\0') { @@ -1901,8 +1904,12 @@ void ParseDecl (const DeclSpec* Spec, Declaration* D, declmode_t Mode) Error ("Invalid size in declaration (0x%06X)", Size); } } - } + if (PrevErrorCount != ErrorCount) { + /* Don't give storage if the declaration is not parsed correctly */ + D->StorageClass |= SC_DECL | SC_ALIAS; + } + } } diff --git a/src/cc65/locals.c b/src/cc65/locals.c index 3fa26021f..03bc80f21 100644 --- a/src/cc65/locals.c +++ b/src/cc65/locals.c @@ -443,13 +443,14 @@ static void ParseOneDecl (const DeclSpec* Spec) } /* If the symbol is not marked as external, it will be defined now */ - if ((Decl.StorageClass & SC_EXTERN) == 0) { + if ((Decl.StorageClass & SC_ALIAS) == 0 && + (Decl.StorageClass & SC_EXTERN) == 0) { Decl.StorageClass |= SC_DEF; } /* Handle anything that needs storage (no functions, no typdefs) */ - if ((Decl.StorageClass & SC_FUNC) != SC_FUNC && - (Decl.StorageClass & SC_TYPEMASK) != SC_TYPEDEF) { + if ((Decl.StorageClass & SC_DEF) == SC_DEF && + (Decl.StorageClass & SC_TYPEMASK) != SC_TYPEDEF) { /* If we have a register variable, try to allocate a register and ** convert the declaration to "auto" if this is not possible. @@ -468,13 +469,6 @@ static void ParseOneDecl (const DeclSpec* Spec) } else if ((Decl.StorageClass & SC_AUTO) == SC_AUTO) { /* Auto variable */ ParseAutoDecl (&Decl); - } else if ((Decl.StorageClass & SC_EXTERN) == SC_EXTERN) { - /* External identifier - may not get initialized */ - if (CurTok.Tok == TOK_ASSIGN) { - Error ("Cannot initialize externals"); - } - /* Add the external symbol to the symbol table */ - AddLocalSym (Decl.Ident, Decl.Type, Decl.StorageClass, 0); } else if ((Decl.StorageClass & SC_STATIC) == SC_STATIC) { /* Static variable */ ParseStaticDecl (&Decl); @@ -484,6 +478,13 @@ static void ParseOneDecl (const DeclSpec* Spec) } else { + if ((Decl.StorageClass & SC_EXTERN) == SC_EXTERN) { + /* External identifier - may not get initialized */ + if (CurTok.Tok == TOK_ASSIGN) { + Error ("Cannot initialize externals"); + } + } + /* Add the symbol to the symbol table */ AddLocalSym (Decl.Ident, Decl.Type, Decl.StorageClass, 0);