tree.c revision 1.681 1 /* $NetBSD: tree.c,v 1.681 2025/04/12 15:49:49 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.681 2025/04/12 15:49:49 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 NOT:
2033 if (ln->tn_op == ASSIGN && ln->u.ops.right->tn_op == CON) {
2034 /* constant assignment of type '%s' in operand ... */
2035 warning(382, type_name(ln->tn_type),
2036 is_nonzero_val(&ln->u.ops.right->u.value)
2037 ? "true" : "false");
2038 }
2039 ntn = build_op(op, sys, gettyp(Tflag ? BOOL : INT), ln, NULL);
2040 break;
2041 case INCAFT:
2042 case DECAFT:
2043 case INCBEF:
2044 case DECBEF:
2045 ntn = build_prepost_incdec(op, sys, ln);
2046 break;
2047 case ADDR:
2048 ntn = build_address(sys, ln);
2049 break;
2050 case INDIR:
2051 ntn = build_op(INDIR, sys, ln->tn_type->t_subt, ln, NULL);
2052 break;
2053 case PLUS:
2054 case MINUS:
2055 ntn = build_plus_minus(op, sys, ln, rn);
2056 break;
2057 case SHL:
2058 case SHR:
2059 ntn = build_bit_shift(op, sys, ln, rn);
2060 break;
2061 case COLON:
2062 ntn = build_colon(sys, ln, rn);
2063 break;
2064 case ASSIGN:
2065 case MULASS:
2066 case DIVASS:
2067 case MODASS:
2068 case ADDASS:
2069 case SUBASS:
2070 case SHLASS:
2071 case SHRASS:
2072 case ANDASS:
2073 case XORASS:
2074 case ORASS:
2075 case RETURN:
2076 case INIT:
2077 ntn = build_assignment(op, sys, ln, rn);
2078 break;
2079 case COMMA:
2080 /* comma operator with types '%s' and '%s' */
2081 query_message(12,
2082 type_name(ln->tn_type), type_name(rn->tn_type));
2083 /* FALLTHROUGH */
2084 case QUEST:
2085 ntn = build_op(op, sys, rn->tn_type, ln, rn);
2086 break;
2087 case REAL:
2088 case IMAG:
2089 ntn = build_real_imag(op, sys, ln);
2090 break;
2091 default:
2092 lint_assert(mp->m_binary == (rn != NULL));
2093 type_t *rettp = mp->m_returns_bool
2094 ? gettyp(Tflag ? BOOL : INT) : ln->tn_type;
2095 ntn = build_op(op, sys, rettp, ln, rn);
2096 break;
2097 }
2098
2099 if (ntn == NULL)
2100 return NULL;
2101
2102 if (mp->m_possible_precedence_confusion)
2103 check_precedence_confusion(ntn);
2104
2105 if (mp->m_fold_constant_operands && ln->tn_op == CON) {
2106 if (!mp->m_binary || rn->tn_op == CON) {
2107 if (mp->m_compares_with_zero)
2108 ntn = fold_constant_compare_zero(ntn);
2109 else if (is_floating(ntn->tn_type->t_tspec))
2110 ntn = fold_constant_floating(ntn);
2111 else
2112 ntn = fold_constant_integer(ntn);
2113 } else if (op == QUEST) {
2114 lint_assert(has_operands(rn));
2115 use(ln->u.value.u.integer != 0
2116 ? rn->u.ops.right : rn->u.ops.left);
2117 ntn = ln->u.value.u.integer != 0
2118 ? rn->u.ops.left : rn->u.ops.right;
2119 }
2120 }
2121
2122 return ntn;
2123 }
2124
2125 tnode_t *
2126 build_unary(op_t op, bool sys, tnode_t *tn)
2127 {
2128 return build_binary(tn, op, sys, NULL);
2129 }
2130
2131 static bool
2132 are_members_compatible(const sym_t *a, const sym_t *b)
2133 {
2134 if (a->u.s_member.sm_offset_in_bits != b->u.s_member.sm_offset_in_bits)
2135 return false;
2136
2137 const type_t *atp = a->s_type;
2138 const type_t *btp = b->s_type;
2139 bool w = false;
2140 if (!types_compatible(atp, btp, false, false, &w) && !w)
2141 return false;
2142 if (a->s_bitfield != b->s_bitfield)
2143 return false;
2144 if (a->s_bitfield) {
2145 if (atp->t_bit_field_width != btp->t_bit_field_width)
2146 return false;
2147 if (atp->t_bit_field_offset != btp->t_bit_field_offset)
2148 return false;
2149 }
2150 return true;
2151 }
2152
2153 /*
2154 * Return whether all struct/union members with the same name have the same
2155 * type and offset.
2156 */
2157 static bool
2158 all_members_compatible(const sym_t *msym)
2159 {
2160 for (const sym_t *csym = msym;
2161 csym != NULL; csym = csym->s_symtab_next) {
2162 if (!is_member(csym))
2163 continue;
2164 if (strcmp(msym->s_name, csym->s_name) != 0)
2165 continue;
2166
2167 for (const sym_t *sym = csym->s_symtab_next;
2168 sym != NULL; sym = sym->s_symtab_next) {
2169 if (is_member(sym)
2170 && strcmp(csym->s_name, sym->s_name) == 0
2171 && !are_members_compatible(csym, sym))
2172 return false;
2173 }
2174 }
2175 return true;
2176 }
2177
2178 sym_t *
2179 find_member(const struct_or_union *sou, const char *name)
2180 {
2181 for (sym_t *mem = sou->sou_first_member;
2182 mem != NULL; mem = mem->s_next) {
2183 lint_assert(is_member(mem));
2184 lint_assert(mem->u.s_member.sm_containing_type == sou);
2185 if (strcmp(mem->s_name, name) == 0)
2186 return mem;
2187 }
2188
2189 for (sym_t *mem = sou->sou_first_member;
2190 mem != NULL; mem = mem->s_next) {
2191 if (is_struct_or_union(mem->s_type->t_tspec)
2192 && mem->s_name == unnamed) {
2193 sym_t *nested_mem =
2194 find_member(mem->s_type->u.sou, name);
2195 if (nested_mem != NULL)
2196 return nested_mem;
2197 }
2198 }
2199 return NULL;
2200 }
2201
2202 /*
2203 * Remove the member if it was unknown until now, which means
2204 * that no defined struct or union has a member with the same name.
2205 */
2206 static void
2207 remove_unknown_member(tnode_t *tn, sym_t *msym)
2208 {
2209 /* type '%s' does not have member '%s' */
2210 error(101, type_name(tn->tn_type), msym->s_name);
2211 symtab_remove_forever(msym);
2212 msym->s_kind = SK_MEMBER;
2213 msym->s_scl = STRUCT_MEMBER;
2214
2215 struct_or_union *sou = expr_zero_alloc(sizeof(*sou),
2216 "struct_or_union");
2217 sou->sou_tag = expr_zero_alloc(sizeof(*sou->sou_tag), "sym");
2218 sou->sou_tag->s_name = unnamed;
2219
2220 msym->u.s_member.sm_containing_type = sou;
2221 /*
2222 * The member sm_offset_in_bits is not needed here since this symbol
2223 * can only be used for error reporting.
2224 */
2225 }
2226
2227 /*
2228 * Returns a symbol which has the same name as 'msym' and is a member of the
2229 * struct or union specified by 'tn'.
2230 */
2231 static sym_t *
2232 struct_or_union_member(tnode_t *tn, op_t op, sym_t *msym)
2233 {
2234
2235 /* Determine the tag type of which msym is expected to be a member. */
2236 const type_t *tp = NULL;
2237 if (op == POINT && is_struct_or_union(tn->tn_type->t_tspec))
2238 tp = tn->tn_type;
2239 if (op == ARROW && tn->tn_type->t_tspec == PTR
2240 && is_struct_or_union(tn->tn_type->t_subt->t_tspec))
2241 tp = tn->tn_type->t_subt;
2242 struct_or_union *sou = tp != NULL ? tp->u.sou : NULL;
2243
2244 if (sou != NULL) {
2245 sym_t *nested_mem = find_member(sou, msym->s_name);
2246 if (nested_mem != NULL)
2247 return nested_mem;
2248 }
2249
2250 if (msym->s_scl == NO_SCL) {
2251 remove_unknown_member(tn, msym);
2252 return msym;
2253 }
2254
2255 bool eq = all_members_compatible(msym);
2256
2257 /*
2258 * Now handle the case in which the left operand refers really to a
2259 * struct/union, but the right operand is not member of it.
2260 */
2261 if (sou != NULL) {
2262 if (eq && !allow_c90)
2263 /* invalid use of member '%s' */
2264 warning(102, msym->s_name);
2265 else
2266 /* invalid use of member '%s' */
2267 error(102, msym->s_name);
2268 return msym;
2269 }
2270
2271 if (eq) {
2272 if (op == POINT) {
2273 if (!allow_c90)
2274 /* left operand of '.' must be struct ... */
2275 warning(103, type_name(tn->tn_type));
2276 else
2277 /* left operand of '.' must be struct ... */
2278 error(103, type_name(tn->tn_type));
2279 } else {
2280 if (!allow_c90 && tn->tn_type->t_tspec == PTR)
2281 /* left operand of '->' must be pointer ... */
2282 warning(104, type_name(tn->tn_type));
2283 else
2284 /* left operand of '->' must be pointer ... */
2285 error(104, type_name(tn->tn_type));
2286 }
2287 } else {
2288 if (!allow_c90)
2289 /* non-unique member requires struct/union %s */
2290 error(105, op == POINT ? "object" : "pointer");
2291 else
2292 /* unacceptable operand of '%s' */
2293 error(111, op_name(op));
2294 }
2295
2296 return msym;
2297 }
2298
2299 tnode_t *
2300 build_member_access(tnode_t *ln, op_t op, bool sys, sbuf_t *member)
2301 {
2302 sym_t *msym;
2303
2304 if (ln == NULL)
2305 return NULL;
2306
2307 if (op == ARROW)
2308 /* must do this before struct_or_union_member is called */
2309 ln = cconv(ln);
2310 msym = struct_or_union_member(ln, op, getsym(member));
2311 return build_binary(ln, op, sys, build_name(msym, false));
2312 }
2313
2314 /*
2315 * Perform class conversions.
2316 *
2317 * Arrays of type T are converted into pointers to type T.
2318 * Functions are converted to pointers to functions.
2319 * Lvalues are converted to rvalues.
2320 *
2321 * C99 6.3 "Conversions"
2322 * C99 6.3.2 "Other operands"
2323 * C99 6.3.2.1 "Lvalues, arrays, and function designators"
2324 */
2325 tnode_t *
2326 cconv(tnode_t *tn)
2327 {
2328 if (tn->tn_type->t_tspec == ARRAY) {
2329 if (!tn->tn_lvalue) {
2330 /* XXX print correct operator */
2331 /* %soperand of '%s' must be lvalue */
2332 gnuism(114, "", op_name(ADDR));
2333 }
2334 tn = build_op(ADDR, tn->tn_sys,
2335 expr_derive_type(tn->tn_type->t_subt, PTR), tn, NULL);
2336 }
2337
2338 if (tn->tn_type->t_tspec == FUNC)
2339 tn = build_address(tn->tn_sys, tn);
2340
2341 if (tn->tn_lvalue) {
2342 type_t *tp = expr_dup_type(tn->tn_type);
2343 /* C99 6.3.2.1p2 sentence 2 says to remove the qualifiers. */
2344 tp->t_const = tp->t_volatile = false;
2345 tn = build_op(LOAD, tn->tn_sys, tp, tn, NULL);
2346 }
2347
2348 return tn;
2349 }
2350
2351 const tnode_t *
2352 before_conversion(const tnode_t *tn)
2353 {
2354 while (tn->tn_op == CVT && !tn->tn_cast)
2355 tn = tn->u.ops.left;
2356 return tn;
2357 }
2358
2359 /*
2360 * Most errors required by C90 are reported in struct_or_union_member().
2361 * Here we only check for totally wrong things.
2362 */
2363 static bool
2364 typeok_point(const tnode_t *ln, const type_t *ltp, tspec_t lt)
2365 {
2366 if (is_struct_or_union(lt))
2367 return true;
2368
2369 if (lt == FUNC || lt == VOID || ltp->t_bitfield)
2370 goto wrong;
2371
2372 /*
2373 * Some C dialects from before C90 tolerated any lvalue on the
2374 * left-hand side of the '.' operator, allowing things like 'char
2375 * st[100]; st.st_mtime', assuming that the member 'st_mtime' only
2376 * occurred in a single struct; see typeok_arrow.
2377 */
2378 if (ln->tn_lvalue)
2379 return true;
2380
2381 wrong:
2382 /* With allow_c90 we already got an error */
2383 if (!allow_c90)
2384 /* unacceptable operand of '%s' */
2385 error(111, op_name(POINT));
2386
2387 return false;
2388 }
2389
2390 static bool
2391 typeok_arrow(tspec_t lt)
2392 {
2393 /*
2394 * C1978 Appendix A 14.1 says: <quote>In fact, any lvalue is allowed
2395 * before '.', and that lvalue is then assumed to have the form of the
2396 * structure of which the name of the right is a member. [...] Such
2397 * constructions are non-portable.</quote>
2398 */
2399 if (lt == PTR || (!allow_c90 && is_integer(lt)))
2400 return true;
2401
2402 /* With allow_c90 we already got an error */
2403 if (!allow_c90)
2404 /* unacceptable operand of '%s' */
2405 error(111, op_name(ARROW));
2406 return false;
2407 }
2408
2409 static bool
2410 typeok_incdec(op_t op, const tnode_t *tn, const type_t *tp)
2411 {
2412 /* operand has scalar type (checked in typeok) */
2413 if (!tn->tn_lvalue) {
2414 if (tn->tn_op == CVT && tn->tn_cast &&
2415 tn->u.ops.left->tn_op == LOAD)
2416 /* a cast does not yield an lvalue */
2417 error(163);
2418 /* %soperand of '%s' must be lvalue */
2419 error(114, "", op_name(op));
2420 return false;
2421 }
2422 if (tp->t_const && allow_c90)
2423 /* %soperand of '%s' must be modifiable lvalue */
2424 warning(115, "", op_name(op));
2425 return true;
2426 }
2427
2428 static bool
2429 typeok_address(op_t op, const tnode_t *tn, const type_t *tp, tspec_t t)
2430 {
2431 if (t == ARRAY || t == FUNC) {
2432 /* ok, a warning comes later (in build_address()) */
2433 } else if (!tn->tn_lvalue) {
2434 if (tn->tn_op == CVT && tn->tn_cast &&
2435 tn->u.ops.left->tn_op == LOAD)
2436 /* a cast does not yield an lvalue */
2437 error(163);
2438 /* %soperand of '%s' must be lvalue */
2439 error(114, "", op_name(op));
2440 return false;
2441 } else if (is_scalar(t)) {
2442 if (tp->t_bitfield) {
2443 /* cannot take address of bit-field */
2444 error(112);
2445 return false;
2446 }
2447 } else if (t != STRUCT && t != UNION) {
2448 /* unacceptable operand of '%s' */
2449 error(111, op_name(op));
2450 return false;
2451 }
2452 if (tn->tn_op == NAME && tn->u.sym->s_register) {
2453 /* cannot take address of register '%s' */
2454 error(113, tn->u.sym->s_name);
2455 return false;
2456 }
2457 return true;
2458 }
2459
2460 static bool
2461 typeok_indir(const type_t *tp, tspec_t t)
2462 {
2463
2464 if (t != PTR) {
2465 /* cannot dereference non-pointer type '%s' */
2466 error(96, type_name(tp));
2467 return false;
2468 }
2469 return true;
2470 }
2471
2472 static void
2473 warn_incompatible_types(op_t op,
2474 const type_t *ltp, tspec_t lt,
2475 const type_t *rtp, tspec_t rt)
2476 {
2477 bool binary = modtab[op].m_binary;
2478
2479 if (lt == VOID || (binary && rt == VOID)) {
2480 /* void type invalid in expression */
2481 error(109);
2482 } else if (op == ASSIGN)
2483 /* cannot assign to '%s' from '%s' */
2484 error(171, type_name(ltp), type_name(rtp));
2485 else if (binary)
2486 /* operands of '%s' have incompatible types '%s' and '%s' */
2487 error(107, op_name(op), type_name(ltp), type_name(rtp));
2488 else {
2489 lint_assert(rt == NO_TSPEC);
2490 /* operand of '%s' has invalid type '%s' */
2491 error(108, op_name(op), type_name(ltp));
2492 }
2493 }
2494
2495 static bool
2496 typeok_plus(op_t op,
2497 const type_t *ltp, tspec_t lt,
2498 const type_t *rtp, tspec_t rt)
2499 {
2500 /* operands have scalar types (checked in typeok) */
2501 if ((lt == PTR && !is_integer(rt)) || (rt == PTR && !is_integer(lt))) {
2502 warn_incompatible_types(op, ltp, lt, rtp, rt);
2503 return false;
2504 }
2505 return true;
2506 }
2507
2508 static bool
2509 typeok_minus(op_t op,
2510 const type_t *ltp, tspec_t lt,
2511 const type_t *rtp, tspec_t rt)
2512 {
2513 /* operands have scalar types (checked in typeok) */
2514 if ((lt == PTR && rt != PTR && !is_integer(rt)) ||
2515 (lt != PTR && rt == PTR)) {
2516 warn_incompatible_types(op, ltp, lt, rtp, rt);
2517 return false;
2518 }
2519 if (lt == PTR && rt == PTR &&
2520 !types_compatible(ltp->t_subt, rtp->t_subt, true, false, NULL)) {
2521 /* invalid pointer subtraction */
2522 error(116);
2523 }
2524 return true;
2525 }
2526
2527 static void
2528 typeok_shr(op_t op,
2529 const tnode_t *ln, tspec_t lt,
2530 const tnode_t *rn, tspec_t rt)
2531 {
2532 tspec_t olt = before_conversion(ln)->tn_type->t_tspec;
2533 tspec_t ort = before_conversion(rn)->tn_type->t_tspec;
2534
2535 /* operands have integer types (checked in typeok) */
2536 if (pflag && !is_uinteger(olt)) {
2537 integer_constraints lc = ic_expr(ln);
2538 if (lc.bclr >> 63 != 0)
2539 return;
2540
2541 if (ln->tn_op != CON)
2542 /* bitwise '%s' on signed value possibly nonportable */
2543 warning(117, op_name(op));
2544 else if (ln->u.value.u.integer < 0)
2545 /* bitwise '%s' on signed value nonportable */
2546 warning(120, op_name(op));
2547 } else if (allow_trad && allow_c90 &&
2548 !is_uinteger(olt) && is_uinteger(ort)) {
2549 /* The left operand would become unsigned in traditional C. */
2550 if (hflag && (ln->tn_op != CON || ln->u.value.u.integer < 0)) {
2551 /* semantics of '%s' change in C90; use ... */
2552 warning(118, op_name(op));
2553 }
2554 } else if (allow_trad && allow_c90 &&
2555 !is_uinteger(olt) && !is_uinteger(ort) &&
2556 portable_rank_cmp(lt, rt) < 0) {
2557 /*
2558 * In traditional C, the left operand would be extended
2559 * (possibly sign-extended) and then shifted.
2560 */
2561 if (hflag && (ln->tn_op != CON || ln->u.value.u.integer < 0)) {
2562 /* semantics of '%s' change in C90; use ... */
2563 warning(118, op_name(op));
2564 }
2565 }
2566 }
2567
2568 static void
2569 typeok_shl(op_t op, tspec_t lt, tspec_t rt)
2570 {
2571 /*
2572 * C90 does not perform balancing for shift operations, but traditional
2573 * C does. If the width of the right operand is greater than the width
2574 * of the left operand, then in traditional C the left operand would be
2575 * extended to the width of the right operand. For SHL this may result
2576 * in different results.
2577 */
2578 if (portable_rank_cmp(lt, rt) < 0) {
2579 /*
2580 * XXX If both operands are constant, make sure that there is
2581 * really a difference between C90 and traditional C.
2582 */
2583 if (hflag && allow_trad && allow_c90)
2584 /* semantics of '%s' change in C90; use ... */
2585 warning(118, op_name(op));
2586 }
2587 }
2588
2589 static void
2590 typeok_shift(const type_t *ltp, tspec_t lt, const tnode_t *rn, tspec_t rt)
2591 {
2592 if (rn->tn_op != CON)
2593 return;
2594
2595 if (!is_uinteger(rt) && rn->u.value.u.integer < 0)
2596 /* negative shift */
2597 warning(121);
2598 else if ((uint64_t)rn->u.value.u.integer == size_in_bits(lt))
2599 /* shift amount %u equals bit-size of '%s' */
2600 warning(267, (unsigned)rn->u.value.u.integer, type_name(ltp));
2601 else if ((uint64_t)rn->u.value.u.integer > size_in_bits(lt)) {
2602 /* shift amount %llu is greater than bit-size %llu of '%s' */
2603 warning(122, (unsigned long long)rn->u.value.u.integer,
2604 (unsigned long long)size_in_bits(lt),
2605 tspec_name(lt));
2606 }
2607 }
2608
2609 static bool
2610 is_typeok_eq(const tnode_t *ln, tspec_t lt, const tnode_t *rn, tspec_t rt)
2611 {
2612 if (lt == PTR && is_null_pointer(rn))
2613 return true;
2614 if (rt == PTR && is_null_pointer(ln))
2615 return true;
2616 return false;
2617 }
2618
2619 static void
2620 warn_incompatible_pointers(op_t op, const type_t *ltp, const type_t *rtp)
2621 {
2622 lint_assert(ltp->t_tspec == PTR);
2623 lint_assert(rtp->t_tspec == PTR);
2624
2625 tspec_t lt = ltp->t_subt->t_tspec;
2626 tspec_t rt = rtp->t_subt->t_tspec;
2627
2628 if (is_struct_or_union(lt) && is_struct_or_union(rt)) {
2629 if (op == RETURN)
2630 /* invalid structure pointer combination */
2631 warning(244);
2632 else {
2633 /* incompatible structure pointers: '%s' '%s' '%s' */
2634 warning(245, type_name(ltp),
2635 op_name(op), type_name(rtp));
2636 }
2637 } else {
2638 if (op == RETURN)
2639 /* invalid combination of '%s' and '%s' */
2640 warning(184, type_name(ltp), type_name(rtp));
2641 else {
2642 /* invalid combination of '%s' and '%s', op '%s' */
2643 warning(124,
2644 type_name(ltp), type_name(rtp), op_name(op));
2645 }
2646 }
2647 }
2648
2649 static void
2650 check_pointer_comparison(op_t op, const tnode_t *ln, const tnode_t *rn)
2651 {
2652 type_t *ltp = ln->tn_type, *rtp = rn->tn_type;
2653 tspec_t lst = ltp->t_subt->t_tspec, rst = rtp->t_subt->t_tspec;
2654
2655 if (lst == VOID || rst == VOID) {
2656 /* TODO: C99 behaves like C90 here. */
2657 if (!allow_trad && !allow_c99 &&
2658 (lst == FUNC || rst == FUNC)) {
2659 /* (void *)0 is already handled in typeok() */
2660 const char *lsts, *rsts;
2661 *(lst == FUNC ? &lsts : &rsts) = "function pointer";
2662 *(lst == VOID ? &lsts : &rsts) = "'void *'";
2663 /* C90 or later forbid comparison of %s with %s */
2664 warning(274, lsts, rsts);
2665 }
2666 return;
2667 }
2668
2669 if (!types_compatible(ltp->t_subt, rtp->t_subt, true, false, NULL)) {
2670 warn_incompatible_pointers(op, ltp, rtp);
2671 return;
2672 }
2673
2674 if (lst == FUNC && rst == FUNC) {
2675 /* TODO: C99 behaves like C90 here, see C99 6.5.8p2. */
2676 if (!allow_trad && !allow_c99 && op != EQ && op != NE)
2677 /* pointers to functions can only be compared ... */
2678 warning(125);
2679 }
2680 }
2681
2682 static bool
2683 typeok_compare(op_t op,
2684 const tnode_t *ln, const type_t *ltp, tspec_t lt,
2685 const tnode_t *rn, const type_t *rtp, tspec_t rt)
2686 {
2687 if (lt == PTR && rt == PTR) {
2688 check_pointer_comparison(op, ln, rn);
2689 return true;
2690 }
2691
2692 if (lt != PTR && rt != PTR)
2693 return true;
2694
2695 if (!is_integer(lt) && !is_integer(rt)) {
2696 warn_incompatible_types(op, ltp, lt, rtp, rt);
2697 return false;
2698 }
2699
2700 const char *lx = lt == PTR ? "pointer" : "integer";
2701 const char *rx = rt == PTR ? "pointer" : "integer";
2702 /* invalid combination of %s '%s' and %s '%s', op '%s' */
2703 warning(123, lx, type_name(ltp), rx, type_name(rtp), op_name(op));
2704 return true;
2705 }
2706
2707 static bool
2708 typeok_quest(tspec_t lt, const tnode_t *rn)
2709 {
2710 if (!is_scalar(lt)) {
2711 /* first operand of '?' must have scalar type */
2712 error(170);
2713 return false;
2714 }
2715 lint_assert(before_conversion(rn)->tn_op == COLON);
2716 return true;
2717 }
2718
2719 static void
2720 typeok_colon_pointer(const type_t *ltp, const type_t *rtp)
2721 {
2722 type_t *lstp = ltp->t_subt;
2723 type_t *rstp = rtp->t_subt;
2724 tspec_t lst = lstp->t_tspec;
2725 tspec_t rst = rstp->t_tspec;
2726
2727 if ((lst == VOID && rst == FUNC) || (lst == FUNC && rst == VOID)) {
2728 /* (void *)0 is handled in typeok_colon */
2729 /* TODO: C99 behaves like C90 here. */
2730 if (!allow_trad && !allow_c99)
2731 /* conversion of %s to %s requires a cast, op %s */
2732 warning(305, "function pointer", "'void *'",
2733 op_name(COLON));
2734 return;
2735 }
2736
2737 if (pointer_types_are_compatible(lstp, rstp, true))
2738 return;
2739 if (!types_compatible(lstp, rstp, true, false, NULL))
2740 warn_incompatible_pointers(COLON, ltp, rtp);
2741 }
2742
2743 static bool
2744 typeok_colon(const tnode_t *ln, const type_t *ltp, tspec_t lt,
2745 const tnode_t *rn, const type_t *rtp, tspec_t rt)
2746 {
2747
2748 if (is_arithmetic(lt) && is_arithmetic(rt))
2749 return true;
2750 if (lt == BOOL && rt == BOOL)
2751 return true;
2752
2753 if (lt == STRUCT && rt == STRUCT && ltp->u.sou == rtp->u.sou)
2754 return true;
2755 if (lt == UNION && rt == UNION && ltp->u.sou == rtp->u.sou)
2756 return true;
2757
2758 if (lt == PTR && is_null_pointer(rn))
2759 return true;
2760 if (rt == PTR && is_null_pointer(ln))
2761 return true;
2762
2763 if ((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)) {
2764 const char *lx = lt == PTR ? "pointer" : "integer";
2765 const char *rx = rt == PTR ? "pointer" : "integer";
2766 /* invalid combination of %s '%s' and %s '%s', op '%s' */
2767 warning(123, lx, type_name(ltp),
2768 rx, type_name(rtp), op_name(COLON));
2769 return true;
2770 }
2771
2772 if (lt == VOID || rt == VOID) {
2773 if (lt != VOID || rt != VOID)
2774 /* incompatible types '%s' and '%s' in conditional */
2775 warning(126, type_name(ltp), type_name(rtp));
2776 return true;
2777 }
2778
2779 if (lt == PTR && rt == PTR) {
2780 typeok_colon_pointer(ltp, rtp);
2781 return true;
2782 }
2783
2784 /* incompatible types '%s' and '%s' in conditional */
2785 error(126, type_name(ltp), type_name(rtp));
2786 return false;
2787 }
2788
2789 static bool
2790 has_constant_member(const type_t *tp)
2791 {
2792 lint_assert(is_struct_or_union(tp->t_tspec));
2793
2794 for (sym_t *m = tp->u.sou->sou_first_member;
2795 m != NULL; m = m->s_next) {
2796 const type_t *mtp = m->s_type;
2797 if (mtp->t_const)
2798 return true;
2799 if (is_struct_or_union(mtp->t_tspec) &&
2800 has_constant_member(mtp))
2801 return true;
2802 }
2803 return false;
2804 }
2805
2806 static bool
2807 typeok_assign(op_t op, const tnode_t *ln, const type_t *ltp, tspec_t lt)
2808 {
2809 if (op == RETURN || op == INIT || op == FARG)
2810 return true;
2811
2812 if (!ln->tn_lvalue) {
2813 if (ln->tn_op == CVT && ln->tn_cast &&
2814 ln->u.ops.left->tn_op == LOAD)
2815 /* a cast does not yield an lvalue */
2816 error(163);
2817 /* %soperand of '%s' must be lvalue */
2818 error(114, "left ", op_name(op));
2819 return false;
2820 } else if (ltp->t_const
2821 || (is_struct_or_union(lt) && has_constant_member(ltp))) {
2822 if (allow_c90)
2823 /* %soperand of '%s' must be modifiable lvalue */
2824 warning(115, "left ", op_name(op));
2825 }
2826 return true;
2827 }
2828
2829 static bool
2830 typeok_scalar(op_t op, const mod_t *mp,
2831 const type_t *ltp, tspec_t lt,
2832 const type_t *rtp, tspec_t rt)
2833 {
2834 if (mp->m_takes_bool && lt == BOOL && rt == BOOL)
2835 return true;
2836 if (mp->m_requires_integer) {
2837 if (!is_integer(lt) || (mp->m_binary && !is_integer(rt))) {
2838 warn_incompatible_types(op, ltp, lt, rtp, rt);
2839 return false;
2840 }
2841 } else if (mp->m_requires_integer_or_complex) {
2842 if ((!is_integer(lt) && !is_complex(lt)) ||
2843 (mp->m_binary && (!is_integer(rt) && !is_complex(rt)))) {
2844 warn_incompatible_types(op, ltp, lt, rtp, rt);
2845 return false;
2846 }
2847 } else if (mp->m_requires_scalar) {
2848 if (!is_scalar(lt) || (mp->m_binary && !is_scalar(rt))) {
2849 warn_incompatible_types(op, ltp, lt, rtp, rt);
2850 return false;
2851 }
2852 } else if (mp->m_requires_arith) {
2853 if (!is_arithmetic(lt) ||
2854 (mp->m_binary && !is_arithmetic(rt))) {
2855 warn_incompatible_types(op, ltp, lt, rtp, rt);
2856 return false;
2857 }
2858 }
2859 return true;
2860 }
2861
2862 static void
2863 check_assign_void_pointer(op_t op, int arg,
2864 tspec_t lt, tspec_t lst,
2865 tspec_t rt, tspec_t rst)
2866 {
2867
2868 if (!(lt == PTR && rt == PTR && (lst == VOID || rst == VOID)))
2869 return;
2870 /* two pointers, at least one pointer to void */
2871
2872 /* TODO: C99 behaves like C90 here. */
2873 if (!(!allow_trad && !allow_c99 && (lst == FUNC || rst == FUNC)))
2874 return;
2875 /* comb. of ptr to func and ptr to void */
2876
2877 const char *lts, *rts;
2878 *(lst == FUNC ? <s : &rts) = "function pointer";
2879 *(lst == VOID ? <s : &rts) = "'void *'";
2880
2881 switch (op) {
2882 case INIT:
2883 case RETURN:
2884 /* conversion of %s to %s requires a cast */
2885 warning(303, rts, lts);
2886 break;
2887 case FARG:
2888 /* conversion of %s to %s requires a cast, arg #%d */
2889 warning(304, rts, lts, arg);
2890 break;
2891 default:
2892 /* conversion of %s to %s requires a cast, op %s */
2893 warning(305, rts, lts, op_name(op));
2894 break;
2895 }
2896 }
2897
2898 static bool
2899 is_direct_function_call(const tnode_t *tn, const char **out_name)
2900 {
2901
2902 if (tn->tn_op == CALL
2903 && tn->u.call->func->tn_op == ADDR
2904 && tn->u.call->func->u.ops.left->tn_op == NAME) {
2905 *out_name = tn->u.call->func->u.ops.left->u.sym->s_name;
2906 return true;
2907 }
2908 return false;
2909 }
2910
2911 static bool
2912 is_unconst_function(const char *name)
2913 {
2914
2915 return strcmp(name, "memchr") == 0 ||
2916 strcmp(name, "strchr") == 0 ||
2917 strcmp(name, "strpbrk") == 0 ||
2918 strcmp(name, "strrchr") == 0 ||
2919 strcmp(name, "strstr") == 0;
2920 }
2921
2922 static bool
2923 is_const_char_pointer(const tnode_t *tn)
2924 {
2925 /*
2926 * For traditional reasons, C99 6.4.5p5 defines that string literals
2927 * have type 'char[]'. They are often implicitly converted to 'char
2928 * *', for example when they are passed as function arguments.
2929 *
2930 * C99 6.4.5p6 further defines that modifying a string that is
2931 * constructed from a string literal invokes undefined behavior.
2932 *
2933 * Out of these reasons, string literals are treated as 'effectively
2934 * const' here.
2935 */
2936 if (tn->tn_op == CVT &&
2937 tn->u.ops.left->tn_op == ADDR &&
2938 tn->u.ops.left->u.ops.left->tn_op == STRING)
2939 return true;
2940
2941 const type_t *tp = before_conversion(tn)->tn_type;
2942 return tp->t_tspec == PTR &&
2943 tp->t_subt->t_tspec == CHAR &&
2944 tp->t_subt->t_const;
2945 }
2946
2947 static bool
2948 is_const_pointer(const tnode_t *tn)
2949 {
2950 const type_t *tp = before_conversion(tn)->tn_type;
2951 return tp->t_tspec == PTR && tp->t_subt->t_const;
2952 }
2953
2954 static void
2955 check_unconst_function(const type_t *lstp, const tnode_t *rn)
2956 {
2957 const char *function_name;
2958
2959 if (lstp->t_tspec == CHAR && !lstp->t_const &&
2960 is_direct_function_call(rn, &function_name) &&
2961 is_unconst_function(function_name) &&
2962 rn->u.call->args_len >= 1 &&
2963 is_const_char_pointer(rn->u.call->args[0])) {
2964 /* call to '%s' effectively discards 'const' from argument */
2965 warning(346, function_name);
2966 }
2967
2968 if (!lstp->t_const &&
2969 is_direct_function_call(rn, &function_name) &&
2970 strcmp(function_name, "bsearch") == 0 &&
2971 rn->u.call->args_len >= 2 &&
2972 is_const_pointer(rn->u.call->args[1])) {
2973 /* call to '%s' effectively discards 'const' from argument */
2974 warning(346, function_name);
2975 }
2976 }
2977
2978 static bool
2979 check_assign_void_pointer_compat(op_t op, int arg,
2980 tspec_t lt,
2981 const type_t *lstp, tspec_t lst,
2982 const tnode_t *rn,
2983 const type_t *rtp, tspec_t rt,
2984 const type_t *rstp, tspec_t rst)
2985 {
2986 if (!(lt == PTR && rt == PTR && (lst == VOID || rst == VOID ||
2987 types_compatible(lstp, rstp,
2988 true, false, NULL))))
2989 return false;
2990
2991 /* compatible pointer types (qualifiers ignored) */
2992 char qualifiers[32];
2993 snprintf(qualifiers, sizeof(qualifiers), "%s%s",
2994 !lstp->t_const && rstp->t_const ? " const" : "",
2995 !lstp->t_volatile && rstp->t_volatile ? " volatile" : "");
2996 if (allow_c90 && qualifiers[0] != '\0') {
2997 switch (op) {
2998 case INIT:
2999 case RETURN:
3000 /* '%s' discards '%s' from '%s' */
3001 warning(182, op_name(op),
3002 qualifiers + 1, type_name(rtp));
3003 break;
3004 case FARG:
3005 /* passing '%s' to argument %d discards '%s' */
3006 warning(383, type_name(rtp), arg, qualifiers + 1);
3007 break;
3008 default:
3009 /* operator '%s' discards '%s' from '%s' */
3010 warning(128, op_name(op),
3011 qualifiers + 1, type_name(rtp));
3012 break;
3013 }
3014 }
3015
3016 if (allow_c90)
3017 check_unconst_function(lstp, rn);
3018
3019 return true;
3020 }
3021
3022 static bool
3023 check_assign_pointer_integer(op_t op, int arg,
3024 const type_t *ltp, tspec_t lt,
3025 const type_t *rtp, tspec_t rt)
3026 {
3027
3028 if (!((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)))
3029 return false;
3030
3031 const char *lx = lt == PTR ? "pointer" : "integer";
3032 const char *rx = rt == PTR ? "pointer" : "integer";
3033
3034 switch (op) {
3035 case INIT:
3036 case RETURN:
3037 /* invalid combination of %s '%s' and %s '%s' for '%s' */
3038 warning(183,
3039 lx, type_name(ltp), rx, type_name(rtp), op_name(op));
3040 break;
3041 case FARG:
3042 /* invalid combination of %s '%s' and %s '%s', arg #%d */
3043 warning(154,
3044 lx, type_name(ltp), rx, type_name(rtp), arg);
3045 break;
3046 default:
3047 /* invalid combination of %s '%s' and %s '%s', op '%s' */
3048 warning(123,
3049 lx, type_name(ltp), rx, type_name(rtp), op_name(op));
3050 break;
3051 }
3052 return true;
3053 }
3054
3055 static bool
3056 check_assign_pointer(op_t op, int arg,
3057 const type_t *ltp, tspec_t lt,
3058 const type_t *rtp, tspec_t rt)
3059 {
3060 if (!(lt == PTR && rt == PTR))
3061 return false;
3062
3063 if (op == FARG)
3064 /* converting '%s' to incompatible '%s' for ... */
3065 warning(153, type_name(rtp), type_name(ltp), arg);
3066 else
3067 warn_incompatible_pointers(op, ltp, rtp);
3068 return true;
3069 }
3070
3071 static void
3072 warn_assign(op_t op, int arg,
3073 const type_t *ltp, tspec_t lt,
3074 const type_t *rtp, tspec_t rt)
3075 {
3076 switch (op) {
3077 case INIT:
3078 /* cannot initialize '%s' from '%s' */
3079 error(185, type_name(ltp), type_name(rtp));
3080 break;
3081 case RETURN:
3082 /* function has return type '%s' but returns '%s' */
3083 error(211, type_name(ltp), type_name(rtp));
3084 break;
3085 case FARG:
3086 /* passing '%s' to incompatible '%s', arg #%d */
3087 warning(155, type_name(rtp), type_name(ltp), arg);
3088 break;
3089 default:
3090 warn_incompatible_types(op, ltp, lt, rtp, rt);
3091 break;
3092 }
3093 }
3094
3095 /*
3096 * Checks type compatibility for ASSIGN, INIT, FARG and RETURN
3097 * and prints warnings/errors if necessary.
3098 * Returns whether the types are (almost) compatible.
3099 */
3100 static bool
3101 check_assign_types_compatible(op_t op, int arg,
3102 const tnode_t *ln, const tnode_t *rn)
3103 {
3104 tspec_t lt, rt, lst = NO_TSPEC, rst = NO_TSPEC;
3105 type_t *ltp, *rtp, *lstp = NULL, *rstp = NULL;
3106
3107 if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
3108 lst = (lstp = ltp->t_subt)->t_tspec;
3109 if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
3110 rst = (rstp = rtp->t_subt)->t_tspec;
3111
3112 if (lt == BOOL && is_scalar(rt)) /* C99 6.3.1.2 */
3113 return true;
3114
3115 if (is_arithmetic(lt) && (is_arithmetic(rt) || rt == BOOL))
3116 return true;
3117
3118 if (is_struct_or_union(lt) && is_struct_or_union(rt))
3119 return ltp->u.sou == rtp->u.sou;
3120
3121 if (lt == PTR && is_null_pointer(rn)) {
3122 if (is_integer(rn->tn_type->t_tspec))
3123 /* implicit conversion from integer 0 to pointer ... */
3124 query_message(15, type_name(ltp));
3125 return true;
3126 }
3127
3128 check_assign_void_pointer(op, arg, lt, lst, rt, rst);
3129
3130 if (check_assign_void_pointer_compat(op, arg,
3131 lt, lstp, lst, rn, rtp, rt, rstp, rst))
3132 return true;
3133
3134 if (check_assign_pointer_integer(op, arg, ltp, lt, rtp, rt))
3135 return true;
3136
3137 if (check_assign_pointer(op, arg, ltp, lt, rtp, rt))
3138 return true;
3139
3140 warn_assign(op, arg, ltp, lt, rtp, rt);
3141 return false;
3142 }
3143
3144 static bool
3145 has_side_effect(const tnode_t *tn) /* NOLINT(misc-no-recursion) */
3146 {
3147 op_t op = tn->tn_op;
3148
3149 if (modtab[op].m_has_side_effect)
3150 return true;
3151
3152 if (op == CVT && tn->tn_type->t_tspec == VOID)
3153 return has_side_effect(tn->u.ops.left);
3154
3155 /* XXX: Why not has_side_effect(tn->u.ops.left) as well? */
3156 if (op == LOGAND || op == LOGOR)
3157 return has_side_effect(tn->u.ops.right);
3158
3159 /* XXX: Why not has_side_effect(tn->u.ops.left) as well? */
3160 if (op == QUEST)
3161 return has_side_effect(tn->u.ops.right);
3162
3163 if (op == COLON || op == COMMA) {
3164 return has_side_effect(tn->u.ops.left) ||
3165 has_side_effect(tn->u.ops.right);
3166 }
3167
3168 return false;
3169 }
3170
3171 static bool
3172 is_void_cast(const tnode_t *tn)
3173 {
3174
3175 return tn->tn_op == CVT && tn->tn_cast &&
3176 tn->tn_type->t_tspec == VOID;
3177 }
3178
3179 static bool
3180 is_local_symbol(const tnode_t *tn)
3181 {
3182
3183 return tn->tn_op == LOAD &&
3184 tn->u.ops.left->tn_op == NAME &&
3185 tn->u.ops.left->u.sym->s_scl == AUTO;
3186 }
3187
3188 static bool
3189 is_int_constant_zero(const tnode_t *tn)
3190 {
3191
3192 return tn->tn_op == CON &&
3193 tn->tn_type->t_tspec == INT &&
3194 tn->u.value.u.integer == 0;
3195 }
3196
3197 static void
3198 check_null_effect(const tnode_t *tn)
3199 {
3200
3201 if (hflag &&
3202 !has_side_effect(tn) &&
3203 !(is_void_cast(tn) && is_local_symbol(tn->u.ops.left)) &&
3204 !(is_void_cast(tn) && is_int_constant_zero(tn->u.ops.left))) {
3205 /* expression has null effect */
3206 warning(129);
3207 }
3208 }
3209
3210 /*
3211 * Check the types for specific operators and type combinations.
3212 *
3213 * At this point, the operands already conform to the type requirements of
3214 * the operator, such as being integer, floating or scalar.
3215 */
3216 static bool
3217 typeok_op(op_t op, int arg,
3218 const tnode_t *ln, const type_t *ltp, tspec_t lt,
3219 const tnode_t *rn, const type_t *rtp, tspec_t rt)
3220 {
3221 switch (op) {
3222 case ARROW:
3223 return typeok_arrow(lt);
3224 case POINT:
3225 return typeok_point(ln, ltp, lt);
3226 case INCBEF:
3227 case DECBEF:
3228 case INCAFT:
3229 case DECAFT:
3230 return typeok_incdec(op, ln, ltp);
3231 case INDIR:
3232 return typeok_indir(ltp, lt);
3233 case ADDR:
3234 return typeok_address(op, ln, ltp, lt);
3235 case PLUS:
3236 return typeok_plus(op, ltp, lt, rtp, rt);
3237 case MINUS:
3238 return typeok_minus(op, ltp, lt, rtp, rt);
3239 case SHL:
3240 typeok_shl(op, lt, rt);
3241 goto shift;
3242 case SHR:
3243 typeok_shr(op, ln, lt, rn, rt);
3244 shift:
3245 typeok_shift(ltp, lt, rn, rt);
3246 break;
3247 case LT:
3248 case LE:
3249 case GT:
3250 case GE:
3251 compare:
3252 return typeok_compare(op, ln, ltp, lt, rn, rtp, rt);
3253 case EQ:
3254 case NE:
3255 if (is_typeok_eq(ln, lt, rn, rt))
3256 break;
3257 goto compare;
3258 case QUEST:
3259 return typeok_quest(lt, rn);
3260 case COLON:
3261 return typeok_colon(ln, ltp, lt, rn, rtp, rt);
3262 case ASSIGN:
3263 case INIT:
3264 case FARG:
3265 case RETURN:
3266 if (!check_assign_types_compatible(op, arg, ln, rn))
3267 return false;
3268 goto assign;
3269 case MULASS:
3270 case DIVASS:
3271 case MODASS:
3272 goto assign;
3273 case ADDASS:
3274 case SUBASS:
3275 if ((lt == PTR && !is_integer(rt)) || rt == PTR) {
3276 warn_incompatible_types(op, ltp, lt, rtp, rt);
3277 return false;
3278 }
3279 goto assign;
3280 case SHLASS:
3281 goto assign;
3282 case SHRASS:
3283 if (pflag && !is_uinteger(lt) &&
3284 !(!allow_c90 && is_uinteger(rt))) {
3285 /* bitwise '%s' on signed value possibly nonportable */
3286 warning(117, op_name(op));
3287 }
3288 goto assign;
3289 case ANDASS:
3290 case XORASS:
3291 case ORASS:
3292 assign:
3293 return typeok_assign(op, ln, ltp, lt);
3294 case COMMA:
3295 if (!modtab[ln->tn_op].m_has_side_effect)
3296 check_null_effect(ln);
3297 break;
3298 default:
3299 break;
3300 }
3301 return true;
3302 }
3303
3304 static void
3305 check_bad_enum_operation(op_t op, const tnode_t *ln, const tnode_t *rn)
3306 {
3307
3308 if (!eflag)
3309 return;
3310
3311 /* Allow enum in array indices. */
3312 if (op == PLUS &&
3313 ((ln->tn_type->t_is_enum && rn->tn_type->t_tspec == PTR) ||
3314 (rn->tn_type->t_is_enum && ln->tn_type->t_tspec == PTR))) {
3315 return;
3316 }
3317
3318 /* dubious operation '%s' on enum */
3319 warning(241, op_name(op));
3320 }
3321
3322 static void
3323 check_enum_type_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
3324 {
3325 const mod_t *mp = &modtab[op];
3326
3327 if (ln->tn_type->u.enumer != rn->tn_type->u.enumer) {
3328 switch (op) {
3329 case INIT:
3330 /* enum type mismatch between '%s' and '%s' in ... */
3331 warning(210,
3332 type_name(ln->tn_type), type_name(rn->tn_type));
3333 break;
3334 case FARG:
3335 /* function expects '%s', passing '%s' for arg #%d */
3336 warning(156,
3337 type_name(ln->tn_type), type_name(rn->tn_type),
3338 arg);
3339 break;
3340 case RETURN:
3341 /* function has return type '%s' but returns '%s' */
3342 warning(211,
3343 type_name(ln->tn_type), type_name(rn->tn_type));
3344 break;
3345 default:
3346 /* enum type mismatch: '%s' '%s' '%s' */
3347 warning(130, type_name(ln->tn_type), op_name(op),
3348 type_name(rn->tn_type));
3349 break;
3350 }
3351 } else if (Pflag && eflag && mp->m_comparison && op != EQ && op != NE)
3352 /* operator '%s' assumes that '%s' is ordered */
3353 warning(243, op_name(op), type_name(ln->tn_type));
3354 }
3355
3356 static void
3357 check_enum_int_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
3358 {
3359
3360 if (!eflag)
3361 return;
3362
3363 switch (op) {
3364 case INIT:
3365 /*
3366 * Initialization with 0 is allowed. Otherwise, all implicit
3367 * initializations would need to be warned upon as well.
3368 */
3369 if (!rn->tn_type->t_is_enum && rn->tn_op == CON &&
3370 is_integer(rn->tn_type->t_tspec) &&
3371 rn->u.value.u.integer == 0) {
3372 return;
3373 }
3374 /* initialization of '%s' with '%s' */
3375 warning(277, type_name(ln->tn_type), type_name(rn->tn_type));
3376 break;
3377 case FARG:
3378 /* combination of '%s' and '%s', arg #%d */
3379 warning(278,
3380 type_name(ln->tn_type), type_name(rn->tn_type), arg);
3381 break;
3382 case RETURN:
3383 /* combination of '%s' and '%s' in return */
3384 warning(279, type_name(ln->tn_type), type_name(rn->tn_type));
3385 break;
3386 default:
3387 /* combination of '%s' and '%s', op '%s' */
3388 warning(242, type_name(ln->tn_type), type_name(rn->tn_type),
3389 op_name(op));
3390 break;
3391 }
3392 }
3393
3394 static void
3395 typeok_enum(op_t op, const mod_t *mp, int arg,
3396 const tnode_t *ln, const type_t *ltp,
3397 const tnode_t *rn, const type_t *rtp)
3398 {
3399 if (mp->m_bad_on_enum &&
3400 (ltp->t_is_enum || (mp->m_binary && rtp->t_is_enum))) {
3401 check_bad_enum_operation(op, ln, rn);
3402 } else if (mp->m_valid_on_enum &&
3403 (ltp->t_is_enum && rtp != NULL && rtp->t_is_enum)) {
3404 check_enum_type_mismatch(op, arg, ln, rn);
3405 } else if (mp->m_valid_on_enum &&
3406 (ltp->t_is_enum || (rtp != NULL && rtp->t_is_enum))) {
3407 check_enum_int_mismatch(op, arg, ln, rn);
3408 }
3409 }
3410
3411 /* Perform most type checks. Return whether the types are ok. */
3412 bool
3413 typeok(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
3414 {
3415
3416 const mod_t *mp = &modtab[op];
3417
3418 type_t *ltp = ln->tn_type;
3419 tspec_t lt = ltp->t_tspec;
3420
3421 type_t *rtp = mp->m_binary ? rn->tn_type : NULL;
3422 tspec_t rt = mp->m_binary ? rtp->t_tspec : NO_TSPEC;
3423
3424 if (Tflag && !typeok_scalar_strict_bool(op, mp, arg, ln, rn))
3425 return false;
3426 if (!typeok_scalar(op, mp, ltp, lt, rtp, rt))
3427 return false;
3428
3429 if (!typeok_op(op, arg, ln, ltp, lt, rn, rtp, rt))
3430 return false;
3431
3432 typeok_enum(op, mp, arg, ln, ltp, rn, rtp);
3433 return true;
3434 }
3435
3436 /* In traditional C, keep unsigned and promote FLOAT to DOUBLE. */
3437 static tspec_t
3438 promote_trad(tspec_t t)
3439 {
3440
3441 if (t == UCHAR || t == USHORT)
3442 return UINT;
3443 if (t == CHAR || t == SCHAR || t == SHORT)
3444 return INT;
3445 if (t == FLOAT)
3446 return DOUBLE;
3447 if (t == ENUM)
3448 return INT;
3449 return t;
3450 }
3451
3452 /*
3453 * C99 6.3.1.1p2 requires for types with lower rank than int that "If an int
3454 * can represent all the values of the original type, the value is converted
3455 * to an int; otherwise it is converted to an unsigned int", and that "All
3456 * other types are unchanged by the integer promotions".
3457 */
3458 static tspec_t
3459 promote_c90(const tnode_t *tn, tspec_t t, bool farg)
3460 {
3461 if (tn->tn_type->t_bitfield) {
3462 unsigned int width = tn->tn_type->t_bit_field_width;
3463 unsigned int int_width = size_in_bits(INT);
3464 // XXX: What about _Bool bit-fields, since C99?
3465 if (width < int_width)
3466 return INT;
3467 if (width == int_width)
3468 return is_uinteger(t) ? UINT : INT;
3469 return t;
3470 }
3471
3472 if (t == CHAR || t == SCHAR)
3473 return INT;
3474 if (t == UCHAR)
3475 return size_in_bits(CHAR) < size_in_bits(INT) ? INT : UINT;
3476 if (t == SHORT)
3477 return INT;
3478 if (t == USHORT)
3479 return size_in_bits(SHORT) < size_in_bits(INT) ? INT : UINT;
3480 if (t == ENUM)
3481 return INT;
3482 if (farg && t == FLOAT)
3483 return DOUBLE;
3484 return t;
3485 }
3486
3487 /*
3488 * Performs the "integer promotions" (C99 6.3.1.1p2), which convert small
3489 * integer types to either int or unsigned int.
3490 *
3491 * If allow_c90 is unset or the operand is a function argument with no type
3492 * information (no prototype or variable # of args), converts float to double.
3493 */
3494 tnode_t *
3495 promote(op_t op, bool farg, tnode_t *tn)
3496 {
3497
3498 const type_t *otp = tn->tn_type;
3499 tspec_t ot = otp->t_tspec;
3500 if (!is_arithmetic(ot))
3501 return tn;
3502
3503 tspec_t nt = allow_c90 ? promote_c90(tn, ot, farg) : promote_trad(ot);
3504 if (nt == ot)
3505 return tn;
3506
3507 type_t *ntp = expr_dup_type(gettyp(nt));
3508 ntp->t_tspec = nt;
3509 ntp->t_is_enum = otp->t_is_enum;
3510 if (ntp->t_is_enum)
3511 ntp->u.enumer = otp->u.enumer;
3512 ntp->t_bitfield = otp->t_bitfield;
3513 if (ntp->t_bitfield) {
3514 ntp->t_bit_field_width = otp->t_bit_field_width;
3515 ntp->t_bit_field_offset = otp->t_bit_field_offset;
3516 }
3517 if (ntp->t_bitfield && is_uinteger(ot) && !is_uinteger(nt))
3518 ntp->t_bit_field_width++;
3519 return convert(op, 0, ntp, tn);
3520 }
3521
3522 static void
3523 check_lossy_floating_to_integer_conversion(
3524 op_t op, int arg, const type_t *tp, const tnode_t *tn)
3525 {
3526 long double x = tn->u.value.u.floating;
3527 integer_constraints ic = ic_any(tp);
3528 if (is_uinteger(tp->t_tspec)
3529 ? x >= ic.umin && x <= ic.umax && x == (uint64_t)x
3530 : x >= ic.smin && x <= ic.smax && x == (int64_t)x)
3531 return;
3532 if (op == FARG)
3533 /* lossy conversion of %Lg to '%s', arg #%d */
3534 warning(380, x, type_name(tp), arg);
3535 else
3536 /* lossy conversion of %Lg to '%s' */
3537 warning(381, x, type_name(tp));
3538 }
3539
3540 static void
3541 convert_integer_from_floating(
3542 op_t op, int arg, const type_t *tp, const tnode_t *tn)
3543 {
3544
3545 if (op == CVT)
3546 /* cast from floating point '%s' to integer '%s' */
3547 query_message(2, type_name(tn->tn_type), type_name(tp));
3548 else
3549 /* implicit conversion from floating point '%s' to ... */
3550 query_message(1, type_name(tn->tn_type), type_name(tp));
3551 if (tn->tn_op == CON)
3552 check_lossy_floating_to_integer_conversion(op, arg, tp, tn);
3553 }
3554
3555 static bool
3556 should_warn_about_prototype_conversion(tspec_t nt,
3557 tspec_t ot, const tnode_t *ptn)
3558 {
3559
3560 if (nt == ot)
3561 return false;
3562
3563 if (nt == ENUM && ot == INT)
3564 return false;
3565
3566 if (is_floating(nt) != is_floating(ot) ||
3567 portable_rank_cmp(nt, ot) != 0) {
3568 /* representation and/or width change */
3569 if (!is_integer(ot))
3570 return true;
3571 /*
3572 * XXX: Investigate whether this rule makes sense; see
3573 * tests/usr.bin/xlint/lint1/platform_long.c.
3574 */
3575 return portable_rank_cmp(ot, INT) > 0;
3576 }
3577
3578 if (!hflag)
3579 return false;
3580
3581 /*
3582 * If the types differ only in sign and the argument has the same
3583 * representation in both types, print no warning.
3584 */
3585 if (ptn->tn_op == CON && is_integer(nt) &&
3586 signed_type(nt) == signed_type(ot) &&
3587 !msb(ptn->u.value.u.integer, ot))
3588 return false;
3589
3590 return true;
3591 }
3592
3593 /*
3594 * Warn if a prototype causes a type conversion that is different from what
3595 * would happen to the same argument in the absence of a prototype. This
3596 * check is intended for code that needs to stay compatible with pre-C90 C.
3597 *
3598 * Errors/warnings about invalid type combinations are already printed
3599 * in check_assign_types_compatible().
3600 */
3601 static void
3602 check_prototype_conversion(int arg, tspec_t nt, tspec_t ot, type_t *tp,
3603 tnode_t *tn)
3604 {
3605
3606 if (!is_arithmetic(nt) || !is_arithmetic(ot))
3607 return;
3608
3609 /*
3610 * If the type of the formal parameter is char/short, a warning would
3611 * be useless, because functions declared the old style can't expect
3612 * char/short arguments.
3613 */
3614 if (nt == CHAR || nt == SCHAR || nt == UCHAR ||
3615 nt == SHORT || nt == USHORT)
3616 return;
3617
3618 tnode_t *ptn = promote(NOOP, true, tn);
3619 ot = ptn->tn_type->t_tspec;
3620
3621 if (should_warn_about_prototype_conversion(nt, ot, ptn)) {
3622 /* argument %d is converted from '%s' to '%s' ... */
3623 warning(259, arg, type_name(tn->tn_type), type_name(tp));
3624 }
3625 }
3626
3627 /*
3628 * When converting a large integer type to a small integer type, in some
3629 * cases the value of the actual expression is further restricted than the
3630 * type bounds, such as in (expr & 0xFF) or (expr % 100) or (expr >> 24).
3631 */
3632 static bool
3633 can_represent(const type_t *tp, const tnode_t *tn)
3634 {
3635 uint64_t nmask = value_bits(width_in_bits(tp));
3636 if (!is_uinteger(tp->t_tspec))
3637 nmask >>= 1;
3638
3639 integer_constraints c = ic_expr(tn);
3640 if ((~c.bclr & ~nmask) == 0)
3641 return true;
3642
3643 integer_constraints tpc = ic_any(tp);
3644 if (is_uinteger(tp->t_tspec)
3645 ? tpc.umin <= c.umin && tpc.umax >= c.umax
3646 : tpc.smin <= c.smin && tpc.smax >= c.smax)
3647 return true;
3648
3649 debug_enter();
3650 debug_step("type '%s' cannot represent:", type_name(tp));
3651 debug_node(tn);
3652 debug_leave();
3653 return false;
3654 }
3655
3656 static bool
3657 should_warn_about_integer_conversion(const type_t *ntp, tspec_t nt,
3658 const tnode_t *otn, tspec_t ot)
3659 {
3660
3661 // XXX: The portable_rank_cmp aims at portable mode, independent of the
3662 // current platform, while can_represent acts on the actual type sizes
3663 // from the current platform. This mix is inconsistent, but anything
3664 // else would make the exact conditions too complicated to grasp.
3665 if (aflag > 0 && portable_rank_cmp(nt, ot) < 0) {
3666 if (ot == LONG || ot == ULONG
3667 || ot == LLONG || ot == ULLONG
3668 #ifdef INT128_SIZE
3669 || ot == INT128 || ot == UINT128
3670 #endif
3671 || aflag > 1)
3672 return !can_represent(ntp, otn);
3673 }
3674 return false;
3675 }
3676
3677 static void
3678 convert_integer_from_integer(op_t op, int arg, tspec_t nt, tspec_t ot,
3679 type_t *tp, tnode_t *tn)
3680 {
3681
3682 if (tn->tn_op == CON)
3683 return;
3684
3685 if (op == CVT)
3686 return;
3687
3688 if (Pflag && pflag && aflag > 0 &&
3689 portable_rank_cmp(nt, ot) > 0 &&
3690 is_uinteger(nt) != is_uinteger(ot)) {
3691 if (op == FARG)
3692 /* conversion to '%s' may sign-extend ... */
3693 warning(297, type_name(tp), arg);
3694 else
3695 /* conversion to '%s' may sign-extend ... */
3696 warning(131, type_name(tp));
3697 }
3698
3699 if (Pflag && portable_rank_cmp(nt, ot) > 0 &&
3700 (tn->tn_op == PLUS || tn->tn_op == MINUS || tn->tn_op == MULT ||
3701 tn->tn_op == SHL)) {
3702 /* suggest cast from '%s' to '%s' on op '%s' to ... */
3703 warning(324, type_name(gettyp(ot)), type_name(tp),
3704 op_name(tn->tn_op));
3705 }
3706
3707 if (should_warn_about_integer_conversion(tp, nt, tn, ot)) {
3708 if (op == FARG) {
3709 /* conversion from '%s' to '%s' may lose ... */
3710 warning(298,
3711 type_name(tn->tn_type), type_name(tp), arg);
3712 } else {
3713 /* conversion from '%s' to '%s' may lose accuracy */
3714 warning(132,
3715 type_name(tn->tn_type), type_name(tp));
3716 }
3717 }
3718
3719 if (is_uinteger(nt) != is_uinteger(ot))
3720 /* implicit conversion changes sign from '%s' to '%s' */
3721 query_message(3, type_name(tn->tn_type), type_name(tp));
3722 }
3723
3724 static void
3725 convert_integer_from_pointer(op_t op, tspec_t nt, type_t *tp, tnode_t *tn)
3726 {
3727
3728 if (tn->tn_op == CON)
3729 return;
3730 if (op != CVT)
3731 return; /* We already got an error. */
3732 if (portable_rank_cmp(nt, PTR) >= 0)
3733 return;
3734
3735 if (pflag && size_in_bits(nt) >= size_in_bits(PTR)) {
3736 /* conversion of pointer to '%s' may lose bits */
3737 warning(134, type_name(tp));
3738 } else {
3739 /* conversion of pointer to '%s' loses bits */
3740 warning(133, type_name(tp));
3741 }
3742 }
3743
3744 static bool
3745 struct_starts_with(const type_t *struct_tp, const type_t *member_tp)
3746 {
3747
3748 return struct_tp->u.sou->sou_first_member != NULL &&
3749 types_compatible(struct_tp->u.sou->sou_first_member->s_type,
3750 member_tp, true, false, NULL);
3751 }
3752
3753 static bool
3754 is_byte_array(const type_t *tp)
3755 {
3756
3757 return tp->t_tspec == ARRAY &&
3758 (tp->t_subt->t_tspec == CHAR || tp->t_subt->t_tspec == UCHAR);
3759 }
3760
3761 static bool
3762 union_contains(const type_t *utp, const type_t *mtp)
3763 {
3764 for (const sym_t *mem = utp->u.sou->sou_first_member;
3765 mem != NULL; mem = mem->s_next) {
3766 if (types_compatible(mem->s_type, mtp, true, false, NULL))
3767 return true;
3768 }
3769 return false;
3770 }
3771
3772 static bool
3773 should_warn_about_pointer_cast(const type_t *nstp, tspec_t nst,
3774 const type_t *ostp, tspec_t ost)
3775 {
3776
3777 while (nst == ARRAY)
3778 nstp = nstp->t_subt, nst = nstp->t_tspec;
3779 while (ost == ARRAY)
3780 ostp = ostp->t_subt, ost = ostp->t_tspec;
3781
3782 if (nst == STRUCT && ost == STRUCT &&
3783 (struct_starts_with(nstp, ostp) ||
3784 struct_starts_with(ostp, nstp)))
3785 return false;
3786
3787 if (is_incomplete(nstp) || is_incomplete(ostp))
3788 return false;
3789
3790 if (nst == CHAR || nst == UCHAR)
3791 return false; /* for the sake of traditional C code */
3792 if (ost == CHAR || ost == UCHAR)
3793 return false; /* for the sake of traditional C code */
3794
3795 /* Allow cast between pointers to sockaddr variants. */
3796 if (nst == STRUCT && ost == STRUCT) {
3797 const sym_t *nmem = nstp->u.sou->sou_first_member;
3798 const sym_t *omem = ostp->u.sou->sou_first_member;
3799 while (nmem != NULL && omem != NULL &&
3800 types_compatible(nmem->s_type, omem->s_type,
3801 true, false, NULL))
3802 nmem = nmem->s_next, omem = omem->s_next;
3803 if (nmem != NULL && is_byte_array(nmem->s_type))
3804 return false;
3805 if (omem != NULL && is_byte_array(omem->s_type))
3806 return false;
3807 if (nmem == NULL && omem == NULL)
3808 return false;
3809 }
3810
3811 if (nst == UNION || ost == UNION) {
3812 const type_t *union_tp = nst == UNION ? nstp : ostp;
3813 const type_t *other_tp = nst == UNION ? ostp : nstp;
3814 if (union_contains(union_tp, other_tp))
3815 return false;
3816 }
3817
3818 if (is_struct_or_union(nst) && is_struct_or_union(ost))
3819 return nstp->u.sou != ostp->u.sou;
3820
3821 enum rank_kind rk1 = type_properties(nst)->tt_rank_kind;
3822 enum rank_kind rk2 = type_properties(ost)->tt_rank_kind;
3823 if (rk1 != rk2 || rk1 == RK_NONE)
3824 return true;
3825
3826 return portable_rank_cmp(nst, ost) != 0;
3827 }
3828
3829 static void
3830 convert_pointer_from_pointer(type_t *ntp, tnode_t *tn)
3831 {
3832 const type_t *nstp = ntp->t_subt;
3833 const type_t *otp = tn->tn_type;
3834 const type_t *ostp = otp->t_subt;
3835 tspec_t nst = nstp->t_tspec;
3836 tspec_t ost = ostp->t_tspec;
3837
3838 if (nst == VOID || ost == VOID) {
3839 /* TODO: C99 behaves like C90 here. */
3840 if (!allow_trad && !allow_c99 && (nst == FUNC || ost == FUNC)) {
3841 const char *nts, *ots;
3842 /* null pointers are already handled in convert() */
3843 *(nst == FUNC ? &nts : &ots) = "function pointer";
3844 *(nst == VOID ? &nts : &ots) = "'void *'";
3845 /* conversion of %s to %s requires a cast */
3846 warning(303, ots, nts);
3847 }
3848 return;
3849 }
3850 if (nst == FUNC && ost == FUNC)
3851 return;
3852 if (nst == FUNC || ost == FUNC) {
3853 /* converting '%s' to '%s' is questionable */
3854 warning(229, type_name(otp), type_name(ntp));
3855 return;
3856 }
3857
3858 if (hflag && alignment(nstp) > alignment(ostp) &&
3859 !is_incomplete(ostp) && alignment(ostp) > 1 &&
3860 !(nst == UNION && union_contains(nstp, ostp))) {
3861 /* converting '%s' to '%s' increases alignment ... */
3862 warning(135, type_name(otp), type_name(ntp),
3863 alignment(ostp), alignment(nstp));
3864 }
3865
3866 if (cflag && should_warn_about_pointer_cast(nstp, nst, ostp, ost)) {
3867 /* pointer cast from '%s' to unrelated '%s' */
3868 warning(247, type_name(ostp), type_name(nstp));
3869 }
3870 }
3871
3872 /*
3873 * Insert a conversion operator, which converts the type of the node
3874 * to another given type.
3875 *
3876 * Possible values for 'op':
3877 * CVT a cast-expression
3878 * binary integer promotion for one of the operands, or a usual
3879 * arithmetic conversion
3880 * binary plain or compound assignments to bit-fields
3881 * FARG 'arg' is the number of the parameter (used for warnings)
3882 * NOOP several other implicit conversions
3883 * ...
3884 */
3885 tnode_t *
3886 convert(op_t op, int arg, type_t *tp, tnode_t *tn)
3887 {
3888 tspec_t nt = tp->t_tspec;
3889 tspec_t ot = tn->tn_type->t_tspec;
3890
3891 if (allow_trad && allow_c90 && op == FARG)
3892 check_prototype_conversion(arg, nt, ot, tp, tn);
3893
3894 if (nt == BOOL) {
3895 /* No further checks. */
3896
3897 } else if (is_integer(nt)) {
3898 if (ot == BOOL) {
3899 /* No further checks. */
3900 } else if (is_integer(ot))
3901 convert_integer_from_integer(op, arg, nt, ot, tp, tn);
3902 else if (is_floating(ot))
3903 convert_integer_from_floating(op, arg, tp, tn);
3904 else if (ot == PTR)
3905 convert_integer_from_pointer(op, nt, tp, tn);
3906
3907 } else if (is_floating(nt)) {
3908 if (is_integer(ot) && op != CVT) {
3909 /* implicit conversion from integer '%s' to ... */
3910 query_message(19,
3911 type_name(tn->tn_type), type_name(tp));
3912 }
3913
3914 } else if (nt == PTR) {
3915 if (is_null_pointer(tn)) {
3916 /* a null pointer may be assigned to any pointer. */
3917 } else if (ot == PTR && op == CVT)
3918 convert_pointer_from_pointer(tp, tn);
3919 }
3920
3921 tnode_t *ntn = expr_alloc_tnode();
3922 ntn->tn_op = CVT;
3923 ntn->tn_type = tp;
3924 ntn->tn_cast = op == CVT;
3925 ntn->tn_sys |= tn->tn_sys;
3926 ntn->u.ops.right = NULL;
3927 if (tn->tn_op != CON || nt == VOID) {
3928 ntn->u.ops.left = tn;
3929 } else {
3930 ntn->tn_op = CON;
3931 convert_constant(op, arg, ntn->tn_type, &ntn->u.value,
3932 &tn->u.value);
3933 }
3934
3935 return ntn;
3936 }
3937
3938 static void
3939 convert_constant_from_floating(op_t op, int arg, const type_t *ntp,
3940 tspec_t nt, val_t *nv, val_t *ov)
3941 {
3942 long double max = 0.0, min = 0.0;
3943
3944 switch (nt) {
3945 case CHAR:
3946 max = TARG_CHAR_MAX; min = TARG_CHAR_MIN; break;
3947 case UCHAR:
3948 max = TARG_UCHAR_MAX; min = 0; break;
3949 case SCHAR:
3950 max = TARG_SCHAR_MAX; min = TARG_SCHAR_MIN; break;
3951 case SHORT:
3952 max = TARG_SHRT_MAX; min = TARG_SHRT_MIN; break;
3953 case USHORT:
3954 max = TARG_USHRT_MAX; min = 0; break;
3955 case ENUM:
3956 case INT:
3957 max = TARG_INT_MAX; min = TARG_INT_MIN; break;
3958 case UINT:
3959 max = TARG_UINT_MAX; min = 0; break;
3960 case LONG:
3961 max = TARG_LONG_MAX; min = TARG_LONG_MIN; break;
3962 case ULONG:
3963 max = TARG_ULONG_MAX; min = 0; break;
3964 case LLONG:
3965 max = LLONG_MAX; min = LLONG_MIN; break;
3966 case ULLONG:
3967 max = ULLONG_MAX; min = 0; break;
3968 case FLOAT:
3969 case FCOMPLEX:
3970 max = FLT_MAX; min = -FLT_MAX; break;
3971 case DOUBLE:
3972 case DCOMPLEX:
3973 max = DBL_MAX; min = -DBL_MAX; break;
3974 case LDOUBLE:
3975 case LCOMPLEX:
3976 /* LINTED 248; see floating_error_value. */
3977 max = LDBL_MAX; min = -max; break;
3978 default:
3979 lint_assert(false);
3980 }
3981 if (ov->u.floating > max || ov->u.floating < min) {
3982 lint_assert(nt != LDOUBLE);
3983 const char *ot_name = type_name(gettyp(ov->v_tspec));
3984 const char *nt_name = type_name(ntp);
3985 if (is_integer(nt))
3986 goto after_warning;
3987 if (op == FARG)
3988 /* conversion of '%s' to '%s' is out of range, ... */
3989 warning(295, ot_name, nt_name, arg);
3990 else
3991 /* conversion of '%s' to '%s' is out of range */
3992 warning(119, ot_name, nt_name);
3993 after_warning:
3994 ov->u.floating = ov->u.floating > 0 ? max : min;
3995 }
3996
3997 if (nt == FLOAT || nt == FCOMPLEX)
3998 nv->u.floating = (float)ov->u.floating;
3999 else if (nt == DOUBLE || nt == DCOMPLEX)
4000 nv->u.floating = (double)ov->u.floating;
4001 else if (nt == LDOUBLE || nt == LCOMPLEX)
4002 nv->u.floating = ov->u.floating;
4003 else
4004 nv->u.integer = (int64_t)ov->u.floating;
4005 }
4006
4007 static bool
4008 convert_constant_to_floating(tspec_t nt, val_t *nv,
4009 tspec_t ot, const val_t *v)
4010 {
4011 if (nt == FLOAT) {
4012 nv->u.floating = (ot == PTR || is_uinteger(ot)) ?
4013 (float)(uint64_t)v->u.integer : (float)v->u.integer;
4014 } else if (nt == DOUBLE) {
4015 nv->u.floating = (ot == PTR || is_uinteger(ot)) ?
4016 (double)(uint64_t)v->u.integer : (double)v->u.integer;
4017 } else if (nt == LDOUBLE) {
4018 nv->u.floating = (ot == PTR || is_uinteger(ot))
4019 ? (long double)(uint64_t)v->u.integer
4020 : (long double)v->u.integer;
4021 } else
4022 return false;
4023 return true;
4024 }
4025
4026 static void
4027 warn_constant_truncated(op_t op, const val_t *v)
4028 {
4029 char buf[256];
4030 bool is_unsigned = is_uinteger(v->v_tspec);
4031 int64_t val = v->u.integer;
4032 unsigned long long abs_val = is_unsigned || val >= 0
4033 ? (unsigned long long)val
4034 : -(unsigned long long)val;
4035 const char *sign = is_unsigned || val >= 0 ? "" : "-";
4036 snprintf(buf, sizeof(buf), "%s%#llx", sign, abs_val);
4037 /* constant %s truncated by conversion, op '%s' */
4038 warning(306, buf, op_name(op));
4039 }
4040
4041 static void
4042 convert_constant_check_range_bitor(size_t nsz, size_t osz, const val_t *v,
4043 uint64_t xmask, op_t op)
4044 {
4045 if (nsz < osz && (v->u.integer & xmask) != 0)
4046 warn_constant_truncated(op, v);
4047 }
4048
4049 static void
4050 convert_constant_check_range_bitand(size_t nsz, size_t osz,
4051 uint64_t xmask, const val_t *nv,
4052 tspec_t ot, const val_t *v,
4053 const type_t *tp, op_t op)
4054 {
4055 if (nsz > osz &&
4056 (nv->u.integer & bit((unsigned int)(osz - 1))) != 0 &&
4057 (nv->u.integer & xmask) != xmask) {
4058 /* extra bits set to 0 in conversion of '%s' to '%s', ... */
4059 warning(309, type_name(gettyp(ot)),
4060 type_name(tp), op_name(op));
4061 } else if (nsz < osz &&
4062 (v->u.integer & xmask) != xmask &&
4063 (v->u.integer & xmask) != 0)
4064 warn_constant_truncated(op, v);
4065 }
4066
4067 static void
4068 convert_constant_check_range_signed(op_t op, int arg,
4069 const type_t *ntp, int64_t ov)
4070 {
4071 if (op == ASSIGN)
4072 /* assignment of negative constant %lld to unsigned ... */
4073 warning(164, (long long)ov, type_name(ntp));
4074 else if (op == INIT)
4075 /* initialization of unsigned type '%s' with negative ... */
4076 warning(221, type_name(ntp), (long long)ov);
4077 else if (op == FARG)
4078 /* conversion of negative constant %lld to unsigned ... */
4079 warning(296, (long long)ov, type_name(ntp), arg);
4080 else if (modtab[op].m_comparison) {
4081 /* handled by check_integer_comparison() */
4082 } else
4083 /* conversion of negative constant %lld to unsigned ... */
4084 warning(222, (long long)ov, type_name(ntp));
4085 }
4086
4087 /*
4088 * Loss of significant bit(s). All truncated bits of unsigned types or all
4089 * truncated bits plus the msb of the target for signed types are considered
4090 * to be significant bits. Loss of significant bits means that at least one
4091 * of the bits was set in an unsigned type or that at least one but not all
4092 * of the bits was set in a signed type. Loss of significant bits means that
4093 * it is not possible, also not with necessary casts, to convert back to the
4094 * original type. An example for a necessary cast is:
4095 * char c; int i; c = 128;
4096 * i = c; ** yields -128 **
4097 * i = (unsigned char)c; ** yields 128 **
4098 */
4099 static void
4100 warn_constant_check_range_truncated(op_t op, int arg, const type_t *tp,
4101 tspec_t ot)
4102 {
4103 if (op == ASSIGN && tp->t_bitfield)
4104 /* precision lost in bit-field assignment */
4105 warning(166);
4106 else if (op == ASSIGN)
4107 /* constant truncated by assignment */
4108 warning(165);
4109 else if (op == INIT && tp->t_bitfield)
4110 /* bit-field initializer does not fit */
4111 warning(180);
4112 else if (op == INIT)
4113 /* initializer does not fit */
4114 warning(178);
4115 else if (op == CASE)
4116 /* case label is converted from '%s' to '%s' */
4117 warning(196, tspec_name(ot), type_name(tp));
4118 else if (op == FARG)
4119 /* conversion of '%s' to '%s' is out of range, arg #%d */
4120 warning(295, type_name(gettyp(ot)), type_name(tp), arg);
4121 else
4122 /* conversion of '%s' to '%s' is out of range */
4123 warning(119, type_name(gettyp(ot)), type_name(tp));
4124 }
4125
4126 static void
4127 warn_constant_check_range_loss(op_t op, int arg, const type_t *tp,
4128 tspec_t ot)
4129 {
4130 if (op == ASSIGN && tp->t_bitfield)
4131 /* precision lost in bit-field assignment */
4132 warning(166);
4133 else if (op == INIT && tp->t_bitfield)
4134 /* bit-field initializer out of range */
4135 warning(11);
4136 else if (op == CASE)
4137 /* case label is converted from '%s' to '%s' */
4138 warning(196, tspec_name(ot), type_name(tp));
4139 else if (op == FARG)
4140 /* conversion of '%s' to '%s' is out of range, arg #%d */
4141 warning(295, type_name(gettyp(ot)), type_name(tp), arg);
4142 else
4143 /* conversion of '%s' to '%s' is out of range */
4144 warning(119, type_name(gettyp(ot)), type_name(tp));
4145 }
4146
4147 static void
4148 convert_constant_check_range(tspec_t ot, const type_t *tp, tspec_t nt,
4149 op_t op, int arg, const val_t *v, val_t *nv)
4150 {
4151 unsigned int obitsz, nbitsz;
4152 uint64_t xmask, xmsk1;
4153
4154 obitsz = size_in_bits(ot);
4155 nbitsz = tp->t_bitfield ? tp->t_bit_field_width : size_in_bits(nt);
4156 xmask = value_bits(nbitsz) ^ value_bits(obitsz);
4157 xmsk1 = value_bits(nbitsz) ^ value_bits(obitsz - 1);
4158 if (op == ORASS || op == BITOR || op == BITXOR) {
4159 convert_constant_check_range_bitor(
4160 nbitsz, obitsz, v, xmask, op);
4161 } else if (op == ANDASS || op == BITAND) {
4162 convert_constant_check_range_bitand(
4163 nbitsz, obitsz, xmask, nv, ot, v, tp, op);
4164 } else if (nt != PTR && is_uinteger(nt) &&
4165 ot != PTR && !is_uinteger(ot) &&
4166 v->u.integer < 0)
4167 convert_constant_check_range_signed(op, arg,
4168 tp, v->u.integer);
4169 else if (nv->u.integer != v->u.integer && nbitsz <= obitsz &&
4170 (v->u.integer & xmask) != 0 &&
4171 (is_uinteger(ot) || (v->u.integer & xmsk1) != xmsk1))
4172 warn_constant_check_range_truncated(op, arg, tp, ot);
4173 else if (nv->u.integer != v->u.integer)
4174 warn_constant_check_range_loss(op, arg, tp, ot);
4175 }
4176
4177 /* Converts a typed constant to a constant of another type. */
4178 void
4179 convert_constant(op_t op, int arg, const type_t *ntp, val_t *nv, val_t *ov)
4180 {
4181 /*
4182 * TODO: make 'ov' const; the name of this function does not suggest
4183 * that it modifies 'ov'.
4184 */
4185 tspec_t ot = ov->v_tspec;
4186 tspec_t nt = nv->v_tspec = ntp->t_tspec;
4187 bool range_check = false;
4188
4189 if (nt == BOOL) { /* C99 6.3.1.2 */
4190 nv->v_unsigned_since_c90 = false;
4191 nv->u.integer = is_nonzero_val(ov) ? 1 : 0;
4192 return;
4193 }
4194
4195 if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE)
4196 convert_constant_from_floating(op, arg, ntp, nt, nv, ov);
4197 else if (!convert_constant_to_floating(nt, nv, ot, ov)) {
4198 range_check = true; /* Check for lost precision. */
4199 nv->u.integer = ov->u.integer;
4200 }
4201
4202 if (allow_trad && allow_c90 && ov->v_unsigned_since_c90 &&
4203 (is_floating(nt) || (
4204 (is_integer(nt) && !is_uinteger(nt) &&
4205 portable_rank_cmp(nt, ot) > 0)))) {
4206 /* C90 treats constant as unsigned */
4207 warning(157);
4208 ov->v_unsigned_since_c90 = false;
4209 }
4210
4211 if (is_integer(nt)) {
4212 unsigned int size = ntp->t_bitfield
4213 ? ntp->t_bit_field_width : size_in_bits(nt);
4214 nv->u.integer = convert_integer(nv->u.integer, nt, size);
4215 }
4216
4217 if (range_check && op != CVT)
4218 convert_constant_check_range(ot, ntp, nt, op, arg, ov, nv);
4219 }
4220
4221 tnode_t *
4222 build_sizeof(const type_t *tp)
4223 {
4224 unsigned int size_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
4225 tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, size_in_bytes);
4226 tn->tn_system_dependent = true;
4227 debug_step("build_sizeof '%s' = %u", type_name(tp), size_in_bytes);
4228 return tn;
4229 }
4230
4231 tnode_t *
4232 build_offsetof(const type_t *tp, designation dn)
4233 {
4234 unsigned int offset_in_bits = 0;
4235
4236 if (!is_struct_or_union(tp->t_tspec)) {
4237 /* unacceptable operand of '%s' */
4238 error(111, "offsetof");
4239 goto proceed;
4240 }
4241 for (size_t i = 0; i < dn.dn_len; i++) {
4242 const designator *dr = dn.dn_items + i;
4243 if (dr->dr_kind == DK_SUBSCRIPT) {
4244 if (tp->t_tspec != ARRAY)
4245 goto proceed; /* silent error */
4246 tp = tp->t_subt;
4247 offset_in_bits += (unsigned)dr->dr_subscript
4248 * type_size_in_bits(tp);
4249 } else {
4250 if (!is_struct_or_union(tp->t_tspec))
4251 goto proceed; /* silent error */
4252 const char *name = dr->dr_member->s_name;
4253 sym_t *mem = find_member(tp->u.sou, name);
4254 if (mem == NULL) {
4255 /* type '%s' does not have member '%s' */
4256 error(101, name, type_name(tp));
4257 goto proceed;
4258 }
4259 tp = mem->s_type;
4260 offset_in_bits += mem->u.s_member.sm_offset_in_bits;
4261 }
4262 }
4263 free(dn.dn_items);
4264
4265 proceed:;
4266 unsigned int offset_in_bytes = offset_in_bits / CHAR_SIZE;
4267 tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, offset_in_bytes);
4268 tn->tn_system_dependent = true;
4269 return tn;
4270 }
4271
4272 unsigned int
4273 type_size_in_bits(const type_t *tp)
4274 {
4275
4276 unsigned int elem = 1;
4277 bool flex = false;
4278 lint_assert(tp != NULL);
4279 while (tp->t_tspec == ARRAY) {
4280 flex = true; /* allow c99 flex arrays [] [0] */
4281 elem *= tp->u.dimension;
4282 tp = tp->t_subt;
4283 }
4284 if (elem == 0 && !flex) {
4285 /* cannot take size/alignment of incomplete type */
4286 error(143);
4287 elem = 1;
4288 }
4289
4290 unsigned int elsz;
4291 switch (tp->t_tspec) {
4292 case VOID:
4293 /* cannot take size/alignment of void */
4294 error(146);
4295 elsz = 1;
4296 break;
4297 case FUNC:
4298 /* cannot take size/alignment of function type '%s' */
4299 error(144, type_name(tp));
4300 elsz = 1;
4301 break;
4302 case STRUCT:
4303 case UNION:
4304 if (is_incomplete(tp)) {
4305 /* cannot take size/alignment of incomplete type */
4306 error(143);
4307 elsz = 1;
4308 } else
4309 elsz = tp->u.sou->sou_size_in_bits;
4310 break;
4311 case ENUM:
4312 if (is_incomplete(tp)) {
4313 /* cannot take size/alignment of incomplete type */
4314 warning(143);
4315 }
4316 /* FALLTHROUGH */
4317 default:
4318 if (tp->t_bitfield)
4319 /* cannot take size/alignment of bit-field */
4320 error(145);
4321 elsz = size_in_bits(tp->t_tspec);
4322 lint_assert(elsz > 0);
4323 break;
4324 }
4325
4326 return elem * elsz;
4327 }
4328
4329 /* C11 6.5.3.4, GCC */
4330 tnode_t *
4331 build_alignof(const type_t *tp)
4332 {
4333 if (tp->t_tspec == FUNC) {
4334 /* cannot take size/alignment of function type '%s' */
4335 error(144, type_name(tp));
4336 return NULL;
4337 }
4338 if (tp->t_tspec == VOID) {
4339 /* cannot take size/alignment of void */
4340 error(146);
4341 return NULL;
4342 }
4343 if (is_incomplete(tp)) {
4344 /* cannot take size/alignment of incomplete type */
4345 error(143);
4346 return NULL;
4347 }
4348 if (tp->t_bitfield) {
4349 /* cannot take size/alignment of bit-field */
4350 error(145);
4351 return NULL;
4352 }
4353 return build_integer_constant(SIZEOF_TSPEC, (int64_t)alignment(tp));
4354 }
4355
4356 static tnode_t *
4357 cast_to_union(tnode_t *otn, bool sys, type_t *ntp)
4358 {
4359
4360 if (!allow_gcc) {
4361 /* union cast is a GCC extension */
4362 error(328);
4363 return NULL;
4364 }
4365
4366 for (const sym_t *m = ntp->u.sou->sou_first_member;
4367 m != NULL; m = m->s_next) {
4368 if (types_compatible(m->s_type, otn->tn_type,
4369 false, false, NULL)) {
4370 tnode_t *ntn = build_op(CVT, sys, ntp, otn, NULL);
4371 ntn->tn_cast = true;
4372 return ntn;
4373 }
4374 }
4375
4376 /* type '%s' is not a member of '%s' */
4377 error(329, type_name(otn->tn_type), type_name(ntp));
4378 return NULL;
4379 }
4380
4381 // In GCC mode, allow 'nullptr + offset' as a constant expression.
4382 static tnode_t *
4383 null_pointer_offset(tnode_t *tn)
4384 {
4385 uint64_t off = 0;
4386 const tnode_t *n = tn;
4387 while ((n->tn_op == PLUS || n->tn_op == MINUS)
4388 && is_integer(n->u.ops.right->tn_type->t_tspec)) {
4389 off += (uint64_t)n->u.ops.right->u.value.u.integer;
4390 n = n->u.ops.left;
4391 }
4392 if (n->tn_type->t_tspec == PTR
4393 && n->tn_op == ADDR
4394 && n->u.ops.left->tn_op == INDIR
4395 && n->u.ops.left->u.ops.left->tn_op == CON
4396 && n->u.ops.left->u.ops.left->tn_type->t_tspec == PTR) {
4397 off += (uint64_t)n->u.ops.left->u.ops.left->u.value.u.integer;
4398 return build_integer_constant(SIZEOF_TSPEC, (int64_t)off);
4399 }
4400 return tn;
4401 }
4402
4403 tnode_t *
4404 cast(tnode_t *tn, bool sys, type_t *tp)
4405 {
4406
4407 if (tn == NULL)
4408 return NULL;
4409
4410 tn = cconv(tn);
4411
4412 lint_assert(tp != NULL);
4413 tspec_t nt = tp->t_tspec;
4414 tspec_t ot = tn->tn_type->t_tspec;
4415
4416 if (nt == VOID) {
4417 /*
4418 * C90 6.3.4, C99 6.5.4p2 and C11 6.5.4p2 allow any type to be
4419 * cast to void. The only other allowed casts are from a
4420 * scalar type to a scalar type.
4421 */
4422 } else if (nt == UNION)
4423 return cast_to_union(tn, sys, tp);
4424 else if (nt == STRUCT || nt == ARRAY || nt == FUNC) {
4425 /* Casting to a struct is an undocumented GCC extension. */
4426 if (!(allow_gcc && nt == STRUCT))
4427 goto invalid_cast;
4428 } else if (is_struct_or_union(ot))
4429 goto invalid_cast;
4430 else if (ot == VOID) {
4431 /* improper cast of void expression */
4432 error(148);
4433 return NULL;
4434 } else if (is_integer(nt) && is_scalar(ot)) {
4435 tn = null_pointer_offset(tn);
4436 } else if (is_floating(nt) && is_arithmetic(ot)) {
4437 /* ok */
4438 } else if (nt == PTR && is_integer(ot)) {
4439 /* ok */
4440 } else if (nt == PTR && ot == PTR) {
4441 if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
4442 if (hflag)
4443 /* cast discards 'const' from type '%s' */
4444 warning(275, type_name(tn->tn_type));
4445 }
4446 } else
4447 goto invalid_cast;
4448
4449 if (any_query_enabled
4450 && types_compatible(tp, tn->tn_type, false, false, NULL))
4451 /* no-op cast from '%s' to '%s' */
4452 query_message(6, type_name(tn->tn_type), type_name(tp));
4453
4454 tn = convert(CVT, 0, tp, tn);
4455 tn->tn_cast = true;
4456 tn->tn_sys = sys;
4457
4458 return tn;
4459
4460 invalid_cast:
4461 /* invalid cast from '%s' to '%s' */
4462 error(147, type_name(tn->tn_type), type_name(tp));
4463 return NULL;
4464 }
4465
4466 void
4467 add_function_argument(function_call *call, tnode_t *arg)
4468 {
4469 /*
4470 * If there was a serious error in the expression for the argument,
4471 * create a dummy argument so the positions of the remaining arguments
4472 * will not change.
4473 */
4474 if (arg == NULL)
4475 arg = build_integer_constant(INT, 0);
4476
4477 if (call->args_len >= call->args_cap) {
4478 call->args_cap += 8;
4479 tnode_t **new_args = expr_zero_alloc(
4480 call->args_cap * sizeof(*call->args), "tnode*[]");
4481 if (call->args_len > 0)
4482 memcpy(new_args, call->args,
4483 call->args_len * sizeof(*call->args));
4484 call->args = new_args;
4485 }
4486 call->args[call->args_len++] = arg;
4487 }
4488
4489 /*
4490 * Compare the type of an argument with the corresponding type of a
4491 * prototype parameter. If it is a valid combination, but both types
4492 * are not the same, insert a conversion to convert the argument into
4493 * the type of the parameter.
4494 */
4495 static tnode_t *
4496 check_prototype_argument(
4497 int n, /* pos of arg */
4498 type_t *tp, /* expected type (from prototype) */
4499 tnode_t *tn) /* argument */
4500 {
4501 tnode_t *ln = xcalloc(1, sizeof(*ln));
4502 ln->tn_type = expr_unqualified_type(tp);
4503 ln->tn_lvalue = true;
4504 if (typeok(FARG, n, ln, tn)) {
4505 bool dowarn;
4506 if (!types_compatible(tp, tn->tn_type,
4507 true, false, (dowarn = false, &dowarn)) || dowarn)
4508 tn = convert(FARG, n, tp, tn);
4509 }
4510 free(ln);
4511 return tn;
4512 }
4513
4514 /*
4515 * Check types of all function arguments and insert conversions,
4516 * if necessary.
4517 */
4518 static void
4519 check_function_arguments(const function_call *call)
4520 {
4521 type_t *ftp = call->func->tn_type->t_subt;
4522
4523 /* get # of parameters in the prototype */
4524 int npar = 0;
4525 for (const sym_t *p = ftp->u.params; p != NULL; p = p->s_next)
4526 npar++;
4527
4528 int narg = (int)call->args_len;
4529
4530 const sym_t *param = ftp->u.params;
4531 if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
4532 /* argument mismatch: %d %s passed, %d expected */
4533 error(150, narg, narg != 1 ? "arguments" : "argument", npar);
4534 param = NULL;
4535 }
4536
4537 for (int i = 0; i < narg; i++) {
4538 tnode_t *arg = call->args[i];
4539
4540 /* some things which are always not allowed */
4541 tspec_t at = arg->tn_type->t_tspec;
4542 if (at == VOID) {
4543 /* void expressions may not be arguments, arg #%d */
4544 error(151, i + 1);
4545 return;
4546 }
4547 if (is_struct_or_union(at) && is_incomplete(arg->tn_type)) {
4548 /* argument cannot have unknown size, arg #%d */
4549 error(152, i + 1);
4550 return;
4551 }
4552 if (is_integer(at) &&
4553 arg->tn_type->t_is_enum &&
4554 is_incomplete(arg->tn_type)) {
4555 /* argument cannot have unknown size, arg #%d */
4556 warning(152, i + 1);
4557 }
4558
4559 arg = cconv(arg);
4560 call->args[i] = arg;
4561
4562 arg = param != NULL
4563 ? check_prototype_argument(i + 1, param->s_type, arg)
4564 : promote(NOOP, true, arg);
4565 call->args[i] = arg;
4566
4567 if (param != NULL)
4568 param = param->s_next;
4569 }
4570 }
4571
4572 tnode_t *
4573 build_function_call(tnode_t *func, bool sys, function_call *call)
4574 {
4575
4576 if (func == NULL)
4577 return NULL;
4578
4579 call->func = func;
4580 check_ctype_function_call(call);
4581
4582 func = cconv(func);
4583 call->func = func;
4584
4585 if (func->tn_type->t_tspec != PTR ||
4586 func->tn_type->t_subt->t_tspec != FUNC) {
4587 /* cannot call '%s', must be a function */
4588 error(149, type_name(func->tn_type));
4589 return NULL;
4590 }
4591
4592 check_function_arguments(call);
4593
4594 tnode_t *ntn = expr_alloc_tnode();
4595 ntn->tn_op = CALL;
4596 ntn->tn_type = func->tn_type->t_subt->t_subt;
4597 ntn->tn_sys = sys;
4598 ntn->u.call = call;
4599 return ntn;
4600 }
4601
4602 /*
4603 * Return the value of an integral constant expression.
4604 * If the expression is not constant or its type is not an integer
4605 * type, an error message is printed.
4606 */
4607 val_t *
4608 integer_constant(tnode_t *tn, bool required)
4609 {
4610
4611 if (tn != NULL)
4612 tn = cconv(tn);
4613 if (tn != NULL)
4614 tn = promote(NOOP, false, tn);
4615
4616 val_t *v = xcalloc(1, sizeof(*v));
4617
4618 if (tn == NULL) {
4619 lint_assert(seen_error);
4620 debug_step("constant node is null; returning 1 instead");
4621 v->v_tspec = INT;
4622 v->u.integer = 1;
4623 return v;
4624 }
4625
4626 v->v_tspec = tn->tn_type->t_tspec;
4627
4628 if (tn->tn_op == CON) {
4629 lint_assert(tn->tn_type->t_tspec == tn->u.value.v_tspec);
4630 if (is_integer(tn->u.value.v_tspec)) {
4631 v->v_unsigned_since_c90 =
4632 tn->u.value.v_unsigned_since_c90;
4633 v->u.integer = tn->u.value.u.integer;
4634 return v;
4635 }
4636 v->u.integer = (int64_t)tn->u.value.u.floating;
4637 } else
4638 v->u.integer = 1;
4639
4640 if (required)
4641 /* integral constant expression expected */
4642 error(55);
4643 else
4644 /* variable array dimension is a C99/GCC extension */
4645 c99ism(318);
4646
4647 if (!is_integer(v->v_tspec))
4648 v->v_tspec = INT;
4649
4650 return v;
4651 }
4652
4653 /*
4654 * Perform some tests on expressions which can't be done in build_binary()
4655 * and functions called by build_binary(). These tests must be done here
4656 * because we need some information about the context in which the operations
4657 * are performed.
4658 * After all tests are performed and dofreeblk is true, expr() frees the
4659 * memory which is used for the expression.
4660 */
4661 void
4662 expr(tnode_t *tn, bool vctx, bool cond, bool dofreeblk, bool is_do_while,
4663 const char *stmt_kind)
4664 {
4665
4666 if (tn == NULL) { /* in case of errors */
4667 expr_free_all();
4668 return;
4669 }
4670
4671 /* expr() is also called in global initializations */
4672 if (dcs->d_kind != DLK_EXTERN && !is_do_while)
4673 check_statement_reachable(stmt_kind);
4674
4675 check_expr_misc(tn, vctx, cond, !cond, false, false, false);
4676 if (tn->tn_op == ASSIGN && !tn->tn_parenthesized) {
4677 if (hflag && cond)
4678 /* assignment in conditional context */
4679 warning(159);
4680 }
4681 if (!modtab[tn->tn_op].m_has_side_effect) {
4682 /*
4683 * for left operands of COMMA this warning is already printed
4684 */
4685 if (tn->tn_op != COMMA && !vctx && !cond)
4686 check_null_effect(tn);
4687 }
4688 debug_node(tn);
4689
4690 if (dofreeblk)
4691 expr_free_all();
4692 }
4693
4694 /* If the expression has the form '*(arr + idx)', check the array index. */
4695 static void
4696 check_array_index(const tnode_t *indir, bool taking_address)
4697 {
4698 const tnode_t *plus, *arr, *idx;
4699
4700 if (indir->tn_op == INDIR
4701 && (plus = indir->u.ops.left, plus->tn_op == PLUS)
4702 && plus->u.ops.left->tn_op == ADDR
4703 && (arr = plus->u.ops.left->u.ops.left, true)
4704 && (arr->tn_op == STRING || arr->tn_op == NAME)
4705 && arr->tn_type->t_tspec == ARRAY
4706 && (idx = plus->u.ops.right, idx->tn_op == CON)
4707 && (!is_incomplete(arr->tn_type) || idx->u.value.u.integer < 0))
4708 goto proceed;
4709 return;
4710
4711 proceed:;
4712 int elsz = length_in_bits(arr->tn_type->t_subt, NULL);
4713 if (elsz == 0)
4714 return;
4715 elsz /= CHAR_SIZE;
4716
4717 /* Change the unit of the index from bytes to element size. */
4718 int64_t con = is_uinteger(idx->tn_type->t_tspec)
4719 ? (int64_t)((uint64_t)idx->u.value.u.integer / elsz)
4720 : idx->u.value.u.integer / elsz;
4721
4722 int dim = arr->tn_type->u.dimension + (taking_address ? 1 : 0);
4723
4724 if (!is_uinteger(idx->tn_type->t_tspec) && con < 0)
4725 /* array subscript %jd cannot be negative */
4726 warning(167, (intmax_t)con);
4727 else if (dim > 0 && (uint64_t)con >= (uint64_t)dim)
4728 /* array subscript %ju cannot be > %d */
4729 warning(168, (uintmax_t)con, dim - 1);
4730 }
4731
4732 static void
4733 check_expr_addr(const tnode_t *ln, bool szof, bool fcall)
4734 {
4735 /* XXX: Taking warn_about_unreachable into account here feels wrong. */
4736 if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
4737 if (!szof)
4738 mark_as_set(ln->u.sym);
4739 mark_as_used(ln->u.sym, fcall, szof);
4740 }
4741 check_array_index(ln, true);
4742 }
4743
4744 /*
4745 * If there is an asm statement in one of the compound statements around,
4746 * there may be other side effects, so don't warn.
4747 */
4748 static bool
4749 is_asm_around(void)
4750 {
4751 for (decl_level *dl = dcs; dl != NULL; dl = dl->d_enclosing)
4752 if (dl->d_asm)
4753 return true;
4754 return false;
4755 }
4756
4757 static void
4758 check_expr_side_effect(const tnode_t *ln, bool szof)
4759 {
4760
4761 /* XXX: Taking warn_about_unreachable into account here feels wrong. */
4762 if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
4763 scl_t sc = ln->u.sym->s_scl;
4764 if (sc != EXTERN && sc != STATIC &&
4765 !ln->u.sym->s_set && !szof && !is_asm_around()) {
4766 /* '%s' may be used before set */
4767 warning(158, ln->u.sym->s_name);
4768 mark_as_set(ln->u.sym);
4769 }
4770 mark_as_used(ln->u.sym, false, false);
4771 }
4772 }
4773
4774 static void
4775 check_expr_assign(const tnode_t *ln, bool szof)
4776 {
4777 /* XXX: Taking warn_about_unreachable into account here feels wrong. */
4778 if (ln->tn_op == NAME && !szof && (reached || !warn_about_unreachable)) {
4779 mark_as_set(ln->u.sym);
4780 if (ln->u.sym->s_scl == EXTERN)
4781 outusg(ln->u.sym);
4782 }
4783 check_array_index(ln, false);
4784 }
4785
4786 static void
4787 check_expr_call(const tnode_t *tn, const tnode_t *ln,
4788 bool szof, bool vctx, bool cond, bool retval_discarded)
4789 {
4790 lint_assert(ln->tn_op == ADDR);
4791 lint_assert(ln->u.ops.left->tn_op == NAME);
4792 if (!szof && !is_compiler_builtin(ln->u.ops.left->u.sym->s_name))
4793 outcall(tn, vctx || cond, retval_discarded);
4794
4795 const function_call *call = tn->u.call;
4796 if (call->args_len == 4 || call->args_len == 5)
4797 check_snprintb(call);
4798 }
4799
4800 static void
4801 check_expr_op(op_t op, const tnode_t *ln, bool szof, bool fcall, bool eqwarn)
4802 {
4803 switch (op) {
4804 case ADDR:
4805 check_expr_addr(ln, szof, fcall);
4806 break;
4807 case LOAD:
4808 check_array_index(ln, false);
4809 /* FALLTHROUGH */
4810 case INCBEF:
4811 case DECBEF:
4812 case INCAFT:
4813 case DECAFT:
4814 case ADDASS:
4815 case SUBASS:
4816 case MULASS:
4817 case DIVASS:
4818 case MODASS:
4819 case ANDASS:
4820 case ORASS:
4821 case XORASS:
4822 case SHLASS:
4823 case SHRASS:
4824 case REAL:
4825 case IMAG:
4826 check_expr_side_effect(ln, szof);
4827 break;
4828 case ASSIGN:
4829 check_expr_assign(ln, szof);
4830 break;
4831 case EQ:
4832 if (hflag && eqwarn)
4833 /* operator '==' found where '=' was expected */
4834 warning(160);
4835 break;
4836 default:
4837 break;
4838 }
4839 }
4840
4841 /*
4842 * vctx ???
4843 * cond whether the expression is a condition that
4844 * will be compared with 0
4845 * eqwarn whether the operator '==' might be a
4846 * misspelled '='
4847 * fcall whether the expression is a function call
4848 * retval_discarded whether the return value of a function call
4849 * is discarded; such calls will be analyzed by
4850 * lint2 in messages 4, 8 and 9
4851 * szof whether the expression is part of a sizeof
4852 * expression, which means that its value is
4853 * discarded since only the type is relevant
4854 */
4855 void
4856 check_expr_misc(const tnode_t *tn, bool vctx, bool cond,
4857 bool eqwarn, bool fcall, bool retval_discarded, bool szof)
4858 {
4859
4860 if (tn == NULL)
4861 return;
4862 op_t op = tn->tn_op;
4863 if (op == NAME || op == CON || op == STRING)
4864 return;
4865 bool is_direct = op == CALL
4866 && tn->u.call->func->tn_op == ADDR
4867 && tn->u.call->func->u.ops.left->tn_op == NAME;
4868 if (op == CALL) {
4869 const function_call *call = tn->u.call;
4870 if (is_direct)
4871 check_expr_call(tn, call->func,
4872 szof, vctx, cond, retval_discarded);
4873 bool discard = op == CVT && tn->tn_type->t_tspec == VOID;
4874 check_expr_misc(call->func, false, false, false, is_direct,
4875 discard, szof);
4876 for (size_t i = 0, n = call->args_len; i < n; i++)
4877 check_expr_misc(call->args[i],
4878 true, false, false, false, false, szof);
4879 return;
4880 }
4881
4882 lint_assert(has_operands(tn));
4883 tnode_t *ln = tn->u.ops.left;
4884 tnode_t *rn = tn->u.ops.right;
4885 check_expr_op(op, ln, szof, fcall, eqwarn);
4886
4887 const mod_t *mp = &modtab[op];
4888 bool cvctx = mp->m_value_context;
4889 bool ccond = mp->m_compares_with_zero;
4890 bool eq = mp->m_warn_if_operand_eq &&
4891 !ln->tn_parenthesized &&
4892 rn != NULL && !rn->tn_parenthesized;
4893
4894 /*
4895 * Values of operands of ':' are not used if the type of at least
4896 * one of the operands (for GCC compatibility) is 'void'.
4897 *
4898 * XXX test/value context of QUEST should probably be used as
4899 * context for both operands of COLON.
4900 */
4901 if (op == COLON && tn->tn_type->t_tspec == VOID)
4902 cvctx = ccond = false;
4903 bool discard = op == CVT && tn->tn_type->t_tspec == VOID;
4904 check_expr_misc(ln, cvctx, ccond, eq, is_direct, discard, szof);
4905
4906 switch (op) {
4907 case LOGAND:
4908 case LOGOR:
4909 check_expr_misc(rn, false, true, eq, false, false, szof);
4910 break;
4911 case COLON:
4912 check_expr_misc(rn, cvctx, ccond, eq, false, false, szof);
4913 break;
4914 case COMMA:
4915 check_expr_misc(rn, vctx, cond, false, false, false, szof);
4916 break;
4917 default:
4918 if (mp->m_binary)
4919 check_expr_misc(rn, true, false, eq, false, false,
4920 szof);
4921 break;
4922 }
4923 }
4924
4925 /*
4926 * Return whether the expression can be used for static initialization.
4927 *
4928 * Constant initialization expressions must be constant or an address
4929 * of a static object with an optional offset. In the first case,
4930 * the result is returned in *offsp. In the second case, the static
4931 * object is returned in *symp and the offset in *offsp.
4932 *
4933 * The expression can consist of PLUS, MINUS, ADDR, NAME, STRING and
4934 * CON. Type conversions are allowed if they do not change binary
4935 * representation (including width).
4936 *
4937 * C99 6.6 "Constant expressions"
4938 * C99 6.7.8p4 restricts initializers for static storage duration
4939 */
4940 bool
4941 constant_addr(const tnode_t *tn, const sym_t **symp, ptrdiff_t *offsp)
4942 {
4943 const sym_t *sym;
4944 ptrdiff_t offs1, offs2;
4945 tspec_t t, ot;
4946
4947 switch (tn->tn_op) {
4948 case MINUS:
4949 if (tn->u.ops.right->tn_op == CVT)
4950 return constant_addr(tn->u.ops.right, symp, offsp);
4951 else if (tn->u.ops.right->tn_op != CON)
4952 return false;
4953 /* FALLTHROUGH */
4954 case PLUS:
4955 offs1 = offs2 = 0;
4956 if (tn->u.ops.left->tn_op == CON) {
4957 offs1 = (ptrdiff_t)tn->u.ops.left->u.value.u.integer;
4958 if (!constant_addr(tn->u.ops.right, &sym, &offs2))
4959 return false;
4960 } else if (tn->u.ops.right->tn_op == CON) {
4961 offs2 = (ptrdiff_t)tn->u.ops.right->u.value.u.integer;
4962 if (tn->tn_op == MINUS)
4963 offs2 = -offs2;
4964 if (!constant_addr(tn->u.ops.left, &sym, &offs1))
4965 return false;
4966 } else {
4967 return false;
4968 }
4969 *symp = sym;
4970 *offsp = offs1 + offs2;
4971 return true;
4972 case ADDR:
4973 if (tn->u.ops.left->tn_op == NAME) {
4974 *symp = tn->u.ops.left->u.sym;
4975 *offsp = 0;
4976 return true;
4977 } else {
4978 /*
4979 * If this were the front end of a compiler, we would
4980 * return a label instead of 0, at least if
4981 * 'tn->u.ops.left->tn_op == STRING'.
4982 */
4983 *symp = NULL;
4984 *offsp = 0;
4985 return true;
4986 }
4987 case CVT:
4988 t = tn->tn_type->t_tspec;
4989 ot = tn->u.ops.left->tn_type->t_tspec;
4990 if ((!is_integer(t) && t != PTR) ||
4991 (!is_integer(ot) && ot != PTR)) {
4992 return false;
4993 }
4994 #if 0
4995 /*-
4996 * consider:
4997 * struct foo {
4998 * unsigned char a;
4999 * } f = {
5000 * (unsigned char)(unsigned long)
5001 * (&(((struct foo *)0)->a))
5002 * };
5003 * since psize(unsigned long) != psize(unsigned char),
5004 * this fails.
5005 */
5006 else if (psize(t) != psize(ot))
5007 return -1;
5008 #endif
5009 return constant_addr(tn->u.ops.left, symp, offsp);
5010 default:
5011 return false;
5012 }
5013 }
5014
5015 /* Append s2 to s1, then free s2. */
5016 buffer *
5017 cat_strings(buffer *s1, buffer *s2)
5018 {
5019
5020 if ((s1->data != NULL) != (s2->data != NULL)) {
5021 /* cannot concatenate wide and regular string literals */
5022 error(292);
5023 return s1;
5024 }
5025
5026 if (s1->data != NULL) {
5027 while (s1->len + s2->len + 1 > s1->cap)
5028 s1->cap *= 2;
5029 s1->data = xrealloc(s1->data, s1->cap);
5030 memcpy(s1->data + s1->len, s2->data, s2->len + 1);
5031 free(s2->data);
5032 }
5033 s1->len += s2->len;
5034 free(s2);
5035
5036 return s1;
5037 }
5038
5039
5040 typedef struct stmt_expr {
5041 memory_pool se_mem;
5042 sym_t *se_sym;
5043 struct stmt_expr *se_enclosing;
5044 } stmt_expr;
5045
5046 static stmt_expr *stmt_exprs;
5047
5048 void
5049 begin_statement_expr(void)
5050 {
5051 debug_enter();
5052
5053 stmt_expr *se = xmalloc(sizeof(*se));
5054 se->se_mem = expr_save_memory();
5055 se->se_sym = NULL;
5056 se->se_enclosing = stmt_exprs;
5057 stmt_exprs = se;
5058 }
5059
5060 void
5061 do_statement_expr(tnode_t *tn)
5062 {
5063 block_level--;
5064 mem_block_level--;
5065 stmt_exprs->se_sym = tn != NULL
5066 ? mktempsym(block_dup_type(tn->tn_type))
5067 : NULL; /* after a syntax error */
5068 mem_block_level++;
5069 block_level++;
5070 /* '({ ... })' is a GCC extension */
5071 gnuism(320);
5072 }
5073
5074 tnode_t *
5075 end_statement_expr(void)
5076 {
5077 tnode_t *tn;
5078
5079 stmt_expr *se = stmt_exprs;
5080 if (se->se_sym == NULL) {
5081 tn = NULL; /* after a syntax error */
5082 goto end;
5083 }
5084
5085 tn = build_name(se->se_sym, false);
5086 (void)expr_save_memory(); /* leak */
5087 expr_restore_memory(se->se_mem);
5088 stmt_exprs = se->se_enclosing;
5089 free(se);
5090
5091 end:
5092 debug_leave();
5093 return tn;
5094 }
5095
5096 bool
5097 in_statement_expr(void)
5098 {
5099 return stmt_exprs != NULL;
5100 }
5101