Skip to content

Commit 1102bc3

Browse files
committed
Implement warning 210
1 parent ad730d3 commit 1102bc3

File tree

4 files changed

+122
-28
lines changed

4 files changed

+122
-28
lines changed

source/compiler/sc.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,11 @@ typedef struct s_symbol {
241241
* used during parsing a function, to detect a mix of "return;" and
242242
* "return value;" in a few special cases.
243243
*/
244-
#define uRETNONE 0x10
244+
#define uRETNONE 0x010
245+
/* uEXPLINIT is set when a variable/array is explicitly initialized at
246+
* definition or assigned a value. This flag differs from uWRITTEN
247+
* because it doesn't mean the variable/array has been used somewhere. */
248+
#define uEXPLINIT 0x020
245249

246250
#define flagDEPRECATED 0x01 /* symbol is deprecated (avoid use) */
247251
#define flagNAKED 0x10 /* function is naked */
@@ -650,6 +654,8 @@ SC_FUNC void delete_symbol(symbol *root,symbol *sym);
650654
SC_FUNC void delete_symbols(symbol *root,int level,int del_labels,int delete_functions);
651655
SC_FUNC int refer_symbol(symbol *entry,symbol *bywhom);
652656
SC_FUNC void markusage(symbol *sym,int usage);
657+
SC_FUNC void markinitialized(symbol *sym);
658+
SC_FUNC void checkinitialized(symbol *sym);
653659
SC_FUNC void rename_symbol(symbol *sym,const char *newname);
654660
SC_FUNC symbol *findglb(const char *name,int filter);
655661
SC_FUNC symbol *findloc(const char *name);

source/compiler/sc1.c

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ static void decl_const(int table);
9595
static void decl_enum(int table,int fstatic);
9696
static cell needsub(int *tag,constvalue_root **enumroot);
9797
static void initials(int ident,int tag,cell *size,int dim[],int numdim,
98-
constvalue_root *enumroot);
98+
constvalue_root *enumroot,int *explicit_init);
9999
static cell initarray(int ident,int tag,int dim[],int numdim,int cur,
100100
int startlit,int counteddim[],constvalue_root *lastdim,
101101
constvalue_root *enumroot,int *errorfound);
@@ -2004,6 +2004,7 @@ static void declglb(char *firstname,int firsttag,int fpublic,int fstatic,int fst
20042004
char *str;
20052005
int dim[sDIMEN_MAX];
20062006
int numdim;
2007+
int explicit_init;
20072008
short filenum;
20082009
symbol *sym;
20092010
constvalue_root *enumroot=NULL;
@@ -2108,7 +2109,7 @@ static void declglb(char *firstname,int firsttag,int fpublic,int fstatic,int fst
21082109
litidx=0; /* global initial data is dumped, so restart at zero */
21092110
} /* if */
21102111
assert(litidx==0); /* literal queue should be empty (again) */
2111-
initials(ident,tag,&size,dim,numdim,enumroot);/* stores values in the literal queue */
2112+
initials(ident,tag,&size,dim,numdim,enumroot,&explicit_init);/* stores values in the literal queue */
21122113
assert(size>=litidx);
21132114
if (numdim==1)
21142115
dim[0]=(int)size;
@@ -2232,6 +2233,8 @@ static void declglb(char *firstname,int firsttag,int fpublic,int fstatic,int fst
22322233
sym->usage|=uSTOCK;
22332234
if (fstatic)
22342235
sym->fnumber=filenum;
2236+
if (explicit_init)
2237+
markinitialized(sym);
22352238
sc_attachdocumentation(sym);/* attach any documenation to the variable */
22362239
if (sc_status==statSKIP) {
22372240
sc_status=statWRITE;
@@ -2268,6 +2271,7 @@ static int declloc(int fstatic)
22682271
int numdim;
22692272
int fconst;
22702273
int staging_start;
2274+
int explicit_init; /* is the variable explicitly initialized? */
22712275

22722276
fconst=matchtoken(tCONST);
22732277
do {
@@ -2318,7 +2322,7 @@ static int declloc(int fstatic)
23182322
sc_alignnext=FALSE;
23192323
} /* if */
23202324
cur_lit=litidx; /* save current index in the literal table */
2321-
initials(ident,tag,&size,dim,numdim,enumroot);
2325+
initials(ident,tag,&size,dim,numdim,enumroot,&explicit_init);
23222326
if (size==0)
23232327
return ident; /* error message already given */
23242328
if (numdim==1)
@@ -2357,7 +2361,7 @@ static int declloc(int fstatic)
23572361
if (ident==iVARIABLE) {
23582362
/* simple variable, also supports initialization */
23592363
int ctag = tag; /* set to "tag" by default */
2360-
int explicit_init=FALSE;/* is the variable explicitly initialized? */
2364+
explicit_init=FALSE;
23612365
if (matchtoken('=')) {
23622366
sym->usage &= ~uDEFINE; /* temporarily mark the variable as undefined to prevent
23632367
* possible self-assignment through its initialization expression */
@@ -2416,6 +2420,8 @@ static int declloc(int fstatic)
24162420
} /* if */
24172421
} /* if */
24182422
} /* if */
2423+
if (explicit_init)
2424+
markinitialized(sym);
24192425
} while (matchtoken(',')); /* enddo */ /* more? */
24202426
needtoken(tTERM); /* if not comma, must be semicolumn */
24212427
return ident;
@@ -2496,14 +2502,16 @@ static int base;
24962502
* Global references: litidx (altered)
24972503
*/
24982504
static void initials(int ident,int tag,cell *size,int dim[],int numdim,
2499-
constvalue_root *enumroot)
2505+
constvalue_root *enumroot,int *explicit_init)
25002506
{
25012507
int ctag;
25022508
cell tablesize;
25032509
int curlit=litidx;
25042510
int err=0;
25052511
int i;
25062512

2513+
if (explicit_init!=NULL)
2514+
*explicit_init=FALSE;
25072515
if (!matchtoken('=')) {
25082516
assert(ident!=iARRAY || numdim>0);
25092517
if (ident==iARRAY) {
@@ -2534,6 +2542,8 @@ static void initials(int ident,int tag,cell *size,int dim[],int numdim,
25342542
return;
25352543
} /* if */
25362544

2545+
if (explicit_init!=NULL)
2546+
*explicit_init=TRUE;
25372547
if (ident==iVARIABLE) {
25382548
assert(*size==1);
25392549
init(ident,&ctag,NULL);
@@ -4160,7 +4170,7 @@ static void doarg(char *name,int ident,int offset,int tags[],int numtags,
41604170
lexpush(); /* initials() needs the "=" token again */
41614171
assert(litidx==0); /* at the start of a function, this is reset */
41624172
assert(numtags>0);
4163-
initials(ident,tags[0],&size,arg->dim,arg->numdim,enumroot);
4173+
initials(ident,tags[0],&size,arg->dim,arg->numdim,enumroot,NULL);
41644174
assert(size>=litidx);
41654175
/* allocate memory to hold the initial values */
41664176
arg->defvalue.array.data=(cell *)malloc(litidx*sizeof(cell));
@@ -4237,14 +4247,15 @@ static void doarg(char *name,int ident,int offset,int tags[],int numtags,
42374247
assert(numtags>0);
42384248
argsym=addvariable(name,offset,ident,sLOCAL,tags[0],
42394249
arg->dim,arg->numdim,arg->idxtag,0);
4250+
markinitialized(argsym);
42404251
if (fpublic) {
4241-
argsym->usage|=uREAD; /* arguments of public functions are always "used" */
4242-
if(argsym->ident==iREFARRAY || argsym->ident==iREFERENCE)
4243-
argsym->usage|=uWRITTEN;
4244-
}
4252+
argsym->usage |= uREAD; /* arguments of public functions are always "used" */
4253+
if (argsym->ident==iREFARRAY || argsym->ident==iREFERENCE)
4254+
argsym->usage |= uWRITTEN;
4255+
} /* if */
42454256

42464257
if (fconst)
4247-
argsym->usage|=uCONST;
4258+
argsym->usage |= uCONST;
42484259
} /* if */
42494260
}
42504261

@@ -5240,9 +5251,9 @@ SC_FUNC symbol *add_builtin_string_constant(char *name,const char *val,
52405251
glb_declared+=litidx;
52415252
dumplits();
52425253
litidx=0;
5243-
}
5244-
sym->usage|=uDEFINE;
5245-
sym->flags|=flagPREDEF;
5254+
} /* if */
5255+
sym->usage |= (uDEFINE | uEXPLINIT);
5256+
sym->flags |= flagPREDEF;
52465257
return sym;
52475258
}
52485259

@@ -6094,6 +6105,7 @@ static int SC_FASTCALL emit_getlval(int *identptr,emit_outval *p,int *islocal, r
60946105
error(17,str); /* undefined symbol */
60956106
return FALSE;
60966107
} /* if */
6108+
markinitialized(sym);
60976109
markusage(sym,uREAD | uWRITTEN);
60986110

60996111
p->type=eotNUMBER;
@@ -6490,6 +6502,7 @@ static void SC_FASTCALL emit_param_data(emit_outval *p)
64906502
case tSYMBOL:
64916503
sym=findloc(str);
64926504
if (sym!=NULL) {
6505+
markinitialized(sym);
64936506
markusage(sym,uREAD | uWRITTEN);
64946507
if (sym->ident==iLABEL) {
64956508
tok=tLABEL;
@@ -6508,6 +6521,7 @@ static void SC_FASTCALL emit_param_data(emit_outval *p)
65086521
error(17,str); /* undefined symbol */
65096522
return;
65106523
} /* if */
6524+
markinitialized(sym);
65116525
markusage(sym,(sym->ident==iFUNCTN || sym->ident==iREFFUNC) ? uREAD : (uREAD | uWRITTEN));
65126526
if (sym->ident==iFUNCTN || sym->ident==iREFFUNC) {
65136527
tok=((sym->usage & uNATIVE)!=0) ? teNATIVE : teFUNCTN;
@@ -6548,6 +6562,7 @@ static void SC_FASTCALL emit_param_local(emit_outval *p,int allow_ref)
65486562
case tSYMBOL:
65496563
sym=findloc(str);
65506564
if (sym!=NULL) {
6565+
markinitialized(sym);
65516566
markusage(sym,uREAD | uWRITTEN);
65526567
if (sym->ident==iLABEL) {
65536568
tok=tLABEL;
@@ -6571,6 +6586,7 @@ static void SC_FASTCALL emit_param_local(emit_outval *p,int allow_ref)
65716586
error(17,str); /* undefined symbol */
65726587
return;
65736588
} /* if */
6589+
markinitialized(sym);
65746590
markusage(sym,(sym->ident==iFUNCTN || sym->ident==iREFFUNC) ? uREAD : (uREAD | uWRITTEN));
65756591
if (sym->ident!=iCONSTEXPR) {
65766592
if (sym->ident==iFUNCTN || sym->ident==iREFFUNC)

source/compiler/sc2.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,7 @@ static int command(void)
14211421
} else {
14221422
outval(sym->addr,FALSE);
14231423
/* mark symbol as "used", unknown whether for read or write */
1424+
markinitialized(sym);
14241425
markusage(sym,uREAD | uWRITTEN);
14251426
} /* if */
14261427
code_idx+=opargs(1);
@@ -3160,6 +3161,8 @@ SC_FUNC void markusage(symbol *sym,int usage)
31603161
sym->usage |= (char)usage;
31613162
if ((usage & uWRITTEN)!=0)
31623163
sym->lnumber=fline;
3164+
if ((usage & uREAD)!=0)
3165+
checkinitialized(sym);
31633166
/* check if (global) reference must be added to the symbol */
31643167
if ((usage & (uREAD | uWRITTEN))!=0) {
31653168
/* only do this for global symbols */
@@ -3174,6 +3177,36 @@ SC_FUNC void markusage(symbol *sym,int usage)
31743177
} /* if */
31753178
}
31763179

3180+
SC_FUNC void markinitialized(symbol *sym)
3181+
{
3182+
symbol *cursym;
3183+
assert(sym!=NULL);
3184+
if (sym->ident!=iVARIABLE && sym->ident!=iARRAY)
3185+
return;
3186+
if (sc_status==statFIRST && (sym->vclass==sLOCAL || sym->vclass==sSTATIC))
3187+
return;
3188+
cursym=sym;
3189+
do {
3190+
cursym->usage |= uEXPLINIT;
3191+
} while ((cursym=cursym->child)!=NULL);
3192+
cursym=sym;
3193+
while ((cursym=cursym->parent)!=NULL)
3194+
cursym->usage |= uEXPLINIT;
3195+
}
3196+
3197+
SC_FUNC void checkinitialized(symbol *sym)
3198+
{
3199+
assert(sym!=NULL);
3200+
if (sc_status==statFIRST)
3201+
return;
3202+
if (sym->ident!=iVARIABLE && sym->ident!=iARRAY)
3203+
return;
3204+
if ((sym->usage & uDEFINE)==0 || (sym->usage & uEXPLINIT)!=0)
3205+
return;
3206+
error(210,sym->name); /* possible use of symbol before initialization */
3207+
markinitialized(sym); /* don't issue the same error for this symbol again */
3208+
}
3209+
31773210

31783211
/* findglb
31793212
*

0 commit comments

Comments
 (0)