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