Home | History | Annotate | Line # | Download | only in lint1
      1 /* $NetBSD: lint1.h,v 1.241 2026/06/16 05:27:57 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 #include "lint.h"
     36 #include "op.h"
     37 
     38 /*
     39  * A memory pool collects allocated objects that must be available until:
     40  * - the end of a block,
     41  * - the end of an expression, or
     42  * - the end of the translation unit.
     43  */
     44 typedef struct memory_pool {
     45 	struct memory_pool_item {
     46 		void *p;
     47 #ifdef DEBUG_MEM
     48 		size_t size;
     49 		const char *descr;
     50 #endif
     51 	} *items;
     52 	size_t	len;
     53 	size_t	cap;
     54 } memory_pool;
     55 
     56 /*
     57  * By default, all expressions are fully evaluated. In some cases such as the
     58  * sizeof operator, only the type of an expression is needed, but not its
     59  * value. In yet other cases such as a _Generic expression, not even the type
     60  * is needed, so just parsing the expression suffices.
     61  */
     62 typedef enum {
     63 	/* Only parse expressions, don't determine their type. */
     64 	EM_PARSE,
     65 	/* Determine the type of the expressions, but don't evaluate them. */
     66 	EM_TYPE,
     67 	/* Fully evaluate expressions. */
     68 	EM_EVAL,
     69 } evaluation_mode;
     70 
     71 /* See saved_lwarn in cgram.y. */
     72 #define LWARN_ALL	(-2)
     73 #define LWARN_NONE	(-1)
     74 
     75 /*
     76  * Describes the position of a declaration or anything else.
     77  *
     78  * FIXME: Just a single file:lineno pair is not enough to accurately describe
     79  *  the position of a symbol.  The whole inclusion path at that point must be
     80  *  stored as well.  This makes a difference for symbols from included
     81  *  headers, see print_stack_trace.
     82  */
     83 typedef struct {
     84 	const char *p_file;
     85 	int	p_line;
     86 	int	p_uniq;			/* uniquifier */
     87 } pos_t;
     88 
     89 typedef struct {
     90 	bool tq_const;
     91 	bool tq_restrict;
     92 	bool tq_volatile;
     93 	bool tq_atomic;
     94 } type_qualifiers;
     95 
     96 typedef struct {
     97 	bool used;
     98 	bool noreturn;
     99 	unsigned bit_width;
    100 } type_attributes;
    101 
    102 /* A bool, integer or floating-point value. */
    103 typedef struct {
    104 	tspec_t	v_tspec;
    105 	/*
    106 	 * Set if an integer constant is unsigned only in C90 and later, but
    107 	 * not in traditional C.
    108 	 *
    109 	 * See the operators table in oper.c, columns "l r".
    110 	 */
    111 	bool	v_unsigned_since_c90;
    112 	bool	v_char_constant;
    113 	union {
    114 		int64_t		integer;
    115 		long double	floating;
    116 	} u;
    117 } val_t;
    118 
    119 typedef struct sym sym_t;
    120 
    121 /*
    122  * Structures of type struct_or_union uniquely identify structures. This can't
    123  * be done in structures of type type_t, because these are copied if they must
    124  * be modified. So it would not be possible to check if two structures are
    125  * identical by comparing the pointers to the type structures.
    126  *
    127  * If the structure has no tag name, its first typedef name is used to identify
    128  * the structure in lint2.
    129  */
    130 typedef struct {
    131 	unsigned int sou_size_in_bits;
    132 	unsigned int sou_align;
    133 	bool	sou_incomplete:1;
    134 	sym_t	*sou_first_member;
    135 	sym_t	*sou_tag;
    136 	sym_t	*sou_first_typedef;
    137 } struct_or_union;
    138 
    139 /* The same as in struct_or_union, only for enums. */
    140 typedef struct {
    141 	bool	en_incomplete:1;
    142 	sym_t	*en_first_enumerator;
    143 	sym_t	*en_tag;
    144 	sym_t	*en_first_typedef;
    145 } enumeration;
    146 
    147 /* The type of an expression, object or function. */
    148 struct lint1_type {
    149 	tspec_t	t_tspec;	/* type specifier */
    150 	bool	t_incomplete_array:1;
    151 	bool	t_const:1;
    152 	bool	t_volatile:1;
    153 	bool	t_proto:1;	/* function prototype (u.params valid) */
    154 	bool	t_identifier_params:1;	/* if FUNC and not t_proto */
    155 	bool	t_vararg:1;	/* prototype with '...' */
    156 	bool	t_noreturn:1;	/* function never returns normally */
    157 	bool	t_typedef:1;	/* type defined with typedef */
    158 	bool	t_typeof:1;	/* type defined with GCC's __typeof__ */
    159 	bool	t_bitfield:1;
    160 	/*
    161 	 * Either the type is currently an enum (having t_tspec ENUM), or it
    162 	 * is an integer type (typically INT) that has been implicitly
    163 	 * converted from an enum type.  In both cases, u.enumer is valid.
    164 	 */
    165 	bool	t_is_enum:1;
    166 	bool	t_packed:1;
    167 	union {
    168 		int		dimension;	/* if ARRAY */
    169 		struct_or_union	*sou;
    170 		enumeration	*enumer;
    171 		sym_t		*params;	/* if t_proto */
    172 	} u;
    173 	unsigned int	t_bit_field_width:8;
    174 	unsigned int	t_bit_field_offset:24;
    175 	struct lint1_type *t_subt;	/*- element type (if ARRAY),
    176 					 * return value (if FUNC),
    177 					 * target type (if PTR) */
    178 };
    179 
    180 typedef enum {
    181 	SK_VCFT,		/* variable, constant, function, type */
    182 	SK_MEMBER,		/* member of a struct or union */
    183 	SK_TAG,
    184 	SK_LABEL
    185 } symbol_kind;
    186 
    187 /* storage classes and related things */
    188 typedef enum {
    189 	NO_SCL,
    190 	EXTERN,			/* external symbols (independent of decl_t) */
    191 	STATIC,			/* static symbols (local and global) */
    192 	AUTO,			/* automatic symbols (except register) */
    193 	REG,			/* register */
    194 	TYPEDEF,
    195 	THREAD_LOCAL,
    196 	STRUCT_TAG,
    197 	UNION_TAG,
    198 	ENUM_TAG,
    199 	STRUCT_MEMBER,
    200 	UNION_MEMBER,
    201 	BOOL_CONST,
    202 	ENUM_CONST,
    203 	ABSTRACT,		/* abstract symbol (sizeof, casts, unnamed
    204 				 * argument) */
    205 } scl_t;
    206 
    207 /* C23 6.7.4 */
    208 typedef enum {
    209 	FS_INLINE,		/* since C99 */
    210 	FS_NORETURN,		/* since C11 */
    211 } function_specifier;
    212 
    213 typedef enum {
    214 	NC_FALSE,		/* since C23 */
    215 	NC_TRUE,		/* since C23 */
    216 	NC_NULLPTR,		/* since C23 */
    217 } named_constant;
    218 
    219 /* A type, variable, keyword; basically anything that has a name. */
    220 struct sym {
    221 	const char *s_name;
    222 	const char *s_rename;	/* renamed symbol's given name */
    223 	pos_t	s_def_pos;	/* position of last (prototype) definition,
    224 				 * prototype declaration, no-prototype-def.,
    225 				 * tentative definition or declaration, in this
    226 				 * order */
    227 	pos_t	s_set_pos;	/* position of first initialization */
    228 	pos_t	s_use_pos;	/* position of first use */
    229 	symbol_kind s_kind;
    230 	const struct keyword *s_keyword;
    231 	bool	s_bitfield:1;
    232 	bool	s_set:1;
    233 	bool	s_used:1;
    234 	bool	s_param:1;
    235 	bool	s_register:1;
    236 	bool	s_defparam:1;	/* undefined symbol in old-style function
    237 				 * definition */
    238 	bool	s_return_type_implicit_int:1;
    239 	bool	s_osdef:1;	/* symbol stems from old-style function def. */
    240 	bool	s_inline:1;
    241 	sym_t	*s_ext_sym;	/* for locally declared external symbols, the
    242 				 * pointer to the external symbol with the same
    243 				 * name */
    244 	def_t	s_def;		/* declared, tentative defined, defined */
    245 	scl_t	s_scl;
    246 	int	s_block_level;	/* level of declaration, -1 if not in symbol
    247 				 * table */
    248 	type_t	*s_type;
    249 	union {
    250 		bool s_bool_constant;
    251 		int s_enum_constant;
    252 		struct {
    253 			struct_or_union *sm_containing_type;
    254 			unsigned int sm_offset_in_bits;
    255 		} s_member;
    256 		struct {
    257 			int sk_token;
    258 			union {
    259 				/* if T_TYPE or T_STRUCT_OR_UNION */
    260 				tspec_t sk_tspec;
    261 				/* if T_QUAL */
    262 				type_qualifiers sk_type_qualifier;
    263 				/* if T_FUNCTION_SPECIFIER */
    264 				function_specifier function_specifier;
    265 				/* if T_CON */
    266 				named_constant named_constant;
    267 			} u;
    268 		} s_keyword;
    269 		sym_t	*s_old_style_params;	/* parameters in an old-style
    270 						 * function definition */
    271 		/*
    272 		 * Only for string initializers with automatic size.
    273 		 * Does not take '\0' characters in the middle into account.
    274 		 */
    275 		size_t s_array_nonnull_dimension;
    276 	} u;
    277 	sym_t	*s_symtab_next;	/* next symbol in the same symtab bucket */
    278 	sym_t	**s_symtab_ref;	/* pointer to s_symtab_next of the previous
    279 				 * symbol */
    280 	sym_t	*s_next;	/* next struct/union member, enumerator,
    281 				 * parameter */
    282 	sym_t	*s_level_next;	/* next symbol declared on the same level */
    283 };
    284 
    285 /*
    286  * Used to keep some information about symbols before they are entered
    287  * into the symbol table.
    288  */
    289 typedef struct {
    290 	const char *sb_name;		/* name of symbol */
    291 	size_t	sb_len;			/* length (without '\0') */
    292 	sym_t	*sb_sym;		/* symbol table entry */
    293 } sbuf_t;
    294 
    295 
    296 typedef struct {
    297 	struct tnode *func;
    298 	struct tnode **args;
    299 	size_t args_len;
    300 	size_t args_cap;
    301 } function_call;
    302 
    303 typedef struct tnode tnode_t;
    304 
    305 /* An expression, forming an abstract syntax tree. */
    306 struct tnode {
    307 	op_t	tn_op;
    308 	type_t	*tn_type;
    309 	bool	tn_lvalue:1;
    310 	bool	tn_cast:1;	/* if tn_op == CVT, it's an explicit cast */
    311 	bool	tn_parenthesized:1;
    312 	bool	tn_sys:1;	/* comes from a system header */
    313 	bool	tn_system_dependent:1; /* depends on sizeof or offsetof */
    314 	union {
    315 		struct {
    316 			tnode_t *left;	/* (left) operand */
    317 			tnode_t *right;	/* right operand */
    318 		} ops;
    319 		sym_t	*sym;		/* if NAME */
    320 		val_t	value;		/* if CON */
    321 		buffer	*str_literals;	/* if STRING; for
    322 					 * character strings, 'data' points to
    323 					 * the concatenated string literals in
    324 					 * source form, and 'len' is the
    325 					 * length of the concatenation; for
    326 					 * wide strings, 'data' is NULL and
    327 					 * 'len' is the number of resulting
    328 					 * characters */
    329 		function_call *call;	/* if CALL */
    330 	} u;
    331 };
    332 
    333 struct generic_selection {
    334 	const type_t *expr_type;
    335 	tnode_t *fallback;
    336 	tnode_t *matched;
    337 	bool assoc_matched;
    338 };
    339 
    340 typedef struct {
    341 	bool has_dim;
    342 	int dim;
    343 } array_size;
    344 
    345 typedef enum decl_level_kind {
    346 	DLK_EXTERN,		/* global types, variables or functions */
    347 	DLK_STRUCT,		/* struct members */
    348 	DLK_UNION,		/* union members */
    349 	DLK_ENUM,		/* enum constants */
    350 	DLK_OLD_STYLE_PARAMS,	/* parameters in an old-style function
    351 				 * definition */
    352 	DLK_PROTO_PARAMS,	/* parameters in a prototype function
    353 				 * definition */
    354 	DLK_AUTO,		/* local types or variables */
    355 	DLK_ABSTRACT		/* abstract (unnamed) declaration; type name;
    356 				 * used in casts and sizeof */
    357 } decl_level_kind;
    358 
    359 /*
    360  * A declaration level collects information for a declarator in a struct,
    361  * union or enum declaration, a parameter declaration list, or a plain
    362  * declaration in or outside a function body.
    363  *
    364  * For nested declarations, the global 'dcs' holds all information needed for
    365  * the current level, the outer levels are available via 'd_enclosing'.
    366  */
    367 typedef struct decl_level {
    368 	decl_level_kind d_kind;
    369 	tspec_t	d_abstract_type;/* VOID, BOOL, CHAR, INT or COMPLEX */
    370 	tspec_t	d_complex_mod;	/* FLOAT or DOUBLE */
    371 	tspec_t	d_sign_mod;	/* SIGNED or UNSIGN */
    372 	tspec_t	d_rank_mod;	/* SHORT, LONG or LLONG */
    373 	scl_t	d_scl;		/* storage class */
    374 	type_t	*d_type;	/* after dcs_end_type, the pointer to the type
    375 				 * used for all declarators */
    376 	sym_t	*d_redeclared_symbol;
    377 	unsigned int d_sou_size_in_bits;	/* size of the structure or
    378 						 * union being built, without
    379 						 * trailing padding */
    380 	unsigned int d_sou_align;	/* alignment of the structure
    381 					 * or union being built */
    382 	unsigned int d_mem_align;	/* alignment of the structure
    383 					 * or union member */
    384 	type_qualifiers d_qual;	/* in declaration specifiers */
    385 	bool	d_inline:1;	/* inline in declaration specifiers */
    386 	bool	d_multiple_storage_classes:1; /* reported in dcs_end_type */
    387 	bool	d_invalid_type_combination:1;
    388 	bool	d_nonempty_decl:1; /* in a function declaration, whether at
    389 				 * least one tag was declared */
    390 	bool	d_no_type_specifier:1;
    391 	bool	d_asm:1;	/* set if d_ctx == AUTO and asm() present */
    392 	bool	d_packed:1;
    393 	bool	d_used:1;
    394 	bool	d_noreturn:1;	/* function never returns normally */
    395 	type_t	*d_tag_type;	/* during a member or enumerator declaration,
    396 				 * the tag type to which the member belongs */
    397 	sym_t	*d_func_params;	/* during a function declaration, the
    398 				 * parameters, stored in the enclosing level */
    399 	pos_t	d_func_def_pos;	/* position of the function definition */
    400 	sym_t	*d_first_dlsym;	/* first symbol declared at this level */
    401 	sym_t	**d_last_dlsym;	/* points to s_level_next in the last symbol
    402 				   declaration at this level */
    403 	sym_t	*d_func_proto_syms;	/* symbols defined in prototype, such
    404 					 * as tagged types or parameter names,
    405 					 * may overlap d_func_params */
    406 	bool	d_finished;	/* the declaration-specifiers are complete,
    407 				 * the declarator is being parsed */
    408 	struct decl_level *d_enclosing; /* the enclosing declaration level */
    409 } decl_level;
    410 
    411 typedef struct {
    412 	sym_t	*first;
    413 	bool	vararg:1;
    414 	bool	prototype:1;
    415 	bool	used:1;
    416 	bool	noreturn:1;
    417 	bool	identifier:1;
    418 } parameter_list;
    419 
    420 /*
    421  * A sequence of asterisks and qualifiers, from right to left.  For example,
    422  * 'const ***volatile **const volatile' results in [c-v-, ----, --v-, ----,
    423  * ----].  The leftmost 'const' is not included in this list, it is stored in
    424  * dcs->d_qual instead.
    425  */
    426 typedef struct qual_ptr {
    427 	type_qualifiers qualifiers;
    428 	struct qual_ptr *p_next;
    429 } qual_ptr;
    430 
    431 /* The values of the 'case' labels. */
    432 typedef struct {
    433 	val_t	*vals;
    434 	size_t	len;
    435 	size_t	cap;
    436 } case_labels;
    437 
    438 typedef enum {
    439 	CS_DO_WHILE,
    440 	CS_FOR,
    441 	CS_FUNCTION_BODY,
    442 	CS_IF,
    443 	CS_SWITCH,
    444 	CS_WHILE
    445 } control_statement_kind;
    446 
    447 /*
    448  * Used to keep information about nested control statements.
    449  */
    450 typedef struct control_statement {
    451 	control_statement_kind c_kind;	/* to ensure proper nesting */
    452 	bool	c_loop:1;	/* 'continue' and 'break' are valid */
    453 	bool	c_switch:1;	/* 'case' and 'break' are valid */
    454 	bool	c_break:1;	/* the loop/switch has a reachable 'break'
    455 				 * statement */
    456 	bool	c_continue:1;	/* the loop has a reachable 'continue'
    457 				 * statement */
    458 	bool	c_default:1;	/* the switch has a 'default' label */
    459 	bool	c_maybe_endless:1;	/* the controlling expression is
    460 					 * always true (as in 'for (;;)' or
    461 					 * 'while (1)'), there may be break
    462 					 * statements though */
    463 	bool	c_always_then:1;
    464 	bool	c_reached_end_of_then:1;
    465 	bool	c_had_return_noval:1;	/* had "return;" */
    466 	bool	c_had_return_value:1;	/* had "return expr;" */
    467 
    468 	type_t	*c_switch_type;	/* type of switch expression */
    469 	tnode_t	*c_switch_expr;
    470 	case_labels c_case_labels;	/* list of case values */
    471 
    472 	memory_pool c_for_expr3_mem;	/* saved memory for end of loop
    473 					 * expression in for() */
    474 	tnode_t	*c_for_expr3;	/* end of loop expr in for() */
    475 	pos_t	c_for_expr3_pos;	/* position of end of loop expr */
    476 	pos_t	c_for_expr3_csrc_pos;	/* same for csrc_pos */
    477 
    478 	struct control_statement *c_surrounding;
    479 } control_statement;
    480 
    481 typedef struct {
    482 	size_t lo;		/* inclusive */
    483 	size_t hi;		/* inclusive */
    484 } range_t;
    485 
    486 typedef enum designator_kind {
    487 	DK_MEMBER,		/* .member */
    488 	DK_SUBSCRIPT,		/* [subscript] */
    489 	DK_SCALAR		/* no textual representation, not generated by
    490 				 * the parser; used for scalar initializer
    491 				 * expressions surrounded by braces */
    492 } designator_kind;
    493 
    494 /*
    495  * A single component on the path from the "current object" of a brace level
    496  * to the sub-object that is initialized by an expression.
    497  *
    498  * C99 6.7.8p6, 6.7.8p7
    499  */
    500 typedef struct designator {
    501 	designator_kind	dr_kind;
    502 	const sym_t	*dr_member;	/* for DK_MEMBER */
    503 	size_t		dr_subscript;	/* for DK_SUBSCRIPT */
    504 	bool		dr_done;
    505 } designator;
    506 
    507 /*
    508  * The path from the "current object" of a brace level to the sub-object that
    509  * is initialized by an expression.  Examples of designations are '.member'
    510  * or '.member[123].member.member[1][1]'.
    511  *
    512  * C99 6.7.8p6, 6.7.8p7
    513  */
    514 typedef struct designation {
    515 	designator	*dn_items;
    516 	size_t		dn_len;
    517 	size_t		dn_cap;
    518 } designation;
    519 
    520 typedef enum {
    521 	LC_ARGSUSED,
    522 	LC_BITFIELDTYPE,
    523 	LC_FALLTHROUGH,
    524 	LC_LINTLIBRARY,
    525 	LC_LINTED,
    526 	LC_LONGLONG,
    527 	LC_NOTREACHED,
    528 	LC_PRINTFLIKE,
    529 	LC_PROTOLIB,
    530 	LC_SCANFLIKE,
    531 	LC_VARARGS,
    532 } lint_comment;
    533 
    534 typedef struct {
    535 	size_t start;
    536 	size_t end;
    537 	uint64_t value;
    538 	bool escaped;		/* \n, \003, \x24 */
    539 	bool named_escape;	/* \a, \n, etc. */
    540 	bool literal_escape;	/* \?, \\, etc. */
    541 	uint8_t octal_digits;	/* 1 to 3; 0 means not applicable */
    542 	uint8_t hex_digits;	/* 1 to 3; 0 means not applicable */
    543 	bool next_literal;	/* when a new string literal begins */
    544 	bool invalid_escape;	/* single-character escape, recoverable */
    545 	bool overflow;		/* for octal and hex escapes */
    546 	bool missing_hex_digits;
    547 	bool unescaped_newline;	/* stops iterating */
    548 } quoted_iterator;
    549 
    550 typedef enum {
    551 	TK_IDENTIFIER,
    552 	TK_CONSTANT,
    553 	TK_STRING_LITERALS,
    554 	TK_PUNCTUATOR,
    555 } token_kind;
    556 
    557 typedef struct token {
    558 	token_kind kind;
    559 	union {
    560 		const char *identifier;
    561 		val_t constant;
    562 		buffer string_literals;
    563 		const char *punctuator;
    564 	} u;
    565 } token;
    566 
    567 typedef struct balanced_token_sequence balanced_token_sequence;
    568 typedef struct balanced_token balanced_token;
    569 
    570 struct balanced_token_sequence {
    571 	balanced_token *tokens;
    572 	size_t len;
    573 	size_t cap;
    574 };
    575 
    576 struct balanced_token {
    577 	char kind;	// '\0', '(', '[', '{'
    578 	union {
    579 		token token;
    580 		balanced_token_sequence tokens;
    581 	} u;
    582 };
    583 
    584 typedef struct {
    585 	const char *prefix;
    586 	const char *name;
    587 	balanced_token_sequence *arg;
    588 } attribute;
    589 
    590 typedef struct {
    591 	attribute *attrs;
    592 	size_t len;
    593 	size_t cap;
    594 } attribute_list;
    595 
    596 #include "externs1.h"
    597 
    598 #define lint_assert(cond)						\
    599 	do {								\
    600 		if (!(cond))						\
    601 			assert_failed(__FILE__, __LINE__, __func__, #cond); \
    602 	} while (false)
    603 
    604 #ifdef DEBUG
    605 #  include "err-msgs.h"
    606 
    607 /* ARGSUSED */
    608 static inline void __printflike(1, 2)
    609 check_printf(const char *fmt, ...)
    610 {
    611 }
    612 
    613 #  define wrap_check_printf_at(func, msgid, pos, args...)		\
    614 	do {								\
    615 		check_printf(__CONCAT(MSG_, msgid), ##args);		\
    616 		(func)(msgid, pos, ##args);				\
    617 	} while (false)
    618 
    619 #  define error_at(msgid, pos, args...) \
    620 	wrap_check_printf_at(error_at, msgid, pos, ##args)
    621 #  define warning_at(msgid, pos, args...) \
    622 	wrap_check_printf_at(warning_at, msgid, pos, ##args)
    623 #  define message_at(msgid, pos, args...) \
    624 	wrap_check_printf_at(message_at, msgid, pos, ##args)
    625 
    626 #  define wrap_check_printf(func, cond, msgid, args...)			\
    627 	({								\
    628 		if (cond)						\
    629 			debug_step("%s:%d: %s %d '%s' in %s",		\
    630 			    __FILE__, __LINE__,	#func, msgid,		\
    631 			    __CONCAT(MSG_, msgid), __func__);		\
    632 		check_printf(__CONCAT(MSG_, msgid), ##args);		\
    633 		(func)(msgid, ##args);					\
    634 		/* LINTED 129 */					\
    635 	})
    636 
    637 #  define error(msgid, args...) wrap_check_printf(error, \
    638     true, msgid, ##args)
    639 #  define warning(msgid, args...) wrap_check_printf(warning, \
    640     true, msgid, ##args)
    641 #  define gnuism(msgid, args...) wrap_check_printf(gnuism, \
    642     !allow_gcc || (!allow_trad && !allow_c99), msgid, ##args)
    643 #  define c99ism(msgid, args...) wrap_check_printf(c99ism, \
    644     !allow_c99 && (!allow_gcc || !allow_trad), msgid, ##args)
    645 #  define c11ism(msgid, args...) wrap_check_printf(c11ism, \
    646     !allow_c11 && !allow_gcc, msgid, ##args)
    647 #  define c23ism(msgid, args...) wrap_check_printf(c23ism, \
    648     !allow_c23, msgid, ##args)
    649 #endif
    650 
    651 #ifdef DEBUG
    652 #  define query_message(query_id, args...)				\
    653 	do {								\
    654 		debug_step("%s:%d: query %d '%s' in %s",		\
    655 		    __FILE__, __LINE__,					\
    656 		    query_id, __CONCAT(MSG_Q, query_id), __func__);	\
    657 		check_printf(__CONCAT(MSG_Q, query_id), ##args);	\
    658 		(query_message)(query_id, ##args);			\
    659 	} while (false)
    660 #else
    661 #  define query_message(...)						\
    662 	do {								\
    663 		if (any_query_enabled)					\
    664 			(query_message)(__VA_ARGS__);			\
    665 	} while (false)
    666 #endif
    667 
    668 /* Copies curr_pos, keeping things unique. */
    669 static inline pos_t
    670 unique_curr_pos(void)
    671 {
    672 	pos_t curr = curr_pos;
    673 	curr_pos.p_uniq++;
    674 	if (curr_pos.p_file == csrc_pos.p_file)
    675 		csrc_pos.p_uniq++;
    676 	return curr;
    677 }
    678 
    679 static inline bool
    680 is_nonzero_val(const val_t *val)
    681 {
    682 	return is_floating(val->v_tspec)
    683 	    ? val->u.floating != 0.0
    684 	    : val->u.integer != 0;
    685 }
    686 
    687 static inline bool
    688 constant_is_nonzero(const tnode_t *tn)
    689 {
    690 	lint_assert(tn->tn_op == CON);
    691 	lint_assert(tn->tn_type->t_tspec == tn->u.value.v_tspec);
    692 	return is_nonzero_val(&tn->u.value);
    693 }
    694 
    695 static inline bool
    696 is_zero(const tnode_t *tn)
    697 {
    698 	return tn != NULL && tn->tn_op == CON && !is_nonzero_val(&tn->u.value);
    699 }
    700 
    701 static inline bool
    702 is_nonzero(const tnode_t *tn)
    703 {
    704 	return tn != NULL && tn->tn_op == CON && is_nonzero_val(&tn->u.value);
    705 }
    706 
    707 static inline const char *
    708 op_name(op_t op)
    709 {
    710 	return modtab[op].m_name;
    711 }
    712 
    713 static inline bool
    714 is_binary(const tnode_t *tn)
    715 {
    716 	return modtab[tn->tn_op].m_binary;
    717 }
    718 
    719 static inline uint64_t
    720 bit(unsigned i)
    721 {
    722 	/*
    723 	 * TODO: Add proper support for INT128. This involves changing val_t to
    724 	 * 128 bits.
    725 	 */
    726 	if (i >= 64)
    727 		return 0;	/* XXX: not correct for INT128 and UINT128 */
    728 
    729 	lint_assert(i < 64);
    730 	return (uint64_t)1 << i;
    731 }
    732 
    733 static inline bool
    734 msb(int64_t si, tspec_t t)
    735 {
    736 	return ((uint64_t)si & bit(size_in_bits(t) - 1)) != 0;
    737 }
    738 
    739 static inline uint64_t
    740 value_bits(unsigned bitsize)
    741 {
    742 	lint_assert(bitsize > 0);
    743 
    744 	/* for long double (80 or 128), double _Complex (128) */
    745 	/*
    746 	 * XXX: double _Complex does not have 128 bits of precision, therefore
    747 	 * it should never be necessary to query the value bits of such a type;
    748 	 * see d_c99_complex_split.c to trigger this case.
    749 	 */
    750 	if (bitsize >= 64)
    751 		return ~(uint64_t)0;
    752 
    753 	return ~(~(uint64_t)0 << bitsize);
    754 }
    755 
    756 /* C99 6.7.8p7 */
    757 static inline bool
    758 is_struct_or_union(tspec_t t)
    759 {
    760 	return t == STRUCT || t == UNION;
    761 }
    762 
    763 static inline bool
    764 is_member(const sym_t *sym)
    765 {
    766 	return sym->s_scl == STRUCT_MEMBER || sym->s_scl == UNION_MEMBER;
    767 }
    768 
    769 static inline void
    770 set_sym_kind(symbol_kind kind)
    771 {
    772 	if (yflag)
    773 		debug_step("%s: %s -> %s", __func__,
    774 		    symbol_kind_name(sym_kind), symbol_kind_name(kind));
    775 	sym_kind = kind;
    776 }
    777 
    778 static inline type_attributes
    779 merge_type_attributes(type_attributes a, type_attributes b)
    780 {
    781 	return (type_attributes){
    782 		.used = a.used || b.used,
    783 		.noreturn = a.noreturn || b.noreturn,
    784 		.bit_width = b.bit_width > 0 ? b.bit_width : a.bit_width,
    785 	};
    786 }
    787 
    788 static inline type_attributes
    789 no_type_attributes(void)
    790 {
    791 	return (type_attributes){ .used = false };
    792 }
    793