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