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