c-warn.c revision 1.1.1.3
1/* Diagnostic routines shared by all languages that are variants of C. 2 Copyright (C) 1992-2019 Free Software Foundation, Inc. 3 4This file is part of GCC. 5 6GCC is free software; you can redistribute it and/or modify it under 7the terms of the GNU General Public License as published by the Free 8Software Foundation; either version 3, or (at your option) any later 9version. 10 11GCC is distributed in the hope that it will be useful, but WITHOUT ANY 12WARRANTY; without even the implied warranty of MERCHANTABILITY or 13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14for more details. 15 16You should have received a copy of the GNU General Public License 17along with GCC; see the file COPYING3. If not see 18<http://www.gnu.org/licenses/>. */ 19 20#include "config.h" 21#include "system.h" 22#include "coretypes.h" 23#include "target.h" 24#include "function.h" 25#include "tree.h" 26#include "c-common.h" 27#include "memmodel.h" 28#include "tm_p.h" 29#include "diagnostic.h" 30#include "intl.h" 31#include "stringpool.h" 32#include "attribs.h" 33#include "asan.h" 34#include "gcc-rich-location.h" 35#include "gimplify.h" 36#include "c-family/c-indentation.h" 37#include "calls.h" 38#include "stor-layout.h" 39 40/* Print a warning if a constant expression had overflow in folding. 41 Invoke this function on every expression that the language 42 requires to be a constant expression. 43 Note the ANSI C standard says it is erroneous for a 44 constant expression to overflow. */ 45 46void 47constant_expression_warning (tree value) 48{ 49 if (warn_overflow && pedantic 50 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST 51 || TREE_CODE (value) == FIXED_CST 52 || TREE_CODE (value) == VECTOR_CST 53 || TREE_CODE (value) == COMPLEX_CST) 54 && TREE_OVERFLOW (value)) 55 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression"); 56} 57 58/* The same as above but print an unconditional error. */ 59 60void 61constant_expression_error (tree value) 62{ 63 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST 64 || TREE_CODE (value) == FIXED_CST 65 || TREE_CODE (value) == VECTOR_CST 66 || TREE_CODE (value) == COMPLEX_CST) 67 && TREE_OVERFLOW (value)) 68 error ("overflow in constant expression"); 69} 70 71/* Print a warning if an expression result VALUE had an overflow 72 in folding and its operands hadn't. EXPR, which may be null, is 73 the operand of the expression. 74 75 Invoke this function on every expression that 76 (1) appears in the source code, and 77 (2) is a constant expression that overflowed, and 78 (3) is not already checked by convert_and_check; 79 however, do not invoke this function on operands of explicit casts 80 or when the expression is the result of an operator and any operand 81 already overflowed. */ 82 83void 84overflow_warning (location_t loc, tree value, tree expr) 85{ 86 if (c_inhibit_evaluation_warnings != 0) 87 return; 88 89 const char *warnfmt = NULL; 90 91 switch (TREE_CODE (value)) 92 { 93 case INTEGER_CST: 94 warnfmt = (expr 95 ? G_("integer overflow in expression %qE of type %qT " 96 "results in %qE") 97 : G_("integer overflow in expression of type %qT " 98 "results in %qE")); 99 break; 100 101 case REAL_CST: 102 warnfmt = (expr 103 ? G_("floating point overflow in expression %qE " 104 "of type %qT results in %qE") 105 : G_("floating point overflow in expression of type %qT " 106 "results in %qE")); 107 break; 108 109 case FIXED_CST: 110 warnfmt = (expr 111 ? G_("fixed-point overflow in expression %qE of type %qT " 112 "results in %qE") 113 : G_("fixed-point overflow in expression of type %qT " 114 "results in %qE")); 115 break; 116 117 case VECTOR_CST: 118 warnfmt = (expr 119 ? G_("vector overflow in expression %qE of type %qT " 120 "results in %qE") 121 : G_("vector overflow in expression of type %qT " 122 "results in %qE")); 123 break; 124 125 case COMPLEX_CST: 126 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST) 127 warnfmt = (expr 128 ? G_("complex integer overflow in expression %qE " 129 "of type %qT results in %qE") 130 : G_("complex integer overflow in expression of type %qT " 131 "results in %qE")); 132 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST) 133 warnfmt = (expr 134 ? G_("complex floating point overflow in expression %qE " 135 "of type %qT results in %qE") 136 : G_("complex floating point overflow in expression " 137 "of type %qT results in %qE")); 138 else 139 return; 140 break; 141 142 default: 143 return; 144 } 145 146 bool warned; 147 if (expr) 148 warned = warning_at (loc, OPT_Woverflow, warnfmt, expr, TREE_TYPE (expr), 149 value); 150 else 151 warned = warning_at (loc, OPT_Woverflow, warnfmt, TREE_TYPE (value), 152 value); 153 154 if (warned) 155 TREE_NO_WARNING (value) = 1; 156} 157 158/* Helper function for walk_tree. Unwrap C_MAYBE_CONST_EXPRs in an expression 159 pointed to by TP. */ 160 161static tree 162unwrap_c_maybe_const (tree *tp, int *walk_subtrees, void *) 163{ 164 if (TREE_CODE (*tp) == C_MAYBE_CONST_EXPR) 165 { 166 *tp = C_MAYBE_CONST_EXPR_EXPR (*tp); 167 /* C_MAYBE_CONST_EXPRs don't nest. */ 168 *walk_subtrees = false; 169 } 170 return NULL_TREE; 171} 172 173/* Warn about uses of logical || / && operator in a context where it 174 is likely that the bitwise equivalent was intended by the 175 programmer. We have seen an expression in which CODE is a binary 176 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding 177 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */ 178 179void 180warn_logical_operator (location_t location, enum tree_code code, tree type, 181 enum tree_code code_left, tree op_left, 182 enum tree_code ARG_UNUSED (code_right), tree op_right) 183{ 184 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR); 185 int in0_p, in1_p, in_p; 186 tree low0, low1, low, high0, high1, high, lhs, rhs, tem; 187 bool strict_overflow_p = false; 188 189 if (!warn_logical_op) 190 return; 191 192 if (code != TRUTH_ANDIF_EXPR 193 && code != TRUTH_AND_EXPR 194 && code != TRUTH_ORIF_EXPR 195 && code != TRUTH_OR_EXPR) 196 return; 197 198 /* We don't want to warn if either operand comes from a macro 199 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet; 200 see PR61534. */ 201 if (from_macro_expansion_at (EXPR_LOCATION (op_left)) 202 || from_macro_expansion_at (EXPR_LOCATION (op_right))) 203 return; 204 205 /* Warn if &&/|| are being used in a context where it is 206 likely that the bitwise equivalent was intended by the 207 programmer. That is, an expression such as op && MASK 208 where op should not be any boolean expression, nor a 209 constant, and mask seems to be a non-boolean integer constant. */ 210 if (TREE_CODE (op_right) == CONST_DECL) 211 /* An enumerator counts as a constant. */ 212 op_right = DECL_INITIAL (op_right); 213 if (!truth_value_p (code_left) 214 && INTEGRAL_TYPE_P (TREE_TYPE (op_left)) 215 && !CONSTANT_CLASS_P (op_left) 216 && !TREE_NO_WARNING (op_left)) 217 { 218 tree folded_op_right = fold_for_warn (op_right); 219 if (TREE_CODE (folded_op_right) == INTEGER_CST 220 && !integer_zerop (folded_op_right) 221 && !integer_onep (folded_op_right)) 222 { 223 bool warned; 224 if (or_op) 225 warned 226 = warning_at (location, OPT_Wlogical_op, 227 "logical %<or%> applied to non-boolean constant"); 228 else 229 warned 230 = warning_at (location, OPT_Wlogical_op, 231 "logical %<and%> applied to non-boolean constant"); 232 if (warned) 233 TREE_NO_WARNING (op_left) = true; 234 return; 235 } 236 } 237 238 /* We do not warn for constants because they are typical of macro 239 expansions that test for features. */ 240 if (CONSTANT_CLASS_P (fold_for_warn (op_left)) 241 || CONSTANT_CLASS_P (fold_for_warn (op_right))) 242 return; 243 244 /* This warning only makes sense with logical operands. */ 245 if (!(truth_value_p (TREE_CODE (op_left)) 246 || INTEGRAL_TYPE_P (TREE_TYPE (op_left))) 247 || !(truth_value_p (TREE_CODE (op_right)) 248 || INTEGRAL_TYPE_P (TREE_TYPE (op_right)))) 249 return; 250 251 /* The range computations only work with scalars. */ 252 if (VECTOR_TYPE_P (TREE_TYPE (op_left)) 253 || VECTOR_TYPE_P (TREE_TYPE (op_right))) 254 return; 255 256 /* We first test whether either side separately is trivially true 257 (with OR) or trivially false (with AND). If so, do not warn. 258 This is a common idiom for testing ranges of data types in 259 portable code. */ 260 op_left = unshare_expr (op_left); 261 walk_tree_without_duplicates (&op_left, unwrap_c_maybe_const, NULL); 262 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p); 263 if (!lhs) 264 return; 265 266 /* If this is an OR operation, invert both sides; now, the result 267 should be always false to get a warning. */ 268 if (or_op) 269 in0_p = !in0_p; 270 271 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0); 272 if (tem && integer_zerop (tem)) 273 return; 274 275 op_right = unshare_expr (op_right); 276 walk_tree_without_duplicates (&op_right, unwrap_c_maybe_const, NULL); 277 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p); 278 if (!rhs) 279 return; 280 281 /* If this is an OR operation, invert both sides; now, the result 282 should be always false to get a warning. */ 283 if (or_op) 284 in1_p = !in1_p; 285 286 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1); 287 if (tem && integer_zerop (tem)) 288 return; 289 290 /* If both expressions have the same operand, if we can merge the 291 ranges, ... */ 292 if (operand_equal_p (lhs, rhs, 0) 293 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0, 294 in1_p, low1, high1)) 295 { 296 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high); 297 /* ... and if the range test is always false, then warn. */ 298 if (tem && integer_zerop (tem)) 299 { 300 if (or_op) 301 warning_at (location, OPT_Wlogical_op, 302 "logical %<or%> of collectively exhaustive tests is " 303 "always true"); 304 else 305 warning_at (location, OPT_Wlogical_op, 306 "logical %<and%> of mutually exclusive tests is " 307 "always false"); 308 } 309 /* Or warn if the operands have exactly the same range, e.g. 310 A > 0 && A > 0. */ 311 else if (tree_int_cst_equal (low0, low1) 312 && tree_int_cst_equal (high0, high1)) 313 { 314 if (or_op) 315 warning_at (location, OPT_Wlogical_op, 316 "logical %<or%> of equal expressions"); 317 else 318 warning_at (location, OPT_Wlogical_op, 319 "logical %<and%> of equal expressions"); 320 } 321 } 322} 323 324/* Helper function for warn_tautological_cmp. Look for ARRAY_REFs 325 with constant indices. */ 326 327static tree 328find_array_ref_with_const_idx_r (tree *expr_p, int *, void *) 329{ 330 tree expr = *expr_p; 331 332 if ((TREE_CODE (expr) == ARRAY_REF 333 || TREE_CODE (expr) == ARRAY_RANGE_REF) 334 && (TREE_CODE (fold_for_warn (TREE_OPERAND (expr, 1))) 335 == INTEGER_CST)) 336 return integer_type_node; 337 338 return NULL_TREE; 339} 340 341/* Subroutine of warn_tautological_cmp. Warn about bitwise comparison 342 that always evaluate to true or false. LOC is the location of the 343 ==/!= comparison specified by CODE; LHS and RHS are the usual operands 344 of this comparison. */ 345 346static void 347warn_tautological_bitwise_comparison (const op_location_t &loc, tree_code code, 348 tree lhs, tree rhs) 349{ 350 if (code != EQ_EXPR && code != NE_EXPR) 351 return; 352 353 /* Extract the operands from e.g. (x & 8) == 4. */ 354 tree bitop; 355 tree cst; 356 tree stripped_lhs = tree_strip_any_location_wrapper (lhs); 357 tree stripped_rhs = tree_strip_any_location_wrapper (rhs); 358 if ((TREE_CODE (lhs) == BIT_AND_EXPR 359 || TREE_CODE (lhs) == BIT_IOR_EXPR) 360 && TREE_CODE (stripped_rhs) == INTEGER_CST) 361 bitop = lhs, cst = stripped_rhs; 362 else if ((TREE_CODE (rhs) == BIT_AND_EXPR 363 || TREE_CODE (rhs) == BIT_IOR_EXPR) 364 && TREE_CODE (stripped_lhs) == INTEGER_CST) 365 bitop = rhs, cst = stripped_lhs; 366 else 367 return; 368 369 tree bitopcst; 370 tree bitop_op0 = fold_for_warn (TREE_OPERAND (bitop, 0)); 371 if (TREE_CODE (bitop_op0) == INTEGER_CST) 372 bitopcst = bitop_op0; 373 else { 374 tree bitop_op1 = fold_for_warn (TREE_OPERAND (bitop, 1)); 375 if (TREE_CODE (bitop_op1) == INTEGER_CST) 376 bitopcst = bitop_op1; 377 else 378 return; 379 } 380 381 /* Note that the two operands are from before the usual integer 382 conversions, so their types might not be the same. 383 Use the larger of the two precisions and ignore bits outside 384 of that. */ 385 int prec = MAX (TYPE_PRECISION (TREE_TYPE (cst)), 386 TYPE_PRECISION (TREE_TYPE (bitopcst))); 387 388 wide_int bitopcstw = wi::to_wide (bitopcst, prec); 389 wide_int cstw = wi::to_wide (cst, prec); 390 391 wide_int res; 392 if (TREE_CODE (bitop) == BIT_AND_EXPR) 393 res = bitopcstw & cstw; 394 else 395 res = bitopcstw | cstw; 396 397 /* For BIT_AND only warn if (CST2 & CST1) != CST1, and 398 for BIT_OR only if (CST2 | CST1) != CST1. */ 399 if (res == cstw) 400 return; 401 402 binary_op_rich_location richloc (loc, lhs, rhs, false); 403 if (code == EQ_EXPR) 404 warning_at (&richloc, OPT_Wtautological_compare, 405 "bitwise comparison always evaluates to false"); 406 else 407 warning_at (&richloc, OPT_Wtautological_compare, 408 "bitwise comparison always evaluates to true"); 409} 410 411/* Given LOC from a macro expansion, return the map for the outermost 412 macro in the nest of expansions. */ 413 414static const line_map_macro * 415get_outermost_macro_expansion (location_t loc) 416{ 417 gcc_assert (from_macro_expansion_at (loc)); 418 419 const line_map *map = linemap_lookup (line_table, loc); 420 const line_map_macro *macro_map; 421 do 422 { 423 macro_map = linemap_check_macro (map); 424 loc = linemap_unwind_toward_expansion (line_table, loc, &map); 425 } while (linemap_macro_expansion_map_p (map)); 426 427 return macro_map; 428} 429 430/* Given LOC_A and LOC_B from macro expansions, return true if 431 they are "spelled the same" i.e. if they are both directly from 432 expansion of the same non-function-like macro. */ 433 434static bool 435spelled_the_same_p (location_t loc_a, location_t loc_b) 436{ 437 gcc_assert (from_macro_expansion_at (loc_a)); 438 gcc_assert (from_macro_expansion_at (loc_b)); 439 440 const line_map_macro *map_a = get_outermost_macro_expansion (loc_a); 441 const line_map_macro *map_b = get_outermost_macro_expansion (loc_b); 442 443 if (map_a->macro == map_b->macro) 444 if (!cpp_fun_like_macro_p (map_a->macro)) 445 return true; 446 447 return false; 448} 449 450/* Warn if a self-comparison always evaluates to true or false. LOC 451 is the location of the comparison with code CODE, LHS and RHS are 452 operands of the comparison. */ 453 454void 455warn_tautological_cmp (const op_location_t &loc, enum tree_code code, 456 tree lhs, tree rhs) 457{ 458 if (TREE_CODE_CLASS (code) != tcc_comparison) 459 return; 460 461 /* Don't warn for various macro expansions. */ 462 if (from_macro_expansion_at (loc)) 463 return; 464 bool lhs_in_macro = from_macro_expansion_at (EXPR_LOCATION (lhs)); 465 bool rhs_in_macro = from_macro_expansion_at (EXPR_LOCATION (rhs)); 466 if (lhs_in_macro || rhs_in_macro) 467 { 468 /* Don't warn if exactly one is from a macro. */ 469 if (!(lhs_in_macro && rhs_in_macro)) 470 return; 471 472 /* If both are in a macro, only warn if they're spelled the same. */ 473 if (!spelled_the_same_p (EXPR_LOCATION (lhs), EXPR_LOCATION (rhs))) 474 return; 475 } 476 477 warn_tautological_bitwise_comparison (loc, code, lhs, rhs); 478 479 /* We do not warn for constants because they are typical of macro 480 expansions that test for features, sizeof, and similar. */ 481 if (CONSTANT_CLASS_P (fold_for_warn (lhs)) 482 || CONSTANT_CLASS_P (fold_for_warn (rhs))) 483 return; 484 485 /* Don't warn for e.g. 486 HOST_WIDE_INT n; 487 ... 488 if (n == (long) n) ... 489 */ 490 if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR) 491 || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR)) 492 return; 493 494 /* Don't warn if either LHS or RHS has an IEEE floating-point type. 495 It could be a NaN, and NaN never compares equal to anything, even 496 itself. */ 497 if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs))) 498 return; 499 500 if (operand_equal_p (lhs, rhs, 0)) 501 { 502 /* Don't warn about array references with constant indices; 503 these are likely to come from a macro. */ 504 if (walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r, 505 NULL)) 506 return; 507 const bool always_true = (code == EQ_EXPR || code == LE_EXPR 508 || code == GE_EXPR || code == UNLE_EXPR 509 || code == UNGE_EXPR || code == UNEQ_EXPR); 510 binary_op_rich_location richloc (loc, lhs, rhs, false); 511 if (always_true) 512 warning_at (&richloc, OPT_Wtautological_compare, 513 "self-comparison always evaluates to true"); 514 else 515 warning_at (&richloc, OPT_Wtautological_compare, 516 "self-comparison always evaluates to false"); 517 } 518} 519 520/* Return true iff EXPR only contains boolean operands, or comparisons. */ 521 522static bool 523expr_has_boolean_operands_p (tree expr) 524{ 525 STRIP_NOPS (expr); 526 527 if (CONVERT_EXPR_P (expr)) 528 return bool_promoted_to_int_p (expr); 529 else if (UNARY_CLASS_P (expr)) 530 return expr_has_boolean_operands_p (TREE_OPERAND (expr, 0)); 531 else if (BINARY_CLASS_P (expr)) 532 return (expr_has_boolean_operands_p (TREE_OPERAND (expr, 0)) 533 && expr_has_boolean_operands_p (TREE_OPERAND (expr, 1))); 534 else if (COMPARISON_CLASS_P (expr)) 535 return true; 536 else 537 return false; 538} 539 540/* Warn about logical not used on the left hand side operand of a comparison. 541 This function assumes that the LHS is inside of TRUTH_NOT_EXPR. 542 Do not warn if RHS is of a boolean type, a logical operator, or 543 a comparison. */ 544 545void 546warn_logical_not_parentheses (location_t location, enum tree_code code, 547 tree lhs, tree rhs) 548{ 549 if (TREE_CODE_CLASS (code) != tcc_comparison 550 || TREE_TYPE (rhs) == NULL_TREE 551 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE 552 || truth_value_p (TREE_CODE (rhs))) 553 return; 554 555 /* Don't warn for expression like !x == ~(bool1 | bool2). */ 556 if (expr_has_boolean_operands_p (rhs)) 557 return; 558 559 /* Don't warn for !x == 0 or !y != 0, those are equivalent to 560 !(x == 0) or !(y != 0). */ 561 if ((code == EQ_EXPR || code == NE_EXPR) 562 && integer_zerop (rhs)) 563 return; 564 565 auto_diagnostic_group d; 566 if (warning_at (location, OPT_Wlogical_not_parentheses, 567 "logical not is only applied to the left hand side of " 568 "comparison") 569 && EXPR_HAS_LOCATION (lhs)) 570 { 571 location_t lhs_loc = EXPR_LOCATION (lhs); 572 rich_location richloc (line_table, lhs_loc); 573 richloc.add_fixit_insert_before (lhs_loc, "("); 574 richloc.add_fixit_insert_after (lhs_loc, ")"); 575 inform (&richloc, "add parentheses around left hand side " 576 "expression to silence this warning"); 577 } 578} 579 580/* Warn if EXP contains any computations whose results are not used. 581 Return true if a warning is printed; false otherwise. LOCUS is the 582 (potential) location of the expression. */ 583 584bool 585warn_if_unused_value (const_tree exp, location_t locus) 586{ 587 restart: 588 if (TREE_USED (exp) || TREE_NO_WARNING (exp)) 589 return false; 590 591 /* Don't warn about void constructs. This includes casting to void, 592 void function calls, and statement expressions with a final cast 593 to void. */ 594 if (VOID_TYPE_P (TREE_TYPE (exp))) 595 return false; 596 597 if (EXPR_HAS_LOCATION (exp)) 598 locus = EXPR_LOCATION (exp); 599 600 switch (TREE_CODE (exp)) 601 { 602 case PREINCREMENT_EXPR: 603 case POSTINCREMENT_EXPR: 604 case PREDECREMENT_EXPR: 605 case POSTDECREMENT_EXPR: 606 case MODIFY_EXPR: 607 case INIT_EXPR: 608 case TARGET_EXPR: 609 case CALL_EXPR: 610 case TRY_CATCH_EXPR: 611 case EXIT_EXPR: 612 case VA_ARG_EXPR: 613 return false; 614 615 case BIND_EXPR: 616 /* For a binding, warn if no side effect within it. */ 617 exp = BIND_EXPR_BODY (exp); 618 goto restart; 619 620 case SAVE_EXPR: 621 case NON_LVALUE_EXPR: 622 case NOP_EXPR: 623 exp = TREE_OPERAND (exp, 0); 624 goto restart; 625 626 case TRUTH_ORIF_EXPR: 627 case TRUTH_ANDIF_EXPR: 628 /* In && or ||, warn if 2nd operand has no side effect. */ 629 exp = TREE_OPERAND (exp, 1); 630 goto restart; 631 632 case COMPOUND_EXPR: 633 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus)) 634 return true; 635 /* Let people do `(foo (), 0)' without a warning. */ 636 if (TREE_CONSTANT (TREE_OPERAND (exp, 1))) 637 return false; 638 exp = TREE_OPERAND (exp, 1); 639 goto restart; 640 641 case COND_EXPR: 642 /* If this is an expression with side effects, don't warn; this 643 case commonly appears in macro expansions. */ 644 if (TREE_SIDE_EFFECTS (exp)) 645 return false; 646 goto warn; 647 648 case INDIRECT_REF: 649 /* Don't warn about automatic dereferencing of references, since 650 the user cannot control it. */ 651 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE) 652 { 653 exp = TREE_OPERAND (exp, 0); 654 goto restart; 655 } 656 /* Fall through. */ 657 658 default: 659 /* Referencing a volatile value is a side effect, so don't warn. */ 660 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp)) 661 && TREE_THIS_VOLATILE (exp)) 662 return false; 663 664 /* If this is an expression which has no operands, there is no value 665 to be unused. There are no such language-independent codes, 666 but front ends may define such. */ 667 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0) 668 return false; 669 670 warn: 671 return warning_at (locus, OPT_Wunused_value, "value computed is not used"); 672 } 673} 674 675/* Print a warning about casts that might indicate violation of strict 676 aliasing rules if -Wstrict-aliasing is used and strict aliasing 677 mode is in effect. LOC is the location of the expression being 678 cast, EXPR might be from inside it. TYPE is the type we're casting 679 to. */ 680 681bool 682strict_aliasing_warning (location_t loc, tree type, tree expr) 683{ 684 if (loc == UNKNOWN_LOCATION) 685 loc = input_location; 686 687 /* Strip pointer conversion chains and get to the correct original type. */ 688 STRIP_NOPS (expr); 689 tree otype = TREE_TYPE (expr); 690 691 if (!(flag_strict_aliasing 692 && POINTER_TYPE_P (type) 693 && POINTER_TYPE_P (otype) 694 && !VOID_TYPE_P (TREE_TYPE (type))) 695 /* If the type we are casting to is a ref-all pointer 696 dereferencing it is always valid. */ 697 || TYPE_REF_CAN_ALIAS_ALL (type)) 698 return false; 699 700 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR 701 && (DECL_P (TREE_OPERAND (expr, 0)) 702 || handled_component_p (TREE_OPERAND (expr, 0)))) 703 { 704 /* Casting the address of an object to non void pointer. Warn 705 if the cast breaks type based aliasing. */ 706 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2) 707 { 708 warning_at (loc, OPT_Wstrict_aliasing, 709 "type-punning to incomplete type " 710 "might break strict-aliasing rules"); 711 return true; 712 } 713 else 714 { 715 /* warn_strict_aliasing >= 3. This includes the default (3). 716 Only warn if the cast is dereferenced immediately. */ 717 alias_set_type set1 718 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))); 719 alias_set_type set2 = get_alias_set (TREE_TYPE (type)); 720 721 if (set2 != 0 722 && set1 != set2 723 && !alias_set_subset_of (set2, set1) 724 && !alias_sets_conflict_p (set1, set2)) 725 { 726 warning_at (loc, OPT_Wstrict_aliasing, 727 "dereferencing type-punned " 728 "pointer will break strict-aliasing rules"); 729 return true; 730 } 731 else if (warn_strict_aliasing == 2 732 && !alias_sets_must_conflict_p (set1, set2)) 733 { 734 warning_at (loc, OPT_Wstrict_aliasing, 735 "dereferencing type-punned " 736 "pointer might break strict-aliasing rules"); 737 return true; 738 } 739 } 740 } 741 else if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype))) 742 { 743 /* At this level, warn for any conversions, even if an address is 744 not taken in the same statement. This will likely produce many 745 false positives, but could be useful to pinpoint problems that 746 are not revealed at higher levels. */ 747 alias_set_type set1 = get_alias_set (TREE_TYPE (otype)); 748 alias_set_type set2 = get_alias_set (TREE_TYPE (type)); 749 if (!COMPLETE_TYPE_P (type) 750 || !alias_sets_must_conflict_p (set1, set2)) 751 { 752 warning_at (loc, OPT_Wstrict_aliasing, 753 "dereferencing type-punned " 754 "pointer might break strict-aliasing rules"); 755 return true; 756 } 757 } 758 759 return false; 760} 761 762/* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with 763 sizeof as last operand of certain builtins. */ 764 765void 766sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee, 767 vec<tree, va_gc> *params, tree *sizeof_arg, 768 bool (*comp_types) (tree, tree)) 769{ 770 tree type, dest = NULL_TREE, src = NULL_TREE, tem; 771 bool strop = false, cmp = false; 772 unsigned int idx = ~0; 773 location_t loc; 774 775 if (TREE_CODE (callee) != FUNCTION_DECL 776 || !fndecl_built_in_p (callee, BUILT_IN_NORMAL) 777 || vec_safe_length (params) <= 1) 778 return; 779 780 enum built_in_function fncode = DECL_FUNCTION_CODE (callee); 781 switch (fncode) 782 { 783 case BUILT_IN_STRNCMP: 784 case BUILT_IN_STRNCASECMP: 785 cmp = true; 786 /* FALLTHRU */ 787 case BUILT_IN_STRNCPY: 788 case BUILT_IN_STRNCPY_CHK: 789 case BUILT_IN_STRNCAT: 790 case BUILT_IN_STRNCAT_CHK: 791 case BUILT_IN_STPNCPY: 792 case BUILT_IN_STPNCPY_CHK: 793 strop = true; 794 /* FALLTHRU */ 795 case BUILT_IN_MEMCPY: 796 case BUILT_IN_MEMCPY_CHK: 797 case BUILT_IN_MEMMOVE: 798 case BUILT_IN_MEMMOVE_CHK: 799 if (params->length () < 3) 800 return; 801 src = (*params)[1]; 802 dest = (*params)[0]; 803 idx = 2; 804 break; 805 case BUILT_IN_BCOPY: 806 if (params->length () < 3) 807 return; 808 src = (*params)[0]; 809 dest = (*params)[1]; 810 idx = 2; 811 break; 812 case BUILT_IN_MEMCMP: 813 case BUILT_IN_BCMP: 814 if (params->length () < 3) 815 return; 816 src = (*params)[1]; 817 dest = (*params)[0]; 818 idx = 2; 819 cmp = true; 820 break; 821 case BUILT_IN_MEMSET: 822 case BUILT_IN_MEMSET_CHK: 823 if (params->length () < 3) 824 return; 825 dest = (*params)[0]; 826 idx = 2; 827 break; 828 case BUILT_IN_BZERO: 829 dest = (*params)[0]; 830 idx = 1; 831 break; 832 case BUILT_IN_STRNDUP: 833 src = (*params)[0]; 834 strop = true; 835 idx = 1; 836 break; 837 case BUILT_IN_MEMCHR: 838 if (params->length () < 3) 839 return; 840 src = (*params)[0]; 841 idx = 2; 842 break; 843 case BUILT_IN_SNPRINTF: 844 case BUILT_IN_SNPRINTF_CHK: 845 case BUILT_IN_VSNPRINTF: 846 case BUILT_IN_VSNPRINTF_CHK: 847 dest = (*params)[0]; 848 idx = 1; 849 strop = true; 850 break; 851 default: 852 break; 853 } 854 855 if (idx >= 3) 856 return; 857 858 /* Use error_operand_p to detect non-error arguments with an error 859 type that the C++ front-end constructs. */ 860 if (error_operand_p (src) 861 || error_operand_p (dest) 862 || !sizeof_arg[idx] 863 || error_operand_p (sizeof_arg[idx])) 864 return; 865 866 type = TYPE_P (sizeof_arg[idx]) 867 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]); 868 869 if (!POINTER_TYPE_P (type)) 870 { 871 /* The argument type may be an array. Diagnose bounded string 872 copy functions that specify the bound in terms of the source 873 argument rather than the destination unless they are equal 874 to one another. Handle constant sizes and also try to handle 875 sizeof expressions involving VLAs. */ 876 if (strop && !cmp && fncode != BUILT_IN_STRNDUP && src) 877 { 878 tem = tree_strip_nop_conversions (src); 879 if (TREE_CODE (tem) == ADDR_EXPR) 880 tem = TREE_OPERAND (tem, 0); 881 882 /* Avoid diagnosing sizeof SRC when SRC is declared with 883 attribute nonstring. */ 884 tree dummy; 885 if (get_attr_nonstring_decl (tem, &dummy)) 886 return; 887 888 tree d = tree_strip_nop_conversions (dest); 889 if (TREE_CODE (d) == ADDR_EXPR) 890 d = TREE_OPERAND (d, 0); 891 892 tree dstsz = TYPE_SIZE_UNIT (TREE_TYPE (d)); 893 tree srcsz = TYPE_SIZE_UNIT (TREE_TYPE (tem)); 894 895 if ((!dstsz 896 || !srcsz 897 || !operand_equal_p (dstsz, srcsz, OEP_LEXICOGRAPHIC)) 898 && operand_equal_p (tem, sizeof_arg[idx], OEP_ADDRESS_OF)) 899 warning_at (sizeof_arg_loc[idx], OPT_Wsizeof_pointer_memaccess, 900 "argument to %<sizeof%> in %qD call is the same " 901 "expression as the source; did you mean to use " 902 "the size of the destination?", 903 callee); 904 } 905 906 return; 907 } 908 909 if (dest 910 && (tem = tree_strip_nop_conversions (dest)) 911 && POINTER_TYPE_P (TREE_TYPE (tem)) 912 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type)) 913 return; 914 915 if (src 916 && (tem = tree_strip_nop_conversions (src)) 917 && POINTER_TYPE_P (TREE_TYPE (tem)) 918 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type)) 919 return; 920 921 loc = sizeof_arg_loc[idx]; 922 923 if (dest && !cmp) 924 { 925 if (!TYPE_P (sizeof_arg[idx]) 926 && operand_equal_p (dest, sizeof_arg[idx], 0) 927 && comp_types (TREE_TYPE (dest), type)) 928 { 929 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) 930 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 931 "argument to %<sizeof%> in %qD call is the same " 932 "expression as the destination; did you mean to " 933 "remove the addressof?", callee); 934 else if ((TYPE_PRECISION (TREE_TYPE (type)) 935 == TYPE_PRECISION (char_type_node)) 936 || strop) 937 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 938 "argument to %<sizeof%> in %qD call is the same " 939 "expression as the destination; did you mean to " 940 "provide an explicit length?", callee); 941 else 942 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 943 "argument to %<sizeof%> in %qD call is the same " 944 "expression as the destination; did you mean to " 945 "dereference it?", callee); 946 return; 947 } 948 949 if (POINTER_TYPE_P (TREE_TYPE (dest)) 950 && !strop 951 && comp_types (TREE_TYPE (dest), type) 952 && !VOID_TYPE_P (TREE_TYPE (type))) 953 { 954 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 955 "argument to %<sizeof%> in %qD call is the same " 956 "pointer type %qT as the destination; expected %qT " 957 "or an explicit length", callee, TREE_TYPE (dest), 958 TREE_TYPE (TREE_TYPE (dest))); 959 return; 960 } 961 } 962 963 if (src && !cmp) 964 { 965 if (!TYPE_P (sizeof_arg[idx]) 966 && operand_equal_p (src, sizeof_arg[idx], 0) 967 && comp_types (TREE_TYPE (src), type)) 968 { 969 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) 970 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 971 "argument to %<sizeof%> in %qD call is the same " 972 "expression as the source; did you mean to " 973 "remove the addressof?", callee); 974 else if ((TYPE_PRECISION (TREE_TYPE (type)) 975 == TYPE_PRECISION (char_type_node)) 976 || strop) 977 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 978 "argument to %<sizeof%> in %qD call is the same " 979 "expression as the source; did you mean to " 980 "provide an explicit length?", callee); 981 else 982 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 983 "argument to %<sizeof%> in %qD call is the same " 984 "expression as the source; did you mean to " 985 "dereference it?", callee); 986 return; 987 } 988 989 if (POINTER_TYPE_P (TREE_TYPE (src)) 990 && !strop 991 && comp_types (TREE_TYPE (src), type) 992 && !VOID_TYPE_P (TREE_TYPE (type))) 993 { 994 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 995 "argument to %<sizeof%> in %qD call is the same " 996 "pointer type %qT as the source; expected %qT " 997 "or an explicit length", callee, TREE_TYPE (src), 998 TREE_TYPE (TREE_TYPE (src))); 999 return; 1000 } 1001 } 1002 1003 if (dest) 1004 { 1005 if (!TYPE_P (sizeof_arg[idx]) 1006 && operand_equal_p (dest, sizeof_arg[idx], 0) 1007 && comp_types (TREE_TYPE (dest), type)) 1008 { 1009 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) 1010 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 1011 "argument to %<sizeof%> in %qD call is the same " 1012 "expression as the first source; did you mean to " 1013 "remove the addressof?", callee); 1014 else if ((TYPE_PRECISION (TREE_TYPE (type)) 1015 == TYPE_PRECISION (char_type_node)) 1016 || strop) 1017 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 1018 "argument to %<sizeof%> in %qD call is the same " 1019 "expression as the first source; did you mean to " 1020 "provide an explicit length?", callee); 1021 else 1022 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 1023 "argument to %<sizeof%> in %qD call is the same " 1024 "expression as the first source; did you mean to " 1025 "dereference it?", callee); 1026 return; 1027 } 1028 1029 if (POINTER_TYPE_P (TREE_TYPE (dest)) 1030 && !strop 1031 && comp_types (TREE_TYPE (dest), type) 1032 && !VOID_TYPE_P (TREE_TYPE (type))) 1033 { 1034 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 1035 "argument to %<sizeof%> in %qD call is the same " 1036 "pointer type %qT as the first source; expected %qT " 1037 "or an explicit length", callee, TREE_TYPE (dest), 1038 TREE_TYPE (TREE_TYPE (dest))); 1039 return; 1040 } 1041 } 1042 1043 if (src) 1044 { 1045 if (!TYPE_P (sizeof_arg[idx]) 1046 && operand_equal_p (src, sizeof_arg[idx], 0) 1047 && comp_types (TREE_TYPE (src), type)) 1048 { 1049 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) 1050 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 1051 "argument to %<sizeof%> in %qD call is the same " 1052 "expression as the second source; did you mean to " 1053 "remove the addressof?", callee); 1054 else if ((TYPE_PRECISION (TREE_TYPE (type)) 1055 == TYPE_PRECISION (char_type_node)) 1056 || strop) 1057 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 1058 "argument to %<sizeof%> in %qD call is the same " 1059 "expression as the second source; did you mean to " 1060 "provide an explicit length?", callee); 1061 else 1062 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 1063 "argument to %<sizeof%> in %qD call is the same " 1064 "expression as the second source; did you mean to " 1065 "dereference it?", callee); 1066 return; 1067 } 1068 1069 if (POINTER_TYPE_P (TREE_TYPE (src)) 1070 && !strop 1071 && comp_types (TREE_TYPE (src), type) 1072 && !VOID_TYPE_P (TREE_TYPE (type))) 1073 { 1074 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 1075 "argument to %<sizeof%> in %qD call is the same " 1076 "pointer type %qT as the second source; expected %qT " 1077 "or an explicit length", callee, TREE_TYPE (src), 1078 TREE_TYPE (TREE_TYPE (src))); 1079 return; 1080 } 1081 } 1082 1083} 1084 1085/* Warn for unlikely, improbable, or stupid DECL declarations 1086 of `main'. */ 1087 1088void 1089check_main_parameter_types (tree decl) 1090{ 1091 function_args_iterator iter; 1092 tree type; 1093 int argct = 0; 1094 1095 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter) 1096 { 1097 /* XXX void_type_node belies the abstraction. */ 1098 if (type == void_type_node || type == error_mark_node) 1099 break; 1100 1101 tree t = type; 1102 if (TYPE_ATOMIC (t)) 1103 pedwarn (input_location, OPT_Wmain, 1104 "%<_Atomic%>-qualified parameter type %qT of %q+D", 1105 type, decl); 1106 while (POINTER_TYPE_P (t)) 1107 { 1108 t = TREE_TYPE (t); 1109 if (TYPE_ATOMIC (t)) 1110 pedwarn (input_location, OPT_Wmain, 1111 "%<_Atomic%>-qualified parameter type %qT of %q+D", 1112 type, decl); 1113 } 1114 1115 ++argct; 1116 switch (argct) 1117 { 1118 case 1: 1119 if (TYPE_MAIN_VARIANT (type) != integer_type_node) 1120 pedwarn (input_location, OPT_Wmain, 1121 "first argument of %q+D should be %<int%>", decl); 1122 break; 1123 1124 case 2: 1125 if (TREE_CODE (type) != POINTER_TYPE 1126 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE 1127 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) 1128 != char_type_node)) 1129 pedwarn (input_location, OPT_Wmain, 1130 "second argument of %q+D should be %<char **%>", decl); 1131 break; 1132 1133 case 3: 1134 if (TREE_CODE (type) != POINTER_TYPE 1135 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE 1136 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) 1137 != char_type_node)) 1138 pedwarn (input_location, OPT_Wmain, 1139 "third argument of %q+D should probably be " 1140 "%<char **%>", decl); 1141 break; 1142 } 1143 } 1144 1145 /* It is intentional that this message does not mention the third 1146 argument because it's only mentioned in an appendix of the 1147 standard. */ 1148 if (argct > 0 && (argct < 2 || argct > 3)) 1149 pedwarn (input_location, OPT_Wmain, 1150 "%q+D takes only zero or two arguments", decl); 1151 1152 if (stdarg_p (TREE_TYPE (decl))) 1153 pedwarn (input_location, OPT_Wmain, 1154 "%q+D declared as variadic function", decl); 1155} 1156 1157/* Warns if the conversion of EXPR to TYPE may alter a value. 1158 This is a helper function for warnings_for_convert_and_check. */ 1159 1160static void 1161conversion_warning (location_t loc, tree type, tree expr, tree result) 1162{ 1163 tree expr_type = TREE_TYPE (expr); 1164 enum conversion_safety conversion_kind; 1165 1166 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion) 1167 return; 1168 1169 /* This may happen, because for LHS op= RHS we preevaluate 1170 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which 1171 means we could no longer see the code of the EXPR. */ 1172 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR) 1173 expr = C_MAYBE_CONST_EXPR_EXPR (expr); 1174 if (TREE_CODE (expr) == SAVE_EXPR) 1175 expr = TREE_OPERAND (expr, 0); 1176 1177 switch (TREE_CODE (expr)) 1178 { 1179 case EQ_EXPR: 1180 case NE_EXPR: 1181 case LE_EXPR: 1182 case GE_EXPR: 1183 case LT_EXPR: 1184 case GT_EXPR: 1185 case TRUTH_ANDIF_EXPR: 1186 case TRUTH_ORIF_EXPR: 1187 case TRUTH_AND_EXPR: 1188 case TRUTH_OR_EXPR: 1189 case TRUTH_XOR_EXPR: 1190 case TRUTH_NOT_EXPR: 1191 /* Conversion from boolean to a signed:1 bit-field (which only 1192 can hold the values 0 and -1) doesn't lose information - but 1193 it does change the value. */ 1194 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type)) 1195 warning_at (loc, OPT_Wconversion, 1196 "conversion to %qT from boolean expression", type); 1197 return; 1198 1199 case REAL_CST: 1200 case INTEGER_CST: 1201 case COMPLEX_CST: 1202 { 1203 conversion_kind = unsafe_conversion_p (loc, type, expr, result, true); 1204 int warnopt; 1205 if (conversion_kind == UNSAFE_REAL) 1206 warnopt = OPT_Wfloat_conversion; 1207 else if (conversion_kind) 1208 warnopt = OPT_Wconversion; 1209 else 1210 break; 1211 1212 if (TREE_CODE_CLASS (TREE_CODE (result)) == tcc_constant) 1213 warning_at (loc, warnopt, 1214 "conversion from %qT to %qT changes value from %qE to %qE", 1215 expr_type, type, expr, result); 1216 else 1217 warning_at (loc, warnopt, 1218 "conversion from %qT to %qT changes the value of %qE", 1219 expr_type, type, expr); 1220 break; 1221 } 1222 case COND_EXPR: 1223 { 1224 /* In case of COND_EXPR, we do not care about the type of 1225 COND_EXPR, only about the conversion of each operand. */ 1226 tree op1 = TREE_OPERAND (expr, 1); 1227 tree op2 = TREE_OPERAND (expr, 2); 1228 1229 conversion_warning (loc, type, op1, result); 1230 conversion_warning (loc, type, op2, result); 1231 return; 1232 } 1233 1234 default: /* 'expr' is not a constant. */ 1235 conversion_kind = unsafe_conversion_p (loc, type, expr, result, true); 1236 if (conversion_kind == UNSAFE_IMAGINARY) 1237 warning_at (loc, OPT_Wconversion, 1238 "conversion from %qT to %qT discards imaginary component", 1239 expr_type, type); 1240 else 1241 { 1242 int warnopt; 1243 if (conversion_kind == UNSAFE_REAL) 1244 warnopt = OPT_Wfloat_conversion; 1245 else if (conversion_kind) 1246 warnopt = OPT_Wconversion; 1247 else 1248 break; 1249 warning_at (loc, warnopt, 1250 "conversion from %qT to %qT may change value", 1251 expr_type, type); 1252 } 1253 } 1254} 1255 1256/* Produce warnings after a conversion. RESULT is the result of 1257 converting EXPR to TYPE. This is a helper function for 1258 convert_and_check and cp_convert_and_check. */ 1259 1260void 1261warnings_for_convert_and_check (location_t loc, tree type, tree expr, 1262 tree result) 1263{ 1264 loc = expansion_point_location_if_in_system_header (loc); 1265 1266 bool cst = TREE_CODE_CLASS (TREE_CODE (result)) == tcc_constant; 1267 1268 tree exprtype = TREE_TYPE (expr); 1269 1270 if (TREE_CODE (expr) == INTEGER_CST 1271 && (TREE_CODE (type) == INTEGER_TYPE 1272 || TREE_CODE (type) == ENUMERAL_TYPE) 1273 && !int_fits_type_p (expr, type)) 1274 { 1275 /* Do not diagnose overflow in a constant expression merely 1276 because a conversion overflowed. */ 1277 if (TREE_OVERFLOW (result)) 1278 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr); 1279 1280 if (TYPE_UNSIGNED (type)) 1281 { 1282 /* This detects cases like converting -129 or 256 to 1283 unsigned char. */ 1284 if (!int_fits_type_p (expr, c_common_signed_type (type))) 1285 { 1286 if (cst) 1287 warning_at (loc, OPT_Woverflow, 1288 (TYPE_UNSIGNED (exprtype) 1289 ? G_("conversion from %qT to %qT " 1290 "changes value from %qE to %qE") 1291 : G_("unsigned conversion from %qT to %qT " 1292 "changes value from %qE to %qE")), 1293 exprtype, type, expr, result); 1294 else 1295 warning_at (loc, OPT_Woverflow, 1296 (TYPE_UNSIGNED (exprtype) 1297 ? G_("conversion from %qT to %qT " 1298 "changes the value of %qE") 1299 : G_("unsigned conversion from %qT to %qT " 1300 "changes the value of %qE")), 1301 exprtype, type, expr); 1302 } 1303 else 1304 conversion_warning (loc, type, expr, result); 1305 } 1306 else if (!int_fits_type_p (expr, c_common_unsigned_type (type))) 1307 { 1308 if (cst) 1309 warning_at (loc, OPT_Woverflow, 1310 "overflow in conversion from %qT to %qT " 1311 "changes value from %qE to %qE", 1312 exprtype, type, expr, result); 1313 else 1314 warning_at (loc, OPT_Woverflow, 1315 "overflow in conversion from %qT to %qT " 1316 "changes the value of %qE", 1317 exprtype, type, expr); 1318 } 1319 /* No warning for converting 0x80000000 to int. */ 1320 else if (pedantic 1321 && (TREE_CODE (exprtype) != INTEGER_TYPE 1322 || TYPE_PRECISION (exprtype) 1323 != TYPE_PRECISION (type))) 1324 { 1325 if (cst) 1326 warning_at (loc, OPT_Woverflow, 1327 "overflow in conversion from %qT to %qT " 1328 "changes value from %qE to %qE", 1329 exprtype, type, expr, result); 1330 else 1331 warning_at (loc, OPT_Woverflow, 1332 "overflow in conversion from %qT to %qT " 1333 "changes the value of %qE", 1334 exprtype, type, expr); 1335 } 1336 else 1337 conversion_warning (loc, type, expr, result); 1338 } 1339 else if ((TREE_CODE (result) == INTEGER_CST 1340 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result)) 1341 { 1342 if (cst) 1343 warning_at (loc, OPT_Woverflow, 1344 "overflow in conversion from %qT to %qT " 1345 "changes value from %qE to %qE", 1346 exprtype, type, expr, result); 1347 else 1348 warning_at (loc, OPT_Woverflow, 1349 "overflow in conversion from %qT to %qT " 1350 "changes the value of %qE", 1351 exprtype, type, expr); 1352 } 1353 else 1354 conversion_warning (loc, type, expr, result); 1355} 1356 1357/* Subroutines of c_do_switch_warnings, called via splay_tree_foreach. 1358 Used to verify that case values match up with enumerator values. */ 1359 1360static void 1361match_case_to_enum_1 (tree key, tree type, tree label) 1362{ 1363 /* Avoid warning about enums that have no enumerators. */ 1364 if (TYPE_VALUES (type) == NULL_TREE) 1365 return; 1366 1367 char buf[WIDE_INT_PRINT_BUFFER_SIZE]; 1368 1369 if (tree_fits_uhwi_p (key)) 1370 print_dec (wi::to_wide (key), buf, UNSIGNED); 1371 else if (tree_fits_shwi_p (key)) 1372 print_dec (wi::to_wide (key), buf, SIGNED); 1373 else 1374 print_hex (wi::to_wide (key), buf); 1375 1376 if (TYPE_NAME (type) == NULL_TREE) 1377 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)), 1378 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum, 1379 "case value %qs not in enumerated type", 1380 buf); 1381 else 1382 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)), 1383 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum, 1384 "case value %qs not in enumerated type %qT", 1385 buf, type); 1386} 1387 1388/* Subroutine of c_do_switch_warnings, called via splay_tree_foreach. 1389 Used to verify that case values match up with enumerator values. */ 1390 1391static int 1392match_case_to_enum (splay_tree_node node, void *data) 1393{ 1394 tree label = (tree) node->value; 1395 tree type = (tree) data; 1396 1397 /* Skip default case. */ 1398 if (!CASE_LOW (label)) 1399 return 0; 1400 1401 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear 1402 when we did our enum->case scan. Reset our scratch bit after. */ 1403 if (!CASE_LOW_SEEN (label)) 1404 match_case_to_enum_1 (CASE_LOW (label), type, label); 1405 else 1406 CASE_LOW_SEEN (label) = 0; 1407 1408 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is 1409 not set, that means that CASE_HIGH did not appear when we did our 1410 enum->case scan. Reset our scratch bit after. */ 1411 if (CASE_HIGH (label)) 1412 { 1413 if (!CASE_HIGH_SEEN (label)) 1414 match_case_to_enum_1 (CASE_HIGH (label), type, label); 1415 else 1416 CASE_HIGH_SEEN (label) = 0; 1417 } 1418 1419 return 0; 1420} 1421 1422/* Handle -Wswitch*. Called from the front end after parsing the 1423 switch construct. */ 1424/* ??? Should probably be somewhere generic, since other languages 1425 besides C and C++ would want this. At the moment, however, C/C++ 1426 are the only tree-ssa languages that support enumerations at all, 1427 so the point is moot. */ 1428 1429void 1430c_do_switch_warnings (splay_tree cases, location_t switch_location, 1431 tree type, tree cond, bool bool_cond_p) 1432{ 1433 splay_tree_node default_node; 1434 splay_tree_node node; 1435 tree chain; 1436 bool outside_range_p = false; 1437 1438 if (type != error_mark_node 1439 && type != TREE_TYPE (cond) 1440 && INTEGRAL_TYPE_P (type) 1441 && INTEGRAL_TYPE_P (TREE_TYPE (cond)) 1442 && (!tree_int_cst_equal (TYPE_MIN_VALUE (type), 1443 TYPE_MIN_VALUE (TREE_TYPE (cond))) 1444 || !tree_int_cst_equal (TYPE_MAX_VALUE (type), 1445 TYPE_MAX_VALUE (TREE_TYPE (cond))))) 1446 { 1447 tree min_value = TYPE_MIN_VALUE (type); 1448 tree max_value = TYPE_MAX_VALUE (type); 1449 1450 node = splay_tree_predecessor (cases, (splay_tree_key) min_value); 1451 if (node && node->key) 1452 { 1453 outside_range_p = true; 1454 /* There is at least one case smaller than TYPE's minimum value. 1455 NODE itself could be still a range overlapping the valid values, 1456 but any predecessors thereof except the default case will be 1457 completely outside of range. */ 1458 if (CASE_HIGH ((tree) node->value) 1459 && tree_int_cst_compare (CASE_HIGH ((tree) node->value), 1460 min_value) >= 0) 1461 { 1462 location_t loc = EXPR_LOCATION ((tree) node->value); 1463 warning_at (loc, 0, "lower value in case label range" 1464 " less than minimum value for type"); 1465 CASE_LOW ((tree) node->value) = convert (TREE_TYPE (cond), 1466 min_value); 1467 node->key = (splay_tree_key) CASE_LOW ((tree) node->value); 1468 } 1469 /* All the following ones are completely outside of range. */ 1470 do 1471 { 1472 node = splay_tree_predecessor (cases, 1473 (splay_tree_key) min_value); 1474 if (node == NULL || !node->key) 1475 break; 1476 location_t loc = EXPR_LOCATION ((tree) node->value); 1477 warning_at (loc, 0, "case label value is less than minimum " 1478 "value for type"); 1479 splay_tree_remove (cases, node->key); 1480 } 1481 while (1); 1482 } 1483 node = splay_tree_lookup (cases, (splay_tree_key) max_value); 1484 if (node == NULL) 1485 node = splay_tree_predecessor (cases, (splay_tree_key) max_value); 1486 /* Handle a single node that might partially overlap the range. */ 1487 if (node 1488 && node->key 1489 && CASE_HIGH ((tree) node->value) 1490 && tree_int_cst_compare (CASE_HIGH ((tree) node->value), 1491 max_value) > 0) 1492 { 1493 location_t loc = EXPR_LOCATION ((tree) node->value); 1494 warning_at (loc, 0, "upper value in case label range" 1495 " exceeds maximum value for type"); 1496 CASE_HIGH ((tree) node->value) 1497 = convert (TREE_TYPE (cond), max_value); 1498 outside_range_p = true; 1499 } 1500 /* And any nodes that are completely outside of the range. */ 1501 while ((node = splay_tree_successor (cases, 1502 (splay_tree_key) max_value)) 1503 != NULL) 1504 { 1505 location_t loc = EXPR_LOCATION ((tree) node->value); 1506 warning_at (loc, 0, 1507 "case label value exceeds maximum value for type"); 1508 splay_tree_remove (cases, node->key); 1509 outside_range_p = true; 1510 } 1511 } 1512 1513 if (!warn_switch && !warn_switch_enum && !warn_switch_default 1514 && !warn_switch_bool) 1515 return; 1516 1517 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL); 1518 if (!default_node) 1519 warning_at (switch_location, OPT_Wswitch_default, 1520 "switch missing default case"); 1521 1522 /* There are certain cases where -Wswitch-bool warnings aren't 1523 desirable, such as 1524 switch (boolean) 1525 { 1526 case true: ... 1527 case false: ... 1528 } 1529 so be careful here. */ 1530 if (warn_switch_bool && bool_cond_p) 1531 { 1532 splay_tree_node min_node; 1533 /* If there's a default node, it's also the value with the minimal 1534 key. So look at the penultimate key (if any). */ 1535 if (default_node) 1536 min_node = splay_tree_successor (cases, (splay_tree_key) NULL); 1537 else 1538 min_node = splay_tree_min (cases); 1539 tree min = min_node ? (tree) min_node->key : NULL_TREE; 1540 1541 splay_tree_node max_node = splay_tree_max (cases); 1542 /* This might be a case range, so look at the value with the 1543 maximal key and then check CASE_HIGH. */ 1544 tree max = max_node ? (tree) max_node->value : NULL_TREE; 1545 if (max) 1546 max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max); 1547 1548 /* If there's a case value > 1 or < 0, that is outside bool 1549 range, warn. */ 1550 if (outside_range_p 1551 || (max && wi::gts_p (wi::to_wide (max), 1)) 1552 || (min && wi::lts_p (wi::to_wide (min), 0)) 1553 /* And handle the 1554 switch (boolean) 1555 { 1556 case true: ... 1557 case false: ... 1558 default: ... 1559 } 1560 case, where we want to warn. */ 1561 || (default_node 1562 && max && wi::to_wide (max) == 1 1563 && min && wi::to_wide (min) == 0)) 1564 warning_at (switch_location, OPT_Wswitch_bool, 1565 "switch condition has boolean value"); 1566 } 1567 1568 /* From here on, we only care about enumerated types. */ 1569 if (!type || TREE_CODE (type) != ENUMERAL_TYPE) 1570 return; 1571 1572 /* From here on, we only care about -Wswitch and -Wswitch-enum. */ 1573 if (!warn_switch_enum && !warn_switch) 1574 return; 1575 1576 /* Check the cases. Warn about case values which are not members of 1577 the enumerated type. For -Wswitch-enum, or for -Wswitch when 1578 there is no default case, check that exactly all enumeration 1579 literals are covered by the cases. */ 1580 1581 /* Clearing COND if it is not an integer constant simplifies 1582 the tests inside the loop below. */ 1583 if (TREE_CODE (cond) != INTEGER_CST) 1584 cond = NULL_TREE; 1585 1586 /* The time complexity here is O(N*lg(N)) worst case, but for the 1587 common case of monotonically increasing enumerators, it is 1588 O(N), since the nature of the splay tree will keep the next 1589 element adjacent to the root at all times. */ 1590 1591 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain)) 1592 { 1593 tree value = TREE_VALUE (chain); 1594 if (TREE_CODE (value) == CONST_DECL) 1595 value = DECL_INITIAL (value); 1596 node = splay_tree_lookup (cases, (splay_tree_key) value); 1597 if (node) 1598 { 1599 /* Mark the CASE_LOW part of the case entry as seen. */ 1600 tree label = (tree) node->value; 1601 CASE_LOW_SEEN (label) = 1; 1602 continue; 1603 } 1604 1605 /* Even though there wasn't an exact match, there might be a 1606 case range which includes the enumerator's value. */ 1607 node = splay_tree_predecessor (cases, (splay_tree_key) value); 1608 if (node && CASE_HIGH ((tree) node->value)) 1609 { 1610 tree label = (tree) node->value; 1611 int cmp = tree_int_cst_compare (CASE_HIGH (label), value); 1612 if (cmp >= 0) 1613 { 1614 /* If we match the upper bound exactly, mark the CASE_HIGH 1615 part of the case entry as seen. */ 1616 if (cmp == 0) 1617 CASE_HIGH_SEEN (label) = 1; 1618 continue; 1619 } 1620 } 1621 1622 /* We've now determined that this enumerated literal isn't 1623 handled by the case labels of the switch statement. */ 1624 1625 /* If the switch expression is a constant, we only really care 1626 about whether that constant is handled by the switch. */ 1627 if (cond && tree_int_cst_compare (cond, value)) 1628 continue; 1629 1630 /* If there is a default_node, the only relevant option is 1631 Wswitch-enum. Otherwise, if both are enabled then we prefer 1632 to warn using -Wswitch because -Wswitch is enabled by -Wall 1633 while -Wswitch-enum is explicit. */ 1634 warning_at (switch_location, 1635 (default_node || !warn_switch 1636 ? OPT_Wswitch_enum 1637 : OPT_Wswitch), 1638 "enumeration value %qE not handled in switch", 1639 TREE_PURPOSE (chain)); 1640 } 1641 1642 /* Warn if there are case expressions that don't correspond to 1643 enumerators. This can occur since C and C++ don't enforce 1644 type-checking of assignments to enumeration variables. 1645 1646 The time complexity here is now always O(N) worst case, since 1647 we should have marked both the lower bound and upper bound of 1648 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN 1649 above. This scan also resets those fields. */ 1650 1651 splay_tree_foreach (cases, match_case_to_enum, type); 1652} 1653 1654/* Warn for A ?: C expressions (with B omitted) where A is a boolean 1655 expression, because B will always be true. */ 1656 1657void 1658warn_for_omitted_condop (location_t location, tree cond) 1659{ 1660 /* In C++ template declarations it can happen that the type is dependent 1661 and not yet known, thus TREE_TYPE (cond) == NULL_TREE. */ 1662 if (truth_value_p (TREE_CODE (cond)) 1663 || (TREE_TYPE (cond) != NULL_TREE 1664 && TREE_CODE (TREE_TYPE (cond)) == BOOLEAN_TYPE)) 1665 warning_at (location, OPT_Wparentheses, 1666 "the omitted middle operand in ?: will always be %<true%>, " 1667 "suggest explicit middle operand"); 1668} 1669 1670/* Give an error for storing into ARG, which is 'const'. USE indicates 1671 how ARG was being used. */ 1672 1673void 1674readonly_error (location_t loc, tree arg, enum lvalue_use use) 1675{ 1676 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement 1677 || use == lv_asm); 1678 STRIP_ANY_LOCATION_WRAPPER (arg); 1679 /* Using this macro rather than (for example) arrays of messages 1680 ensures that all the format strings are checked at compile 1681 time. */ 1682#define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \ 1683 : (use == lv_increment ? (I) \ 1684 : (use == lv_decrement ? (D) : (AS)))) 1685 if (TREE_CODE (arg) == COMPONENT_REF) 1686 { 1687 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0)))) 1688 error_at (loc, READONLY_MSG (G_("assignment of member " 1689 "%qD in read-only object"), 1690 G_("increment of member " 1691 "%qD in read-only object"), 1692 G_("decrement of member " 1693 "%qD in read-only object"), 1694 G_("member %qD in read-only object " 1695 "used as %<asm%> output")), 1696 TREE_OPERAND (arg, 1)); 1697 else 1698 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"), 1699 G_("increment of read-only member %qD"), 1700 G_("decrement of read-only member %qD"), 1701 G_("read-only member %qD used as %<asm%> output")), 1702 TREE_OPERAND (arg, 1)); 1703 } 1704 else if (VAR_P (arg)) 1705 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"), 1706 G_("increment of read-only variable %qD"), 1707 G_("decrement of read-only variable %qD"), 1708 G_("read-only variable %qD used as %<asm%> output")), 1709 arg); 1710 else if (TREE_CODE (arg) == PARM_DECL) 1711 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"), 1712 G_("increment of read-only parameter %qD"), 1713 G_("decrement of read-only parameter %qD"), 1714 G_("read-only parameter %qD use as %<asm%> output")), 1715 arg); 1716 else if (TREE_CODE (arg) == RESULT_DECL) 1717 { 1718 gcc_assert (c_dialect_cxx ()); 1719 error_at (loc, READONLY_MSG (G_("assignment of " 1720 "read-only named return value %qD"), 1721 G_("increment of " 1722 "read-only named return value %qD"), 1723 G_("decrement of " 1724 "read-only named return value %qD"), 1725 G_("read-only named return value %qD " 1726 "used as %<asm%>output")), 1727 arg); 1728 } 1729 else if (TREE_CODE (arg) == FUNCTION_DECL) 1730 error_at (loc, READONLY_MSG (G_("assignment of function %qD"), 1731 G_("increment of function %qD"), 1732 G_("decrement of function %qD"), 1733 G_("function %qD used as %<asm%> output")), 1734 arg); 1735 else 1736 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"), 1737 G_("increment of read-only location %qE"), 1738 G_("decrement of read-only location %qE"), 1739 G_("read-only location %qE used as %<asm%> output")), 1740 arg); 1741} 1742 1743/* Print an error message for an invalid lvalue. USE says 1744 how the lvalue is being used and so selects the error message. LOC 1745 is the location for the error. */ 1746 1747void 1748lvalue_error (location_t loc, enum lvalue_use use) 1749{ 1750 switch (use) 1751 { 1752 case lv_assign: 1753 error_at (loc, "lvalue required as left operand of assignment"); 1754 break; 1755 case lv_increment: 1756 error_at (loc, "lvalue required as increment operand"); 1757 break; 1758 case lv_decrement: 1759 error_at (loc, "lvalue required as decrement operand"); 1760 break; 1761 case lv_addressof: 1762 error_at (loc, "lvalue required as unary %<&%> operand"); 1763 break; 1764 case lv_asm: 1765 error_at (loc, "lvalue required in asm statement"); 1766 break; 1767 default: 1768 gcc_unreachable (); 1769 } 1770} 1771 1772/* Print an error message for an invalid indirection of type TYPE. 1773 ERRSTRING is the name of the operator for the indirection. */ 1774 1775void 1776invalid_indirection_error (location_t loc, tree type, ref_operator errstring) 1777{ 1778 switch (errstring) 1779 { 1780 case RO_NULL: 1781 gcc_assert (c_dialect_cxx ()); 1782 error_at (loc, "invalid type argument (have %qT)", type); 1783 break; 1784 case RO_ARRAY_INDEXING: 1785 error_at (loc, 1786 "invalid type argument of array indexing (have %qT)", 1787 type); 1788 break; 1789 case RO_UNARY_STAR: 1790 error_at (loc, 1791 "invalid type argument of unary %<*%> (have %qT)", 1792 type); 1793 break; 1794 case RO_ARROW: 1795 error_at (loc, 1796 "invalid type argument of %<->%> (have %qT)", 1797 type); 1798 break; 1799 case RO_ARROW_STAR: 1800 error_at (loc, 1801 "invalid type argument of %<->*%> (have %qT)", 1802 type); 1803 break; 1804 case RO_IMPLICIT_CONVERSION: 1805 error_at (loc, 1806 "invalid type argument of implicit conversion (have %qT)", 1807 type); 1808 break; 1809 default: 1810 gcc_unreachable (); 1811 } 1812} 1813 1814/* Subscripting with type char is likely to lose on a machine where 1815 chars are signed. So warn on any machine, but optionally. Don't 1816 warn for unsigned char since that type is safe. Don't warn for 1817 signed char because anyone who uses that must have done so 1818 deliberately. Furthermore, we reduce the false positive load by 1819 warning only for non-constant value of type char. 1820 LOC is the location of the subscripting expression. */ 1821 1822void 1823warn_array_subscript_with_type_char (location_t loc, tree index) 1824{ 1825 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node) 1826 { 1827 /* If INDEX has a location, use it; otherwise use LOC (the location 1828 of the subscripting expression as a whole). */ 1829 loc = EXPR_LOC_OR_LOC (index, loc); 1830 STRIP_ANY_LOCATION_WRAPPER (index); 1831 if (TREE_CODE (index) != INTEGER_CST) 1832 warning_at (loc, OPT_Wchar_subscripts, 1833 "array subscript has type %<char%>"); 1834 } 1835} 1836 1837/* Implement -Wparentheses for the unexpected C precedence rules, to 1838 cover cases like x + y << z which readers are likely to 1839 misinterpret. We have seen an expression in which CODE is a binary 1840 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which 1841 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and 1842 CODE_RIGHT may be ERROR_MARK, which means that that side of the 1843 expression was not formed using a binary or unary operator, or it 1844 was enclosed in parentheses. */ 1845 1846void 1847warn_about_parentheses (location_t loc, enum tree_code code, 1848 enum tree_code code_left, tree arg_left, 1849 enum tree_code code_right, tree arg_right) 1850{ 1851 if (!warn_parentheses) 1852 return; 1853 1854 /* This macro tests that the expression ARG with original tree code 1855 CODE appears to be a boolean expression. or the result of folding a 1856 boolean expression. */ 1857#define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \ 1858 (truth_value_p (TREE_CODE (ARG)) \ 1859 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \ 1860 /* Folding may create 0 or 1 integers from other expressions. */ \ 1861 || ((CODE) != INTEGER_CST \ 1862 && (integer_onep (ARG) || integer_zerop (ARG)))) 1863 1864 switch (code) 1865 { 1866 case LSHIFT_EXPR: 1867 if (code_left == PLUS_EXPR) 1868 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1869 "suggest parentheses around %<+%> inside %<<<%>"); 1870 else if (code_right == PLUS_EXPR) 1871 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1872 "suggest parentheses around %<+%> inside %<<<%>"); 1873 else if (code_left == MINUS_EXPR) 1874 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1875 "suggest parentheses around %<-%> inside %<<<%>"); 1876 else if (code_right == MINUS_EXPR) 1877 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1878 "suggest parentheses around %<-%> inside %<<<%>"); 1879 return; 1880 1881 case RSHIFT_EXPR: 1882 if (code_left == PLUS_EXPR) 1883 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1884 "suggest parentheses around %<+%> inside %<>>%>"); 1885 else if (code_right == PLUS_EXPR) 1886 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1887 "suggest parentheses around %<+%> inside %<>>%>"); 1888 else if (code_left == MINUS_EXPR) 1889 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1890 "suggest parentheses around %<-%> inside %<>>%>"); 1891 else if (code_right == MINUS_EXPR) 1892 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1893 "suggest parentheses around %<-%> inside %<>>%>"); 1894 return; 1895 1896 case TRUTH_ORIF_EXPR: 1897 if (code_left == TRUTH_ANDIF_EXPR) 1898 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1899 "suggest parentheses around %<&&%> within %<||%>"); 1900 else if (code_right == TRUTH_ANDIF_EXPR) 1901 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1902 "suggest parentheses around %<&&%> within %<||%>"); 1903 return; 1904 1905 case BIT_IOR_EXPR: 1906 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR 1907 || code_left == PLUS_EXPR || code_left == MINUS_EXPR) 1908 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1909 "suggest parentheses around arithmetic in operand of %<|%>"); 1910 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR 1911 || code_right == PLUS_EXPR || code_right == MINUS_EXPR) 1912 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1913 "suggest parentheses around arithmetic in operand of %<|%>"); 1914 /* Check cases like x|y==z */ 1915 else if (TREE_CODE_CLASS (code_left) == tcc_comparison) 1916 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1917 "suggest parentheses around comparison in operand of %<|%>"); 1918 else if (TREE_CODE_CLASS (code_right) == tcc_comparison) 1919 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1920 "suggest parentheses around comparison in operand of %<|%>"); 1921 /* Check cases like !x | y */ 1922 else if (code_left == TRUTH_NOT_EXPR 1923 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right)) 1924 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1925 "suggest parentheses around operand of " 1926 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>"); 1927 return; 1928 1929 case BIT_XOR_EXPR: 1930 if (code_left == BIT_AND_EXPR 1931 || code_left == PLUS_EXPR || code_left == MINUS_EXPR) 1932 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1933 "suggest parentheses around arithmetic in operand of %<^%>"); 1934 else if (code_right == BIT_AND_EXPR 1935 || code_right == PLUS_EXPR || code_right == MINUS_EXPR) 1936 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1937 "suggest parentheses around arithmetic in operand of %<^%>"); 1938 /* Check cases like x^y==z */ 1939 else if (TREE_CODE_CLASS (code_left) == tcc_comparison) 1940 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1941 "suggest parentheses around comparison in operand of %<^%>"); 1942 else if (TREE_CODE_CLASS (code_right) == tcc_comparison) 1943 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1944 "suggest parentheses around comparison in operand of %<^%>"); 1945 return; 1946 1947 case BIT_AND_EXPR: 1948 if (code_left == PLUS_EXPR) 1949 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1950 "suggest parentheses around %<+%> in operand of %<&%>"); 1951 else if (code_right == PLUS_EXPR) 1952 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1953 "suggest parentheses around %<+%> in operand of %<&%>"); 1954 else if (code_left == MINUS_EXPR) 1955 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1956 "suggest parentheses around %<-%> in operand of %<&%>"); 1957 else if (code_right == MINUS_EXPR) 1958 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1959 "suggest parentheses around %<-%> in operand of %<&%>"); 1960 /* Check cases like x&y==z */ 1961 else if (TREE_CODE_CLASS (code_left) == tcc_comparison) 1962 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1963 "suggest parentheses around comparison in operand of %<&%>"); 1964 else if (TREE_CODE_CLASS (code_right) == tcc_comparison) 1965 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1966 "suggest parentheses around comparison in operand of %<&%>"); 1967 /* Check cases like !x & y */ 1968 else if (code_left == TRUTH_NOT_EXPR 1969 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right)) 1970 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1971 "suggest parentheses around operand of " 1972 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>"); 1973 return; 1974 1975 case EQ_EXPR: 1976 if (TREE_CODE_CLASS (code_left) == tcc_comparison) 1977 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1978 "suggest parentheses around comparison in operand of %<==%>"); 1979 else if (TREE_CODE_CLASS (code_right) == tcc_comparison) 1980 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1981 "suggest parentheses around comparison in operand of %<==%>"); 1982 return; 1983 case NE_EXPR: 1984 if (TREE_CODE_CLASS (code_left) == tcc_comparison) 1985 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1986 "suggest parentheses around comparison in operand of %<!=%>"); 1987 else if (TREE_CODE_CLASS (code_right) == tcc_comparison) 1988 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1989 "suggest parentheses around comparison in operand of %<!=%>"); 1990 return; 1991 1992 default: 1993 if (TREE_CODE_CLASS (code) == tcc_comparison) 1994 { 1995 if (TREE_CODE_CLASS (code_left) == tcc_comparison 1996 && code_left != NE_EXPR && code_left != EQ_EXPR 1997 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left))) 1998 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1999 "comparisons like %<X<=Y<=Z%> do not " 2000 "have their mathematical meaning"); 2001 else if (TREE_CODE_CLASS (code_right) == tcc_comparison 2002 && code_right != NE_EXPR && code_right != EQ_EXPR 2003 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right))) 2004 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 2005 "comparisons like %<X<=Y<=Z%> do not " 2006 "have their mathematical meaning"); 2007 } 2008 return; 2009 } 2010#undef NOT_A_BOOLEAN_EXPR_P 2011} 2012 2013/* If LABEL (a LABEL_DECL) has not been used, issue a warning. */ 2014 2015void 2016warn_for_unused_label (tree label) 2017{ 2018 if (!TREE_USED (label)) 2019 { 2020 if (DECL_INITIAL (label)) 2021 warning (OPT_Wunused_label, "label %q+D defined but not used", label); 2022 else 2023 warning (OPT_Wunused_label, "label %q+D declared but not defined", label); 2024 } 2025 else if (asan_sanitize_use_after_scope ()) 2026 { 2027 if (asan_used_labels == NULL) 2028 asan_used_labels = new hash_set<tree> (16); 2029 2030 asan_used_labels->add (label); 2031 } 2032} 2033 2034/* Warn for division by zero according to the value of DIVISOR. LOC 2035 is the location of the division operator. */ 2036 2037void 2038warn_for_div_by_zero (location_t loc, tree divisor) 2039{ 2040 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning 2041 about division by zero. Do not issue a warning if DIVISOR has a 2042 floating-point type, since we consider 0.0/0.0 a valid way of 2043 generating a NaN. */ 2044 if (c_inhibit_evaluation_warnings == 0 2045 && (integer_zerop (divisor) || fixed_zerop (divisor))) 2046 warning_at (loc, OPT_Wdiv_by_zero, "division by zero"); 2047} 2048 2049/* Warn for patterns where memset appears to be used incorrectly. The 2050 warning location should be LOC. ARG0, and ARG2 are the first and 2051 last arguments to the call, while LITERAL_ZERO_MASK has a 1 bit for 2052 each argument that was a literal zero. */ 2053 2054void 2055warn_for_memset (location_t loc, tree arg0, tree arg2, 2056 int literal_zero_mask) 2057{ 2058 arg0 = fold_for_warn (arg0); 2059 arg2 = fold_for_warn (arg2); 2060 2061 if (warn_memset_transposed_args 2062 && integer_zerop (arg2) 2063 && (literal_zero_mask & (1 << 2)) != 0 2064 && (literal_zero_mask & (1 << 1)) == 0) 2065 warning_at (loc, OPT_Wmemset_transposed_args, 2066 "%<memset%> used with constant zero length " 2067 "parameter; this could be due to transposed " 2068 "parameters"); 2069 2070 if (warn_memset_elt_size && TREE_CODE (arg2) == INTEGER_CST) 2071 { 2072 STRIP_NOPS (arg0); 2073 if (TREE_CODE (arg0) == ADDR_EXPR) 2074 arg0 = TREE_OPERAND (arg0, 0); 2075 tree type = TREE_TYPE (arg0); 2076 if (type != NULL_TREE && TREE_CODE (type) == ARRAY_TYPE) 2077 { 2078 tree elt_type = TREE_TYPE (type); 2079 tree domain = TYPE_DOMAIN (type); 2080 if (COMPLETE_TYPE_P (elt_type) 2081 && !integer_onep (TYPE_SIZE_UNIT (elt_type)) 2082 && domain != NULL_TREE 2083 && TYPE_MAX_VALUE (domain) 2084 && TYPE_MIN_VALUE (domain) 2085 && integer_zerop (TYPE_MIN_VALUE (domain)) 2086 && integer_onep (fold_build2 (MINUS_EXPR, domain, 2087 arg2, 2088 TYPE_MAX_VALUE (domain)))) 2089 warning_at (loc, OPT_Wmemset_elt_size, 2090 "%<memset%> used with length equal to " 2091 "number of elements without multiplication " 2092 "by element size"); 2093 } 2094 } 2095} 2096 2097/* Subroutine of build_binary_op. Give warnings for comparisons 2098 between signed and unsigned quantities that may fail. Do the 2099 checking based on the original operand trees ORIG_OP0 and ORIG_OP1, 2100 so that casts will be considered, but default promotions won't 2101 be. 2102 2103 LOCATION is the location of the comparison operator. 2104 2105 The arguments of this function map directly to local variables 2106 of build_binary_op. */ 2107 2108void 2109warn_for_sign_compare (location_t location, 2110 tree orig_op0, tree orig_op1, 2111 tree op0, tree op1, 2112 tree result_type, enum tree_code resultcode) 2113{ 2114 if (error_operand_p (orig_op0) || error_operand_p (orig_op1)) 2115 return; 2116 2117 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0)); 2118 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1)); 2119 int unsignedp0, unsignedp1; 2120 2121 /* In C++, check for comparison of different enum types. */ 2122 if (c_dialect_cxx() 2123 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE 2124 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE 2125 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0)) 2126 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1))) 2127 { 2128 warning_at (location, 2129 OPT_Wsign_compare, "comparison between types %qT and %qT", 2130 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1)); 2131 } 2132 2133 /* Do not warn if the comparison is being done in a signed type, 2134 since the signed type will only be chosen if it can represent 2135 all the values of the unsigned type. */ 2136 if (!TYPE_UNSIGNED (result_type)) 2137 /* OK */; 2138 /* Do not warn if both operands are unsigned. */ 2139 else if (op0_signed == op1_signed) 2140 /* OK */; 2141 else 2142 { 2143 tree sop, uop, base_type; 2144 bool ovf; 2145 2146 if (op0_signed) 2147 sop = orig_op0, uop = orig_op1; 2148 else 2149 sop = orig_op1, uop = orig_op0; 2150 2151 sop = fold_for_warn (sop); 2152 uop = fold_for_warn (uop); 2153 2154 STRIP_TYPE_NOPS (sop); 2155 STRIP_TYPE_NOPS (uop); 2156 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE 2157 ? TREE_TYPE (result_type) : result_type); 2158 2159 /* Do not warn if the signed quantity is an unsuffixed integer 2160 literal (or some static constant expression involving such 2161 literals or a conditional expression involving such literals) 2162 and it is non-negative. */ 2163 if (tree_expr_nonnegative_warnv_p (sop, &ovf)) 2164 /* OK */; 2165 /* Do not warn if the comparison is an equality operation, the 2166 unsigned quantity is an integral constant, and it would fit 2167 in the result if the result were signed. */ 2168 else if (TREE_CODE (uop) == INTEGER_CST 2169 && (resultcode == EQ_EXPR || resultcode == NE_EXPR) 2170 && int_fits_type_p (uop, c_common_signed_type (base_type))) 2171 /* OK */; 2172 /* In C, do not warn if the unsigned quantity is an enumeration 2173 constant and its maximum value would fit in the result if the 2174 result were signed. */ 2175 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST 2176 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE 2177 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)), 2178 c_common_signed_type (base_type))) 2179 /* OK */; 2180 else 2181 warning_at (location, OPT_Wsign_compare, 2182 "comparison of integer expressions of different " 2183 "signedness: %qT and %qT", TREE_TYPE (orig_op0), 2184 TREE_TYPE (orig_op1)); 2185 } 2186 2187 /* Warn if two unsigned values are being compared in a size larger 2188 than their original size, and one (and only one) is the result of 2189 a `~' operator. This comparison will always fail. 2190 2191 Also warn if one operand is a constant, and the constant does not 2192 have all bits set that are set in the ~ operand when it is 2193 extended. */ 2194 2195 op0 = c_common_get_narrower (op0, &unsignedp0); 2196 op1 = c_common_get_narrower (op1, &unsignedp1); 2197 2198 if ((TREE_CODE (op0) == BIT_NOT_EXPR) 2199 ^ (TREE_CODE (op1) == BIT_NOT_EXPR)) 2200 { 2201 if (TREE_CODE (op0) == BIT_NOT_EXPR) 2202 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0); 2203 if (TREE_CODE (op1) == BIT_NOT_EXPR) 2204 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1); 2205 2206 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1)) 2207 { 2208 tree primop; 2209 HOST_WIDE_INT constant, mask; 2210 int unsignedp; 2211 unsigned int bits; 2212 2213 if (tree_fits_shwi_p (op0)) 2214 { 2215 primop = op1; 2216 unsignedp = unsignedp1; 2217 constant = tree_to_shwi (op0); 2218 } 2219 else 2220 { 2221 primop = op0; 2222 unsignedp = unsignedp0; 2223 constant = tree_to_shwi (op1); 2224 } 2225 2226 bits = TYPE_PRECISION (TREE_TYPE (primop)); 2227 if (bits < TYPE_PRECISION (result_type) 2228 && bits < HOST_BITS_PER_LONG && unsignedp) 2229 { 2230 mask = HOST_WIDE_INT_M1U << bits; 2231 if ((mask & constant) != mask) 2232 { 2233 if (constant == 0) 2234 warning_at (location, OPT_Wsign_compare, 2235 "promoted ~unsigned is always non-zero"); 2236 else 2237 warning_at (location, OPT_Wsign_compare, 2238 "comparison of promoted ~unsigned with constant"); 2239 } 2240 } 2241 } 2242 else if (unsignedp0 && unsignedp1 2243 && (TYPE_PRECISION (TREE_TYPE (op0)) 2244 < TYPE_PRECISION (result_type)) 2245 && (TYPE_PRECISION (TREE_TYPE (op1)) 2246 < TYPE_PRECISION (result_type))) 2247 warning_at (location, OPT_Wsign_compare, 2248 "comparison of promoted ~unsigned with unsigned"); 2249 } 2250} 2251 2252/* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common 2253 type via c_common_type. If -Wdouble-promotion is in use, and the 2254 conditions for warning have been met, issue a warning. GMSGID is 2255 the warning message. It must have two %T specifiers for the type 2256 that was converted (generally "float") and the type to which it was 2257 converted (generally "double), respectively. LOC is the location 2258 to which the warning should refer. */ 2259 2260void 2261do_warn_double_promotion (tree result_type, tree type1, tree type2, 2262 const char *gmsgid, location_t loc) 2263{ 2264 tree source_type; 2265 2266 if (!warn_double_promotion) 2267 return; 2268 /* If the conversion will not occur at run-time, there is no need to 2269 warn about it. */ 2270 if (c_inhibit_evaluation_warnings) 2271 return; 2272 /* If an invalid conversion has occured, don't warn. */ 2273 if (result_type == error_mark_node) 2274 return; 2275 if (TYPE_MAIN_VARIANT (result_type) != double_type_node 2276 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node) 2277 return; 2278 if (TYPE_MAIN_VARIANT (type1) == float_type_node 2279 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node) 2280 source_type = type1; 2281 else if (TYPE_MAIN_VARIANT (type2) == float_type_node 2282 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node) 2283 source_type = type2; 2284 else 2285 return; 2286 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type); 2287} 2288 2289/* Possibly warn about unused parameters. */ 2290 2291void 2292do_warn_unused_parameter (tree fn) 2293{ 2294 tree decl; 2295 2296 for (decl = DECL_ARGUMENTS (fn); 2297 decl; decl = DECL_CHAIN (decl)) 2298 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL 2299 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl) 2300 && !TREE_NO_WARNING (decl)) 2301 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter, 2302 "unused parameter %qD", decl); 2303} 2304 2305/* If DECL is a typedef that is declared in the current function, 2306 record it for the purpose of -Wunused-local-typedefs. */ 2307 2308void 2309record_locally_defined_typedef (tree decl) 2310{ 2311 struct c_language_function *l; 2312 2313 if (!warn_unused_local_typedefs 2314 || cfun == NULL 2315 /* if this is not a locally defined typedef then we are not 2316 interested. */ 2317 || !is_typedef_decl (decl) 2318 || !decl_function_context (decl)) 2319 return; 2320 2321 l = (struct c_language_function *) cfun->language; 2322 vec_safe_push (l->local_typedefs, decl); 2323} 2324 2325/* If T is a TYPE_DECL declared locally, mark it as used. */ 2326 2327void 2328maybe_record_typedef_use (tree t) 2329{ 2330 if (!is_typedef_decl (t)) 2331 return; 2332 2333 TREE_USED (t) = true; 2334} 2335 2336/* Warn if there are some unused locally defined typedefs in the 2337 current function. */ 2338 2339void 2340maybe_warn_unused_local_typedefs (void) 2341{ 2342 int i; 2343 tree decl; 2344 /* The number of times we have emitted -Wunused-local-typedefs 2345 warnings. If this is different from errorcount, that means some 2346 unrelated errors have been issued. In which case, we'll avoid 2347 emitting "unused-local-typedefs" warnings. */ 2348 static int unused_local_typedefs_warn_count; 2349 struct c_language_function *l; 2350 2351 if (cfun == NULL) 2352 return; 2353 2354 if ((l = (struct c_language_function *) cfun->language) == NULL) 2355 return; 2356 2357 if (warn_unused_local_typedefs 2358 && errorcount == unused_local_typedefs_warn_count) 2359 { 2360 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl) 2361 if (!TREE_USED (decl)) 2362 warning_at (DECL_SOURCE_LOCATION (decl), 2363 OPT_Wunused_local_typedefs, 2364 "typedef %qD locally defined but not used", decl); 2365 unused_local_typedefs_warn_count = errorcount; 2366 } 2367 2368 vec_free (l->local_typedefs); 2369} 2370 2371/* If we're creating an if-else-if condition chain, first see if we 2372 already have this COND in the CHAIN. If so, warn and don't add COND 2373 into the vector, otherwise add the COND there. LOC is the location 2374 of COND. */ 2375 2376void 2377warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain) 2378{ 2379 /* No chain has been created yet. Do nothing. */ 2380 if (*chain == NULL) 2381 return; 2382 2383 if (TREE_SIDE_EFFECTS (cond)) 2384 { 2385 /* Uh-oh! This condition has a side-effect, thus invalidates 2386 the whole chain. */ 2387 delete *chain; 2388 *chain = NULL; 2389 return; 2390 } 2391 2392 unsigned int ix; 2393 tree t; 2394 bool found = false; 2395 FOR_EACH_VEC_ELT (**chain, ix, t) 2396 if (operand_equal_p (cond, t, 0)) 2397 { 2398 auto_diagnostic_group d; 2399 if (warning_at (loc, OPT_Wduplicated_cond, 2400 "duplicated %<if%> condition")) 2401 inform (EXPR_LOCATION (t), "previously used here"); 2402 found = true; 2403 break; 2404 } 2405 2406 if (!found 2407 && !CONSTANT_CLASS_P (cond) 2408 /* Don't infinitely grow the chain. */ 2409 && (*chain)->length () < 512) 2410 (*chain)->safe_push (cond); 2411} 2412 2413/* Check and possibly warn if two declarations have contradictory 2414 attributes, such as always_inline vs. noinline. */ 2415 2416bool 2417diagnose_mismatched_attributes (tree olddecl, tree newdecl) 2418{ 2419 bool warned = false; 2420 2421 tree a1 = lookup_attribute ("optimize", DECL_ATTRIBUTES (olddecl)); 2422 tree a2 = lookup_attribute ("optimize", DECL_ATTRIBUTES (newdecl)); 2423 /* An optimization attribute applied on a declaration after the 2424 definition is likely not what the user wanted. */ 2425 if (a2 != NULL_TREE 2426 && DECL_SAVED_TREE (olddecl) != NULL_TREE 2427 && (a1 == NULL_TREE || !attribute_list_equal (a1, a2))) 2428 warned |= warning (OPT_Wattributes, 2429 "optimization attribute on %qD follows " 2430 "definition but the attribute doesn%'t match", 2431 newdecl); 2432 2433 /* Diagnose inline __attribute__ ((noinline)) which is silly. */ 2434 if (DECL_DECLARED_INLINE_P (newdecl) 2435 && DECL_UNINLINABLE (olddecl) 2436 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl))) 2437 warned |= warning (OPT_Wattributes, "inline declaration of %qD follows " 2438 "declaration with attribute %<noinline%>", newdecl); 2439 else if (DECL_DECLARED_INLINE_P (olddecl) 2440 && DECL_UNINLINABLE (newdecl) 2441 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))) 2442 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute " 2443 "%<noinline%> follows inline declaration", newdecl); 2444 2445 return warned; 2446} 2447 2448/* Warn if signed left shift overflows. We don't warn 2449 about left-shifting 1 into the sign bit in C++14; cf. 2450 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457> 2451 and don't warn for C++2a at all, as signed left shifts never 2452 overflow. 2453 LOC is a location of the shift; OP0 and OP1 are the operands. 2454 Return true if an overflow is detected, false otherwise. */ 2455 2456bool 2457maybe_warn_shift_overflow (location_t loc, tree op0, tree op1) 2458{ 2459 if (TREE_CODE (op0) != INTEGER_CST 2460 || TREE_CODE (op1) != INTEGER_CST) 2461 return false; 2462 2463 tree type0 = TREE_TYPE (op0); 2464 unsigned int prec0 = TYPE_PRECISION (type0); 2465 2466 /* Left-hand operand must be signed. */ 2467 if (TYPE_UNSIGNED (type0) || cxx_dialect >= cxx2a) 2468 return false; 2469 2470 unsigned int min_prec = (wi::min_precision (wi::to_wide (op0), SIGNED) 2471 + TREE_INT_CST_LOW (op1)); 2472 /* Handle the case of left-shifting 1 into the sign bit. 2473 * However, shifting 1 _out_ of the sign bit, as in 2474 * INT_MIN << 1, is considered an overflow. 2475 */ 2476 if (!tree_int_cst_sign_bit (op0) && min_prec == prec0 + 1) 2477 { 2478 /* Never warn for C++14 onwards. */ 2479 if (cxx_dialect >= cxx14) 2480 return false; 2481 /* Otherwise only if -Wshift-overflow=2. But return 2482 true to signal an overflow for the sake of integer 2483 constant expressions. */ 2484 if (warn_shift_overflow < 2) 2485 return true; 2486 } 2487 2488 bool overflowed = min_prec > prec0; 2489 if (overflowed && c_inhibit_evaluation_warnings == 0) 2490 warning_at (loc, OPT_Wshift_overflow_, 2491 "result of %qE requires %u bits to represent, " 2492 "but %qT only has %u bits", 2493 build2_loc (loc, LSHIFT_EXPR, type0, op0, op1), 2494 min_prec, type0, prec0); 2495 2496 return overflowed; 2497} 2498 2499/* Warn about boolean expression compared with an integer value different 2500 from true/false. Warns also e.g. about "(i1 == i2) == 2". 2501 LOC is the location of the comparison, CODE is its code, OP0 and OP1 2502 are the operands of the comparison. The caller must ensure that 2503 either operand is a boolean expression. */ 2504 2505void 2506maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0, 2507 tree op1) 2508{ 2509 if (TREE_CODE_CLASS (code) != tcc_comparison) 2510 return; 2511 2512 tree f, cst; 2513 if (f = fold_for_warn (op0), 2514 TREE_CODE (f) == INTEGER_CST) 2515 cst = op0 = f; 2516 else if (f = fold_for_warn (op1), 2517 TREE_CODE (f) == INTEGER_CST) 2518 cst = op1 = f; 2519 else 2520 return; 2521 2522 if (!integer_zerop (cst) && !integer_onep (cst)) 2523 { 2524 int sign = (TREE_CODE (op0) == INTEGER_CST 2525 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst)); 2526 if (code == EQ_EXPR 2527 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0) 2528 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0)) 2529 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE " 2530 "with boolean expression is always false", cst); 2531 else 2532 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE " 2533 "with boolean expression is always true", cst); 2534 } 2535 else if (integer_zerop (cst) || integer_onep (cst)) 2536 { 2537 /* If the non-constant operand isn't of a boolean type, we 2538 don't want to warn here. */ 2539 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0; 2540 /* Handle booleans promoted to integers. */ 2541 if (bool_promoted_to_int_p (noncst)) 2542 /* Warn. */; 2543 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE 2544 && !truth_value_p (TREE_CODE (noncst))) 2545 return; 2546 /* Do some magic to get the right diagnostics. */ 2547 bool flag = TREE_CODE (op0) == INTEGER_CST; 2548 flag = integer_zerop (cst) ? flag : !flag; 2549 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag)) 2550 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE " 2551 "with boolean expression is always true", cst); 2552 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag)) 2553 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE " 2554 "with boolean expression is always false", cst); 2555 } 2556} 2557 2558/* Warn if an argument at position param_pos is passed to a 2559 restrict-qualified param, and it aliases with another argument. 2560 Return true if a warning has been issued. */ 2561 2562bool 2563warn_for_restrict (unsigned param_pos, tree *argarray, unsigned nargs) 2564{ 2565 tree arg = argarray[param_pos]; 2566 if (TREE_VISITED (arg) || integer_zerop (arg)) 2567 return false; 2568 2569 location_t loc = EXPR_LOC_OR_LOC (arg, input_location); 2570 gcc_rich_location richloc (loc); 2571 2572 unsigned i; 2573 auto_vec<int, 16> arg_positions; 2574 2575 for (i = 0; i < nargs; i++) 2576 { 2577 if (i == param_pos) 2578 continue; 2579 2580 tree current_arg = argarray[i]; 2581 if (operand_equal_p (arg, current_arg, 0)) 2582 { 2583 TREE_VISITED (current_arg) = 1; 2584 arg_positions.safe_push (i + 1); 2585 } 2586 } 2587 2588 if (arg_positions.is_empty ()) 2589 return false; 2590 2591 int pos; 2592 FOR_EACH_VEC_ELT (arg_positions, i, pos) 2593 { 2594 arg = argarray[pos - 1]; 2595 if (EXPR_HAS_LOCATION (arg)) 2596 richloc.add_range (EXPR_LOCATION (arg)); 2597 } 2598 2599 return warning_n (&richloc, OPT_Wrestrict, arg_positions.length (), 2600 "passing argument %i to restrict-qualified parameter" 2601 " aliases with argument %Z", 2602 "passing argument %i to restrict-qualified parameter" 2603 " aliases with arguments %Z", 2604 param_pos + 1, arg_positions.address (), 2605 arg_positions.length ()); 2606} 2607 2608/* Callback function to determine whether an expression TP or one of its 2609 subexpressions comes from macro expansion. Used to suppress bogus 2610 warnings. */ 2611 2612static tree 2613expr_from_macro_expansion_r (tree *tp, int *, void *) 2614{ 2615 if (CAN_HAVE_LOCATION_P (*tp) 2616 && from_macro_expansion_at (EXPR_LOCATION (*tp))) 2617 return integer_zero_node; 2618 2619 return NULL_TREE; 2620} 2621 2622/* Possibly warn when an if-else has identical branches. */ 2623 2624static void 2625do_warn_duplicated_branches (tree expr) 2626{ 2627 tree thenb = COND_EXPR_THEN (expr); 2628 tree elseb = COND_EXPR_ELSE (expr); 2629 2630 /* Don't bother if any of the branches is missing. */ 2631 if (thenb == NULL_TREE || elseb == NULL_TREE) 2632 return; 2633 2634 /* And don't warn for empty statements. */ 2635 if (TREE_CODE (thenb) == NOP_EXPR 2636 && TREE_TYPE (thenb) == void_type_node 2637 && TREE_OPERAND (thenb, 0) == size_zero_node) 2638 return; 2639 2640 /* ... or empty branches. */ 2641 if (TREE_CODE (thenb) == STATEMENT_LIST 2642 && STATEMENT_LIST_HEAD (thenb) == NULL) 2643 return; 2644 2645 /* Compute the hash of the then branch. */ 2646 inchash::hash hstate0 (0); 2647 inchash::add_expr (thenb, hstate0); 2648 hashval_t h0 = hstate0.end (); 2649 2650 /* Compute the hash of the else branch. */ 2651 inchash::hash hstate1 (0); 2652 inchash::add_expr (elseb, hstate1); 2653 hashval_t h1 = hstate1.end (); 2654 2655 /* Compare the hashes. */ 2656 if (h0 == h1 2657 && operand_equal_p (thenb, elseb, OEP_LEXICOGRAPHIC) 2658 /* Don't warn if any of the branches or their subexpressions comes 2659 from a macro. */ 2660 && !walk_tree_without_duplicates (&thenb, expr_from_macro_expansion_r, 2661 NULL) 2662 && !walk_tree_without_duplicates (&elseb, expr_from_macro_expansion_r, 2663 NULL)) 2664 warning_at (EXPR_LOCATION (expr), OPT_Wduplicated_branches, 2665 "this condition has identical branches"); 2666} 2667 2668/* Callback for c_genericize to implement -Wduplicated-branches. */ 2669 2670tree 2671do_warn_duplicated_branches_r (tree *tp, int *, void *) 2672{ 2673 if (TREE_CODE (*tp) == COND_EXPR) 2674 do_warn_duplicated_branches (*tp); 2675 return NULL_TREE; 2676} 2677 2678/* Implementation of -Wmultistatement-macros. This warning warns about 2679 cases when a macro expands to multiple statements not wrapped in 2680 do {} while (0) or ({ }) and is used as a body of if/else/for/while 2681 conditionals. For example, 2682 2683 #define DOIT x++; y++ 2684 2685 if (c) 2686 DOIT; 2687 2688 will increment y unconditionally. 2689 2690 BODY_LOC is the location of the first token in the body after labels 2691 have been parsed, NEXT_LOC is the location of the next token after the 2692 body of the conditional has been parsed, and GUARD_LOC is the location 2693 of the conditional. */ 2694 2695void 2696warn_for_multistatement_macros (location_t body_loc, location_t next_loc, 2697 location_t guard_loc, enum rid keyword) 2698{ 2699 if (!warn_multistatement_macros) 2700 return; 2701 2702 /* Ain't got time to waste. We only care about macros here. */ 2703 if (!from_macro_expansion_at (body_loc) 2704 || !from_macro_expansion_at (next_loc)) 2705 return; 2706 2707 /* Let's skip macros defined in system headers. */ 2708 if (in_system_header_at (body_loc) 2709 || in_system_header_at (next_loc)) 2710 return; 2711 2712 /* Find the actual tokens in the macro definition. BODY_LOC and 2713 NEXT_LOC have to come from the same spelling location, but they 2714 will resolve to different locations in the context of the macro 2715 definition. */ 2716 location_t body_loc_exp 2717 = linemap_resolve_location (line_table, body_loc, 2718 LRK_MACRO_DEFINITION_LOCATION, NULL); 2719 location_t next_loc_exp 2720 = linemap_resolve_location (line_table, next_loc, 2721 LRK_MACRO_DEFINITION_LOCATION, NULL); 2722 location_t guard_loc_exp 2723 = linemap_resolve_location (line_table, guard_loc, 2724 LRK_MACRO_DEFINITION_LOCATION, NULL); 2725 2726 /* These are some funky cases we don't want to warn about. */ 2727 if (body_loc_exp == guard_loc_exp 2728 || next_loc_exp == guard_loc_exp 2729 || body_loc_exp == next_loc_exp) 2730 return; 2731 2732 /* Find the macro maps for the macro expansions. */ 2733 const line_map *body_map = linemap_lookup (line_table, body_loc); 2734 const line_map *next_map = linemap_lookup (line_table, next_loc); 2735 const line_map *guard_map = linemap_lookup (line_table, guard_loc); 2736 2737 /* Now see if the following token (after the body) is coming from the 2738 same macro expansion. If it is, it might be a problem. */ 2739 if (body_map != next_map) 2740 return; 2741 2742 /* The conditional itself must not come from the same expansion, because 2743 we don't want to warn about 2744 #define IF if (x) x++; y++ 2745 and similar. */ 2746 if (guard_map == body_map) 2747 return; 2748 2749 /* Handle the case where NEXT and BODY come from the same expansion while 2750 GUARD doesn't, yet we shouldn't warn. E.g. 2751 2752 #define GUARD if (...) 2753 #define GUARD2 GUARD 2754 2755 and in the definition of another macro: 2756 2757 GUARD2 2758 foo (); 2759 return 1; 2760 */ 2761 while (linemap_macro_expansion_map_p (guard_map)) 2762 { 2763 const line_map_macro *mm = linemap_check_macro (guard_map); 2764 guard_loc_exp = MACRO_MAP_EXPANSION_POINT_LOCATION (mm); 2765 guard_map = linemap_lookup (line_table, guard_loc_exp); 2766 if (guard_map == body_map) 2767 return; 2768 } 2769 2770 auto_diagnostic_group d; 2771 if (warning_at (body_loc, OPT_Wmultistatement_macros, 2772 "macro expands to multiple statements")) 2773 inform (guard_loc, "some parts of macro expansion are not guarded by " 2774 "this %qs clause", guard_tinfo_to_string (keyword)); 2775} 2776 2777/* Return struct or union type if the alignment of data memeber, FIELD, 2778 is less than the alignment of TYPE. Otherwise, return NULL_TREE. 2779 If RVALUE is true, only arrays evaluate to pointers. */ 2780 2781static tree 2782check_alignment_of_packed_member (tree type, tree field, bool rvalue) 2783{ 2784 /* Check alignment of the data member. */ 2785 if (TREE_CODE (field) == FIELD_DECL 2786 && (DECL_PACKED (field) || TYPE_PACKED (TREE_TYPE (field))) 2787 /* Ignore FIELDs not laid out yet. */ 2788 && DECL_FIELD_OFFSET (field) 2789 && (!rvalue || TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)) 2790 { 2791 /* Check the expected alignment against the field alignment. */ 2792 unsigned int type_align = min_align_of_type (type); 2793 tree context = DECL_CONTEXT (field); 2794 unsigned int record_align = min_align_of_type (context); 2795 if (record_align < type_align) 2796 return context; 2797 tree field_off = byte_position (field); 2798 if (!multiple_of_p (TREE_TYPE (field_off), field_off, 2799 size_int (type_align))) 2800 return context; 2801 } 2802 2803 return NULL_TREE; 2804} 2805 2806/* Return struct or union type if the right hand value, RHS: 2807 1. Is a pointer value which isn't aligned to a pointer type TYPE. 2808 2. Is an address which takes the unaligned address of packed member 2809 of struct or union when assigning to TYPE. 2810 Otherwise, return NULL_TREE. */ 2811 2812static tree 2813check_address_or_pointer_of_packed_member (tree type, tree rhs) 2814{ 2815 bool rvalue = true; 2816 bool indirect = false; 2817 2818 if (INDIRECT_REF_P (rhs)) 2819 { 2820 rhs = TREE_OPERAND (rhs, 0); 2821 STRIP_NOPS (rhs); 2822 indirect = true; 2823 } 2824 2825 if (TREE_CODE (rhs) == ADDR_EXPR) 2826 { 2827 rhs = TREE_OPERAND (rhs, 0); 2828 rvalue = indirect; 2829 } 2830 2831 if (!POINTER_TYPE_P (type)) 2832 return NULL_TREE; 2833 2834 type = TREE_TYPE (type); 2835 2836 if (TREE_CODE (rhs) == PARM_DECL 2837 || VAR_P (rhs) 2838 || TREE_CODE (rhs) == CALL_EXPR) 2839 { 2840 tree rhstype = TREE_TYPE (rhs); 2841 if (TREE_CODE (rhs) == CALL_EXPR) 2842 { 2843 rhs = CALL_EXPR_FN (rhs); /* Pointer expression. */ 2844 if (rhs == NULL_TREE) 2845 return NULL_TREE; 2846 rhs = TREE_TYPE (rhs); /* Pointer type. */ 2847 rhs = TREE_TYPE (rhs); /* Function type. */ 2848 rhstype = TREE_TYPE (rhs); 2849 if (!rhstype || !POINTER_TYPE_P (rhstype)) 2850 return NULL_TREE; 2851 rvalue = true; 2852 } 2853 if (rvalue && POINTER_TYPE_P (rhstype)) 2854 rhstype = TREE_TYPE (rhstype); 2855 while (TREE_CODE (rhstype) == ARRAY_TYPE) 2856 rhstype = TREE_TYPE (rhstype); 2857 if (TYPE_PACKED (rhstype)) 2858 { 2859 unsigned int type_align = min_align_of_type (type); 2860 unsigned int rhs_align = min_align_of_type (rhstype); 2861 if (rhs_align < type_align) 2862 { 2863 auto_diagnostic_group d; 2864 location_t location = EXPR_LOC_OR_LOC (rhs, input_location); 2865 if (warning_at (location, OPT_Waddress_of_packed_member, 2866 "converting a packed %qT pointer (alignment %d) " 2867 "to a %qT pointer (alignment %d) may result in " 2868 "an unaligned pointer value", 2869 rhstype, rhs_align, type, type_align)) 2870 { 2871 tree decl = TYPE_STUB_DECL (rhstype); 2872 if (decl) 2873 inform (DECL_SOURCE_LOCATION (decl), "defined here"); 2874 decl = TYPE_STUB_DECL (type); 2875 if (decl) 2876 inform (DECL_SOURCE_LOCATION (decl), "defined here"); 2877 } 2878 } 2879 } 2880 return NULL_TREE; 2881 } 2882 2883 tree context = NULL_TREE; 2884 2885 /* Check alignment of the object. */ 2886 while (handled_component_p (rhs)) 2887 { 2888 if (TREE_CODE (rhs) == COMPONENT_REF) 2889 { 2890 tree field = TREE_OPERAND (rhs, 1); 2891 context = check_alignment_of_packed_member (type, field, rvalue); 2892 if (context) 2893 break; 2894 } 2895 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE) 2896 rvalue = false; 2897 if (rvalue) 2898 return NULL_TREE; 2899 rhs = TREE_OPERAND (rhs, 0); 2900 } 2901 2902 return context; 2903} 2904 2905/* Check and warn if the right hand value, RHS: 2906 1. Is a pointer value which isn't aligned to a pointer type TYPE. 2907 2. Is an address which takes the unaligned address of packed member 2908 of struct or union when assigning to TYPE. 2909 */ 2910 2911static void 2912check_and_warn_address_or_pointer_of_packed_member (tree type, tree rhs) 2913{ 2914 bool nop_p = false; 2915 tree orig_rhs; 2916 2917 do 2918 { 2919 while (TREE_CODE (rhs) == COMPOUND_EXPR) 2920 rhs = TREE_OPERAND (rhs, 1); 2921 orig_rhs = rhs; 2922 STRIP_NOPS (rhs); 2923 nop_p |= orig_rhs != rhs; 2924 } 2925 while (orig_rhs != rhs); 2926 2927 if (TREE_CODE (rhs) == COND_EXPR) 2928 { 2929 /* Check the THEN path. */ 2930 check_and_warn_address_or_pointer_of_packed_member 2931 (type, TREE_OPERAND (rhs, 1)); 2932 2933 /* Check the ELSE path. */ 2934 check_and_warn_address_or_pointer_of_packed_member 2935 (type, TREE_OPERAND (rhs, 2)); 2936 } 2937 else 2938 { 2939 if (nop_p) 2940 { 2941 switch (TREE_CODE (rhs)) 2942 { 2943 case ADDR_EXPR: 2944 /* Address is taken. */ 2945 case PARM_DECL: 2946 case VAR_DECL: 2947 /* Pointer conversion. */ 2948 break; 2949 case CALL_EXPR: 2950 /* Function call. */ 2951 break; 2952 default: 2953 return; 2954 } 2955 } 2956 2957 tree context 2958 = check_address_or_pointer_of_packed_member (type, rhs); 2959 if (context) 2960 { 2961 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location); 2962 warning_at (loc, OPT_Waddress_of_packed_member, 2963 "taking address of packed member of %qT may result " 2964 "in an unaligned pointer value", 2965 context); 2966 } 2967 } 2968} 2969 2970/* Warn if the right hand value, RHS: 2971 1. Is a pointer value which isn't aligned to a pointer type TYPE. 2972 2. Is an address which takes the unaligned address of packed member 2973 of struct or union when assigning to TYPE. 2974*/ 2975 2976void 2977warn_for_address_or_pointer_of_packed_member (tree type, tree rhs) 2978{ 2979 if (!warn_address_of_packed_member) 2980 return; 2981 2982 /* Don't warn if we don't assign RHS to a pointer. */ 2983 if (!POINTER_TYPE_P (type)) 2984 return; 2985 2986 check_and_warn_address_or_pointer_of_packed_member (type, rhs); 2987} 2988