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