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