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