Skip to content

Commit 22ce305

Browse files
committed
Implement warning 210
1 parent ad730d3 commit 22ce305

File tree

4 files changed

+74
-28
lines changed

4 files changed

+74
-28
lines changed

Diff for: source/compiler/sc.h

+1
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ typedef struct s_symbol {
232232
#define uPUBLIC 0x010
233233
#define uNATIVE 0x020
234234
#define uENUMROOT 0x020
235+
#define uEXPLINIT 0x020 /* variable/array is explicitly initialized */
235236
#define uSTOCK 0x040
236237
#define uENUMFIELD 0x040
237238
#define uMISSING 0x080

Diff for: source/compiler/sc1.c

+36-15
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,12 @@ 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+
symbol *cursym=sym;
2238+
do {
2239+
cursym->usage |= uEXPLINIT;
2240+
} while ((cursym=cursym->child)!=NULL);
2241+
} /* if */
22352242
sc_attachdocumentation(sym);/* attach any documenation to the variable */
22362243
if (sc_status==statSKIP) {
22372244
sc_status=statWRITE;
@@ -2268,12 +2275,14 @@ static int declloc(int fstatic)
22682275
int numdim;
22692276
int fconst;
22702277
int staging_start;
2278+
int explicit_init; /* is the variable explicitly initialized? */
22712279

22722280
fconst=matchtoken(tCONST);
22732281
do {
22742282
ident=iVARIABLE;
22752283
size=1;
22762284
numdim=0; /* no dimensions */
2285+
explicit_init=FALSE;
22772286
tag=pc_addtag(NULL);
22782287
if (!needtoken(tSYMBOL)) {
22792288
lexclr(TRUE); /* drop the rest of the line... */
@@ -2318,7 +2327,7 @@ static int declloc(int fstatic)
23182327
sc_alignnext=FALSE;
23192328
} /* if */
23202329
cur_lit=litidx; /* save current index in the literal table */
2321-
initials(ident,tag,&size,dim,numdim,enumroot);
2330+
initials(ident,tag,&size,dim,numdim,enumroot,&explicit_init);
23222331
if (size==0)
23232332
return ident; /* error message already given */
23242333
if (numdim==1)
@@ -2357,7 +2366,6 @@ static int declloc(int fstatic)
23572366
if (ident==iVARIABLE) {
23582367
/* simple variable, also supports initialization */
23592368
int ctag = tag; /* set to "tag" by default */
2360-
int explicit_init=FALSE;/* is the variable explicitly initialized? */
23612369
if (matchtoken('=')) {
23622370
sym->usage &= ~uDEFINE; /* temporarily mark the variable as undefined to prevent
23632371
* possible self-assignment through its initialization expression */
@@ -2416,6 +2424,12 @@ static int declloc(int fstatic)
24162424
} /* if */
24172425
} /* if */
24182426
} /* if */
2427+
if ((ident == iARRAY || fstatic) && explicit_init) {
2428+
symbol *cursym=sym;
2429+
do {
2430+
cursym->usage |= uEXPLINIT;
2431+
} while ((cursym=cursym->child)!=NULL);
2432+
} /* if */
24192433
} while (matchtoken(',')); /* enddo */ /* more? */
24202434
needtoken(tTERM); /* if not comma, must be semicolumn */
24212435
return ident;
@@ -2496,7 +2510,7 @@ static int base;
24962510
* Global references: litidx (altered)
24972511
*/
24982512
static void initials(int ident,int tag,cell *size,int dim[],int numdim,
2499-
constvalue_root *enumroot)
2513+
constvalue_root *enumroot,int *explicit_init)
25002514
{
25012515
int ctag;
25022516
cell tablesize;
@@ -2533,7 +2547,9 @@ static void initials(int ident,int tag,cell *size,int dim[],int numdim,
25332547
} /* if */
25342548
return;
25352549
} /* if */
2536-
2550+
2551+
if (explicit_init!=NULL)
2552+
*explicit_init=TRUE;
25372553
if (ident==iVARIABLE) {
25382554
assert(*size==1);
25392555
init(ident,&ctag,NULL);
@@ -4160,7 +4176,7 @@ static void doarg(char *name,int ident,int offset,int tags[],int numtags,
41604176
lexpush(); /* initials() needs the "=" token again */
41614177
assert(litidx==0); /* at the start of a function, this is reset */
41624178
assert(numtags>0);
4163-
initials(ident,tags[0],&size,arg->dim,arg->numdim,enumroot);
4179+
initials(ident,tags[0],&size,arg->dim,arg->numdim,enumroot,NULL);
41644180
assert(size>=litidx);
41654181
/* allocate memory to hold the initial values */
41664182
arg->defvalue.array.data=(cell *)malloc(litidx*sizeof(cell));
@@ -4231,20 +4247,25 @@ static void doarg(char *name,int ident,int offset,int tags[],int numtags,
42314247
if (argsym!=NULL) {
42324248
error(21,name); /* symbol already defined */
42334249
} else {
4250+
symbol *cursym;
42344251
if (chkshadow && (argsym=findglb(name,sSTATEVAR))!=NULL && argsym->ident!=iFUNCTN)
42354252
error(219,name); /* variable shadows another symbol */
42364253
/* add details of type and address */
42374254
assert(numtags>0);
42384255
argsym=addvariable(name,offset,ident,sLOCAL,tags[0],
42394256
arg->dim,arg->numdim,arg->idxtag,0);
4257+
cursym=argsym;
4258+
do {
4259+
cursym->usage |= uEXPLINIT;
4260+
} while ((cursym=cursym->child)!=NULL);
42404261
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-
}
4262+
argsym->usage |= uREAD; /* arguments of public functions are always "used" */
4263+
if (argsym->ident==iREFARRAY || argsym->ident==iREFERENCE)
4264+
argsym->usage |= uWRITTEN;
4265+
} /* if */
42454266

42464267
if (fconst)
4247-
argsym->usage|=uCONST;
4268+
argsym->usage |= uCONST;
42484269
} /* if */
42494270
}
42504271

