lint1.h revision 1.94 1 /* $NetBSD: lint1.h,v 1.94 2021/04/10 18:36:27 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 lint1_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 lint1_type *t_subt; /* element type (if ARRAY),
173 * return value (if FUNC),
174 * target type (if PTR) */
175 };
176
177 #define t_dim t_u._t_dim
178 #define t_str t_u._t_str
179 #define t_enum t_u._t_enum
180 #define t_args t_u._t_args
181 #define t_flen t_b._t_flen
182 #define t_foffs t_b._t_foffs
183
184 /*
185 * types of symbols
186 */
187 typedef enum {
188 FVFT, /* variables, functions, type names, enums */
189 FMEMBER, /* members of structs or unions */
190 FTAG, /* tags */
191 FLABEL /* labels */
192 } symt_t;
193
194 /*
195 * storage classes
196 */
197 typedef enum {
198 NOSCL,
199 EXTERN, /* external symbols (indep. of decl_t) */
200 STATIC, /* static symbols (local and global) */
201 AUTO, /* automatic symbols (except register) */
202 REG, /* register */
203 TYPEDEF, /* typedef */
204 STRUCT_TAG,
205 UNION_TAG,
206 ENUM_TAG,
207 MOS, /* member of struct */
208 MOU, /* member of union */
209 CTCONST, /* enumerator, enum constant or bool constant */
210 ABSTRACT, /* abstract symbol (sizeof, casts, unnamed argument) */
211 ARG, /* argument */
212 PROTO_ARG, /* used in declaration stack during prototype
213 declaration */
214 INLINE /* only used by the parser */
215 } scl_t;
216
217 /*
218 * symbol table entry
219 */
220 typedef struct sym {
221 const char *s_name;
222 const char *s_rename; /* renamed symbol's given name */
223 pos_t s_def_pos; /* position of last (prototype) definition,
224 prototype declaration, no-prototype-def.,
225 tentative definition or declaration,
226 in this order */
227 pos_t s_set_pos; /* position of first initialization */
228 pos_t s_use_pos; /* position of first use */
229 symt_t s_kind; /* type of symbol */
230 void *s_keyword;
231 bool s_bitfield : 1;
232 bool s_set : 1; /* variable set, label defined */
233 bool s_used : 1; /* variable/label used */
234 bool s_arg : 1; /* symbol is function argument */
235 bool s_reg : 1; /* symbol is register variable */
236 bool s_defarg : 1; /* undefined symbol in old style function
237 definition */
238 bool s_return_type_implicit_int : 1;
239 bool s_osdef : 1; /* symbol stems from old style function def. */
240 bool s_inline : 1; /* true if this is an inline function */
241 struct sym *s_ext_sym; /* for local declared external symbols pointer
242 to external symbol with same name */
243 def_t s_def; /* declared, tentative defined, defined */
244 scl_t s_scl; /* storage class */
245 int s_block_level; /* level of declaration, -1 if not in symbol
246 table */
247 type_t *s_type;
248 val_t s_value; /* value (if enum or bool constant) */
249 union {
250 struct_or_union *_s_st;
251 enumeration *_s_et;
252 tspec_t _s_tsp; /* type (only for keywords) */
253 tqual_t _s_tqu; /* qualifier (only for keywords) */
254 struct sym *_s_args; /* arguments in old style function
255 definitions */
256 } u;
257 struct sym *s_link; /* next symbol with same hash value */
258 struct sym **s_rlink; /* pointer to s_link of prev. symbol */
259 struct sym *s_next; /* next struct/union member, enumerator,
260 argument */
261 struct sym *s_dlnxt; /* next symbol declared on same level */
262 } sym_t;
263
264 #define s_styp u._s_st
265 #define s_etyp u._s_et
266 #define s_tspec u._s_tsp
267 #define s_tqual u._s_tqu
268 #define s_args u._s_args
269
270 /*
271 * Used to keep some information about symbols before they are entered
272 * into the symbol table.
273 */
274 typedef struct sbuf {
275 const char *sb_name; /* name of symbol */
276 size_t sb_len; /* length (without '\0') */
277 int sb_hash; /* hash value */
278 sym_t *sb_sym; /* symbol table entry */
279 struct sbuf *sb_next; /* for freelist */
280 } sbuf_t;
281
282
283 /*
284 * tree node
285 */
286 typedef struct tnode {
287 op_t tn_op; /* operator */
288 type_t *tn_type; /* type */
289 bool tn_lvalue : 1; /* node is lvalue */
290 bool tn_cast : 1; /* if tn_op == CVT, it's an explicit cast */
291 bool tn_parenthesized : 1;
292 bool tn_from_system_header : 1;
293 bool tn_system_dependent : 1; /* depends on sizeof or offsetof */
294 union {
295 struct {
296 struct tnode *_tn_left; /* (left) operand */
297 struct tnode *_tn_right; /* right operand */
298 } tn_s;
299 sym_t *_tn_sym; /* symbol if op == NAME */
300 val_t *_tn_val; /* value if op == CON */
301 strg_t *_tn_string; /* string if op == STRING */
302 } tn_u;
303 } tnode_t;
304
305 #define tn_left tn_u.tn_s._tn_left
306 #define tn_right tn_u.tn_s._tn_right
307 #define tn_sym tn_u._tn_sym
308 #define tn_val tn_u._tn_val
309 #define tn_string tn_u._tn_string
310
311 /*
312 * For nested declarations a stack exists, which holds all information
313 * needed for the current level. dcs points to the innermost element of this
314 * stack.
315 *
316 * d_ctx describes the context of the current declaration. Its value is
317 * one of
318 * EXTERN global declarations
319 * MOS or MOU declarations of struct or union members
320 * CTCONST declarations of enums or boolean constants
321 * ARG declaration of arguments in old-style function
322 * definitions
323 * PROTO_ARG declaration of arguments in function prototypes
324 * AUTO declaration of local symbols
325 * ABSTRACT abstract declarations (sizeof, casts)
326 *
327 */
328 typedef struct dinfo {
329 tspec_t d_abstract_type;/* VOID, BOOL, CHAR, INT or COMPLEX */
330 tspec_t d_complex_mod; /* FLOAT or DOUBLE */
331 tspec_t d_sign_mod; /* SIGNED or UNSIGN */
332 tspec_t d_rank_mod; /* SHORT, LONG or QUAD */
333 scl_t d_scl; /* storage class */
334 type_t *d_type; /* after deftyp() pointer to the type used
335 for all declarators */
336 sym_t *d_redeclared_symbol;
337 int d_offset; /* offset of next structure member */
338 int d_stralign; /* alignment required for current structure */
339 scl_t d_ctx; /* context of declaration */
340 bool d_const : 1; /* const in declaration specifiers */
341 bool d_volatile : 1; /* volatile in declaration specifiers */
342 bool d_inline : 1; /* inline in declaration specifiers */
343 bool d_mscl : 1; /* multiple storage classes */
344 bool d_terr : 1; /* invalid type combination */
345 bool d_nonempty_decl : 1; /* if at least one tag is declared
346 * ... in the current function decl. */
347 bool d_vararg : 1;
348 bool d_proto : 1; /* current function decl. is prototype */
349 bool d_notyp : 1; /* set if no type specifier was present */
350 bool d_asm : 1; /* set if d_ctx == AUTO and asm() present */
351 bool d_packed : 1;
352 bool d_used : 1;
353 type_t *d_tagtyp; /* tag during member declaration */
354 sym_t *d_func_args; /* list of arguments during function def. */
355 pos_t d_func_def_pos; /* position of function definition */
356 sym_t *d_dlsyms; /* first symbol declared at this level */
357 sym_t **d_ldlsym; /* points to s_dlnxt in last symbol decl.
358 at this level */
359 sym_t *d_func_proto_syms; /* symbols defined in prototype */
360 struct dinfo *d_next; /* next level */
361 } dinfo_t;
362
363 /*
364 * Used to collect information about pointers and qualifiers in
365 * declarators.
366 */
367 typedef struct pqinf {
368 int p_pcnt; /* number of asterisks */
369 bool p_const : 1;
370 bool p_volatile : 1;
371 struct pqinf *p_next;
372 } pqinf_t;
373
374 /*
375 * The values of the 'case' labels, linked via cl_next in reverse order of
376 * appearance in the code, that is from bottom to top.
377 */
378 typedef struct case_label {
379 val_t cl_val;
380 struct case_label *cl_next;
381 } case_label_t;
382
383 typedef enum {
384 CS_DO_WHILE,
385 CS_FOR,
386 CS_FUNCTION_BODY,
387 CS_IF,
388 CS_SWITCH,
389 CS_WHILE
390 } control_statement_kind;
391
392 /*
393 * Used to keep information about nested control statements.
394 */
395 typedef struct control_statement {
396 control_statement_kind c_kind; /* to ensure proper nesting */
397 bool c_loop : 1; /* 'continue' and 'break' are valid */
398 bool c_switch : 1; /* 'case' and 'break' are valid */
399 bool c_break : 1; /* the loop/switch has a reachable
400 * 'break' statement */
401 bool c_continue : 1; /* the loop has a reachable 'continue'
402 * statement */
403 bool c_default : 1; /* the switch has a 'default' label */
404 bool c_maybe_endless : 1; /* the controlling expression is
405 * always true (as in 'for (;;)' or
406 * 'while (1)'), there may be break
407 * statements though */
408 bool c_always_then : 1;
409 bool c_reached_end_of_then : 1;
410 bool c_had_return_noval : 1; /* had "return;" */
411 bool c_had_return_value : 1; /* had "return expr;" */
412
413 type_t *c_switch_type; /* type of switch expression */
414 case_label_t *c_case_labels; /* list of case values */
415
416 struct memory_block *c_for_expr3_mem; /* saved memory for end of loop
417 * expression in for() */
418 tnode_t *c_for_expr3; /* end of loop expr in for() */
419 pos_t c_for_expr3_pos; /* position of end of loop expr */
420 pos_t c_for_expr3_csrc_pos; /* same for csrc_pos */
421
422 struct control_statement *c_surrounding;
423 } cstk_t;
424
425 typedef struct {
426 size_t lo; /* inclusive */
427 size_t hi; /* inclusive */
428 } range_t;
429
430 #include "externs1.h"
431
432 #define ERR_SETSIZE 1024
433 #define __NERRBITS (sizeof(unsigned int))
434
435 typedef struct err_set {
436 unsigned int errs_bits[(ERR_SETSIZE + __NERRBITS-1) / __NERRBITS];
437 } err_set;
438
439 #define ERR_SET(n, p) \
440 ((p)->errs_bits[(n)/__NERRBITS] |= (1 << ((n) % __NERRBITS)))
441 #define ERR_CLR(n, p) \
442 ((p)->errs_bits[(n)/__NERRBITS] &= ~(1 << ((n) % __NERRBITS)))
443 #define ERR_ISSET(n, p) \
444 (((p)->errs_bits[(n)/__NERRBITS] & (1 << ((n) % __NERRBITS))) != 0)
445 #define ERR_ZERO(p) (void)memset((p), 0, sizeof(*(p)))
446
447 #define INTERNAL_ERROR(fmt, args...) \
448 internal_error(__FILE__, __LINE__, fmt, ##args)
449
450 #define lint_assert(cond) \
451 do { \
452 if (!(cond)) \
453 assert_failed(__FILE__, __LINE__, __func__, #cond); \
454 } while (/*CONSTCOND*/false)
455
456 #ifdef BLKDEBUG
457 #define ZERO 0xa5
458 #else
459 #define ZERO 0
460 #endif
461
462 extern err_set msgset;
463
464
465 #ifdef DEBUG
466 # include "err-msgs.h"
467
468 /* ARGSUSED */
469 static inline void __attribute__((format(printf, 1, 2)))
470 check_printf(const char *fmt, ...)
471 {
472 }
473
474 # define wrap_check_printf(func, id, args...) \
475 do { \
476 check_printf(__CONCAT(MSG_, id), ##args); \
477 (func)(id, ##args); \
478 } while (/*CONSTCOND*/false)
479
480 # define error(id, args...) wrap_check_printf(error, id, ##args)
481 # define warning(id, args...) wrap_check_printf(warning, id, ##args)
482 # define message(id, args...) wrap_check_printf(message, id, ##args)
483 # define gnuism(id, args...) wrap_check_printf(gnuism, id, ##args)
484 # define c99ism(id, args...) wrap_check_printf(c99ism, id, ##args)
485 #endif
486
487 static inline bool
488 is_nonzero_val(const val_t *val)
489 {
490 return is_floating(val->v_tspec)
491 ? val->v_ldbl != 0.0
492 : val->v_quad != 0;
493 }
494
495 static inline bool
496 constant_is_nonzero(const tnode_t *tn)
497 {
498 lint_assert(tn->tn_op == CON);
499 lint_assert(tn->tn_type->t_tspec == tn->tn_val->v_tspec);
500 return is_nonzero_val(tn->tn_val);
501 }
502
503 static inline bool
504 is_zero(const tnode_t *tn)
505 {
506 return tn != NULL && tn->tn_op == CON && !is_nonzero_val(tn->tn_val);
507 }
508
509 static inline bool
510 is_nonzero(const tnode_t *tn)
511 {
512 return tn != NULL && tn->tn_op == CON && is_nonzero_val(tn->tn_val);
513 }
514