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