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