1 /* $NetBSD: chk.c,v 1.2 1995/07/03 21:24:42 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: chk.c,v 1.2 1995/07/03 21:24:42 cgd Exp $
43 /* various type information */
47 static void chkund __P((hte_t *));
48 static void chkdnu __P((hte_t *));
49 static void chkdnud __P((hte_t *));
50 static void chkmd __P((hte_t *));
51 static void chkvtui __P((hte_t *, sym_t *, sym_t *));
52 static void chkvtdi __P((hte_t *, sym_t *, sym_t *));
53 static void chkfaui __P((hte_t *, sym_t *, sym_t *));
54 static void chkau __P((hte_t *, int, sym_t *, sym_t *, pos_t *,
55 fcall_t *, fcall_t *, type_t *, type_t *));
56 static void chkrvu __P((hte_t *, sym_t *));
57 static void chkadecl __P((hte_t *, sym_t *, sym_t *));
58 static void printflike __P((hte_t *,fcall_t *, int,
59 const char *, type_t **));
60 static void scanflike __P((hte_t *, fcall_t *, int,
61 const char *, type_t **));
62 static void badfmt __P((hte_t *, fcall_t *));
63 static void inconarg __P((hte_t *, fcall_t *, int));
64 static void tofewarg __P((hte_t *, fcall_t *));
65 static void tomanyarg __P((hte_t *, fcall_t *));
66 static int eqtype __P((type_t *, type_t *, int, int, int, int *));
67 static int eqargs __P((type_t *, type_t *, int *));
68 static int mnoarg __P((type_t *, int *));
81 0, 0, 0, 0, 0, "signed" } },
84 0, 0, 0, 0, 0, "unsigned" } },
85 { CHAR, { CHAR_BIT, CHAR_BIT,
87 1, 0, 0, 1, 1, "char" } },
88 { SCHAR, { CHAR_BIT, CHAR_BIT,
90 1, 0, 0, 1, 1, "signed char" } },
91 { UCHAR, { CHAR_BIT, CHAR_BIT,
93 1, 1, 0, 1, 1, "unsigned char" } },
94 { SHORT, { sizeof (short) * CHAR_BIT, 2 * CHAR_BIT,
96 1, 0, 0, 1, 1, "short" } },
97 { USHORT, { sizeof (u_short) * CHAR_BIT, 2 * CHAR_BIT,
99 1, 1, 0, 1, 1, "unsigned short" } },
100 { INT, { sizeof (int) * CHAR_BIT, 3 * CHAR_BIT,
102 1, 0, 0, 1, 1, "int" } },
103 { UINT, { sizeof (u_int) * CHAR_BIT, 3 * CHAR_BIT,
105 1, 1, 0, 1, 1, "unsigned int" } },
106 { LONG, { sizeof (long) * CHAR_BIT, 4 * CHAR_BIT,
108 1, 0, 0, 1, 1, "long" } },
109 { ULONG, { sizeof (u_long) * CHAR_BIT, 4 * CHAR_BIT,
111 1, 1, 0, 1, 1, "unsigned long" } },
112 { QUAD, { sizeof (quad_t) * CHAR_BIT, 8 * CHAR_BIT,
114 1, 0, 0, 1, 1, "long long" } },
115 { UQUAD, { sizeof (u_quad_t) * CHAR_BIT, 8 * CHAR_BIT,
117 1, 1, 0, 1, 1, "unsigned long long" } },
118 { FLOAT, { sizeof (float) * CHAR_BIT, 4 * CHAR_BIT,
120 0, 0, 1, 1, 1, "float" } },
121 { DOUBLE, { sizeof (double) * CHAR_BIT, 8 * CHAR_BIT,
123 0, 0, 1, 1, 1, "double" } },
124 { LDOUBLE, { sizeof (ldbl_t) * CHAR_BIT, 10 * CHAR_BIT,
126 0, 0, 1, 1, 1, "long double" } },
129 0, 0, 0, 0, 0, "void" } },
132 0, 0, 0, 0, 0, "struct" } },
135 0, 0, 0, 0, 0, "union" } },
136 { ENUM, { sizeof (int) * CHAR_BIT, 3 * CHAR_BIT,
138 1, 0, 0, 1, 1, "enum" } },
139 { PTR, { sizeof (void *) * CHAR_BIT, 4 * CHAR_BIT,
141 0, 1, 0, 0, 1, "pointer" } },
144 0, 0, 0, 0, 0, "array" } },
147 0, 0, 0, 0, 0, "function" } },
150 for (i = 0; i < sizeof (ittab) / sizeof (ittab[0]); i++)
151 STRUCT_ASSIGN(ttab[ittab[i].it_tspec], ittab[i].it_ttab);
153 for (i = 0; i < NTSPEC; i++)
154 ttab[i].tt_psz = ttab[i].tt_sz;
160 * If there is a symbol named "main", mark it as used.
167 if ((hte = hsearch("main", 0)) != NULL)
172 * Performs all tests for a single name
178 sym_t *sym, *def, *pdecl, *decl;
188 /* Get definition, prototype declaration and declaration */
189 def = pdecl = decl = NULL;
190 for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
191 if (def == NULL && (sym->s_def == DEF || sym->s_def == TDEF))
193 if (pdecl == NULL && sym->s_def == DECL &&
194 TP(sym->s_type)->t_tspec == FUNC &&
195 TP(sym->s_type)->t_proto) {
198 if (decl == NULL && sym->s_def == DECL)
202 /* A prototype is better than an old style declaration. */
206 chkvtui(hte, def, decl);
208 chkvtdi(hte, def, decl);
210 chkfaui(hte, def, decl);
214 chkadecl(hte, def, decl);
218 * Print a warning if the name has been used, but not defined.
227 if (!hte->h_used || hte->h_def)
230 if ((fcall = hte->h_calls) != NULL) {
231 /* %s used( %s ), but not defined */
232 msg(0, hte->h_name, mkpos(&fcall->f_pos));
233 } else if ((usym = hte->h_usyms) != NULL) {
234 /* %s used( %s ), but not defined */
235 msg(0, hte->h_name, mkpos(&usym->u_pos));
240 * Print a warning if the name has been defined, but never used.
248 if (!hte->h_def || hte->h_used)
251 for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
252 if (sym->s_def == DEF || sym->s_def == TDEF) {
253 /* %s defined( %s ), but never used */
254 msg(1, hte->h_name, mkpos(&sym->s_pos));
261 * Print a warning if the name has been declared, but is not used
270 if (hte->h_syms == NULL || hte->h_used || hte->h_def)
273 if ((sym = hte->h_syms) != NULL) {
274 if (sym->s_def != DECL)
275 errx(1, "internal error: chkdnud() 1");
276 /* %s declared( %s ), but never used or defined */
277 msg(2, hte->h_name, mkpos(&sym->s_pos));
282 * Print a warning if there is more then one definition for
296 for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
298 * ANSI C allows tentative definitions of the same name in
299 * only one compilation unit.
301 if (sym->s_def != DEF && (!sflag || sym->s_def != TDEF))
307 pos1 = xstrdup(mkpos(&def1->s_pos));
308 /* %s multiply defined\t%s :: %s */
309 msg(3, hte->h_name, pos1, mkpos(&sym->s_pos));
315 * Print a warning if the return value assumed for a function call
316 * differs from the return value of the function definition or
317 * function declaration.
319 * If no definition/declaration can be found, the assumed return values
320 * are always int. So there is no need to compare with another function
321 * call as it's done for function arguments.
324 chkvtui(hte, def, decl)
331 /* LINTED (automatic hides external declaration: warn) */
335 if (hte->h_calls == NULL)
343 t1 = (tp1 = TP(def->s_type)->t_subt)->t_tspec;
344 for (call = hte->h_calls; call != NULL; call = call->f_nxt) {
345 tp2 = TP(call->f_type)->t_subt;
346 eq = eqtype(tp1, tp2, 1, 0, 0, (warn = 0, &warn));
347 if (!call->f_rused) {
348 /* no return value used */
349 if ((t1 == STRUCT || t1 == UNION) && !eq) {
351 * If a function returns a struct or union it
352 * must be declared to return a struct or
353 * union, also if the return value is ignored.
354 * This is necessary because the caller must
355 * allocate stack space for the return value.
356 * If it does not, the return value would over-
358 * XXX Following massage may be confusing
359 * because it appears also if the return value
360 * was declared inconsistently. But this
361 * behaviour matches pcc based lint, so it is
364 pos1 = xstrdup(mkpos(&def->s_pos));
365 /* %s value must be decl. before use %s :: %s */
367 pos1, mkpos(&call->f_pos));
372 if (!eq || (sflag && warn)) {
373 pos1 = xstrdup(mkpos(&def->s_pos));
374 /* %s value used inconsistenty\t%s :: %s */
375 msg(4, hte->h_name, pos1, mkpos(&call->f_pos));
382 * Print a warning if a definition/declaration does not match another
383 * definition/declaration of the same name. For functions, only the
384 * types of return values are tested.
387 chkvtdi(hte, def, decl)
393 /* LINTED (automatic hides external declaration: warn) */
402 tp1 = TP(def->s_type);
403 for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
406 tp2 = TP(sym->s_type);
408 if (tp1->t_tspec == FUNC && tp2->t_tspec == FUNC) {
409 eq = eqtype(tp1->t_subt, tp2->t_subt, 1, 0, 0, &warn);
411 eq = eqtype(tp1, tp2, 0, 0, 0, &warn);
413 if (!eq || (sflag && warn)) {
414 pos1 = xstrdup(mkpos(&def->s_pos));
415 /* %s value declared inconsistently\t%s :: %s */
416 msg(5, hte->h_name, pos1, mkpos(&sym->s_pos));
423 * Print a warning if a function is called with arguments which does
424 * not match the function definition, declaration or another call
425 * of the same function.
428 chkfaui(hte, def, decl)
432 type_t *tp1, *tp2, **ap1, **ap2;
434 fcall_t *calls, *call, *call1;
439 if ((calls = hte->h_calls) == NULL)
443 * If we find a function definition, we use this for comparision,
444 * otherwise the first prototype we can find. If there is no
445 * definition or prototype declaration, the first function call
451 if ((tp1 = TP(def->s_type))->t_tspec != FUNC)
454 } else if (decl != NULL && TP(decl->s_type)->t_proto) {
455 if ((tp1 = TP(decl->s_type))->t_tspec != FUNC)
457 pos1p = &decl->s_pos;
461 calls = calls->f_nxt;
462 if ((tp1 = TP(call1->f_type))->t_tspec != FUNC)
464 pos1p = &call1->f_pos;
468 for (call = calls; call != NULL; call = call->f_nxt) {
469 if ((tp2 = TP(call->f_type))->t_tspec != FUNC)
474 while (*ap1 != NULL && *ap2 != NULL) {
475 if (def != NULL && def->s_va && n >= def->s_nva)
478 chkau(hte, n, def, decl, pos1p, call1, call,
484 /* equal # of arguments */
485 } else if (def != NULL && def->s_va && n >= def->s_nva) {
487 * function definition with VARARGS; The # of
488 * arguments of the call must be at least as large
489 * as the parameter of VARARGS.
491 } else if (*ap2 != NULL && tp1->t_proto && tp1->t_vararg) {
493 * prototype with ... and function call with
494 * at least the same # of arguments as declared
498 pos1 = xstrdup(mkpos(pos1p));
499 /* %s: variable # of args\t%s :: %s */
500 msg(7, hte->h_name, pos1, mkpos(&call->f_pos));
505 /* perform SCANFLIKE/PRINTFLIKE tests */
506 if (def == NULL || (!def->s_prfl && !def->s_scfl))
508 as = def->s_prfl ? def->s_nprfl : def->s_nscfl;
509 for (ai = call->f_args; ai != NULL; ai = ai->a_nxt) {
513 if (ai == NULL || !ai->a_fmt)
516 printflike(hte, call, n, ai->a_fstrg, ap2);
518 scanflike(hte, call, n, ai->a_fstrg, ap2);
524 * Check a single argument in a function call.
526 * hte a pointer to the hash table entry of the function
527 * n the number of the argument (1..)
528 * def the function definition or NULL
529 * decl prototype declaration, old style declaration or NULL
530 * pos1p position of definition, declaration of first call
531 * call1 first call, if both def and decl are old style def/decl
533 * arg1 currently checked argument of def/decl/call1
534 * arg2 currently checked argument of call
538 chkau(hte, n, def, decl, pos1p, call1, call, arg1, arg2)
543 fcall_t *call1, *call;
546 /* LINTED (automatic hides external declaration: warn) */
547 int promote, asgn, warn;
553 * If a function definition is available (def != NULL), we compair the
554 * function call (call) with the definition. Otherwise, if a function
555 * definition is available and it is not an old style definition
556 * (decl != NULL && TP(decl->s_type)->t_proto), we compair the call
557 * with this declaration. Otherwise we compair it with the first
558 * call we have found (call1).
561 /* arg1 must be promoted if it stems from an old style definition */
562 promote = def != NULL && def->s_osdef;
565 * If we compair with a definition or declaration, we must perform
566 * the same checks for qualifiers in indirected types as in
569 asgn = def != NULL || (decl != NULL && TP(decl->s_type)->t_proto);
572 if (eqtype(arg1, arg2, 1, promote, asgn, &warn) && (!sflag || !warn))
576 * Other lint implementations print warnings as soon as the type
577 * of an argument does not match exactly the expected type. The
578 * result are lots of warnings which are really not neccessary.
579 * We print a warning only if
580 * (0) at least one type is not an interger type and types differ
581 * (1) hflag is set and types differ
582 * (2) types differ, except in signedness
583 * If the argument is an integer constant whose msb is not set,
584 * signedness is ignored (e.g. 0 matches both signed and unsigned
585 * int). This is with and without hflag.
586 * If the argument is an integer constant with value 0 and the
587 * expected argument is of type pointer and the width of the
588 * interger constant is the same as the width of the pointer,
589 * no warning is printed.
593 if (isityp(t1) && isityp(t2) && !arg1->t_isenum && !arg2->t_isenum) {
596 * XXX Here is a problem: Althrough it is possible to
597 * pass an int where a char/short it expected, there
598 * may be loss in significant digits. We should first
599 * check for const arguments if they can be converted
600 * into the original parameter type.
604 } else if (t1 == CHAR || t1 == SCHAR) {
606 } else if (t1 == UCHAR) {
607 t1 = tflag ? UINT : INT;
608 } else if (t1 == SHORT) {
610 } else if (t1 == USHORT) {
612 t1 = INT_MAX < USHRT_MAX || tflag ? UINT : INT;
616 if (styp(t1) == styp(t2)) {
619 * types differ only in signedness; get information
624 * treat a definition like a call with variable
627 ai1 = call1 != NULL ? call1->f_args : NULL;
630 * if two calls are compared, ai1 is set to the
631 * information for the n-th argument, if this was
632 * a constant, otherwise to NULL
634 for ( ; ai1 != NULL; ai1 = ai1->a_nxt) {
639 * ai is set to the information of the n-th arg
640 * of the (second) call, if this was a constant,
643 for (ai = call->f_args; ai != NULL; ai = ai->a_nxt) {
648 if (ai1 == NULL && ai == NULL) {
649 /* no constant at all */
652 } else if (ai1 == NULL || ai == NULL) {
656 if (ai->a_zero || ai->a_pcon)
657 /* same value in signed and unsigned */
659 /* value (not representation) differently */
662 * two constants, one signed, one unsigned;
663 * if the msb of one of the constants is set,
664 * the argument is used inconsistently.
666 if (!ai1->a_ncon && !ai->a_ncon)
671 } else if (t1 == PTR && isityp(t2) && psize(t1) == psize(t2)) {
672 for (ai = call->f_args; ai != NULL; ai = ai->a_nxt) {
676 if (ai != NULL && ai->a_zero)
680 pos1 = xstrdup(mkpos(pos1p));
681 /* %s, arg %d used inconsistently\t%s :: %s */
682 msg(6, hte->h_name, n, pos1, mkpos(&call->f_pos));
687 * Compare the types in the NULL-terminated array ap with the format
691 printflike(hte, call, n, fmt, ap)
700 int fwidth, prec, left, sign, space, alt, zero;
710 tomanyarg(hte, call);
718 fwidth = prec = left = sign = space = alt = zero = 0;
727 } else if (fc == '+') {
731 } else if (fc == ' ') {
735 } else if (fc == '#') {
739 } else if (fc == '0') {
752 do { fc = *fp++; } while (isdigit(fc)) ;
753 } else if (fc == '*') {
756 if ((tp = *ap++) == NULL) {
761 if ((t1 = tp->t_tspec) != INT && (hflag || t1 != UINT))
762 inconarg(hte, call, n);
770 do { fc = *fp++; } while (isdigit(fc));
771 } else if (fc == '*') {
773 if ((tp = *ap++) == NULL) {
778 if (tp->t_tspec != INT)
779 inconarg(hte, call, n);
788 } else if (fc == 'l') {
790 } else if (fc == 'q') {
792 } else if (fc == 'L') {
799 if (sz != NOTSPEC || left || sign || space ||
800 alt || zero || prec || fwidth) {
812 if ((tp = *ap++) == NULL) {
817 if ((t1 = tp->t_tspec) == PTR)
818 t2 = tp->t_subt->t_tspec;
820 if (fc == 'd' || fc == 'i') {
821 if (alt || sz == LDOUBLE) {
827 if (t1 != LONG && (hflag || t1 != ULONG))
828 inconarg(hte, call, n);
829 } else if (sz == QUAD) {
830 if (t1 != QUAD && (hflag || t1 != UQUAD))
831 inconarg(hte, call, n);
834 * SHORT is always promoted to INT, USHORT
837 if (t1 != INT && (hflag || t1 != UINT))
838 inconarg(hte, call, n);
840 } else if (fc == 'o' || fc == 'u' || fc == 'x' || fc == 'X') {
841 if ((alt && fc == 'u') || sz == LDOUBLE)
845 if (t1 != ULONG && (hflag || t1 != LONG))
846 inconarg(hte, call, n);
847 } else if (sz == QUAD) {
848 if (t1 != UQUAD && (hflag || t1 != QUAD))
849 inconarg(hte, call, n);
850 } else if (sz == SHORT) {
851 /* USHORT was promoted to INT or UINT */
852 if (t1 != UINT && t1 != INT)
853 inconarg(hte, call, n);
855 if (t1 != UINT && (hflag || t1 != INT))
856 inconarg(hte, call, n);
858 } else if (fc == 'D' || fc == 'O' || fc == 'U') {
859 if ((alt && fc != 'O') || sz != NOTSPEC || !tflag)
867 } else if (fc == 'f' || fc == 'e' || fc == 'E' ||
868 fc == 'g' || fc == 'G') {
871 if (sz != DOUBLE && sz != LDOUBLE)
874 inconarg(hte, call, n);
875 } else if (fc == 'c') {
876 if (sz != NOTSPEC || alt || zero)
879 inconarg(hte, call, n);
880 } else if (fc == 's') {
881 if (sz != NOTSPEC || alt || zero)
884 (t2 != CHAR && t2 != UCHAR && t2 != SCHAR)) {
885 inconarg(hte, call, n);
887 } else if (fc == 'p') {
888 if (fwidth || prec || sz != NOTSPEC || alt || zero)
890 if (t1 != PTR || (hflag && t2 != VOID))
891 inconarg(hte, call, n);
892 } else if (fc == 'n') {
893 if (fwidth || prec || alt || zero || sz == LDOUBLE)
896 inconarg(hte, call, n);
897 } else if (sz == LONG) {
898 if (t2 != LONG && t2 != ULONG)
899 inconarg(hte, call, n);
900 } else if (sz == SHORT) {
901 if (t2 != SHORT && t2 != USHORT)
902 inconarg(hte, call, n);
904 if (t2 != INT && t2 != UINT)
905 inconarg(hte, call, n);
917 * Compare the types in the NULL-terminated array ap with the format
921 scanflike(hte, call, n, fmt, ap)
940 tomanyarg(hte, call);
959 do { fc = *fp++; } while (isdigit(fc));
964 } else if (fc == 'l') {
966 } else if (fc == 'q') {
968 } else if (fc == 'L') {
975 if (sz != NOTSPEC || noasgn || fwidth)
982 if ((tp = *ap++) == NULL) {
987 if ((t1 = tp->t_tspec) == PTR)
988 t2 = tp->t_subt->t_tspec;
991 if (fc == 'd' || fc == 'i' || fc == 'n') {
994 if (sz != SHORT && sz != LONG && sz != QUAD)
999 inconarg(hte, call, n);
1000 } else if (t2 != styp(sz)) {
1001 inconarg(hte, call, n);
1002 } else if (hflag && t2 != sz) {
1003 inconarg(hte, call, n);
1004 } else if (tp->t_subt->t_const) {
1005 inconarg(hte, call, n);
1008 } else if (fc == 'o' || fc == 'u' || fc == 'x') {
1013 } else if (sz == LONG) {
1015 } else if (sz == QUAD) {
1021 } else if (fc == 'D') {
1022 if (sz != NOTSPEC || !tflag)
1026 } else if (fc == 'O') {
1027 if (sz != NOTSPEC || !tflag)
1031 } else if (fc == 'X') {
1033 * XXX valid in ANSI C, but in NetBSD's libc imple-
1034 * mented as "lx". Thats why it should be avoided.
1036 if (sz != NOTSPEC || !tflag)
1040 } else if (fc == 'E') {
1042 * XXX valid in ANSI C, but in NetBSD's libc imple-
1043 * mented as "lf". Thats why it should be avoided.
1045 if (sz != NOTSPEC || !tflag)
1049 } else if (fc == 'F') {
1050 /* XXX only for backward compatibility */
1051 if (sz != NOTSPEC || !tflag)
1055 } else if (fc == 'G') {
1057 * XXX valid in ANSI C, but in NetBSD's libc not
1060 if (sz != NOTSPEC && sz != LONG && sz != LDOUBLE)
1063 } else if (fc == 'e' || fc == 'f' || fc == 'g') {
1065 if (sz == NOTSPEC) {
1067 } else if (sz == LONG) {
1069 } else if (sz != LDOUBLE) {
1074 } else if (fc == 's' || fc == '[' || fc == 'c') {
1078 if ((fc = *fp++) == '-') {
1090 inconarg(hte, call, n);
1091 } else if (t2 != CHAR && t2 != UCHAR &&
1093 inconarg(hte, call, n);
1096 } else if (fc == 'p') {
1100 if (t1 != PTR || t2 != PTR) {
1101 inconarg(hte, call, n);
1102 } else if (tp->t_subt->t_subt->t_tspec!=VOID) {
1104 inconarg(hte, call, n);
1121 /* %s: malformed format string\t%s */
1122 msg(13, hte->h_name, mkpos(&call->f_pos));
1126 inconarg(hte, call, n)
1131 /* %s, arg %d inconsistent with format\t%s(%d) */
1132 msg(14, hte->h_name, n, mkpos(&call->f_pos));
1140 /* %s: too few args for format \t%s */
1141 msg(15, hte->h_name, mkpos(&call->f_pos));
1145 tomanyarg(hte, call)
1149 /* %s: too many args for format \t%s */
1150 msg(16, hte->h_name, mkpos(&call->f_pos));
1155 * Print warnings for return values which are used, but not returned,
1156 * or return values which are always or sometimes ignored.
1167 /* don't know wheter or not the functions returns a value */
1170 if (hte->h_calls == NULL)
1174 /* function has return value */
1176 for (call = hte->h_calls; call != NULL; call = call->f_nxt) {
1177 used |= call->f_rused;
1178 ignored |= !call->f_rused && !call->f_rdisc;
1181 * XXX as soon as we are able to disable single warnings
1182 * the following dependencies from hflag should be removed.
1183 * but for now I do'nt want to be botherd by this warnings
1184 * which are almost always useless.
1186 if (!used && ignored) {
1188 /* %s returns value which is always ignored */
1189 msg(8, hte->h_name);
1190 } else if (used && ignored) {
1192 /* %s returns value which is sometimes ign. */
1193 msg(9, hte->h_name);
1196 /* function has no return value */
1197 for (call = hte->h_calls; call != NULL; call = call->f_nxt) {
1199 /* %s value is used( %s ), but none ret. */
1200 msg(10, hte->h_name, mkpos(&call->f_pos));
1206 * Print warnings for inconsistent argument declarations.
1209 chkadecl(hte, def, decl)
1213 /* LINTED (automatic hides external declaration: warn) */
1214 int osdef, eq, warn, n;
1216 type_t **ap1, **ap2, *tp1, *tp2;
1222 osdef = def->s_osdef;
1224 } else if (decl != NULL && TP(decl->s_type)->t_proto) {
1229 if (TP(sym1->s_type)->t_tspec != FUNC)
1233 * XXX Prototypes should also be compared with old style function
1237 for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
1238 if (sym == sym1 || !TP(sym->s_type)->t_proto)
1240 ap1 = TP(sym1->s_type)->t_args;
1241 ap2 = TP(sym->s_type)->t_args;
1243 while (*ap1 != NULL && *ap2 != NULL) {
1245 eq = eqtype(*ap1, *ap2, 1, osdef, 0, &warn);
1247 pos1 = xstrdup(mkpos(&sym1->s_pos));
1248 pos2 = mkpos(&sym->s_pos);
1249 /* %s, arg %d declared inconsistently ... */
1250 msg(11, hte->h_name, n + 1, pos1, pos2);
1258 tp1 = TP(sym1->s_type);
1259 tp2 = TP(sym->s_type);
1260 if (tp1->t_vararg == tp2->t_vararg)
1262 if (tp2->t_vararg &&
1263 sym1->s_va && sym1->s_nva == n && !sflag) {
1267 /* %s: variable # of args declared\t%s :: %s */
1268 pos1 = xstrdup(mkpos(&sym1->s_pos));
1269 msg(12, hte->h_name, pos1, mkpos(&sym->s_pos));
1276 * Check compatibility of two types. Returns 1 if types are compatible,
1279 * ignqual if set, ignore qualifiers of outhermost type; used for
1280 * function arguments
1281 * promote if set, promote left type before comparision; used for
1282 * comparisions of arguments with parameters of old style
1284 * asgn left indirected type must have at least the same qualifiers
1285 * like right indirected type (for assignments and function
1287 * *warn set to 1 if an old style declaration was compared with
1288 * an incompatible prototype declaration
1291 eqtype(tp1, tp2, ignqual, promot, asgn, warn)
1293 int ignqual, promot, asgn, *warn;
1301 while (tp1 != NULL && tp2 != NULL) {
1307 } else if (t == CHAR || t == SCHAR) {
1309 } else if (t == UCHAR) {
1310 t = tflag ? UINT : INT;
1311 } else if (t == SHORT) {
1313 } else if (t == USHORT) {
1315 t = INT_MAX < USHRT_MAX || tflag ? UINT : INT;
1319 if (asgn && to == PTR) {
1320 if (indir == 1 && (t == VOID || tp2->t_tspec == VOID))
1324 if (t != tp2->t_tspec) {
1326 * Give pointer to types which differ only in
1327 * signedness a chance if not sflag and not hflag.
1329 if (sflag || hflag || to != PTR)
1331 if (styp(t) != styp(tp2->t_tspec))
1335 if (tp1->t_isenum && tp2->t_isenum) {
1336 if (tp1->t_istag && tp2->t_istag) {
1337 return (tp1->t_tag == tp2->t_tag);
1338 } else if (tp1->t_istynam && tp2->t_istynam) {
1339 return (tp1->t_tynam == tp2->t_tynam);
1346 * XXX Handle combinations of enum and int if eflag is set.
1347 * But note: enum and 0 should be allowed.
1350 if (asgn && indir == 1) {
1351 if (!tp1->t_const && tp2->t_const)
1353 if (!tp1->t_volatile && tp2->t_volatile)
1355 } else if (!ignqual && !tflag) {
1356 if (tp1->t_const != tp2->t_const)
1358 if (tp1->t_const != tp2->t_const)
1362 if (t == STRUCT || t == UNION) {
1363 if (tp1->t_istag && tp2->t_istag) {
1364 return (tp1->t_tag == tp2->t_tag);
1365 } else if (tp1->t_istynam && tp2->t_istynam) {
1366 return (tp1->t_tynam == tp2->t_tynam);
1372 if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
1373 if (tp1->t_dim != 0 && tp2->t_dim != 0)
1378 if (tp1->t_proto && tp2->t_proto) {
1379 if (!eqargs(tp1, tp2, warn))
1381 } else if (tp1->t_proto) {
1382 if (!mnoarg(tp1, warn))
1384 } else if (tp2->t_proto) {
1385 if (!mnoarg(tp2, warn))
1392 ignqual = promot = 0;
1398 return (tp1 == tp2);
1402 * Compares arguments of two prototypes
1405 eqargs(tp1, tp2, warn)
1411 if (tp1->t_vararg != tp2->t_vararg)
1417 while (*a1 != NULL && *a2 != NULL) {
1419 if (eqtype(*a1, *a2, 1, 0, 0, warn) == 0)
1427 return (*a1 == *a2);
1431 * mnoarg() (matches functions with no argument type information)
1432 * returns 1 if all parameters of a prototype are compatible with
1433 * and old style function declaration.
1434 * This is the case if following conditions are met:
1435 * 1. the prototype must have a fixed number of parameters
1436 * 2. no parameter is of type float
1437 * 3. no parameter is converted to another type if integer promotion
1448 if (tp->t_vararg && warn != NULL)
1450 for (arg = tp->t_args; *arg != NULL; arg++) {
1451 if ((t = (*arg)->t_tspec) == FLOAT)
1453 if (t == CHAR || t == SCHAR || t == UCHAR)
1455 if (t == SHORT || t == USHORT)