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