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