lint1.h revision 1.224 1 /* $NetBSD: lint1.h,v 1.224 2024/05/03 04:04:18 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 "op.h"
37
38 /*
39 * A memory pool collects allocated objects that must be available until:
40 * - the end of a block,
41 * - the end of an expression, or
42 * - the end of the translation unit.
43 */
44 typedef struct memory_pool {
45 struct memory_pool_item {
46 void *p;
47 #ifdef DEBUG_MEM
48 size_t size;
49 const char *descr;
50 #endif
51 } *items;
52 size_t len;
53 size_t cap;
54 } memory_pool;
55
56 /* See saved_lwarn in cgram.y. */
57 #define LWARN_ALL (-2)
58 #define LWARN_NONE (-1)
59
60 /*
61 * Describes the position of a declaration or anything else.
62 *
63 * FIXME: Just a single file:lineno pair is not enough to accurately describe
64 * the position of a symbol. The whole inclusion path at that point must be
65 * stored as well. This makes a difference for symbols from included
66 * headers, see print_stack_trace.
67 */
68 typedef struct {
69 const char *p_file;
70 int p_line;
71 int p_uniq; /* uniquifier */
72 } pos_t;
73
74 typedef struct {
75 bool tq_const;
76 bool tq_restrict;
77 bool tq_volatile;
78 bool tq_atomic;
79 } type_qualifiers;
80
81 /* A bool, integer or floating-point value. */
82 typedef struct {
83 tspec_t v_tspec;
84 /*
85 * Set if an integer constant is unsigned only in C90 and later, but
86 * not in traditional C.
87 *
88 * See the operators table in oper.c, columns "l r".
89 */
90 bool v_unsigned_since_c90;
91 bool v_char_constant;
92 union {
93 int64_t integer;
94 long double floating;
95 } u;
96 } val_t;
97
98 typedef struct sym sym_t;
99
100 /*
101 * Structures of type struct_or_union uniquely identify structures. This can't
102 * be done in structures of type type_t, because these are copied if they must
103 * be modified. So it would not be possible to check if two structures are
104 * identical by comparing the pointers to the type structures.
105 *
106 * If the structure has no tag name, its first typedef name is used to identify
107 * the structure in lint2.
108 */
109 typedef struct {
110 unsigned int sou_size_in_bits;
111 unsigned int sou_align;
112 bool sou_incomplete:1;
113 sym_t *sou_first_member;
114 sym_t *sou_tag;
115 sym_t *sou_first_typedef;
116 } struct_or_union;
117
118 /* The same as in struct_or_union, only for enums. */
119 typedef struct {
120 bool en_incomplete:1;
121 sym_t *en_first_enumerator;
122 sym_t *en_tag;
123 sym_t *en_first_typedef;
124 } enumeration;
125
126 /* The type of an expression, object or function. */
127 struct lint1_type {
128 tspec_t t_tspec; /* type specifier */
129 bool t_incomplete_array:1;
130 bool t_const:1;
131 bool t_volatile:1;
132 bool t_proto:1; /* function prototype (u.params valid) */
133 bool t_vararg:1; /* prototype with '...' */
134 bool t_typedef:1; /* type defined with typedef */
135 bool t_typeof:1; /* type defined with GCC's __typeof__ */
136 bool t_bitfield:1;
137 /*
138 * Either the type is currently an enum (having t_tspec ENUM), or it
139 * is an integer type (typically INT) that has been implicitly
140 * converted from an enum type. In both cases, u.enumer is valid.
141 */
142 bool t_is_enum:1;
143 bool t_packed:1;
144 union {
145 int dimension; /* if ARRAY */
146 struct_or_union *sou;
147 enumeration *enumer;
148 sym_t *params; /* if t_proto */
149 } u;
150 unsigned int t_bit_field_width:8;
151 unsigned int t_bit_field_offset:24;
152 struct lint1_type *t_subt; /*- element type (if ARRAY),
153 * return value (if FUNC),
154 * target type (if PTR) */
155 };
156
157 typedef enum {
158 SK_VCFT, /* variable, constant, function, type */
159 SK_MEMBER, /* member of a struct or union */
160 SK_TAG,
161 SK_LABEL
162 } symbol_kind;
163
164 /* storage classes and related things */
165 typedef enum {
166 NO_SCL,
167 EXTERN, /* external symbols (independent of decl_t) */
168 STATIC, /* static symbols (local and global) */
169 AUTO, /* automatic symbols (except register) */
170 REG, /* register */
171 TYPEDEF,
172 THREAD_LOCAL,
173 STRUCT_TAG,
174 UNION_TAG,
175 ENUM_TAG,
176 STRUCT_MEMBER,
177 UNION_MEMBER,
178 BOOL_CONST,
179 ENUM_CONST,
180 ABSTRACT, /* abstract symbol (sizeof, casts, unnamed
181 * argument) */
182 } scl_t;
183
184 /* C23 6.7.4 */
185 typedef enum {
186 FS_INLINE, /* since C99 */
187 FS_NORETURN, /* since C11 */
188 } function_specifier;
189
190 /* A type, variable, keyword; basically anything that has a name. */
191 struct sym {
192 const char *s_name;
193 const char *s_rename; /* renamed symbol's given name */
194 pos_t s_def_pos; /* position of last (prototype) definition,
195 * prototype declaration, no-prototype-def.,
196 * tentative definition or declaration, in this
197 * order */
198 pos_t s_set_pos; /* position of first initialization */
199 pos_t s_use_pos; /* position of first use */
200 symbol_kind s_kind;
201 const struct keyword *s_keyword;
202 bool s_bitfield:1;
203 bool s_set:1;
204 bool s_used:1;
205 bool s_param:1;
206 bool s_register:1;
207 bool s_defparam:1; /* undefined symbol in old-style function
208 * definition */
209 bool s_return_type_implicit_int:1;
210 bool s_osdef:1; /* symbol stems from old-style function def. */
211 bool s_inline:1;
212 sym_t *s_ext_sym; /* for locally declared external symbols, the
213 * pointer to the external symbol with the same
214 * name */
215 def_t s_def; /* declared, tentative defined, defined */
216 scl_t s_scl;
217 int s_block_level; /* level of declaration, -1 if not in symbol
218 * table */
219 type_t *s_type;
220 union {
221 bool s_bool_constant;
222 int s_enum_constant;
223 struct {
224 struct_or_union *sm_containing_type;
225 unsigned int sm_offset_in_bits;
226 } s_member;
227 struct {
228 int sk_token;
229 union {
230 /* if T_TYPE or T_STRUCT_OR_UNION */
231 tspec_t sk_tspec;
232 /* if T_QUAL */
233 type_qualifiers sk_type_qualifier;
234 /* if T_FUNCTION_SPECIFIER */
235 function_specifier function_specifier;
236 } u;
237 } s_keyword;
238 sym_t *s_old_style_params; /* parameters in an old-style
239 * function definition */
240 } u;
241 sym_t *s_symtab_next; /* next symbol in the same symtab bucket */
242 sym_t **s_symtab_ref; /* pointer to s_symtab_next of the previous
243 * symbol */
244 sym_t *s_next; /* next struct/union member, enumerator,
245 * parameter */
246 sym_t *s_level_next; /* next symbol declared on the same level */
247 };
248
249 /*
250 * Used to keep some information about symbols before they are entered
251 * into the symbol table.
252 */
253 typedef struct {
254 const char *sb_name; /* name of symbol */
255 size_t sb_len; /* length (without '\0') */
256 sym_t *sb_sym; /* symbol table entry */
257 } sbuf_t;
258
259
260 typedef struct {
261 struct tnode *func;
262 struct tnode **args;
263 size_t args_len;
264 size_t args_cap;
265 } function_call;
266
267 typedef struct tnode tnode_t;
268
269 /* An expression, forming an abstract syntax tree. */
270 struct tnode {
271 op_t tn_op;
272 type_t *tn_type;
273 bool tn_lvalue:1;
274 bool tn_cast:1; /* if tn_op == CVT, it's an explicit cast */
275 bool tn_parenthesized:1;
276 bool tn_sys:1; /* comes from a system header */
277 bool tn_system_dependent:1; /* depends on sizeof or offsetof */
278 union {
279 struct {
280 tnode_t *left; /* (left) operand */
281 tnode_t *right; /* right operand */
282 } ops;
283 sym_t *sym; /* if NAME */
284 val_t value; /* if CON */
285 buffer *str_literals; /* if STRING; for
286 * character strings, 'data' points to
287 * the concatenated string literals in
288 * source form, and 'len' is the
289 * length of the concatenation; for
290 * wide strings, 'data' is NULL and
291 * 'len' is the number of resulting
292 * characters */
293 function_call *call; /* if CALL */
294 } u;
295 };
296
297 struct generic_association {
298 type_t *ga_arg; /* NULL means default or error */
299 tnode_t *ga_result; /* NULL means error */
300 struct generic_association *ga_prev;
301 };
302
303 typedef struct {
304 bool has_dim;
305 int dim;
306 } array_size;
307
308 typedef enum decl_level_kind {
309 DLK_EXTERN, /* global types, variables or functions */
310 DLK_STRUCT, /* struct members */
311 DLK_UNION, /* union members */
312 DLK_ENUM, /* enum constants */
313 DLK_OLD_STYLE_PARAMS, /* parameters in an old-style function
314 * definition */
315 DLK_PROTO_PARAMS, /* parameters in a prototype function
316 * definition */
317 DLK_AUTO, /* local types or variables */
318 DLK_ABSTRACT /* abstract (unnamed) declaration; type name;
319 * used in casts and sizeof */
320 } decl_level_kind;
321
322 /*
323 * A declaration level collects information for a declarator in a struct,
324 * union or enum declaration, a parameter declaration list, or a plain
325 * declaration in or outside a function body.
326 *
327 * For nested declarations, the global 'dcs' holds all information needed for
328 * the current level, the outer levels are available via 'd_enclosing'.
329 */
330 typedef struct decl_level {
331 decl_level_kind d_kind;
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 LLONG */
336 scl_t d_scl; /* storage class */
337 type_t *d_type; /* after dcs_end_type, the pointer to the type
338 * used for all declarators */
339 sym_t *d_redeclared_symbol;
340 unsigned int d_sou_size_in_bits; /* size of the structure or
341 * union being built, without
342 * trailing padding */
343 unsigned int d_sou_align; /* alignment of the structure
344 * or union being built */
345 unsigned int d_mem_align; /* alignment of the structure
346 * or union member */
347 type_qualifiers d_qual; /* in declaration specifiers */
348 bool d_inline:1; /* inline in declaration specifiers */
349 bool d_multiple_storage_classes:1; /* reported in dcs_end_type */
350 bool d_invalid_type_combination:1;
351 bool d_nonempty_decl:1; /* in a function declaration, whether at
352 * least one tag was declared */
353 bool d_no_type_specifier:1;
354 bool d_asm:1; /* set if d_ctx == AUTO and asm() present */
355 bool d_packed:1;
356 bool d_used:1;
357 type_t *d_tag_type; /* during a member or enumerator declaration,
358 * the tag type to which the member belongs */
359 sym_t *d_func_params; /* during a function declaration, the
360 * parameters, stored in the enclosing level */
361 pos_t d_func_def_pos; /* position of the function definition */
362 sym_t *d_first_dlsym; /* first symbol declared at this level */
363 sym_t **d_last_dlsym; /* points to s_level_next in the last symbol
364 declaration at this level */
365 sym_t *d_func_proto_syms; /* symbols defined in prototype, such
366 * as tagged types or parameter names,
367 * may overlap d_func_params */
368 struct decl_level *d_enclosing; /* the enclosing declaration level */
369 } decl_level;
370
371 typedef struct {
372 sym_t *first;
373 bool vararg:1;
374 bool prototype:1;
375 } parameter_list;
376
377 /*
378 * A sequence of asterisks and qualifiers, from right to left. For example,
379 * 'const ***volatile **const volatile' results in [c-v-, ----, --v-, ----,
380 * ----]. The leftmost 'const' is not included in this list, it is stored in
381 * dcs->d_qual instead.
382 */
383 typedef struct qual_ptr {
384 type_qualifiers qualifiers;
385 struct qual_ptr *p_next;
386 } qual_ptr;
387
388 /* The values of the 'case' labels. */
389 typedef struct {
390 val_t *vals;
391 size_t len;
392 size_t cap;
393 } case_labels;
394
395 typedef enum {
396 CS_DO_WHILE,
397 CS_FOR,
398 CS_FUNCTION_BODY,
399 CS_IF,
400 CS_SWITCH,
401 CS_WHILE
402 } control_statement_kind;
403
404 /*
405 * Used to keep information about nested control statements.
406 */
407 typedef struct control_statement {
408 control_statement_kind c_kind; /* to ensure proper nesting */
409 bool c_loop:1; /* 'continue' and 'break' are valid */
410 bool c_switch:1; /* 'case' and 'break' are valid */
411 bool c_break:1; /* the loop/switch has a reachable 'break'
412 * statement */
413 bool c_continue:1; /* the loop has a reachable 'continue'
414 * statement */
415 bool c_default:1; /* the switch has a 'default' label */
416 bool c_maybe_endless:1; /* the controlling expression is
417 * always true (as in 'for (;;)' or
418 * 'while (1)'), there may be break
419 * statements though */
420 bool c_always_then:1;
421 bool c_reached_end_of_then:1;
422 bool c_had_return_noval:1; /* had "return;" */
423 bool c_had_return_value:1; /* had "return expr;" */
424
425 type_t *c_switch_type; /* type of switch expression */
426 tnode_t *c_switch_expr;
427 case_labels c_case_labels; /* list of case values */
428
429 memory_pool c_for_expr3_mem; /* saved memory for end of loop
430 * expression in for() */
431 tnode_t *c_for_expr3; /* end of loop expr in for() */
432 pos_t c_for_expr3_pos; /* position of end of loop expr */
433 pos_t c_for_expr3_csrc_pos; /* same for csrc_pos */
434
435 struct control_statement *c_surrounding;
436 } control_statement;
437
438 typedef struct {
439 size_t lo; /* inclusive */
440 size_t hi; /* inclusive */
441 } range_t;
442
443 typedef enum designator_kind {
444 DK_MEMBER, /* .member */
445 DK_SUBSCRIPT, /* [subscript] */
446 DK_SCALAR /* no textual representation, not generated by
447 * the parser; used for scalar initializer
448 * expressions surrounded by braces */
449 } designator_kind;
450
451 /*
452 * A single component on the path from the "current object" of a brace level
453 * to the sub-object that is initialized by an expression.
454 *
455 * C99 6.7.8p6, 6.7.8p7
456 */
457 typedef struct designator {
458 designator_kind dr_kind;
459 const sym_t *dr_member; /* for DK_MEMBER */
460 size_t dr_subscript; /* for DK_SUBSCRIPT */
461 bool dr_done;
462 } designator;
463
464 /*
465 * The path from the "current object" of a brace level to the sub-object that
466 * is initialized by an expression. Examples of designations are '.member'
467 * or '.member[123].member.member[1][1]'.
468 *
469 * C99 6.7.8p6, 6.7.8p7
470 */
471 typedef struct designation {
472 designator *dn_items;
473 size_t dn_len;
474 size_t dn_cap;
475 } designation;
476
477 typedef enum {
478 LC_ARGSUSED,
479 LC_BITFIELDTYPE,
480 LC_CONSTCOND,
481 LC_FALLTHROUGH,
482 LC_LINTLIBRARY,
483 LC_LINTED,
484 LC_LONGLONG,
485 LC_NOTREACHED,
486 LC_PRINTFLIKE,
487 LC_PROTOLIB,
488 LC_SCANFLIKE,
489 LC_VARARGS,
490 } lint_comment;
491
492 typedef struct {
493 size_t start;
494 size_t end;
495 uint64_t value;
496 bool escaped; /* \n, \003, \x24 */
497 bool named_escape; /* \a, \n, etc. */
498 bool literal_escape; /* \?, \\, etc. */
499 uint8_t octal_digits; /* 1 to 3; 0 means not applicable */
500 uint8_t hex_digits; /* 1 to 3; 0 means not applicable */
501 bool next_literal; /* when a new string literal begins */
502 bool invalid_escape; /* single-character escape, recoverable */
503 bool overflow; /* for octal and hex escapes */
504 bool missing_hex_digits;
505 bool unescaped_newline; /* stops iterating */
506 } quoted_iterator;
507
508 #include "externs1.h"
509
510 #define lint_assert(cond) \
511 do { \
512 if (!(cond)) \
513 assert_failed(__FILE__, __LINE__, __func__, #cond); \
514 } while (false)
515
516 #ifdef DEBUG
517 # include "err-msgs.h"
518
519 /* ARGSUSED */
520 static inline void __printflike(1, 2)
521 check_printf(const char *fmt, ...)
522 {
523 }
524
525 # define wrap_check_printf_at(func, msgid, pos, args...) \
526 do { \
527 check_printf(__CONCAT(MSG_, msgid), ##args); \
528 (func)(msgid, pos, ##args); \
529 } while (false)
530
531 # define error_at(msgid, pos, args...) \
532 wrap_check_printf_at(error_at, msgid, pos, ##args)
533 # define warning_at(msgid, pos, args...) \
534 wrap_check_printf_at(warning_at, msgid, pos, ##args)
535 # define message_at(msgid, pos, args...) \
536 wrap_check_printf_at(message_at, msgid, pos, ##args)
537
538 # define wrap_check_printf(func, cond, msgid, args...) \
539 ({ \
540 if (/* CONSTCOND */cond) \
541 debug_step("%s:%d: %s %d '%s' in %s", \
542 __FILE__, __LINE__, #func, msgid, \
543 __CONCAT(MSG_, msgid), __func__); \
544 check_printf(__CONCAT(MSG_, msgid), ##args); \
545 (func)(msgid, ##args); \
546 /* LINTED 129 */ \
547 })
548
549 # define error(msgid, args...) wrap_check_printf(error, \
550 true, msgid, ##args)
551 # define warning(msgid, args...) wrap_check_printf(warning, \
552 true, msgid, ##args)
553 # define gnuism(msgid, args...) wrap_check_printf(gnuism, \
554 !allow_gcc || (!allow_trad && !allow_c99), msgid, ##args)
555 # define c99ism(msgid, args...) wrap_check_printf(c99ism, \
556 !allow_c99 && (!allow_gcc || !allow_trad), msgid, ##args)
557 # define c11ism(msgid, args...) wrap_check_printf(c11ism, \
558 !allow_c11 && !allow_gcc, msgid, ##args)
559 # define c23ism(msgid, args...) wrap_check_printf(c23ism, \
560 !allow_c23, msgid, ##args)
561 #endif
562
563 #ifdef DEBUG
564 # define query_message(query_id, args...) \
565 do { \
566 debug_step("%s:%d: query %d '%s' in %s", \
567 __FILE__, __LINE__, \
568 query_id, __CONCAT(MSG_Q, query_id), __func__); \
569 check_printf(__CONCAT(MSG_Q, query_id), ##args); \
570 (query_message)(query_id, ##args); \
571 } while (false)
572 #else
573 # define query_message(...) \
574 do { \
575 if (any_query_enabled) \
576 (query_message)(__VA_ARGS__); \
577 } while (false)
578 #endif
579
580 /* Copies curr_pos, keeping things unique. */
581 static inline pos_t
582 unique_curr_pos(void)
583 {
584 pos_t curr = curr_pos;
585 curr_pos.p_uniq++;
586 if (curr_pos.p_file == csrc_pos.p_file)
587 csrc_pos.p_uniq++;
588 return curr;
589 }
590
591 static inline bool
592 is_nonzero_val(const val_t *val)
593 {
594 return is_floating(val->v_tspec)
595 ? val->u.floating != 0.0
596 : val->u.integer != 0;
597 }
598
599 static inline bool
600 constant_is_nonzero(const tnode_t *tn)
601 {
602 lint_assert(tn->tn_op == CON);
603 lint_assert(tn->tn_type->t_tspec == tn->u.value.v_tspec);
604 return is_nonzero_val(&tn->u.value);
605 }
606
607 static inline bool
608 is_zero(const tnode_t *tn)
609 {
610 return tn != NULL && tn->tn_op == CON && !is_nonzero_val(&tn->u.value);
611 }
612
613 static inline bool
614 is_nonzero(const tnode_t *tn)
615 {
616 return tn != NULL && tn->tn_op == CON && is_nonzero_val(&tn->u.value);
617 }
618
619 static inline const char *
620 op_name(op_t op)
621 {
622 return modtab[op].m_name;
623 }
624
625 static inline bool
626 is_binary(const tnode_t *tn)
627 {
628 return modtab[tn->tn_op].m_binary;
629 }
630
631 static inline uint64_t
632 bit(unsigned i)
633 {
634 /*
635 * TODO: Add proper support for INT128. This involves changing val_t to
636 * 128 bits.
637 */
638 if (i >= 64)
639 return 0; /* XXX: not correct for INT128 and UINT128 */
640
641 lint_assert(i < 64);
642 return (uint64_t)1 << i;
643 }
644
645 static inline bool
646 msb(int64_t si, tspec_t t)
647 {
648 return ((uint64_t)si & bit(size_in_bits(t) - 1)) != 0;
649 }
650
651 static inline uint64_t
652 value_bits(unsigned bitsize)
653 {
654 lint_assert(bitsize > 0);
655
656 /* for long double (80 or 128), double _Complex (128) */
657 /*
658 * XXX: double _Complex does not have 128 bits of precision, therefore
659 * it should never be necessary to query the value bits of such a type;
660 * see d_c99_complex_split.c to trigger this case.
661 */
662 if (bitsize >= 64)
663 return ~(uint64_t)0;
664
665 return ~(~(uint64_t)0 << bitsize);
666 }
667
668 /* C99 6.7.8p7 */
669 static inline bool
670 is_struct_or_union(tspec_t t)
671 {
672 return t == STRUCT || t == UNION;
673 }
674
675 static inline bool
676 is_member(const sym_t *sym)
677 {
678 return sym->s_scl == STRUCT_MEMBER || sym->s_scl == UNION_MEMBER;
679 }
680
681 static inline void
682 set_sym_kind(symbol_kind kind)
683 {
684 if (yflag)
685 debug_step("%s: %s -> %s", __func__,
686 symbol_kind_name(sym_kind), symbol_kind_name(kind));
687 sym_kind = kind;
688 }
689