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