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