1 /* $NetBSD: read.c,v 1.12 2002/01/21 19:49:52 tv 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.12 2002/01/21 19:49:52 tv Exp $");
39 __FBSDID("$FreeBSD$");
50 /* index of current (included) source file */
54 * The array pointed to by inpfns maps the file name indices of input files
55 * to the file name indices used in lint2
58 static size_t ninpfns;
61 * The array pointed to by *fnames maps file name indizes to file names.
62 * Indices of type short are used instead of pointers to save memory.
65 static size_t nfnames;
68 * Types are shared (to save memory for the types itself) and accessed
69 * via indices (to save memory for references to types (indices are short)).
70 * To share types, a equal type must be located fast. This is done by a
71 * hash table. Access by indices is done via an array of pointers to the
74 typedef struct thtab {
79 static thtab_t **thtab; /* hash table */
80 type_t **tlst; /* array for indexed access */
81 static size_t tlstlen; /* length of tlst */
83 static hte_t **renametab;
85 /* index of current C source file (as spezified at the command line) */
89 static void inperr(void);
90 static void setsrc(const char *);
91 static void setfnid(int, const char *);
92 static void funccall(pos_t *, const char *);
93 static void decldef(pos_t *, const char *);
94 static void usedsym(pos_t *, const char *);
95 static u_short inptype(const char *, const char **);
96 static int gettlen(const char *, const char **);
97 static u_short findtype(const char *, size_t, int);
98 static u_short storetyp(type_t *, const char *, size_t, int);
99 static int thash(const char *, size_t);
100 static char *inpqstrg(const char *, const char **);
101 static const char *inpname(const char *, const char **);
102 static int getfnidx(const char *);
105 readfile(const char *name)
110 char *line, *eptr, rt = '\0';
111 int cline, isrc, iline;
115 if ((inpfns = calloc(ninpfns = 128, sizeof (short))) == NULL)
118 if ((fnames = calloc(nfnames = 256, sizeof (char *))) == NULL)
121 if ((tlst = calloc(tlstlen = 256, sizeof (type_t *))) == NULL)
124 if ((thtab = calloc(THSHSIZ2, sizeof (thtab_t))) == NULL)
127 _inithash(&renametab);
129 srcfile = getfnidx(name);
131 if ((inp = fopen(name, "r")) == NULL)
132 err(1, "cannot open %s", name);
134 while ((line = fgetln(inp, &len)) != NULL) {
136 if (len == 0 || line[len - 1] != '\n')
138 line[len - 1] = '\0';
141 /* line number in csrcfile */
142 cline = (int)strtol(cp, &eptr, 10);
159 } else if (rt == 's') {
165 * Index of (included) source file. If this index is
166 * different from csrcfile, it refers to an included
169 isrc = (int)strtol(cp, &eptr, 10);
175 /* line number in isrc */
178 iline = (int)strtol(cp, &eptr, 10);
183 pos.p_src = (u_short)csrcfile;
184 pos.p_line = (u_short)cline;
185 pos.p_isrc = (u_short)isrc;
186 pos.p_iline = (u_short)iline;
188 /* process rest of this record */
205 _destroyhash(renametab);
208 err(1, "read error on %s", name);
218 errx(1, "input file error: %s", fnames[srcfile]);
222 * Set the name of the C source file of the .ln file which is
226 setsrc(const char *cp)
229 csrcfile = getfnidx(cp);
233 * setfnid() gets as input an index as used in an input file and the
234 * associated file name. If necessary, it creates a new lint2 file
235 * name index for this file name and creates the mapping of the index
236 * as used in the input file to the index used in lint2.
239 setfnid(int fid, const char *cp)
245 if (fid >= ninpfns) {
246 if ((inpfns = realloc(inpfns, (ninpfns * 2) * sizeof (short)))
249 (void)memset(inpfns + ninpfns, 0, ninpfns * sizeof (short));
253 * Should always be true because indices written in the output
254 * file by lint1 are always the previous index + 1.
257 errx(1, "internal error: setfnid()");
258 inpfns[fid] = (u_short)getfnidx(cp);
262 * Process a function call record (c-record).
265 funccall(pos_t *posp, const char *cp)
274 fcall = xalloc(sizeof (fcall_t));
275 STRUCT_ASSIGN(fcall->f_pos, *posp);
279 lai = &fcall->f_args;
280 while ((c = *cp) == 'u' || c == 'i' || c == 'd' ||
281 c == 'z' || c == 'p' || c == 'n' || c == 's') {
302 ai = xalloc(sizeof (arginf_t));
303 ai->a_num = (int)strtol(cp, &eptr, 10);
308 ai->a_pcon = ai->a_zero = 1;
309 } else if (c == 'p') {
311 } else if (c == 'n') {
315 ai->a_fstrg = inpqstrg(cp, &cp);
322 fcall->f_rused = rused;
323 fcall->f_rdisc = rdisc;
325 /* read name of function */
326 name = inpname(cp, &cp);
328 /* first look it up in the renaming table, then in the normal table */
329 hte = _hsearch(renametab, name, 0);
333 hte = hsearch(name, 1);
336 fcall->f_type = inptype(cp, &cp);
338 *hte->h_lcall = fcall;
339 hte->h_lcall = &fcall->f_nxt;
346 * Process a declaration or definition (d-record).
349 decldef(pos_t *posp, const char *cp)
354 hte_t *hte, *renamehte = NULL;
355 const char *name, *rename;
357 (void)memset(&sym, 0, sizeof (sym));
358 STRUCT_ASSIGN(sym.s_pos, *posp);
363 while ((c = *cp) == 't' || c == 'd' || c == 'e' || c == 'u' ||
364 c == 'r' || c == 'o' || c == 's' || c == 'v' ||
365 c == 'P' || c == 'S') {
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 (emited 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 tp->t_subt = TP(inptype(cp, &cp));
626 tp->t_subt = TP(inptype(cp, &cp));
630 if (isdigit((u_char)c)) {
633 narg = (int)strtol(cp, &eptr, 10);
635 if ((tp->t_args = calloc((size_t)(narg + 1),
636 sizeof (type_t *))) == NULL)
638 for (i = 0; i < narg; i++) {
639 if (i == narg - 1 && *cp == 'E') {
643 tp->t_args[i] = TP(inptype(cp, &cp));
647 tp->t_subt = TP(inptype(cp, &cp));
658 tp->t_tag = hsearch(inpname(cp, &cp), 1);
662 tp->t_tynam = hsearch(inpname(cp, &cp), 1);
666 tp->t_uniqpos.p_line = strtol(cp, &eptr, 10);
669 /* xlate to 'global' file name. */
670 tp->t_uniqpos.p_file =
671 addoutfile(inpfns[strtol(cp, &eptr, 10)]);
674 tp->t_uniqpos.p_uniq = strtol(cp, &eptr, 10);
707 * Get the length of a type string.
710 gettlen(const char *cp, const char **epp)
723 while (c == 'c' || c == 'v') {
736 if (c == 's' || c == 'u' || c == 'l' || c == 'e') {
749 } else if (s == 'u') {
751 } else if (s == '\0') {
758 } else if (s == '\0') {
765 } else if (s == '\0') {
772 } else if (s == '\0') {
779 } else if (s == '\0') {
786 } else if (s == 'l') {
788 } else if (s == '\0') {
812 } else if (s == 's') {
814 } else if (s == 'u') {
827 (void)strtol(cp, &eptr, 10);
831 (void)gettlen(cp, &cp);
834 (void)gettlen(cp, &cp);
838 if (isdigit((u_char)c)) {
839 narg = (int)strtol(cp, &eptr, 10);
841 for (i = 0; i < narg; i++) {
842 if (i == narg - 1 && *cp == 'E') {
845 (void)gettlen(cp, &cp);
849 (void)gettlen(cp, &cp);
856 (void)inpname(cp, &cp);
859 (void)inpname(cp, &cp);
862 /* unique position: line.file.uniquifier */
863 (void)strtol(cp, &eptr, 10);
869 (void)strtol(cp, &eptr, 10);
875 (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) {