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