Home | History | Annotate | Line # | Download | only in error
pi.c revision 1.12
      1 /*	$NetBSD: pi.c,v 1.12 2006/04/09 19:21:26 christos 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.12 2006/04/09 19:21:26 christos 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 boolean	alldigits(char *);
     51 boolean	isdateformat(int, char **);
     52 boolean	instringset(char *, char **);
     53 Errorclass pi(void);
     54 boolean	piptr(char *);
     55 
     56 
     57 /*
     58  *	Attempt to handle error messages produced by pi (and by pc)
     59  *
     60  *	problem #1:	There is no file name available when a file does not
     61  *			use a #include; this will have to be given to error
     62  *			in the command line.
     63  *	problem #2:	pi doesn't always tell you what line number
     64  *			a error refers to; for example during the tree
     65  *			walk phase of code generation and error detection,
     66  *			an error can refer to "variable foo in procedure bletch"
     67  *			without giving a line number
     68  *	problem #3:	line numbers, when available, are attached to
     69  *			the source line, along with the source line itself
     70  *			These line numbers must be extracted, and
     71  *			the source line thrown away.
     72  *	problem #4:	Some error messages produce more than one line number
     73  *			on the same message.
     74  *			There are only two (I think):
     75  *				%s undefined on line%s
     76  *				%s improperly used on line%s
     77  *			here, the %s makes line plural or singular.
     78  *
     79  *	Here are the error strings used in pi version 1.2 that can refer
     80  *	to a file name or line number:
     81  *
     82  *		Multiply defined label in case, lines %d and %d
     83  *		Goto %s from line %d is into a structured statement
     84  *		End matched %s on line %d
     85  *		Inserted keyword end matching %s on line %d
     86  *
     87  *	Here are the general pi patterns recognized:
     88  *	define piptr == -.*^-.*
     89  *	define msg = .*
     90  *	define digit = [0-9]
     91  *	definename = .*
     92  *	define date_format letter*3 letter*3 (digit | (digit digit))
     93  *			(digit | (digit digit)):digit*2 digit*4
     94  *
     95  *	{e,E} (piptr) (msg)	Encounter an error during textual scan
     96  *	E {digit}* - (msg)	Have an error message that refers to a new line
     97  *	E - msg			Have an error message that refers to current
     98  *					function, program or procedure
     99  *	(date_format) (name):	When switch compilation files
    100  *	... (msg)		When refer to the previous line
    101  *	'In' ('procedure'|'function'|'program') (name):
    102  *				pi is now complaining about 2nd pass errors.
    103  *
    104  *	Here is the output from a compilation
    105  *
    106  *
    107  *	     2  	var	i:integer;
    108  *	e --------------^--- Inserted ';'
    109  *	E 2 - All variables must be declared in one var part
    110  *	E 5 - Include filename must end in .i
    111  *	Mon Apr 21 15:56 1980  test.h:
    112  *	     2  begin
    113  *	e ------^--- Inserted ';'
    114  *	Mon Apr 21 16:06 1980  test.p:
    115  *	E 2 - Function type must be specified
    116  *	     6  procedure foo(var x:real);
    117  *	e ------^--- Inserted ';'
    118  *	In function bletch:
    119  *	  E - No assignment to the function variable
    120  *	  w - variable x is never used
    121  *	E 6 - foo is already defined in this block
    122  *	In procedure foo:
    123  *	  w - variable x is neither used nor set
    124  *	     9  	z : = 23;
    125  *	E --------------^--- Undefined variable
    126  *	    10  	y = [1];
    127  *	e ----------------^--- Inserted ':'
    128  *	    13  	z := 345.;
    129  *	e -----------------------^--- Digits required after decimal point
    130  *	E 10 - Constant set involved in non set context
    131  *	E 11 - Type clash: real is incompatible with integer
    132  *	   ... Type of expression clashed with type of variable in assignment
    133  *	E 12 - Parameter type not identical to type of var parameter x of foo
    134  *	In program mung:
    135  *	  w - variable y is never used
    136  *	  w - type foo is never used
    137  *	  w - function bletch is never used
    138  *	  E - z undefined on lines 9 13
    139  */
    140 char *Months[] = {
    141 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
    142 	"Jul", "Aug", "Sep", "Oct","Nov", "Dec",
    143 	0
    144 };
    145 char *Days[] = {
    146 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", 0
    147 };
    148 char *Piroutines[] = {
    149 		"program", "function", "procedure", 0
    150 };
    151 
    152 
    153 static boolean	structured, multiple;
    154 
    155 char *pi_Endmatched[] = {"End", "matched"};
    156 char *pi_Inserted[] = {"Inserted", "keyword", "end", "matching"};
    157 
    158 char *pi_multiple[] = {"Mutiply", "defined", "label", "in", "case,", "line"};
    159 char *pi_structured[] = {"is", "into", "a", "structured", "statement"};
    160 
    161 char *pi_und1[] = {"undefined", "on", "line"};
    162 char *pi_und2[] = {"undefined", "on", "lines"};
    163 char *pi_imp1[] = {"improperly", "used", "on", "line"};
    164 char *pi_imp2[] = {"improperly", "used", "on", "lines"};
    165 
    166 boolean
    167 alldigits(char *string)
    168 {
    169 	for (; *string && isdigit((unsigned char)*string); string++)
    170 		continue;
    171 	return(*string == '\0');
    172 }
    173 
    174 boolean
    175 instringset(char *member, char **set)
    176 {
    177 	for(; *set; set++){
    178 		if (strcmp(*set, member) == 0)
    179 			return(TRUE);
    180 	}
    181 	return(FALSE);
    182 }
    183 
    184 boolean
    185 isdateformat(int wordc, char **wordv)
    186 {
    187 	return(
    188 	        (wordc == 5)
    189 	     && (instringset(wordv[0], Days))
    190 	     && (instringset(wordv[1], Months))
    191 	     && (alldigits(wordv[2]))
    192 	     && (alldigits(wordv[4])) );
    193 }
    194 
    195 boolean
    196 piptr(char *string)
    197 {
    198 	if (*string != '-')
    199 		return(FALSE);
    200 	while (*string && *string == '-')
    201 		string++;
    202 	if (*string != '^')
    203 		return(FALSE);
    204 	string++;
    205 	while (*string && *string == '-')
    206 		string++;
    207 	return(*string == '\0');
    208 }
    209 
    210 extern	int	wordc;
    211 extern	char	**wordv;
    212 
    213 Errorclass
    214 pi(void)
    215 {
    216 	char	**nwordv;
    217 
    218 	nwordv = NULL;
    219 	if (wordc < 2)
    220 		return (C_UNKNOWN);
    221 	if (   ( strlen(wordv[1]) == 1)
    222 	    && ( (wordv[1][0] == 'e') || (wordv[1][0] == 'E') )
    223 	    && ( piptr(wordv[2]) )
    224 	) {
    225 		boolean	longpiptr = 0;
    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 		 *	This happens only when we are traversing the tree
    295 		 *	during the second pass of pi, and discover semantic
    296 		 *	errors.
    297 		 *
    298 		 *	We have already (presumably) saved the header message
    299 		 *	and can now construct a nulled error message for the
    300 		 *	current file.
    301 		 *
    302 		 *	Turns into a message of the form:
    303 		 *	filename (header) letter - message
    304 		 *
    305 		 *	First, see if it is a message referring to more than
    306 		 *	one line number.  Only of the form:
    307  		 *		%s undefined on line%s
    308  		 *		%s improperly used on line%s
    309 		 */
    310 		boolean undefined = 0;
    311 		int	wordindex;
    312 
    313 		language = INPI;
    314 		if (    (undefined = (wordvcmp(wordv+2, 3, pi_und1) == 0) )
    315 		     || (undefined = (wordvcmp(wordv+2, 3, pi_und2) == 0) )
    316 		     || (wordvcmp(wordv+2, 4, pi_imp1) == 0)
    317 		     || (wordvcmp(wordv+2, 4, pi_imp2) == 0)
    318 		){
    319 			for (wordindex = undefined ? 5 : 6; wordindex <= wordc;
    320 			    wordindex++){
    321 				if (nwordv) {
    322 					free(nwordv[0]);
    323 					free(nwordv);
    324 				}
    325 				nwordv = wordvsplice(2, undefined ? 2 : 3, wordv+1);
    326 				nwordv[0] = strdup(currentfilename);
    327 				nwordv[1] = wordv[wordindex];
    328 				if (wordindex != wordc)
    329 					erroradd(undefined ? 4 : 5, nwordv,
    330 						C_TRUE, C_UNKNOWN);
    331 			}
    332 			wordc = undefined ? 4 : 5;
    333 			wordv = nwordv - 1;
    334 			return(C_TRUE);
    335 		}
    336 
    337 		nwordv = wordvsplice(1+3, wordc, wordv+1);
    338 		nwordv[0] = strdup(currentfilename);
    339 		nwordv[1] = strdup(c_header[0]);
    340 		nwordv[2] = strdup(c_header[1]);
    341 		nwordv[3] = strdup(c_header[2]);
    342 		wordv = nwordv - 1;
    343 		wordc += 1 + 3;
    344 		return(C_THISFILE);
    345 	}
    346 	if (strcmp(wordv[1], "...") == 0){
    347 		/*
    348 		 *	have a continuation error message
    349 		 *	of the form: ... message
    350 		 *	Turn into form : filename linenumber message
    351 		 */
    352 		language = INPI;
    353 		nwordv = wordvsplice(1, wordc, wordv+1);
    354 		nwordv[0] = strdup(currentfilename);
    355 		nwordv[1] = strdup(c_linenumber);
    356 		wordv = nwordv - 1;
    357 		wordc += 1;
    358 		return(C_TRUE);
    359 	}
    360 	if(   (wordc == 6)
    361 	   && (lastchar(wordv[6]) == ':')
    362 	   && (isdateformat(5, wordv + 1))
    363 	){
    364 		/*
    365 		 *	Have message that tells us we have changed files
    366 		 */
    367 		language = INPI;
    368 		currentfilename = strdup(wordv[6]);
    369 		clob_last(currentfilename, '\0');
    370 		return(C_SYNC);
    371 	}
    372 	if(   (wordc == 3)
    373 	   && (strcmp(wordv[1], "In") == 0)
    374 	   && (lastchar(wordv[3]) == ':')
    375 	   && (instringset(wordv[2], Piroutines))
    376 	) {
    377 		language = INPI;
    378 		c_header = wordvsplice(0, wordc, wordv+1);
    379 		return(C_SYNC);
    380 	}
    381 	/*
    382 	 *	now, check for just the line number followed by the text
    383 	 */
    384 	if (alldigits(wordv[1])){
    385 		language = INPI;
    386 		c_linenumber = wordv[1];
    387 		return(C_IGNORE);
    388 	}
    389 	/*
    390 	 *	Attempt to match messages refering to a line number
    391 	 *
    392 	 *	Multiply defined label in case, lines %d and %d
    393 	 *	Goto %s from line %d is into a structured statement
    394 	 *	End matched %s on line %d
    395 	 *	Inserted keyword end matching %s on line %d
    396 	 */
    397 	multiple = structured = 0;
    398 	if (
    399 	       ( (wordc == 6) && (wordvcmp(wordv+1, 2, pi_Endmatched) == 0))
    400 	    || ( (wordc == 8) && (wordvcmp(wordv+1, 4, pi_Inserted) == 0))
    401 	    || ( multiple = ((wordc == 9) && (wordvcmp(wordv+1,6, pi_multiple) == 0) ) )
    402 	    || ( structured = ((wordc == 10) && (wordvcmp(wordv+6,5, pi_structured) == 0 ) ))
    403 	){
    404 		language = INPI;
    405 		nwordv = wordvsplice(2, wordc, wordv+1);
    406 		nwordv[0] = strdup(currentfilename);
    407 		nwordv[1] = structured ? wordv [5] : wordv[wordc];
    408 		wordc += 2;
    409 		wordv = nwordv - 1;
    410 		if (!multiple)
    411 			return(C_TRUE);
    412 		erroradd(wordc, nwordv, C_TRUE, C_UNKNOWN);
    413 		nwordv = wordvsplice(0, wordc, nwordv);
    414 		nwordv[1] = wordv[wordc - 2];
    415 		return(C_TRUE);
    416 	}
    417 	return(C_UNKNOWN);
    418 }
    419