Merge pull request #302 from YashasSamaga/fix-i276

suggest `const` qualifier for arguments and inform user when an array/string literal is passed as a non-const argument
This commit is contained in:
Zeex 2018-06-03 03:36:05 +06:00 committed by GitHub
commit 537c91ab91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 17 deletions

View File

@ -4903,12 +4903,10 @@ static int testsymbols(symbol *root,int level,int testlabs,int testconst)
errorset(sSETPOS,sym->lnumber); errorset(sSETPOS,sym->lnumber);
error(204,sym->name); /* value assigned to symbol is never used */ error(204,sym->name); /* value assigned to symbol is never used */
errorset(sSETPOS,-1); errorset(sSETPOS,-1);
#if 0 // ??? not sure whether it is a good idea to force people use "const"
} else if ((sym->usage & (uWRITTEN | uPUBLIC | uCONST))==0 && sym->ident==iREFARRAY) { } else if ((sym->usage & (uWRITTEN | uPUBLIC | uCONST))==0 && sym->ident==iREFARRAY) {
errorset(sSETPOS,sym->lnumber); errorset(sSETPOS,sym->lnumber);
error(214,sym->name); /* make array argument "const" */ error(214,sym->name); /* make array argument "const" */
errorset(sSETPOS,-1); errorset(sSETPOS,-1);
#endif
} /* if */ } /* if */
/* also mark the variable (local or global) to the debug information */ /* also mark the variable (local or global) to the debug information */
if ((sym->usage & (uWRITTEN | uREAD))!=0 && (sym->usage & uNATIVE)==0) if ((sym->usage & (uWRITTEN | uREAD))!=0 && (sym->usage & uNATIVE)==0)

View File

@ -2214,19 +2214,23 @@ static int nesting=0;
if (lval.sym==NULL || lval.ident==iARRAYCELL) { if (lval.sym==NULL || lval.ident==iARRAYCELL) {
if (arg[argidx].numdim!=1) { if (arg[argidx].numdim!=1) {
error(48); /* array dimensions must match */ error(48); /* array dimensions must match */
} else if (arg[argidx].dim[0]!=0) { } else {
assert(arg[argidx].dim[0]>0); if (lval.sym==NULL && (arg[argidx].usage & uCONST)==0)
if (lval.ident==iARRAYCELL) { error(239);
error(47); /* array sizes must match */ if (arg[argidx].dim[0]!=0) {
} else { assert(arg[argidx].dim[0]>0);
assert(lval.constval!=0); /* literal array must have a size */ if (lval.ident==iARRAYCELL) {
/* A literal array must have exactly the same size as the error(7); /* array sizes must match */
* function argument; a literal string may be smaller than } else {
* the function argument. assert(lval.constval!=0); /* literal array must have a size */
*/ /* A literal array must have exactly the same size as the
if ((lval.constval>0 && arg[argidx].dim[0]!=lval.constval) * function argument; a literal string may be smaller than
|| (lval.constval<0 && arg[argidx].dim[0] < -lval.constval)) * the function argument.
error(47); /* array sizes must match */ */
if ((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 */ } /* if */
if (lval.ident!=iARRAYCELL || lval.constval > 0) { if (lval.ident!=iARRAYCELL || lval.constval > 0) {
@ -2270,7 +2274,8 @@ static int nesting=0;
check_tagmismatch_multiple(arg[argidx].tags,arg[argidx].numtags,lval.tag,-1); check_tagmismatch_multiple(arg[argidx].tags,arg[argidx].numtags,lval.tag,-1);
if (lval.tag!=0) if (lval.tag!=0)
append_constval(&taglst,arg[argidx].name,lval.tag,0); append_constval(&taglst,arg[argidx].name,lval.tag,0);
// ??? set uWRITTEN? if (lval.sym!=NULL && (arg[argidx].usage & uCONST)==0)
markusage(lval.sym,uWRITTEN);
argidx++; /* argument done */ argidx++; /* argument done */
break; break;
} /* switch */ } /* switch */

View File

@ -192,7 +192,8 @@ static char *warnmsg[] = {
/*235*/ "public function lacks forward declaration (symbol \"%s\")\n", /*235*/ "public function lacks forward declaration (symbol \"%s\")\n",
/*236*/ "unknown parameter in substitution (incorrect #define pattern)\n", /*236*/ "unknown parameter in substitution (incorrect #define pattern)\n",
/*237*/ "user warning: %s\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]) #define NUM_WARNINGS (sizeof warnmsg / sizeof warnmsg[0])