lint1.h revision 1.143 1 /* $NetBSD: lint1.h,v 1.143 2022/04/02 14:28: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 * XXX - Super conservative so that works for most systems, but we should
41 * not depend on the host settings but the target settings in determining
42 * the alignment. The only valid use for this is in mem1.c; uses in decl.c
43 * are bogus.
44 */
45 #ifndef WORST_ALIGN
46 #ifdef _LP64
47 # define AVAL 15
48 #else
49 # define AVAL 7
50 #endif
51 #define WORST_ALIGN(x) (((x) + AVAL) & ~AVAL)
52 #endif
53
54 #define LWARN_BAD (-3)
55 #define LWARN_ALL (-2)
56 #define LWARN_NONE (-1)
57
58 /*
59 * Describes the position of a declaration or anything else.
60 *
61 * FIXME: Just a single file:lineno pair is not enough to accurately describe
62 * the position of a symbol. The whole inclusion path at that point must be
63 * stored as well. This makes a difference for symbols from included
64 * headers, see print_stack_trace.
65 */
66 typedef struct {
67 const char *p_file;
68 int p_line;
69 int p_uniq; /* uniquifier */
70 } pos_t;
71
72 /* Copies curr_pos, keeping things unique. */
73 #define UNIQUE_CURR_POS(pos) \
74 do { \
75 (pos) = curr_pos; \
76 curr_pos.p_uniq++; \
77 if (curr_pos.p_file == csrc_pos.p_file) \
78 csrc_pos.p_uniq++; \
79 } while (false)
80
81 /*
82 * Strings cannot be referenced simply by a pointer to their first
83 * char. This is because strings can contain NUL characters other than the
84 * trailing NUL.
85 *
86 * Strings are stored with a trailing NUL.
87 */
88 typedef struct strg {
89 bool st_char; /* string doesn't have an 'L' prefix */
90 size_t st_len; /* length without trailing NUL */
91 void *st_mem; /* char[] for st_char, or wchar_t[] */
92 } strg_t;
93
94 /*
95 * qualifiers (only for lex/yacc interface)
96 */
97 typedef enum {
98 CONST, VOLATILE, RESTRICT, THREAD
99 } tqual_t;
100
101 /* An integer or floating-point value. */
102 typedef struct {
103 tspec_t v_tspec;
104 /*
105 * Set if an integer constant is unsigned only in C90 and later, but
106 * not in traditional C.
107 *
108 * See the operators table in ops.def, columns "l r".
109 */
110 bool v_unsigned_since_c90;
111 union {
112 int64_t _v_quad; /* integers */
113 ldbl_t _v_ldbl; /* floats */
114 } v_u;
115 } val_t;
116
117 #define v_quad v_u._v_quad
118 #define v_ldbl v_u._v_ldbl
119
120 /*
121 * Structures of type struct_or_union uniquely identify structures. This can't
122 * be done in structures of type type_t, because these are copied
123 * if they must be modified. So it would not be possible to check
124 * if two structures are identical by comparing the pointers to
125 * the type structures.
126 *
127 * The typename is used if the structure is unnamed to identify
128 * the structure type in pass 2.
129 */
130 typedef struct {
131 unsigned int sou_size_in_bits;
132 unsigned short sou_align_in_bits;
133 bool sou_incomplete:1;
134 struct sym *sou_first_member;
135 struct sym *sou_tag;
136 struct sym *sou_first_typedef;
137 } struct_or_union;
138
139 /*
140 * same as above for enums
141 */
142 typedef struct {
143 bool en_incomplete:1;
144 struct sym *en_first_enumerator;
145 struct sym *en_tag;
146 struct sym *en_first_typedef;
147 } enumeration;
148
149 /*
150 * The type of an expression or object. Complex types are formed via t_subt
151 * (for arrays, pointers and functions), as well as t_str.
152 */
153 struct lint1_type {
154 tspec_t t_tspec; /* type specifier */
155 bool t_incomplete_array:1;
156 bool t_const:1; /* const modifier */
157 bool t_volatile:1; /* volatile modifier */
158 bool t_proto:1; /* function prototype (t_args valid) */
159 bool t_vararg:1; /* prototype with '...' */
160 bool t_typedef:1; /* type defined with typedef */
161 bool t_bitfield:1;
162 /*
163 * Either the type is currently an enum (having t_tspec ENUM), or
164 * it is an integer type (typically INT) that has been implicitly
165 * converted from an enum type. In both cases, t_enum is valid.
166 *
167 * The information about a former enum type is retained to allow
168 * type checks in expressions such as ((var1 & 0x0001) == var2), to
169 * detect when var1 and var2 are from incompatible enum types.
170 */
171 bool t_is_enum:1;
172 bool t_packed:1;
173 union {
174 int _t_dim; /* dimension (if ARRAY) */
175 struct_or_union *_t_str;
176 enumeration *_t_enum;
177 struct sym *_t_args; /* arguments (if t_proto) */
178 } t_u;
179 struct {
180 unsigned int _t_flen:8; /* length of bit-field */
181 unsigned int _t_foffs:24; /* offset of bit-field */
182 } t_b;
183 struct lint1_type *t_subt; /* element type (if ARRAY),
184 * return value (if FUNC),
185 * target type (if PTR) */
186 };
187
188 #define t_dim t_u._t_dim
189 /* TODO: rename t_str to t_sou, to avoid confusing it with strings. */
190 #define t_str t_u._t_str
191 #define t_enum t_u._t_enum
192 #define t_args t_u._t_args
193 #define t_flen t_b._t_flen
194 #define t_foffs t_b._t_foffs
195
196 /*
197 * types of symbols
198 */
199 typedef enum {
200 FVFT, /* variables, functions, type names, enums */
201 FMEMBER, /* members of structs or unions */
202 FTAG, /* tags */
203 FLABEL /* labels */
204 } symt_t;
205
206 /*
207 * storage classes and related things
208 */
209 typedef enum {
210 NOSCL,
211 EXTERN, /* external symbols (independent of decl_t) */
212 STATIC, /* static symbols (local and global) */
213 AUTO, /* automatic symbols (except register) */
214 REG, /* register */
215 TYPEDEF, /* typedef */
216 STRUCT_TAG,
217 UNION_TAG,
218 ENUM_TAG,
219 MOS, /* member of struct */
220 MOU, /* member of union */
221 CTCONST, /* enumerator, enum constant or bool constant */
222 ABSTRACT, /* abstract symbol (sizeof, casts, unnamed argument) */
223 OLD_STYLE_ARG, /* old-style function argument declarations */
224 PROTO_ARG, /* used in declaration stack during prototype
225 declaration */
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 val_t s_value; /* value (if enum or bool constant) */
262 union {
263 /* XXX: what is the difference to s_type->t_str? */
264 struct_or_union *_s_st;
265 tspec_t _s_tsp; /* type (only for keywords) */
266 tqual_t _s_tqu; /* qualifier (only for keywords) */
267 struct sym *_s_args; /* arguments in old style function
268 definitions */
269 } u;
270 struct sym *s_symtab_next; /* next symbol with same hash value */
271 struct sym **s_symtab_ref; /* pointer to s_symtab_next of the
272 * previous symbol */
273 struct sym *s_next; /* next struct/union member, enumerator,
274 argument */
275 struct sym *s_level_next; /* next symbol declared on the same
276 * level */
277 } sym_t;
278
279 #define s_sou_type u._s_st
280 #define s_tspec u._s_tsp
281 #define s_tqual u._s_tqu
282 #define s_args u._s_args
283
284 /*
285 * Used to keep some information about symbols before they are entered
286 * into the symbol table.
287 */
288 typedef struct sbuf {
289 const char *sb_name; /* name of symbol */
290 size_t sb_len; /* length (without '\0') */
291 sym_t *sb_sym; /* symbol table entry */
292 } sbuf_t;
293
294
295 /*
296 * tree node
297 */
298 typedef struct tnode {
299 op_t tn_op; /* operator */
300 type_t *tn_type; /* type */
301 bool tn_lvalue:1; /* node is lvalue */
302 bool tn_cast:1; /* if tn_op == CVT, it's an explicit cast */
303 bool tn_parenthesized:1;
304 bool tn_sys:1; /* in strict bool mode, allow mixture between
305 * bool and scalar, for code from system
306 * headers that may be a mixture between
307 * scalar types and bool
308 */
309 bool tn_system_dependent:1; /* depends on sizeof or offsetof */
310 union {
311 struct {
312 struct tnode *_tn_left; /* (left) operand */
313 struct tnode *_tn_right; /* right operand */
314 } tn_s;
315 sym_t *_tn_sym; /* symbol if op == NAME */
316 val_t *_tn_val; /* value if op == CON */
317 strg_t *_tn_string; /* string if op == STRING */
318 } tn_u;
319 } tnode_t;
320
321 #define tn_left tn_u.tn_s._tn_left
322 #define tn_right tn_u.tn_s._tn_right
323 #define tn_sym tn_u._tn_sym
324 #define tn_val tn_u._tn_val
325 #define tn_string tn_u._tn_string
326
327 struct generic_association {
328 type_t *ga_arg; /* NULL means default or error */
329 tnode_t *ga_result; /* NULL means error */
330 struct generic_association *ga_prev;
331 };
332
333 struct array_size {
334 bool has_dim;
335 int dim;
336 };
337
338 /*
339 * For nested declarations there is a stack that holds all information
340 * needed for the current level. dcs points to the innermost element of this
341 * stack.
342 *
343 * d_ctx describes the context of the current declaration. Its value is
344 * one of
345 * EXTERN global declarations
346 * MOS or MOU declarations of struct or union members
347 * CTCONST declarations of enums or boolean constants
348 * OLD_STYLE_ARG declaration of arguments in old-style function
349 * definitions
350 * PROTO_ARG declaration of arguments in function prototypes
351 * AUTO declaration of local symbols
352 * ABSTRACT abstract declarations (sizeof, casts)
353 *
354 */
355 typedef struct dinfo {
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 end_type() pointer to the type used
362 for all declarators */
363 sym_t *d_redeclared_symbol;
364 unsigned int d_offset; /* offset of next structure member */
365 unsigned short d_sou_align_in_bits; /* alignment required for current
366 * structure */
367 scl_t d_ctx; /* context of declaration */
368 bool d_const:1; /* const in declaration specifiers */
369 bool d_volatile:1; /* volatile in declaration specifiers */
370 bool d_inline:1; /* inline in declaration specifiers */
371 bool d_multiple_storage_classes:1; /* reported in end_type */
372 bool d_invalid_type_combination:1;
373 bool d_nonempty_decl:1; /* if at least one tag is declared
374 * ... in the current function decl. */
375 bool d_vararg:1;
376 bool d_proto:1; /* current function decl. is prototype */
377 bool d_notyp:1; /* set if no type specifier was present */
378 bool d_asm:1; /* set if d_ctx == AUTO and asm() present */
379 bool d_packed:1;
380 bool d_used:1;
381 type_t *d_tagtyp; /* tag during member declaration */
382 sym_t *d_func_args; /* list of arguments during function def. */
383 pos_t d_func_def_pos; /* position of function definition */
384 sym_t *d_dlsyms; /* first symbol declared at this level */
385 sym_t **d_ldlsym; /* points to s_level_next in the last symbol
386 declaration at this level */
387 sym_t *d_func_proto_syms; /* symbols defined in prototype */
388 struct dinfo *d_enclosing; /* the enclosing declaration level */
389 } dinfo_t;
390
391 /* One level of pointer indirection in declarators, including qualifiers. */
392 typedef struct qual_ptr {
393 bool p_const: 1;
394 bool p_volatile: 1;
395 bool p_pointer: 1;
396 struct qual_ptr *p_next;
397 } qual_ptr;
398
399 /*
400 * The values of the 'case' labels, linked via cl_next in reverse order of
401 * appearance in the code, that is from bottom to top.
402 */
403 typedef struct case_label {
404 val_t cl_val;
405 struct case_label *cl_next;
406 } case_label_t;
407
408 typedef enum {
409 CS_DO_WHILE,
410 CS_FOR,
411 CS_FUNCTION_BODY,
412 CS_IF,
413 CS_SWITCH,
414 CS_WHILE
415 } control_statement_kind;
416
417 /*
418 * Used to keep information about nested control statements.
419 */
420 typedef struct control_statement {
421 control_statement_kind c_kind; /* to ensure proper nesting */
422 bool c_loop:1; /* 'continue' and 'break' are valid */
423 bool c_switch:1; /* 'case' and 'break' are valid */
424 bool c_break:1; /* the loop/switch has a reachable
425 * 'break' statement */
426 bool c_continue:1; /* the loop has a reachable 'continue'
427 * statement */
428 bool c_default:1; /* the switch has a 'default' label */
429 bool c_maybe_endless:1; /* the controlling expression is
430 * always true (as in 'for (;;)' or
431 * 'while (1)'), there may be break
432 * statements though */
433 bool c_always_then:1;
434 bool c_reached_end_of_then:1;
435 bool c_had_return_noval:1; /* had "return;" */
436 bool c_had_return_value:1; /* had "return expr;" */
437
438 type_t *c_switch_type; /* type of switch expression */
439 tnode_t *c_switch_expr;
440 case_label_t *c_case_labels; /* list of case values */
441
442 struct memory_block *c_for_expr3_mem; /* saved memory for end of loop
443 * expression in for() */
444 tnode_t *c_for_expr3; /* end of loop expr in for() */
445 pos_t c_for_expr3_pos; /* position of end of loop expr */
446 pos_t c_for_expr3_csrc_pos; /* same for csrc_pos */
447
448 struct control_statement *c_surrounding;
449 } control_statement;
450
451 typedef struct {
452 size_t lo; /* inclusive */
453 size_t hi; /* inclusive */
454 } range_t;
455
456 #include "externs1.h"
457
458 #define ERR_SETSIZE 1024
459 #define __NERRBITS (sizeof(unsigned int))
460
461 typedef struct err_set {
462 unsigned int errs_bits[(ERR_SETSIZE + __NERRBITS-1) / __NERRBITS];
463 } err_set;
464
465 #define ERR_SET(n, p) \
466 ((p)->errs_bits[(n)/__NERRBITS] |= (1 << ((n) % __NERRBITS)))
467 #define ERR_CLR(n, p) \
468 ((p)->errs_bits[(n)/__NERRBITS] &= ~(1 << ((n) % __NERRBITS)))
469 #define ERR_ISSET(n, p) \
470 (((p)->errs_bits[(n)/__NERRBITS] & (1 << ((n) % __NERRBITS))) != 0)
471 #define ERR_ZERO(p) (void)memset((p), 0, sizeof(*(p)))
472
473 #define INTERNAL_ERROR(fmt, args...) \
474 internal_error(__FILE__, __LINE__, fmt, ##args)
475
476 #define lint_assert(cond) \
477 do { \
478 if (!(cond)) \
479 assert_failed(__FILE__, __LINE__, __func__, #cond); \
480 } while (false)
481
482 extern err_set msgset;
483
484
485 #ifdef DEBUG
486 # include "err-msgs.h"
487
488 /* ARGSUSED */
489 static inline void __attribute__((format(printf, 1, 2)))
490 check_printf(const char *fmt, ...)
491 {
492 }
493
494 # define wrap_check_printf_at(func, msgid, pos, args...) \
495 do { \
496 check_printf(__CONCAT(MSG_, msgid), ##args); \
497 (func)(msgid, pos, ##args); \
498 } while (false)
499
500 # define error_at(msgid, pos, args...) \
501 wrap_check_printf_at(error_at, msgid, pos, ##args)
502 # define warning_at(msgid, pos, args...) \
503 wrap_check_printf_at(warning_at, msgid, pos, ##args)
504 # define message_at(msgid, pos, args...) \
505 wrap_check_printf_at(message_at, msgid, pos, ##args)
506
507 # define wrap_check_printf(func, msgid, args...) \
508 do { \
509 check_printf(__CONCAT(MSG_, msgid), ##args); \
510 (func)(msgid, ##args); \
511 } while (false)
512
513 # define error(msgid, args...) wrap_check_printf(error, msgid, ##args)
514 # define warning(msgid, args...) wrap_check_printf(warning, msgid, ##args)
515 # define gnuism(msgid, args...) wrap_check_printf(gnuism, msgid, ##args)
516 # define c99ism(msgid, args...) wrap_check_printf(c99ism, msgid, ##args)
517 # define c11ism(msgid, args...) wrap_check_printf(c11ism, msgid, ##args)
518 #endif
519
520 static inline bool
521 is_nonzero_val(const val_t *val)
522 {
523 return is_floating(val->v_tspec)
524 ? val->v_ldbl != 0.0
525 : val->v_quad != 0;
526 }
527
528 static inline bool
529 constant_is_nonzero(const tnode_t *tn)
530 {
531 lint_assert(tn->tn_op == CON);
532 lint_assert(tn->tn_type->t_tspec == tn->tn_val->v_tspec);
533 return is_nonzero_val(tn->tn_val);
534 }
535
536 static inline bool
537 is_zero(const tnode_t *tn)
538 {
539 return tn != NULL && tn->tn_op == CON && !is_nonzero_val(tn->tn_val);
540 }
541
542 static inline bool
543 is_nonzero(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_binary(const tnode_t *tn)
550 {
551 return modtab[tn->tn_op].m_binary;
552 }
553
554 static inline uint64_t
555 bit(unsigned i)
556 {
557 /*
558 * TODO: Add proper support for INT128.
559 * This involves changing val_t to 128 bits.
560 */
561 if (i >= 64)
562 return 0; /* XXX: not correct for INT128 and UINT128 */
563
564 lint_assert(i < 64);
565 return (uint64_t)1 << i;
566 }
567
568 static inline bool
569 msb(int64_t q, tspec_t t)
570 {
571 return (q & bit((unsigned int)size_in_bits(t) - 1)) != 0;
572 }
573
574 static inline uint64_t
575 value_bits(unsigned bitsize)
576 {
577 lint_assert(bitsize > 0);
578
579 /* for long double (80 or 128), double _Complex (128) */
580 /*
581 * XXX: double _Complex does not have 128 bits of precision,
582 * therefore it should never be necessary to query the value bits
583 * of such a type; see d_c99_complex_split.c to trigger this case.
584 */
585 if (bitsize >= 64)
586 return ~((uint64_t)0);
587
588 return ~(~(uint64_t)0 << bitsize);
589 }
590
591 /* C99 6.7.8p7 */
592 static inline bool
593 is_struct_or_union(tspec_t t)
594 {
595 return t == STRUCT || t == UNION;
596 }
597