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