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