Home | History | Annotate | Line # | Download | only in error
pi.c revision 1.13
      1 /*	$NetBSD: pi.c,v 1.13 2009/08/13 02:10:50 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.13 2009/08/13 02:10:50 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(char *);
     51 static boolean isdateformat(int, char **);
     52 static boolean instringset(char *, char **);
     53 static 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 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(char *string)
    167 {
    168 	for (; *string && isdigit((unsigned char)*string); string++)
    169 		continue;
    170 	return(*string == '\0');
    171 }
    172 
    173 static 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 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(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 				if (nwordv) {
    321 					free(nwordv[0]);
    322 					free(nwordv);
    323 				}
    324 				nwordv = wordvsplice(2, undefined ? 2 : 3, wordv+1);
    325 				nwordv[0] = strdup(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] = strdup(currentfilename);
    338 		nwordv[1] = strdup(c_header[0]);
    339 		nwordv[2] = strdup(c_header[1]);
    340 		nwordv[3] = strdup(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] = strdup(currentfilename);
    354 		nwordv[1] = strdup(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 = strdup(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] = strdup(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