lint1.h revision 1.169 1 /* $NetBSD: lint1.h,v 1.169 2023/06/29 12:52:06 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 * A memory pool collects allocated objects that must be available until:
41 * - the end of a block,
42 * - the end of an expression, or
43 * - the end of the translation unit.
44 */
45 typedef struct memory_pool {
46 void **items;
47 size_t len;
48 size_t cap;
49 } memory_pool;
50
51 /* See saved_lwarn in cgram.y. */
52 #define LWARN_ALL (-2)
53 #define LWARN_NONE (-1)
54
55 /*
56 * Describes the position of a declaration or anything else.
57 *
58 * FIXME: Just a single file:lineno pair is not enough to accurately describe
59 * the position of a symbol. The whole inclusion path at that point must be
60 * stored as well. This makes a difference for symbols from included
61 * headers, see print_stack_trace.
62 */
63 typedef struct {
64 const char *p_file;
65 int p_line;
66 int p_uniq; /* uniquifier */
67 } pos_t;
68
69 /* Copies curr_pos, keeping things unique. */
70 #define UNIQUE_CURR_POS(pos) \
71 do { \
72 (pos) = curr_pos; \
73 curr_pos.p_uniq++; \
74 if (curr_pos.p_file == csrc_pos.p_file) \
75 csrc_pos.p_uniq++; \
76 } while (false)
77
78 /*
79 * Strings cannot be referenced simply by a pointer to their first
80 * char. This is because strings can contain NUL characters other than the
81 * trailing NUL.
82 *
83 * Strings are stored with a trailing NUL.
84 */
85 typedef struct strg {
86 bool st_char; /* string doesn't have an 'L' prefix */
87 size_t st_len; /* length without trailing NUL */
88 void *st_mem; /* char[] for st_char, or wchar_t[] */
89 } strg_t;
90
91 /*
92 * qualifiers (only for lex/yacc interface)
93 */
94 typedef enum {
95 CONST,
96 VOLATILE,
97 RESTRICT,
98 THREAD, /* XXX: storage-class-qualifier */
99 ATOMIC,
100 } tqual_t;
101
102 /* An integer or floating-point value. */
103 typedef struct {
104 tspec_t v_tspec;
105 /*
106 * Set if an integer constant is unsigned only in C90 and later, but
107 * not in traditional C.
108 *
109 * See the operators table in ops.def, columns "l r".
110 */
111 bool v_unsigned_since_c90;
112 bool v_char_constant;
113 union {
114 int64_t _v_quad; /* integers */
115 long double _v_ldbl; /* floats */
116 } v_u;
117 } val_t;
118
119 #define v_quad v_u._v_quad
120 #define v_ldbl v_u._v_ldbl
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
125 * if they must be modified. So it would not be possible to check
126 * if two structures are identical by comparing the pointers to
127 * the type structures.
128 *
129 * The typename is used if the structure is unnamed to identify
130 * the structure type in pass 2.
131 */
132 typedef struct {
133 unsigned int sou_size_in_bits;
134 unsigned short sou_align_in_bits;
135 bool sou_incomplete:1;
136 struct sym *sou_first_member;
137 struct sym *sou_tag;
138 struct sym *sou_first_typedef;
139 } struct_or_union;
140
141 /*
142 * same as above for enums
143 */
144 typedef struct {
145 bool en_incomplete:1;
146 struct sym *en_first_enumerator;
147 struct sym *en_tag;
148 struct sym *en_first_typedef;
149 } enumeration;
150
151 /*
152 * The type of an expression or object. Complex types are formed via t_subt
153 * (for arrays, pointers and functions), as well as t_sou.
154 */
155 struct lint1_type {
156 tspec_t t_tspec; /* type specifier */
157 bool t_incomplete_array:1;
158 bool t_const:1; /* const modifier */
159 bool t_volatile:1; /* volatile modifier */
160 bool t_proto:1; /* function prototype (t_args valid) */
161 bool t_vararg:1; /* prototype with '...' */
162 bool t_typedef:1; /* type defined with typedef */
163 bool t_typeof:1; /* type defined with GCC's __typeof__ */
164 bool t_bitfield:1;
165 /*
166 * Either the type is currently an enum (having t_tspec ENUM), or
167 * it is an integer type (typically INT) that has been implicitly
168 * converted from an enum type. In both cases, t_enum is valid.
169 *
170 * The information about a former enum type is retained to allow
171 * type checks in expressions such as ((var1 & 0x0001) == var2), to
172 * detect when var1 and var2 are from incompatible enum types.
173 */
174 bool t_is_enum:1;
175 bool t_packed:1;
176 union {
177 int _t_dim; /* dimension (if ARRAY) */
178 struct_or_union *_t_sou;
179 enumeration *_t_enum;
180 struct sym *_t_args; /* arguments (if t_proto) */
181 } t_u;
182 unsigned int t_bit_field_width:8;
183 unsigned int t_bit_field_offset:24;
184 struct lint1_type *t_subt; /* element type (if ARRAY),
185 * return value (if FUNC),
186 * target type (if PTR) */
187 };
188
189 #define t_dim t_u._t_dim
190 #define t_sou t_u._t_sou
191 #define t_enum t_u._t_enum
192 #define t_args t_u._t_args
193
194 /*
195 * types of symbols
196 */
197 typedef enum {
198 FVFT, /* variables, functions, type names, enums */
199 FMEMBER, /* members of structs or unions */
200 FTAG, /* tags */
201 FLABEL /* labels */
202 } symt_t;
203
204 /*
205 * storage classes and related things
206 */
207 typedef enum {
208 NOSCL,
209 EXTERN, /* external symbols (independent of decl_t) */
210 STATIC, /* static symbols (local and global) */
211 AUTO, /* automatic symbols (except register) */
212 REG, /* register */
213 TYPEDEF, /* typedef */
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 INLINE /* only used by the parser */
223 } scl_t;
224
225 /*
226 * symbol table entry
227 */
228 typedef struct sym {
229 const char *s_name;
230 const char *s_rename; /* renamed symbol's given name */
231 pos_t s_def_pos; /* position of last (prototype) definition,
232 prototype declaration, no-prototype-def.,
233 tentative definition or declaration,
234 in this order */
235 pos_t s_set_pos; /* position of first initialization */
236 pos_t s_use_pos; /* position of first use */
237 symt_t s_kind; /* type of symbol */
238 const struct keyword *s_keyword;
239 bool s_bitfield:1;
240 bool s_set:1; /* variable set, label defined */
241 bool s_used:1; /* variable/label used */
242 bool s_arg:1; /* symbol is function argument */
243 bool s_register:1; /* symbol is register variable */
244 bool s_defarg:1; /* undefined symbol in old-style function
245 definition */
246 bool s_return_type_implicit_int:1;
247 bool s_osdef:1; /* symbol stems from old-style function def. */
248 bool s_inline:1; /* true if this is an inline function */
249 struct sym *s_ext_sym; /* for locally declared external symbols, the
250 * pointer to the external symbol with the
251 * same name */
252 def_t s_def; /* declared, tentative defined, defined */
253 scl_t s_scl; /* storage class */
254 int s_block_level; /* level of declaration, -1 if not in symbol
255 table */
256 type_t *s_type;
257 union {
258 bool s_bool_constant;
259 int s_enum_constant; /* XXX: should be TARG_INT */
260 struct {
261 /* XXX: what is the difference to s_type->t_sou? */
262 struct_or_union *sm_sou_type;
263 unsigned int sm_offset_in_bits;
264 } s_member;
265 struct {
266 int sk_token;
267 tspec_t sk_tspec; /* only for types */
268 tqual_t sk_qualifier; /* only for qualifiers */
269 } s_keyword;
270 struct sym *s_old_style_args; /* arguments in an old-style
271 * function definition */
272 } u;
273 struct sym *s_symtab_next; /* next symbol with same hash value */
274 struct sym **s_symtab_ref; /* pointer to s_symtab_next of the
275 * previous symbol */
276 struct sym *s_next; /* next struct/union member, enumerator,
277 argument */
278 struct sym *s_level_next; /* next symbol declared on the same
279 * level */
280 } sym_t;
281
282 /*
283 * Used to keep some information about symbols before they are entered
284 * into the symbol table.
285 */
286 typedef struct sbuf {
287 const char *sb_name; /* name of symbol */
288 size_t sb_len; /* length (without '\0') */
289 sym_t *sb_sym; /* symbol table entry */
290 } sbuf_t;
291
292
293 /*
294 * tree node
295 */
296 typedef struct tnode {
297 op_t tn_op; /* operator */
298 type_t *tn_type; /* type */
299 bool tn_lvalue:1; /* node is lvalue */
300 bool tn_cast:1; /* if tn_op == CVT, it's an explicit cast */
301 bool tn_parenthesized:1;
302 bool tn_sys:1; /* in strict bool mode, allow mixture between
303 * bool and scalar, for code from system
304 * headers that may be a mixture between
305 * scalar types and bool
306 */
307 bool tn_system_dependent:1; /* depends on sizeof or offsetof */
308 union {
309 struct {
310 struct tnode *_tn_left; /* (left) operand */
311 struct tnode *_tn_right; /* right operand */
312 } tn_s;
313 sym_t *_tn_sym; /* symbol if op == NAME */
314 val_t _tn_val; /* value if op == CON */
315 strg_t *_tn_string; /* string if op == STRING */
316 } tn_u;
317 } tnode_t;
318
319 #define tn_left tn_u.tn_s._tn_left
320 #define tn_right tn_u.tn_s._tn_right
321 #define tn_sym tn_u._tn_sym
322 #define tn_val tn_u._tn_val
323 #define tn_string tn_u._tn_string
324
325 struct generic_association {
326 type_t *ga_arg; /* NULL means default or error */
327 tnode_t *ga_result; /* NULL means error */
328 struct generic_association *ga_prev;
329 };
330
331 struct array_size {
332 bool has_dim;
333 int dim;
334 };
335
336 typedef enum declaration_kind {
337 DK_EXTERN, /* global variable or function */
338 DK_STRUCT_MEMBER,
339 DK_UNION_MEMBER,
340 DK_ENUM_CONSTANT,
341 DK_OLD_STYLE_ARG, /* argument in an old-style function
342 * definition */
343 DK_PROTO_ARG, /* argument in a prototype function
344 * definition */
345 DK_AUTO, /* local symbol */
346 DK_ABSTRACT /* abstract declaration; type name */
347 } declaration_kind;
348
349 /*
350 * For nested declarations there is a stack that holds all information
351 * needed for the current level. dcs points to the innermost element of this
352 * stack.
353 */
354 typedef struct dinfo {
355 declaration_kind d_kind;
356 tspec_t d_abstract_type;/* VOID, BOOL, CHAR, INT or COMPLEX */
357 tspec_t d_complex_mod; /* FLOAT or DOUBLE */
358 tspec_t d_sign_mod; /* SIGNED or UNSIGN */
359 tspec_t d_rank_mod; /* SHORT, LONG or QUAD */
360 scl_t d_scl; /* storage class */
361 type_t *d_type; /* after dcs_end_type pointer to the type used
362 for all declarators */
363 sym_t *d_redeclared_symbol;
364 unsigned int d_offset_in_bits; /* offset of next structure member */
365 unsigned short d_sou_align_in_bits; /* alignment required for current
366 * structure */
367 bool d_const:1; /* const in declaration specifiers */
368 bool d_volatile:1; /* volatile in declaration specifiers */
369 bool d_inline:1; /* inline in declaration specifiers */
370 bool d_multiple_storage_classes:1; /* reported in dcs_end_type */
371 bool d_invalid_type_combination:1;
372 bool d_nonempty_decl:1; /* if at least one tag is declared
373 * ... in the current function decl. */
374 bool d_vararg:1;
375 bool d_proto:1; /* current function decl. is a prototype */
376 bool d_notyp:1; /* set if no type specifier was present */
377 bool d_asm:1; /* set if d_ctx == AUTO and asm() present */
378 bool d_packed:1;
379 bool d_used:1;
380 type_t *d_tagtyp; /* tag during member declaration */
381 sym_t *d_func_args; /* list of arguments during function def. */
382 pos_t d_func_def_pos; /* position of function definition */
383 sym_t *d_dlsyms; /* first symbol declared at this level */
384 sym_t **d_ldlsym; /* points to s_level_next in the last symbol
385 declaration at this level */
386 sym_t *d_func_proto_syms; /* symbols defined in prototype */
387 struct dinfo *d_enclosing; /* the enclosing declaration level */
388 } dinfo_t;
389
390 /* One level of pointer indirection in declarators, including qualifiers. */
391 typedef struct qual_ptr {
392 bool p_const: 1;
393 bool p_volatile: 1;
394 bool p_pointer: 1;
395 struct qual_ptr *p_next;
396 } qual_ptr;
397
398 /*
399 * The values of the 'case' labels, linked via cl_next in reverse order of
400 * appearance in the code, that is from bottom to top.
401 */
402 typedef struct case_label {
403 val_t cl_val;
404 struct case_label *cl_next;
405 } case_label_t;
406
407 typedef enum {
408 CS_DO_WHILE,
409 CS_FOR,
410 CS_FUNCTION_BODY,
411 CS_IF,
412 CS_SWITCH,
413 CS_WHILE
414 } control_statement_kind;
415
416 /*
417 * Used to keep information about nested control statements.
418 */
419 typedef struct control_statement {
420 control_statement_kind c_kind; /* to ensure proper nesting */
421 bool c_loop:1; /* 'continue' and 'break' are valid */
422 bool c_switch:1; /* 'case' and 'break' are valid */
423 bool c_break:1; /* the loop/switch has a reachable
424 * 'break' statement */
425 bool c_continue:1; /* the loop has a reachable 'continue'
426 * statement */
427 bool c_default:1; /* the switch has a 'default' label */
428 bool c_maybe_endless:1; /* the controlling expression is
429 * always true (as in 'for (;;)' or
430 * 'while (1)'), there may be break
431 * statements though */
432 bool c_always_then:1;
433 bool c_reached_end_of_then:1;
434 bool c_had_return_noval:1; /* had "return;" */
435 bool c_had_return_value:1; /* had "return expr;" */
436
437 type_t *c_switch_type; /* type of switch expression */
438 tnode_t *c_switch_expr;
439 case_label_t *c_case_labels; /* list of case values */
440
441 memory_pool c_for_expr3_mem; /* saved memory for end of loop
442 * expression in for() */
443 tnode_t *c_for_expr3; /* end of loop expr in for() */
444 pos_t c_for_expr3_pos; /* position of end of loop expr */
445 pos_t c_for_expr3_csrc_pos; /* same for csrc_pos */
446
447 struct control_statement *c_surrounding;
448 } control_statement;
449
450 typedef struct {
451 size_t lo; /* inclusive */
452 size_t hi; /* inclusive */
453 } range_t;
454
455 #include "externs1.h"
456
457 #define lint_assert(cond) \
458 do { \
459 if (!(cond)) \
460 assert_failed(__FILE__, __LINE__, __func__, #cond); \
461 } while (false)
462
463 #ifdef DEBUG
464 # include "err-msgs.h"
465
466 /* ARGSUSED */
467 static inline void __printflike(1, 2)
468 check_printf(const char *fmt, ...)
469 {
470 }
471
472 # define wrap_check_printf_at(func, msgid, pos, args...) \
473 do { \
474 check_printf(__CONCAT(MSG_, msgid), ##args); \
475 (func)(msgid, pos, ##args); \
476 } while (false)
477
478 # define error_at(msgid, pos, args...) \
479 wrap_check_printf_at(error_at, msgid, pos, ##args)
480 # define warning_at(msgid, pos, args...) \
481 wrap_check_printf_at(warning_at, msgid, pos, ##args)
482 # define message_at(msgid, pos, args...) \
483 wrap_check_printf_at(message_at, msgid, pos, ##args)
484
485 # define wrap_check_printf(func, cond, msgid, args...) \
486 ({ \
487 if (/* CONSTCOND */cond) \
488 debug_step("%s:%d: %s %d '%s' in %s", \
489 __FILE__, __LINE__, #func, msgid, \
490 __CONCAT(MSG_, msgid), __func__); \
491 check_printf(__CONCAT(MSG_, msgid), ##args); \
492 (func)(msgid, ##args); \
493 /* LINTED 129 */ \
494 })
495
496 # define error(msgid, args...) wrap_check_printf(error, true, msgid, ##args)
497 # define warning(msgid, args...) wrap_check_printf(warning, true, msgid, ##args)
498 # define gnuism(msgid, args...) wrap_check_printf(gnuism, !allow_gcc || (!allow_trad && !allow_c99), msgid, ##args)
499 # define c99ism(msgid, args...) wrap_check_printf(c99ism, !allow_c99 && (!allow_gcc || !allow_trad), msgid, ##args)
500 # define c11ism(msgid, args...) wrap_check_printf(c11ism, !allow_c11 && !allow_gcc, msgid, ##args)
501 #endif
502
503 #ifdef DEBUG
504 # define query_message(query_id, args...) \
505 do { \
506 debug_step("%s:%d: query %d '%s' in %s", \
507 __FILE__, __LINE__, \
508 query_id, __CONCAT(MSG_Q, query_id), __func__); \
509 check_printf(__CONCAT(MSG_Q, query_id), ##args); \
510 (query_message)(query_id, ##args); \
511 } while (false)
512 #else
513 # define query_message(...) \
514 do { \
515 if (any_query_enabled) \
516 (query_message)(__VA_ARGS__); \
517 } while (false)
518 #endif
519
520 static inline bool
521 is_nonzero_val(const val_t *val)
522 {
523 return is_floating(val->v_tspec)
524 ? val->v_ldbl != 0.0
525 : val->v_quad != 0;
526 }
527
528 static inline bool
529 constant_is_nonzero(const tnode_t *tn)
530 {
531 lint_assert(tn->tn_op == CON);
532 lint_assert(tn->tn_type->t_tspec == tn->tn_val.v_tspec);
533 return is_nonzero_val(&tn->tn_val);
534 }
535
536 static inline bool
537 is_zero(const tnode_t *tn)
538 {
539 return tn != NULL && tn->tn_op == CON && !is_nonzero_val(&tn->tn_val);
540 }
541
542 static inline bool
543 is_nonzero(const tnode_t *tn)
544 {
545 return tn != NULL && tn->tn_op == CON && is_nonzero_val(&tn->tn_val);
546 }
547
548 static inline bool
549 is_binary(const tnode_t *tn)
550 {
551 return modtab[tn->tn_op].m_binary;
552 }
553
554 static inline uint64_t
555 bit(unsigned i)
556 {
557 /*
558 * TODO: Add proper support for INT128.
559 * This involves changing val_t to 128 bits.
560 */
561 if (i >= 64)
562 return 0; /* XXX: not correct for INT128 and UINT128 */
563
564 lint_assert(i < 64);
565 return (uint64_t)1 << i;
566 }
567
568 static inline bool
569 msb(int64_t q, tspec_t t)
570 {
571 return (q & bit((unsigned int)size_in_bits(t) - 1)) != 0;
572 }
573
574 static inline uint64_t
575 value_bits(unsigned bitsize)
576 {
577 lint_assert(bitsize > 0);
578
579 /* for long double (80 or 128), double _Complex (128) */
580 /*
581 * XXX: double _Complex does not have 128 bits of precision,
582 * therefore it should never be necessary to query the value bits
583 * of such a type; see d_c99_complex_split.c to trigger this case.
584 */
585 if (bitsize >= 64)
586 return ~((uint64_t)0);
587
588 return ~(~(uint64_t)0 << bitsize);
589 }
590
591 /* C99 6.7.8p7 */
592 static inline bool
593 is_struct_or_union(tspec_t t)
594 {
595 return t == STRUCT || t == UNION;
596 }
597
598 static inline bool
599 is_member(const sym_t *sym)
600 {
601 return sym->s_scl == STRUCT_MEMBER || sym->s_scl == UNION_MEMBER;
602 }
603