lint1.h revision 1.103 1 1.103 rillig /* $NetBSD: lint1.h,v 1.103 2021/06/19 15:23:57 rillig Exp $ */
2 1.2 cgd
3 1.1 cgd /*
4 1.7 cgd * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
5 1.1 cgd * Copyright (c) 1994, 1995 Jochen Pohl
6 1.1 cgd * All Rights Reserved.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by Jochen Pohl for
19 1.1 cgd * The NetBSD Project.
20 1.1 cgd * 4. The name of the author may not be used to endorse or promote products
21 1.1 cgd * derived from this software without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 1.1 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.1 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.1 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 1.1 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 1.1 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 1.1 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 1.1 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 1.1 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 1.1 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.1 cgd #include "lint.h"
36 1.49 rillig #include "err-msgs.h"
37 1.1 cgd #include "op.h"
38 1.12 tv
39 1.25 christos /*
40 1.25 christos * XXX - Super conservative so that works for most systems, but we should
41 1.25 christos * not depend on the host settings but the target settings in determining
42 1.25 christos * the alignment. The only valid use for this is in mem1.c; uses in decl.c
43 1.25 christos * are bogus.
44 1.25 christos */
45 1.25 christos #ifndef WORST_ALIGN
46 1.25 christos #ifdef _LP64
47 1.25 christos # define AVAL 15
48 1.25 christos #else
49 1.25 christos # define AVAL 7
50 1.25 christos #endif
51 1.25 christos #define WORST_ALIGN(x) (((x) + AVAL) & ~AVAL)
52 1.12 tv #endif
53 1.1 cgd
54 1.53 rillig #define LWARN_BAD (-3)
55 1.53 rillig #define LWARN_ALL (-2)
56 1.53 rillig #define LWARN_NONE (-1)
57 1.27 christos
58 1.1 cgd /*
59 1.1 cgd * Describes the position of a declaration or anything else.
60 1.96 rillig *
61 1.96 rillig * FIXME: Just a single file:lineno pair is not enough to accurately describe
62 1.96 rillig * the position of a symbol. The whole inclusion path at that point must be
63 1.96 rillig * stored as well. This makes a difference for symbols from included
64 1.96 rillig * headers, see print_stack_trace.
65 1.1 cgd */
66 1.1 cgd typedef struct {
67 1.92 rillig const char *p_file;
68 1.1 cgd int p_line;
69 1.7 cgd int p_uniq; /* uniquifier */
70 1.1 cgd } pos_t;
71 1.7 cgd
72 1.7 cgd /* Copies curr_pos, keeping things unique. */
73 1.34 rillig #define UNIQUE_CURR_POS(pos) \
74 1.34 rillig do { \
75 1.45 rillig (pos) = curr_pos; \
76 1.34 rillig curr_pos.p_uniq++; \
77 1.34 rillig if (curr_pos.p_file == csrc_pos.p_file) \
78 1.34 rillig csrc_pos.p_uniq++; \
79 1.100 rillig } while (false)
80 1.1 cgd
81 1.1 cgd /*
82 1.1 cgd * Strings cannot be referenced to simply by a pointer to its first
83 1.1 cgd * char. This is because strings can contain NUL characters other than the
84 1.1 cgd * trailing NUL.
85 1.1 cgd *
86 1.1 cgd * Strings are stored with a trailing NUL.
87 1.1 cgd */
88 1.1 cgd typedef struct strg {
89 1.1 cgd tspec_t st_tspec; /* CHAR or WCHAR */
90 1.1 cgd size_t st_len; /* length without trailing NUL */
91 1.1 cgd union {
92 1.1 cgd u_char *_st_cp;
93 1.1 cgd wchar_t *_st_wcp;
94 1.1 cgd } st_u;
95 1.1 cgd } strg_t;
96 1.1 cgd
97 1.1 cgd #define st_cp st_u._st_cp
98 1.1 cgd #define st_wcp st_u._st_wcp
99 1.1 cgd
100 1.1 cgd /*
101 1.1 cgd * qualifiers (only for lex/yacc interface)
102 1.1 cgd */
103 1.1 cgd typedef enum {
104 1.31 christos CONST, VOLATILE, RESTRICT, THREAD
105 1.1 cgd } tqual_t;
106 1.1 cgd
107 1.1 cgd /*
108 1.1 cgd * Integer and floating point values are stored in this structure
109 1.1 cgd */
110 1.1 cgd typedef struct {
111 1.1 cgd tspec_t v_tspec;
112 1.55 rillig bool v_ansiu; /* set if an integer constant is
113 1.1 cgd unsigned in ANSI C */
114 1.1 cgd union {
115 1.13 thorpej int64_t _v_quad; /* integers */
116 1.1 cgd ldbl_t _v_ldbl; /* floats */
117 1.1 cgd } v_u;
118 1.1 cgd } val_t;
119 1.1 cgd
120 1.1 cgd #define v_quad v_u._v_quad
121 1.1 cgd #define v_ldbl v_u._v_ldbl
122 1.1 cgd
123 1.1 cgd /*
124 1.62 rillig * Structures of type struct_or_union uniquely identify structures. This can't
125 1.1 cgd * be done in structures of type type_t, because these are copied
126 1.1 cgd * if they must be modified. So it would not be possible to check
127 1.29 dholland * if two structures are identical by comparing the pointers to
128 1.1 cgd * the type structures.
129 1.1 cgd *
130 1.1 cgd * The typename is used if the structure is unnamed to identify
131 1.1 cgd * the structure type in pass 2.
132 1.1 cgd */
133 1.1 cgd typedef struct {
134 1.75 rillig u_int sou_size_in_bits;
135 1.75 rillig u_int sou_align_in_bits : 15;
136 1.62 rillig bool sou_incomplete : 1;
137 1.62 rillig struct sym *sou_first_member;
138 1.62 rillig struct sym *sou_tag;
139 1.62 rillig struct sym *sou_first_typedef;
140 1.62 rillig } struct_or_union;
141 1.1 cgd
142 1.1 cgd /*
143 1.1 cgd * same as above for enums
144 1.1 cgd */
145 1.1 cgd typedef struct {
146 1.63 rillig bool en_incomplete : 1;
147 1.63 rillig struct sym *en_first_enumerator;
148 1.63 rillig struct sym *en_tag;
149 1.63 rillig struct sym *en_first_typedef;
150 1.63 rillig } enumeration;
151 1.1 cgd
152 1.1 cgd /*
153 1.93 rillig * The type of an expression or object. Complex types are formed via t_subt
154 1.93 rillig * (for arrays, pointers and functions), as well as t_str.
155 1.1 cgd */
156 1.94 rillig struct lint1_type {
157 1.1 cgd tspec_t t_tspec; /* type specifier */
158 1.64 rillig bool t_incomplete_array : 1;
159 1.47 rillig bool t_const : 1; /* const modifier */
160 1.47 rillig bool t_volatile : 1; /* volatile modifier */
161 1.47 rillig bool t_proto : 1; /* function prototype (t_args valid) */
162 1.58 rillig bool t_vararg : 1; /* prototype with '...' */
163 1.47 rillig bool t_typedef : 1; /* type defined with typedef */
164 1.50 rillig bool t_bitfield : 1;
165 1.64 rillig bool t_is_enum : 1; /* type is (or was) enum (t_enum valid) */
166 1.51 rillig bool t_packed : 1;
167 1.1 cgd union {
168 1.74 rillig int _t_dim; /* dimension (if ARRAY) */
169 1.63 rillig struct_or_union *_t_str;
170 1.63 rillig enumeration *_t_enum;
171 1.1 cgd struct sym *_t_args; /* arguments (if t_proto) */
172 1.1 cgd } t_u;
173 1.8 pk struct {
174 1.8 pk u_int _t_flen : 8; /* length of bit-field */
175 1.8 pk u_int _t_foffs : 24; /* offset of bit-field */
176 1.8 pk } t_b;
177 1.94 rillig struct lint1_type *t_subt; /* element type (if ARRAY),
178 1.94 rillig * return value (if FUNC),
179 1.94 rillig * target type (if PTR) */
180 1.17 christos };
181 1.1 cgd
182 1.1 cgd #define t_dim t_u._t_dim
183 1.1 cgd #define t_str t_u._t_str
184 1.1 cgd #define t_enum t_u._t_enum
185 1.1 cgd #define t_args t_u._t_args
186 1.8 pk #define t_flen t_b._t_flen
187 1.8 pk #define t_foffs t_b._t_foffs
188 1.1 cgd
189 1.1 cgd /*
190 1.1 cgd * types of symbols
191 1.1 cgd */
192 1.1 cgd typedef enum {
193 1.1 cgd FVFT, /* variables, functions, type names, enums */
194 1.39 rillig FMEMBER, /* members of structs or unions */
195 1.1 cgd FTAG, /* tags */
196 1.39 rillig FLABEL /* labels */
197 1.1 cgd } symt_t;
198 1.1 cgd
199 1.1 cgd /*
200 1.1 cgd * storage classes
201 1.1 cgd */
202 1.1 cgd typedef enum {
203 1.1 cgd NOSCL,
204 1.1 cgd EXTERN, /* external symbols (indep. of decl_t) */
205 1.1 cgd STATIC, /* static symbols (local and global) */
206 1.1 cgd AUTO, /* automatic symbols (except register) */
207 1.1 cgd REG, /* register */
208 1.1 cgd TYPEDEF, /* typedef */
209 1.65 rillig STRUCT_TAG,
210 1.65 rillig UNION_TAG,
211 1.65 rillig ENUM_TAG,
212 1.1 cgd MOS, /* member of struct */
213 1.1 cgd MOU, /* member of union */
214 1.56 rillig CTCONST, /* enumerator, enum constant or bool constant */
215 1.1 cgd ABSTRACT, /* abstract symbol (sizeof, casts, unnamed argument) */
216 1.1 cgd ARG, /* argument */
217 1.65 rillig PROTO_ARG, /* used in declaration stack during prototype
218 1.1 cgd declaration */
219 1.4 jpo INLINE /* only used by the parser */
220 1.1 cgd } scl_t;
221 1.1 cgd
222 1.1 cgd /*
223 1.1 cgd * symbol table entry
224 1.1 cgd */
225 1.1 cgd typedef struct sym {
226 1.33 rillig const char *s_name;
227 1.9 cgd const char *s_rename; /* renamed symbol's given name */
228 1.44 rillig pos_t s_def_pos; /* position of last (prototype) definition,
229 1.39 rillig prototype declaration, no-prototype-def.,
230 1.1 cgd tentative definition or declaration,
231 1.1 cgd in this order */
232 1.66 rillig pos_t s_set_pos; /* position of first initialization */
233 1.44 rillig pos_t s_use_pos; /* position of first use */
234 1.1 cgd symt_t s_kind; /* type of symbol */
235 1.43 rillig void *s_keyword;
236 1.47 rillig bool s_bitfield : 1;
237 1.47 rillig bool s_set : 1; /* variable set, label defined */
238 1.47 rillig bool s_used : 1; /* variable/label used */
239 1.47 rillig bool s_arg : 1; /* symbol is function argument */
240 1.47 rillig bool s_reg : 1; /* symbol is register variable */
241 1.47 rillig bool s_defarg : 1; /* undefined symbol in old style function
242 1.1 cgd definition */
243 1.61 rillig bool s_return_type_implicit_int : 1;
244 1.47 rillig bool s_osdef : 1; /* symbol stems from old style function def. */
245 1.47 rillig bool s_inline : 1; /* true if this is an inline function */
246 1.43 rillig struct sym *s_ext_sym; /* for local declared external symbols pointer
247 1.1 cgd to external symbol with same name */
248 1.1 cgd def_t s_def; /* declared, tentative defined, defined */
249 1.1 cgd scl_t s_scl; /* storage class */
250 1.71 rillig int s_block_level; /* level of declaration, -1 if not in symbol
251 1.1 cgd table */
252 1.33 rillig type_t *s_type;
253 1.56 rillig val_t s_value; /* value (if enum or bool constant) */
254 1.1 cgd union {
255 1.62 rillig struct_or_union *_s_st;
256 1.63 rillig enumeration *_s_et;
257 1.1 cgd tspec_t _s_tsp; /* type (only for keywords) */
258 1.1 cgd tqual_t _s_tqu; /* qualifier (only for keywords) */
259 1.1 cgd struct sym *_s_args; /* arguments in old style function
260 1.1 cgd definitions */
261 1.1 cgd } u;
262 1.1 cgd struct sym *s_link; /* next symbol with same hash value */
263 1.1 cgd struct sym **s_rlink; /* pointer to s_link of prev. symbol */
264 1.38 rillig struct sym *s_next; /* next struct/union member, enumerator,
265 1.1 cgd argument */
266 1.34 rillig struct sym *s_dlnxt; /* next symbol declared on same level */
267 1.1 cgd } sym_t;
268 1.1 cgd
269 1.1 cgd #define s_styp u._s_st
270 1.1 cgd #define s_etyp u._s_et
271 1.1 cgd #define s_tspec u._s_tsp
272 1.1 cgd #define s_tqual u._s_tqu
273 1.1 cgd #define s_args u._s_args
274 1.1 cgd
275 1.1 cgd /*
276 1.35 rillig * Used to keep some information about symbols before they are entered
277 1.1 cgd * into the symbol table.
278 1.1 cgd */
279 1.1 cgd typedef struct sbuf {
280 1.1 cgd const char *sb_name; /* name of symbol */
281 1.1 cgd size_t sb_len; /* length (without '\0') */
282 1.1 cgd int sb_hash; /* hash value */
283 1.1 cgd sym_t *sb_sym; /* symbol table entry */
284 1.40 rillig struct sbuf *sb_next; /* for freelist */
285 1.1 cgd } sbuf_t;
286 1.1 cgd
287 1.1 cgd
288 1.1 cgd /*
289 1.1 cgd * tree node
290 1.1 cgd */
291 1.1 cgd typedef struct tnode {
292 1.1 cgd op_t tn_op; /* operator */
293 1.1 cgd type_t *tn_type; /* type */
294 1.47 rillig bool tn_lvalue : 1; /* node is lvalue */
295 1.47 rillig bool tn_cast : 1; /* if tn_op == CVT, it's an explicit cast */
296 1.47 rillig bool tn_parenthesized : 1;
297 1.60 rillig bool tn_from_system_header : 1;
298 1.86 rillig bool tn_system_dependent : 1; /* depends on sizeof or offsetof */
299 1.1 cgd union {
300 1.1 cgd struct {
301 1.1 cgd struct tnode *_tn_left; /* (left) operand */
302 1.1 cgd struct tnode *_tn_right; /* right operand */
303 1.1 cgd } tn_s;
304 1.1 cgd sym_t *_tn_sym; /* symbol if op == NAME */
305 1.1 cgd val_t *_tn_val; /* value if op == CON */
306 1.42 rillig strg_t *_tn_string; /* string if op == STRING */
307 1.1 cgd } tn_u;
308 1.1 cgd } tnode_t;
309 1.1 cgd
310 1.98 rillig #define tn_left tn_u.tn_s._tn_left
311 1.98 rillig #define tn_right tn_u.tn_s._tn_right
312 1.98 rillig #define tn_sym tn_u._tn_sym
313 1.98 rillig #define tn_val tn_u._tn_val
314 1.42 rillig #define tn_string tn_u._tn_string
315 1.1 cgd
316 1.1 cgd /*
317 1.1 cgd * For nested declarations a stack exists, which holds all information
318 1.76 rillig * needed for the current level. dcs points to the innermost element of this
319 1.1 cgd * stack.
320 1.1 cgd *
321 1.73 rillig * d_ctx describes the context of the current declaration. Its value is
322 1.1 cgd * one of
323 1.65 rillig * EXTERN global declarations
324 1.65 rillig * MOS or MOU declarations of struct or union members
325 1.76 rillig * CTCONST declarations of enums or boolean constants
326 1.76 rillig * ARG declaration of arguments in old-style function
327 1.65 rillig * definitions
328 1.65 rillig * PROTO_ARG declaration of arguments in function prototypes
329 1.65 rillig * AUTO declaration of local symbols
330 1.65 rillig * ABSTRACT abstract declarations (sizeof, casts)
331 1.1 cgd *
332 1.1 cgd */
333 1.1 cgd typedef struct dinfo {
334 1.72 rillig tspec_t d_abstract_type;/* VOID, BOOL, CHAR, INT or COMPLEX */
335 1.72 rillig tspec_t d_complex_mod; /* FLOAT or DOUBLE */
336 1.72 rillig tspec_t d_sign_mod; /* SIGNED or UNSIGN */
337 1.72 rillig tspec_t d_rank_mod; /* SHORT, LONG or QUAD */
338 1.3 jpo scl_t d_scl; /* storage class */
339 1.3 jpo type_t *d_type; /* after deftyp() pointer to the type used
340 1.1 cgd for all declarators */
341 1.78 rillig sym_t *d_redeclared_symbol;
342 1.3 jpo int d_offset; /* offset of next structure member */
343 1.3 jpo int d_stralign; /* alignment required for current structure */
344 1.3 jpo scl_t d_ctx; /* context of declaration */
345 1.47 rillig bool d_const : 1; /* const in declaration specifiers */
346 1.47 rillig bool d_volatile : 1; /* volatile in declaration specifiers */
347 1.47 rillig bool d_inline : 1; /* inline in declaration specifiers */
348 1.47 rillig bool d_mscl : 1; /* multiple storage classes */
349 1.47 rillig bool d_terr : 1; /* invalid type combination */
350 1.79 rillig bool d_nonempty_decl : 1; /* if at least one tag is declared
351 1.79 rillig * ... in the current function decl. */
352 1.79 rillig bool d_vararg : 1;
353 1.47 rillig bool d_proto : 1; /* current function decl. is prototype */
354 1.47 rillig bool d_notyp : 1; /* set if no type specifier was present */
355 1.47 rillig bool d_asm : 1; /* set if d_ctx == AUTO and asm() present */
356 1.51 rillig bool d_packed : 1;
357 1.51 rillig bool d_used : 1;
358 1.3 jpo type_t *d_tagtyp; /* tag during member declaration */
359 1.80 rillig sym_t *d_func_args; /* list of arguments during function def. */
360 1.80 rillig pos_t d_func_def_pos; /* position of function definition */
361 1.3 jpo sym_t *d_dlsyms; /* first symbol declared at this level */
362 1.3 jpo sym_t **d_ldlsym; /* points to s_dlnxt in last symbol decl.
363 1.1 cgd at this level */
364 1.80 rillig sym_t *d_func_proto_syms; /* symbols defined in prototype */
365 1.41 rillig struct dinfo *d_next; /* next level */
366 1.1 cgd } dinfo_t;
367 1.1 cgd
368 1.1 cgd /*
369 1.1 cgd * Used to collect information about pointers and qualifiers in
370 1.1 cgd * declarators.
371 1.1 cgd */
372 1.1 cgd typedef struct pqinf {
373 1.1 cgd int p_pcnt; /* number of asterisks */
374 1.47 rillig bool p_const : 1;
375 1.47 rillig bool p_volatile : 1;
376 1.41 rillig struct pqinf *p_next;
377 1.1 cgd } pqinf_t;
378 1.1 cgd
379 1.1 cgd /*
380 1.93 rillig * The values of the 'case' labels, linked via cl_next in reverse order of
381 1.93 rillig * appearance in the code, that is from bottom to top.
382 1.1 cgd */
383 1.77 rillig typedef struct case_label {
384 1.1 cgd val_t cl_val;
385 1.77 rillig struct case_label *cl_next;
386 1.77 rillig } case_label_t;
387 1.1 cgd
388 1.88 rillig typedef enum {
389 1.88 rillig CS_DO_WHILE,
390 1.88 rillig CS_FOR,
391 1.88 rillig CS_FUNCTION_BODY,
392 1.88 rillig CS_IF,
393 1.88 rillig CS_SWITCH,
394 1.88 rillig CS_WHILE
395 1.88 rillig } control_statement_kind;
396 1.88 rillig
397 1.1 cgd /*
398 1.35 rillig * Used to keep information about nested control statements.
399 1.1 cgd */
400 1.52 rillig typedef struct control_statement {
401 1.88 rillig control_statement_kind c_kind; /* to ensure proper nesting */
402 1.89 rillig bool c_loop : 1; /* 'continue' and 'break' are valid */
403 1.89 rillig bool c_switch : 1; /* 'case' and 'break' are valid */
404 1.83 rillig bool c_break : 1; /* the loop/switch has a reachable
405 1.89 rillig * 'break' statement */
406 1.89 rillig bool c_continue : 1; /* the loop has a reachable 'continue'
407 1.89 rillig * statement */
408 1.89 rillig bool c_default : 1; /* the switch has a 'default' label */
409 1.82 rillig bool c_maybe_endless : 1; /* the controlling expression is
410 1.82 rillig * always true (as in 'for (;;)' or
411 1.82 rillig * 'while (1)'), there may be break
412 1.82 rillig * statements though */
413 1.86 rillig bool c_always_then : 1;
414 1.85 rillig bool c_reached_end_of_then : 1;
415 1.59 rillig bool c_had_return_noval : 1; /* had "return;" */
416 1.89 rillig bool c_had_return_value : 1; /* had "return expr;" */
417 1.89 rillig
418 1.89 rillig type_t *c_switch_type; /* type of switch expression */
419 1.101 rillig tnode_t *c_switch_expr;
420 1.77 rillig case_label_t *c_case_labels; /* list of case values */
421 1.89 rillig
422 1.91 rillig struct memory_block *c_for_expr3_mem; /* saved memory for end of loop
423 1.89 rillig * expression in for() */
424 1.89 rillig tnode_t *c_for_expr3; /* end of loop expr in for() */
425 1.89 rillig pos_t c_for_expr3_pos; /* position of end of loop expr */
426 1.89 rillig pos_t c_for_expr3_csrc_pos; /* same for csrc_pos */
427 1.89 rillig
428 1.52 rillig struct control_statement *c_surrounding;
429 1.1 cgd } cstk_t;
430 1.1 cgd
431 1.18 christos typedef struct {
432 1.70 rillig size_t lo; /* inclusive */
433 1.70 rillig size_t hi; /* inclusive */
434 1.18 christos } range_t;
435 1.18 christos
436 1.1 cgd #include "externs1.h"
437 1.11 augustss
438 1.11 augustss #define ERR_SETSIZE 1024
439 1.11 augustss #define __NERRBITS (sizeof(unsigned int))
440 1.11 augustss
441 1.11 augustss typedef struct err_set {
442 1.11 augustss unsigned int errs_bits[(ERR_SETSIZE + __NERRBITS-1) / __NERRBITS];
443 1.11 augustss } err_set;
444 1.11 augustss
445 1.11 augustss #define ERR_SET(n, p) \
446 1.34 rillig ((p)->errs_bits[(n)/__NERRBITS] |= (1 << ((n) % __NERRBITS)))
447 1.11 augustss #define ERR_CLR(n, p) \
448 1.34 rillig ((p)->errs_bits[(n)/__NERRBITS] &= ~(1 << ((n) % __NERRBITS)))
449 1.11 augustss #define ERR_ISSET(n, p) \
450 1.55 rillig (((p)->errs_bits[(n)/__NERRBITS] & (1 << ((n) % __NERRBITS))) != 0)
451 1.11 augustss #define ERR_ZERO(p) (void)memset((p), 0, sizeof(*(p)))
452 1.14 christos
453 1.90 rillig #define INTERNAL_ERROR(fmt, args...) \
454 1.90 rillig internal_error(__FILE__, __LINE__, fmt, ##args)
455 1.11 augustss
456 1.37 rillig #define lint_assert(cond) \
457 1.37 rillig do { \
458 1.37 rillig if (!(cond)) \
459 1.37 rillig assert_failed(__FILE__, __LINE__, __func__, #cond); \
460 1.100 rillig } while (false)
461 1.37 rillig
462 1.28 christos #ifdef BLKDEBUG
463 1.28 christos #define ZERO 0xa5
464 1.28 christos #else
465 1.28 christos #define ZERO 0
466 1.28 christos #endif
467 1.28 christos
468 1.11 augustss extern err_set msgset;
469 1.49 rillig
470 1.49 rillig
471 1.49 rillig #ifdef DEBUG
472 1.49 rillig # include "err-msgs.h"
473 1.49 rillig
474 1.49 rillig /* ARGSUSED */
475 1.49 rillig static inline void __attribute__((format(printf, 1, 2)))
476 1.49 rillig check_printf(const char *fmt, ...)
477 1.49 rillig {
478 1.49 rillig }
479 1.49 rillig
480 1.99 rillig # define wrap_check_printf_at(func, msgid, pos, args...) \
481 1.97 rillig do { \
482 1.99 rillig check_printf(__CONCAT(MSG_, msgid), ##args); \
483 1.99 rillig (func)(msgid, pos, ##args); \
484 1.100 rillig } while (false)
485 1.97 rillig
486 1.99 rillig # define error_at(msgid, pos, args...) \
487 1.99 rillig wrap_check_printf_at(error_at, msgid, pos, ##args)
488 1.99 rillig # define warning_at(msgid, pos, args...) \
489 1.99 rillig wrap_check_printf_at(warning_at, msgid, pos, ##args)
490 1.99 rillig # define message_at(msgid, pos, args...) \
491 1.99 rillig wrap_check_printf_at(message_at, msgid, pos, ##args)
492 1.97 rillig
493 1.99 rillig # define wrap_check_printf(func, msgid, args...) \
494 1.49 rillig do { \
495 1.99 rillig check_printf(__CONCAT(MSG_, msgid), ##args); \
496 1.99 rillig (func)(msgid, ##args); \
497 1.100 rillig } while (false)
498 1.49 rillig
499 1.99 rillig # define error(msgid, args...) wrap_check_printf(error, msgid, ##args)
500 1.99 rillig # define warning(msgid, args...) wrap_check_printf(warning, msgid, ##args)
501 1.99 rillig # define message(msgid, args...) wrap_check_printf(message, msgid, ##args)
502 1.99 rillig # define gnuism(msgid, args...) wrap_check_printf(gnuism, msgid, ##args)
503 1.99 rillig # define c99ism(msgid, args...) wrap_check_printf(c99ism, msgid, ##args)
504 1.99 rillig # define c11ism(msgid, args...) wrap_check_printf(c11ism, msgid, ##args)
505 1.49 rillig #endif
506 1.54 rillig
507 1.54 rillig static inline bool
508 1.69 rillig is_nonzero_val(const val_t *val)
509 1.54 rillig {
510 1.69 rillig return is_floating(val->v_tspec)
511 1.69 rillig ? val->v_ldbl != 0.0
512 1.69 rillig : val->v_quad != 0;
513 1.54 rillig }
514 1.54 rillig
515 1.54 rillig static inline bool
516 1.68 rillig constant_is_nonzero(const tnode_t *tn)
517 1.54 rillig {
518 1.54 rillig lint_assert(tn->tn_op == CON);
519 1.69 rillig lint_assert(tn->tn_type->t_tspec == tn->tn_val->v_tspec);
520 1.69 rillig return is_nonzero_val(tn->tn_val);
521 1.54 rillig }
522 1.87 rillig
523 1.87 rillig static inline bool
524 1.87 rillig is_zero(const tnode_t *tn)
525 1.87 rillig {
526 1.87 rillig return tn != NULL && tn->tn_op == CON && !is_nonzero_val(tn->tn_val);
527 1.87 rillig }
528 1.87 rillig
529 1.87 rillig static inline bool
530 1.87 rillig is_nonzero(const tnode_t *tn)
531 1.87 rillig {
532 1.87 rillig return tn != NULL && tn->tn_op == CON && is_nonzero_val(tn->tn_val);
533 1.87 rillig }
534 1.102 rillig
535 1.102 rillig static inline uint64_t
536 1.102 rillig bit(unsigned i)
537 1.102 rillig {
538 1.102 rillig lint_assert(i < 64);
539 1.102 rillig return (uint64_t)1 << i;
540 1.102 rillig }
541 1.102 rillig
542 1.102 rillig static inline uint64_t
543 1.102 rillig value_bits(unsigned bitsize)
544 1.102 rillig {
545 1.102 rillig lint_assert(bitsize > 0);
546 1.102 rillig
547 1.102 rillig /* for long double (80 or 128), double _Complex (128) */
548 1.102 rillig /*
549 1.102 rillig * XXX: double _Complex does not have 128 bits of precision,
550 1.102 rillig * therefore it should never be necessary to query the value bits
551 1.102 rillig * of such a type; see d_c99_complex_split.c to trigger this case.
552 1.102 rillig */
553 1.102 rillig if (bitsize >= 64)
554 1.102 rillig return ~((uint64_t)0);
555 1.102 rillig
556 1.102 rillig return ~(~(uint64_t)0 << bitsize);
557 1.102 rillig }
558 1.103 rillig
559 1.103 rillig /* C99 6.7.8p7 */
560 1.103 rillig static inline bool
561 1.103 rillig is_struct_or_union(tspec_t t)
562 1.103 rillig {
563 1.103 rillig return t == STRUCT || t == UNION;
564 1.103 rillig }
565