Skip to content

Commit a50b791

Browse files
committed
Don't issue warning 210 in a few special cases:
* A variable is defined as a loop counter inside a 'for' loop. * A variable is incremented/decremented using the '++'/'--' operator.
1 parent d3a002b commit a50b791

File tree

4 files changed

+21
-28
lines changed

4 files changed

+21
-28
lines changed

source/compiler/sc1.c

+5
Original file line numberDiff line numberDiff line change
@@ -5730,8 +5730,13 @@ static int dofor(void)
57305730
/* The variable in expr1 of the for loop is at a
57315731
* 'compound statement' level of it own.
57325732
*/
5733+
symbol *sym;
57335734
nestlevel++;
57345735
declloc(FALSE); /* declare local variable */
5736+
for (sym=loctab.next; sym!=NULL; sym=sym->next) {
5737+
if (sym->compound==nestlevel)
5738+
markinitialized(sym);
5739+
} /* for */
57355740
} else {
57365741
doexpr(TRUE,TRUE,TRUE,TRUE,NULL,NULL,FALSE); /* expression 1 */
57375742
needtoken(';');

source/compiler/sc3.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,7 @@ static int hier2(value *lval)
12531253
assert(lval->sym!=NULL);
12541254
if ((lval->sym->usage & uCONST)!=0)
12551255
return error(22); /* assignment to const argument */
1256-
checkinitialized(lval->sym);
1256+
markinitialized(lval->sym);
12571257
if (!check_userop(user_inc,lval->tag,0,1,lval,&lval->tag))
12581258
inc(lval); /* increase variable first */
12591259
rvalue(lval); /* and read the result into PRI */
@@ -1265,7 +1265,7 @@ static int hier2(value *lval)
12651265
assert(lval->sym!=NULL);
12661266
if ((lval->sym->usage & uCONST)!=0)
12671267
return error(22); /* assignment to const argument */
1268-
checkinitialized(lval->sym);
1268+
markinitialized(lval->sym);
12691269
if (!check_userop(user_dec,lval->tag,0,1,lval,&lval->tag))
12701270
dec(lval); /* decrease variable first */
12711271
rvalue(lval); /* and read the result into PRI */
@@ -1516,7 +1516,7 @@ static int hier2(value *lval)
15161516
assert(lval->sym!=NULL);
15171517
if ((lval->sym->usage & uCONST)!=0)
15181518
return error(22); /* assignment to const argument */
1519-
checkinitialized(lval->sym);
1519+
markinitialized(lval->sym);
15201520
/* on incrementing array cells, the address in PRI must be saved for
15211521
* incremening the value, whereas the current value must be in PRI
15221522
* on exit.
@@ -1539,7 +1539,7 @@ static int hier2(value *lval)
15391539
assert(lval->sym!=NULL);
15401540
if ((lval->sym->usage & uCONST)!=0)
15411541
return error(22); /* assignment to const argument */
1542-
checkinitialized(lval->sym);
1542+
markinitialized(lval->sym);
15431543
saveresult= (lval->ident==iARRAYCELL || lval->ident==iARRAYCHAR);
15441544
if (saveresult)
15451545
pushreg(sPRI); /* save address in PRI */

source/compiler/tests/warning_210.meta

-12
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,17 @@
33
'errors': """
44
warning_210.pwn(59) : warning 210: possible use of symbol before initialization: "local_var3"
55
warning_210.pwn(62) : warning 210: possible use of symbol before initialization: "local_var4"
6-
warning_210.pwn(66) : warning 210: possible use of symbol before initialization: "local_var5"
7-
warning_210.pwn(70) : warning 210: possible use of symbol before initialization: "local_var6"
86
warning_210.pwn(85) : warning 210: possible use of symbol before initialization: "local_arr1d4"
97
warning_210.pwn(88) : warning 210: possible use of symbol before initialization: "local_arr1d5"
108
warning_210.pwn(92) : warning 210: possible use of symbol before initialization: "local_arr1d6"
119
warning_210.pwn(96) : warning 210: possible use of symbol before initialization: "local_arr1d7"
12-
warning_210.pwn(100) : warning 210: possible use of symbol before initialization: "local_arr1d8"
13-
warning_210.pwn(104) : warning 210: possible use of symbol before initialization: "local_arr1d9"
1410
warning_210.pwn(119) : warning 210: possible use of symbol before initialization: "local_arr2d4"
1511
warning_210.pwn(122) : warning 210: possible use of symbol before initialization: "local_arr2d5"
1612
warning_210.pwn(126) : warning 210: possible use of symbol before initialization: "local_arr2d6"
1713
warning_210.pwn(130) : warning 210: possible use of symbol before initialization: "local_arr2d7"
1814
warning_210.pwn(134) : warning 210: possible use of symbol before initialization: "local_arr2d8"
19-
warning_210.pwn(138) : warning 210: possible use of symbol before initialization: "local_arr2d9"
20-
warning_210.pwn(142) : warning 210: possible use of symbol before initialization: "local_arr2d10"
2115
warning_210.pwn(149) : warning 210: possible use of symbol before initialization: "global_var3"
22-
warning_210.pwn(151) : warning 210: possible use of symbol before initialization: "global_var4"
23-
warning_210.pwn(154) : warning 210: possible use of symbol before initialization: "global_var5"
2416
warning_210.pwn(165) : warning 210: possible use of symbol before initialization: "global_arr1d4"
25-
warning_210.pwn(167) : warning 210: possible use of symbol before initialization: "global_arr1d5"
26-
warning_210.pwn(170) : warning 210: possible use of symbol before initialization: "global_arr1d6"
2717
warning_210.pwn(181) : warning 210: possible use of symbol before initialization: "global_arr2d4"
28-
warning_210.pwn(184) : warning 210: possible use of symbol before initialization: "global_arr2d5"
29-
warning_210.pwn(187) : warning 210: possible use of symbol before initialization: "global_arr2d6"
3018
"""
3119
}

