Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.bin / xlint / lint2 / read.c
blobb57b27f14f7c08944f29d459e1c482fbad9787f8
1 /* $NetBSD: read.c,v 1.24 2009/04/14 09:06:25 lukem Exp $ */
3 /*
4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
5 * Copyright (c) 1994, 1995 Jochen Pohl
6 * All Rights Reserved.
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.
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.
35 #if HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
37 #endif
39 #include <sys/cdefs.h>
40 #if defined(__RCSID) && !defined(lint)
41 __RCSID("$NetBSD: read.c,v 1.24 2009/04/14 09:06:25 lukem Exp $");
42 #endif
44 #include <ctype.h>
45 #include <limits.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <stdarg.h>
51 #include "lint2.h"
54 /* index of current (included) source file */
55 static int srcfile;
58 * The array pointed to by inpfns maps the file name indices of input files
59 * to the file name indices used in lint2
61 static short *inpfns;
62 static size_t ninpfns;
65 * The array pointed to by *fnames maps file name indizes to file names.
66 * Indices of type short are used instead of pointers to save memory.
68 const char **fnames;
69 static size_t *flines;
70 static size_t nfnames;
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.
79 typedef struct thtab {
80 const char *th_name;
81 u_short th_idx;
82 struct thtab *th_nxt;
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 */
88 static hte_t **renametab;
90 /* index of current C source file (as spezified at the command line) */
91 static int csrcfile;
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 *);
111 void
112 readfile(const char *name)
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;
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));
132 _inithash(&renametab);
134 srcfile = getfnidx(name);
136 if ((inp = fopen(name, "r")) == NULL)
137 err(1, "cannot open %s", name);
139 while ((line = fgetln(inp, &len)) != NULL) {
140 flines[srcfile]++;
142 if (len == 0 || line[len - 1] != '\n')
143 inperr("%s", &line[len - 1]);
144 line[len - 1] = '\0';
145 cp = line;
147 /* line number in csrcfile */
148 cline = (int)strtol(cp, &eptr, 10);
149 if (cp == eptr) {
150 cline = -1;
151 } else {
152 cp = eptr;
155 /* record type */
156 if (*cp != '\0') {
157 rt = *cp++;
158 } else {
159 inperr("null cp");
162 if (rt == 'S') {
163 setsrc(cp);
164 continue;
165 } else if (rt == 's') {
166 setfnid(cline, cp);
167 continue;
171 * Index of (included) source file. If this index is
172 * different from csrcfile, it refers to an included
173 * file.
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];
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;
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;
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);
211 _destroyhash(renametab);
213 if (ferror(inp))
214 err(1, "read error on %s", name);
216 (void)fclose(inp);
220 static void
221 inperror(const char *file, size_t line, const char *fmt, ...)
223 va_list ap;
224 char buf[1024];
226 va_start(ap, fmt);
227 (void)vsnprintf(buf, sizeof(buf), fmt, ap);
228 va_end(ap);
230 errx(1, "%s,%zu: input file error: %s,%zu (%s)", file, line,
231 fnames[srcfile], flines[srcfile], buf);
235 * Set the name of the C source file of the .ln file which is
236 * currently read.
238 static void
239 setsrc(const char *cp)
242 csrcfile = getfnidx(cp);
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.
251 static void
252 setfnid(int fid, const char *cp)
255 if (fid < 0)
256 inperr("bad fid");
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;
264 * Should always be true because indices written in the output
265 * file by lint1 are always the previous index + 1.
267 if ((size_t)fid >= ninpfns)
268 errx(1, "internal error: setfnid()");
269 inpfns[fid] = (u_short)getfnidx(cp);
273 * Process a function call record (c-record).
275 static void
276 funccall(pos_t *posp, const char *cp)
278 arginf_t *ai, **lai;
279 char c, *eptr;
280 int rused, rdisc;
281 hte_t *hte;
282 fcall_t *fcall;
283 const char *name;
285 fcall = xalloc(sizeof (fcall_t));
286 STRUCT_ASSIGN(fcall->f_pos, *posp);
288 /* read flags */
289 rused = rdisc = 0;
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 = 1;
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 = 1;
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 = 1;
320 } else if (c == 'p') {
321 ai->a_pcon = 1;
322 } else if (c == 'n') {
323 ai->a_ncon = 1;
324 } else {
325 ai->a_fmt = 1;
326 ai->a_fstrg = inpqstrg(cp, &cp);
328 *lai = ai;
329 lai = &ai->a_nxt;
330 break;
333 fcall->f_rused = rused;
334 fcall->f_rdisc = rdisc;
336 /* read name of function */
337 name = inpname(cp, &cp);
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 = 1;
347 fcall->f_type = inptype(cp, &cp);
349 *hte->h_lcall = fcall;
350 hte->h_lcall = &fcall->f_nxt;
352 if (*cp != '\0')
353 inperr("trailing line data: %s", cp);
357 * Process a declaration or definition (d-record).
359 static void
360 decldef(pos_t *posp, const char *cp)
362 sym_t *symp, sym;
363 char c, *ep, *pos1, *tname;
364 int used, renamed;
365 hte_t *hte, *renamehte = NULL;
366 const char *name, *newname;
368 (void)memset(&sym, 0, sizeof (sym));
369 STRUCT_ASSIGN(sym.s_pos, *posp);
370 sym.s_def = NODECL;
372 used = 0;
374 while (strchr("tdeurosvPS", (c = *cp)) != NULL) {
375 cp++;
376 switch (c) {
377 case 't':
378 if (sym.s_def != NODECL)
379 inperr("nodecl %c", c);
380 sym.s_def = TDEF;
381 break;
382 case 'd':
383 if (sym.s_def != NODECL)
384 inperr("nodecl %c", c);
385 sym.s_def = DEF;
386 break;
387 case 'e':
388 if (sym.s_def != NODECL)
389 inperr("nodecl %c", c);
390 sym.s_def = DECL;
391 break;
392 case 'u':
393 if (used)
394 inperr("used %c", c);
395 used = 1;
396 break;
397 case 'r':
398 if (sym.s_rval)
399 inperr("rval");
400 sym.s_rval = 1;
401 break;
402 case 'o':
403 if (sym.s_osdef)
404 inperr("osdef");
405 sym.s_osdef = 1;
406 break;
407 case 's':
408 if (sym.s_static)
409 inperr("static");
410 sym.s_static = 1;
411 break;
412 case 'v':
413 if (sym.s_va)
414 inperr("va");
415 sym.s_va = 1;
416 sym.s_nva = (short)strtol(cp, &ep, 10);
417 if (cp == ep)
418 inperr("bad number: %s", cp);
419 cp = ep;
420 break;
421 case 'P':
422 if (sym.s_prfl)
423 inperr("prfl");
424 sym.s_prfl = 1;
425 sym.s_nprfl = (short)strtol(cp, &ep, 10);
426 if (cp == ep)
427 inperr("bad number: %s", cp);
428 cp = ep;
429 break;
430 case 'S':
431 if (sym.s_scfl)
432 inperr("scfl");
433 sym.s_scfl = 1;
434 sym.s_nscfl = (short)strtol(cp, &ep, 10);
435 if (cp == ep)
436 inperr("bad number: %s", cp);
437 cp = ep;
438 break;
442 /* read symbol name, doing renaming if necessary */
443 name = inpname(cp, &cp);
444 renamed = 0;
445 if (*cp == 'r') {
446 cp++;
447 tname = xstrdup(name);
448 newname = inpname(cp, &cp);
450 /* enter it and see if it's already been renamed */
451 renamehte = _hsearch(renametab, tname, 1);
452 if (renamehte->h_hte == NULL) {
453 hte = hsearch(newname, 1);
454 renamehte->h_hte = hte;
455 renamed = 1;
456 } else if (strcmp((hte = renamehte->h_hte)->h_name, newname)) {
457 pos1 = xstrdup(mkpos(&renamehte->h_syms->s_pos));
458 /* %s renamed multiple times\t%s :: %s */
459 msg(18, tname, pos1, mkpos(&sym.s_pos));
460 free(pos1);
462 free(tname);
463 } else {
464 /* it might be a previously-done rename */
465 hte = _hsearch(renametab, name, 0);
466 if (hte != NULL)
467 hte = hte->h_hte;
468 else
469 hte = hsearch(name, 1);
471 hte->h_used |= used;
472 if (sym.s_def == DEF || sym.s_def == TDEF)
473 hte->h_def = 1;
475 sym.s_type = inptype(cp, &cp);
478 * Allocate memory for this symbol only if it was not already
479 * declared or tentatively defined at the same location with
480 * the same type. Works only for symbols with external linkage,
481 * because static symbols, tentatively defined at the same location
482 * but in different translation units are really different symbols.
484 for (symp = hte->h_syms; symp != NULL; symp = symp->s_nxt) {
485 if (symp->s_pos.p_isrc == sym.s_pos.p_isrc &&
486 symp->s_pos.p_iline == sym.s_pos.p_iline &&
487 symp->s_type == sym.s_type &&
488 ((symp->s_def == DECL && sym.s_def == DECL) ||
489 (!sflag && symp->s_def == TDEF && sym.s_def == TDEF)) &&
490 !symp->s_static && !sym.s_static) {
491 break;
495 if (symp == NULL) {
496 /* allocsym reserviert keinen Platz fuer s_nva */
497 if (sym.s_va || sym.s_prfl || sym.s_scfl) {
498 symp = xalloc(sizeof (sym_t));
499 STRUCT_ASSIGN(*symp, sym);
500 } else {
501 symp = xalloc(sizeof (symp->s_s));
502 STRUCT_ASSIGN(symp->s_s, sym.s_s);
504 *hte->h_lsym = symp;
505 hte->h_lsym = &symp->s_nxt;
507 /* XXX hack so we can remember where a symbol was renamed */
508 if (renamed)
509 renamehte->h_syms = symp;
512 if (*cp != '\0')
513 inperr("trailing line: %s", cp);
517 * Read an u-record (emited by lint1 if a symbol was used).
519 static void
520 usedsym(pos_t *posp, const char *cp)
522 usym_t *usym;
523 hte_t *hte;
524 const char *name;
526 usym = xalloc(sizeof (usym_t));
527 STRUCT_ASSIGN(usym->u_pos, *posp);
529 /* needed as delimiter between two numbers */
530 if (*cp++ != 'x')
531 inperr("bad delim %c", cp[-1]);
533 name = inpname(cp, &cp);
534 hte = _hsearch(renametab, name, 0);
535 if (hte != NULL)
536 hte = hte->h_hte;
537 else
538 hte = hsearch(name, 1);
539 hte->h_used = 1;
541 *hte->h_lusym = usym;
542 hte->h_lusym = &usym->u_nxt;
546 * Read a type and return the index of this type.
548 static u_short
549 inptype(const char *cp, const char **epp)
551 char c, s, *eptr;
552 const char *ep;
553 type_t *tp;
554 int narg, i, osdef = 0;
555 size_t tlen;
556 u_short tidx, sidx;
557 int h;
559 /* If we have this type already, return it's index. */
560 tlen = gettlen(cp, &ep);
561 h = thash(cp, tlen);
562 if ((tidx = findtype(cp, tlen, h)) != 0) {
563 *epp = ep;
564 return (tidx);
567 /* No, we must create a new type. */
568 tp = xalloc(sizeof (type_t));
570 tidx = storetyp(tp, cp, tlen, h);
572 c = *cp++;
574 while (c == 'c' || c == 'v') {
575 if (c == 'c') {
576 tp->t_const = 1;
577 } else {
578 tp->t_volatile = 1;
580 c = *cp++;
583 switch (c) {
584 case 's':
585 case 'u':
586 case 'l':
587 case 'e':
588 s = c;
589 c = *cp++;
590 break;
591 default:
592 s = '\0';
593 break;
596 switch (c) {
597 case 'B':
598 tp->t_tspec = BOOL;
599 break;
600 case 'C':
601 tp->t_tspec = s == 's' ? SCHAR : (s == 'u' ? UCHAR : CHAR);
602 break;
603 case 'S':
604 tp->t_tspec = s == 'u' ? USHORT : SHORT;
605 break;
606 case 'I':
607 tp->t_tspec = s == 'u' ? UINT : INT;
608 break;
609 case 'L':
610 tp->t_tspec = s == 'u' ? ULONG : LONG;
611 break;
612 case 'Q':
613 tp->t_tspec = s == 'u' ? UQUAD : QUAD;
614 break;
615 case 'D':
616 tp->t_tspec = s == 's' ? FLOAT : (s == 'l' ? LDOUBLE : DOUBLE);
617 break;
618 case 'V':
619 tp->t_tspec = VOID;
620 break;
621 case 'P':
622 tp->t_tspec = PTR;
623 break;
624 case 'A':
625 tp->t_tspec = ARRAY;
626 break;
627 case 'F':
628 case 'f':
629 osdef = c == 'f';
630 tp->t_tspec = FUNC;
631 break;
632 case 'T':
633 tp->t_tspec = s == 'e' ? ENUM : (s == 's' ? STRUCT : UNION);
634 break;
635 case 'X':
636 tp->t_tspec = s == 's' ? FCOMPLEX
637 : (s == 'l' ? LCOMPLEX : DCOMPLEX);
638 break;
641 switch (tp->t_tspec) {
642 case ARRAY:
643 tp->t_dim = (int)strtol(cp, &eptr, 10);
644 cp = eptr;
645 sidx = inptype(cp, &cp); /* force seq. point! (ditto below) */
646 tp->t_subt = TP(sidx);
647 break;
648 case PTR:
649 sidx = inptype(cp, &cp);
650 tp->t_subt = TP(sidx);
651 break;
652 case FUNC:
653 c = *cp;
654 if (isdigit((u_char)c)) {
655 if (!osdef)
656 tp->t_proto = 1;
657 narg = (int)strtol(cp, &eptr, 10);
658 cp = eptr;
659 tp->t_args = xcalloc((size_t)(narg + 1),
660 sizeof (type_t *));
661 for (i = 0; i < narg; i++) {
662 if (i == narg - 1 && *cp == 'E') {
663 tp->t_vararg = 1;
664 cp++;
665 } else {
666 sidx = inptype(cp, &cp);
667 tp->t_args[i] = TP(sidx);
671 sidx = inptype(cp, &cp);
672 tp->t_subt = TP(sidx);
673 break;
674 case ENUM:
675 tp->t_tspec = INT;
676 tp->t_isenum = 1;
677 /* FALLTHROUGH */
678 case STRUCT:
679 case UNION:
680 switch (*cp++) {
681 case '1':
682 tp->t_istag = 1;
683 tp->t_tag = hsearch(inpname(cp, &cp), 1);
684 break;
685 case '2':
686 tp->t_istynam = 1;
687 tp->t_tynam = hsearch(inpname(cp, &cp), 1);
688 break;
689 case '3':
690 tp->t_isuniqpos = 1;
691 tp->t_uniqpos.p_line = strtol(cp, &eptr, 10);
692 cp = eptr;
693 cp++;
694 /* xlate to 'global' file name. */
695 tp->t_uniqpos.p_file =
696 addoutfile(inpfns[strtol(cp, &eptr, 10)]);
697 cp = eptr;
698 cp++;
699 tp->t_uniqpos.p_uniq = strtol(cp, &eptr, 10);
700 cp = eptr;
701 break;
703 break;
704 case LONG:
705 case VOID:
706 case LDOUBLE:
707 case DOUBLE:
708 case FLOAT:
709 case UQUAD:
710 case QUAD:
711 case ULONG:
712 case UINT:
713 case INT:
714 case USHORT:
715 case SHORT:
716 case UCHAR:
717 case SCHAR:
718 case CHAR:
719 case BOOL:
720 case UNSIGN:
721 case SIGNED:
722 case NOTSPEC:
723 case FCOMPLEX:
724 case DCOMPLEX:
725 case LCOMPLEX:
726 case COMPLEX:
727 break;
728 case NTSPEC:
729 abort();
732 *epp = cp;
733 return (tidx);
737 * Get the length of a type string.
739 static int
740 gettlen(const char *cp, const char **epp)
742 const char *cp1;
743 char c, s, *eptr;
744 tspec_t t;
745 int narg, i, cm, vm;
747 cp1 = cp;
749 c = *cp++;
751 cm = vm = 0;
753 while (c == 'c' || c == 'v') {
754 if (c == 'c') {
755 if (cm)
756 inperr("cm: %c", c);
757 cm = 1;
758 } else {
759 if (vm)
760 inperr("vm: %c", c);
761 vm = 1;
763 c = *cp++;
766 switch (c) {
767 case 's':
768 case 'u':
769 case 'l':
770 case 'e':
771 s = c;
772 c = *cp++;
773 break;
774 default:
775 s = '\0';
776 break;
779 t = NOTSPEC;
781 switch (c) {
782 case 'B':
783 if (s == '\0')
784 t = BOOL;
785 break;
786 case 'C':
787 if (s == 's') {
788 t = SCHAR;
789 } else if (s == 'u') {
790 t = UCHAR;
791 } else if (s == '\0') {
792 t = CHAR;
794 break;
795 case 'S':
796 if (s == 'u') {
797 t = USHORT;
798 } else if (s == '\0') {
799 t = SHORT;
801 break;
802 case 'I':
803 if (s == 'u') {
804 t = UINT;
805 } else if (s == '\0') {
806 t = INT;
808 break;
809 case 'L':
810 if (s == 'u') {
811 t = ULONG;
812 } else if (s == '\0') {
813 t = LONG;
815 break;
816 case 'Q':
817 if (s == 'u') {
818 t = UQUAD;
819 } else if (s == '\0') {
820 t = QUAD;
822 break;
823 case 'D':
824 if (s == 's') {
825 t = FLOAT;
826 } else if (s == 'l') {
827 t = LDOUBLE;
828 } else if (s == '\0') {
829 t = DOUBLE;
831 break;
832 case 'V':
833 if (s == '\0')
834 t = VOID;
835 break;
836 case 'P':
837 if (s == '\0')
838 t = PTR;
839 break;
840 case 'A':
841 if (s == '\0')
842 t = ARRAY;
843 break;
844 case 'F':
845 case 'f':
846 if (s == '\0')
847 t = FUNC;
848 break;
849 case 'T':
850 if (s == 'e') {
851 t = ENUM;
852 } else if (s == 's') {
853 t = STRUCT;
854 } else if (s == 'u') {
855 t = UNION;
857 break;
858 case 'X':
859 if (s == 's') {
860 t = FCOMPLEX;
861 } else if (s == 'l') {
862 t = LCOMPLEX;
863 } else if (s == '\0') {
864 t = DCOMPLEX;
866 break;
867 default:
868 inperr("bad type: %c %c", c, s);
871 if (t == NOTSPEC) {
872 inperr("undefined type: %c %c", c, s);
875 switch (t) {
876 case ARRAY:
877 (void)strtol(cp, &eptr, 10);
878 if (cp == eptr)
879 inperr("bad number: %s", cp);
880 cp = eptr;
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 (isdigit((u_char)c)) {
889 narg = (int)strtol(cp, &eptr, 10);
890 cp = eptr;
891 for (i = 0; i < narg; i++) {
892 if (i == narg - 1 && *cp == 'E') {
893 cp++;
894 } else {
895 (void)gettlen(cp, &cp);
899 (void)gettlen(cp, &cp);
900 break;
901 case ENUM:
902 case STRUCT:
903 case UNION:
904 switch (*cp++) {
905 case '1':
906 (void)inpname(cp, &cp);
907 break;
908 case '2':
909 (void)inpname(cp, &cp);
910 break;
911 case '3':
912 /* unique position: line.file.uniquifier */
913 (void)strtol(cp, &eptr, 10);
914 if (cp == eptr)
915 inperr("bad number: %s", cp);
916 cp = eptr;
917 if (*cp++ != '.')
918 inperr("not dot: %c", cp[-1]);
919 (void)strtol(cp, &eptr, 10);
920 if (cp == eptr)
921 inperr("bad number: %s", cp);
922 cp = eptr;
923 if (*cp++ != '.')
924 inperr("not dot: %c", cp[-1]);
925 (void)strtol(cp, &eptr, 10);
926 if (cp == eptr)
927 inperr("bad number: %s", cp);
928 cp = eptr;
929 break;
930 default:
931 inperr("bad value: %c\n", cp[-1]);
933 break;
934 case FLOAT:
935 case USHORT:
936 case SHORT:
937 case UCHAR:
938 case SCHAR:
939 case CHAR:
940 case BOOL:
941 case UNSIGN:
942 case SIGNED:
943 case NOTSPEC:
944 case INT:
945 case UINT:
946 case DOUBLE:
947 case LDOUBLE:
948 case VOID:
949 case ULONG:
950 case QUAD:
951 case UQUAD:
952 case LONG:
953 case FCOMPLEX:
954 case DCOMPLEX:
955 case LCOMPLEX:
956 case COMPLEX:
957 break;
958 #ifndef __COVERITY__
959 case NTSPEC:
960 abort();
961 #endif
964 *epp = cp;
965 return (cp - cp1);
969 * Search a type by it's type string.
971 static u_short
972 findtype(const char *cp, size_t len, int h)
974 thtab_t *thte;
976 for (thte = thtab[h]; thte != NULL; thte = thte->th_nxt) {
977 if (strncmp(thte->th_name, cp, len) != 0)
978 continue;
979 if (thte->th_name[len] == '\0')
980 return (thte->th_idx);
983 return (0);
987 * Store a type and it's type string so we can later share this type
988 * if we read the same type string from the input file.
990 static u_short
991 storetyp(type_t *tp, const char *cp, size_t len, int h)
993 static u_int tidx = 1; /* 0 is reserved */
994 thtab_t *thte;
995 char *name;
997 if (tidx >= USHRT_MAX)
998 errx(1, "sorry, too many types");
1000 if (tidx == tlstlen - 1) {
1001 tlst = xrealloc(tlst, (tlstlen * 2) * sizeof (type_t *));
1002 (void)memset(tlst + tlstlen, 0, tlstlen * sizeof (type_t *));
1003 tlstlen *= 2;
1006 tlst[tidx] = tp;
1008 /* create a hash table entry */
1009 name = xalloc(len + 1);
1010 (void)memcpy(name, cp, len);
1011 name[len] = '\0';
1013 thte = xalloc(sizeof (thtab_t));
1014 thte->th_name = name;
1015 thte->th_idx = tidx;
1016 thte->th_nxt = thtab[h];
1017 thtab[h] = thte;
1019 return ((u_short)tidx++);
1023 * Hash function for types
1025 static int
1026 thash(const char *s, size_t len)
1028 u_int v;
1030 v = 0;
1031 while (len-- != 0) {
1032 v = (v << sizeof (v)) + (u_char)*s++;
1033 v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v));
1035 return (v % THSHSIZ2);
1039 * Read a string enclosed by "". This string may contain quoted chars.
1041 static char *
1042 inpqstrg(const char *src, const char **epp)
1044 char *strg, *dst;
1045 size_t slen;
1046 int c;
1047 int v;
1049 dst = strg = xmalloc(slen = 32);
1051 if ((c = *src++) != '"')
1052 inperr("not quote: %c", c);
1053 if ((c = *src++) == '\0')
1054 inperr("trailing data: %c", c);
1056 while (c != '"') {
1057 if (c == '\\') {
1058 if ((c = *src++) == '\0')
1059 inperr("missing after \\");
1060 switch (c) {
1061 case 'n':
1062 c = '\n';
1063 break;
1064 case 't':
1065 c = '\t';
1066 break;
1067 case 'v':
1068 c = '\v';
1069 break;
1070 case 'b':
1071 c = '\b';
1072 break;
1073 case 'r':
1074 c = '\r';
1075 break;
1076 case 'f':
1077 c = '\f';
1078 break;
1079 case 'a':
1080 c = '\a';
1081 break;
1082 case '\\':
1083 c = '\\';
1084 break;
1085 case '"':
1086 c = '"';
1087 break;
1088 case '\'':
1089 c = '\'';
1090 break;
1091 case '0': case '1': case '2': case '3':
1092 v = (c - '0') << 6;
1093 if ((c = *src++) < '0' || c > '7')
1094 inperr("not octal: %c", c);
1095 v |= (c - '0') << 3;
1096 if ((c = *src++) < '0' || c > '7')
1097 inperr("not octal: %c", c);
1098 v |= c - '0';
1099 c = (u_char)v;
1100 break;
1101 default:
1102 inperr("bad \\ escape: %c", c);
1105 /* keep space for trailing '\0' */
1106 if ((size_t)(dst - strg) == slen - 1) {
1107 strg = xrealloc(strg, slen * 2);
1108 dst = strg + (slen - 1);
1109 slen *= 2;
1111 *dst++ = (char)c;
1112 if ((c = *src++) == '\0')
1113 inperr("missing closing quote");
1115 *dst = '\0';
1117 *epp = src;
1118 return (strg);
1122 * Read the name of a symbol in static memory.
1124 static const char *
1125 inpname(const char *cp, const char **epp)
1127 static char *buf;
1128 static size_t blen = 0;
1129 size_t len, i;
1130 char *eptr, c;
1132 len = (int)strtol(cp, &eptr, 10);
1133 if (cp == eptr)
1134 inperr("bad number: %s", cp);
1135 cp = eptr;
1136 if (len + 1 > blen)
1137 buf = xrealloc(buf, blen = len + 1);
1138 for (i = 0; i < len; i++) {
1139 c = *cp++;
1140 if (!isalnum((unsigned char)c) && c != '_')
1141 inperr("not alnum or _: %c", c);
1142 buf[i] = c;
1144 buf[i] = '\0';
1146 *epp = cp;
1147 return (buf);
1151 * Return the index of a file name. If the name cannot be found, create
1152 * a new entry and return the index of the newly created entry.
1154 static int
1155 getfnidx(const char *fn)
1157 size_t i;
1159 /* 0 is reserved */
1160 for (i = 1; fnames[i] != NULL; i++) {
1161 if (strcmp(fnames[i], fn) == 0)
1162 return i;
1165 if (i == nfnames - 1) {
1166 size_t nlen = nfnames * 2;
1167 fnames = xrealloc(fnames, nlen * sizeof(char *));
1168 (void)memset(fnames + nfnames, 0, nfnames * sizeof(char *));
1169 flines = xrealloc(flines, nlen * sizeof(size_t));
1170 (void)memset(flines + nfnames, 0, nfnames * sizeof(size_t));
1171 nfnames = nlen;
1174 fnames[i] = xstrdup(fn);
1175 flines[i] = 0;
1176 return (i);
1180 * Separate symbols with static and external linkage.
1182 void
1183 mkstatic(hte_t *hte)
1185 sym_t *sym1, **symp, *sym;
1186 fcall_t **callp, *call;
1187 usym_t **usymp, *usym;
1188 hte_t *nhte;
1189 int ofnd;
1191 /* Look for first static definition */
1192 for (sym1 = hte->h_syms; sym1 != NULL; sym1 = sym1->s_nxt) {
1193 if (sym1->s_static)
1194 break;
1196 if (sym1 == NULL)
1197 return;
1199 /* Do nothing if this name is used only in one translation unit. */
1200 ofnd = 0;
1201 for (sym = hte->h_syms; sym != NULL && !ofnd; sym = sym->s_nxt) {
1202 if (sym->s_pos.p_src != sym1->s_pos.p_src)
1203 ofnd = 1;
1205 for (call = hte->h_calls; call != NULL && !ofnd; call = call->f_nxt) {
1206 if (call->f_pos.p_src != sym1->s_pos.p_src)
1207 ofnd = 1;
1209 for (usym = hte->h_usyms; usym != NULL && !ofnd; usym = usym->u_nxt) {
1210 if (usym->u_pos.p_src != sym1->s_pos.p_src)
1211 ofnd = 1;
1213 if (!ofnd) {
1214 hte->h_used = 1;
1215 /* errors about undef. static symbols are printed in lint1 */
1216 hte->h_def = 1;
1217 hte->h_static = 1;
1218 return;
1222 * Create a new hash table entry
1224 * XXX this entry should be put at the beginning of the list to
1225 * avoid to process the same symbol twice.
1227 for (nhte = hte; nhte->h_link != NULL; nhte = nhte->h_link)
1228 continue;
1229 nhte->h_link = xmalloc(sizeof (hte_t));
1230 nhte = nhte->h_link;
1231 nhte->h_name = hte->h_name;
1232 nhte->h_used = 1;
1233 nhte->h_def = 1; /* error in lint1 */
1234 nhte->h_static = 1;
1235 nhte->h_syms = NULL;
1236 nhte->h_lsym = &nhte->h_syms;
1237 nhte->h_calls = NULL;
1238 nhte->h_lcall = &nhte->h_calls;
1239 nhte->h_usyms = NULL;
1240 nhte->h_lusym = &nhte->h_usyms;
1241 nhte->h_link = NULL;
1242 nhte->h_hte = NULL;
1245 * move all symbols used in this translation unit into the new
1246 * hash table entry.
1248 for (symp = &hte->h_syms; (sym = *symp) != NULL; ) {
1249 if (sym->s_pos.p_src == sym1->s_pos.p_src) {
1250 sym->s_static = 1;
1251 (*symp) = sym->s_nxt;
1252 if (hte->h_lsym == &sym->s_nxt)
1253 hte->h_lsym = symp;
1254 sym->s_nxt = NULL;
1255 *nhte->h_lsym = sym;
1256 nhte->h_lsym = &sym->s_nxt;
1257 } else {
1258 symp = &sym->s_nxt;
1261 for (callp = &hte->h_calls; (call = *callp) != NULL; ) {
1262 if (call->f_pos.p_src == sym1->s_pos.p_src) {
1263 (*callp) = call->f_nxt;
1264 if (hte->h_lcall == &call->f_nxt)
1265 hte->h_lcall = callp;
1266 call->f_nxt = NULL;
1267 *nhte->h_lcall = call;
1268 nhte->h_lcall = &call->f_nxt;
1269 } else {
1270 callp = &call->f_nxt;
1273 for (usymp = &hte->h_usyms; (usym = *usymp) != NULL; ) {
1274 if (usym->u_pos.p_src == sym1->s_pos.p_src) {
1275 (*usymp) = usym->u_nxt;
1276 if (hte->h_lusym == &usym->u_nxt)
1277 hte->h_lusym = usymp;
1278 usym->u_nxt = NULL;
1279 *nhte->h_lusym = usym;
1280 nhte->h_lusym = &usym->u_nxt;
1281 } else {
1282 usymp = &usym->u_nxt;
1286 /* h_def must be recalculated for old hte */
1287 hte->h_def = nhte->h_def = 0;
1288 for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
1289 if (sym->s_def == DEF || sym->s_def == TDEF) {
1290 hte->h_def = 1;
1291 break;
1295 mkstatic(hte);