lint1.h revision 1.194 1 /* $NetBSD: lint1.h,v 1.194 2023/07/15 13:35:24 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 struct memory_pool_item {
47 void *p;
48 #ifdef DEBUG_MEM
49 size_t size;
50 const char *descr;
51 #endif
52 } *items;
53 size_t len;
54 size_t cap;
55 } memory_pool;
56
57 /* See saved_lwarn in cgram.y. */
58 #define LWARN_ALL (-2)
59 #define LWARN_NONE (-1)
60
61 /*
62 * Describes the position of a declaration or anything else.
63 *
64 * FIXME: Just a single file:lineno pair is not enough to accurately describe
65 * the position of a symbol. The whole inclusion path at that point must be
66 * stored as well. This makes a difference for symbols from included
67 * headers, see print_stack_trace.
68 */
69 typedef struct {
70 const char *p_file;
71 int p_line;
72 int p_uniq; /* uniquifier */
73 } pos_t;
74
75 /*
76 * Strings cannot be referenced simply by a pointer to their first
77 * char. This is because strings can contain NUL characters other than the
78 * trailing NUL.
79 *
80 * Strings are stored with a trailing NUL.
81 */
82 typedef struct strg {
83 bool st_char; /* string doesn't have an 'L' prefix */
84 size_t st_len; /* length without trailing NUL */
85 void *st_mem; /* char[] for st_char, or wchar_t[] */
86 } strg_t;
87
88 typedef struct {
89 bool tq_const:1;
90 bool tq_restrict:1;
91 bool tq_volatile:1;
92 bool tq_atomic:1;
93 } type_qualifiers;
94
95 /* A bool, integer or floating-point value. */
96 typedef struct {
97 tspec_t v_tspec;
98 /*
99 * Set if an integer constant is unsigned only in C90 and later, but
100 * not in traditional C.
101 *
102 * See the operators table in ops.def, columns "l r".
103 */
104 bool v_unsigned_since_c90;
105 bool v_char_constant;
106 union {
107 int64_t integer;
108 long double floating;
109 } u;
110 } val_t;
111
112 /*
113 * Structures of type struct_or_union uniquely identify structures. This can't
114 * be done in structures of type type_t, because these are copied if they must
115 * be modified. So it would not be possible to check if two structures are
116 * identical by comparing the pointers to the type structures.
117 *
118 * If the structure has no tag name, its first typedef name is used to identify
119 * the structure in lint2.
120 */
121 typedef struct {
122 unsigned int sou_size_in_bits;
123 unsigned int sou_align_in_bits;
124 bool sou_incomplete:1;
125 struct sym *sou_first_member;
126 struct sym *sou_tag;
127 struct sym *sou_first_typedef;
128 } struct_or_union;
129
130 /*
131 * same as above for enums
132 */
133 typedef struct {
134 bool en_incomplete:1;
135 struct sym *en_first_enumerator;
136 struct sym *en_tag;
137 struct sym *en_first_typedef;
138 } enumeration;
139
140 /*
141 * The type of an expression or object. Complex types are formed via t_subt
142 * (for arrays, pointers and functions), as well as t_sou.
143 */
144 struct lint1_type {
145 tspec_t t_tspec; /* type specifier */
146 bool t_incomplete_array:1;
147 bool t_const:1; /* const modifier */
148 bool t_volatile:1; /* volatile modifier */
149 bool t_proto:1; /* function prototype (t_args valid) */
150 bool t_vararg:1; /* prototype with '...' */
151 bool t_typedef:1; /* type defined with typedef */
152 bool t_typeof:1; /* type defined with GCC's __typeof__ */
153 bool t_bitfield:1;
154 /*
155 * Either the type is currently an enum (having t_tspec ENUM), or
156 * it is an integer type (typically INT) that has been implicitly
157 * converted from an enum type. In both cases, t_enum is valid.
158 *
159 * The information about a former enum type is retained to allow
160 * type checks in expressions such as ((var1 & 0x0001) == var2), to
161 * detect when var1 and var2 are from incompatible enum types.
162 */
163 bool t_is_enum:1;
164 bool t_packed:1;
165 union {
166 int _t_dim; /* dimension (if ARRAY) */
167 struct_or_union *_t_sou;
168 enumeration *_t_enum;
169 struct sym *_t_args; /* arguments (if t_proto) */
170 } t_u;
171 unsigned int t_bit_field_width:8;
172 unsigned int t_bit_field_offset:24;
173 struct lint1_type *t_subt; /* element type (if ARRAY),
174 * return value (if FUNC),
175 * target type (if PTR) */
176 };
177
178 #define t_dim t_u._t_dim
179 #define t_sou t_u._t_sou
180 #define t_enum t_u._t_enum
181 #define t_args t_u._t_args
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 and related things
195 */
196 typedef enum {
197 NOSCL,
198 EXTERN, /* external symbols (independent of decl_t) */
199 STATIC, /* static symbols (local and global) */
200 AUTO, /* automatic symbols (except register) */
201 REG, /* register */
202 TYPEDEF, /* typedef */
203 THREAD_LOCAL,
204 STRUCT_TAG,
205 UNION_TAG,
206 ENUM_TAG,
207 STRUCT_MEMBER,
208 UNION_MEMBER,
209 BOOL_CONST,
210 ENUM_CONST,
211 ABSTRACT, /* abstract symbol (sizeof, casts, unnamed argument) */
212 } scl_t;
213
214 /* C23 6.7.4 */
215 typedef enum {
216 FS_INLINE, /* since C99 */
217 FS_NORETURN, /* since C11 */
218 } function_specifier;
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 const struct keyword *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_register: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 locally declared external symbols, the
245 * pointer to the external symbol with the
246 * same name */
247 def_t s_def; /* declared, tentative defined, defined */
248 scl_t s_scl; /* storage class, more or less */
249 int s_block_level; /* level of declaration, -1 if not in symbol
250 table */
251 type_t *s_type;
252 union {
253 bool s_bool_constant;
254 int s_enum_constant; /* XXX: should be TARG_INT */
255 struct {
256 struct_or_union *sm_containing_type;
257 unsigned int sm_offset_in_bits;
258 } s_member;
259 struct {
260 int sk_token;
261 union {
262 /* if T_TYPE or T_STRUCT_OR_UNION */
263 tspec_t sk_tspec;
264 /* if T_QUAL */
265 type_qualifiers sk_type_qualifier;
266 /* if T_FUNCTION_SPECIFIER */
267 function_specifier function_specifier;
268 } u;
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; /* the operator comes from a system header;
303 * used in strict bool mode to allow mixing
304 * bool and scalar, as these places are not
305 * considered fixable */
306 bool tn_system_dependent:1; /* depends on sizeof or offsetof */
307 union {
308 struct {
309 struct tnode *_tn_left; /* (left) operand */
310 struct tnode *_tn_right; /* right operand */
311 } tn_s;
312 sym_t *_tn_sym; /* symbol if op == NAME */
313 val_t _tn_val; /* value if op == CON */
314 strg_t *_tn_string; /* string if op == STRING */
315 } tn_u;
316 } tnode_t;
317
318 #define tn_left tn_u.tn_s._tn_left
319 #define tn_right tn_u.tn_s._tn_right
320 #define tn_sym tn_u._tn_sym
321 #define tn_val tn_u._tn_val
322 #define tn_string tn_u._tn_string
323
324 struct generic_association {
325 type_t *ga_arg; /* NULL means default or error */
326 tnode_t *ga_result; /* NULL means error */
327 struct generic_association *ga_prev;
328 };
329
330 struct array_size {
331 bool has_dim;
332 int dim;
333 };
334
335 typedef enum decl_level_kind {
336 DLK_EXTERN, /* global types, variables or functions */
337 DLK_STRUCT, /* members */
338 DLK_UNION, /* members */
339 DLK_ENUM, /* constants */
340 DLK_OLD_STYLE_ARGS, /* arguments in an old-style function
341 * definition */
342 DLK_PROTO_PARAMS, /* parameters in a prototype function
343 * definition */
344 DLK_AUTO, /* local types or variables */
345 DLK_ABSTRACT /* abstract (unnamed) declaration; type name;
346 * used in casts and sizeof */
347 } decl_level_kind;
348
349 /*
350 * A declaration level describes a struct, union, enum, block, argument
351 * declaration list or an abstract (unnamed) type.
352 *
353 * For nested declarations, the global 'dcs' holds all information needed for
354 * the current level, the outer levels are available via 'd_enclosing'.
355 */
356 typedef struct decl_level {
357 decl_level_kind d_kind;
358 tspec_t d_abstract_type;/* VOID, BOOL, CHAR, INT or COMPLEX */
359 tspec_t d_complex_mod; /* FLOAT or DOUBLE */
360 tspec_t d_sign_mod; /* SIGNED or UNSIGN */
361 tspec_t d_rank_mod; /* SHORT, LONG or LLONG */
362 scl_t d_scl; /* storage class */
363 type_t *d_type; /* after dcs_end_type, the pointer to the type
364 * used for all declarators */
365 sym_t *d_redeclared_symbol;
366 unsigned int d_sou_size_in_bits; /* size of the structure or
367 * union being built, without
368 * trailing padding */
369 unsigned int d_sou_align_in_bits; /* alignment of the structure
370 * or union being built */
371 type_qualifiers d_qual; /* in declaration specifiers */
372 bool d_inline:1; /* inline in declaration specifiers */
373 bool d_multiple_storage_classes:1; /* reported in dcs_end_type */
374 bool d_invalid_type_combination:1;
375 bool d_nonempty_decl:1; /* in a function declaration, whether at
376 * least one tag was declared */
377 bool d_vararg:1;
378 bool d_prototype:1; /* in a function declaration, whether the
379 * function has a prototype */
380 bool d_no_type_specifier:1;
381 bool d_asm:1; /* set if d_ctx == AUTO and asm() present */
382 bool d_packed:1;
383 bool d_used:1;
384 type_t *d_tag_type; /* during a member declaration, the tag type to
385 * which the member belongs */
386 sym_t *d_func_args; /* during a function declaration, the list of
387 * arguments */
388 pos_t d_func_def_pos; /* position of the function definition */
389 sym_t *d_first_dlsym; /* first symbol declared at this level */
390 sym_t **d_last_dlsym; /* points to s_level_next in the last symbol
391 declaration at this level */
392 sym_t *d_func_proto_syms; /* symbols defined in prototype */
393 struct decl_level *d_enclosing; /* the enclosing declaration level */
394 } decl_level;
395
396 /*
397 * A sequence of asterisks and qualifiers, from right to left. For example,
398 * 'const ***volatile **const volatile' results in [c-v-, ----, --v-, ----,
399 * ----]. The leftmost 'const' is not included in this list, it is stored in
400 * dcs->d_qual instead.
401 */
402 typedef struct qual_ptr {
403 type_qualifiers qualifiers;
404 struct qual_ptr *p_next;
405 } qual_ptr;
406
407 /*
408 * The values of the 'case' labels, linked via cl_next in reverse order of
409 * appearance in the code, that is from bottom to top.
410 */
411 typedef struct case_label {
412 val_t cl_val;
413 struct case_label *cl_next;
414 } case_label_t;
415
416 typedef enum {
417 CS_DO_WHILE,
418 CS_FOR,
419 CS_FUNCTION_BODY,
420 CS_IF,
421 CS_SWITCH,
422 CS_WHILE
423 } control_statement_kind;
424
425 /*
426 * Used to keep information about nested control statements.
427 */
428 typedef struct control_statement {
429 control_statement_kind c_kind; /* to ensure proper nesting */
430 bool c_loop:1; /* 'continue' and 'break' are valid */
431 bool c_switch:1; /* 'case' and 'break' are valid */
432 bool c_break:1; /* the loop/switch has a reachable
433 * 'break' statement */
434 bool c_continue:1; /* the loop has a reachable 'continue'
435 * statement */
436 bool c_default:1; /* the switch has a 'default' label */
437 bool c_maybe_endless:1; /* the controlling expression is
438 * always true (as in 'for (;;)' or
439 * 'while (1)'), there may be break
440 * statements though */
441 bool c_always_then:1;
442 bool c_reached_end_of_then:1;
443 bool c_had_return_noval:1; /* had "return;" */
444 bool c_had_return_value:1; /* had "return expr;" */
445
446 type_t *c_switch_type; /* type of switch expression */
447 tnode_t *c_switch_expr;
448 case_label_t *c_case_labels; /* list of case values */
449
450 memory_pool c_for_expr3_mem; /* saved memory for end of loop
451 * expression in for() */
452 tnode_t *c_for_expr3; /* end of loop expr in for() */
453 pos_t c_for_expr3_pos; /* position of end of loop expr */
454 pos_t c_for_expr3_csrc_pos; /* same for csrc_pos */
455
456 struct control_statement *c_surrounding;
457 } control_statement;
458
459 typedef struct {
460 size_t lo; /* inclusive */
461 size_t hi; /* inclusive */
462 } range_t;
463
464 typedef enum {
465 LC_ARGSUSED,
466 LC_BITFIELDTYPE,
467 LC_CONSTCOND,
468 LC_FALLTHROUGH,
469 LC_LINTLIBRARY,
470 LC_LINTED,
471 LC_LONGLONG,
472 LC_NOTREACHED,
473 LC_PRINTFLIKE,
474 LC_PROTOLIB,
475 LC_SCANFLIKE,
476 LC_VARARGS,
477 } lint_comment;
478
479 #include "externs1.h"
480
481 #define lint_assert(cond) \
482 do { \
483 if (!(cond)) \
484 assert_failed(__FILE__, __LINE__, __func__, #cond); \
485 } while (false)
486
487 #ifdef DEBUG
488 # include "err-msgs.h"
489
490 /* ARGSUSED */
491 static inline void __printflike(1, 2)
492 check_printf(const char *fmt, ...)
493 {
494 }
495
496 # define wrap_check_printf_at(func, msgid, pos, args...) \
497 do { \
498 check_printf(__CONCAT(MSG_, msgid), ##args); \
499 (func)(msgid, pos, ##args); \
500 } while (false)
501
502 # define error_at(msgid, pos, args...) \
503 wrap_check_printf_at(error_at, msgid, pos, ##args)
504 # define warning_at(msgid, pos, args...) \
505 wrap_check_printf_at(warning_at, msgid, pos, ##args)
506 # define message_at(msgid, pos, args...) \
507 wrap_check_printf_at(message_at, msgid, pos, ##args)
508
509 # define wrap_check_printf(func, cond, msgid, args...) \
510 ({ \
511 if (/* CONSTCOND */cond) \
512 debug_step("%s:%d: %s %d '%s' in %s", \
513 __FILE__, __LINE__, #func, msgid, \
514 __CONCAT(MSG_, msgid), __func__); \
515 check_printf(__CONCAT(MSG_, msgid), ##args); \
516 (func)(msgid, ##args); \
517 /* LINTED 129 */ \
518 })
519
520 # define error(msgid, args...) wrap_check_printf(error, \
521 true, msgid, ##args)
522 # define warning(msgid, args...) wrap_check_printf(warning, \
523 true, msgid, ##args)
524 # define gnuism(msgid, args...) wrap_check_printf(gnuism, \
525 !allow_gcc || (!allow_trad && !allow_c99), msgid, ##args)
526 # define c99ism(msgid, args...) wrap_check_printf(c99ism, \
527 !allow_c99 && (!allow_gcc || !allow_trad), msgid, ##args)
528 # define c11ism(msgid, args...) wrap_check_printf(c11ism, \
529 !allow_c11 && !allow_gcc, msgid, ##args)
530 # define c23ism(msgid, args...) wrap_check_printf(c23ism, \
531 !allow_c23, msgid, ##args)
532 #endif
533
534 #ifdef DEBUG
535 # define query_message(query_id, args...) \
536 do { \
537 debug_step("%s:%d: query %d '%s' in %s", \
538 __FILE__, __LINE__, \
539 query_id, __CONCAT(MSG_Q, query_id), __func__); \
540 check_printf(__CONCAT(MSG_Q, query_id), ##args); \
541 (query_message)(query_id, ##args); \
542 } while (false)
543 #else
544 # define query_message(...) \
545 do { \
546 if (any_query_enabled) \
547 (query_message)(__VA_ARGS__); \
548 } while (false)
549 #endif
550
551 /* Copies curr_pos, keeping things unique. */
552 static inline pos_t
553 unique_curr_pos(void)
554 {
555 pos_t curr = curr_pos;
556 curr_pos.p_uniq++;
557 if (curr_pos.p_file == csrc_pos.p_file)
558 csrc_pos.p_uniq++;
559 return curr;
560 }
561
562 static inline bool
563 is_nonzero_val(const val_t *val)
564 {
565 return is_floating(val->v_tspec)
566 ? val->u.floating != 0.0
567 : val->u.integer != 0;
568 }
569
570 static inline bool
571 constant_is_nonzero(const tnode_t *tn)
572 {
573 lint_assert(tn->tn_op == CON);
574 lint_assert(tn->tn_type->t_tspec == tn->tn_val.v_tspec);
575 return is_nonzero_val(&tn->tn_val);
576 }
577
578 static inline bool
579 is_zero(const tnode_t *tn)
580 {
581 return tn != NULL && tn->tn_op == CON && !is_nonzero_val(&tn->tn_val);
582 }
583
584 static inline bool
585 is_nonzero(const tnode_t *tn)
586 {
587 return tn != NULL && tn->tn_op == CON && is_nonzero_val(&tn->tn_val);
588 }
589
590 static inline const char *
591 op_name(op_t op)
592 {
593 return modtab[op].m_name;
594 }
595
596 static inline bool
597 is_binary(const tnode_t *tn)
598 {
599 return modtab[tn->tn_op].m_binary;
600 }
601
602 static inline uint64_t
603 bit(unsigned i)
604 {
605 /*
606 * TODO: Add proper support for INT128.
607 * This involves changing val_t to 128 bits.
608 */
609 if (i >= 64)
610 return 0; /* XXX: not correct for INT128 and UINT128 */
611
612 lint_assert(i < 64);
613 return (uint64_t)1 << i;
614 }
615
616 static inline bool
617 msb(int64_t si, tspec_t t)
618 {
619 return ((uint64_t)si & bit(size_in_bits(t) - 1)) != 0;
620 }
621
622 static inline uint64_t
623 value_bits(unsigned bitsize)
624 {
625 lint_assert(bitsize > 0);
626
627 /* for long double (80 or 128), double _Complex (128) */
628 /*
629 * XXX: double _Complex does not have 128 bits of precision,
630 * therefore it should never be necessary to query the value bits
631 * of such a type; see d_c99_complex_split.c to trigger this case.
632 */
633 if (bitsize >= 64)
634 return ~((uint64_t)0);
635
636 return ~(~(uint64_t)0 << bitsize);
637 }
638
639 /* C99 6.7.8p7 */
640 static inline bool
641 is_struct_or_union(tspec_t t)
642 {
643 return t == STRUCT || t == UNION;
644 }
645
646 static inline bool
647 is_member(const sym_t *sym)
648 {
649 return sym->s_scl == STRUCT_MEMBER || sym->s_scl == UNION_MEMBER;
650 }
651
652 static inline void
653 set_symtyp(symt_t symt)
654 {
655 if (yflag)
656 debug_step("%s: %s -> %s", __func__,
657 symt_name(symtyp), symt_name(symt));
658 symtyp = symt;
659 }
660