decl.c revision 1.203 1 /* $NetBSD: decl.c,v 1.203 2021/07/15 22:47:17 rillig Exp $ */
2
3 /*
4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
5 * Copyright (c) 1994, 1995 Jochen Pohl
6 * All Rights Reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Jochen Pohl for
19 * The NetBSD Project.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #if HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
37 #endif
38
39 #include <sys/cdefs.h>
40 #if defined(__RCSID) && !defined(lint)
41 __RCSID("$NetBSD: decl.c,v 1.203 2021/07/15 22:47:17 rillig Exp $");
42 #endif
43
44 #include <sys/param.h>
45 #include <limits.h>
46 #include <stdlib.h>
47 #include <string.h>
48
49 #include "lint1.h"
50
51 const char *unnamed = "<unnamed>";
52
53 /* shared type structures for arithmetic types and void */
54 static type_t *typetab;
55
56 /* value of next enumerator during declaration of enum types */
57 int enumval;
58
59 /*
60 * pointer to top element of a stack which contains information local
61 * to nested declarations
62 */
63 dinfo_t *dcs;
64
65 static type_t *tdeferr(type_t *, tspec_t);
66 static void settdsym(type_t *, sym_t *);
67 static tspec_t merge_type_specifiers(tspec_t, tspec_t);
68 static void align(int, int);
69 static sym_t *newtag(sym_t *, scl_t, bool, bool);
70 static bool eqargs(const type_t *, const type_t *, bool *);
71 static bool mnoarg(const type_t *, bool *);
72 static bool check_old_style_definition(sym_t *, sym_t *);
73 static bool check_prototype_declaration(sym_t *, sym_t *);
74 static sym_t *new_style_function(sym_t *, sym_t *);
75 static void old_style_function(sym_t *, sym_t *);
76 static void declare_external_in_block(sym_t *);
77 static bool check_init(sym_t *);
78 static void check_argument_usage(bool, sym_t *);
79 static void check_variable_usage(bool, sym_t *);
80 static void check_label_usage(sym_t *);
81 static void check_tag_usage(sym_t *);
82 static void check_global_variable(const sym_t *);
83 static void check_global_variable_size(const sym_t *);
84
85 /*
86 * initializes all global vars used in declarations
87 */
88 void
89 #ifdef __sh3__
90 /* XXX port-sh3/56311 */
91 __attribute__((optimize("O0")))
92 #endif
93 initdecl(void)
94 {
95 int i;
96
97 /* declaration stack */
98 dcs = xcalloc(1, sizeof(*dcs));
99 dcs->d_ctx = EXTERN;
100 dcs->d_ldlsym = &dcs->d_dlsyms;
101
102 /* type information and classification */
103 inittyp();
104
105 /* shared type structures */
106 typetab = xcalloc(NTSPEC, sizeof(*typetab));
107 for (i = 0; i < NTSPEC; i++)
108 typetab[i].t_tspec = NOTSPEC;
109 typetab[BOOL].t_tspec = BOOL;
110 typetab[CHAR].t_tspec = CHAR;
111 typetab[SCHAR].t_tspec = SCHAR;
112 typetab[UCHAR].t_tspec = UCHAR;
113 typetab[SHORT].t_tspec = SHORT;
114 typetab[USHORT].t_tspec = USHORT;
115 typetab[INT].t_tspec = INT;
116 typetab[UINT].t_tspec = UINT;
117 typetab[LONG].t_tspec = LONG;
118 typetab[ULONG].t_tspec = ULONG;
119 typetab[QUAD].t_tspec = QUAD;
120 typetab[UQUAD].t_tspec = UQUAD;
121 typetab[FLOAT].t_tspec = FLOAT;
122 typetab[DOUBLE].t_tspec = DOUBLE;
123 typetab[LDOUBLE].t_tspec = LDOUBLE;
124 typetab[FCOMPLEX].t_tspec = FCOMPLEX;
125 typetab[DCOMPLEX].t_tspec = DCOMPLEX;
126 typetab[LCOMPLEX].t_tspec = LCOMPLEX;
127 typetab[COMPLEX].t_tspec = COMPLEX;
128 typetab[VOID].t_tspec = VOID;
129 /*
130 * Next two are not real types. They are only used by the parser
131 * to return keywords "signed" and "unsigned"
132 */
133 typetab[SIGNED].t_tspec = SIGNED;
134 typetab[UNSIGN].t_tspec = UNSIGN;
135 }
136
137 /*
138 * Returns a shared type structure for arithmetic types and void.
139 *
140 * It's important to duplicate this structure (using dup_type() or
141 * expr_dup_type()) if it is to be modified (adding qualifiers or anything
142 * else).
143 */
144 type_t *
145 gettyp(tspec_t t)
146 {
147
148 /* TODO: make the return type 'const' */
149 return &typetab[t];
150 }
151
152 type_t *
153 dup_type(const type_t *tp)
154 {
155 type_t *ntp;
156
157 ntp = getblk(sizeof(*ntp));
158 *ntp = *tp;
159 return ntp;
160 }
161
162 /*
163 * Use expr_dup_type() instead of dup_type() inside expressions (if the
164 * allocated memory should be freed after the expr).
165 */
166 type_t *
167 expr_dup_type(const type_t *tp)
168 {
169 type_t *ntp;
170
171 ntp = expr_zalloc(sizeof(*ntp));
172 *ntp = *tp;
173 return ntp;
174 }
175
176 /*
177 * Returns whether the argument is void or an incomplete array,
178 * struct, union or enum type.
179 */
180 bool
181 is_incomplete(const type_t *tp)
182 {
183 tspec_t t;
184
185 if ((t = tp->t_tspec) == VOID) {
186 return true;
187 } else if (t == ARRAY) {
188 return tp->t_incomplete_array;
189 } else if (t == STRUCT || t == UNION) {
190 return tp->t_str->sou_incomplete;
191 } else if (t == ENUM) {
192 return tp->t_enum->en_incomplete;
193 }
194 return false;
195 }
196
197 /*
198 * Mark an array, struct, union or enum type as complete or incomplete.
199 */
200 void
201 setcomplete(type_t *tp, bool complete)
202 {
203 tspec_t t;
204
205 lint_assert(tp != NULL);
206 if ((t = tp->t_tspec) == ARRAY) {
207 tp->t_incomplete_array = !complete;
208 } else if (t == STRUCT || t == UNION) {
209 tp->t_str->sou_incomplete = !complete;
210 } else {
211 lint_assert(t == ENUM);
212 tp->t_enum->en_incomplete = !complete;
213 }
214 }
215
216 /*
217 * Remember the storage class of the current declaration in dcs->d_scl
218 * (the top element of the declaration stack) and detect multiple
219 * storage classes.
220 */
221 void
222 add_storage_class(scl_t sc)
223 {
224
225 if (sc == INLINE) {
226 if (dcs->d_inline)
227 /* duplicate '%s' */
228 warning(10, "inline");
229 dcs->d_inline = true;
230 return;
231 }
232 if (dcs->d_type != NULL || dcs->d_abstract_type != NOTSPEC ||
233 dcs->d_sign_mod != NOTSPEC || dcs->d_rank_mod != NOTSPEC) {
234 /* storage class after type is obsolescent */
235 warning(83);
236 }
237 if (dcs->d_scl == NOSCL) {
238 dcs->d_scl = sc;
239 } else {
240 /*
241 * multiple storage classes. An error will be reported in
242 * end_type().
243 */
244 dcs->d_mscl = true;
245 }
246 }
247
248 /*
249 * Remember the type, modifier or typedef name returned by the parser
250 * in *dcs (top element of decl stack). This information is used in
251 * end_type() to build the type used for all declarators in this
252 * declaration.
253 *
254 * If tp->t_typedef is 1, the type comes from a previously defined typename.
255 * Otherwise it comes from a type specifier (int, long, ...) or a
256 * struct/union/enum tag.
257 */
258 void
259 add_type(type_t *tp)
260 {
261 tspec_t t;
262 #ifdef DEBUG
263 printf("%s: %s\n", __func__, type_name(tp));
264 #endif
265 if (tp->t_typedef) {
266 /*
267 * something like "typedef int a; int a b;"
268 * This should not happen with current grammar.
269 */
270 lint_assert(dcs->d_type == NULL);
271 lint_assert(dcs->d_abstract_type == NOTSPEC);
272 lint_assert(dcs->d_sign_mod == NOTSPEC);
273 lint_assert(dcs->d_rank_mod == NOTSPEC);
274
275 dcs->d_type = tp;
276 return;
277 }
278
279 t = tp->t_tspec;
280
281 if (t == STRUCT || t == UNION || t == ENUM) {
282 /*
283 * something like "int struct a ..."
284 * struct/union/enum with anything else is not allowed
285 */
286 if (dcs->d_type != NULL || dcs->d_abstract_type != NOTSPEC ||
287 dcs->d_rank_mod != NOTSPEC || dcs->d_sign_mod != NOTSPEC) {
288 /*
289 * remember that an error must be reported in
290 * end_type().
291 */
292 dcs->d_terr = true;
293 dcs->d_abstract_type = NOTSPEC;
294 dcs->d_sign_mod = NOTSPEC;
295 dcs->d_rank_mod = NOTSPEC;
296 }
297 dcs->d_type = tp;
298 return;
299 }
300
301 if (dcs->d_type != NULL && !dcs->d_type->t_typedef) {
302 /*
303 * something like "struct a int"
304 * struct/union/enum with anything else is not allowed
305 */
306 dcs->d_terr = true;
307 return;
308 }
309
310 if (t == COMPLEX) {
311 if (dcs->d_complex_mod == FLOAT)
312 t = FCOMPLEX;
313 else if (dcs->d_complex_mod == DOUBLE)
314 t = DCOMPLEX;
315 else {
316 /* invalid type for _Complex */
317 error(308);
318 t = DCOMPLEX; /* just as a fallback */
319 }
320 dcs->d_complex_mod = NOTSPEC;
321 }
322
323 if (t == LONG && dcs->d_rank_mod == LONG) {
324 /* "long long" or "long ... long" */
325 t = QUAD;
326 dcs->d_rank_mod = NOTSPEC;
327 if (!quadflg)
328 /* %s C does not support 'long long' */
329 c99ism(265, tflag ? "traditional" : "c89");
330 }
331
332 if (dcs->d_type != NULL && dcs->d_type->t_typedef) {
333 /* something like "typedef int a; a long ..." */
334 dcs->d_type = tdeferr(dcs->d_type, t);
335 return;
336 }
337
338 /* now it can be only a combination of arithmetic types and void */
339 if (t == SIGNED || t == UNSIGN) {
340 /*
341 * remember specifiers "signed" & "unsigned" in
342 * dcs->d_sign_mod
343 */
344 if (dcs->d_sign_mod != NOTSPEC)
345 /*
346 * more than one "signed" and/or "unsigned"; print
347 * an error in end_type()
348 */
349 dcs->d_terr = true;
350 dcs->d_sign_mod = t;
351 } else if (t == SHORT || t == LONG || t == QUAD) {
352 /*
353 * remember specifiers "short", "long" and "long long" in
354 * dcs->d_rank_mod
355 */
356 if (dcs->d_rank_mod != NOTSPEC)
357 /* more than one, print error in end_type() */
358 dcs->d_terr = true;
359 dcs->d_rank_mod = t;
360 } else if (t == FLOAT || t == DOUBLE) {
361 if (dcs->d_rank_mod == NOTSPEC || dcs->d_rank_mod == LONG) {
362 if (dcs->d_complex_mod != NOTSPEC
363 || (t == FLOAT && dcs->d_rank_mod == LONG))
364 dcs->d_terr = true;
365 dcs->d_complex_mod = t;
366 } else {
367 if (dcs->d_abstract_type != NOTSPEC)
368 dcs->d_terr = true;
369 dcs->d_abstract_type = t;
370 }
371 } else if (t == PTR) {
372 dcs->d_type = tp;
373 } else {
374 /*
375 * remember specifiers "void", "char", "int",
376 * or "_Complex" in dcs->d_abstract_type
377 */
378 if (dcs->d_abstract_type != NOTSPEC)
379 /* more than one, print error in end_type() */
380 dcs->d_terr = true;
381 dcs->d_abstract_type = t;
382 }
383 }
384
385 /*
386 * called if a list of declaration specifiers contains a typedef name
387 * and other specifiers (except struct, union, enum, typedef name)
388 */
389 static type_t *
390 tdeferr(type_t *td, tspec_t t)
391 {
392 tspec_t t2;
393
394 t2 = td->t_tspec;
395
396 switch (t) {
397 case SIGNED:
398 case UNSIGN:
399 if (t2 == CHAR || t2 == SHORT || t2 == INT || t2 == LONG ||
400 t2 == QUAD) {
401 if (!tflag)
402 /* modifying typedef with '%s'; only ... */
403 warning(5, ttab[t].tt_name);
404 td = dup_type(gettyp(merge_type_specifiers(t2, t)));
405 td->t_typedef = true;
406 return td;
407 }
408 break;
409 case SHORT:
410 if (t2 == INT || t2 == UINT) {
411 /* modifying typedef with '%s'; only qualifiers ... */
412 warning(5, "short");
413 td = dup_type(gettyp(t2 == INT ? SHORT : USHORT));
414 td->t_typedef = true;
415 return td;
416 }
417 break;
418 case LONG:
419 if (t2 == INT || t2 == UINT || t2 == LONG || t2 == ULONG ||
420 t2 == FLOAT || t2 == DOUBLE || t2 == DCOMPLEX) {
421 /* modifying typedef with '%s'; only qualifiers ... */
422 warning(5, "long");
423 if (t2 == INT) {
424 td = gettyp(LONG);
425 } else if (t2 == UINT) {
426 td = gettyp(ULONG);
427 } else if (t2 == LONG) {
428 td = gettyp(QUAD);
429 } else if (t2 == ULONG) {
430 td = gettyp(UQUAD);
431 } else if (t2 == FLOAT) {
432 td = gettyp(DOUBLE);
433 } else if (t2 == DOUBLE) {
434 td = gettyp(LDOUBLE);
435 } else if (t2 == DCOMPLEX) {
436 td = gettyp(LCOMPLEX);
437 }
438 td = dup_type(td);
439 td->t_typedef = true;
440 return td;
441 }
442 break;
443 /* LINTED206: (enumeration values not handled in switch) */
444 case NOTSPEC:
445 case USHORT:
446 case UCHAR:
447 case SCHAR:
448 case CHAR:
449 case BOOL:
450 case FUNC:
451 case ARRAY:
452 case PTR:
453 case ENUM:
454 case UNION:
455 case STRUCT:
456 case VOID:
457 case LDOUBLE:
458 case FLOAT:
459 case DOUBLE:
460 case UQUAD:
461 case QUAD:
462 #ifdef INT128_SIZE
463 case UINT128:
464 case INT128:
465 #endif
466 case ULONG:
467 case UINT:
468 case INT:
469 case FCOMPLEX:
470 case DCOMPLEX:
471 case LCOMPLEX:
472 case COMPLEX:
473 break;
474 }
475
476 /* Anything other is not accepted. */
477
478 dcs->d_terr = true;
479 return td;
480 }
481
482 /*
483 * Remember the symbol of a typedef name (2nd arg) in a struct, union
484 * or enum tag if the typedef name is the first defined for this tag.
485 *
486 * If the tag is unnamed, the typdef name is used for identification
487 * of this tag in lint2. Although it's possible that more than one typedef
488 * name is defined for one tag, the first name defined should be unique
489 * if the tag is unnamed.
490 */
491 static void
492 settdsym(type_t *tp, sym_t *sym)
493 {
494 tspec_t t;
495
496 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
497 if (tp->t_str->sou_first_typedef == NULL)
498 tp->t_str->sou_first_typedef = sym;
499 } else if (t == ENUM) {
500 if (tp->t_enum->en_first_typedef == NULL)
501 tp->t_enum->en_first_typedef = sym;
502 }
503 }
504
505 static size_t
506 bitfieldsize(sym_t **mem)
507 {
508 size_t len = (*mem)->s_type->t_flen;
509 while (*mem != NULL && (*mem)->s_type->t_bitfield) {
510 len += (*mem)->s_type->t_flen;
511 *mem = (*mem)->s_next;
512 }
513 return ((len + INT_SIZE - 1) / INT_SIZE) * INT_SIZE;
514 }
515
516 static void
517 setpackedsize(type_t *tp)
518 {
519 struct_or_union *sp;
520 sym_t *mem;
521
522 switch (tp->t_tspec) {
523 case STRUCT:
524 case UNION:
525 sp = tp->t_str;
526 sp->sou_size_in_bits = 0;
527 for (mem = sp->sou_first_member;
528 mem != NULL; mem = mem->s_next) {
529 if (mem->s_type->t_bitfield) {
530 sp->sou_size_in_bits += bitfieldsize(&mem);
531 if (mem == NULL)
532 break;
533 }
534 size_t x = (size_t)type_size_in_bits(mem->s_type);
535 if (tp->t_tspec == STRUCT)
536 sp->sou_size_in_bits += x;
537 else if (x > sp->sou_size_in_bits)
538 sp->sou_size_in_bits = x;
539 }
540 break;
541 default:
542 /* %s attribute ignored for %s */
543 warning(326, "packed", type_name(tp));
544 break;
545 }
546 }
547
548 void
549 addpacked(void)
550 {
551 if (dcs->d_type == NULL)
552 dcs->d_packed = true;
553 else
554 setpackedsize(dcs->d_type);
555 }
556
557 void
558 add_attr_used(void)
559 {
560 dcs->d_used = true;
561 }
562
563 /*
564 * Remember a qualifier which is part of the declaration specifiers
565 * (and not the declarator) in the top element of the declaration stack.
566 * Also detect multiple qualifiers of the same kind.
567
568 * The remembered qualifier is used by end_type() to construct the type
569 * for all declarators.
570 */
571 void
572 add_qualifier(tqual_t q)
573 {
574
575 if (q == CONST) {
576 if (dcs->d_const) {
577 /* duplicate '%s' */
578 warning(10, "const");
579 }
580 dcs->d_const = true;
581 } else if (q == VOLATILE) {
582 if (dcs->d_volatile) {
583 /* duplicate '%s' */
584 warning(10, "volatile");
585 }
586 dcs->d_volatile = true;
587 } else {
588 lint_assert(q == RESTRICT || q == THREAD);
589 /* Silently ignore these qualifiers. */
590 }
591 }
592
593 /*
594 * Go to the next declaration level (structs, nested structs, blocks,
595 * argument declaration lists ...)
596 */
597 void
598 begin_declaration_level(scl_t sc)
599 {
600 dinfo_t *di;
601
602 /* put a new element on the declaration stack */
603 di = xcalloc(1, sizeof(*di));
604 di->d_next = dcs;
605 dcs = di;
606 di->d_ctx = sc;
607 di->d_ldlsym = &di->d_dlsyms;
608 if (dflag)
609 (void)printf("%s(%p %s)\n", __func__, dcs, scl_name(sc));
610 }
611
612 /*
613 * Go back to previous declaration level
614 */
615 void
616 end_declaration_level(void)
617 {
618 dinfo_t *di;
619
620 if (dflag)
621 (void)printf("%s(%p %s)\n",
622 __func__, dcs, scl_name(dcs->d_ctx));
623
624 lint_assert(dcs->d_next != NULL);
625 di = dcs;
626 dcs = di->d_next;
627 switch (di->d_ctx) {
628 case MOS:
629 case MOU:
630 case CTCONST:
631 /*
632 * Symbols declared in (nested) structs or enums are
633 * part of the next level (they are removed from the
634 * symbol table if the symbols of the outer level are
635 * removed).
636 */
637 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL)
638 dcs->d_ldlsym = di->d_ldlsym;
639 break;
640 case ARG:
641 /*
642 * All symbols in dcs->d_dlsyms are introduced in old style
643 * argument declarations (it's not clean, but possible).
644 * They are appended to the list of symbols declared in
645 * an old style argument identifier list or a new style
646 * parameter type list.
647 */
648 if (di->d_dlsyms != NULL) {
649 *di->d_ldlsym = dcs->d_func_proto_syms;
650 dcs->d_func_proto_syms = di->d_dlsyms;
651 }
652 break;
653 case ABSTRACT:
654 /*
655 * casts and sizeof
656 * Append all symbols declared in the abstract declaration
657 * to the list of symbols declared in the surrounding
658 * declaration or block.
659 * XXX I'm not sure whether they should be removed from the
660 * symbol table now or later.
661 */
662 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL)
663 dcs->d_ldlsym = di->d_ldlsym;
664 break;
665 case AUTO:
666 /* check usage of local vars */
667 check_usage(di);
668 /* FALLTHROUGH */
669 case PROTO_ARG:
670 /* usage of arguments will be checked by funcend() */
671 rmsyms(di->d_dlsyms);
672 break;
673 case EXTERN:
674 /* there is nothing after external declarations */
675 /* FALLTHROUGH */
676 default:
677 lint_assert(/*CONSTCOND*/false);
678 }
679 free(di);
680 }
681
682 /*
683 * Set flag d_asm in all declaration stack elements up to the
684 * outermost one.
685 *
686 * This is used to mark compound statements which have, possibly in
687 * nested compound statements, asm statements. For these compound
688 * statements no warnings about unused or uninitialized variables are
689 * printed.
690 *
691 * There is no need to clear d_asm in dinfo structs with context AUTO,
692 * because these structs are freed at the end of the compound statement.
693 * But it must be cleared in the outermost dinfo struct, which has
694 * context EXTERN. This could be done in begin_type() and would work for C90,
695 * but not for C99 or C++ (due to mixed statements and declarations). Thus
696 * we clear it in global_clean_up_decl(), which is used to do some cleanup
697 * after global declarations/definitions.
698 */
699 void
700 setasm(void)
701 {
702 dinfo_t *di;
703
704 for (di = dcs; di != NULL; di = di->d_next)
705 di->d_asm = true;
706 }
707
708 /*
709 * Clean all elements of the top element of declaration stack which
710 * will be used by the next declaration
711 */
712 void
713 begin_type(void)
714 {
715
716 dcs->d_abstract_type = NOTSPEC;
717 dcs->d_complex_mod = NOTSPEC;
718 dcs->d_sign_mod = NOTSPEC;
719 dcs->d_rank_mod = NOTSPEC;
720 dcs->d_scl = NOSCL;
721 dcs->d_type = NULL;
722 dcs->d_const = false;
723 dcs->d_volatile = false;
724 dcs->d_inline = false;
725 dcs->d_mscl = false;
726 dcs->d_terr = false;
727 dcs->d_nonempty_decl = false;
728 dcs->d_notyp = false;
729 }
730
731 static void
732 dcs_adjust_storage_class(void)
733 {
734 if (dcs->d_ctx == EXTERN) {
735 if (dcs->d_scl == REG || dcs->d_scl == AUTO) {
736 /* illegal storage class */
737 error(8);
738 dcs->d_scl = NOSCL;
739 }
740 } else if (dcs->d_ctx == ARG || dcs->d_ctx == PROTO_ARG) {
741 if (dcs->d_scl != NOSCL && dcs->d_scl != REG) {
742 /* only register valid as formal parameter storage... */
743 error(9);
744 dcs->d_scl = NOSCL;
745 }
746 }
747 }
748
749 /*
750 * Create a type structure from the information gathered in
751 * the declaration stack.
752 * Complain about storage classes which are not possible in current
753 * context.
754 */
755 void
756 end_type(void)
757 {
758 tspec_t t, s, l, c;
759 type_t *tp;
760
761 t = dcs->d_abstract_type; /* VOID, BOOL, CHAR, INT or COMPLEX */
762 c = dcs->d_complex_mod; /* FLOAT or DOUBLE */
763 s = dcs->d_sign_mod; /* SIGNED or UNSIGN */
764 l = dcs->d_rank_mod; /* SHORT, LONG or QUAD */
765 tp = dcs->d_type;
766
767 #ifdef DEBUG
768 printf("%s: %s\n", __func__, type_name(tp));
769 #endif
770 if (t == NOTSPEC && s == NOTSPEC && l == NOTSPEC && c == NOTSPEC &&
771 tp == NULL)
772 dcs->d_notyp = true;
773 if (t == NOTSPEC && s == NOTSPEC && (l == NOTSPEC || l == LONG) &&
774 tp == NULL)
775 t = c;
776
777 if (tp != NULL) {
778 lint_assert(t == NOTSPEC);
779 lint_assert(s == NOTSPEC);
780 lint_assert(l == NOTSPEC);
781 }
782
783 if (tp == NULL) {
784 switch (t) {
785 case BOOL:
786 break;
787 case NOTSPEC:
788 t = INT;
789 /* FALLTHROUGH */
790 case INT:
791 if (s == NOTSPEC)
792 s = SIGNED;
793 break;
794 case CHAR:
795 if (l != NOTSPEC) {
796 dcs->d_terr = true;
797 l = NOTSPEC;
798 }
799 break;
800 case FLOAT:
801 if (l == LONG) {
802 l = NOTSPEC;
803 t = DOUBLE;
804 if (!tflag)
805 /* use 'double' instead of 'long ... */
806 warning(6);
807 }
808 break;
809 case DOUBLE:
810 if (l != LONG)
811 break;
812 /* FALLTHROUGH */
813 case LDOUBLE:
814 l = NOTSPEC;
815 t = LDOUBLE;
816 if (tflag)
817 /* 'long double' is illegal in ... */
818 warning(266);
819 break;
820 case DCOMPLEX:
821 if (l == LONG) {
822 l = NOTSPEC;
823 t = LCOMPLEX;
824 }
825 break;
826 case VOID:
827 case FCOMPLEX:
828 case LCOMPLEX:
829 break;
830 default:
831 if (is_integer(t))
832 break;
833 INTERNAL_ERROR("end_type(%s)", tspec_name(t));
834 }
835 if (t != INT && t != CHAR && (s != NOTSPEC || l != NOTSPEC)) {
836 dcs->d_terr = true;
837 l = s = NOTSPEC;
838 }
839 if (l != NOTSPEC)
840 t = l;
841 dcs->d_type = gettyp(merge_type_specifiers(t, s));
842 }
843
844 if (dcs->d_mscl) {
845 /* only one storage class allowed */
846 error(7);
847 }
848 if (dcs->d_terr) {
849 /* illegal type combination */
850 error(4);
851 }
852
853 dcs_adjust_storage_class();
854
855 if (dcs->d_const && dcs->d_type->t_const) {
856 lint_assert(dcs->d_type->t_typedef);
857 /* typedef already qualified with '%s' */
858 warning(68, "const");
859 }
860 if (dcs->d_volatile && dcs->d_type->t_volatile) {
861 lint_assert(dcs->d_type->t_typedef);
862 /* typedef already qualified with '%s' */
863 warning(68, "volatile");
864 }
865
866 if (dcs->d_const || dcs->d_volatile) {
867 dcs->d_type = dup_type(dcs->d_type);
868 dcs->d_type->t_const |= dcs->d_const;
869 dcs->d_type->t_volatile |= dcs->d_volatile;
870 }
871 }
872
873 /*
874 * Merge type specifiers (char, ..., long long, signed, unsigned).
875 */
876 static tspec_t
877 merge_type_specifiers(tspec_t t, tspec_t s)
878 {
879
880 if (s != SIGNED && s != UNSIGN)
881 return t;
882
883 if (t == CHAR)
884 return s == SIGNED ? SCHAR : UCHAR;
885 if (t == SHORT)
886 return s == SIGNED ? SHORT : USHORT;
887 if (t == INT)
888 return s == SIGNED ? INT : UINT;
889 if (t == LONG)
890 return s == SIGNED ? LONG : ULONG;
891 if (t == QUAD)
892 return s == SIGNED ? QUAD : UQUAD;
893 return t;
894 }
895
896 /*
897 * Return the length of a type in bits.
898 *
899 * Printing a message if the outermost dimension of an array is 0 must
900 * be done by the caller. All other problems are reported by length()
901 * if name is not NULL.
902 */
903 int
904 length(const type_t *tp, const char *name)
905 {
906 int elem, elsz;
907
908 elem = 1;
909 while (tp != NULL && tp->t_tspec == ARRAY) {
910 elem *= tp->t_dim;
911 tp = tp->t_subt;
912 }
913 if (tp == NULL)
914 return -1;
915
916 switch (tp->t_tspec) {
917 case FUNC:
918 /* compiler takes size of function */
919 INTERNAL_ERROR("%s", msgs[12]);
920 /* NOTREACHED */
921 case STRUCT:
922 case UNION:
923 if (is_incomplete(tp) && name != NULL) {
924 /* '%s' has incomplete type '%s' */
925 error(31, name, type_name(tp));
926 }
927 elsz = tp->t_str->sou_size_in_bits;
928 break;
929 case ENUM:
930 if (is_incomplete(tp) && name != NULL) {
931 /* incomplete enum type: %s */
932 warning(13, name);
933 }
934 /* FALLTHROUGH */
935 default:
936 elsz = size_in_bits(tp->t_tspec);
937 if (elsz <= 0)
938 INTERNAL_ERROR("length(%d)", elsz);
939 break;
940 }
941 return elem * elsz;
942 }
943
944 int
945 alignment_in_bits(const type_t *tp)
946 {
947 size_t a;
948 tspec_t t;
949
950 while (tp != NULL && tp->t_tspec == ARRAY)
951 tp = tp->t_subt;
952
953 if (tp == NULL)
954 return -1;
955
956 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
957 a = tp->t_str->sou_align_in_bits;
958 } else if (t == FUNC) {
959 /* compiler takes alignment of function */
960 error(14);
961 a = WORST_ALIGN(1) * CHAR_SIZE;
962 } else {
963 if ((a = size_in_bits(t)) == 0) {
964 a = CHAR_SIZE;
965 } else if (a > WORST_ALIGN(1) * CHAR_SIZE) {
966 a = WORST_ALIGN(1) * CHAR_SIZE;
967 }
968 }
969 lint_assert(a >= CHAR_SIZE);
970 lint_assert(a <= WORST_ALIGN(1) * CHAR_SIZE);
971 return a;
972 }
973
974 /*
975 * Concatenate two lists of symbols by s_next. Used by declarations of
976 * struct/union/enum elements and parameters.
977 */
978 sym_t *
979 lnklst(sym_t *l1, sym_t *l2)
980 {
981 sym_t *l;
982
983 if ((l = l1) == NULL)
984 return l2;
985 while (l1->s_next != NULL)
986 l1 = l1->s_next;
987 l1->s_next = l2;
988 return l;
989 }
990
991 /*
992 * Check if the type of the given symbol is valid and print an error
993 * message if it is not.
994 *
995 * Invalid types are:
996 * - arrays of incomplete types or functions
997 * - functions returning arrays or functions
998 * - void types other than type of function or pointer
999 */
1000 void
1001 check_type(sym_t *sym)
1002 {
1003 tspec_t to, t;
1004 type_t **tpp, *tp;
1005
1006 tpp = &sym->s_type;
1007 to = NOTSPEC;
1008 while ((tp = *tpp) != NULL) {
1009 t = tp->t_tspec;
1010 /*
1011 * If this is the type of an old style function definition,
1012 * a better warning is printed in funcdef().
1013 */
1014 if (t == FUNC && !tp->t_proto &&
1015 !(to == NOTSPEC && sym->s_osdef)) {
1016 if (sflag && hflag)
1017 /* function declaration is not a prototype */
1018 warning(287);
1019 }
1020 if (to == FUNC) {
1021 if (t == FUNC || t == ARRAY) {
1022 /* function returns illegal type */
1023 error(15);
1024 if (t == FUNC) {
1025 *tpp = derive_type(*tpp, PTR);
1026 } else {
1027 *tpp = derive_type((*tpp)->t_subt, PTR);
1028 }
1029 return;
1030 } else if (tp->t_const || tp->t_volatile) {
1031 if (sflag) { /* XXX or better !tflag ? */
1032 /* function cannot return const... */
1033 warning(228);
1034 }
1035 }
1036 } if (to == ARRAY) {
1037 if (t == FUNC) {
1038 /* array of function is illegal */
1039 error(16);
1040 *tpp = gettyp(INT);
1041 return;
1042 } else if (t == ARRAY && tp->t_dim == 0) {
1043 /* null dimension */
1044 error(17);
1045 return;
1046 } else if (t == VOID) {
1047 /* illegal use of 'void' */
1048 error(18);
1049 *tpp = gettyp(INT);
1050 #if 0 /* errors are produced by length() */
1051 } else if (is_incomplete(tp)) {
1052 /* array of incomplete type */
1053 if (sflag) {
1054 /* array of incomplete type */
1055 error(301);
1056 } else {
1057 /* array of incomplete type */
1058 warning(301);
1059 }
1060 #endif
1061 }
1062 } else if (to == NOTSPEC && t == VOID) {
1063 if (dcs->d_ctx == PROTO_ARG) {
1064 if (sym->s_scl != ABSTRACT) {
1065 lint_assert(sym->s_name != unnamed);
1066 /* void param. cannot have name: %s */
1067 error(61, sym->s_name);
1068 *tpp = gettyp(INT);
1069 }
1070 } else if (dcs->d_ctx == ABSTRACT) {
1071 /* ok */
1072 } else if (sym->s_scl != TYPEDEF) {
1073 /* void type for '%s' */
1074 error(19, sym->s_name);
1075 *tpp = gettyp(INT);
1076 }
1077 }
1078 if (t == VOID && to != PTR) {
1079 if (tp->t_const || tp->t_volatile) {
1080 /* inappropriate qualifiers with 'void' */
1081 warning(69);
1082 tp->t_const = tp->t_volatile = false;
1083 }
1084 }
1085 tpp = &tp->t_subt;
1086 to = t;
1087 }
1088 }
1089
1090 /*
1091 * In traditional C, the only portable type for bit-fields is unsigned int.
1092 *
1093 * In C90, the only allowed types for bit-fields are int, signed int and
1094 * unsigned int (3.5.2.1). There is no mention of implementation-defined
1095 * types.
1096 *
1097 * In C99, the only portable types for bit-fields are _Bool, signed int and
1098 * unsigned int (6.7.2.1p4). In addition, C99 allows "or some other
1099 * implementation-defined type".
1100 */
1101 static void
1102 check_bit_field_type(sym_t *dsym, type_t **const inout_tp, tspec_t *inout_t)
1103 {
1104 type_t *tp = *inout_tp;
1105 tspec_t t = *inout_t;
1106
1107 if (t == CHAR || t == UCHAR || t == SCHAR ||
1108 t == SHORT || t == USHORT || t == ENUM) {
1109 if (!bitfieldtype_ok) {
1110 if (sflag) {
1111 /* bit-field type '%s' invalid in ANSI C */
1112 warning(273, type_name(tp));
1113 } else if (pflag) {
1114 /* nonportable bit-field type '%s' */
1115 warning(34, type_name(tp));
1116 }
1117 }
1118 } else if (t == INT && dcs->d_sign_mod == NOTSPEC) {
1119 if (pflag && !bitfieldtype_ok) {
1120 /* bit-field of type plain 'int' has ... */
1121 warning(344);
1122 }
1123 } else if (t != INT && t != UINT && t != BOOL) {
1124 /*
1125 * Non-integer types are always illegal for bitfields,
1126 * regardless of BITFIELDTYPE. Integer types not dealt with
1127 * above are okay only if BITFIELDTYPE is in effect.
1128 */
1129 if (!(bitfieldtype_ok || gflag) || !is_integer(t)) {
1130 /* illegal bit-field type '%s' */
1131 warning(35, type_name(tp));
1132 int sz = tp->t_flen;
1133 dsym->s_type = tp = dup_type(gettyp(t = INT));
1134 if ((tp->t_flen = sz) > size_in_bits(t))
1135 tp->t_flen = size_in_bits(t);
1136 *inout_t = t;
1137 *inout_tp = tp;
1138 }
1139 }
1140 }
1141
1142 static void
1143 declare_bit_field(sym_t *dsym, tspec_t *inout_t, type_t **const inout_tp)
1144 {
1145
1146 check_bit_field_type(dsym, inout_tp, inout_t);
1147
1148 type_t *const tp = *inout_tp;
1149 tspec_t const t = *inout_t;
1150 if (tp->t_flen < 0 || tp->t_flen > (ssize_t)size_in_bits(t)) {
1151 /* illegal bit-field size: %d */
1152 error(36, tp->t_flen);
1153 tp->t_flen = size_in_bits(t);
1154 } else if (tp->t_flen == 0 && dsym->s_name != unnamed) {
1155 /* zero size bit-field */
1156 error(37);
1157 tp->t_flen = size_in_bits(t);
1158 }
1159 if (dsym->s_scl == MOU) {
1160 /* illegal use of bit-field */
1161 error(41);
1162 dsym->s_type->t_bitfield = false;
1163 dsym->s_bitfield = false;
1164 }
1165 }
1166
1167 /*
1168 * Process the declarator of a struct/union element.
1169 */
1170 sym_t *
1171 declarator_1_struct_union(sym_t *dsym)
1172 {
1173 type_t *tp;
1174 tspec_t t;
1175 int sz;
1176 int o = 0; /* Appease GCC */
1177
1178 lint_assert(dsym->s_scl == MOS || dsym->s_scl == MOU);
1179
1180 if (dcs->d_redeclared_symbol != NULL) {
1181 /* should be ensured by storesym() */
1182 lint_assert(dcs->d_redeclared_symbol->s_scl == MOS ||
1183 dcs->d_redeclared_symbol->s_scl == MOU);
1184
1185 if (dsym->s_styp == dcs->d_redeclared_symbol->s_styp) {
1186 /* duplicate member name: %s */
1187 error(33, dsym->s_name);
1188 rmsym(dcs->d_redeclared_symbol);
1189 }
1190 }
1191
1192 check_type(dsym);
1193
1194 t = (tp = dsym->s_type)->t_tspec;
1195
1196 if (dsym->s_bitfield) {
1197 declare_bit_field(dsym, &t, &tp);
1198 } else if (t == FUNC) {
1199 /* function illegal in structure or union */
1200 error(38);
1201 dsym->s_type = tp = derive_type(tp, t = PTR);
1202 }
1203
1204 /*
1205 * bit-fields of length 0 are not warned about because length()
1206 * does not return the length of the bit-field but the length
1207 * of the type the bit-field is packed in (it's ok)
1208 */
1209 if ((sz = length(dsym->s_type, dsym->s_name)) == 0) {
1210 if (t == ARRAY && dsym->s_type->t_dim == 0) {
1211 /* zero sized array in struct is a C99 extension: %s */
1212 c99ism(39, dsym->s_name);
1213 }
1214 }
1215
1216 if (dcs->d_ctx == MOU) {
1217 o = dcs->d_offset;
1218 dcs->d_offset = 0;
1219 }
1220 if (dsym->s_bitfield) {
1221 align(alignment_in_bits(tp), tp->t_flen);
1222 dsym->s_value.v_quad =
1223 (dcs->d_offset / size_in_bits(t)) * size_in_bits(t);
1224 tp->t_foffs = dcs->d_offset - (int)dsym->s_value.v_quad;
1225 dcs->d_offset += tp->t_flen;
1226 } else {
1227 align(alignment_in_bits(tp), 0);
1228 dsym->s_value.v_quad = dcs->d_offset;
1229 dcs->d_offset += sz;
1230 }
1231 if (dcs->d_ctx == MOU) {
1232 if (o > dcs->d_offset)
1233 dcs->d_offset = o;
1234 }
1235
1236 check_function_definition(dsym, false);
1237
1238 /*
1239 * Clear the BITFIELDTYPE indicator after processing each
1240 * structure element.
1241 */
1242 bitfieldtype_ok = false;
1243
1244 return dsym;
1245 }
1246
1247 /*
1248 * Aligns next structure element as required.
1249 *
1250 * al contains the required alignment, len the length of a bit-field.
1251 */
1252 static void
1253 align(int al, int len)
1254 {
1255 int no;
1256
1257 /*
1258 * The alignment of the current element becomes the alignment of
1259 * the struct/union if it is larger than the current alignment
1260 * of the struct/union.
1261 */
1262 if (al > dcs->d_sou_align_in_bits)
1263 dcs->d_sou_align_in_bits = al;
1264
1265 no = (dcs->d_offset + (al - 1)) & ~(al - 1);
1266 if (len == 0 || dcs->d_offset + len > no)
1267 dcs->d_offset = no;
1268 }
1269
1270 /*
1271 * Remember the width of the field in its type structure.
1272 */
1273 sym_t *
1274 bitfield(sym_t *dsym, int len)
1275 {
1276
1277 if (dsym == NULL) {
1278 dsym = getblk(sizeof(*dsym));
1279 dsym->s_name = unnamed;
1280 dsym->s_kind = FMEMBER;
1281 dsym->s_scl = MOS;
1282 dsym->s_type = gettyp(UINT);
1283 dsym->s_block_level = -1;
1284 }
1285 dsym->s_type = dup_type(dsym->s_type);
1286 dsym->s_type->t_bitfield = true;
1287 dsym->s_type->t_flen = len;
1288 dsym->s_bitfield = true;
1289 return dsym;
1290 }
1291
1292 /*
1293 * A sequence of asterisks and qualifiers, from right to left. For example,
1294 * 'const ***volatile **const volatile' results in [cvp, p, vp, p, p]. The
1295 * leftmost 'const' is not included in this list, it is stored in dcs->d_const
1296 * instead.
1297 */
1298 qual_ptr *
1299 merge_qualified_pointer(qual_ptr *p1, qual_ptr *p2)
1300 {
1301 qual_ptr *tail;
1302
1303 if (p2 == NULL)
1304 return p1; /* for optional qualifiers */
1305
1306 if (p2->p_pointer) {
1307 /* append p1 to p2, keeping p2 */
1308 for (tail = p2; tail->p_next != NULL; tail = tail->p_next)
1309 continue;
1310 tail->p_next = p1;
1311 return p2;
1312 }
1313
1314 /* merge p2 into p1, keeping p1 */
1315 if (p2->p_const) {
1316 if (p1->p_const) {
1317 /* duplicate '%s' */
1318 warning(10, "const");
1319 }
1320 p1->p_const = true;
1321 }
1322 if (p2->p_volatile) {
1323 if (p1->p_volatile) {
1324 /* duplicate '%s' */
1325 warning(10, "volatile");
1326 }
1327 p1->p_volatile = true;
1328 }
1329 free(p2);
1330 return p1;
1331 }
1332
1333 /*
1334 * The following 3 functions extend the type of a declarator with
1335 * pointer, function and array types.
1336 *
1337 * The current type is the type built by end_type() (dcs->d_type) and
1338 * pointer, function and array types already added for this
1339 * declarator. The new type extension is inserted between both.
1340 */
1341 sym_t *
1342 add_pointer(sym_t *decl, qual_ptr *p)
1343 {
1344 type_t **tpp, *tp;
1345 qual_ptr *next;
1346
1347 tpp = &decl->s_type;
1348 while (*tpp != NULL && *tpp != dcs->d_type)
1349 tpp = &(*tpp)->t_subt;
1350 if (*tpp == NULL)
1351 return decl;
1352
1353 while (p != NULL) {
1354 *tpp = tp = getblk(sizeof(*tp));
1355 tp->t_tspec = PTR;
1356 tp->t_const = p->p_const;
1357 tp->t_volatile = p->p_volatile;
1358 *(tpp = &tp->t_subt) = dcs->d_type;
1359 next = p->p_next;
1360 free(p);
1361 p = next;
1362 }
1363 return decl;
1364 }
1365
1366 /*
1367 * If a dimension was specified, dim is true, otherwise false
1368 * n is the specified dimension
1369 */
1370 sym_t *
1371 add_array(sym_t *decl, bool dim, int n)
1372 {
1373 type_t **tpp, *tp;
1374
1375 tpp = &decl->s_type;
1376 while (*tpp != NULL && *tpp != dcs->d_type)
1377 tpp = &(*tpp)->t_subt;
1378 if (*tpp == NULL)
1379 return decl;
1380
1381 *tpp = tp = getblk(sizeof(*tp));
1382 tp->t_tspec = ARRAY;
1383 tp->t_subt = dcs->d_type;
1384 tp->t_dim = n;
1385
1386 if (n < 0) {
1387 /* negative array dimension (%d) */
1388 error(20, n);
1389 n = 0;
1390 } else if (n == 0 && dim) {
1391 /* zero sized array is a C99 extension */
1392 c99ism(322);
1393 } else if (n == 0 && !dim) {
1394 setcomplete(tp, false);
1395 }
1396
1397 return decl;
1398 }
1399
1400 sym_t *
1401 add_function(sym_t *decl, sym_t *args)
1402 {
1403 type_t **tpp, *tp;
1404
1405 if (dcs->d_proto) {
1406 if (tflag)
1407 /* function prototypes are illegal in traditional C */
1408 warning(270);
1409 args = new_style_function(decl, args);
1410 } else {
1411 old_style_function(decl, args);
1412 }
1413
1414 /*
1415 * The symbols are removed from the symbol table by
1416 * end_declaration_level after add_function. To be able to restore
1417 * them if this is a function definition, a pointer to the list of all
1418 * symbols is stored in dcs->d_next->d_func_proto_syms. Also a list of
1419 * the arguments (concatenated by s_next) is stored in
1420 * dcs->d_next->d_func_args. (dcs->d_next must be used because *dcs is
1421 * the declaration stack element created for the list of params and is
1422 * removed after add_function.)
1423 */
1424 if (dcs->d_next->d_ctx == EXTERN &&
1425 decl->s_type == dcs->d_next->d_type) {
1426 dcs->d_next->d_func_proto_syms = dcs->d_dlsyms;
1427 dcs->d_next->d_func_args = args;
1428 }
1429
1430 tpp = &decl->s_type;
1431 while (*tpp != NULL && *tpp != dcs->d_next->d_type)
1432 tpp = &(*tpp)->t_subt;
1433 if (*tpp == NULL)
1434 return decl;
1435
1436 *tpp = tp = getblk(sizeof(*tp));
1437 tp->t_tspec = FUNC;
1438 tp->t_subt = dcs->d_next->d_type;
1439 if ((tp->t_proto = dcs->d_proto) != false)
1440 tp->t_args = args;
1441 tp->t_vararg = dcs->d_vararg;
1442
1443 return decl;
1444 }
1445
1446 /*
1447 * Called for new style function declarations.
1448 */
1449 /* ARGSUSED */
1450 static sym_t *
1451 new_style_function(sym_t *decl, sym_t *args)
1452 {
1453 sym_t *arg, *sym;
1454 scl_t sc;
1455 int n;
1456
1457 /*
1458 * Declarations of structs/unions/enums in param lists are legal,
1459 * but senseless.
1460 */
1461 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
1462 sc = sym->s_scl;
1463 if (sc == STRUCT_TAG || sc == UNION_TAG || sc == ENUM_TAG) {
1464 /* dubious tag declaration: %s %s */
1465 warning(85, storage_class_name(sc), sym->s_name);
1466 }
1467 }
1468
1469 n = 1;
1470 for (arg = args; arg != NULL; arg = arg->s_next) {
1471 if (arg->s_type->t_tspec == VOID) {
1472 if (n > 1 || arg->s_next != NULL) {
1473 /* void must be sole parameter */
1474 error(60);
1475 arg->s_type = gettyp(INT);
1476 }
1477 }
1478 n++;
1479 }
1480
1481 /* return NULL if first param is VOID */
1482 return args != NULL && args->s_type->t_tspec != VOID ? args : NULL;
1483 }
1484
1485 /*
1486 * Called for old style function declarations.
1487 */
1488 static void
1489 old_style_function(sym_t *decl, sym_t *args)
1490 {
1491
1492 /*
1493 * Remember list of params only if this is really seams to be
1494 * a function definition.
1495 */
1496 if (dcs->d_next->d_ctx == EXTERN &&
1497 decl->s_type == dcs->d_next->d_type) {
1498 /*
1499 * We assume that this becomes a function definition. If
1500 * we are wrong, it's corrected in check_function_definition().
1501 */
1502 if (args != NULL) {
1503 decl->s_osdef = true;
1504 decl->s_args = args;
1505 }
1506 } else {
1507 if (args != NULL)
1508 /* function prototype parameters must have types */
1509 warning(62);
1510 }
1511 }
1512
1513 /*
1514 * Lists of identifiers in functions declarations are allowed only if
1515 * it's also a function definition. If this is not the case, print an
1516 * error message.
1517 */
1518 void
1519 check_function_definition(sym_t *sym, bool msg)
1520 {
1521
1522 if (sym->s_osdef) {
1523 if (msg) {
1524 /* incomplete or misplaced function definition */
1525 error(22);
1526 }
1527 sym->s_osdef = false;
1528 sym->s_args = NULL;
1529 }
1530 }
1531
1532 /*
1533 * Process the name in a declarator.
1534 * The symbol gets one of the storage classes EXTERN, STATIC, AUTO or
1535 * TYPEDEF.
1536 * s_def and s_reg are valid after declarator_name().
1537 */
1538 sym_t *
1539 declarator_name(sym_t *sym)
1540 {
1541 scl_t sc = NOSCL;
1542
1543 if (sym->s_scl == NOSCL) {
1544 dcs->d_redeclared_symbol = NULL;
1545 } else if (sym->s_defarg) {
1546 sym->s_defarg = false;
1547 dcs->d_redeclared_symbol = NULL;
1548 } else {
1549 dcs->d_redeclared_symbol = sym;
1550 sym = pushdown(sym);
1551 }
1552
1553 switch (dcs->d_ctx) {
1554 case MOS:
1555 case MOU:
1556 /* Set parent */
1557 sym->s_styp = dcs->d_tagtyp->t_str;
1558 sym->s_def = DEF;
1559 sym->s_value.v_tspec = INT;
1560 sc = dcs->d_ctx;
1561 break;
1562 case EXTERN:
1563 /*
1564 * static and external symbols without "extern" are
1565 * considered to be tentative defined, external
1566 * symbols with "extern" are declared, and typedef names
1567 * are defined. Tentative defined and declared symbols
1568 * may become defined if an initializer is present or
1569 * this is a function definition.
1570 */
1571 if ((sc = dcs->d_scl) == NOSCL) {
1572 sc = EXTERN;
1573 sym->s_def = TDEF;
1574 } else if (sc == STATIC) {
1575 sym->s_def = TDEF;
1576 } else if (sc == TYPEDEF) {
1577 sym->s_def = DEF;
1578 } else {
1579 lint_assert(sc == EXTERN);
1580 sym->s_def = DECL;
1581 }
1582 break;
1583 case PROTO_ARG:
1584 sym->s_arg = true;
1585 /* FALLTHROUGH */
1586 case ARG:
1587 if ((sc = dcs->d_scl) == NOSCL) {
1588 sc = AUTO;
1589 } else {
1590 lint_assert(sc == REG);
1591 sym->s_reg = true;
1592 sc = AUTO;
1593 }
1594 sym->s_def = DEF;
1595 break;
1596 case AUTO:
1597 if ((sc = dcs->d_scl) == NOSCL) {
1598 /*
1599 * XXX somewhat ugly because we dont know whether
1600 * this is AUTO or EXTERN (functions). If we are
1601 * wrong it must be corrected in declare_local(),
1602 * where we have the necessary type information.
1603 */
1604 sc = AUTO;
1605 sym->s_def = DEF;
1606 } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF) {
1607 sym->s_def = DEF;
1608 } else if (sc == REG) {
1609 sym->s_reg = true;
1610 sc = AUTO;
1611 sym->s_def = DEF;
1612 } else {
1613 lint_assert(sc == EXTERN);
1614 sym->s_def = DECL;
1615 }
1616 break;
1617 default:
1618 lint_assert(/*CONSTCOND*/false);
1619 }
1620 sym->s_scl = sc;
1621
1622 sym->s_type = dcs->d_type;
1623
1624 dcs->d_func_proto_syms = NULL;
1625
1626 return sym;
1627 }
1628
1629 /*
1630 * Process a name in the list of formal parameters in an old style function
1631 * definition.
1632 */
1633 sym_t *
1634 old_style_function_name(sym_t *sym)
1635 {
1636
1637 if (sym->s_scl != NOSCL) {
1638 if (block_level == sym->s_block_level) {
1639 /* redeclaration of formal parameter %s */
1640 error(21, sym->s_name);
1641 lint_assert(sym->s_defarg);
1642 }
1643 sym = pushdown(sym);
1644 }
1645 sym->s_type = gettyp(INT);
1646 sym->s_scl = AUTO;
1647 sym->s_def = DEF;
1648 sym->s_defarg = sym->s_arg = true;
1649 return sym;
1650 }
1651
1652 /*
1653 * Create the type of a tag.
1654 *
1655 * tag points to the symbol table entry of the tag
1656 * kind is the kind of the tag (STRUCT/UNION/ENUM)
1657 * decl is true if the type of the tag will be completed in this declaration
1658 * (the following token is T_LBRACE)
1659 * semi is true if the following token is T_SEMI
1660 */
1661 type_t *
1662 mktag(sym_t *tag, tspec_t kind, bool decl, bool semi)
1663 {
1664 scl_t scl;
1665 type_t *tp;
1666
1667 if (kind == STRUCT) {
1668 scl = STRUCT_TAG;
1669 } else if (kind == UNION) {
1670 scl = UNION_TAG;
1671 } else {
1672 lint_assert(kind == ENUM);
1673 scl = ENUM_TAG;
1674 }
1675
1676 if (tag != NULL) {
1677 if (tag->s_scl != NOSCL) {
1678 tag = newtag(tag, scl, decl, semi);
1679 } else {
1680 /* a new tag, no empty declaration */
1681 dcs->d_next->d_nonempty_decl = true;
1682 if (scl == ENUM_TAG && !decl) {
1683 if (!tflag && (sflag || pflag))
1684 /* forward reference to enum type */
1685 warning(42);
1686 }
1687 }
1688 if (tag->s_scl == NOSCL) {
1689 tag->s_scl = scl;
1690 tag->s_type = tp = getblk(sizeof(*tp));
1691 tp->t_packed = dcs->d_packed;
1692 } else {
1693 tp = tag->s_type;
1694 }
1695 } else {
1696 tag = getblk(sizeof(*tag));
1697 tag->s_name = unnamed;
1698 UNIQUE_CURR_POS(tag->s_def_pos);
1699 tag->s_kind = FTAG;
1700 tag->s_scl = scl;
1701 tag->s_block_level = -1;
1702 tag->s_type = tp = getblk(sizeof(*tp));
1703 tp->t_packed = dcs->d_packed;
1704 dcs->d_next->d_nonempty_decl = true;
1705 }
1706
1707 if (tp->t_tspec == NOTSPEC) {
1708 tp->t_tspec = kind;
1709 if (kind != ENUM) {
1710 tp->t_str = getblk(sizeof(*tp->t_str));
1711 tp->t_str->sou_align_in_bits = CHAR_SIZE;
1712 tp->t_str->sou_tag = tag;
1713 } else {
1714 tp->t_is_enum = true;
1715 tp->t_enum = getblk(sizeof(*tp->t_enum));
1716 tp->t_enum->en_tag = tag;
1717 }
1718 setcomplete(tp, false);
1719 }
1720 return tp;
1721 }
1722
1723 /*
1724 * Checks all possible cases of tag redeclarations.
1725 * decl is true if T_LBRACE follows
1726 * semi is true if T_SEMI follows
1727 */
1728 static sym_t *
1729 newtag(sym_t *tag, scl_t scl, bool decl, bool semi)
1730 {
1731
1732 if (tag->s_block_level < block_level) {
1733 if (semi) {
1734 /* "struct a;" */
1735 if (!tflag) {
1736 if (!sflag)
1737 /* decl. introduces new type ... */
1738 warning(44, storage_class_name(scl),
1739 tag->s_name);
1740 tag = pushdown(tag);
1741 } else if (tag->s_scl != scl) {
1742 /* base type is really '%s %s' */
1743 warning(45, storage_class_name(tag->s_scl),
1744 tag->s_name);
1745 }
1746 dcs->d_next->d_nonempty_decl = true;
1747 } else if (decl) {
1748 /* "struct a { ... } " */
1749 if (hflag)
1750 /* redefinition hides earlier one: %s */
1751 warning(43, tag->s_name);
1752 tag = pushdown(tag);
1753 dcs->d_next->d_nonempty_decl = true;
1754 } else if (tag->s_scl != scl) {
1755 /* base type is really '%s %s' */
1756 warning(45, storage_class_name(tag->s_scl),
1757 tag->s_name);
1758 /* declaration introduces new type in ANSI C: %s %s */
1759 if (!sflag) {
1760 /* decl. introduces new type in ANSI C: %s %s */
1761 warning(44, storage_class_name(scl),
1762 tag->s_name);
1763 }
1764 tag = pushdown(tag);
1765 dcs->d_next->d_nonempty_decl = true;
1766 }
1767 } else {
1768 if (tag->s_scl != scl) {
1769 /* (%s) tag redeclared */
1770 error(46, storage_class_name(tag->s_scl));
1771 print_previous_declaration(-1, tag);
1772 tag = pushdown(tag);
1773 dcs->d_next->d_nonempty_decl = true;
1774 } else if (decl && !is_incomplete(tag->s_type)) {
1775 /* (%s) tag redeclared */
1776 error(46, storage_class_name(tag->s_scl));
1777 print_previous_declaration(-1, tag);
1778 tag = pushdown(tag);
1779 dcs->d_next->d_nonempty_decl = true;
1780 } else if (semi || decl) {
1781 dcs->d_next->d_nonempty_decl = true;
1782 }
1783 }
1784 return tag;
1785 }
1786
1787 const char *
1788 storage_class_name(scl_t sc)
1789 {
1790 switch (sc) {
1791 case EXTERN: return "extern";
1792 case STATIC: return "static";
1793 case AUTO: return "auto";
1794 case REG: return "register";
1795 case TYPEDEF: return "typedef";
1796 case STRUCT_TAG:return "struct";
1797 case UNION_TAG: return "union";
1798 case ENUM_TAG: return "enum";
1799 default: lint_assert(/*CONSTCOND*/false);
1800 }
1801 }
1802
1803 /*
1804 * tp points to the type of the tag, fmem to the list of members.
1805 */
1806 type_t *
1807 complete_tag_struct_or_union(type_t *tp, sym_t *fmem)
1808 {
1809 tspec_t t;
1810 struct_or_union *sp;
1811 int n;
1812 sym_t *mem;
1813
1814 if (tp == NULL) /* in case of syntax errors */
1815 return gettyp(INT);
1816
1817 setcomplete(tp, true);
1818
1819 t = tp->t_tspec;
1820 align(dcs->d_sou_align_in_bits, 0);
1821 sp = tp->t_str;
1822 sp->sou_align_in_bits = dcs->d_sou_align_in_bits;
1823 sp->sou_first_member = fmem;
1824 if (tp->t_packed)
1825 setpackedsize(tp);
1826 else
1827 sp->sou_size_in_bits = dcs->d_offset;
1828
1829 if (sp->sou_size_in_bits == 0) {
1830 /* zero sized %s is a C9X feature */
1831 c99ism(47, ttab[t].tt_name);
1832 }
1833
1834 n = 0;
1835 for (mem = fmem; mem != NULL; mem = mem->s_next) {
1836 /* bind anonymous members to the structure */
1837 if (mem->s_styp == NULL) {
1838 mem->s_styp = sp;
1839 if (mem->s_type->t_bitfield) {
1840 sp->sou_size_in_bits += bitfieldsize(&mem);
1841 if (mem == NULL)
1842 break;
1843 }
1844 sp->sou_size_in_bits += type_size_in_bits(mem->s_type);
1845 }
1846 if (mem->s_name != unnamed)
1847 n++;
1848 }
1849
1850 if (n == 0 && sp->sou_size_in_bits != 0) {
1851 /* %s has no named members */
1852 warning(65, t == STRUCT ? "structure" : "union");
1853 }
1854 return tp;
1855 }
1856
1857 type_t *
1858 complete_tag_enum(type_t *tp, sym_t *fmem)
1859 {
1860
1861 setcomplete(tp, true);
1862 tp->t_enum->en_first_enumerator = fmem;
1863 return tp;
1864 }
1865
1866 /*
1867 * Processes the name of an enumerator in an enum declaration.
1868 *
1869 * sym points to the enumerator
1870 * val is the value of the enumerator
1871 * impl is true if the value of the enumerator was not explicitly specified.
1872 */
1873 sym_t *
1874 enumeration_constant(sym_t *sym, int val, bool impl)
1875 {
1876
1877 if (sym->s_scl != NOSCL) {
1878 if (sym->s_block_level == block_level) {
1879 /* no hflag, because this is illegal!!! */
1880 if (sym->s_arg) {
1881 /* enumeration constant hides parameter: %s */
1882 warning(57, sym->s_name);
1883 } else {
1884 /* redeclaration of %s */
1885 error(27, sym->s_name);
1886 /*
1887 * inside blocks it should not be too
1888 * complicated to find the position of the
1889 * previous declaration
1890 */
1891 if (block_level == 0)
1892 print_previous_declaration(-1, sym);
1893 }
1894 } else {
1895 if (hflag)
1896 /* redefinition hides earlier one: %s */
1897 warning(43, sym->s_name);
1898 }
1899 sym = pushdown(sym);
1900 }
1901 sym->s_scl = CTCONST;
1902 sym->s_type = dcs->d_tagtyp;
1903 sym->s_value.v_tspec = INT;
1904 sym->s_value.v_quad = val;
1905 if (impl && val - 1 == TARG_INT_MAX) {
1906 /* overflow in enumeration values: %s */
1907 warning(48, sym->s_name);
1908 }
1909 enumval = val + 1;
1910 return sym;
1911 }
1912
1913 /*
1914 * Process a single external declarator.
1915 */
1916 static void
1917 declare_extern(sym_t *dsym, bool initflg, sbuf_t *renaming)
1918 {
1919 bool dowarn, rval, redec;
1920 sym_t *rdsym;
1921 char *s;
1922
1923 if (renaming != NULL) {
1924 lint_assert(dsym->s_rename == NULL);
1925
1926 s = getlblk(1, renaming->sb_len + 1);
1927 (void)memcpy(s, renaming->sb_name, renaming->sb_len + 1);
1928 dsym->s_rename = s;
1929 }
1930
1931 check_function_definition(dsym, true);
1932
1933 check_type(dsym);
1934
1935 if (initflg && !check_init(dsym))
1936 dsym->s_def = DEF;
1937
1938 /*
1939 * Declarations of functions are marked as "tentative" in
1940 * declarator_name(). This is wrong because there are no
1941 * tentative function definitions.
1942 */
1943 if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
1944 dsym->s_def = DECL;
1945
1946 if (dcs->d_inline) {
1947 if (dsym->s_type->t_tspec == FUNC) {
1948 dsym->s_inline = true;
1949 } else {
1950 /* variable declared inline: %s */
1951 warning(268, dsym->s_name);
1952 }
1953 }
1954
1955 /* Write the declaration into the output file */
1956 if (plibflg && llibflg &&
1957 dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
1958 /*
1959 * With both LINTLIBRARY and PROTOLIB the prototype is
1960 * written as a function definition to the output file.
1961 */
1962 rval = dsym->s_type->t_subt->t_tspec != VOID;
1963 outfdef(dsym, &dsym->s_def_pos, rval, false, NULL);
1964 } else {
1965 outsym(dsym, dsym->s_scl, dsym->s_def);
1966 }
1967
1968 if ((rdsym = dcs->d_redeclared_symbol) != NULL) {
1969
1970 /*
1971 * If the old symbol stems from an old style function
1972 * definition, we have remembered the params in rdsmy->s_args
1973 * and compare them with the params of the prototype.
1974 */
1975 if (rdsym->s_osdef && dsym->s_type->t_proto) {
1976 redec = check_old_style_definition(rdsym, dsym);
1977 } else {
1978 redec = false;
1979 }
1980
1981 if (!redec &&
1982 !check_redeclaration(dsym, (dowarn = false, &dowarn))) {
1983
1984 if (dowarn) {
1985 if (sflag)
1986 /* redeclaration of %s */
1987 error(27, dsym->s_name);
1988 else
1989 /* redeclaration of %s */
1990 warning(27, dsym->s_name);
1991 print_previous_declaration(-1, rdsym);
1992 }
1993
1994 /*
1995 * Take over the remembered params if the new symbol
1996 * is not a prototype.
1997 */
1998 if (rdsym->s_osdef && !dsym->s_type->t_proto) {
1999 dsym->s_osdef = rdsym->s_osdef;
2000 dsym->s_args = rdsym->s_args;
2001 dsym->s_def_pos = rdsym->s_def_pos;
2002 }
2003
2004 /*
2005 * Remember the position of the declaration if the
2006 * old symbol was a prototype and the new is not.
2007 * Also remember the position if the old symbol
2008 * was defined and the new is not.
2009 */
2010 if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) {
2011 dsym->s_def_pos = rdsym->s_def_pos;
2012 } else if (rdsym->s_def == DEF && dsym->s_def != DEF) {
2013 dsym->s_def_pos = rdsym->s_def_pos;
2014 }
2015
2016 /*
2017 * Copy usage information of the name into the new
2018 * symbol.
2019 */
2020 copy_usage_info(dsym, rdsym);
2021
2022 /* Once a name is defined, it remains defined. */
2023 if (rdsym->s_def == DEF)
2024 dsym->s_def = DEF;
2025
2026 /* once a function is inline, it remains inline */
2027 if (rdsym->s_inline)
2028 dsym->s_inline = true;
2029
2030 complete_type(dsym, rdsym);
2031
2032 }
2033
2034 rmsym(rdsym);
2035 }
2036
2037 if (dsym->s_scl == TYPEDEF) {
2038 dsym->s_type = dup_type(dsym->s_type);
2039 dsym->s_type->t_typedef = true;
2040 settdsym(dsym->s_type, dsym);
2041 }
2042
2043 }
2044
2045 void
2046 declare(sym_t *decl, bool initflg, sbuf_t *renaming)
2047 {
2048
2049 if (dcs->d_ctx == EXTERN) {
2050 declare_extern(decl, initflg, renaming);
2051 } else if (dcs->d_ctx == ARG || dcs->d_ctx == PROTO_ARG) {
2052 if (renaming != NULL) {
2053 /* symbol renaming can't be used on function arguments */
2054 error(310);
2055 } else
2056 (void)declare_argument(decl, initflg);
2057 } else {
2058 lint_assert(dcs->d_ctx == AUTO);
2059 if (renaming != NULL) {
2060 /* symbol renaming can't be used on automatic variables */
2061 error(311);
2062 } else
2063 declare_local(decl, initflg);
2064 }
2065 }
2066
2067 /*
2068 * Copies information about usage into a new symbol table entry of
2069 * the same symbol.
2070 */
2071 void
2072 copy_usage_info(sym_t *sym, sym_t *rdsym)
2073 {
2074
2075 sym->s_set_pos = rdsym->s_set_pos;
2076 sym->s_use_pos = rdsym->s_use_pos;
2077 sym->s_set = rdsym->s_set;
2078 sym->s_used = rdsym->s_used;
2079 }
2080
2081 /*
2082 * Prints an error and returns true if a symbol is redeclared/redefined.
2083 * Otherwise returns false and, in some cases of minor problems, prints
2084 * a warning.
2085 */
2086 bool
2087 check_redeclaration(sym_t *dsym, bool *dowarn)
2088 {
2089 sym_t *rsym;
2090
2091 if ((rsym = dcs->d_redeclared_symbol)->s_scl == CTCONST) {
2092 /* redeclaration of %s */
2093 error(27, dsym->s_name);
2094 print_previous_declaration(-1, rsym);
2095 return true;
2096 }
2097 if (rsym->s_scl == TYPEDEF) {
2098 /* typedef redeclared: %s */
2099 error(89, dsym->s_name);
2100 print_previous_declaration(-1, rsym);
2101 return true;
2102 }
2103 if (dsym->s_scl == TYPEDEF) {
2104 /* redeclaration of %s */
2105 error(27, dsym->s_name);
2106 print_previous_declaration(-1, rsym);
2107 return true;
2108 }
2109 if (rsym->s_def == DEF && dsym->s_def == DEF) {
2110 /* redefinition of %s */
2111 error(28, dsym->s_name);
2112 print_previous_declaration(-1, rsym);
2113 return true;
2114 }
2115 if (!eqtype(rsym->s_type, dsym->s_type, false, false, dowarn)) {
2116 /* redeclaration of %s */
2117 error(27, dsym->s_name);
2118 print_previous_declaration(-1, rsym);
2119 return true;
2120 }
2121 if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
2122 return false;
2123 if (rsym->s_scl == STATIC && dsym->s_scl == STATIC)
2124 return false;
2125 if (rsym->s_scl == STATIC && dsym->s_def == DECL)
2126 return false;
2127 if (rsym->s_scl == EXTERN && rsym->s_def == DEF) {
2128 /*
2129 * All cases except "int a = 1; static int a;" are caught
2130 * above with or without a warning
2131 */
2132 /* redeclaration of %s */
2133 error(27, dsym->s_name);
2134 print_previous_declaration(-1, rsym);
2135 return true;
2136 }
2137 if (rsym->s_scl == EXTERN) {
2138 /* previously declared extern, becomes static: %s */
2139 warning(29, dsym->s_name);
2140 print_previous_declaration(-1, rsym);
2141 return false;
2142 }
2143 /*
2144 * Now it's one of:
2145 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
2146 */
2147 /* redeclaration of %s; ANSI C requires "static" */
2148 if (sflag) {
2149 /* redeclaration of %s; ANSI C requires static */
2150 warning(30, dsym->s_name);
2151 print_previous_declaration(-1, rsym);
2152 }
2153 dsym->s_scl = STATIC;
2154 return false;
2155 }
2156
2157 static bool
2158 qualifiers_correspond(const type_t *tp1, const type_t *tp2, bool ignqual)
2159 {
2160 if (tp1->t_const != tp2->t_const && !ignqual && !tflag)
2161 return false;
2162
2163 if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag)
2164 return false;
2165
2166 return true;
2167 }
2168
2169 bool
2170 eqptrtype(const type_t *tp1, const type_t *tp2, bool ignqual)
2171 {
2172 if (tp1->t_tspec != VOID && tp2->t_tspec != VOID)
2173 return false;
2174
2175 if (!qualifiers_correspond(tp1, tp2, ignqual))
2176 return false;
2177
2178 return true;
2179 }
2180
2181
2182 /*
2183 * Checks if two types are compatible.
2184 *
2185 * ignqual ignore qualifiers of type; used for function params
2186 * promot promote left type; used for comparison of params of
2187 * old style function definitions with params of prototypes.
2188 * *dowarn set to true if an old style function declaration is not
2189 * compatible with a prototype
2190 */
2191 bool
2192 eqtype(const type_t *tp1, const type_t *tp2,
2193 bool ignqual, bool promot, bool *dowarn)
2194 {
2195 tspec_t t;
2196
2197 while (tp1 != NULL && tp2 != NULL) {
2198
2199 t = tp1->t_tspec;
2200 if (promot) {
2201 if (t == FLOAT) {
2202 t = DOUBLE;
2203 } else if (t == CHAR || t == SCHAR) {
2204 t = INT;
2205 } else if (t == UCHAR) {
2206 t = tflag ? UINT : INT;
2207 } else if (t == SHORT) {
2208 t = INT;
2209 } else if (t == USHORT) {
2210 /* CONSTCOND */
2211 t = TARG_INT_MAX < TARG_USHRT_MAX || tflag ? UINT : INT;
2212 }
2213 }
2214
2215 if (t != tp2->t_tspec)
2216 return false;
2217
2218 if (!qualifiers_correspond(tp1, tp2, ignqual))
2219 return false;
2220
2221 if (t == STRUCT || t == UNION)
2222 return tp1->t_str == tp2->t_str;
2223
2224 if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
2225 if (tp1->t_dim != 0 && tp2->t_dim != 0)
2226 return false;
2227 }
2228
2229 /* dont check prototypes for traditional */
2230 if (t == FUNC && !tflag) {
2231 if (tp1->t_proto && tp2->t_proto) {
2232 if (!eqargs(tp1, tp2, dowarn))
2233 return false;
2234 } else if (tp1->t_proto) {
2235 if (!mnoarg(tp1, dowarn))
2236 return false;
2237 } else if (tp2->t_proto) {
2238 if (!mnoarg(tp2, dowarn))
2239 return false;
2240 }
2241 }
2242
2243 tp1 = tp1->t_subt;
2244 tp2 = tp2->t_subt;
2245 ignqual = promot = false;
2246
2247 }
2248
2249 return tp1 == tp2;
2250 }
2251
2252 /*
2253 * Compares the parameter types of two prototypes.
2254 */
2255 static bool
2256 eqargs(const type_t *tp1, const type_t *tp2, bool *dowarn)
2257 {
2258 sym_t *a1, *a2;
2259
2260 if (tp1->t_vararg != tp2->t_vararg)
2261 return false;
2262
2263 a1 = tp1->t_args;
2264 a2 = tp2->t_args;
2265
2266 while (a1 != NULL && a2 != NULL) {
2267
2268 if (!eqtype(a1->s_type, a2->s_type, true, false, dowarn))
2269 return false;
2270
2271 a1 = a1->s_next;
2272 a2 = a2->s_next;
2273
2274 }
2275
2276 return a1 == a2;
2277 }
2278
2279 /*
2280 * mnoarg() (matches functions with no argument type information)
2281 * returns whether all parameters of a prototype are compatible with
2282 * an old style function declaration.
2283 * This is the case if the following conditions are met:
2284 * 1. the prototype has a fixed number of parameters
2285 * 2. no parameter is of type float
2286 * 3. no parameter is converted to another type if integer promotion
2287 * is applied on it
2288 */
2289 static bool
2290 mnoarg(const type_t *tp, bool *dowarn)
2291 {
2292 sym_t *arg;
2293 tspec_t t;
2294
2295 if (tp->t_vararg) {
2296 if (dowarn != NULL)
2297 *dowarn = true;
2298 }
2299 for (arg = tp->t_args; arg != NULL; arg = arg->s_next) {
2300 if ((t = arg->s_type->t_tspec) == FLOAT ||
2301 t == CHAR || t == SCHAR || t == UCHAR ||
2302 t == SHORT || t == USHORT) {
2303 if (dowarn != NULL)
2304 *dowarn = true;
2305 }
2306 }
2307 return true;
2308 }
2309
2310 /*
2311 * Compares a prototype declaration with the remembered arguments of
2312 * a previous old style function definition.
2313 */
2314 static bool
2315 check_old_style_definition(sym_t *rdsym, sym_t *dsym)
2316 {
2317 sym_t *args, *pargs, *arg, *parg;
2318 int narg, nparg, n;
2319 bool dowarn, msg;
2320
2321 args = rdsym->s_args;
2322 pargs = dsym->s_type->t_args;
2323
2324 msg = false;
2325
2326 narg = nparg = 0;
2327 for (arg = args; arg != NULL; arg = arg->s_next)
2328 narg++;
2329 for (parg = pargs; parg != NULL; parg = parg->s_next)
2330 nparg++;
2331 if (narg != nparg) {
2332 /* prototype does not match old-style definition */
2333 error(63);
2334 msg = true;
2335 goto end;
2336 }
2337
2338 arg = args;
2339 parg = pargs;
2340 n = 1;
2341 while (narg-- > 0) {
2342 dowarn = false;
2343 /*
2344 * If it does not match due to promotion and sflag is
2345 * not set we print only a warning.
2346 */
2347 if (!eqtype(arg->s_type, parg->s_type, true, true, &dowarn) ||
2348 dowarn) {
2349 /* prototype does not match old style defn., arg #%d */
2350 error(299, n);
2351 msg = true;
2352 }
2353 arg = arg->s_next;
2354 parg = parg->s_next;
2355 n++;
2356 }
2357
2358 end:
2359 if (msg)
2360 /* old style definition */
2361 print_previous_declaration(300, rdsym);
2362
2363 return msg;
2364 }
2365
2366 /*
2367 * Completes a type by copying the dimension and prototype information
2368 * from a second compatible type.
2369 *
2370 * Following lines are legal:
2371 * "typedef a[]; a b; a b[10]; a c; a c[20];"
2372 * "typedef ft(); ft f; f(int); ft g; g(long);"
2373 * This means that, if a type is completed, the type structure must
2374 * be duplicated.
2375 */
2376 void
2377 complete_type(sym_t *dsym, sym_t *ssym)
2378 {
2379 type_t **dstp, *src;
2380 type_t *dst;
2381
2382 dstp = &dsym->s_type;
2383 src = ssym->s_type;
2384
2385 while ((dst = *dstp) != NULL) {
2386 lint_assert(src != NULL);
2387 lint_assert(dst->t_tspec == src->t_tspec);
2388 if (dst->t_tspec == ARRAY) {
2389 if (dst->t_dim == 0 && src->t_dim != 0) {
2390 *dstp = dst = dup_type(dst);
2391 dst->t_dim = src->t_dim;
2392 setcomplete(dst, true);
2393 }
2394 } else if (dst->t_tspec == FUNC) {
2395 if (!dst->t_proto && src->t_proto) {
2396 *dstp = dst = dup_type(dst);
2397 dst->t_proto = true;
2398 dst->t_args = src->t_args;
2399 }
2400 }
2401 dstp = &dst->t_subt;
2402 src = src->t_subt;
2403 }
2404 }
2405
2406 /*
2407 * Completes the declaration of a single argument.
2408 */
2409 sym_t *
2410 declare_argument(sym_t *sym, bool initflg)
2411 {
2412 tspec_t t;
2413
2414 check_function_definition(sym, true);
2415
2416 check_type(sym);
2417
2418 if (dcs->d_redeclared_symbol != NULL &&
2419 dcs->d_redeclared_symbol->s_block_level == block_level) {
2420 /* redeclaration of formal parameter %s */
2421 error(237, sym->s_name);
2422 rmsym(dcs->d_redeclared_symbol);
2423 sym->s_arg = true;
2424 }
2425
2426 if (!sym->s_arg) {
2427 /* declared argument %s is missing */
2428 error(53, sym->s_name);
2429 sym->s_arg = true;
2430 }
2431
2432 if (initflg) {
2433 /* cannot initialize parameter: %s */
2434 error(52, sym->s_name);
2435 }
2436
2437 if (sym->s_type == NULL) /* for c(void()) */
2438 sym->s_type = gettyp(VOID);
2439
2440 if ((t = sym->s_type->t_tspec) == ARRAY) {
2441 sym->s_type = derive_type(sym->s_type->t_subt, PTR);
2442 } else if (t == FUNC) {
2443 if (tflag)
2444 /* a function is declared as an argument: %s */
2445 warning(50, sym->s_name);
2446 sym->s_type = derive_type(sym->s_type, PTR);
2447 } else if (t == FLOAT) {
2448 if (tflag)
2449 sym->s_type = gettyp(DOUBLE);
2450 }
2451
2452 if (dcs->d_inline)
2453 /* argument declared inline: %s */
2454 warning(269, sym->s_name);
2455
2456 /*
2457 * Arguments must have complete types. length() prints the needed
2458 * error messages (null dimension is impossible because arrays are
2459 * converted to pointers).
2460 */
2461 if (sym->s_type->t_tspec != VOID)
2462 (void)length(sym->s_type, sym->s_name);
2463
2464 sym->s_used = dcs->d_used;
2465 mark_as_set(sym);
2466
2467 return sym;
2468 }
2469
2470 void
2471 check_func_lint_directives(void)
2472 {
2473 sym_t *arg;
2474 int narg, n;
2475 tspec_t t;
2476
2477 /* check for illegal combinations of lint directives */
2478 if (printflike_argnum != -1 && scanflike_argnum != -1) {
2479 /* can't be used together: ** PRINTFLIKE ** ** SCANFLIKE ** */
2480 warning(289);
2481 printflike_argnum = scanflike_argnum = -1;
2482 }
2483 if (nvararg != -1 &&
2484 (printflike_argnum != -1 || scanflike_argnum != -1)) {
2485 /* dubious use of ** VARARGS ** with ** %s ** */
2486 warning(288,
2487 printflike_argnum != -1 ? "PRINTFLIKE" : "SCANFLIKE");
2488 nvararg = -1;
2489 }
2490
2491 /*
2492 * check if the argument of a lint directive is compatible with the
2493 * number of arguments.
2494 */
2495 narg = 0;
2496 for (arg = dcs->d_func_args; arg != NULL; arg = arg->s_next)
2497 narg++;
2498 if (nargusg > narg) {
2499 /* argument number mismatch with directive: ** %s ** */
2500 warning(283, "ARGSUSED");
2501 nargusg = 0;
2502 }
2503 if (nvararg > narg) {
2504 /* argument number mismatch with directive: ** %s ** */
2505 warning(283, "VARARGS");
2506 nvararg = 0;
2507 }
2508 if (printflike_argnum > narg) {
2509 /* argument number mismatch with directive: ** %s ** */
2510 warning(283, "PRINTFLIKE");
2511 printflike_argnum = -1;
2512 } else if (printflike_argnum == 0) {
2513 printflike_argnum = -1;
2514 }
2515 if (scanflike_argnum > narg) {
2516 /* argument number mismatch with directive: ** %s ** */
2517 warning(283, "SCANFLIKE");
2518 scanflike_argnum = -1;
2519 } else if (scanflike_argnum == 0) {
2520 scanflike_argnum = -1;
2521 }
2522 if (printflike_argnum != -1 || scanflike_argnum != -1) {
2523 narg = printflike_argnum != -1
2524 ? printflike_argnum : scanflike_argnum;
2525 arg = dcs->d_func_args;
2526 for (n = 1; n < narg; n++)
2527 arg = arg->s_next;
2528 if (arg->s_type->t_tspec != PTR ||
2529 ((t = arg->s_type->t_subt->t_tspec) != CHAR &&
2530 t != UCHAR && t != SCHAR)) {
2531 /* arg. %d must be 'char *' for PRINTFLIKE/SCANFLIKE */
2532 warning(293, narg);
2533 printflike_argnum = scanflike_argnum = -1;
2534 }
2535 }
2536 }
2537
2538 /*
2539 * Warn about arguments in old style function definitions that default to int.
2540 * Check that an old style function definition is compatible to a previous
2541 * prototype.
2542 */
2543 void
2544 check_func_old_style_arguments(void)
2545 {
2546 sym_t *args, *arg, *pargs, *parg;
2547 int narg, nparg;
2548 bool msg;
2549
2550 args = funcsym->s_args;
2551 pargs = funcsym->s_type->t_args;
2552
2553 /*
2554 * print a warning for each argument of an old style function
2555 * definition which defaults to int
2556 */
2557 for (arg = args; arg != NULL; arg = arg->s_next) {
2558 if (arg->s_defarg) {
2559 /* argument type defaults to 'int': %s */
2560 warning(32, arg->s_name);
2561 arg->s_defarg = false;
2562 mark_as_set(arg);
2563 }
2564 }
2565
2566 /*
2567 * If this is an old style function definition and a prototype
2568 * exists, compare the types of arguments.
2569 */
2570 if (funcsym->s_osdef && funcsym->s_type->t_proto) {
2571 /*
2572 * If the number of arguments does not match, we need not
2573 * continue.
2574 */
2575 narg = nparg = 0;
2576 msg = false;
2577 for (parg = pargs; parg != NULL; parg = parg->s_next)
2578 nparg++;
2579 for (arg = args; arg != NULL; arg = arg->s_next)
2580 narg++;
2581 if (narg != nparg) {
2582 /* parameter mismatch: %d declared, %d defined */
2583 error(51, nparg, narg);
2584 msg = true;
2585 } else {
2586 parg = pargs;
2587 arg = args;
2588 while (narg-- > 0) {
2589 msg |= check_prototype_declaration(arg, parg);
2590 parg = parg->s_next;
2591 arg = arg->s_next;
2592 }
2593 }
2594 if (msg)
2595 /* prototype declaration */
2596 print_previous_declaration(285,
2597 dcs->d_redeclared_symbol);
2598
2599 /* from now on the prototype is valid */
2600 funcsym->s_osdef = false;
2601 funcsym->s_args = NULL;
2602 }
2603 }
2604
2605 /*
2606 * Checks compatibility of an old style function definition with a previous
2607 * prototype declaration.
2608 * Returns true if the position of the previous declaration should be reported.
2609 */
2610 static bool
2611 check_prototype_declaration(sym_t *arg, sym_t *parg)
2612 {
2613 type_t *tp, *ptp;
2614 bool dowarn, msg;
2615
2616 tp = arg->s_type;
2617 ptp = parg->s_type;
2618
2619 msg = false;
2620 dowarn = false;
2621
2622 if (!eqtype(tp, ptp, true, true, &dowarn)) {
2623 if (eqtype(tp, ptp, true, false, &dowarn)) {
2624 /* type does not match prototype: %s */
2625 gnuism(58, arg->s_name);
2626 msg = sflag || !gflag;
2627 } else {
2628 /* type does not match prototype: %s */
2629 error(58, arg->s_name);
2630 msg = true;
2631 }
2632 } else if (dowarn) {
2633 if (sflag)
2634 /* type does not match prototype: %s */
2635 error(58, arg->s_name);
2636 else
2637 /* type does not match prototype: %s */
2638 warning(58, arg->s_name);
2639 msg = true;
2640 }
2641
2642 return msg;
2643 }
2644
2645 /*
2646 * Completes a single local declaration/definition.
2647 */
2648 void
2649 declare_local(sym_t *dsym, bool initflg)
2650 {
2651
2652 /* Correct a mistake done in declarator_name(). */
2653 if (dsym->s_type->t_tspec == FUNC) {
2654 dsym->s_def = DECL;
2655 if (dcs->d_scl == NOSCL)
2656 dsym->s_scl = EXTERN;
2657 }
2658
2659 if (dsym->s_type->t_tspec == FUNC) {
2660 if (dsym->s_scl == STATIC) {
2661 /* dubious static function at block level: %s */
2662 warning(93, dsym->s_name);
2663 dsym->s_scl = EXTERN;
2664 } else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) {
2665 /* function has illegal storage class: %s */
2666 error(94, dsym->s_name);
2667 dsym->s_scl = EXTERN;
2668 }
2669 }
2670
2671 /*
2672 * functions may be declared inline at local scope, although
2673 * this has no effect for a later definition of the same
2674 * function.
2675 * XXX it should have an effect if tflag is set. this would
2676 * also be the way gcc behaves.
2677 */
2678 if (dcs->d_inline) {
2679 if (dsym->s_type->t_tspec == FUNC) {
2680 dsym->s_inline = true;
2681 } else {
2682 /* variable declared inline: %s */
2683 warning(268, dsym->s_name);
2684 }
2685 }
2686
2687 check_function_definition(dsym, true);
2688
2689 check_type(dsym);
2690
2691 if (dcs->d_redeclared_symbol != NULL && dsym->s_scl == EXTERN)
2692 declare_external_in_block(dsym);
2693
2694 if (dsym->s_scl == EXTERN) {
2695 /*
2696 * XXX if the static variable at level 0 is only defined
2697 * later, checking will be possible.
2698 */
2699 if (dsym->s_ext_sym == NULL) {
2700 outsym(dsym, EXTERN, dsym->s_def);
2701 } else {
2702 outsym(dsym, dsym->s_ext_sym->s_scl, dsym->s_def);
2703 }
2704 }
2705
2706 if (dcs->d_redeclared_symbol != NULL) {
2707
2708 if (dcs->d_redeclared_symbol->s_block_level == 0) {
2709
2710 switch (dsym->s_scl) {
2711 case AUTO:
2712 if (hflag)
2713 /* automatic hides external decl.: %s */
2714 warning(86, dsym->s_name);
2715 break;
2716 case STATIC:
2717 if (hflag)
2718 /* static hides external decl.: %s */
2719 warning(87, dsym->s_name);
2720 break;
2721 case TYPEDEF:
2722 if (hflag)
2723 /* typedef hides external decl.: %s */
2724 warning(88, dsym->s_name);
2725 break;
2726 case EXTERN:
2727 /*
2728 * Warnings and errors are printed in
2729 * declare_external_in_block()
2730 */
2731 break;
2732 default:
2733 lint_assert(/*CONSTCOND*/false);
2734 }
2735
2736 } else if (dcs->d_redeclared_symbol->s_block_level ==
2737 block_level) {
2738
2739 /* no hflag, because it's illegal! */
2740 if (dcs->d_redeclared_symbol->s_arg) {
2741 /*
2742 * if !tflag, a "redeclaration of %s" error
2743 * is produced below
2744 */
2745 if (tflag) {
2746 if (hflag)
2747 /* decl. hides parameter: %s */
2748 warning(91, dsym->s_name);
2749 rmsym(dcs->d_redeclared_symbol);
2750 }
2751 }
2752
2753 } else if (dcs->d_redeclared_symbol->s_block_level <
2754 block_level) {
2755
2756 if (hflag)
2757 /* declaration hides earlier one: %s */
2758 warning(95, dsym->s_name);
2759
2760 }
2761
2762 if (dcs->d_redeclared_symbol->s_block_level == block_level) {
2763
2764 /* redeclaration of %s */
2765 error(27, dsym->s_name);
2766 rmsym(dcs->d_redeclared_symbol);
2767
2768 }
2769
2770 }
2771
2772 if (initflg && !check_init(dsym)) {
2773 dsym->s_def = DEF;
2774 mark_as_set(dsym);
2775 }
2776
2777 if (dsym->s_scl == TYPEDEF) {
2778 dsym->s_type = dup_type(dsym->s_type);
2779 dsym->s_type->t_typedef = true;
2780 settdsym(dsym->s_type, dsym);
2781 }
2782
2783 /*
2784 * Before we can check the size we must wait for a initialization
2785 * which may follow.
2786 */
2787 }
2788
2789 /*
2790 * Processes (re)declarations of external symbols inside blocks.
2791 */
2792 static void
2793 declare_external_in_block(sym_t *dsym)
2794 {
2795 bool eqt, dowarn;
2796 sym_t *esym;
2797
2798 /* look for a symbol with the same name */
2799 esym = dcs->d_redeclared_symbol;
2800 while (esym != NULL && esym->s_block_level != 0) {
2801 while ((esym = esym->s_link) != NULL) {
2802 if (esym->s_kind != FVFT)
2803 continue;
2804 if (strcmp(dsym->s_name, esym->s_name) == 0)
2805 break;
2806 }
2807 }
2808 if (esym == NULL)
2809 return;
2810 if (esym->s_scl != EXTERN && esym->s_scl != STATIC) {
2811 /* gcc accepts this without a warning, pcc prints an error. */
2812 /* redeclaration of %s */
2813 warning(27, dsym->s_name);
2814 print_previous_declaration(-1, esym);
2815 return;
2816 }
2817
2818 dowarn = false;
2819 eqt = eqtype(esym->s_type, dsym->s_type, false, false, &dowarn);
2820
2821 if (!eqt || dowarn) {
2822 if (esym->s_scl == EXTERN) {
2823 /* inconsistent redeclaration of extern: %s */
2824 warning(90, dsym->s_name);
2825 print_previous_declaration(-1, esym);
2826 } else {
2827 /* inconsistent redeclaration of static: %s */
2828 warning(92, dsym->s_name);
2829 print_previous_declaration(-1, esym);
2830 }
2831 }
2832
2833 if (eqt) {
2834 /*
2835 * Remember the external symbol so we can update usage
2836 * information at the end of the block.
2837 */
2838 dsym->s_ext_sym = esym;
2839 }
2840 }
2841
2842 /*
2843 * Print an error or a warning if the symbol cannot be initialized due
2844 * to type/storage class. Return whether an error has been detected.
2845 */
2846 static bool
2847 check_init(sym_t *sym)
2848 {
2849 bool erred;
2850
2851 erred = false;
2852
2853 if (sym->s_type->t_tspec == FUNC) {
2854 /* cannot initialize function: %s */
2855 error(24, sym->s_name);
2856 erred = true;
2857 } else if (sym->s_scl == TYPEDEF) {
2858 /* cannot initialize typedef: %s */
2859 error(25, sym->s_name);
2860 erred = true;
2861 } else if (sym->s_scl == EXTERN && sym->s_def == DECL) {
2862 /* cannot initialize "extern" declaration: %s */
2863 if (dcs->d_ctx == EXTERN) {
2864 /* cannot initialize extern declaration: %s */
2865 warning(26, sym->s_name);
2866 } else {
2867 /* cannot initialize extern declaration: %s */
2868 error(26, sym->s_name);
2869 erred = true;
2870 }
2871 }
2872
2873 return erred;
2874 }
2875
2876 /*
2877 * Create a symbol for an abstract declaration.
2878 */
2879 sym_t *
2880 abstract_name(void)
2881 {
2882 sym_t *sym;
2883
2884 lint_assert(dcs->d_ctx == ABSTRACT || dcs->d_ctx == PROTO_ARG);
2885
2886 sym = getblk(sizeof(*sym));
2887
2888 sym->s_name = unnamed;
2889 sym->s_def = DEF;
2890 sym->s_scl = ABSTRACT;
2891 sym->s_block_level = -1;
2892
2893 if (dcs->d_ctx == PROTO_ARG)
2894 sym->s_arg = true;
2895
2896 sym->s_type = dcs->d_type;
2897 dcs->d_redeclared_symbol = NULL;
2898 dcs->d_vararg = false;
2899
2900 return sym;
2901 }
2902
2903 /*
2904 * Removes anything which has nothing to do on global level.
2905 */
2906 void
2907 global_clean_up(void)
2908 {
2909
2910 while (dcs->d_next != NULL)
2911 end_declaration_level();
2912
2913 cleanup();
2914 block_level = 0;
2915 mem_block_level = 0;
2916
2917 /*
2918 * remove all information about pending lint directives without
2919 * warnings.
2920 */
2921 global_clean_up_decl(true);
2922 }
2923
2924 /*
2925 * Process an abstract type declaration
2926 */
2927 sym_t *
2928 declare_1_abstract(sym_t *sym)
2929 {
2930
2931 check_function_definition(sym, true);
2932 check_type(sym);
2933 return sym;
2934 }
2935
2936 /*
2937 * Checks size after declarations of variables and their initialization.
2938 */
2939 void
2940 check_size(sym_t *dsym)
2941 {
2942
2943 if (dsym->s_def != DEF)
2944 return;
2945 if (dsym->s_scl == TYPEDEF)
2946 return;
2947 if (dsym->s_type->t_tspec == FUNC)
2948 return;
2949
2950 if (length(dsym->s_type, dsym->s_name) == 0 &&
2951 dsym->s_type->t_tspec == ARRAY && dsym->s_type->t_dim == 0) {
2952 if (tflag) {
2953 /* empty array declaration: %s */
2954 warning(190, dsym->s_name);
2955 } else {
2956 /* empty array declaration: %s */
2957 error(190, dsym->s_name);
2958 }
2959 }
2960 }
2961
2962 /*
2963 * Mark an object as set if it is not already
2964 */
2965 void
2966 mark_as_set(sym_t *sym)
2967 {
2968
2969 if (!sym->s_set) {
2970 sym->s_set = true;
2971 UNIQUE_CURR_POS(sym->s_set_pos);
2972 }
2973 }
2974
2975 /*
2976 * Mark an object as used if it is not already
2977 */
2978 void
2979 mark_as_used(sym_t *sym, bool fcall, bool szof)
2980 {
2981
2982 if (!sym->s_used) {
2983 sym->s_used = true;
2984 UNIQUE_CURR_POS(sym->s_use_pos);
2985 }
2986 /*
2987 * for function calls another record is written
2988 *
2989 * XXX Should symbols used in sizeof() be treated as used or not?
2990 * Probably not, because there is no sense to declare an
2991 * external variable only to get their size.
2992 */
2993 if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN)
2994 outusg(sym);
2995 }
2996
2997 /*
2998 * Prints warnings for a list of variables and labels (concatenated
2999 * with s_dlnxt) if these are not used or only set.
3000 */
3001 void
3002 check_usage(dinfo_t *di)
3003 {
3004 sym_t *sym;
3005 int mklwarn;
3006
3007 /* for this warning LINTED has no effect */
3008 mklwarn = lwarn;
3009 lwarn = LWARN_ALL;
3010
3011 #ifdef DEBUG
3012 printf("%s, %d: >temp lwarn = %d\n", curr_pos.p_file, curr_pos.p_line,
3013 lwarn);
3014 #endif
3015 for (sym = di->d_dlsyms; sym != NULL; sym = sym->s_dlnxt)
3016 check_usage_sym(di->d_asm, sym);
3017 lwarn = mklwarn;
3018 #ifdef DEBUG
3019 printf("%s, %d: <temp lwarn = %d\n", curr_pos.p_file, curr_pos.p_line,
3020 lwarn);
3021 #endif
3022 }
3023
3024 /*
3025 * Prints a warning for a single variable or label if it is not used or
3026 * only set.
3027 */
3028 void
3029 check_usage_sym(bool novar, sym_t *sym)
3030 {
3031
3032 if (sym->s_block_level == -1)
3033 return;
3034
3035 if (sym->s_kind == FVFT && sym->s_arg)
3036 check_argument_usage(novar, sym);
3037 else if (sym->s_kind == FVFT)
3038 check_variable_usage(novar, sym);
3039 else if (sym->s_kind == FLABEL)
3040 check_label_usage(sym);
3041 else if (sym->s_kind == FTAG)
3042 check_tag_usage(sym);
3043 }
3044
3045 static void
3046 check_argument_usage(bool novar, sym_t *arg)
3047 {
3048
3049 lint_assert(arg->s_set);
3050
3051 if (novar)
3052 return;
3053
3054 if (!arg->s_used && vflag) {
3055 /* argument '%s' unused in function '%s' */
3056 warning_at(231, &arg->s_def_pos, arg->s_name, funcsym->s_name);
3057 }
3058 }
3059
3060 static void
3061 check_variable_usage(bool novar, sym_t *sym)
3062 {
3063 scl_t sc;
3064 sym_t *xsym;
3065
3066 lint_assert(block_level != 0);
3067
3068 /* example at file scope: int c = ({ return 3; }); */
3069 if (sym->s_block_level == 0 && ch_isdigit(sym->s_name[0]))
3070 return;
3071
3072 /* errors in expressions easily cause lots of these warnings */
3073 if (nerr != 0)
3074 return;
3075
3076 /*
3077 * XXX Only variables are checked, although types should
3078 * probably also be checked
3079 */
3080 if ((sc = sym->s_scl) != EXTERN && sc != STATIC &&
3081 sc != AUTO && sc != REG) {
3082 return;
3083 }
3084
3085 if (novar)
3086 return;
3087
3088 if (sc == EXTERN) {
3089 if (!sym->s_used && !sym->s_set) {
3090 /* '%s' unused in function '%s' */
3091 warning_at(192, &sym->s_def_pos,
3092 sym->s_name, funcsym->s_name);
3093 }
3094 } else {
3095 if (sym->s_set && !sym->s_used) {
3096 /* '%s' set but not used in function '%s' */
3097 warning_at(191, &sym->s_set_pos,
3098 sym->s_name, funcsym->s_name);
3099 } else if (!sym->s_used) {
3100 /* '%s' unused in function '%s' */
3101 warning_at(192, &sym->s_def_pos,
3102 sym->s_name, funcsym->s_name);
3103 }
3104 }
3105
3106 if (sc == EXTERN) {
3107 /*
3108 * information about usage is taken over into the symbol
3109 * table entry at level 0 if the symbol was locally declared
3110 * as an external symbol.
3111 *
3112 * XXX This is wrong for symbols declared static at level 0
3113 * if the usage information stems from sizeof(). This is
3114 * because symbols at level 0 only used in sizeof() are
3115 * considered to not be used.
3116 */
3117 if ((xsym = sym->s_ext_sym) != NULL) {
3118 if (sym->s_used && !xsym->s_used) {
3119 xsym->s_used = true;
3120 xsym->s_use_pos = sym->s_use_pos;
3121 }
3122 if (sym->s_set && !xsym->s_set) {
3123 xsym->s_set = true;
3124 xsym->s_set_pos = sym->s_set_pos;
3125 }
3126 }
3127 }
3128 }
3129
3130 static void
3131 check_label_usage(sym_t *lab)
3132 {
3133
3134 lint_assert(block_level == 1);
3135 lint_assert(lab->s_block_level == 1);
3136
3137 if (lab->s_set && !lab->s_used) {
3138 /* label '%s' unused in function '%s' */
3139 warning_at(232, &lab->s_set_pos, lab->s_name, funcsym->s_name);
3140 } else if (!lab->s_set) {
3141 /* undefined label '%s' */
3142 warning_at(23, &lab->s_use_pos, lab->s_name);
3143 }
3144 }
3145
3146 static void
3147 check_tag_usage(sym_t *sym)
3148 {
3149
3150 if (!is_incomplete(sym->s_type))
3151 return;
3152
3153 /* always complain about incomplete tags declared inside blocks */
3154 if (!zflag || dcs->d_ctx != EXTERN)
3155 return;
3156
3157 switch (sym->s_type->t_tspec) {
3158 case STRUCT:
3159 /* struct %s never defined */
3160 warning_at(233, &sym->s_def_pos, sym->s_name);
3161 break;
3162 case UNION:
3163 /* union %s never defined */
3164 warning_at(234, &sym->s_def_pos, sym->s_name);
3165 break;
3166 case ENUM:
3167 /* enum %s never defined */
3168 warning_at(235, &sym->s_def_pos, sym->s_name);
3169 break;
3170 default:
3171 lint_assert(/*CONSTCOND*/false);
3172 }
3173 }
3174
3175 /*
3176 * Called after the entire translation unit has been parsed.
3177 * Changes tentative definitions into definitions.
3178 * Performs some tests on global symbols. Detected problems are:
3179 * - defined variables of incomplete type
3180 * - constant variables which are not initialized
3181 * - static symbols which are never used
3182 */
3183 void
3184 check_global_symbols(void)
3185 {
3186 sym_t *sym;
3187
3188 if (block_level != 0 || dcs->d_next != NULL)
3189 norecover();
3190
3191 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
3192 if (sym->s_block_level == -1)
3193 continue;
3194 if (sym->s_kind == FVFT) {
3195 check_global_variable(sym);
3196 } else if (sym->s_kind == FTAG) {
3197 check_tag_usage(sym);
3198 } else {
3199 lint_assert(sym->s_kind == FMEMBER);
3200 }
3201 }
3202 }
3203
3204 static void
3205 check_unused_static_global_variable(const sym_t *sym)
3206 {
3207 if (sym->s_type->t_tspec == FUNC) {
3208 if (sym->s_def == DEF) {
3209 if (!sym->s_inline)
3210 /* static function %s unused */
3211 warning_at(236, &sym->s_def_pos, sym->s_name);
3212 } else {
3213 /* static function %s declared but not defined */
3214 warning_at(290, &sym->s_def_pos, sym->s_name);
3215 }
3216 } else if (!sym->s_set) {
3217 /* static variable %s unused */
3218 warning_at(226, &sym->s_def_pos, sym->s_name);
3219 } else {
3220 /* static variable %s set but not used */
3221 warning_at(307, &sym->s_def_pos, sym->s_name);
3222 }
3223 }
3224
3225 static void
3226 check_static_global_variable(const sym_t *sym)
3227 {
3228 if (sym->s_type->t_tspec == FUNC && sym->s_used && sym->s_def != DEF) {
3229 /* static function called but not defined: %s() */
3230 error_at(225, &sym->s_use_pos, sym->s_name);
3231 }
3232
3233 if (!sym->s_used)
3234 check_unused_static_global_variable(sym);
3235
3236 if (!tflag && sym->s_def == TDEF && sym->s_type->t_const) {
3237 /* const object %s should have initializer */
3238 warning_at(227, &sym->s_def_pos, sym->s_name);
3239 }
3240 }
3241
3242 static void
3243 check_global_variable(const sym_t *sym)
3244 {
3245
3246 if (sym->s_scl == TYPEDEF || sym->s_scl == CTCONST)
3247 return;
3248
3249 if (sym->s_scl == NOSCL)
3250 return; /* May be caused by a syntax error. */
3251
3252 lint_assert(sym->s_scl == EXTERN || sym->s_scl == STATIC);
3253
3254 check_global_variable_size(sym);
3255
3256 if (sym->s_scl == STATIC)
3257 check_static_global_variable(sym);
3258 }
3259
3260 static void
3261 check_global_variable_size(const sym_t *sym)
3262 {
3263 pos_t cpos;
3264 int length_in_bits;
3265
3266 if (sym->s_def != TDEF)
3267 return;
3268 if (sym->s_type->t_tspec == FUNC)
3269 /*
3270 * this can happen if a syntax error occurred after a
3271 * function declaration
3272 */
3273 return;
3274 if (sym->s_def == TDEF && sym->s_type->t_tspec == VOID)
3275 return; /* prevent internal error in length() below */
3276
3277 cpos = curr_pos;
3278 curr_pos = sym->s_def_pos;
3279 length_in_bits = length(sym->s_type, sym->s_name);
3280 curr_pos = cpos;
3281
3282 if (length_in_bits == 0 &&
3283 sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) {
3284 if (tflag || (sym->s_scl == EXTERN && !sflag)) {
3285 /* empty array declaration: %s */
3286 warning_at(190, &sym->s_def_pos, sym->s_name);
3287 } else {
3288 /* empty array declaration: %s */
3289 error_at(190, &sym->s_def_pos, sym->s_name);
3290 }
3291 }
3292 }
3293
3294 /*
3295 * Prints information about location of previous definition/declaration.
3296 */
3297 void
3298 print_previous_declaration(int msg, const sym_t *psym)
3299 {
3300
3301 if (!rflag)
3302 return;
3303
3304 if (msg != -1) {
3305 (message_at)(msg, &psym->s_def_pos);
3306 } else if (psym->s_def == DEF || psym->s_def == TDEF) {
3307 /* previous definition of %s */
3308 message_at(261, &psym->s_def_pos, psym->s_name);
3309 } else {
3310 /* previous declaration of %s */
3311 message_at(260, &psym->s_def_pos, psym->s_name);
3312 }
3313 }
3314
3315 /*
3316 * Gets a node for a constant and returns the value of this constant
3317 * as integer.
3318 *
3319 * If the node is not constant or too large for int or of type float,
3320 * a warning will be printed.
3321 *
3322 * to_int_constant() should be used only inside declarations. If it is used in
3323 * expressions, it frees the memory used for the expression.
3324 */
3325 int
3326 to_int_constant(tnode_t *tn, bool required)
3327 {
3328 int i;
3329 tspec_t t;
3330 val_t *v;
3331
3332 v = constant(tn, required);
3333
3334 if (tn == NULL) {
3335 i = 1;
3336 goto done;
3337 }
3338
3339 /*
3340 * Abstract declarations are used inside expression. To free
3341 * the memory would be a fatal error.
3342 * We don't free blocks that are inside casts because these
3343 * will be used later to match types.
3344 */
3345 if (tn->tn_op != CON && dcs->d_ctx != ABSTRACT)
3346 expr_free_all();
3347
3348 if ((t = v->v_tspec) == FLOAT || t == DOUBLE || t == LDOUBLE) {
3349 i = (int)v->v_ldbl;
3350 /* integral constant expression expected */
3351 error(55);
3352 } else {
3353 i = (int)v->v_quad;
3354 if (is_uinteger(t)) {
3355 if ((uint64_t)v->v_quad > (uint64_t)TARG_INT_MAX) {
3356 /* integral constant too large */
3357 warning(56);
3358 }
3359 } else {
3360 if (v->v_quad > (int64_t)TARG_INT_MAX ||
3361 v->v_quad < (int64_t)TARG_INT_MIN) {
3362 /* integral constant too large */
3363 warning(56);
3364 }
3365 }
3366 }
3367
3368 done:
3369 free(v);
3370 return i;
3371 }
3372