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