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