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