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