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