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