1 /* $NetBSD: pi.c,v 1.16 2009/08/13 05:53:58 dholland Exp $ */
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)pi.c 8.1 (Berkeley) 6/6/93";
37 __RCSID("$NetBSD: pi.c,v 1.16 2009/08/13 05:53:58 dholland Exp $");
46 #if 0 /* not const-correct */
47 static char *unk_hdr
[] = {"In", "program", "???"};
49 DECL_STRINGS_3(static, unk_hdr
, "In", "program", "???");
52 static char *c_linenumber
;
53 static char **c_header
= &unk_hdr
[0];
55 static boolean
alldigits(const char *);
56 static boolean
isdateformat(int, char **);
57 static boolean
instringset(const char *, const char **);
58 static boolean
piptr(const char *);
62 * Attempt to handle error messages produced by pi (and by pc)
64 * problem #1: There is no file name available when a file does not
65 * use a #include; this will have to be given to error
66 * in the command line.
67 * problem #2: pi doesn't always tell you what line number
68 * a error refers to; for example during the tree
69 * walk phase of code generation and error detection,
70 * an error can refer to "variable foo in procedure bletch"
71 * without giving a line number
72 * problem #3: line numbers, when available, are attached to
73 * the source line, along with the source line itself
74 * These line numbers must be extracted, and
75 * the source line thrown away.
76 * problem #4: Some error messages produce more than one line number
77 * on the same message.
78 * There are only two (I think):
79 * %s undefined on line%s
80 * %s improperly used on line%s
81 * here, the %s makes line plural or singular.
83 * Here are the error strings used in pi version 1.2 that can refer
84 * to a file name or line number:
86 * Multiply defined label in case, lines %d and %d
87 * Goto %s from line %d is into a structured statement
88 * End matched %s on line %d
89 * Inserted keyword end matching %s on line %d
91 * Here are the general pi patterns recognized:
92 * define piptr == -.*^-.*
94 * define digit = [0-9]
96 * define date_format letter*3 letter*3 (digit | (digit digit))
97 * (digit | (digit digit)):digit*2 digit*4
99 * {e,E} (piptr) (msg) Encounter an error during textual scan
100 * E {digit}* - (msg) Have an error message that refers to a new line
101 * E - msg Have an error message that refers to current
102 * function, program or procedure
103 * (date_format) (name): When switch compilation files
104 * ... (msg) When refer to the previous line
105 * 'In' ('procedure'|'function'|'program') (name):
106 * pi is now complaining about 2nd pass errors.
108 * Here is the output from a compilation
112 * e --------------^--- Inserted ';'
113 * E 2 - All variables must be declared in one var part
114 * E 5 - Include filename must end in .i
115 * Mon Apr 21 15:56 1980 test.h:
117 * e ------^--- Inserted ';'
118 * Mon Apr 21 16:06 1980 test.p:
119 * E 2 - Function type must be specified
120 * 6 procedure foo(var x:real);
121 * e ------^--- Inserted ';'
122 * In function bletch:
123 * E - No assignment to the function variable
124 * w - variable x is never used
125 * E 6 - foo is already defined in this block
127 * w - variable x is neither used nor set
129 * E --------------^--- Undefined variable
131 * e ----------------^--- Inserted ':'
133 * e -----------------------^--- Digits required after decimal point
134 * E 10 - Constant set involved in non set context
135 * E 11 - Type clash: real is incompatible with integer
136 * ... Type of expression clashed with type of variable in assignment
137 * E 12 - Parameter type not identical to type of var parameter x of foo
139 * w - variable y is never used
140 * w - type foo is never used
141 * w - function bletch is never used
142 * E - z undefined on lines 9 13
144 static const char *Months
[] = {
145 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
146 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
149 static const char *Days
[] = {
150 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", 0
152 static const char *Piroutines
[] = {
153 "program", "function", "procedure", 0
157 static boolean structured
, multiple
;
159 #if 0 /* not const-correct */
160 static char *pi_Endmatched
[] = {"End", "matched"};
161 static char *pi_Inserted
[] = {"Inserted", "keyword", "end", "matching"};
163 static char *pi_multiple
[] = {"Mutiply", "defined", "label", "in", "case,", "line"};
164 static char *pi_structured
[] = {"is", "into", "a", "structured", "statement"};
166 static char *pi_und1
[] = {"undefined", "on", "line"};
167 static char *pi_und2
[] = {"undefined", "on", "lines"};
168 static char *pi_imp1
[] = {"improperly", "used", "on", "line"};
169 static char *pi_imp2
[] = {"improperly", "used", "on", "lines"};
172 DECL_STRINGS_2(static, pi_Endmatched
, "End", "matched");
173 DECL_STRINGS_4(static, pi_Inserted
, "Inserted", "keyword", "end", "matching");
175 DECL_STRINGS_6(static, pi_multiple
,
176 "Mutiply", "defined", "label", "in", "case,", "line");
177 DECL_STRINGS_5(static, pi_structured
,
178 "is", "into", "a", "structured", "statement");
180 DECL_STRINGS_3(static, pi_und1
, "undefined", "on", "line");
181 DECL_STRINGS_3(static, pi_und2
, "undefined", "on", "lines");
182 DECL_STRINGS_4(static, pi_imp1
, "improperly", "used", "on", "line");
183 DECL_STRINGS_4(static, pi_imp2
, "improperly", "used", "on", "lines");
188 alldigits(const char *string
)
190 for (; *string
&& isdigit((unsigned char)*string
); string
++)
192 return (*string
== '\0');
196 instringset(const char *member
, const char **set
)
198 for (; *set
; set
++) {
199 if (strcmp(*set
, member
) == 0)
206 isdateformat(int wordc
, char **wordv
)
210 && (instringset(wordv
[0], Days
))
211 && (instringset(wordv
[1], Months
))
212 && (alldigits(wordv
[2]))
213 && (alldigits(wordv
[4])));
217 piptr(const char *string
)
221 while (*string
&& *string
== '-')
226 while (*string
&& *string
== '-')
228 return (*string
== '\0');
239 if (strlen(cur_wordv
[1]) == 1
240 && ( cur_wordv
[1][0] == 'e' || cur_wordv
[1][0] == 'E')
241 && piptr(cur_wordv
[2])
243 boolean longpiptr
= 0;
246 * We have recognized a first pass error of the form:
247 * letter ------^---- message
249 * turn into an error message of the form:
251 * file line 'pascal errortype' letter \n |---- message
253 * file line letter |---- message
254 * when there are strlen("(*[pi]") or more
255 * preceding '-' on the error pointer.
257 * Where the | is intended to be a down arrow, so that
258 * the pi error messages can be inserted above the
259 * line in error, instead of below. (All of the other
260 * languages put their messages before the source line,
261 * instead of after it as does pi.)
263 * where the pointer to the error has been truncated
264 * by 6 characters to account for the fact that
265 * the pointer points into a tab preceded input line.
268 (void)substitute(cur_wordv
[2], '^', '|');
269 longpiptr
= position(cur_wordv
[2],'|') > (6+8);
270 nwordv
= wordvsplice(longpiptr
? 2 : 4, cur_wordc
, cur_wordv
+1);
271 nwordv
[0] = strdup(currentfilename
);
272 nwordv
[1] = strdup(c_linenumber
);
274 nwordv
[2] = Strdup("pascal errortype"); /* XXX leaked */
275 nwordv
[3] = cur_wordv
[1];
276 nwordv
[4] = strdup("%%%\n");
277 if (strlen(nwordv
[5]) > (8-2)) /* this is the pointer */
278 nwordv
[5] += (8-2); /* bump over 6 characters */
280 cur_wordv
= nwordv
- 1; /* convert to 1 based */
281 cur_wordc
+= longpiptr
? 2 : 4;
285 && strlen(cur_wordv
[1]) == 1
286 && (*cur_wordv
[1] == 'E' || *cur_wordv
[1] == 'w' || *cur_wordv
[1] == 'e')
287 && alldigits(cur_wordv
[2])
288 && strlen(cur_wordv
[3]) == 1
289 && cur_wordv
[3][0] == '-'
292 * Message of the form: letter linenumber - message
293 * Turn into form: filename linenumber letter - message
296 nwordv
= wordvsplice(1, cur_wordc
, cur_wordv
+ 1);
297 nwordv
[0] = strdup(currentfilename
);
298 nwordv
[1] = cur_wordv
[2];
299 nwordv
[2] = cur_wordv
[1];
300 c_linenumber
= cur_wordv
[2];
302 cur_wordv
= nwordv
- 1;
306 && strlen(cur_wordv
[1]) == 1
307 && (*cur_wordv
[1] == 'E' || *cur_wordv
[1] == 'w' || *cur_wordv
[1] == 'e')
308 && strlen(cur_wordv
[2]) == 1
309 && cur_wordv
[2][0] == '-'
312 * Message of the form: letter - message
314 * This happens only when we are traversing the tree
315 * during the second pass of pi, and discover semantic
318 * We have already (presumably) saved the header message
319 * and can now construct a nulled error message for the
322 * Turns into a message of the form:
323 * filename (header) letter - message
325 * First, see if it is a message referring to more than
326 * one line number. Only of the form:
327 * %s undefined on line%s
328 * %s improperly used on line%s
330 boolean undefined
= 0;
334 if ((undefined
= (wordvcmp(cur_wordv
+2, 3, pi_und1
) == 0))
335 || (undefined
= (wordvcmp(cur_wordv
+2, 3, pi_und2
) == 0))
336 || wordvcmp(cur_wordv
+2, 4, pi_imp1
) == 0
337 || wordvcmp(cur_wordv
+2, 4, pi_imp2
) == 0
339 for (wordindex
= undefined
? 5 : 6;
340 wordindex
<= cur_wordc
;
346 nwordv
= wordvsplice(2, undefined
? 2 : 3, cur_wordv
+1);
347 nwordv
[0] = strdup(currentfilename
);
348 nwordv
[1] = cur_wordv
[wordindex
];
349 if (wordindex
!= cur_wordc
)
350 erroradd(undefined
? 4 : 5, nwordv
,
353 cur_wordc
= undefined
? 4 : 5;
354 cur_wordv
= nwordv
- 1;
358 nwordv
= wordvsplice(1+3, cur_wordc
, cur_wordv
+1);
359 nwordv
[0] = strdup(currentfilename
);
360 nwordv
[1] = strdup(c_header
[0]);
361 nwordv
[2] = strdup(c_header
[1]);
362 nwordv
[3] = strdup(c_header
[2]);
363 cur_wordv
= nwordv
- 1;
367 if (strcmp(cur_wordv
[1], "...") == 0) {
369 * have a continuation error message
370 * of the form: ... message
371 * Turn into form : filename linenumber message
374 nwordv
= wordvsplice(1, cur_wordc
, cur_wordv
+1);
375 nwordv
[0] = strdup(currentfilename
);
376 nwordv
[1] = strdup(c_linenumber
);
377 cur_wordv
= nwordv
- 1;
382 && lastchar(cur_wordv
[6]) == ':'
383 && isdateformat(5, cur_wordv
+ 1)
386 * Have message that tells us we have changed files
389 currentfilename
= strdup(cur_wordv
[6]);
390 clob_last(currentfilename
, '\0');
394 && strcmp(cur_wordv
[1], "In") == 0
395 && lastchar(cur_wordv
[3]) == ':'
396 && instringset(cur_wordv
[2], Piroutines
)
399 c_header
= wordvsplice(0, cur_wordc
, cur_wordv
+1);
404 * now, check for just the line number followed by the text
406 if (alldigits(cur_wordv
[1])) {
408 c_linenumber
= cur_wordv
[1];
413 * Attempt to match messages refering to a line number
415 * Multiply defined label in case, lines %d and %d
416 * Goto %s from line %d is into a structured statement
417 * End matched %s on line %d
418 * Inserted keyword end matching %s on line %d
420 multiple
= structured
= 0;
422 (cur_wordc
== 6 && wordvcmp(cur_wordv
+1, 2, pi_Endmatched
) == 0)
423 || (cur_wordc
== 8 && wordvcmp(cur_wordv
+1, 4, pi_Inserted
) == 0)
424 || (multiple
= (cur_wordc
== 9 && wordvcmp(cur_wordv
+1,6, pi_multiple
) == 0))
425 || (structured
= (cur_wordc
== 10 && wordvcmp(cur_wordv
+6,5, pi_structured
) == 0))
428 nwordv
= wordvsplice(2, cur_wordc
, cur_wordv
+1);
429 nwordv
[0] = strdup(currentfilename
);
430 nwordv
[1] = structured
? cur_wordv
[5] : cur_wordv
[cur_wordc
];
432 cur_wordv
= nwordv
- 1;
435 erroradd(cur_wordc
, nwordv
, C_TRUE
, C_UNKNOWN
);
436 nwordv
= wordvsplice(0, cur_wordc
, nwordv
);
437 nwordv
[1] = cur_wordv
[cur_wordc
- 2];