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