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