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