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