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