1 /* $NetBSD: read.c,v 1.2 1995/07/03 21:24:59 cgd Exp $ */
4 * Copyright (c) 1994, 1995 Jochen Pohl
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Jochen Pohl for
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * $NetBSD: read.c,v 1.2 1995/07/03 21:24:59 cgd Exp $
34 * $DragonFly: src/usr.bin/xlint/lint2/read.c,v 1.4 2003/11/03 19:31:34 eirikn Exp $
47 /* index of current (included) source file */
51 * The array pointed to by inpfns maps the file name indices of input files
52 * to the file name indices used in lint2
55 static size_t ninpfns;
58 * The array pointed to by *fnames maps file name indizes to file names.
59 * Indices of type short are used instead of pointers to save memory.
62 static size_t nfnames;
65 * Types are shared (to save memory for the types itself) and accessed
66 * via indices (to save memory for references to types (indices are short)).
67 * To share types, a equal type must be located fast. This is done by a
68 * hash table. Access by indices is done via an array of pointers to the
71 typedef struct thtab {
76 static thtab_t **thtab; /* hash table */
77 type_t **tlst; /* array for indexed access */
78 static size_t tlstlen; /* length of tlst */
80 /* index of current C source file (as spezified at the command line) */
84 static void inperr(void);
85 static void setsrc(const char *);
86 static void setfnid(int, const char *);
87 static void funccall(pos_t *, const char *);
88 static void decldef(pos_t *, const char *);
89 static void usedsym(pos_t *, const char *);
90 static u_short inptype(const char *, const char **);
91 static int gettlen(const char *, const char **);
92 static u_short findtype(const char *, size_t, int);
93 static u_short storetyp(type_t *, const char *, size_t, int);
94 static int thash(const char *, size_t);
95 static char *inpqstrg(const char *, const char **);
96 static const char *inpname(const char *, const char **);
97 static int getfnidx(const char *);
106 char *line, *eptr, rt;
107 int cline, isrc, iline;
111 inpfns = xcalloc(ninpfns = 128, sizeof (short));
113 fnames = xcalloc(nfnames = 256, sizeof (char *));
115 tlst = xcalloc(tlstlen = 256, sizeof (type_t *));
117 thtab = xcalloc(THSHSIZ2, sizeof (thtab_t));
119 srcfile = getfnidx(name);
121 if ((inp = fopen(name, "r")) == NULL)
122 err(1, "cannot open %s", name);
124 while ((line = fgetln(inp, &len)) != NULL) {
126 if (len == 0 || line[len - 1] != '\n')
128 line[len - 1] = '\0';
131 /* line number in csrcfile */
132 cline = (int)strtol(cp, &eptr, 10);
149 } else if (rt == 's') {
155 * Index of (included) source file. If this index is
156 * different from csrcfile, it refers to an included
159 isrc = (int)strtol(cp, &eptr, 10);
165 /* line number in isrc */
168 iline = (int)strtol(cp, &eptr, 10);
173 pos.p_src = (u_short)csrcfile;
174 pos.p_line = (u_short)cline;
175 pos.p_isrc = (u_short)isrc;
176 pos.p_iline = (u_short)iline;
178 /* process rest of this record */
196 err(1, "read error on %s", name);
205 errx(1, "input file error: %s", fnames[srcfile]);
209 * Set the name of the C source file of the .ln file which is
216 csrcfile = getfnidx(cp);
220 * setfnid() gets as input an index as used in an input file and the
221 * associated file name. If neccessary, it creates a new lint2 file
222 * name index for this file name and creates the mapping of the index
223 * as used in the input file to the index used in lint2.
233 if (fid >= ninpfns) {
234 inpfns = xrealloc(inpfns, (ninpfns * 2) * sizeof (short));
235 (void)memset(inpfns + ninpfns, 0, ninpfns * sizeof (short));
239 * Should always be true because indices written in the output
240 * file by lint1 are always the previous index + 1.
243 errx(1, "internal error: setfnid()");
244 inpfns[fid] = (u_short)getfnidx(cp);
248 * Process a function call record (c-record).
261 fcall = xalloc(sizeof (fcall_t));
262 STRUCT_ASSIGN(fcall->f_pos, *posp);
266 lai = &fcall->f_args;
267 while ((c = *cp) == 'u' || c == 'i' || c == 'd' ||
268 c == 'z' || c == 'p' || c == 'n' || c == 's') {
289 ai = xalloc(sizeof (arginf_t));
290 ai->a_num = (int)strtol(cp, &eptr, 10);
295 ai->a_pcon = ai->a_zero = 1;
296 } else if (c == 'p') {
298 } else if (c == 'n') {
302 ai->a_fstrg = inpqstrg(cp, &cp);
309 fcall->f_rused = rused;
310 fcall->f_rdisc = rdisc;
312 /* read name of function */
313 hte = hsearch(inpname(cp, &cp), 1);
316 fcall->f_type = inptype(cp, &cp);
318 *hte->h_lcall = fcall;
319 hte->h_lcall = &fcall->f_nxt;
326 * Process a declaration or definition (d-record).
338 (void)memset(&sym, 0, sizeof (sym));
339 STRUCT_ASSIGN(sym.s_pos, *posp);
344 while ((c = *cp) == 't' || c == 'd' || c == 'e' || c == 'u' ||
345 c == 'r' || c == 'o' || c == 's' || c == 'v' ||
346 c == 'P' || c == 'S') {
350 if (sym.s_def != NODECL)
355 if (sym.s_def != NODECL)
360 if (sym.s_def != NODECL)
388 sym.s_nva = (short)strtol(cp, &ep, 10);
397 sym.s_nprfl = (short)strtol(cp, &ep, 10);
406 sym.s_nscfl = (short)strtol(cp, &ep, 10);
414 /* read symbol name */
415 hte = hsearch(inpname(cp, &cp), 1);
417 if (sym.s_def == DEF || sym.s_def == TDEF)
420 sym.s_type = inptype(cp, &cp);
423 * Allocate memory for this symbol only if it was not already
424 * declared or tentatively defined at the same location with
425 * the same type. Works only for symbols with external linkage,
426 * because static symbols, tentatively defined at the same location
427 * but in different translation units are really different symbols.
429 for (symp = hte->h_syms; symp != NULL; symp = symp->s_nxt) {
430 if (symp->s_pos.p_isrc == sym.s_pos.p_isrc &&
431 symp->s_pos.p_iline == sym.s_pos.p_iline &&
432 symp->s_type == sym.s_type &&
433 ((symp->s_def == DECL && sym.s_def == DECL) ||
434 (!sflag && symp->s_def == TDEF && sym.s_def == TDEF)) &&
435 !symp->s_static && !sym.s_static) {
441 /* allocsym reserviert keinen Platz fuer s_nva */
442 if (sym.s_va || sym.s_prfl || sym.s_scfl) {
443 symp = xalloc(sizeof (sym_t));
444 STRUCT_ASSIGN(*symp, sym);
446 symp = xalloc(sizeof (symp->s_s));
447 STRUCT_ASSIGN(symp->s_s, sym.s_s);
450 hte->h_lsym = &symp->s_nxt;
458 * Read an u-record (emited by lint1 if a symbol was used).
468 usym = xalloc(sizeof (usym_t));
469 STRUCT_ASSIGN(usym->u_pos, *posp);
471 /* needed as delimiter between two numbers */
475 hte = hsearch(inpname(cp, &cp), 1);
478 *hte->h_lusym = usym;
479 hte->h_lusym = &usym->u_nxt;
483 * Read a type and return the index of this type.
487 const char *cp, **epp;
497 /* If we have this type already, return it's index. */
498 tlen = gettlen(cp, &ep);
500 if ((tidx = findtype(cp, tlen, h)) != 0) {
505 /* No, we must create a new type. */
506 tp = xalloc(sizeof (type_t));
508 tidx = storetyp(tp, cp, tlen, h);
512 while (c == 'c' || c == 'v') {
521 if (c == 's' || c == 'u' || c == 'l' || c == 'e') {
530 tp->t_tspec = s == 's' ? SCHAR : (s == 'u' ? UCHAR : CHAR);
533 tp->t_tspec = s == 'u' ? USHORT : SHORT;
536 tp->t_tspec = s == 'u' ? UINT : INT;
539 tp->t_tspec = s == 'u' ? ULONG : LONG;
542 tp->t_tspec = s == 'u' ? UQUAD : QUAD;
545 tp->t_tspec = s == 's' ? FLOAT : (s == 'l' ? LDOUBLE : DOUBLE);
562 tp->t_tspec = s == 'e' ? ENUM : (s == 's' ? STRUCT : UNION);
566 switch (tp->t_tspec) {
568 tp->t_dim = (int)strtol(cp, &eptr, 10);
570 tp->t_subt = TP(inptype(cp, &cp));
573 tp->t_subt = TP(inptype(cp, &cp));
577 if (isdigit((u_char)c)) {
580 narg = (int)strtol(cp, &eptr, 10);
582 tp->t_args = xcalloc((size_t)(narg + 1),
584 for (i = 0; i < narg; i++) {
585 if (i == narg - 1 && *cp == 'E') {
589 tp->t_args[i] = TP(inptype(cp, &cp));
593 tp->t_subt = TP(inptype(cp, &cp));
606 tp->t_tag = hsearch(inpname(cp, &cp), 1);
610 tp->t_tynam = hsearch(inpname(cp, &cp), 1);
614 /* LINTED (enumeration value(s) not handled in switch) */
623 * Get the length of a type string.
627 const char *cp, **epp;
640 while (c == 'c' || c == 'v') {
653 if (c == 's' || c == 'u' || c == 'l' || c == 'e') {
666 } else if (s == 'u') {
668 } else if (s == '\0') {
675 } else if (s == '\0') {
682 } else if (s == '\0') {
689 } else if (s == '\0') {
696 } else if (s == '\0') {
703 } else if (s == 'l') {
705 } else if (s == '\0') {
729 } else if (s == 's') {
731 } else if (s == 'u') {
744 (void)strtol(cp, &eptr, 10);
748 (void)gettlen(cp, &cp);
751 (void)gettlen(cp, &cp);
755 if (isdigit((u_char)c)) {
756 narg = (int)strtol(cp, &eptr, 10);
758 for (i = 0; i < narg; i++) {
759 if (i == narg - 1 && *cp == 'E') {
762 (void)gettlen(cp, &cp);
766 (void)gettlen(cp, &cp);
775 (void)inpname(cp, &cp);
778 (void)inpname(cp, &cp);
784 /* LINTED (enumeration value(s) not handled in switch) */
793 * Search a type by it's type string.
803 for (thte = thtab[h]; thte != NULL; thte = thte->th_nxt) {
804 if (strncmp(thte->th_name, cp, len) != 0)
806 if (thte->th_name[len] == '\0')
807 return (thte->th_idx);
814 * Store a type and it's type string so we can later share this type
815 * if we read the same type string from the input file.
818 storetyp(tp, cp, len, h)
825 static u_int tidx = 1;
829 if (tidx >= USHRT_MAX)
830 errx(1, "sorry, too many types");
832 if (tidx == tlstlen - 1) {
833 tlst = xrealloc(tlst, (tlstlen * 2) * sizeof (type_t *));
834 (void)memset(tlst + tlstlen, 0, tlstlen * sizeof (type_t *));
840 /* create a hash table entry */
841 name = xalloc(len + 1);
842 (void)memcpy(name, cp, len);
845 thte = xalloc(sizeof (thtab_t));
846 thte->th_name = name;
848 thte->th_nxt = thtab[h];
851 return ((u_short)tidx++);
855 * Hash function for types
866 v = (v << sizeof (v)) + (u_char)*s++;
867 v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v));
869 return (v % THSHSIZ2);
873 * Read a string enclosed by "". This string may contain quoted chars.
877 const char *src, **epp;
884 dst = strg = xmalloc(slen = 32);
886 if ((c = *src++) != '"')
888 if ((c = *src++) == '\0')
893 if ((c = *src++) == '\0')
934 case '0': case '1': case '2': case '3':
936 if ((c = *src++) < '0' || c > '7')
939 if ((c = *src++) < '0' || c > '7')
948 /* keep space for trailing '\0' */
949 if (dst - strg == slen - 1) {
950 strg = xrealloc(strg, slen * 2);
951 dst = strg + (slen - 1);
955 if ((c = *src++) == '\0')
965 * Read the name of a symbol in static memory.
969 const char *cp, **epp;
972 static size_t blen = 0;
976 len = (int)strtol(cp, &eptr, 10);
981 buf = xrealloc(buf, blen = len + 1);
982 for (i = 0; i < len; i++) {
984 if (!isalnum(c) && c != '_')
995 * Return the index of a file name. If the name cannot be found, create
996 * a new entry and return the index of the newly created entry.
1004 /* 0 ist reserved */
1005 for (i = 1; fnames[i] != NULL; i++) {
1006 if (strcmp(fnames[i], fn) == 0)
1009 if (fnames[i] != NULL)
1012 if (i == nfnames - 1) {
1013 fnames = xrealloc(fnames, (nfnames * 2) * sizeof (char *));
1014 (void)memset(fnames + nfnames, 0, nfnames * sizeof (char *));
1018 fnames[i] = xstrdup(fn);
1023 * Separate symbols with static and external linkage.
1029 sym_t *sym1, **symp, *sym;
1030 fcall_t **callp, *call;
1031 usym_t **usymp, *usym;
1035 /* Look for first static definition */
1036 for (sym1 = hte->h_syms; sym1 != NULL; sym1 = sym1->s_nxt) {
1043 /* Do nothing if this name is used only in one translation unit. */
1045 for (sym = hte->h_syms; sym != NULL && !ofnd; sym = sym->s_nxt) {
1046 if (sym->s_pos.p_src != sym1->s_pos.p_src)
1049 for (call = hte->h_calls; call != NULL && !ofnd; call = call->f_nxt) {
1050 if (call->f_pos.p_src != sym1->s_pos.p_src)
1053 for (usym = hte->h_usyms; usym != NULL && !ofnd; usym = usym->u_nxt) {
1054 if (usym->u_pos.p_src != sym1->s_pos.p_src)
1059 /* errors about undef. static symbols are printed in lint1 */
1066 * Create a new hash table entry
1068 * XXX this entry should be put at the beginning of the list to
1069 * avoid to process the same symbol twice.
1071 for (nhte = hte; nhte->h_link != NULL; nhte = nhte->h_link) ;
1072 nhte->h_link = xalloc(sizeof (hte_t));
1073 nhte = nhte->h_link;
1074 nhte->h_name = hte->h_name;
1077 nhte->h_def = 1; /* error in lint1 */
1078 nhte->h_lsym = &nhte->h_syms;
1079 nhte->h_lcall = &nhte->h_calls;
1080 nhte->h_lusym = &nhte->h_usyms;
1083 * move all symbols used in this translation unit into the new
1086 for (symp = &hte->h_syms; (sym = *symp) != NULL; ) {
1087 if (sym->s_pos.p_src == sym1->s_pos.p_src) {
1089 (*symp) = sym->s_nxt;
1090 if (hte->h_lsym == &sym->s_nxt)
1093 *nhte->h_lsym = sym;
1094 nhte->h_lsym = &sym->s_nxt;
1099 for (callp = &hte->h_calls; (call = *callp) != NULL; ) {
1100 if (call->f_pos.p_src == sym1->s_pos.p_src) {
1101 (*callp) = call->f_nxt;
1102 if (hte->h_lcall == &call->f_nxt)
1103 hte->h_lcall = callp;
1105 *nhte->h_lcall = call;
1106 nhte->h_lcall = &call->f_nxt;
1108 callp = &call->f_nxt;
1111 for (usymp = &hte->h_usyms; (usym = *usymp) != NULL; ) {
1112 if (usym->u_pos.p_src == sym1->s_pos.p_src) {
1113 (*usymp) = usym->u_nxt;
1114 if (hte->h_lusym == &usym->u_nxt)
1115 hte->h_lusym = usymp;
1117 *nhte->h_lusym = usym;
1118 nhte->h_lusym = &usym->u_nxt;
1120 usymp = &usym->u_nxt;
1124 /* h_def must be recalculated for old hte */
1125 hte->h_def = nhte->h_def = 0;
1126 for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
1127 if (sym->s_def == DEF || sym->s_def == TDEF) {