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