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$");
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 static void inperr(void);
91 static void setsrc(const char *);
92 static void setfnid(int, const char *);
93 static void funccall(pos_t *, const char *);
94 static void decldef(pos_t *, const char *);
95 static void usedsym(pos_t *, const char *);
96 static u_short inptype(const char *, const char **);
97 static int gettlen(const char *, const char **);
98 static u_short findtype(const char *, size_t, int);
99 static u_short storetyp(type_t *, const char *, size_t, int);
100 static int thash(const char *, size_t);
101 static char *inpqstrg(const char *, const char **);
102 static const char *inpname(const char *, const char **);
103 static int getfnidx(const char *);
106 readfile(const char *name)
111 char *line, *eptr, rt = '\0';
112 int cline, isrc, iline;
116 if ((inpfns = calloc(ninpfns = 128, sizeof (short))) == NULL)
119 if ((fnames = calloc(nfnames = 256, sizeof (char *))) == NULL)
122 if ((tlst = calloc(tlstlen = 256, sizeof (type_t *))) == NULL)
125 if ((thtab = calloc(THSHSIZ2, sizeof (thtab_t))) == NULL)
128 _inithash(&renametab);
130 srcfile = getfnidx(name);
132 if ((inp = fopen(name, "r")) == NULL)
133 err(1, "cannot open %s", name);
135 while ((line = fgetln(inp, &len)) != NULL) {
137 if (len == 0 || line[len - 1] != '\n')
139 line[len - 1] = '\0';
142 /* line number in csrcfile */
143 cline = (int)strtol(cp, &eptr, 10);
160 } else if (rt == 's') {
166 * Index of (included) source file. If this index is
167 * different from csrcfile, it refers to an included
170 isrc = (int)strtol(cp, &eptr, 10);
176 /* line number in isrc */
179 iline = (int)strtol(cp, &eptr, 10);
184 pos.p_src = (u_short)csrcfile;
185 pos.p_line = (u_short)cline;
186 pos.p_isrc = (u_short)isrc;
187 pos.p_iline = (u_short)iline;
189 /* process rest of this record */
206 _destroyhash(renametab);
209 err(1, "read error on %s", name);
219 errx(1, "input file error: %s", fnames[srcfile]);
223 * Set the name of the C source file of the .ln file which is
227 setsrc(const char *cp)
230 csrcfile = getfnidx(cp);
234 * setfnid() gets as input an index as used in an input file and the
235 * associated file name. If necessary, it creates a new lint2 file
236 * name index for this file name and creates the mapping of the index
237 * as used in the input file to the index used in lint2.
240 setfnid(int fid, const char *cp)
246 if (fid >= ninpfns) {
247 if ((inpfns = realloc(inpfns, (ninpfns * 2) * sizeof (short)))
250 (void)memset(inpfns + ninpfns, 0, ninpfns * sizeof (short));
254 * Should always be true because indices written in the output
255 * file by lint1 are always the previous index + 1.
258 errx(1, "internal error: setfnid()");
259 inpfns[fid] = (u_short)getfnidx(cp);
263 * Process a function call record (c-record).
266 funccall(pos_t *posp, const char *cp)
275 fcall = xalloc(sizeof (fcall_t));
276 STRUCT_ASSIGN(fcall->f_pos, *posp);
280 lai = &fcall->f_args;
281 while ((c = *cp) == 'u' || c == 'i' || c == 'd' ||
282 c == 'z' || c == 'p' || c == 'n' || c == 's') {
303 ai = xalloc(sizeof (arginf_t));
304 ai->a_num = (int)strtol(cp, &eptr, 10);
309 ai->a_pcon = ai->a_zero = 1;
310 } else if (c == 'p') {
312 } else if (c == 'n') {
316 ai->a_fstrg = inpqstrg(cp, &cp);
323 fcall->f_rused = rused;
324 fcall->f_rdisc = rdisc;
326 /* read name of function */
327 name = inpname(cp, &cp);
329 /* first look it up in the renaming table, then in the normal table */
330 hte = _hsearch(renametab, name, 0);
334 hte = hsearch(name, 1);
337 fcall->f_type = inptype(cp, &cp);
339 *hte->h_lcall = fcall;
340 hte->h_lcall = &fcall->f_nxt;
347 * Process a declaration or definition (d-record).
350 decldef(pos_t *posp, const char *cp)
355 hte_t *hte, *renamehte = NULL;
356 const char *name, *rename;
358 (void)memset(&sym, 0, sizeof (sym));
359 STRUCT_ASSIGN(sym.s_pos, *posp);
364 while ((c = *cp) == 't' || c == 'd' || c == 'e' || c == 'u' ||
365 c == 'r' || c == 'o' || c == 's' || c == 'v' ||
366 c == 'P' || c == 'S') {
370 if (sym.s_def != NODECL)
375 if (sym.s_def != NODECL)
380 if (sym.s_def != NODECL)
408 sym.s_nva = (short)strtol(cp, &ep, 10);
417 sym.s_nprfl = (short)strtol(cp, &ep, 10);
426 sym.s_nscfl = (short)strtol(cp, &ep, 10);
434 /* read symbol name, doing renaming if necessary */
435 name = inpname(cp, &cp);
439 name = xstrdup(name);
440 rename = inpname(cp, &cp);
442 /* enter it and see if it's already been renamed */
443 renamehte = _hsearch(renametab, name, 1);
444 if (renamehte->h_hte == NULL) {
445 hte = hsearch(rename, 1);
446 renamehte->h_hte = hte;
448 } else if (strcmp((hte = renamehte->h_hte)->h_name, rename)) {
449 pos1 = xstrdup(mkpos(&renamehte->h_syms->s_pos));
450 /* %s renamed multiple times\t%s :: %s */
451 msg(18, name, pos1, mkpos(&sym.s_pos));
456 /* it might be a previously-done rename */
457 hte = _hsearch(renametab, name, 0);
461 hte = hsearch(name, 1);
464 if (sym.s_def == DEF || sym.s_def == TDEF)
467 sym.s_type = inptype(cp, &cp);
470 * Allocate memory for this symbol only if it was not already
471 * declared or tentatively defined at the same location with
472 * the same type. Works only for symbols with external linkage,
473 * because static symbols, tentatively defined at the same location
474 * but in different translation units are really different symbols.
476 for (symp = hte->h_syms; symp != NULL; symp = symp->s_nxt) {
477 if (symp->s_pos.p_isrc == sym.s_pos.p_isrc &&
478 symp->s_pos.p_iline == sym.s_pos.p_iline &&
479 symp->s_type == sym.s_type &&
480 ((symp->s_def == DECL && sym.s_def == DECL) ||
481 (!sflag && symp->s_def == TDEF && sym.s_def == TDEF)) &&
482 !symp->s_static && !sym.s_static) {
488 /* allocsym reserviert keinen Platz fuer s_nva */
489 if (sym.s_va || sym.s_prfl || sym.s_scfl) {
490 symp = xalloc(sizeof (sym_t));
491 STRUCT_ASSIGN(*symp, sym);
493 symp = xalloc(sizeof (symp->s_s));
494 STRUCT_ASSIGN(symp->s_s, sym.s_s);
497 hte->h_lsym = &symp->s_nxt;
499 /* XXX hack so we can remember where a symbol was renamed */
501 renamehte->h_syms = symp;
509 * Read an u-record (emited by lint1 if a symbol was used).
512 usedsym(pos_t *posp, const char *cp)
518 usym = xalloc(sizeof (usym_t));
519 STRUCT_ASSIGN(usym->u_pos, *posp);
521 /* needed as delimiter between two numbers */
525 name = inpname(cp, &cp);
526 hte = _hsearch(renametab, name, 0);
530 hte = hsearch(name, 1);
533 *hte->h_lusym = usym;
534 hte->h_lusym = &usym->u_nxt;
538 * Read a type and return the index of this type.
541 inptype(const char *cp, const char **epp)
546 int narg, i, osdef = 0;
551 /* If we have this type already, return it's index. */
552 tlen = gettlen(cp, &ep);
554 if ((tidx = findtype(cp, tlen, h)) != 0) {
559 /* No, we must create a new type. */
560 tp = xalloc(sizeof (type_t));
562 tidx = storetyp(tp, cp, tlen, h);
566 while (c == 'c' || c == 'v') {
575 if (c == 's' || c == 'u' || c == 'l' || c == 'e') {
584 tp->t_tspec = s == 's' ? SCHAR : (s == 'u' ? UCHAR : CHAR);
587 tp->t_tspec = s == 'u' ? USHORT : SHORT;
590 tp->t_tspec = s == 'u' ? UINT : INT;
593 tp->t_tspec = s == 'u' ? ULONG : LONG;
596 tp->t_tspec = s == 'u' ? UQUAD : QUAD;
599 tp->t_tspec = s == 's' ? FLOAT : (s == 'l' ? LDOUBLE : DOUBLE);
616 tp->t_tspec = s == 'e' ? ENUM : (s == 's' ? STRUCT : UNION);
620 switch (tp->t_tspec) {
622 tp->t_dim = (int)strtol(cp, &eptr, 10);
624 tp->t_subt = TP(inptype(cp, &cp));
627 tp->t_subt = TP(inptype(cp, &cp));
631 if (isdigit((u_char)c)) {
634 narg = (int)strtol(cp, &eptr, 10);
636 if ((tp->t_args = calloc((size_t)(narg + 1),
637 sizeof (type_t *))) == NULL)
639 for (i = 0; i < narg; i++) {
640 if (i == narg - 1 && *cp == 'E') {
644 tp->t_args[i] = TP(inptype(cp, &cp));
648 tp->t_subt = TP(inptype(cp, &cp));
659 tp->t_tag = hsearch(inpname(cp, &cp), 1);
663 tp->t_tynam = hsearch(inpname(cp, &cp), 1);
667 tp->t_uniqpos.p_line = strtol(cp, &eptr, 10);
670 /* xlate to 'global' file name. */
671 tp->t_uniqpos.p_file =
672 addoutfile(inpfns[strtol(cp, &eptr, 10)]);
675 tp->t_uniqpos.p_uniq = strtol(cp, &eptr, 10);
708 * Get the length of a type string.
711 gettlen(const char *cp, const char **epp)
724 while (c == 'c' || c == 'v') {
737 if (c == 's' || c == 'u' || c == 'l' || c == 'e') {
750 } else if (s == 'u') {
752 } else if (s == '\0') {
759 } else if (s == '\0') {
766 } else if (s == '\0') {
773 } else if (s == '\0') {
780 } else if (s == '\0') {
787 } else if (s == 'l') {
789 } else if (s == '\0') {
813 } else if (s == 's') {
815 } else if (s == 'u') {
828 (void)strtol(cp, &eptr, 10);
832 (void)gettlen(cp, &cp);
835 (void)gettlen(cp, &cp);
839 if (isdigit((u_char)c)) {
840 narg = (int)strtol(cp, &eptr, 10);
842 for (i = 0; i < narg; i++) {
843 if (i == narg - 1 && *cp == 'E') {
846 (void)gettlen(cp, &cp);
850 (void)gettlen(cp, &cp);
857 (void)inpname(cp, &cp);
860 (void)inpname(cp, &cp);
863 /* unique position: line.file.uniquifier */
864 (void)strtol(cp, &eptr, 10);
870 (void)strtol(cp, &eptr, 10);
876 (void)strtol(cp, &eptr, 10);
913 * Search a type by it's type string.
916 findtype(const char *cp, size_t len, int h)
920 for (thte = thtab[h]; thte != NULL; thte = thte->th_nxt) {
921 if (strncmp(thte->th_name, cp, len) != 0)
923 if (thte->th_name[len] == '\0')
924 return (thte->th_idx);
931 * Store a type and it's type string so we can later share this type
932 * if we read the same type string from the input file.
935 storetyp(type_t *tp, const char *cp, size_t len, int h)
937 static u_int tidx = 1; /* 0 is reserved */
941 if (tidx >= USHRT_MAX)
942 errx(1, "sorry, too many types");
944 if (tidx == tlstlen - 1) {
945 if ((tlst = realloc(tlst, (tlstlen * 2) * sizeof (type_t *)))
948 (void)memset(tlst + tlstlen, 0, tlstlen * sizeof (type_t *));
954 /* create a hash table entry */
955 name = xalloc(len + 1);
956 (void)memcpy(name, cp, len);
959 thte = xalloc(sizeof (thtab_t));
960 thte->th_name = name;
962 thte->th_nxt = thtab[h];
965 return ((u_short)tidx++);
969 * Hash function for types
972 thash(const char *s, size_t len)
978 v = (v << sizeof (v)) + (u_char)*s++;
979 v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v));
981 return (v % THSHSIZ2);
985 * Read a string enclosed by "". This string may contain quoted chars.
988 inpqstrg(const char *src, const char **epp)
995 if ((dst = strg = malloc(slen = 32)) == NULL)
998 if ((c = *src++) != '"')
1000 if ((c = *src++) == '\0')
1005 if ((c = *src++) == '\0')
1038 case '0': case '1': case '2': case '3':
1040 if ((c = *src++) < '0' || c > '7')
1042 v |= (c - '0') << 3;
1043 if ((c = *src++) < '0' || c > '7')
1052 /* keep space for trailing '\0' */
1053 if (dst - strg == slen - 1) {
1054 if ((strg = realloc(strg, slen * 2)) == NULL)
1056 dst = strg + (slen - 1);
1060 if ((c = *src++) == '\0')
1070 * Read the name of a symbol in static memory.
1073 inpname(const char *cp, const char **epp)
1076 static size_t blen = 0;
1080 len = (int)strtol(cp, &eptr, 10);
1085 if ((buf = realloc(buf, blen = len + 1)) == NULL)
1087 for (i = 0; i < len; i++) {
1089 if (!isalnum((unsigned char)c) && c != '_')
1100 * Return the index of a file name. If the name cannot be found, create
1101 * a new entry and return the index of the newly created entry.
1104 getfnidx(const char *fn)
1108 /* 0 ist reserved */
1109 for (i = 1; fnames[i] != NULL; i++) {
1110 if (strcmp(fnames[i], fn) == 0)
1113 if (fnames[i] != NULL)
1116 if (i == nfnames - 1) {
1117 if ((fnames = realloc(fnames, (nfnames * 2) * sizeof (char *)))
1120 (void)memset(fnames + nfnames, 0, nfnames * sizeof (char *));
1124 if ((fnames[i] = strdup(fn)) == NULL)
1130 * Separate symbols with static and external linkage.
1133 mkstatic(hte_t *hte)
1135 sym_t *sym1, **symp, *sym;
1136 fcall_t **callp, *call;
1137 usym_t **usymp, *usym;
1141 /* Look for first static definition */
1142 for (sym1 = hte->h_syms; sym1 != NULL; sym1 = sym1->s_nxt) {
1149 /* Do nothing if this name is used only in one translation unit. */
1151 for (sym = hte->h_syms; sym != NULL && !ofnd; sym = sym->s_nxt) {
1152 if (sym->s_pos.p_src != sym1->s_pos.p_src)
1155 for (call = hte->h_calls; call != NULL && !ofnd; call = call->f_nxt) {
1156 if (call->f_pos.p_src != sym1->s_pos.p_src)
1159 for (usym = hte->h_usyms; usym != NULL && !ofnd; usym = usym->u_nxt) {
1160 if (usym->u_pos.p_src != sym1->s_pos.p_src)
1165 /* errors about undef. static symbols are printed in lint1 */
1172 * Create a new hash table entry
1174 * XXX this entry should be put at the beginning of the list to
1175 * avoid to process the same symbol twice.
1177 for (nhte = hte; nhte->h_link != NULL; nhte = nhte->h_link)
1179 nhte->h_link = xmalloc(sizeof (hte_t));
1180 nhte = nhte->h_link;
1181 nhte->h_name = hte->h_name;
1183 nhte->h_def = 1; /* error in lint1 */
1185 nhte->h_syms = NULL;
1186 nhte->h_lsym = &nhte->h_syms;
1187 nhte->h_calls = NULL;
1188 nhte->h_lcall = &nhte->h_calls;
1189 nhte->h_usyms = NULL;
1190 nhte->h_lusym = &nhte->h_usyms;
1191 nhte->h_link = NULL;
1195 * move all symbols used in this translation unit into the new
1198 for (symp = &hte->h_syms; (sym = *symp) != NULL; ) {
1199 if (sym->s_pos.p_src == sym1->s_pos.p_src) {
1201 (*symp) = sym->s_nxt;
1202 if (hte->h_lsym == &sym->s_nxt)
1205 *nhte->h_lsym = sym;
1206 nhte->h_lsym = &sym->s_nxt;
1211 for (callp = &hte->h_calls; (call = *callp) != NULL; ) {
1212 if (call->f_pos.p_src == sym1->s_pos.p_src) {
1213 (*callp) = call->f_nxt;
1214 if (hte->h_lcall == &call->f_nxt)
1215 hte->h_lcall = callp;
1217 *nhte->h_lcall = call;
1218 nhte->h_lcall = &call->f_nxt;
1220 callp = &call->f_nxt;
1223 for (usymp = &hte->h_usyms; (usym = *usymp) != NULL; ) {
1224 if (usym->u_pos.p_src == sym1->s_pos.p_src) {
1225 (*usymp) = usym->u_nxt;
1226 if (hte->h_lusym == &usym->u_nxt)
1227 hte->h_lusym = usymp;
1229 *nhte->h_lusym = usym;
1230 nhte->h_lusym = &usym->u_nxt;
1232 usymp = &usym->u_nxt;
1236 /* h_def must be recalculated for old hte */
1237 hte->h_def = nhte->h_def = 0;
1238 for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
1239 if (sym->s_def == DEF || sym->s_def == TDEF) {