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