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 $
46 /* index of current (included) source file */
50 * The array pointed to by inpfns maps the file name indices of input files
51 * to the file name indices used in lint2
54 static size_t ninpfns;
57 * The array pointed to by *fnames maps file name indizes to file names.
58 * Indices of type short are used instead of pointers to save memory.
61 static size_t nfnames;
64 * Types are shared (to save memory for the types itself) and accessed
65 * via indices (to save memory for references to types (indices are short)).
66 * To share types, a equal type must be located fast. This is done by a
67 * hash table. Access by indices is done via an array of pointers to the
70 typedef struct thtab {
75 static thtab_t **thtab; /* hash table */
76 type_t **tlst; /* array for indexed access */
77 static size_t tlstlen; /* length of tlst */
79 /* index of current C source file (as spezified at the command line) */
83 static void inperr __P((void));
84 static void setsrc __P((const char *));
85 static void setfnid __P((int, const char *));
86 static void funccall __P((pos_t *, const char *));
87 static void decldef __P((pos_t *, const char *));
88 static void usedsym __P((pos_t *, const char *));
89 static u_short inptype __P((const char *, const char **));
90 static int gettlen __P((const char *, const char **));
91 static u_short findtype __P((const char *, size_t, int));
92 static u_short storetyp __P((type_t *, const char *, size_t, int));
93 static int thash __P((const char *, size_t));
94 static char *inpqstrg __P((const char *, const char **));
95 static const char *inpname __P((const char *, const char **));
96 static int getfnidx __P((const char *));
105 char *line, *eptr, rt;
106 int cline, isrc, iline;
110 inpfns = xcalloc(ninpfns = 128, sizeof (short));
112 fnames = xcalloc(nfnames = 256, sizeof (char *));
114 tlst = xcalloc(tlstlen = 256, sizeof (type_t *));
116 thtab = xcalloc(THSHSIZ2, sizeof (thtab_t));
118 srcfile = getfnidx(name);
120 if ((inp = fopen(name, "r")) == NULL)
121 err(1, "cannot open %s", name);
123 while ((line = fgetln(inp, &len)) != NULL) {
125 if (len == 0 || line[len - 1] != '\n')
127 line[len - 1] = '\0';
130 /* line number in csrcfile */
131 cline = (int)strtol(cp, &eptr, 10);
148 } else if (rt == 's') {
154 * Index of (included) source file. If this index is
155 * different from csrcfile, it refers to an included
158 isrc = (int)strtol(cp, &eptr, 10);
164 /* line number in isrc */
167 iline = (int)strtol(cp, &eptr, 10);
172 pos.p_src = (u_short)csrcfile;
173 pos.p_line = (u_short)cline;
174 pos.p_isrc = (u_short)isrc;
175 pos.p_iline = (u_short)iline;
177 /* process rest of this record */
195 err(1, "read error on %s", name);
204 errx(1, "input file error: %s", fnames[srcfile]);
208 * Set the name of the C source file of the .ln file which is
215 csrcfile = getfnidx(cp);
219 * setfnid() gets as input an index as used in an input file and the
220 * associated file name. If neccessary, it creates a new lint2 file
221 * name index for this file name and creates the mapping of the index
222 * as used in the input file to the index used in lint2.
232 if (fid >= ninpfns) {
233 inpfns = xrealloc(inpfns, (ninpfns * 2) * sizeof (short));
234 (void)memset(inpfns + ninpfns, 0, ninpfns * sizeof (short));
238 * Should always be true because indices written in the output
239 * file by lint1 are always the previous index + 1.
242 errx(1, "internal error: setfnid()");
243 inpfns[fid] = (u_short)getfnidx(cp);
247 * Process a function call record (c-record).
260 fcall = xalloc(sizeof (fcall_t));
261 STRUCT_ASSIGN(fcall->f_pos, *posp);
265 lai = &fcall->f_args;
266 while ((c = *cp) == 'u' || c == 'i' || c == 'd' ||
267 c == 'z' || c == 'p' || c == 'n' || c == 's') {
288 ai = xalloc(sizeof (arginf_t));
289 ai->a_num = (int)strtol(cp, &eptr, 10);
294 ai->a_pcon = ai->a_zero = 1;
295 } else if (c == 'p') {
297 } else if (c == 'n') {
301 ai->a_fstrg = inpqstrg(cp, &cp);
308 fcall->f_rused = rused;
309 fcall->f_rdisc = rdisc;
311 /* read name of function */
312 hte = hsearch(inpname(cp, &cp), 1);
315 fcall->f_type = inptype(cp, &cp);
317 *hte->h_lcall = fcall;
318 hte->h_lcall = &fcall->f_nxt;
325 * Process a declaration or definition (d-record).
337 (void)memset(&sym, 0, sizeof (sym));
338 STRUCT_ASSIGN(sym.s_pos, *posp);
343 while ((c = *cp) == 't' || c == 'd' || c == 'e' || c == 'u' ||
344 c == 'r' || c == 'o' || c == 's' || c == 'v' ||
345 c == 'P' || c == 'S') {
349 if (sym.s_def != NODECL)
354 if (sym.s_def != NODECL)
359 if (sym.s_def != NODECL)
387 sym.s_nva = (short)strtol(cp, &ep, 10);
396 sym.s_nprfl = (short)strtol(cp, &ep, 10);
405 sym.s_nscfl = (short)strtol(cp, &ep, 10);
413 /* read symbol name */
414 hte = hsearch(inpname(cp, &cp), 1);
416 if (sym.s_def == DEF || sym.s_def == TDEF)
419 sym.s_type = inptype(cp, &cp);
422 * Allocate memory for this symbol only if it was not already
423 * declared or tentatively defined at the same location with
424 * the same type. Works only for symbols with external linkage,
425 * because static symbols, tentatively defined at the same location
426 * but in different translation units are really different symbols.
428 for (symp = hte->h_syms; symp != NULL; symp = symp->s_nxt) {
429 if (symp->s_pos.p_isrc == sym.s_pos.p_isrc &&
430 symp->s_pos.p_iline == sym.s_pos.p_iline &&
431 symp->s_type == sym.s_type &&
432 ((symp->s_def == DECL && sym.s_def == DECL) ||
433 (!sflag && symp->s_def == TDEF && sym.s_def == TDEF)) &&
434 !symp->s_static && !sym.s_static) {
440 /* allocsym reserviert keinen Platz fuer s_nva */
441 if (sym.s_va || sym.s_prfl || sym.s_scfl) {
442 symp = xalloc(sizeof (sym_t));
443 STRUCT_ASSIGN(*symp, sym);
445 symp = xalloc(sizeof (symp->s_s));
446 STRUCT_ASSIGN(symp->s_s, sym.s_s);
449 hte->h_lsym = &symp->s_nxt;
457 * Read an u-record (emited by lint1 if a symbol was used).
467 usym = xalloc(sizeof (usym_t));
468 STRUCT_ASSIGN(usym->u_pos, *posp);
470 /* needed as delimiter between two numbers */
474 hte = hsearch(inpname(cp, &cp), 1);
477 *hte->h_lusym = usym;
478 hte->h_lusym = &usym->u_nxt;
482 * Read a type and return the index of this type.
486 const char *cp, **epp;
496 /* If we have this type already, return it's index. */
497 tlen = gettlen(cp, &ep);
499 if ((tidx = findtype(cp, tlen, h)) != 0) {
504 /* No, we must create a new type. */
505 tp = xalloc(sizeof (type_t));
507 tidx = storetyp(tp, cp, tlen, h);
511 while (c == 'c' || c == 'v') {
520 if (c == 's' || c == 'u' || c == 'l' || c == 'e') {
529 tp->t_tspec = s == 's' ? SCHAR : (s == 'u' ? UCHAR : CHAR);
532 tp->t_tspec = s == 'u' ? USHORT : SHORT;
535 tp->t_tspec = s == 'u' ? UINT : INT;
538 tp->t_tspec = s == 'u' ? ULONG : LONG;
541 tp->t_tspec = s == 'u' ? UQUAD : QUAD;
544 tp->t_tspec = s == 's' ? FLOAT : (s == 'l' ? LDOUBLE : DOUBLE);
561 tp->t_tspec = s == 'e' ? ENUM : (s == 's' ? STRUCT : UNION);
565 switch (tp->t_tspec) {
567 tp->t_dim = (int)strtol(cp, &eptr, 10);
569 tp->t_subt = TP(inptype(cp, &cp));
572 tp->t_subt = TP(inptype(cp, &cp));
576 if (isdigit((u_char)c)) {
579 narg = (int)strtol(cp, &eptr, 10);
581 tp->t_args = xcalloc((size_t)(narg + 1),
583 for (i = 0; i < narg; i++) {
584 if (i == narg - 1 && *cp == 'E') {
588 tp->t_args[i] = TP(inptype(cp, &cp));
592 tp->t_subt = TP(inptype(cp, &cp));
605 tp->t_tag = hsearch(inpname(cp, &cp), 1);
609 tp->t_tynam = hsearch(inpname(cp, &cp), 1);
613 /* LINTED (enumeration value(s) not handled in switch) */
622 * Get the length of a type string.
626 const char *cp, **epp;
639 while (c == 'c' || c == 'v') {
652 if (c == 's' || c == 'u' || c == 'l' || c == 'e') {
665 } else if (s == 'u') {
667 } else if (s == '\0') {
674 } else if (s == '\0') {
681 } else if (s == '\0') {
688 } else if (s == '\0') {
695 } else if (s == '\0') {
702 } else if (s == 'l') {
704 } else if (s == '\0') {
728 } else if (s == 's') {
730 } else if (s == 'u') {
743 (void)strtol(cp, &eptr, 10);
747 (void)gettlen(cp, &cp);
750 (void)gettlen(cp, &cp);
754 if (isdigit((u_char)c)) {
755 narg = (int)strtol(cp, &eptr, 10);
757 for (i = 0; i < narg; i++) {
758 if (i == narg - 1 && *cp == 'E') {
761 (void)gettlen(cp, &cp);
765 (void)gettlen(cp, &cp);
774 (void)inpname(cp, &cp);
777 (void)inpname(cp, &cp);
783 /* LINTED (enumeration value(s) not handled in switch) */
792 * Search a type by it's type string.
802 for (thte = thtab[h]; thte != NULL; thte = thte->th_nxt) {
803 if (strncmp(thte->th_name, cp, len) != 0)
805 if (thte->th_name[len] == '\0')
806 return (thte->th_idx);
813 * Store a type and it's type string so we can later share this type
814 * if we read the same type string from the input file.
817 storetyp(tp, cp, len, h)
824 static u_int tidx = 1;
828 if (tidx >= USHRT_MAX)
829 errx(1, "sorry, too many types");
831 if (tidx == tlstlen - 1) {
832 tlst = xrealloc(tlst, (tlstlen * 2) * sizeof (type_t *));
833 (void)memset(tlst + tlstlen, 0, tlstlen * sizeof (type_t *));
839 /* create a hash table entry */
840 name = xalloc(len + 1);
841 (void)memcpy(name, cp, len);
844 thte = xalloc(sizeof (thtab_t));
845 thte->th_name = name;
847 thte->th_nxt = thtab[h];
850 return ((u_short)tidx++);
854 * Hash function for types
865 v = (v << sizeof (v)) + (u_char)*s++;
866 v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v));
868 return (v % THSHSIZ2);
872 * Read a string enclosed by "". This string may contain quoted chars.
876 const char *src, **epp;
883 dst = strg = xmalloc(slen = 32);
885 if ((c = *src++) != '"')
887 if ((c = *src++) == '\0')
892 if ((c = *src++) == '\0')
933 case '0': case '1': case '2': case '3':
935 if ((c = *src++) < '0' || c > '7')
938 if ((c = *src++) < '0' || c > '7')
947 /* keep space for trailing '\0' */
948 if (dst - strg == slen - 1) {
949 strg = xrealloc(strg, slen * 2);
950 dst = strg + (slen - 1);
954 if ((c = *src++) == '\0')
964 * Read the name of a symbol in static memory.
968 const char *cp, **epp;
971 static size_t blen = 0;
975 len = (int)strtol(cp, &eptr, 10);
980 buf = xrealloc(buf, blen = len + 1);
981 for (i = 0; i < len; i++) {
983 if (!isalnum(c) && c != '_')
994 * Return the index of a file name. If the name cannot be found, create
995 * a new entry and return the index of the newly created entry.
1003 /* 0 ist reserved */
1004 for (i = 1; fnames[i] != NULL; i++) {
1005 if (strcmp(fnames[i], fn) == 0)
1008 if (fnames[i] != NULL)
1011 if (i == nfnames - 1) {
1012 fnames = xrealloc(fnames, (nfnames * 2) * sizeof (char *));
1013 (void)memset(fnames + nfnames, 0, nfnames * sizeof (char *));
1017 fnames[i] = xstrdup(fn);
1022 * Separate symbols with static and external linkage.
1028 sym_t *sym1, **symp, *sym;
1029 fcall_t **callp, *call;
1030 usym_t **usymp, *usym;
1034 /* Look for first static definition */
1035 for (sym1 = hte->h_syms; sym1 != NULL; sym1 = sym1->s_nxt) {
1042 /* Do nothing if this name is used only in one translation unit. */
1044 for (sym = hte->h_syms; sym != NULL && !ofnd; sym = sym->s_nxt) {
1045 if (sym->s_pos.p_src != sym1->s_pos.p_src)
1048 for (call = hte->h_calls; call != NULL && !ofnd; call = call->f_nxt) {
1049 if (call->f_pos.p_src != sym1->s_pos.p_src)
1052 for (usym = hte->h_usyms; usym != NULL && !ofnd; usym = usym->u_nxt) {
1053 if (usym->u_pos.p_src != sym1->s_pos.p_src)
1058 /* errors about undef. static symbols are printed in lint1 */
1065 * Create a new hash table entry
1067 * XXX this entry should be put at the beginning of the list to
1068 * avoid to process the same symbol twice.
1070 for (nhte = hte; nhte->h_link != NULL; nhte = nhte->h_link) ;
1071 nhte->h_link = xalloc(sizeof (hte_t));
1072 nhte = nhte->h_link;
1073 nhte->h_name = hte->h_name;
1076 nhte->h_def = 1; /* error in lint1 */
1077 nhte->h_lsym = &nhte->h_syms;
1078 nhte->h_lcall = &nhte->h_calls;
1079 nhte->h_lusym = &nhte->h_usyms;
1082 * move all symbols used in this translation unit into the new
1085 for (symp = &hte->h_syms; (sym = *symp) != NULL; ) {
1086 if (sym->s_pos.p_src == sym1->s_pos.p_src) {
1088 (*symp) = sym->s_nxt;
1089 if (hte->h_lsym == &sym->s_nxt)
1092 *nhte->h_lsym = sym;
1093 nhte->h_lsym = &sym->s_nxt;
1098 for (callp = &hte->h_calls; (call = *callp) != NULL; ) {
1099 if (call->f_pos.p_src == sym1->s_pos.p_src) {
1100 (*callp) = call->f_nxt;
1101 if (hte->h_lcall == &call->f_nxt)
1102 hte->h_lcall = callp;
1104 *nhte->h_lcall = call;
1105 nhte->h_lcall = &call->f_nxt;
1107 callp = &call->f_nxt;
1110 for (usymp = &hte->h_usyms; (usym = *usymp) != NULL; ) {
1111 if (usym->u_pos.p_src == sym1->s_pos.p_src) {
1112 (*usymp) = usym->u_nxt;
1113 if (hte->h_lusym == &usym->u_nxt)
1114 hte->h_lusym = usymp;
1116 *nhte->h_lusym = usym;
1117 nhte->h_lusym = &usym->u_nxt;
1119 usymp = &usym->u_nxt;
1123 /* h_def must be recalculated for old hte */
1124 hte->h_def = nhte->h_def = 0;
1125 for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
1126 if (sym->s_def == DEF || sym->s_def == TDEF) {