1 /* $NetBSD: chk.c,v 1.15 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: chk.c,v 1.15 2002/01/21 19:49:52 tv Exp $");
39 __FBSDID("$FreeBSD$");
48 static void chkund(hte_t *);
49 static void chkdnu(hte_t *);
50 static void chkdnud(hte_t *);
51 static void chkmd(hte_t *);
52 static void chkvtui(hte_t *, sym_t *, sym_t *);
53 static void chkvtdi(hte_t *, sym_t *, sym_t *);
54 static void chkfaui(hte_t *, sym_t *, sym_t *);
55 static void chkau(hte_t *, int, sym_t *, sym_t *, pos_t *,
56 fcall_t *, fcall_t *, type_t *, type_t *);
57 static void chkrvu(hte_t *, sym_t *);
58 static void chkadecl(hte_t *, sym_t *, sym_t *);
59 static void printflike(hte_t *,fcall_t *, int, const char *, type_t **);
60 static void scanflike(hte_t *, fcall_t *, int, const char *, type_t **);
61 static void badfmt(hte_t *, fcall_t *);
62 static void inconarg(hte_t *, fcall_t *, int);
63 static void tofewarg(hte_t *, fcall_t *);
64 static void tomanyarg(hte_t *, fcall_t *);
65 static int eqtype(type_t *, type_t *, int, int, int, int *);
66 static int eqargs(type_t *, type_t *, int *);
67 static int mnoarg(type_t *, int *);
71 * If there is a symbol named "main", mark it as used.
78 if ((hte = hsearch("main", 0)) != NULL)
83 * Performs all tests for a single name
88 sym_t *sym, *def, *pdecl, *decl;
98 /* Get definition, prototype declaration and declaration */
99 def = pdecl = decl = NULL;
100 for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
101 if (def == NULL && (sym->s_def == DEF || sym->s_def == TDEF))
103 if (pdecl == NULL && sym->s_def == DECL &&
104 TP(sym->s_type)->t_tspec == FUNC &&
105 TP(sym->s_type)->t_proto) {
108 if (decl == NULL && sym->s_def == DECL)
112 /* A prototype is better than an old style declaration. */
116 chkvtui(hte, def, decl);
118 chkvtdi(hte, def, decl);
120 chkfaui(hte, def, decl);
124 chkadecl(hte, def, decl);
128 * Print a warning if the name has been used, but not defined.
136 if (!hte->h_used || hte->h_def)
139 if ((fcall = hte->h_calls) != NULL) {
140 /* %s used( %s ), but not defined */
141 msg(0, hte->h_name, mkpos(&fcall->f_pos));
142 } else if ((usym = hte->h_usyms) != NULL) {
143 /* %s used( %s ), but not defined */
144 msg(0, hte->h_name, mkpos(&usym->u_pos));
149 * Print a warning if the name has been defined, but never used.
156 if (!hte->h_def || hte->h_used)
159 for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
160 if (sym->s_def == DEF || sym->s_def == TDEF) {
161 /* %s defined( %s ), but never used */
162 msg(1, hte->h_name, mkpos(&sym->s_pos));
169 * Print a warning if the variable has been declared, but is not used
177 if (hte->h_syms == NULL || hte->h_used || hte->h_def)
181 if (TP(sym->s_type)->t_tspec == FUNC)
184 if (sym->s_def != DECL)
185 errx(1, "internal error: chkdnud() 1");
186 /* %s declared( %s ), but never used or defined */
187 msg(2, hte->h_name, mkpos(&sym->s_pos));
191 * Print a warning if there is more than one definition for
204 for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
206 * ANSI C allows tentative definitions of the same name in
207 * only one compilation unit.
209 if (sym->s_def != DEF && (!sflag || sym->s_def != TDEF))
215 pos1 = xstrdup(mkpos(&def1->s_pos));
216 /* %s multiply defined\t%s :: %s */
217 msg(3, hte->h_name, pos1, mkpos(&sym->s_pos));
223 * Print a warning if the return value assumed for a function call
224 * differs from the return value of the function definition or
225 * function declaration.
227 * If no definition/declaration can be found, the assumed return values
228 * are always int. So there is no need to compare with another function
229 * call as it's done for function arguments.
232 chkvtui(hte_t *hte, sym_t *def, sym_t *decl)
237 /* LINTED (automatic hides external declaration: warn) */
241 if (hte->h_calls == NULL)
249 t1 = (tp1 = TP(def->s_type)->t_subt)->t_tspec;
250 for (call = hte->h_calls; call != NULL; call = call->f_nxt) {
251 tp2 = TP(call->f_type)->t_subt;
252 eq = eqtype(tp1, tp2, 1, 0, 0, (warn = 0, &warn));
253 if (!call->f_rused) {
254 /* no return value used */
255 if ((t1 == STRUCT || t1 == UNION) && !eq) {
257 * If a function returns a struct or union it
258 * must be declared to return a struct or
259 * union, also if the return value is ignored.
260 * This is necessary because the caller must
261 * allocate stack space for the return value.
262 * If it does not, the return value would over-
264 * XXX Following massage may be confusing
265 * because it appears also if the return value
266 * was declared inconsistently. But this
267 * behaviour matches pcc based lint, so it is
270 pos1 = xstrdup(mkpos(&def->s_pos));
271 /* %s value must be decl. before use %s :: %s */
273 pos1, mkpos(&call->f_pos));
278 if (!eq || (sflag && warn)) {
279 pos1 = xstrdup(mkpos(&def->s_pos));
280 /* %s value used inconsistenty\t%s :: %s */
281 msg(4, hte->h_name, pos1, mkpos(&call->f_pos));
288 * Print a warning if a definition/declaration does not match another
289 * definition/declaration of the same name. For functions, only the
290 * types of return values are tested.
293 chkvtdi(hte_t *hte, sym_t *def, sym_t *decl)
297 /* LINTED (automatic hides external declaration: warn) */
306 tp1 = TP(def->s_type);
307 for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
310 tp2 = TP(sym->s_type);
312 if (tp1->t_tspec == FUNC && tp2->t_tspec == FUNC) {
313 eq = eqtype(tp1->t_subt, tp2->t_subt, 1, 0, 0, &warn);
315 eq = eqtype(tp1, tp2, 0, 0, 0, &warn);
317 if (!eq || (sflag && warn)) {
318 pos1 = xstrdup(mkpos(&def->s_pos));
319 /* %s value declared inconsistently\t%s :: %s */
320 msg(5, hte->h_name, pos1, mkpos(&sym->s_pos));
327 * Print a warning if a function is called with arguments which does
328 * not match the function definition, declaration or another call
329 * of the same function.
332 chkfaui(hte_t *hte, sym_t *def, sym_t *decl)
334 type_t *tp1, *tp2, **ap1, **ap2;
336 fcall_t *calls, *call, *call1;
341 if ((calls = hte->h_calls) == NULL)
345 * If we find a function definition, we use this for comparison,
346 * otherwise the first prototype we can find. If there is no
347 * definition or prototype declaration, the first function call
353 if ((tp1 = TP(def->s_type))->t_tspec != FUNC)
356 } else if (decl != NULL && TP(decl->s_type)->t_proto) {
357 if ((tp1 = TP(decl->s_type))->t_tspec != FUNC)
359 pos1p = &decl->s_pos;
363 calls = calls->f_nxt;
364 if ((tp1 = TP(call1->f_type))->t_tspec != FUNC)
366 pos1p = &call1->f_pos;
370 for (call = calls; call != NULL; call = call->f_nxt) {
371 if ((tp2 = TP(call->f_type))->t_tspec != FUNC)
376 while (*ap1 != NULL && *ap2 != NULL) {
377 if (def != NULL && def->s_va && n >= def->s_nva)
380 chkau(hte, n, def, decl, pos1p, call1, call,
386 /* equal # of arguments */
387 } else if (def != NULL && def->s_va && n >= def->s_nva) {
389 * function definition with VARARGS; The # of
390 * arguments of the call must be at least as large
391 * as the parameter of VARARGS.
393 } else if (*ap2 != NULL && tp1->t_proto && tp1->t_vararg) {
395 * prototype with ... and function call with
396 * at least the same # of arguments as declared
400 pos1 = xstrdup(mkpos(pos1p));
401 /* %s: variable # of args\t%s :: %s */
402 msg(7, hte->h_name, pos1, mkpos(&call->f_pos));
407 /* perform SCANFLIKE/PRINTFLIKE tests */
408 if (def == NULL || (!def->s_prfl && !def->s_scfl))
410 as = def->s_prfl ? def->s_nprfl : def->s_nscfl;
411 for (ai = call->f_args; ai != NULL; ai = ai->a_nxt) {
415 if (ai == NULL || !ai->a_fmt)
418 printflike(hte, call, n, ai->a_fstrg, ap2);
420 scanflike(hte, call, n, ai->a_fstrg, ap2);
426 * Check a single argument in a function call.
428 * hte a pointer to the hash table entry of the function
429 * n the number of the argument (1..)
430 * def the function definition or NULL
431 * decl prototype declaration, old style declaration or NULL
432 * pos1p position of definition, declaration of first call
433 * call1 first call, if both def and decl are old style def/decl
435 * arg1 currently checked argument of def/decl/call1
436 * arg2 currently checked argument of call
440 chkau(hte_t *hte, int n, sym_t *def, sym_t *decl, pos_t *pos1p,
441 fcall_t *call1, fcall_t *call, type_t *arg1, type_t *arg2)
443 /* LINTED (automatic hides external declaration: warn) */
444 int promote, asgn, warn;
450 * If a function definition is available (def != NULL), we compair the
451 * function call (call) with the definition. Otherwise, if a function
452 * definition is available and it is not an old style definition
453 * (decl != NULL && TP(decl->s_type)->t_proto), we compair the call
454 * with this declaration. Otherwise we compair it with the first
455 * call we have found (call1).
458 /* arg1 must be promoted if it stems from an old style definition */
459 promote = def != NULL && def->s_osdef;
462 * If we compair with a definition or declaration, we must perform
463 * the same checks for qualifiers in indirected types as in
466 asgn = def != NULL || (decl != NULL && TP(decl->s_type)->t_proto);
469 if (eqtype(arg1, arg2, 1, promote, asgn, &warn) && (!sflag || !warn))
473 * Other lint implementations print warnings as soon as the type
474 * of an argument does not match exactly the expected type. The
475 * result are lots of warnings which are really not necessary.
476 * We print a warning only if
477 * (0) at least one type is not an interger type and types differ
478 * (1) hflag is set and types differ
479 * (2) types differ, except in signedness
480 * If the argument is an integer constant whose msb is not set,
481 * signedness is ignored (e.g. 0 matches both signed and unsigned
482 * int). This is with and without hflag.
483 * If the argument is an integer constant with value 0 and the
484 * expected argument is of type pointer and the width of the
485 * interger constant is the same as the width of the pointer,
486 * no warning is printed.
490 if (isityp(t1) && isityp(t2) && !arg1->t_isenum && !arg2->t_isenum) {
493 * XXX Here is a problem: Althrough it is possible to
494 * pass an int where a char/short it expected, there
495 * may be loss in significant digits. We should first
496 * check for const arguments if they can be converted
497 * into the original parameter type.
501 } else if (t1 == CHAR || t1 == SCHAR) {
503 } else if (t1 == UCHAR) {
504 t1 = tflag ? UINT : INT;
505 } else if (t1 == SHORT) {
507 } else if (t1 == USHORT) {
509 t1 = INT_MAX < USHRT_MAX || tflag ? UINT : INT;
513 if (styp(t1) == styp(t2)) {
516 * types differ only in signedness; get information
521 * treat a definition like a call with variable
524 ai1 = call1 != NULL ? call1->f_args : NULL;
527 * if two calls are compared, ai1 is set to the
528 * information for the n-th argument, if this was
529 * a constant, otherwise to NULL
531 for ( ; ai1 != NULL; ai1 = ai1->a_nxt) {
536 * ai is set to the information of the n-th arg
537 * of the (second) call, if this was a constant,
540 for (ai = call->f_args; ai != NULL; ai = ai->a_nxt) {
545 if (ai1 == NULL && ai == NULL) {
546 /* no constant at all */
549 } else if (ai1 == NULL || ai == NULL) {
553 if (ai->a_zero || ai->a_pcon)
554 /* same value in signed and unsigned */
556 /* value (not representation) differently */
559 * two constants, one signed, one unsigned;
560 * if the msb of one of the constants is set,
561 * the argument is used inconsistently.
563 if (!ai1->a_ncon && !ai->a_ncon)
568 } else if (t1 == PTR && isityp(t2)) {
569 for (ai = call->f_args; ai != NULL; ai = ai->a_nxt) {
574 * Vendor implementations of lint (e.g. HP-UX, Digital UNIX)
575 * don't care about the size of the integer argument,
576 * only whether or not it is zero. We do the same.
578 if (ai != NULL && ai->a_zero)
582 pos1 = xstrdup(mkpos(pos1p));
583 /* %s, arg %d used inconsistently\t%s :: %s */
584 msg(6, hte->h_name, n, pos1, mkpos(&call->f_pos));
589 * Compare the types in the NULL-terminated array ap with the format
593 printflike(hte_t *hte, fcall_t *call, int n, const char *fmt, type_t **ap)
597 int fwidth, prec, left, sign, space, alt, zero;
598 tspec_t sz, t1, t2 = NOTSPEC;
607 tomanyarg(hte, call);
615 fwidth = prec = left = sign = space = alt = zero = 0;
624 } else if (fc == '+') {
628 } else if (fc == ' ') {
632 } else if (fc == '#') {
636 } else if (fc == '0') {
649 do { fc = *fp++; } while (isdigit(fc)) ;
650 } else if (fc == '*') {
653 if ((tp = *ap++) == NULL) {
658 if ((t1 = tp->t_tspec) != INT && (hflag || t1 != UINT))
659 inconarg(hte, call, n);
667 do { fc = *fp++; } while (isdigit(fc));
668 } else if (fc == '*') {
670 if ((tp = *ap++) == NULL) {
675 if (tp->t_tspec != INT)
676 inconarg(hte, call, n);
685 } else if (fc == 'l') {
687 } else if (fc == 'q') {
689 } else if (fc == 'L') {
696 if (sz != NOTSPEC || left || sign || space ||
697 alt || zero || prec || fwidth) {
709 if ((tp = *ap++) == NULL) {
714 if ((t1 = tp->t_tspec) == PTR)
715 t2 = tp->t_subt->t_tspec;
717 if (fc == 'd' || fc == 'i') {
718 if (alt || sz == LDOUBLE) {
724 if (t1 != LONG && (hflag || t1 != ULONG))
725 inconarg(hte, call, n);
726 } else if (sz == QUAD) {
727 if (t1 != QUAD && (hflag || t1 != UQUAD))
728 inconarg(hte, call, n);
731 * SHORT is always promoted to INT, USHORT
734 if (t1 != INT && (hflag || t1 != UINT))
735 inconarg(hte, call, n);
737 } else if (fc == 'o' || fc == 'u' || fc == 'x' || fc == 'X') {
738 if ((alt && fc == 'u') || sz == LDOUBLE)
742 if (t1 != ULONG && (hflag || t1 != LONG))
743 inconarg(hte, call, n);
744 } else if (sz == QUAD) {
745 if (t1 != UQUAD && (hflag || t1 != QUAD))
746 inconarg(hte, call, n);
747 } else if (sz == SHORT) {
748 /* USHORT was promoted to INT or UINT */
749 if (t1 != UINT && t1 != INT)
750 inconarg(hte, call, n);
752 if (t1 != UINT && (hflag || t1 != INT))
753 inconarg(hte, call, n);
755 } else if (fc == 'D' || fc == 'O' || fc == 'U') {
756 if ((alt && fc != 'O') || sz != NOTSPEC || !tflag)
764 } else if (fc == 'f' || fc == 'e' || fc == 'E' ||
765 fc == 'g' || fc == 'G') {
768 if (sz != DOUBLE && sz != LDOUBLE)
771 inconarg(hte, call, n);
772 } else if (fc == 'c') {
773 if (sz != NOTSPEC || alt || zero)
776 inconarg(hte, call, n);
777 } else if (fc == 's') {
778 if (sz != NOTSPEC || alt || zero)
781 (t2 != CHAR && t2 != UCHAR && t2 != SCHAR)) {
782 inconarg(hte, call, n);
784 } else if (fc == 'p') {
785 if (fwidth || prec || sz != NOTSPEC || alt || zero)
787 if (t1 != PTR || (hflag && t2 != VOID))
788 inconarg(hte, call, n);
789 } else if (fc == 'n') {
790 if (fwidth || prec || alt || zero || sz == LDOUBLE)
793 inconarg(hte, call, n);
794 } else if (sz == LONG) {
795 if (t2 != LONG && t2 != ULONG)
796 inconarg(hte, call, n);
797 } else if (sz == SHORT) {
798 if (t2 != SHORT && t2 != USHORT)
799 inconarg(hte, call, n);
801 if (t2 != INT && t2 != UINT)
802 inconarg(hte, call, n);
814 * Compare the types in the NULL-terminated array ap with the format
818 scanflike(hte_t *hte, fcall_t *call, int n, const char *fmt, type_t **ap)
823 tspec_t sz, t1 = NOTSPEC, t2 = NOTSPEC;
832 tomanyarg(hte, call);
851 do { fc = *fp++; } while (isdigit(fc));
856 } else if (fc == 'l') {
858 } else if (fc == 'q') {
860 } else if (fc == 'L') {
867 if (sz != NOTSPEC || noasgn || fwidth)
874 if ((tp = *ap++) == NULL) {
879 if ((t1 = tp->t_tspec) == PTR)
880 t2 = tp->t_subt->t_tspec;
883 if (fc == 'd' || fc == 'i' || fc == 'n') {
886 if (sz != SHORT && sz != LONG && sz != QUAD)
891 inconarg(hte, call, n);
892 } else if (t2 != styp(sz)) {
893 inconarg(hte, call, n);
894 } else if (hflag && t2 != sz) {
895 inconarg(hte, call, n);
896 } else if (tp->t_subt->t_const) {
897 inconarg(hte, call, n);
900 } else if (fc == 'o' || fc == 'u' || fc == 'x') {
905 } else if (sz == LONG) {
907 } else if (sz == QUAD) {
913 } else if (fc == 'D') {
914 if (sz != NOTSPEC || !tflag)
918 } else if (fc == 'O') {
919 if (sz != NOTSPEC || !tflag)
923 } else if (fc == 'X') {
925 * XXX valid in ANSI C, but in NetBSD's libc imple-
926 * mented as "lx". Thats why it should be avoided.
928 if (sz != NOTSPEC || !tflag)
932 } else if (fc == 'E') {
934 * XXX valid in ANSI C, but in NetBSD's libc imple-
935 * mented as "lf". Thats why it should be avoided.
937 if (sz != NOTSPEC || !tflag)
941 } else if (fc == 'F') {
942 /* XXX only for backward compatibility */
943 if (sz != NOTSPEC || !tflag)
947 } else if (fc == 'G') {
949 * XXX valid in ANSI C, but in NetBSD's libc not
952 if (sz != NOTSPEC && sz != LONG && sz != LDOUBLE)
955 } else if (fc == 'e' || fc == 'f' || fc == 'g') {
959 } else if (sz == LONG) {
961 } else if (sz != LDOUBLE) {
966 } else if (fc == 's' || fc == '[' || fc == 'c') {
970 if ((fc = *fp++) == '-') {
982 inconarg(hte, call, n);
983 } else if (t2 != CHAR && t2 != UCHAR &&
985 inconarg(hte, call, n);
988 } else if (fc == 'p') {
992 if (t1 != PTR || t2 != PTR) {
993 inconarg(hte, call, n);
994 } else if (tp->t_subt->t_subt->t_tspec!=VOID) {
996 inconarg(hte, call, n);
1009 badfmt(hte_t *hte, fcall_t *call)
1012 /* %s: malformed format string\t%s */
1013 msg(13, hte->h_name, mkpos(&call->f_pos));
1017 inconarg(hte_t *hte, fcall_t *call, int n)
1020 /* %s, arg %d inconsistent with format\t%s(%d) */
1021 msg(14, hte->h_name, n, mkpos(&call->f_pos));
1025 tofewarg(hte_t *hte, fcall_t *call)
1028 /* %s: too few args for format \t%s */
1029 msg(15, hte->h_name, mkpos(&call->f_pos));
1033 tomanyarg(hte_t *hte, fcall_t *call)
1036 /* %s: too many args for format \t%s */
1037 msg(16, hte->h_name, mkpos(&call->f_pos));
1042 * Print warnings for return values which are used, but not returned,
1043 * or return values which are always or sometimes ignored.
1046 chkrvu(hte_t *hte, sym_t *def)
1052 /* don't know wheter or not the functions returns a value */
1055 if (hte->h_calls == NULL)
1059 /* function has return value */
1061 for (call = hte->h_calls; call != NULL; call = call->f_nxt) {
1062 used |= call->f_rused || call->f_rdisc;
1063 ignored |= !call->f_rused && !call->f_rdisc;
1066 * XXX as soon as we are able to disable single warnings
1067 * the following dependencies from hflag should be removed.
1068 * but for now I do'nt want to be botherd by this warnings
1069 * which are almost always useless.
1071 if (!used && ignored) {
1073 /* %s returns value which is always ignored */
1074 msg(8, hte->h_name);
1075 } else if (used && ignored) {
1077 /* %s returns value which is sometimes ign. */
1078 msg(9, hte->h_name);
1081 /* function has no return value */
1082 for (call = hte->h_calls; call != NULL; call = call->f_nxt) {
1084 /* %s value is used( %s ), but none ret. */
1085 msg(10, hte->h_name, mkpos(&call->f_pos));
1091 * Print warnings for inconsistent argument declarations.
1094 chkadecl(hte_t *hte, sym_t *def, sym_t *decl)
1096 /* LINTED (automatic hides external declaration: warn) */
1097 int osdef, eq, warn, n;
1099 type_t **ap1, **ap2, *tp1, *tp2;
1105 osdef = def->s_osdef;
1107 } else if (decl != NULL && TP(decl->s_type)->t_proto) {
1112 if (TP(sym1->s_type)->t_tspec != FUNC)
1116 * XXX Prototypes should also be compared with old style function
1120 for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
1121 if (sym == sym1 || !TP(sym->s_type)->t_proto)
1123 ap1 = TP(sym1->s_type)->t_args;
1124 ap2 = TP(sym->s_type)->t_args;
1126 while (*ap1 != NULL && *ap2 != NULL) {
1128 eq = eqtype(*ap1, *ap2, 1, osdef, 0, &warn);
1130 pos1 = xstrdup(mkpos(&sym1->s_pos));
1131 pos2 = mkpos(&sym->s_pos);
1132 /* %s, arg %d declared inconsistently ... */
1133 msg(11, hte->h_name, n + 1, pos1, pos2);
1141 tp1 = TP(sym1->s_type);
1142 tp2 = TP(sym->s_type);
1143 if (tp1->t_vararg == tp2->t_vararg)
1145 if (tp2->t_vararg &&
1146 sym1->s_va && sym1->s_nva == n && !sflag) {
1150 /* %s: variable # of args declared\t%s :: %s */
1151 pos1 = xstrdup(mkpos(&sym1->s_pos));
1152 msg(12, hte->h_name, pos1, mkpos(&sym->s_pos));
1159 * Check compatibility of two types. Returns 1 if types are compatible,
1162 * ignqual if set, ignore qualifiers of outhermost type; used for
1163 * function arguments
1164 * promote if set, promote left type before comparison; used for
1165 * comparisons of arguments with parameters of old style
1167 * asgn left indirected type must have at least the same qualifiers
1168 * like right indirected type (for assignments and function
1170 * *warn set to 1 if an old style declaration was compared with
1171 * an incompatible prototype declaration
1174 eqtype(type_t *tp1, type_t *tp2, int ignqual, int promot, int asgn, int *warn)
1182 while (tp1 != NULL && tp2 != NULL) {
1188 } else if (t == CHAR || t == SCHAR) {
1190 } else if (t == UCHAR) {
1191 t = tflag ? UINT : INT;
1192 } else if (t == SHORT) {
1194 } else if (t == USHORT) {
1196 t = INT_MAX < USHRT_MAX || tflag ? UINT : INT;
1200 if (asgn && to == PTR) {
1201 if (indir == 1 && (t == VOID || tp2->t_tspec == VOID))
1205 if (t != tp2->t_tspec) {
1207 * Give pointer to types which differ only in
1208 * signedness a chance if not sflag and not hflag.
1210 if (sflag || hflag || to != PTR)
1212 if (styp(t) != styp(tp2->t_tspec))
1216 if (tp1->t_isenum && tp2->t_isenum) {
1217 if (tp1->t_istag && tp2->t_istag) {
1218 return (tp1->t_tag == tp2->t_tag);
1219 } else if (tp1->t_istynam && tp2->t_istynam) {
1220 return (tp1->t_tynam == tp2->t_tynam);
1221 } else if (tp1->t_isuniqpos && tp2->t_isuniqpos) {
1222 return (tp1->t_uniqpos.p_line ==
1223 tp2->t_uniqpos.p_line &&
1224 tp1->t_uniqpos.p_file ==
1225 tp2->t_uniqpos.p_file &&
1226 tp1->t_uniqpos.p_uniq ==
1227 tp2->t_uniqpos.p_uniq);
1234 * XXX Handle combinations of enum and int if eflag is set.
1235 * But note: enum and 0 should be allowed.
1238 if (asgn && indir == 1) {
1239 if (!tp1->t_const && tp2->t_const)
1241 if (!tp1->t_volatile && tp2->t_volatile)
1243 } else if (!ignqual && !tflag) {
1244 if (tp1->t_const != tp2->t_const)
1246 if (tp1->t_const != tp2->t_const)
1250 if (t == STRUCT || t == UNION) {
1251 if (tp1->t_istag && tp2->t_istag) {
1252 return (tp1->t_tag == tp2->t_tag);
1253 } else if (tp1->t_istynam && tp2->t_istynam) {
1254 return (tp1->t_tynam == tp2->t_tynam);
1255 } else if (tp1->t_isuniqpos && tp2->t_isuniqpos) {
1256 return (tp1->t_uniqpos.p_line ==
1257 tp2->t_uniqpos.p_line &&
1258 tp1->t_uniqpos.p_file ==
1259 tp2->t_uniqpos.p_file &&
1260 tp1->t_uniqpos.p_uniq ==
1261 tp2->t_uniqpos.p_uniq);
1267 if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
1268 if (tp1->t_dim != 0 && tp2->t_dim != 0)
1273 if (tp1->t_proto && tp2->t_proto) {
1274 if (!eqargs(tp1, tp2, warn))
1276 } else if (tp1->t_proto) {
1277 if (!mnoarg(tp1, warn))
1279 } else if (tp2->t_proto) {
1280 if (!mnoarg(tp2, warn))
1287 ignqual = promot = 0;
1293 return (tp1 == tp2);
1297 * Compares arguments of two prototypes
1300 eqargs(type_t *tp1, type_t *tp2, int *warn)
1304 if (tp1->t_vararg != tp2->t_vararg)
1310 while (*a1 != NULL && *a2 != NULL) {
1312 if (eqtype(*a1, *a2, 1, 0, 0, warn) == 0)
1320 return (*a1 == *a2);
1324 * mnoarg() (matches functions with no argument type information)
1325 * returns 1 if all parameters of a prototype are compatible with
1326 * and old style function declaration.
1327 * This is the case if following conditions are met:
1328 * 1. the prototype must have a fixed number of parameters
1329 * 2. no parameter is of type float
1330 * 3. no parameter is converted to another type if integer promotion
1334 mnoarg(type_t *tp, int *warn)
1339 if (tp->t_vararg && warn != NULL)
1341 for (arg = tp->t_args; *arg != NULL; arg++) {
1342 if ((t = (*arg)->t_tspec) == FLOAT)
1344 if (t == CHAR || t == SCHAR || t == UCHAR)
1346 if (t == SHORT || t == USHORT)