lint1.h revision 1.107 1 /* $NetBSD: lint1.h,v 1.107 2021/06/27 20:47:13 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 to simply by a pointer to its 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 tspec_t st_tspec; /* CHAR or WCHAR */
90 size_t st_len; /* length without trailing NUL */
91 union {
92 u_char *_st_cp;
93 wchar_t *_st_wcp;
94 } st_u;
95 } strg_t;
96
97 #define st_cp st_u._st_cp
98 #define st_wcp st_u._st_wcp
99
100 /*
101 * qualifiers (only for lex/yacc interface)
102 */
103 typedef enum {
104 CONST, VOLATILE, RESTRICT, THREAD
105 } tqual_t;
106
107 /* An integer or floating-point value. */
108 typedef struct {
109 tspec_t v_tspec;
110 /*
111 * Set if an integer constant is unsigned only in C90 and later, but
112 * not in traditional C.
113 *
114 * See the operators table in ops.def, columns "l r".
115 */
116 bool v_unsigned_since_c90;
117 union {
118 int64_t _v_quad; /* integers */
119 ldbl_t _v_ldbl; /* floats */
120 } v_u;
121 } val_t;
122
123 #define v_quad v_u._v_quad
124 #define v_ldbl v_u._v_ldbl
125
126 /*
127 * Structures of type struct_or_union uniquely identify structures. This can't
128 * be done in structures of type type_t, because these are copied
129 * if they must be modified. So it would not be possible to check
130 * if two structures are identical by comparing the pointers to
131 * the type structures.
132 *
133 * The typename is used if the structure is unnamed to identify
134 * the structure type in pass 2.
135 */
136 typedef struct {
137 u_int sou_size_in_bits;
138 u_int sou_align_in_bits : 15;
139 bool sou_incomplete : 1;
140 struct sym *sou_first_member;
141 struct sym *sou_tag;
142 struct sym *sou_first_typedef;
143 } struct_or_union;
144
145 /*
146 * same as above for enums
147 */
148 typedef struct {
149 bool en_incomplete : 1;
150 struct sym *en_first_enumerator;
151 struct sym *en_tag;
152 struct sym *en_first_typedef;
153 } enumeration;
154
155 /*
156 * The type of an expression or object. Complex types are formed via t_subt
157 * (for arrays, pointers and functions), as well as t_str.
158 */
159 struct lint1_type {
160 tspec_t t_tspec; /* type specifier */
161 bool t_incomplete_array : 1;
162 bool t_const : 1; /* const modifier */
163 bool t_volatile : 1; /* volatile modifier */
164 bool t_proto : 1; /* function prototype (t_args valid) */
165 bool t_vararg : 1; /* prototype with '...' */
166 bool t_typedef : 1; /* type defined with typedef */
167 bool t_bitfield : 1;
168 bool t_is_enum : 1; /* type is (or was) enum (t_enum valid) */
169 bool t_packed : 1;
170 union {
171 int _t_dim; /* dimension (if ARRAY) */
172 struct_or_union *_t_str;
173 enumeration *_t_enum;
174 struct sym *_t_args; /* arguments (if t_proto) */
175 } t_u;
176 struct {
177 u_int _t_flen : 8; /* length of bit-field */
178 u_int _t_foffs : 24; /* offset of bit-field */
179 } t_b;
180 struct lint1_type *t_subt; /* element type (if ARRAY),
181 * return value (if FUNC),
182 * target type (if PTR) */
183 };
184
185 #define t_dim t_u._t_dim
186 #define t_str t_u._t_str
187 #define t_enum t_u._t_enum
188 #define t_args t_u._t_args
189 #define t_flen t_b._t_flen
190 #define t_foffs t_b._t_foffs
191
192 /*
193 * types of symbols
194 */
195 typedef enum {
196 FVFT, /* variables, functions, type names, enums */
197 FMEMBER, /* members of structs or unions */
198 FTAG, /* tags */
199 FLABEL /* labels */
200 } symt_t;
201
202 /*
203 * storage classes
204 */
205 typedef enum {
206 NOSCL,
207 EXTERN, /* external symbols (indep. of decl_t) */
208 STATIC, /* static symbols (local and global) */
209 AUTO, /* automatic symbols (except register) */
210 REG, /* register */
211 TYPEDEF, /* typedef */
212 STRUCT_TAG,
213 UNION_TAG,
214 ENUM_TAG,
215 MOS, /* member of struct */
216 MOU, /* member of union */
217 CTCONST, /* enumerator, enum constant or bool constant */
218 ABSTRACT, /* abstract symbol (sizeof, casts, unnamed argument) */
219 ARG, /* argument */
220 PROTO_ARG, /* used in declaration stack during prototype
221 declaration */
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 void *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_reg : 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 local declared external symbols pointer
250 to external symbol with same name */
251 def_t s_def; /* declared, tentative defined, defined */
252 scl_t s_scl; /* storage class */
253 int s_block_level; /* level of declaration, -1 if not in symbol
254 table */
255 type_t *s_type;
256 val_t s_value; /* value (if enum or bool constant) */
257 union {
258 struct_or_union *_s_st;
259 enumeration *_s_et;
260 tspec_t _s_tsp; /* type (only for keywords) */
261 tqual_t _s_tqu; /* qualifier (only for keywords) */
262 struct sym *_s_args; /* arguments in old style function
263 definitions */
264 } u;
265 struct sym *s_link; /* next symbol with same hash value */
266 struct sym **s_rlink; /* pointer to s_link of prev. symbol */
267 struct sym *s_next; /* next struct/union member, enumerator,
268 argument */
269 struct sym *s_dlnxt; /* next symbol declared on same level */
270 } sym_t;
271
272 #define s_styp u._s_st
273 #define s_etyp u._s_et
274 #define s_tspec u._s_tsp
275 #define s_tqual u._s_tqu
276 #define s_args u._s_args
277
278 /*
279 * Used to keep some information about symbols before they are entered
280 * into the symbol table.
281 */
282 typedef struct sbuf {
283 const char *sb_name; /* name of symbol */
284 size_t sb_len; /* length (without '\0') */
285 int sb_hash; /* hash value */
286 sym_t *sb_sym; /* symbol table entry */
287 struct sbuf *sb_next; /* for freelist */
288 } sbuf_t;
289
290
291 /*
292 * tree node
293 */
294 typedef struct tnode {
295 op_t tn_op; /* operator */
296 type_t *tn_type; /* type */
297 bool tn_lvalue : 1; /* node is lvalue */
298 bool tn_cast : 1; /* if tn_op == CVT, it's an explicit cast */
299 bool tn_parenthesized : 1;
300 bool tn_from_system_header : 1;
301 bool tn_system_dependent : 1; /* depends on sizeof or offsetof */
302 union {
303 struct {
304 struct tnode *_tn_left; /* (left) operand */
305 struct tnode *_tn_right; /* right operand */
306 } tn_s;
307 sym_t *_tn_sym; /* symbol if op == NAME */
308 val_t *_tn_val; /* value if op == CON */
309 strg_t *_tn_string; /* string if op == STRING */
310 } tn_u;
311 } tnode_t;
312
313 #define tn_left tn_u.tn_s._tn_left
314 #define tn_right tn_u.tn_s._tn_right
315 #define tn_sym tn_u._tn_sym
316 #define tn_val tn_u._tn_val
317 #define tn_string tn_u._tn_string
318
319 struct generic_association_types {
320 type_t *gat_arg; /* NULL means default or error */
321 tnode_t *gat_result; /* NULL means error */
322 struct generic_association_types *gat_prev;
323 };
324
325 /*
326 * For nested declarations a stack exists, which holds all information
327 * needed for the current level. dcs points to the innermost element of this
328 * stack.
329 *
330 * d_ctx describes the context of the current declaration. Its value is
331 * one of
332 * EXTERN global declarations
333 * MOS or MOU declarations of struct or union members
334 * CTCONST declarations of enums or boolean constants
335 * ARG declaration of arguments in old-style function
336 * definitions
337 * PROTO_ARG declaration of arguments in function prototypes
338 * AUTO declaration of local symbols
339 * ABSTRACT abstract declarations (sizeof, casts)
340 *
341 */
342 typedef struct dinfo {
343 tspec_t d_abstract_type;/* VOID, BOOL, CHAR, INT or COMPLEX */
344 tspec_t d_complex_mod; /* FLOAT or DOUBLE */
345 tspec_t d_sign_mod; /* SIGNED or UNSIGN */
346 tspec_t d_rank_mod; /* SHORT, LONG or QUAD */
347 scl_t d_scl; /* storage class */
348 type_t *d_type; /* after deftyp() pointer to the type used
349 for all declarators */
350 sym_t *d_redeclared_symbol;
351 int d_offset; /* offset of next structure member */
352 int d_stralign; /* alignment required for current structure */
353 scl_t d_ctx; /* context of declaration */
354 bool d_const : 1; /* const in declaration specifiers */
355 bool d_volatile : 1; /* volatile in declaration specifiers */
356 bool d_inline : 1; /* inline in declaration specifiers */
357 bool d_mscl : 1; /* multiple storage classes */
358 bool d_terr : 1; /* invalid type combination */
359 bool d_nonempty_decl : 1; /* if at least one tag is declared
360 * ... in the current function decl. */
361 bool d_vararg : 1;
362 bool d_proto : 1; /* current function decl. is prototype */
363 bool d_notyp : 1; /* set if no type specifier was present */
364 bool d_asm : 1; /* set if d_ctx == AUTO and asm() present */
365 bool d_packed : 1;
366 bool d_used : 1;
367 type_t *d_tagtyp; /* tag during member declaration */
368 sym_t *d_func_args; /* list of arguments during function def. */
369 pos_t d_func_def_pos; /* position of function definition */
370 sym_t *d_dlsyms; /* first symbol declared at this level */
371 sym_t **d_ldlsym; /* points to s_dlnxt in last symbol decl.
372 at this level */
373 sym_t *d_func_proto_syms; /* symbols defined in prototype */
374 struct dinfo *d_next; /* next level */
375 } dinfo_t;
376
377 /*
378 * Used to collect information about pointers and qualifiers in
379 * declarators.
380 */
381 typedef struct pqinf {
382 bool p_pointer: 1;
383 bool p_const: 1;
384 bool p_volatile: 1;
385 struct pqinf *p_next;
386 } pqinf_t;
387
388 /*
389 * The values of the 'case' labels, linked via cl_next in reverse order of
390 * appearance in the code, that is from bottom to top.
391 */
392 typedef struct case_label {
393 val_t cl_val;
394 struct case_label *cl_next;
395 } case_label_t;
396
397 typedef enum {
398 CS_DO_WHILE,
399 CS_FOR,
400 CS_FUNCTION_BODY,
401 CS_IF,
402 CS_SWITCH,
403 CS_WHILE
404 } control_statement_kind;
405
406 /*
407 * Used to keep information about nested control statements.
408 */
409 typedef struct control_statement {
410 control_statement_kind c_kind; /* to ensure proper nesting */
411 bool c_loop : 1; /* 'continue' and 'break' are valid */
412 bool c_switch : 1; /* 'case' and 'break' are valid */
413 bool c_break : 1; /* the loop/switch has a reachable
414 * 'break' statement */
415 bool c_continue : 1; /* the loop has a reachable 'continue'
416 * statement */
417 bool c_default : 1; /* the switch has a 'default' label */
418 bool c_maybe_endless : 1; /* the controlling expression is
419 * always true (as in 'for (;;)' or
420 * 'while (1)'), there may be break
421 * statements though */
422 bool c_always_then : 1;
423 bool c_reached_end_of_then : 1;
424 bool c_had_return_noval : 1; /* had "return;" */
425 bool c_had_return_value : 1; /* had "return expr;" */
426
427 type_t *c_switch_type; /* type of switch expression */
428 tnode_t *c_switch_expr;
429 case_label_t *c_case_labels; /* list of case values */
430
431 struct memory_block *c_for_expr3_mem; /* saved memory for end of loop
432 * expression in for() */
433 tnode_t *c_for_expr3; /* end of loop expr in for() */
434 pos_t c_for_expr3_pos; /* position of end of loop expr */
435 pos_t c_for_expr3_csrc_pos; /* same for csrc_pos */
436
437 struct control_statement *c_surrounding;
438 } cstk_t;
439
440 typedef struct {
441 size_t lo; /* inclusive */
442 size_t hi; /* inclusive */
443 } range_t;
444
445 #include "externs1.h"
446
447 #define ERR_SETSIZE 1024
448 #define __NERRBITS (sizeof(unsigned int))
449
450 typedef struct err_set {
451 unsigned int errs_bits[(ERR_SETSIZE + __NERRBITS-1) / __NERRBITS];
452 } err_set;
453
454 #define ERR_SET(n, p) \
455 ((p)->errs_bits[(n)/__NERRBITS] |= (1 << ((n) % __NERRBITS)))
456 #define ERR_CLR(n, p) \
457 ((p)->errs_bits[(n)/__NERRBITS] &= ~(1 << ((n) % __NERRBITS)))
458 #define ERR_ISSET(n, p) \
459 (((p)->errs_bits[(n)/__NERRBITS] & (1 << ((n) % __NERRBITS))) != 0)
460 #define ERR_ZERO(p) (void)memset((p), 0, sizeof(*(p)))
461
462 #define INTERNAL_ERROR(fmt, args...) \
463 internal_error(__FILE__, __LINE__, fmt, ##args)
464
465 #define lint_assert(cond) \
466 do { \
467 if (!(cond)) \
468 assert_failed(__FILE__, __LINE__, __func__, #cond); \
469 } while (false)
470
471 #ifdef BLKDEBUG
472 #define ZERO 0xa5
473 #else
474 #define ZERO 0
475 #endif
476
477 extern err_set msgset;
478
479
480 #ifdef DEBUG
481 # include "err-msgs.h"
482
483 /* ARGSUSED */
484 static inline void __attribute__((format(printf, 1, 2)))
485 check_printf(const char *fmt, ...)
486 {
487 }
488
489 # define wrap_check_printf_at(func, msgid, pos, args...) \
490 do { \
491 check_printf(__CONCAT(MSG_, msgid), ##args); \
492 (func)(msgid, pos, ##args); \
493 } while (false)
494
495 # define error_at(msgid, pos, args...) \
496 wrap_check_printf_at(error_at, msgid, pos, ##args)
497 # define warning_at(msgid, pos, args...) \
498 wrap_check_printf_at(warning_at, msgid, pos, ##args)
499 # define message_at(msgid, pos, args...) \
500 wrap_check_printf_at(message_at, msgid, pos, ##args)
501
502 # define wrap_check_printf(func, msgid, args...) \
503 do { \
504 check_printf(__CONCAT(MSG_, msgid), ##args); \
505 (func)(msgid, ##args); \
506 } while (false)
507
508 # define error(msgid, args...) wrap_check_printf(error, msgid, ##args)
509 # define warning(msgid, args...) wrap_check_printf(warning, msgid, ##args)
510 # define message(msgid, args...) wrap_check_printf(message, msgid, ##args)
511 # define gnuism(msgid, args...) wrap_check_printf(gnuism, msgid, ##args)
512 # define c99ism(msgid, args...) wrap_check_printf(c99ism, msgid, ##args)
513 # define c11ism(msgid, args...) wrap_check_printf(c11ism, msgid, ##args)
514 #endif
515
516 static inline bool
517 is_nonzero_val(const val_t *val)
518 {
519 return is_floating(val->v_tspec)
520 ? val->v_ldbl != 0.0
521 : val->v_quad != 0;
522 }
523
524 static inline bool
525 constant_is_nonzero(const tnode_t *tn)
526 {
527 lint_assert(tn->tn_op == CON);
528 lint_assert(tn->tn_type->t_tspec == tn->tn_val->v_tspec);
529 return is_nonzero_val(tn->tn_val);
530 }
531
532 static inline bool
533 is_zero(const tnode_t *tn)
534 {
535 return tn != NULL && tn->tn_op == CON && !is_nonzero_val(tn->tn_val);
536 }
537
538 static inline bool
539 is_nonzero(const tnode_t *tn)
540 {
541 return tn != NULL && tn->tn_op == CON && is_nonzero_val(tn->tn_val);
542 }
543
544 static inline uint64_t
545 bit(unsigned i)
546 {
547 lint_assert(i < 64);
548 return (uint64_t)1 << i;
549 }
550
551 static inline uint64_t
552 value_bits(unsigned bitsize)
553 {
554 lint_assert(bitsize > 0);
555
556 /* for long double (80 or 128), double _Complex (128) */
557 /*
558 * XXX: double _Complex does not have 128 bits of precision,
559 * therefore it should never be necessary to query the value bits
560 * of such a type; see d_c99_complex_split.c to trigger this case.
561 */
562 if (bitsize >= 64)
563 return ~((uint64_t)0);
564
565 return ~(~(uint64_t)0 << bitsize);
566 }
567
568 /* C99 6.7.8p7 */
569 static inline bool
570 is_struct_or_union(tspec_t t)
571 {
572 return t == STRUCT || t == UNION;
573 }
574