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