lint1.h revision 1.169 1 1.169 rillig /* $NetBSD: lint1.h,v 1.169 2023/06/29 12:52:06 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.159 rillig /*
40 1.159 rillig * A memory pool collects allocated objects that must be available until:
41 1.159 rillig * - the end of a block,
42 1.159 rillig * - the end of an expression, or
43 1.159 rillig * - the end of the translation unit.
44 1.159 rillig */
45 1.159 rillig typedef struct memory_pool {
46 1.159 rillig void **items;
47 1.159 rillig size_t len;
48 1.159 rillig size_t cap;
49 1.159 rillig } memory_pool;
50 1.159 rillig
51 1.154 rillig /* See saved_lwarn in cgram.y. */
52 1.53 rillig #define LWARN_ALL (-2)
53 1.53 rillig #define LWARN_NONE (-1)
54 1.27 christos
55 1.1 cgd /*
56 1.1 cgd * Describes the position of a declaration or anything else.
57 1.96 rillig *
58 1.96 rillig * FIXME: Just a single file:lineno pair is not enough to accurately describe
59 1.96 rillig * the position of a symbol. The whole inclusion path at that point must be
60 1.96 rillig * stored as well. This makes a difference for symbols from included
61 1.96 rillig * headers, see print_stack_trace.
62 1.1 cgd */
63 1.1 cgd typedef struct {
64 1.92 rillig const char *p_file;
65 1.1 cgd int p_line;
66 1.7 cgd int p_uniq; /* uniquifier */
67 1.1 cgd } pos_t;
68 1.7 cgd
69 1.7 cgd /* Copies curr_pos, keeping things unique. */
70 1.34 rillig #define UNIQUE_CURR_POS(pos) \
71 1.34 rillig do { \
72 1.45 rillig (pos) = curr_pos; \
73 1.34 rillig curr_pos.p_uniq++; \
74 1.34 rillig if (curr_pos.p_file == csrc_pos.p_file) \
75 1.34 rillig csrc_pos.p_uniq++; \
76 1.100 rillig } while (false)
77 1.1 cgd
78 1.1 cgd /*
79 1.125 rillig * Strings cannot be referenced simply by a pointer to their first
80 1.1 cgd * char. This is because strings can contain NUL characters other than the
81 1.1 cgd * trailing NUL.
82 1.1 cgd *
83 1.1 cgd * Strings are stored with a trailing NUL.
84 1.1 cgd */
85 1.1 cgd typedef struct strg {
86 1.142 rillig bool st_char; /* string doesn't have an 'L' prefix */
87 1.142 rillig size_t st_len; /* length without trailing NUL */
88 1.142 rillig void *st_mem; /* char[] for st_char, or wchar_t[] */
89 1.1 cgd } strg_t;
90 1.1 cgd
91 1.1 cgd /*
92 1.1 cgd * qualifiers (only for lex/yacc interface)
93 1.1 cgd */
94 1.1 cgd typedef enum {
95 1.161 rillig CONST,
96 1.161 rillig VOLATILE,
97 1.161 rillig RESTRICT,
98 1.161 rillig THREAD, /* XXX: storage-class-qualifier */
99 1.161 rillig ATOMIC,
100 1.1 cgd } tqual_t;
101 1.1 cgd
102 1.104 rillig /* An integer or floating-point value. */
103 1.1 cgd typedef struct {
104 1.1 cgd tspec_t v_tspec;
105 1.105 rillig /*
106 1.105 rillig * Set if an integer constant is unsigned only in C90 and later, but
107 1.105 rillig * not in traditional C.
108 1.105 rillig *
109 1.105 rillig * See the operators table in ops.def, columns "l r".
110 1.105 rillig */
111 1.105 rillig bool v_unsigned_since_c90;
112 1.166 rillig bool v_char_constant;
113 1.1 cgd union {
114 1.165 rillig int64_t _v_quad; /* integers */
115 1.165 rillig long double _v_ldbl; /* floats */
116 1.1 cgd } v_u;
117 1.1 cgd } val_t;
118 1.1 cgd
119 1.1 cgd #define v_quad v_u._v_quad
120 1.1 cgd #define v_ldbl v_u._v_ldbl
121 1.1 cgd
122 1.1 cgd /*
123 1.62 rillig * Structures of type struct_or_union uniquely identify structures. This can't
124 1.1 cgd * be done in structures of type type_t, because these are copied
125 1.1 cgd * if they must be modified. So it would not be possible to check
126 1.29 dholland * if two structures are identical by comparing the pointers to
127 1.1 cgd * the type structures.
128 1.1 cgd *
129 1.1 cgd * The typename is used if the structure is unnamed to identify
130 1.1 cgd * the structure type in pass 2.
131 1.1 cgd */
132 1.1 cgd typedef struct {
133 1.126 rillig unsigned int sou_size_in_bits;
134 1.126 rillig unsigned short sou_align_in_bits;
135 1.132 rillig bool sou_incomplete:1;
136 1.62 rillig struct sym *sou_first_member;
137 1.62 rillig struct sym *sou_tag;
138 1.62 rillig struct sym *sou_first_typedef;
139 1.62 rillig } struct_or_union;
140 1.1 cgd
141 1.1 cgd /*
142 1.1 cgd * same as above for enums
143 1.1 cgd */
144 1.1 cgd typedef struct {
145 1.132 rillig bool en_incomplete:1;
146 1.63 rillig struct sym *en_first_enumerator;
147 1.63 rillig struct sym *en_tag;
148 1.63 rillig struct sym *en_first_typedef;
149 1.63 rillig } enumeration;
150 1.1 cgd
151 1.1 cgd /*
152 1.93 rillig * The type of an expression or object. Complex types are formed via t_subt
153 1.164 rillig * (for arrays, pointers and functions), as well as t_sou.
154 1.1 cgd */
155 1.94 rillig struct lint1_type {
156 1.1 cgd tspec_t t_tspec; /* type specifier */
157 1.132 rillig bool t_incomplete_array:1;
158 1.132 rillig bool t_const:1; /* const modifier */
159 1.132 rillig bool t_volatile:1; /* volatile modifier */
160 1.132 rillig bool t_proto:1; /* function prototype (t_args valid) */
161 1.132 rillig bool t_vararg:1; /* prototype with '...' */
162 1.132 rillig bool t_typedef:1; /* type defined with typedef */
163 1.151 rillig bool t_typeof:1; /* type defined with GCC's __typeof__ */
164 1.132 rillig bool t_bitfield:1;
165 1.109 rillig /*
166 1.109 rillig * Either the type is currently an enum (having t_tspec ENUM), or
167 1.109 rillig * it is an integer type (typically INT) that has been implicitly
168 1.109 rillig * converted from an enum type. In both cases, t_enum is valid.
169 1.109 rillig *
170 1.109 rillig * The information about a former enum type is retained to allow
171 1.109 rillig * type checks in expressions such as ((var1 & 0x0001) == var2), to
172 1.109 rillig * detect when var1 and var2 are from incompatible enum types.
173 1.109 rillig */
174 1.132 rillig bool t_is_enum:1;
175 1.132 rillig bool t_packed:1;
176 1.1 cgd union {
177 1.74 rillig int _t_dim; /* dimension (if ARRAY) */
178 1.164 rillig struct_or_union *_t_sou;
179 1.63 rillig enumeration *_t_enum;
180 1.1 cgd struct sym *_t_args; /* arguments (if t_proto) */
181 1.1 cgd } t_u;
182 1.169 rillig unsigned int t_bit_field_width:8;
183 1.169 rillig unsigned int t_bit_field_offset:24;
184 1.169 rillig struct lint1_type *t_subt; /* element type (if ARRAY),
185 1.169 rillig * return value (if FUNC),
186 1.169 rillig * target type (if PTR) */
187 1.17 christos };
188 1.1 cgd
189 1.1 cgd #define t_dim t_u._t_dim
190 1.164 rillig #define t_sou t_u._t_sou
191 1.1 cgd #define t_enum t_u._t_enum
192 1.1 cgd #define t_args t_u._t_args
193 1.1 cgd
194 1.1 cgd /*
195 1.1 cgd * types of symbols
196 1.1 cgd */
197 1.1 cgd typedef enum {
198 1.1 cgd FVFT, /* variables, functions, type names, enums */
199 1.39 rillig FMEMBER, /* members of structs or unions */
200 1.1 cgd FTAG, /* tags */
201 1.39 rillig FLABEL /* labels */
202 1.1 cgd } symt_t;
203 1.1 cgd
204 1.1 cgd /*
205 1.115 rillig * storage classes and related things
206 1.1 cgd */
207 1.1 cgd typedef enum {
208 1.1 cgd NOSCL,
209 1.115 rillig EXTERN, /* external symbols (independent of decl_t) */
210 1.1 cgd STATIC, /* static symbols (local and global) */
211 1.1 cgd AUTO, /* automatic symbols (except register) */
212 1.1 cgd REG, /* register */
213 1.1 cgd TYPEDEF, /* typedef */
214 1.65 rillig STRUCT_TAG,
215 1.65 rillig UNION_TAG,
216 1.65 rillig ENUM_TAG,
217 1.160 rillig STRUCT_MEMBER,
218 1.160 rillig UNION_MEMBER,
219 1.146 rillig BOOL_CONST,
220 1.146 rillig ENUM_CONST,
221 1.1 cgd ABSTRACT, /* abstract symbol (sizeof, casts, unnamed argument) */
222 1.4 jpo INLINE /* only used by the parser */
223 1.1 cgd } scl_t;
224 1.1 cgd
225 1.1 cgd /*
226 1.1 cgd * symbol table entry
227 1.1 cgd */
228 1.1 cgd typedef struct sym {
229 1.33 rillig const char *s_name;
230 1.9 cgd const char *s_rename; /* renamed symbol's given name */
231 1.44 rillig pos_t s_def_pos; /* position of last (prototype) definition,
232 1.39 rillig prototype declaration, no-prototype-def.,
233 1.1 cgd tentative definition or declaration,
234 1.1 cgd in this order */
235 1.66 rillig pos_t s_set_pos; /* position of first initialization */
236 1.44 rillig pos_t s_use_pos; /* position of first use */
237 1.1 cgd symt_t s_kind; /* type of symbol */
238 1.133 rillig const struct keyword *s_keyword;
239 1.132 rillig bool s_bitfield:1;
240 1.132 rillig bool s_set:1; /* variable set, label defined */
241 1.132 rillig bool s_used:1; /* variable/label used */
242 1.132 rillig bool s_arg:1; /* symbol is function argument */
243 1.141 rillig bool s_register:1; /* symbol is register variable */
244 1.158 rillig bool s_defarg:1; /* undefined symbol in old-style function
245 1.1 cgd definition */
246 1.132 rillig bool s_return_type_implicit_int:1;
247 1.158 rillig bool s_osdef:1; /* symbol stems from old-style function def. */
248 1.132 rillig bool s_inline:1; /* true if this is an inline function */
249 1.141 rillig struct sym *s_ext_sym; /* for locally declared external symbols, the
250 1.141 rillig * pointer to the external symbol with the
251 1.141 rillig * same name */
252 1.1 cgd def_t s_def; /* declared, tentative defined, defined */
253 1.1 cgd scl_t s_scl; /* storage class */
254 1.71 rillig int s_block_level; /* level of declaration, -1 if not in symbol
255 1.1 cgd table */
256 1.33 rillig type_t *s_type;
257 1.1 cgd union {
258 1.147 rillig bool s_bool_constant;
259 1.147 rillig int s_enum_constant; /* XXX: should be TARG_INT */
260 1.147 rillig struct {
261 1.164 rillig /* XXX: what is the difference to s_type->t_sou? */
262 1.147 rillig struct_or_union *sm_sou_type;
263 1.147 rillig unsigned int sm_offset_in_bits;
264 1.147 rillig } s_member;
265 1.147 rillig struct {
266 1.147 rillig int sk_token;
267 1.147 rillig tspec_t sk_tspec; /* only for types */
268 1.147 rillig tqual_t sk_qualifier; /* only for qualifiers */
269 1.147 rillig } s_keyword;
270 1.147 rillig struct sym *s_old_style_args; /* arguments in an old-style
271 1.147 rillig * function definition */
272 1.1 cgd } u;
273 1.139 rillig struct sym *s_symtab_next; /* next symbol with same hash value */
274 1.139 rillig struct sym **s_symtab_ref; /* pointer to s_symtab_next of the
275 1.139 rillig * previous symbol */
276 1.38 rillig struct sym *s_next; /* next struct/union member, enumerator,
277 1.1 cgd argument */
278 1.139 rillig struct sym *s_level_next; /* next symbol declared on the same
279 1.139 rillig * level */
280 1.1 cgd } sym_t;
281 1.1 cgd
282 1.1 cgd /*
283 1.35 rillig * Used to keep some information about symbols before they are entered
284 1.1 cgd * into the symbol table.
285 1.1 cgd */
286 1.1 cgd typedef struct sbuf {
287 1.1 cgd const char *sb_name; /* name of symbol */
288 1.1 cgd size_t sb_len; /* length (without '\0') */
289 1.1 cgd sym_t *sb_sym; /* symbol table entry */
290 1.1 cgd } sbuf_t;
291 1.1 cgd
292 1.1 cgd
293 1.1 cgd /*
294 1.1 cgd * tree node
295 1.1 cgd */
296 1.1 cgd typedef struct tnode {
297 1.1 cgd op_t tn_op; /* operator */
298 1.1 cgd type_t *tn_type; /* type */
299 1.132 rillig bool tn_lvalue:1; /* node is lvalue */
300 1.132 rillig bool tn_cast:1; /* if tn_op == CVT, it's an explicit cast */
301 1.132 rillig bool tn_parenthesized:1;
302 1.132 rillig bool tn_sys:1; /* in strict bool mode, allow mixture between
303 1.131 rillig * bool and scalar, for code from system
304 1.131 rillig * headers that may be a mixture between
305 1.131 rillig * scalar types and bool
306 1.110 rillig */
307 1.132 rillig bool tn_system_dependent:1; /* depends on sizeof or offsetof */
308 1.1 cgd union {
309 1.1 cgd struct {
310 1.1 cgd struct tnode *_tn_left; /* (left) operand */
311 1.1 cgd struct tnode *_tn_right; /* right operand */
312 1.1 cgd } tn_s;
313 1.1 cgd sym_t *_tn_sym; /* symbol if op == NAME */
314 1.167 rillig val_t _tn_val; /* value if op == CON */
315 1.42 rillig strg_t *_tn_string; /* string if op == STRING */
316 1.1 cgd } tn_u;
317 1.1 cgd } tnode_t;
318 1.1 cgd
319 1.98 rillig #define tn_left tn_u.tn_s._tn_left
320 1.98 rillig #define tn_right tn_u.tn_s._tn_right
321 1.98 rillig #define tn_sym tn_u._tn_sym
322 1.98 rillig #define tn_val tn_u._tn_val
323 1.42 rillig #define tn_string tn_u._tn_string
324 1.1 cgd
325 1.112 rillig struct generic_association {
326 1.112 rillig type_t *ga_arg; /* NULL means default or error */
327 1.112 rillig tnode_t *ga_result; /* NULL means error */
328 1.112 rillig struct generic_association *ga_prev;
329 1.107 rillig };
330 1.107 rillig
331 1.129 rillig struct array_size {
332 1.129 rillig bool has_dim;
333 1.129 rillig int dim;
334 1.129 rillig };
335 1.129 rillig
336 1.150 rillig typedef enum declaration_kind {
337 1.150 rillig DK_EXTERN, /* global variable or function */
338 1.160 rillig DK_STRUCT_MEMBER,
339 1.160 rillig DK_UNION_MEMBER,
340 1.160 rillig DK_ENUM_CONSTANT,
341 1.150 rillig DK_OLD_STYLE_ARG, /* argument in an old-style function
342 1.150 rillig * definition */
343 1.150 rillig DK_PROTO_ARG, /* argument in a prototype function
344 1.150 rillig * definition */
345 1.150 rillig DK_AUTO, /* local symbol */
346 1.150 rillig DK_ABSTRACT /* abstract declaration; type name */
347 1.150 rillig } declaration_kind;
348 1.150 rillig
349 1.1 cgd /*
350 1.136 rillig * For nested declarations there is a stack that holds all information
351 1.76 rillig * needed for the current level. dcs points to the innermost element of this
352 1.1 cgd * stack.
353 1.1 cgd */
354 1.1 cgd typedef struct dinfo {
355 1.150 rillig declaration_kind d_kind;
356 1.72 rillig tspec_t d_abstract_type;/* VOID, BOOL, CHAR, INT or COMPLEX */
357 1.72 rillig tspec_t d_complex_mod; /* FLOAT or DOUBLE */
358 1.72 rillig tspec_t d_sign_mod; /* SIGNED or UNSIGN */
359 1.72 rillig tspec_t d_rank_mod; /* SHORT, LONG or QUAD */
360 1.3 jpo scl_t d_scl; /* storage class */
361 1.157 rillig type_t *d_type; /* after dcs_end_type pointer to the type used
362 1.1 cgd for all declarators */
363 1.78 rillig sym_t *d_redeclared_symbol;
364 1.149 rillig unsigned int d_offset_in_bits; /* offset of next structure member */
365 1.126 rillig unsigned short d_sou_align_in_bits; /* alignment required for current
366 1.111 rillig * structure */
367 1.132 rillig bool d_const:1; /* const in declaration specifiers */
368 1.132 rillig bool d_volatile:1; /* volatile in declaration specifiers */
369 1.132 rillig bool d_inline:1; /* inline in declaration specifiers */
370 1.157 rillig bool d_multiple_storage_classes:1; /* reported in dcs_end_type */
371 1.132 rillig bool d_invalid_type_combination:1;
372 1.132 rillig bool d_nonempty_decl:1; /* if at least one tag is declared
373 1.79 rillig * ... in the current function decl. */
374 1.132 rillig bool d_vararg:1;
375 1.165 rillig bool d_proto:1; /* current function decl. is a prototype */
376 1.132 rillig bool d_notyp:1; /* set if no type specifier was present */
377 1.132 rillig bool d_asm:1; /* set if d_ctx == AUTO and asm() present */
378 1.132 rillig bool d_packed:1;
379 1.132 rillig bool d_used:1;
380 1.3 jpo type_t *d_tagtyp; /* tag during member declaration */
381 1.80 rillig sym_t *d_func_args; /* list of arguments during function def. */
382 1.80 rillig pos_t d_func_def_pos; /* position of function definition */
383 1.3 jpo sym_t *d_dlsyms; /* first symbol declared at this level */
384 1.139 rillig sym_t **d_ldlsym; /* points to s_level_next in the last symbol
385 1.139 rillig declaration at this level */
386 1.80 rillig sym_t *d_func_proto_syms; /* symbols defined in prototype */
387 1.143 rillig struct dinfo *d_enclosing; /* the enclosing declaration level */
388 1.1 cgd } dinfo_t;
389 1.1 cgd
390 1.108 rillig /* One level of pointer indirection in declarators, including qualifiers. */
391 1.108 rillig typedef struct qual_ptr {
392 1.106 rillig bool p_const: 1;
393 1.106 rillig bool p_volatile: 1;
394 1.108 rillig bool p_pointer: 1;
395 1.108 rillig struct qual_ptr *p_next;
396 1.108 rillig } qual_ptr;
397 1.1 cgd
398 1.1 cgd /*
399 1.93 rillig * The values of the 'case' labels, linked via cl_next in reverse order of
400 1.93 rillig * appearance in the code, that is from bottom to top.
401 1.1 cgd */
402 1.77 rillig typedef struct case_label {
403 1.1 cgd val_t cl_val;
404 1.77 rillig struct case_label *cl_next;
405 1.77 rillig } case_label_t;
406 1.1 cgd
407 1.88 rillig typedef enum {
408 1.88 rillig CS_DO_WHILE,
409 1.88 rillig CS_FOR,
410 1.88 rillig CS_FUNCTION_BODY,
411 1.88 rillig CS_IF,
412 1.88 rillig CS_SWITCH,
413 1.88 rillig CS_WHILE
414 1.88 rillig } control_statement_kind;
415 1.88 rillig
416 1.1 cgd /*
417 1.35 rillig * Used to keep information about nested control statements.
418 1.1 cgd */
419 1.52 rillig typedef struct control_statement {
420 1.88 rillig control_statement_kind c_kind; /* to ensure proper nesting */
421 1.132 rillig bool c_loop:1; /* 'continue' and 'break' are valid */
422 1.132 rillig bool c_switch:1; /* 'case' and 'break' are valid */
423 1.132 rillig bool c_break:1; /* the loop/switch has a reachable
424 1.89 rillig * 'break' statement */
425 1.132 rillig bool c_continue:1; /* the loop has a reachable 'continue'
426 1.89 rillig * statement */
427 1.132 rillig bool c_default:1; /* the switch has a 'default' label */
428 1.132 rillig bool c_maybe_endless:1; /* the controlling expression is
429 1.82 rillig * always true (as in 'for (;;)' or
430 1.82 rillig * 'while (1)'), there may be break
431 1.82 rillig * statements though */
432 1.132 rillig bool c_always_then:1;
433 1.132 rillig bool c_reached_end_of_then:1;
434 1.132 rillig bool c_had_return_noval:1; /* had "return;" */
435 1.132 rillig bool c_had_return_value:1; /* had "return expr;" */
436 1.89 rillig
437 1.89 rillig type_t *c_switch_type; /* type of switch expression */
438 1.101 rillig tnode_t *c_switch_expr;
439 1.77 rillig case_label_t *c_case_labels; /* list of case values */
440 1.89 rillig
441 1.159 rillig memory_pool c_for_expr3_mem; /* saved memory for end of loop
442 1.89 rillig * expression in for() */
443 1.89 rillig tnode_t *c_for_expr3; /* end of loop expr in for() */
444 1.89 rillig pos_t c_for_expr3_pos; /* position of end of loop expr */
445 1.89 rillig pos_t c_for_expr3_csrc_pos; /* same for csrc_pos */
446 1.89 rillig
447 1.118 rillig struct control_statement *c_surrounding;
448 1.118 rillig } control_statement;
449 1.1 cgd
450 1.18 christos typedef struct {
451 1.70 rillig size_t lo; /* inclusive */
452 1.70 rillig size_t hi; /* inclusive */
453 1.18 christos } range_t;
454 1.18 christos
455 1.1 cgd #include "externs1.h"
456 1.11 augustss
457 1.37 rillig #define lint_assert(cond) \
458 1.37 rillig do { \
459 1.37 rillig if (!(cond)) \
460 1.37 rillig assert_failed(__FILE__, __LINE__, __func__, #cond); \
461 1.100 rillig } while (false)
462 1.37 rillig
463 1.49 rillig #ifdef DEBUG
464 1.49 rillig # include "err-msgs.h"
465 1.49 rillig
466 1.49 rillig /* ARGSUSED */
467 1.163 rillig static inline void __printflike(1, 2)
468 1.49 rillig check_printf(const char *fmt, ...)
469 1.49 rillig {
470 1.49 rillig }
471 1.49 rillig
472 1.99 rillig # define wrap_check_printf_at(func, msgid, pos, args...) \
473 1.97 rillig do { \
474 1.99 rillig check_printf(__CONCAT(MSG_, msgid), ##args); \
475 1.99 rillig (func)(msgid, pos, ##args); \
476 1.100 rillig } while (false)
477 1.97 rillig
478 1.99 rillig # define error_at(msgid, pos, args...) \
479 1.99 rillig wrap_check_printf_at(error_at, msgid, pos, ##args)
480 1.99 rillig # define warning_at(msgid, pos, args...) \
481 1.99 rillig wrap_check_printf_at(warning_at, msgid, pos, ##args)
482 1.99 rillig # define message_at(msgid, pos, args...) \
483 1.99 rillig wrap_check_printf_at(message_at, msgid, pos, ##args)
484 1.97 rillig
485 1.168 rillig # define wrap_check_printf(func, cond, msgid, args...) \
486 1.152 rillig ({ \
487 1.168 rillig if (/* CONSTCOND */cond) \
488 1.168 rillig debug_step("%s:%d: %s %d '%s' in %s", \
489 1.168 rillig __FILE__, __LINE__, #func, msgid, \
490 1.168 rillig __CONCAT(MSG_, msgid), __func__); \
491 1.99 rillig check_printf(__CONCAT(MSG_, msgid), ##args); \
492 1.99 rillig (func)(msgid, ##args); \
493 1.152 rillig /* LINTED 129 */ \
494 1.152 rillig })
495 1.49 rillig
496 1.168 rillig # define error(msgid, args...) wrap_check_printf(error, true, msgid, ##args)
497 1.168 rillig # define warning(msgid, args...) wrap_check_printf(warning, true, msgid, ##args)
498 1.168 rillig # define gnuism(msgid, args...) wrap_check_printf(gnuism, !allow_gcc || (!allow_trad && !allow_c99), msgid, ##args)
499 1.168 rillig # define c99ism(msgid, args...) wrap_check_printf(c99ism, !allow_c99 && (!allow_gcc || !allow_trad), msgid, ##args)
500 1.168 rillig # define c11ism(msgid, args...) wrap_check_printf(c11ism, !allow_c11 && !allow_gcc, msgid, ##args)
501 1.49 rillig #endif
502 1.54 rillig
503 1.156 rillig #ifdef DEBUG
504 1.156 rillig # define query_message(query_id, args...) \
505 1.156 rillig do { \
506 1.168 rillig debug_step("%s:%d: query %d '%s' in %s", \
507 1.168 rillig __FILE__, __LINE__, \
508 1.168 rillig query_id, __CONCAT(MSG_Q, query_id), __func__); \
509 1.156 rillig check_printf(__CONCAT(MSG_Q, query_id), ##args); \
510 1.156 rillig (query_message)(query_id, ##args); \
511 1.156 rillig } while (false)
512 1.156 rillig #else
513 1.156 rillig # define query_message(...) \
514 1.156 rillig do { \
515 1.156 rillig if (any_query_enabled) \
516 1.156 rillig (query_message)(__VA_ARGS__); \
517 1.156 rillig } while (false)
518 1.156 rillig #endif
519 1.156 rillig
520 1.135 rillig static inline bool
521 1.69 rillig is_nonzero_val(const val_t *val)
522 1.54 rillig {
523 1.69 rillig return is_floating(val->v_tspec)
524 1.69 rillig ? val->v_ldbl != 0.0
525 1.69 rillig : val->v_quad != 0;
526 1.54 rillig }
527 1.54 rillig
528 1.135 rillig static inline bool
529 1.68 rillig constant_is_nonzero(const tnode_t *tn)
530 1.54 rillig {
531 1.54 rillig lint_assert(tn->tn_op == CON);
532 1.167 rillig lint_assert(tn->tn_type->t_tspec == tn->tn_val.v_tspec);
533 1.167 rillig return is_nonzero_val(&tn->tn_val);
534 1.54 rillig }
535 1.87 rillig
536 1.135 rillig static inline bool
537 1.87 rillig is_zero(const tnode_t *tn)
538 1.87 rillig {
539 1.167 rillig return tn != NULL && tn->tn_op == CON && !is_nonzero_val(&tn->tn_val);
540 1.87 rillig }
541 1.87 rillig
542 1.135 rillig static inline bool
543 1.87 rillig is_nonzero(const tnode_t *tn)
544 1.87 rillig {
545 1.167 rillig return tn != NULL && tn->tn_op == CON && is_nonzero_val(&tn->tn_val);
546 1.87 rillig }
547 1.102 rillig
548 1.135 rillig static inline bool
549 1.123 rillig is_binary(const tnode_t *tn)
550 1.123 rillig {
551 1.123 rillig return modtab[tn->tn_op].m_binary;
552 1.123 rillig }
553 1.123 rillig
554 1.135 rillig static inline uint64_t
555 1.102 rillig bit(unsigned i)
556 1.102 rillig {
557 1.120 rillig /*
558 1.120 rillig * TODO: Add proper support for INT128.
559 1.120 rillig * This involves changing val_t to 128 bits.
560 1.120 rillig */
561 1.120 rillig if (i >= 64)
562 1.120 rillig return 0; /* XXX: not correct for INT128 and UINT128 */
563 1.120 rillig
564 1.102 rillig lint_assert(i < 64);
565 1.102 rillig return (uint64_t)1 << i;
566 1.102 rillig }
567 1.102 rillig
568 1.135 rillig static inline bool
569 1.124 rillig msb(int64_t q, tspec_t t)
570 1.124 rillig {
571 1.127 rillig return (q & bit((unsigned int)size_in_bits(t) - 1)) != 0;
572 1.124 rillig }
573 1.124 rillig
574 1.135 rillig static inline uint64_t
575 1.102 rillig value_bits(unsigned bitsize)
576 1.102 rillig {
577 1.102 rillig lint_assert(bitsize > 0);
578 1.102 rillig
579 1.102 rillig /* for long double (80 or 128), double _Complex (128) */
580 1.102 rillig /*
581 1.102 rillig * XXX: double _Complex does not have 128 bits of precision,
582 1.102 rillig * therefore it should never be necessary to query the value bits
583 1.102 rillig * of such a type; see d_c99_complex_split.c to trigger this case.
584 1.102 rillig */
585 1.102 rillig if (bitsize >= 64)
586 1.102 rillig return ~((uint64_t)0);
587 1.102 rillig
588 1.102 rillig return ~(~(uint64_t)0 << bitsize);
589 1.102 rillig }
590 1.103 rillig
591 1.103 rillig /* C99 6.7.8p7 */
592 1.135 rillig static inline bool
593 1.103 rillig is_struct_or_union(tspec_t t)
594 1.103 rillig {
595 1.103 rillig return t == STRUCT || t == UNION;
596 1.103 rillig }
597 1.148 rillig
598 1.148 rillig static inline bool
599 1.148 rillig is_member(const sym_t *sym)
600 1.148 rillig {
601 1.160 rillig return sym->s_scl == STRUCT_MEMBER || sym->s_scl == UNION_MEMBER;
602 1.148 rillig }
603