Remove __P macros from src/usr.bin and src/usr.sbin.
[dragonfly.git] / usr.bin / xlint / lint2 / chk.c
1 /*      $NetBSD: chk.c,v 1.2 1995/07/03 21:24:42 cgd Exp $      */
2
3 /*
4  * Copyright (c) 1994, 1995 Jochen Pohl
5  * All Rights Reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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
18  *      The NetBSD Project.
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.
21  *
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.
32  *
33  * $NetBSD: chk.c,v 1.2 1995/07/03 21:24:42 cgd Exp $
34  * $DragonFly: src/usr.bin/xlint/lint2/chk.c,v 1.4 2003/11/03 19:31:34 eirikn Exp $
35  */
36
37 #include <stdlib.h>
38 #include <ctype.h>
39 #include <limits.h>
40 #include <err.h>
41
42 #include "lint2.h"
43
44 /* various type information */
45 ttab_t  ttab[NTSPEC];
46
47
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 __P((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 __P((hte_t *,fcall_t *, int,
60                                 const char *, type_t **));
61 static  void    scanflike __P((hte_t *, fcall_t *, int,
62                                const char *, type_t **));
63 static  void    badfmt(hte_t *, fcall_t *);
64 static  void    inconarg(hte_t *, fcall_t *, int);
65 static  void    tofewarg(hte_t *, fcall_t *);
66 static  void    tomanyarg(hte_t *, fcall_t *);
67 static  int     eqtype(type_t *, type_t *, int, int, int, int *);
68 static  int     eqargs(type_t *, type_t *, int *);
69 static  int     mnoarg(type_t *, int *);
70
71
72 void
73 inittyp()
74 {
75         int     i;
76         static  struct {
77                 tspec_t it_tspec;
78                 ttab_t  it_ttab;
79         } ittab[] = {
80                 { SIGNED,   { 0, 0,
81                                       SIGNED, UNSIGN,
82                                       0, 0, 0, 0, 0, "signed" } },
83                 { UNSIGN,   { 0, 0,
84                                       SIGNED, UNSIGN,
85                                       0, 0, 0, 0, 0, "unsigned" } },
86                 { CHAR,     { CHAR_BIT, CHAR_BIT,
87                                       SCHAR, UCHAR,
88                                       1, 0, 0, 1, 1, "char" } },
89                 { SCHAR,    { CHAR_BIT, CHAR_BIT,
90                                       SCHAR, UCHAR,
91                                       1, 0, 0, 1, 1, "signed char" } },
92                 { UCHAR,    { CHAR_BIT, CHAR_BIT,
93                                       SCHAR, UCHAR,
94                                       1, 1, 0, 1, 1, "unsigned char" } },
95                 { SHORT,    { sizeof (short) * CHAR_BIT, 2 * CHAR_BIT,
96                                       SHORT, USHORT,
97                                       1, 0, 0, 1, 1, "short" } },
98                 { USHORT,   { sizeof (u_short) * CHAR_BIT, 2 * CHAR_BIT,
99                                       SHORT, USHORT,
100                                       1, 1, 0, 1, 1, "unsigned short" } },
101                 { INT,      { sizeof (int) * CHAR_BIT, 3 * CHAR_BIT,
102                                       INT, UINT,
103                                       1, 0, 0, 1, 1, "int" } },
104                 { UINT,     { sizeof (u_int) * CHAR_BIT, 3 * CHAR_BIT,
105                                       INT, UINT,
106                                       1, 1, 0, 1, 1, "unsigned int" } },
107                 { LONG,     { sizeof (long) * CHAR_BIT, 4 * CHAR_BIT,
108                                       LONG, ULONG,
109                                       1, 0, 0, 1, 1, "long" } },
110                 { ULONG,    { sizeof (u_long) * CHAR_BIT, 4 * CHAR_BIT,
111                                       LONG, ULONG,
112                                       1, 1, 0, 1, 1, "unsigned long" } },
113                 { QUAD,     { sizeof (quad_t) * CHAR_BIT, 8 * CHAR_BIT,
114                                       QUAD, UQUAD,
115                                       1, 0, 0, 1, 1, "long long" } },
116                 { UQUAD,    { sizeof (u_quad_t) * CHAR_BIT, 8 * CHAR_BIT,
117                                       QUAD, UQUAD,
118                                       1, 1, 0, 1, 1, "unsigned long long" } },
119                 { FLOAT,    { sizeof (float) * CHAR_BIT, 4 * CHAR_BIT,
120                                       FLOAT, FLOAT,
121                                       0, 0, 1, 1, 1, "float" } },
122                 { DOUBLE,   { sizeof (double) * CHAR_BIT, 8 * CHAR_BIT,
123                                       DOUBLE, DOUBLE,
124                                       0, 0, 1, 1, 1, "double" } },
125                 { LDOUBLE,  { sizeof (ldbl_t) * CHAR_BIT, 10 * CHAR_BIT,
126                                       LDOUBLE, LDOUBLE,
127                                       0, 0, 1, 1, 1, "long double" } },
128                 { VOID,     { -1, -1,
129                                       VOID, VOID,
130                                       0, 0, 0, 0, 0, "void" } },
131                 { STRUCT,   { -1, -1,
132                                       STRUCT, STRUCT,
133                                       0, 0, 0, 0, 0, "struct" } },
134                 { UNION,    { -1, -1,
135                                       UNION, UNION,
136                                       0, 0, 0, 0, 0, "union" } },
137                 { ENUM,     { sizeof (int) * CHAR_BIT, 3 * CHAR_BIT,
138                                       ENUM, ENUM,
139                                       1, 0, 0, 1, 1, "enum" } },
140                 { PTR,      { sizeof (void *) * CHAR_BIT, 4 * CHAR_BIT,
141                                       PTR, PTR,
142                                       0, 1, 0, 0, 1, "pointer" } },
143                 { ARRAY,    { -1, -1,
144                                       ARRAY, ARRAY,
145                                       0, 0, 0, 0, 0, "array" } },
146                 { FUNC,     { -1, -1,
147                                       FUNC, FUNC,
148                                       0, 0, 0, 0, 0, "function" } },
149         };
150
151         for (i = 0; i < sizeof (ittab) / sizeof (ittab[0]); i++)
152                 STRUCT_ASSIGN(ttab[ittab[i].it_tspec], ittab[i].it_ttab);
153         if (!pflag) {
154                 for (i = 0; i < NTSPEC; i++)
155                         ttab[i].tt_psz = ttab[i].tt_sz;
156         }
157 }
158
159
160 /*
161  * If there is a symbol named "main", mark it as used.
162  */
163 void
164 mainused()
165 {
166         hte_t   *hte;
167
168         if ((hte = hsearch("main", 0)) != NULL)
169                 hte->h_used = 1;
170 }
171
172 /*
173  * Performs all tests for a single name
174  */
175 void
176 chkname(hte)
177         hte_t   *hte;
178 {
179         sym_t   *sym, *def, *pdecl, *decl;
180
181         if (uflag) {
182                 chkund(hte);
183                 chkdnu(hte);
184                 if (xflag)
185                         chkdnud(hte);
186         }
187         chkmd(hte);
188
189         /* Get definition, prototype declaration and declaration */
190         def = pdecl = decl = NULL;
191         for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
192                 if (def == NULL && (sym->s_def == DEF || sym->s_def == TDEF))
193                         def = sym;
194                 if (pdecl == NULL && sym->s_def == DECL &&
195                     TP(sym->s_type)->t_tspec == FUNC &&
196                     TP(sym->s_type)->t_proto) {
197                         pdecl = sym;
198                 }
199                 if (decl == NULL && sym->s_def == DECL)
200                         decl = sym;
201         }
202
203         /* A prototype is better than an old style declaration. */
204         if (pdecl != NULL)
205                 decl = pdecl;
206
207         chkvtui(hte, def, decl);
208
209         chkvtdi(hte, def, decl);
210
211         chkfaui(hte, def, decl);
212
213         chkrvu(hte, def);
214
215         chkadecl(hte, def, decl);
216 }
217
218 /*
219  * Print a warning if the name has been used, but not defined.
220  */
221 static void
222 chkund(hte)
223         hte_t   *hte;
224 {
225         fcall_t *fcall;
226         usym_t  *usym;
227
228         if (!hte->h_used || hte->h_def)
229                 return;
230
231         if ((fcall = hte->h_calls) != NULL) {
232                 /* %s used( %s ), but not defined */
233                 msg(0, hte->h_name, mkpos(&fcall->f_pos));
234         } else if ((usym = hte->h_usyms) != NULL) {
235                 /* %s used( %s ), but not defined */
236                 msg(0, hte->h_name, mkpos(&usym->u_pos));
237         }
238 }
239
240 /*
241  * Print a warning if the name has been defined, but never used.
242  */
243 static void
244 chkdnu(hte)
245         hte_t   *hte;
246 {
247         sym_t   *sym;
248
249         if (!hte->h_def || hte->h_used)
250                 return;
251
252         for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
253                 if (sym->s_def == DEF || sym->s_def == TDEF) {
254                         /* %s defined( %s ), but never used */
255                         msg(1, hte->h_name, mkpos(&sym->s_pos));
256                         break;
257                 }
258         }
259 }
260
261 /*
262  * Print a warning if the name has been declared, but is not used
263  * or defined.
264  */
265 static void
266 chkdnud(hte)
267         hte_t   *hte;
268 {
269         sym_t   *sym;
270
271         if (hte->h_syms == NULL || hte->h_used || hte->h_def)
272                 return;
273         
274         if ((sym = hte->h_syms) != NULL) {
275                 if (sym->s_def != DECL)
276                         errx(1, "internal error: chkdnud() 1");
277                 /* %s declared( %s ), but never used or defined */
278                 msg(2, hte->h_name, mkpos(&sym->s_pos));
279         }
280 }
281
282 /*
283  * Print a warning if there is more then one definition for
284  * this name.
285  */
286 static void
287 chkmd(hte)
288         hte_t   *hte;
289 {
290         sym_t   *sym, *def1;
291         char    *pos1;
292
293         if (!hte->h_def)
294                 return;
295
296         def1 = NULL;
297         for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
298                 /*
299                  * ANSI C allows tentative definitions of the same name in
300                  * only one compilation unit.
301                  */
302                 if (sym->s_def != DEF && (!sflag || sym->s_def != TDEF))
303                         continue;
304                 if (def1 == NULL) {
305                         def1 = sym;
306                         continue;
307                 }
308                 pos1 = xstrdup(mkpos(&def1->s_pos));
309                 /* %s multiply defined\t%s  ::  %s */
310                 msg(3, hte->h_name, pos1, mkpos(&sym->s_pos));
311                 free(pos1);
312         }
313 }
314
315 /*
316  * Print a warning if the return value assumed for a function call
317  * differs from the return value of the function definition or
318  * function declaration.
319  *
320  * If no definition/declaration can be found, the assumed return values
321  * are always int. So there is no need to compare with another function
322  * call as it's done for function arguments.
323  */
324 static void
325 chkvtui(hte, def, decl)
326         hte_t   *hte;
327         sym_t   *def, *decl;
328 {
329         fcall_t *call;
330         char    *pos1;
331         type_t  *tp1, *tp2;
332         /* LINTED (automatic hides external declaration: warn) */
333         int     warn, eq;
334         tspec_t t1;
335
336         if (hte->h_calls == NULL)
337                 return;
338
339         if (def == NULL)
340                 def = decl;
341         if (def == NULL)
342                 return;
343
344         t1 = (tp1 = TP(def->s_type)->t_subt)->t_tspec;
345         for (call = hte->h_calls; call != NULL; call = call->f_nxt) {
346                 tp2 = TP(call->f_type)->t_subt;
347                 eq = eqtype(tp1, tp2, 1, 0, 0, (warn = 0, &warn));
348                 if (!call->f_rused) {
349                         /* no return value used */
350                         if ((t1 == STRUCT || t1 == UNION) && !eq) {
351                                 /*
352                                  * If a function returns a struct or union it
353                                  * must be declared to return a struct or
354                                  * union, also if the return value is ignored.
355                                  * This is necessary because the caller must
356                                  * allocate stack space for the return value.
357                                  * If it does not, the return value would over-
358                                  * write other data.
359                                  * XXX Following massage may be confusing
360                                  * because it appears also if the return value
361                                  * was declared inconsistently. But this
362                                  * behaviour matches pcc based lint, so it is
363                                  * accepted for now.
364                                  */
365                                 pos1 = xstrdup(mkpos(&def->s_pos));
366                                 /* %s value must be decl. before use %s :: %s */
367                                 msg(17, hte->h_name,
368                                     pos1, mkpos(&call->f_pos));
369                                 free(pos1);
370                         }
371                         continue;
372                 }
373                 if (!eq || (sflag && warn)) {
374                         pos1 = xstrdup(mkpos(&def->s_pos));
375                         /* %s value used inconsistenty\t%s  ::  %s */
376                         msg(4, hte->h_name, pos1, mkpos(&call->f_pos));
377                         free(pos1);
378                 }
379         }
380 }
381
382 /*
383  * Print a warning if a definition/declaration does not match another
384  * definition/declaration of the same name. For functions, only the
385  * types of return values are tested.
386  */
387 static void
388 chkvtdi(hte, def, decl)
389         hte_t   *hte;
390         sym_t   *def, *decl;
391 {
392         sym_t   *sym;
393         type_t  *tp1, *tp2;
394         /* LINTED (automatic hides external declaration: warn) */
395         int     eq, warn;
396         char    *pos1;
397
398         if (def == NULL)
399                 def = decl;
400         if (def == NULL)
401                 return;
402
403         tp1 = TP(def->s_type);
404         for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
405                 if (sym == def)
406                         continue;
407                 tp2 = TP(sym->s_type);
408                 warn = 0;
409                 if (tp1->t_tspec == FUNC && tp2->t_tspec == FUNC) {
410                         eq = eqtype(tp1->t_subt, tp2->t_subt, 1, 0, 0, &warn);
411                 } else {
412                         eq = eqtype(tp1, tp2, 0, 0, 0, &warn);
413                 }
414                 if (!eq || (sflag && warn)) {
415                         pos1 = xstrdup(mkpos(&def->s_pos));
416                         /* %s value declared inconsistently\t%s  ::  %s */
417                         msg(5, hte->h_name, pos1, mkpos(&sym->s_pos));
418                         free(pos1);
419                 }
420         }
421 }
422
423 /*
424  * Print a warning if a function is called with arguments which does
425  * not match the function definition, declaration or another call
426  * of the same function.
427  */
428 static void
429 chkfaui(hte, def, decl)
430         hte_t   *hte;
431         sym_t   *def, *decl;
432 {
433         type_t  *tp1, *tp2, **ap1, **ap2;
434         pos_t   *pos1p;
435         fcall_t *calls, *call, *call1;
436         int     n, as;
437         char    *pos1;
438         arginf_t *ai;
439
440         if ((calls = hte->h_calls) == NULL)
441                 return;
442
443         /*
444          * If we find a function definition, we use this for comparision,
445          * otherwise the first prototype we can find. If there is no
446          * definition or prototype declaration, the first function call
447          * is used.
448          */
449         tp1 = NULL;
450         call1 = NULL;
451         if (def != NULL) {
452                 if ((tp1 = TP(def->s_type))->t_tspec != FUNC)
453                         return;
454                 pos1p = &def->s_pos;
455         } else if (decl != NULL && TP(decl->s_type)->t_proto) {
456                 if ((tp1 = TP(decl->s_type))->t_tspec != FUNC)
457                         return;
458                 pos1p = &decl->s_pos;
459         }
460         if (tp1 == NULL) {
461                 call1 = calls;
462                 calls = calls->f_nxt;
463                 if ((tp1 = TP(call1->f_type))->t_tspec != FUNC)
464                         return;
465                 pos1p = &call1->f_pos;
466         }
467
468         n = 1;
469         for (call = calls; call != NULL; call = call->f_nxt) {
470                 if ((tp2 = TP(call->f_type))->t_tspec != FUNC)
471                         continue;
472                 ap1 = tp1->t_args;
473                 ap2 = tp2->t_args;
474                 n = 0;
475                 while (*ap1 != NULL && *ap2 != NULL) {
476                         if (def != NULL && def->s_va && n >= def->s_nva)
477                                 break;
478                         n++;
479                         chkau(hte, n, def, decl, pos1p, call1, call,
480                               *ap1, *ap2);
481                         ap1++;
482                         ap2++;
483                 }
484                 if (*ap1 == *ap2) {
485                         /* equal # of arguments */
486                 } else if (def != NULL && def->s_va && n >= def->s_nva) {
487                         /*
488                          * function definition with VARARGS; The # of
489                          * arguments of the call must be at least as large
490                          * as the parameter of VARARGS.
491                          */
492                 } else if (*ap2 != NULL && tp1->t_proto && tp1->t_vararg) {
493                         /*
494                          * prototype with ... and function call with
495                          * at least the same # of arguments as declared
496                          * in the prototype.
497                          */
498                 } else {
499                         pos1 = xstrdup(mkpos(pos1p));
500                         /* %s: variable # of args\t%s  ::  %s */
501                         msg(7, hte->h_name, pos1, mkpos(&call->f_pos));
502                         free(pos1);
503                         continue;
504                 }
505
506                 /* perform SCANFLIKE/PRINTFLIKE tests */
507                 if (def == NULL || (!def->s_prfl && !def->s_scfl))
508                         continue;
509                 as = def->s_prfl ? def->s_nprfl : def->s_nscfl;
510                 for (ai = call->f_args; ai != NULL; ai = ai->a_nxt) {
511                         if (ai->a_num == as)
512                                 break;
513                 }
514                 if (ai == NULL || !ai->a_fmt)
515                         continue;
516                 if (def->s_prfl) {
517                         printflike(hte, call, n, ai->a_fstrg, ap2);
518                 } else {
519                         scanflike(hte, call, n, ai->a_fstrg, ap2);
520                 }
521         }
522 }
523
524 /*
525  * Check a single argument in a function call.
526  *
527  *  hte         a pointer to the hash table entry of the function
528  *  n           the number of the argument (1..)
529  *  def         the function definition or NULL
530  *  decl        prototype declaration, old style declaration or NULL
531  *  pos1p       position of definition, declaration of first call
532  *  call1       first call, if both def and decl are old style def/decl
533  *  call        checked call
534  *  arg1        currently checked argument of def/decl/call1
535  *  arg2        currently checked argument of call
536  *
537  */
538 static void
539 chkau(hte, n, def, decl, pos1p, call1, call, arg1, arg2)
540         hte_t   *hte;
541         int     n;
542         sym_t   *def, *decl;
543         pos_t   *pos1p;
544         fcall_t *call1, *call;
545         type_t  *arg1, *arg2;
546 {
547         /* LINTED (automatic hides external declaration: warn) */
548         int     promote, asgn, warn;
549         tspec_t t1, t2;
550         arginf_t *ai, *ai1;
551         char    *pos1;
552
553         /*
554          * If a function definition is available (def != NULL), we compair the
555          * function call (call) with the definition. Otherwise, if a function
556          * definition is available and it is not an old style definition
557          * (decl != NULL && TP(decl->s_type)->t_proto), we compair the call
558          * with this declaration. Otherwise we compair it with the first
559          * call we have found (call1).
560          */
561
562         /* arg1 must be promoted if it stems from an old style definition */
563         promote = def != NULL && def->s_osdef;
564
565         /*
566          * If we compair with a definition or declaration, we must perform
567          * the same checks for qualifiers in indirected types as in
568          * assignments.
569          */
570         asgn = def != NULL || (decl != NULL && TP(decl->s_type)->t_proto);
571
572         warn = 0;
573         if (eqtype(arg1, arg2, 1, promote, asgn, &warn) && (!sflag || !warn))
574                 return;
575
576         /*
577          * Other lint implementations print warnings as soon as the type
578          * of an argument does not match exactly the expected type. The
579          * result are lots of warnings which are really not neccessary.
580          * We print a warning only if
581          *   (0) at least one type is not an interger type and types differ
582          *   (1) hflag is set and types differ
583          *   (2) types differ, except in signedness
584          * If the argument is an integer constant whose msb is not set,
585          * signedness is ignored (e.g. 0 matches both signed and unsigned
586          * int). This is with and without hflag.
587          * If the argument is an integer constant with value 0 and the
588          * expected argument is of type pointer and the width of the
589          * interger constant is the same as the width of the pointer,
590          * no warning is printed.
591          */
592         t1 = arg1->t_tspec;
593         t2 = arg2->t_tspec;
594         if (isityp(t1) && isityp(t2) && !arg1->t_isenum && !arg2->t_isenum) {
595                 if (promote) {
596                         /*
597                          * XXX Here is a problem: Althrough it is possible to
598                          * pass an int where a char/short it expected, there
599                          * may be loss in significant digits. We should first
600                          * check for const arguments if they can be converted
601                          * into the original parameter type.
602                          */
603                         if (t1 == FLOAT) {
604                                 t1 = DOUBLE;
605                         } else if (t1 == CHAR || t1 == SCHAR) {
606                                 t1 = INT;
607                         } else if (t1 == UCHAR) {
608                                 t1 = tflag ? UINT : INT;
609                         } else if (t1 == SHORT) {
610                                 t1 = INT;
611                         } else if (t1 == USHORT) {
612                                 /* CONSTCOND */
613                                 t1 = INT_MAX < USHRT_MAX || tflag ? UINT : INT;
614                         }
615                 }
616
617                 if (styp(t1) == styp(t2)) {
618
619                         /*
620                          * types differ only in signedness; get information
621                          * about arguments
622                          */
623
624                         /*
625                          * treat a definition like a call with variable
626                          * arguments
627                          */
628                         ai1 = call1 != NULL ? call1->f_args : NULL;
629
630                         /*
631                          * if two calls are compared, ai1 is set to the
632                          * information for the n-th argument, if this was
633                          * a constant, otherwise to NULL
634                          */
635                         for ( ; ai1 != NULL; ai1 = ai1->a_nxt) {
636                                 if (ai1->a_num == n)
637                                         break;
638                         }
639                         /*
640                          * ai is set to the information of the n-th arg
641                          * of the (second) call, if this was a constant,
642                          * otherwise to NULL
643                          */
644                         for (ai = call->f_args; ai != NULL; ai = ai->a_nxt) {
645                                 if (ai->a_num == n)
646                                         break;
647                         }
648
649                         if (ai1 == NULL && ai == NULL) {
650                                 /* no constant at all */
651                                 if (!hflag)
652                                         return;
653                         } else if (ai1 == NULL || ai == NULL) {
654                                 /* one constant */
655                                 if (ai == NULL)
656                                         ai = ai1;
657                                 if (ai->a_zero || ai->a_pcon)
658                                         /* same value in signed and unsigned */
659                                         return;
660                                 /* value (not representation) differently */
661                         } else {
662                                 /*
663                                  * two constants, one signed, one unsigned;
664                                  * if the msb of one of the constants is set,
665                                  * the argument is used inconsistently.
666                                  */
667                                 if (!ai1->a_ncon && !ai->a_ncon)
668                                         return;
669                         }
670                 }
671
672         } else if (t1 == PTR && isityp(t2) && psize(t1) == psize(t2)) {
673                 for (ai = call->f_args; ai != NULL; ai = ai->a_nxt) {
674                         if (ai->a_num == n)
675                                 break;
676                 }
677                 if (ai != NULL && ai->a_zero)
678                         return;
679         }
680
681         pos1 = xstrdup(mkpos(pos1p));
682         /* %s, arg %d used inconsistently\t%s  ::  %s */
683         msg(6, hte->h_name, n, pos1, mkpos(&call->f_pos));
684         free(pos1);
685 }
686
687 /*
688  * Compare the types in the NULL-terminated array ap with the format
689  * string fmt.
690  */
691 static void
692 printflike(hte, call, n, fmt, ap)
693         hte_t   *hte;
694         fcall_t *call;
695         int     n;
696         const   char *fmt;
697         type_t  **ap;
698 {
699         const   char *fp;
700         int     fc;
701         int     fwidth, prec, left, sign, space, alt, zero;
702         tspec_t sz, t1, t2;
703         type_t  *tp;
704
705         fp = fmt;
706         fc = *fp++;
707
708         for ( ; ; ) {
709                 if (fc == '\0') {
710                         if (*ap != NULL)
711                                 tomanyarg(hte, call);
712                         break;
713                 }
714                 if (fc != '%') {
715                         badfmt(hte, call);
716                         break;
717                 }
718                 fc = *fp++;
719                 fwidth = prec = left = sign = space = alt = zero = 0;
720                 sz = NOTSPEC;
721
722                 /* Flags */
723                 for ( ; ; ) {
724                         if (fc == '-') {
725                                 if (left)
726                                         break;
727                                 left = 1;
728                         } else if (fc == '+') {
729                                 if (sign)
730                                         break;
731                                 sign = 1;
732                         } else if (fc == ' ') {
733                                 if (space)
734                                         break;
735                                 space = 1;
736                         } else if (fc == '#') {
737                                 if (alt)
738                                         break;
739                                 alt = 1;
740                         } else if (fc == '0') {
741                                 if (zero)
742                                         break;
743                                 zero = 1;
744                         } else {
745                                 break;
746                         }
747                         fc = *fp++;
748                 }
749
750                 /* field width */
751                 if (isdigit(fc)) {
752                         fwidth = 1;
753                         do { fc = *fp++; } while (isdigit(fc)) ;
754                 } else if (fc == '*') {
755                         fwidth = 1;
756                         fc = *fp++;
757                         if ((tp = *ap++) == NULL) {
758                                 tofewarg(hte, call);
759                                 break;
760                         }
761                         n++;
762                         if ((t1 = tp->t_tspec) != INT && (hflag || t1 != UINT))
763                                 inconarg(hte, call, n);
764                 }
765
766                 /* precision */
767                 if (fc == '.') {
768                         fc = *fp++;
769                         prec = 1;
770                         if (isdigit(fc)) {
771                                 do { fc = *fp++; } while (isdigit(fc));
772                         } else if (fc == '*') {
773                                 fc = *fp++;
774                                 if ((tp = *ap++) == NULL) {
775                                         tofewarg(hte, call);
776                                         break;
777                                 }
778                                 n++;
779                                 if (tp->t_tspec != INT)
780                                         inconarg(hte, call, n);
781                         } else {
782                                 badfmt(hte, call);
783                                 break;
784                         }
785                 }
786
787                 if (fc == 'h') {
788                         sz = SHORT;
789                 } else if (fc == 'l') {
790                         sz = LONG;
791                 } else if (fc == 'q') {
792                         sz = QUAD;
793                 } else if (fc == 'L') {
794                         sz = LDOUBLE;
795                 }
796                 if (sz != NOTSPEC)
797                         fc = *fp++;
798
799                 if (fc == '%') {
800                         if (sz != NOTSPEC || left || sign || space ||
801                             alt || zero || prec || fwidth) {
802                                 badfmt(hte, call);
803                         }
804                         fc = *fp++;
805                         continue;
806                 }
807
808                 if (fc == '\0') {
809                         badfmt(hte, call);
810                         break;
811                 }
812
813                 if ((tp = *ap++) == NULL) {
814                         tofewarg(hte, call);
815                         break;
816                 }
817                 n++;
818                 if ((t1 = tp->t_tspec) == PTR)
819                         t2 = tp->t_subt->t_tspec;
820
821                 if (fc == 'd' || fc == 'i') {
822                         if (alt || sz == LDOUBLE) {
823                                 badfmt(hte, call);
824                                 break;
825                         }
826                 int_conv:
827                         if (sz == LONG) {
828                                 if (t1 != LONG && (hflag || t1 != ULONG))
829                                         inconarg(hte, call, n);
830                         } else if (sz == QUAD) {
831                                 if (t1 != QUAD && (hflag || t1 != UQUAD))
832                                         inconarg(hte, call, n);
833                         } else {
834                                 /*
835                                  * SHORT is always promoted to INT, USHORT
836                                  * to INT or UINT.
837                                  */
838                                 if (t1 != INT && (hflag || t1 != UINT))
839                                         inconarg(hte, call, n);
840                         }
841                 } else if (fc == 'o' || fc == 'u' || fc == 'x' || fc == 'X') {
842                         if ((alt && fc == 'u') || sz == LDOUBLE)
843                                 badfmt(hte, call);
844                 uint_conv:
845                         if (sz == LONG) {
846                                 if (t1 != ULONG && (hflag || t1 != LONG))
847                                         inconarg(hte, call, n);
848                         } else if (sz == QUAD) {
849                                 if (t1 != UQUAD && (hflag || t1 != QUAD))
850                                         inconarg(hte, call, n);
851                         } else if (sz == SHORT) {
852                                 /* USHORT was promoted to INT or UINT */
853                                 if (t1 != UINT && t1 != INT)
854                                         inconarg(hte, call, n);
855                         } else {
856                                 if (t1 != UINT && (hflag || t1 != INT))
857                                         inconarg(hte, call, n);
858                         }
859                 } else if (fc == 'D' || fc == 'O' || fc == 'U') {
860                         if ((alt && fc != 'O') || sz != NOTSPEC || !tflag)
861                                 badfmt(hte, call);
862                         sz = LONG;
863                         if (fc == 'D') {
864                                 goto int_conv;
865                         } else {
866                                 goto uint_conv;
867                         }
868                 } else if (fc == 'f' || fc == 'e' || fc == 'E' ||
869                            fc == 'g' || fc == 'G') {
870                         if (sz == NOTSPEC)
871                                 sz = DOUBLE;
872                         if (sz != DOUBLE && sz != LDOUBLE)
873                                 badfmt(hte, call);
874                         if (t1 != sz)
875                                 inconarg(hte, call, n);
876                 } else if (fc == 'c') {
877                         if (sz != NOTSPEC || alt || zero)
878                                 badfmt(hte, call);
879                         if (t1 != INT)
880                                 inconarg(hte, call, n);
881                 } else if (fc == 's') {
882                         if (sz != NOTSPEC || alt || zero)
883                                 badfmt(hte, call);
884                         if (t1 != PTR ||
885                             (t2 != CHAR && t2 != UCHAR && t2 != SCHAR)) {
886                                 inconarg(hte, call, n);
887                         }
888                 } else if (fc == 'p') {
889                         if (fwidth || prec || sz != NOTSPEC || alt || zero)
890                                 badfmt(hte, call);
891                         if (t1 != PTR || (hflag && t2 != VOID))
892                                 inconarg(hte, call, n);
893                 } else if (fc == 'n') {
894                         if (fwidth || prec || alt || zero || sz == LDOUBLE)
895                                 badfmt(hte, call);
896                         if (t1 != PTR) {
897                                 inconarg(hte, call, n);
898                         } else if (sz == LONG) {
899                                 if (t2 != LONG && t2 != ULONG)
900                                         inconarg(hte, call, n);
901                         } else if (sz == SHORT) {
902                                 if (t2 != SHORT && t2 != USHORT)
903                                         inconarg(hte, call, n);
904                         } else {
905                                 if (t2 != INT && t2 != UINT)
906                                         inconarg(hte, call, n);
907                         }
908                 } else {
909                         badfmt(hte, call);
910                         break;
911                 }
912
913                 fc = *fp++;
914         }
915 }
916
917 /*
918  * Compare the types in the NULL-terminated array ap with the format
919  * string fmt.
920  */
921 static void
922 scanflike(hte, call, n, fmt, ap)
923         hte_t   *hte;
924         fcall_t *call;
925         int     n;
926         const   char *fmt;
927         type_t  **ap;
928 {
929         const   char *fp;
930         int     fc;
931         int     noasgn, fwidth;
932         tspec_t sz, t1, t2;
933         type_t  *tp;
934
935         fp = fmt;
936         fc = *fp++;
937
938         for ( ; ; ) {
939                 if (fc == '\0') {
940                         if (*ap != NULL)
941                                 tomanyarg(hte, call);
942                         break;
943                 }
944                 if (fc != '%') {
945                         badfmt(hte, call);
946                         break;
947                 }
948                 fc = *fp++;
949
950                 noasgn = fwidth = 0;
951                 sz = NOTSPEC;
952
953                 if (fc == '*') {
954                         noasgn = 1;
955                         fc = *fp++;
956                 }
957                 
958                 if (isdigit(fc)) {
959                         fwidth = 1;
960                         do { fc = *fp++; } while (isdigit(fc));
961                 }
962
963                 if (fc == 'h') {
964                         sz = SHORT;
965                 } else if (fc == 'l') {
966                         sz = LONG;
967                 } else if (fc == 'q') {
968                         sz = QUAD;
969                 } else if (fc == 'L') {
970                         sz = LDOUBLE;
971                 }
972                 if (sz != NOTSPEC)
973                         fc = *fp++;
974
975                 if (fc == '%') {
976                         if (sz != NOTSPEC || noasgn || fwidth)
977                                 badfmt(hte, call);
978                         fc = *fp++;
979                         continue;
980                 }
981
982                 if (!noasgn) {
983                         if ((tp = *ap++) == NULL) {
984                                 tofewarg(hte, call);
985                                 break;
986                         }
987                         n++;
988                         if ((t1 = tp->t_tspec) == PTR)
989                                 t2 = tp->t_subt->t_tspec;
990                 }
991
992                 if (fc == 'd' || fc == 'i' || fc == 'n') {
993                         if (sz == LDOUBLE)
994                                 badfmt(hte, call);
995                         if (sz != SHORT && sz != LONG && sz != QUAD)
996                                 sz = INT;
997                 conv:
998                         if (!noasgn) {
999                                 if (t1 != PTR) {
1000                                         inconarg(hte, call, n);
1001                                 } else if (t2 != styp(sz)) {
1002                                         inconarg(hte, call, n);
1003                                 } else if (hflag && t2 != sz) {
1004                                         inconarg(hte, call, n);
1005                                 } else if (tp->t_subt->t_const) {
1006                                         inconarg(hte, call, n);
1007                                 }
1008                         }
1009                 } else if (fc == 'o' || fc == 'u' || fc == 'x') {
1010                         if (sz == LDOUBLE)
1011                                 badfmt(hte, call);
1012                         if (sz == SHORT) {
1013                                 sz = USHORT;
1014                         } else if (sz == LONG) {
1015                                 sz = ULONG;
1016                         } else if (sz == QUAD) {
1017                                 sz = UQUAD;
1018                         } else {
1019                                 sz = UINT;
1020                         }
1021                         goto conv;
1022                 } else if (fc == 'D') {
1023                         if (sz != NOTSPEC || !tflag)
1024                                 badfmt(hte, call);
1025                         sz = LONG;
1026                         goto conv;
1027                 } else if (fc == 'O') {
1028                         if (sz != NOTSPEC || !tflag)
1029                                 badfmt(hte, call);
1030                         sz = ULONG;
1031                         goto conv;
1032                 } else if (fc == 'X') {
1033                         /*
1034                          * XXX valid in ANSI C, but in NetBSD's libc imple-
1035                          * mented as "lx". Thats why it should be avoided.
1036                          */
1037                         if (sz != NOTSPEC || !tflag)
1038                                 badfmt(hte, call);
1039                         sz = ULONG;
1040                         goto conv;
1041                 } else if (fc == 'E') {
1042                         /*
1043                          * XXX valid in ANSI C, but in NetBSD's libc imple-
1044                          * mented as "lf". Thats why it should be avoided.
1045                          */
1046                         if (sz != NOTSPEC || !tflag)
1047                                 badfmt(hte, call);
1048                         sz = DOUBLE;
1049                         goto conv;
1050                 } else if (fc == 'F') {
1051                         /* XXX only for backward compatibility */
1052                         if (sz != NOTSPEC || !tflag)
1053                                 badfmt(hte, call);
1054                         sz = DOUBLE;
1055                         goto conv;
1056                 } else if (fc == 'G') {
1057                         /*
1058                          * XXX valid in ANSI C, but in NetBSD's libc not
1059                          * implemented
1060                          */
1061                         if (sz != NOTSPEC && sz != LONG && sz != LDOUBLE)
1062                                 badfmt(hte, call);
1063                         goto fconv;
1064                 } else if (fc == 'e' || fc == 'f' || fc == 'g') {
1065                 fconv:
1066                         if (sz == NOTSPEC) {
1067                                 sz = FLOAT;
1068                         } else if (sz == LONG) {
1069                                 sz = DOUBLE;
1070                         } else if (sz != LDOUBLE) {
1071                                 badfmt(hte, call);
1072                                 sz = FLOAT;
1073                         }
1074                         goto conv;
1075                 } else if (fc == 's' || fc == '[' || fc == 'c') {
1076                         if (sz != NOTSPEC)
1077                                 badfmt(hte, call);
1078                         if (fc == '[') {
1079                                 if ((fc = *fp++) == '-') {
1080                                         badfmt(hte, call);
1081                                         fc = *fp++;
1082                                 }
1083                                 if (fc != ']') {
1084                                         badfmt(hte, call);
1085                                         if (fc == '\0')
1086                                                 break;
1087                                 }
1088                         }
1089                         if (!noasgn) {
1090                                 if (t1 != PTR) {
1091                                         inconarg(hte, call, n);
1092                                 } else if (t2 != CHAR && t2 != UCHAR &&
1093                                            t2 != SCHAR) {
1094                                         inconarg(hte, call, n);
1095                                 }
1096                         }
1097                 } else if (fc == 'p') {
1098                         if (sz != NOTSPEC)
1099                                 badfmt(hte, call);
1100                         if (!noasgn) {
1101                                 if (t1 != PTR || t2 != PTR) {
1102                                         inconarg(hte, call, n);
1103                                 } else if (tp->t_subt->t_subt->t_tspec!=VOID) {
1104                                         if (hflag)
1105                                                 inconarg(hte, call, n);
1106                                 }
1107                         }
1108                 } else {
1109                         badfmt(hte, call);
1110                         break;
1111                 }
1112
1113                 fc = *fp++;
1114         }
1115 }
1116
1117 static void
1118 badfmt(hte, call)
1119         hte_t   *hte;
1120         fcall_t *call;
1121 {
1122         /* %s: malformed format string\t%s */
1123         msg(13, hte->h_name, mkpos(&call->f_pos));
1124 }
1125
1126 static void
1127 inconarg(hte, call, n)
1128         hte_t   *hte;
1129         fcall_t *call;
1130         int     n;
1131 {
1132         /* %s, arg %d inconsistent with format\t%s(%d) */
1133         msg(14, hte->h_name, n, mkpos(&call->f_pos));
1134 }
1135
1136 static void
1137 tofewarg(hte, call)
1138         hte_t   *hte;
1139         fcall_t *call;
1140 {
1141         /* %s: too few args for format  \t%s */
1142         msg(15, hte->h_name, mkpos(&call->f_pos));
1143 }
1144
1145 static void
1146 tomanyarg(hte, call)
1147         hte_t   *hte;
1148         fcall_t *call;
1149 {
1150         /* %s: too many args for format  \t%s */
1151         msg(16, hte->h_name, mkpos(&call->f_pos));
1152 }
1153
1154
1155 /*
1156  * Print warnings for return values which are used, but not returned,
1157  * or return values which are always or sometimes ignored.
1158  */
1159 static void
1160 chkrvu(hte, def)
1161         hte_t   *hte;
1162         sym_t   *def;
1163 {
1164         fcall_t *call;
1165         int     used, ignored;
1166
1167         if (def == NULL)
1168                 /* don't know wheter or not the functions returns a value */
1169                 return;
1170
1171         if (hte->h_calls == NULL)
1172                 return;
1173
1174         if (def->s_rval) {
1175                 /* function has return value */
1176                 used = ignored = 0;
1177                 for (call = hte->h_calls; call != NULL; call = call->f_nxt) {
1178                         used |= call->f_rused;
1179                         ignored |= !call->f_rused && !call->f_rdisc;
1180                 }
1181                 /*
1182                  * XXX as soon as we are able to disable single warnings
1183                  * the following dependencies from hflag should be removed.
1184                  * but for now I do'nt want to be botherd by this warnings
1185                  * which are almost always useless.
1186                  */
1187                 if (!used && ignored) {
1188                         if (hflag)
1189                                 /* %s returns value which is always ignored */
1190                                 msg(8, hte->h_name);
1191                 } else if (used && ignored) {
1192                         if (hflag)
1193                                 /* %s returns value which is sometimes ign. */
1194                                 msg(9, hte->h_name);
1195                 }
1196         } else {
1197                 /* function has no return value */
1198                 for (call = hte->h_calls; call != NULL; call = call->f_nxt) {
1199                         if (call->f_rused)
1200                                 /* %s value is used( %s ), but none ret. */
1201                                 msg(10, hte->h_name, mkpos(&call->f_pos));
1202                 }
1203         }
1204 }
1205
1206 /*
1207  * Print warnings for inconsistent argument declarations.
1208  */
1209 static void
1210 chkadecl(hte, def, decl)
1211         hte_t   *hte;
1212         sym_t   *def, *decl;
1213 {
1214         /* LINTED (automatic hides external declaration: warn) */
1215         int     osdef, eq, warn, n;
1216         sym_t   *sym1, *sym;
1217         type_t  **ap1, **ap2, *tp1, *tp2;
1218         char    *pos1;
1219         const   char *pos2;
1220
1221         osdef = 0;
1222         if (def != NULL) {
1223                 osdef = def->s_osdef;
1224                 sym1 = def;
1225         } else if (decl != NULL && TP(decl->s_type)->t_proto) {
1226                 sym1 = decl;
1227         } else {
1228                 return;
1229         }
1230         if (TP(sym1->s_type)->t_tspec != FUNC)
1231                 return;
1232
1233         /*
1234          * XXX Prototypes should also be compared with old style function
1235          * declarations.
1236          */
1237
1238         for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
1239                 if (sym == sym1 || !TP(sym->s_type)->t_proto)
1240                         continue;
1241                 ap1 = TP(sym1->s_type)->t_args;
1242                 ap2 = TP(sym->s_type)->t_args;
1243                 n = 0;
1244                 while (*ap1 != NULL && *ap2 != NULL) {
1245                         warn = 0;
1246                         eq = eqtype(*ap1, *ap2, 1, osdef, 0, &warn);
1247                         if (!eq || warn) {
1248                                 pos1 = xstrdup(mkpos(&sym1->s_pos));
1249                                 pos2 = mkpos(&sym->s_pos);
1250                                 /* %s, arg %d declared inconsistently ... */
1251                                 msg(11, hte->h_name, n + 1, pos1, pos2);
1252                                 free(pos1);
1253                         }
1254                         n++;
1255                         ap1++;
1256                         ap2++;
1257                 }
1258                 if (*ap1 == *ap2) {
1259                         tp1 = TP(sym1->s_type);
1260                         tp2 = TP(sym->s_type);
1261                         if (tp1->t_vararg == tp2->t_vararg)
1262                                 continue;
1263                         if (tp2->t_vararg &&
1264                             sym1->s_va && sym1->s_nva == n && !sflag) {
1265                                 continue;
1266                         }
1267                 }
1268                 /* %s: variable # of args declared\t%s  ::  %s */
1269                 pos1 = xstrdup(mkpos(&sym1->s_pos));
1270                 msg(12, hte->h_name, pos1, mkpos(&sym->s_pos));
1271                 free(pos1);
1272         }
1273 }
1274
1275
1276 /*
1277  * Check compatibility of two types. Returns 1 if types are compatible,
1278  * otherwise 0.
1279  *
1280  * ignqual      if set, ignore qualifiers of outhermost type; used for
1281  *              function arguments
1282  * promote      if set, promote left type before comparision; used for
1283  *              comparisions of arguments with parameters of old style
1284  *              definitions
1285  * asgn         left indirected type must have at least the same qualifiers
1286  *              like right indirected type (for assignments and function
1287  *              arguments)
1288  * *warn        set to 1 if an old style declaration was compared with
1289  *              an incompatible prototype declaration
1290  */
1291 static int
1292 eqtype(tp1, tp2, ignqual, promot, asgn, warn)
1293         type_t  *tp1, *tp2;
1294         int     ignqual, promot, asgn, *warn;
1295 {
1296         tspec_t t, to;
1297         int     indir;
1298
1299         to = NOTSPEC;
1300         indir = 0;
1301
1302         while (tp1 != NULL && tp2 != NULL) {
1303
1304                 t = tp1->t_tspec;
1305                 if (promot) {
1306                         if (t == FLOAT) {
1307                                 t = DOUBLE;
1308                         } else if (t == CHAR || t == SCHAR) {
1309                                 t = INT;
1310                         } else if (t == UCHAR) {
1311                                 t = tflag ? UINT : INT;
1312                         } else if (t == SHORT) {
1313                                 t = INT;
1314                         } else if (t == USHORT) {
1315                                 /* CONSTCOND */
1316                                 t = INT_MAX < USHRT_MAX || tflag ? UINT : INT;
1317                         }
1318                 }
1319
1320                 if (asgn && to == PTR) {
1321                         if (indir == 1 && (t == VOID || tp2->t_tspec == VOID))
1322                                 return (1);
1323                 }
1324                 
1325                 if (t != tp2->t_tspec) {
1326                         /*
1327                          * Give pointer to types which differ only in
1328                          * signedness a chance if not sflag and not hflag.
1329                          */
1330                         if (sflag || hflag || to != PTR)
1331                                 return (0);
1332                         if (styp(t) != styp(tp2->t_tspec))
1333                                 return (0);
1334                 }
1335
1336                 if (tp1->t_isenum && tp2->t_isenum) {
1337                         if (tp1->t_istag && tp2->t_istag) {
1338                                 return (tp1->t_tag == tp2->t_tag);
1339                         } else if (tp1->t_istynam && tp2->t_istynam) {
1340                                 return (tp1->t_tynam == tp2->t_tynam);
1341                         } else {
1342                                 return (0);
1343                         }
1344                 }
1345
1346                 /*
1347                  * XXX Handle combinations of enum and int if eflag is set.
1348                  * But note: enum and 0 should be allowed.
1349                  */
1350
1351                 if (asgn && indir == 1) {
1352                         if (!tp1->t_const && tp2->t_const)
1353                                 return (0);
1354                         if (!tp1->t_volatile && tp2->t_volatile)
1355                                 return (0);
1356                 } else if (!ignqual && !tflag) {
1357                         if (tp1->t_const != tp2->t_const)
1358                                 return (0);
1359                         if (tp1->t_const != tp2->t_const)
1360                                 return (0);
1361                 }
1362
1363                 if (t == STRUCT || t == UNION) {
1364                         if (tp1->t_istag && tp2->t_istag) {
1365                                 return (tp1->t_tag == tp2->t_tag);
1366                         } else if (tp1->t_istynam && tp2->t_istynam) {
1367                                 return (tp1->t_tynam == tp2->t_tynam);
1368                         } else {
1369                                 return (0);
1370                         }
1371                 }
1372
1373                 if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
1374                         if (tp1->t_dim != 0 && tp2->t_dim != 0)
1375                                 return (0);
1376                 }
1377
1378                 if (t == FUNC) {
1379                         if (tp1->t_proto && tp2->t_proto) {
1380                                 if (!eqargs(tp1, tp2, warn))
1381                                         return (0);
1382                         } else if (tp1->t_proto) {
1383                                 if (!mnoarg(tp1, warn))
1384                                         return (0);
1385                         } else if (tp2->t_proto) {
1386                                 if (!mnoarg(tp2, warn))
1387                                         return (0);
1388                         }
1389                 }
1390
1391                 tp1 = tp1->t_subt;
1392                 tp2 = tp2->t_subt;
1393                 ignqual = promot = 0;
1394                 to = t;
1395                 indir++;
1396
1397         }
1398
1399         return (tp1 == tp2);
1400 }
1401
1402 /*
1403  * Compares arguments of two prototypes
1404  */
1405 static int
1406 eqargs(tp1, tp2, warn)
1407         type_t  *tp1, *tp2;
1408         int     *warn;
1409 {
1410         type_t  **a1, **a2;
1411
1412         if (tp1->t_vararg != tp2->t_vararg)
1413                 return (0);
1414
1415         a1 = tp1->t_args;
1416         a2 = tp2->t_args;
1417
1418         while (*a1 != NULL && *a2 != NULL) {
1419
1420                 if (eqtype(*a1, *a2, 1, 0, 0, warn) == 0)
1421                         return (0);
1422
1423                 a1++;
1424                 a2++;
1425
1426         }
1427
1428         return (*a1 == *a2);
1429 }
1430
1431 /*
1432  * mnoarg() (matches functions with no argument type information)
1433  * returns 1 if all parameters of a prototype are compatible with
1434  * and old style function declaration.
1435  * This is the case if following conditions are met:
1436  *      1. the prototype must have a fixed number of parameters
1437  *      2. no parameter is of type float
1438  *      3. no parameter is converted to another type if integer promotion
1439  *         is applied on it
1440  */
1441 static int
1442 mnoarg(tp, warn)
1443         type_t  *tp;
1444         int     *warn;
1445 {
1446         type_t  **arg;
1447         tspec_t t;
1448
1449         if (tp->t_vararg && warn != NULL)
1450                 *warn = 1;
1451         for (arg = tp->t_args; *arg != NULL; arg++) {
1452                 if ((t = (*arg)->t_tspec) == FLOAT)
1453                         return (0);
1454                 if (t == CHAR || t == SCHAR || t == UCHAR)
1455                         return (0);
1456                 if (t == SHORT || t == USHORT)
1457                         return (0);
1458         }
1459         return (1);
1460 }
1461