From 836d4d9e355f7a0136bc1f966bfe8232f0a8975c Mon Sep 17 00:00:00 2001 From: Y_Less Date: Sat, 16 Jun 2018 09:53:14 +0100 Subject: [PATCH 01/12] Fix #317 --- source/compiler/sc1.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index 54878fc..b3eddeb 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -3150,8 +3150,7 @@ SC_FUNC symbol *fetchfunc(char *name,int tag) sym=addsym(name,code_idx,iFUNCTN,sGLOBAL,tag,0); assert(sym!=NULL); /* fatal error 103 must be given on error */ /* assume no arguments */ - sym->dim.arglist=(arginfo*)malloc(1*sizeof(arginfo)); - sym->dim.arglist[0].ident=0; + sym->dim.arglist=(arginfo*)calloc(1,sizeof(arginfo)); /* set library ID to NULL (only for native functions) */ sym->x.lib=NULL; /* set the required stack size to zero (only for non-native functions) */ From 9136cf29461bc2c1dfa68bd0aeff895bfa3e6340 Mon Sep 17 00:00:00 2001 From: Y_Less Date: Sat, 30 Jun 2018 16:41:50 +0200 Subject: [PATCH 02/12] Partially revert #c325ca3e028388ae6f934df2a4057229d24d021e to strip spaces in strings again. --- source/compiler/sc2.c | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/source/compiler/sc2.c b/source/compiler/sc2.c index 7a123f8..fa3fdfe 100644 --- a/source/compiler/sc2.c +++ b/source/compiler/sc2.c @@ -396,8 +396,11 @@ static void readline(unsigned char *line) *line='\0'; /* delete line */ cont=FALSE; } else { - /* check whether to erase leading whitespace after '\\' on next line */ + /* check whether to erase leading spaces */ if (cont) { + unsigned char *ptr=line; + while (*ptr<=' ' && *ptr!='\0') + ptr++; if (ptr!=line) memmove(line,ptr,strlen((char*)ptr)+1); } /* if */ @@ -512,6 +515,8 @@ static void stripcomment(unsigned char *line) if (icomment==2) *line++=' '; } else if (*line=='/' && *(line+1)=='/'){ /* comment to end of line */ + if (strchr((char*)line,'\a')!=NULL) + error(49); /* invalid line continuation */ #if !defined SC_LIGHT if (*(line+2)=='/' && *(line+3)<=' ') { /* documentation comment */ @@ -903,6 +908,23 @@ static const unsigned char *getstring(unsigned char *dest,int max,const unsigned return line; } +/* strdupwithouta + * + * Duplicate a string, stripping out `\a`s. + */ +static char* strdupwithouta(const char* sourcestring) +{ + char* result=strdup(sourcestring); + char* a=result; + if (result==NULL) { + return NULL; + } + while ((a=strchr(a,'\a'))!=NULL) { + strcpy(a,a+1); + } + return result; +} + enum { CMD_NONE, CMD_TERM, @@ -1115,7 +1137,7 @@ static int command(void) /* remove leading whitespace */ while (*lptr<=' ' && *lptr!='\0') lptr++; - pc_deprecate=strdup((const char *)lptr); + pc_deprecate=strdupwithouta((const char *)lptr); if (pc_deprecate!=NULL) { char *ptr=pc_deprecate+strlen(pc_deprecate)-1; /* remove trailing whitespace */ @@ -1499,7 +1521,7 @@ static int command(void) while (*lptr<=' ' && *lptr!='\0') lptr++; if (!SKIPPING) { - char *usermsg=strdup((const char *)lptr); + char *usermsg=strdupwithouta((const char *)lptr); if (usermsg!=NULL) { char *ptr=usermsg+strlen(usermsg)-1; /* remove trailing whitespace and newlines */ @@ -1893,6 +1915,10 @@ static const unsigned char *unpackedstring(const unsigned char *lptr,int *flags) while (*lptr==' ' || *lptr=='\t') /* this is as defines with parameters may add them */ lptr++; /* when you use a space after , in a match pattern */ while (*lptr!='\0') { + if (*lptr=='\a') { + lptr++; + continue; + } /* if */ if (!instring) { if (*lptr=='\"') { instring=1; @@ -1963,6 +1989,10 @@ static const unsigned char *packedstring(const unsigned char *lptr,int *flags) i=sizeof(ucell)-(sCHARBITS/8); /* start at most significant byte */ val=0; while (*lptr!='\0') { + if (*lptr=='\a') { /* ignore '\a' (which was inserted at a line concatenation) */ + lptr++; + continue; + } /* if */ if (!instring) { if (*lptr=='\"') { instring=1; From 62805cb4000f1ce784628d07b114ec8a50de0bc6 Mon Sep 17 00:00:00 2001 From: Y_Less Date: Sat, 30 Jun 2018 16:47:47 +0200 Subject: [PATCH 03/12] Redetect error 49, and make `deprecated` auto-insert spaces. --- source/compiler/sc2.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/source/compiler/sc2.c b/source/compiler/sc2.c index fa3fdfe..c726aac 100644 --- a/source/compiler/sc2.c +++ b/source/compiler/sc2.c @@ -417,6 +417,10 @@ static void readline(unsigned char *line) ptr--; /* skip trailing whitespace */ if (*ptr=='\\') { cont=TRUE; + /* set '\a' at the position of '\\' to make it possible to check + * for a line continuation in a single line comment (error 49) + */ + *ptr++='\a'; *ptr='\0'; /* erase '\n' (and any trailing whitespace) */ } /* if */ } /* if */ @@ -920,7 +924,7 @@ static char* strdupwithouta(const char* sourcestring) return NULL; } while ((a=strchr(a,'\a'))!=NULL) { - strcpy(a,a+1); + *a=' '; } return result; } From 23485cfeda023d4bd9161801e815ddb9a3148556 Mon Sep 17 00:00:00 2001 From: Y_Less Date: Sat, 30 Jun 2018 17:00:46 +0200 Subject: [PATCH 04/12] Allow line continuations in single-line comments only when they are followed by another comment: It is quite annoying to get an error for commenting out a define using: // // #define LONG_MACRO\ // did span \ // multiple lines // --- source/compiler/sc2.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/source/compiler/sc2.c b/source/compiler/sc2.c index c726aac..ffa5e25 100644 --- a/source/compiler/sc2.c +++ b/source/compiler/sc2.c @@ -448,6 +448,7 @@ static void readline(unsigned char *line) static void stripcomment(unsigned char *line) { char c; + char* continuation; #if !defined SC_LIGHT #define COMMENT_LIMIT 100 #define COMMENT_MARGIN 40 /* length of the longest word */ @@ -519,8 +520,24 @@ static void stripcomment(unsigned char *line) if (icomment==2) *line++=' '; } else if (*line=='/' && *(line+1)=='/'){ /* comment to end of line */ - if (strchr((char*)line,'\a')!=NULL) - error(49); /* invalid line continuation */ + continuation=line; + while ((continuation=strchr(continuation,'\a'))!=NULL){ + /* don't give the error if the next line is also commented out. + it is quite annoying to get an error for commenting out a define using: + + // + // #define LONG_MACRO\ + // did span \ + // multiple lines + // + */ + while (*continuation<=' ' && *continuation!='\0') + continuation++; /* skip whitespace */ + if (*continuation!='/' || *(continuation+1)!='/') { + error(49); /* invalid line continuation */ + break; + } + } #if !defined SC_LIGHT if (*(line+2)=='/' && *(line+3)<=' ') { /* documentation comment */ @@ -2062,7 +2079,7 @@ static const unsigned char *packedstring(const unsigned char *lptr,int *flags) if (*lptr==',' || *lptr==')' || *lptr=='}' || *lptr==';' || *lptr==':' || *lptr=='\n' || *lptr=='\r') - lptr=stringize; /* backtrack to end of last string for closing " */ + lptr=stringize; /* backtrack to end of last string for closing " */ return lptr; } From aa71219b8d6320dd16c44348095d892a28d752de Mon Sep 17 00:00:00 2001 From: Y_Less Date: Sat, 30 Jun 2018 17:09:53 +0200 Subject: [PATCH 05/12] Remove multi-line comment test. --- source/compiler/tests/gh_217.pwn | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/compiler/tests/gh_217.pwn b/source/compiler/tests/gh_217.pwn index 2d2d24c..c84af2c 100644 --- a/source/compiler/tests/gh_217.pwn +++ b/source/compiler/tests/gh_217.pwn @@ -17,7 +17,8 @@ warning 3 warning 4 // single-line comments can span \ -multiple lines if you really want it +//multiple lines if you really want it +// no they can't, only with more //s. #pragma deprecated don't\ use \ From 2f33bbae7672011287542d5f96a7ecab9b725d05 Mon Sep 17 00:00:00 2001 From: Y_Less Date: Sat, 30 Jun 2018 17:14:12 +0200 Subject: [PATCH 06/12] Fix test 217 again... --- source/compiler/tests/CMakeLists.txt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/source/compiler/tests/CMakeLists.txt b/source/compiler/tests/CMakeLists.txt index ef77935..57538fc 100644 --- a/source/compiler/tests/CMakeLists.txt +++ b/source/compiler/tests/CMakeLists.txt @@ -16,12 +16,12 @@ endfunction() # doesn't match the expected pattern. add_compiler_test(gh_217 ${CMAKE_CURRENT_SOURCE_DIR}/gh_217.pwn) -set_tests_properties(gh_217 PROPERTIES PASS_REGULAR_EXPRESSION ".*\\.pwn\\(11\\) : warning 237: user warning: this is warning 1 -.*\\.pwn\\(13\\) : warning 237: user warning: this iswarning 2 -.*\\.pwn\\(15\\) : warning 237: user warning: this is warning 3 -.*\\.pwn\\(17\\) : warning 237: user warning: this is warning 4 -.*\\.pwn\\(27\\) : warning 234: function is deprecated \\(symbol \"f\"\\) don't use this functionplease -.*\\.pwn\\(32\\) : warning 234: function is deprecated \\(symbol \"f\"\\) don't use this functionplease +set_tests_properties(gh_217 PROPERTIES PASS_REGULAR_EXPRESSION ".*\\.pwn\\(11\\) : warning 237: user warning: this is warning 1 +.*\\.pwn\\(13\\) : warning 237: user warning: this is warning 2 +.*\\.pwn\\(15\\) : warning 237: user warning: this is warning 3 +.*\\.pwn\\(17\\) : warning 237: user warning: this is warning 4 +.*\\.pwn\\(27\\) : warning 234: function is deprecated \\(symbol \"f\"\\) don't use this function please +.*\\.pwn\\(32\\) : warning 234: function is deprecated \\(symbol \"f\"\\) don't use this function please ") add_compiler_test(reset_errline_gh_230 ${CMAKE_CURRENT_SOURCE_DIR}/reset_errline_gh_230.pwn) From 000b4d52b98ae26e7acb83cd1642dee7dbcc7e38 Mon Sep 17 00:00:00 2001 From: Y_Less Date: Sat, 30 Jun 2018 17:23:12 +0200 Subject: [PATCH 07/12] Cast error on mac. Yet again fixing the tests... --- source/compiler/sc2.c | 2 +- source/compiler/tests/CMakeLists.txt | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/source/compiler/sc2.c b/source/compiler/sc2.c index ffa5e25..ef9151e 100644 --- a/source/compiler/sc2.c +++ b/source/compiler/sc2.c @@ -520,7 +520,7 @@ static void stripcomment(unsigned char *line) if (icomment==2) *line++=' '; } else if (*line=='/' && *(line+1)=='/'){ /* comment to end of line */ - continuation=line; + continuation=(char*)line; while ((continuation=strchr(continuation,'\a'))!=NULL){ /* don't give the error if the next line is also commented out. it is quite annoying to get an error for commenting out a define using: diff --git a/source/compiler/tests/CMakeLists.txt b/source/compiler/tests/CMakeLists.txt index 57538fc..5d22a65 100644 --- a/source/compiler/tests/CMakeLists.txt +++ b/source/compiler/tests/CMakeLists.txt @@ -20,8 +20,8 @@ set_tests_properties(gh_217 PROPERTIES PASS_REGULAR_EXPRESSION ".*\\.pwn\\(11\\) .*\\.pwn\\(13\\) : warning 237: user warning: this is warning 2 .*\\.pwn\\(15\\) : warning 237: user warning: this is warning 3 .*\\.pwn\\(17\\) : warning 237: user warning: this is warning 4 -.*\\.pwn\\(27\\) : warning 234: function is deprecated \\(symbol \"f\"\\) don't use this function please -.*\\.pwn\\(32\\) : warning 234: function is deprecated \\(symbol \"f\"\\) don't use this function please +.*\\.pwn\\(28\\) : warning 234: function is deprecated \\(symbol \"f\"\\) don't use this function please +.*\\.pwn\\(33\\) : warning 234: function is deprecated \\(symbol \"f\"\\) don't use this function please ") add_compiler_test(reset_errline_gh_230 ${CMAKE_CURRENT_SOURCE_DIR}/reset_errline_gh_230.pwn) From 116ec5f0fd9c8a5e0ce55752b1343c6154792239 Mon Sep 17 00:00:00 2001 From: Yashas Date: Sun, 17 Jun 2018 12:07:24 +0530 Subject: [PATCH 08/12] mark array arguments of public functions modified The arguments of public functions are marked as READ by default to prevent 203/204 warnings. The reason is probably that there is no requirement imposed by the host on the user that they read all the arguments. Smilarly the array/reference arguments need not be modified but the forward takes both cases into consideration. This prevents warning 214 on array arguments of public functions. --- source/compiler/sc1.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index b3eddeb..4f1d7bc 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -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 */ From ffb161ad955f04f5ca59a3c60fa4d9e35c5a67d7 Mon Sep 17 00:00:00 2001 From: Yashas Date: Sun, 24 Jun 2018 10:15:03 +0530 Subject: [PATCH 09/12] check array depends for modifications for W214 --- source/compiler/sc1.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index 4f1d7bc..de4796a 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -4907,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) From 8948f207ea9c7cef4e7808751a499310d1407148 Mon Sep 17 00:00:00 2001 From: Yashas Date: Sun, 24 Jun 2018 10:20:30 +0530 Subject: [PATCH 10/12] disable 239 for native functions --- source/compiler/sc3.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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); From 1cdb8b7d6b90910bb08e6444e0c729dbf6d57f42 Mon Sep 17 00:00:00 2001 From: Yashas Date: Sun, 8 Jul 2018 13:29:52 +0530 Subject: [PATCH 11/12] add tests for i276 --- source/compiler/tests/CMakeLists.txt | 10 +++ .../const_array_args_and_literals_gh_276.pwn | 61 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 source/compiler/tests/const_array_args_and_literals_gh_276.pwn 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); +} From 0c9ddcfad1b58d29dccf7867d5a87e2dfa1fda65 Mon Sep 17 00:00:00 2001 From: Adrian Graber Date: Sun, 8 Jul 2018 18:00:15 +0200 Subject: [PATCH 12/12] Don't generate function preamble on naked functions --- source/compiler/sc.h | 2 +- source/compiler/sc1.c | 2 +- source/compiler/sc4.c | 8 +++++--- 3 files changed, 7 insertions(+), 5 deletions(-) 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..3cb0eff 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) { 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