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