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