source/compiler/tests/warning_210.pwn

+12-12
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ TestFunc(arg_var, const arg_arr1d[], const arg_arr2d[][])
6363
UseVariable(local_var4);
6464

6565
new local_var5;
66-
++local_var5; // warning
66+
++local_var5;
6767
UseVariable(local_var5);
6868

6969
new local_var6;
70-
local_var6++; // warning
70+
local_var6++;
7171
UseVariable(local_var6);
7272

7373
new local_arr1d1[] = "hi there!";
@@ -97,11 +97,11 @@ TestFunc(arg_var, const arg_arr1d[], const arg_arr2d[][])
9797
Use1DArray(local_arr1d7);
9898

9999
new local_arr1d8[8];
100-
++local_arr1d8[0]; // warning
100+
++local_arr1d8[0];
101101
Use1DArray(local_arr1d8);
102102

103103
new local_arr1d9[8];
104-
local_arr1d9[0]++; // warning
104+
local_arr1d9[0]++;
105105
Use1DArray(local_arr1d9);
106106

107107
new local_arr2d1[2][8] = { { 0 }, ... };
@@ -135,11 +135,11 @@ TestFunc(arg_var, const arg_arr1d[], const arg_arr2d[][])
135135
Use2DArray(local_arr2d8);
136136

137137
new local_arr2d9[2][8];
138-
++local_arr2d9[0][0]; // warning
138+
++local_arr2d9[0][0];
139139
Use2DArray(local_arr2d9);
140140

141141
new local_arr2d10[2][8];
142-
local_arr2d10[0][0]++; // warning
142+
local_arr2d10[0][0]++;
143143
Use2DArray(local_arr2d10);
144144
UseVariable(global_var1);
145145

@@ -148,10 +148,10 @@ TestFunc(arg_var, const arg_arr1d[], const arg_arr2d[][])
148148

149149
UseVariable(global_var3); // warning
150150

151-
++global_var4; // warning
151+
++global_var4;
152152
UseVariable(global_var4);
153153

154-
global_var5++; // warning
154+
global_var5++;
155155
UseVariable(global_var5);
156156

157157
Use1DArray(global_arr1d1);
@@ -164,10 +164,10 @@ TestFunc(arg_var, const arg_arr1d[], const arg_arr2d[][])
164164

165165
Use1DArray(global_arr1d4); // warning
166166

167-
++global_arr1d5[0]; // warning
167+
++global_arr1d5[0];
168168
Use1DArray(global_arr1d5);
169169

170-
global_arr1d6[0]++; // warning
170+
global_arr1d6[0]++;
171171
Use1DArray(global_arr1d6);
172172

173173
Use2DArray(global_arr2d1);
@@ -180,10 +180,10 @@ TestFunc(arg_var, const arg_arr1d[], const arg_arr2d[][])
180180

181181
Use2DArray(global_arr2d4); // warning
182182

183-
++global_arr2d5[0][0]; // warning
183+
++global_arr2d5[0][0];
184184
Use2DArray(global_arr2d5);
185185

186-
global_arr2d6[0][0]++; // warning
186+
global_arr2d6[0][0]++;
187187
Use2DArray(global_arr2d6);
188188
}
189189

0 commit comments

Comments
 (0)