Skip to content

Commit c04bbe8

Browse files
committed
Implement warning 210
1 parent ad730d3 commit c04bbe8

File tree

4 files changed

+131
-32
lines changed

4 files changed

+131
-32
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: 34 additions & 17 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=FALSE;
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,12 +2271,14 @@ 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 {
22742278
ident=iVARIABLE;
22752279
size=1;
22762280
numdim=0; /* no dimensions */
2281+
explicit_init=FALSE;
22772282
tag=pc_addtag(NULL);
22782283
if (!needtoken(tSYMBOL)) {
22792284
lexclr(TRUE); /* drop the rest of the line... */
@@ -2318,7 +2323,7 @@ static int declloc(int fstatic)
23182323
sc_alignnext=FALSE;
23192324
} /* if */
23202325
cur_lit=litidx; /* save current index in the literal table */
2321-
initials(ident,tag,&size,dim,numdim,enumroot);
2326+
initials(ident,tag,&size,dim,numdim,enumroot,&explicit_init);
23222327
if (size==0)
23232328
return ident; /* error message already given */
23242329
if (numdim==1)
@@ -2357,7 +2362,6 @@ static int declloc(int fstatic)
23572362
if (ident==iVARIABLE) {
23582363
/* simple variable, also supports initialization */
23592364
int ctag = tag; /* set to "tag" by default */
2360-
int explicit_init=FALSE;/* is the variable explicitly initialized? */
23612365
if (matchtoken('=')) {
23622366
sym->usage &= ~uDEFINE; /* temporarily mark the variable as undefined to prevent
23632367
* possible self-assignment through its initialization expression */
@@ -2379,10 +2383,13 @@ static int declloc(int fstatic)
23792383
stgout(staging_start);
23802384
stgset(FALSE);
23812385
check_tagmismatch(tag,ctag,TRUE,-1);
2382-
/* if the variable was not explicitly initialized, reset the
2383-
* "uWRITTEN" flag that store() set */
2384-
if (!explicit_init)
2386+
if (explicit_init) {
2387+
sym->usage |= uEXPLINIT;
2388+
} else {
2389+
/* if the variable was not explicitly initialized, reset the
2390+
* "uWRITTEN" flag that store() set */
23852391
sym->usage &= ~uWRITTEN;
2392+
} /* if */
23862393
} else {
23872394
/* an array */
23882395
assert(cur_lit>=0 && cur_lit<=litidx && litidx<=litmax);
@@ -2416,6 +2423,8 @@ static int declloc(int fstatic)
24162423
} /* if */
24172424
} /* if */
24182425
} /* if */
2426+
if ((ident==iARRAY || fstatic) && explicit_init)
2427+
markinitialized(sym);
24192428
} while (matchtoken(',')); /* enddo */ /* more? */
24202429
needtoken(tTERM); /* if not comma, must be semicolumn */
24212430
return ident;
@@ -2496,7 +2505,7 @@ static int base;
24962505
* Global references: litidx (altered)
24972506
*/
24982507
static void initials(int ident,int tag,cell *size,int dim[],int numdim,
2499-
constvalue_root *enumroot)
2508+
constvalue_root *enumroot,int *explicit_init)
25002509
{
25012510
int ctag;
25022511
cell tablesize;
@@ -2534,6 +2543,8 @@ static void initials(int ident,int tag,cell *size,int dim[],int numdim,
25342543
return;
25352544
} /* if */
25362545

2546+
if (explicit_init!=NULL)
2547+
*explicit_init=TRUE;
25372548
if (ident==iVARIABLE) {
25382549
assert(*size==1);
25392550
init(ident,&ctag,NULL);
@@ -4160,7 +4171,7 @@ static void doarg(char *name,int ident,int offset,int tags[],int numtags,
41604171
lexpush(); /* initials() needs the "=" token again */
41614172
assert(litidx==0); /* at the start of a function, this is reset */
41624173
assert(numtags>0);
4163-
initials(ident,tags[0],&size,arg->dim,arg->numdim,enumroot);
4174+
initials(ident,tags[0],&size,arg->dim,arg->numdim,enumroot,NULL);
41644175
assert(size>=litidx);
41654176
/* allocate memory to hold the initial values */
41664177
arg->defvalue.array.data=(cell *)malloc(litidx*sizeof(cell));
@@ -4237,14 +4248,15 @@ static void doarg(char *name,int ident,int offset,int tags[],int numtags,
42374248
assert(numtags>0);
42384249
argsym=addvariable(name,offset,ident,sLOCAL,tags[0],
42394250
arg->dim,arg->numdim,arg->idxtag,0);
4251+
markinitialized(argsym);
42404252
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-
}
4253+
argsym->usage |= uREAD; /* arguments of public functions are always "used" */
4254+
if (argsym->ident==iREFARRAY || argsym->ident==iREFERENCE)
4255+
argsym->usage |= uWRITTEN;
4256+
} /* if */
42454257

42464258
if (fconst)
4247-
argsym->usage|=uCONST;
4259+
argsym->usage |= uCONST;
42484260
} /* if */
42494261
}
42504262

