lint1.h revision 1.75 1 /* $NetBSD: lint1.h,v 1.75 2021/03/19 08:21:26 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 typedef struct {
62 int p_line;
63 const char *p_file;
64 int p_uniq; /* uniquifier */
65 } pos_t;
66
67 /* Copies curr_pos, keeping things unique. */
68 #define UNIQUE_CURR_POS(pos) \
69 do { \
70 (pos) = curr_pos; \
71 curr_pos.p_uniq++; \
72 if (curr_pos.p_file == csrc_pos.p_file) \
73 csrc_pos.p_uniq++; \
74 } while (/*CONSTCOND*/false)
75
76 /*
77 * Strings cannot be referenced to simply by a pointer to its first
78 * char. This is because strings can contain NUL characters other than the
79 * trailing NUL.
80 *
81 * Strings are stored with a trailing NUL.
82 */
83 typedef struct strg {
84 tspec_t st_tspec; /* CHAR or WCHAR */
85 size_t st_len; /* length without trailing NUL */
86 union {
87 u_char *_st_cp;
88 wchar_t *_st_wcp;
89 } st_u;
90 } strg_t;
91
92 #define st_cp st_u._st_cp
93 #define st_wcp st_u._st_wcp
94
95 /*
96 * qualifiers (only for lex/yacc interface)
97 */
98 typedef enum {
99 CONST, VOLATILE, RESTRICT, THREAD
100 } tqual_t;
101
102 /*
103 * Integer and floating point values are stored in this structure
104 */
105 typedef struct {
106 tspec_t v_tspec;
107 bool v_ansiu; /* set if an integer constant is
108 unsigned in ANSI C */
109 union {
110 int64_t _v_quad; /* integers */
111 ldbl_t _v_ldbl; /* floats */
112 } v_u;
113 } val_t;
114
115 #define v_quad v_u._v_quad
116 #define v_ldbl v_u._v_ldbl
117
118 /*
119 * Structures of type struct_or_union uniquely identify structures. This can't
120 * be done in structures of type type_t, because these are copied
121 * if they must be modified. So it would not be possible to check
122 * if two structures are identical by comparing the pointers to
123 * the type structures.
124 *
125 * The typename is used if the structure is unnamed to identify
126 * the structure type in pass 2.
127 */
128 typedef struct {
129 u_int sou_size_in_bits;
130 u_int sou_align_in_bits : 15;
131 bool sou_incomplete : 1;
132 struct sym *sou_first_member;
133 struct sym *sou_tag;
134 struct sym *sou_first_typedef;
135 } struct_or_union;
136
137 /*
138 * same as above for enums
139 */
140 typedef struct {
141 bool en_incomplete : 1;
142 struct sym *en_first_enumerator;
143 struct sym *en_tag;
144 struct sym *en_first_typedef;
145 } enumeration;
146
147 /*
148 * Types are represented by concatenation of structures of type type_t
149 * via t_subt.
150 */
151 struct type {
152 tspec_t t_tspec; /* type specifier */
153 bool t_incomplete_array : 1;
154 bool t_const : 1; /* const modifier */
155 bool t_volatile : 1; /* volatile modifier */
156 bool t_proto : 1; /* function prototype (t_args valid) */
157 bool t_vararg : 1; /* prototype with '...' */
158 bool t_typedef : 1; /* type defined with typedef */
159 bool t_bitfield : 1;
160 bool t_is_enum : 1; /* type is (or was) enum (t_enum valid) */
161 bool t_packed : 1;
162 union {
163 int _t_dim; /* dimension (if ARRAY) */
164 struct_or_union *_t_str;
165 enumeration *_t_enum;
166 struct sym *_t_args; /* arguments (if t_proto) */
167 } t_u;
168 struct {
169 u_int _t_flen : 8; /* length of bit-field */
170 u_int _t_foffs : 24; /* offset of bit-field */
171 } t_b;
172 struct type *t_subt; /* element type (arrays), return value
173 (functions), or type pointer points to */
174 };
175
176 #define t_dim t_u._t_dim
177 #define t_str t_u._t_str
178 #define t_enum t_u._t_enum
179 #define t_args t_u._t_args
180 #define t_flen t_b._t_flen
181 #define t_foffs t_b._t_foffs
182
183 /*
184 * types of symbols
185 */
186 typedef enum {
187 FVFT, /* variables, functions, type names, enums */
188 FMEMBER, /* members of structs or unions */
189 FTAG, /* tags */
190 FLABEL /* labels */
191 } symt_t;
192
193 /*
194 * storage classes
195 */
196 typedef enum {
197 NOSCL,
198 EXTERN, /* external symbols (indep. of decl_t) */
199 STATIC, /* static symbols (local and global) */
200 AUTO, /* automatic symbols (except register) */
201 REG, /* register */
202 TYPEDEF, /* typedef */
203 STRUCT_TAG,
204 UNION_TAG,
205 ENUM_TAG,
206 MOS, /* member of struct */
207 MOU, /* member of union */
208 CTCONST, /* enumerator, enum constant or bool constant */
209 ABSTRACT, /* abstract symbol (sizeof, casts, unnamed argument) */
210 ARG, /* argument */
211 PROTO_ARG, /* used in declaration stack during prototype
212 declaration */
213 INLINE /* only used by the parser */
214 } scl_t;
215
216 /*
217 * symbol table entry
218 */
219 typedef struct sym {
220 const char *s_name;
221 const char *s_rename; /* renamed symbol's given name */
222 pos_t s_def_pos; /* position of last (prototype) definition,
223 prototype declaration, no-prototype-def.,
224 tentative definition or declaration,
225 in this order */
226 pos_t s_set_pos; /* position of first initialization */
227 pos_t s_use_pos; /* position of first use */
228 symt_t s_kind; /* type of symbol */
229 void *s_keyword;
230 bool s_bitfield : 1;
231 bool s_set : 1; /* variable set, label defined */
232 bool s_used : 1; /* variable/label used */
233 bool s_arg : 1; /* symbol is function argument */
234 bool s_reg : 1; /* symbol is register variable */
235 bool s_defarg : 1; /* undefined symbol in old style function
236 definition */
237 bool s_return_type_implicit_int : 1;
238 bool s_osdef : 1; /* symbol stems from old style function def. */
239 bool s_inline : 1; /* true if this is an inline function */
240 struct sym *s_ext_sym; /* for local declared external symbols pointer
241 to external symbol with 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 val_t s_value; /* value (if enum or bool constant) */
248 union {
249 struct_or_union *_s_st;
250 enumeration *_s_et;
251 tspec_t _s_tsp; /* type (only for keywords) */
252 tqual_t _s_tqu; /* qualifier (only for keywords) */
253 struct sym *_s_args; /* arguments in old style function
254 definitions */
255 } u;
256 struct sym *s_link; /* next symbol with same hash value */
257 struct sym **s_rlink; /* pointer to s_link of prev. symbol */
258 struct sym *s_next; /* next struct/union member, enumerator,
259 argument */
260 struct sym *s_dlnxt; /* next symbol declared on same level */
261 } sym_t;
262
263 #define s_styp u._s_st
264 #define s_etyp u._s_et
265 #define s_tspec u._s_tsp
266 #define s_tqual u._s_tqu
267 #define s_args u._s_args
268
269 /*
270 * Used to keep some information about symbols before they are entered
271 * into the symbol table.
272 */
273 typedef struct sbuf {
274 const char *sb_name; /* name of symbol */
275 size_t sb_len; /* length (without '\0') */
276 int sb_hash; /* hash value */
277 sym_t *sb_sym; /* symbol table entry */
278 struct sbuf *sb_next; /* for freelist */
279 } sbuf_t;
280
281
282 /*
283 * tree node
284 */
285 typedef struct tnode {
286 op_t tn_op; /* operator */
287 type_t *tn_type; /* type */
288 bool tn_lvalue : 1; /* node is lvalue */
289 bool tn_cast : 1; /* if tn_op == CVT, it's an explicit cast */
290 bool tn_parenthesized : 1;
291 bool tn_from_system_header : 1;
292 bool tn_system_dependent : 1;
293 union {
294 struct {
295 struct tnode *_tn_left; /* (left) operand */
296 struct tnode *_tn_right; /* right operand */
297 } tn_s;
298 sym_t *_tn_sym; /* symbol if op == NAME */
299 val_t *_tn_val; /* value if op == CON */
300 strg_t *_tn_string; /* string if op == STRING */
301 } tn_u;
302 } tnode_t;
303
304 #define tn_left tn_u.tn_s._tn_left
305 #define tn_right tn_u.tn_s._tn_right
306 #define tn_sym tn_u._tn_sym
307 #define tn_val tn_u._tn_val
308 #define tn_string tn_u._tn_string
309
310 /*
311 * For nested declarations a stack exists, which holds all information
312 * needed for the current level. dcs points to the top element of this
313 * stack.
314 *
315 * d_ctx describes the context of the current declaration. Its value is
316 * one of
317 * EXTERN global declarations
318 * MOS or MOU declarations of struct or union members
319 * CTCONST declarations of enums
320 * ARG declaration of arguments in old style function
321 * definitions
322 * PROTO_ARG declaration of arguments in function prototypes
323 * AUTO declaration of local symbols
324 * ABSTRACT abstract declarations (sizeof, casts)
325 *
326 */
327 typedef struct dinfo {
328 tspec_t d_abstract_type;/* VOID, BOOL, CHAR, INT or COMPLEX */
329 tspec_t d_complex_mod; /* FLOAT or DOUBLE */
330 tspec_t d_sign_mod; /* SIGNED or UNSIGN */
331 tspec_t d_rank_mod; /* SHORT, LONG or QUAD */
332 scl_t d_scl; /* storage class */
333 type_t *d_type; /* after deftyp() pointer to the type used
334 for all declarators */
335 sym_t *d_rdcsym; /* redeclared symbol */
336 int d_offset; /* offset of next structure member */
337 int d_stralign; /* alignment required for current structure */
338 scl_t d_ctx; /* context of declaration */
339 bool d_const : 1; /* const in declaration specifiers */
340 bool d_volatile : 1; /* volatile in declaration specifiers */
341 bool d_inline : 1; /* inline in declaration specifiers */
342 bool d_mscl : 1; /* multiple storage classes */
343 bool d_terr : 1; /* invalid type combination */
344 bool d_nedecl : 1; /* if at least one tag is declared */
345 bool d_vararg : 1; /* ... in the current function decl. */
346 bool d_proto : 1; /* current function decl. is prototype */
347 bool d_notyp : 1; /* set if no type specifier was present */
348 bool d_asm : 1; /* set if d_ctx == AUTO and asm() present */
349 bool d_packed : 1;
350 bool d_used : 1;
351 type_t *d_tagtyp; /* tag during member declaration */
352 sym_t *d_fargs; /* list of arguments during function def. */
353 pos_t d_fdpos; /* position of function definition */
354 sym_t *d_dlsyms; /* first symbol declared at this level */
355 sym_t **d_ldlsym; /* points to s_dlnxt in last symbol decl.
356 at this level */
357 sym_t *d_fpsyms; /* symbols defined in prototype */
358 struct dinfo *d_next; /* next level */
359 } dinfo_t;
360
361 /*
362 * Used to collect information about pointers and qualifiers in
363 * declarators.
364 */
365 typedef struct pqinf {
366 int p_pcnt; /* number of asterisks */
367 bool p_const : 1;
368 bool p_volatile : 1;
369 struct pqinf *p_next;
370 } pqinf_t;
371
372 /*
373 * Case values are stored in a list of type clst_t.
374 */
375 typedef struct clst {
376 val_t cl_val;
377 struct clst *cl_next;
378 } clst_t;
379
380 /*
381 * Used to keep information about nested control statements.
382 */
383 typedef struct control_statement {
384 int c_env; /* type of statement (T_IF, ...) */
385 bool c_loop : 1; /* continue && break are valid */
386 bool c_switch : 1; /* case && break are valid */
387 bool c_break : 1; /* loop/switch has break */
388 bool c_cont : 1; /* loop has continue */
389 bool c_default : 1; /* switch has default */
390 bool c_infinite : 1; /* break condition always false
391 (for (;;), while (1)) */
392 bool c_rchif : 1; /* end of if-branch reached */
393 bool c_had_return_noval : 1; /* had "return;" */
394 bool c_had_return_value : 1; /* had "return (e);" */
395 type_t *c_swtype; /* type of switch expression */
396 clst_t *c_clst; /* list of case values */
397 struct mbl *c_fexprm; /* saved memory for end of loop
398 expression in for() */
399 tnode_t *c_f3expr; /* end of loop expr in for() */
400 pos_t c_fpos; /* position of end of loop expr */
401 pos_t c_cfpos; /* same for csrc_pos */
402 struct control_statement *c_surrounding;
403 } cstk_t;
404
405 typedef struct {
406 size_t lo; /* inclusive */
407 size_t hi; /* inclusive */
408 } range_t;
409
410 #include "externs1.h"
411
412 #define ERR_SETSIZE 1024
413 #define __NERRBITS (sizeof(unsigned int))
414
415 typedef struct err_set {
416 unsigned int errs_bits[(ERR_SETSIZE + __NERRBITS-1) / __NERRBITS];
417 } err_set;
418
419 #define ERR_SET(n, p) \
420 ((p)->errs_bits[(n)/__NERRBITS] |= (1 << ((n) % __NERRBITS)))
421 #define ERR_CLR(n, p) \
422 ((p)->errs_bits[(n)/__NERRBITS] &= ~(1 << ((n) % __NERRBITS)))
423 #define ERR_ISSET(n, p) \
424 (((p)->errs_bits[(n)/__NERRBITS] & (1 << ((n) % __NERRBITS))) != 0)
425 #define ERR_ZERO(p) (void)memset((p), 0, sizeof(*(p)))
426
427 #define LERROR(fmt, args...) lerror(__FILE__, __LINE__, fmt, ##args)
428
429 #define lint_assert(cond) \
430 do { \
431 if (!(cond)) \
432 assert_failed(__FILE__, __LINE__, __func__, #cond); \
433 } while (/*CONSTCOND*/false)
434
435 #ifdef BLKDEBUG
436 #define ZERO 0xa5
437 #else
438 #define ZERO 0
439 #endif
440
441 extern err_set msgset;
442
443
444 #ifdef DEBUG
445 # include "err-msgs.h"
446
447 /* ARGSUSED */
448 static inline void __attribute__((format(printf, 1, 2)))
449 check_printf(const char *fmt, ...)
450 {
451 }
452
453 # define wrap_check_printf(func, id, args...) \
454 do { \
455 check_printf(__CONCAT(MSG_, id), ##args); \
456 (func)(id, ##args); \
457 } while (/*CONSTCOND*/false)
458
459 # define error(id, args...) wrap_check_printf(error, id, ##args)
460 # define warning(id, args...) wrap_check_printf(warning, id, ##args)
461 # define message(id, args...) wrap_check_printf(message, id, ##args)
462 # define gnuism(id, args...) wrap_check_printf(gnuism, id, ##args)
463 # define c99ism(id, args...) wrap_check_printf(c99ism, id, ##args)
464 #endif
465
466 static inline bool
467 is_nonzero_val(const val_t *val)
468 {
469 return is_floating(val->v_tspec)
470 ? val->v_ldbl != 0.0
471 : val->v_quad != 0;
472 }
473
474 static inline bool
475 constant_is_nonzero(const tnode_t *tn)
476 {
477 lint_assert(tn->tn_op == CON);
478 lint_assert(tn->tn_type->t_tspec == tn->tn_val->v_tspec);
479 return is_nonzero_val(tn->tn_val);
480 }
481