@@ -5240,9 +5261,9 @@ SC_FUNC symbol *add_builtin_string_constant(char *name,const char *val,
52405261
glb_declared+=litidx;
52415262
dumplits();
52425263
litidx=0;
5243-
}
5244-
sym->usage|=uDEFINE;
5245-
sym->flags|=flagPREDEF;
5264+
} /* if */
5265+
sym->usage |= (uDEFINE | uEXPLINIT);
5266+
sym->flags |= flagPREDEF;
52465267
return sym;
52475268
}
52485269

Diff for: source/compiler/sc2.c

+10
Original file line numberDiff line numberDiff line change
@@ -3160,6 +3160,16 @@ SC_FUNC void markusage(symbol *sym,int usage)
31603160
sym->usage |= (char)usage;
31613161
if ((usage & uWRITTEN)!=0)
31623162
sym->lnumber=fline;
3163+
if ((usage & uREAD)!=0 && sc_status!=statFIRST) {
3164+
switch (sym->ident) {
3165+
case iVARIABLE:
3166+
case iARRAY:
3167+
if ((sym->usage & (uEXPLINIT | uWRITTEN))==0) {
3168+
error(210,sym->name); /* possible use of symbol before initialization */
3169+
sym->usage |= uEXPLINIT;/* don't issue the same error for this symbol again */
3170+
} /* if */
3171+
} /* switch */
3172+
} /* if */
31633173
/* check if (global) reference must be added to the symbol */
31643174
if ((usage & (uREAD | uWRITTEN))!=0) {
31653175
/* only do this for global symbols */

Diff for: source/compiler/sc3.c

+27-13
Original file line numberDiff line numberDiff line change
@@ -1839,6 +1839,9 @@ static int primary(value *lval)
18391839
lval->ident=sym->ident;
18401840
lval->tag=sym->tag;
18411841
if (sym->ident==iARRAY || sym->ident==iREFARRAY) {
1842+
/* mark the symbol as initialized before obtaining its address in order to
1843+
* avoid warning 210 ("possible use of symbol before initialization") */
1844+
sym->usage |= uEXPLINIT;
18421845
address(sym,sPRI); /* get starting address in primary register */
18431846
return FALSE; /* return 0 for array (not lvalue) */
18441847
} else {
@@ -1861,6 +1864,9 @@ static int primary(value *lval)
18611864
lval->ident=sym->ident;
18621865
lval->tag=sym->tag;
18631866
if (sym->ident==iARRAY || sym->ident==iREFARRAY) {
1867+
/* mark the symbol as initialized before obtaining its address in order to
1868+
* avoid warning 210 ("possible use of symbol before initialization") */
1869+
sym->usage |= uEXPLINIT;
18641870
address(sym,sPRI); /* get starting address in primary register */
18651871
return FALSE; /* return 0 for array (not lvalue) */
18661872
} else {
@@ -2108,20 +2114,19 @@ static int nesting=0;
21082114
if (arg[argidx].ident!=0 && arg[argidx].numtags==1)
21092115
lval.cmptag=arg[argidx].tags[0]; /* set the expected tag, if any */
21102116
lvalue=hier14(&lval);
2111-
/* Mark the symbol as "read" so it won't be omitted from P-code.
2112-
* Native functions are marked as read at the point of their call,
2113-
* so we don't handle them here; see ffcall().
2114-
*/
2115-
if (lval.sym!=NULL && (lval.sym->usage & uNATIVE)==0) {
2116-
markusage(lval.sym,uREAD);
2117-
} /* if */
21182117
assert(sc_status==statFIRST || arg[argidx].ident==0 || arg[argidx].tags!=NULL);
21192118
switch (arg[argidx].ident) {
21202119
case 0:
21212120
error(202); /* argument count mismatch */
21222121
break;
21232122
case iVARARGS:
21242123
/* always pass by reference */
2124+
if (lval.sym!=NULL) {
2125+
symbol *cursym=lval.sym;
2126+
do {
2127+
markusage(cursym,uWRITTEN);
2128+
} while ((cursym=cursym->child)!=NULL);
2129+
} /* if */
21252130
if (lval.ident==iVARIABLE || lval.ident==iREFERENCE) {
21262131
assert(lval.sym!=NULL);
21272132
if ((lval.sym->usage & uCONST)!=0 && (arg[argidx].usage & uCONST)==0) {
@@ -2156,8 +2161,6 @@ static int nesting=0;
21562161
} /* if */
21572162
/* ??? handle const array passed by reference */
21582163
/* otherwise, the address is already in PRI */
2159-
if (lval.sym!=NULL)
2160-
markusage(lval.sym,uWRITTEN);
21612164
check_tagmismatch_multiple(arg[argidx].tags,arg[argidx].numtags,lval.tag,-1);
21622165
if (lval.tag!=0)
21632166
append_constval(&taglst,arg[argidx].name,lval.tag,0);
@@ -2181,6 +2184,8 @@ static int nesting=0;
21812184
error(35,argidx+1); /* argument type mismatch */
21822185
if (lval.sym!=NULL && (lval.sym->usage & uCONST)!=0 && (arg[argidx].usage & uCONST)==0)
21832186
error(35,argidx+1); /* argument type mismatch */
2187+
if (lval.sym!=NULL)
2188+
markusage(lval.sym,uWRITTEN);
21842189
if (lval.ident==iVARIABLE || lval.ident==iREFERENCE) {
21852190
if (lvalue) {
21862191
assert(lval.sym!=NULL);
@@ -2196,8 +2201,6 @@ static int nesting=0;
21962201
if (lval.tag!=0)
21972202
append_constval(&taglst,arg[argidx].name,lval.tag,0);
21982203
argidx++; /* argument done */
2199-
if (lval.sym!=NULL)
2200-
markusage(lval.sym,uWRITTEN);
22012204
break;
22022205
case iREFARRAY:
22032206
if (lval.ident!=iARRAY && lval.ident!=iREFARRAY
@@ -2275,11 +2278,22 @@ static int nesting=0;
22752278
check_tagmismatch_multiple(arg[argidx].tags,arg[argidx].numtags,lval.tag,-1);
22762279
if (lval.tag!=0)
22772280
append_constval(&taglst,arg[argidx].name,lval.tag,0);
2278-
if (lval.sym!=NULL && (arg[argidx].usage & uCONST)==0)
2279-
markusage(lval.sym,uWRITTEN);
2281+
if (lval.sym!=NULL && (arg[argidx].usage & uCONST)==0) {
2282+
symbol *cursym=lval.sym;
2283+
do {
2284+
markusage(cursym,uWRITTEN);
2285+
} while ((cursym=cursym->child)!=NULL);
2286+
} /* if */
22802287
argidx++; /* argument done */
22812288
break;
22822289
} /* switch */
2290+
/* Mark the symbol as "read" so it won't be omitted from P-code.
2291+
* Native functions are marked as read at the point of their call,
2292+
* so we don't handle them here; see ffcall().
2293+
*/
2294+
if (lval.sym!=NULL && (lval.sym->usage & uNATIVE)==0) {
2295+
markusage(lval.sym,uREAD);
2296+
} /* if */
22832297
pushreg(sPRI); /* store the function argument on the stack */
22842298
markexpr(sPARM,NULL,0); /* mark the end of a sub-expression */
22852299
nest_stkusage++;

0 commit comments

Comments
 (0)