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