xref: /csrg-svn/usr.bin/error/pi.c (revision 34664)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)pi.c	5.2 (Berkeley) 06/06/88";
15 #endif /* not lint */
16 
17 #include <stdio.h>
18 #include <ctype.h>
19 #include "error.h"
20 
21 extern	char	*currentfilename;
22 static	char	*c_linenumber;
23 static	char	*unk_hdr[] = {"In", "program", "???"};
24 static	char	**c_header = &unk_hdr[0];
25 
26 /*
27  *	Attempt to handle error messages produced by pi (and by pc)
28  *
29  *	problem #1:	There is no file name available when a file does not
30  *			use a #include; this will have to be given to error
31  *			in the command line.
32  *	problem #2:	pi doesn't always tell you what line number
33  *			a error refers to; for example during the tree
34  *			walk phase of code generation and error detection,
35  *			an error can refer to "variable foo in procedure bletch"
36  *			without giving a line number
37  *	problem #3:	line numbers, when available, are attached to
38  *			the source line, along with the source line itself
39  *			These line numbers must be extracted, and
40  *			the source line thrown away.
41  *	problem #4:	Some error messages produce more than one line number
42  *			on the same message.
43  *			There are only two (I think):
44  *				%s undefined on line%s
45  *				%s improperly used on line%s
46  *			here, the %s makes line plural or singular.
47  *
48  *	Here are the error strings used in pi version 1.2 that can refer
49  *	to a file name or line number:
50  *
51  *		Multiply defined label in case, lines %d and %d
52  *		Goto %s from line %d is into a structured statement
53  *		End matched %s on line %d
54  *		Inserted keyword end matching %s on line %d
55  *
56  *	Here are the general pi patterns recognized:
57  *	define piptr == -.*^-.*
58  *	define msg = .*
59  *	define digit = [0-9]
60  *	definename = .*
61  *	define date_format letter*3 letter*3 (digit | (digit digit))
62  *			(digit | (digit digit)):digit*2 digit*4
63  *
64  *	{e,E} (piptr) (msg)	Encounter an error during textual scan
65  *	E {digit}* - (msg)	Have an error message that refers to a new line
66  *	E - msg			Have an error message that refers to current
67  *					function, program or procedure
68  *	(date_format) (name):	When switch compilation files
69  *	... (msg)		When refer to the previous line
70  *	'In' ('procedure'|'function'|'program') (name):
71  *				pi is now complaining about 2nd pass errors.
72  *
73  *	Here is the output from a compilation
74  *
75  *
76  *	     2  	var	i:integer;
77  *	e --------------^--- Inserted ';'
78  *	E 2 - All variables must be declared in one var part
79  *	E 5 - Include filename must end in .i
80  *	Mon Apr 21 15:56 1980  test.h:
81  *	     2  begin
82  *	e ------^--- Inserted ';'
83  *	Mon Apr 21 16:06 1980  test.p:
84  *	E 2 - Function type must be specified
85  *	     6  procedure foo(var x:real);
86  *	e ------^--- Inserted ';'
87  *	In function bletch:
88  *	  E - No assignment to the function variable
89  *	  w - variable x is never used
90  *	E 6 - foo is already defined in this block
91  *	In procedure foo:
92  *	  w - variable x is neither used nor set
93  *	     9  	z : = 23;
94  *	E --------------^--- Undefined variable
95  *	    10  	y = [1];
96  *	e ----------------^--- Inserted ':'
97  *	    13  	z := 345.;
98  *	e -----------------------^--- Digits required after decimal point
99  *	E 10 - Constant set involved in non set context
100  *	E 11 - Type clash: real is incompatible with integer
101  *	   ... Type of expression clashed with type of variable in assignment
102  *	E 12 - Parameter type not identical to type of var parameter x of foo
103  *	In program mung:
104  *	  w - variable y is never used
105  *	  w - type foo is never used
106  *	  w - function bletch is never used
107  *	  E - z undefined on lines 9 13
108  */
109 char *Months[] = {
110 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
111 	"Jul", "Aug", "Sep", "Oct","Nov", "Dec",
112 	0
113 };
114 char *Days[] = {
115 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", 0
116 };
117 char *Piroutines[] = {
118 		"program", "function", "procedure", 0
119 };
120 
121 
122 static boolean	structured, multiple;
123 
124 char *pi_Endmatched[] = {"End", "matched"};
125 char *pi_Inserted[] = {"Inserted", "keyword", "end", "matching"};
126 
127 char *pi_multiple[] = {"Mutiply", "defined", "label", "in", "case,", "line"};
128 char *pi_structured[] = {"is", "into", "a", "structured", "statement"};
129 
130 char *pi_und1[] = {"undefined", "on", "line"};
131 char *pi_und2[] = {"undefined", "on", "lines"};
132 char *pi_imp1[] = {"improperly", "used", "on", "line"};
133 char *pi_imp2[] = {"improperly", "used", "on", "lines"};
134 
135 boolean alldigits(string)
136 	reg	char	*string;
137 {
138 	for (; *string && isdigit(*string); string++)
139 		continue;
140 	return(*string == '\0');
141 }
142 boolean instringset(member, set)
143 		char	*member;
144 	reg	char	**set;
145 {
146 	for(; *set; set++){
147 		if (strcmp(*set, member) == 0)
148 			return(TRUE);
149 	}
150 	return(FALSE);
151 }
152 
153 boolean isdateformat(wordc, wordv)
154 	int	wordc;
155 	char	**wordv;
156 {
157 	return(
158 	        (wordc == 5)
159 	     && (instringset(wordv[0], Days))
160 	     && (instringset(wordv[1], Months))
161 	     && (alldigits(wordv[2]))
162 	     && (alldigits(wordv[4])) );
163 }
164 
165 boolean piptr(string)
166 	reg	char	*string;
167 {
168 	if (*string != '-')
169 		return(FALSE);
170 	while (*string && *string == '-')
171 		string++;
172 	if (*string != '^')
173 		return(FALSE);
174 	string++;
175 	while (*string && *string == '-')
176 		string++;
177 	return(*string == '\0');
178 }
179 
180 extern	int	wordc;
181 extern	char	**wordv;
182 
183 Errorclass pi()
184 {
185 	char	**nwordv;
186 
187 	if (wordc < 2)
188 		return (C_UNKNOWN);
189 	if (   ( strlen(wordv[1]) == 1)
190 	    && ( (wordv[1][0] == 'e') || (wordv[1][0] == 'E') )
191 	    && ( piptr(wordv[2]) )
192 	) {
193 		boolean	longpiptr = 0;
194 		/*
195 		 *	We have recognized a first pass error of the form:
196 		 *	letter ------^---- message
197 		 *
198 		 *	turn into an error message of the form:
199 		 *
200 		 *	file line 'pascal errortype' letter \n |---- message
201 		 *	or of the form:
202 		 *	file line letter |---- message
203 		 *		when there are strlen("(*[pi]") or more
204 		 *		preceding '-' on the error pointer.
205 		 *
206 		 *	Where the | is intended to be a down arrow, so that
207 		 *	the pi error messages can be inserted above the
208 		 *	line in error, instead of below.  (All of the other
209 		 *	langauges put thier messages before the source line,
210 		 *	instead of after it as does pi.)
211 		 *
212 		 *	where the pointer to the error has been truncated
213 		 *	by 6 characters to account for the fact that
214 		 *	the pointer points into a tab preceded input line.
215 		 */
216 		language = INPI;
217 		(void)substitute(wordv[2], '^', '|');
218 		longpiptr = position(wordv[2],'|') > (6+8);
219 		nwordv = wordvsplice(longpiptr ? 2 : 4, wordc, wordv+1);
220 		nwordv[0] = strsave(currentfilename);
221 		nwordv[1] = strsave(c_linenumber);
222 		if (!longpiptr){
223 			nwordv[2] = "pascal errortype";
224 			nwordv[3] = wordv[1];
225 			nwordv[4] = strsave("%%%\n");
226 			if (strlen(nwordv[5]) > (8-2))	/* this is the pointer */
227 				nwordv[5] += (8-2);	/* bump over 6 characters */
228 		}
229 		wordv = nwordv - 1;		/* convert to 1 based */
230 		wordc += longpiptr ? 2 : 4;
231 		return(C_TRUE);
232 	}
233 	if (   (wordc >= 4)
234 	    && (strlen(wordv[1]) == 1)
235 	    && ( (*wordv[1] == 'E') || (*wordv[1] == 'w') || (*wordv[1] == 'e') )
236 	    && (alldigits(wordv[2]))
237 	    && (strlen(wordv[3]) == 1)
238 	    && (wordv[3][0] == '-')
239 	){
240 		/*
241 		 *	Message of the form: letter linenumber - message
242 		 *	Turn into form: filename linenumber letter - message
243 		 */
244 		language = INPI;
245 		nwordv = wordvsplice(1, wordc, wordv + 1);
246 		nwordv[0] = strsave(currentfilename);
247 		nwordv[1] = wordv[2];
248 		nwordv[2] = wordv[1];
249 		c_linenumber = wordv[2];
250 		wordc += 1;
251 		wordv = nwordv - 1;
252 		return(C_TRUE);
253 	}
254 	if (   (wordc >= 3)
255 	    && (strlen(wordv[1]) == 1)
256 	    && ( (*(wordv[1]) == 'E') || (*(wordv[1]) == 'w') || (*(wordv[1]) == 'e') )
257 	    && (strlen(wordv[2]) == 1)
258 	    && (wordv[2][0] == '-')
259 	) {
260 		/*
261 		 *	Message of the form: letter - message
262 		 *	This happens only when we are traversing the tree
263 		 *	during the second pass of pi, and discover semantic
264 		 *	errors.
265 		 *
266 		 *	We have already (presumably) saved the header message
267 		 *	and can now construct a nulled error message for the
268 		 *	current file.
269 		 *
270 		 *	Turns into a message of the form:
271 		 *	filename (header) letter - message
272 		 *
273 		 *	First, see if it is a message referring to more than
274 		 *	one line number.  Only of the form:
275  		 *		%s undefined on line%s
276  		 *		%s improperly used on line%s
277 		 */
278 		boolean undefined = 0;
279 		int	wordindex;
280 
281 		language = INPI;
282 		if (    (undefined = (wordvcmp(wordv+2, 3, pi_und1) == 0) )
283 		     || (undefined = (wordvcmp(wordv+2, 3, pi_und2) == 0) )
284 		     || (wordvcmp(wordv+2, 4, pi_imp1) == 0)
285 		     || (wordvcmp(wordv+2, 4, pi_imp2) == 0)
286 		){
287 			for (wordindex = undefined ? 5 : 6; wordindex <= wordc;
288 			    wordindex++){
289 				nwordv = wordvsplice(2, undefined ? 2 : 3, wordv+1);
290 				nwordv[0] = strsave(currentfilename);
291 				nwordv[1] = wordv[wordindex];
292 				if (wordindex != wordc)
293 					erroradd(undefined ? 4 : 5, nwordv,
294 						C_TRUE, C_UNKNOWN);
295 			}
296 			wordc = undefined ? 4 : 5;
297 			wordv = nwordv - 1;
298 			return(C_TRUE);
299 		}
300 
301 		nwordv = wordvsplice(1+3, wordc, wordv+1);
302 		nwordv[0] = strsave(currentfilename);
303 		nwordv[1] = strsave(c_header[0]);
304 		nwordv[2] = strsave(c_header[1]);
305 		nwordv[3] = strsave(c_header[2]);
306 		wordv = nwordv - 1;
307 		wordc += 1 + 3;
308 		return(C_THISFILE);
309 	}
310 	if (strcmp(wordv[1], "...") == 0){
311 		/*
312 		 *	have a continuation error message
313 		 *	of the form: ... message
314 		 *	Turn into form : filename linenumber message
315 		 */
316 		language = INPI;
317 		nwordv = wordvsplice(1, wordc, wordv+1);
318 		nwordv[0] = strsave(currentfilename);
319 		nwordv[1] = strsave(c_linenumber);
320 		wordv = nwordv - 1;
321 		wordc += 1;
322 		return(C_TRUE);
323 	}
324 	if(   (wordc == 6)
325 	   && (lastchar(wordv[6]) == ':')
326 	   && (isdateformat(5, wordv + 1))
327 	){
328 		/*
329 		 *	Have message that tells us we have changed files
330 		 */
331 		language = INPI;
332 		currentfilename = strsave(wordv[6]);
333 		clob_last(currentfilename, '\0');
334 		return(C_SYNC);
335 	}
336 	if(   (wordc == 3)
337 	   && (strcmp(wordv[1], "In") == 0)
338 	   && (lastchar(wordv[3]) == ':')
339 	   && (instringset(wordv[2], Piroutines))
340 	) {
341 		language = INPI;
342 		c_header = wordvsplice(0, wordc, wordv+1);
343 		return(C_SYNC);
344 	}
345 	/*
346 	 *	now, check for just the line number followed by the text
347 	 */
348 	if (alldigits(wordv[1])){
349 		language = INPI;
350 		c_linenumber = wordv[1];
351 		return(C_IGNORE);
352 	}
353 	/*
354 	 *	Attempt to match messages refering to a line number
355 	 *
356 	 *	Multiply defined label in case, lines %d and %d
357 	 *	Goto %s from line %d is into a structured statement
358 	 *	End matched %s on line %d
359 	 *	Inserted keyword end matching %s on line %d
360 	 */
361 	multiple = structured = 0;
362 	if (
363 	       ( (wordc == 6) && (wordvcmp(wordv+1, 2, pi_Endmatched) == 0))
364 	    || ( (wordc == 8) && (wordvcmp(wordv+1, 4, pi_Inserted) == 0))
365 	    || ( multiple = ((wordc == 9) && (wordvcmp(wordv+1,6, pi_multiple) == 0) ) )
366 	    || ( structured = ((wordc == 10) && (wordvcmp(wordv+6,5, pi_structured) == 0 ) ))
367 	){
368 		language = INPI;
369 		nwordv = wordvsplice(2, wordc, wordv+1);
370 		nwordv[0] = strsave(currentfilename);
371 		nwordv[1] = structured ? wordv [5] : wordv[wordc];
372 		wordc += 2;
373 		wordv = nwordv - 1;
374 		if (!multiple)
375 			return(C_TRUE);
376 		erroradd(wordc, nwordv, C_TRUE, C_UNKNOWN);
377 		nwordv = wordvsplice(0, wordc, nwordv);
378 		nwordv[1] = wordv[wordc - 2];
379 		return(C_TRUE);
380 	}
381 	return(C_UNKNOWN);
382 }
383