Merge pull request #397 from Daniel-Cortez/suggestions-fix

More fixes related to suggestions
This commit is contained in:
Zeex 2019-06-16 12:48:26 +06:00 committed by GitHub
commit e645ffceef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 100 additions and 67 deletions

View File

@ -516,13 +516,18 @@ enum { /* identifier types */
estAUTOMATON, estAUTOMATON,
estSTATE estSTATE
}; };
enum { /* symbol types */ enum { /* symbol type flags */
essNONLABEL, /* find symbols of any type but labels */ esfLABEL = 1 << 0,
essVARCONST, /* array, single variable or named constant */ esfCONST = 1 << 1, /* named constant */
essARRAY, esfVARIABLE = 1 << 2, /* single variable */
essCONST, esfARRAY = 1 << 3, /* array */
essFUNCTN, esfFUNCTION = 1 << 4, /* Pawn or native function */
essLABEL
/* find symbols of any type but labels */
esfNONLABEL = esfCONST | esfVARIABLE | esfARRAY | esfFUNCTION,
/* find an array, a single variable, or a named constant */
esfVARCONST = esfCONST | esfVARIABLE | esfARRAY
}; };
/* interface functions */ /* interface functions */

View File

@ -2028,7 +2028,7 @@ static void declglb(char *firstname,int firsttag,int fpublic,int fstatic,int fst
} else { } else {
tag=pc_addtag(NULL); tag=pc_addtag(NULL);
if (lex(&val,&str)!=tSYMBOL) /* read in (new) token */ if (lex(&val,&str)!=tSYMBOL) /* read in (new) token */
error_suggest(20,str,NULL,estSYMBOL,essFUNCTN); /* invalid symbol name */ error_suggest(20,str,NULL,estSYMBOL,esfFUNCTION); /* invalid symbol name */
assert(strlen(str)<=sNAMEMAX); assert(strlen(str)<=sNAMEMAX);
strcpy(name,str); /* save symbol name */ strcpy(name,str); /* save symbol name */
} /* if */ } /* if */
@ -4879,7 +4879,7 @@ static int testsymbols(symbol *root,int level,int testlabs,int testconst)
case iLABEL: case iLABEL:
if (testlabs) { if (testlabs) {
if ((sym->usage & uDEFINE)==0) { if ((sym->usage & uDEFINE)==0) {
error_suggest(19,sym->name,NULL,estSYMBOL,essLABEL); /* not a label: ... */ error_suggest(19,sym->name,NULL,estSYMBOL,esfLABEL); /* not a label: ... */
} else if ((sym->usage & uREAD)==0) { } else if ((sym->usage & uREAD)==0) {
errorset(sSETPOS,sym->lnumber); errorset(sSETPOS,sym->lnumber);
error(203,sym->name); /* symbol isn't used: ... */ error(203,sym->name); /* symbol isn't used: ... */
@ -5983,7 +5983,7 @@ static void dogoto(void)
// sym->compound (nesting level of the label) against nestlevel; // sym->compound (nesting level of the label) against nestlevel;
// if sym->compound < nestlevel, call the destructor operator // if sym->compound < nestlevel, call the destructor operator
} else { } else {
error_suggest(20,st,NULL,estSYMBOL,essLABEL); /* illegal symbol name */ error_suggest(20,st,NULL,estSYMBOL,esfLABEL); /* illegal symbol name */
} /* if */ } /* if */
needtoken(tTERM); needtoken(tTERM);
} }
@ -6023,7 +6023,7 @@ static symbol *fetchlab(char *name)
sym=findloc(name); /* labels are local in scope */ sym=findloc(name); /* labels are local in scope */
if (sym) { if (sym) {
if (sym->ident!=iLABEL) if (sym->ident!=iLABEL)
error_suggest(19,sym->name,NULL,estSYMBOL,essLABEL); /* not a label: ... */ error_suggest(19,sym->name,NULL,estSYMBOL,esfLABEL); /* not a label: ... */
} else { } else {
sym=addsym(name,getlabel(),iLABEL,sLOCAL,0,0); sym=addsym(name,getlabel(),iLABEL,sLOCAL,0,0);
assert(sym!=NULL); /* fatal error 103 must be given on error */ assert(sym!=NULL); /* fatal error 103 must be given on error */

View File

@ -1349,13 +1349,13 @@ static int hier2(value *lval)
if (sym==NULL) if (sym==NULL)
sym=findglb(st,sSTATEVAR); sym=findglb(st,sSTATEVAR);
if (sym==NULL) if (sym==NULL)
return error_suggest(17,st,NULL,estSYMBOL,essVARCONST); /* undefined symbol */ return error_suggest(17,st,NULL,estSYMBOL,esfVARCONST); /* undefined symbol */
if (sym->ident==iCONSTEXPR) if (sym->ident==iCONSTEXPR)
error(39); /* constant symbol has no size */ error(39); /* constant symbol has no size */
else if (sym->ident==iFUNCTN || sym->ident==iREFFUNC) else if (sym->ident==iFUNCTN || sym->ident==iREFFUNC)
error(72); /* "function" symbol has no size */ error(72); /* "function" symbol has no size */
else if ((sym->usage & uDEFINE)==0) else if ((sym->usage & uDEFINE)==0)
return error_suggest(17,st,NULL,estSYMBOL,essVARCONST); /* undefined symbol (symbol is in the table, but it is "used" only) */ return error_suggest(17,st,NULL,estSYMBOL,esfVARCONST); /* undefined symbol (symbol is in the table, but it is "used" only) */
clear_value(lval); clear_value(lval);
lval->ident=iCONSTEXPR; lval->ident=iCONSTEXPR;
lval->constval=1; /* preset */ lval->constval=1; /* preset */
@ -1370,7 +1370,7 @@ static int hier2(value *lval)
int cmptag=subsym->x.tags.index; int cmptag=subsym->x.tags.index;
tokeninfo(&val,&idxname); tokeninfo(&val,&idxname);
if ((idxsym=findconst(idxname,&cmptag))==NULL) if ((idxsym=findconst(idxname,&cmptag))==NULL)
error_suggest(80,idxname,NULL,estSYMBOL,essCONST); /* unknown symbol, or non-constant */ error_suggest(80,idxname,NULL,estSYMBOL,esfCONST); /* unknown symbol, or non-constant */
else if (cmptag>1) else if (cmptag>1)
error(91,idxname); /* ambiguous constant */ error(91,idxname); /* ambiguous constant */
} /* if */ } /* if */
@ -1406,9 +1406,9 @@ static int hier2(value *lval)
if (sym==NULL) if (sym==NULL)
sym=findglb(st,sSTATEVAR); sym=findglb(st,sSTATEVAR);
if (sym==NULL) if (sym==NULL)
return error_suggest(17,st,NULL,estSYMBOL,essNONLABEL); /* undefined symbol */ return error_suggest(17,st,NULL,estSYMBOL,esfNONLABEL); /* undefined symbol */
if ((sym->usage & uDEFINE)==0) if ((sym->usage & uDEFINE)==0)
return error_suggest(17,st,NULL,estSYMBOL,essNONLABEL); /* undefined symbol (symbol is in the table, but it is "used" only) */ return error_suggest(17,st,NULL,estSYMBOL,esfNONLABEL); /* undefined symbol (symbol is in the table, but it is "used" only) */
tag=sym->tag; tag=sym->tag;
} /* if */ } /* if */
if (sym!=NULL && (sym->ident==iARRAY || sym->ident==iREFARRAY)) { if (sym!=NULL && (sym->ident==iARRAY || sym->ident==iREFARRAY)) {
@ -1422,7 +1422,7 @@ static int hier2(value *lval)
int cmptag=subsym->x.tags.index; int cmptag=subsym->x.tags.index;
tokeninfo(&val,&idxname); tokeninfo(&val,&idxname);
if ((idxsym=findconst(idxname,&cmptag))==NULL) if ((idxsym=findconst(idxname,&cmptag))==NULL)
error_suggest(80,idxname,NULL,estSYMBOL,essCONST); /* unknown symbol, or non-constant */ error_suggest(80,idxname,NULL,estSYMBOL,esfCONST); /* unknown symbol, or non-constant */
else if (cmptag>1) else if (cmptag>1)
error(91,idxname); /* ambiguous constant */ error(91,idxname); /* ambiguous constant */
} /* if */ } /* if */
@ -1598,7 +1598,7 @@ restart:
needtoken(close); needtoken(close);
return FALSE; return FALSE;
} else if (sym->ident!=iARRAY && sym->ident!=iREFARRAY){ } else if (sym->ident!=iARRAY && sym->ident!=iREFARRAY){
error_suggest(28,sym->name,NULL,estSYMBOL,essARRAY);/* cannot subscript, variable is not an array */ error_suggest(28,sym->name,NULL,estSYMBOL,esfARRAY);/* cannot subscript, variable is not an array */
needtoken(close); needtoken(close);
return FALSE; return FALSE;
} else if (sym->dim.array.level>0 && close!=']') { } else if (sym->dim.array.level>0 && close!=']') {
@ -1851,10 +1851,10 @@ static int primary(value *lval)
* implemented, issue an error * implemented, issue an error
*/ */
if ((sym->usage & uPROTOTYPED)==0) if ((sym->usage & uPROTOTYPED)==0)
error_suggest(17,st,NULL,estSYMBOL,essFUNCTN); /* undefined symbol */ error_suggest(17,st,NULL,estSYMBOL,esfFUNCTION); /* undefined symbol */
} else { } else {
if ((sym->usage & uDEFINE)==0) if ((sym->usage & uDEFINE)==0)
error_suggest(17,st,NULL,estSYMBOL,essVARCONST); /* undefined symbol */ error_suggest(17,st,NULL,estSYMBOL,esfVARCONST); /* undefined symbol */
lval->sym=sym; lval->sym=sym;
lval->ident=sym->ident; lval->ident=sym->ident;
lval->tag=sym->tag; lval->tag=sym->tag;
@ -1867,7 +1867,7 @@ static int primary(value *lval)
} /* if */ } /* if */
} else { } else {
if (!sc_allowproccall) if (!sc_allowproccall)
return error_suggest(17,st,NULL,estSYMBOL,essVARCONST); /* undefined symbol */ return error_suggest(17,st,NULL,estSYMBOL,esfVARCONST); /* undefined symbol */
/* an unknown symbol, but used in a way compatible with the "procedure /* an unknown symbol, but used in a way compatible with the "procedure
* call" syntax. So assume that the symbol refers to a function. * call" syntax. So assume that the symbol refers to a function.
*/ */

View File

@ -259,31 +259,31 @@ static short lastfile;
} /* if */ } /* if */
if (number<100) { if (number<100) {
assert(number>0 && number<(1+arraysize(errmsg))); assert(number>0 && number<(1+sizeof(errmsg)/sizeof(errmsg[0])));
msg=errmsg[number-1]; msg=errmsg[number-1];
pre=prefix[0]; pre=prefix[0];
errflag=TRUE; /* set errflag (skip rest of erroneous expression) */ errflag=TRUE; /* set errflag (skip rest of erroneous expression) */
errnum++; errnum++;
} else if (number<200) { } else if (number<200) {
assert(number>=100 && number<(100+arraysize(fatalmsg))); assert(number>=100 && number<(100+sizeof(fatalmsg)/sizeof(fatalmsg[0])));
msg=fatalmsg[number-100]; msg=fatalmsg[number-100];
pre=prefix[1]; pre=prefix[1];
errnum++; /* a fatal error also counts as an error */ errnum++; /* a fatal error also counts as an error */
} else if (errwarn) { } else if (errwarn) {
assert(number>=200 && number<(200+arraysize(warnmsg))); assert(number>=200 && number<(200+sizeof(warnmsg)/sizeof(warnmsg[0])));
msg=warnmsg[number-200]; msg=warnmsg[number-200];
pre=prefix[0]; pre=prefix[0];
errflag=TRUE; errflag=TRUE;
errnum++; errnum++;
} else { } else {
assert(number>=200 && number<(200+arraysize(warnmsg))); assert(number>=200 && number<(200+sizeof(warnmsg)/sizeof(warnmsg[0])));
msg=warnmsg[number-200]; msg=warnmsg[number-200];
pre=prefix[2]; pre=prefix[2];
warnnum++; warnnum++;
} /* if */ } /* if */
if (notice!=0) { if (notice!=0) {
assert(notice>0 && notice<(1+arraysize(noticemsg)) && noticemsg[notice-1][0]!='\0'); assert(notice>0 && notice<(1+sizeof(noticemsg)/sizeof(noticemsg[0])) && noticemsg[notice-1][0]!='\0');
strcpy(string,msg); strcpy(string,msg);
strcpy(&string[strlen(string)-1],noticemsg[notice-1]); strcpy(&string[strlen(string)-1],noticemsg[notice-1]);
msg=string; msg=string;
@ -502,7 +502,6 @@ static int find_closest_symbol_table(const char *name,const symbol *root,int sym
int dist,max_dist,closest_dist=INT_MAX; int dist,max_dist,closest_dist=INT_MAX;
char symname[2*sNAMEMAX+16]; char symname[2*sNAMEMAX+16];
symbol *sym; symbol *sym;
int ident;
assert(closest_sym!=NULL); assert(closest_sym!=NULL);
*closest_sym =NULL; *closest_sym =NULL;
assert(name!=NULL); assert(name!=NULL);
@ -510,33 +509,41 @@ static int find_closest_symbol_table(const char *name,const symbol *root,int sym
for (sym=root->next; sym!=NULL; sym=sym->next) { for (sym=root->next; sym!=NULL; sym=sym->next) {
if (sym->fnumber!=-1 && sym->fnumber!=fcurrent) if (sym->fnumber!=-1 && sym->fnumber!=fcurrent)
continue; continue;
if ((sym->usage & uDEFINE)==0) if ((sym->usage & uDEFINE)==0 && (sym->ident!=iFUNCTN || (sym->usage & (uNATIVE | uPROTOTYPED))!=uPROTOTYPED))
continue; continue;
ident=sym->ident; switch (sym->ident)
if (symboltype==essNONLABEL) { {
if (ident==iLABEL) case iLABEL:
if ((symboltype & esfLABEL)==0)
continue; continue;
} else if (symboltype==essVARCONST) { break;
if (ident!=iCONSTEXPR && ident!=iVARIABLE && ident!=iREFERENCE && ident!=iARRAY && ident!=iREFARRAY) case iCONSTEXPR:
if ((symboltype & esfCONST)==0)
continue; continue;
} else if (symboltype==essARRAY) { break;
if (ident!=iARRAY && ident!=iREFARRAY) case iVARIABLE:
case iREFERENCE:
if ((symboltype & esfVARIABLE)==0)
continue; continue;
} else if (symboltype==essCONST) { break;
if (ident!=iCONSTEXPR) case iARRAY:
case iREFARRAY:
if ((symboltype & esfARRAY)==0)
continue; continue;
} else if (symboltype==essFUNCTN) { break;
if (ident!=iFUNCTN && ident!=iREFFUNC) case iFUNCTN:
case iREFFUNC:
if ((symboltype & esfFUNCTION)==0)
continue; continue;
} else if (symboltype==essLABEL) { break;
if (ident!=iLABEL) default:
continue; assert(0);
} /* if */ } /* switch */
funcdisplayname(symname,sym->name); funcdisplayname(symname,sym->name);
dist=levenshtein_distance(name,symname); dist=levenshtein_distance(name,symname);
if (dist>max_dist || dist>=closest_dist) if (dist>max_dist || dist>=closest_dist)
continue; continue;
*closest_sym =sym; *closest_sym=sym;
closest_dist=dist; closest_dist=dist;
if (closest_dist<=1) if (closest_dist<=1)
break; break;
@ -642,6 +649,7 @@ SC_FUNC int error_suggest(int number,const char *name,const char *name2,int type
{ {
char string[sNAMEMAX*2+2]; /* for "<automaton>:<state>" */ char string[sNAMEMAX*2+2]; /* for "<automaton>:<state>" */
const char *closest_name=NULL; const char *closest_name=NULL;
symbol *closest_sym;
/* don't bother finding the closest names on errors /* don't bother finding the closest names on errors
* that aren't going to be shown on the 1'st pass * that aren't going to be shown on the 1'st pass
@ -649,28 +657,30 @@ SC_FUNC int error_suggest(int number,const char *name,const char *name2,int type
if ((errflag || sc_status!=statWRITE) && (number<100 || number>=200)) if ((errflag || sc_status!=statWRITE) && (number<100 || number>=200))
return 0; return 0;
if (type==estSYMBOL || (type==estNONSYMBOL && tMIDDLE<subtype && subtype<=tLAST)) { if (type==estSYMBOL) {
symbol *closest_sym; find_symbol:
if (type!=estSYMBOL) { closest_sym=find_closest_symbol(name,subtype);
if (closest_sym!=NULL)
closest_name=closest_sym->name;
} else if (type==estNONSYMBOL) {
if (tMIDDLE<subtype && subtype<=tLAST) {
extern char *sc_tokens[]; extern char *sc_tokens[];
name=sc_tokens[subtype-tFIRST]; name=sc_tokens[subtype-tFIRST];
subtype=essVARCONST; subtype=esfVARCONST;
goto find_symbol;
} /* if */ } /* if */
closest_sym =find_closest_symbol(name,subtype);
if (closest_sym !=NULL)
closest_name= closest_sym->name;
} else if (type==estAUTOMATON) { } else if (type==estAUTOMATON) {
constvalue *closest_automaton=find_closest_automaton(name); constvalue *closest_automaton=find_closest_automaton(name);
if (closest_automaton!=NULL) if (closest_automaton!=NULL)
closest_name=closest_automaton->name; closest_name=closest_automaton->name;
} else if (type==estSTATE) { } else if (type==estSTATE) {
constvalue *closest_state=find_closest_state(name,subtype); constvalue *closest_state=find_closest_state(name,subtype);
if (closest_state !=NULL) { if (closest_state!=NULL) {
closest_name=closest_state->name; closest_name=closest_state->name;
} else { } else {
constvalue *closest_automaton=find_closest_automaton_for_state(name,subtype); constvalue *closest_automaton=find_closest_automaton_for_state(name,subtype);
if (closest_automaton !=NULL) { if (closest_automaton!=NULL) {
sprintf(string,"%s:%s", closest_automaton->name,name); sprintf(string,"%s:%s",closest_automaton->name,name);
closest_name=string; closest_name=string;
} /* if */ } /* if */
} /* if */ } /* if */

View File

@ -8,18 +8,21 @@ gh_353_symbol_suggestions.pwn(40) : error 017: undefined symbol "ab"
gh_353_symbol_suggestions.pwn(41) : error 017: undefined symbol "ab" gh_353_symbol_suggestions.pwn(41) : error 017: undefined symbol "ab"
gh_353_symbol_suggestions.pwn(50) : error 017: undefined symbol "staticval" gh_353_symbol_suggestions.pwn(50) : error 017: undefined symbol "staticval"
gh_353_symbol_suggestions.pwn(58) : error 017: undefined symbol "test_nosuggest6_val" gh_353_symbol_suggestions.pwn(58) : error 017: undefined symbol "test_nosuggest6_val"
gh_353_symbol_suggestions.pwn(67) : error 017: undefined symbol "val"; did you mean "var"? gh_353_symbol_suggestions.pwn(67) : error 020: invalid symbol name ""
gh_353_symbol_suggestions.pwn(71) : error 017: undefined symbol "celmax"; did you mean "cellmax"? gh_353_symbol_suggestions.pwn(68) : error 020: invalid symbol name ""
gh_353_symbol_suggestions.pwn(75) : error 017: undefined symbol "strcaf"; did you mean "strcat"? gh_353_symbol_suggestions.pwn(77) : error 017: undefined symbol "val"; did you mean "var"?
gh_353_symbol_suggestions.pwn(78) : error 017: undefined symbol "test_e17"; did you mean "test_e017"? gh_353_symbol_suggestions.pwn(81) : error 017: undefined symbol "celmax"; did you mean "cellmax"?
gh_353_symbol_suggestions.pwn(87) : error 019: not a label: "lb"; did you mean "lbl"? gh_353_symbol_suggestions.pwn(85) : error 017: undefined symbol "strcaf"; did you mean "strcat"?
gh_353_symbol_suggestions.pwn(94) : error 020: invalid symbol name "assert"; did you mean "asset"? gh_353_symbol_suggestions.pwn(88) : error 017: undefined symbol "DoNothin"; did you mean "DoNothing"?
gh_353_symbol_suggestions.pwn(105) : error 080: unknown symbol, or not a constant symbol (symbol "idx"); did you mean "id"? gh_353_symbol_suggestions.pwn(91) : error 017: undefined symbol "test_e17"; did you mean "test_e017"?
gh_353_symbol_suggestions.pwn(116) : error 086: unknown automaton "automaton1"; did you mean "automaton_1"? gh_353_symbol_suggestions.pwn(102) : error 019: not a label: "lb"; did you mean "lbl"?
gh_353_symbol_suggestions.pwn(116) : error 036: empty statement gh_353_symbol_suggestions.pwn(109) : error 020: invalid symbol name "assert"; did you mean "asset"?
gh_353_symbol_suggestions.pwn(123) : error 087: unknown state "BEING1" for automaton "automaton_2"; did you mean "BEING_1"? gh_353_symbol_suggestions.pwn(120) : error 080: unknown symbol, or not a constant symbol (symbol "idx"); did you mean "id"?
gh_353_symbol_suggestions.pwn(123) : error 036: empty statement gh_353_symbol_suggestions.pwn(131) : error 086: unknown automaton "automaton1"; did you mean "automaton_1"?
gh_353_symbol_suggestions.pwn(126) : error 087: unknown state "STATE_1" for automaton "automaton_2"; did you mean "automaton_1:STATE_1"? gh_353_symbol_suggestions.pwn(131) : error 036: empty statement
gh_353_symbol_suggestions.pwn(126) : error 036: empty statement gh_353_symbol_suggestions.pwn(138) : error 087: unknown state "BEING1" for automaton "automaton_2"; did you mean "BEING_1"?
gh_353_symbol_suggestions.pwn(138) : error 036: empty statement
gh_353_symbol_suggestions.pwn(141) : error 087: unknown state "STATE_1" for automaton "automaton_2"; did you mean "automaton_1:STATE_1"?
gh_353_symbol_suggestions.pwn(141) : error 036: empty statement
""" """
} }

View File

@ -59,6 +59,16 @@ public test_nosuggest6()
} }
static test_nosuggest6_val; static test_nosuggest6_val;
forward test_nosuggest7();
public test_nosuggest7()
{
// The compiler shouldn't try to suggest anything when tagof is used on
// string/numeric literals.
new a = tagof 0;
new b = tagof "";
return a + b;
}
forward test_e017(); forward test_e017();
public test_e017() public test_e017()
{ {
@ -74,9 +84,14 @@ public test_e017()
new str[4] = "a"; new str[4] = "a";
strcaf(str, "b"); strcaf(str, "b");
// error 017: undefined symbol "DoNothin"; did you mean "DoNothing"?
DoNothin();
// error 017: undefined symbol "test_e17"; did you mean "test_e017"? // error 017: undefined symbol "test_e17"; did you mean "test_e017"?
printf("%d\n", tagof test_e17); printf("%d\n", tagof test_e17);
} }
DoNothing(){}
#pragma unused DoNothing
forward test_e019(); forward test_e019();
public test_e019() public test_e019()