Home | History | Annotate | Line # | Download | only in error
pi.c revision 1.4
      1 /*	$NetBSD: pi.c,v 1.4 1997/10/18 14:44:37 lukem 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.4 1997/10/18 14:44:37 lukem Exp $");
     42 #endif /* not lint */
     43 
     44 #include <stdio.h>
     45 #include <ctype.h>
     46 #include <string.h>
     47 #include "error.h"
     48 
     49 extern	char	*currentfilename;
     50 static	char	*c_linenumber;
     51 static	char	*unk_hdr[] = {"In", "program", "???"};
     52 static	char	**c_header = &unk_hdr[0];
     53 
     54 boolean	alldigits __P((char *));
     55 boolean	isdateformat __P((int, char **));
     56 boolean	instringset __P((char *, char **));
     57 Errorclass pi __P((void));
     58 boolean	piptr __P((char *));
     59 
     60 
     61 /*
     62  *	Attempt to handle error messages produced by pi (and by pc)
     63  *
     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.
     82  *
     83  *	Here are the error strings used in pi version 1.2 that can refer
     84  *	to a file name or line number:
     85  *
     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
     90  *
     91  *	Here are the general pi patterns recognized:
     92  *	define piptr == -.*^-.*
     93  *	define msg = .*
     94  *	define digit = [0-9]
     95  *	definename = .*
     96  *	define date_format letter*3 letter*3 (digit | (digit digit))
     97  *			(digit | (digit digit)):digit*2 digit*4
     98  *
     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.
    107  *
    108  *	Here is the output from a compilation
    109  *
    110  *
    111  *	     2  	var	i:integer;
    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:
    116  *	     2  begin
    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
    126  *	In procedure foo:
    127  *	  w - variable x is neither used nor set
    128  *	     9  	z : = 23;
    129  *	E --------------^--- Undefined variable
    130  *	    10  	y = [1];
    131  *	e ----------------^--- Inserted ':'
    132  *	    13  	z := 345.;
    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
    138  *	In program mung:
    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
    143  */
    144 char *Months[] = {
    145 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
    146 	"Jul", "Aug", "Sep", "Oct","Nov", "Dec",
    147 	0
    148 };
    149 char *Days[] = {
    150 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", 0
    151 };
    152 char *Piroutines[] = {
    153 		"program", "function", "procedure", 0
    154 };
    155 
    156 
    157 static boolean	structured, multiple;
    158 
    159 char *pi_Endmatched[] = {"End", "matched"};
    160 char *pi_Inserted[] = {"Inserted", "keyword", "end", "matching"};
    161 
    162 char *pi_multiple[] = {"Mutiply", "defined", "label", "in", "case,", "line"};
    163 char *pi_structured[] = {"is", "into", "a", "structured", "statement"};
    164 
    165 char *pi_und1[] = {"undefined", "on", "line"};
    166 char *pi_und2[] = {"undefined", "on", "lines"};
    167 char *pi_imp1[] = {"improperly", "used", "on", "line"};
    168 char *pi_imp2[] = {"improperly", "used", "on", "lines"};
    169 
    170 boolean
    171 alldigits(string)
    172 	char	*string;
    173 {
    174 	for (; *string && isdigit(*string); string++)
    175 		continue;
    176 	return(*string == '\0');
    177 }
    178 
    179 boolean
    180 instringset(member, set)
    181 	char	*member;
    182 	char	**set;
    183 {
    184 	for(; *set; set++){
    185 		if (strcmp(*set, member) == 0)
    186 			return(TRUE);
    187 	}
    188 	return(FALSE);
    189 }
    190 
    191 boolean
    192 isdateformat(wordc, wordv)
    193 	int	wordc;
    194 	char	**wordv;
    195 {
    196 	return(
    197 	        (wordc == 5)
    198 	     && (instringset(wordv[0], Days))
    199 	     && (instringset(wordv[1], Months))
    200 	     && (alldigits(wordv[2]))
    201 	     && (alldigits(wordv[4])) );
    202 }
    203 
    204 boolean
    205 piptr(string)
    206 	char	*string;
    207 {
    208 	if (*string != '-')
    209 		return(FALSE);
    210 	while (*string && *string == '-')
    211 		string++;
    212 	if (*string != '^')
    213 		return(FALSE);
    214 	string++;
    215 	while (*string && *string == '-')
    216 		string++;
    217 	return(*string == '\0');
    218 }
    219 
    220 extern	int	wordc;
    221 extern	char	**wordv;
    222 
    223 Errorclass
    224 pi()
    225 {
    226 	char	**nwordv;
    227 
    228 	nwordv = NULL;
    229 	if (wordc < 2)
    230 		return (C_UNKNOWN);
    231 	if (   ( strlen(wordv[1]) == 1)
    232 	    && ( (wordv[1][0] == 'e') || (wordv[1][0] == 'E') )
    233 	    && ( piptr(wordv[2]) )
    234 	) {
    235 		boolean	longpiptr = 0;
    236 		/*
    237 		 *	We have recognized a first pass error of the form:
    238 		 *	letter ------^---- message
    239 		 *
    240 		 *	turn into an error message of the form:
    241 		 *
    242 		 *	file line 'pascal errortype' letter \n |---- message
    243 		 *	or of the form:
    244 		 *	file line letter |---- message
    245 		 *		when there are strlen("(*[pi]") or more
    246 		 *		preceding '-' on the error pointer.
    247 		 *
    248 		 *	Where the | is intended to be a down arrow, so that
    249 		 *	the pi error messages can be inserted above the
    250 		 *	line in error, instead of below.  (All of the other
    251 		 *	langauges put thier messages before the source line,
    252 		 *	instead of after it as does pi.)
    253 		 *
    254 		 *	where the pointer to the error has been truncated
    255 		 *	by 6 characters to account for the fact that
    256 		 *	the pointer points into a tab preceded input line.
    257 		 */
    258 		language = INPI;
    259 		(void)substitute(wordv[2], '^', '|');
    260 		longpiptr = position(wordv[2],'|') > (6+8);
    261 		nwordv = wordvsplice(longpiptr ? 2 : 4, wordc, wordv+1);
    262 		nwordv[0] = strsave(currentfilename);
    263 		nwordv[1] = strsave(c_linenumber);
    264 		if (!longpiptr){
    265 			nwordv[2] = "pascal errortype";
    266 			nwordv[3] = wordv[1];
    267 			nwordv[4] = strsave("%%%\n");
    268 			if (strlen(nwordv[5]) > (8-2))	/* this is the pointer */
    269 				nwordv[5] += (8-2);	/* bump over 6 characters */
    270 		}
    271 		wordv = nwordv - 1;		/* convert to 1 based */
    272 		wordc += longpiptr ? 2 : 4;
    273 		return(C_TRUE);
    274 	}
    275 	if (   (wordc >= 4)
    276 	    && (strlen(wordv[1]) == 1)
    277 	    && ( (*wordv[1] == 'E') || (*wordv[1] == 'w') || (*wordv[1] == 'e') )
    278 	    && (alldigits(wordv[2]))
    279 	    && (strlen(wordv[3]) == 1)
    280 	    && (wordv[3][0] == '-')
    281 	){
    282 		/*
    283 		 *	Message of the form: letter linenumber - message
    284 		 *	Turn into form: filename linenumber letter - message
    285 		 */
    286 		language = INPI;
    287 		nwordv = wordvsplice(1, wordc, wordv + 1);
    288 		nwordv[0] = strsave(currentfilename);
    289 		nwordv[1] = wordv[2];
    290 		nwordv[2] = wordv[1];
    291 		c_linenumber = wordv[2];
    292 		wordc += 1;
    293 		wordv = nwordv - 1;
    294 		return(C_TRUE);
    295 	}
    296 	if (   (wordc >= 3)
    297 	    && (strlen(wordv[1]) == 1)
    298 	    && ( (*(wordv[1]) == 'E') || (*(wordv[1]) == 'w') || (*(wordv[1]) == 'e') )
    299 	    && (strlen(wordv[2]) == 1)
    300 	    && (wordv[2][0] == '-')
    301 	) {
    302 		/*
    303 		 *	Message of the form: letter - message
    304 		 *	This happens only when we are traversing the tree
    305 		 *	during the second pass of pi, and discover semantic
    306 		 *	errors.
    307 		 *
    308 		 *	We have already (presumably) saved the header message
    309 		 *	and can now construct a nulled error message for the
    310 		 *	current file.
    311 		 *
    312 		 *	Turns into a message of the form:
    313 		 *	filename (header) letter - message
    314 		 *
    315 		 *	First, see if it is a message referring to more than
    316 		 *	one line number.  Only of the form:
    317  		 *		%s undefined on line%s
    318  		 *		%s improperly used on line%s
    319 		 */
    320 		boolean undefined = 0;
    321 		int	wordindex;
    322 
    323 		language = INPI;
    324 		if (    (undefined = (wordvcmp(wordv+2, 3, pi_und1) == 0) )
    325 		     || (undefined = (wordvcmp(wordv+2, 3, pi_und2) == 0) )
    326 		     || (wordvcmp(wordv+2, 4, pi_imp1) == 0)
    327 		     || (wordvcmp(wordv+2, 4, pi_imp2) == 0)
    328 		){
    329 			for (wordindex = undefined ? 5 : 6; wordindex <= wordc;
    330 			    wordindex++){
    331 				nwordv = wordvsplice(2, undefined ? 2 : 3, wordv+1);
    332 				nwordv[0] = strsave(currentfilename);
    333 				nwordv[1] = wordv[wordindex];
    334 				if (wordindex != wordc)
    335 					erroradd(undefined ? 4 : 5, nwordv,
    336 						C_TRUE, C_UNKNOWN);
    337 			}
    338 			wordc = undefined ? 4 : 5;
    339 			wordv = nwordv - 1;
    340 			return(C_TRUE);
    341 		}
    342 
    343 		nwordv = wordvsplice(1+3, wordc, wordv+1);
    344 		nwordv[0] = strsave(currentfilename);
    345 		nwordv[1] = strsave(c_header[0]);
    346 		nwordv[2] = strsave(c_header[1]);
    347 		nwordv[3] = strsave(c_header[2]);
    348 		wordv = nwordv - 1;
    349 		wordc += 1 + 3;
    350 		return(C_THISFILE);
    351 	}
    352 	if (strcmp(wordv[1], "...") == 0){
    353 		/*
    354 		 *	have a continuation error message
    355 		 *	of the form: ... message
    356 		 *	Turn into form : filename linenumber message
    357 		 */
    358 		language = INPI;
    359 		nwordv = wordvsplice(1, wordc, wordv+1);
    360 		nwordv[0] = strsave(currentfilename);
    361 		nwordv[1] = strsave(c_linenumber);
    362 		wordv = nwordv - 1;
    363 		wordc += 1;
    364 		return(C_TRUE);
    365 	}
    366 	if(   (wordc == 6)
    367 	   && (lastchar(wordv[6]) == ':')
    368 	   && (isdateformat(5, wordv + 1))
    369 	){
    370 		/*
    371 		 *	Have message that tells us we have changed files
    372 		 */
    373 		language = INPI;
    374 		currentfilename = strsave(wordv[6]);
    375 		clob_last(currentfilename, '\0');
    376 		return(C_SYNC);
    377 	}
    378 	if(   (wordc == 3)
    379 	   && (strcmp(wordv[1], "In") == 0)
    380 	   && (lastchar(wordv[3]) == ':')
    381 	   && (instringset(wordv[2], Piroutines))
    382 	) {
    383 		language = INPI;
    384 		c_header = wordvsplice(0, wordc, wordv+1);
    385 		return(C_SYNC);
    386 	}
    387 	/*
    388 	 *	now, check for just the line number followed by the text
    389 	 */
    390 	if (alldigits(wordv[1])){
    391 		language = INPI;
    392 		c_linenumber = wordv[1];
    393 		return(C_IGNORE);
    394 	}
    395 	/*
    396 	 *	Attempt to match messages refering to a line number
    397 	 *
    398 	 *	Multiply defined label in case, lines %d and %d
    399 	 *	Goto %s from line %d is into a structured statement
    400 	 *	End matched %s on line %d
    401 	 *	Inserted keyword end matching %s on line %d
    402 	 */
    403 	multiple = structured = 0;
    404 	if (
    405 	       ( (wordc == 6) && (wordvcmp(wordv+1, 2, pi_Endmatched) == 0))
    406 	    || ( (wordc == 8) && (wordvcmp(wordv+1, 4, pi_Inserted) == 0))
    407 	    || ( multiple = ((wordc == 9) && (wordvcmp(wordv+1,6, pi_multiple) == 0) ) )
    408 	    || ( structured = ((wordc == 10) && (wordvcmp(wordv+6,5, pi_structured) == 0 ) ))
    409 	){
    410 		language = INPI;
    411 		nwordv = wordvsplice(2, wordc, wordv+1);
    412 		nwordv[0] = strsave(currentfilename);
    413 		nwordv[1] = structured ? wordv [5] : wordv[wordc];
    414 		wordc += 2;
    415 		wordv = nwordv - 1;
    416 		if (!multiple)
    417 			return(C_TRUE);
    418 		erroradd(wordc, nwordv, C_TRUE, C_UNKNOWN);
    419 		nwordv = wordvsplice(0, wordc, nwordv);
    420 		nwordv[1] = wordv[wordc - 2];
    421 		return(C_TRUE);
    422 	}
    423 	return(C_UNKNOWN);
    424 }
    425