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