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