lint1.h revision 1.29 1 /* $NetBSD: lint1.h,v 1.29 2014/07/20 23:00:49 dholland 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 "op.h"
37
38 /*
39 * XXX - Super conservative so that works for most systems, but we should
40 * not depend on the host settings but the target settings in determining
41 * the alignment. The only valid use for this is in mem1.c; uses in decl.c
42 * are bogus.
43 */
44 #ifndef WORST_ALIGN
45 #ifdef _LP64
46 # define AVAL 15
47 #else
48 # define AVAL 7
49 #endif
50 #define WORST_ALIGN(x) (((x) + AVAL) & ~AVAL)
51 #endif
52
53 #define LWARN_BAD -3
54 #define LWARN_ALL -2
55 #define LWARN_NONE -1
56
57 /*
58 * Describes the position of a declaration or anything else.
59 */
60 typedef struct {
61 int p_line;
62 const char *p_file;
63 int p_uniq; /* uniquifier */
64 } pos_t;
65
66 /* Copies curr_pos, keeping things unique. */
67 #define UNIQUE_CURR_POS(pos) \
68 do { \
69 STRUCT_ASSIGN((pos), curr_pos); \
70 curr_pos.p_uniq++; \
71 if (curr_pos.p_file == csrc_pos.p_file) \
72 csrc_pos.p_uniq++; \
73 } while (0)
74
75 /*
76 * Strings cannot be referenced to simply by a pointer to its first
77 * char. This is because strings can contain NUL characters other than the
78 * trailing NUL.
79 *
80 * Strings are stored with a trailing NUL.
81 */
82 typedef struct strg {
83 tspec_t st_tspec; /* CHAR or WCHAR */
84 size_t st_len; /* length without trailing NUL */
85 union {
86 u_char *_st_cp;
87 wchar_t *_st_wcp;
88 } st_u;
89 } strg_t;
90
91 #define st_cp st_u._st_cp
92 #define st_wcp st_u._st_wcp
93
94 /*
95 * qualifiers (only for lex/yacc interface)
96 */
97 typedef enum {
98 CONST, VOLATILE, RESTRICT
99 } tqual_t;
100
101 /*
102 * Integer and floating point values are stored in this structure
103 */
104 typedef struct {
105 tspec_t v_tspec;
106 int v_ansiu; /* set if an integer constant is
107 unsigned in ANSI C */
108 union {
109 int64_t _v_quad; /* integers */
110 ldbl_t _v_ldbl; /* floats */
111 } v_u;
112 } val_t;
113
114 #define v_quad v_u._v_quad
115 #define v_ldbl v_u._v_ldbl
116
117 /*
118 * Structures of type str_t uniqely identify structures. This can't
119 * be done in structures of type type_t, because these are copied
120 * if they must be modified. So it would not be possible to check
121 * if two structures are identical by comparing the pointers to
122 * the type structures.
123 *
124 * The typename is used if the structure is unnamed to identify
125 * the structure type in pass 2.
126 */
127 typedef struct {
128 u_int size; /* size in bit */
129 u_int align : 15; /* alignment in bit */
130 u_int sincompl : 1; /* set if incomplete type */
131 struct sym *memb; /* list of members */
132 struct sym *stag; /* symbol table entry of tag */
133 struct sym *stdef; /* symbol table entry of first typename */
134 } str_t;
135
136 /*
137 * same as above for enums
138 */
139 typedef struct {
140 u_int eincompl : 1; /* incomplete enum type */
141 struct sym *elem; /* list of enumerators */
142 struct sym *etag; /* symbol table entry of tag */
143 struct sym *etdef; /* symbol table entry of first typename */
144 } tenum_t;
145
146 /*
147 * Types are represented by concatenation of structures of type type_t
148 * via t_subt.
149 */
150 struct type {
151 tspec_t t_tspec; /* type specifier */
152 u_int t_aincompl : 1; /* incomplete array type */
153 u_int t_const : 1; /* const modifier */
154 u_int t_volatile : 1; /* volatile modifier */
155 u_int t_proto : 1; /* function prototype (t_args valid) */
156 u_int t_vararg : 1; /* prototype with ... */
157 u_int t_typedef : 1; /* type defined with typedef */
158 u_int t_isfield : 1; /* type is bitfield */
159 u_int t_isenum : 1; /* type is (or was) enum (t_enum valid) */
160 u_int t_ispacked : 1; /* type is packed */
161 union {
162 int _t_dim; /* dimension */
163 str_t *_t_str; /* struct/union tag */
164 tenum_t *_t_enum; /* enum tag */
165 struct sym *_t_args; /* arguments (if t_proto) */
166 } t_u;
167 struct {
168 u_int _t_flen : 8; /* length of bit-field */
169 u_int _t_foffs : 24; /* offset of bit-field */
170 } t_b;
171 struct type *t_subt; /* element type (arrays), return value
172 (functions), or type pointer points to */
173 };
174
175 #define t_dim t_u._t_dim
176 #define t_str t_u._t_str
177 #define t_field t_u._t_field
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 FMOS, /* members of structs or unions */
189 FTAG, /* tags */
190 FLAB /* 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 STRTAG,
204 UNIONTAG,
205 ENUMTAG,
206 MOS, /* member of struct */
207 MOU, /* member of union */
208 ENUMCON, /* enumerator */
209 ABSTRACT, /* abstract symbol (sizeof, casts, unnamed argument) */
210 ARG, /* argument */
211 PARG, /* 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; /* name */
221 const char *s_rename; /* renamed symbol's given name */
222 pos_t s_dpos; /* position of last (prototype)definition,
223 prototypedeclaration, no-prototype-def.,
224 tentative definition or declaration,
225 in this order */
226 pos_t s_spos; /* position of first initialisation */
227 pos_t s_upos; /* position of first use */
228 symt_t s_kind; /* type of symbol */
229 void *s_keyw; /* keyword */
230 u_int s_field : 1; /* bit-field */
231 u_int s_set : 1; /* variable set, label defined */
232 u_int s_used : 1; /* variable/label used */
233 u_int s_arg : 1; /* symbol is function argument */
234 u_int s_reg : 1; /* symbol is register variable */
235 u_int s_defarg : 1; /* undefined symbol in old style function
236 definition */
237 u_int s_rimpl : 1; /* return value of function implicit decl. */
238 u_int s_osdef : 1; /* symbol stems from old style function def. */
239 u_int s_inline : 1; /* true if this is a inline function */
240 struct sym *s_xsym; /* 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_blklev; /* level of declaration, -1 if not in symbol
245 table */
246 type_t *s_type; /* type */
247 val_t s_value; /* value (if enumcon) */
248 union {
249 str_t *_s_st; /* tag, if it is a struct/union member */
250 tenum_t *_s_et; /* tag, if it is a enumerator */
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_nxt; /* 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 informations 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_nxt; /* 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 u_int tn_lvalue : 1; /* node is lvalue */
289 u_int tn_cast : 1; /* if tn_op == CVT its an explicit cast */
290 u_int tn_parn : 1; /* node parenthesized */
291 union {
292 struct {
293 struct tnode *_tn_left; /* (left) operand */
294 struct tnode *_tn_right; /* right operand */
295 } tn_s;
296 sym_t *_tn_sym; /* symbol if op == NAME */
297 val_t *_tn_val; /* value if op == CON */
298 strg_t *_tn_strg; /* string if op == STRING */
299 } tn_u;
300 } tnode_t;
301
302 #define tn_left tn_u.tn_s._tn_left
303 #define tn_right tn_u.tn_s._tn_right
304 #define tn_sym tn_u._tn_sym
305 #define tn_val tn_u._tn_val
306 #define tn_strg tn_u._tn_strg
307
308 /*
309 * For nested declarations a stack exists, which holds all information
310 * needed for the current level. dcs points to the top element of this
311 * stack.
312 *
313 * ctx describes the context of the current declaration. Its value is
314 * one of
315 * EXTERN global declarations
316 * MOS oder MOU declarations of struct or union members
317 * ENUMCON declarations of enums
318 * ARG declaration of arguments in old style function definitions
319 * PARG declaration of arguments in function prototypes
320 * AUTO declaration of local symbols
321 * ABSTRACT abstract declarations (sizeof, casts)
322 *
323 */
324 typedef struct dinfo {
325 tspec_t d_atyp; /* VOID, CHAR, INT, or COMPLEX */
326 tspec_t d_cmod; /* FLOAT, or DOUBLE */
327 tspec_t d_smod; /* SIGNED or UNSIGN */
328 tspec_t d_lmod; /* SHORT, LONG or QUAD */
329 scl_t d_scl; /* storage class */
330 type_t *d_type; /* after deftyp() pointer to the type used
331 for all declarators */
332 sym_t *d_rdcsym; /* redeclared symbol */
333 int d_offset; /* offset of next structure member */
334 int d_stralign; /* alignment required for current structure */
335 scl_t d_ctx; /* context of declaration */
336 u_int d_const : 1; /* const in declaration specifiers */
337 u_int d_volatile : 1; /* volatile in declaration specifiers */
338 u_int d_inline : 1; /* inline in declaration specifiers */
339 u_int d_mscl : 1; /* multiple storage classes */
340 u_int d_terr : 1; /* invalid type combination */
341 u_int d_nedecl : 1; /* 1 if at least a tag is declared */
342 u_int d_vararg : 1; /* ... in in current function decl. */
343 u_int d_proto : 1; /* current funct. decl. is prototype */
344 u_int d_notyp : 1; /* set if no type specifier was present */
345 u_int d_asm : 1; /* set if d_ctx == AUTO and asm() present */
346 u_int d_ispacked : 1; /* packed */
347 type_t *d_tagtyp; /* tag during member declaration */
348 sym_t *d_fargs; /* list of arguments during function def. */
349 pos_t d_fdpos; /* position of function definition */
350 sym_t *d_dlsyms; /* first symbol declared at this level */
351 sym_t **d_ldlsym; /* points to s_dlnxt in last symbol decl.
352 at this level */
353 sym_t *d_fpsyms; /* symbols defined in prototype */
354 struct dinfo *d_nxt; /* next level */
355 } dinfo_t;
356
357 /*
358 * Type of stack which is used for initialisation of aggregate types.
359 */
360 typedef struct istk {
361 type_t *i_type; /* type of initialisation */
362 type_t *i_subt; /* type of next level */
363 u_int i_brace : 1; /* need } for pop */
364 u_int i_nolimit : 1; /* incomplete array type */
365 u_int i_namedmem : 1; /* has c9x named members */
366 sym_t *i_mem; /* next structure member */
367 int i_cnt; /* # of remaining elements */
368 struct istk *i_nxt; /* previous level */
369 } istk_t;
370
371 /*
372 * Used to collect information about pointers and qualifiers in
373 * declarators.
374 */
375 typedef struct pqinf {
376 int p_pcnt; /* number of asterisks */
377 u_int p_const : 1;
378 u_int p_volatile : 1;
379 struct pqinf *p_nxt;
380 } pqinf_t;
381
382 /*
383 * Case values are stored in a list of type clst_t.
384 */
385 typedef struct clst {
386 val_t cl_val;
387 struct clst *cl_nxt;
388 } clst_t;
389
390 /*
391 * Used to keep informations about nested control statements.
392 */
393 typedef struct cstk {
394 int c_env; /* type of statement (T_IF, ...) */
395 u_int c_loop : 1; /* continue && break are valid */
396 u_int c_switch : 1; /* case && break are valid */
397 u_int c_break : 1; /* loop/switch has break */
398 u_int c_cont : 1; /* loop has continue */
399 u_int c_default : 1; /* switch has default */
400 u_int c_infinite : 1; /* break condition always false
401 (for (;;), while (1)) */
402 u_int c_rchif : 1; /* end of if-branch reached */
403 u_int c_noretval : 1; /* had "return;" */
404 u_int c_retval : 1; /* had "return (e);" */
405 type_t *c_swtype; /* type of switch expression */
406 clst_t *c_clst; /* list of case values */
407 struct mbl *c_fexprm; /* saved memory for end of loop
408 expression in for() */
409 tnode_t *c_f3expr; /* end of loop expr in for() */
410 pos_t c_fpos; /* position of end of loop expr */
411 pos_t c_cfpos; /* same for csrc_pos */
412 struct cstk *c_nxt; /* outer control statement */
413 } cstk_t;
414
415 typedef struct {
416 size_t lo;
417 size_t hi;
418 } range_t;
419
420 #include "externs1.h"
421
422 #define ERR_SETSIZE 1024
423 #define __NERRBITS (sizeof(unsigned int))
424
425 typedef struct err_set {
426 unsigned int errs_bits[(ERR_SETSIZE + __NERRBITS-1) / __NERRBITS];
427 } err_set;
428
429 #define ERR_SET(n, p) \
430 ((p)->errs_bits[(n)/__NERRBITS] |= (1 << ((n) % __NERRBITS)))
431 #define ERR_CLR(n, p) \
432 ((p)->errs_bits[(n)/__NERRBITS] &= ~(1 << ((n) % __NERRBITS)))
433 #define ERR_ISSET(n, p) \
434 ((p)->errs_bits[(n)/__NERRBITS] & (1 << ((n) % __NERRBITS)))
435 #define ERR_ZERO(p) (void)memset((p), 0, sizeof(*(p)))
436
437 #define LERROR(fmt, args...) lerror(__FILE__, __LINE__, fmt, ##args)
438
439 #ifdef BLKDEBUG
440 #define ZERO 0xa5
441 #else
442 #define ZERO 0
443 #endif
444
445 extern err_set msgset;
446