lint1.h revision 1.147 1 /* $NetBSD: lint1.h,v 1.147 2022/04/09 15:43:41 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 #define LWARN_BAD (-3)
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_bitfield:1;
147 /*
148 * Either the type is currently an enum (having t_tspec ENUM), or
149 * it is an integer type (typically INT) that has been implicitly
150 * converted from an enum type. In both cases, t_enum is valid.
151 *
152 * The information about a former enum type is retained to allow
153 * type checks in expressions such as ((var1 & 0x0001) == var2), to
154 * detect when var1 and var2 are from incompatible enum types.
155 */
156 bool t_is_enum:1;
157 bool t_packed:1;
158 union {
159 int _t_dim; /* dimension (if ARRAY) */
160 struct_or_union *_t_str;
161 enumeration *_t_enum;
162 struct sym *_t_args; /* arguments (if t_proto) */
163 } t_u;
164 struct {
165 unsigned int _t_flen:8; /* length of bit-field */
166 unsigned int _t_foffs:24; /* offset of bit-field */
167 } t_b;
168 struct lint1_type *t_subt; /* element type (if ARRAY),
169 * return value (if FUNC),
170 * target type (if PTR) */
171 };
172
173 #define t_dim t_u._t_dim
174 /* TODO: rename t_str to t_sou, to avoid confusing it with strings. */
175 #define t_str t_u._t_str
176 #define t_enum t_u._t_enum
177 #define t_args t_u._t_args
178 #define t_flen t_b._t_flen
179 #define t_foffs t_b._t_foffs
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 MOS, /* member of struct */
205 MOU, /* member of union */
206 BOOL_CONST,
207 ENUM_CONST,
208 ABSTRACT, /* abstract symbol (sizeof, casts, unnamed argument) */
209 OLD_STYLE_ARG, /* old-style function argument declarations */
210 PROTO_ARG, /* used in declaration stack during prototype
211 declaration */
212 INLINE /* only used by the parser */
213 } scl_t;
214
215 /*
216 * symbol table entry
217 */
218 typedef struct sym {
219 const char *s_name;
220 const char *s_rename; /* renamed symbol's given name */
221 pos_t s_def_pos; /* position of last (prototype) definition,
222 prototype declaration, no-prototype-def.,
223 tentative definition or declaration,
224 in this order */
225 pos_t s_set_pos; /* position of first initialization */
226 pos_t s_use_pos; /* position of first use */
227 symt_t s_kind; /* type of symbol */
228 const struct keyword *s_keyword;
229 bool s_bitfield:1;
230 bool s_set:1; /* variable set, label defined */
231 bool s_used:1; /* variable/label used */
232 bool s_arg:1; /* symbol is function argument */
233 bool s_register:1; /* symbol is register variable */
234 bool s_defarg:1; /* undefined symbol in old style function
235 definition */
236 bool s_return_type_implicit_int:1;
237 bool s_osdef:1; /* symbol stems from old style function def. */
238 bool s_inline:1; /* true if this is an inline function */
239 struct sym *s_ext_sym; /* for locally declared external symbols, the
240 * pointer to the external symbol with the
241 * same name */
242 def_t s_def; /* declared, tentative defined, defined */
243 scl_t s_scl; /* storage class */
244 int s_block_level; /* level of declaration, -1 if not in symbol
245 table */
246 type_t *s_type;
247 union {
248 bool s_bool_constant;
249 int s_enum_constant; /* XXX: should be TARG_INT */
250 struct {
251 /* XXX: what is the difference to s_type->t_str? */
252 struct_or_union *sm_sou_type;
253 unsigned int sm_offset_in_bits;
254 } s_member;
255 struct {
256 int sk_token;
257 tspec_t sk_tspec; /* only for types */
258 tqual_t sk_qualifier; /* only for qualifiers */
259 } s_keyword;
260 struct sym *s_old_style_args; /* arguments in an old-style
261 * function definition */
262 } u;
263 struct sym *s_symtab_next; /* next symbol with same hash value */
264 struct sym **s_symtab_ref; /* pointer to s_symtab_next of the
265 * previous symbol */
266 struct sym *s_next; /* next struct/union member, enumerator,
267 argument */
268 struct sym *s_level_next; /* next symbol declared on the same
269 * level */
270 } sym_t;
271
272 /*
273 * Used to keep some information about symbols before they are entered
274 * into the symbol table.
275 */
276 typedef struct sbuf {
277 const char *sb_name; /* name of symbol */
278 size_t sb_len; /* length (without '\0') */
279 sym_t *sb_sym; /* symbol table entry */
280 } sbuf_t;
281
282
283 /*
284 * tree node
285 */
286 typedef struct tnode {
287 op_t tn_op; /* operator */
288 type_t *tn_type; /* type */
289 bool tn_lvalue:1; /* node is lvalue */
290 bool tn_cast:1; /* if tn_op == CVT, it's an explicit cast */
291 bool tn_parenthesized:1;
292 bool tn_sys:1; /* in strict bool mode, allow mixture between
293 * bool and scalar, for code from system
294 * headers that may be a mixture between
295 * scalar types and bool
296 */
297 bool tn_system_dependent:1; /* depends on sizeof or offsetof */
298 union {
299 struct {
300 struct tnode *_tn_left; /* (left) operand */
301 struct tnode *_tn_right; /* right operand */
302 } tn_s;
303 sym_t *_tn_sym; /* symbol if op == NAME */
304 val_t *_tn_val; /* value if op == CON */
305 strg_t *_tn_string; /* string if op == STRING */
306 } tn_u;
307 } tnode_t;
308
309 #define tn_left tn_u.tn_s._tn_left
310 #define tn_right tn_u.tn_s._tn_right
311 #define tn_sym tn_u._tn_sym
312 #define tn_val tn_u._tn_val
313 #define tn_string tn_u._tn_string
314
315 struct generic_association {
316 type_t *ga_arg; /* NULL means default or error */
317 tnode_t *ga_result; /* NULL means error */
318 struct generic_association *ga_prev;
319 };
320
321 struct array_size {
322 bool has_dim;
323 int dim;
324 };
325
326 /*
327 * For nested declarations there is a stack that holds all information
328 * needed for the current level. dcs points to the innermost element of this
329 * stack.
330 *
331 * d_ctx describes the context of the current declaration. Its value is
332 * one of
333 * EXTERN global declarations
334 * MOS or MOU declarations of struct or union members
335 * CTCONST declarations of enums or boolean constants
336 * OLD_STYLE_ARG declaration of arguments in old-style function
337 * definitions
338 * PROTO_ARG declaration of arguments in function prototypes
339 * AUTO declaration of local symbols
340 * ABSTRACT abstract declarations (sizeof, casts)
341 *
342 */
343 typedef struct dinfo {
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 end_type() pointer to the type used
350 for all declarators */
351 sym_t *d_redeclared_symbol;
352 unsigned int d_offset; /* offset of next structure member in bits */
353 unsigned short d_sou_align_in_bits; /* alignment required for current
354 * structure */
355 scl_t d_ctx; /* context of declaration */
356 bool d_const:1; /* const in declaration specifiers */
357 bool d_volatile:1; /* volatile in declaration specifiers */
358 bool d_inline:1; /* inline in declaration specifiers */
359 bool d_multiple_storage_classes:1; /* reported in end_type */
360 bool d_invalid_type_combination:1;
361 bool d_nonempty_decl:1; /* if at least one tag is declared
362 * ... in the current function decl. */
363 bool d_vararg:1;
364 bool d_proto:1; /* current function decl. is prototype */
365 bool d_notyp:1; /* set if no type specifier was present */
366 bool d_asm:1; /* set if d_ctx == AUTO and asm() present */
367 bool d_packed:1;
368 bool d_used:1;
369 type_t *d_tagtyp; /* tag during member declaration */
370 sym_t *d_func_args; /* list of arguments during function def. */
371 pos_t d_func_def_pos; /* position of function definition */
372 sym_t *d_dlsyms; /* first symbol declared at this level */
373 sym_t **d_ldlsym; /* points to s_level_next in the last symbol
374 declaration at this level */
375 sym_t *d_func_proto_syms; /* symbols defined in prototype */
376 struct dinfo *d_enclosing; /* the enclosing declaration level */
377 } dinfo_t;
378
379 /* One level of pointer indirection in declarators, including qualifiers. */
380 typedef struct qual_ptr {
381 bool p_const: 1;
382 bool p_volatile: 1;
383 bool p_pointer: 1;
384 struct qual_ptr *p_next;
385 } qual_ptr;
386
387 /*
388 * The values of the 'case' labels, linked via cl_next in reverse order of
389 * appearance in the code, that is from bottom to top.
390 */
391 typedef struct case_label {
392 val_t cl_val;
393 struct case_label *cl_next;
394 } case_label_t;
395
396 typedef enum {
397 CS_DO_WHILE,
398 CS_FOR,
399 CS_FUNCTION_BODY,
400 CS_IF,
401 CS_SWITCH,
402 CS_WHILE
403 } control_statement_kind;
404
405 /*
406 * Used to keep information about nested control statements.
407 */
408 typedef struct control_statement {
409 control_statement_kind c_kind; /* to ensure proper nesting */
410 bool c_loop:1; /* 'continue' and 'break' are valid */
411 bool c_switch:1; /* 'case' and 'break' are valid */
412 bool c_break:1; /* the loop/switch has a reachable
413 * 'break' statement */
414 bool c_continue:1; /* the loop has a reachable 'continue'
415 * statement */
416 bool c_default:1; /* the switch has a 'default' label */
417 bool c_maybe_endless:1; /* the controlling expression is
418 * always true (as in 'for (;;)' or
419 * 'while (1)'), there may be break
420 * statements though */
421 bool c_always_then:1;
422 bool c_reached_end_of_then:1;
423 bool c_had_return_noval:1; /* had "return;" */
424 bool c_had_return_value:1; /* had "return expr;" */
425
426 type_t *c_switch_type; /* type of switch expression */
427 tnode_t *c_switch_expr;
428 case_label_t *c_case_labels; /* list of case values */
429
430 struct memory_block *c_for_expr3_mem; /* saved memory for end of loop
431 * expression in for() */
432 tnode_t *c_for_expr3; /* end of loop expr in for() */
433 pos_t c_for_expr3_pos; /* position of end of loop expr */
434 pos_t c_for_expr3_csrc_pos; /* same for csrc_pos */
435
436 struct control_statement *c_surrounding;
437 } control_statement;
438
439 typedef struct {
440 size_t lo; /* inclusive */
441 size_t hi; /* inclusive */
442 } range_t;
443
444 #include "externs1.h"
445
446 #define ERR_SETSIZE 1024
447 #define __NERRBITS (sizeof(unsigned int))
448
449 typedef struct err_set {
450 unsigned int errs_bits[(ERR_SETSIZE + __NERRBITS-1) / __NERRBITS];
451 } err_set;
452
453 #define ERR_SET(n, p) \
454 ((p)->errs_bits[(n)/__NERRBITS] |= (1 << ((n) % __NERRBITS)))
455 #define ERR_CLR(n, p) \
456 ((p)->errs_bits[(n)/__NERRBITS] &= ~(1 << ((n) % __NERRBITS)))
457 #define ERR_ISSET(n, p) \
458 (((p)->errs_bits[(n)/__NERRBITS] & (1 << ((n) % __NERRBITS))) != 0)
459 #define ERR_ZERO(p) (void)memset((p), 0, sizeof(*(p)))
460
461 #define INTERNAL_ERROR(fmt, args...) \
462 internal_error(__FILE__, __LINE__, fmt, ##args)
463
464 #define lint_assert(cond) \
465 do { \
466 if (!(cond)) \
467 assert_failed(__FILE__, __LINE__, __func__, #cond); \
468 } while (false)
469
470 extern err_set msgset;
471
472
473 #ifdef DEBUG
474 # include "err-msgs.h"
475
476 /* ARGSUSED */
477 static inline void __attribute__((format(printf, 1, 2)))
478 check_printf(const char *fmt, ...)
479 {
480 }
481
482 # define wrap_check_printf_at(func, msgid, pos, args...) \
483 do { \
484 check_printf(__CONCAT(MSG_, msgid), ##args); \
485 (func)(msgid, pos, ##args); \
486 } while (false)
487
488 # define error_at(msgid, pos, args...) \
489 wrap_check_printf_at(error_at, msgid, pos, ##args)
490 # define warning_at(msgid, pos, args...) \
491 wrap_check_printf_at(warning_at, msgid, pos, ##args)
492 # define message_at(msgid, pos, args...) \
493 wrap_check_printf_at(message_at, msgid, pos, ##args)
494
495 # define wrap_check_printf(func, msgid, args...) \
496 do { \
497 check_printf(__CONCAT(MSG_, msgid), ##args); \
498 (func)(msgid, ##args); \
499 } while (false)
500
501 # define error(msgid, args...) wrap_check_printf(error, msgid, ##args)
502 # define warning(msgid, args...) wrap_check_printf(warning, msgid, ##args)
503 # define gnuism(msgid, args...) wrap_check_printf(gnuism, msgid, ##args)
504 # define c99ism(msgid, args...) wrap_check_printf(c99ism, msgid, ##args)
505 # define c11ism(msgid, args...) wrap_check_printf(c11ism, msgid, ##args)
506 #endif
507
508 static inline bool
509 is_nonzero_val(const val_t *val)
510 {
511 return is_floating(val->v_tspec)
512 ? val->v_ldbl != 0.0
513 : val->v_quad != 0;
514 }
515
516 static inline bool
517 constant_is_nonzero(const tnode_t *tn)
518 {
519 lint_assert(tn->tn_op == CON);
520 lint_assert(tn->tn_type->t_tspec == tn->tn_val->v_tspec);
521 return is_nonzero_val(tn->tn_val);
522 }
523
524 static inline bool
525 is_zero(const tnode_t *tn)
526 {
527 return tn != NULL && tn->tn_op == CON && !is_nonzero_val(tn->tn_val);
528 }
529
530 static inline bool
531 is_nonzero(const tnode_t *tn)
532 {
533 return tn != NULL && tn->tn_op == CON && is_nonzero_val(tn->tn_val);
534 }
535
536 static inline bool
537 is_binary(const tnode_t *tn)
538 {
539 return modtab[tn->tn_op].m_binary;
540 }
541
542 static inline uint64_t
543 bit(unsigned i)
544 {
545 /*
546 * TODO: Add proper support for INT128.
547 * This involves changing val_t to 128 bits.
548 */
549 if (i >= 64)
550 return 0; /* XXX: not correct for INT128 and UINT128 */
551
552 lint_assert(i < 64);
553 return (uint64_t)1 << i;
554 }
555
556 static inline bool
557 msb(int64_t q, tspec_t t)
558 {
559 return (q & bit((unsigned int)size_in_bits(t) - 1)) != 0;
560 }
561
562 static inline uint64_t
563 value_bits(unsigned bitsize)
564 {
565 lint_assert(bitsize > 0);
566
567 /* for long double (80 or 128), double _Complex (128) */
568 /*
569 * XXX: double _Complex does not have 128 bits of precision,
570 * therefore it should never be necessary to query the value bits
571 * of such a type; see d_c99_complex_split.c to trigger this case.
572 */
573 if (bitsize >= 64)
574 return ~((uint64_t)0);
575
576 return ~(~(uint64_t)0 << bitsize);
577 }
578
579 /* C99 6.7.8p7 */
580 static inline bool
581 is_struct_or_union(tspec_t t)
582 {
583 return t == STRUCT || t == UNION;
584 }
585