lint1.h revision 1.167 1 /* $NetBSD: lint1.h,v 1.167 2023/06/24 20:50:54 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 /* Copies curr_pos, keeping things unique. */
70 #define UNIQUE_CURR_POS(pos) \
71 do { \
72 (pos) = curr_pos; \
73 curr_pos.p_uniq++; \
74 if (curr_pos.p_file == csrc_pos.p_file) \
75 csrc_pos.p_uniq++; \
76 } while (false)
77
78 /*
79 * Strings cannot be referenced simply by a pointer to their first
80 * char. This is because strings can contain NUL characters other than the
81 * trailing NUL.
82 *
83 * Strings are stored with a trailing NUL.
84 */
85 typedef struct strg {
86 bool st_char; /* string doesn't have an 'L' prefix */
87 size_t st_len; /* length without trailing NUL */
88 void *st_mem; /* char[] for st_char, or wchar_t[] */
89 } strg_t;
90
91 /*
92 * qualifiers (only for lex/yacc interface)
93 */
94 typedef enum {
95 CONST,
96 VOLATILE,
97 RESTRICT,
98 THREAD, /* XXX: storage-class-qualifier */
99 ATOMIC,
100 } tqual_t;
101
102 /* An integer or floating-point value. */
103 typedef struct {
104 tspec_t v_tspec;
105 /*
106 * Set if an integer constant is unsigned only in C90 and later, but
107 * not in traditional C.
108 *
109 * See the operators table in ops.def, columns "l r".
110 */
111 bool v_unsigned_since_c90;
112 bool v_char_constant;
113 union {
114 int64_t _v_quad; /* integers */
115 long double _v_ldbl; /* floats */
116 } v_u;
117 } val_t;
118
119 #define v_quad v_u._v_quad
120 #define v_ldbl v_u._v_ldbl
121
122 /*
123 * Structures of type struct_or_union uniquely identify structures. This can't
124 * be done in structures of type type_t, because these are copied
125 * if they must be modified. So it would not be possible to check
126 * if two structures are identical by comparing the pointers to
127 * the type structures.
128 *
129 * The typename is used if the structure is unnamed to identify
130 * the structure type in pass 2.
131 */
132 typedef struct {
133 unsigned int sou_size_in_bits;
134 unsigned short sou_align_in_bits;
135 bool sou_incomplete:1;
136 struct sym *sou_first_member;
137 struct sym *sou_tag;
138 struct sym *sou_first_typedef;
139 } struct_or_union;
140
141 /*
142 * same as above for enums
143 */
144 typedef struct {
145 bool en_incomplete:1;
146 struct sym *en_first_enumerator;
147 struct sym *en_tag;
148 struct sym *en_first_typedef;
149 } enumeration;
150
151 /*
152 * The type of an expression or object. Complex types are formed via t_subt
153 * (for arrays, pointers and functions), as well as t_sou.
154 */
155 struct lint1_type {
156 tspec_t t_tspec; /* type specifier */
157 bool t_incomplete_array:1;
158 bool t_const:1; /* const modifier */
159 bool t_volatile:1; /* volatile modifier */
160 bool t_proto:1; /* function prototype (t_args valid) */
161 bool t_vararg:1; /* prototype with '...' */
162 bool t_typedef:1; /* type defined with typedef */
163 bool t_typeof:1; /* type defined with GCC's __typeof__ */
164 bool t_bitfield:1;
165 /*
166 * Either the type is currently an enum (having t_tspec ENUM), or
167 * it is an integer type (typically INT) that has been implicitly
168 * converted from an enum type. In both cases, t_enum is valid.
169 *
170 * The information about a former enum type is retained to allow
171 * type checks in expressions such as ((var1 & 0x0001) == var2), to
172 * detect when var1 and var2 are from incompatible enum types.
173 */
174 bool t_is_enum:1;
175 bool t_packed:1;
176 union {
177 int _t_dim; /* dimension (if ARRAY) */
178 struct_or_union *_t_sou;
179 enumeration *_t_enum;
180 struct sym *_t_args; /* arguments (if t_proto) */
181 } t_u;
182 struct {
183 unsigned int _t_flen:8; /* length of bit-field */
184 unsigned int _t_foffs:24; /* offset of bit-field */
185 } t_b;
186 struct lint1_type *t_subt; /* element type (if ARRAY),
187 * return value (if FUNC),
188 * target type (if PTR) */
189 };
190
191 #define t_dim t_u._t_dim
192 #define t_sou t_u._t_sou
193 #define t_enum t_u._t_enum
194 #define t_args t_u._t_args
195 #define t_flen t_b._t_flen
196 #define t_foffs t_b._t_foffs
197
198 /*
199 * types of symbols
200 */
201 typedef enum {
202 FVFT, /* variables, functions, type names, enums */
203 FMEMBER, /* members of structs or unions */
204 FTAG, /* tags */
205 FLABEL /* labels */
206 } symt_t;
207
208 /*
209 * storage classes and related things
210 */
211 typedef enum {
212 NOSCL,
213 EXTERN, /* external symbols (independent of decl_t) */
214 STATIC, /* static symbols (local and global) */
215 AUTO, /* automatic symbols (except register) */
216 REG, /* register */
217 TYPEDEF, /* typedef */
218 STRUCT_TAG,
219 UNION_TAG,
220 ENUM_TAG,
221 STRUCT_MEMBER,
222 UNION_MEMBER,
223 BOOL_CONST,
224 ENUM_CONST,
225 ABSTRACT, /* abstract symbol (sizeof, casts, unnamed argument) */
226 INLINE /* only used by the parser */
227 } scl_t;
228
229 /*
230 * symbol table entry
231 */
232 typedef struct sym {
233 const char *s_name;
234 const char *s_rename; /* renamed symbol's given name */
235 pos_t s_def_pos; /* position of last (prototype) definition,
236 prototype declaration, no-prototype-def.,
237 tentative definition or declaration,
238 in this order */
239 pos_t s_set_pos; /* position of first initialization */
240 pos_t s_use_pos; /* position of first use */
241 symt_t s_kind; /* type of symbol */
242 const struct keyword *s_keyword;
243 bool s_bitfield:1;
244 bool s_set:1; /* variable set, label defined */
245 bool s_used:1; /* variable/label used */
246 bool s_arg:1; /* symbol is function argument */
247 bool s_register:1; /* symbol is register variable */
248 bool s_defarg:1; /* undefined symbol in old-style function
249 definition */
250 bool s_return_type_implicit_int:1;
251 bool s_osdef:1; /* symbol stems from old-style function def. */
252 bool s_inline:1; /* true if this is an inline function */
253 struct sym *s_ext_sym; /* for locally declared external symbols, the
254 * pointer to the external symbol with the
255 * same name */
256 def_t s_def; /* declared, tentative defined, defined */
257 scl_t s_scl; /* storage class */
258 int s_block_level; /* level of declaration, -1 if not in symbol
259 table */
260 type_t *s_type;
261 union {
262 bool s_bool_constant;
263 int s_enum_constant; /* XXX: should be TARG_INT */
264 struct {
265 /* XXX: what is the difference to s_type->t_sou? */
266 struct_or_union *sm_sou_type;
267 unsigned int sm_offset_in_bits;
268 } s_member;
269 struct {
270 int sk_token;
271 tspec_t sk_tspec; /* only for types */
272 tqual_t sk_qualifier; /* only for qualifiers */
273 } s_keyword;
274 struct sym *s_old_style_args; /* arguments in an old-style
275 * function definition */
276 } u;
277 struct sym *s_symtab_next; /* next symbol with same hash value */
278 struct sym **s_symtab_ref; /* pointer to s_symtab_next of the
279 * previous symbol */
280 struct sym *s_next; /* next struct/union member, enumerator,
281 argument */
282 struct sym *s_level_next; /* next symbol declared on the same
283 * level */
284 } sym_t;
285
286 /*
287 * Used to keep some information about symbols before they are entered
288 * into the symbol table.
289 */
290 typedef struct sbuf {
291 const char *sb_name; /* name of symbol */
292 size_t sb_len; /* length (without '\0') */
293 sym_t *sb_sym; /* symbol table entry */
294 } sbuf_t;
295
296
297 /*
298 * tree node
299 */
300 typedef struct tnode {
301 op_t tn_op; /* operator */
302 type_t *tn_type; /* type */
303 bool tn_lvalue:1; /* node is lvalue */
304 bool tn_cast:1; /* if tn_op == CVT, it's an explicit cast */
305 bool tn_parenthesized:1;
306 bool tn_sys:1; /* in strict bool mode, allow mixture between
307 * bool and scalar, for code from system
308 * headers that may be a mixture between
309 * scalar types and bool
310 */
311 bool tn_system_dependent:1; /* depends on sizeof or offsetof */
312 union {
313 struct {
314 struct tnode *_tn_left; /* (left) operand */
315 struct tnode *_tn_right; /* right operand */
316 } tn_s;
317 sym_t *_tn_sym; /* symbol if op == NAME */
318 val_t _tn_val; /* value if op == CON */
319 strg_t *_tn_string; /* string if op == STRING */
320 } tn_u;
321 } tnode_t;
322
323 #define tn_left tn_u.tn_s._tn_left
324 #define tn_right tn_u.tn_s._tn_right
325 #define tn_sym tn_u._tn_sym
326 #define tn_val tn_u._tn_val
327 #define tn_string tn_u._tn_string
328
329 struct generic_association {
330 type_t *ga_arg; /* NULL means default or error */
331 tnode_t *ga_result; /* NULL means error */
332 struct generic_association *ga_prev;
333 };
334
335 struct array_size {
336 bool has_dim;
337 int dim;
338 };
339
340 typedef enum declaration_kind {
341 DK_EXTERN, /* global variable or function */
342 DK_STRUCT_MEMBER,
343 DK_UNION_MEMBER,
344 DK_ENUM_CONSTANT,
345 DK_OLD_STYLE_ARG, /* argument in an old-style function
346 * definition */
347 DK_PROTO_ARG, /* argument in a prototype function
348 * definition */
349 DK_AUTO, /* local symbol */
350 DK_ABSTRACT /* abstract declaration; type name */
351 } declaration_kind;
352
353 /*
354 * For nested declarations there is a stack that holds all information
355 * needed for the current level. dcs points to the innermost element of this
356 * stack.
357 */
358 typedef struct dinfo {
359 declaration_kind d_kind;
360 tspec_t d_abstract_type;/* VOID, BOOL, CHAR, INT or COMPLEX */
361 tspec_t d_complex_mod; /* FLOAT or DOUBLE */
362 tspec_t d_sign_mod; /* SIGNED or UNSIGN */
363 tspec_t d_rank_mod; /* SHORT, LONG or QUAD */
364 scl_t d_scl; /* storage class */
365 type_t *d_type; /* after dcs_end_type pointer to the type used
366 for all declarators */
367 sym_t *d_redeclared_symbol;
368 unsigned int d_offset_in_bits; /* offset of next structure member */
369 unsigned short d_sou_align_in_bits; /* alignment required for current
370 * structure */
371 bool d_const:1; /* const in declaration specifiers */
372 bool d_volatile:1; /* volatile in declaration specifiers */
373 bool d_inline:1; /* inline in declaration specifiers */
374 bool d_multiple_storage_classes:1; /* reported in dcs_end_type */
375 bool d_invalid_type_combination:1;
376 bool d_nonempty_decl:1; /* if at least one tag is declared
377 * ... in the current function decl. */
378 bool d_vararg:1;
379 bool d_proto:1; /* current function decl. is a prototype */
380 bool d_notyp:1; /* set if no type specifier was present */
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_tagtyp; /* tag during member declaration */
385 sym_t *d_func_args; /* list of arguments during function def. */
386 pos_t d_func_def_pos; /* position of function definition */
387 sym_t *d_dlsyms; /* first symbol declared at this level */
388 sym_t **d_ldlsym; /* points to s_level_next in the last symbol
389 declaration at this level */
390 sym_t *d_func_proto_syms; /* symbols defined in prototype */
391 struct dinfo *d_enclosing; /* the enclosing declaration level */
392 } dinfo_t;
393
394 /* One level of pointer indirection in declarators, including qualifiers. */
395 typedef struct qual_ptr {
396 bool p_const: 1;
397 bool p_volatile: 1;
398 bool p_pointer: 1;
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 #include "externs1.h"
460
461 #define lint_assert(cond) \
462 do { \
463 if (!(cond)) \
464 assert_failed(__FILE__, __LINE__, __func__, #cond); \
465 } while (false)
466
467 #ifdef DEBUG
468 # include "err-msgs.h"
469
470 /* ARGSUSED */
471 static inline void __printflike(1, 2)
472 check_printf(const char *fmt, ...)
473 {
474 }
475
476 # define wrap_check_printf_at(func, msgid, pos, args...) \
477 do { \
478 check_printf(__CONCAT(MSG_, msgid), ##args); \
479 (func)(msgid, pos, ##args); \
480 } while (false)
481
482 # define error_at(msgid, pos, args...) \
483 wrap_check_printf_at(error_at, msgid, pos, ##args)
484 # define warning_at(msgid, pos, args...) \
485 wrap_check_printf_at(warning_at, msgid, pos, ##args)
486 # define message_at(msgid, pos, args...) \
487 wrap_check_printf_at(message_at, msgid, pos, ##args)
488
489 # define wrap_check_printf(func, msgid, args...) \
490 ({ \
491 debug_step("%s:%d: %s", __FILE__, __LINE__, __func__); \
492 check_printf(__CONCAT(MSG_, msgid), ##args); \
493 (func)(msgid, ##args); \
494 /* LINTED 129 */ \
495 })
496
497 # define error(msgid, args...) wrap_check_printf(error, msgid, ##args)
498 # define warning(msgid, args...) wrap_check_printf(warning, msgid, ##args)
499 # define gnuism(msgid, args...) wrap_check_printf(gnuism, msgid, ##args)
500 # define c99ism(msgid, args...) wrap_check_printf(c99ism, msgid, ##args)
501 # define c11ism(msgid, args...) wrap_check_printf(c11ism, msgid, ##args)
502 #endif
503
504 #ifdef DEBUG
505 # define query_message(query_id, args...) \
506 do { \
507 debug_step("%s:%d: %s", __FILE__, __LINE__, __func__); \
508 check_printf(__CONCAT(MSG_Q, query_id), ##args); \
509 (query_message)(query_id, ##args); \
510 } while (false)
511 #else
512 # define query_message(...) \
513 do { \
514 if (any_query_enabled) \
515 (query_message)(__VA_ARGS__); \
516 } while (false)
517 #endif
518
519 static inline bool
520 is_nonzero_val(const val_t *val)
521 {
522 return is_floating(val->v_tspec)
523 ? val->v_ldbl != 0.0
524 : val->v_quad != 0;
525 }
526
527 static inline bool
528 constant_is_nonzero(const tnode_t *tn)
529 {
530 lint_assert(tn->tn_op == CON);
531 lint_assert(tn->tn_type->t_tspec == tn->tn_val.v_tspec);
532 return is_nonzero_val(&tn->tn_val);
533 }
534
535 static inline bool
536 is_zero(const tnode_t *tn)
537 {
538 return tn != NULL && tn->tn_op == CON && !is_nonzero_val(&tn->tn_val);
539 }
540
541 static inline bool
542 is_nonzero(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_binary(const tnode_t *tn)
549 {
550 return modtab[tn->tn_op].m_binary;
551 }
552
553 static inline uint64_t
554 bit(unsigned i)
555 {
556 /*
557 * TODO: Add proper support for INT128.
558 * This involves changing val_t to 128 bits.
559 */
560 if (i >= 64)
561 return 0; /* XXX: not correct for INT128 and UINT128 */
562
563 lint_assert(i < 64);
564 return (uint64_t)1 << i;
565 }
566
567 static inline bool
568 msb(int64_t q, tspec_t t)
569 {
570 return (q & bit((unsigned int)size_in_bits(t) - 1)) != 0;
571 }
572
573 static inline uint64_t
574 value_bits(unsigned bitsize)
575 {
576 lint_assert(bitsize > 0);
577
578 /* for long double (80 or 128), double _Complex (128) */
579 /*
580 * XXX: double _Complex does not have 128 bits of precision,
581 * therefore it should never be necessary to query the value bits
582 * of such a type; see d_c99_complex_split.c to trigger this case.
583 */
584 if (bitsize >= 64)
585 return ~((uint64_t)0);
586
587 return ~(~(uint64_t)0 << bitsize);
588 }
589
590 /* C99 6.7.8p7 */
591 static inline bool
592 is_struct_or_union(tspec_t t)
593 {
594 return t == STRUCT || t == UNION;
595 }
596
597 static inline bool
598 is_member(const sym_t *sym)
599 {
600 return sym->s_scl == STRUCT_MEMBER || sym->s_scl == UNION_MEMBER;
601 }
602