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