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