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