@@ -5240,9 +5252,9 @@ SC_FUNC symbol *add_builtin_string_constant(char *name,const char *val,
52405252
glb_declared+=litidx;
52415253
dumplits();
52425254
litidx=0;
5243-
}
5244-
sym->usage|=uDEFINE;
5245-
sym->flags|=flagPREDEF;
5255+
} /* if */
5256+
sym->usage |= (uDEFINE | uEXPLINIT);
5257+
sym->flags |= flagPREDEF;
52465258
return sym;
52475259
}
52485260

@@ -6094,6 +6106,7 @@ static int SC_FASTCALL emit_getlval(int *identptr,emit_outval *p,int *islocal, r
60946106
error(17,str); /* undefined symbol */
60956107
return FALSE;
60966108
} /* if */
6109+
markinitialized(sym);
60976110
markusage(sym,uREAD | uWRITTEN);
60986111

60996112
p->type=eotNUMBER;
@@ -6490,6 +6503,7 @@ static void SC_FASTCALL emit_param_data(emit_outval *p)
64906503
case tSYMBOL:
64916504
sym=findloc(str);
64926505
if (sym!=NULL) {
6506+
markinitialized(sym);
64936507
markusage(sym,uREAD | uWRITTEN);
64946508
if (sym->ident==iLABEL) {
64956509
tok=tLABEL;
@@ -6508,6 +6522,7 @@ static void SC_FASTCALL emit_param_data(emit_outval *p)
65086522
error(17,str); /* undefined symbol */
65096523
return;
65106524
} /* if */
6525+
markinitialized(sym);
65116526
markusage(sym,(sym->ident==iFUNCTN || sym->ident==iREFFUNC) ? uREAD : (uREAD | uWRITTEN));
65126527
if (sym->ident==iFUNCTN || sym->ident==iREFFUNC) {
65136528
tok=((sym->usage & uNATIVE)!=0) ? teNATIVE : teFUNCTN;
@@ -6548,6 +6563,7 @@ static void SC_FASTCALL emit_param_local(emit_outval *p,int allow_ref)
65486563
case tSYMBOL:
65496564
sym=findloc(str);
65506565
if (sym!=NULL) {
6566+
markinitialized(sym);
65516567
markusage(sym,uREAD | uWRITTEN);
65526568
if (sym->ident==iLABEL) {
65536569
tok=tLABEL;
@@ -6571,6 +6587,7 @@ static void SC_FASTCALL emit_param_local(emit_outval *p,int allow_ref)
65716587
error(17,str); /* undefined symbol */
65726588
return;
65736589
} /* if */
6590+
markinitialized(sym);
65746591
markusage(sym,(sym->ident==iFUNCTN || sym->ident==iREFFUNC) ? uREAD : (uREAD | uWRITTEN));
65756592
if (sym->ident!=iCONSTEXPR) {
65766593
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)