114531783Sdist /*
2*06380a9eSbostic * Copyright (c) 1980, 1993
3*06380a9eSbostic * The Regents of the University of California. All rights reserved.
48da9c229Sbostic *
51808f06cSbostic * %sccs.include.redist.c%
614531783Sdist */
714531783Sdist
814531783Sdist #ifndef lint
9*06380a9eSbostic static char sccsid[] = "@(#)pi.c 8.1 (Berkeley) 06/06/93";
108da9c229Sbostic #endif /* not lint */
1114531783Sdist
128f7b35b5Sroot #include <stdio.h>
138f7b35b5Sroot #include <ctype.h>
14583df676Sbostic #include <string.h>
158f7b35b5Sroot #include "error.h"
168f7b35b5Sroot
178f7b35b5Sroot extern char *currentfilename;
188f7b35b5Sroot static char *c_linenumber;
198f7b35b5Sroot static char *unk_hdr[] = {"In", "program", "???"};
208f7b35b5Sroot static char **c_header = &unk_hdr[0];
218f7b35b5Sroot
228f7b35b5Sroot /*
238f7b35b5Sroot * Attempt to handle error messages produced by pi (and by pc)
248f7b35b5Sroot *
258f7b35b5Sroot * problem #1: There is no file name available when a file does not
268f7b35b5Sroot * use a #include; this will have to be given to error
278f7b35b5Sroot * in the command line.
288f7b35b5Sroot * problem #2: pi doesn't always tell you what line number
298f7b35b5Sroot * a error refers to; for example during the tree
308f7b35b5Sroot * walk phase of code generation and error detection,
318f7b35b5Sroot * an error can refer to "variable foo in procedure bletch"
328f7b35b5Sroot * without giving a line number
338f7b35b5Sroot * problem #3: line numbers, when available, are attached to
348f7b35b5Sroot * the source line, along with the source line itself
358f7b35b5Sroot * These line numbers must be extracted, and
368f7b35b5Sroot * the source line thrown away.
378f7b35b5Sroot * problem #4: Some error messages produce more than one line number
388f7b35b5Sroot * on the same message.
398f7b35b5Sroot * There are only two (I think):
408f7b35b5Sroot * %s undefined on line%s
418f7b35b5Sroot * %s improperly used on line%s
428f7b35b5Sroot * here, the %s makes line plural or singular.
438f7b35b5Sroot *
448f7b35b5Sroot * Here are the error strings used in pi version 1.2 that can refer
458f7b35b5Sroot * to a file name or line number:
468f7b35b5Sroot *
478f7b35b5Sroot * Multiply defined label in case, lines %d and %d
488f7b35b5Sroot * Goto %s from line %d is into a structured statement
498f7b35b5Sroot * End matched %s on line %d
508f7b35b5Sroot * Inserted keyword end matching %s on line %d
518f7b35b5Sroot *
528f7b35b5Sroot * Here are the general pi patterns recognized:
538f7b35b5Sroot * define piptr == -.*^-.*
548f7b35b5Sroot * define msg = .*
558f7b35b5Sroot * define digit = [0-9]
568f7b35b5Sroot * definename = .*
578f7b35b5Sroot * define date_format letter*3 letter*3 (digit | (digit digit))
588f7b35b5Sroot * (digit | (digit digit)):digit*2 digit*4
598f7b35b5Sroot *
608f7b35b5Sroot * {e,E} (piptr) (msg) Encounter an error during textual scan
618f7b35b5Sroot * E {digit}* - (msg) Have an error message that refers to a new line
628f7b35b5Sroot * E - msg Have an error message that refers to current
638f7b35b5Sroot * function, program or procedure
648f7b35b5Sroot * (date_format) (name): When switch compilation files
658f7b35b5Sroot * ... (msg) When refer to the previous line
668f7b35b5Sroot * 'In' ('procedure'|'function'|'program') (name):
678f7b35b5Sroot * pi is now complaining about 2nd pass errors.
688f7b35b5Sroot *
698f7b35b5Sroot * Here is the output from a compilation
708f7b35b5Sroot *
718f7b35b5Sroot *
728f7b35b5Sroot * 2 var i:integer;
738f7b35b5Sroot * e --------------^--- Inserted ';'
748f7b35b5Sroot * E 2 - All variables must be declared in one var part
758f7b35b5Sroot * E 5 - Include filename must end in .i
768f7b35b5Sroot * Mon Apr 21 15:56 1980 test.h:
778f7b35b5Sroot * 2 begin
788f7b35b5Sroot * e ------^--- Inserted ';'
798f7b35b5Sroot * Mon Apr 21 16:06 1980 test.p:
808f7b35b5Sroot * E 2 - Function type must be specified
818f7b35b5Sroot * 6 procedure foo(var x:real);
828f7b35b5Sroot * e ------^--- Inserted ';'
838f7b35b5Sroot * In function bletch:
848f7b35b5Sroot * E - No assignment to the function variable
858f7b35b5Sroot * w - variable x is never used
868f7b35b5Sroot * E 6 - foo is already defined in this block
878f7b35b5Sroot * In procedure foo:
888f7b35b5Sroot * w - variable x is neither used nor set
898f7b35b5Sroot * 9 z : = 23;
908f7b35b5Sroot * E --------------^--- Undefined variable
918f7b35b5Sroot * 10 y = [1];
928f7b35b5Sroot * e ----------------^--- Inserted ':'
938f7b35b5Sroot * 13 z := 345.;
948f7b35b5Sroot * e -----------------------^--- Digits required after decimal point
958f7b35b5Sroot * E 10 - Constant set involved in non set context
968f7b35b5Sroot * E 11 - Type clash: real is incompatible with integer
978f7b35b5Sroot * ... Type of expression clashed with type of variable in assignment
988f7b35b5Sroot * E 12 - Parameter type not identical to type of var parameter x of foo
998f7b35b5Sroot * In program mung:
1008f7b35b5Sroot * w - variable y is never used
1018f7b35b5Sroot * w - type foo is never used
1028f7b35b5Sroot * w - function bletch is never used
1038f7b35b5Sroot * E - z undefined on lines 9 13
1048f7b35b5Sroot */
1058f7b35b5Sroot char *Months[] = {
1068f7b35b5Sroot "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1078f7b35b5Sroot "Jul", "Aug", "Sep", "Oct","Nov", "Dec",
1088f7b35b5Sroot 0
1098f7b35b5Sroot };
1108f7b35b5Sroot char *Days[] = {
1118f7b35b5Sroot "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", 0
1128f7b35b5Sroot };
1138f7b35b5Sroot char *Piroutines[] = {
1148f7b35b5Sroot "program", "function", "procedure", 0
1158f7b35b5Sroot };
1168f7b35b5Sroot
1178f7b35b5Sroot
1188f7b35b5Sroot static boolean structured, multiple;
1198f7b35b5Sroot
1208f7b35b5Sroot char *pi_Endmatched[] = {"End", "matched"};
1218f7b35b5Sroot char *pi_Inserted[] = {"Inserted", "keyword", "end", "matching"};
1228f7b35b5Sroot
1238f7b35b5Sroot char *pi_multiple[] = {"Mutiply", "defined", "label", "in", "case,", "line"};
1248f7b35b5Sroot char *pi_structured[] = {"is", "into", "a", "structured", "statement"};
1258f7b35b5Sroot
1268f7b35b5Sroot char *pi_und1[] = {"undefined", "on", "line"};
1278f7b35b5Sroot char *pi_und2[] = {"undefined", "on", "lines"};
1288f7b35b5Sroot char *pi_imp1[] = {"improperly", "used", "on", "line"};
1298f7b35b5Sroot char *pi_imp2[] = {"improperly", "used", "on", "lines"};
1308f7b35b5Sroot
alldigits(string)1318f7b35b5Sroot boolean alldigits(string)
1322357a0a9Srrh reg char *string;
1338f7b35b5Sroot {
1348f7b35b5Sroot for (; *string && isdigit(*string); string++)
1358f7b35b5Sroot continue;
1368f7b35b5Sroot return(*string == '\0');
1378f7b35b5Sroot }
instringset(member,set)1388f7b35b5Sroot boolean instringset(member, set)
1398f7b35b5Sroot char *member;
1402357a0a9Srrh reg char **set;
1418f7b35b5Sroot {
1428f7b35b5Sroot for(; *set; set++){
1438f7b35b5Sroot if (strcmp(*set, member) == 0)
1448f7b35b5Sroot return(TRUE);
1458f7b35b5Sroot }
1468f7b35b5Sroot return(FALSE);
1478f7b35b5Sroot }
1488f7b35b5Sroot
isdateformat(wordc,wordv)1498f7b35b5Sroot boolean isdateformat(wordc, wordv)
1508f7b35b5Sroot int wordc;
1518f7b35b5Sroot char **wordv;
1528f7b35b5Sroot {
1538f7b35b5Sroot return(
1548f7b35b5Sroot (wordc == 5)
1558f7b35b5Sroot && (instringset(wordv[0], Days))
1568f7b35b5Sroot && (instringset(wordv[1], Months))
1578f7b35b5Sroot && (alldigits(wordv[2]))
1588f7b35b5Sroot && (alldigits(wordv[4])) );
1598f7b35b5Sroot }
1608f7b35b5Sroot
piptr(string)1618f7b35b5Sroot boolean piptr(string)
1622357a0a9Srrh reg char *string;
1638f7b35b5Sroot {
1648f7b35b5Sroot if (*string != '-')
1658f7b35b5Sroot return(FALSE);
1668f7b35b5Sroot while (*string && *string == '-')
1678f7b35b5Sroot string++;
1688f7b35b5Sroot if (*string != '^')
1698f7b35b5Sroot return(FALSE);
1708f7b35b5Sroot string++;
1718f7b35b5Sroot while (*string && *string == '-')
1728f7b35b5Sroot string++;
1738f7b35b5Sroot return(*string == '\0');
1748f7b35b5Sroot }
1758f7b35b5Sroot
1768f7b35b5Sroot extern int wordc;
1778f7b35b5Sroot extern char **wordv;
1788f7b35b5Sroot
pi()1798f7b35b5Sroot Errorclass pi()
1808f7b35b5Sroot {
1818f7b35b5Sroot char **nwordv;
1828f7b35b5Sroot
1837589d586Ssam if (wordc < 2)
1847589d586Ssam return (C_UNKNOWN);
1858f7b35b5Sroot if ( ( strlen(wordv[1]) == 1)
1868f7b35b5Sroot && ( (wordv[1][0] == 'e') || (wordv[1][0] == 'E') )
1878f7b35b5Sroot && ( piptr(wordv[2]) )
1888f7b35b5Sroot ) {
1898f7b35b5Sroot boolean longpiptr = 0;
1908f7b35b5Sroot /*
1918f7b35b5Sroot * We have recognized a first pass error of the form:
1928f7b35b5Sroot * letter ------^---- message
1938f7b35b5Sroot *
1948f7b35b5Sroot * turn into an error message of the form:
1958f7b35b5Sroot *
1968f7b35b5Sroot * file line 'pascal errortype' letter \n |---- message
1978f7b35b5Sroot * or of the form:
1988f7b35b5Sroot * file line letter |---- message
1998f7b35b5Sroot * when there are strlen("(*[pi]") or more
2008f7b35b5Sroot * preceding '-' on the error pointer.
2018f7b35b5Sroot *
2028f7b35b5Sroot * Where the | is intended to be a down arrow, so that
2038f7b35b5Sroot * the pi error messages can be inserted above the
2048f7b35b5Sroot * line in error, instead of below. (All of the other
2058f7b35b5Sroot * langauges put thier messages before the source line,
2068f7b35b5Sroot * instead of after it as does pi.)
2078f7b35b5Sroot *
2088f7b35b5Sroot * where the pointer to the error has been truncated
2098f7b35b5Sroot * by 6 characters to account for the fact that
2108f7b35b5Sroot * the pointer points into a tab preceded input line.
2118f7b35b5Sroot */
2128f7b35b5Sroot language = INPI;
2132357a0a9Srrh (void)substitute(wordv[2], '^', '|');
2148f7b35b5Sroot longpiptr = position(wordv[2],'|') > (6+8);
2158f7b35b5Sroot nwordv = wordvsplice(longpiptr ? 2 : 4, wordc, wordv+1);
2168f7b35b5Sroot nwordv[0] = strsave(currentfilename);
2178f7b35b5Sroot nwordv[1] = strsave(c_linenumber);
2188f7b35b5Sroot if (!longpiptr){
2198f7b35b5Sroot nwordv[2] = "pascal errortype";
2208f7b35b5Sroot nwordv[3] = wordv[1];
2218f7b35b5Sroot nwordv[4] = strsave("%%%\n");
2228f7b35b5Sroot if (strlen(nwordv[5]) > (8-2)) /* this is the pointer */
2238f7b35b5Sroot nwordv[5] += (8-2); /* bump over 6 characters */
2248f7b35b5Sroot }
2258f7b35b5Sroot wordv = nwordv - 1; /* convert to 1 based */
2268f7b35b5Sroot wordc += longpiptr ? 2 : 4;
2278f7b35b5Sroot return(C_TRUE);
2288f7b35b5Sroot }
2298f7b35b5Sroot if ( (wordc >= 4)
2308f7b35b5Sroot && (strlen(wordv[1]) == 1)
2318f7b35b5Sroot && ( (*wordv[1] == 'E') || (*wordv[1] == 'w') || (*wordv[1] == 'e') )
2328f7b35b5Sroot && (alldigits(wordv[2]))
2338f7b35b5Sroot && (strlen(wordv[3]) == 1)
2348f7b35b5Sroot && (wordv[3][0] == '-')
2358f7b35b5Sroot ){
2368f7b35b5Sroot /*
2378f7b35b5Sroot * Message of the form: letter linenumber - message
2388f7b35b5Sroot * Turn into form: filename linenumber letter - message
2398f7b35b5Sroot */
2408f7b35b5Sroot language = INPI;
2418f7b35b5Sroot nwordv = wordvsplice(1, wordc, wordv + 1);
2428f7b35b5Sroot nwordv[0] = strsave(currentfilename);
2438f7b35b5Sroot nwordv[1] = wordv[2];
2448f7b35b5Sroot nwordv[2] = wordv[1];
2458f7b35b5Sroot c_linenumber = wordv[2];
2468f7b35b5Sroot wordc += 1;
2478f7b35b5Sroot wordv = nwordv - 1;
2488f7b35b5Sroot return(C_TRUE);
2498f7b35b5Sroot }
2508f7b35b5Sroot if ( (wordc >= 3)
2518f7b35b5Sroot && (strlen(wordv[1]) == 1)
2528f7b35b5Sroot && ( (*(wordv[1]) == 'E') || (*(wordv[1]) == 'w') || (*(wordv[1]) == 'e') )
2538f7b35b5Sroot && (strlen(wordv[2]) == 1)
2548f7b35b5Sroot && (wordv[2][0] == '-')
2558f7b35b5Sroot ) {
2568f7b35b5Sroot /*
2578f7b35b5Sroot * Message of the form: letter - message
2588f7b35b5Sroot * This happens only when we are traversing the tree
2598f7b35b5Sroot * during the second pass of pi, and discover semantic
2608f7b35b5Sroot * errors.
2618f7b35b5Sroot *
2628f7b35b5Sroot * We have already (presumably) saved the header message
2638f7b35b5Sroot * and can now construct a nulled error message for the
2648f7b35b5Sroot * current file.
2658f7b35b5Sroot *
2668f7b35b5Sroot * Turns into a message of the form:
2678f7b35b5Sroot * filename (header) letter - message
2688f7b35b5Sroot *
2698f7b35b5Sroot * First, see if it is a message referring to more than
2708f7b35b5Sroot * one line number. Only of the form:
2718f7b35b5Sroot * %s undefined on line%s
2728f7b35b5Sroot * %s improperly used on line%s
2738f7b35b5Sroot */
2748f7b35b5Sroot boolean undefined = 0;
2758f7b35b5Sroot int wordindex;
2768f7b35b5Sroot
2778f7b35b5Sroot language = INPI;
2788f7b35b5Sroot if ( (undefined = (wordvcmp(wordv+2, 3, pi_und1) == 0) )
2798f7b35b5Sroot || (undefined = (wordvcmp(wordv+2, 3, pi_und2) == 0) )
2808f7b35b5Sroot || (wordvcmp(wordv+2, 4, pi_imp1) == 0)
2818f7b35b5Sroot || (wordvcmp(wordv+2, 4, pi_imp2) == 0)
2828f7b35b5Sroot ){
2838f7b35b5Sroot for (wordindex = undefined ? 5 : 6; wordindex <= wordc;
2848f7b35b5Sroot wordindex++){
2858f7b35b5Sroot nwordv = wordvsplice(2, undefined ? 2 : 3, wordv+1);
2868f7b35b5Sroot nwordv[0] = strsave(currentfilename);
2878f7b35b5Sroot nwordv[1] = wordv[wordindex];
2888f7b35b5Sroot if (wordindex != wordc)
2898f7b35b5Sroot erroradd(undefined ? 4 : 5, nwordv,
2908f7b35b5Sroot C_TRUE, C_UNKNOWN);
2918f7b35b5Sroot }
2928f7b35b5Sroot wordc = undefined ? 4 : 5;
2938f7b35b5Sroot wordv = nwordv - 1;
2948f7b35b5Sroot return(C_TRUE);
2958f7b35b5Sroot }
2968f7b35b5Sroot
2978f7b35b5Sroot nwordv = wordvsplice(1+3, wordc, wordv+1);
2988f7b35b5Sroot nwordv[0] = strsave(currentfilename);
2998f7b35b5Sroot nwordv[1] = strsave(c_header[0]);
3008f7b35b5Sroot nwordv[2] = strsave(c_header[1]);
3018f7b35b5Sroot nwordv[3] = strsave(c_header[2]);
3028f7b35b5Sroot wordv = nwordv - 1;
3038f7b35b5Sroot wordc += 1 + 3;
3048f7b35b5Sroot return(C_THISFILE);
3058f7b35b5Sroot }
3068f7b35b5Sroot if (strcmp(wordv[1], "...") == 0){
3078f7b35b5Sroot /*
3088f7b35b5Sroot * have a continuation error message
3098f7b35b5Sroot * of the form: ... message
3108f7b35b5Sroot * Turn into form : filename linenumber message
3118f7b35b5Sroot */
3128f7b35b5Sroot language = INPI;
3138f7b35b5Sroot nwordv = wordvsplice(1, wordc, wordv+1);
3148f7b35b5Sroot nwordv[0] = strsave(currentfilename);
3158f7b35b5Sroot nwordv[1] = strsave(c_linenumber);
3168f7b35b5Sroot wordv = nwordv - 1;
3178f7b35b5Sroot wordc += 1;
3188f7b35b5Sroot return(C_TRUE);
3198f7b35b5Sroot }
3208f7b35b5Sroot if( (wordc == 6)
3218f7b35b5Sroot && (lastchar(wordv[6]) == ':')
3228f7b35b5Sroot && (isdateformat(5, wordv + 1))
3238f7b35b5Sroot ){
3248f7b35b5Sroot /*
3258f7b35b5Sroot * Have message that tells us we have changed files
3268f7b35b5Sroot */
3278f7b35b5Sroot language = INPI;
3288f7b35b5Sroot currentfilename = strsave(wordv[6]);
3298f7b35b5Sroot clob_last(currentfilename, '\0');
3308f7b35b5Sroot return(C_SYNC);
3318f7b35b5Sroot }
3328f7b35b5Sroot if( (wordc == 3)
3338f7b35b5Sroot && (strcmp(wordv[1], "In") == 0)
3348f7b35b5Sroot && (lastchar(wordv[3]) == ':')
3358f7b35b5Sroot && (instringset(wordv[2], Piroutines))
3368f7b35b5Sroot ) {
3378f7b35b5Sroot language = INPI;
3388f7b35b5Sroot c_header = wordvsplice(0, wordc, wordv+1);
3398f7b35b5Sroot return(C_SYNC);
3408f7b35b5Sroot }
3418f7b35b5Sroot /*
3428f7b35b5Sroot * now, check for just the line number followed by the text
3438f7b35b5Sroot */
3448f7b35b5Sroot if (alldigits(wordv[1])){
3458f7b35b5Sroot language = INPI;
3468f7b35b5Sroot c_linenumber = wordv[1];
3478f7b35b5Sroot return(C_IGNORE);
3488f7b35b5Sroot }
3498f7b35b5Sroot /*
3508f7b35b5Sroot * Attempt to match messages refering to a line number
3518f7b35b5Sroot *
3528f7b35b5Sroot * Multiply defined label in case, lines %d and %d
3538f7b35b5Sroot * Goto %s from line %d is into a structured statement
3548f7b35b5Sroot * End matched %s on line %d
3558f7b35b5Sroot * Inserted keyword end matching %s on line %d
3568f7b35b5Sroot */
3578f7b35b5Sroot multiple = structured = 0;
3588f7b35b5Sroot if (
3598f7b35b5Sroot ( (wordc == 6) && (wordvcmp(wordv+1, 2, pi_Endmatched) == 0))
3608f7b35b5Sroot || ( (wordc == 8) && (wordvcmp(wordv+1, 4, pi_Inserted) == 0))
3618f7b35b5Sroot || ( multiple = ((wordc == 9) && (wordvcmp(wordv+1,6, pi_multiple) == 0) ) )
3628f7b35b5Sroot || ( structured = ((wordc == 10) && (wordvcmp(wordv+6,5, pi_structured) == 0 ) ))
3638f7b35b5Sroot ){
3648f7b35b5Sroot language = INPI;
3658f7b35b5Sroot nwordv = wordvsplice(2, wordc, wordv+1);
3668f7b35b5Sroot nwordv[0] = strsave(currentfilename);
3678f7b35b5Sroot nwordv[1] = structured ? wordv [5] : wordv[wordc];
3688f7b35b5Sroot wordc += 2;
3698f7b35b5Sroot wordv = nwordv - 1;
3708f7b35b5Sroot if (!multiple)
3718f7b35b5Sroot return(C_TRUE);
3728f7b35b5Sroot erroradd(wordc, nwordv, C_TRUE, C_UNKNOWN);
3738f7b35b5Sroot nwordv = wordvsplice(0, wordc, nwordv);
3748f7b35b5Sroot nwordv[1] = wordv[wordc - 2];
3758f7b35b5Sroot return(C_TRUE);
3768f7b35b5Sroot }
3778f7b35b5Sroot return(C_UNKNOWN);
3788f7b35b5Sroot }
379