Home | History | Annotate | Line # | Download | only in lint2
read.c revision 1.21
      1 /* $NetBSD: read.c,v 1.21 2008/04/26 17:11:52 christos Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
      5  * Copyright (c) 1994, 1995 Jochen Pohl
      6  * All Rights Reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by Jochen Pohl for
     19  *	The NetBSD Project.
     20  * 4. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #if HAVE_NBTOOL_CONFIG_H
     36 #include "nbtool_config.h"
     37 #endif
     38 
     39 #include <sys/cdefs.h>
     40 #if defined(__RCSID) && !defined(lint)
     41 __RCSID("$NetBSD: read.c,v 1.21 2008/04/26 17:11:52 christos Exp $");
     42 #endif
     43 
     44 #include <ctype.h>
     45 #include <limits.h>
     46 #include <stdio.h>
     47 #include <stdlib.h>
     48 #include <string.h>
     49 #include <stdarg.h>
     50 
     51 #include "lint2.h"
     52 
     53 
     54 /* index of current (included) source file */
     55 static	int	srcfile;
     56 
     57 /*
     58  * The array pointed to by inpfns maps the file name indices of input files
     59  * to the file name indices used in lint2
     60  */
     61 static	short	*inpfns;
     62 static	size_t	ninpfns;
     63 
     64 /*
     65  * The array pointed to by *fnames maps file name indizes to file names.
     66  * Indices of type short are used instead of pointers to save memory.
     67  */
     68 const	char **fnames;
     69 static	size_t *flines;
     70 static	size_t	nfnames;
     71 
     72 /*
     73  * Types are shared (to save memory for the types itself) and accessed
     74  * via indices (to save memory for references to types (indices are short)).
     75  * To share types, a equal type must be located fast. This is done by a
     76  * hash table. Access by indices is done via an array of pointers to the
     77  * types.
     78  */
     79 typedef struct thtab {
     80 	const	char *th_name;
     81 	u_short	th_idx;
     82 	struct	thtab *th_nxt;
     83 } thtab_t;
     84 static	thtab_t	**thtab;		/* hash table */
     85 type_t	**tlst;				/* array for indexed access */
     86 static	size_t	tlstlen;		/* length of tlst */
     87 
     88 static	hte_t **renametab;
     89 
     90 /* index of current C source file (as spezified at the command line) */
     91 static	int	csrcfile;
     92 
     93 
     94 #define 	inperr(fmt, args...) \
     95     inperror(__FILE__, __LINE__, fmt, ##args)
     96 static	void	inperror(const char *, size_t, const char *, ...);
     97 static	void	setsrc(const char *);
     98 static	void	setfnid(int, const char *);
     99 static	void	funccall(pos_t *, const char *);
    100 static	void	decldef(pos_t *, const char *);
    101 static	void	usedsym(pos_t *, const char *);
    102 static	u_short	inptype(const char *, const char **);
    103 static	int	gettlen(const char *, const char **);
    104 static	u_short	findtype(const char *, size_t, int);
    105 static	u_short	storetyp(type_t *, const char *, size_t, int);
    106 static	int	thash(const char *, size_t);
    107 static	char	*inpqstrg(const char *, const char **);
    108 static	const	char *inpname(const char *, const char **);
    109 static	int	getfnidx(const char *);
    110 
    111 void
    112 readfile(const char *name)
    113 {
    114 	FILE	*inp;
    115 	size_t	len;
    116 	const	char *cp;
    117 	char	*line, *eptr, rt = '\0';
    118 	int	cline, isrc, iline;
    119 	pos_t	pos;
    120 
    121 	if (inpfns == NULL)
    122 		inpfns = xcalloc(ninpfns = 128, sizeof (short));
    123 	if (fnames == NULL)
    124 		fnames = xcalloc(nfnames = 256, sizeof (char *));
    125 	if (flines == NULL)
    126 		flines = xcalloc(nfnames, sizeof (size_t));
    127 	if (tlstlen == 0)
    128 		tlst = xcalloc(tlstlen = 256, sizeof (type_t *));
    129 	if (thtab == NULL)
    130 		thtab = xcalloc(THSHSIZ2, sizeof (thtab_t));
    131 
    132 	_inithash(&renametab);
    133 
    134 	srcfile = getfnidx(name);
    135 
    136 	if ((inp = fopen(name, "r")) == NULL)
    137 		err(1, "cannot open %s", name);
    138 
    139 	while ((line = fgetln(inp, &len)) != NULL) {
    140 		flines[srcfile]++;
    141 
    142 		if (len == 0 || line[len - 1] != '\n')
    143 			inperr("%s", &line[len - 1]);
    144 		line[len - 1] = '\0';
    145 		cp = line;
    146 
    147 		/* line number in csrcfile */
    148 		cline = (int)strtol(cp, &eptr, 10);
    149 		if (cp == eptr) {
    150 		        cline = -1;
    151 		} else {
    152 			cp = eptr;
    153 		}
    154 
    155 		/* record type */
    156 		if (*cp != '\0') {
    157 			rt = *cp++;
    158 		} else {
    159 			inperr("null cp");
    160 		}
    161 
    162 		if (rt == 'S') {
    163 			setsrc(cp);
    164 			continue;
    165 		} else if (rt == 's') {
    166 			setfnid(cline, cp);
    167 			continue;
    168 		}
    169 
    170 		/*
    171 		 * Index of (included) source file. If this index is
    172 		 * different from csrcfile, it refers to an included
    173 		 * file.
    174 		 */
    175 		isrc = (int)strtol(cp, &eptr, 10);
    176 		if (cp == eptr)
    177 			inperr("not a number: %s", cp);
    178 		cp = eptr;
    179 		isrc = inpfns[isrc];
    180 
    181 		/* line number in isrc */
    182 		if (*cp++ != '.')
    183 			inperr("bad line number");
    184 		iline = (int)strtol(cp, &eptr, 10);
    185 		if (cp == eptr)
    186 			inperr("not a number: %s", cp);
    187 		cp = eptr;
    188 
    189 		pos.p_src = (u_short)csrcfile;
    190 		pos.p_line = (u_short)cline;
    191 		pos.p_isrc = (u_short)isrc;
    192 		pos.p_iline = (u_short)iline;
    193 
    194 		/* process rest of this record */
    195 		switch (rt) {
    196 		case 'c':
    197 			funccall(&pos, cp);
    198 			break;
    199 		case 'd':
    200 			decldef(&pos, cp);
    201 			break;
    202 		case 'u':
    203 			usedsym(&pos, cp);
    204 			break;
    205 		default:
    206 			inperr("bad record type %c", rt);
    207 		}
    208 
    209 	}
    210 
    211 	_destroyhash(renametab);
    212 
    213 	if (ferror(inp))
    214 		err(1, "read error on %s", name);
    215 
    216 	(void)fclose(inp);
    217 }
    218 
    219 
    220 static void
    221 inperror(const char *file, size_t line, const char *fmt, ...)
    222 {
    223 	va_list ap;
    224 	char buf[1024];
    225 
    226 	va_start(ap, fmt);
    227 	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
    228 	va_end(ap);
    229 
    230 	errx(1, "%s,%zu: input file error: %s,%zu (%s)", file, line,
    231 	    fnames[srcfile], flines[srcfile], buf);
    232 }
    233 
    234 /*
    235  * Set the name of the C source file of the .ln file which is
    236  * currently read.
    237  */
    238 static void
    239 setsrc(const char *cp)
    240 {
    241 
    242 	csrcfile = getfnidx(cp);
    243 }
    244 
    245 /*
    246  * setfnid() gets as input an index as used in an input file and the
    247  * associated file name. If necessary, it creates a new lint2 file
    248  * name index for this file name and creates the mapping of the index
    249  * as used in the input file to the index used in lint2.
    250  */
    251 static void
    252 setfnid(int fid, const char *cp)
    253 {
    254 
    255 	if (fid == -1)
    256 		inperr("bad fid");
    257 
    258 	if (fid >= ninpfns) {
    259 		inpfns = xrealloc(inpfns, (ninpfns * 2) * sizeof (short));
    260 		(void)memset(inpfns + ninpfns, 0, ninpfns * sizeof (short));
    261 		ninpfns *= 2;
    262 	}
    263 	/*
    264 	 * Should always be true because indices written in the output
    265 	 * file by lint1 are always the previous index + 1.
    266 	 */
    267 	if (fid >= ninpfns)
    268 		errx(1, "internal error: setfnid()");
    269 	inpfns[fid] = (u_short)getfnidx(cp);
    270 }
    271 
    272 /*
    273  * Process a function call record (c-record).
    274  */
    275 static void
    276 funccall(pos_t *posp, const char *cp)
    277 {
    278 	arginf_t *ai, **lai;
    279 	char	c, *eptr;
    280 	int	rused, rdisc;
    281 	hte_t	*hte;
    282 	fcall_t	*fcall;
    283 	const char *name;
    284 
    285 	fcall = xalloc(sizeof (fcall_t));
    286 	STRUCT_ASSIGN(fcall->f_pos, *posp);
    287 
    288 	/* read flags */
    289 	rused = rdisc = 0;
    290 	lai = &fcall->f_args;
    291 	while ((c = *cp) == 'u' || c == 'i' || c == 'd' ||
    292 	       c == 'z' || c == 'p' || c == 'n' || c == 's') {
    293 		cp++;
    294 		switch (c) {
    295 		case 'u':
    296 			if (rused || rdisc)
    297 				inperr("used or discovered: %c", c);
    298 			rused = 1;
    299 			break;
    300 		case 'i':
    301 			if (rused || rdisc)
    302 				inperr("used or discovered: %c", c);
    303 			break;
    304 		case 'd':
    305 			if (rused || rdisc)
    306 				inperr("used or discovered: %c", c);
    307 			rdisc = 1;
    308 			break;
    309 		case 'z':
    310 		case 'p':
    311 		case 'n':
    312 		case 's':
    313 			ai = xalloc(sizeof (arginf_t));
    314 			ai->a_num = (int)strtol(cp, &eptr, 10);
    315 			if (cp == eptr)
    316 				inperr("bad number: %s", cp);
    317 			cp = eptr;
    318 			if (c == 'z') {
    319 				ai->a_pcon = ai->a_zero = 1;
    320 			} else if (c == 'p') {
    321 				ai->a_pcon = 1;
    322 			} else if (c == 'n') {
    323 				ai->a_ncon = 1;
    324 			} else {
    325 				ai->a_fmt = 1;
    326 				ai->a_fstrg = inpqstrg(cp, &cp);
    327 			}
    328 			*lai = ai;
    329 			lai = &ai->a_nxt;
    330 			break;
    331 		}
    332 	}
    333 	fcall->f_rused = rused;
    334 	fcall->f_rdisc = rdisc;
    335 
    336 	/* read name of function */
    337 	name = inpname(cp, &cp);
    338 
    339 	/* first look it up in the renaming table, then in the normal table */
    340 	hte = _hsearch(renametab, name, 0);
    341 	if (hte != NULL)
    342 		hte = hte->h_hte;
    343 	else
    344 		hte = hsearch(name, 1);
    345 	hte->h_used = 1;
    346 
    347 	fcall->f_type = inptype(cp, &cp);
    348 
    349 	*hte->h_lcall = fcall;
    350 	hte->h_lcall = &fcall->f_nxt;
    351 
    352 	if (*cp != '\0')
    353 		inperr("trailing line data: %s", cp);
    354 }
    355 
    356 /*
    357  * Process a declaration or definition (d-record).
    358  */
    359 static void
    360 decldef(pos_t *posp, const char *cp)
    361 {
    362 	sym_t	*symp, sym;
    363 	char	c, *ep, *pos1;
    364 	int	used, renamed;
    365 	hte_t	*hte, *renamehte = NULL;
    366 	const char *name, *rename;
    367 
    368 	(void)memset(&sym, 0, sizeof (sym));
    369 	STRUCT_ASSIGN(sym.s_pos, *posp);
    370 	sym.s_def = NODECL;
    371 
    372 	used = 0;
    373 
    374 	while (strchr("tdeurosvPS", (c = *cp)) != NULL) {
    375 		cp++;
    376 		switch (c) {
    377 		case 't':
    378 			if (sym.s_def != NODECL)
    379 				inperr("nodecl %c", c);
    380 			sym.s_def = TDEF;
    381 			break;
    382 		case 'd':
    383 			if (sym.s_def != NODECL)
    384 				inperr("nodecl %c", c);
    385 			sym.s_def = DEF;
    386 			break;
    387 		case 'e':
    388 			if (sym.s_def != NODECL)
    389 				inperr("nodecl %c", c);
    390 			sym.s_def = DECL;
    391 			break;
    392 		case 'u':
    393 			if (used)
    394 				inperr("used %c", c);
    395 			used = 1;
    396 			break;
    397 		case 'r':
    398 			if (sym.s_rval)
    399 				inperr("rval");
    400 			sym.s_rval = 1;
    401 			break;
    402 		case 'o':
    403 			if (sym.s_osdef)
    404 				inperr("osdef");
    405 			sym.s_osdef = 1;
    406 			break;
    407 		case 's':
    408 			if (sym.s_static)
    409 				inperr("static");
    410 			sym.s_static = 1;
    411 			break;
    412 		case 'v':
    413 			if (sym.s_va)
    414 				inperr("va");
    415 			sym.s_va = 1;
    416 			sym.s_nva = (short)strtol(cp, &ep, 10);
    417 			if (cp == ep)
    418 				inperr("bad number: %s", cp);
    419 			cp = ep;
    420 			break;
    421 		case 'P':
    422 			if (sym.s_prfl)
    423 				inperr("prfl");
    424 			sym.s_prfl = 1;
    425 			sym.s_nprfl = (short)strtol(cp, &ep, 10);
    426 			if (cp == ep)
    427 				inperr("bad number: %s", cp);
    428 			cp = ep;
    429 			break;
    430 		case 'S':
    431 			if (sym.s_scfl)
    432 				inperr("scfl");
    433 			sym.s_scfl = 1;
    434 			sym.s_nscfl = (short)strtol(cp, &ep, 10);
    435 			if (cp == ep)
    436 				inperr("bad number: %s", cp);
    437 			cp = ep;
    438 			break;
    439 		}
    440 	}
    441 
    442 	/* read symbol name, doing renaming if necessary */
    443 	name = inpname(cp, &cp);
    444 	renamed = 0;
    445 	if (*cp == 'r') {
    446 		cp++;
    447 		name = xstrdup(name);
    448 		rename = inpname(cp, &cp);
    449 
    450 		/* enter it and see if it's already been renamed */
    451 		renamehte = _hsearch(renametab, name, 1);
    452 		if (renamehte->h_hte == NULL) {
    453 			hte = hsearch(rename, 1);
    454 			renamehte->h_hte = hte;
    455 			renamed = 1;
    456 		} else if (strcmp((hte = renamehte->h_hte)->h_name, rename)) {
    457 			pos1 = xstrdup(mkpos(&renamehte->h_syms->s_pos));
    458 			/* %s renamed multiple times\t%s  ::  %s */
    459 			msg(18, name, pos1, mkpos(&sym.s_pos));
    460 			free(pos1);
    461 		}
    462 		free((char *)name);
    463 	} else {
    464 		/* it might be a previously-done rename */
    465 		hte = _hsearch(renametab, name, 0);
    466 		if (hte != NULL)
    467 			hte = hte->h_hte;
    468 		else
    469 			hte = hsearch(name, 1);
    470 	}
    471 	hte->h_used |= used;
    472 	if (sym.s_def == DEF || sym.s_def == TDEF)
    473 		hte->h_def = 1;
    474 
    475 	sym.s_type = inptype(cp, &cp);
    476 
    477 	/*
    478 	 * Allocate memory for this symbol only if it was not already
    479 	 * declared or tentatively defined at the same location with
    480 	 * the same type. Works only for symbols with external linkage,
    481 	 * because static symbols, tentatively defined at the same location
    482 	 * but in different translation units are really different symbols.
    483 	 */
    484 	for (symp = hte->h_syms; symp != NULL; symp = symp->s_nxt) {
    485 		if (symp->s_pos.p_isrc == sym.s_pos.p_isrc &&
    486 		    symp->s_pos.p_iline == sym.s_pos.p_iline &&
    487 		    symp->s_type == sym.s_type &&
    488 		    ((symp->s_def == DECL && sym.s_def == DECL) ||
    489 		     (!sflag && symp->s_def == TDEF && sym.s_def == TDEF)) &&
    490 		    !symp->s_static && !sym.s_static) {
    491 			break;
    492 		}
    493 	}
    494 
    495 	if (symp == NULL) {
    496 		/* allocsym reserviert keinen Platz fuer s_nva */
    497 		if (sym.s_va || sym.s_prfl || sym.s_scfl) {
    498 			symp = xalloc(sizeof (sym_t));
    499 			STRUCT_ASSIGN(*symp, sym);
    500 		} else {
    501 			symp = xalloc(sizeof (symp->s_s));
    502 			STRUCT_ASSIGN(symp->s_s, sym.s_s);
    503 		}
    504 		*hte->h_lsym = symp;
    505 		hte->h_lsym = &symp->s_nxt;
    506 
    507 		/* XXX hack so we can remember where a symbol was renamed */
    508 		if (renamed)
    509 			renamehte->h_syms = symp;
    510 	}
    511 
    512 	if (*cp != '\0')
    513 		inperr("trailing line: %s", cp);
    514 }
    515 
    516 /*
    517  * Read an u-record (emited by lint1 if a symbol was used).
    518  */
    519 static void
    520 usedsym(pos_t *posp, const char *cp)
    521 {
    522 	usym_t	*usym;
    523 	hte_t	*hte;
    524 	const char *name;
    525 
    526 	usym = xalloc(sizeof (usym_t));
    527 	STRUCT_ASSIGN(usym->u_pos, *posp);
    528 
    529 	/* needed as delimiter between two numbers */
    530 	if (*cp++ != 'x')
    531 		inperr("bad delim %c", cp[-1]);
    532 
    533 	name = inpname(cp, &cp);
    534 	hte = _hsearch(renametab, name, 0);
    535 	if (hte != NULL)
    536 		hte = hte->h_hte;
    537 	else
    538 		hte = hsearch(name, 1);
    539 	hte->h_used = 1;
    540 
    541 	*hte->h_lusym = usym;
    542 	hte->h_lusym = &usym->u_nxt;
    543 }
    544 
    545 /*
    546  * Read a type and return the index of this type.
    547  */
    548 static u_short
    549 inptype(const char *cp, const char **epp)
    550 {
    551 	char	c, s, *eptr;
    552 	const	char *ep;
    553 	type_t	*tp;
    554 	int	narg, i, osdef = 0;
    555 	size_t	tlen;
    556 	u_short	tidx, sidx;
    557 	int	h;
    558 
    559 	/* If we have this type already, return it's index. */
    560 	tlen = gettlen(cp, &ep);
    561 	h = thash(cp, tlen);
    562 	if ((tidx = findtype(cp, tlen, h)) != 0) {
    563 		*epp = ep;
    564 		return (tidx);
    565 	}
    566 
    567 	/* No, we must create a new type. */
    568 	tp = xalloc(sizeof (type_t));
    569 
    570 	tidx = storetyp(tp, cp, tlen, h);
    571 
    572 	c = *cp++;
    573 
    574 	while (c == 'c' || c == 'v') {
    575 		if (c == 'c') {
    576 			tp->t_const = 1;
    577 		} else {
    578 			tp->t_volatile = 1;
    579 		}
    580 		c = *cp++;
    581 	}
    582 
    583 	switch (c) {
    584 	case 's':
    585 	case 'u':
    586 	case 'l':
    587 	case 'e':
    588 		s = c;
    589 		c = *cp++;
    590 		break;
    591 	default:
    592 		s = '\0';
    593 		break;
    594 	}
    595 
    596 	switch (c) {
    597 	case 'B':
    598 		tp->t_tspec = BOOL;
    599 		break;
    600 	case 'C':
    601 		tp->t_tspec = s == 's' ? SCHAR : (s == 'u' ? UCHAR : CHAR);
    602 		break;
    603 	case 'S':
    604 		tp->t_tspec = s == 'u' ? USHORT : SHORT;
    605 		break;
    606 	case 'I':
    607 		tp->t_tspec = s == 'u' ? UINT : INT;
    608 		break;
    609 	case 'L':
    610 		tp->t_tspec = s == 'u' ? ULONG : LONG;
    611 		break;
    612 	case 'Q':
    613 		tp->t_tspec = s == 'u' ? UQUAD : QUAD;
    614 		break;
    615 	case 'D':
    616 		tp->t_tspec = s == 's' ? FLOAT : (s == 'l' ? LDOUBLE : DOUBLE);
    617 		break;
    618 	case 'V':
    619 		tp->t_tspec = VOID;
    620 		break;
    621 	case 'P':
    622 		tp->t_tspec = PTR;
    623 		break;
    624 	case 'A':
    625 		tp->t_tspec = ARRAY;
    626 		break;
    627 	case 'F':
    628 	case 'f':
    629 		osdef = c == 'f';
    630 		tp->t_tspec = FUNC;
    631 		break;
    632 	case 'T':
    633 		tp->t_tspec = s == 'e' ? ENUM : (s == 's' ? STRUCT : UNION);
    634 		break;
    635 	case 'X':
    636 		tp->t_tspec = s == 'u' ? DCOMPLEX : FCOMPLEX;
    637 		break;
    638 	}
    639 
    640 	switch (tp->t_tspec) {
    641 	case ARRAY:
    642 		tp->t_dim = (int)strtol(cp, &eptr, 10);
    643 		cp = eptr;
    644 		sidx = inptype(cp, &cp); /* force seq. point! (ditto below) */
    645 		tp->t_subt = TP(sidx);
    646 		break;
    647 	case PTR:
    648 		sidx = inptype(cp, &cp);
    649 		tp->t_subt = TP(sidx);
    650 		break;
    651 	case FUNC:
    652 		c = *cp;
    653 		if (isdigit((u_char)c)) {
    654 			if (!osdef)
    655 				tp->t_proto = 1;
    656 			narg = (int)strtol(cp, &eptr, 10);
    657 			cp = eptr;
    658 			tp->t_args = xcalloc((size_t)(narg + 1),
    659 					     sizeof (type_t *));
    660 			for (i = 0; i < narg; i++) {
    661 				if (i == narg - 1 && *cp == 'E') {
    662 					tp->t_vararg = 1;
    663 					cp++;
    664 				} else {
    665 					sidx = inptype(cp, &cp);
    666 					tp->t_args[i] = TP(sidx);
    667 				}
    668 			}
    669 		}
    670 		sidx = inptype(cp, &cp);
    671 		tp->t_subt = TP(sidx);
    672 		break;
    673 	case ENUM:
    674 		tp->t_tspec = INT;
    675 		tp->t_isenum = 1;
    676 		/* FALLTHROUGH */
    677 	case STRUCT:
    678 	case UNION:
    679 		switch (*cp++) {
    680 		case '1':
    681 			tp->t_istag = 1;
    682 			tp->t_tag = hsearch(inpname(cp, &cp), 1);
    683 			break;
    684 		case '2':
    685 			tp->t_istynam = 1;
    686 			tp->t_tynam = hsearch(inpname(cp, &cp), 1);
    687 			break;
    688 		case '3':
    689 			tp->t_isuniqpos = 1;
    690 			tp->t_uniqpos.p_line = strtol(cp, &eptr, 10);
    691 			cp = eptr;
    692 			cp++;
    693 			/* xlate to 'global' file name. */
    694 			tp->t_uniqpos.p_file =
    695 			    addoutfile(inpfns[strtol(cp, &eptr, 10)]);
    696 			cp = eptr;
    697 			cp++;
    698 			tp->t_uniqpos.p_uniq = strtol(cp, &eptr, 10);
    699 			cp = eptr;
    700 			break;
    701 		}
    702 		break;
    703 	case LONG:
    704 	case VOID:
    705 	case LDOUBLE:
    706 	case DOUBLE:
    707 	case FLOAT:
    708 	case UQUAD:
    709 	case QUAD:
    710 	case ULONG:
    711 	case UINT:
    712 	case INT:
    713 	case USHORT:
    714 	case SHORT:
    715 	case UCHAR:
    716 	case SCHAR:
    717 	case CHAR:
    718 	case BOOL:
    719 	case UNSIGN:
    720 	case SIGNED:
    721 	case NOTSPEC:
    722 	case FCOMPLEX:
    723 	case DCOMPLEX:
    724 	case COMPLEX:
    725 		break;
    726 	case NTSPEC:
    727 		abort();
    728 	}
    729 
    730 	*epp = cp;
    731 	return (tidx);
    732 }
    733 
    734 /*
    735  * Get the length of a type string.
    736  */
    737 static int
    738 gettlen(const char *cp, const char **epp)
    739 {
    740 	const	char *cp1;
    741 	char	c, s, *eptr;
    742 	tspec_t	t;
    743 	int	narg, i, cm, vm;
    744 
    745 	cp1 = cp;
    746 
    747 	c = *cp++;
    748 
    749 	cm = vm = 0;
    750 
    751 	while (c == 'c' || c == 'v') {
    752 		if (c == 'c') {
    753 			if (cm)
    754 				inperr("cm: %c", c);
    755 			cm = 1;
    756 		} else {
    757 			if (vm)
    758 				inperr("vm: %c", c);
    759 			vm = 1;
    760 		}
    761 		c = *cp++;
    762 	}
    763 
    764 	if (c == 's' || c == 'u' || c == 'l' || c == 'e') {
    765 		s = c;
    766 		c = *cp++;
    767 	} else {
    768 		s = '\0';
    769 	}
    770 
    771 	t = NOTSPEC;
    772 
    773 	switch (c) {
    774 	case 'B':
    775 		if (s == '\0')
    776 			t = BOOL;
    777 		break;
    778 	case 'C':
    779 		if (s == 's') {
    780 			t = SCHAR;
    781 		} else if (s == 'u') {
    782 			t = UCHAR;
    783 		} else if (s == '\0') {
    784 			t = CHAR;
    785 		}
    786 		break;
    787 	case 'S':
    788 		if (s == 'u') {
    789 			t = USHORT;
    790 		} else if (s == '\0') {
    791 			t = SHORT;
    792 		}
    793 		break;
    794 	case 'I':
    795 		if (s == 'u') {
    796 			t = UINT;
    797 		} else if (s == '\0') {
    798 			t = INT;
    799 		}
    800 		break;
    801 	case 'L':
    802 		if (s == 'u') {
    803 			t = ULONG;
    804 		} else if (s == '\0') {
    805 			t = LONG;
    806 		}
    807 		break;
    808 	case 'Q':
    809 		if (s == 'u') {
    810 			t = UQUAD;
    811 		} else if (s == '\0') {
    812 			t = QUAD;
    813 		}
    814 		break;
    815 	case 'D':
    816 		if (s == 's') {
    817 			t = FLOAT;
    818 		} else if (s == 'l') {
    819 			t = LDOUBLE;
    820 		} else if (s == '\0') {
    821 			t = DOUBLE;
    822 		}
    823 		break;
    824 	case 'V':
    825 		if (s == '\0')
    826 			t = VOID;
    827 		break;
    828 	case 'P':
    829 		if (s == '\0')
    830 			t = PTR;
    831 		break;
    832 	case 'A':
    833 		if (s == '\0')
    834 			t = ARRAY;
    835 		break;
    836 	case 'F':
    837 	case 'f':
    838 		if (s == '\0')
    839 			t = FUNC;
    840 		break;
    841 	case 'T':
    842 		if (s == 'e') {
    843 			t = ENUM;
    844 		} else if (s == 's') {
    845 			t = STRUCT;
    846 		} else if (s == 'u') {
    847 			t = UNION;
    848 		}
    849 		break;
    850 	case 'X':
    851 		if (s == 'u') {
    852 			t = DCOMPLEX;
    853 		} else if (s == 's') {
    854 			t = FCOMPLEX;
    855 		}
    856 		break;
    857 	default:
    858 		inperr("bad type: %c %c", c, s);
    859 	}
    860 
    861 	if (t == NOTSPEC) {
    862 		inperr("undefined type: %c %c", c, s);
    863 	}
    864 
    865 	switch (t) {
    866 	case ARRAY:
    867 		(void)strtol(cp, &eptr, 10);
    868 		if (cp == eptr)
    869 			inperr("bad number: %s", cp);
    870 		cp = eptr;
    871 		(void)gettlen(cp, &cp);
    872 		break;
    873 	case PTR:
    874 		(void)gettlen(cp, &cp);
    875 		break;
    876 	case FUNC:
    877 		c = *cp;
    878 		if (isdigit((u_char)c)) {
    879 			narg = (int)strtol(cp, &eptr, 10);
    880 			cp = eptr;
    881 			for (i = 0; i < narg; i++) {
    882 				if (i == narg - 1 && *cp == 'E') {
    883 					cp++;
    884 				} else {
    885 					(void)gettlen(cp, &cp);
    886 				}
    887 			}
    888 		}
    889 		(void)gettlen(cp, &cp);
    890 		break;
    891 	case ENUM:
    892 	case STRUCT:
    893 	case UNION:
    894 		switch (*cp++) {
    895 		case '1':
    896 			(void)inpname(cp, &cp);
    897 			break;
    898 		case '2':
    899 			(void)inpname(cp, &cp);
    900 			break;
    901 		case '3':
    902 			/* unique position: line.file.uniquifier */
    903 			(void)strtol(cp, &eptr, 10);
    904 			if (cp == eptr)
    905 				inperr("bad number: %s", cp);
    906 			cp = eptr;
    907 			if (*cp++ != '.')
    908 				inperr("not dot: %c", cp[-1]);
    909 			(void)strtol(cp, &eptr, 10);
    910 			if (cp == eptr)
    911 				inperr("bad number: %s", cp);
    912 			cp = eptr;
    913 			if (*cp++ != '.')
    914 				inperr("not dot: %c", cp[-1]);
    915 			(void)strtol(cp, &eptr, 10);
    916 			if (cp == eptr)
    917 				inperr("bad number: %s", cp);
    918 			cp = eptr;
    919 			break;
    920 		default:
    921 			inperr("bad value: %c\n", cp[-1]);
    922 		}
    923 		break;
    924 	case FLOAT:
    925 	case USHORT:
    926 	case SHORT:
    927 	case UCHAR:
    928 	case SCHAR:
    929 	case CHAR:
    930 	case BOOL:
    931 	case UNSIGN:
    932 	case SIGNED:
    933 	case NOTSPEC:
    934 	case INT:
    935 	case UINT:
    936 	case DOUBLE:
    937 	case LDOUBLE:
    938 	case VOID:
    939 	case ULONG:
    940 	case QUAD:
    941 	case UQUAD:
    942 	case LONG:
    943 	case FCOMPLEX:
    944 	case DCOMPLEX:
    945 	case COMPLEX:
    946 		break;
    947 #ifndef __COVERITY__
    948 	case NTSPEC:
    949 		abort();
    950 #endif
    951 	}
    952 
    953 	*epp = cp;
    954 	return (cp - cp1);
    955 }
    956 
    957 /*
    958  * Search a type by it's type string.
    959  */
    960 static u_short
    961 findtype(const char *cp, size_t len, int h)
    962 {
    963 	thtab_t	*thte;
    964 
    965 	for (thte = thtab[h]; thte != NULL; thte = thte->th_nxt) {
    966 		if (strncmp(thte->th_name, cp, len) != 0)
    967 			continue;
    968 		if (thte->th_name[len] == '\0')
    969 			return (thte->th_idx);
    970 	}
    971 
    972 	return (0);
    973 }
    974 
    975 /*
    976  * Store a type and it's type string so we can later share this type
    977  * if we read the same type string from the input file.
    978  */
    979 static u_short
    980 storetyp(type_t *tp, const char *cp, size_t len, int h)
    981 {
    982 	static	u_int	tidx = 1;	/* 0 is reserved */
    983 	thtab_t	*thte;
    984 	char	*name;
    985 
    986 	if (tidx >= USHRT_MAX)
    987 		errx(1, "sorry, too many types");
    988 
    989 	if (tidx == tlstlen - 1) {
    990 		tlst = xrealloc(tlst, (tlstlen * 2) * sizeof (type_t *));
    991 		(void)memset(tlst + tlstlen, 0, tlstlen * sizeof (type_t *));
    992 		tlstlen *= 2;
    993 	}
    994 
    995 	tlst[tidx] = tp;
    996 
    997 	/* create a hash table entry */
    998 	name = xalloc(len + 1);
    999 	(void)memcpy(name, cp, len);
   1000 	name[len] = '\0';
   1001 
   1002 	thte = xalloc(sizeof (thtab_t));
   1003 	thte->th_name = name;
   1004 	thte->th_idx = tidx;
   1005 	thte->th_nxt = thtab[h];
   1006 	thtab[h] = thte;
   1007 
   1008 	return ((u_short)tidx++);
   1009 }
   1010 
   1011 /*
   1012  * Hash function for types
   1013  */
   1014 static int
   1015 thash(const char *s, size_t len)
   1016 {
   1017 	u_int	v;
   1018 
   1019 	v = 0;
   1020 	while (len-- != 0) {
   1021 		v = (v << sizeof (v)) + (u_char)*s++;
   1022 		v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v));
   1023 	}
   1024 	return (v % THSHSIZ2);
   1025 }
   1026 
   1027 /*
   1028  * Read a string enclosed by "". This string may contain quoted chars.
   1029  */
   1030 static char *
   1031 inpqstrg(const char *src, const char **epp)
   1032 {
   1033 	char	*strg, *dst;
   1034 	size_t	slen;
   1035 	int	c;
   1036 	int	v;
   1037 
   1038 	dst = strg = xmalloc(slen = 32);
   1039 
   1040 	if ((c = *src++) != '"')
   1041 		inperr("not quote: %c", c);
   1042 	if ((c = *src++) == '\0')
   1043 		inperr("trailing data: %c", c);
   1044 
   1045 	while (c != '"') {
   1046 		if (c == '\\') {
   1047 			if ((c = *src++) == '\0')
   1048 				inperr("missing after \\");
   1049 			switch (c) {
   1050 			case 'n':
   1051 				c = '\n';
   1052 				break;
   1053 			case 't':
   1054 				c = '\t';
   1055 				break;
   1056 			case 'v':
   1057 				c = '\v';
   1058 				break;
   1059 			case 'b':
   1060 				c = '\b';
   1061 				break;
   1062 			case 'r':
   1063 				c = '\r';
   1064 				break;
   1065 			case 'f':
   1066 				c = '\f';
   1067 				break;
   1068 			case 'a':
   1069 				c = '\a';
   1070 				break;
   1071 			case '\\':
   1072 				c = '\\';
   1073 				break;
   1074 			case '"':
   1075 				c = '"';
   1076 				break;
   1077 			case '\'':
   1078 				c = '\'';
   1079 				break;
   1080 			case '0': case '1': case '2': case '3':
   1081 				v = (c - '0') << 6;
   1082 				if ((c = *src++) < '0' || c > '7')
   1083 					inperr("not octal: %c", c);
   1084 				v |= (c - '0') << 3;
   1085 				if ((c = *src++) < '0' || c > '7')
   1086 					inperr("not octal: %c", c);
   1087 				v |= c - '0';
   1088 				c = (u_char)v;
   1089 				break;
   1090 			default:
   1091 				inperr("bad \\ escape: %c", c);
   1092 			}
   1093 		}
   1094 		/* keep space for trailing '\0' */
   1095 		if (dst - strg == slen - 1) {
   1096 			strg = xrealloc(strg, slen * 2);
   1097 			dst = strg + (slen - 1);
   1098 			slen *= 2;
   1099 		}
   1100 		*dst++ = (char)c;
   1101 		if ((c = *src++) == '\0')
   1102 			inperr("missing closing quote");
   1103 	}
   1104 	*dst = '\0';
   1105 
   1106 	*epp = src;
   1107 	return (strg);
   1108 }
   1109 
   1110 /*
   1111  * Read the name of a symbol in static memory.
   1112  */
   1113 static const char *
   1114 inpname(const char *cp, const char **epp)
   1115 {
   1116 	static	char	*buf;
   1117 	static	size_t	blen = 0;
   1118 	size_t	len, i;
   1119 	char	*eptr, c;
   1120 
   1121 	len = (int)strtol(cp, &eptr, 10);
   1122 	if (cp == eptr)
   1123 		inperr("bad number: %s", cp);
   1124 	cp = eptr;
   1125 	if (len + 1 > blen)
   1126 		buf = xrealloc(buf, blen = len + 1);
   1127 	for (i = 0; i < len; i++) {
   1128 		c = *cp++;
   1129 		if (!isalnum((unsigned char)c) && c != '_')
   1130 			inperr("not alnum or _: %c", c);
   1131 		buf[i] = c;
   1132 	}
   1133 	buf[i] = '\0';
   1134 
   1135 	*epp = cp;
   1136 	return (buf);
   1137 }
   1138 
   1139 /*
   1140  * Return the index of a file name. If the name cannot be found, create
   1141  * a new entry and return the index of the newly created entry.
   1142  */
   1143 static int
   1144 getfnidx(const char *fn)
   1145 {
   1146 	size_t	i;
   1147 
   1148 	/* 0 is reserved */
   1149 	for (i = 1; fnames[i] != NULL; i++) {
   1150 		if (strcmp(fnames[i], fn) == 0)
   1151 			return i;
   1152 	}
   1153 
   1154 	if (i == nfnames - 1) {
   1155 		size_t nlen = nfnames * 2;
   1156 		fnames = xrealloc(fnames, nlen * sizeof(char *));
   1157 		(void)memset(fnames + nfnames, 0, nfnames * sizeof(char *));
   1158 		flines = xrealloc(flines, nlen * sizeof(size_t));
   1159 		(void)memset(flines + nfnames, 0, nfnames * sizeof(size_t));
   1160 		nfnames = nlen;
   1161 	}
   1162 
   1163 	fnames[i] = xstrdup(fn);
   1164 	flines[i] = 0;
   1165 	return (i);
   1166 }
   1167 
   1168 /*
   1169  * Separate symbols with static and external linkage.
   1170  */
   1171 void
   1172 mkstatic(hte_t *hte)
   1173 {
   1174 	sym_t	*sym1, **symp, *sym;
   1175 	fcall_t	**callp, *call;
   1176 	usym_t	**usymp, *usym;
   1177 	hte_t	*nhte;
   1178 	int	ofnd;
   1179 
   1180 	/* Look for first static definition */
   1181 	for (sym1 = hte->h_syms; sym1 != NULL; sym1 = sym1->s_nxt) {
   1182 		if (sym1->s_static)
   1183 			break;
   1184 	}
   1185 	if (sym1 == NULL)
   1186 		return;
   1187 
   1188 	/* Do nothing if this name is used only in one translation unit. */
   1189 	ofnd = 0;
   1190 	for (sym = hte->h_syms; sym != NULL && !ofnd; sym = sym->s_nxt) {
   1191 		if (sym->s_pos.p_src != sym1->s_pos.p_src)
   1192 			ofnd = 1;
   1193 	}
   1194 	for (call = hte->h_calls; call != NULL && !ofnd; call = call->f_nxt) {
   1195 		if (call->f_pos.p_src != sym1->s_pos.p_src)
   1196 			ofnd = 1;
   1197 	}
   1198 	for (usym = hte->h_usyms; usym != NULL && !ofnd; usym = usym->u_nxt) {
   1199 		if (usym->u_pos.p_src != sym1->s_pos.p_src)
   1200 			ofnd = 1;
   1201 	}
   1202 	if (!ofnd) {
   1203 		hte->h_used = 1;
   1204 		/* errors about undef. static symbols are printed in lint1 */
   1205 		hte->h_def = 1;
   1206 		hte->h_static = 1;
   1207 		return;
   1208 	}
   1209 
   1210 	/*
   1211 	 * Create a new hash table entry
   1212 	 *
   1213 	 * XXX this entry should be put at the beginning of the list to
   1214 	 * avoid to process the same symbol twice.
   1215 	 */
   1216 	for (nhte = hte; nhte->h_link != NULL; nhte = nhte->h_link)
   1217 		continue;
   1218 	nhte->h_link = xmalloc(sizeof (hte_t));
   1219 	nhte = nhte->h_link;
   1220 	nhte->h_name = hte->h_name;
   1221 	nhte->h_used = 1;
   1222 	nhte->h_def = 1;	/* error in lint1 */
   1223 	nhte->h_static = 1;
   1224 	nhte->h_syms = NULL;
   1225 	nhte->h_lsym = &nhte->h_syms;
   1226 	nhte->h_calls = NULL;
   1227 	nhte->h_lcall = &nhte->h_calls;
   1228 	nhte->h_usyms = NULL;
   1229 	nhte->h_lusym = &nhte->h_usyms;
   1230 	nhte->h_link = NULL;
   1231 	nhte->h_hte = NULL;
   1232 
   1233 	/*
   1234 	 * move all symbols used in this translation unit into the new
   1235 	 * hash table entry.
   1236 	 */
   1237 	for (symp = &hte->h_syms; (sym = *symp) != NULL; ) {
   1238 		if (sym->s_pos.p_src == sym1->s_pos.p_src) {
   1239 			sym->s_static = 1;
   1240 			(*symp) = sym->s_nxt;
   1241 			if (hte->h_lsym == &sym->s_nxt)
   1242 				hte->h_lsym = symp;
   1243 			sym->s_nxt = NULL;
   1244 			*nhte->h_lsym = sym;
   1245 			nhte->h_lsym = &sym->s_nxt;
   1246 		} else {
   1247 			symp = &sym->s_nxt;
   1248 		}
   1249 	}
   1250 	for (callp = &hte->h_calls; (call = *callp) != NULL; ) {
   1251 		if (call->f_pos.p_src == sym1->s_pos.p_src) {
   1252 			(*callp) = call->f_nxt;
   1253 			if (hte->h_lcall == &call->f_nxt)
   1254 				hte->h_lcall = callp;
   1255 			call->f_nxt = NULL;
   1256 			*nhte->h_lcall = call;
   1257 			nhte->h_lcall = &call->f_nxt;
   1258 		} else {
   1259 			callp = &call->f_nxt;
   1260 		}
   1261 	}
   1262 	for (usymp = &hte->h_usyms; (usym = *usymp) != NULL; ) {
   1263 		if (usym->u_pos.p_src == sym1->s_pos.p_src) {
   1264 			(*usymp) = usym->u_nxt;
   1265 			if (hte->h_lusym == &usym->u_nxt)
   1266 				hte->h_lusym = usymp;
   1267 			usym->u_nxt = NULL;
   1268 			*nhte->h_lusym = usym;
   1269 			nhte->h_lusym = &usym->u_nxt;
   1270 		} else {
   1271 			usymp = &usym->u_nxt;
   1272 		}
   1273 	}
   1274 
   1275 	/* h_def must be recalculated for old hte */
   1276 	hte->h_def = nhte->h_def = 0;
   1277 	for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
   1278 		if (sym->s_def == DEF || sym->s_def == TDEF) {
   1279 			hte->h_def = 1;
   1280 			break;
   1281 		}
   1282 	}
   1283 
   1284 	mkstatic(hte);
   1285 }
   1286