read.c revision 1.19.6.1 1 /* $NetBSD: read.c,v 1.19.6.1 2008/05/18 12:36:13 yamt 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.19.6.1 2008/05/18 12:36:13 yamt Exp $");
42 #endif
43
44 #include <ctype.h>
45 #include <limits.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <stdarg.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 indizes 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_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 */
87
88 static hte_t **renametab;
89
90 /* index of current C source file (as spezified 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))
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 == -1)
256 inperr("bad fid");
257
258 if (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 (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 int rused, rdisc;
281 hte_t *hte;
282 fcall_t *fcall;
283 const char *name;
284
285 fcall = xalloc(sizeof (fcall_t));
286 STRUCT_ASSIGN(fcall->f_pos, *posp);
287
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);
327 }
328 *lai = ai;
329 lai = &ai->a_nxt;
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 = 1;
346
347 fcall->f_type = inptype(cp, &cp);
348
349 *hte->h_lcall = fcall;
350 hte->h_lcall = &fcall->f_nxt;
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;
364 int used, renamed;
365 hte_t *hte, *renamehte = NULL;
366 const char *name, *rename;
367
368 (void)memset(&sym, 0, sizeof (sym));
369 STRUCT_ASSIGN(sym.s_pos, *posp);
370 sym.s_def = NODECL;
371
372 used = 0;
373
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;
439 }
440 }
441
442 /* read symbol name, doing renaming if necessary */
443 name = inpname(cp, &cp);
444 renamed = 0;
445 if (*cp == 'r') {
446 cp++;
447 name = xstrdup(name);
448 rename = inpname(cp, &cp);
449
450 /* enter it and see if it's already been renamed */
451 renamehte = _hsearch(renametab, name, 1);
452 if (renamehte->h_hte == NULL) {
453 hte = hsearch(rename, 1);
454 renamehte->h_hte = hte;
455 renamed = 1;
456 } else if (strcmp((hte = renamehte->h_hte)->h_name, rename)) {
457 pos1 = xstrdup(mkpos(&renamehte->h_syms->s_pos));
458 /* %s renamed multiple times\t%s :: %s */
459 msg(18, name, pos1, mkpos(&sym.s_pos));
460 free(pos1);
461 }
462 free((char *)name);
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);
470 }
471 hte->h_used |= used;
472 if (sym.s_def == DEF || sym.s_def == TDEF)
473 hte->h_def = 1;
474
475 sym.s_type = inptype(cp, &cp);
476
477 /*
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.
483 */
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;
492 }
493 }
494
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);
503 }
504 *hte->h_lsym = symp;
505 hte->h_lsym = &symp->s_nxt;
506
507 /* XXX hack so we can remember where a symbol was renamed */
508 if (renamed)
509 renamehte->h_syms = symp;
510 }
511
512 if (*cp != '\0')
513 inperr("trailing line: %s", cp);
514 }
515
516 /*
517 * Read an u-record (emited by lint1 if a symbol was used).
518 */
519 static void
520 usedsym(pos_t *posp, const char *cp)
521 {
522 usym_t *usym;
523 hte_t *hte;
524 const char *name;
525
526 usym = xalloc(sizeof (usym_t));
527 STRUCT_ASSIGN(usym->u_pos, *posp);
528
529 /* needed as delimiter between two numbers */
530 if (*cp++ != 'x')
531 inperr("bad delim %c", cp[-1]);
532
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;
540
541 *hte->h_lusym = usym;
542 hte->h_lusym = &usym->u_nxt;
543 }
544
545 /*
546 * Read a type and return the index of this type.
547 */
548 static u_short
549 inptype(const char *cp, const char **epp)
550 {
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;
558
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);
565 }
566
567 /* No, we must create a new type. */
568 tp = xalloc(sizeof (type_t));
569
570 tidx = storetyp(tp, cp, tlen, h);
571
572 c = *cp++;
573
574 while (c == 'c' || c == 'v') {
575 if (c == 'c') {
576 tp->t_const = 1;
577 } else {
578 tp->t_volatile = 1;
579 }
580 c = *cp++;
581 }
582
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;
594 }
595
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 : DCOMPLEX;
637 break;
638 }
639
640 switch (tp->t_tspec) {
641 case ARRAY:
642 tp->t_dim = (int)strtol(cp, &eptr, 10);
643 cp = eptr;
644 sidx = inptype(cp, &cp); /* force seq. point! (ditto below) */
645 tp->t_subt = TP(sidx);
646 break;
647 case PTR:
648 sidx = inptype(cp, &cp);
649 tp->t_subt = TP(sidx);
650 break;
651 case FUNC:
652 c = *cp;
653 if (isdigit((u_char)c)) {
654 if (!osdef)
655 tp->t_proto = 1;
656 narg = (int)strtol(cp, &eptr, 10);
657 cp = eptr;
658 tp->t_args = xcalloc((size_t)(narg + 1),
659 sizeof (type_t *));
660 for (i = 0; i < narg; i++) {
661 if (i == narg - 1 && *cp == 'E') {
662 tp->t_vararg = 1;
663 cp++;
664 } else {
665 sidx = inptype(cp, &cp);
666 tp->t_args[i] = TP(sidx);
667 }
668 }
669 }
670 sidx = inptype(cp, &cp);
671 tp->t_subt = TP(sidx);
672 break;
673 case ENUM:
674 tp->t_tspec = INT;
675 tp->t_isenum = 1;
676 /* FALLTHROUGH */
677 case STRUCT:
678 case UNION:
679 switch (*cp++) {
680 case '1':
681 tp->t_istag = 1;
682 tp->t_tag = hsearch(inpname(cp, &cp), 1);
683 break;
684 case '2':
685 tp->t_istynam = 1;
686 tp->t_tynam = hsearch(inpname(cp, &cp), 1);
687 break;
688 case '3':
689 tp->t_isuniqpos = 1;
690 tp->t_uniqpos.p_line = strtol(cp, &eptr, 10);
691 cp = eptr;
692 cp++;
693 /* xlate to 'global' file name. */
694 tp->t_uniqpos.p_file =
695 addoutfile(inpfns[strtol(cp, &eptr, 10)]);
696 cp = eptr;
697 cp++;
698 tp->t_uniqpos.p_uniq = strtol(cp, &eptr, 10);
699 cp = eptr;
700 break;
701 }
702 break;
703 case LONG:
704 case VOID:
705 case LDOUBLE:
706 case DOUBLE:
707 case FLOAT:
708 case UQUAD:
709 case QUAD:
710 case ULONG:
711 case UINT:
712 case INT:
713 case USHORT:
714 case SHORT:
715 case UCHAR:
716 case SCHAR:
717 case CHAR:
718 case BOOL:
719 case UNSIGN:
720 case SIGNED:
721 case NOTSPEC:
722 case FCOMPLEX:
723 case DCOMPLEX:
724 case COMPLEX:
725 break;
726 case NTSPEC:
727 abort();
728 }
729
730 *epp = cp;
731 return (tidx);
732 }
733
734 /*
735 * Get the length of a type string.
736 */
737 static int
738 gettlen(const char *cp, const char **epp)
739 {
740 const char *cp1;
741 char c, s, *eptr;
742 tspec_t t;
743 int narg, i, cm, vm;
744
745 cp1 = cp;
746
747 c = *cp++;
748
749 cm = vm = 0;
750
751 while (c == 'c' || c == 'v') {
752 if (c == 'c') {
753 if (cm)
754 inperr("cm: %c", c);
755 cm = 1;
756 } else {
757 if (vm)
758 inperr("vm: %c", c);
759 vm = 1;
760 }
761 c = *cp++;
762 }
763
764 switch (c) {
765 case 's':
766 case 'u':
767 case 'l':
768 case 'e':
769 s = c;
770 c = *cp++;
771 break;
772 default:
773 s = '\0';
774 break;
775 }
776
777 t = NOTSPEC;
778
779 switch (c) {
780 case 'B':
781 if (s == '\0')
782 t = BOOL;
783 break;
784 case 'C':
785 if (s == 's') {
786 t = SCHAR;
787 } else if (s == 'u') {
788 t = UCHAR;
789 } else if (s == '\0') {
790 t = CHAR;
791 }
792 break;
793 case 'S':
794 if (s == 'u') {
795 t = USHORT;
796 } else if (s == '\0') {
797 t = SHORT;
798 }
799 break;
800 case 'I':
801 if (s == 'u') {
802 t = UINT;
803 } else if (s == '\0') {
804 t = INT;
805 }
806 break;
807 case 'L':
808 if (s == 'u') {
809 t = ULONG;
810 } else if (s == '\0') {
811 t = LONG;
812 }
813 break;
814 case 'Q':
815 if (s == 'u') {
816 t = UQUAD;
817 } else if (s == '\0') {
818 t = QUAD;
819 }
820 break;
821 case 'D':
822 if (s == 's') {
823 t = FLOAT;
824 } else if (s == 'l') {
825 t = LDOUBLE;
826 } else if (s == '\0') {
827 t = DOUBLE;
828 }
829 break;
830 case 'V':
831 if (s == '\0')
832 t = VOID;
833 break;
834 case 'P':
835 if (s == '\0')
836 t = PTR;
837 break;
838 case 'A':
839 if (s == '\0')
840 t = ARRAY;
841 break;
842 case 'F':
843 case 'f':
844 if (s == '\0')
845 t = FUNC;
846 break;
847 case 'T':
848 if (s == 'e') {
849 t = ENUM;
850 } else if (s == 's') {
851 t = STRUCT;
852 } else if (s == 'u') {
853 t = UNION;
854 }
855 break;
856 case 'X':
857 if (s == 's') {
858 t = FCOMPLEX;
859 } else if (s == '\0') {
860 t = DCOMPLEX;
861 }
862 break;
863 default:
864 inperr("bad type: %c %c", c, s);
865 }
866
867 if (t == NOTSPEC) {
868 inperr("undefined type: %c %c", c, s);
869 }
870
871 switch (t) {
872 case ARRAY:
873 (void)strtol(cp, &eptr, 10);
874 if (cp == eptr)
875 inperr("bad number: %s", cp);
876 cp = eptr;
877 (void)gettlen(cp, &cp);
878 break;
879 case PTR:
880 (void)gettlen(cp, &cp);
881 break;
882 case FUNC:
883 c = *cp;
884 if (isdigit((u_char)c)) {
885 narg = (int)strtol(cp, &eptr, 10);
886 cp = eptr;
887 for (i = 0; i < narg; i++) {
888 if (i == narg - 1 && *cp == 'E') {
889 cp++;
890 } else {
891 (void)gettlen(cp, &cp);
892 }
893 }
894 }
895 (void)gettlen(cp, &cp);
896 break;
897 case ENUM:
898 case STRUCT:
899 case UNION:
900 switch (*cp++) {
901 case '1':
902 (void)inpname(cp, &cp);
903 break;
904 case '2':
905 (void)inpname(cp, &cp);
906 break;
907 case '3':
908 /* unique position: line.file.uniquifier */
909 (void)strtol(cp, &eptr, 10);
910 if (cp == eptr)
911 inperr("bad number: %s", cp);
912 cp = eptr;
913 if (*cp++ != '.')
914 inperr("not dot: %c", cp[-1]);
915 (void)strtol(cp, &eptr, 10);
916 if (cp == eptr)
917 inperr("bad number: %s", cp);
918 cp = eptr;
919 if (*cp++ != '.')
920 inperr("not dot: %c", cp[-1]);
921 (void)strtol(cp, &eptr, 10);
922 if (cp == eptr)
923 inperr("bad number: %s", cp);
924 cp = eptr;
925 break;
926 default:
927 inperr("bad value: %c\n", cp[-1]);
928 }
929 break;
930 case FLOAT:
931 case USHORT:
932 case SHORT:
933 case UCHAR:
934 case SCHAR:
935 case CHAR:
936 case BOOL:
937 case UNSIGN:
938 case SIGNED:
939 case NOTSPEC:
940 case INT:
941 case UINT:
942 case DOUBLE:
943 case LDOUBLE:
944 case VOID:
945 case ULONG:
946 case QUAD:
947 case UQUAD:
948 case LONG:
949 case FCOMPLEX:
950 case DCOMPLEX:
951 case COMPLEX:
952 break;
953 #ifndef __COVERITY__
954 case NTSPEC:
955 abort();
956 #endif
957 }
958
959 *epp = cp;
960 return (cp - cp1);
961 }
962
963 /*
964 * Search a type by it's type string.
965 */
966 static u_short
967 findtype(const char *cp, size_t len, int h)
968 {
969 thtab_t *thte;
970
971 for (thte = thtab[h]; thte != NULL; thte = thte->th_nxt) {
972 if (strncmp(thte->th_name, cp, len) != 0)
973 continue;
974 if (thte->th_name[len] == '\0')
975 return (thte->th_idx);
976 }
977
978 return (0);
979 }
980
981 /*
982 * Store a type and it's type string so we can later share this type
983 * if we read the same type string from the input file.
984 */
985 static u_short
986 storetyp(type_t *tp, const char *cp, size_t len, int h)
987 {
988 static u_int tidx = 1; /* 0 is reserved */
989 thtab_t *thte;
990 char *name;
991
992 if (tidx >= USHRT_MAX)
993 errx(1, "sorry, too many types");
994
995 if (tidx == tlstlen - 1) {
996 tlst = xrealloc(tlst, (tlstlen * 2) * sizeof (type_t *));
997 (void)memset(tlst + tlstlen, 0, tlstlen * sizeof (type_t *));
998 tlstlen *= 2;
999 }
1000
1001 tlst[tidx] = tp;
1002
1003 /* create a hash table entry */
1004 name = xalloc(len + 1);
1005 (void)memcpy(name, cp, len);
1006 name[len] = '\0';
1007
1008 thte = xalloc(sizeof (thtab_t));
1009 thte->th_name = name;
1010 thte->th_idx = tidx;
1011 thte->th_nxt = thtab[h];
1012 thtab[h] = thte;
1013
1014 return ((u_short)tidx++);
1015 }
1016
1017 /*
1018 * Hash function for types
1019 */
1020 static int
1021 thash(const char *s, size_t len)
1022 {
1023 u_int v;
1024
1025 v = 0;
1026 while (len-- != 0) {
1027 v = (v << sizeof (v)) + (u_char)*s++;
1028 v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v));
1029 }
1030 return (v % THSHSIZ2);
1031 }
1032
1033 /*
1034 * Read a string enclosed by "". This string may contain quoted chars.
1035 */
1036 static char *
1037 inpqstrg(const char *src, const char **epp)
1038 {
1039 char *strg, *dst;
1040 size_t slen;
1041 int c;
1042 int v;
1043
1044 dst = strg = xmalloc(slen = 32);
1045
1046 if ((c = *src++) != '"')
1047 inperr("not quote: %c", c);
1048 if ((c = *src++) == '\0')
1049 inperr("trailing data: %c", c);
1050
1051 while (c != '"') {
1052 if (c == '\\') {
1053 if ((c = *src++) == '\0')
1054 inperr("missing after \\");
1055 switch (c) {
1056 case 'n':
1057 c = '\n';
1058 break;
1059 case 't':
1060 c = '\t';
1061 break;
1062 case 'v':
1063 c = '\v';
1064 break;
1065 case 'b':
1066 c = '\b';
1067 break;
1068 case 'r':
1069 c = '\r';
1070 break;
1071 case 'f':
1072 c = '\f';
1073 break;
1074 case 'a':
1075 c = '\a';
1076 break;
1077 case '\\':
1078 c = '\\';
1079 break;
1080 case '"':
1081 c = '"';
1082 break;
1083 case '\'':
1084 c = '\'';
1085 break;
1086 case '0': case '1': case '2': case '3':
1087 v = (c - '0') << 6;
1088 if ((c = *src++) < '0' || c > '7')
1089 inperr("not octal: %c", c);
1090 v |= (c - '0') << 3;
1091 if ((c = *src++) < '0' || c > '7')
1092 inperr("not octal: %c", c);
1093 v |= c - '0';
1094 c = (u_char)v;
1095 break;
1096 default:
1097 inperr("bad \\ escape: %c", c);
1098 }
1099 }
1100 /* keep space for trailing '\0' */
1101 if (dst - strg == slen - 1) {
1102 strg = xrealloc(strg, slen * 2);
1103 dst = strg + (slen - 1);
1104 slen *= 2;
1105 }
1106 *dst++ = (char)c;
1107 if ((c = *src++) == '\0')
1108 inperr("missing closing quote");
1109 }
1110 *dst = '\0';
1111
1112 *epp = src;
1113 return (strg);
1114 }
1115
1116 /*
1117 * Read the name of a symbol in static memory.
1118 */
1119 static const char *
1120 inpname(const char *cp, const char **epp)
1121 {
1122 static char *buf;
1123 static size_t blen = 0;
1124 size_t len, i;
1125 char *eptr, c;
1126
1127 len = (int)strtol(cp, &eptr, 10);
1128 if (cp == eptr)
1129 inperr("bad number: %s", cp);
1130 cp = eptr;
1131 if (len + 1 > blen)
1132 buf = xrealloc(buf, blen = len + 1);
1133 for (i = 0; i < len; i++) {
1134 c = *cp++;
1135 if (!isalnum((unsigned char)c) && c != '_')
1136 inperr("not alnum or _: %c", c);
1137 buf[i] = c;
1138 }
1139 buf[i] = '\0';
1140
1141 *epp = cp;
1142 return (buf);
1143 }
1144
1145 /*
1146 * Return the index of a file name. If the name cannot be found, create
1147 * a new entry and return the index of the newly created entry.
1148 */
1149 static int
1150 getfnidx(const char *fn)
1151 {
1152 size_t i;
1153
1154 /* 0 is reserved */
1155 for (i = 1; fnames[i] != NULL; i++) {
1156 if (strcmp(fnames[i], fn) == 0)
1157 return i;
1158 }
1159
1160 if (i == nfnames - 1) {
1161 size_t nlen = nfnames * 2;
1162 fnames = xrealloc(fnames, nlen * sizeof(char *));
1163 (void)memset(fnames + nfnames, 0, nfnames * sizeof(char *));
1164 flines = xrealloc(flines, nlen * sizeof(size_t));
1165 (void)memset(flines + nfnames, 0, nfnames * sizeof(size_t));
1166 nfnames = nlen;
1167 }
1168
1169 fnames[i] = xstrdup(fn);
1170 flines[i] = 0;
1171 return (i);
1172 }
1173
1174 /*
1175 * Separate symbols with static and external linkage.
1176 */
1177 void
1178 mkstatic(hte_t *hte)
1179 {
1180 sym_t *sym1, **symp, *sym;
1181 fcall_t **callp, *call;
1182 usym_t **usymp, *usym;
1183 hte_t *nhte;
1184 int ofnd;
1185
1186 /* Look for first static definition */
1187 for (sym1 = hte->h_syms; sym1 != NULL; sym1 = sym1->s_nxt) {
1188 if (sym1->s_static)
1189 break;
1190 }
1191 if (sym1 == NULL)
1192 return;
1193
1194 /* Do nothing if this name is used only in one translation unit. */
1195 ofnd = 0;
1196 for (sym = hte->h_syms; sym != NULL && !ofnd; sym = sym->s_nxt) {
1197 if (sym->s_pos.p_src != sym1->s_pos.p_src)
1198 ofnd = 1;
1199 }
1200 for (call = hte->h_calls; call != NULL && !ofnd; call = call->f_nxt) {
1201 if (call->f_pos.p_src != sym1->s_pos.p_src)
1202 ofnd = 1;
1203 }
1204 for (usym = hte->h_usyms; usym != NULL && !ofnd; usym = usym->u_nxt) {
1205 if (usym->u_pos.p_src != sym1->s_pos.p_src)
1206 ofnd = 1;
1207 }
1208 if (!ofnd) {
1209 hte->h_used = 1;
1210 /* errors about undef. static symbols are printed in lint1 */
1211 hte->h_def = 1;
1212 hte->h_static = 1;
1213 return;
1214 }
1215
1216 /*
1217 * Create a new hash table entry
1218 *
1219 * XXX this entry should be put at the beginning of the list to
1220 * avoid to process the same symbol twice.
1221 */
1222 for (nhte = hte; nhte->h_link != NULL; nhte = nhte->h_link)
1223 continue;
1224 nhte->h_link = xmalloc(sizeof (hte_t));
1225 nhte = nhte->h_link;
1226 nhte->h_name = hte->h_name;
1227 nhte->h_used = 1;
1228 nhte->h_def = 1; /* error in lint1 */
1229 nhte->h_static = 1;
1230 nhte->h_syms = NULL;
1231 nhte->h_lsym = &nhte->h_syms;
1232 nhte->h_calls = NULL;
1233 nhte->h_lcall = &nhte->h_calls;
1234 nhte->h_usyms = NULL;
1235 nhte->h_lusym = &nhte->h_usyms;
1236 nhte->h_link = NULL;
1237 nhte->h_hte = NULL;
1238
1239 /*
1240 * move all symbols used in this translation unit into the new
1241 * hash table entry.
1242 */
1243 for (symp = &hte->h_syms; (sym = *symp) != NULL; ) {
1244 if (sym->s_pos.p_src == sym1->s_pos.p_src) {
1245 sym->s_static = 1;
1246 (*symp) = sym->s_nxt;
1247 if (hte->h_lsym == &sym->s_nxt)
1248 hte->h_lsym = symp;
1249 sym->s_nxt = NULL;
1250 *nhte->h_lsym = sym;
1251 nhte->h_lsym = &sym->s_nxt;
1252 } else {
1253 symp = &sym->s_nxt;
1254 }
1255 }
1256 for (callp = &hte->h_calls; (call = *callp) != NULL; ) {
1257 if (call->f_pos.p_src == sym1->s_pos.p_src) {
1258 (*callp) = call->f_nxt;
1259 if (hte->h_lcall == &call->f_nxt)
1260 hte->h_lcall = callp;
1261 call->f_nxt = NULL;
1262 *nhte->h_lcall = call;
1263 nhte->h_lcall = &call->f_nxt;
1264 } else {
1265 callp = &call->f_nxt;
1266 }
1267 }
1268 for (usymp = &hte->h_usyms; (usym = *usymp) != NULL; ) {
1269 if (usym->u_pos.p_src == sym1->s_pos.p_src) {
1270 (*usymp) = usym->u_nxt;
1271 if (hte->h_lusym == &usym->u_nxt)
1272 hte->h_lusym = usymp;
1273 usym->u_nxt = NULL;
1274 *nhte->h_lusym = usym;
1275 nhte->h_lusym = &usym->u_nxt;
1276 } else {
1277 usymp = &usym->u_nxt;
1278 }
1279 }
1280
1281 /* h_def must be recalculated for old hte */
1282 hte->h_def = nhte->h_def = 0;
1283 for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
1284 if (sym->s_def == DEF || sym->s_def == TDEF) {
1285 hte->h_def = 1;
1286 break;
1287 }
1288 }
1289
1290 mkstatic(hte);
1291 }
1292