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