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