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