1*13611Ssam static char *sccsid = "@(#)pi.c 1.3 (Berkeley) 07/02/83"; 21458Sroot #include <stdio.h> 31458Sroot #include <ctype.h> 41458Sroot #include "error.h" 51458Sroot 61458Sroot extern char *currentfilename; 71458Sroot static char *c_linenumber; 81458Sroot static char *unk_hdr[] = {"In", "program", "???"}; 91458Sroot static char **c_header = &unk_hdr[0]; 101458Sroot 111458Sroot /* 121458Sroot * Attempt to handle error messages produced by pi (and by pc) 131458Sroot * 141458Sroot * problem #1: There is no file name available when a file does not 151458Sroot * use a #include; this will have to be given to error 161458Sroot * in the command line. 171458Sroot * problem #2: pi doesn't always tell you what line number 181458Sroot * a error refers to; for example during the tree 191458Sroot * walk phase of code generation and error detection, 201458Sroot * an error can refer to "variable foo in procedure bletch" 211458Sroot * without giving a line number 221458Sroot * problem #3: line numbers, when available, are attached to 231458Sroot * the source line, along with the source line itself 241458Sroot * These line numbers must be extracted, and 251458Sroot * the source line thrown away. 261458Sroot * problem #4: Some error messages produce more than one line number 271458Sroot * on the same message. 281458Sroot * There are only two (I think): 291458Sroot * %s undefined on line%s 301458Sroot * %s improperly used on line%s 311458Sroot * here, the %s makes line plural or singular. 321458Sroot * 331458Sroot * Here are the error strings used in pi version 1.2 that can refer 341458Sroot * to a file name or line number: 351458Sroot * 361458Sroot * Multiply defined label in case, lines %d and %d 371458Sroot * Goto %s from line %d is into a structured statement 381458Sroot * End matched %s on line %d 391458Sroot * Inserted keyword end matching %s on line %d 401458Sroot * 411458Sroot * Here are the general pi patterns recognized: 421458Sroot * define piptr == -.*^-.* 431458Sroot * define msg = .* 441458Sroot * define digit = [0-9] 451458Sroot * definename = .* 461458Sroot * define date_format letter*3 letter*3 (digit | (digit digit)) 471458Sroot * (digit | (digit digit)):digit*2 digit*4 481458Sroot * 491458Sroot * {e,E} (piptr) (msg) Encounter an error during textual scan 501458Sroot * E {digit}* - (msg) Have an error message that refers to a new line 511458Sroot * E - msg Have an error message that refers to current 521458Sroot * function, program or procedure 531458Sroot * (date_format) (name): When switch compilation files 541458Sroot * ... (msg) When refer to the previous line 551458Sroot * 'In' ('procedure'|'function'|'program') (name): 561458Sroot * pi is now complaining about 2nd pass errors. 571458Sroot * 581458Sroot * Here is the output from a compilation 591458Sroot * 601458Sroot * 611458Sroot * 2 var i:integer; 621458Sroot * e --------------^--- Inserted ';' 631458Sroot * E 2 - All variables must be declared in one var part 641458Sroot * E 5 - Include filename must end in .i 651458Sroot * Mon Apr 21 15:56 1980 test.h: 661458Sroot * 2 begin 671458Sroot * e ------^--- Inserted ';' 681458Sroot * Mon Apr 21 16:06 1980 test.p: 691458Sroot * E 2 - Function type must be specified 701458Sroot * 6 procedure foo(var x:real); 711458Sroot * e ------^--- Inserted ';' 721458Sroot * In function bletch: 731458Sroot * E - No assignment to the function variable 741458Sroot * w - variable x is never used 751458Sroot * E 6 - foo is already defined in this block 761458Sroot * In procedure foo: 771458Sroot * w - variable x is neither used nor set 781458Sroot * 9 z : = 23; 791458Sroot * E --------------^--- Undefined variable 801458Sroot * 10 y = [1]; 811458Sroot * e ----------------^--- Inserted ':' 821458Sroot * 13 z := 345.; 831458Sroot * e -----------------------^--- Digits required after decimal point 841458Sroot * E 10 - Constant set involved in non set context 851458Sroot * E 11 - Type clash: real is incompatible with integer 861458Sroot * ... Type of expression clashed with type of variable in assignment 871458Sroot * E 12 - Parameter type not identical to type of var parameter x of foo 881458Sroot * In program mung: 891458Sroot * w - variable y is never used 901458Sroot * w - type foo is never used 911458Sroot * w - function bletch is never used 921458Sroot * E - z undefined on lines 9 13 931458Sroot */ 941458Sroot char *Months[] = { 951458Sroot "Jan", "Feb", "Mar", "Apr", "May", "Jun", 961458Sroot "Jul", "Aug", "Sep", "Oct","Nov", "Dec", 971458Sroot 0 981458Sroot }; 991458Sroot char *Days[] = { 1001458Sroot "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", 0 1011458Sroot }; 1021458Sroot char *Piroutines[] = { 1031458Sroot "program", "function", "procedure", 0 1041458Sroot }; 1051458Sroot 1061458Sroot 1071458Sroot static boolean structured, multiple; 1081458Sroot 1091458Sroot char *pi_Endmatched[] = {"End", "matched"}; 1101458Sroot char *pi_Inserted[] = {"Inserted", "keyword", "end", "matching"}; 1111458Sroot 1121458Sroot char *pi_multiple[] = {"Mutiply", "defined", "label", "in", "case,", "line"}; 1131458Sroot char *pi_structured[] = {"is", "into", "a", "structured", "statement"}; 1141458Sroot 1151458Sroot char *pi_und1[] = {"undefined", "on", "line"}; 1161458Sroot char *pi_und2[] = {"undefined", "on", "lines"}; 1171458Sroot char *pi_imp1[] = {"improperly", "used", "on", "line"}; 1181458Sroot char *pi_imp2[] = {"improperly", "used", "on", "lines"}; 1191458Sroot 1201458Sroot boolean alldigits(string) 1215593Srrh reg char *string; 1221458Sroot { 1231458Sroot for (; *string && isdigit(*string); string++) 1241458Sroot continue; 1251458Sroot return(*string == '\0'); 1261458Sroot } 1271458Sroot boolean instringset(member, set) 1285593Srrh char *member; 1295593Srrh reg char **set; 1301458Sroot { 1311458Sroot for(; *set; set++){ 1321458Sroot if (strcmp(*set, member) == 0) 1331458Sroot return(TRUE); 1341458Sroot } 1351458Sroot return(FALSE); 1361458Sroot } 1371458Sroot 1381458Sroot boolean isdateformat(wordc, wordv) 1391458Sroot int wordc; 1401458Sroot char **wordv; 1411458Sroot { 1421458Sroot return( 1431458Sroot (wordc == 5) 1441458Sroot && (instringset(wordv[0], Days)) 1451458Sroot && (instringset(wordv[1], Months)) 1461458Sroot && (alldigits(wordv[2])) 1471458Sroot && (alldigits(wordv[4])) ); 1481458Sroot } 1491458Sroot 1501458Sroot boolean piptr(string) 1515593Srrh reg char *string; 1521458Sroot { 1531458Sroot if (*string != '-') 1541458Sroot return(FALSE); 1551458Sroot while (*string && *string == '-') 1561458Sroot string++; 1571458Sroot if (*string != '^') 1581458Sroot return(FALSE); 1591458Sroot string++; 1601458Sroot while (*string && *string == '-') 1611458Sroot string++; 1621458Sroot return(*string == '\0'); 1631458Sroot } 1641458Sroot 1651458Sroot extern int wordc; 1661458Sroot extern char **wordv; 1671458Sroot 1681458Sroot Errorclass pi() 1691458Sroot { 1701458Sroot char **nwordv; 1711458Sroot 172*13611Ssam if (wordc < 2) 173*13611Ssam return (C_UNKNOWN); 1741458Sroot if ( ( strlen(wordv[1]) == 1) 1751458Sroot && ( (wordv[1][0] == 'e') || (wordv[1][0] == 'E') ) 1761458Sroot && ( piptr(wordv[2]) ) 1771458Sroot ) { 1781458Sroot boolean longpiptr = 0; 1791458Sroot /* 1801458Sroot * We have recognized a first pass error of the form: 1811458Sroot * letter ------^---- message 1821458Sroot * 1831458Sroot * turn into an error message of the form: 1841458Sroot * 1851458Sroot * file line 'pascal errortype' letter \n |---- message 1861458Sroot * or of the form: 1871458Sroot * file line letter |---- message 1881458Sroot * when there are strlen("(*[pi]") or more 1891458Sroot * preceding '-' on the error pointer. 1901458Sroot * 1911458Sroot * Where the | is intended to be a down arrow, so that 1921458Sroot * the pi error messages can be inserted above the 1931458Sroot * line in error, instead of below. (All of the other 1941458Sroot * langauges put thier messages before the source line, 1951458Sroot * instead of after it as does pi.) 1961458Sroot * 1971458Sroot * where the pointer to the error has been truncated 1981458Sroot * by 6 characters to account for the fact that 1991458Sroot * the pointer points into a tab preceded input line. 2001458Sroot */ 2011458Sroot language = INPI; 2025593Srrh (void)substitute(wordv[2], '^', '|'); 2031458Sroot longpiptr = position(wordv[2],'|') > (6+8); 2041458Sroot nwordv = wordvsplice(longpiptr ? 2 : 4, wordc, wordv+1); 2051458Sroot nwordv[0] = strsave(currentfilename); 2061458Sroot nwordv[1] = strsave(c_linenumber); 2071458Sroot if (!longpiptr){ 2081458Sroot nwordv[2] = "pascal errortype"; 2091458Sroot nwordv[3] = wordv[1]; 2101458Sroot nwordv[4] = strsave("%%%\n"); 2111458Sroot if (strlen(nwordv[5]) > (8-2)) /* this is the pointer */ 2121458Sroot nwordv[5] += (8-2); /* bump over 6 characters */ 2131458Sroot } 2141458Sroot wordv = nwordv - 1; /* convert to 1 based */ 2151458Sroot wordc += longpiptr ? 2 : 4; 2161458Sroot return(C_TRUE); 2171458Sroot } 2181458Sroot if ( (wordc >= 4) 2191458Sroot && (strlen(wordv[1]) == 1) 2201458Sroot && ( (*wordv[1] == 'E') || (*wordv[1] == 'w') || (*wordv[1] == 'e') ) 2211458Sroot && (alldigits(wordv[2])) 2221458Sroot && (strlen(wordv[3]) == 1) 2231458Sroot && (wordv[3][0] == '-') 2241458Sroot ){ 2251458Sroot /* 2261458Sroot * Message of the form: letter linenumber - message 2271458Sroot * Turn into form: filename linenumber letter - message 2281458Sroot */ 2291458Sroot language = INPI; 2301458Sroot nwordv = wordvsplice(1, wordc, wordv + 1); 2311458Sroot nwordv[0] = strsave(currentfilename); 2321458Sroot nwordv[1] = wordv[2]; 2331458Sroot nwordv[2] = wordv[1]; 2341458Sroot c_linenumber = wordv[2]; 2351458Sroot wordc += 1; 2361458Sroot wordv = nwordv - 1; 2371458Sroot return(C_TRUE); 2381458Sroot } 2391458Sroot if ( (wordc >= 3) 2401458Sroot && (strlen(wordv[1]) == 1) 2411458Sroot && ( (*(wordv[1]) == 'E') || (*(wordv[1]) == 'w') || (*(wordv[1]) == 'e') ) 2421458Sroot && (strlen(wordv[2]) == 1) 2431458Sroot && (wordv[2][0] == '-') 2441458Sroot ) { 2451458Sroot /* 2461458Sroot * Message of the form: letter - message 2471458Sroot * This happens only when we are traversing the tree 2481458Sroot * during the second pass of pi, and discover semantic 2491458Sroot * errors. 2501458Sroot * 2511458Sroot * We have already (presumably) saved the header message 2521458Sroot * and can now construct a nulled error message for the 2531458Sroot * current file. 2541458Sroot * 2551458Sroot * Turns into a message of the form: 2561458Sroot * filename (header) letter - message 2571458Sroot * 2581458Sroot * First, see if it is a message referring to more than 2591458Sroot * one line number. Only of the form: 2601458Sroot * %s undefined on line%s 2611458Sroot * %s improperly used on line%s 2621458Sroot */ 2631458Sroot boolean undefined = 0; 2641458Sroot int wordindex; 2651458Sroot 2661458Sroot language = INPI; 2671458Sroot if ( (undefined = (wordvcmp(wordv+2, 3, pi_und1) == 0) ) 2681458Sroot || (undefined = (wordvcmp(wordv+2, 3, pi_und2) == 0) ) 2691458Sroot || (wordvcmp(wordv+2, 4, pi_imp1) == 0) 2701458Sroot || (wordvcmp(wordv+2, 4, pi_imp2) == 0) 2711458Sroot ){ 2721458Sroot for (wordindex = undefined ? 5 : 6; wordindex <= wordc; 2731458Sroot wordindex++){ 2741458Sroot nwordv = wordvsplice(2, undefined ? 2 : 3, wordv+1); 2751458Sroot nwordv[0] = strsave(currentfilename); 2761458Sroot nwordv[1] = wordv[wordindex]; 2771458Sroot if (wordindex != wordc) 2781458Sroot erroradd(undefined ? 4 : 5, nwordv, 2791458Sroot C_TRUE, C_UNKNOWN); 2801458Sroot } 2811458Sroot wordc = undefined ? 4 : 5; 2821458Sroot wordv = nwordv - 1; 2831458Sroot return(C_TRUE); 2841458Sroot } 2851458Sroot 2861458Sroot nwordv = wordvsplice(1+3, wordc, wordv+1); 2871458Sroot nwordv[0] = strsave(currentfilename); 2881458Sroot nwordv[1] = strsave(c_header[0]); 2891458Sroot nwordv[2] = strsave(c_header[1]); 2901458Sroot nwordv[3] = strsave(c_header[2]); 2911458Sroot wordv = nwordv - 1; 2921458Sroot wordc += 1 + 3; 2931458Sroot return(C_THISFILE); 2941458Sroot } 2951458Sroot if (strcmp(wordv[1], "...") == 0){ 2961458Sroot /* 2971458Sroot * have a continuation error message 2981458Sroot * of the form: ... message 2991458Sroot * Turn into form : filename linenumber message 3001458Sroot */ 3011458Sroot language = INPI; 3021458Sroot nwordv = wordvsplice(1, wordc, wordv+1); 3031458Sroot nwordv[0] = strsave(currentfilename); 3041458Sroot nwordv[1] = strsave(c_linenumber); 3051458Sroot wordv = nwordv - 1; 3061458Sroot wordc += 1; 3071458Sroot return(C_TRUE); 3081458Sroot } 3091458Sroot if( (wordc == 6) 3101458Sroot && (lastchar(wordv[6]) == ':') 3111458Sroot && (isdateformat(5, wordv + 1)) 3121458Sroot ){ 3131458Sroot /* 3141458Sroot * Have message that tells us we have changed files 3151458Sroot */ 3161458Sroot language = INPI; 3171458Sroot currentfilename = strsave(wordv[6]); 3181458Sroot clob_last(currentfilename, '\0'); 3191458Sroot return(C_SYNC); 3201458Sroot } 3211458Sroot if( (wordc == 3) 3221458Sroot && (strcmp(wordv[1], "In") == 0) 3231458Sroot && (lastchar(wordv[3]) == ':') 3241458Sroot && (instringset(wordv[2], Piroutines)) 3251458Sroot ) { 3261458Sroot language = INPI; 3271458Sroot c_header = wordvsplice(0, wordc, wordv+1); 3281458Sroot return(C_SYNC); 3291458Sroot } 3301458Sroot /* 3311458Sroot * now, check for just the line number followed by the text 3321458Sroot */ 3331458Sroot if (alldigits(wordv[1])){ 3341458Sroot language = INPI; 3351458Sroot c_linenumber = wordv[1]; 3361458Sroot return(C_IGNORE); 3371458Sroot } 3381458Sroot /* 3391458Sroot * Attempt to match messages refering to a line number 3401458Sroot * 3411458Sroot * Multiply defined label in case, lines %d and %d 3421458Sroot * Goto %s from line %d is into a structured statement 3431458Sroot * End matched %s on line %d 3441458Sroot * Inserted keyword end matching %s on line %d 3451458Sroot */ 3461458Sroot multiple = structured = 0; 3471458Sroot if ( 3481458Sroot ( (wordc == 6) && (wordvcmp(wordv+1, 2, pi_Endmatched) == 0)) 3491458Sroot || ( (wordc == 8) && (wordvcmp(wordv+1, 4, pi_Inserted) == 0)) 3501458Sroot || ( multiple = ((wordc == 9) && (wordvcmp(wordv+1,6, pi_multiple) == 0) ) ) 3511458Sroot || ( structured = ((wordc == 10) && (wordvcmp(wordv+6,5, pi_structured) == 0 ) )) 3521458Sroot ){ 3531458Sroot language = INPI; 3541458Sroot nwordv = wordvsplice(2, wordc, wordv+1); 3551458Sroot nwordv[0] = strsave(currentfilename); 3561458Sroot nwordv[1] = structured ? wordv [5] : wordv[wordc]; 3571458Sroot wordc += 2; 3581458Sroot wordv = nwordv - 1; 3591458Sroot if (!multiple) 3601458Sroot return(C_TRUE); 3611458Sroot erroradd(wordc, nwordv, C_TRUE, C_UNKNOWN); 3621458Sroot nwordv = wordvsplice(0, wordc, nwordv); 3631458Sroot nwordv[1] = wordv[wordc - 2]; 3641458Sroot return(C_TRUE); 3651458Sroot } 3661458Sroot return(C_UNKNOWN); 3671458Sroot } 368