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