Annotation of src/usr.bin/error/pi.c, Revision 1.6
1.6 ! deraadt 1: /* $OpenBSD: pi.c,v 1.5 2003/06/03 02:56:07 millert 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.6 ! deraadt 37: static char rcsid[] = "$OpenBSD: pi.c,v 1.5 2003/06/03 02:56:07 millert 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:
1.6 ! deraadt 207: Errorclass
! 208: pi(void)
1.1 deraadt 209: {
210: char **nwordv;
211:
212: if (wordc < 2)
213: return (C_UNKNOWN);
214: if ( ( strlen(wordv[1]) == 1)
215: && ( (wordv[1][0] == 'e') || (wordv[1][0] == 'E') )
216: && ( piptr(wordv[2]) )
217: ) {
218: boolean longpiptr = 0;
219: /*
220: * We have recognized a first pass error of the form:
221: * letter ------^---- message
222: *
223: * turn into an error message of the form:
224: *
225: * file line 'pascal errortype' letter \n |---- message
226: * or of the form:
227: * file line letter |---- message
228: * when there are strlen("(*[pi]") or more
229: * preceding '-' on the error pointer.
230: *
231: * Where the | is intended to be a down arrow, so that
232: * the pi error messages can be inserted above the
233: * line in error, instead of below. (All of the other
1.4 todd 234: * language put their messages before the source line,
1.1 deraadt 235: * instead of after it as does pi.)
236: *
237: * where the pointer to the error has been truncated
238: * by 6 characters to account for the fact that
239: * the pointer points into a tab preceded input line.
240: */
241: language = INPI;
242: (void)substitute(wordv[2], '^', '|');
243: longpiptr = position(wordv[2],'|') > (6+8);
244: nwordv = wordvsplice(longpiptr ? 2 : 4, wordc, wordv+1);
245: nwordv[0] = strsave(currentfilename);
246: nwordv[1] = strsave(c_linenumber);
247: if (!longpiptr){
248: nwordv[2] = "pascal errortype";
249: nwordv[3] = wordv[1];
250: nwordv[4] = strsave("%%%\n");
251: if (strlen(nwordv[5]) > (8-2)) /* this is the pointer */
252: nwordv[5] += (8-2); /* bump over 6 characters */
253: }
254: wordv = nwordv - 1; /* convert to 1 based */
255: wordc += longpiptr ? 2 : 4;
256: return(C_TRUE);
257: }
258: if ( (wordc >= 4)
259: && (strlen(wordv[1]) == 1)
260: && ( (*wordv[1] == 'E') || (*wordv[1] == 'w') || (*wordv[1] == 'e') )
261: && (alldigits(wordv[2]))
262: && (strlen(wordv[3]) == 1)
263: && (wordv[3][0] == '-')
264: ){
265: /*
266: * Message of the form: letter linenumber - message
267: * Turn into form: filename linenumber letter - message
268: */
269: language = INPI;
270: nwordv = wordvsplice(1, wordc, wordv + 1);
271: nwordv[0] = strsave(currentfilename);
272: nwordv[1] = wordv[2];
273: nwordv[2] = wordv[1];
274: c_linenumber = wordv[2];
275: wordc += 1;
276: wordv = nwordv - 1;
277: return(C_TRUE);
278: }
279: if ( (wordc >= 3)
280: && (strlen(wordv[1]) == 1)
281: && ( (*(wordv[1]) == 'E') || (*(wordv[1]) == 'w') || (*(wordv[1]) == 'e') )
282: && (strlen(wordv[2]) == 1)
283: && (wordv[2][0] == '-')
284: ) {
285: /*
286: * Message of the form: letter - message
287: * This happens only when we are traversing the tree
288: * during the second pass of pi, and discover semantic
289: * errors.
290: *
291: * We have already (presumably) saved the header message
292: * and can now construct a nulled error message for the
293: * current file.
294: *
295: * Turns into a message of the form:
296: * filename (header) letter - message
297: *
298: * First, see if it is a message referring to more than
299: * one line number. Only of the form:
300: * %s undefined on line%s
301: * %s improperly used on line%s
302: */
303: boolean undefined = 0;
304: int wordindex;
305:
306: language = INPI;
307: if ( (undefined = (wordvcmp(wordv+2, 3, pi_und1) == 0) )
308: || (undefined = (wordvcmp(wordv+2, 3, pi_und2) == 0) )
309: || (wordvcmp(wordv+2, 4, pi_imp1) == 0)
310: || (wordvcmp(wordv+2, 4, pi_imp2) == 0)
311: ){
312: for (wordindex = undefined ? 5 : 6; wordindex <= wordc;
313: wordindex++){
314: nwordv = wordvsplice(2, undefined ? 2 : 3, wordv+1);
315: nwordv[0] = strsave(currentfilename);
316: nwordv[1] = wordv[wordindex];
317: if (wordindex != wordc)
318: erroradd(undefined ? 4 : 5, nwordv,
319: C_TRUE, C_UNKNOWN);
320: }
321: wordc = undefined ? 4 : 5;
322: wordv = nwordv - 1;
323: return(C_TRUE);
324: }
325:
326: nwordv = wordvsplice(1+3, wordc, wordv+1);
327: nwordv[0] = strsave(currentfilename);
328: nwordv[1] = strsave(c_header[0]);
329: nwordv[2] = strsave(c_header[1]);
330: nwordv[3] = strsave(c_header[2]);
331: wordv = nwordv - 1;
332: wordc += 1 + 3;
333: return(C_THISFILE);
334: }
335: if (strcmp(wordv[1], "...") == 0){
336: /*
337: * have a continuation error message
338: * of the form: ... message
339: * Turn into form : filename linenumber message
340: */
341: language = INPI;
342: nwordv = wordvsplice(1, wordc, wordv+1);
343: nwordv[0] = strsave(currentfilename);
344: nwordv[1] = strsave(c_linenumber);
345: wordv = nwordv - 1;
346: wordc += 1;
347: return(C_TRUE);
348: }
349: if( (wordc == 6)
350: && (lastchar(wordv[6]) == ':')
351: && (isdateformat(5, wordv + 1))
352: ){
353: /*
354: * Have message that tells us we have changed files
355: */
356: language = INPI;
357: currentfilename = strsave(wordv[6]);
358: clob_last(currentfilename, '\0');
359: return(C_SYNC);
360: }
361: if( (wordc == 3)
362: && (strcmp(wordv[1], "In") == 0)
363: && (lastchar(wordv[3]) == ':')
364: && (instringset(wordv[2], Piroutines))
365: ) {
366: language = INPI;
367: c_header = wordvsplice(0, wordc, wordv+1);
368: return(C_SYNC);
369: }
370: /*
371: * now, check for just the line number followed by the text
372: */
373: if (alldigits(wordv[1])){
374: language = INPI;
375: c_linenumber = wordv[1];
376: return(C_IGNORE);
377: }
378: /*
1.4 todd 379: * Attempt to match messages referring to a line number
1.1 deraadt 380: *
381: * Multiply defined label in case, lines %d and %d
382: * Goto %s from line %d is into a structured statement
383: * End matched %s on line %d
384: * Inserted keyword end matching %s on line %d
385: */
386: multiple = structured = 0;
387: if (
388: ( (wordc == 6) && (wordvcmp(wordv+1, 2, pi_Endmatched) == 0))
389: || ( (wordc == 8) && (wordvcmp(wordv+1, 4, pi_Inserted) == 0))
390: || ( multiple = ((wordc == 9) && (wordvcmp(wordv+1,6, pi_multiple) == 0) ) )
391: || ( structured = ((wordc == 10) && (wordvcmp(wordv+6,5, pi_structured) == 0 ) ))
392: ){
393: language = INPI;
394: nwordv = wordvsplice(2, wordc, wordv+1);
395: nwordv[0] = strsave(currentfilename);
396: nwordv[1] = structured ? wordv [5] : wordv[wordc];
397: wordc += 2;
398: wordv = nwordv - 1;
399: if (!multiple)
400: return(C_TRUE);
401: erroradd(wordc, nwordv, C_TRUE, C_UNKNOWN);
402: nwordv = wordvsplice(0, wordc, nwordv);
403: nwordv[1] = wordv[wordc - 2];
404: return(C_TRUE);
405: }
406: return(C_UNKNOWN);
407: }