diff --git a/source/compiler/sc.h b/source/compiler/sc.h index 1a517a4..e6e5a26 100644 --- a/source/compiler/sc.h +++ b/source/compiler/sc.h @@ -640,7 +640,7 @@ SC_FUNC void setlinedirect(int line); SC_FUNC void setlineconst(int line); SC_FUNC void setlabel(int index); SC_FUNC void markexpr(optmark type,const char *name,cell offset); -SC_FUNC void startfunc(char *fname); +SC_FUNC void startfunc(char *fname,int generateproc); SC_FUNC void endfunc(void); SC_FUNC void alignframe(int numbytes); SC_FUNC void rvalue(value *lval); diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index b3eddeb..116a65e 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -3798,7 +3798,7 @@ static int newfunc(char *firstname,int firsttag,int fpublic,int fstatic,int stoc ptr=ptr->next; } /* while */ } /* if */ - startfunc(sym->name); /* creates stack frame */ + startfunc(sym->name,(sym->flags & flagNAKED)==0); /* creates stack frame */ insert_dbgline(funcline); setline(FALSE); if (sc_alignnext) { @@ -4223,8 +4223,12 @@ static void doarg(char *name,int ident,int offset,int tags[],int numtags, assert(numtags>0); argsym=addvariable(name,offset,ident,sLOCAL,tags[0], arg->dim,arg->numdim,arg->idxtag,0); - if (fpublic) + if (fpublic) { argsym->usage|=uREAD; /* arguments of public functions are always "used" */ + if(argsym->ident==iREFARRAY || argsym->ident==iREFERENCE) + argsym->usage|=uWRITTEN; + } + if (fconst) argsym->usage|=uCONST; } /* if */ @@ -4903,9 +4907,20 @@ static int testsymbols(symbol *root,int level,int testlabs,int testconst) error(204,sym->name); /* value assigned to symbol is never used */ errorset(sSETPOS,-1); } else if ((sym->usage & (uWRITTEN | uPUBLIC | uCONST))==0 && sym->ident==iREFARRAY) { - errorset(sSETPOS,sym->lnumber); - error(214,sym->name); /* make array argument "const" */ - errorset(sSETPOS,-1); + int warn = 1; + symbol* depend = finddepend(sym); + while (depend != NULL) { + if ((depend->usage & (uWRITTEN | uPUBLIC | uCONST)) != 0) { + warn = 0; + break; + } + depend = finddepend(depend); + } /* while */ + if (warn) { + errorset(sSETPOS, sym->lnumber); + error(214, sym->name); /* make array argument "const" */ + errorset(sSETPOS, -1); + } /* if */ } /* if */ /* also mark the variable (local or global) to the debug information */ if ((sym->usage & (uWRITTEN | uREAD))!=0 && (sym->usage & uNATIVE)==0) diff --git a/source/compiler/sc3.c b/source/compiler/sc3.c index c632b40..c61a04c 100644 --- a/source/compiler/sc3.c +++ b/source/compiler/sc3.c @@ -2215,7 +2215,7 @@ static int nesting=0; if (arg[argidx].numdim!=1) { error(48); /* array dimensions must match */ } else { - if (lval.sym==NULL && (arg[argidx].usage & uCONST)==0) + if (lval.sym==NULL && (arg[argidx].usage & uCONST)==0 && (sym->usage & uNATIVE)==0) error(239); if (arg[argidx].dim[0]!=0) { assert(arg[argidx].dim[0]>0); diff --git a/source/compiler/sc4.c b/source/compiler/sc4.c index f2dd349..576c16b 100644 --- a/source/compiler/sc4.c +++ b/source/compiler/sc4.c @@ -343,9 +343,12 @@ SC_FUNC void markexpr(optmark type,const char *name,cell offset) * * Global references: funcstatus (referred to only) */ -SC_FUNC void startfunc(char *fname) +SC_FUNC void startfunc(char *fname,int generateproc) { - stgwrite("\tproc"); + if (generateproc) { + stgwrite("\tproc"); + code_idx+=opcodes(1); + } /* if */ if (sc_asmfile) { char symname[2*sNAMEMAX+16]; funcdisplayname(symname,fname); @@ -353,7 +356,6 @@ SC_FUNC void startfunc(char *fname) stgwrite(symname); } /* if */ stgwrite("\n"); - code_idx+=opcodes(1); } /* endfunc diff --git a/source/compiler/tests/CMakeLists.txt b/source/compiler/tests/CMakeLists.txt index 5d22a65..6da0c00 100644 --- a/source/compiler/tests/CMakeLists.txt +++ b/source/compiler/tests/CMakeLists.txt @@ -52,6 +52,16 @@ set_tests_properties(meaningless_class_specifiers_gh_172 PROPERTIES PASS_REGULAR .*\\.pwn\\(1 \\-\\- 2\\) : warning 238: meaningless combination of class specifiers \\(const variable arguments\\) ") +add_compiler_test(const_array_args_and_literals_gh_276 ${CMAKE_CURRENT_SOURCE_DIR}/const_array_args_and_literals_gh_276.pwn) +set_tests_properties(const_array_args_and_literals_gh_276 PROPERTIES PASS_REGULAR_EXPRESSION +".*\\.pwn\\(13\\) : warning 214: possibly a \\\"const\\\" array argument was intended: \\\"arr\\\" +.*\\.pwn\\(18\\) : warning 214: possibly a \\\"const\\\" array argument was intended: \\\"arr\\\" +.*\\.pwn\\(30\\) : warning 214: possibly a \\\"const\\\" array argument was intended: \\\"arr\\\" +.*\\.pwn\\(39\\) : warning 239: literal array/string passed to a non-const parameter +.*\\.pwn\\(40\\) : warning 239: literal array/string passed to a non-const parameter +.*\\.pwn\\(41\\) : warning 239: literal array/string passed to a non-const parameter +") + # Crashers # # These tests simply check that the compiler doesn't crash. diff --git a/source/compiler/tests/const_array_args_and_literals_gh_276.pwn b/source/compiler/tests/const_array_args_and_literals_gh_276.pwn new file mode 100644 index 0000000..fdc990d --- /dev/null +++ b/source/compiler/tests/const_array_args_and_literals_gh_276.pwn @@ -0,0 +1,61 @@ +forward OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]); + +native SetTimer(funcname[], interval, repeating); +public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) +{ + +} + +f0(arr[]) { + #pragma unused arr +} + +f1(arr[]) { // line 13 + new a = arr[0]; + #pragma unused a +} + +f2(arr[5]) {// line 18 + 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 +} +f5(arr[][]) { // line 30 + new a = arr[0][0]; + #pragma unused a +} +f6(arr[][]) { + arr[0][0] = 0; +} + +main () { + f0("test"); // line 39 + f1("test"); // line 40 + f2("test"); // line 41 + 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 + + new arr2[1][1]; + f5(arr2); + f6(arr2); + + SetTimer("test", 0, 0); +}