Home | History | Annotate | Line # | Download | only in lint1
lint1.h revision 1.200
      1 /* $NetBSD: lint1.h,v 1.200 2023/08/26 10:43:53 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 ops.def, 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
    168 	 * it is an integer type (typically INT) that has been implicitly
    169 	 * converted from an enum type.  In both cases, t_enum is valid.
    170 	 *
    171 	 * The information about a former enum type is retained to allow
    172 	 * type checks in expressions such as ((var1 & 0x0001) == var2), to
    173 	 * detect 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 	NOSCL,
    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 argument) */
    224 } scl_t;
    225 
    226 /* C23 6.7.4 */
    227 typedef enum {
    228 	FS_INLINE,		/* since C99 */
    229 	FS_NORETURN,		/* since C11 */
    230 } function_specifier;
    231 
    232 /*
    233  * symbol table entry
    234  */
    235 typedef	struct sym {
    236 	const	char *s_name;
    237 	const	char *s_rename;	/* renamed symbol's given name */
    238 	pos_t	s_def_pos;	/* position of last (prototype) definition,
    239 				   prototype declaration, no-prototype-def.,
    240 				   tentative definition or declaration,
    241 				   in this order */
    242 	pos_t	s_set_pos;	/* position of first initialization */
    243 	pos_t	s_use_pos;	/* position of first use */
    244 	symt_t	s_kind;		/* type of symbol */
    245 	const struct keyword *s_keyword;
    246 	bool	s_bitfield:1;
    247 	bool	s_set:1;	/* variable set, label defined */
    248 	bool	s_used:1;	/* variable/label used */
    249 	bool	s_param:1;	/* symbol is function parameter */
    250 	bool	s_register:1;	/* symbol is register variable */
    251 	bool	s_defparam:1;	/* undefined symbol in old-style function
    252 				   definition */
    253 	bool	s_return_type_implicit_int:1;
    254 	bool	s_osdef:1;	/* symbol stems from old-style function def. */
    255 	bool	s_inline:1;	/* true if this is an inline function */
    256 	struct	sym *s_ext_sym;	/* for locally declared external symbols, the
    257 				 * pointer to the external symbol with the
    258 				 * same name */
    259 	def_t	s_def;		/* declared, tentative defined, defined */
    260 	scl_t	s_scl;		/* storage class, more or less */
    261 	int	s_block_level;	/* level of declaration, -1 if not in symbol
    262 				   table */
    263 	type_t	*s_type;
    264 	union {
    265 		bool s_bool_constant;
    266 		int s_enum_constant;	/* XXX: should be TARG_INT */
    267 		struct {
    268 			struct_or_union	*sm_containing_type;
    269 			unsigned int sm_offset_in_bits;
    270 		} s_member;
    271 		struct {
    272 			int sk_token;
    273 			union {
    274 				/* if T_TYPE or T_STRUCT_OR_UNION */
    275 				tspec_t sk_tspec;
    276 				/* if T_QUAL */
    277 				type_qualifiers sk_type_qualifier;
    278 				/* if T_FUNCTION_SPECIFIER */
    279 				function_specifier function_specifier;
    280 			} u;
    281 		} s_keyword;
    282 		struct	sym *s_old_style_params;	/* parameters in an old-style
    283 						 * function definition */
    284 	} u;
    285 	struct	sym *s_symtab_next;	/* next symbol with same hash value */
    286 	struct	sym **s_symtab_ref;	/* pointer to s_symtab_next of the
    287 					 * previous symbol */
    288 	struct	sym *s_next;	/* next struct/union member, enumerator,
    289 				   parameter */
    290 	struct	sym *s_level_next;	/* next symbol declared on the same
    291 					 * level */
    292 } sym_t;
    293 
    294 /*
    295  * Used to keep some information about symbols before they are entered
    296  * into the symbol table.
    297  */
    298 typedef	struct sbuf {
    299 	const	char *sb_name;		/* name of symbol */
    300 	size_t	sb_len;			/* length (without '\0') */
    301 	sym_t	*sb_sym;		/* symbol table entry */
    302 } sbuf_t;
    303 
    304 
    305 /*
    306  * tree node
    307  */
    308 typedef	struct tnode {
    309 	op_t	tn_op;		/* operator */
    310 	type_t	*tn_type;	/* type */
    311 	bool	tn_lvalue:1;	/* node is lvalue */
    312 	bool	tn_cast:1;	/* if tn_op == CVT, it's an explicit cast */
    313 	bool	tn_parenthesized:1;
    314 	bool	tn_sys:1;	/* the operator comes from a system header;
    315 				 * used in strict bool mode to allow mixing
    316 				 * bool and scalar, as these places are not
    317 				 * considered fixable */
    318 	bool	tn_system_dependent:1; /* depends on sizeof or offsetof */
    319 	union {
    320 		struct {
    321 			struct	tnode *_tn_left;	/* (left) operand */
    322 			struct	tnode *_tn_right;	/* right operand */
    323 		} tn_s;
    324 		sym_t	*_tn_sym;	/* symbol if op == NAME */
    325 		val_t	_tn_val;	/* value if op == CON */
    326 		strg_t	*_tn_string;	/* string if op == STRING */
    327 	} tn_u;
    328 } tnode_t;
    329 
    330 #define	tn_left		tn_u.tn_s._tn_left
    331 #define tn_right	tn_u.tn_s._tn_right
    332 #define tn_sym		tn_u._tn_sym
    333 #define	tn_val		tn_u._tn_val
    334 #define	tn_string	tn_u._tn_string
    335 
    336 struct generic_association {
    337 	type_t *ga_arg;		/* NULL means default or error */
    338 	tnode_t *ga_result;	/* NULL means error */
    339 	struct generic_association *ga_prev;
    340 };
    341 
    342 struct array_size {
    343 	bool has_dim;
    344 	int dim;
    345 };
    346 
    347 typedef enum decl_level_kind {
    348 	DLK_EXTERN,		/* global types, variables or functions */
    349 	DLK_STRUCT,		/* members */
    350 	DLK_UNION,		/* members */
    351 	DLK_ENUM,		/* constants */
    352 	DLK_OLD_STYLE_PARAMS,	/* parameters in an old-style function
    353 				 * definition */
    354 	DLK_PROTO_PARAMS,	/* parameters in a prototype function
    355 				 * definition */
    356 	DLK_AUTO,		/* local types or variables */
    357 	DLK_ABSTRACT		/* abstract (unnamed) declaration; type name;
    358 				 * used in casts and sizeof */
    359 } decl_level_kind;
    360 
    361 /*
    362  * A declaration level collects information for a declarator in a struct,
    363  * union or enum declaration, a parameter declaration list, or a plain
    364  * declaration in or outside a function body.
    365  *
    366  * For nested declarations, the global 'dcs' holds all information needed for
    367  * the current level, the outer levels are available via 'd_enclosing'.
    368  */
    369 typedef	struct decl_level {
    370 	decl_level_kind d_kind;
    371 	tspec_t	d_abstract_type;/* VOID, BOOL, CHAR, INT or COMPLEX */
    372 	tspec_t	d_complex_mod;	/* FLOAT or DOUBLE */
    373 	tspec_t	d_sign_mod;	/* SIGNED or UNSIGN */
    374 	tspec_t	d_rank_mod;	/* SHORT, LONG or LLONG */
    375 	scl_t	d_scl;		/* storage class */
    376 	type_t	*d_type;	/* after dcs_end_type, the pointer to the type
    377 				 * used for all declarators */
    378 	sym_t	*d_redeclared_symbol;
    379 	unsigned int d_sou_size_in_bits;	/* size of the structure or
    380 						 * union being built, without
    381 						 * trailing padding */
    382 	unsigned int d_sou_align_in_bits;	/* alignment of the structure
    383 						 * or union being built */
    384 	type_qualifiers d_qual;	/* in declaration specifiers */
    385 	bool	d_inline:1;	/* inline in declaration specifiers */
    386 	bool	d_multiple_storage_classes:1; /* reported in dcs_end_type */
    387 	bool	d_invalid_type_combination:1;
    388 	bool	d_nonempty_decl:1; /* in a function declaration, whether at
    389 				 * least one tag was declared */
    390 	bool	d_no_type_specifier:1;
    391 	bool	d_asm:1;	/* set if d_ctx == AUTO and asm() present */
    392 	bool	d_packed:1;
    393 	bool	d_used:1;
    394 	type_t	*d_tag_type;	/* during a member declaration, the tag type to
    395 				 * 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 	struct decl_level *d_enclosing; /* the enclosing declaration level */
    406 } decl_level;
    407 
    408 struct parameter_list {
    409 	sym_t	*first;
    410 	bool	vararg:1;
    411 	bool	prototype:1;
    412 };
    413 
    414 /*
    415  * A sequence of asterisks and qualifiers, from right to left.  For example,
    416  * 'const ***volatile **const volatile' results in [c-v-, ----, --v-, ----,
    417  * ----].  The leftmost 'const' is not included in this list, it is stored in
    418  * dcs->d_qual instead.
    419  */
    420 typedef	struct qual_ptr {
    421 	type_qualifiers qualifiers;
    422 	struct	qual_ptr *p_next;
    423 } qual_ptr;
    424 
    425 /*
    426  * The values of the 'case' labels, linked via cl_next in reverse order of
    427  * appearance in the code, that is from bottom to top.
    428  */
    429 typedef	struct case_label {
    430 	val_t	cl_val;
    431 	struct case_label *cl_next;
    432 } case_label_t;
    433 
    434 typedef enum {
    435 	CS_DO_WHILE,
    436 	CS_FOR,
    437 	CS_FUNCTION_BODY,
    438 	CS_IF,
    439 	CS_SWITCH,
    440 	CS_WHILE
    441 } control_statement_kind;
    442 
    443 /*
    444  * Used to keep information about nested control statements.
    445  */
    446 typedef struct control_statement {
    447 	control_statement_kind c_kind;	/* to ensure proper nesting */
    448 	bool	c_loop:1;		/* 'continue' and 'break' are valid */
    449 	bool	c_switch:1;		/* 'case' and 'break' are valid */
    450 	bool	c_break:1;		/* the loop/switch has a reachable
    451 					 * 'break' statement */
    452 	bool	c_continue:1;		/* the loop has a reachable 'continue'
    453 					 * statement */
    454 	bool	c_default:1;		/* the switch has a 'default' label */
    455 	bool	c_maybe_endless:1;	/* the controlling expression is
    456 					 * always true (as in 'for (;;)' or
    457 					 * 'while (1)'), there may be break
    458 					 * statements though */
    459 	bool	c_always_then:1;
    460 	bool	c_reached_end_of_then:1;
    461 	bool	c_had_return_noval:1;	/* had "return;" */
    462 	bool	c_had_return_value:1;	/* had "return expr;" */
    463 
    464 	type_t	*c_switch_type;		/* type of switch expression */
    465 	tnode_t	*c_switch_expr;
    466 	case_label_t *c_case_labels;	/* list of case values */
    467 
    468 	memory_pool c_for_expr3_mem;	/* saved memory for end of loop
    469 					 * expression in for() */
    470 	tnode_t	*c_for_expr3;		/* end of loop expr in for() */
    471 	pos_t	c_for_expr3_pos;	/* position of end of loop expr */
    472 	pos_t	c_for_expr3_csrc_pos;	/* same for csrc_pos */
    473 
    474 	struct control_statement *c_surrounding;
    475 } control_statement;
    476 
    477 typedef struct {
    478 	size_t lo;			/* inclusive */
    479 	size_t hi;			/* inclusive */
    480 } range_t;
    481 
    482 typedef enum {
    483 	LC_ARGSUSED,
    484 	LC_BITFIELDTYPE,
    485 	LC_CONSTCOND,
    486 	LC_FALLTHROUGH,
    487 	LC_LINTLIBRARY,
    488 	LC_LINTED,
    489 	LC_LONGLONG,
    490 	LC_NOTREACHED,
    491 	LC_PRINTFLIKE,
    492 	LC_PROTOLIB,
    493 	LC_SCANFLIKE,
    494 	LC_VARARGS,
    495 } lint_comment;
    496 
    497 #include "externs1.h"
    498 
    499 #define lint_assert(cond)						\
    500 	do {								\
    501 		if (!(cond))						\
    502 			assert_failed(__FILE__, __LINE__, __func__, #cond); \
    503 	} while (false)
    504 
    505 #ifdef DEBUG
    506 #  include "err-msgs.h"
    507 
    508 /* ARGSUSED */
    509 static inline void __printflike(1, 2)
    510 check_printf(const char *fmt, ...)
    511 {
    512 }
    513 
    514 #  define wrap_check_printf_at(func, msgid, pos, args...)		\
    515 	do {								\
    516 		check_printf(__CONCAT(MSG_, msgid), ##args);		\
    517 		(func)(msgid, pos, ##args);				\
    518 	} while (false)
    519 
    520 #  define error_at(msgid, pos, args...) \
    521 	wrap_check_printf_at(error_at, msgid, pos, ##args)
    522 #  define warning_at(msgid, pos, args...) \
    523 	wrap_check_printf_at(warning_at, msgid, pos, ##args)
    524 #  define message_at(msgid, pos, args...) \
    525 	wrap_check_printf_at(message_at, msgid, pos, ##args)
    526 
    527 #  define wrap_check_printf(func, cond, msgid, args...)			\
    528 	({								\
    529 		if (/* CONSTCOND */cond)				\
    530 			debug_step("%s:%d: %s %d '%s' in %s",		\
    531 			    __FILE__, __LINE__,	#func, msgid,		\
    532 			    __CONCAT(MSG_, msgid), __func__);		\
    533 		check_printf(__CONCAT(MSG_, msgid), ##args);		\
    534 		(func)(msgid, ##args);					\
    535 		/* LINTED 129 */					\
    536 	})
    537 
    538 #  define error(msgid, args...) wrap_check_printf(error, \
    539     true, msgid, ##args)
    540 #  define warning(msgid, args...) wrap_check_printf(warning, \
    541     true, msgid, ##args)
    542 #  define gnuism(msgid, args...) wrap_check_printf(gnuism, \
    543     !allow_gcc || (!allow_trad && !allow_c99), msgid, ##args)
    544 #  define c99ism(msgid, args...) wrap_check_printf(c99ism, \
    545     !allow_c99 && (!allow_gcc || !allow_trad), msgid, ##args)
    546 #  define c11ism(msgid, args...) wrap_check_printf(c11ism, \
    547     !allow_c11 && !allow_gcc, msgid, ##args)
    548 #  define c23ism(msgid, args...) wrap_check_printf(c23ism, \
    549     !allow_c23, msgid, ##args)
    550 #endif
    551 
    552 #ifdef DEBUG
    553 #  define query_message(query_id, args...)				\
    554 	do {								\
    555 		debug_step("%s:%d: query %d '%s' in %s",		\
    556 		    __FILE__, __LINE__,					\
    557 		    query_id, __CONCAT(MSG_Q, query_id), __func__);	\
    558 		check_printf(__CONCAT(MSG_Q, query_id), ##args);	\
    559 		(query_message)(query_id, ##args);			\
    560 	} while (false)
    561 #else
    562 #  define query_message(...)						\
    563 	do {								\
    564 		if (any_query_enabled)					\
    565 			(query_message)(__VA_ARGS__);			\
    566 	} while (false)
    567 #endif
    568 
    569 /* Copies curr_pos, keeping things unique. */
    570 static inline pos_t
    571 unique_curr_pos(void)
    572 {
    573 	pos_t curr = curr_pos;
    574 	curr_pos.p_uniq++;
    575 	if (curr_pos.p_file == csrc_pos.p_file)
    576 		csrc_pos.p_uniq++;
    577 	return curr;
    578 }
    579 
    580 static inline bool
    581 is_nonzero_val(const val_t *val)
    582 {
    583 	return is_floating(val->v_tspec)
    584 	    ? val->u.floating != 0.0
    585 	    : val->u.integer != 0;
    586 }
    587 
    588 static inline bool
    589 constant_is_nonzero(const tnode_t *tn)
    590 {
    591 	lint_assert(tn->tn_op == CON);
    592 	lint_assert(tn->tn_type->t_tspec == tn->tn_val.v_tspec);
    593 	return is_nonzero_val(&tn->tn_val);
    594 }
    595 
    596 static inline bool
    597 is_zero(const tnode_t *tn)
    598 {
    599 	return tn != NULL && tn->tn_op == CON && !is_nonzero_val(&tn->tn_val);
    600 }
    601 
    602 static inline bool
    603 is_nonzero(const tnode_t *tn)
    604 {
    605 	return tn != NULL && tn->tn_op == CON && is_nonzero_val(&tn->tn_val);
    606 }
    607 
    608 static inline const char *
    609 op_name(op_t op)
    610 {
    611 	return modtab[op].m_name;
    612 }
    613 
    614 static inline bool
    615 is_binary(const tnode_t *tn)
    616 {
    617 	return modtab[tn->tn_op].m_binary;
    618 }
    619 
    620 static inline uint64_t
    621 bit(unsigned i)
    622 {
    623 	/*
    624 	 * TODO: Add proper support for INT128.
    625 	 * This involves changing val_t to 128 bits.
    626 	 */
    627 	if (i >= 64)
    628 		return 0;	/* XXX: not correct for INT128 and UINT128 */
    629 
    630 	lint_assert(i < 64);
    631 	return (uint64_t)1 << i;
    632 }
    633 
    634 static inline bool
    635 msb(int64_t si, tspec_t t)
    636 {
    637 	return ((uint64_t)si & bit(size_in_bits(t) - 1)) != 0;
    638 }
    639 
    640 static inline uint64_t
    641 value_bits(unsigned bitsize)
    642 {
    643 	lint_assert(bitsize > 0);
    644 
    645 	/* for long double (80 or 128), double _Complex (128) */
    646 	/*
    647 	 * XXX: double _Complex does not have 128 bits of precision,
    648 	 * therefore it should never be necessary to query the value bits
    649 	 * of such a type; see d_c99_complex_split.c to trigger this case.
    650 	 */
    651 	if (bitsize >= 64)
    652 		return ~((uint64_t)0);
    653 
    654 	return ~(~(uint64_t)0 << bitsize);
    655 }
    656 
    657 /* C99 6.7.8p7 */
    658 static inline bool
    659 is_struct_or_union(tspec_t t)
    660 {
    661 	return t == STRUCT || t == UNION;
    662 }
    663 
    664 static inline bool
    665 is_member(const sym_t *sym)
    666 {
    667 	return sym->s_scl == STRUCT_MEMBER || sym->s_scl == UNION_MEMBER;
    668 }
    669 
    670 static inline void
    671 set_symtyp(symt_t symt)
    672 {
    673 	if (yflag)
    674 		debug_step("%s: %s -> %s", __func__,
    675 		    symt_name(symtyp), symt_name(symt));
    676 	symtyp = symt;
    677 }
    678