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