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