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