Skip to content

Implement warning 210 #440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion source/compiler/sc.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,11 @@ typedef struct s_symbol {
* used during parsing a function, to detect a mix of "return;" and
* "return value;" in a few special cases.
*/
#define uRETNONE 0x10
#define uRETNONE 0x010
/* uEXPLINIT is set when a variable/array is explicitly initialized at
* definition or assigned a value. This flag differs from uWRITTEN
* because it doesn't mean the variable/array has been used somewhere. */
#define uEXPLINIT 0x020

#define flagDEPRECATED 0x01 /* symbol is deprecated (avoid use) */
#define flagNAKED 0x10 /* function is naked */
Expand Down Expand Up @@ -650,6 +654,8 @@ SC_FUNC void delete_symbol(symbol *root,symbol *sym);
SC_FUNC void delete_symbols(symbol *root,int level,int del_labels,int delete_functions);
SC_FUNC int refer_symbol(symbol *entry,symbol *bywhom);
SC_FUNC void markusage(symbol *sym,int usage);
SC_FUNC void markinitialized(symbol *sym);
SC_FUNC void checkinitialized(symbol *sym);
SC_FUNC void rename_symbol(symbol *sym,const char *newname);
SC_FUNC symbol *findglb(const char *name,int filter);
SC_FUNC symbol *findloc(const char *name);
Expand Down
49 changes: 35 additions & 14 deletions source/compiler/sc1.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ static void decl_const(int table);
static void decl_enum(int table,int fstatic);
static cell needsub(int *tag,constvalue_root **enumroot);
static void initials(int ident,int tag,cell *size,int dim[],int numdim,
constvalue_root *enumroot);
constvalue_root *enumroot,int *explicit_init);
static cell initarray(int ident,int tag,int dim[],int numdim,int cur,
int startlit,int counteddim[],constvalue_root *lastdim,
constvalue_root *enumroot,int *errorfound);
Expand Down Expand Up @@ -2004,6 +2004,7 @@ static void declglb(char *firstname,int firsttag,int fpublic,int fstatic,int fst
char *str;
int dim[sDIMEN_MAX];
int numdim;
int explicit_init;
short filenum;
symbol *sym;
constvalue_root *enumroot=NULL;
Expand Down Expand Up @@ -2108,7 +2109,7 @@ static void declglb(char *firstname,int firsttag,int fpublic,int fstatic,int fst
litidx=0; /* global initial data is dumped, so restart at zero */
} /* if */
assert(litidx==0); /* literal queue should be empty (again) */
initials(ident,tag,&size,dim,numdim,enumroot);/* stores values in the literal queue */
initials(ident,tag,&size,dim,numdim,enumroot,&explicit_init);/* stores values in the literal queue */
assert(size>=litidx);
if (numdim==1)
dim[0]=(int)size;
Expand Down Expand Up @@ -2232,6 +2233,8 @@ static void declglb(char *firstname,int firsttag,int fpublic,int fstatic,int fst
sym->usage|=uSTOCK;
if (fstatic)
sym->fnumber=filenum;
if (explicit_init)
markinitialized(sym);
sc_attachdocumentation(sym);/* attach any documenation to the variable */
if (sc_status==statSKIP) {
sc_status=statWRITE;
Expand Down Expand Up @@ -2268,6 +2271,7 @@ static int declloc(int fstatic)
int numdim;
int fconst;
int staging_start;
int explicit_init; /* is the variable explicitly initialized? */

fconst=matchtoken(tCONST);
do {
Expand Down Expand Up @@ -2318,7 +2322,7 @@ static int declloc(int fstatic)
sc_alignnext=FALSE;
} /* if */
cur_lit=litidx; /* save current index in the literal table */
initials(ident,tag,&size,dim,numdim,enumroot);
initials(ident,tag,&size,dim,numdim,enumroot,&explicit_init);
if (size==0)
return ident; /* error message already given */
if (numdim==1)
Expand Down Expand Up @@ -2357,7 +2361,7 @@ static int declloc(int fstatic)
if (ident==iVARIABLE) {
/* simple variable, also supports initialization */
int ctag = tag; /* set to "tag" by default */
int explicit_init=FALSE;/* is the variable explicitly initialized? */
explicit_init=FALSE;
if (matchtoken('=')) {
sym->usage &= ~uDEFINE; /* temporarily mark the variable as undefined to prevent
* possible self-assignment through its initialization expression */
Expand Down Expand Up @@ -2416,6 +2420,8 @@ static int declloc(int fstatic)
} /* if */
} /* if */
} /* if */
if (explicit_init)
markinitialized(sym);
} while (matchtoken(',')); /* enddo */ /* more? */
needtoken(tTERM); /* if not comma, must be semicolumn */
return ident;
Expand Down Expand Up @@ -2496,14 +2502,16 @@ static int base;
* Global references: litidx (altered)
*/
static void initials(int ident,int tag,cell *size,int dim[],int numdim,
constvalue_root *enumroot)
constvalue_root *enumroot,int *explicit_init)
{
int ctag;
cell tablesize;
int curlit=litidx;
int err=0;
int i;

if (explicit_init!=NULL)
*explicit_init=FALSE;
if (!matchtoken('=')) {
assert(ident!=iARRAY || numdim>0);
if (ident==iARRAY) {
Expand Down Expand Up @@ -2534,6 +2542,8 @@ static void initials(int ident,int tag,cell *size,int dim[],int numdim,
return;
} /* if */

if (explicit_init!=NULL)
*explicit_init=TRUE;
if (ident==iVARIABLE) {
assert(*size==1);
init(ident,&ctag,NULL);
Expand Down Expand Up @@ -4160,7 +4170,7 @@ static void doarg(char *name,int ident,int offset,int tags[],int numtags,
lexpush(); /* initials() needs the "=" token again */
assert(litidx==0); /* at the start of a function, this is reset */
assert(numtags>0);
initials(ident,tags[0],&size,arg->dim,arg->numdim,enumroot);
initials(ident,tags[0],&size,arg->dim,arg->numdim,enumroot,NULL);
assert(size>=litidx);
/* allocate memory to hold the initial values */
arg->defvalue.array.data=(cell *)malloc(litidx*sizeof(cell));
Expand Down Expand Up @@ -4237,14 +4247,15 @@ static void doarg(char *name,int ident,int offset,int tags[],int numtags,
assert(numtags>0);
argsym=addvariable(name,offset,ident,sLOCAL,tags[0],
arg->dim,arg->numdim,arg->idxtag,0);
markinitialized(argsym);
if (fpublic) {
argsym->usage|=uREAD; /* arguments of public functions are always "used" */
if(argsym->ident==iREFARRAY || argsym->ident==iREFERENCE)
argsym->usage|=uWRITTEN;
}
argsym->usage |= uREAD; /* arguments of public functions are always "used" */
if (argsym->ident==iREFARRAY || argsym->ident==iREFERENCE)
argsym->usage |= uWRITTEN;
} /* if */

if (fconst)
argsym->usage|=uCONST;
argsym->usage |= uCONST;
} /* if */
}

Expand Down Expand Up @@ -5240,9 +5251,9 @@ SC_FUNC symbol *add_builtin_string_constant(char *name,const char *val,
glb_declared+=litidx;
dumplits();
litidx=0;
}
sym->usage|=uDEFINE;
sym->flags|=flagPREDEF;
} /* if */
sym->usage |= (uDEFINE | uEXPLINIT);
sym->flags |= flagPREDEF;
return sym;
}

Expand Down Expand Up @@ -5719,8 +5730,13 @@ static int dofor(void)
/* The variable in expr1 of the for loop is at a
* 'compound statement' level of it own.
*/
symbol *sym;
nestlevel++;
declloc(FALSE); /* declare local variable */
for (sym=loctab.next; sym!=NULL; sym=sym->next) {
if (sym->compound==nestlevel)
markinitialized(sym);
} /* for */
} else {
doexpr(TRUE,TRUE,TRUE,TRUE,NULL,NULL,FALSE); /* expression 1 */
needtoken(';');
Expand Down Expand Up @@ -6094,6 +6110,7 @@ static int SC_FASTCALL emit_getlval(int *identptr,emit_outval *p,int *islocal, r
error(17,str); /* undefined symbol */
return FALSE;
} /* if */
markinitialized(sym);
markusage(sym,uREAD | uWRITTEN);

p->type=eotNUMBER;
Expand Down Expand Up @@ -6490,6 +6507,7 @@ static void SC_FASTCALL emit_param_data(emit_outval *p)
case tSYMBOL:
sym=findloc(str);
if (sym!=NULL) {
markinitialized(sym);
markusage(sym,uREAD | uWRITTEN);
if (sym->ident==iLABEL) {
tok=tLABEL;
Expand All @@ -6508,6 +6526,7 @@ static void SC_FASTCALL emit_param_data(emit_outval *p)
error(17,str); /* undefined symbol */
return;
} /* if */
markinitialized(sym);
markusage(sym,(sym->ident==iFUNCTN || sym->ident==iREFFUNC) ? uREAD : (uREAD | uWRITTEN));
if (sym->ident==iFUNCTN || sym->ident==iREFFUNC) {
tok=((sym->usage & uNATIVE)!=0) ? teNATIVE : teFUNCTN;
Expand Down Expand Up @@ -6548,6 +6567,7 @@ static void SC_FASTCALL emit_param_local(emit_outval *p,int allow_ref)
case tSYMBOL:
sym=findloc(str);
if (sym!=NULL) {
markinitialized(sym);
markusage(sym,uREAD | uWRITTEN);
if (sym->ident==iLABEL) {
tok=tLABEL;
Expand All @@ -6571,6 +6591,7 @@ static void SC_FASTCALL emit_param_local(emit_outval *p,int allow_ref)
error(17,str); /* undefined symbol */
return;
} /* if */
markinitialized(sym);
markusage(sym,(sym->ident==iFUNCTN || sym->ident==iREFFUNC) ? uREAD : (uREAD | uWRITTEN));
if (sym->ident!=iCONSTEXPR) {
if (sym->ident==iFUNCTN || sym->ident==iREFFUNC)
Expand Down
33 changes: 33 additions & 0 deletions source/compiler/sc2.c
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,7 @@ static int command(void)
} else {
outval(sym->addr,FALSE);
/* mark symbol as "used", unknown whether for read or write */
markinitialized(sym);
markusage(sym,uREAD | uWRITTEN);
} /* if */
code_idx+=opargs(1);
Expand Down Expand Up @@ -3160,6 +3161,8 @@ SC_FUNC void markusage(symbol *sym,int usage)
sym->usage |= (char)usage;
if ((usage & uWRITTEN)!=0)
sym->lnumber=fline;
if ((usage & uREAD)!=0)
checkinitialized(sym);
/* check if (global) reference must be added to the symbol */
if ((usage & (uREAD | uWRITTEN))!=0) {
/* only do this for global symbols */
Expand All @@ -3174,6 +3177,36 @@ SC_FUNC void markusage(symbol *sym,int usage)
} /* if */
}

SC_FUNC void markinitialized(symbol *sym)
{
symbol *cursym;
assert(sym!=NULL);
if (sym->ident!=iVARIABLE && sym->ident!=iARRAY)
return;
if (sc_status==statFIRST && (sym->vclass==sLOCAL || sym->vclass==sSTATIC))
return;
cursym=sym;
do {
cursym->usage |= uEXPLINIT;
} while ((cursym=cursym->child)!=NULL);
cursym=sym;
while ((cursym=cursym->parent)!=NULL)
cursym->usage |= uEXPLINIT;
}

SC_FUNC void checkinitialized(symbol *sym)
{
assert(sym!=NULL);
if (sc_status==statFIRST)
return;
if (sym->ident!=iVARIABLE && sym->ident!=iARRAY)
return;
if ((sym->usage & uDEFINE)==0 || (sym->usage & uEXPLINIT)!=0)
return;
error(210,sym->name); /* possible use of symbol before initialization */
markinitialized(sym); /* don't issue the same error for this symbol again */
}


/* findglb
*
Expand Down
Loading