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