Home | History | Annotate | Line # | Download | only in lint1
lint1.h revision 1.219
      1 /* $NetBSD: lint1.h,v 1.219 2024/03/09 11:05:05 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_in_bits;
    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 (t_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, t_enum is valid.
    141 	 */
    142 	bool	t_is_enum:1;
    143 	bool	t_packed:1;
    144 	union {
    145 		int		_t_dim;		/* dimension (if ARRAY) */
    146 		struct_or_union	*_t_sou;
    147 		enumeration	*_t_enum;
    148 		sym_t		*_t_params;	/* parameters (if t_proto) */
    149 	} t_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 #define	t_dim	t_u._t_dim
    158 #define	t_sou	t_u._t_sou
    159 #define	t_enum	t_u._t_enum
    160 #define	t_params t_u._t_params
    161 
    162 
    163 typedef enum {
    164 	SK_VCFT,		/* variable, constant, function, type */
    165 	SK_MEMBER,		/* member of a struct or union */
    166 	SK_TAG,
    167 	SK_LABEL
    168 } symbol_kind;
    169 
    170 /* storage classes and related things */
    171 typedef enum {
    172 	NO_SCL,
    173 	EXTERN,			/* external symbols (independent of decl_t) */
    174 	STATIC,			/* static symbols (local and global) */
    175 	AUTO,			/* automatic symbols (except register) */
    176 	REG,			/* register */
    177 	TYPEDEF,
    178 	THREAD_LOCAL,
    179 	STRUCT_TAG,
    180 	UNION_TAG,
    181 	ENUM_TAG,
    182 	STRUCT_MEMBER,
    183 	UNION_MEMBER,
    184 	BOOL_CONST,
    185 	ENUM_CONST,
    186 	ABSTRACT,		/* abstract symbol (sizeof, casts, unnamed
    187 				 * argument) */
    188 } scl_t;
    189 
    190 /* C23 6.7.4 */
    191 typedef enum {
    192 	FS_INLINE,		/* since C99 */
    193 	FS_NORETURN,		/* since C11 */
    194 } function_specifier;
    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 			} u;
    243 		} s_keyword;
    244 		sym_t	*s_old_style_params;	/* parameters in an old-style
    245 						 * function definition */
    246 	} u;
    247 	sym_t	*s_symtab_next;	/* next symbol in the same symtab bucket */
    248 	sym_t	**s_symtab_ref;	/* pointer to s_symtab_next of the previous
    249 				 * symbol */
    250 	sym_t	*s_next;	/* next struct/union member, enumerator,
    251 				 * parameter */
    252 	sym_t	*s_level_next;	/* next symbol declared on the same level */
    253 };
    254 
    255 /*
    256  * Used to keep some information about symbols before they are entered
    257  * into the symbol table.
    258  */
    259 typedef struct {
    260 	const char *sb_name;		/* name of symbol */
    261 	size_t	sb_len;			/* length (without '\0') */
    262 	sym_t	*sb_sym;		/* symbol table entry */
    263 } sbuf_t;
    264 
    265 
    266 typedef struct {
    267 	struct tnode *func;
    268 	struct tnode **args;
    269 	size_t args_len;
    270 	size_t args_cap;
    271 } function_call;
    272 
    273 typedef struct tnode tnode_t;
    274 
    275 /* An expression, forming an abstract syntax tree. */
    276 struct tnode {
    277 	op_t	tn_op;
    278 	type_t	*tn_type;
    279 	bool	tn_lvalue:1;
    280 	bool	tn_cast:1;	/* if tn_op == CVT, it's an explicit cast */
    281 	bool	tn_parenthesized:1;
    282 	bool	tn_sys:1;	/* comes from a system header */
    283 	bool	tn_system_dependent:1; /* depends on sizeof or offsetof */
    284 	union {
    285 		struct {
    286 			tnode_t *_tn_left;	/* (left) operand */
    287 			tnode_t *_tn_right;	/* right operand */
    288 		} tn_s;
    289 		sym_t	*_tn_sym;	/* symbol if op == NAME */
    290 		val_t	_tn_val;	/* value if op == CON */
    291 		buffer	*_tn_string;	/* string if op == STRING; for
    292 					 * character strings, 'data' points to
    293 					 * the concatenated string literals in
    294 					 * source form, and 'len' is the
    295 					 * length of the concatenation; for
    296 					 * wide strings, 'data' is NULL and
    297 					 * 'len' is the number of resulting
    298 					 * characters */
    299 		function_call *_tn_call;
    300 	} tn_u;
    301 };
    302 
    303 #define	tn_left		tn_u.tn_s._tn_left
    304 #define tn_right	tn_u.tn_s._tn_right
    305 #define tn_sym		tn_u._tn_sym
    306 #define	tn_val		tn_u._tn_val
    307 #define	tn_string	tn_u._tn_string
    308 #define tn_call		tn_u._tn_call
    309 
    310 struct generic_association {
    311 	type_t *ga_arg;		/* NULL means default or error */
    312 	tnode_t *ga_result;	/* NULL means error */
    313 	struct generic_association *ga_prev;
    314 };
    315 
    316 typedef struct {
    317 	bool has_dim;
    318 	int dim;
    319 } array_size;
    320 
    321 typedef enum decl_level_kind {
    322 	DLK_EXTERN,		/* global types, variables or functions */
    323 	DLK_STRUCT,		/* struct members */
    324 	DLK_UNION,		/* union members */
    325 	DLK_ENUM,		/* enum constants */
    326 	DLK_OLD_STYLE_PARAMS,	/* parameters in an old-style function
    327 				 * definition */
    328 	DLK_PROTO_PARAMS,	/* parameters in a prototype function
    329 				 * definition */
    330 	DLK_AUTO,		/* local types or variables */
    331 	DLK_ABSTRACT		/* abstract (unnamed) declaration; type name;
    332 				 * used in casts and sizeof */
    333 } decl_level_kind;
    334 
    335 /*
    336  * A declaration level collects information for a declarator in a struct,
    337  * union or enum declaration, a parameter declaration list, or a plain
    338  * declaration in or outside a function body.
    339  *
    340  * For nested declarations, the global 'dcs' holds all information needed for
    341  * the current level, the outer levels are available via 'd_enclosing'.
    342  */
    343 typedef struct decl_level {
    344 	decl_level_kind d_kind;
    345 	tspec_t	d_abstract_type;/* VOID, BOOL, CHAR, INT or COMPLEX */
    346 	tspec_t	d_complex_mod;	/* FLOAT or DOUBLE */
    347 	tspec_t	d_sign_mod;	/* SIGNED or UNSIGN */
    348 	tspec_t	d_rank_mod;	/* SHORT, LONG or LLONG */
    349 	scl_t	d_scl;		/* storage class */
    350 	type_t	*d_type;	/* after dcs_end_type, the pointer to the type
    351 				 * used for all declarators */
    352 	sym_t	*d_redeclared_symbol;
    353 	unsigned int d_sou_size_in_bits;	/* size of the structure or
    354 						 * union being built, without
    355 						 * trailing padding */
    356 	unsigned int d_sou_align_in_bits;	/* alignment of the structure
    357 						 * or union being built */
    358 	type_qualifiers d_qual;	/* in declaration specifiers */
    359 	bool	d_inline:1;	/* inline in declaration specifiers */
    360 	bool	d_multiple_storage_classes:1; /* reported in dcs_end_type */
    361 	bool	d_invalid_type_combination:1;
    362 	bool	d_nonempty_decl:1; /* in a function declaration, whether at
    363 				 * least one tag was declared */
    364 	bool	d_no_type_specifier:1;
    365 	bool	d_asm:1;	/* set if d_ctx == AUTO and asm() present */
    366 	bool	d_packed:1;
    367 	bool	d_used:1;
    368 	type_t	*d_tag_type;	/* during a member declaration, the tag type to
    369 				 * which the member belongs */
    370 	sym_t	*d_func_params;	/* during a function declaration, the
    371 				 * parameters, stored in the enclosing level */
    372 	pos_t	d_func_def_pos;	/* position of the function definition */
    373 	sym_t	*d_first_dlsym;	/* first symbol declared at this level */
    374 	sym_t	**d_last_dlsym;	/* points to s_level_next in the last symbol
    375 				   declaration at this level */
    376 	sym_t	*d_func_proto_syms;	/* symbols defined in prototype, such
    377 					 * as tagged types or parameter names,
    378 					 * may overlap d_func_params */
    379 	struct decl_level *d_enclosing; /* the enclosing declaration level */
    380 } decl_level;
    381 
    382 typedef struct {
    383 	sym_t	*first;
    384 	bool	vararg:1;
    385 	bool	prototype:1;
    386 } parameter_list;
    387 
    388 /*
    389  * A sequence of asterisks and qualifiers, from right to left.  For example,
    390  * 'const ***volatile **const volatile' results in [c-v-, ----, --v-, ----,
    391  * ----].  The leftmost 'const' is not included in this list, it is stored in
    392  * dcs->d_qual instead.
    393  */
    394 typedef struct qual_ptr {
    395 	type_qualifiers qualifiers;
    396 	struct qual_ptr *p_next;
    397 } qual_ptr;
    398 
    399 /* The values of the 'case' labels. */
    400 typedef struct {
    401 	val_t	*vals;
    402 	size_t	len;
    403 	size_t	cap;
    404 } case_labels;
    405 
    406 typedef enum {
    407 	CS_DO_WHILE,
    408 	CS_FOR,
    409 	CS_FUNCTION_BODY,
    410 	CS_IF,
    411 	CS_SWITCH,
    412 	CS_WHILE
    413 } control_statement_kind;
    414 
    415 /*
    416  * Used to keep information about nested control statements.
    417  */
    418 typedef struct control_statement {
    419 	control_statement_kind c_kind;	/* to ensure proper nesting */
    420 	bool	c_loop:1;	/* 'continue' and 'break' are valid */
    421 	bool	c_switch:1;	/* 'case' and 'break' are valid */
    422 	bool	c_break:1;	/* the loop/switch has a reachable 'break'
    423 				 * statement */
    424 	bool	c_continue:1;	/* the loop has a reachable 'continue'
    425 				 * statement */
    426 	bool	c_default:1;	/* the switch has a 'default' label */
    427 	bool	c_maybe_endless:1;	/* the controlling expression is
    428 					 * always true (as in 'for (;;)' or
    429 					 * 'while (1)'), there may be break
    430 					 * statements though */
    431 	bool	c_always_then:1;
    432 	bool	c_reached_end_of_then:1;
    433 	bool	c_had_return_noval:1;	/* had "return;" */
    434 	bool	c_had_return_value:1;	/* had "return expr;" */
    435 
    436 	type_t	*c_switch_type;	/* type of switch expression */
    437 	tnode_t	*c_switch_expr;
    438 	case_labels c_case_labels;	/* list of case values */
    439 
    440 	memory_pool c_for_expr3_mem;	/* saved memory for end of loop
    441 					 * expression in for() */
    442 	tnode_t	*c_for_expr3;	/* end of loop expr in for() */
    443 	pos_t	c_for_expr3_pos;	/* position of end of loop expr */
    444 	pos_t	c_for_expr3_csrc_pos;	/* same for csrc_pos */
    445 
    446 	struct control_statement *c_surrounding;
    447 } control_statement;
    448 
    449 typedef struct {
    450 	size_t lo;		/* inclusive */
    451 	size_t hi;		/* inclusive */
    452 } range_t;
    453 
    454 typedef enum designator_kind {
    455 	DK_MEMBER,		/* .member */
    456 	DK_SUBSCRIPT,		/* [subscript] */
    457 	DK_SCALAR		/* no textual representation, not generated by
    458 				 * the parser; used for scalar initializer
    459 				 * expressions surrounded by braces */
    460 } designator_kind;
    461 
    462 /*
    463  * A single component on the path from the "current object" of a brace level
    464  * to the sub-object that is initialized by an expression.
    465  *
    466  * C99 6.7.8p6, 6.7.8p7
    467  */
    468 typedef struct designator {
    469 	designator_kind	dr_kind;
    470 	const sym_t	*dr_member;	/* for DK_MEMBER */
    471 	size_t		dr_subscript;	/* for DK_SUBSCRIPT */
    472 	bool		dr_done;
    473 } designator;
    474 
    475 /*
    476  * The path from the "current object" of a brace level to the sub-object that
    477  * is initialized by an expression.  Examples of designations are '.member'
    478  * or '.member[123].member.member[1][1]'.
    479  *
    480  * C99 6.7.8p6, 6.7.8p7
    481  */
    482 typedef struct designation {
    483 	designator	*dn_items;
    484 	size_t		dn_len;
    485 	size_t		dn_cap;
    486 } designation;
    487 
    488 typedef enum {
    489 	LC_ARGSUSED,
    490 	LC_BITFIELDTYPE,
    491 	LC_CONSTCOND,
    492 	LC_FALLTHROUGH,
    493 	LC_LINTLIBRARY,
    494 	LC_LINTED,
    495 	LC_LONGLONG,
    496 	LC_NOTREACHED,
    497 	LC_PRINTFLIKE,
    498 	LC_PROTOLIB,
    499 	LC_SCANFLIKE,
    500 	LC_VARARGS,
    501 } lint_comment;
    502 
    503 typedef struct {
    504 	size_t start;
    505 	size_t end;
    506 	uint64_t value;
    507 	bool escaped;		/* \n, \003, \x24 */
    508 	bool named_escape;	/* \a, \n, etc. */
    509 	bool literal_escape;	/* \?, \\, etc. */
    510 	uint8_t octal_digits;	/* 1 to 3; 0 means not applicable */
    511 	uint8_t hex_digits;	/* 1 to 3; 0 means not applicable */
    512 	bool next_literal;	/* when a new string literal begins */
    513 	bool invalid_escape;	/* single-character escape, recoverable */
    514 	bool overflow;		/* for octal and hex escapes */
    515 	bool missing_hex_digits;
    516 	bool unescaped_newline;	/* stops iterating */
    517 } quoted_iterator;
    518 
    519 #include "externs1.h"
    520 
    521 #define lint_assert(cond)						\
    522 	do {								\
    523 		if (!(cond))						\
    524 			assert_failed(__FILE__, __LINE__, __func__, #cond); \
    525 	} while (false)
    526 
    527 #ifdef DEBUG
    528 #  include "err-msgs.h"
    529 
    530 /* ARGSUSED */
    531 static inline void __printflike(1, 2)
    532 check_printf(const char *fmt, ...)
    533 {
    534 }
    535 
    536 #  define wrap_check_printf_at(func, msgid, pos, args...)		\
    537 	do {								\
    538 		check_printf(__CONCAT(MSG_, msgid), ##args);		\
    539 		(func)(msgid, pos, ##args);				\
    540 	} while (false)
    541 
    542 #  define error_at(msgid, pos, args...) \
    543 	wrap_check_printf_at(error_at, msgid, pos, ##args)
    544 #  define warning_at(msgid, pos, args...) \
    545 	wrap_check_printf_at(warning_at, msgid, pos, ##args)
    546 #  define message_at(msgid, pos, args...) \
    547 	wrap_check_printf_at(message_at, msgid, pos, ##args)
    548 
    549 #  define wrap_check_printf(func, cond, msgid, args...)			\
    550 	({								\
    551 		if (/* CONSTCOND */cond)				\
    552 			debug_step("%s:%d: %s %d '%s' in %s",		\
    553 			    __FILE__, __LINE__,	#func, msgid,		\
    554 			    __CONCAT(MSG_, msgid), __func__);		\
    555 		check_printf(__CONCAT(MSG_, msgid), ##args);		\
    556 		(func)(msgid, ##args);					\
    557 		/* LINTED 129 */					\
    558 	})
    559 
    560 #  define error(msgid, args...) wrap_check_printf(error, \
    561     true, msgid, ##args)
    562 #  define warning(msgid, args...) wrap_check_printf(warning, \
    563     true, msgid, ##args)
    564 #  define gnuism(msgid, args...) wrap_check_printf(gnuism, \
    565     !allow_gcc || (!allow_trad && !allow_c99), msgid, ##args)
    566 #  define c99ism(msgid, args...) wrap_check_printf(c99ism, \
    567     !allow_c99 && (!allow_gcc || !allow_trad), msgid, ##args)
    568 #  define c11ism(msgid, args...) wrap_check_printf(c11ism, \
    569     !allow_c11 && !allow_gcc, msgid, ##args)
    570 #  define c23ism(msgid, args...) wrap_check_printf(c23ism, \
    571     !allow_c23, msgid, ##args)
    572 #endif
    573 
    574 #ifdef DEBUG
    575 #  define query_message(query_id, args...)				\
    576 	do {								\
    577 		debug_step("%s:%d: query %d '%s' in %s",		\
    578 		    __FILE__, __LINE__,					\
    579 		    query_id, __CONCAT(MSG_Q, query_id), __func__);	\
    580 		check_printf(__CONCAT(MSG_Q, query_id), ##args);	\
    581 		(query_message)(query_id, ##args);			\
    582 	} while (false)
    583 #else
    584 #  define query_message(...)						\
    585 	do {								\
    586 		if (any_query_enabled)					\
    587 			(query_message)(__VA_ARGS__);			\
    588 	} while (false)
    589 #endif
    590 
    591 /* Copies curr_pos, keeping things unique. */
    592 static inline pos_t
    593 unique_curr_pos(void)
    594 {
    595 	pos_t curr = curr_pos;
    596 	curr_pos.p_uniq++;
    597 	if (curr_pos.p_file == csrc_pos.p_file)
    598 		csrc_pos.p_uniq++;
    599 	return curr;
    600 }
    601 
    602 static inline bool
    603 is_nonzero_val(const val_t *val)
    604 {
    605 	return is_floating(val->v_tspec)
    606 	    ? val->u.floating != 0.0
    607 	    : val->u.integer != 0;
    608 }
    609 
    610 static inline bool
    611 constant_is_nonzero(const tnode_t *tn)
    612 {
    613 	lint_assert(tn->tn_op == CON);
    614 	lint_assert(tn->tn_type->t_tspec == tn->tn_val.v_tspec);
    615 	return is_nonzero_val(&tn->tn_val);
    616 }
    617 
    618 static inline bool
    619 is_zero(const tnode_t *tn)
    620 {
    621 	return tn != NULL && tn->tn_op == CON && !is_nonzero_val(&tn->tn_val);
    622 }
    623 
    624 static inline bool
    625 is_nonzero(const tnode_t *tn)
    626 {
    627 	return tn != NULL && tn->tn_op == CON && is_nonzero_val(&tn->tn_val);
    628 }
    629 
    630 static inline const char *
    631 op_name(op_t op)
    632 {
    633 	return modtab[op].m_name;
    634 }
    635 
    636 static inline bool
    637 is_binary(const tnode_t *tn)
    638 {
    639 	return modtab[tn->tn_op].m_binary;
    640 }
    641 
    642 static inline uint64_t
    643 bit(unsigned i)
    644 {
    645 	/*
    646 	 * TODO: Add proper support for INT128. This involves changing val_t to
    647 	 * 128 bits.
    648 	 */
    649 	if (i >= 64)
    650 		return 0;	/* XXX: not correct for INT128 and UINT128 */
    651 
    652 	lint_assert(i < 64);
    653 	return (uint64_t)1 << i;
    654 }
    655 
    656 static inline bool
    657 msb(int64_t si, tspec_t t)
    658 {
    659 	return ((uint64_t)si & bit(size_in_bits(t) - 1)) != 0;
    660 }
    661 
    662 static inline uint64_t
    663 value_bits(unsigned bitsize)
    664 {
    665 	lint_assert(bitsize > 0);
    666 
    667 	/* for long double (80 or 128), double _Complex (128) */
    668 	/*
    669 	 * XXX: double _Complex does not have 128 bits of precision, therefore
    670 	 * it should never be necessary to query the value bits of such a type;
    671 	 * see d_c99_complex_split.c to trigger this case.
    672 	 */
    673 	if (bitsize >= 64)
    674 		return ~(uint64_t)0;
    675 
    676 	return ~(~(uint64_t)0 << bitsize);
    677 }
    678 
    679 /* C99 6.7.8p7 */
    680 static inline bool
    681 is_struct_or_union(tspec_t t)
    682 {
    683 	return t == STRUCT || t == UNION;
    684 }
    685 
    686 static inline bool
    687 is_member(const sym_t *sym)
    688 {
    689 	return sym->s_scl == STRUCT_MEMBER || sym->s_scl == UNION_MEMBER;
    690 }
    691 
    692 static inline void
    693 set_sym_kind(symbol_kind kind)
    694 {
    695 	if (yflag)
    696 		debug_step("%s: %s -> %s", __func__,
    697 		    symbol_kind_name(sym_kind), symbol_kind_name(kind));
    698 	sym_kind = kind;
    699 }
    700