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