Home | History | Annotate | Line # | Download | only in error
pi.c revision 1.15
      1 /*	$NetBSD: pi.c,v 1.15 2009/08/13 03:50:02 dholland 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.15 2009/08/13 03:50:02 dholland Exp $");
     38 #endif /* not lint */
     39 
     40 #include <stdio.h>
     41 #include <ctype.h>
     42 #include <string.h>
     43 #include <stdlib.h>
     44 #include "error.h"
     45 
     46 static char *c_linenumber;
     47 static char *unk_hdr[] = {"In", "program", "???"};
     48 static char **c_header = &unk_hdr[0];
     49 
     50 static boolean alldigits(const char *);
     51 static boolean isdateformat(int, char **);
     52 static boolean instringset(const char *, char **);
     53 static boolean piptr(const 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 static char *Months[] = {
    140 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
    141 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
    142 	0
    143 };
    144 static char *Days[] = {
    145 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", 0
    146 };
    147 static char *Piroutines[] = {
    148 	"program", "function", "procedure", 0
    149 };
    150 
    151 
    152 static boolean structured, multiple;
    153 
    154 static char *pi_Endmatched[] = {"End", "matched"};
    155 static char *pi_Inserted[] = {"Inserted", "keyword", "end", "matching"};
    156 
    157 static char *pi_multiple[] = {"Mutiply", "defined", "label", "in", "case,", "line"};
    158 static char *pi_structured[] = {"is", "into", "a", "structured", "statement"};
    159 
    160 static char *pi_und1[] = {"undefined", "on", "line"};
    161 static char *pi_und2[] = {"undefined", "on", "lines"};
    162 static char *pi_imp1[] = {"improperly", "used", "on", "line"};
    163 static char *pi_imp2[] = {"improperly", "used", "on", "lines"};
    164 
    165 static boolean
    166 alldigits(const char *string)
    167 {
    168 	for (; *string && isdigit((unsigned char)*string); string++)
    169 		continue;
    170 	return (*string == '\0');
    171 }
    172 
    173 static boolean
    174 instringset(const 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 static 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 static boolean
    195 piptr(const 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 		/*
    227 		 *	We have recognized a first pass error of the form:
    228 		 *	letter ------^---- message
    229 		 *
    230 		 *	turn into an error message of the form:
    231 		 *
    232 		 *	file line 'pascal errortype' letter \n |---- message
    233 		 *	or of the form:
    234 		 *	file line letter |---- message
    235 		 *		when there are strlen("(*[pi]") or more
    236 		 *		preceding '-' on the error pointer.
    237 		 *
    238 		 *	Where the | is intended to be a down arrow, so that
    239 		 *	the pi error messages can be inserted above the
    240 		 *	line in error, instead of below.  (All of the other
    241 		 *	languages put their messages before the source line,
    242 		 *	instead of after it as does pi.)
    243 		 *
    244 		 *	where the pointer to the error has been truncated
    245 		 *	by 6 characters to account for the fact that
    246 		 *	the pointer points into a tab preceded input line.
    247 		 */
    248 		language = INPI;
    249 		(void)substitute(wordv[2], '^', '|');
    250 		longpiptr = position(wordv[2],'|') > (6+8);
    251 		nwordv = wordvsplice(longpiptr ? 2 : 4, wordc, wordv+1);
    252 		nwordv[0] = strdup(currentfilename);
    253 		nwordv[1] = strdup(c_linenumber);
    254 		if (!longpiptr) {
    255 			nwordv[2] = "pascal errortype";
    256 			nwordv[3] = wordv[1];
    257 			nwordv[4] = strdup("%%%\n");
    258 			if (strlen(nwordv[5]) > (8-2))	/* this is the pointer */
    259 				nwordv[5] += (8-2);	/* bump over 6 characters */
    260 		}
    261 		wordv = nwordv - 1;		/* convert to 1 based */
    262 		wordc += longpiptr ? 2 : 4;
    263 		return (C_TRUE);
    264 	}
    265 	if ((wordc >= 4)
    266 	    && (strlen(wordv[1]) == 1)
    267 	    && ((*wordv[1] == 'E') || (*wordv[1] == 'w') || (*wordv[1] == 'e'))
    268 	    && (alldigits(wordv[2]))
    269 	    && (strlen(wordv[3]) == 1)
    270 	    && (wordv[3][0] == '-')
    271 	) {
    272 		/*
    273 		 * Message of the form: letter linenumber - message
    274 		 * Turn into form: filename linenumber letter - message
    275 		 */
    276 		language = INPI;
    277 		nwordv = wordvsplice(1, wordc, wordv + 1);
    278 		nwordv[0] = strdup(currentfilename);
    279 		nwordv[1] = wordv[2];
    280 		nwordv[2] = wordv[1];
    281 		c_linenumber = wordv[2];
    282 		wordc += 1;
    283 		wordv = nwordv - 1;
    284 		return (C_TRUE);
    285 	}
    286 	if ((wordc >= 3)
    287 	    && (strlen(wordv[1]) == 1)
    288 	    && ((*(wordv[1]) == 'E') || (*(wordv[1]) == 'w') || (*(wordv[1]) == 'e'))
    289 	    && (strlen(wordv[2]) == 1)
    290 	    && (wordv[2][0] == '-')
    291 	) {
    292 		/*
    293 		 * Message of the form: letter - message
    294 		 *
    295 		 * This happens only when we are traversing the tree
    296 		 * during the second pass of pi, and discover semantic
    297 		 * errors.
    298 		 *
    299 		 * We have already (presumably) saved the header message
    300 		 * and can now construct a nulled error message for the
    301 		 * current file.
    302 		 *
    303 		 * Turns into a message of the form:
    304 		 *      filename (header) letter - message
    305 		 *
    306 		 * First, see if it is a message referring to more than
    307 		 * one line number.  Only of the form:
    308  		 *      %s undefined on line%s
    309  		 *      %s improperly used on line%s
    310 		 */
    311 		boolean undefined = 0;
    312 		int wordindex;
    313 
    314 		language = INPI;
    315 		if ((undefined = (wordvcmp(wordv+2, 3, pi_und1) == 0))
    316 		     || (undefined = (wordvcmp(wordv+2, 3, pi_und2) == 0))
    317 		     || (wordvcmp(wordv+2, 4, pi_imp1) == 0)
    318 		     || (wordvcmp(wordv+2, 4, pi_imp2) == 0)
    319 		) {
    320 			for (wordindex = undefined ? 5 : 6; wordindex <= wordc;
    321 			    wordindex++) {
    322 				if (nwordv) {
    323 					free(nwordv[0]);
    324 					free(nwordv);
    325 				}
    326 				nwordv = wordvsplice(2, undefined ? 2 : 3, wordv+1);
    327 				nwordv[0] = strdup(currentfilename);
    328 				nwordv[1] = wordv[wordindex];
    329 				if (wordindex != wordc)
    330 					erroradd(undefined ? 4 : 5, nwordv,
    331 						C_TRUE, C_UNKNOWN);
    332 			}
    333 			wordc = undefined ? 4 : 5;
    334 			wordv = nwordv - 1;
    335 			return (C_TRUE);
    336 		}
    337 
    338 		nwordv = wordvsplice(1+3, wordc, wordv+1);
    339 		nwordv[0] = strdup(currentfilename);
    340 		nwordv[1] = strdup(c_header[0]);
    341 		nwordv[2] = strdup(c_header[1]);
    342 		nwordv[3] = strdup(c_header[2]);
    343 		wordv = nwordv - 1;
    344 		wordc += 1 + 3;
    345 		return (C_THISFILE);
    346 	}
    347 	if (strcmp(wordv[1], "...") == 0) {
    348 		/*
    349 		 * have a continuation error message
    350 		 * of the form: ... message
    351 		 * Turn into form : filename linenumber message
    352 		 */
    353 		language = INPI;
    354 		nwordv = wordvsplice(1, wordc, wordv+1);
    355 		nwordv[0] = strdup(currentfilename);
    356 		nwordv[1] = strdup(c_linenumber);
    357 		wordv = nwordv - 1;
    358 		wordc += 1;
    359 		return (C_TRUE);
    360 	}
    361 	if ((wordc == 6)
    362 	   && (lastchar(wordv[6]) == ':')
    363 	   && (isdateformat(5, wordv + 1))
    364 	) {
    365 		/*
    366 		 * Have message that tells us we have changed files
    367 		 */
    368 		language = INPI;
    369 		currentfilename = strdup(wordv[6]);
    370 		clob_last(currentfilename, '\0');
    371 		return (C_SYNC);
    372 	}
    373 	if ((wordc == 3)
    374 	   && (strcmp(wordv[1], "In") == 0)
    375 	   && (lastchar(wordv[3]) == ':')
    376 	   && (instringset(wordv[2], Piroutines))
    377 	) {
    378 		language = INPI;
    379 		c_header = wordvsplice(0, wordc, wordv+1);
    380 		return (C_SYNC);
    381 	}
    382 
    383 	/*
    384 	 * now, check for just the line number followed by the text
    385 	 */
    386 	if (alldigits(wordv[1])) {
    387 		language = INPI;
    388 		c_linenumber = wordv[1];
    389 		return (C_IGNORE);
    390 	}
    391 
    392 	/*
    393 	 * Attempt to match messages refering to a line number
    394 	 *
    395 	 * Multiply defined label in case, lines %d and %d
    396 	 * Goto %s from line %d is into a structured statement
    397 	 * End matched %s on line %d
    398 	 * Inserted keyword end matching %s on line %d
    399 	 */
    400 	multiple = structured = 0;
    401 	if (
    402 	       ((wordc == 6) && (wordvcmp(wordv+1, 2, pi_Endmatched) == 0))
    403 	    || ((wordc == 8) && (wordvcmp(wordv+1, 4, pi_Inserted) == 0))
    404 	    || (multiple = ((wordc == 9) && (wordvcmp(wordv+1,6, pi_multiple) == 0)))
    405 	    || (structured = ((wordc == 10) && (wordvcmp(wordv+6,5, pi_structured) == 0 )))
    406 	) {
    407 		language = INPI;
    408 		nwordv = wordvsplice(2, wordc, wordv+1);
    409 		nwordv[0] = strdup(currentfilename);
    410 		nwordv[1] = structured ? wordv [5] : wordv[wordc];
    411 		wordc += 2;
    412 		wordv = nwordv - 1;
    413 		if (!multiple)
    414 			return (C_TRUE);
    415 		erroradd(wordc, nwordv, C_TRUE, C_UNKNOWN);
    416 		nwordv = wordvsplice(0, wordc, nwordv);
    417 		nwordv[1] = wordv[wordc - 2];
    418 		return (C_TRUE);
    419 	}
    420 	return (C_UNKNOWN);
    421 }
    422