/* * Write a definition. */ static void outdef(hte_t *hte, sym_t *sym) { /* reset output buffer */ outclr(); /* line number in C source file */ outint(0); /* this is a definition */ outchar('d'); /* index of file where symbol was defined and line number of def. */ outint(0); outchar('.'); outint(0); /* flags */ if (sym->s_va) { outchar('v'); /* varargs */ outint(sym->s_nva); } if (sym->s_scfl) { outchar('S'); /* scanflike */ outint(sym->s_nscfl); } if (sym->s_prfl) { outchar('P'); /* printflike */ outint(sym->s_nprfl); } /* definition or tentative definition */ outchar(sym->s_def == DEF ? 'd' : 't'); if (TP(sym->s_type)->t_tspec == FUNC) { if (sym->s_rval) outchar('r'); /* fkt. has return value */ if (sym->s_osdef) outchar('o'); /* old style definition */ } outchar('u'); /* used (no warning if not used) */ /* name */ outname(hte->h_name); /* type */ outtype(TP(sym->s_type)); }
/* * type to string * used for debugging output * * it uses its own output buffer for conversion */ const char * ttos(type_t *tp) { static ob_t tob; ob_t tmp; if (tob.o_buf == NULL) { tob.o_len = 64; tob.o_buf = tob.o_nxt = xmalloc(tob.o_len); tob.o_end = tob.o_buf + tob.o_len; } tmp = ob; ob = tob; ob.o_nxt = ob.o_buf; outtype(tp); outchar('\0'); tob = ob; ob = tmp; return (tob.o_buf); }
/* vfprint - formatted output to f or string bp */ void vfprint(FILE *f, char *bp, const char *fmt, va_list ap) { for (; *fmt; fmt++) if (*fmt == '%') switch (*++fmt) { case 'd': bp = outd(va_arg(ap, int), f, bp); break; case 'D': bp = outd(va_arg(ap, long), f, bp); break; case 'U': bp = outu(va_arg(ap, unsigned long), 10, f, bp); break; case 'u': bp = outu(va_arg(ap, unsigned), 10, f, bp); break; case 'o': bp = outu(va_arg(ap, unsigned), 8, f, bp); break; case 'X': bp = outu(va_arg(ap, unsigned long), 16, f, bp); break; case 'x': bp = outu(va_arg(ap, unsigned), 16, f, bp); break; case 'f': case 'e': case 'g': { static char format[] = "%f"; char buf[128]; format[1] = *fmt; sprintf(buf, format, va_arg(ap, double)); bp = outs(buf, f, bp); } ; break; case 's': bp = outs(va_arg(ap, char *), f, bp); break; case 'p': { void *p = va_arg(ap, void *); if (p) bp = outs("0x", f, bp); bp = outu((unsigned long)p, 16, f, bp); break; } case 'c': if (f) fputc(va_arg(ap, int), f); else *bp++ = va_arg(ap, int); break; case 'S': { char *s = va_arg(ap, char *); int n = va_arg(ap, int); if (s) for ( ; n-- > 0; s++) if (f) (void)putc(*s, f); else *bp++ = *s; } break; case 'k': { int t = va_arg(ap, int); static char *tokens[] = { #define xx(a,b,c,d,e,f,g) g, #define yy(a,b,c,d,e,f,g) g, #include "token.h" }; assert(tokens[t&0177]); bp = outs(tokens[t&0177], f, bp); } break; case 't': { Type ty = va_arg(ap, Type); assert(f); outtype(ty ? ty : voidtype, f); } break; case 'w': { Coordinate *p = va_arg(ap, Coordinate *); if (p->file && *p->file) { bp = outs(p->file, f, bp); bp = outs(":", f, bp); } bp = outd(p->y, f, bp); } break; case 'I': { int n = va_arg(ap, int); while (--n >= 0) if (f) (void)putc(' ', f); else *bp++ = ' '; } break; default: if (f) (void)putc(*fmt, f); else *bp++ = *fmt; break; } else if (f)
/* * Write type into the output buffer. * The type is written as a sequence of substrings, each of which describes a * node of type type_t * a node is coded as follows: * _Bool B * _Complex float s X * _Complex double X * _Complex long double l X * char C * signed char s C * unsigned char u C * short S * unsigned short u S * int I * unsigned int u I * long L * unsigned long u L * long long Q * unsigned long long u Q * float s D * double D * long double l D * void V * * P * [n] A n * () F * (void) F 0 * (n arguments) F n arg1 arg2 ... argn * (n arguments, ...) F n arg1 arg2 ... argn-1 E * (a, b, c, ...) f n arg1 arg2 ... * enum tag e T tag_or_typename * struct tag s T tag_or_typename * union tag u T tag_or_typename * * tag_or_typename 0 no tag or type name * 1 n tag Tag * 2 n typename only type name * * spaces are only for better readability * additionaly it is possible to prepend the characters 'c' (for const) * and 'v' (for volatile) */ void outtype(type_t *tp) { int t, s, na; sym_t *arg; tspec_t ts; while (tp != NULL) { if ((ts = tp->t_tspec) == INT && tp->t_isenum) ts = ENUM; switch (ts) { case BOOL: t = 'B'; s = '\0'; break; case CHAR: t = 'C'; s = '\0'; break; case SCHAR: t = 'C'; s = 's'; break; case UCHAR: t = 'C'; s = 'u'; break; case SHORT: t = 'S'; s = '\0'; break; case USHORT: t = 'S'; s = 'u'; break; case INT: t = 'I'; s = '\0'; break; case UINT: t = 'I'; s = 'u'; break; case LONG: t = 'L'; s = '\0'; break; case ULONG: t = 'L'; s = 'u'; break; case QUAD: t = 'Q'; s = '\0'; break; case UQUAD: t = 'Q'; s = 'u'; break; case FLOAT: t = 'D'; s = 's'; break; case DOUBLE: t = 'D'; s = '\0'; break; case LDOUBLE: t = 'D'; s = 'l'; break; case VOID: t = 'V'; s = '\0'; break; case PTR: t = 'P'; s = '\0'; break; case ARRAY: t = 'A'; s = '\0'; break; case FUNC: t = 'F'; s = '\0'; break; case ENUM: t = 'T'; s = 'e'; break; case STRUCT: t = 'T'; s = 's'; break; case UNION: t = 'T'; s = 'u'; break; case FCOMPLEX: t = 'X'; s = 's'; break; case DCOMPLEX: t = 'X'; s = '\0'; break; case LCOMPLEX: t = 'X'; s = 'l'; break; default: LERROR("outtyp()"); } if (tp->t_const) outchar('c'); if (tp->t_volatile) outchar('v'); if (s != '\0') outchar(s); outchar(t); if (ts == ARRAY) { outint(tp->t_dim); } else if (ts == ENUM) { outtt(tp->t_enum->etag, tp->t_enum->etdef); } else if (ts == STRUCT || ts == UNION) { outtt(tp->t_str->stag, tp->t_str->stdef); } else if (ts == FUNC && tp->t_proto) { na = 0; for (arg = tp->t_args; arg != NULL; arg = arg->s_nxt) na++; if (tp->t_vararg) na++; outint(na); for (arg = tp->t_args; arg != NULL; arg = arg->s_nxt) outtype(arg->s_type); if (tp->t_vararg) outchar('E'); } tp = tp->t_subt; } }
/* * write out all information necessary for lint2 to check function * calls * * rvused is set if the return value is used (asigned to a variable) * rvdisc is set if the return value is not used and not ignored * (casted to void) */ void outcall(tnode_t *tn, int rvused, int rvdisc) { tnode_t *args, *arg; int narg, n, i; int64_t q; tspec_t t; /* reset buffer */ outclr(); /* * line number of .c source, 'c' for function call, Id of current * source (.c or .h), and line in current source */ outint(csrc_pos.p_line); outchar('c'); outint(getfnid(curr_pos.p_file)); outchar('.'); outint(curr_pos.p_line); /* * flags; 'u' and 'i' must be last to make sure a letter * is between the numeric argument of a flag and the name of * the function */ narg = 0; args = tn->tn_right; for (arg = args; arg != NULL; arg = arg->tn_right) narg++; /* informations about arguments */ for (n = 1; n <= narg; n++) { /* the last argument is the top one in the tree */ for (i = narg, arg = args; i > n; i--, arg = arg->tn_right) continue; arg = arg->tn_left; if (arg->tn_op == CON) { if (isityp(t = arg->tn_type->t_tspec)) { /* * XXX it would probably be better to * explicitly test the sign */ if ((q = arg->tn_val->v_quad) == 0) { /* zero constant */ outchar('z'); } else if (msb(q, t, 0) == 0) { /* positive if casted to signed */ outchar('p'); } else { /* negative if casted to signed */ outchar('n'); } outint(n); } } else if (arg->tn_op == AMPER && arg->tn_left->tn_op == STRING && arg->tn_left->tn_strg->st_tspec == CHAR) { /* constant string, write all format specifiers */ outchar('s'); outint(n); outfstrg(arg->tn_left->tn_strg); } } /* return value discarded/used/ignored */ outchar(rvdisc ? 'd' : (rvused ? 'u' : 'i')); /* name of the called function */ outname(tn->tn_left->tn_left->tn_sym->s_name); /* types of arguments */ outchar('f'); outint(narg); for (n = 1; n <= narg; n++) { /* the last argument is the top one in the tree */ for (i = narg, arg = args; i > n; i--, arg = arg->tn_right) continue; outtype(arg->tn_left->tn_type); } /* expected type of return value */ outtype(tn->tn_type); }
/* * write information about function definition * * this is also done for static functions so we are able to check if * they are called with proper argument types */ void outfdef(sym_t *fsym, pos_t *posp, int rval, int osdef, sym_t *args) { int narg; sym_t *arg; /* reset the buffer */ outclr(); /* * line number of .c source, 'd' for declaration, Id of current * source (.c or .h), and line in current source * * we are already at the end of the function. If we are in the * .c source, posp->p_line is correct, otherwise csrc_pos.p_line * (for functions defined in header files). */ if (posp->p_file == csrc_pos.p_file) { outint(posp->p_line); } else { outint(csrc_pos.p_line); } outchar('d'); outint(getfnid(posp->p_file)); outchar('.'); outint(posp->p_line); /* flags */ /* both SCANFLIKE and PRINTFLIKE imply VARARGS */ if (prflstrg != -1) { nvararg = prflstrg; } else if (scflstrg != -1) { nvararg = scflstrg; } if (nvararg != -1) { outchar('v'); outint(nvararg); } if (scflstrg != -1) { outchar('S'); outint(scflstrg); } if (prflstrg != -1) { outchar('P'); outint(prflstrg); } nvararg = prflstrg = scflstrg = -1; outchar('d'); if (rval) /* has return value */ outchar('r'); if (llibflg) /* * mark it as used so lint2 does not complain about * unused symbols in libraries */ outchar('u'); if (osdef) /* old style function definition */ outchar('o'); if (fsym->s_scl == STATIC) outchar('s'); /* name of function */ outname(fsym->s_name); /* renamed name of function, if necessary */ if (fsym->s_rename) { outchar('r'); outname(fsym->s_rename); } /* argument types and return value */ if (osdef) { narg = 0; for (arg = args; arg != NULL; arg = arg->s_nxt) narg++; outchar('f'); outint(narg); for (arg = args; arg != NULL; arg = arg->s_nxt) outtype(arg->s_type); outtype(fsym->s_type->t_subt); } else { outtype(fsym->s_type); } }
/* * write information about an global declared/defined symbol * with storage class extern * * informations about function definitions are written in outfdef(), * not here */ void outsym(sym_t *sym, scl_t sc, def_t def) { /* * Static function declarations must also be written to the output * file. Compatibility of function declarations (for both static * and extern functions) must be checked in lint2. Lint1 can't do * this, especially not, if functions are declared at block level * before their first declaration at level 0. */ if (sc != EXTERN && !(sc == STATIC && sym->s_type->t_tspec == FUNC)) return; /* reset buffer */ outclr(); /* * line number of .c source, 'd' for declaration, Id of current * source (.c or .h), and line in current source. */ outint(csrc_pos.p_line); outchar('d'); outint(getfnid(sym->s_dpos.p_file)); outchar('.'); outint(sym->s_dpos.p_line); /* flags */ switch (def) { case DEF: /* defined */ outchar('d'); break; case TDEF: /* tentative defined */ outchar('t'); break; case DECL: /* declared */ outchar('e'); break; default: LERROR("outsym()"); } if (llibflg && def != DECL) { /* * mark it as used so we get no warnings from lint2 about * unused symbols in libraries. */ outchar('u'); } if (sc == STATIC) outchar('s'); /* name of the symbol */ outname(sym->s_name); /* renamed name of symbol, if necessary */ if (sym->s_rename) { outchar('r'); outname(sym->s_rename); } /* type of the symbol */ outtype(sym->s_type); }
/* * Write type into the output buffer. */ static void outtype(type_t *tp) { int t, s, na; tspec_t ts; type_t **ap; while (tp != NULL) { if ((ts = tp->t_tspec) == INT && tp->t_isenum) ts = ENUM; switch (ts) { case BOOL: t = 'B'; s = '\0'; break; case CHAR: t = 'C'; s = '\0'; break; case SCHAR: t = 'C'; s = 's'; break; case UCHAR: t = 'C'; s = 'u'; break; case SHORT: t = 'S'; s = '\0'; break; case USHORT: t = 'S'; s = 'u'; break; case INT: t = 'I'; s = '\0'; break; case UINT: t = 'I'; s = 'u'; break; case LONG: t = 'L'; s = '\0'; break; case ULONG: t = 'L'; s = 'u'; break; case QUAD: t = 'Q'; s = '\0'; break; case UQUAD: t = 'Q'; s = 'u'; break; case FLOAT: t = 'D'; s = 's'; break; case DOUBLE: t = 'D'; s = '\0'; break; case LDOUBLE: t = 'D'; s = 'l'; break; case COMPLEX: t = 'X'; s = 's'; break; case DCOMPLEX: t = 'X'; s = '\0'; break; case LDCOMPLEX: t = 'X'; s = 'l'; break; case IMAGINARY: t = 'J'; s = 's'; break; case DIMAGINARY: t = 'J'; s = '\0'; break; case LDIMAGINARY:t = 'J'; s = 'l'; break; case VOID: t = 'V'; s = '\0'; break; case PTR: t = 'P'; s = '\0'; break; case ARRAY: t = 'A'; s = '\0'; break; case ENUM: t = 'T'; s = 'e'; break; case STRUCT: t = 'T'; s = 's'; break; case UNION: t = 'T'; s = 'u'; break; case FUNC: if (tp->t_args != NULL && !tp->t_proto) { t = 'f'; } else { t = 'F'; } s = '\0'; break; default: errx(1, "internal error: outtype() 1"); } if (tp->t_const) outchar('c'); if (tp->t_volatile) outchar('v'); if (s != '\0') outchar(s); outchar(t); if (ts == ARRAY) { outint(tp->t_dim); } else if (ts == ENUM || ts == STRUCT || ts == UNION) { if (tp->t_istag) { outint(1); outname(tp->t_tag->h_name); } else if (tp->t_istynam) { outint(2); outname(tp->t_tynam->h_name); } else { outint(0); } } else if (ts == FUNC && tp->t_args != NULL) { na = 0; for (ap = tp->t_args; *ap != NULL; ap++) na++; if (tp->t_vararg) na++; outint(na); for (ap = tp->t_args; *ap != NULL; ap++) outtype(*ap); if (tp->t_vararg) outchar('E'); } tp = tp->t_subt; } }