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