tree.c revision 1.659 1 /* $NetBSD: tree.c,v 1.659 2024/11/23 00:01:48 rillig Exp $ */
2
3 /*
4 * Copyright (c) 1994, 1995 Jochen Pohl
5 * All Rights Reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Jochen Pohl for
18 * The NetBSD Project.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #if HAVE_NBTOOL_CONFIG_H
35 #include "nbtool_config.h"
36 #endif
37
38 #include <sys/cdefs.h>
39 #if defined(__RCSID)
40 __RCSID("$NetBSD: tree.c,v 1.659 2024/11/23 00:01:48 rillig Exp $");
41 #endif
42
43 #include <float.h>
44 #include <limits.h>
45 #include <math.h>
46 #include <signal.h>
47 #include <stdlib.h>
48 #include <string.h>
49
50 #include "lint1.h"
51
52
53 typedef struct integer_constraints {
54 int64_t smin; /* signed minimum */
55 int64_t smax; /* signed maximum */
56 uint64_t umin; /* unsigned minimum */
57 uint64_t umax; /* unsigned maximum */
58 uint64_t bclr; /* bits that are definitely clear */
59 } integer_constraints;
60
61
62 static int64_t
63 s64_min(int64_t a, int64_t b)
64 {
65 return a < b ? a : b;
66 }
67
68 static int64_t
69 s64_max(int64_t a, int64_t b)
70 {
71 return a > b ? a : b;
72 }
73
74 static uint64_t
75 s64_abs(int64_t x)
76 {
77 return x >= 0 ? (uint64_t)x : -(uint64_t)x;
78 }
79
80 static uint64_t
81 u64_max(uint64_t a, uint64_t b)
82 {
83 return a > b ? a : b;
84 }
85
86 static uint64_t
87 u64_fill_right(uint64_t x)
88 {
89 x |= x >> 1;
90 x |= x >> 2;
91 x |= x >> 4;
92 x |= x >> 8;
93 x |= x >> 16;
94 x |= x >> 32;
95 return x;
96 }
97
98 static bool
99 str_ends_with(const char *haystack, const char *needle)
100 {
101 size_t hlen = strlen(haystack);
102 size_t nlen = strlen(needle);
103
104 return nlen <= hlen &&
105 memcmp(haystack + hlen - nlen, needle, nlen) == 0;
106 }
107
108 static unsigned
109 width_in_bits(const type_t *tp)
110 {
111
112 lint_assert(is_integer(tp->t_tspec));
113 return tp->t_bitfield
114 ? tp->t_bit_field_width
115 : size_in_bits(tp->t_tspec);
116 }
117
118 static int
119 portable_rank_cmp(tspec_t t1, tspec_t t2)
120 {
121 const ttab_t *p1 = type_properties(t1), *p2 = type_properties(t2);
122 lint_assert(p1->tt_rank_kind == p2->tt_rank_kind);
123 lint_assert(p1->tt_rank_value > 0);
124 lint_assert(p2->tt_rank_value > 0);
125 return (int)p1->tt_rank_value - (int)p2->tt_rank_value;
126 }
127
128 static bool
129 ic_maybe_signed(const type_t *tp, const integer_constraints *ic)
130 {
131 return !is_uinteger(tp->t_tspec) && ic->bclr >> 63 == 0;
132 }
133
134 static integer_constraints
135 ic_any(const type_t *tp)
136 {
137 integer_constraints c;
138
139 uint64_t vbits = value_bits(width_in_bits(tp));
140 if (is_uinteger(tp->t_tspec)) {
141 c.smin = INT64_MIN;
142 c.smax = INT64_MAX;
143 c.umin = 0;
144 c.umax = vbits;
145 c.bclr = ~c.umax;
146 } else {
147 c.smin = (int64_t)-1 - (int64_t)(vbits >> 1);
148 c.smax = (int64_t)(vbits >> 1);
149 c.umin = 0;
150 c.umax = UINT64_MAX;
151 c.bclr = 0;
152 }
153 return c;
154 }
155
156 static integer_constraints
157 ic_mult(const type_t *tp, integer_constraints a, integer_constraints b)
158 {
159 integer_constraints c;
160
161 if (ic_maybe_signed(tp, &a) || ic_maybe_signed(tp, &b)
162 || (a.umax > 0 && b.umax > ic_any(tp).umax / a.umax))
163 return ic_any(tp);
164
165 c.smin = INT64_MIN;
166 c.smax = INT64_MAX;
167 c.umin = a.umin * b.umin;
168 c.umax = a.umax * b.umax;
169 c.bclr = ~u64_fill_right(c.umax);
170 return c;
171 }
172
173 static integer_constraints
174 ic_div(const type_t *tp, integer_constraints a, integer_constraints b)
175 {
176 integer_constraints c;
177
178 if (ic_maybe_signed(tp, &a) || ic_maybe_signed(tp, &b) || b.umin == 0)
179 return ic_any(tp);
180
181 c.smin = INT64_MIN;
182 c.smax = INT64_MAX;
183 c.umin = a.umin / b.umax;
184 c.umax = a.umax / b.umin;
185 c.bclr = ~u64_fill_right(c.umax);
186 return c;
187 }
188
189 static integer_constraints
190 ic_mod_signed(integer_constraints a, integer_constraints b)
191 {
192 integer_constraints c;
193
194 uint64_t max_abs_b = u64_max(s64_abs(b.smin), s64_abs(b.smax));
195 if (max_abs_b >> 63 != 0 || max_abs_b == 0)
196 return a;
197 c.smin = s64_max(a.smin, -(int64_t)(max_abs_b - 1));
198 c.smax = s64_min(a.smax, (int64_t)(max_abs_b - 1));
199 c.umin = 0;
200 c.umax = UINT64_MAX;
201 c.bclr = 0;
202 return c;
203 }
204
205 static integer_constraints
206 ic_mod(const type_t *tp, integer_constraints a, integer_constraints b)
207 {
208 integer_constraints c;
209
210 if (ic_maybe_signed(tp, &a) || ic_maybe_signed(tp, &b))
211 return ic_mod_signed(a, b);
212
213 c.smin = INT64_MIN;
214 c.smax = INT64_MAX;
215 c.umin = 0;
216 c.umax = b.umax - 1;
217 c.bclr = ~u64_fill_right(c.umax);
218 return c;
219 }
220
221 static integer_constraints
222 ic_shl(const type_t *tp, integer_constraints a, integer_constraints b)
223 {
224 integer_constraints c;
225 unsigned int amount;
226
227 if (ic_maybe_signed(tp, &a))
228 return ic_any(tp);
229
230 if (b.smin == b.smax && b.smin >= 0 && b.smin < 64)
231 amount = (unsigned int)b.smin;
232 else if (b.umin == b.umax && b.umin < 64)
233 amount = (unsigned int)b.umin;
234 else
235 return ic_any(tp);
236
237 c.smin = INT64_MIN;
238 c.smax = INT64_MAX;
239 c.umin = 0;
240 c.umax = UINT64_MAX;
241 c.bclr = a.bclr << amount | (((uint64_t)1 << amount) - 1);
242 return c;
243 }
244
245 static integer_constraints
246 ic_shr(const type_t *tp, integer_constraints a, integer_constraints b)
247 {
248 integer_constraints c;
249 unsigned int amount;
250
251 if (ic_maybe_signed(tp, &a))
252 return ic_any(tp);
253
254 if (b.smin == b.smax && b.smin >= 0 && b.smin < 64)
255 amount = (unsigned int)b.smin;
256 else if (b.umin == b.umax && b.umin < 64)
257 amount = (unsigned int)b.umin;
258 else
259 return ic_any(tp);
260
261 c.smin = INT64_MIN;
262 c.smax = INT64_MAX;
263 c.umin = 0;
264 c.umax = UINT64_MAX;
265 c.bclr = a.bclr >> amount | ~(~(uint64_t)0 >> amount);
266 return c;
267 }
268
269 static integer_constraints
270 ic_bitand(integer_constraints a, integer_constraints b)
271 {
272 integer_constraints c;
273
274 c.smin = INT64_MIN;
275 c.smax = INT64_MAX;
276 c.umin = 0;
277 c.umax = ~(a.bclr | b.bclr);
278 if (c.umax >> 63 == 0) {
279 c.smin = 0;
280 c.smax = (int64_t)c.umax;
281 }
282 c.bclr = a.bclr | b.bclr;
283 return c;
284 }
285
286 static integer_constraints
287 ic_bitor(integer_constraints a, integer_constraints b)
288 {
289 integer_constraints c;
290
291 c.smin = INT64_MIN;
292 c.smax = INT64_MAX;
293 c.umin = 0;
294 c.umax = ~(a.bclr & b.bclr);
295 c.bclr = a.bclr & b.bclr;
296 return c;
297 }
298
299 static integer_constraints
300 ic_quest_colon(integer_constraints a, integer_constraints b)
301 {
302 integer_constraints c;
303
304 c.smin = a.smin < b.smin ? a.smin : b.smin;
305 c.smax = a.smax > b.smax ? a.smax : b.smax;
306 c.umin = a.umin < b.umin ? a.umin : b.umin;
307 c.umax = a.umax > b.umax ? a.umax : b.umax;
308 c.bclr = a.bclr & b.bclr;
309 return c;
310 }
311
312 static integer_constraints
313 ic_con(const type_t *tp, const val_t *v)
314 {
315 integer_constraints c;
316
317 lint_assert(is_integer(tp->t_tspec));
318 int64_t si = v->u.integer;
319 uint64_t ui = (uint64_t)si;
320 c.smin = si;
321 c.smax = si;
322 c.umin = ui;
323 c.umax = ui;
324 c.bclr = ~ui;
325 return c;
326 }
327
328 static integer_constraints
329 ic_cvt(const type_t *ntp, const type_t *otp, integer_constraints a)
330 {
331 unsigned nw = width_in_bits(ntp);
332 unsigned ow = width_in_bits(otp);
333 bool nu = is_uinteger(ntp->t_tspec);
334 bool ou = is_uinteger(otp->t_tspec);
335
336 if (nw >= ow && nu == ou)
337 return a;
338 if (nw > ow && ou)
339 return a;
340 return ic_any(ntp);
341 }
342
343 static integer_constraints
344 ic_expr(const tnode_t *tn)
345 {
346 integer_constraints lc, rc;
347
348 lint_assert(is_integer(tn->tn_type->t_tspec));
349
350 switch (tn->tn_op) {
351 case MULT:
352 lc = ic_expr(before_conversion(tn->u.ops.left));
353 rc = ic_expr(before_conversion(tn->u.ops.right));
354 return ic_mult(tn->tn_type, lc, rc);
355 case DIV:
356 lc = ic_expr(before_conversion(tn->u.ops.left));
357 rc = ic_expr(before_conversion(tn->u.ops.right));
358 return ic_div(tn->tn_type, lc, rc);
359 case MOD:
360 lc = ic_expr(before_conversion(tn->u.ops.left));
361 rc = ic_expr(before_conversion(tn->u.ops.right));
362 return ic_mod(tn->tn_type, lc, rc);
363 case SHL:
364 lc = ic_expr(tn->u.ops.left);
365 rc = ic_expr(tn->u.ops.right);
366 return ic_shl(tn->tn_type, lc, rc);
367 case SHR:
368 lc = ic_expr(tn->u.ops.left);
369 rc = ic_expr(tn->u.ops.right);
370 return ic_shr(tn->tn_type, lc, rc);
371 case BITAND:
372 lc = ic_expr(tn->u.ops.left);
373 rc = ic_expr(tn->u.ops.right);
374 return ic_bitand(lc, rc);
375 case BITOR:
376 lc = ic_expr(tn->u.ops.left);
377 rc = ic_expr(tn->u.ops.right);
378 return ic_bitor(lc, rc);
379 case QUEST:
380 lc = ic_expr(tn->u.ops.right->u.ops.left);
381 rc = ic_expr(tn->u.ops.right->u.ops.right);
382 return ic_quest_colon(lc, rc);
383 case CON:
384 return ic_con(tn->tn_type, &tn->u.value);
385 case CVT:
386 if (!is_integer(tn->u.ops.left->tn_type->t_tspec))
387 return ic_any(tn->tn_type);
388 lc = ic_expr(tn->u.ops.left);
389 return ic_cvt(tn->tn_type, tn->u.ops.left->tn_type, lc);
390 default:
391 return ic_any(tn->tn_type);
392 }
393 }
394
395 uint64_t
396 possible_bits(const tnode_t *tn)
397 {
398 return ~ic_expr(tn).bclr;
399 }
400
401 bool
402 attributes_contain(const attribute_list *attrs, const char *str)
403 {
404 for (size_t i = 0, n = attrs->len; i < n; i++) {
405 const attribute *attr = attrs->attrs + i;
406 if (attr->prefix == NULL && strcmp(attr->name, str) == 0)
407 return true;
408 }
409 return false;
410 }
411
412 /* Build 'pointer to tp', 'array of tp' or 'function returning tp'. */
413 type_t *
414 block_derive_type(type_t *tp, tspec_t t)
415 {
416
417 type_t *tp2 = block_zero_alloc(sizeof(*tp2), "type");
418 tp2->t_tspec = t;
419 tp2->t_subt = tp;
420 return tp2;
421 }
422
423 /*
424 * Derive 'pointer to tp' or 'function returning tp'.
425 * The memory is freed at the end of the current expression.
426 */
427 type_t *
428 expr_derive_type(type_t *tp, tspec_t t)
429 {
430
431 type_t *tp2 = expr_zero_alloc(sizeof(*tp2), "type");
432 tp2->t_tspec = t;
433 tp2->t_subt = tp;
434 return tp2;
435 }
436
437 /* Create an expression from a unary or binary operator and its operands. */
438 static tnode_t *
439 build_op(op_t op, bool sys, type_t *type, tnode_t *ln, tnode_t *rn)
440 {
441
442 tnode_t *ntn = expr_alloc_tnode();
443 ntn->tn_op = op;
444 ntn->tn_type = type;
445 ntn->tn_sys = sys;
446 ntn->u.ops.left = ln;
447 ntn->u.ops.right = rn;
448
449 if (op == INDIR || op == FSEL) {
450 lint_assert(ln->tn_type->t_tspec == PTR);
451 tspec_t t = ln->tn_type->t_subt->t_tspec;
452 ntn->tn_lvalue = t != FUNC && t != VOID;
453 }
454
455 return ntn;
456 }
457
458 tnode_t *
459 build_constant(type_t *tp, val_t *v)
460 {
461
462 tnode_t *n = expr_alloc_tnode();
463 n->tn_op = CON;
464 n->tn_type = tp;
465 n->u.value = *v;
466 n->u.value.v_tspec = tp->t_tspec;
467 free(v);
468 return n;
469 }
470
471 static tnode_t *
472 build_integer_constant(tspec_t t, int64_t si)
473 {
474
475 tnode_t *n = expr_alloc_tnode();
476 n->tn_op = CON;
477 n->tn_type = gettyp(t);
478 n->u.value.v_tspec = t;
479 n->u.value.v_unsigned_since_c90 = false;
480 n->u.value.v_char_constant = false;
481 n->u.value.u.integer = si;
482 return n;
483 }
484
485 static void
486 fallback_symbol(sym_t *sym)
487 {
488
489 if (Tflag && fallback_symbol_strict_bool(sym))
490 return;
491
492 if (funcsym != NULL && (strcmp(sym->s_name, "__FUNCTION__") == 0 ||
493 strcmp(sym->s_name, "__PRETTY_FUNCTION__") == 0)) {
494 /* __FUNCTION__/__PRETTY_FUNCTION__ is a GCC extension */
495 gnuism(316);
496 goto return_function_name;
497 }
498
499 if (funcsym != NULL && strcmp(sym->s_name, "__func__") == 0) {
500 if (!allow_c99)
501 /* __func__ is a C99 feature */
502 warning(317);
503 /* C11 6.4.2.2 */
504 return_function_name:
505 sym->s_type = block_derive_type(gettyp(CHAR), ARRAY);
506 sym->s_type->t_const = true;
507 sym->s_type->u.dimension = (int)strlen(funcsym->s_name) + 1;
508 return;
509 }
510
511 /* '%s' undefined */
512 error(99, sym->s_name);
513 }
514
515 /*
516 * Functions that are predeclared by GCC or other compilers can be called
517 * with arbitrary arguments. Since lint usually runs after a successful
518 * compilation, it's the compiler's job to catch any errors.
519 */
520 bool
521 is_compiler_builtin(const char *name)
522 {
523 /* https://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html */
524 if (allow_gcc) {
525 if (strncmp(name, "__atomic_", 9) == 0 ||
526 strncmp(name, "__builtin_", 10) == 0 ||
527 strcmp(name, "alloca") == 0 ||
528 /* obsolete but still in use, as of 2021 */
529 strncmp(name, "__sync_", 7) == 0)
530 return true;
531 }
532
533 /* https://software.intel.com/sites/landingpage/IntrinsicsGuide/ */
534 if (strncmp(name, "_mm_", 4) == 0)
535 return true;
536
537 return false;
538 }
539
540 /* https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html */
541 static bool
542 is_gcc_bool_builtin(const char *name)
543 {
544 return strncmp(name, "__builtin_", 10) == 0 &&
545 (str_ends_with(name, "_overflow") ||
546 str_ends_with(name, "_overflow_p"));
547 }
548
549 static void
550 build_name_call(sym_t *sym)
551 {
552
553 if (is_compiler_builtin(sym->s_name)) {
554 /*
555 * Do not warn about these, just assume that they are regular
556 * functions compatible with non-prototype calling conventions.
557 */
558 if (allow_gcc && is_gcc_bool_builtin(sym->s_name))
559 sym->s_type = gettyp(BOOL);
560 } else if (allow_c99)
561 /* function '%s' implicitly declared to return int */
562 error(215, sym->s_name);
563 else if (!allow_trad)
564 /* function '%s' implicitly declared to return int */
565 warning(215, sym->s_name);
566
567 /* XXX if !allow_c90, the symbol should be exported to level 0 */
568 sym->s_type = block_derive_type(sym->s_type, FUNC);
569 }
570
571 /* Create a node for a name (symbol table entry). */
572 tnode_t *
573 build_name(sym_t *sym, bool is_funcname)
574 {
575
576 if (sym->s_scl == NO_SCL && !in_gcc_attribute) {
577 sym->s_scl = EXTERN;
578 sym->s_def = DECL;
579 if (is_funcname)
580 build_name_call(sym);
581 else
582 fallback_symbol(sym);
583 }
584
585 lint_assert(sym->s_kind == SK_VCFT || sym->s_kind == SK_MEMBER);
586
587 tnode_t *n = expr_alloc_tnode();
588 n->tn_type = sym->s_type;
589 if (sym->s_scl == BOOL_CONST) {
590 n->tn_op = CON;
591 n->u.value.v_tspec = BOOL;
592 n->u.value.v_unsigned_since_c90 = false;
593 n->u.value.v_char_constant = false;
594 n->u.value.u.integer = sym->u.s_bool_constant ? 1 : 0;
595 } else if (sym->s_scl == ENUM_CONST) {
596 n->tn_op = CON;
597 n->u.value.v_tspec = INT; /* ENUM is in n->tn_type */
598 n->u.value.v_unsigned_since_c90 = false;
599 n->u.value.v_char_constant = false;
600 n->u.value.u.integer = sym->u.s_enum_constant;
601 } else {
602 n->tn_op = NAME;
603 n->u.sym = sym;
604 if (sym->s_kind == SK_VCFT && sym->s_type->t_tspec != FUNC)
605 n->tn_lvalue = true;
606 }
607
608 return n;
609 }
610
611 tnode_t *
612 build_string(buffer *lit)
613 {
614 size_t value_len = lit->len;
615 if (lit->data != NULL) {
616 quoted_iterator it = { .end = 0 };
617 for (value_len = 0; quoted_next(lit, &it); value_len++)
618 continue;
619 }
620
621 type_t *tp = expr_zero_alloc(sizeof(*tp), "type");
622 tp->t_tspec = ARRAY;
623 tp->t_subt = gettyp(lit->data != NULL ? CHAR : WCHAR_TSPEC);
624 tp->u.dimension = (int)(value_len + 1);
625
626 tnode_t *n = expr_alloc_tnode();
627 n->tn_op = STRING;
628 n->tn_type = tp;
629 n->tn_lvalue = true;
630
631 n->u.str_literals = expr_zero_alloc(sizeof(*n->u.str_literals), "tnode.string");
632 n->u.str_literals->len = lit->len;
633
634 if (lit->data != NULL) {
635 n->u.str_literals->data = expr_zero_alloc(lit->len + 1,
636 "tnode.string.data");
637 (void)memcpy(n->u.str_literals->data, lit->data, lit->len + 1);
638 free(lit->data);
639 }
640 free(lit);
641
642 return n;
643 }
644
645 tnode_t *
646 build_generic_selection(const tnode_t *expr,
647 struct generic_association *sel)
648 {
649 tnode_t *default_result = NULL;
650
651 for (; sel != NULL; sel = sel->ga_prev) {
652 if (expr != NULL &&
653 types_compatible(sel->ga_arg, expr->tn_type,
654 false, false, NULL))
655 return sel->ga_result;
656 else if (sel->ga_arg == NULL)
657 default_result = sel->ga_result;
658 }
659 return default_result;
660 }
661
662 static bool
663 is_out_of_char_range(const tnode_t *tn)
664 {
665 return tn->tn_op == CON &&
666 !tn->u.value.v_char_constant &&
667 !(0 <= tn->u.value.u.integer &&
668 tn->u.value.u.integer < 1 << (CHAR_SIZE - 1));
669 }
670
671 static bool
672 check_nonportable_char_comparison(op_t op,
673 const tnode_t *ln, tspec_t lt,
674 const tnode_t *rn, tspec_t rt)
675 {
676 if (!(hflag || pflag))
677 return true;
678
679 if (lt == CHAR && is_out_of_char_range(rn)) {
680 char buf[128];
681 (void)snprintf(buf, sizeof(buf), "%s %d",
682 op_name(op), (int)rn->u.value.u.integer);
683 /* nonportable character comparison '%s' */
684 warning(230, buf);
685 return false;
686 }
687 if (rt == CHAR && is_out_of_char_range(ln)) {
688 char buf[128];
689 (void)snprintf(buf, sizeof(buf), "%d %s ?",
690 (int)ln->u.value.u.integer, op_name(op));
691 /* nonportable character comparison '%s' */
692 warning(230, buf);
693 return false;
694 }
695 return true;
696 }
697
698 static void
699 check_integer_comparison(op_t op, tnode_t *ln, tnode_t *rn)
700 {
701
702 tspec_t lt = ln->tn_type->t_tspec;
703 tspec_t rt = rn->tn_type->t_tspec;
704
705 if (ln->tn_op != CON && rn->tn_op != CON)
706 return;
707
708 if (!is_integer(lt) || !is_integer(rt))
709 return;
710
711 if (any_query_enabled && !in_system_header) {
712 if (lt == CHAR && rn->tn_op == CON &&
713 !rn->u.value.v_char_constant) {
714 /* comparison '%s' of 'char' with plain integer %d */
715 query_message(14,
716 op_name(op), (int)rn->u.value.u.integer);
717 }
718 if (rt == CHAR && ln->tn_op == CON &&
719 !ln->u.value.v_char_constant) {
720 /* comparison '%s' of 'char' with plain integer %d */
721 query_message(14,
722 op_name(op), (int)ln->u.value.u.integer);
723 }
724 }
725
726 if (!check_nonportable_char_comparison(op, ln, lt, rn, rt))
727 return;
728
729 if (is_uinteger(lt) && !is_uinteger(rt) &&
730 rn->tn_op == CON && rn->u.value.u.integer <= 0) {
731 if (rn->u.value.u.integer < 0) {
732 /* operator '%s' compares '%s' with '%s' */
733 warning(162, op_name(op),
734 type_name(ln->tn_type), "negative constant");
735 } else if (op == LT || op == GE)
736 /* operator '%s' compares '%s' with '%s' */
737 warning(162, op_name(op), type_name(ln->tn_type), "0");
738 return;
739 }
740 if (is_uinteger(rt) && !is_uinteger(lt) &&
741 ln->tn_op == CON && ln->u.value.u.integer <= 0) {
742 if (ln->u.value.u.integer < 0) {
743 /* operator '%s' compares '%s' with '%s' */
744 warning(162, op_name(op),
745 "negative constant", type_name(rn->tn_type));
746 } else if (op == GT || op == LE)
747 /* operator '%s' compares '%s' with '%s' */
748 warning(162, op_name(op), "0", type_name(rn->tn_type));
749 return;
750 }
751 }
752
753 static const tspec_t arith_rank[] = {
754 LDOUBLE, DOUBLE, FLOAT,
755 #ifdef INT128_SIZE
756 UINT128, INT128,
757 #endif
758 ULLONG, LLONG,
759 ULONG, LONG,
760 UINT, INT,
761 };
762
763 /* Keep unsigned in traditional C */
764 static tspec_t
765 usual_arithmetic_conversion_trad(tspec_t lt, tspec_t rt)
766 {
767
768 size_t i;
769 for (i = 0; arith_rank[i] != INT; i++)
770 if (lt == arith_rank[i] || rt == arith_rank[i])
771 break;
772
773 tspec_t t = arith_rank[i];
774 if (is_uinteger(lt) || is_uinteger(rt))
775 if (is_integer(t) && !is_uinteger(t))
776 return unsigned_type(t);
777 return t;
778 }
779
780 static tspec_t
781 usual_arithmetic_conversion_c90(tspec_t lt, tspec_t rt)
782 {
783
784 if (lt == rt)
785 return lt;
786
787 if (lt == LCOMPLEX || rt == LCOMPLEX)
788 return LCOMPLEX;
789 if (lt == DCOMPLEX || rt == DCOMPLEX)
790 return DCOMPLEX;
791 if (lt == FCOMPLEX || rt == FCOMPLEX)
792 return FCOMPLEX;
793 if (lt == LDOUBLE || rt == LDOUBLE)
794 return LDOUBLE;
795 if (lt == DOUBLE || rt == DOUBLE)
796 return DOUBLE;
797 if (lt == FLOAT || rt == FLOAT)
798 return FLOAT;
799
800 if (size_in_bits(lt) > size_in_bits(rt))
801 return lt;
802 if (size_in_bits(lt) < size_in_bits(rt))
803 return rt;
804
805 size_t i;
806 for (i = 3; arith_rank[i] != INT; i++)
807 if (arith_rank[i] == lt || arith_rank[i] == rt)
808 break;
809 if ((is_uinteger(lt) || is_uinteger(rt)) &&
810 !is_uinteger(arith_rank[i]))
811 i--;
812 return arith_rank[i];
813 }
814
815 static tnode_t *
816 apply_usual_arithmetic_conversions(op_t op, tnode_t *tn, tspec_t t)
817 {
818 type_t *ntp = expr_dup_type(tn->tn_type);
819 ntp->t_tspec = t;
820 if (tn->tn_op != CON) {
821 /* usual arithmetic conversion for '%s' from '%s' to '%s' */
822 query_message(4, op_name(op),
823 type_name(tn->tn_type), type_name(ntp));
824 }
825 return convert(op, 0, ntp, tn);
826 }
827
828 /*
829 * Apply the "usual arithmetic conversions" (C99 6.3.1.8), which gives both
830 * operands the same type.
831 */
832 static void
833 balance(op_t op, tnode_t **lnp, tnode_t **rnp)
834 {
835
836 tspec_t lt = (*lnp)->tn_type->t_tspec;
837 tspec_t rt = (*rnp)->tn_type->t_tspec;
838 if (!is_arithmetic(lt) || !is_arithmetic(rt))
839 return;
840
841 tspec_t t = allow_c90
842 ? usual_arithmetic_conversion_c90(lt, rt)
843 : usual_arithmetic_conversion_trad(lt, rt);
844
845 if (modtab[op].m_comparison
846 && is_integer(lt) && (*lnp)->tn_op != CON
847 && is_floating(t) && (*rnp)->tn_op == CON)
848 /* comparing integer '%s' to floating point constant %Lg */
849 warning(379, type_name((*lnp)->tn_type),
850 (*rnp)->u.value.u.floating);
851
852 if (t != lt)
853 *lnp = apply_usual_arithmetic_conversions(op, *lnp, t);
854 if (t != rt)
855 *rnp = apply_usual_arithmetic_conversions(op, *rnp, t);
856
857 if (is_integer(t)) {
858 unsigned lw = width_in_bits((*lnp)->tn_type);
859 unsigned rw = width_in_bits((*rnp)->tn_type);
860 if (lw < rw)
861 *lnp = convert(NOOP, 0, (*rnp)->tn_type, *lnp);
862 if (rw < lw)
863 *rnp = convert(NOOP, 0, (*lnp)->tn_type, *rnp);
864 }
865 }
866
867 static tnode_t *
868 build_address(bool sys, tnode_t *tn, bool force)
869 {
870 tspec_t t;
871
872 if (!force && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) {
873 if (!allow_c90)
874 /* '&' before array or function: ignored */
875 warning(127);
876 return tn;
877 }
878
879 /* eliminate '&*' */
880 if (tn->tn_op == INDIR &&
881 tn->u.ops.left->tn_type->t_tspec == PTR &&
882 tn->u.ops.left->tn_type->t_subt == tn->tn_type) {
883 return tn->u.ops.left;
884 }
885
886 return build_op(ADDR, sys, expr_derive_type(tn->tn_type, PTR),
887 tn, NULL);
888 }
889
890 static uint64_t
891 fold_unsigned_integer(op_t op, uint64_t l, uint64_t r,
892 uint64_t max_value, bool *overflow)
893 {
894 switch (op) {
895 case COMPL:
896 return ~l & max_value;
897 case UPLUS:
898 return +l;
899 case UMINUS:
900 return -l & max_value;
901 case MULT:
902 *overflow = r > 0 && l > max_value / r;
903 return l * r;
904 case DIV:
905 if (r == 0) {
906 /* division by 0 */
907 error(139);
908 return max_value;
909 }
910 return l / r;
911 case MOD:
912 if (r == 0) {
913 /* modulus by 0 */
914 error(140);
915 return 0;
916 }
917 return l % r;
918 case PLUS:
919 *overflow = l > max_value - r;
920 return l + r;
921 case MINUS:
922 *overflow = l < r;
923 return l - r;
924 case SHL:
925 /* TODO: warn about out-of-bounds 'r'. */
926 /* TODO: warn about overflow. */
927 return l << (r & 63);
928 case SHR:
929 /* TODO: warn about out-of-bounds 'r'. */
930 return l >> (r & 63);
931 case LT:
932 return l < r ? 1 : 0;
933 case LE:
934 return l <= r ? 1 : 0;
935 case GT:
936 return l > r ? 1 : 0;
937 case GE:
938 return l >= r ? 1 : 0;
939 case EQ:
940 return l == r ? 1 : 0;
941 case NE:
942 return l != r ? 1 : 0;
943 case BITAND:
944 return l & r;
945 case BITXOR:
946 return l ^ r;
947 case BITOR:
948 return l | r;
949 default:
950 lint_assert(/*CONSTCOND*/false);
951 /* NOTREACHED */
952 }
953 }
954
955 static int64_t
956 fold_signed_integer(op_t op, int64_t l, int64_t r,
957 int64_t min_value, int64_t max_value, bool *overflow)
958 {
959 switch (op) {
960 case COMPL:
961 return ~l;
962 case UPLUS:
963 return +l;
964 case UMINUS:
965 *overflow = l == min_value;
966 return *overflow ? l : -l;
967 case MULT:;
968 uint64_t al = l >= 0 ? (uint64_t)l : -(uint64_t)l;
969 uint64_t ar = r >= 0 ? (uint64_t)r : -(uint64_t)r;
970 bool neg = (l >= 0) != (r >= 0);
971 uint64_t max_prod = (uint64_t)max_value + (neg ? 1 : 0);
972 if (al > 0 && ar > max_prod / al) {
973 *overflow = true;
974 return neg ? min_value : max_value;
975 }
976 return l * r;
977 case DIV:
978 if (r == 0) {
979 /* division by 0 */
980 error(139);
981 return max_value;
982 }
983 if (l == min_value && r == -1) {
984 *overflow = true;
985 return l;
986 }
987 return l / r;
988 case MOD:
989 if (r == 0) {
990 /* modulus by 0 */
991 error(140);
992 return 0;
993 }
994 if (l == min_value && r == -1) {
995 *overflow = true;
996 return 0;
997 }
998 return l % r;
999 case PLUS:
1000 if (r > 0 && l > max_value - r) {
1001 *overflow = true;
1002 return max_value;
1003 }
1004 if (r < 0 && l < min_value - r) {
1005 *overflow = true;
1006 return min_value;
1007 }
1008 return l + r;
1009 case MINUS:
1010 if (r > 0 && l < min_value + r) {
1011 *overflow = true;
1012 return min_value;
1013 }
1014 if (r < 0 && l > max_value + r) {
1015 *overflow = true;
1016 return max_value;
1017 }
1018 return l - r;
1019 case SHL:
1020 /* TODO: warn about out-of-bounds 'r'. */
1021 /* TODO: warn about overflow. */
1022 return l << (r & 63);
1023 case SHR:
1024 /* TODO: warn about out-of-bounds 'r'. */
1025 if (l < 0)
1026 return (int64_t)~(~(uint64_t)l >> (r & 63));
1027 return (int64_t)((uint64_t)l >> (r & 63));
1028 case LT:
1029 return l < r ? 1 : 0;
1030 case LE:
1031 return l <= r ? 1 : 0;
1032 case GT:
1033 return l > r ? 1 : 0;
1034 case GE:
1035 return l >= r ? 1 : 0;
1036 case EQ:
1037 return l == r ? 1 : 0;
1038 case NE:
1039 return l != r ? 1 : 0;
1040 case BITAND:
1041 return l & r;
1042 case BITXOR:
1043 return l ^ r;
1044 case BITOR:
1045 return l | r;
1046 default:
1047 lint_assert(/*CONSTCOND*/false);
1048 /* NOTREACHED */
1049 }
1050 }
1051
1052 static tnode_t *
1053 fold_constant_integer(tnode_t *tn)
1054 {
1055
1056 lint_assert(has_operands(tn));
1057 tspec_t t = tn->u.ops.left->tn_type->t_tspec;
1058 int64_t l = tn->u.ops.left->u.value.u.integer;
1059 int64_t r = is_binary(tn) ? tn->u.ops.right->u.value.u.integer : 0;
1060 uint64_t mask = value_bits(size_in_bits(t));
1061
1062 int64_t res;
1063 bool overflow = false;
1064 if (!is_integer(t) || is_uinteger(t)) {
1065 uint64_t u_res = fold_unsigned_integer(tn->tn_op,
1066 (uint64_t)l, (uint64_t)r, mask, &overflow);
1067 if (u_res > mask)
1068 overflow = true;
1069 res = (int64_t)u_res;
1070 if (overflow && hflag) {
1071 char buf[128];
1072 if (is_binary(tn)) {
1073 snprintf(buf, sizeof(buf), "%ju %s %ju",
1074 (uintmax_t)l, op_name(tn->tn_op),
1075 (uintmax_t)r);
1076 } else {
1077 snprintf(buf, sizeof(buf), "%s%ju",
1078 op_name(tn->tn_op), (uintmax_t)l);
1079 }
1080 /* '%s' overflows '%s' */
1081 warning(141, buf, type_name(tn->tn_type));
1082 }
1083 } else {
1084 int64_t max_value = (int64_t)(mask >> 1);
1085 int64_t min_value = -max_value - 1;
1086 res = fold_signed_integer(tn->tn_op,
1087 l, r, min_value, max_value, &overflow);
1088 if (res < min_value || res > max_value)
1089 overflow = true;
1090 if (overflow && hflag) {
1091 char buf[128];
1092 if (is_binary(tn)) {
1093 snprintf(buf, sizeof(buf), "%jd %s %jd",
1094 (intmax_t)l, op_name(tn->tn_op),
1095 (intmax_t)r);
1096 } else if (tn->tn_op == UMINUS && l < 0) {
1097 snprintf(buf, sizeof(buf), "-(%jd)",
1098 (intmax_t)l);
1099 } else {
1100 snprintf(buf, sizeof(buf), "%s%jd",
1101 op_name(tn->tn_op), (intmax_t)l);
1102 }
1103 /* '%s' overflows '%s' */
1104 warning(141, buf, type_name(tn->tn_type));
1105 }
1106 }
1107
1108 val_t *v = xcalloc(1, sizeof(*v));
1109 v->v_tspec = tn->tn_type->t_tspec;
1110 v->u.integer = convert_integer(res, t, size_in_bits(t));
1111
1112 tnode_t *cn = build_constant(tn->tn_type, v);
1113 if (tn->u.ops.left->tn_system_dependent)
1114 cn->tn_system_dependent = true;
1115 if (is_binary(tn) && tn->u.ops.right->tn_system_dependent)
1116 cn->tn_system_dependent = true;
1117
1118 return cn;
1119 }
1120
1121 static tnode_t *
1122 build_struct_access(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
1123 {
1124
1125 lint_assert(rn->tn_op == NAME);
1126 lint_assert(is_member(rn->u.sym));
1127
1128 bool lvalue = op == ARROW || ln->tn_lvalue;
1129
1130 if (op == POINT)
1131 ln = build_address(sys, ln, true);
1132 else if (ln->tn_type->t_tspec != PTR) {
1133 lint_assert(!allow_c90);
1134 lint_assert(is_integer(ln->tn_type->t_tspec));
1135 ln = convert(NOOP, 0, expr_derive_type(gettyp(VOID), PTR), ln);
1136 }
1137
1138 tnode_t *ctn = build_integer_constant(PTRDIFF_TSPEC,
1139 rn->u.sym->u.s_member.sm_offset_in_bits / CHAR_SIZE);
1140
1141 type_t *ptr_tp = expr_derive_type(rn->tn_type, PTR);
1142 tnode_t *ntn = build_op(PLUS, sys, ptr_tp, ln, ctn);
1143 if (ln->tn_op == CON)
1144 ntn = fold_constant_integer(ntn);
1145
1146 op_t nop = rn->tn_type->t_bitfield ? FSEL : INDIR;
1147 ntn = build_op(nop, sys, ntn->tn_type->t_subt, ntn, NULL);
1148 if (!lvalue)
1149 ntn->tn_lvalue = false;
1150
1151 return ntn;
1152 }
1153
1154 /*
1155 * Get the size in bytes of type tp->t_subt, as a constant expression of type
1156 * ptrdiff_t as seen from the target platform.
1157 */
1158 static tnode_t *
1159 subt_size_in_bytes(type_t *tp)
1160 {
1161
1162 lint_assert(tp->t_tspec == PTR);
1163 tp = tp->t_subt;
1164
1165 int elem = 1;
1166 while (tp->t_tspec == ARRAY) {
1167 elem *= tp->u.dimension;
1168 tp = tp->t_subt;
1169 }
1170
1171 int elsz_in_bits = 0;
1172 switch (tp->t_tspec) {
1173 case FUNC:
1174 /* pointer to function is not allowed here */
1175 error(110);
1176 break;
1177 case VOID:
1178 /* cannot do pointer arithmetic on operand of unknown size */
1179 gnuism(136);
1180 break;
1181 case STRUCT:
1182 case UNION:
1183 if ((elsz_in_bits = (int)tp->u.sou->sou_size_in_bits) == 0)
1184 /* cannot do pointer arithmetic on operand of ... */
1185 error(136);
1186 break;
1187 case ENUM:
1188 if (is_incomplete(tp))
1189 /* cannot do pointer arithmetic on operand of ... */
1190 warning(136);
1191 /* FALLTHROUGH */
1192 default:
1193 if ((elsz_in_bits = size_in_bits(tp->t_tspec)) == 0)
1194 /* cannot do pointer arithmetic on operand of ... */
1195 error(136);
1196 else
1197 lint_assert(elsz_in_bits != -1);
1198 break;
1199 }
1200
1201 if (elem == 0 && elsz_in_bits != 0)
1202 /* cannot do pointer arithmetic on operand of unknown size */
1203 error(136);
1204
1205 if (elsz_in_bits == 0)
1206 elsz_in_bits = CHAR_SIZE;
1207
1208 return build_integer_constant(PTRDIFF_TSPEC,
1209 (int64_t)(elem * elsz_in_bits / CHAR_SIZE));
1210 }
1211
1212 static tnode_t *
1213 build_prepost_incdec(op_t op, bool sys, tnode_t *ln)
1214 {
1215
1216 lint_assert(ln != NULL);
1217 tnode_t *cn = ln->tn_type->t_tspec == PTR
1218 ? subt_size_in_bytes(ln->tn_type)
1219 : build_integer_constant(INT, 1);
1220 return build_op(op, sys, ln->tn_type, ln, cn);
1221 }
1222
1223 static void
1224 check_enum_array_index(const tnode_t *ln, const tnode_t *rn)
1225 {
1226
1227 if (ln->tn_op != ADDR || ln->u.ops.left->tn_op != NAME)
1228 return;
1229
1230 const type_t *ltp = ln->u.ops.left->tn_type;
1231 if (ltp->t_tspec != ARRAY || ltp->t_incomplete_array)
1232 return;
1233
1234 if (rn->tn_op != CVT || !rn->tn_type->t_is_enum)
1235 return;
1236 if (rn->u.ops.left->tn_op != LOAD)
1237 return;
1238
1239 const type_t *rtp = rn->u.ops.left->tn_type;
1240 const sym_t *ec = rtp->u.enumer->en_first_enumerator;
1241 const sym_t *max_ec = ec;
1242 lint_assert(ec != NULL);
1243 for (ec = ec->s_next; ec != NULL; ec = ec->s_next)
1244 if (ec->u.s_enum_constant > max_ec->u.s_enum_constant)
1245 max_ec = ec;
1246
1247 int64_t max_enum_value = max_ec->u.s_enum_constant;
1248 lint_assert(INT_MIN <= max_enum_value && max_enum_value <= INT_MAX);
1249
1250 int max_array_index = ltp->u.dimension - 1;
1251 if (max_enum_value == max_array_index)
1252 return;
1253
1254 if (max_enum_value == max_array_index + 1 &&
1255 (strstr(max_ec->s_name, "MAX") != NULL ||
1256 strstr(max_ec->s_name, "max") != NULL ||
1257 strstr(max_ec->s_name, "NUM") != NULL ||
1258 strstr(max_ec->s_name, "num") != NULL ||
1259 strncmp(max_ec->s_name, "N_", 2) == 0))
1260 return;
1261
1262 /* maximum value %d for '%s' of type '%s' does not match maximum array index %d */
1263 warning(348, (int)max_enum_value, max_ec->s_name, type_name(rtp),
1264 max_array_index);
1265 print_previous_declaration(max_ec);
1266 }
1267
1268 static tnode_t *
1269 build_plus_minus(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
1270 {
1271
1272 if (rn->tn_type->t_tspec == PTR && is_integer(ln->tn_type->t_tspec)) {
1273 tnode_t *tmp = ln;
1274 ln = rn;
1275 rn = tmp;
1276 /* pointer addition has integer on the left-hand side */
1277 query_message(5);
1278 }
1279
1280 /* pointer +- integer */
1281 tspec_t lt = ln->tn_type->t_tspec;
1282 tspec_t rt = rn->tn_type->t_tspec;
1283 if (lt == PTR && rt != PTR) {
1284 lint_assert(is_integer(rt));
1285
1286 check_ctype_macro_invocation(ln, rn);
1287 check_enum_array_index(ln, rn);
1288
1289 tnode_t *elsz = subt_size_in_bytes(ln->tn_type);
1290 tspec_t szt = elsz->tn_type->t_tspec;
1291 if (rt != szt && rt != unsigned_type(szt))
1292 rn = convert(NOOP, 0, elsz->tn_type, rn);
1293
1294 tnode_t *prod = build_op(MULT, sys, rn->tn_type, rn, elsz);
1295 if (rn->tn_op == CON)
1296 prod = fold_constant_integer(prod);
1297
1298 return build_op(op, sys, ln->tn_type, ln, prod);
1299 }
1300
1301 /* pointer - pointer */
1302 if (rt == PTR) {
1303 lint_assert(lt == PTR);
1304 lint_assert(op == MINUS);
1305
1306 type_t *ptrdiff = gettyp(PTRDIFF_TSPEC);
1307 tnode_t *raw_diff = build_op(op, sys, ptrdiff, ln, rn);
1308 if (ln->tn_op == CON && rn->tn_op == CON)
1309 raw_diff = fold_constant_integer(raw_diff);
1310
1311 tnode_t *elsz = subt_size_in_bytes(ln->tn_type);
1312 balance(NOOP, &raw_diff, &elsz);
1313
1314 return build_op(DIV, sys, ptrdiff, raw_diff, elsz);
1315 }
1316
1317 return build_op(op, sys, ln->tn_type, ln, rn);
1318 }
1319
1320 static tnode_t *
1321 build_bit_shift(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
1322 {
1323
1324 if (!allow_c90 && rn->tn_type->t_tspec != INT)
1325 // XXX: C1978 7.5 says: "Both [operators] perform the usual
1326 // arithmetic conversions on their operands."
1327 // TODO: Add a test to exercise this part of the code.
1328 rn = convert(NOOP, 0, gettyp(INT), rn);
1329 return build_op(op, sys, ln->tn_type, ln, rn);
1330 }
1331
1332 static bool
1333 is_null_pointer(const tnode_t *tn)
1334 {
1335 tspec_t t = tn->tn_type->t_tspec;
1336
1337 // TODO: Investigate how other pointers are stored, in particular,
1338 // whether a pointer constant can have a non-zero value.
1339 // If not, simplify the code below.
1340 return ((t == PTR && tn->tn_type->t_subt->t_tspec == VOID)
1341 || is_integer(t))
1342 && (tn->tn_op == CON && tn->u.value.u.integer == 0);
1343 }
1344
1345 /* Return a type based on tp1, with added qualifiers from tp2. */
1346 static type_t *
1347 merge_qualifiers(type_t *tp1, const type_t *tp2)
1348 {
1349
1350 lint_assert(tp1->t_tspec == PTR);
1351 lint_assert(tp2->t_tspec == PTR);
1352
1353 bool c1 = tp1->t_subt->t_const;
1354 bool c2 = tp2->t_subt->t_const;
1355 bool v1 = tp1->t_subt->t_volatile;
1356 bool v2 = tp2->t_subt->t_volatile;
1357
1358 if (c1 == (c1 | c2) && v1 == (v1 | v2))
1359 return tp1;
1360
1361 type_t *nstp = expr_dup_type(tp1->t_subt);
1362 nstp->t_const |= c2;
1363 nstp->t_volatile |= v2;
1364
1365 type_t *ntp = expr_dup_type(tp1);
1366 ntp->t_subt = nstp;
1367 return ntp;
1368 }
1369
1370 /* See C99 6.5.15 "Conditional operator". */
1371 static tnode_t *
1372 build_colon(bool sys, tnode_t *ln, tnode_t *rn)
1373 {
1374 tspec_t lt = ln->tn_type->t_tspec;
1375 tspec_t rt = rn->tn_type->t_tspec;
1376
1377 type_t *tp;
1378 if (is_arithmetic(lt) && is_arithmetic(rt))
1379 /* The operands were already balanced in build_binary. */
1380 tp = ln->tn_type;
1381 else if (lt == BOOL && rt == BOOL)
1382 tp = ln->tn_type;
1383 else if (lt == VOID || rt == VOID)
1384 tp = gettyp(VOID);
1385 else if (is_struct_or_union(lt)) {
1386 lint_assert(is_struct_or_union(rt));
1387 lint_assert(ln->tn_type->u.sou == rn->tn_type->u.sou);
1388 if (is_incomplete(ln->tn_type)) {
1389 /* unknown operand size, op '%s' */
1390 error(138, op_name(COLON));
1391 return NULL;
1392 }
1393 tp = ln->tn_type;
1394 } else if (lt == PTR && is_integer(rt)) {
1395 if (rt != PTRDIFF_TSPEC)
1396 rn = convert(NOOP, 0, gettyp(PTRDIFF_TSPEC), rn);
1397 tp = ln->tn_type;
1398 } else if (rt == PTR && is_integer(lt)) {
1399 if (lt != PTRDIFF_TSPEC)
1400 ln = convert(NOOP, 0, gettyp(PTRDIFF_TSPEC), ln);
1401 tp = rn->tn_type;
1402 } else if (lt == PTR && is_null_pointer(rn))
1403 tp = merge_qualifiers(ln->tn_type, rn->tn_type);
1404 else if (rt == PTR && is_null_pointer(ln))
1405 tp = merge_qualifiers(rn->tn_type, ln->tn_type);
1406 else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID)
1407 tp = merge_qualifiers(ln->tn_type, rn->tn_type);
1408 else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID)
1409 tp = merge_qualifiers(rn->tn_type, ln->tn_type);
1410 else {
1411 /*
1412 * XXX For now we simply take the left type. This is probably
1413 * wrong, if one type contains a function prototype and the
1414 * other one, at the same place, only an old-style declaration.
1415 */
1416 tp = merge_qualifiers(ln->tn_type, rn->tn_type);
1417 }
1418
1419 return build_op(COLON, sys, tp, ln, rn);
1420 }
1421
1422 /* TODO: check for varargs */
1423 static bool
1424 is_cast_redundant(const tnode_t *tn)
1425 {
1426 const type_t *ntp = tn->tn_type, *otp = tn->u.ops.left->tn_type;
1427 tspec_t nt = ntp->t_tspec, ot = otp->t_tspec;
1428
1429 if (nt == BOOL || ot == BOOL)
1430 return nt == BOOL && ot == BOOL;
1431
1432 if (is_integer(nt) && is_integer(ot)) {
1433 unsigned int nw = width_in_bits(ntp), ow = width_in_bits(otp);
1434 if (is_uinteger(nt) == is_uinteger(ot))
1435 return nw >= ow;
1436 return is_uinteger(ot) && nw > ow;
1437 }
1438
1439 if (is_complex(nt) || is_complex(ot))
1440 return is_complex(nt) && is_complex(ot) &&
1441 size_in_bits(nt) >= size_in_bits(ot);
1442
1443 if (is_floating(nt) && is_floating(ot))
1444 return size_in_bits(nt) >= size_in_bits(ot);
1445
1446 if (nt == PTR && ot == PTR) {
1447 if (!ntp->t_subt->t_const && otp->t_subt->t_const)
1448 return false;
1449 if (!ntp->t_subt->t_volatile && otp->t_subt->t_volatile)
1450 return false;
1451
1452 if (ntp->t_subt->t_tspec == VOID ||
1453 otp->t_subt->t_tspec == VOID ||
1454 types_compatible(ntp->t_subt, otp->t_subt,
1455 false, false, NULL))
1456 return true;
1457 }
1458
1459 return false;
1460 }
1461
1462 static bool
1463 is_assignment(op_t op)
1464 {
1465
1466 return op == ASSIGN ||
1467 op == MULASS ||
1468 op == DIVASS ||
1469 op == MODASS ||
1470 op == ADDASS ||
1471 op == SUBASS ||
1472 op == SHLASS ||
1473 op == SHRASS ||
1474 op == ANDASS ||
1475 op == XORASS ||
1476 op == ORASS ||
1477 op == RETURN ||
1478 op == INIT;
1479 }
1480
1481 static tnode_t *
1482 build_assignment(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
1483 {
1484
1485 tspec_t lt = ln->tn_type->t_tspec;
1486 tspec_t rt = rn->tn_type->t_tspec;
1487
1488 if (any_query_enabled && is_assignment(rn->tn_op)) {
1489 /* chained assignment with '%s' and '%s' */
1490 query_message(10, op_name(op), op_name(rn->tn_op));
1491 }
1492
1493 if ((op == ADDASS || op == SUBASS) && lt == PTR) {
1494 lint_assert(is_integer(rt));
1495 tnode_t *ctn = subt_size_in_bytes(ln->tn_type);
1496 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
1497 rn = convert(NOOP, 0, ctn->tn_type, rn);
1498 rn = build_op(MULT, sys, rn->tn_type, rn, ctn);
1499 if (rn->u.ops.left->tn_op == CON)
1500 rn = fold_constant_integer(rn);
1501 }
1502
1503 if ((op == ASSIGN || op == RETURN || op == INIT) &&
1504 (lt == STRUCT || rt == STRUCT)) {
1505 lint_assert(lt == rt);
1506 lint_assert(ln->tn_type->u.sou == rn->tn_type->u.sou);
1507 if (is_incomplete(ln->tn_type)) {
1508 if (op == RETURN)
1509 /* cannot return incomplete type */
1510 error(212);
1511 else
1512 /* unknown operand size, op '%s' */
1513 error(138, op_name(op));
1514 return NULL;
1515 }
1516 }
1517
1518 if (op == SHLASS && hflag && allow_trad && allow_c90
1519 && portable_rank_cmp(lt, rt) < 0)
1520 /* semantics of '%s' change in C90; ... */
1521 warning(118, "<<=");
1522
1523 if (op != SHLASS && op != SHRASS
1524 && (op == ASSIGN || lt != PTR)
1525 && (lt != rt || (ln->tn_type->t_bitfield && rn->tn_op == CON))) {
1526 rn = convert(op, 0, ln->tn_type, rn);
1527 rt = lt;
1528 }
1529
1530 if (is_query_enabled[20]
1531 && lt == PTR && ln->tn_type->t_subt->t_tspec != VOID
1532 && rt == PTR && rn->tn_type->t_subt->t_tspec == VOID
1533 && !is_null_pointer(rn))
1534 /* implicit narrowing conversion from void ... */
1535 query_message(20, type_name(ln->tn_type));
1536
1537 if (any_query_enabled && rn->tn_op == CVT && rn->tn_cast &&
1538 types_compatible(ln->tn_type, rn->tn_type, false, false, NULL) &&
1539 is_cast_redundant(rn)) {
1540 /* redundant cast from '%s' to '%s' before assignment */
1541 query_message(7, type_name(rn->u.ops.left->tn_type),
1542 type_name(rn->tn_type));
1543 }
1544
1545 return build_op(op, sys, ln->tn_type, ln, rn);
1546 }
1547
1548 static tnode_t *
1549 build_real_imag(op_t op, bool sys, tnode_t *ln)
1550 {
1551
1552 lint_assert(ln != NULL);
1553 if (ln->tn_op == NAME) {
1554 /*
1555 * This may be too much, but it avoids wrong warnings. See
1556 * d_c99_complex_split.c.
1557 */
1558 mark_as_used(ln->u.sym, false, false);
1559 mark_as_set(ln->u.sym);
1560 }
1561
1562 tspec_t t;
1563 switch (ln->tn_type->t_tspec) {
1564 case LCOMPLEX:
1565 t = LDOUBLE;
1566 break;
1567 case DCOMPLEX:
1568 t = DOUBLE;
1569 break;
1570 case FCOMPLEX:
1571 t = FLOAT;
1572 break;
1573 default:
1574 /* '__%s__' is illegal for type '%s' */
1575 error(276, op == REAL ? "real" : "imag",
1576 type_name(ln->tn_type));
1577 return NULL;
1578 }
1579
1580 tnode_t *ntn = build_op(op, sys, gettyp(t), ln, NULL);
1581 ntn->tn_lvalue = true;
1582 return ntn;
1583 }
1584
1585 static bool
1586 is_confusing_precedence(op_t op, op_t lop, bool lparen, op_t rop, bool rparen)
1587 {
1588
1589 if (op == SHL || op == SHR) {
1590 if (!lparen && (lop == PLUS || lop == MINUS))
1591 return true;
1592 if (!rparen && (rop == PLUS || rop == MINUS))
1593 return true;
1594 return false;
1595 }
1596
1597 if (op == LOGOR) {
1598 if (!lparen && lop == LOGAND)
1599 return true;
1600 if (!rparen && rop == LOGAND)
1601 return true;
1602 return false;
1603 }
1604
1605 lint_assert(op == BITAND || op == BITXOR || op == BITOR);
1606 if (!lparen && lop != op) {
1607 if (lop == PLUS || lop == MINUS)
1608 return true;
1609 if (lop == BITAND || lop == BITXOR)
1610 return true;
1611 }
1612 if (!rparen && rop != op) {
1613 if (rop == PLUS || rop == MINUS)
1614 return true;
1615 if (rop == BITAND || rop == BITXOR)
1616 return true;
1617 }
1618 return false;
1619 }
1620
1621 /*
1622 * Print a warning if the given node has operands which should be
1623 * parenthesized.
1624 *
1625 * XXX Does not work if an operand is a constant expression. Constant
1626 * expressions are already folded.
1627 */
1628 static void
1629 check_precedence_confusion(tnode_t *tn)
1630 {
1631 tnode_t *ln, *rn;
1632
1633 if (!hflag)
1634 return;
1635
1636 debug_node(tn);
1637
1638 lint_assert(is_binary(tn));
1639 for (ln = tn->u.ops.left; ln->tn_op == CVT; ln = ln->u.ops.left)
1640 continue;
1641 for (rn = tn->u.ops.right; rn->tn_op == CVT; rn = rn->u.ops.left)
1642 continue;
1643
1644 if (is_confusing_precedence(tn->tn_op,
1645 ln->tn_op, ln->tn_parenthesized,
1646 rn->tn_op, rn->tn_parenthesized)) {
1647 /* precedence confusion possible: parenthesize! */
1648 warning(169);
1649 }
1650 }
1651
1652 static tnode_t *
1653 fold_constant_compare_zero(tnode_t *tn)
1654 {
1655
1656 val_t *v = xcalloc(1, sizeof(*v));
1657 v->v_tspec = tn->tn_type->t_tspec;
1658 lint_assert(v->v_tspec == INT || (Tflag && v->v_tspec == BOOL));
1659
1660 lint_assert(has_operands(tn));
1661 bool l = constant_is_nonzero(tn->u.ops.left);
1662 bool r = is_binary(tn) && constant_is_nonzero(tn->u.ops.right);
1663
1664 switch (tn->tn_op) {
1665 case NOT:
1666 if (hflag && !suppress_constcond)
1667 /* constant operand to '!' */
1668 warning(239);
1669 v->u.integer = !l ? 1 : 0;
1670 break;
1671 case LOGAND:
1672 v->u.integer = l && r ? 1 : 0;
1673 break;
1674 case LOGOR:
1675 v->u.integer = l || r ? 1 : 0;
1676 break;
1677 default:
1678 lint_assert(/*CONSTCOND*/false);
1679 }
1680
1681 return build_constant(tn->tn_type, v);
1682 }
1683
1684 static long double
1685 floating_error_value(tspec_t t, long double lv)
1686 {
1687 if (t == FLOAT)
1688 return lv < 0 ? -FLT_MAX : FLT_MAX;
1689 if (t == DOUBLE)
1690 return lv < 0 ? -DBL_MAX : DBL_MAX;
1691 /*
1692 * When NetBSD is cross-built in MKLINT=yes mode on x86_64 for sparc64,
1693 * tools/lint checks this code while building usr.bin/xlint. In that
1694 * situation, lint uses the preprocessor for sparc64, in which the type
1695 * 'long double' is IEEE-754-binary128, affecting the macro LDBL_MAX
1696 * below. The type 'long double', as well as the strtold
1697 * implementation, comes from the host platform x86_64 though, where
1698 * 'long double' consumes 128 bits as well but only uses 80 of them.
1699 * The exponent range of the two 'long double' types is the same, but
1700 * the maximum finite value differs due to the extended precision on
1701 * sparc64.
1702 *
1703 * To properly handle the data types of the target platform, lint would
1704 * have to implement the floating-point types in a platform-independent
1705 * way, which is not worth the effort, given how few programs
1706 * practically use 'long double'.
1707 */
1708 /* LINTED 248: floating-point constant out of range */
1709 long double max = LDBL_MAX;
1710 return lv < 0 ? -max : max;
1711 }
1712
1713 static bool
1714 is_floating_overflow(tspec_t t, long double val)
1715 {
1716 if (fpe != 0 || isfinite(val) == 0)
1717 return true;
1718 if (t == FLOAT && (val > FLT_MAX || val < -FLT_MAX))
1719 return true;
1720 if (t == DOUBLE && (val > DBL_MAX || val < -DBL_MAX))
1721 return true;
1722 return false;
1723 }
1724
1725 static tnode_t *
1726 fold_constant_floating(tnode_t *tn)
1727 {
1728
1729 fpe = 0;
1730
1731 tspec_t t = tn->tn_type->t_tspec;
1732
1733 val_t *v = xcalloc(1, sizeof(*v));
1734 v->v_tspec = t;
1735
1736 lint_assert(is_floating(t));
1737 lint_assert(has_operands(tn));
1738 lint_assert(t == tn->u.ops.left->tn_type->t_tspec);
1739 lint_assert(!is_binary(tn) || t == tn->u.ops.right->tn_type->t_tspec);
1740
1741 long double lv = tn->u.ops.left->u.value.u.floating;
1742 long double rv = is_binary(tn) ? tn->u.ops.right->u.value.u.floating
1743 : 0.0;
1744
1745 switch (tn->tn_op) {
1746 case UPLUS:
1747 v->u.floating = lv;
1748 break;
1749 case UMINUS:
1750 v->u.floating = -lv;
1751 break;
1752 case MULT:
1753 v->u.floating = lv * rv;
1754 break;
1755 case DIV:
1756 if (rv == 0.0) {
1757 /* division by 0 */
1758 error(139);
1759 v->u.floating = floating_error_value(t, lv);
1760 } else {
1761 v->u.floating = lv / rv;
1762 }
1763 break;
1764 case PLUS:
1765 v->u.floating = lv + rv;
1766 break;
1767 case MINUS:
1768 v->u.floating = lv - rv;
1769 break;
1770 case LT:
1771 v->u.integer = lv < rv ? 1 : 0;
1772 break;
1773 case LE:
1774 v->u.integer = lv <= rv ? 1 : 0;
1775 break;
1776 case GE:
1777 v->u.integer = lv >= rv ? 1 : 0;
1778 break;
1779 case GT:
1780 v->u.integer = lv > rv ? 1 : 0;
1781 break;
1782 case EQ:
1783 v->u.integer = lv == rv ? 1 : 0;
1784 break;
1785 case NE:
1786 v->u.integer = lv != rv ? 1 : 0;
1787 break;
1788 default:
1789 lint_assert(/*CONSTCOND*/false);
1790 }
1791
1792 // XXX: Must not access u.floating after setting u.integer.
1793 lint_assert(fpe != 0 || isnan(v->u.floating) == 0);
1794 if (is_complex(v->v_tspec)) {
1795 /*
1796 * Don't warn, as lint doesn't model the imaginary part of
1797 * complex numbers.
1798 */
1799 fpe = 0;
1800 } else if (is_floating_overflow(t, v->u.floating)) {
1801 /* operator '%s' produces floating point overflow */
1802 warning(142, op_name(tn->tn_op));
1803 v->u.floating = floating_error_value(t, v->u.floating);
1804 fpe = 0;
1805 }
1806
1807 return build_constant(tn->tn_type, v);
1808 }
1809
1810 static void
1811 use(const tnode_t *tn)
1812 {
1813 if (tn == NULL)
1814 return;
1815 switch (tn->tn_op) {
1816 case NAME:
1817 mark_as_used(tn->u.sym, false /* XXX */, false /* XXX */);
1818 break;
1819 case CON:
1820 case STRING:
1821 break;
1822 case CALL:;
1823 const function_call *call = tn->u.call;
1824 for (size_t i = 0, n = call->args_len; i < n; i++)
1825 use(call->args[i]);
1826 break;
1827 default:
1828 lint_assert(has_operands(tn));
1829 use(tn->u.ops.left);
1830 if (is_binary(tn))
1831 use(tn->u.ops.right);
1832 }
1833 }
1834
1835 /*
1836 * Create a tree node for a binary operator and its two operands. Also called
1837 * for unary operators; in that case rn is NULL.
1838 *
1839 * Function calls, sizeof and casts are handled elsewhere.
1840 */
1841 tnode_t *
1842 build_binary(tnode_t *ln, op_t op, bool sys, tnode_t *rn)
1843 {
1844 const mod_t *mp = &modtab[op];
1845
1846 /* If there was an error in one of the operands, return. */
1847 if (ln == NULL || (mp->m_binary && rn == NULL))
1848 return NULL;
1849
1850 if (mp->m_value_context || mp->m_compares_with_zero)
1851 ln = cconv(ln);
1852 if (mp->m_binary && op != ARROW && op != POINT)
1853 rn = cconv(rn);
1854
1855 if (mp->m_comparison)
1856 check_integer_comparison(op, ln, rn);
1857
1858 if (mp->m_value_context || mp->m_compares_with_zero)
1859 ln = promote(op, false, ln);
1860 if (mp->m_binary && op != ARROW && op != POINT &&
1861 op != ASSIGN && op != RETURN && op != INIT)
1862 rn = promote(op, false, rn);
1863
1864 if (mp->m_warn_if_left_unsigned_in_c90 &&
1865 ln->tn_op == CON && ln->u.value.v_unsigned_since_c90) {
1866 /* C90 treats constant as unsigned, op '%s' */
1867 warning(218, op_name(op));
1868 ln->u.value.v_unsigned_since_c90 = false;
1869 }
1870 if (mp->m_warn_if_right_unsigned_in_c90 &&
1871 rn->tn_op == CON && rn->u.value.v_unsigned_since_c90) {
1872 /* C90 treats constant as unsigned, op '%s' */
1873 warning(218, op_name(op));
1874 rn->u.value.v_unsigned_since_c90 = false;
1875 }
1876
1877 if (mp->m_balance_operands || (!allow_c90 && (op == SHL || op == SHR)))
1878 balance(op, &ln, &rn);
1879
1880 if (!typeok(op, 0, ln, rn))
1881 return NULL;
1882
1883 tnode_t *ntn;
1884 switch (op) {
1885 case POINT:
1886 case ARROW:
1887 ntn = build_struct_access(op, sys, ln, rn);
1888 break;
1889 case NOT:
1890 if (ln->tn_op == ASSIGN && ln->u.ops.right->tn_op == CON) {
1891 /* constant assignment of type '%s' in operand ... */
1892 warning(382, type_name(ln->tn_type),
1893 is_nonzero_val(&ln->u.ops.right->u.value)
1894 ? "true" : "false");
1895 }
1896 ntn = build_op(op, sys, gettyp(Tflag ? BOOL : INT), ln, NULL);
1897 break;
1898 case INCAFT:
1899 case DECAFT:
1900 case INCBEF:
1901 case DECBEF:
1902 ntn = build_prepost_incdec(op, sys, ln);
1903 break;
1904 case ADDR:
1905 ntn = build_address(sys, ln, false);
1906 break;
1907 case INDIR:
1908 ntn = build_op(INDIR, sys, ln->tn_type->t_subt, ln, NULL);
1909 break;
1910 case PLUS:
1911 case MINUS:
1912 ntn = build_plus_minus(op, sys, ln, rn);
1913 break;
1914 case SHL:
1915 case SHR:
1916 ntn = build_bit_shift(op, sys, ln, rn);
1917 break;
1918 case COLON:
1919 ntn = build_colon(sys, ln, rn);
1920 break;
1921 case ASSIGN:
1922 case MULASS:
1923 case DIVASS:
1924 case MODASS:
1925 case ADDASS:
1926 case SUBASS:
1927 case SHLASS:
1928 case SHRASS:
1929 case ANDASS:
1930 case XORASS:
1931 case ORASS:
1932 case RETURN:
1933 case INIT:
1934 ntn = build_assignment(op, sys, ln, rn);
1935 break;
1936 case COMMA:
1937 if (any_query_enabled) {
1938 /* comma operator with types '%s' and '%s' */
1939 query_message(12,
1940 type_name(ln->tn_type), type_name(rn->tn_type));
1941 }
1942 /* FALLTHROUGH */
1943 case QUEST:
1944 ntn = build_op(op, sys, rn->tn_type, ln, rn);
1945 break;
1946 case REAL:
1947 case IMAG:
1948 ntn = build_real_imag(op, sys, ln);
1949 break;
1950 default:
1951 lint_assert(mp->m_binary == (rn != NULL));
1952 type_t *rettp = mp->m_returns_bool
1953 ? gettyp(Tflag ? BOOL : INT) : ln->tn_type;
1954 ntn = build_op(op, sys, rettp, ln, rn);
1955 break;
1956 }
1957
1958 if (ntn == NULL)
1959 return NULL;
1960
1961 if (mp->m_possible_precedence_confusion)
1962 check_precedence_confusion(ntn);
1963
1964 if (hflag && !suppress_constcond &&
1965 mp->m_compares_with_zero &&
1966 (ln->tn_op == CON ||
1967 (mp->m_binary && op != QUEST && rn->tn_op == CON)) &&
1968 /* XXX: rn->tn_system_dependent should be checked as well */
1969 !ln->tn_system_dependent)
1970 /* constant in conditional context */
1971 warning(161);
1972
1973 if (mp->m_fold_constant_operands && ln->tn_op == CON) {
1974 if (!mp->m_binary || rn->tn_op == CON) {
1975 if (mp->m_compares_with_zero)
1976 ntn = fold_constant_compare_zero(ntn);
1977 else if (is_floating(ntn->tn_type->t_tspec))
1978 ntn = fold_constant_floating(ntn);
1979 else
1980 ntn = fold_constant_integer(ntn);
1981 } else if (op == QUEST) {
1982 lint_assert(has_operands(rn));
1983 use(ln->u.value.u.integer != 0
1984 ? rn->u.ops.right : rn->u.ops.left);
1985 ntn = ln->u.value.u.integer != 0
1986 ? rn->u.ops.left : rn->u.ops.right;
1987 }
1988 }
1989
1990 return ntn;
1991 }
1992
1993 tnode_t *
1994 build_unary(op_t op, bool sys, tnode_t *tn)
1995 {
1996 return build_binary(tn, op, sys, NULL);
1997 }
1998
1999 static bool
2000 are_members_compatible(const sym_t *a, const sym_t *b)
2001 {
2002 if (a->u.s_member.sm_offset_in_bits != b->u.s_member.sm_offset_in_bits)
2003 return false;
2004
2005 const type_t *atp = a->s_type;
2006 const type_t *btp = b->s_type;
2007 bool w = false;
2008 if (!types_compatible(atp, btp, false, false, &w) && !w)
2009 return false;
2010 if (a->s_bitfield != b->s_bitfield)
2011 return false;
2012 if (a->s_bitfield) {
2013 if (atp->t_bit_field_width != btp->t_bit_field_width)
2014 return false;
2015 if (atp->t_bit_field_offset != btp->t_bit_field_offset)
2016 return false;
2017 }
2018 return true;
2019 }
2020
2021 /*
2022 * Return whether all struct/union members with the same name have the same
2023 * type and offset.
2024 */
2025 static bool
2026 all_members_compatible(const sym_t *msym)
2027 {
2028 for (const sym_t *csym = msym;
2029 csym != NULL; csym = csym->s_symtab_next) {
2030 if (!is_member(csym))
2031 continue;
2032 if (strcmp(msym->s_name, csym->s_name) != 0)
2033 continue;
2034
2035 for (const sym_t *sym = csym->s_symtab_next;
2036 sym != NULL; sym = sym->s_symtab_next) {
2037 if (is_member(sym)
2038 && strcmp(csym->s_name, sym->s_name) == 0
2039 && !are_members_compatible(csym, sym))
2040 return false;
2041 }
2042 }
2043 return true;
2044 }
2045
2046 sym_t *
2047 find_member(const struct_or_union *sou, const char *name)
2048 {
2049 for (sym_t *mem = sou->sou_first_member;
2050 mem != NULL; mem = mem->s_next) {
2051 lint_assert(is_member(mem));
2052 lint_assert(mem->u.s_member.sm_containing_type == sou);
2053 if (strcmp(mem->s_name, name) == 0)
2054 return mem;
2055 }
2056
2057 for (sym_t *mem = sou->sou_first_member;
2058 mem != NULL; mem = mem->s_next) {
2059 if (is_struct_or_union(mem->s_type->t_tspec)
2060 && mem->s_name == unnamed) {
2061 sym_t *nested_mem =
2062 find_member(mem->s_type->u.sou, name);
2063 if (nested_mem != NULL)
2064 return nested_mem;
2065 }
2066 }
2067 return NULL;
2068 }
2069
2070 /*
2071 * Remove the member if it was unknown until now, which means
2072 * that no defined struct or union has a member with the same name.
2073 */
2074 static void
2075 remove_unknown_member(tnode_t *tn, sym_t *msym)
2076 {
2077 /* type '%s' does not have member '%s' */
2078 error(101, type_name(tn->tn_type), msym->s_name);
2079 symtab_remove_forever(msym);
2080 msym->s_kind = SK_MEMBER;
2081 msym->s_scl = STRUCT_MEMBER;
2082
2083 struct_or_union *sou = expr_zero_alloc(sizeof(*sou),
2084 "struct_or_union");
2085 sou->sou_tag = expr_zero_alloc(sizeof(*sou->sou_tag), "sym");
2086 sou->sou_tag->s_name = unnamed;
2087
2088 msym->u.s_member.sm_containing_type = sou;
2089 /*
2090 * The member sm_offset_in_bits is not needed here since this symbol
2091 * can only be used for error reporting.
2092 */
2093 }
2094
2095 /*
2096 * Returns a symbol which has the same name as 'msym' and is a member of the
2097 * struct or union specified by 'tn'.
2098 */
2099 static sym_t *
2100 struct_or_union_member(tnode_t *tn, op_t op, sym_t *msym)
2101 {
2102
2103 /* Determine the tag type of which msym is expected to be a member. */
2104 const type_t *tp = NULL;
2105 if (op == POINT && is_struct_or_union(tn->tn_type->t_tspec))
2106 tp = tn->tn_type;
2107 if (op == ARROW && tn->tn_type->t_tspec == PTR
2108 && is_struct_or_union(tn->tn_type->t_subt->t_tspec))
2109 tp = tn->tn_type->t_subt;
2110 struct_or_union *sou = tp != NULL ? tp->u.sou : NULL;
2111
2112 if (sou != NULL) {
2113 sym_t *nested_mem = find_member(sou, msym->s_name);
2114 if (nested_mem != NULL)
2115 return nested_mem;
2116 }
2117
2118 if (msym->s_scl == NO_SCL) {
2119 remove_unknown_member(tn, msym);
2120 return msym;
2121 }
2122
2123 bool eq = all_members_compatible(msym);
2124
2125 /*
2126 * Now handle the case in which the left operand refers really to a
2127 * struct/union, but the right operand is not member of it.
2128 */
2129 if (sou != NULL) {
2130 if (eq && !allow_c90)
2131 /* illegal use of member '%s' */
2132 warning(102, msym->s_name);
2133 else
2134 /* illegal use of member '%s' */
2135 error(102, msym->s_name);
2136 return msym;
2137 }
2138
2139 if (eq) {
2140 if (op == POINT) {
2141 if (!allow_c90)
2142 /* left operand of '.' must be struct ... */
2143 warning(103, type_name(tn->tn_type));
2144 else
2145 /* left operand of '.' must be struct ... */
2146 error(103, type_name(tn->tn_type));
2147 } else {
2148 if (!allow_c90 && tn->tn_type->t_tspec == PTR)
2149 /* left operand of '->' must be pointer ... */
2150 warning(104, type_name(tn->tn_type));
2151 else
2152 /* left operand of '->' must be pointer ... */
2153 error(104, type_name(tn->tn_type));
2154 }
2155 } else {
2156 if (!allow_c90)
2157 /* non-unique member requires struct/union %s */
2158 error(105, op == POINT ? "object" : "pointer");
2159 else
2160 /* unacceptable operand of '%s' */
2161 error(111, op_name(op));
2162 }
2163
2164 return msym;
2165 }
2166
2167 tnode_t *
2168 build_member_access(tnode_t *ln, op_t op, bool sys, sbuf_t *member)
2169 {
2170 sym_t *msym;
2171
2172 if (ln == NULL)
2173 return NULL;
2174
2175 if (op == ARROW)
2176 /* must do this before struct_or_union_member is called */
2177 ln = cconv(ln);
2178 msym = struct_or_union_member(ln, op, getsym(member));
2179 return build_binary(ln, op, sys, build_name(msym, false));
2180 }
2181
2182 /*
2183 * Perform class conversions.
2184 *
2185 * Arrays of type T are converted into pointers to type T.
2186 * Functions are converted to pointers to functions.
2187 * Lvalues are converted to rvalues.
2188 *
2189 * C99 6.3 "Conversions"
2190 * C99 6.3.2 "Other operands"
2191 * C99 6.3.2.1 "Lvalues, arrays, and function designators"
2192 */
2193 tnode_t *
2194 cconv(tnode_t *tn)
2195 {
2196 if (tn->tn_type->t_tspec == ARRAY) {
2197 if (!tn->tn_lvalue) {
2198 /* XXX print correct operator */
2199 /* %soperand of '%s' must be lvalue */
2200 gnuism(114, "", op_name(ADDR));
2201 }
2202 tn = build_op(ADDR, tn->tn_sys,
2203 expr_derive_type(tn->tn_type->t_subt, PTR), tn, NULL);
2204 }
2205
2206 if (tn->tn_type->t_tspec == FUNC)
2207 tn = build_address(tn->tn_sys, tn, true);
2208
2209 if (tn->tn_lvalue) {
2210 type_t *tp = expr_dup_type(tn->tn_type);
2211 /* C99 6.3.2.1p2 sentence 2 says to remove the qualifiers. */
2212 tp->t_const = tp->t_volatile = false;
2213 tn = build_op(LOAD, tn->tn_sys, tp, tn, NULL);
2214 }
2215
2216 return tn;
2217 }
2218
2219 const tnode_t *
2220 before_conversion(const tnode_t *tn)
2221 {
2222 while (tn->tn_op == CVT && !tn->tn_cast)
2223 tn = tn->u.ops.left;
2224 return tn;
2225 }
2226
2227 /*
2228 * Most errors required by C90 are reported in struct_or_union_member().
2229 * Here we only check for totally wrong things.
2230 */
2231 static bool
2232 typeok_point(const tnode_t *ln, const type_t *ltp, tspec_t lt)
2233 {
2234 if (is_struct_or_union(lt))
2235 return true;
2236
2237 if (lt == FUNC || lt == VOID || ltp->t_bitfield)
2238 goto wrong;
2239
2240 /*
2241 * Some C dialects from before C90 tolerated any lvalue on the
2242 * left-hand side of the '.' operator, allowing things like 'char
2243 * st[100]; st.st_mtime', assuming that the member 'st_mtime' only
2244 * occurred in a single struct; see typeok_arrow.
2245 */
2246 if (ln->tn_lvalue)
2247 return true;
2248
2249 wrong:
2250 /* With allow_c90 we already got an error */
2251 if (!allow_c90)
2252 /* unacceptable operand of '%s' */
2253 error(111, op_name(POINT));
2254
2255 return false;
2256 }
2257
2258 static bool
2259 typeok_arrow(tspec_t lt)
2260 {
2261 /*
2262 * C1978 Appendix A 14.1 says: <quote>In fact, any lvalue is allowed
2263 * before '.', and that lvalue is then assumed to have the form of the
2264 * structure of which the name of the right is a member. [...] Such
2265 * constructions are non-portable.</quote>
2266 */
2267 if (lt == PTR || (!allow_c90 && is_integer(lt)))
2268 return true;
2269
2270 /* With allow_c90 we already got an error */
2271 if (!allow_c90)
2272 /* unacceptable operand of '%s' */
2273 error(111, op_name(ARROW));
2274 return false;
2275 }
2276
2277 static bool
2278 typeok_incdec(op_t op, const tnode_t *tn, const type_t *tp)
2279 {
2280 /* operand has scalar type (checked in typeok) */
2281 if (!tn->tn_lvalue) {
2282 if (tn->tn_op == CVT && tn->tn_cast &&
2283 tn->u.ops.left->tn_op == LOAD)
2284 /* a cast does not yield an lvalue */
2285 error(163);
2286 /* %soperand of '%s' must be lvalue */
2287 error(114, "", op_name(op));
2288 return false;
2289 }
2290 if (tp->t_const && allow_c90)
2291 /* %soperand of '%s' must be modifiable lvalue */
2292 warning(115, "", op_name(op));
2293 return true;
2294 }
2295
2296 static bool
2297 typeok_address(op_t op, const tnode_t *tn, const type_t *tp, tspec_t t)
2298 {
2299 if (t == ARRAY || t == FUNC) {
2300 /* ok, a warning comes later (in build_address()) */
2301 } else if (!tn->tn_lvalue) {
2302 if (tn->tn_op == CVT && tn->tn_cast &&
2303 tn->u.ops.left->tn_op == LOAD)
2304 /* a cast does not yield an lvalue */
2305 error(163);
2306 /* %soperand of '%s' must be lvalue */
2307 error(114, "", op_name(op));
2308 return false;
2309 } else if (is_scalar(t)) {
2310 if (tp->t_bitfield) {
2311 /* cannot take address of bit-field */
2312 error(112);
2313 return false;
2314 }
2315 } else if (t != STRUCT && t != UNION) {
2316 /* unacceptable operand of '%s' */
2317 error(111, op_name(op));
2318 return false;
2319 }
2320 if (tn->tn_op == NAME && tn->u.sym->s_register) {
2321 /* cannot take address of register '%s' */
2322 error(113, tn->u.sym->s_name);
2323 return false;
2324 }
2325 return true;
2326 }
2327
2328 static bool
2329 typeok_indir(const type_t *tp, tspec_t t)
2330 {
2331
2332 if (t != PTR) {
2333 /* cannot dereference non-pointer type '%s' */
2334 error(96, type_name(tp));
2335 return false;
2336 }
2337 return true;
2338 }
2339
2340 static void
2341 warn_incompatible_types(op_t op,
2342 const type_t *ltp, tspec_t lt,
2343 const type_t *rtp, tspec_t rt)
2344 {
2345 bool binary = modtab[op].m_binary;
2346
2347 if (lt == VOID || (binary && rt == VOID)) {
2348 /* void type illegal in expression */
2349 error(109);
2350 } else if (op == ASSIGN)
2351 /* cannot assign to '%s' from '%s' */
2352 error(171, type_name(ltp), type_name(rtp));
2353 else if (binary)
2354 /* operands of '%s' have incompatible types '%s' and '%s' */
2355 error(107, op_name(op), type_name(ltp), type_name(rtp));
2356 else {
2357 lint_assert(rt == NO_TSPEC);
2358 /* operand of '%s' has invalid type '%s' */
2359 error(108, op_name(op), type_name(ltp));
2360 }
2361 }
2362
2363 static bool
2364 typeok_plus(op_t op,
2365 const type_t *ltp, tspec_t lt,
2366 const type_t *rtp, tspec_t rt)
2367 {
2368 /* operands have scalar types (checked in typeok) */
2369 if ((lt == PTR && !is_integer(rt)) || (rt == PTR && !is_integer(lt))) {
2370 warn_incompatible_types(op, ltp, lt, rtp, rt);
2371 return false;
2372 }
2373 return true;
2374 }
2375
2376 static bool
2377 typeok_minus(op_t op,
2378 const type_t *ltp, tspec_t lt,
2379 const type_t *rtp, tspec_t rt)
2380 {
2381 /* operands have scalar types (checked in typeok) */
2382 if ((lt == PTR && rt != PTR && !is_integer(rt)) ||
2383 (lt != PTR && rt == PTR)) {
2384 warn_incompatible_types(op, ltp, lt, rtp, rt);
2385 return false;
2386 }
2387 if (lt == PTR && rt == PTR &&
2388 !types_compatible(ltp->t_subt, rtp->t_subt, true, false, NULL)) {
2389 /* illegal pointer subtraction */
2390 error(116);
2391 }
2392 return true;
2393 }
2394
2395 static void
2396 typeok_shr(op_t op,
2397 const tnode_t *ln, tspec_t lt,
2398 const tnode_t *rn, tspec_t rt)
2399 {
2400 tspec_t olt = before_conversion(ln)->tn_type->t_tspec;
2401 tspec_t ort = before_conversion(rn)->tn_type->t_tspec;
2402
2403 /* operands have integer types (checked in typeok) */
2404 if (pflag && !is_uinteger(olt)) {
2405 integer_constraints lc = ic_expr(ln);
2406 if (!ic_maybe_signed(ln->tn_type, &lc))
2407 return;
2408
2409 if (ln->tn_op != CON)
2410 /* bitwise '%s' on signed value possibly nonportable */
2411 warning(117, op_name(op));
2412 else if (ln->u.value.u.integer < 0)
2413 /* bitwise '%s' on signed value nonportable */
2414 warning(120, op_name(op));
2415 } else if (allow_trad && allow_c90 &&
2416 !is_uinteger(olt) && is_uinteger(ort)) {
2417 /* The left operand would become unsigned in traditional C. */
2418 if (hflag && (ln->tn_op != CON || ln->u.value.u.integer < 0)) {
2419 /* semantics of '%s' change in C90; use ... */
2420 warning(118, op_name(op));
2421 }
2422 } else if (allow_trad && allow_c90 &&
2423 !is_uinteger(olt) && !is_uinteger(ort) &&
2424 portable_rank_cmp(lt, rt) < 0) {
2425 /*
2426 * In traditional C, the left operand would be extended
2427 * (possibly sign-extended) and then shifted.
2428 */
2429 if (hflag && (ln->tn_op != CON || ln->u.value.u.integer < 0)) {
2430 /* semantics of '%s' change in C90; use ... */
2431 warning(118, op_name(op));
2432 }
2433 }
2434 }
2435
2436 static void
2437 typeok_shl(op_t op, tspec_t lt, tspec_t rt)
2438 {
2439 /*
2440 * C90 does not perform balancing for shift operations, but traditional
2441 * C does. If the width of the right operand is greater than the width
2442 * of the left operand, then in traditional C the left operand would be
2443 * extended to the width of the right operand. For SHL this may result
2444 * in different results.
2445 */
2446 if (portable_rank_cmp(lt, rt) < 0) {
2447 /*
2448 * XXX If both operands are constant, make sure that there is
2449 * really a difference between C90 and traditional C.
2450 */
2451 if (hflag && allow_trad && allow_c90)
2452 /* semantics of '%s' change in C90; use ... */
2453 warning(118, op_name(op));
2454 }
2455 }
2456
2457 static void
2458 typeok_shift(const type_t *ltp, tspec_t lt, const tnode_t *rn, tspec_t rt)
2459 {
2460 if (rn->tn_op != CON)
2461 return;
2462
2463 if (!is_uinteger(rt) && rn->u.value.u.integer < 0)
2464 /* negative shift */
2465 warning(121);
2466 else if ((uint64_t)rn->u.value.u.integer == size_in_bits(lt))
2467 /* shift amount %u equals bit-size of '%s' */
2468 warning(267, (unsigned)rn->u.value.u.integer, type_name(ltp));
2469 else if ((uint64_t)rn->u.value.u.integer > size_in_bits(lt)) {
2470 /* shift amount %llu is greater than bit-size %llu of '%s' */
2471 warning(122, (unsigned long long)rn->u.value.u.integer,
2472 (unsigned long long)size_in_bits(lt),
2473 tspec_name(lt));
2474 }
2475 }
2476
2477 static bool
2478 is_typeok_eq(const tnode_t *ln, tspec_t lt, const tnode_t *rn, tspec_t rt)
2479 {
2480 if (lt == PTR && is_null_pointer(rn))
2481 return true;
2482 if (rt == PTR && is_null_pointer(ln))
2483 return true;
2484 return false;
2485 }
2486
2487 static void
2488 warn_incompatible_pointers(op_t op, const type_t *ltp, const type_t *rtp)
2489 {
2490 lint_assert(ltp->t_tspec == PTR);
2491 lint_assert(rtp->t_tspec == PTR);
2492
2493 tspec_t lt = ltp->t_subt->t_tspec;
2494 tspec_t rt = rtp->t_subt->t_tspec;
2495
2496 if (is_struct_or_union(lt) && is_struct_or_union(rt)) {
2497 if (op == RETURN)
2498 /* illegal structure pointer combination */
2499 warning(244);
2500 else {
2501 /* incompatible structure pointers: '%s' '%s' '%s' */
2502 warning(245, type_name(ltp),
2503 op_name(op), type_name(rtp));
2504 }
2505 } else {
2506 if (op == RETURN)
2507 /* illegal combination of '%s' and '%s' */
2508 warning(184, type_name(ltp), type_name(rtp));
2509 else {
2510 /* illegal combination of '%s' and '%s', op '%s' */
2511 warning(124,
2512 type_name(ltp), type_name(rtp), op_name(op));
2513 }
2514 }
2515 }
2516
2517 static void
2518 check_pointer_comparison(op_t op, const tnode_t *ln, const tnode_t *rn)
2519 {
2520 type_t *ltp = ln->tn_type, *rtp = rn->tn_type;
2521 tspec_t lst = ltp->t_subt->t_tspec, rst = rtp->t_subt->t_tspec;
2522
2523 if (lst == VOID || rst == VOID) {
2524 /* TODO: C99 behaves like C90 here. */
2525 if (!allow_trad && !allow_c99 &&
2526 (lst == FUNC || rst == FUNC)) {
2527 /* (void *)0 is already handled in typeok() */
2528 const char *lsts, *rsts;
2529 *(lst == FUNC ? &lsts : &rsts) = "function pointer";
2530 *(lst == VOID ? &lsts : &rsts) = "'void *'";
2531 /* C90 or later forbid comparison of %s with %s */
2532 warning(274, lsts, rsts);
2533 }
2534 return;
2535 }
2536
2537 if (!types_compatible(ltp->t_subt, rtp->t_subt, true, false, NULL)) {
2538 warn_incompatible_pointers(op, ltp, rtp);
2539 return;
2540 }
2541
2542 if (lst == FUNC && rst == FUNC) {
2543 /* TODO: C99 behaves like C90 here, see C99 6.5.8p2. */
2544 if (!allow_trad && !allow_c99 && op != EQ && op != NE)
2545 /* pointers to functions can only be compared ... */
2546 warning(125);
2547 }
2548 }
2549
2550 static bool
2551 typeok_compare(op_t op,
2552 const tnode_t *ln, const type_t *ltp, tspec_t lt,
2553 const tnode_t *rn, const type_t *rtp, tspec_t rt)
2554 {
2555 if (lt == PTR && rt == PTR) {
2556 check_pointer_comparison(op, ln, rn);
2557 return true;
2558 }
2559
2560 if (lt != PTR && rt != PTR)
2561 return true;
2562
2563 if (!is_integer(lt) && !is_integer(rt)) {
2564 warn_incompatible_types(op, ltp, lt, rtp, rt);
2565 return false;
2566 }
2567
2568 const char *lx = lt == PTR ? "pointer" : "integer";
2569 const char *rx = rt == PTR ? "pointer" : "integer";
2570 /* illegal combination of %s '%s' and %s '%s', op '%s' */
2571 warning(123, lx, type_name(ltp), rx, type_name(rtp), op_name(op));
2572 return true;
2573 }
2574
2575 static bool
2576 typeok_quest(tspec_t lt, const tnode_t *rn)
2577 {
2578 if (!is_scalar(lt)) {
2579 /* first operand of '?' must have scalar type */
2580 error(170);
2581 return false;
2582 }
2583 lint_assert(before_conversion(rn)->tn_op == COLON);
2584 return true;
2585 }
2586
2587 static void
2588 typeok_colon_pointer(const type_t *ltp, const type_t *rtp)
2589 {
2590 type_t *lstp = ltp->t_subt;
2591 type_t *rstp = rtp->t_subt;
2592 tspec_t lst = lstp->t_tspec;
2593 tspec_t rst = rstp->t_tspec;
2594
2595 if ((lst == VOID && rst == FUNC) || (lst == FUNC && rst == VOID)) {
2596 /* (void *)0 is handled in typeok_colon */
2597 /* TODO: C99 behaves like C90 here. */
2598 if (!allow_trad && !allow_c99)
2599 /* conversion of %s to %s requires a cast, op %s */
2600 warning(305, "function pointer", "'void *'",
2601 op_name(COLON));
2602 return;
2603 }
2604
2605 if (pointer_types_are_compatible(lstp, rstp, true))
2606 return;
2607 if (!types_compatible(lstp, rstp, true, false, NULL))
2608 warn_incompatible_pointers(COLON, ltp, rtp);
2609 }
2610
2611 static bool
2612 typeok_colon(const tnode_t *ln, const type_t *ltp, tspec_t lt,
2613 const tnode_t *rn, const type_t *rtp, tspec_t rt)
2614 {
2615
2616 if (is_arithmetic(lt) && is_arithmetic(rt))
2617 return true;
2618 if (lt == BOOL && rt == BOOL)
2619 return true;
2620
2621 if (lt == STRUCT && rt == STRUCT && ltp->u.sou == rtp->u.sou)
2622 return true;
2623 if (lt == UNION && rt == UNION && ltp->u.sou == rtp->u.sou)
2624 return true;
2625
2626 if (lt == PTR && is_null_pointer(rn))
2627 return true;
2628 if (rt == PTR && is_null_pointer(ln))
2629 return true;
2630
2631 if ((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)) {
2632 const char *lx = lt == PTR ? "pointer" : "integer";
2633 const char *rx = rt == PTR ? "pointer" : "integer";
2634 /* illegal combination of %s '%s' and %s '%s', op '%s' */
2635 warning(123, lx, type_name(ltp),
2636 rx, type_name(rtp), op_name(COLON));
2637 return true;
2638 }
2639
2640 if (lt == VOID || rt == VOID) {
2641 if (lt != VOID || rt != VOID)
2642 /* incompatible types '%s' and '%s' in conditional */
2643 warning(126, type_name(ltp), type_name(rtp));
2644 return true;
2645 }
2646
2647 if (lt == PTR && rt == PTR) {
2648 typeok_colon_pointer(ltp, rtp);
2649 return true;
2650 }
2651
2652 /* incompatible types '%s' and '%s' in conditional */
2653 error(126, type_name(ltp), type_name(rtp));
2654 return false;
2655 }
2656
2657 static bool
2658 has_constant_member(const type_t *tp)
2659 {
2660 lint_assert(is_struct_or_union(tp->t_tspec));
2661
2662 for (sym_t *m = tp->u.sou->sou_first_member;
2663 m != NULL; m = m->s_next) {
2664 const type_t *mtp = m->s_type;
2665 if (mtp->t_const)
2666 return true;
2667 if (is_struct_or_union(mtp->t_tspec) &&
2668 has_constant_member(mtp))
2669 return true;
2670 }
2671 return false;
2672 }
2673
2674 static bool
2675 typeok_assign(op_t op, const tnode_t *ln, const type_t *ltp, tspec_t lt)
2676 {
2677 if (op == RETURN || op == INIT || op == FARG)
2678 return true;
2679
2680 if (!ln->tn_lvalue) {
2681 if (ln->tn_op == CVT && ln->tn_cast &&
2682 ln->u.ops.left->tn_op == LOAD)
2683 /* a cast does not yield an lvalue */
2684 error(163);
2685 /* %soperand of '%s' must be lvalue */
2686 error(114, "left ", op_name(op));
2687 return false;
2688 } else if (ltp->t_const
2689 || (is_struct_or_union(lt) && has_constant_member(ltp))) {
2690 if (allow_c90)
2691 /* %soperand of '%s' must be modifiable lvalue */
2692 warning(115, "left ", op_name(op));
2693 }
2694 return true;
2695 }
2696
2697 static bool
2698 typeok_scalar(op_t op, const mod_t *mp,
2699 const type_t *ltp, tspec_t lt,
2700 const type_t *rtp, tspec_t rt)
2701 {
2702 if (mp->m_takes_bool && lt == BOOL && rt == BOOL)
2703 return true;
2704 if (mp->m_requires_integer) {
2705 if (!is_integer(lt) || (mp->m_binary && !is_integer(rt))) {
2706 warn_incompatible_types(op, ltp, lt, rtp, rt);
2707 return false;
2708 }
2709 } else if (mp->m_requires_integer_or_complex) {
2710 if ((!is_integer(lt) && !is_complex(lt)) ||
2711 (mp->m_binary && (!is_integer(rt) && !is_complex(rt)))) {
2712 warn_incompatible_types(op, ltp, lt, rtp, rt);
2713 return false;
2714 }
2715 } else if (mp->m_requires_scalar) {
2716 if (!is_scalar(lt) || (mp->m_binary && !is_scalar(rt))) {
2717 warn_incompatible_types(op, ltp, lt, rtp, rt);
2718 return false;
2719 }
2720 } else if (mp->m_requires_arith) {
2721 if (!is_arithmetic(lt) ||
2722 (mp->m_binary && !is_arithmetic(rt))) {
2723 warn_incompatible_types(op, ltp, lt, rtp, rt);
2724 return false;
2725 }
2726 }
2727 return true;
2728 }
2729
2730 static void
2731 check_assign_void_pointer(op_t op, int arg,
2732 tspec_t lt, tspec_t lst,
2733 tspec_t rt, tspec_t rst)
2734 {
2735
2736 if (!(lt == PTR && rt == PTR && (lst == VOID || rst == VOID)))
2737 return;
2738 /* two pointers, at least one pointer to void */
2739
2740 /* TODO: C99 behaves like C90 here. */
2741 if (!(!allow_trad && !allow_c99 && (lst == FUNC || rst == FUNC)))
2742 return;
2743 /* comb. of ptr to func and ptr to void */
2744
2745 const char *lts, *rts;
2746 *(lst == FUNC ? <s : &rts) = "function pointer";
2747 *(lst == VOID ? <s : &rts) = "'void *'";
2748
2749 switch (op) {
2750 case INIT:
2751 case RETURN:
2752 /* conversion of %s to %s requires a cast */
2753 warning(303, rts, lts);
2754 break;
2755 case FARG:
2756 /* conversion of %s to %s requires a cast, arg #%d */
2757 warning(304, rts, lts, arg);
2758 break;
2759 default:
2760 /* conversion of %s to %s requires a cast, op %s */
2761 warning(305, rts, lts, op_name(op));
2762 break;
2763 }
2764 }
2765
2766 static bool
2767 is_direct_function_call(const tnode_t *tn, const char **out_name)
2768 {
2769
2770 if (tn->tn_op == CALL
2771 && tn->u.call->func->tn_op == ADDR
2772 && tn->u.call->func->u.ops.left->tn_op == NAME) {
2773 *out_name = tn->u.call->func->u.ops.left->u.sym->s_name;
2774 return true;
2775 }
2776 return false;
2777 }
2778
2779 static bool
2780 is_unconst_function(const char *name)
2781 {
2782
2783 return strcmp(name, "memchr") == 0 ||
2784 strcmp(name, "strchr") == 0 ||
2785 strcmp(name, "strpbrk") == 0 ||
2786 strcmp(name, "strrchr") == 0 ||
2787 strcmp(name, "strstr") == 0;
2788 }
2789
2790 static bool
2791 is_const_char_pointer(const tnode_t *tn)
2792 {
2793 /*
2794 * For traditional reasons, C99 6.4.5p5 defines that string literals
2795 * have type 'char[]'. They are often implicitly converted to 'char
2796 * *', for example when they are passed as function arguments.
2797 *
2798 * C99 6.4.5p6 further defines that modifying a string that is
2799 * constructed from a string literal invokes undefined behavior.
2800 *
2801 * Out of these reasons, string literals are treated as 'effectively
2802 * const' here.
2803 */
2804 if (tn->tn_op == CVT &&
2805 tn->u.ops.left->tn_op == ADDR &&
2806 tn->u.ops.left->u.ops.left->tn_op == STRING)
2807 return true;
2808
2809 const type_t *tp = before_conversion(tn)->tn_type;
2810 return tp->t_tspec == PTR &&
2811 tp->t_subt->t_tspec == CHAR &&
2812 tp->t_subt->t_const;
2813 }
2814
2815 static bool
2816 is_const_pointer(const tnode_t *tn)
2817 {
2818 const type_t *tp = before_conversion(tn)->tn_type;
2819 return tp->t_tspec == PTR && tp->t_subt->t_const;
2820 }
2821
2822 static void
2823 check_unconst_function(const type_t *lstp, const tnode_t *rn)
2824 {
2825 const char *function_name;
2826
2827 if (lstp->t_tspec == CHAR && !lstp->t_const &&
2828 is_direct_function_call(rn, &function_name) &&
2829 is_unconst_function(function_name) &&
2830 rn->u.call->args_len >= 1 &&
2831 is_const_char_pointer(rn->u.call->args[0])) {
2832 /* call to '%s' effectively discards 'const' from argument */
2833 warning(346, function_name);
2834 }
2835
2836 if (!lstp->t_const &&
2837 is_direct_function_call(rn, &function_name) &&
2838 strcmp(function_name, "bsearch") == 0 &&
2839 rn->u.call->args_len >= 2 &&
2840 is_const_pointer(rn->u.call->args[1])) {
2841 /* call to '%s' effectively discards 'const' from argument */
2842 warning(346, function_name);
2843 }
2844 }
2845
2846 static bool
2847 check_assign_void_pointer_compat(op_t op, int arg,
2848 const type_t *ltp, tspec_t lt,
2849 const type_t *lstp, tspec_t lst,
2850 const tnode_t *rn,
2851 const type_t *rtp, tspec_t rt,
2852 const type_t *rstp, tspec_t rst)
2853 {
2854 if (!(lt == PTR && rt == PTR && (lst == VOID || rst == VOID ||
2855 types_compatible(lstp, rstp,
2856 true, false, NULL))))
2857 return false;
2858
2859 /* compatible pointer types (qualifiers ignored) */
2860 char qualifiers[32];
2861 snprintf(qualifiers, sizeof(qualifiers), "%s%s",
2862 !lstp->t_const && rstp->t_const ? " const" : "",
2863 !lstp->t_volatile && rstp->t_volatile ? " volatile" : "");
2864 if (allow_c90 && qualifiers[0] != '\0') {
2865 switch (op) {
2866 case INIT:
2867 case RETURN:
2868 /* '%s' discards '%s' from '%s' */
2869 warning(182, op_name(op),
2870 qualifiers + 1, type_name(rtp));
2871 break;
2872 case FARG:
2873 /* converting '%s' to incompatible '%s' ... */
2874 warning(153,
2875 type_name(rtp), type_name(ltp), arg);
2876 break;
2877 default:
2878 /* operator '%s' discards '%s' from '%s' */
2879 warning(128, op_name(op),
2880 qualifiers + 1, type_name(rtp));
2881 break;
2882 }
2883 }
2884
2885 if (allow_c90)
2886 check_unconst_function(lstp, rn);
2887
2888 return true;
2889 }
2890
2891 static bool
2892 check_assign_pointer_integer(op_t op, int arg,
2893 const type_t *ltp, tspec_t lt,
2894 const type_t *rtp, tspec_t rt)
2895 {
2896
2897 if (!((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)))
2898 return false;
2899
2900 const char *lx = lt == PTR ? "pointer" : "integer";
2901 const char *rx = rt == PTR ? "pointer" : "integer";
2902
2903 switch (op) {
2904 case INIT:
2905 case RETURN:
2906 /* illegal combination of %s '%s' and %s '%s' */
2907 warning(183, lx, type_name(ltp), rx, type_name(rtp));
2908 break;
2909 case FARG:
2910 /* illegal combination of %s '%s' and %s '%s', arg #%d */
2911 warning(154,
2912 lx, type_name(ltp), rx, type_name(rtp), arg);
2913 break;
2914 default:
2915 /* illegal combination of %s '%s' and %s '%s', op '%s' */
2916 warning(123,
2917 lx, type_name(ltp), rx, type_name(rtp), op_name(op));
2918 break;
2919 }
2920 return true;
2921 }
2922
2923 static bool
2924 check_assign_pointer(op_t op, int arg,
2925 const type_t *ltp, tspec_t lt,
2926 const type_t *rtp, tspec_t rt)
2927 {
2928 if (!(lt == PTR && rt == PTR))
2929 return false;
2930
2931 if (op == FARG)
2932 /* converting '%s' to incompatible '%s' for ... */
2933 warning(153, type_name(rtp), type_name(ltp), arg);
2934 else
2935 warn_incompatible_pointers(op, ltp, rtp);
2936 return true;
2937 }
2938
2939 static void
2940 warn_assign(op_t op, int arg,
2941 const type_t *ltp, tspec_t lt,
2942 const type_t *rtp, tspec_t rt)
2943 {
2944 switch (op) {
2945 case INIT:
2946 /* cannot initialize '%s' from '%s' */
2947 error(185, type_name(ltp), type_name(rtp));
2948 break;
2949 case RETURN:
2950 /* function has return type '%s' but returns '%s' */
2951 error(211, type_name(ltp), type_name(rtp));
2952 break;
2953 case FARG:
2954 /* passing '%s' to incompatible '%s', arg #%d */
2955 warning(155, type_name(rtp), type_name(ltp), arg);
2956 break;
2957 default:
2958 warn_incompatible_types(op, ltp, lt, rtp, rt);
2959 break;
2960 }
2961 }
2962
2963 /*
2964 * Checks type compatibility for ASSIGN, INIT, FARG and RETURN
2965 * and prints warnings/errors if necessary.
2966 * Returns whether the types are (almost) compatible.
2967 */
2968 static bool
2969 check_assign_types_compatible(op_t op, int arg,
2970 const tnode_t *ln, const tnode_t *rn)
2971 {
2972 tspec_t lt, rt, lst = NO_TSPEC, rst = NO_TSPEC;
2973 type_t *ltp, *rtp, *lstp = NULL, *rstp = NULL;
2974
2975 if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
2976 lst = (lstp = ltp->t_subt)->t_tspec;
2977 if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
2978 rst = (rstp = rtp->t_subt)->t_tspec;
2979
2980 if (lt == BOOL && is_scalar(rt)) /* C99 6.3.1.2 */
2981 return true;
2982
2983 if (is_arithmetic(lt) && (is_arithmetic(rt) || rt == BOOL))
2984 return true;
2985
2986 if (is_struct_or_union(lt) && is_struct_or_union(rt))
2987 return ltp->u.sou == rtp->u.sou;
2988
2989 if (lt == PTR && is_null_pointer(rn)) {
2990 if (is_integer(rn->tn_type->t_tspec))
2991 /* implicit conversion from integer 0 to pointer ... */
2992 query_message(15, type_name(ltp));
2993 return true;
2994 }
2995
2996 check_assign_void_pointer(op, arg, lt, lst, rt, rst);
2997
2998 if (check_assign_void_pointer_compat(op, arg,
2999 ltp, lt, lstp, lst, rn, rtp, rt, rstp, rst))
3000 return true;
3001
3002 if (check_assign_pointer_integer(op, arg, ltp, lt, rtp, rt))
3003 return true;
3004
3005 if (check_assign_pointer(op, arg, ltp, lt, rtp, rt))
3006 return true;
3007
3008 warn_assign(op, arg, ltp, lt, rtp, rt);
3009 return false;
3010 }
3011
3012 static bool
3013 has_side_effect(const tnode_t *tn) /* NOLINT(misc-no-recursion) */
3014 {
3015 op_t op = tn->tn_op;
3016
3017 if (modtab[op].m_has_side_effect)
3018 return true;
3019
3020 if (op == CVT && tn->tn_type->t_tspec == VOID)
3021 return has_side_effect(tn->u.ops.left);
3022
3023 /* XXX: Why not has_side_effect(tn->u.ops.left) as well? */
3024 if (op == LOGAND || op == LOGOR)
3025 return has_side_effect(tn->u.ops.right);
3026
3027 /* XXX: Why not has_side_effect(tn->u.ops.left) as well? */
3028 if (op == QUEST)
3029 return has_side_effect(tn->u.ops.right);
3030
3031 if (op == COLON || op == COMMA) {
3032 return has_side_effect(tn->u.ops.left) ||
3033 has_side_effect(tn->u.ops.right);
3034 }
3035
3036 return false;
3037 }
3038
3039 static bool
3040 is_void_cast(const tnode_t *tn)
3041 {
3042
3043 return tn->tn_op == CVT && tn->tn_cast &&
3044 tn->tn_type->t_tspec == VOID;
3045 }
3046
3047 static bool
3048 is_local_symbol(const tnode_t *tn)
3049 {
3050
3051 return tn->tn_op == LOAD &&
3052 tn->u.ops.left->tn_op == NAME &&
3053 tn->u.ops.left->u.sym->s_scl == AUTO;
3054 }
3055
3056 static bool
3057 is_int_constant_zero(const tnode_t *tn)
3058 {
3059
3060 return tn->tn_op == CON &&
3061 tn->tn_type->t_tspec == INT &&
3062 tn->u.value.u.integer == 0;
3063 }
3064
3065 static void
3066 check_null_effect(const tnode_t *tn)
3067 {
3068
3069 if (hflag &&
3070 !has_side_effect(tn) &&
3071 !(is_void_cast(tn) && is_local_symbol(tn->u.ops.left)) &&
3072 !(is_void_cast(tn) && is_int_constant_zero(tn->u.ops.left))) {
3073 /* expression has null effect */
3074 warning(129);
3075 }
3076 }
3077
3078 /*
3079 * Check the types for specific operators and type combinations.
3080 *
3081 * At this point, the operands already conform to the type requirements of
3082 * the operator, such as being integer, floating or scalar.
3083 */
3084 static bool
3085 typeok_op(op_t op, int arg,
3086 const tnode_t *ln, const type_t *ltp, tspec_t lt,
3087 const tnode_t *rn, const type_t *rtp, tspec_t rt)
3088 {
3089 switch (op) {
3090 case ARROW:
3091 return typeok_arrow(lt);
3092 case POINT:
3093 return typeok_point(ln, ltp, lt);
3094 case INCBEF:
3095 case DECBEF:
3096 case INCAFT:
3097 case DECAFT:
3098 return typeok_incdec(op, ln, ltp);
3099 case INDIR:
3100 return typeok_indir(ltp, lt);
3101 case ADDR:
3102 return typeok_address(op, ln, ltp, lt);
3103 case PLUS:
3104 return typeok_plus(op, ltp, lt, rtp, rt);
3105 case MINUS:
3106 return typeok_minus(op, ltp, lt, rtp, rt);
3107 case SHL:
3108 typeok_shl(op, lt, rt);
3109 goto shift;
3110 case SHR:
3111 typeok_shr(op, ln, lt, rn, rt);
3112 shift:
3113 typeok_shift(ltp, lt, rn, rt);
3114 break;
3115 case LT:
3116 case LE:
3117 case GT:
3118 case GE:
3119 compare:
3120 return typeok_compare(op, ln, ltp, lt, rn, rtp, rt);
3121 case EQ:
3122 case NE:
3123 if (is_typeok_eq(ln, lt, rn, rt))
3124 break;
3125 goto compare;
3126 case QUEST:
3127 return typeok_quest(lt, rn);
3128 case COLON:
3129 return typeok_colon(ln, ltp, lt, rn, rtp, rt);
3130 case ASSIGN:
3131 case INIT:
3132 case FARG:
3133 case RETURN:
3134 if (!check_assign_types_compatible(op, arg, ln, rn))
3135 return false;
3136 goto assign;
3137 case MULASS:
3138 case DIVASS:
3139 case MODASS:
3140 goto assign;
3141 case ADDASS:
3142 case SUBASS:
3143 if ((lt == PTR && !is_integer(rt)) || rt == PTR) {
3144 warn_incompatible_types(op, ltp, lt, rtp, rt);
3145 return false;
3146 }
3147 goto assign;
3148 case SHLASS:
3149 goto assign;
3150 case SHRASS:
3151 if (pflag && !is_uinteger(lt) &&
3152 !(!allow_c90 && is_uinteger(rt))) {
3153 /* bitwise '%s' on signed value possibly nonportable */
3154 warning(117, op_name(op));
3155 }
3156 goto assign;
3157 case ANDASS:
3158 case XORASS:
3159 case ORASS:
3160 assign:
3161 return typeok_assign(op, ln, ltp, lt);
3162 case COMMA:
3163 if (!modtab[ln->tn_op].m_has_side_effect)
3164 check_null_effect(ln);
3165 break;
3166 default:
3167 break;
3168 }
3169 return true;
3170 }
3171
3172 static void
3173 check_bad_enum_operation(op_t op, const tnode_t *ln, const tnode_t *rn)
3174 {
3175
3176 if (!eflag)
3177 return;
3178
3179 /* Allow enum in array indices. */
3180 if (op == PLUS &&
3181 ((ln->tn_type->t_is_enum && rn->tn_type->t_tspec == PTR) ||
3182 (rn->tn_type->t_is_enum && ln->tn_type->t_tspec == PTR))) {
3183 return;
3184 }
3185
3186 /* dubious operation '%s' on enum */
3187 warning(241, op_name(op));
3188 }
3189
3190 static void
3191 check_enum_type_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
3192 {
3193 const mod_t *mp = &modtab[op];
3194
3195 if (ln->tn_type->u.enumer != rn->tn_type->u.enumer) {
3196 switch (op) {
3197 case INIT:
3198 /* enum type mismatch between '%s' and '%s' in ... */
3199 warning(210,
3200 type_name(ln->tn_type), type_name(rn->tn_type));
3201 break;
3202 case FARG:
3203 /* function expects '%s', passing '%s' for arg #%d */
3204 warning(156,
3205 type_name(ln->tn_type), type_name(rn->tn_type),
3206 arg);
3207 break;
3208 case RETURN:
3209 /* function has return type '%s' but returns '%s' */
3210 warning(211,
3211 type_name(ln->tn_type), type_name(rn->tn_type));
3212 break;
3213 default:
3214 /* enum type mismatch: '%s' '%s' '%s' */
3215 warning(130, type_name(ln->tn_type), op_name(op),
3216 type_name(rn->tn_type));
3217 break;
3218 }
3219 } else if (Pflag && eflag && mp->m_comparison && op != EQ && op != NE)
3220 /* operator '%s' assumes that '%s' is ordered */
3221 warning(243, op_name(op), type_name(ln->tn_type));
3222 }
3223
3224 static void
3225 check_enum_int_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
3226 {
3227
3228 if (!eflag)
3229 return;
3230
3231 switch (op) {
3232 case INIT:
3233 /*
3234 * Initialization with 0 is allowed. Otherwise, all implicit
3235 * initializations would need to be warned upon as well.
3236 */
3237 if (!rn->tn_type->t_is_enum && rn->tn_op == CON &&
3238 is_integer(rn->tn_type->t_tspec) &&
3239 rn->u.value.u.integer == 0) {
3240 return;
3241 }
3242 /* initialization of '%s' with '%s' */
3243 warning(277, type_name(ln->tn_type), type_name(rn->tn_type));
3244 break;
3245 case FARG:
3246 /* combination of '%s' and '%s', arg #%d */
3247 warning(278,
3248 type_name(ln->tn_type), type_name(rn->tn_type), arg);
3249 break;
3250 case RETURN:
3251 /* combination of '%s' and '%s' in return */
3252 warning(279, type_name(ln->tn_type), type_name(rn->tn_type));
3253 break;
3254 default:
3255 /* combination of '%s' and '%s', op '%s' */
3256 warning(242, type_name(ln->tn_type), type_name(rn->tn_type),
3257 op_name(op));
3258 break;
3259 }
3260 }
3261
3262 static void
3263 typeok_enum(op_t op, const mod_t *mp, int arg,
3264 const tnode_t *ln, const type_t *ltp,
3265 const tnode_t *rn, const type_t *rtp)
3266 {
3267 if (mp->m_bad_on_enum &&
3268 (ltp->t_is_enum || (mp->m_binary && rtp->t_is_enum))) {
3269 check_bad_enum_operation(op, ln, rn);
3270 } else if (mp->m_valid_on_enum &&
3271 (ltp->t_is_enum && rtp != NULL && rtp->t_is_enum)) {
3272 check_enum_type_mismatch(op, arg, ln, rn);
3273 } else if (mp->m_valid_on_enum &&
3274 (ltp->t_is_enum || (rtp != NULL && rtp->t_is_enum))) {
3275 check_enum_int_mismatch(op, arg, ln, rn);
3276 }
3277 }
3278
3279 /* Perform most type checks. Return whether the types are ok. */
3280 bool
3281 typeok(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
3282 {
3283
3284 const mod_t *mp = &modtab[op];
3285
3286 type_t *ltp = ln->tn_type;
3287 tspec_t lt = ltp->t_tspec;
3288
3289 type_t *rtp = mp->m_binary ? rn->tn_type : NULL;
3290 tspec_t rt = mp->m_binary ? rtp->t_tspec : NO_TSPEC;
3291
3292 if (Tflag && !typeok_scalar_strict_bool(op, mp, arg, ln, rn))
3293 return false;
3294 if (!typeok_scalar(op, mp, ltp, lt, rtp, rt))
3295 return false;
3296
3297 if (!typeok_op(op, arg, ln, ltp, lt, rn, rtp, rt))
3298 return false;
3299
3300 typeok_enum(op, mp, arg, ln, ltp, rn, rtp);
3301 return true;
3302 }
3303
3304 /* In traditional C, keep unsigned and promote FLOAT to DOUBLE. */
3305 static tspec_t
3306 promote_trad(tspec_t t)
3307 {
3308
3309 if (t == UCHAR || t == USHORT)
3310 return UINT;
3311 if (t == CHAR || t == SCHAR || t == SHORT)
3312 return INT;
3313 if (t == FLOAT)
3314 return DOUBLE;
3315 if (t == ENUM)
3316 return INT;
3317 return t;
3318 }
3319
3320 /*
3321 * C99 6.3.1.1p2 requires for types with lower rank than int that "If an int
3322 * can represent all the values of the original type, the value is converted
3323 * to an int; otherwise it is converted to an unsigned int", and that "All
3324 * other types are unchanged by the integer promotions".
3325 */
3326 static tspec_t
3327 promote_c90(const tnode_t *tn, tspec_t t, bool farg)
3328 {
3329 if (tn->tn_type->t_bitfield) {
3330 unsigned int width = tn->tn_type->t_bit_field_width;
3331 unsigned int int_width = size_in_bits(INT);
3332 // XXX: What about _Bool bit-fields, since C99?
3333 if (width < int_width)
3334 return INT;
3335 if (width == int_width)
3336 return is_uinteger(t) ? UINT : INT;
3337 return t;
3338 }
3339
3340 if (t == CHAR || t == SCHAR)
3341 return INT;
3342 if (t == UCHAR)
3343 return size_in_bits(CHAR) < size_in_bits(INT) ? INT : UINT;
3344 if (t == SHORT)
3345 return INT;
3346 if (t == USHORT)
3347 return size_in_bits(SHORT) < size_in_bits(INT) ? INT : UINT;
3348 if (t == ENUM)
3349 return INT;
3350 if (farg && t == FLOAT)
3351 return DOUBLE;
3352 return t;
3353 }
3354
3355 /*
3356 * Performs the "integer promotions" (C99 6.3.1.1p2), which convert small
3357 * integer types to either int or unsigned int.
3358 *
3359 * If allow_c90 is unset or the operand is a function argument with no type
3360 * information (no prototype or variable # of args), converts float to double.
3361 */
3362 tnode_t *
3363 promote(op_t op, bool farg, tnode_t *tn)
3364 {
3365
3366 const type_t *otp = tn->tn_type;
3367 tspec_t ot = otp->t_tspec;
3368 if (!is_arithmetic(ot))
3369 return tn;
3370
3371 tspec_t nt = allow_c90 ? promote_c90(tn, ot, farg) : promote_trad(ot);
3372 if (nt == ot)
3373 return tn;
3374
3375 type_t *ntp = expr_dup_type(gettyp(nt));
3376 ntp->t_tspec = nt;
3377 ntp->t_is_enum = otp->t_is_enum;
3378 if (ntp->t_is_enum)
3379 ntp->u.enumer = otp->u.enumer;
3380 ntp->t_bitfield = otp->t_bitfield;
3381 if (ntp->t_bitfield) {
3382 ntp->t_bit_field_width = otp->t_bit_field_width;
3383 ntp->t_bit_field_offset = otp->t_bit_field_offset;
3384 }
3385 if (ntp->t_bitfield && is_uinteger(ot) && !is_uinteger(nt))
3386 ntp->t_bit_field_width++;
3387 return convert(op, 0, ntp, tn);
3388 }
3389
3390 static void
3391 check_lossy_floating_to_integer_conversion(
3392 op_t op, int arg, const type_t *tp, const tnode_t *tn)
3393 {
3394 long double x = tn->u.value.u.floating;
3395 integer_constraints ic = ic_any(tp);
3396 if (is_uinteger(tp->t_tspec)
3397 ? x >= ic.umin && x <= ic.umax && x == (uint64_t)x
3398 : x >= ic.smin && x <= ic.smax && x == (int64_t)x)
3399 return;
3400 if (op == FARG)
3401 /* lossy conversion of %Lg to '%s', arg #%d */
3402 warning(380, x, type_name(tp), arg);
3403 else
3404 /* lossy conversion of %Lg to '%s' */
3405 warning(381, x, type_name(tp));
3406 }
3407
3408 static void
3409 convert_integer_from_floating(
3410 op_t op, int arg, const type_t *tp, const tnode_t *tn)
3411 {
3412
3413 if (op == CVT)
3414 /* cast from floating point '%s' to integer '%s' */
3415 query_message(2, type_name(tn->tn_type), type_name(tp));
3416 else
3417 /* implicit conversion from floating point '%s' to ... */
3418 query_message(1, type_name(tn->tn_type), type_name(tp));
3419 if (tn->tn_op == CON)
3420 check_lossy_floating_to_integer_conversion(op, arg, tp, tn);
3421 }
3422
3423 static bool
3424 should_warn_about_prototype_conversion(tspec_t nt,
3425 tspec_t ot, const tnode_t *ptn)
3426 {
3427
3428 if (nt == ot)
3429 return false;
3430
3431 if (nt == ENUM && ot == INT)
3432 return false;
3433
3434 if (is_floating(nt) != is_floating(ot) ||
3435 portable_rank_cmp(nt, ot) != 0) {
3436 /* representation and/or width change */
3437 if (!is_integer(ot))
3438 return true;
3439 /*
3440 * XXX: Investigate whether this rule makes sense; see
3441 * tests/usr.bin/xlint/lint1/platform_long.c.
3442 */
3443 return portable_rank_cmp(ot, INT) > 0;
3444 }
3445
3446 if (!hflag)
3447 return false;
3448
3449 /*
3450 * If the types differ only in sign and the argument has the same
3451 * representation in both types, print no warning.
3452 */
3453 if (ptn->tn_op == CON && is_integer(nt) &&
3454 signed_type(nt) == signed_type(ot) &&
3455 !msb(ptn->u.value.u.integer, ot))
3456 return false;
3457
3458 return true;
3459 }
3460
3461 /*
3462 * Warn if a prototype causes a type conversion that is different from what
3463 * would happen to the same argument in the absence of a prototype. This
3464 * check is intended for code that needs to stay compatible with pre-C90 C.
3465 *
3466 * Errors/warnings about illegal type combinations are already printed
3467 * in check_assign_types_compatible().
3468 */
3469 static void
3470 check_prototype_conversion(int arg, tspec_t nt, tspec_t ot, type_t *tp,
3471 tnode_t *tn)
3472 {
3473
3474 if (!is_arithmetic(nt) || !is_arithmetic(ot))
3475 return;
3476
3477 /*
3478 * If the type of the formal parameter is char/short, a warning would
3479 * be useless, because functions declared the old style can't expect
3480 * char/short arguments.
3481 */
3482 if (nt == CHAR || nt == SCHAR || nt == UCHAR ||
3483 nt == SHORT || nt == USHORT)
3484 return;
3485
3486 tnode_t *ptn = promote(NOOP, true, tn);
3487 ot = ptn->tn_type->t_tspec;
3488
3489 if (should_warn_about_prototype_conversion(nt, ot, ptn)) {
3490 /* argument %d is converted from '%s' to '%s' ... */
3491 warning(259, arg, type_name(tn->tn_type), type_name(tp));
3492 }
3493 }
3494
3495 /*
3496 * When converting a large integer type to a small integer type, in some
3497 * cases the value of the actual expression is further restricted than the
3498 * type bounds, such as in (expr & 0xFF) or (expr % 100) or (expr >> 24).
3499 */
3500 static bool
3501 can_represent(const type_t *tp, const tnode_t *tn)
3502 {
3503 uint64_t nmask = value_bits(width_in_bits(tp));
3504 if (!is_uinteger(tp->t_tspec))
3505 nmask >>= 1;
3506
3507 integer_constraints c = ic_expr(tn);
3508 if ((~c.bclr & ~nmask) == 0)
3509 return true;
3510
3511 integer_constraints tpc = ic_any(tp);
3512 if (is_uinteger(tp->t_tspec)
3513 ? tpc.umin <= c.umin && tpc.umax >= c.umax
3514 : tpc.smin <= c.smin && tpc.smax >= c.smax)
3515 return true;
3516
3517 debug_enter();
3518 debug_step("type '%s' cannot represent:", type_name(tp));
3519 debug_node(tn);
3520 debug_leave();
3521 return false;
3522 }
3523
3524 static bool
3525 should_warn_about_integer_conversion(const type_t *ntp, tspec_t nt,
3526 const tnode_t *otn, tspec_t ot)
3527 {
3528
3529 // XXX: The portable_rank_cmp aims at portable mode, independent of the
3530 // current platform, while can_represent acts on the actual type sizes
3531 // from the current platform. This mix is inconsistent, but anything
3532 // else would make the exact conditions too complicated to grasp.
3533 if (aflag > 0 && portable_rank_cmp(nt, ot) < 0) {
3534 if (ot == LONG || ot == ULONG
3535 || ot == LLONG || ot == ULLONG
3536 #ifdef INT128_SIZE
3537 || ot == INT128 || ot == UINT128
3538 #endif
3539 || aflag > 1)
3540 return !can_represent(ntp, otn);
3541 }
3542 return false;
3543 }
3544
3545 static void
3546 convert_integer_from_integer(op_t op, int arg, tspec_t nt, tspec_t ot,
3547 type_t *tp, tnode_t *tn)
3548 {
3549
3550 if (tn->tn_op == CON)
3551 return;
3552
3553 if (op == CVT)
3554 return;
3555
3556 if (Pflag && pflag && aflag > 0 &&
3557 portable_rank_cmp(nt, ot) > 0 &&
3558 is_uinteger(nt) != is_uinteger(ot)) {
3559 if (op == FARG)
3560 /* conversion to '%s' may sign-extend ... */
3561 warning(297, type_name(tp), arg);
3562 else
3563 /* conversion to '%s' may sign-extend ... */
3564 warning(131, type_name(tp));
3565 }
3566
3567 if (Pflag && portable_rank_cmp(nt, ot) > 0 &&
3568 (tn->tn_op == PLUS || tn->tn_op == MINUS || tn->tn_op == MULT ||
3569 tn->tn_op == SHL)) {
3570 /* suggest cast from '%s' to '%s' on op '%s' to ... */
3571 warning(324, type_name(gettyp(ot)), type_name(tp),
3572 op_name(tn->tn_op));
3573 }
3574
3575 if (should_warn_about_integer_conversion(tp, nt, tn, ot)) {
3576 if (op == FARG) {
3577 /* conversion from '%s' to '%s' may lose ... */
3578 warning(298,
3579 type_name(tn->tn_type), type_name(tp), arg);
3580 } else {
3581 /* conversion from '%s' to '%s' may lose accuracy */
3582 warning(132,
3583 type_name(tn->tn_type), type_name(tp));
3584 }
3585 }
3586
3587 if (any_query_enabled && is_uinteger(nt) != is_uinteger(ot))
3588 /* implicit conversion changes sign from '%s' to '%s' */
3589 query_message(3, type_name(tn->tn_type), type_name(tp));
3590 }
3591
3592 static void
3593 convert_integer_from_pointer(op_t op, tspec_t nt, type_t *tp, tnode_t *tn)
3594 {
3595
3596 if (tn->tn_op == CON)
3597 return;
3598 if (op != CVT)
3599 return; /* We already got an error. */
3600 if (portable_rank_cmp(nt, PTR) >= 0)
3601 return;
3602
3603 if (pflag && size_in_bits(nt) >= size_in_bits(PTR)) {
3604 /* conversion of pointer to '%s' may lose bits */
3605 warning(134, type_name(tp));
3606 } else {
3607 /* conversion of pointer to '%s' loses bits */
3608 warning(133, type_name(tp));
3609 }
3610 }
3611
3612 static bool
3613 struct_starts_with(const type_t *struct_tp, const type_t *member_tp)
3614 {
3615
3616 return struct_tp->u.sou->sou_first_member != NULL &&
3617 types_compatible(struct_tp->u.sou->sou_first_member->s_type,
3618 member_tp, true, false, NULL);
3619 }
3620
3621 static bool
3622 is_byte_array(const type_t *tp)
3623 {
3624
3625 return tp->t_tspec == ARRAY &&
3626 (tp->t_subt->t_tspec == CHAR || tp->t_subt->t_tspec == UCHAR);
3627 }
3628
3629 static bool
3630 union_contains(const type_t *utp, const type_t *mtp)
3631 {
3632 for (const sym_t *mem = utp->u.sou->sou_first_member;
3633 mem != NULL; mem = mem->s_next) {
3634 if (types_compatible(mem->s_type, mtp, true, false, NULL))
3635 return true;
3636 }
3637 return false;
3638 }
3639
3640 static bool
3641 should_warn_about_pointer_cast(const type_t *nstp, tspec_t nst,
3642 const type_t *ostp, tspec_t ost)
3643 {
3644
3645 while (nst == ARRAY)
3646 nstp = nstp->t_subt, nst = nstp->t_tspec;
3647 while (ost == ARRAY)
3648 ostp = ostp->t_subt, ost = ostp->t_tspec;
3649
3650 if (nst == STRUCT && ost == STRUCT &&
3651 (struct_starts_with(nstp, ostp) ||
3652 struct_starts_with(ostp, nstp)))
3653 return false;
3654
3655 if (is_incomplete(nstp) || is_incomplete(ostp))
3656 return false;
3657
3658 if (nst == CHAR || nst == UCHAR)
3659 return false; /* for the sake of traditional C code */
3660 if (ost == CHAR || ost == UCHAR)
3661 return false; /* for the sake of traditional C code */
3662
3663 /* Allow cast between pointers to sockaddr variants. */
3664 if (nst == STRUCT && ost == STRUCT) {
3665 const sym_t *nmem = nstp->u.sou->sou_first_member;
3666 const sym_t *omem = ostp->u.sou->sou_first_member;
3667 while (nmem != NULL && omem != NULL &&
3668 types_compatible(nmem->s_type, omem->s_type,
3669 true, false, NULL))
3670 nmem = nmem->s_next, omem = omem->s_next;
3671 if (nmem != NULL && is_byte_array(nmem->s_type))
3672 return false;
3673 if (omem != NULL && is_byte_array(omem->s_type))
3674 return false;
3675 if (nmem == NULL && omem == NULL)
3676 return false;
3677 }
3678
3679 if (nst == UNION || ost == UNION) {
3680 const type_t *union_tp = nst == UNION ? nstp : ostp;
3681 const type_t *other_tp = nst == UNION ? ostp : nstp;
3682 if (union_contains(union_tp, other_tp))
3683 return false;
3684 }
3685
3686 if (is_struct_or_union(nst) && is_struct_or_union(ost))
3687 return nstp->u.sou != ostp->u.sou;
3688
3689 enum rank_kind rk1 = type_properties(nst)->tt_rank_kind;
3690 enum rank_kind rk2 = type_properties(ost)->tt_rank_kind;
3691 if (rk1 != rk2 || rk1 == RK_NONE)
3692 return true;
3693
3694 return portable_rank_cmp(nst, ost) != 0;
3695 }
3696
3697 static void
3698 convert_pointer_from_pointer(type_t *ntp, tnode_t *tn)
3699 {
3700 const type_t *nstp = ntp->t_subt;
3701 const type_t *otp = tn->tn_type;
3702 const type_t *ostp = otp->t_subt;
3703 tspec_t nst = nstp->t_tspec;
3704 tspec_t ost = ostp->t_tspec;
3705
3706 if (nst == VOID || ost == VOID) {
3707 /* TODO: C99 behaves like C90 here. */
3708 if (!allow_trad && !allow_c99 && (nst == FUNC || ost == FUNC)) {
3709 const char *nts, *ots;
3710 /* null pointers are already handled in convert() */
3711 *(nst == FUNC ? &nts : &ots) = "function pointer";
3712 *(nst == VOID ? &nts : &ots) = "'void *'";
3713 /* conversion of %s to %s requires a cast */
3714 warning(303, ots, nts);
3715 }
3716 return;
3717 }
3718 if (nst == FUNC && ost == FUNC)
3719 return;
3720 if (nst == FUNC || ost == FUNC) {
3721 /* converting '%s' to '%s' is questionable */
3722 warning(229, type_name(otp), type_name(ntp));
3723 return;
3724 }
3725
3726 if (hflag && alignment(nstp) > alignment(ostp) &&
3727 ost != CHAR && ost != UCHAR &&
3728 !is_incomplete(ostp) &&
3729 !(nst == UNION && union_contains(nstp, ostp))) {
3730 /* converting '%s' to '%s' increases alignment ... */
3731 warning(135, type_name(otp), type_name(ntp),
3732 alignment(ostp), alignment(nstp));
3733 }
3734
3735 if (cflag && should_warn_about_pointer_cast(nstp, nst, ostp, ost)) {
3736 /* pointer cast from '%s' to unrelated '%s' */
3737 warning(247, type_name(ostp), type_name(nstp));
3738 }
3739 }
3740
3741 /*
3742 * Insert a conversion operator, which converts the type of the node
3743 * to another given type.
3744 *
3745 * Possible values for 'op':
3746 * CVT a cast-expression
3747 * binary integer promotion for one of the operands, or a usual
3748 * arithmetic conversion
3749 * binary plain or compound assignments to bit-fields
3750 * FARG 'arg' is the number of the parameter (used for warnings)
3751 * NOOP several other implicit conversions
3752 * ...
3753 */
3754 tnode_t *
3755 convert(op_t op, int arg, type_t *tp, tnode_t *tn)
3756 {
3757 tspec_t nt = tp->t_tspec;
3758 tspec_t ot = tn->tn_type->t_tspec;
3759
3760 if (allow_trad && allow_c90 && op == FARG)
3761 check_prototype_conversion(arg, nt, ot, tp, tn);
3762
3763 if (nt == BOOL) {
3764 /* No further checks. */
3765
3766 } else if (is_integer(nt)) {
3767 if (ot == BOOL) {
3768 /* No further checks. */
3769 } else if (is_integer(ot))
3770 convert_integer_from_integer(op, arg, nt, ot, tp, tn);
3771 else if (is_floating(ot))
3772 convert_integer_from_floating(op, arg, tp, tn);
3773 else if (ot == PTR)
3774 convert_integer_from_pointer(op, nt, tp, tn);
3775
3776 } else if (is_floating(nt)) {
3777 if (is_integer(ot) && op != CVT) {
3778 /* implicit conversion from integer '%s' to ... */
3779 query_message(19,
3780 type_name(tn->tn_type), type_name(tp));
3781 }
3782
3783 } else if (nt == PTR) {
3784 if (is_null_pointer(tn)) {
3785 /* a null pointer may be assigned to any pointer. */
3786 } else if (ot == PTR && op == CVT)
3787 convert_pointer_from_pointer(tp, tn);
3788 }
3789
3790 tnode_t *ntn = expr_alloc_tnode();
3791 ntn->tn_op = CVT;
3792 ntn->tn_type = tp;
3793 ntn->tn_cast = op == CVT;
3794 ntn->tn_sys |= tn->tn_sys;
3795 ntn->u.ops.right = NULL;
3796 if (tn->tn_op != CON || nt == VOID) {
3797 ntn->u.ops.left = tn;
3798 } else {
3799 ntn->tn_op = CON;
3800 convert_constant(op, arg, ntn->tn_type, &ntn->u.value,
3801 &tn->u.value);
3802 }
3803
3804 return ntn;
3805 }
3806
3807 static void
3808 convert_constant_from_floating(op_t op, int arg, const type_t *ntp,
3809 tspec_t nt, val_t *nv, val_t *ov)
3810 {
3811 long double max = 0.0, min = 0.0;
3812
3813 switch (nt) {
3814 case CHAR:
3815 max = TARG_CHAR_MAX; min = TARG_CHAR_MIN; break;
3816 case UCHAR:
3817 max = TARG_UCHAR_MAX; min = 0; break;
3818 case SCHAR:
3819 max = TARG_SCHAR_MAX; min = TARG_SCHAR_MIN; break;
3820 case SHORT:
3821 max = TARG_SHRT_MAX; min = TARG_SHRT_MIN; break;
3822 case USHORT:
3823 max = TARG_USHRT_MAX; min = 0; break;
3824 case ENUM:
3825 case INT:
3826 max = TARG_INT_MAX; min = TARG_INT_MIN; break;
3827 case UINT:
3828 max = TARG_UINT_MAX; min = 0; break;
3829 case LONG:
3830 max = TARG_LONG_MAX; min = TARG_LONG_MIN; break;
3831 case ULONG:
3832 max = TARG_ULONG_MAX; min = 0; break;
3833 case LLONG:
3834 max = LLONG_MAX; min = LLONG_MIN; break;
3835 case ULLONG:
3836 max = ULLONG_MAX; min = 0; break;
3837 case FLOAT:
3838 case FCOMPLEX:
3839 max = FLT_MAX; min = -FLT_MAX; break;
3840 case DOUBLE:
3841 case DCOMPLEX:
3842 max = DBL_MAX; min = -DBL_MAX; break;
3843 case LDOUBLE:
3844 case LCOMPLEX:
3845 /* LINTED 248; see floating_error_value. */
3846 max = LDBL_MAX; min = -max; break;
3847 default:
3848 lint_assert(/*CONSTCOND*/false);
3849 }
3850 if (ov->u.floating > max || ov->u.floating < min) {
3851 lint_assert(nt != LDOUBLE);
3852 const char *ot_name = type_name(gettyp(ov->v_tspec));
3853 const char *nt_name = type_name(ntp);
3854 if (is_integer(nt))
3855 goto after_warning;
3856 if (op == FARG)
3857 /* conversion of '%s' to '%s' is out of range, ... */
3858 warning(295, ot_name, nt_name, arg);
3859 else
3860 /* conversion of '%s' to '%s' is out of range */
3861 warning(119, ot_name, nt_name);
3862 after_warning:
3863 ov->u.floating = ov->u.floating > 0 ? max : min;
3864 }
3865
3866 if (nt == FLOAT || nt == FCOMPLEX)
3867 nv->u.floating = (float)ov->u.floating;
3868 else if (nt == DOUBLE || nt == DCOMPLEX)
3869 nv->u.floating = (double)ov->u.floating;
3870 else if (nt == LDOUBLE || nt == LCOMPLEX)
3871 nv->u.floating = ov->u.floating;
3872 else
3873 nv->u.integer = (int64_t)ov->u.floating;
3874 }
3875
3876 static bool
3877 convert_constant_to_floating(tspec_t nt, val_t *nv,
3878 tspec_t ot, const val_t *v)
3879 {
3880 if (nt == FLOAT) {
3881 nv->u.floating = (ot == PTR || is_uinteger(ot)) ?
3882 (float)(uint64_t)v->u.integer : (float)v->u.integer;
3883 } else if (nt == DOUBLE) {
3884 nv->u.floating = (ot == PTR || is_uinteger(ot)) ?
3885 (double)(uint64_t)v->u.integer : (double)v->u.integer;
3886 } else if (nt == LDOUBLE) {
3887 nv->u.floating = (ot == PTR || is_uinteger(ot))
3888 ? (long double)(uint64_t)v->u.integer
3889 : (long double)v->u.integer;
3890 } else
3891 return false;
3892 return true;
3893 }
3894
3895 /*
3896 * Print a warning if bits which were set are lost due to the conversion.
3897 * This can happen with operator ORASS only.
3898 */
3899 static void
3900 convert_constant_check_range_bitor(size_t nsz, size_t osz, const val_t *v,
3901 uint64_t xmask, op_t op)
3902 {
3903 if (nsz < osz && (v->u.integer & xmask) != 0)
3904 /* constant truncated by conversion, op '%s' */
3905 warning(306, op_name(op));
3906 }
3907
3908 static void
3909 convert_constant_check_range_bitand(size_t nsz, size_t osz,
3910 uint64_t xmask, const val_t *nv,
3911 tspec_t ot, const val_t *v,
3912 const type_t *tp, op_t op)
3913 {
3914 if (nsz > osz &&
3915 (nv->u.integer & bit((unsigned int)(osz - 1))) != 0 &&
3916 (nv->u.integer & xmask) != xmask) {
3917 /* extra bits set to 0 in conversion of '%s' to '%s', ... */
3918 warning(309, type_name(gettyp(ot)),
3919 type_name(tp), op_name(op));
3920 } else if (nsz < osz &&
3921 (v->u.integer & xmask) != xmask &&
3922 (v->u.integer & xmask) != 0)
3923 /* constant truncated by conversion, op '%s' */
3924 warning(306, op_name(op));
3925 }
3926
3927 static void
3928 convert_constant_check_range_signed(op_t op, int arg,
3929 const type_t *ntp, int64_t ov)
3930 {
3931 if (op == ASSIGN)
3932 /* assignment of negative constant %lld to unsigned ... */
3933 warning(164, (long long)ov, type_name(ntp));
3934 else if (op == INIT)
3935 /* initialization of unsigned type '%s' with negative ... */
3936 warning(221, type_name(ntp), (long long)ov);
3937 else if (op == FARG)
3938 /* conversion of negative constant %lld to unsigned ... */
3939 warning(296, (long long)ov, type_name(ntp), arg);
3940 else if (modtab[op].m_comparison) {
3941 /* handled by check_integer_comparison() */
3942 } else
3943 /* conversion of negative constant %lld to unsigned ... */
3944 warning(222, (long long)ov, type_name(ntp));
3945 }
3946
3947 /*
3948 * Loss of significant bit(s). All truncated bits of unsigned types or all
3949 * truncated bits plus the msb of the target for signed types are considered
3950 * to be significant bits. Loss of significant bits means that at least one
3951 * of the bits was set in an unsigned type or that at least one but not all
3952 * of the bits was set in a signed type. Loss of significant bits means that
3953 * it is not possible, also not with necessary casts, to convert back to the
3954 * original type. An example for a necessary cast is:
3955 * char c; int i; c = 128;
3956 * i = c; ** yields -128 **
3957 * i = (unsigned char)c; ** yields 128 **
3958 */
3959 static void
3960 warn_constant_check_range_truncated(op_t op, int arg, const type_t *tp,
3961 tspec_t ot)
3962 {
3963 if (op == ASSIGN && tp->t_bitfield)
3964 /* precision lost in bit-field assignment */
3965 warning(166);
3966 else if (op == ASSIGN)
3967 /* constant truncated by assignment */
3968 warning(165);
3969 else if (op == INIT && tp->t_bitfield)
3970 /* bit-field initializer does not fit */
3971 warning(180);
3972 else if (op == INIT)
3973 /* initializer does not fit */
3974 warning(178);
3975 else if (op == CASE)
3976 /* case label affected by conversion */
3977 warning(196);
3978 else if (op == FARG)
3979 /* conversion of '%s' to '%s' is out of range, arg #%d */
3980 warning(295, type_name(gettyp(ot)), type_name(tp), arg);
3981 else
3982 /* conversion of '%s' to '%s' is out of range */
3983 warning(119, type_name(gettyp(ot)), type_name(tp));
3984 }
3985
3986 static void
3987 warn_constant_check_range_loss(op_t op, int arg, const type_t *tp,
3988 tspec_t ot)
3989 {
3990 if (op == ASSIGN && tp->t_bitfield)
3991 /* precision lost in bit-field assignment */
3992 warning(166);
3993 else if (op == INIT && tp->t_bitfield)
3994 /* bit-field initializer out of range */
3995 warning(11);
3996 else if (op == CASE)
3997 /* case label affected by conversion */
3998 warning(196);
3999 else if (op == FARG)
4000 /* conversion of '%s' to '%s' is out of range, arg #%d */
4001 warning(295, type_name(gettyp(ot)), type_name(tp), arg);
4002 else
4003 /* conversion of '%s' to '%s' is out of range */
4004 warning(119, type_name(gettyp(ot)), type_name(tp));
4005 }
4006
4007 static void
4008 convert_constant_check_range(tspec_t ot, const type_t *tp, tspec_t nt,
4009 op_t op, int arg, const val_t *v, val_t *nv)
4010 {
4011 unsigned int obitsz, nbitsz;
4012 uint64_t xmask, xmsk1;
4013
4014 obitsz = size_in_bits(ot);
4015 nbitsz = tp->t_bitfield ? tp->t_bit_field_width : size_in_bits(nt);
4016 xmask = value_bits(nbitsz) ^ value_bits(obitsz);
4017 xmsk1 = value_bits(nbitsz) ^ value_bits(obitsz - 1);
4018 if (op == ORASS || op == BITOR || op == BITXOR) {
4019 convert_constant_check_range_bitor(
4020 nbitsz, obitsz, v, xmask, op);
4021 } else if (op == ANDASS || op == BITAND) {
4022 convert_constant_check_range_bitand(
4023 nbitsz, obitsz, xmask, nv, ot, v, tp, op);
4024 } else if (nt != PTR && is_uinteger(nt) &&
4025 ot != PTR && !is_uinteger(ot) &&
4026 v->u.integer < 0)
4027 convert_constant_check_range_signed(op, arg,
4028 tp, v->u.integer);
4029 else if (nv->u.integer != v->u.integer && nbitsz <= obitsz &&
4030 (v->u.integer & xmask) != 0 &&
4031 (is_uinteger(ot) || (v->u.integer & xmsk1) != xmsk1))
4032 warn_constant_check_range_truncated(op, arg, tp, ot);
4033 else if (nv->u.integer != v->u.integer)
4034 warn_constant_check_range_loss(op, arg, tp, ot);
4035 }
4036
4037 /* Converts a typed constant to a constant of another type. */
4038 void
4039 convert_constant(op_t op, int arg, const type_t *ntp, val_t *nv, val_t *ov)
4040 {
4041 /*
4042 * TODO: make 'ov' const; the name of this function does not suggest
4043 * that it modifies 'ov'.
4044 */
4045 tspec_t ot = ov->v_tspec;
4046 tspec_t nt = nv->v_tspec = ntp->t_tspec;
4047 bool range_check = false;
4048
4049 if (nt == BOOL) { /* C99 6.3.1.2 */
4050 nv->v_unsigned_since_c90 = false;
4051 nv->u.integer = is_nonzero_val(ov) ? 1 : 0;
4052 return;
4053 }
4054
4055 if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE)
4056 convert_constant_from_floating(op, arg, ntp, nt, nv, ov);
4057 else if (!convert_constant_to_floating(nt, nv, ot, ov)) {
4058 range_check = true; /* Check for lost precision. */
4059 nv->u.integer = ov->u.integer;
4060 }
4061
4062 if (allow_trad && allow_c90 && ov->v_unsigned_since_c90 &&
4063 (is_floating(nt) || (
4064 (is_integer(nt) && !is_uinteger(nt) &&
4065 portable_rank_cmp(nt, ot) > 0)))) {
4066 /* C90 treats constant as unsigned */
4067 warning(157);
4068 ov->v_unsigned_since_c90 = false;
4069 }
4070
4071 if (is_integer(nt)) {
4072 unsigned int size = ntp->t_bitfield
4073 ? ntp->t_bit_field_width : size_in_bits(nt);
4074 nv->u.integer = convert_integer(nv->u.integer, nt, size);
4075 }
4076
4077 if (range_check && op != CVT)
4078 convert_constant_check_range(ot, ntp, nt, op, arg, ov, nv);
4079 }
4080
4081 tnode_t *
4082 build_sizeof(const type_t *tp)
4083 {
4084 unsigned int size_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
4085 tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, size_in_bytes);
4086 tn->tn_system_dependent = true;
4087 debug_step("build_sizeof '%s' = %u", type_name(tp), size_in_bytes);
4088 return tn;
4089 }
4090
4091 tnode_t *
4092 build_offsetof(const type_t *tp, designation dn)
4093 {
4094 unsigned int offset_in_bits = 0;
4095
4096 if (!is_struct_or_union(tp->t_tspec)) {
4097 /* unacceptable operand of '%s' */
4098 error(111, "offsetof");
4099 goto proceed;
4100 }
4101 for (size_t i = 0; i < dn.dn_len; i++) {
4102 const designator *dr = dn.dn_items + i;
4103 if (dr->dr_kind == DK_SUBSCRIPT) {
4104 if (tp->t_tspec != ARRAY)
4105 goto proceed; /* silent error */
4106 tp = tp->t_subt;
4107 offset_in_bits += (unsigned) dr->dr_subscript
4108 * type_size_in_bits(tp);
4109 } else {
4110 if (!is_struct_or_union(tp->t_tspec))
4111 goto proceed; /* silent error */
4112 const char *name = dr->dr_member->s_name;
4113 sym_t *mem = find_member(tp->u.sou, name);
4114 if (mem == NULL) {
4115 /* type '%s' does not have member '%s' */
4116 error(101, name, type_name(tp));
4117 goto proceed;
4118 }
4119 tp = mem->s_type;
4120 offset_in_bits += mem->u.s_member.sm_offset_in_bits;
4121 }
4122 }
4123 free(dn.dn_items);
4124
4125 proceed:;
4126 unsigned int offset_in_bytes = offset_in_bits / CHAR_SIZE;
4127 tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, offset_in_bytes);
4128 tn->tn_system_dependent = true;
4129 return tn;
4130 }
4131
4132 unsigned int
4133 type_size_in_bits(const type_t *tp)
4134 {
4135
4136 unsigned int elem = 1;
4137 bool flex = false;
4138 lint_assert(tp != NULL);
4139 while (tp->t_tspec == ARRAY) {
4140 flex = true; /* allow c99 flex arrays [] [0] */
4141 elem *= tp->u.dimension;
4142 tp = tp->t_subt;
4143 }
4144 if (elem == 0 && !flex) {
4145 /* cannot take size/alignment of incomplete type */
4146 error(143);
4147 elem = 1;
4148 }
4149
4150 unsigned int elsz;
4151 switch (tp->t_tspec) {
4152 case VOID:
4153 /* cannot take size/alignment of void */
4154 error(146);
4155 elsz = 1;
4156 break;
4157 case FUNC:
4158 /* cannot take size/alignment of function type '%s' */
4159 error(144, type_name(tp));
4160 elsz = 1;
4161 break;
4162 case STRUCT:
4163 case UNION:
4164 if (is_incomplete(tp)) {
4165 /* cannot take size/alignment of incomplete type */
4166 error(143);
4167 elsz = 1;
4168 } else
4169 elsz = tp->u.sou->sou_size_in_bits;
4170 break;
4171 case ENUM:
4172 if (is_incomplete(tp)) {
4173 /* cannot take size/alignment of incomplete type */
4174 warning(143);
4175 }
4176 /* FALLTHROUGH */
4177 default:
4178 if (tp->t_bitfield)
4179 /* cannot take size/alignment of bit-field */
4180 error(145);
4181 elsz = size_in_bits(tp->t_tspec);
4182 lint_assert(elsz > 0);
4183 break;
4184 }
4185
4186 return elem * elsz;
4187 }
4188
4189 /* C11 6.5.3.4, GCC */
4190 tnode_t *
4191 build_alignof(const type_t *tp)
4192 {
4193 if (tp->t_tspec == FUNC) {
4194 /* cannot take size/alignment of function type '%s' */
4195 error(144, type_name(tp));
4196 return NULL;
4197 }
4198 if (tp->t_tspec == VOID) {
4199 /* cannot take size/alignment of void */
4200 error(146);
4201 return NULL;
4202 }
4203 if (is_incomplete(tp)) {
4204 /* cannot take size/alignment of incomplete type */
4205 error(143);
4206 return NULL;
4207 }
4208 if (tp->t_bitfield) {
4209 /* cannot take size/alignment of bit-field */
4210 error(145);
4211 return NULL;
4212 }
4213 return build_integer_constant(SIZEOF_TSPEC, (int64_t)alignment(tp));
4214 }
4215
4216 static tnode_t *
4217 cast_to_union(tnode_t *otn, bool sys, type_t *ntp)
4218 {
4219
4220 if (!allow_gcc) {
4221 /* union cast is a GCC extension */
4222 error(328);
4223 return NULL;
4224 }
4225
4226 for (const sym_t *m = ntp->u.sou->sou_first_member;
4227 m != NULL; m = m->s_next) {
4228 if (types_compatible(m->s_type, otn->tn_type,
4229 false, false, NULL)) {
4230 tnode_t *ntn = build_op(CVT, sys, ntp, otn, NULL);
4231 ntn->tn_cast = true;
4232 return ntn;
4233 }
4234 }
4235
4236 /* type '%s' is not a member of '%s' */
4237 error(329, type_name(otn->tn_type), type_name(ntp));
4238 return NULL;
4239 }
4240
4241 // In GCC mode, allow 'nullptr + offset' as a constant expression.
4242 static tnode_t *
4243 null_pointer_offset(tnode_t *tn)
4244 {
4245 uint64_t off = 0;
4246 const tnode_t *n = tn;
4247 while ((n->tn_op == PLUS || n->tn_op == MINUS)
4248 && is_integer(n->u.ops.right->tn_type->t_tspec)) {
4249 off += (uint64_t)n->u.ops.right->u.value.u.integer;
4250 n = n->u.ops.left;
4251 }
4252 if (n->tn_type->t_tspec == PTR
4253 && n->tn_op == ADDR
4254 && n->u.ops.left->tn_op == INDIR
4255 && n->u.ops.left->u.ops.left->tn_op == CON
4256 && n->u.ops.left->u.ops.left->tn_type->t_tspec == PTR) {
4257 off += (uint64_t)n->u.ops.left->u.ops.left->u.value.u.integer;
4258 return build_integer_constant(SIZEOF_TSPEC, (int64_t)off);
4259 }
4260 return tn;
4261 }
4262
4263 tnode_t *
4264 cast(tnode_t *tn, bool sys, type_t *tp)
4265 {
4266
4267 if (tn == NULL)
4268 return NULL;
4269
4270 tn = cconv(tn);
4271
4272 lint_assert(tp != NULL);
4273 tspec_t nt = tp->t_tspec;
4274 tspec_t ot = tn->tn_type->t_tspec;
4275
4276 if (nt == VOID) {
4277 /*
4278 * C90 6.3.4, C99 6.5.4p2 and C11 6.5.4p2 allow any type to be
4279 * cast to void. The only other allowed casts are from a
4280 * scalar type to a scalar type.
4281 */
4282 } else if (nt == UNION)
4283 return cast_to_union(tn, sys, tp);
4284 else if (nt == STRUCT || nt == ARRAY || nt == FUNC) {
4285 /* Casting to a struct is an undocumented GCC extension. */
4286 if (!(allow_gcc && nt == STRUCT))
4287 goto invalid_cast;
4288 } else if (is_struct_or_union(ot))
4289 goto invalid_cast;
4290 else if (ot == VOID) {
4291 /* improper cast of void expression */
4292 error(148);
4293 return NULL;
4294 } else if (is_integer(nt) && is_scalar(ot)) {
4295 tn = null_pointer_offset(tn);
4296 } else if (is_floating(nt) && is_arithmetic(ot)) {
4297 /* ok */
4298 } else if (nt == PTR && is_integer(ot)) {
4299 /* ok */
4300 } else if (nt == PTR && ot == PTR) {
4301 if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
4302 if (hflag)
4303 /* cast discards 'const' from type '%s' */
4304 warning(275, type_name(tn->tn_type));
4305 }
4306 } else
4307 goto invalid_cast;
4308
4309 if (any_query_enabled
4310 && types_compatible(tp, tn->tn_type, false, false, NULL))
4311 /* no-op cast from '%s' to '%s' */
4312 query_message(6, type_name(tn->tn_type), type_name(tp));
4313
4314 tn = convert(CVT, 0, tp, tn);
4315 tn->tn_cast = true;
4316 tn->tn_sys = sys;
4317
4318 return tn;
4319
4320 invalid_cast:
4321 /* invalid cast from '%s' to '%s' */
4322 error(147, type_name(tn->tn_type), type_name(tp));
4323 return NULL;
4324 }
4325
4326 void
4327 add_function_argument(function_call *call, tnode_t *arg)
4328 {
4329 /*
4330 * If there was a serious error in the expression for the argument,
4331 * create a dummy argument so the positions of the remaining arguments
4332 * will not change.
4333 */
4334 if (arg == NULL)
4335 arg = build_integer_constant(INT, 0);
4336
4337 if (call->args_len >= call->args_cap) {
4338 call->args_cap += 8;
4339 tnode_t **new_args = expr_zero_alloc(
4340 call->args_cap * sizeof(*call->args), "tnode*[]");
4341 if (call->args_len > 0)
4342 memcpy(new_args, call->args,
4343 call->args_len * sizeof(*call->args));
4344 call->args = new_args;
4345 }
4346 call->args[call->args_len++] = arg;
4347 }
4348
4349 /*
4350 * Compare the type of an argument with the corresponding type of a
4351 * prototype parameter. If it is a valid combination, but both types
4352 * are not the same, insert a conversion to convert the argument into
4353 * the type of the parameter.
4354 */
4355 static tnode_t *
4356 check_prototype_argument(
4357 int n, /* pos of arg */
4358 type_t *tp, /* expected type (from prototype) */
4359 tnode_t *tn) /* argument */
4360 {
4361 tnode_t *ln = xcalloc(1, sizeof(*ln));
4362 ln->tn_type = expr_unqualified_type(tp);
4363 ln->tn_lvalue = true;
4364 if (typeok(FARG, n, ln, tn)) {
4365 bool dowarn;
4366 if (!types_compatible(tp, tn->tn_type,
4367 true, false, (dowarn = false, &dowarn)) || dowarn)
4368 tn = convert(FARG, n, tp, tn);
4369 }
4370 free(ln);
4371 return tn;
4372 }
4373
4374 /*
4375 * Check types of all function arguments and insert conversions,
4376 * if necessary.
4377 */
4378 static void
4379 check_function_arguments(const function_call *call)
4380 {
4381 type_t *ftp = call->func->tn_type->t_subt;
4382
4383 /* get # of parameters in the prototype */
4384 int npar = 0;
4385 for (const sym_t *p = ftp->u.params; p != NULL; p = p->s_next)
4386 npar++;
4387
4388 int narg = (int)call->args_len;
4389
4390 const sym_t *param = ftp->u.params;
4391 if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
4392 /* argument mismatch: %d %s passed, %d expected */
4393 error(150, narg, narg != 1 ? "arguments" : "argument", npar);
4394 param = NULL;
4395 }
4396
4397 for (int i = 0; i < narg; i++) {
4398 tnode_t *arg = call->args[i];
4399
4400 /* some things which are always not allowed */
4401 tspec_t at = arg->tn_type->t_tspec;
4402 if (at == VOID) {
4403 /* void expressions may not be arguments, arg #%d */
4404 error(151, i + 1);
4405 return;
4406 }
4407 if (is_struct_or_union(at) && is_incomplete(arg->tn_type)) {
4408 /* argument cannot have unknown size, arg #%d */
4409 error(152, i + 1);
4410 return;
4411 }
4412 if (is_integer(at) &&
4413 arg->tn_type->t_is_enum &&
4414 is_incomplete(arg->tn_type)) {
4415 /* argument cannot have unknown size, arg #%d */
4416 warning(152, i + 1);
4417 }
4418
4419 arg = cconv(arg);
4420 call->args[i] = arg;
4421
4422 arg = param != NULL
4423 ? check_prototype_argument(i + 1, param->s_type, arg)
4424 : promote(NOOP, true, arg);
4425 call->args[i] = arg;
4426
4427 if (param != NULL)
4428 param = param->s_next;
4429 }
4430 }
4431
4432 tnode_t *
4433 build_function_call(tnode_t *func, bool sys, function_call *call)
4434 {
4435
4436 if (func == NULL)
4437 return NULL;
4438
4439 call->func = func;
4440 check_ctype_function_call(call);
4441
4442 func = cconv(func);
4443 call->func = func;
4444
4445 if (func->tn_type->t_tspec != PTR ||
4446 func->tn_type->t_subt->t_tspec != FUNC) {
4447 /* cannot call '%s', must be a function */
4448 error(149, type_name(func->tn_type));
4449 return NULL;
4450 }
4451
4452 check_function_arguments(call);
4453
4454 tnode_t *ntn = expr_alloc_tnode();
4455 ntn->tn_op = CALL;
4456 ntn->tn_type = func->tn_type->t_subt->t_subt;
4457 ntn->tn_sys = sys;
4458 ntn->u.call = call;
4459 return ntn;
4460 }
4461
4462 /*
4463 * Return the value of an integral constant expression.
4464 * If the expression is not constant or its type is not an integer
4465 * type, an error message is printed.
4466 */
4467 val_t *
4468 integer_constant(tnode_t *tn, bool required)
4469 {
4470
4471 if (tn != NULL)
4472 tn = cconv(tn);
4473 if (tn != NULL)
4474 tn = promote(NOOP, false, tn);
4475
4476 val_t *v = xcalloc(1, sizeof(*v));
4477
4478 if (tn == NULL) {
4479 lint_assert(seen_error);
4480 debug_step("constant node is null; returning 1 instead");
4481 v->v_tspec = INT;
4482 v->u.integer = 1;
4483 return v;
4484 }
4485
4486 v->v_tspec = tn->tn_type->t_tspec;
4487
4488 if (tn->tn_op == CON) {
4489 lint_assert(tn->tn_type->t_tspec == tn->u.value.v_tspec);
4490 if (is_integer(tn->u.value.v_tspec)) {
4491 v->v_unsigned_since_c90 =
4492 tn->u.value.v_unsigned_since_c90;
4493 v->u.integer = tn->u.value.u.integer;
4494 return v;
4495 }
4496 v->u.integer = (int64_t)tn->u.value.u.floating;
4497 } else
4498 v->u.integer = 1;
4499
4500 if (required)
4501 /* integral constant expression expected */
4502 error(55);
4503 else
4504 /* variable array dimension is a C99/GCC extension */
4505 c99ism(318);
4506
4507 if (!is_integer(v->v_tspec))
4508 v->v_tspec = INT;
4509
4510 return v;
4511 }
4512
4513 static bool
4514 is_constcond_false(const tnode_t *tn, tspec_t t)
4515 {
4516 return (t == BOOL || t == INT) &&
4517 tn->tn_op == CON && tn->u.value.u.integer == 0;
4518 }
4519
4520 /*
4521 * Perform some tests on expressions which can't be done in build_binary()
4522 * and functions called by build_binary(). These tests must be done here
4523 * because we need some information about the context in which the operations
4524 * are performed.
4525 * After all tests are performed and dofreeblk is true, expr() frees the
4526 * memory which is used for the expression.
4527 */
4528 void
4529 expr(tnode_t *tn, bool vctx, bool cond, bool dofreeblk, bool is_do_while,
4530 const char *stmt_kind)
4531 {
4532
4533 if (tn == NULL) { /* in case of errors */
4534 expr_free_all();
4535 return;
4536 }
4537
4538 /* expr() is also called in global initializations */
4539 if (dcs->d_kind != DLK_EXTERN && !is_do_while)
4540 check_statement_reachable(stmt_kind);
4541
4542 check_expr_misc(tn, vctx, cond, !cond, false, false, false);
4543 if (tn->tn_op == ASSIGN && !tn->tn_parenthesized) {
4544 if (hflag && cond)
4545 /* assignment in conditional context */
4546 warning(159);
4547 } else if (tn->tn_op == CON) {
4548 if (hflag && cond && !suppress_constcond &&
4549 !tn->tn_system_dependent &&
4550 !(is_do_while &&
4551 is_constcond_false(tn, tn->tn_type->t_tspec)))
4552 /* constant in conditional context */
4553 warning(161);
4554 }
4555 if (!modtab[tn->tn_op].m_has_side_effect) {
4556 /*
4557 * for left operands of COMMA this warning is already printed
4558 */
4559 if (tn->tn_op != COMMA && !vctx && !cond)
4560 check_null_effect(tn);
4561 }
4562 debug_node(tn);
4563
4564 if (dofreeblk)
4565 expr_free_all();
4566 }
4567
4568 /* If the expression has the form '*(arr + idx)', check the array index. */
4569 static void
4570 check_array_index(const tnode_t *indir, bool taking_address)
4571 {
4572 const tnode_t *plus, *arr, *idx;
4573
4574 if (indir->tn_op == INDIR
4575 && (plus = indir->u.ops.left, plus->tn_op == PLUS)
4576 && plus->u.ops.left->tn_op == ADDR
4577 && (arr = plus->u.ops.left->u.ops.left, true)
4578 && (arr->tn_op == STRING || arr->tn_op == NAME)
4579 && arr->tn_type->t_tspec == ARRAY
4580 && (idx = plus->u.ops.right, idx->tn_op == CON)
4581 && (!is_incomplete(arr->tn_type) || idx->u.value.u.integer < 0))
4582 goto proceed;
4583 return;
4584
4585 proceed:;
4586 int elsz = length_in_bits(arr->tn_type->t_subt, NULL);
4587 if (elsz == 0)
4588 return;
4589 elsz /= CHAR_SIZE;
4590
4591 /* Change the unit of the index from bytes to element size. */
4592 int64_t con = is_uinteger(idx->tn_type->t_tspec)
4593 ? (int64_t)((uint64_t)idx->u.value.u.integer / elsz)
4594 : idx->u.value.u.integer / elsz;
4595
4596 int dim = arr->tn_type->u.dimension + (taking_address ? 1 : 0);
4597
4598 if (!is_uinteger(idx->tn_type->t_tspec) && con < 0)
4599 /* array subscript %jd cannot be negative */
4600 warning(167, (intmax_t)con);
4601 else if (dim > 0 && (uint64_t)con >= (uint64_t)dim)
4602 /* array subscript %ju cannot be > %d */
4603 warning(168, (uintmax_t)con, dim - 1);
4604 }
4605
4606 static void
4607 check_expr_addr(const tnode_t *ln, bool szof, bool fcall)
4608 {
4609 /* XXX: Taking warn_about_unreachable into account here feels wrong. */
4610 if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
4611 if (!szof)
4612 mark_as_set(ln->u.sym);
4613 mark_as_used(ln->u.sym, fcall, szof);
4614 }
4615 check_array_index(ln, true);
4616 }
4617
4618 /*
4619 * If there is an asm statement in one of the compound statements around,
4620 * there may be other side effects, so don't warn.
4621 */
4622 static bool
4623 is_asm_around(void)
4624 {
4625 for (decl_level *dl = dcs; dl != NULL; dl = dl->d_enclosing)
4626 if (dl->d_asm)
4627 return true;
4628 return false;
4629 }
4630
4631 static void
4632 check_expr_side_effect(const tnode_t *ln, bool szof)
4633 {
4634
4635 /* XXX: Taking warn_about_unreachable into account here feels wrong. */
4636 if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
4637 scl_t sc = ln->u.sym->s_scl;
4638 if (sc != EXTERN && sc != STATIC &&
4639 !ln->u.sym->s_set && !szof && !is_asm_around()) {
4640 /* '%s' may be used before set */
4641 warning(158, ln->u.sym->s_name);
4642 mark_as_set(ln->u.sym);
4643 }
4644 mark_as_used(ln->u.sym, false, false);
4645 }
4646 }
4647
4648 static void
4649 check_expr_assign(const tnode_t *ln, bool szof)
4650 {
4651 /* XXX: Taking warn_about_unreachable into account here feels wrong. */
4652 if (ln->tn_op == NAME && !szof && (reached || !warn_about_unreachable)) {
4653 mark_as_set(ln->u.sym);
4654 if (ln->u.sym->s_scl == EXTERN)
4655 outusg(ln->u.sym);
4656 }
4657 check_array_index(ln, false);
4658 }
4659
4660 static void
4661 check_expr_call(const tnode_t *tn, const tnode_t *ln,
4662 bool szof, bool vctx, bool cond, bool retval_discarded)
4663 {
4664 lint_assert(ln->tn_op == ADDR);
4665 lint_assert(ln->u.ops.left->tn_op == NAME);
4666 if (!szof && !is_compiler_builtin(ln->u.ops.left->u.sym->s_name))
4667 outcall(tn, vctx || cond, retval_discarded);
4668
4669 const function_call *call = tn->u.call;
4670 if (call->args_len == 4 || call->args_len == 5)
4671 check_snprintb(call);
4672 }
4673
4674 static void
4675 check_expr_op(op_t op, const tnode_t *ln, bool szof, bool fcall, bool eqwarn)
4676 {
4677 switch (op) {
4678 case ADDR:
4679 check_expr_addr(ln, szof, fcall);
4680 break;
4681 case LOAD:
4682 check_array_index(ln, false);
4683 /* FALLTHROUGH */
4684 case INCBEF:
4685 case DECBEF:
4686 case INCAFT:
4687 case DECAFT:
4688 case ADDASS:
4689 case SUBASS:
4690 case MULASS:
4691 case DIVASS:
4692 case MODASS:
4693 case ANDASS:
4694 case ORASS:
4695 case XORASS:
4696 case SHLASS:
4697 case SHRASS:
4698 case REAL:
4699 case IMAG:
4700 check_expr_side_effect(ln, szof);
4701 break;
4702 case ASSIGN:
4703 check_expr_assign(ln, szof);
4704 break;
4705 case EQ:
4706 if (hflag && eqwarn)
4707 /* operator '==' found where '=' was expected */
4708 warning(160);
4709 break;
4710 default:
4711 break;
4712 }
4713 }
4714
4715 /*
4716 * vctx ???
4717 * cond whether the expression is a condition that
4718 * will be compared with 0
4719 * eqwarn whether the operator '==' might be a
4720 * misspelled '='
4721 * fcall whether the expression is a function call
4722 * retval_discarded whether the return value of a function call
4723 * is discarded; such calls will be analyzed by
4724 * lint2 in messages 4, 8 and 9
4725 * szof whether the expression is part of a sizeof
4726 * expression, which means that its value is
4727 * discarded since only the type is relevant
4728 */
4729 void
4730 check_expr_misc(const tnode_t *tn, bool vctx, bool cond,
4731 bool eqwarn, bool fcall, bool retval_discarded, bool szof)
4732 {
4733
4734 if (tn == NULL)
4735 return;
4736 op_t op = tn->tn_op;
4737 if (op == NAME || op == CON || op == STRING)
4738 return;
4739 bool is_direct = op == CALL
4740 && tn->u.call->func->tn_op == ADDR
4741 && tn->u.call->func->u.ops.left->tn_op == NAME;
4742 if (op == CALL) {
4743 const function_call *call = tn->u.call;
4744 if (is_direct)
4745 check_expr_call(tn, call->func,
4746 szof, vctx, cond, retval_discarded);
4747 bool discard = op == CVT && tn->tn_type->t_tspec == VOID;
4748 check_expr_misc(call->func, false, false, false, is_direct,
4749 discard, szof);
4750 for (size_t i = 0, n = call->args_len; i < n; i++)
4751 check_expr_misc(call->args[i],
4752 true, false, false, false, false, szof);
4753 return;
4754 }
4755
4756 lint_assert(has_operands(tn));
4757 tnode_t *ln = tn->u.ops.left;
4758 tnode_t *rn = tn->u.ops.right;
4759 check_expr_op(op, ln, szof, fcall, eqwarn);
4760
4761 const mod_t *mp = &modtab[op];
4762 bool cvctx = mp->m_value_context;
4763 bool ccond = mp->m_compares_with_zero;
4764 bool eq = mp->m_warn_if_operand_eq &&
4765 !ln->tn_parenthesized &&
4766 rn != NULL && !rn->tn_parenthesized;
4767
4768 /*
4769 * Values of operands of ':' are not used if the type of at least
4770 * one of the operands (for GCC compatibility) is 'void'.
4771 *
4772 * XXX test/value context of QUEST should probably be used as
4773 * context for both operands of COLON.
4774 */
4775 if (op == COLON && tn->tn_type->t_tspec == VOID)
4776 cvctx = ccond = false;
4777 bool discard = op == CVT && tn->tn_type->t_tspec == VOID;
4778 check_expr_misc(ln, cvctx, ccond, eq, is_direct, discard, szof);
4779
4780 switch (op) {
4781 case LOGAND:
4782 case LOGOR:
4783 check_expr_misc(rn, false, true, eq, false, false, szof);
4784 break;
4785 case COLON:
4786 check_expr_misc(rn, cvctx, ccond, eq, false, false, szof);
4787 break;
4788 case COMMA:
4789 check_expr_misc(rn, vctx, cond, false, false, false, szof);
4790 break;
4791 default:
4792 if (mp->m_binary)
4793 check_expr_misc(rn, true, false, eq, false, false,
4794 szof);
4795 break;
4796 }
4797 }
4798
4799 /*
4800 * Return whether the expression can be used for static initialization.
4801 *
4802 * Constant initialization expressions must be constant or an address
4803 * of a static object with an optional offset. In the first case,
4804 * the result is returned in *offsp. In the second case, the static
4805 * object is returned in *symp and the offset in *offsp.
4806 *
4807 * The expression can consist of PLUS, MINUS, ADDR, NAME, STRING and
4808 * CON. Type conversions are allowed if they do not change binary
4809 * representation (including width).
4810 *
4811 * C99 6.6 "Constant expressions"
4812 * C99 6.7.8p4 restricts initializers for static storage duration
4813 */
4814 bool
4815 constant_addr(const tnode_t *tn, const sym_t **symp, ptrdiff_t *offsp)
4816 {
4817 const sym_t *sym;
4818 ptrdiff_t offs1, offs2;
4819 tspec_t t, ot;
4820
4821 switch (tn->tn_op) {
4822 case MINUS:
4823 if (tn->u.ops.right->tn_op == CVT)
4824 return constant_addr(tn->u.ops.right, symp, offsp);
4825 else if (tn->u.ops.right->tn_op != CON)
4826 return false;
4827 /* FALLTHROUGH */
4828 case PLUS:
4829 offs1 = offs2 = 0;
4830 if (tn->u.ops.left->tn_op == CON) {
4831 offs1 = (ptrdiff_t)tn->u.ops.left->u.value.u.integer;
4832 if (!constant_addr(tn->u.ops.right, &sym, &offs2))
4833 return false;
4834 } else if (tn->u.ops.right->tn_op == CON) {
4835 offs2 = (ptrdiff_t)tn->u.ops.right->u.value.u.integer;
4836 if (tn->tn_op == MINUS)
4837 offs2 = -offs2;
4838 if (!constant_addr(tn->u.ops.left, &sym, &offs1))
4839 return false;
4840 } else {
4841 return false;
4842 }
4843 *symp = sym;
4844 *offsp = offs1 + offs2;
4845 return true;
4846 case ADDR:
4847 if (tn->u.ops.left->tn_op == NAME) {
4848 *symp = tn->u.ops.left->u.sym;
4849 *offsp = 0;
4850 return true;
4851 } else {
4852 /*
4853 * If this were the front end of a compiler, we would
4854 * return a label instead of 0, at least if
4855 * 'tn->u.ops.left->tn_op == STRING'.
4856 */
4857 *symp = NULL;
4858 *offsp = 0;
4859 return true;
4860 }
4861 case CVT:
4862 t = tn->tn_type->t_tspec;
4863 ot = tn->u.ops.left->tn_type->t_tspec;
4864 if ((!is_integer(t) && t != PTR) ||
4865 (!is_integer(ot) && ot != PTR)) {
4866 return false;
4867 }
4868 #if 0
4869 /*-
4870 * consider:
4871 * struct foo {
4872 * unsigned char a;
4873 * } f = {
4874 * (unsigned char)(unsigned long)
4875 * (&(((struct foo *)0)->a))
4876 * };
4877 * since psize(unsigned long) != psize(unsigned char),
4878 * this fails.
4879 */
4880 else if (psize(t) != psize(ot))
4881 return -1;
4882 #endif
4883 return constant_addr(tn->u.ops.left, symp, offsp);
4884 default:
4885 return false;
4886 }
4887 }
4888
4889 /* Append s2 to s1, then free s2. */
4890 buffer *
4891 cat_strings(buffer *s1, buffer *s2)
4892 {
4893
4894 if ((s1->data != NULL) != (s2->data != NULL)) {
4895 /* cannot concatenate wide and regular string literals */
4896 error(292);
4897 return s1;
4898 }
4899
4900 if (s1->data != NULL) {
4901 while (s1->len + s2->len + 1 > s1->cap)
4902 s1->cap *= 2;
4903 s1->data = xrealloc(s1->data, s1->cap);
4904 memcpy(s1->data + s1->len, s2->data, s2->len + 1);
4905 free(s2->data);
4906 }
4907 s1->len += s2->len;
4908 free(s2);
4909
4910 return s1;
4911 }
4912
4913
4914 typedef struct stmt_expr {
4915 memory_pool se_mem;
4916 sym_t *se_sym;
4917 struct stmt_expr *se_enclosing;
4918 } stmt_expr;
4919
4920 static stmt_expr *stmt_exprs;
4921
4922 void
4923 begin_statement_expr(void)
4924 {
4925 debug_enter();
4926
4927 stmt_expr *se = xmalloc(sizeof(*se));
4928 se->se_mem = expr_save_memory();
4929 se->se_sym = NULL;
4930 se->se_enclosing = stmt_exprs;
4931 stmt_exprs = se;
4932 }
4933
4934 void
4935 do_statement_expr(tnode_t *tn)
4936 {
4937 block_level--;
4938 mem_block_level--;
4939 stmt_exprs->se_sym = tn != NULL
4940 ? mktempsym(block_dup_type(tn->tn_type))
4941 : NULL; /* after a syntax error */
4942 mem_block_level++;
4943 block_level++;
4944 /* '({ ... })' is a GCC extension */
4945 gnuism(320);
4946 }
4947
4948 tnode_t *
4949 end_statement_expr(void)
4950 {
4951 tnode_t *tn;
4952
4953 stmt_expr *se = stmt_exprs;
4954 if (se->se_sym == NULL) {
4955 tn = NULL; /* after a syntax error */
4956 goto end;
4957 }
4958
4959 tn = build_name(se->se_sym, false);
4960 (void)expr_save_memory(); /* leak */
4961 expr_restore_memory(se->se_mem);
4962 stmt_exprs = se->se_enclosing;
4963 free(se);
4964
4965 end:
4966 debug_leave();
4967 return tn;
4968 }
4969
4970 bool
4971 in_statement_expr(void)
4972 {
4973 return stmt_exprs != NULL;
4974 }
4975