Home | History | Annotate | Line # | Download | only in lint1
lint1.h revision 1.103
      1 /* $NetBSD: lint1.h,v 1.103 2021/06/19 15:23:57 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 "err-msgs.h"
     37 #include "op.h"
     38 
     39 /*
     40  * XXX - Super conservative so that works for most systems, but we should
     41  * not depend on the host settings but the target settings in determining
     42  * the alignment. The only valid use for this is in mem1.c; uses in decl.c
     43  * are bogus.
     44  */
     45 #ifndef WORST_ALIGN
     46 #ifdef _LP64
     47 # define AVAL	15
     48 #else
     49 # define AVAL	7
     50 #endif
     51 #define WORST_ALIGN(x) (((x) + AVAL) & ~AVAL)
     52 #endif
     53 
     54 #define LWARN_BAD	(-3)
     55 #define LWARN_ALL	(-2)
     56 #define LWARN_NONE	(-1)
     57 
     58 /*
     59  * Describes the position of a declaration or anything else.
     60  *
     61  * FIXME: Just a single file:lineno pair is not enough to accurately describe
     62  *  the position of a symbol.  The whole inclusion path at that point must be
     63  *  stored as well.  This makes a difference for symbols from included
     64  *  headers, see print_stack_trace.
     65  */
     66 typedef struct {
     67 	const	char *p_file;
     68 	int	p_line;
     69 	int	p_uniq;			/* uniquifier */
     70 } pos_t;
     71 
     72 /* Copies curr_pos, keeping things unique. */
     73 #define	UNIQUE_CURR_POS(pos)						\
     74 	do {								\
     75 		(pos) = curr_pos;					\
     76 		curr_pos.p_uniq++;					\
     77 		if (curr_pos.p_file == csrc_pos.p_file)			\
     78 			csrc_pos.p_uniq++;				\
     79 	} while (false)
     80 
     81 /*
     82  * Strings cannot be referenced to simply by a pointer to its first
     83  * char. This is because strings can contain NUL characters other than the
     84  * trailing NUL.
     85  *
     86  * Strings are stored with a trailing NUL.
     87  */
     88 typedef	struct strg {
     89 	tspec_t	st_tspec;		/* CHAR or WCHAR */
     90 	size_t	st_len;			/* length without trailing NUL */
     91 	union {
     92 		u_char	*_st_cp;
     93 		wchar_t	*_st_wcp;
     94 	} st_u;
     95 } strg_t;
     96 
     97 #define st_cp	st_u._st_cp
     98 #define	st_wcp	st_u._st_wcp
     99 
    100 /*
    101  * qualifiers (only for lex/yacc interface)
    102  */
    103 typedef enum {
    104 	CONST, VOLATILE, RESTRICT, THREAD
    105 } tqual_t;
    106 
    107 /*
    108  * Integer and floating point values are stored in this structure
    109  */
    110 typedef struct {
    111 	tspec_t	v_tspec;
    112 	bool	v_ansiu;		/* set if an integer constant is
    113 					   unsigned in ANSI C */
    114 	union {
    115 		int64_t	_v_quad;	/* integers */
    116 		ldbl_t	_v_ldbl;	/* floats */
    117 	} v_u;
    118 } val_t;
    119 
    120 #define v_quad	v_u._v_quad
    121 #define v_ldbl	v_u._v_ldbl
    122 
    123 /*
    124  * Structures of type struct_or_union uniquely identify structures. This can't
    125  * be done in structures of type type_t, because these are copied
    126  * if they must be modified. So it would not be possible to check
    127  * if two structures are identical by comparing the pointers to
    128  * the type structures.
    129  *
    130  * The typename is used if the structure is unnamed to identify
    131  * the structure type in pass 2.
    132  */
    133 typedef	struct {
    134 	u_int	sou_size_in_bits;
    135 	u_int	sou_align_in_bits : 15;
    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_str.
    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_args valid) */
    162 	bool	t_vararg : 1;	/* prototype with '...' */
    163 	bool	t_typedef : 1;	/* type defined with typedef */
    164 	bool	t_bitfield : 1;
    165 	bool	t_is_enum : 1;	/* type is (or was) enum (t_enum valid) */
    166 	bool	t_packed : 1;
    167 	union {
    168 		int	_t_dim;		/* dimension (if ARRAY) */
    169 		struct_or_union	*_t_str;
    170 		enumeration	*_t_enum;
    171 		struct	sym *_t_args;	/* arguments (if t_proto) */
    172 	} t_u;
    173 	struct {
    174 		u_int	_t_flen : 8;	/* length of bit-field */
    175 		u_int	_t_foffs : 24;	/* offset of bit-field */
    176 	} t_b;
    177 	struct	lint1_type *t_subt; /* element type (if ARRAY),
    178 				 * return value (if FUNC),
    179 				 * target type (if PTR) */
    180 };
    181 
    182 #define	t_dim	t_u._t_dim
    183 #define	t_str	t_u._t_str
    184 #define	t_enum	t_u._t_enum
    185 #define	t_args	t_u._t_args
    186 #define	t_flen	t_b._t_flen
    187 #define	t_foffs	t_b._t_foffs
    188 
    189 /*
    190  * types of symbols
    191  */
    192 typedef	enum {
    193 	FVFT,		/* variables, functions, type names, enums */
    194 	FMEMBER,	/* members of structs or unions */
    195 	FTAG,		/* tags */
    196 	FLABEL		/* labels */
    197 } symt_t;
    198 
    199 /*
    200  * storage classes
    201  */
    202 typedef enum {
    203 	NOSCL,
    204 	EXTERN,		/* external symbols (indep. of decl_t) */
    205 	STATIC,		/* static symbols (local and global) */
    206 	AUTO,		/* automatic symbols (except register) */
    207 	REG,		/* register */
    208 	TYPEDEF,	/* typedef */
    209 	STRUCT_TAG,
    210 	UNION_TAG,
    211 	ENUM_TAG,
    212 	MOS,		/* member of struct */
    213 	MOU,		/* member of union */
    214 	CTCONST,	/* enumerator, enum constant or bool constant */
    215 	ABSTRACT,	/* abstract symbol (sizeof, casts, unnamed argument) */
    216 	ARG,		/* argument */
    217 	PROTO_ARG,	/* used in declaration stack during prototype
    218 			   declaration */
    219 	INLINE		/* only used by the parser */
    220 } scl_t;
    221 
    222 /*
    223  * symbol table entry
    224  */
    225 typedef	struct sym {
    226 	const	char *s_name;
    227 	const	char *s_rename;	/* renamed symbol's given name */
    228 	pos_t	s_def_pos;	/* position of last (prototype) definition,
    229 				   prototype declaration, no-prototype-def.,
    230 				   tentative definition or declaration,
    231 				   in this order */
    232 	pos_t	s_set_pos;	/* position of first initialization */
    233 	pos_t	s_use_pos;	/* position of first use */
    234 	symt_t	s_kind;		/* type of symbol */
    235 	void   *s_keyword;
    236 	bool	s_bitfield : 1;
    237 	bool	s_set : 1;	/* variable set, label defined */
    238 	bool	s_used : 1;	/* variable/label used */
    239 	bool	s_arg : 1;	/* symbol is function argument */
    240 	bool	s_reg : 1;	/* symbol is register variable */
    241 	bool	s_defarg : 1;	/* undefined symbol in old style function
    242 				   definition */
    243 	bool	s_return_type_implicit_int : 1;
    244 	bool	s_osdef : 1;	/* symbol stems from old style function def. */
    245 	bool	s_inline : 1;	/* true if this is an inline function */
    246 	struct	sym *s_ext_sym;	/* for local declared external symbols pointer
    247 				   to external symbol with same name */
    248 	def_t	s_def;		/* declared, tentative defined, defined */
    249 	scl_t	s_scl;		/* storage class */
    250 	int	s_block_level;	/* level of declaration, -1 if not in symbol
    251 				   table */
    252 	type_t	*s_type;
    253 	val_t	s_value;	/* value (if enum or bool constant) */
    254 	union {
    255 		struct_or_union	*_s_st;
    256 		enumeration	*_s_et;
    257 		tspec_t	_s_tsp;	/* type (only for keywords) */
    258 		tqual_t	_s_tqu;	/* qualifier (only for keywords) */
    259 		struct	sym *_s_args; /* arguments in old style function
    260 					 definitions */
    261 	} u;
    262 	struct	sym *s_link;	/* next symbol with same hash value */
    263 	struct	sym **s_rlink;	/* pointer to s_link of prev. symbol */
    264 	struct	sym *s_next;	/* next struct/union member, enumerator,
    265 				   argument */
    266 	struct	sym *s_dlnxt;	/* next symbol declared on same level */
    267 } sym_t;
    268 
    269 #define	s_styp	u._s_st
    270 #define	s_etyp	u._s_et
    271 #define	s_tspec	u._s_tsp
    272 #define	s_tqual	u._s_tqu
    273 #define	s_args	u._s_args
    274 
    275 /*
    276  * Used to keep some information about symbols before they are entered
    277  * into the symbol table.
    278  */
    279 typedef	struct sbuf {
    280 	const	char *sb_name;		/* name of symbol */
    281 	size_t	sb_len;			/* length (without '\0') */
    282 	int	sb_hash;		/* hash value */
    283 	sym_t	*sb_sym;		/* symbol table entry */
    284 	struct	sbuf *sb_next;		/* for freelist */
    285 } sbuf_t;
    286 
    287 
    288 /*
    289  * tree node
    290  */
    291 typedef	struct tnode {
    292 	op_t	tn_op;		/* operator */
    293 	type_t	*tn_type;	/* type */
    294 	bool	tn_lvalue : 1;	/* node is lvalue */
    295 	bool	tn_cast : 1;	/* if tn_op == CVT, it's an explicit cast */
    296 	bool	tn_parenthesized : 1;
    297 	bool	tn_from_system_header : 1;
    298 	bool	tn_system_dependent : 1; /* depends on sizeof or offsetof */
    299 	union {
    300 		struct {
    301 			struct	tnode *_tn_left;	/* (left) operand */
    302 			struct	tnode *_tn_right;	/* right operand */
    303 		} tn_s;
    304 		sym_t	*_tn_sym;	/* symbol if op == NAME */
    305 		val_t	*_tn_val;	/* value if op == CON */
    306 		strg_t	*_tn_string;	/* string if op == STRING */
    307 	} tn_u;
    308 } tnode_t;
    309 
    310 #define	tn_left		tn_u.tn_s._tn_left
    311 #define tn_right	tn_u.tn_s._tn_right
    312 #define tn_sym		tn_u._tn_sym
    313 #define	tn_val		tn_u._tn_val
    314 #define	tn_string	tn_u._tn_string
    315 
    316 /*
    317  * For nested declarations a stack exists, which holds all information
    318  * needed for the current level. dcs points to the innermost element of this
    319  * stack.
    320  *
    321  * d_ctx describes the context of the current declaration. Its value is
    322  * one of
    323  *	EXTERN		global declarations
    324  *	MOS or MOU	declarations of struct or union members
    325  *	CTCONST		declarations of enums or boolean constants
    326  *	ARG		declaration of arguments in old-style function
    327  *			definitions
    328  *	PROTO_ARG	declaration of arguments in function prototypes
    329  *	AUTO		declaration of local symbols
    330  *	ABSTRACT	abstract declarations (sizeof, casts)
    331  *
    332  */
    333 typedef	struct dinfo {
    334 	tspec_t	d_abstract_type;/* VOID, BOOL, CHAR, INT or COMPLEX */
    335 	tspec_t	d_complex_mod;	/* FLOAT or DOUBLE */
    336 	tspec_t	d_sign_mod;	/* SIGNED or UNSIGN */
    337 	tspec_t	d_rank_mod;	/* SHORT, LONG or QUAD */
    338 	scl_t	d_scl;		/* storage class */
    339 	type_t	*d_type;	/* after deftyp() pointer to the type used
    340 				   for all declarators */
    341 	sym_t	*d_redeclared_symbol;
    342 	int	d_offset;	/* offset of next structure member */
    343 	int	d_stralign;	/* alignment required for current structure */
    344 	scl_t	d_ctx;		/* context of declaration */
    345 	bool	d_const : 1;	/* const in declaration specifiers */
    346 	bool	d_volatile : 1;	/* volatile in declaration specifiers */
    347 	bool	d_inline : 1;	/* inline in declaration specifiers */
    348 	bool	d_mscl : 1;	/* multiple storage classes */
    349 	bool	d_terr : 1;	/* invalid type combination */
    350 	bool	d_nonempty_decl : 1; /* if at least one tag is declared
    351 				 * ... in the current function decl. */
    352 	bool	d_vararg : 1;
    353 	bool	d_proto : 1;	/* current function decl. is prototype */
    354 	bool	d_notyp : 1;	/* set if no type specifier was present */
    355 	bool	d_asm : 1;	/* set if d_ctx == AUTO and asm() present */
    356 	bool	d_packed : 1;
    357 	bool	d_used : 1;
    358 	type_t	*d_tagtyp;	/* tag during member declaration */
    359 	sym_t	*d_func_args;	/* list of arguments during function def. */
    360 	pos_t	d_func_def_pos;	/* position of function definition */
    361 	sym_t	*d_dlsyms;	/* first symbol declared at this level */
    362 	sym_t	**d_ldlsym;	/* points to s_dlnxt in last symbol decl.
    363 				   at this level */
    364 	sym_t	*d_func_proto_syms; /* symbols defined in prototype */
    365 	struct	dinfo *d_next;	/* next level */
    366 } dinfo_t;
    367 
    368 /*
    369  * Used to collect information about pointers and qualifiers in
    370  * declarators.
    371  */
    372 typedef	struct pqinf {
    373 	int	p_pcnt;			/* number of asterisks */
    374 	bool	p_const : 1;
    375 	bool	p_volatile : 1;
    376 	struct	pqinf *p_next;
    377 } pqinf_t;
    378 
    379 /*
    380  * The values of the 'case' labels, linked via cl_next in reverse order of
    381  * appearance in the code, that is from bottom to top.
    382  */
    383 typedef	struct case_label {
    384 	val_t	cl_val;
    385 	struct case_label *cl_next;
    386 } case_label_t;
    387 
    388 typedef enum {
    389 	CS_DO_WHILE,
    390 	CS_FOR,
    391 	CS_FUNCTION_BODY,
    392 	CS_IF,
    393 	CS_SWITCH,
    394 	CS_WHILE
    395 } control_statement_kind;
    396 
    397 /*
    398  * Used to keep information about nested control statements.
    399  */
    400 typedef struct control_statement {
    401 	control_statement_kind c_kind;	/* to ensure proper nesting */
    402 	bool	c_loop : 1;		/* 'continue' and 'break' are valid */
    403 	bool	c_switch : 1;		/* 'case' and 'break' are valid */
    404 	bool	c_break : 1;		/* the loop/switch has a reachable
    405 					 * 'break' statement */
    406 	bool	c_continue : 1;		/* the loop has a reachable 'continue'
    407 					 * statement */
    408 	bool	c_default : 1;		/* the switch has a 'default' label */
    409 	bool	c_maybe_endless : 1;	/* the controlling expression is
    410 					 * always true (as in 'for (;;)' or
    411 					 * 'while (1)'), there may be break
    412 					 * statements though */
    413 	bool	c_always_then : 1;
    414 	bool	c_reached_end_of_then : 1;
    415 	bool	c_had_return_noval : 1;	/* had "return;" */
    416 	bool	c_had_return_value : 1;	/* had "return expr;" */
    417 
    418 	type_t	*c_switch_type;		/* type of switch expression */
    419 	tnode_t	*c_switch_expr;
    420 	case_label_t *c_case_labels;	/* list of case values */
    421 
    422 	struct	memory_block *c_for_expr3_mem; /* saved memory for end of loop
    423 					 * expression in for() */
    424 	tnode_t	*c_for_expr3;		/* end of loop expr in for() */
    425 	pos_t	c_for_expr3_pos;	/* position of end of loop expr */
    426 	pos_t	c_for_expr3_csrc_pos;	/* same for csrc_pos */
    427 
    428 	struct	control_statement *c_surrounding;
    429 } cstk_t;
    430 
    431 typedef struct {
    432 	size_t lo;			/* inclusive */
    433 	size_t hi;			/* inclusive */
    434 } range_t;
    435 
    436 #include "externs1.h"
    437 
    438 #define	ERR_SETSIZE	1024
    439 #define __NERRBITS (sizeof(unsigned int))
    440 
    441 typedef	struct err_set {
    442 	unsigned int	errs_bits[(ERR_SETSIZE + __NERRBITS-1) / __NERRBITS];
    443 } err_set;
    444 
    445 #define	ERR_SET(n, p)	\
    446 	((p)->errs_bits[(n)/__NERRBITS] |= (1 << ((n) % __NERRBITS)))
    447 #define	ERR_CLR(n, p)	\
    448 	((p)->errs_bits[(n)/__NERRBITS] &= ~(1 << ((n) % __NERRBITS)))
    449 #define	ERR_ISSET(n, p)	\
    450 	(((p)->errs_bits[(n)/__NERRBITS] & (1 << ((n) % __NERRBITS))) != 0)
    451 #define	ERR_ZERO(p)	(void)memset((p), 0, sizeof(*(p)))
    452 
    453 #define INTERNAL_ERROR(fmt, args...) \
    454 	internal_error(__FILE__, __LINE__, fmt, ##args)
    455 
    456 #define lint_assert(cond)						\
    457 	do {								\
    458 		if (!(cond))						\
    459 			assert_failed(__FILE__, __LINE__, __func__, #cond); \
    460 	} while (false)
    461 
    462 #ifdef BLKDEBUG
    463 #define ZERO	0xa5
    464 #else
    465 #define	ZERO	0
    466 #endif
    467 
    468 extern err_set	msgset;
    469 
    470 
    471 #ifdef DEBUG
    472 #  include "err-msgs.h"
    473 
    474 /* ARGSUSED */
    475 static inline void __attribute__((format(printf, 1, 2)))
    476 check_printf(const char *fmt, ...)
    477 {
    478 }
    479 
    480 #  define wrap_check_printf_at(func, msgid, pos, args...)		\
    481 	do {								\
    482 		check_printf(__CONCAT(MSG_, msgid), ##args);		\
    483 		(func)(msgid, pos, ##args);				\
    484 	} while (false)
    485 
    486 #  define error_at(msgid, pos, args...) \
    487 	wrap_check_printf_at(error_at, msgid, pos, ##args)
    488 #  define warning_at(msgid, pos, args...) \
    489 	wrap_check_printf_at(warning_at, msgid, pos, ##args)
    490 #  define message_at(msgid, pos, args...) \
    491 	wrap_check_printf_at(message_at, msgid, pos, ##args)
    492 
    493 #  define wrap_check_printf(func, msgid, args...)			\
    494 	do {								\
    495 		check_printf(__CONCAT(MSG_, msgid), ##args);		\
    496 		(func)(msgid, ##args);					\
    497 	} while (false)
    498 
    499 #  define error(msgid, args...) wrap_check_printf(error, msgid, ##args)
    500 #  define warning(msgid, args...) wrap_check_printf(warning, msgid, ##args)
    501 #  define message(msgid, args...) wrap_check_printf(message, msgid, ##args)
    502 #  define gnuism(msgid, args...) wrap_check_printf(gnuism, msgid, ##args)
    503 #  define c99ism(msgid, args...) wrap_check_printf(c99ism, msgid, ##args)
    504 #  define c11ism(msgid, args...) wrap_check_printf(c11ism, msgid, ##args)
    505 #endif
    506 
    507 static inline bool
    508 is_nonzero_val(const val_t *val)
    509 {
    510 	return is_floating(val->v_tspec)
    511 	    ? val->v_ldbl != 0.0
    512 	    : val->v_quad != 0;
    513 }
    514 
    515 static inline bool
    516 constant_is_nonzero(const tnode_t *tn)
    517 {
    518 	lint_assert(tn->tn_op == CON);
    519 	lint_assert(tn->tn_type->t_tspec == tn->tn_val->v_tspec);
    520 	return is_nonzero_val(tn->tn_val);
    521 }
    522 
    523 static inline bool
    524 is_zero(const tnode_t *tn)
    525 {
    526 	return tn != NULL && tn->tn_op == CON && !is_nonzero_val(tn->tn_val);
    527 }
    528 
    529 static inline bool
    530 is_nonzero(const tnode_t *tn)
    531 {
    532 	return tn != NULL && tn->tn_op == CON && is_nonzero_val(tn->tn_val);
    533 }
    534 
    535 static inline uint64_t
    536 bit(unsigned i)
    537 {
    538 	lint_assert(i < 64);
    539 	return (uint64_t)1 << i;
    540 }
    541 
    542 static inline uint64_t
    543 value_bits(unsigned bitsize)
    544 {
    545 	lint_assert(bitsize > 0);
    546 
    547 	/* for long double (80 or 128), double _Complex (128) */
    548 	/*
    549 	 * XXX: double _Complex does not have 128 bits of precision,
    550 	 * therefore it should never be necessary to query the value bits
    551 	 * of such a type; see d_c99_complex_split.c to trigger this case.
    552 	 */
    553 	if (bitsize >= 64)
    554 		return ~((uint64_t)0);
    555 
    556 	return ~(~(uint64_t)0 << bitsize);
    557 }
    558 
    559 /* C99 6.7.8p7 */
    560 static inline bool
    561 is_struct_or_union(tspec_t t)
    562 {
    563 	return t == STRUCT || t == UNION;
    564 }
    565