xref: /netbsd-src/usr.bin/xlint/lint2/chk.c (revision de1dfb1250df962f1ff3a011772cf58e605aed11)
1 /* $NetBSD: chk.c,v 1.17 2004/06/20 22:20:17 jmc 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.17 2004/06/20 22:20:17 jmc 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 
451 	/*
452 	 * If a function definition is available (def != NULL), we compair the
453 	 * function call (call) with the definition. Otherwise, if a function
454 	 * definition is available and it is not an old style definition
455 	 * (decl != NULL && TP(decl->s_type)->t_proto), we compair the call
456 	 * with this declaration. Otherwise we compair it with the first
457 	 * call we have found (call1).
458 	 */
459 
460 	/* arg1 must be promoted if it stems from an old style definition */
461 	promote = def != NULL && def->s_osdef;
462 
463 	/*
464 	 * If we compair with a definition or declaration, we must perform
465 	 * the same checks for qualifiers in indirected types as in
466 	 * assignments.
467 	 */
468 	asgn = def != NULL || (decl != NULL && TP(decl->s_type)->t_proto);
469 
470 	warn = 0;
471 	if (eqtype(arg1, arg2, 1, promote, asgn, &warn) && (!sflag || !warn))
472 		return;
473 
474 	/*
475 	 * Other lint implementations print warnings as soon as the type
476 	 * of an argument does not match exactly the expected type. The
477 	 * result are lots of warnings which are really not necessary.
478 	 * We print a warning only if
479 	 *   (0) at least one type is not an interger type and types differ
480 	 *   (1) hflag is set and types differ
481 	 *   (2) types differ, except in signedness
482 	 * If the argument is an integer constant whose msb is not set,
483 	 * signedness is ignored (e.g. 0 matches both signed and unsigned
484 	 * int). This is with and without hflag.
485 	 * If the argument is an integer constant with value 0 and the
486 	 * expected argument is of type pointer and the width of the
487 	 * interger constant is the same as the width of the pointer,
488 	 * no warning is printed.
489 	 */
490 	t1 = arg1->t_tspec;
491 	t2 = arg2->t_tspec;
492 	if (isityp(t1) && isityp(t2) && !arg1->t_isenum && !arg2->t_isenum) {
493 		if (promote) {
494 			/*
495 			 * XXX Here is a problem: Althrough it is possible to
496 			 * pass an int where a char/short it expected, there
497 			 * may be loss in significant digits. We should first
498 			 * check for const arguments if they can be converted
499 			 * into the original parameter type.
500 			 */
501 			if (t1 == FLOAT) {
502 				t1 = DOUBLE;
503 			} else if (t1 == CHAR || t1 == SCHAR) {
504 				t1 = INT;
505 			} else if (t1 == UCHAR) {
506 				t1 = tflag ? UINT : INT;
507 			} else if (t1 == SHORT) {
508 				t1 = INT;
509 			} else if (t1 == USHORT) {
510 				/* CONSTCOND */
511 				t1 = INT_MAX < USHRT_MAX || tflag ? UINT : INT;
512 			}
513 		}
514 
515 		if (styp(t1) == styp(t2)) {
516 
517 			/*
518 			 * types differ only in signedness; get information
519 			 * about arguments
520 			 */
521 
522 			/*
523 			 * treat a definition like a call with variable
524 			 * arguments
525 			 */
526 			ai1 = call1 != NULL ? call1->f_args : NULL;
527 
528 			/*
529 			 * if two calls are compared, ai1 is set to the
530 			 * information for the n-th argument, if this was
531 			 * a constant, otherwise to NULL
532 			 */
533 			for ( ; ai1 != NULL; ai1 = ai1->a_nxt) {
534 				if (ai1->a_num == n)
535 					break;
536 			}
537 			/*
538 			 * ai is set to the information of the n-th arg
539 			 * of the (second) call, if this was a constant,
540 			 * otherwise to NULL
541 			 */
542 			for (ai = call->f_args; ai != NULL; ai = ai->a_nxt) {
543 				if (ai->a_num == n)
544 					break;
545 			}
546 
547 			if (ai1 == NULL && ai == NULL) {
548 				/* no constant at all */
549 				if (!hflag)
550 					return;
551 			} else if (ai1 == NULL || ai == NULL) {
552 				/* one constant */
553 				if (ai == NULL)
554 					ai = ai1;
555 				if (ai->a_zero || ai->a_pcon)
556 					/* same value in signed and unsigned */
557 					return;
558 				/* value (not representation) differently */
559 			} else {
560 				/*
561 				 * two constants, one signed, one unsigned;
562 				 * if the msb of one of the constants is set,
563 				 * the argument is used inconsistently.
564 				 */
565 				if (!ai1->a_ncon && !ai->a_ncon)
566 					return;
567 			}
568 		}
569 
570 	} else if (t1 == PTR && isityp(t2)) {
571 		for (ai = call->f_args; ai != NULL; ai = ai->a_nxt) {
572 			if (ai->a_num == n)
573 				break;
574 		}
575 		/*
576 		 * Vendor implementations of lint (e.g. HP-UX, Digital UNIX)
577 		 * don't care about the size of the integer argument,
578 		 * only whether or not it is zero.  We do the same.
579 		 */
580 		if (ai != NULL && ai->a_zero)
581 			return;
582 	}
583 
584 	pos1 = xstrdup(mkpos(pos1p));
585 	/* %s, arg %d used inconsistently\t%s  ::  %s */
586 	msg(6, hte->h_name, n, pos1, mkpos(&call->f_pos));
587 	free(pos1);
588 }
589 
590 /*
591  * Compare the types in the NULL-terminated array ap with the format
592  * string fmt.
593  */
594 static void
595 printflike(hte_t *hte, fcall_t *call, int n, const char *fmt, type_t **ap)
596 {
597 	const	char *fp;
598 	int	fc;
599 	int	fwidth, prec, left, sign, space, alt, zero;
600 	tspec_t	sz, t1, t2 = NOTSPEC;
601 	type_t	*tp;
602 
603 	fp = fmt;
604 	fc = *fp++;
605 
606 	for ( ; ; ) {
607 		if (fc == '\0') {
608 			if (*ap != NULL)
609 				tomanyarg(hte, call);
610 			break;
611 		}
612 		if (fc != '%') {
613 			badfmt(hte, call);
614 			break;
615 		}
616 		fc = *fp++;
617 		fwidth = prec = left = sign = space = alt = zero = 0;
618 		sz = NOTSPEC;
619 
620 		/* Flags */
621 		for ( ; ; ) {
622 			if (fc == '-') {
623 				if (left)
624 					break;
625 				left = 1;
626 			} else if (fc == '+') {
627 				if (sign)
628 					break;
629 				sign = 1;
630 			} else if (fc == ' ') {
631 				if (space)
632 					break;
633 				space = 1;
634 			} else if (fc == '#') {
635 				if (alt)
636 					break;
637 				alt = 1;
638 			} else if (fc == '0') {
639 				if (zero)
640 					break;
641 				zero = 1;
642 			} else {
643 				break;
644 			}
645 			fc = *fp++;
646 		}
647 
648 		/* field width */
649 		if (isdigit(fc)) {
650 			fwidth = 1;
651 			do { fc = *fp++; } while (isdigit(fc)) ;
652 		} else if (fc == '*') {
653 			fwidth = 1;
654 			fc = *fp++;
655 			if ((tp = *ap++) == NULL) {
656 				tofewarg(hte, call);
657 				break;
658 			}
659 			n++;
660 			if ((t1 = tp->t_tspec) != INT && (hflag || t1 != UINT))
661 				inconarg(hte, call, n);
662 		}
663 
664 		/* precision */
665 		if (fc == '.') {
666 			fc = *fp++;
667 			prec = 1;
668 			if (isdigit(fc)) {
669 				do { fc = *fp++; } while (isdigit(fc));
670 			} else if (fc == '*') {
671 				fc = *fp++;
672 				if ((tp = *ap++) == NULL) {
673 					tofewarg(hte, call);
674 					break;
675 				}
676 				n++;
677 				if (tp->t_tspec != INT)
678 					inconarg(hte, call, n);
679 			} else {
680 				badfmt(hte, call);
681 				break;
682 			}
683 		}
684 
685 		if (fc == 'h') {
686 			sz = SHORT;
687 		} else if (fc == 'l') {
688 			sz = LONG;
689 		} else if (fc == 'q') {
690 			sz = QUAD;
691 		} else if (fc == 'L') {
692 			sz = LDOUBLE;
693 		}
694 		if (sz != NOTSPEC)
695 			fc = *fp++;
696 
697 		if (fc == '%') {
698 			if (sz != NOTSPEC || left || sign || space ||
699 			    alt || zero || prec || fwidth) {
700 				badfmt(hte, call);
701 			}
702 			fc = *fp++;
703 			continue;
704 		}
705 
706 		if (fc == '\0') {
707 			badfmt(hte, call);
708 			break;
709 		}
710 
711 		if ((tp = *ap++) == NULL) {
712 			tofewarg(hte, call);
713 			break;
714 		}
715 		n++;
716 		if ((t1 = tp->t_tspec) == PTR)
717 			t2 = tp->t_subt->t_tspec;
718 
719 		if (fc == 'd' || fc == 'i') {
720 			if (alt || sz == LDOUBLE) {
721 				badfmt(hte, call);
722 				break;
723 			}
724 		int_conv:
725 			if (sz == LONG) {
726 				if (t1 != LONG && (hflag || t1 != ULONG))
727 					inconarg(hte, call, n);
728 			} else if (sz == QUAD) {
729 				if (t1 != QUAD && (hflag || t1 != UQUAD))
730 					inconarg(hte, call, n);
731 			} else {
732 				/*
733 				 * SHORT is always promoted to INT, USHORT
734 				 * to INT or UINT.
735 				 */
736 				if (t1 != INT && (hflag || t1 != UINT))
737 					inconarg(hte, call, n);
738 			}
739 		} else if (fc == 'o' || fc == 'u' || fc == 'x' || fc == 'X') {
740 			if ((alt && fc == 'u') || sz == LDOUBLE)
741 				badfmt(hte, call);
742 		uint_conv:
743 			if (sz == LONG) {
744 				if (t1 != ULONG && (hflag || t1 != LONG))
745 					inconarg(hte, call, n);
746 			} else if (sz == QUAD) {
747 				if (t1 != UQUAD && (hflag || t1 != QUAD))
748 					inconarg(hte, call, n);
749 			} else if (sz == SHORT) {
750 				/* USHORT was promoted to INT or UINT */
751 				if (t1 != UINT && t1 != INT)
752 					inconarg(hte, call, n);
753 			} else {
754 				if (t1 != UINT && (hflag || t1 != INT))
755 					inconarg(hte, call, n);
756 			}
757 		} else if (fc == 'D' || fc == 'O' || fc == 'U') {
758 			if ((alt && fc != 'O') || sz != NOTSPEC || !tflag)
759 				badfmt(hte, call);
760 			sz = LONG;
761 			if (fc == 'D') {
762 				goto int_conv;
763 			} else {
764 				goto uint_conv;
765 			}
766 		} else if (fc == 'f' || fc == 'e' || fc == 'E' ||
767 			   fc == 'g' || fc == 'G') {
768 			if (sz == NOTSPEC)
769 				sz = DOUBLE;
770 			if (sz != DOUBLE && sz != LDOUBLE)
771 				badfmt(hte, call);
772 			if (t1 != sz)
773 				inconarg(hte, call, n);
774 		} else if (fc == 'c') {
775 			if (sz != NOTSPEC || alt || zero)
776 				badfmt(hte, call);
777 			if (t1 != INT)
778 				inconarg(hte, call, n);
779 		} else if (fc == 's') {
780 			if (sz != NOTSPEC || alt || zero)
781 				badfmt(hte, call);
782 			if (t1 != PTR ||
783 			    (t2 != CHAR && t2 != UCHAR && t2 != SCHAR)) {
784 				inconarg(hte, call, n);
785 			}
786 		} else if (fc == 'p') {
787 			if (fwidth || prec || sz != NOTSPEC || alt || zero)
788 				badfmt(hte, call);
789 			if (t1 != PTR || (hflag && t2 != VOID))
790 				inconarg(hte, call, n);
791 		} else if (fc == 'n') {
792 			if (fwidth || prec || alt || zero || sz == LDOUBLE)
793 				badfmt(hte, call);
794 			if (t1 != PTR) {
795 				inconarg(hte, call, n);
796 			} else if (sz == LONG) {
797 				if (t2 != LONG && t2 != ULONG)
798 					inconarg(hte, call, n);
799 			} else if (sz == SHORT) {
800 				if (t2 != SHORT && t2 != USHORT)
801 					inconarg(hte, call, n);
802 			} else {
803 				if (t2 != INT && t2 != UINT)
804 					inconarg(hte, call, n);
805 			}
806 		} else {
807 			badfmt(hte, call);
808 			break;
809 		}
810 
811 		fc = *fp++;
812 	}
813 }
814 
815 /*
816  * Compare the types in the NULL-terminated array ap with the format
817  * string fmt.
818  */
819 static void
820 scanflike(hte_t *hte, fcall_t *call, int n, const char *fmt, type_t **ap)
821 {
822 	const	char *fp;
823 	int	fc;
824 	int	noasgn, fwidth;
825 	tspec_t	sz, t1 = NOTSPEC, t2 = NOTSPEC;
826 	type_t	*tp = NULL;
827 
828 	fp = fmt;
829 	fc = *fp++;
830 
831 	for ( ; ; ) {
832 		if (fc == '\0') {
833 			if (*ap != NULL)
834 				tomanyarg(hte, call);
835 			break;
836 		}
837 		if (fc != '%') {
838 			badfmt(hte, call);
839 			break;
840 		}
841 		fc = *fp++;
842 
843 		noasgn = fwidth = 0;
844 		sz = NOTSPEC;
845 
846 		if (fc == '*') {
847 			noasgn = 1;
848 			fc = *fp++;
849 		}
850 
851 		if (isdigit(fc)) {
852 			fwidth = 1;
853 			do { fc = *fp++; } while (isdigit(fc));
854 		}
855 
856 		if (fc == 'h') {
857 			sz = SHORT;
858 		} else if (fc == 'l') {
859 			sz = LONG;
860 		} else if (fc == 'q') {
861 			sz = QUAD;
862 		} else if (fc == 'L') {
863 			sz = LDOUBLE;
864 		}
865 		if (sz != NOTSPEC)
866 			fc = *fp++;
867 
868 		if (fc == '%') {
869 			if (sz != NOTSPEC || noasgn || fwidth)
870 				badfmt(hte, call);
871 			fc = *fp++;
872 			continue;
873 		}
874 
875 		if (!noasgn) {
876 			if ((tp = *ap++) == NULL) {
877 				tofewarg(hte, call);
878 				break;
879 			}
880 			n++;
881 			if ((t1 = tp->t_tspec) == PTR)
882 				t2 = tp->t_subt->t_tspec;
883 		}
884 
885 		if (fc == 'd' || fc == 'i' || fc == 'n') {
886 			if (sz == LDOUBLE)
887 				badfmt(hte, call);
888 			if (sz != SHORT && sz != LONG && sz != QUAD)
889 				sz = INT;
890 		conv:
891 			if (!noasgn) {
892 				if (t1 != PTR) {
893 					inconarg(hte, call, n);
894 				} else if (t2 != styp(sz)) {
895 					inconarg(hte, call, n);
896 				} else if (hflag && t2 != sz) {
897 					inconarg(hte, call, n);
898 				} else if (tp->t_subt->t_const) {
899 					inconarg(hte, call, n);
900 				}
901 			}
902 		} else if (fc == 'o' || fc == 'u' || fc == 'x') {
903 			if (sz == LDOUBLE)
904 				badfmt(hte, call);
905 			if (sz == SHORT) {
906 				sz = USHORT;
907 			} else if (sz == LONG) {
908 				sz = ULONG;
909 			} else if (sz == QUAD) {
910 				sz = UQUAD;
911 			} else {
912 				sz = UINT;
913 			}
914 			goto conv;
915 		} else if (fc == 'D') {
916 			if (sz != NOTSPEC || !tflag)
917 				badfmt(hte, call);
918 			sz = LONG;
919 			goto conv;
920 		} else if (fc == 'O') {
921 			if (sz != NOTSPEC || !tflag)
922 				badfmt(hte, call);
923 			sz = ULONG;
924 			goto conv;
925 		} else if (fc == 'X') {
926 			/*
927 			 * XXX valid in ANSI C, but in NetBSD's libc imple-
928 			 * mented as "lx". Thats why it should be avoided.
929 			 */
930 			if (sz != NOTSPEC || !tflag)
931 				badfmt(hte, call);
932 			sz = ULONG;
933 			goto conv;
934 		} else if (fc == 'E') {
935 			/*
936 			 * XXX valid in ANSI C, but in NetBSD's libc imple-
937 			 * mented as "lf". Thats why it should be avoided.
938 			 */
939 			if (sz != NOTSPEC || !tflag)
940 				badfmt(hte, call);
941 			sz = DOUBLE;
942 			goto conv;
943 		} else if (fc == 'F') {
944 			/* XXX only for backward compatibility */
945 			if (sz != NOTSPEC || !tflag)
946 				badfmt(hte, call);
947 			sz = DOUBLE;
948 			goto conv;
949 		} else if (fc == 'G') {
950 			/*
951 			 * XXX valid in ANSI C, but in NetBSD's libc not
952 			 * implemented
953 			 */
954 			if (sz != NOTSPEC && sz != LONG && sz != LDOUBLE)
955 				badfmt(hte, call);
956 			goto fconv;
957 		} else if (fc == 'e' || fc == 'f' || fc == 'g') {
958 		fconv:
959 			if (sz == NOTSPEC) {
960 				sz = FLOAT;
961 			} else if (sz == LONG) {
962 				sz = DOUBLE;
963 			} else if (sz != LDOUBLE) {
964 				badfmt(hte, call);
965 				sz = FLOAT;
966 			}
967 			goto conv;
968 		} else if (fc == 's' || fc == '[' || fc == 'c') {
969 			if (sz != NOTSPEC)
970 				badfmt(hte, call);
971 			if (fc == '[') {
972 				if ((fc = *fp++) == '-') {
973 					badfmt(hte, call);
974 					fc = *fp++;
975 				}
976 				if (fc != ']') {
977 					badfmt(hte, call);
978 					if (fc == '\0')
979 						break;
980 				}
981 			}
982 			if (!noasgn) {
983 				if (t1 != PTR) {
984 					inconarg(hte, call, n);
985 				} else if (t2 != CHAR && t2 != UCHAR &&
986 					   t2 != SCHAR) {
987 					inconarg(hte, call, n);
988 				}
989 			}
990 		} else if (fc == 'p') {
991 			if (sz != NOTSPEC)
992 				badfmt(hte, call);
993 			if (!noasgn) {
994 				if (t1 != PTR || t2 != PTR) {
995 					inconarg(hte, call, n);
996 				} else if (tp->t_subt->t_subt->t_tspec!=VOID) {
997 					if (hflag)
998 						inconarg(hte, call, n);
999 				}
1000 			}
1001 		} else {
1002 			badfmt(hte, call);
1003 			break;
1004 		}
1005 
1006 		fc = *fp++;
1007 	}
1008 }
1009 
1010 static void
1011 badfmt(hte_t *hte, fcall_t *call)
1012 {
1013 
1014 	/* %s: malformed format string\t%s */
1015 	msg(13, hte->h_name, mkpos(&call->f_pos));
1016 }
1017 
1018 static void
1019 inconarg(hte_t *hte, fcall_t *call, int n)
1020 {
1021 
1022 	/* %s, arg %d inconsistent with format\t%s(%d) */
1023 	msg(14, hte->h_name, n, mkpos(&call->f_pos));
1024 }
1025 
1026 static void
1027 tofewarg(hte_t *hte, fcall_t *call)
1028 {
1029 
1030 	/* %s: too few args for format  \t%s */
1031 	msg(15, hte->h_name, mkpos(&call->f_pos));
1032 }
1033 
1034 static void
1035 tomanyarg(hte_t *hte, fcall_t *call)
1036 {
1037 
1038 	/* %s: too many args for format  \t%s */
1039 	msg(16, hte->h_name, mkpos(&call->f_pos));
1040 }
1041 
1042 
1043 /*
1044  * Print warnings for return values which are used, but not returned,
1045  * or return values which are always or sometimes ignored.
1046  */
1047 static void
1048 chkrvu(hte_t *hte, sym_t *def)
1049 {
1050 	fcall_t	*call;
1051 	int	used, ignored;
1052 
1053 	if (def == NULL)
1054 		/* don't know wheter or not the functions returns a value */
1055 		return;
1056 
1057 	if (hte->h_calls == NULL)
1058 		return;
1059 
1060 	if (def->s_rval) {
1061 		/* function has return value */
1062 		used = ignored = 0;
1063 		for (call = hte->h_calls; call != NULL; call = call->f_nxt) {
1064 			used |= call->f_rused || call->f_rdisc;
1065 			ignored |= !call->f_rused && !call->f_rdisc;
1066 		}
1067 		/*
1068 		 * XXX as soon as we are able to disable single warnings
1069 		 * the following dependencies from hflag should be removed.
1070 		 * but for now I do'nt want to be botherd by this warnings
1071 		 * which are almost always useless.
1072 		 */
1073 		if (!used && ignored) {
1074 			if (hflag)
1075 				/* %s returns value which is always ignored */
1076 				msg(8, hte->h_name);
1077 		} else if (used && ignored) {
1078 			if (hflag)
1079 				/* %s returns value which is sometimes ign. */
1080 				msg(9, hte->h_name);
1081 		}
1082 	} else {
1083 		/* function has no return value */
1084 		for (call = hte->h_calls; call != NULL; call = call->f_nxt) {
1085 			if (call->f_rused)
1086 				/* %s value is used( %s ), but none ret. */
1087 				msg(10, hte->h_name, mkpos(&call->f_pos));
1088 		}
1089 	}
1090 }
1091 
1092 /*
1093  * Print warnings for inconsistent argument declarations.
1094  */
1095 static void
1096 chkadecl(hte_t *hte, sym_t *def, sym_t *decl)
1097 {
1098 	/* LINTED (automatic hides external declaration: warn) */
1099 	int	osdef, eq, warn, n;
1100 	sym_t	*sym1, *sym;
1101 	type_t	**ap1, **ap2, *tp1, *tp2;
1102 	char	*pos1;
1103 	const	char *pos2;
1104 
1105 	osdef = 0;
1106 	if (def != NULL) {
1107 		osdef = def->s_osdef;
1108 		sym1 = def;
1109 	} else if (decl != NULL && TP(decl->s_type)->t_proto) {
1110 		sym1 = decl;
1111 	} else {
1112 		return;
1113 	}
1114 	if (TP(sym1->s_type)->t_tspec != FUNC)
1115 		return;
1116 
1117 	/*
1118 	 * XXX Prototypes should also be compared with old style function
1119 	 * declarations.
1120 	 */
1121 
1122 	for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
1123 		if (sym == sym1 || !TP(sym->s_type)->t_proto)
1124 			continue;
1125 		ap1 = TP(sym1->s_type)->t_args;
1126 		ap2 = TP(sym->s_type)->t_args;
1127 		n = 0;
1128 		while (*ap1 != NULL && *ap2 != NULL) {
1129 			warn = 0;
1130 			eq = eqtype(*ap1, *ap2, 1, osdef, 0, &warn);
1131 			if (!eq || warn) {
1132 				pos1 = xstrdup(mkpos(&sym1->s_pos));
1133 				pos2 = mkpos(&sym->s_pos);
1134 				/* %s, arg %d declared inconsistently ... */
1135 				msg(11, hte->h_name, n + 1, pos1, pos2);
1136 				free(pos1);
1137 			}
1138 			n++;
1139 			ap1++;
1140 			ap2++;
1141 		}
1142 		if (*ap1 == *ap2) {
1143 			tp1 = TP(sym1->s_type);
1144 			tp2 = TP(sym->s_type);
1145 			if (tp1->t_vararg == tp2->t_vararg)
1146 				continue;
1147 			if (tp2->t_vararg &&
1148 			    sym1->s_va && sym1->s_nva == n && !sflag) {
1149 				continue;
1150 			}
1151 		}
1152 		/* %s: variable # of args declared\t%s  ::  %s */
1153 		pos1 = xstrdup(mkpos(&sym1->s_pos));
1154 		msg(12, hte->h_name, pos1, mkpos(&sym->s_pos));
1155 		free(pos1);
1156 	}
1157 }
1158 
1159 
1160 /*
1161  * Check compatibility of two types. Returns 1 if types are compatible,
1162  * otherwise 0.
1163  *
1164  * ignqual	if set, ignore qualifiers of outhermost type; used for
1165  *		function arguments
1166  * promote	if set, promote left type before comparison; used for
1167  *		comparisons of arguments with parameters of old style
1168  *		definitions
1169  * asgn		left indirected type must have at least the same qualifiers
1170  *		like right indirected type (for assignments and function
1171  *		arguments)
1172  * *warn	set to 1 if an old style declaration was compared with
1173  *		an incompatible prototype declaration
1174  */
1175 static int
1176 eqtype(type_t *tp1, type_t *tp2, int ignqual, int promot, int asgn, int *warn)
1177 {
1178 	tspec_t	t, to;
1179 	int	indir;
1180 
1181 	to = NOTSPEC;
1182 	indir = 0;
1183 
1184 	while (tp1 != NULL && tp2 != NULL) {
1185 
1186 		t = tp1->t_tspec;
1187 		if (promot) {
1188 			if (t == FLOAT) {
1189 				t = DOUBLE;
1190 			} else if (t == CHAR || t == SCHAR) {
1191 				t = INT;
1192 			} else if (t == UCHAR) {
1193 				t = tflag ? UINT : INT;
1194 			} else if (t == SHORT) {
1195 				t = INT;
1196 			} else if (t == USHORT) {
1197 				/* CONSTCOND */
1198 				t = INT_MAX < USHRT_MAX || tflag ? UINT : INT;
1199 			}
1200 		}
1201 
1202 		if (asgn && to == PTR) {
1203 			if (indir == 1 && (t == VOID || tp2->t_tspec == VOID))
1204 				return (1);
1205 		}
1206 
1207 		if (t != tp2->t_tspec) {
1208 			/*
1209 			 * Give pointer to types which differ only in
1210 			 * signedness a chance if not sflag and not hflag.
1211 			 */
1212 			if (sflag || hflag || to != PTR)
1213 				return (0);
1214 			if (styp(t) != styp(tp2->t_tspec))
1215 				return (0);
1216 		}
1217 
1218 		if (tp1->t_isenum && tp2->t_isenum) {
1219 			if (tp1->t_istag && tp2->t_istag) {
1220 				return (tp1->t_tag == tp2->t_tag);
1221 			} else if (tp1->t_istynam && tp2->t_istynam) {
1222 				return (tp1->t_tynam == tp2->t_tynam);
1223 			} else if (tp1->t_isuniqpos && tp2->t_isuniqpos) {
1224 				return (tp1->t_uniqpos.p_line ==
1225 				      tp2->t_uniqpos.p_line &&
1226 				    tp1->t_uniqpos.p_file ==
1227 				      tp2->t_uniqpos.p_file &&
1228 				    tp1->t_uniqpos.p_uniq ==
1229 				      tp2->t_uniqpos.p_uniq);
1230 			} else {
1231 				return (0);
1232 			}
1233 		}
1234 
1235 		/*
1236 		 * XXX Handle combinations of enum and int if eflag is set.
1237 		 * But note: enum and 0 should be allowed.
1238 		 */
1239 
1240 		if (asgn && indir == 1) {
1241 			if (!tp1->t_const && tp2->t_const)
1242 				return (0);
1243 			if (!tp1->t_volatile && tp2->t_volatile)
1244 				return (0);
1245 		} else if (!ignqual && !tflag) {
1246 			if (tp1->t_const != tp2->t_const)
1247 				return (0);
1248 			if (tp1->t_const != tp2->t_const)
1249 				return (0);
1250 		}
1251 
1252 		if (t == STRUCT || t == UNION) {
1253 			if (tp1->t_istag && tp2->t_istag) {
1254 				return (tp1->t_tag == tp2->t_tag);
1255 			} else if (tp1->t_istynam && tp2->t_istynam) {
1256 				return (tp1->t_tynam == tp2->t_tynam);
1257 			} else if (tp1->t_isuniqpos && tp2->t_isuniqpos) {
1258 				return (tp1->t_uniqpos.p_line ==
1259 				      tp2->t_uniqpos.p_line &&
1260 				    tp1->t_uniqpos.p_file ==
1261 				      tp2->t_uniqpos.p_file &&
1262 				    tp1->t_uniqpos.p_uniq ==
1263 				      tp2->t_uniqpos.p_uniq);
1264 			} else {
1265 				return (0);
1266 			}
1267 		}
1268 
1269 		if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
1270 			if (tp1->t_dim != 0 && tp2->t_dim != 0)
1271 				return (0);
1272 		}
1273 
1274 		if (t == FUNC) {
1275 			if (tp1->t_proto && tp2->t_proto) {
1276 				if (!eqargs(tp1, tp2, warn))
1277 					return (0);
1278 			} else if (tp1->t_proto) {
1279 				if (!mnoarg(tp1, warn))
1280 					return (0);
1281 			} else if (tp2->t_proto) {
1282 				if (!mnoarg(tp2, warn))
1283 					return (0);
1284 			}
1285 		}
1286 
1287 		tp1 = tp1->t_subt;
1288 		tp2 = tp2->t_subt;
1289 		ignqual = promot = 0;
1290 		to = t;
1291 		indir++;
1292 
1293 	}
1294 
1295 	return (tp1 == tp2);
1296 }
1297 
1298 /*
1299  * Compares arguments of two prototypes
1300  */
1301 static int
1302 eqargs(type_t *tp1, type_t *tp2, int *warn)
1303 {
1304 	type_t	**a1, **a2;
1305 
1306 	if (tp1->t_vararg != tp2->t_vararg)
1307 		return (0);
1308 
1309 	a1 = tp1->t_args;
1310 	a2 = tp2->t_args;
1311 
1312 	while (*a1 != NULL && *a2 != NULL) {
1313 
1314 		if (eqtype(*a1, *a2, 1, 0, 0, warn) == 0)
1315 			return (0);
1316 
1317 		a1++;
1318 		a2++;
1319 
1320 	}
1321 
1322 	return (*a1 == *a2);
1323 }
1324 
1325 /*
1326  * mnoarg() (matches functions with no argument type information)
1327  * returns 1 if all parameters of a prototype are compatible with
1328  * and old style function declaration.
1329  * This is the case if following conditions are met:
1330  *	1. the prototype must have a fixed number of parameters
1331  *	2. no parameter is of type float
1332  *	3. no parameter is converted to another type if integer promotion
1333  *	   is applied on it
1334  */
1335 static int
1336 mnoarg(type_t *tp, int *warn)
1337 {
1338 	type_t	**arg;
1339 	tspec_t	t;
1340 
1341 	if (tp->t_vararg && warn != NULL)
1342 		*warn = 1;
1343 	for (arg = tp->t_args; *arg != NULL; arg++) {
1344 		if ((t = (*arg)->t_tspec) == FLOAT)
1345 			return (0);
1346 		if (t == CHAR || t == SCHAR || t == UCHAR)
1347 			return (0);
1348 		if (t == SHORT || t == USHORT)
1349 			return (0);
1350 	}
1351 	return (1);
1352 }
1353