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