1 /* $NetBSD: read.c,v 1.19 2007/09/28 21:53:50 uwe Exp $ */
4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
5 * Copyright (c) 1994, 1995 Jochen Pohl
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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 #include <sys/cdefs.h>
36 #if defined(__RCSID) && !defined(lint)
37 __RCSID("$NetBSD: read.c,v 1.19 2007/09/28 21:53:50 uwe Exp $");
39 __FBSDID("$FreeBSD$");
51 /* index of current (included) source file */
55 * The array pointed to by inpfns maps the file name indices of input files
56 * to the file name indices used in lint2
59 static size_t ninpfns;
62 * The array pointed to by *fnames maps file name indizes to file names.
63 * Indices of type short are used instead of pointers to save memory.
66 static size_t nfnames;
69 * Types are shared (to save memory for the types itself) and accessed
70 * via indices (to save memory for references to types (indices are short)).
71 * To share types, an equal type must be located fast. This is done by a
72 * hash table. Access by indices is done via an array of pointers to the
75 typedef struct thtab {
80 static thtab_t **thtab; /* hash table */
81 type_t **tlst; /* array for indexed access */
82 static size_t tlstlen; /* length of tlst */
84 static hte_t **renametab;
86 /* index of current C source file (as spezified at the command line) */
90 #define inperr() inperror(__FILE__, __LINE__)
91 static void inperror(const char *, size_t);
92 static void setsrc(const char *);
93 static void setfnid(int, const char *);
94 static void funccall(pos_t *, const char *);
95 static void decldef(pos_t *, const char *);
96 static void usedsym(pos_t *, const char *);
97 static u_short inptype(const char *, const char **);
98 static int gettlen(const char *, const char **);
99 static u_short findtype(const char *, size_t, int);
100 static u_short storetyp(type_t *, const char *, size_t, int);
101 static int thash(const char *, size_t);
102 static char *inpqstrg(const char *, const char **);
103 static const char *inpname(const char *, const char **);
104 static int getfnidx(const char *);
107 readfile(const char *name)
112 char *line, *eptr, rt = '\0';
113 int cline, isrc, iline;
117 if ((inpfns = calloc(ninpfns = 128, sizeof (short))) == NULL)
120 if ((fnames = calloc(nfnames = 256, sizeof (char *))) == NULL)
123 if ((tlst = calloc(tlstlen = 256, sizeof (type_t *))) == NULL)
126 if ((thtab = calloc(THSHSIZ2, sizeof (thtab_t))) == NULL)
129 _inithash(&renametab);
131 srcfile = getfnidx(name);
133 if ((inp = fopen(name, "r")) == NULL)
134 err(1, "cannot open %s", name);
136 while ((line = fgetln(inp, &len)) != NULL) {
138 if (len == 0 || line[len - 1] != '\n')
140 line[len - 1] = '\0';
143 /* line number in csrcfile */
144 cline = (int)strtol(cp, &eptr, 10);
161 } else if (rt == 's') {
167 * Index of (included) source file. If this index is
168 * different from csrcfile, it refers to an included
171 isrc = (int)strtol(cp, &eptr, 10);
177 /* line number in isrc */
180 iline = (int)strtol(cp, &eptr, 10);
185 pos.p_src = (u_short)csrcfile;
186 pos.p_line = (u_short)cline;
187 pos.p_isrc = (u_short)isrc;
188 pos.p_iline = (u_short)iline;
190 /* process rest of this record */
207 _destroyhash(renametab);
210 err(1, "read error on %s", name);
217 inperror(const char *file, size_t line)
220 errx(1, "%s,%zd: input file error: %s", file, line, fnames[srcfile]);
224 * Set the name of the C source file of the .ln file which is
228 setsrc(const char *cp)
231 csrcfile = getfnidx(cp);
235 * setfnid() gets as input an index as used in an input file and the
236 * associated file name. If necessary, it creates a new lint2 file
237 * name index for this file name and creates the mapping of the index
238 * as used in the input file to the index used in lint2.
241 setfnid(int fid, const char *cp)
247 if (fid >= ninpfns) {
248 if ((inpfns = realloc(inpfns, (ninpfns * 2) * sizeof (short)))
251 (void)memset(inpfns + ninpfns, 0, ninpfns * sizeof (short));
255 * Should always be true because indices written in the output
256 * file by lint1 are always the previous index + 1.
259 errx(1, "internal error: setfnid()");
260 inpfns[fid] = (u_short)getfnidx(cp);
264 * Process a function call record (c-record).
267 funccall(pos_t *posp, const char *cp)
276 fcall = xalloc(sizeof (fcall_t));
277 STRUCT_ASSIGN(fcall->f_pos, *posp);
281 lai = &fcall->f_args;
282 while ((c = *cp) == 'u' || c == 'i' || c == 'd' ||
283 c == 'z' || c == 'p' || c == 'n' || c == 's') {
304 ai = xalloc(sizeof (arginf_t));
305 ai->a_num = (int)strtol(cp, &eptr, 10);
310 ai->a_pcon = ai->a_zero = 1;
311 } else if (c == 'p') {
313 } else if (c == 'n') {
317 ai->a_fstrg = inpqstrg(cp, &cp);
324 fcall->f_rused = rused;
325 fcall->f_rdisc = rdisc;
327 /* read name of function */
328 name = inpname(cp, &cp);
330 /* first look it up in the renaming table, then in the normal table */
331 hte = _hsearch(renametab, name, 0);
335 hte = hsearch(name, 1);
338 fcall->f_type = inptype(cp, &cp);
340 *hte->h_lcall = fcall;
341 hte->h_lcall = &fcall->f_nxt;
348 * Process a declaration or definition (d-record).
351 decldef(pos_t *posp, const char *cp)
356 hte_t *hte, *renamehte = NULL;
357 const char *name, *rename;
359 (void)memset(&sym, 0, sizeof (sym));
360 STRUCT_ASSIGN(sym.s_pos, *posp);
365 while (strchr("tdeurosvPS", (c = *cp)) != NULL) {
369 if (sym.s_def != NODECL)
374 if (sym.s_def != NODECL)
379 if (sym.s_def != NODECL)
407 sym.s_nva = (short)strtol(cp, &ep, 10);
416 sym.s_nprfl = (short)strtol(cp, &ep, 10);
425 sym.s_nscfl = (short)strtol(cp, &ep, 10);
433 /* read symbol name, doing renaming if necessary */
434 name = inpname(cp, &cp);
438 name = xstrdup(name);
439 rename = inpname(cp, &cp);
441 /* enter it and see if it's already been renamed */
442 renamehte = _hsearch(renametab, name, 1);
443 if (renamehte->h_hte == NULL) {
444 hte = hsearch(rename, 1);
445 renamehte->h_hte = hte;
447 } else if (strcmp((hte = renamehte->h_hte)->h_name, rename)) {
448 pos1 = xstrdup(mkpos(&renamehte->h_syms->s_pos));
449 /* %s renamed multiple times\t%s :: %s */
450 msg(18, name, pos1, mkpos(&sym.s_pos));
455 /* it might be a previously-done rename */
456 hte = _hsearch(renametab, name, 0);
460 hte = hsearch(name, 1);
463 if (sym.s_def == DEF || sym.s_def == TDEF)
466 sym.s_type = inptype(cp, &cp);
469 * Allocate memory for this symbol only if it was not already
470 * declared or tentatively defined at the same location with
471 * the same type. Works only for symbols with external linkage,
472 * because static symbols, tentatively defined at the same location
473 * but in different translation units are really different symbols.
475 for (symp = hte->h_syms; symp != NULL; symp = symp->s_nxt) {
476 if (symp->s_pos.p_isrc == sym.s_pos.p_isrc &&
477 symp->s_pos.p_iline == sym.s_pos.p_iline &&
478 symp->s_type == sym.s_type &&
479 ((symp->s_def == DECL && sym.s_def == DECL) ||
480 (!sflag && symp->s_def == TDEF && sym.s_def == TDEF)) &&
481 !symp->s_static && !sym.s_static) {
487 /* allocsym reserviert keinen Platz fuer s_nva */
488 if (sym.s_va || sym.s_prfl || sym.s_scfl) {
489 symp = xalloc(sizeof (sym_t));
490 STRUCT_ASSIGN(*symp, sym);
492 symp = xalloc(sizeof (symp->s_s));
493 STRUCT_ASSIGN(symp->s_s, sym.s_s);
496 hte->h_lsym = &symp->s_nxt;
498 /* XXX hack so we can remember where a symbol was renamed */
500 renamehte->h_syms = symp;
508 * Read an u-record (emitted by lint1 if a symbol was used).
511 usedsym(pos_t *posp, const char *cp)
517 usym = xalloc(sizeof (usym_t));
518 STRUCT_ASSIGN(usym->u_pos, *posp);
520 /* needed as delimiter between two numbers */
524 name = inpname(cp, &cp);
525 hte = _hsearch(renametab, name, 0);
529 hte = hsearch(name, 1);
532 *hte->h_lusym = usym;
533 hte->h_lusym = &usym->u_nxt;
537 * Read a type and return the index of this type.
540 inptype(const char *cp, const char **epp)
545 int narg, i, osdef = 0;
550 /* If we have this type already, return it's index. */
551 tlen = gettlen(cp, &ep);
553 if ((tidx = findtype(cp, tlen, h)) != 0) {
558 /* No, we must create a new type. */
559 tp = xalloc(sizeof (type_t));
561 tidx = storetyp(tp, cp, tlen, h);
565 while (c == 'c' || c == 'v') {
574 if (c == 's' || c == 'u' || c == 'l' || c == 'e') {
583 tp->t_tspec = s == 's' ? SCHAR : (s == 'u' ? UCHAR : CHAR);
586 tp->t_tspec = s == 'u' ? USHORT : SHORT;
589 tp->t_tspec = s == 'u' ? UINT : INT;
592 tp->t_tspec = s == 'u' ? ULONG : LONG;
595 tp->t_tspec = s == 'u' ? UQUAD : QUAD;
598 tp->t_tspec = s == 's' ? FLOAT : (s == 'l' ? LDOUBLE : DOUBLE);
615 tp->t_tspec = s == 'e' ? ENUM : (s == 's' ? STRUCT : UNION);
619 switch (tp->t_tspec) {
621 tp->t_dim = (int)strtol(cp, &eptr, 10);
623 sidx = inptype(cp, &cp); /* force seq. point! (ditto below) */
624 tp->t_subt = TP(sidx);
627 sidx = inptype(cp, &cp);
628 tp->t_subt = TP(sidx);
632 if (isdigit((u_char)c)) {
635 narg = (int)strtol(cp, &eptr, 10);
637 if ((tp->t_args = calloc((size_t)(narg + 1),
638 sizeof (type_t *))) == NULL)
640 for (i = 0; i < narg; i++) {
641 if (i == narg - 1 && *cp == 'E') {
645 sidx = inptype(cp, &cp);
646 tp->t_args[i] = TP(sidx);
650 sidx = inptype(cp, &cp);
651 tp->t_subt = TP(sidx);
662 tp->t_tag = hsearch(inpname(cp, &cp), 1);
666 tp->t_tynam = hsearch(inpname(cp, &cp), 1);
670 tp->t_uniqpos.p_line = strtol(cp, &eptr, 10);
673 /* xlate to 'global' file name. */
674 tp->t_uniqpos.p_file =
675 addoutfile(inpfns[strtol(cp, &eptr, 10)]);
678 tp->t_uniqpos.p_uniq = strtol(cp, &eptr, 10);
709 * Get the length of a type string.
712 gettlen(const char *cp, const char **epp)
725 while (c == 'c' || c == 'v') {
738 if (c == 's' || c == 'u' || c == 'l' || c == 'e') {
751 } else if (s == 'u') {
753 } else if (s == '\0') {
760 } else if (s == '\0') {
767 } else if (s == '\0') {
774 } else if (s == '\0') {
781 } else if (s == '\0') {
788 } else if (s == 'l') {
790 } else if (s == '\0') {
814 } else if (s == 's') {
816 } else if (s == 'u') {
829 (void)strtol(cp, &eptr, 10);
833 (void)gettlen(cp, &cp);
836 (void)gettlen(cp, &cp);
840 if (isdigit((u_char)c)) {
841 narg = (int)strtol(cp, &eptr, 10);
843 for (i = 0; i < narg; i++) {
844 if (i == narg - 1 && *cp == 'E') {
847 (void)gettlen(cp, &cp);
851 (void)gettlen(cp, &cp);
858 (void)inpname(cp, &cp);
861 (void)inpname(cp, &cp);
864 /* unique position: line.file.uniquifier */
865 (void)strtol(cp, &eptr, 10);
871 (void)strtol(cp, &eptr, 10);
877 (void)strtol(cp, &eptr, 10);
912 * Search a type by it's type string.
915 findtype(const char *cp, size_t len, int h)
919 for (thte = thtab[h]; thte != NULL; thte = thte->th_nxt) {
920 if (strncmp(thte->th_name, cp, len) != 0)
922 if (thte->th_name[len] == '\0')
923 return (thte->th_idx);
930 * Store a type and it's type string so we can later share this type
931 * if we read the same type string from the input file.
934 storetyp(type_t *tp, const char *cp, size_t len, int h)
936 static u_int tidx = 1; /* 0 is reserved */
940 if (tidx >= USHRT_MAX)
941 errx(1, "sorry, too many types");
943 if (tidx == tlstlen - 1) {
944 if ((tlst = realloc(tlst, (tlstlen * 2) * sizeof (type_t *)))
947 (void)memset(tlst + tlstlen, 0, tlstlen * sizeof (type_t *));
953 /* create a hash table entry */
954 name = xalloc(len + 1);
955 (void)memcpy(name, cp, len);
958 thte = xalloc(sizeof (thtab_t));
959 thte->th_name = name;
961 thte->th_nxt = thtab[h];
964 return ((u_short)tidx++);
968 * Hash function for types
971 thash(const char *s, size_t len)
977 v = (v << sizeof (v)) + (u_char)*s++;
978 v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v));
980 return (v % THSHSIZ2);
984 * Read a string enclosed by "". This string may contain quoted chars.
987 inpqstrg(const char *src, const char **epp)
994 if ((dst = strg = malloc(slen = 32)) == NULL)
997 if ((c = *src++) != '"')
999 if ((c = *src++) == '\0')
1004 if ((c = *src++) == '\0')
1037 case '0': case '1': case '2': case '3':
1039 if ((c = *src++) < '0' || c > '7')
1041 v |= (c - '0') << 3;
1042 if ((c = *src++) < '0' || c > '7')
1051 /* keep space for trailing '\0' */
1052 if (dst - strg == slen - 1) {
1053 if ((strg = realloc(strg, slen * 2)) == NULL)
1055 dst = strg + (slen - 1);
1059 if ((c = *src++) == '\0')
1069 * Read the name of a symbol in static memory.
1072 inpname(const char *cp, const char **epp)
1075 static size_t blen = 0;
1079 len = (int)strtol(cp, &eptr, 10);
1084 if ((buf = realloc(buf, blen = len + 1)) == NULL)
1086 for (i = 0; i < len; i++) {
1088 if (!isalnum((unsigned char)c) && c != '_')
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.
1103 getfnidx(const char *fn)
1107 /* 0 ist reserved */
1108 for (i = 1; fnames[i] != NULL; i++) {
1109 if (strcmp(fnames[i], fn) == 0)
1112 if (fnames[i] != NULL)
1115 if (i == nfnames - 1) {
1116 if ((fnames = realloc(fnames, (nfnames * 2) * sizeof (char *)))
1119 (void)memset(fnames + nfnames, 0, nfnames * sizeof (char *));
1123 if ((fnames[i] = strdup(fn)) == NULL)
1129 * Separate symbols with static and external linkage.
1132 mkstatic(hte_t *hte)
1134 sym_t *sym1, **symp, *sym;
1135 fcall_t **callp, *call;
1136 usym_t **usymp, *usym;
1140 /* Look for first static definition */
1141 for (sym1 = hte->h_syms; sym1 != NULL; sym1 = sym1->s_nxt) {
1148 /* Do nothing if this name is used only in one translation unit. */
1150 for (sym = hte->h_syms; sym != NULL && !ofnd; sym = sym->s_nxt) {
1151 if (sym->s_pos.p_src != sym1->s_pos.p_src)
1154 for (call = hte->h_calls; call != NULL && !ofnd; call = call->f_nxt) {
1155 if (call->f_pos.p_src != sym1->s_pos.p_src)
1158 for (usym = hte->h_usyms; usym != NULL && !ofnd; usym = usym->u_nxt) {
1159 if (usym->u_pos.p_src != sym1->s_pos.p_src)
1164 /* errors about undef. static symbols are printed in lint1 */
1171 * Create a new hash table entry
1173 * XXX this entry should be put at the beginning of the list to
1174 * avoid to process the same symbol twice.
1176 for (nhte = hte; nhte->h_link != NULL; nhte = nhte->h_link)
1178 nhte->h_link = xmalloc(sizeof (hte_t));
1179 nhte = nhte->h_link;
1180 nhte->h_name = hte->h_name;
1182 nhte->h_def = 1; /* error in lint1 */
1184 nhte->h_syms = NULL;
1185 nhte->h_lsym = &nhte->h_syms;
1186 nhte->h_calls = NULL;
1187 nhte->h_lcall = &nhte->h_calls;
1188 nhte->h_usyms = NULL;
1189 nhte->h_lusym = &nhte->h_usyms;
1190 nhte->h_link = NULL;
1194 * move all symbols used in this translation unit into the new
1197 for (symp = &hte->h_syms; (sym = *symp) != NULL; ) {
1198 if (sym->s_pos.p_src == sym1->s_pos.p_src) {
1200 (*symp) = sym->s_nxt;
1201 if (hte->h_lsym == &sym->s_nxt)
1204 *nhte->h_lsym = sym;
1205 nhte->h_lsym = &sym->s_nxt;
1210 for (callp = &hte->h_calls; (call = *callp) != NULL; ) {
1211 if (call->f_pos.p_src == sym1->s_pos.p_src) {
1212 (*callp) = call->f_nxt;
1213 if (hte->h_lcall == &call->f_nxt)
1214 hte->h_lcall = callp;
1216 *nhte->h_lcall = call;
1217 nhte->h_lcall = &call->f_nxt;
1219 callp = &call->f_nxt;
1222 for (usymp = &hte->h_usyms; (usym = *usymp) != NULL; ) {
1223 if (usym->u_pos.p_src == sym1->s_pos.p_src) {
1224 (*usymp) = usym->u_nxt;
1225 if (hte->h_lusym == &usym->u_nxt)
1226 hte->h_lusym = usymp;
1228 *nhte->h_lusym = usym;
1229 nhte->h_lusym = &usym->u_nxt;
1231 usymp = &usym->u_nxt;
1235 /* h_def must be recalculated for old hte */
1236 hte->h_def = nhte->h_def = 0;
1237 for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
1238 if (sym->s_def == DEF || sym->s_def == TDEF) {