add warning for literal passed to non-const parameter

Adds a new warning to warn users when they pass an array/string literal to a non-const qualified parameter.

```
f1(arr[]) {
 	new a = arr[0];
 	#pragma unused a
}

f2(arr[5]) {
    new a = arr[0];
    #pragma unused a
}
f3(const arr[]) {
    new a = arr[0];
    #pragma unused a
}
f4(const arr[5]) {
    new a = arr[0];
    #pragma unused a
}

main () {
	f1("test");
	f2("test");
	f3("test");
	f4("test");

	new arr[5];
	f1(arr);
	f2(arr);
	f3(arr);
	f4(arr);

	f1(arr[0]);
	//f2(arr[0]); - array size must match
	f3(arr[0]);
	//f4(arr[0]); - array size must match
}
```

```
test.pwn(1) : warning 214: possibly a "const" array argument was intended: "arr"
test.pwn(6) : warning 214: possibly a "const" array argument was intended: "arr"
test.pwn(20) : warning 239: literal array/string passed to a non-const parameter
test.pwn(21) : warning 239: literal array/string passed to a non-const parameter
```
This commit is contained in:
Yashas 2018-05-10 10:44:36 +05:30
parent 29669858b3
commit c363c5b1de
2 changed files with 19 additions and 14 deletions

View File

@ -2214,10 +2214,13 @@ static int nesting=0;
if (lval.sym==NULL || lval.ident==iARRAYCELL) {
if (arg[argidx].numdim!=1) {
error(48); /* array dimensions must match */
} else if (arg[argidx].dim[0]!=0) {
} else {
if (lval.sym==NULL && (arg[argidx].usage & uCONST)==0)
error(239);
if (arg[argidx].dim[0]!=0) {
assert(arg[argidx].dim[0]>0);
if (lval.ident==iARRAYCELL) {
error(47); /* array sizes must match */
error(7); /* array sizes must match */
} else {
assert(lval.constval!=0); /* literal array must have a size */
/* A literal array must have exactly the same size as the
@ -2225,10 +2228,11 @@ static int nesting=0;
* the function argument.
*/
if ((lval.constval>0 && arg[argidx].dim[0]!=lval.constval)
|| (lval.constval<0 && arg[argidx].dim[0] < -lval.constval))
|| (lval.constval<0 && arg[argidx].dim[0]<-lval.constval))
error(47); /* array sizes must match */
} /* if */
} /* if */
} /* if */
if (lval.ident!=iARRAYCELL || lval.constval > 0) {
/* save array size, for default values with uSIZEOF flag */
cell array_sz=lval.constval;

View File

@ -192,7 +192,8 @@ static char *warnmsg[] = {
/*235*/ "public function lacks forward declaration (symbol \"%s\")\n",
/*236*/ "unknown parameter in substitution (incorrect #define pattern)\n",
/*237*/ "user warning: %s\n",
/*238*/ "meaningless combination of class specifiers (%s)\n"
/*238*/ "meaningless combination of class specifiers (%s)\n",
/*239*/ "literal array/string passed to a non-const parameter\n"
};
#define NUM_WARNINGS (sizeof warnmsg / sizeof warnmsg[0])