c-warn.c revision 1.1.1.1
1/* Diagnostic routines shared by all languages that are variants of C. 2 Copyright (C) 1992-2017 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 "asan.h" 32#include "gcc-rich-location.h" 33 34/* Print a warning if a constant expression had overflow in folding. 35 Invoke this function on every expression that the language 36 requires to be a constant expression. 37 Note the ANSI C standard says it is erroneous for a 38 constant expression to overflow. */ 39 40void 41constant_expression_warning (tree value) 42{ 43 if (warn_overflow && pedantic 44 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST 45 || TREE_CODE (value) == FIXED_CST 46 || TREE_CODE (value) == VECTOR_CST 47 || TREE_CODE (value) == COMPLEX_CST) 48 && TREE_OVERFLOW (value)) 49 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression"); 50} 51 52/* The same as above but print an unconditional error. */ 53 54void 55constant_expression_error (tree value) 56{ 57 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST 58 || TREE_CODE (value) == FIXED_CST 59 || TREE_CODE (value) == VECTOR_CST 60 || TREE_CODE (value) == COMPLEX_CST) 61 && TREE_OVERFLOW (value)) 62 error ("overflow in constant expression"); 63} 64 65/* Print a warning if an expression had overflow in folding and its 66 operands hadn't. 67 68 Invoke this function on every expression that 69 (1) appears in the source code, and 70 (2) is a constant expression that overflowed, and 71 (3) is not already checked by convert_and_check; 72 however, do not invoke this function on operands of explicit casts 73 or when the expression is the result of an operator and any operand 74 already overflowed. */ 75 76void 77overflow_warning (location_t loc, tree value) 78{ 79 if (c_inhibit_evaluation_warnings != 0) 80 return; 81 82 switch (TREE_CODE (value)) 83 { 84 case INTEGER_CST: 85 warning_at (loc, OPT_Woverflow, "integer overflow in expression"); 86 break; 87 88 case REAL_CST: 89 warning_at (loc, OPT_Woverflow, 90 "floating point overflow in expression"); 91 break; 92 93 case FIXED_CST: 94 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression"); 95 break; 96 97 case VECTOR_CST: 98 warning_at (loc, OPT_Woverflow, "vector overflow in expression"); 99 break; 100 101 case COMPLEX_CST: 102 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST) 103 warning_at (loc, OPT_Woverflow, 104 "complex integer overflow in expression"); 105 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST) 106 warning_at (loc, OPT_Woverflow, 107 "complex floating point overflow in expression"); 108 break; 109 110 default: 111 break; 112 } 113} 114 115/* Warn about uses of logical || / && operator in a context where it 116 is likely that the bitwise equivalent was intended by the 117 programmer. We have seen an expression in which CODE is a binary 118 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding 119 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */ 120 121void 122warn_logical_operator (location_t location, enum tree_code code, tree type, 123 enum tree_code code_left, tree op_left, 124 enum tree_code ARG_UNUSED (code_right), tree op_right) 125{ 126 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR); 127 int in0_p, in1_p, in_p; 128 tree low0, low1, low, high0, high1, high, lhs, rhs, tem; 129 bool strict_overflow_p = false; 130 131 if (code != TRUTH_ANDIF_EXPR 132 && code != TRUTH_AND_EXPR 133 && code != TRUTH_ORIF_EXPR 134 && code != TRUTH_OR_EXPR) 135 return; 136 137 /* We don't want to warn if either operand comes from a macro 138 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet; 139 see PR61534. */ 140 if (from_macro_expansion_at (EXPR_LOCATION (op_left)) 141 || from_macro_expansion_at (EXPR_LOCATION (op_right))) 142 return; 143 144 /* Warn if &&/|| are being used in a context where it is 145 likely that the bitwise equivalent was intended by the 146 programmer. That is, an expression such as op && MASK 147 where op should not be any boolean expression, nor a 148 constant, and mask seems to be a non-boolean integer constant. */ 149 if (TREE_CODE (op_right) == CONST_DECL) 150 /* An enumerator counts as a constant. */ 151 op_right = DECL_INITIAL (op_right); 152 if (!truth_value_p (code_left) 153 && INTEGRAL_TYPE_P (TREE_TYPE (op_left)) 154 && !CONSTANT_CLASS_P (op_left) 155 && !TREE_NO_WARNING (op_left) 156 && TREE_CODE (op_right) == INTEGER_CST 157 && !integer_zerop (op_right) 158 && !integer_onep (op_right)) 159 { 160 if (or_op) 161 warning_at (location, OPT_Wlogical_op, "logical %<or%>" 162 " applied to non-boolean constant"); 163 else 164 warning_at (location, OPT_Wlogical_op, "logical %<and%>" 165 " applied to non-boolean constant"); 166 TREE_NO_WARNING (op_left) = true; 167 return; 168 } 169 170 /* We do not warn for constants because they are typical of macro 171 expansions that test for features. */ 172 if (CONSTANT_CLASS_P (fold_for_warn (op_left)) 173 || CONSTANT_CLASS_P (fold_for_warn (op_right))) 174 return; 175 176 /* This warning only makes sense with logical operands. */ 177 if (!(truth_value_p (TREE_CODE (op_left)) 178 || INTEGRAL_TYPE_P (TREE_TYPE (op_left))) 179 || !(truth_value_p (TREE_CODE (op_right)) 180 || INTEGRAL_TYPE_P (TREE_TYPE (op_right)))) 181 return; 182 183 /* The range computations only work with scalars. */ 184 if (VECTOR_TYPE_P (TREE_TYPE (op_left)) 185 || VECTOR_TYPE_P (TREE_TYPE (op_right))) 186 return; 187 188 /* We first test whether either side separately is trivially true 189 (with OR) or trivially false (with AND). If so, do not warn. 190 This is a common idiom for testing ranges of data types in 191 portable code. */ 192 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p); 193 if (!lhs) 194 return; 195 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR) 196 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs); 197 198 /* If this is an OR operation, invert both sides; now, the result 199 should be always false to get a warning. */ 200 if (or_op) 201 in0_p = !in0_p; 202 203 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0); 204 if (tem && integer_zerop (tem)) 205 return; 206 207 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p); 208 if (!rhs) 209 return; 210 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR) 211 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs); 212 213 /* If this is an OR operation, invert both sides; now, the result 214 should be always false to get a warning. */ 215 if (or_op) 216 in1_p = !in1_p; 217 218 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1); 219 if (tem && integer_zerop (tem)) 220 return; 221 222 /* If both expressions have the same operand, if we can merge the 223 ranges, ... */ 224 if (operand_equal_p (lhs, rhs, 0) 225 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0, 226 in1_p, low1, high1)) 227 { 228 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high); 229 /* ... and if the range test is always false, then warn. */ 230 if (tem && integer_zerop (tem)) 231 { 232 if (or_op) 233 warning_at (location, OPT_Wlogical_op, 234 "logical %<or%> of collectively exhaustive tests is " 235 "always true"); 236 else 237 warning_at (location, OPT_Wlogical_op, 238 "logical %<and%> of mutually exclusive tests is " 239 "always false"); 240 } 241 /* Or warn if the operands have exactly the same range, e.g. 242 A > 0 && A > 0. */ 243 else if (tree_int_cst_equal (low0, low1) 244 && tree_int_cst_equal (high0, high1)) 245 { 246 if (or_op) 247 warning_at (location, OPT_Wlogical_op, 248 "logical %<or%> of equal expressions"); 249 else 250 warning_at (location, OPT_Wlogical_op, 251 "logical %<and%> of equal expressions"); 252 } 253 } 254} 255 256/* Helper function for warn_tautological_cmp. Look for ARRAY_REFs 257 with constant indices. */ 258 259static tree 260find_array_ref_with_const_idx_r (tree *expr_p, int *, void *) 261{ 262 tree expr = *expr_p; 263 264 if ((TREE_CODE (expr) == ARRAY_REF 265 || TREE_CODE (expr) == ARRAY_RANGE_REF) 266 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST) 267 return integer_type_node; 268 269 return NULL_TREE; 270} 271 272/* Warn if a self-comparison always evaluates to true or false. LOC 273 is the location of the comparison with code CODE, LHS and RHS are 274 operands of the comparison. */ 275 276void 277warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs) 278{ 279 if (TREE_CODE_CLASS (code) != tcc_comparison) 280 return; 281 282 /* Don't warn for various macro expansions. */ 283 if (from_macro_expansion_at (loc) 284 || from_macro_expansion_at (EXPR_LOCATION (lhs)) 285 || from_macro_expansion_at (EXPR_LOCATION (rhs))) 286 return; 287 288 /* We do not warn for constants because they are typical of macro 289 expansions that test for features, sizeof, and similar. */ 290 if (CONSTANT_CLASS_P (fold_for_warn (lhs)) 291 || CONSTANT_CLASS_P (fold_for_warn (rhs))) 292 return; 293 294 /* Don't warn for e.g. 295 HOST_WIDE_INT n; 296 ... 297 if (n == (long) n) ... 298 */ 299 if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR) 300 || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR)) 301 return; 302 303 /* Don't warn if either LHS or RHS has an IEEE floating-point type. 304 It could be a NaN, and NaN never compares equal to anything, even 305 itself. */ 306 if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs))) 307 return; 308 309 if (operand_equal_p (lhs, rhs, 0)) 310 { 311 /* Don't warn about array references with constant indices; 312 these are likely to come from a macro. */ 313 if (walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r, 314 NULL)) 315 return; 316 const bool always_true = (code == EQ_EXPR || code == LE_EXPR 317 || code == GE_EXPR || code == UNLE_EXPR 318 || code == UNGE_EXPR || code == UNEQ_EXPR); 319 if (always_true) 320 warning_at (loc, OPT_Wtautological_compare, 321 "self-comparison always evaluates to true"); 322 else 323 warning_at (loc, OPT_Wtautological_compare, 324 "self-comparison always evaluates to false"); 325 } 326} 327 328/* Return true iff EXPR only contains boolean operands, or comparisons. */ 329 330static bool 331expr_has_boolean_operands_p (tree expr) 332{ 333 STRIP_NOPS (expr); 334 335 if (CONVERT_EXPR_P (expr)) 336 return bool_promoted_to_int_p (expr); 337 else if (UNARY_CLASS_P (expr)) 338 return expr_has_boolean_operands_p (TREE_OPERAND (expr, 0)); 339 else if (BINARY_CLASS_P (expr)) 340 return (expr_has_boolean_operands_p (TREE_OPERAND (expr, 0)) 341 && expr_has_boolean_operands_p (TREE_OPERAND (expr, 1))); 342 else if (COMPARISON_CLASS_P (expr)) 343 return true; 344 else 345 return false; 346} 347 348/* Warn about logical not used on the left hand side operand of a comparison. 349 This function assumes that the LHS is inside of TRUTH_NOT_EXPR. 350 Do not warn if RHS is of a boolean type, a logical operator, or 351 a comparison. */ 352 353void 354warn_logical_not_parentheses (location_t location, enum tree_code code, 355 tree lhs, tree rhs) 356{ 357 if (TREE_CODE_CLASS (code) != tcc_comparison 358 || TREE_TYPE (rhs) == NULL_TREE 359 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE 360 || truth_value_p (TREE_CODE (rhs))) 361 return; 362 363 /* Don't warn for expression like !x == ~(bool1 | bool2). */ 364 if (expr_has_boolean_operands_p (rhs)) 365 return; 366 367 /* Don't warn for !x == 0 or !y != 0, those are equivalent to 368 !(x == 0) or !(y != 0). */ 369 if ((code == EQ_EXPR || code == NE_EXPR) 370 && integer_zerop (rhs)) 371 return; 372 373 if (warning_at (location, OPT_Wlogical_not_parentheses, 374 "logical not is only applied to the left hand side of " 375 "comparison") 376 && EXPR_HAS_LOCATION (lhs)) 377 { 378 location_t lhs_loc = EXPR_LOCATION (lhs); 379 rich_location richloc (line_table, lhs_loc); 380 richloc.add_fixit_insert_before (lhs_loc, "("); 381 richloc.add_fixit_insert_after (lhs_loc, ")"); 382 inform_at_rich_loc (&richloc, "add parentheses around left hand side " 383 "expression to silence this warning"); 384 } 385} 386 387/* Warn if EXP contains any computations whose results are not used. 388 Return true if a warning is printed; false otherwise. LOCUS is the 389 (potential) location of the expression. */ 390 391bool 392warn_if_unused_value (const_tree exp, location_t locus) 393{ 394 restart: 395 if (TREE_USED (exp) || TREE_NO_WARNING (exp)) 396 return false; 397 398 /* Don't warn about void constructs. This includes casting to void, 399 void function calls, and statement expressions with a final cast 400 to void. */ 401 if (VOID_TYPE_P (TREE_TYPE (exp))) 402 return false; 403 404 if (EXPR_HAS_LOCATION (exp)) 405 locus = EXPR_LOCATION (exp); 406 407 switch (TREE_CODE (exp)) 408 { 409 case PREINCREMENT_EXPR: 410 case POSTINCREMENT_EXPR: 411 case PREDECREMENT_EXPR: 412 case POSTDECREMENT_EXPR: 413 case MODIFY_EXPR: 414 case INIT_EXPR: 415 case TARGET_EXPR: 416 case CALL_EXPR: 417 case TRY_CATCH_EXPR: 418 case WITH_CLEANUP_EXPR: 419 case EXIT_EXPR: 420 case VA_ARG_EXPR: 421 return false; 422 423 case BIND_EXPR: 424 /* For a binding, warn if no side effect within it. */ 425 exp = BIND_EXPR_BODY (exp); 426 goto restart; 427 428 case SAVE_EXPR: 429 case NON_LVALUE_EXPR: 430 case NOP_EXPR: 431 exp = TREE_OPERAND (exp, 0); 432 goto restart; 433 434 case TRUTH_ORIF_EXPR: 435 case TRUTH_ANDIF_EXPR: 436 /* In && or ||, warn if 2nd operand has no side effect. */ 437 exp = TREE_OPERAND (exp, 1); 438 goto restart; 439 440 case COMPOUND_EXPR: 441 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus)) 442 return true; 443 /* Let people do `(foo (), 0)' without a warning. */ 444 if (TREE_CONSTANT (TREE_OPERAND (exp, 1))) 445 return false; 446 exp = TREE_OPERAND (exp, 1); 447 goto restart; 448 449 case COND_EXPR: 450 /* If this is an expression with side effects, don't warn; this 451 case commonly appears in macro expansions. */ 452 if (TREE_SIDE_EFFECTS (exp)) 453 return false; 454 goto warn; 455 456 case INDIRECT_REF: 457 /* Don't warn about automatic dereferencing of references, since 458 the user cannot control it. */ 459 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE) 460 { 461 exp = TREE_OPERAND (exp, 0); 462 goto restart; 463 } 464 /* Fall through. */ 465 466 default: 467 /* Referencing a volatile value is a side effect, so don't warn. */ 468 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp)) 469 && TREE_THIS_VOLATILE (exp)) 470 return false; 471 472 /* If this is an expression which has no operands, there is no value 473 to be unused. There are no such language-independent codes, 474 but front ends may define such. */ 475 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0) 476 return false; 477 478 warn: 479 return warning_at (locus, OPT_Wunused_value, "value computed is not used"); 480 } 481} 482 483/* Print a warning about casts that might indicate violation 484 of strict aliasing rules if -Wstrict-aliasing is used and 485 strict aliasing mode is in effect. OTYPE is the original 486 TREE_TYPE of EXPR, and TYPE the type we're casting to. */ 487 488bool 489strict_aliasing_warning (tree otype, tree type, tree expr) 490{ 491 /* Strip pointer conversion chains and get to the correct original type. */ 492 STRIP_NOPS (expr); 493 otype = TREE_TYPE (expr); 494 495 if (!(flag_strict_aliasing 496 && POINTER_TYPE_P (type) 497 && POINTER_TYPE_P (otype) 498 && !VOID_TYPE_P (TREE_TYPE (type))) 499 /* If the type we are casting to is a ref-all pointer 500 dereferencing it is always valid. */ 501 || TYPE_REF_CAN_ALIAS_ALL (type)) 502 return false; 503 504 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR 505 && (DECL_P (TREE_OPERAND (expr, 0)) 506 || handled_component_p (TREE_OPERAND (expr, 0)))) 507 { 508 /* Casting the address of an object to non void pointer. Warn 509 if the cast breaks type based aliasing. */ 510 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2) 511 { 512 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type " 513 "might break strict-aliasing rules"); 514 return true; 515 } 516 else 517 { 518 /* warn_strict_aliasing >= 3. This includes the default (3). 519 Only warn if the cast is dereferenced immediately. */ 520 alias_set_type set1 521 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))); 522 alias_set_type set2 = get_alias_set (TREE_TYPE (type)); 523 524 if (set2 != 0 525 && set1 != set2 526 && !alias_set_subset_of (set2, set1) 527 && !alias_sets_conflict_p (set1, set2)) 528 { 529 warning (OPT_Wstrict_aliasing, "dereferencing type-punned " 530 "pointer will break strict-aliasing rules"); 531 return true; 532 } 533 else if (warn_strict_aliasing == 2 534 && !alias_sets_must_conflict_p (set1, set2)) 535 { 536 warning (OPT_Wstrict_aliasing, "dereferencing type-punned " 537 "pointer might break strict-aliasing rules"); 538 return true; 539 } 540 } 541 } 542 else if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype))) 543 { 544 /* At this level, warn for any conversions, even if an address is 545 not taken in the same statement. This will likely produce many 546 false positives, but could be useful to pinpoint problems that 547 are not revealed at higher levels. */ 548 alias_set_type set1 = get_alias_set (TREE_TYPE (otype)); 549 alias_set_type set2 = get_alias_set (TREE_TYPE (type)); 550 if (!COMPLETE_TYPE_P (type) 551 || !alias_sets_must_conflict_p (set1, set2)) 552 { 553 warning (OPT_Wstrict_aliasing, "dereferencing type-punned " 554 "pointer might break strict-aliasing rules"); 555 return true; 556 } 557 } 558 559 return false; 560} 561 562/* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with 563 sizeof as last operand of certain builtins. */ 564 565void 566sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee, 567 vec<tree, va_gc> *params, tree *sizeof_arg, 568 bool (*comp_types) (tree, tree)) 569{ 570 tree type, dest = NULL_TREE, src = NULL_TREE, tem; 571 bool strop = false, cmp = false; 572 unsigned int idx = ~0; 573 location_t loc; 574 575 if (TREE_CODE (callee) != FUNCTION_DECL 576 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL 577 || vec_safe_length (params) <= 1) 578 return; 579 580 switch (DECL_FUNCTION_CODE (callee)) 581 { 582 case BUILT_IN_STRNCMP: 583 case BUILT_IN_STRNCASECMP: 584 cmp = true; 585 /* FALLTHRU */ 586 case BUILT_IN_STRNCPY: 587 case BUILT_IN_STRNCPY_CHK: 588 case BUILT_IN_STRNCAT: 589 case BUILT_IN_STRNCAT_CHK: 590 case BUILT_IN_STPNCPY: 591 case BUILT_IN_STPNCPY_CHK: 592 strop = true; 593 /* FALLTHRU */ 594 case BUILT_IN_MEMCPY: 595 case BUILT_IN_MEMCPY_CHK: 596 case BUILT_IN_MEMMOVE: 597 case BUILT_IN_MEMMOVE_CHK: 598 if (params->length () < 3) 599 return; 600 src = (*params)[1]; 601 dest = (*params)[0]; 602 idx = 2; 603 break; 604 case BUILT_IN_BCOPY: 605 if (params->length () < 3) 606 return; 607 src = (*params)[0]; 608 dest = (*params)[1]; 609 idx = 2; 610 break; 611 case BUILT_IN_MEMCMP: 612 case BUILT_IN_BCMP: 613 if (params->length () < 3) 614 return; 615 src = (*params)[1]; 616 dest = (*params)[0]; 617 idx = 2; 618 cmp = true; 619 break; 620 case BUILT_IN_MEMSET: 621 case BUILT_IN_MEMSET_CHK: 622 if (params->length () < 3) 623 return; 624 dest = (*params)[0]; 625 idx = 2; 626 break; 627 case BUILT_IN_BZERO: 628 dest = (*params)[0]; 629 idx = 1; 630 break; 631 case BUILT_IN_STRNDUP: 632 src = (*params)[0]; 633 strop = true; 634 idx = 1; 635 break; 636 case BUILT_IN_MEMCHR: 637 if (params->length () < 3) 638 return; 639 src = (*params)[0]; 640 idx = 2; 641 break; 642 case BUILT_IN_SNPRINTF: 643 case BUILT_IN_SNPRINTF_CHK: 644 case BUILT_IN_VSNPRINTF: 645 case BUILT_IN_VSNPRINTF_CHK: 646 dest = (*params)[0]; 647 idx = 1; 648 strop = true; 649 break; 650 default: 651 break; 652 } 653 654 if (idx >= 3) 655 return; 656 657 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node) 658 return; 659 660 type = TYPE_P (sizeof_arg[idx]) 661 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]); 662 if (!POINTER_TYPE_P (type)) 663 return; 664 665 if (dest 666 && (tem = tree_strip_nop_conversions (dest)) 667 && POINTER_TYPE_P (TREE_TYPE (tem)) 668 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type)) 669 return; 670 671 if (src 672 && (tem = tree_strip_nop_conversions (src)) 673 && POINTER_TYPE_P (TREE_TYPE (tem)) 674 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type)) 675 return; 676 677 loc = sizeof_arg_loc[idx]; 678 679 if (dest && !cmp) 680 { 681 if (!TYPE_P (sizeof_arg[idx]) 682 && operand_equal_p (dest, sizeof_arg[idx], 0) 683 && comp_types (TREE_TYPE (dest), type)) 684 { 685 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) 686 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 687 "argument to %<sizeof%> in %qD call is the same " 688 "expression as the destination; did you mean to " 689 "remove the addressof?", callee); 690 else if ((TYPE_PRECISION (TREE_TYPE (type)) 691 == TYPE_PRECISION (char_type_node)) 692 || strop) 693 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 694 "argument to %<sizeof%> in %qD call is the same " 695 "expression as the destination; did you mean to " 696 "provide an explicit length?", callee); 697 else 698 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 699 "argument to %<sizeof%> in %qD call is the same " 700 "expression as the destination; did you mean to " 701 "dereference it?", callee); 702 return; 703 } 704 705 if (POINTER_TYPE_P (TREE_TYPE (dest)) 706 && !strop 707 && comp_types (TREE_TYPE (dest), type) 708 && !VOID_TYPE_P (TREE_TYPE (type))) 709 { 710 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 711 "argument to %<sizeof%> in %qD call is the same " 712 "pointer type %qT as the destination; expected %qT " 713 "or an explicit length", callee, TREE_TYPE (dest), 714 TREE_TYPE (TREE_TYPE (dest))); 715 return; 716 } 717 } 718 719 if (src && !cmp) 720 { 721 if (!TYPE_P (sizeof_arg[idx]) 722 && operand_equal_p (src, sizeof_arg[idx], 0) 723 && comp_types (TREE_TYPE (src), type)) 724 { 725 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) 726 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 727 "argument to %<sizeof%> in %qD call is the same " 728 "expression as the source; did you mean to " 729 "remove the addressof?", callee); 730 else if ((TYPE_PRECISION (TREE_TYPE (type)) 731 == TYPE_PRECISION (char_type_node)) 732 || strop) 733 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 734 "argument to %<sizeof%> in %qD call is the same " 735 "expression as the source; did you mean to " 736 "provide an explicit length?", callee); 737 else 738 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 739 "argument to %<sizeof%> in %qD call is the same " 740 "expression as the source; did you mean to " 741 "dereference it?", callee); 742 return; 743 } 744 745 if (POINTER_TYPE_P (TREE_TYPE (src)) 746 && !strop 747 && comp_types (TREE_TYPE (src), type) 748 && !VOID_TYPE_P (TREE_TYPE (type))) 749 { 750 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 751 "argument to %<sizeof%> in %qD call is the same " 752 "pointer type %qT as the source; expected %qT " 753 "or an explicit length", callee, TREE_TYPE (src), 754 TREE_TYPE (TREE_TYPE (src))); 755 return; 756 } 757 } 758 759 if (dest) 760 { 761 if (!TYPE_P (sizeof_arg[idx]) 762 && operand_equal_p (dest, sizeof_arg[idx], 0) 763 && comp_types (TREE_TYPE (dest), type)) 764 { 765 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) 766 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 767 "argument to %<sizeof%> in %qD call is the same " 768 "expression as the first source; did you mean to " 769 "remove the addressof?", callee); 770 else if ((TYPE_PRECISION (TREE_TYPE (type)) 771 == TYPE_PRECISION (char_type_node)) 772 || strop) 773 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 774 "argument to %<sizeof%> in %qD call is the same " 775 "expression as the first source; did you mean to " 776 "provide an explicit length?", callee); 777 else 778 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 779 "argument to %<sizeof%> in %qD call is the same " 780 "expression as the first source; did you mean to " 781 "dereference it?", callee); 782 return; 783 } 784 785 if (POINTER_TYPE_P (TREE_TYPE (dest)) 786 && !strop 787 && comp_types (TREE_TYPE (dest), type) 788 && !VOID_TYPE_P (TREE_TYPE (type))) 789 { 790 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 791 "argument to %<sizeof%> in %qD call is the same " 792 "pointer type %qT as the first source; expected %qT " 793 "or an explicit length", callee, TREE_TYPE (dest), 794 TREE_TYPE (TREE_TYPE (dest))); 795 return; 796 } 797 } 798 799 if (src) 800 { 801 if (!TYPE_P (sizeof_arg[idx]) 802 && operand_equal_p (src, sizeof_arg[idx], 0) 803 && comp_types (TREE_TYPE (src), type)) 804 { 805 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) 806 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 807 "argument to %<sizeof%> in %qD call is the same " 808 "expression as the second source; did you mean to " 809 "remove the addressof?", callee); 810 else if ((TYPE_PRECISION (TREE_TYPE (type)) 811 == TYPE_PRECISION (char_type_node)) 812 || strop) 813 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 814 "argument to %<sizeof%> in %qD call is the same " 815 "expression as the second source; did you mean to " 816 "provide an explicit length?", callee); 817 else 818 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 819 "argument to %<sizeof%> in %qD call is the same " 820 "expression as the second source; did you mean to " 821 "dereference it?", callee); 822 return; 823 } 824 825 if (POINTER_TYPE_P (TREE_TYPE (src)) 826 && !strop 827 && comp_types (TREE_TYPE (src), type) 828 && !VOID_TYPE_P (TREE_TYPE (type))) 829 { 830 warning_at (loc, OPT_Wsizeof_pointer_memaccess, 831 "argument to %<sizeof%> in %qD call is the same " 832 "pointer type %qT as the second source; expected %qT " 833 "or an explicit length", callee, TREE_TYPE (src), 834 TREE_TYPE (TREE_TYPE (src))); 835 return; 836 } 837 } 838 839} 840 841/* Warn for unlikely, improbable, or stupid DECL declarations 842 of `main'. */ 843 844void 845check_main_parameter_types (tree decl) 846{ 847 function_args_iterator iter; 848 tree type; 849 int argct = 0; 850 851 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter) 852 { 853 /* XXX void_type_node belies the abstraction. */ 854 if (type == void_type_node || type == error_mark_node) 855 break; 856 857 tree t = type; 858 if (TYPE_ATOMIC (t)) 859 pedwarn (input_location, OPT_Wmain, 860 "%<_Atomic%>-qualified parameter type %qT of %q+D", 861 type, decl); 862 while (POINTER_TYPE_P (t)) 863 { 864 t = TREE_TYPE (t); 865 if (TYPE_ATOMIC (t)) 866 pedwarn (input_location, OPT_Wmain, 867 "%<_Atomic%>-qualified parameter type %qT of %q+D", 868 type, decl); 869 } 870 871 ++argct; 872 switch (argct) 873 { 874 case 1: 875 if (TYPE_MAIN_VARIANT (type) != integer_type_node) 876 pedwarn (input_location, OPT_Wmain, 877 "first argument of %q+D should be %<int%>", decl); 878 break; 879 880 case 2: 881 if (TREE_CODE (type) != POINTER_TYPE 882 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE 883 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) 884 != char_type_node)) 885 pedwarn (input_location, OPT_Wmain, 886 "second argument of %q+D should be %<char **%>", decl); 887 break; 888 889 case 3: 890 if (TREE_CODE (type) != POINTER_TYPE 891 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE 892 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) 893 != char_type_node)) 894 pedwarn (input_location, OPT_Wmain, 895 "third argument of %q+D should probably be " 896 "%<char **%>", decl); 897 break; 898 } 899 } 900 901 /* It is intentional that this message does not mention the third 902 argument because it's only mentioned in an appendix of the 903 standard. */ 904 if (argct > 0 && (argct < 2 || argct > 3)) 905 pedwarn (input_location, OPT_Wmain, 906 "%q+D takes only zero or two arguments", decl); 907 908 if (stdarg_p (TREE_TYPE (decl))) 909 pedwarn (input_location, OPT_Wmain, 910 "%q+D declared as variadic function", decl); 911} 912 913/* Warns if the conversion of EXPR to TYPE may alter a value. 914 This is a helper function for warnings_for_convert_and_check. */ 915 916static void 917conversion_warning (location_t loc, tree type, tree expr) 918{ 919 tree expr_type = TREE_TYPE (expr); 920 enum conversion_safety conversion_kind; 921 922 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion) 923 return; 924 925 /* This may happen, because for LHS op= RHS we preevaluate 926 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which 927 means we could no longer see the code of the EXPR. */ 928 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR) 929 expr = C_MAYBE_CONST_EXPR_EXPR (expr); 930 if (TREE_CODE (expr) == SAVE_EXPR) 931 expr = TREE_OPERAND (expr, 0); 932 933 switch (TREE_CODE (expr)) 934 { 935 case EQ_EXPR: 936 case NE_EXPR: 937 case LE_EXPR: 938 case GE_EXPR: 939 case LT_EXPR: 940 case GT_EXPR: 941 case TRUTH_ANDIF_EXPR: 942 case TRUTH_ORIF_EXPR: 943 case TRUTH_AND_EXPR: 944 case TRUTH_OR_EXPR: 945 case TRUTH_XOR_EXPR: 946 case TRUTH_NOT_EXPR: 947 /* Conversion from boolean to a signed:1 bit-field (which only 948 can hold the values 0 and -1) doesn't lose information - but 949 it does change the value. */ 950 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type)) 951 warning_at (loc, OPT_Wconversion, 952 "conversion to %qT from boolean expression", type); 953 return; 954 955 case REAL_CST: 956 case INTEGER_CST: 957 case COMPLEX_CST: 958 conversion_kind = unsafe_conversion_p (loc, type, expr, true); 959 if (conversion_kind == UNSAFE_REAL) 960 warning_at (loc, OPT_Wfloat_conversion, 961 "conversion to %qT alters %qT constant value", 962 type, expr_type); 963 else if (conversion_kind) 964 warning_at (loc, OPT_Wconversion, 965 "conversion to %qT alters %qT constant value", 966 type, expr_type); 967 return; 968 969 case COND_EXPR: 970 { 971 /* In case of COND_EXPR, we do not care about the type of 972 COND_EXPR, only about the conversion of each operand. */ 973 tree op1 = TREE_OPERAND (expr, 1); 974 tree op2 = TREE_OPERAND (expr, 2); 975 976 conversion_warning (loc, type, op1); 977 conversion_warning (loc, type, op2); 978 return; 979 } 980 981 default: /* 'expr' is not a constant. */ 982 conversion_kind = unsafe_conversion_p (loc, type, expr, true); 983 if (conversion_kind == UNSAFE_REAL) 984 warning_at (loc, OPT_Wfloat_conversion, 985 "conversion to %qT from %qT may alter its value", 986 type, expr_type); 987 else if (conversion_kind == UNSAFE_IMAGINARY) 988 warning_at (loc, OPT_Wconversion, 989 "conversion to %qT from %qT discards imaginary component", 990 type, expr_type); 991 else if (conversion_kind) 992 warning_at (loc, OPT_Wconversion, 993 "conversion to %qT from %qT may alter its value", 994 type, expr_type); 995 } 996} 997 998/* Produce warnings after a conversion. RESULT is the result of 999 converting EXPR to TYPE. This is a helper function for 1000 convert_and_check and cp_convert_and_check. */ 1001 1002void 1003warnings_for_convert_and_check (location_t loc, tree type, tree expr, 1004 tree result) 1005{ 1006 loc = expansion_point_location_if_in_system_header (loc); 1007 1008 if (TREE_CODE (expr) == INTEGER_CST 1009 && (TREE_CODE (type) == INTEGER_TYPE 1010 || TREE_CODE (type) == ENUMERAL_TYPE) 1011 && !int_fits_type_p (expr, type)) 1012 { 1013 /* Do not diagnose overflow in a constant expression merely 1014 because a conversion overflowed. */ 1015 if (TREE_OVERFLOW (result)) 1016 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr); 1017 1018 if (TYPE_UNSIGNED (type)) 1019 { 1020 /* This detects cases like converting -129 or 256 to 1021 unsigned char. */ 1022 if (!int_fits_type_p (expr, c_common_signed_type (type))) 1023 warning_at (loc, OPT_Woverflow, 1024 "large integer implicitly truncated to unsigned type"); 1025 else 1026 conversion_warning (loc, type, expr); 1027 } 1028 else if (!int_fits_type_p (expr, c_common_unsigned_type (type))) 1029 warning_at (loc, OPT_Woverflow, 1030 "overflow in implicit constant conversion"); 1031 /* No warning for converting 0x80000000 to int. */ 1032 else if (pedantic 1033 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE 1034 || TYPE_PRECISION (TREE_TYPE (expr)) 1035 != TYPE_PRECISION (type))) 1036 warning_at (loc, OPT_Woverflow, 1037 "overflow in implicit constant conversion"); 1038 1039 else 1040 conversion_warning (loc, type, expr); 1041 } 1042 else if ((TREE_CODE (result) == INTEGER_CST 1043 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result)) 1044 warning_at (loc, OPT_Woverflow, 1045 "overflow in implicit constant conversion"); 1046 else 1047 conversion_warning (loc, type, expr); 1048} 1049 1050/* Subroutines of c_do_switch_warnings, called via splay_tree_foreach. 1051 Used to verify that case values match up with enumerator values. */ 1052 1053static void 1054match_case_to_enum_1 (tree key, tree type, tree label) 1055{ 1056 char buf[WIDE_INT_PRINT_BUFFER_SIZE]; 1057 1058 if (tree_fits_uhwi_p (key)) 1059 print_dec (key, buf, UNSIGNED); 1060 else if (tree_fits_shwi_p (key)) 1061 print_dec (key, buf, SIGNED); 1062 else 1063 print_hex (key, buf); 1064 1065 if (TYPE_NAME (type) == 0) 1066 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)), 1067 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum, 1068 "case value %qs not in enumerated type", 1069 buf); 1070 else 1071 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)), 1072 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum, 1073 "case value %qs not in enumerated type %qT", 1074 buf, type); 1075} 1076 1077/* Subroutine of c_do_switch_warnings, called via splay_tree_foreach. 1078 Used to verify that case values match up with enumerator values. */ 1079 1080static int 1081match_case_to_enum (splay_tree_node node, void *data) 1082{ 1083 tree label = (tree) node->value; 1084 tree type = (tree) data; 1085 1086 /* Skip default case. */ 1087 if (!CASE_LOW (label)) 1088 return 0; 1089 1090 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear 1091 when we did our enum->case scan. Reset our scratch bit after. */ 1092 if (!CASE_LOW_SEEN (label)) 1093 match_case_to_enum_1 (CASE_LOW (label), type, label); 1094 else 1095 CASE_LOW_SEEN (label) = 0; 1096 1097 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is 1098 not set, that means that CASE_HIGH did not appear when we did our 1099 enum->case scan. Reset our scratch bit after. */ 1100 if (CASE_HIGH (label)) 1101 { 1102 if (!CASE_HIGH_SEEN (label)) 1103 match_case_to_enum_1 (CASE_HIGH (label), type, label); 1104 else 1105 CASE_HIGH_SEEN (label) = 0; 1106 } 1107 1108 return 0; 1109} 1110 1111/* Handle -Wswitch*. Called from the front end after parsing the 1112 switch construct. */ 1113/* ??? Should probably be somewhere generic, since other languages 1114 besides C and C++ would want this. At the moment, however, C/C++ 1115 are the only tree-ssa languages that support enumerations at all, 1116 so the point is moot. */ 1117 1118void 1119c_do_switch_warnings (splay_tree cases, location_t switch_location, 1120 tree type, tree cond, bool bool_cond_p, 1121 bool outside_range_p) 1122{ 1123 splay_tree_node default_node; 1124 splay_tree_node node; 1125 tree chain; 1126 1127 if (!warn_switch && !warn_switch_enum && !warn_switch_default 1128 && !warn_switch_bool) 1129 return; 1130 1131 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL); 1132 if (!default_node) 1133 warning_at (switch_location, OPT_Wswitch_default, 1134 "switch missing default case"); 1135 1136 /* There are certain cases where -Wswitch-bool warnings aren't 1137 desirable, such as 1138 switch (boolean) 1139 { 1140 case true: ... 1141 case false: ... 1142 } 1143 so be careful here. */ 1144 if (warn_switch_bool && bool_cond_p) 1145 { 1146 splay_tree_node min_node; 1147 /* If there's a default node, it's also the value with the minimal 1148 key. So look at the penultimate key (if any). */ 1149 if (default_node) 1150 min_node = splay_tree_successor (cases, (splay_tree_key) NULL); 1151 else 1152 min_node = splay_tree_min (cases); 1153 tree min = min_node ? (tree) min_node->key : NULL_TREE; 1154 1155 splay_tree_node max_node = splay_tree_max (cases); 1156 /* This might be a case range, so look at the value with the 1157 maximal key and then check CASE_HIGH. */ 1158 tree max = max_node ? (tree) max_node->value : NULL_TREE; 1159 if (max) 1160 max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max); 1161 1162 /* If there's a case value > 1 or < 0, that is outside bool 1163 range, warn. */ 1164 if (outside_range_p 1165 || (max && wi::gts_p (max, 1)) 1166 || (min && wi::lts_p (min, 0)) 1167 /* And handle the 1168 switch (boolean) 1169 { 1170 case true: ... 1171 case false: ... 1172 default: ... 1173 } 1174 case, where we want to warn. */ 1175 || (default_node 1176 && max && wi::eq_p (max, 1) 1177 && min && wi::eq_p (min, 0))) 1178 warning_at (switch_location, OPT_Wswitch_bool, 1179 "switch condition has boolean value"); 1180 } 1181 1182 /* From here on, we only care about enumerated types. */ 1183 if (!type || TREE_CODE (type) != ENUMERAL_TYPE) 1184 return; 1185 1186 /* From here on, we only care about -Wswitch and -Wswitch-enum. */ 1187 if (!warn_switch_enum && !warn_switch) 1188 return; 1189 1190 /* Check the cases. Warn about case values which are not members of 1191 the enumerated type. For -Wswitch-enum, or for -Wswitch when 1192 there is no default case, check that exactly all enumeration 1193 literals are covered by the cases. */ 1194 1195 /* Clearing COND if it is not an integer constant simplifies 1196 the tests inside the loop below. */ 1197 if (TREE_CODE (cond) != INTEGER_CST) 1198 cond = NULL_TREE; 1199 1200 /* The time complexity here is O(N*lg(N)) worst case, but for the 1201 common case of monotonically increasing enumerators, it is 1202 O(N), since the nature of the splay tree will keep the next 1203 element adjacent to the root at all times. */ 1204 1205 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain)) 1206 { 1207 tree value = TREE_VALUE (chain); 1208 if (TREE_CODE (value) == CONST_DECL) 1209 value = DECL_INITIAL (value); 1210 node = splay_tree_lookup (cases, (splay_tree_key) value); 1211 if (node) 1212 { 1213 /* Mark the CASE_LOW part of the case entry as seen. */ 1214 tree label = (tree) node->value; 1215 CASE_LOW_SEEN (label) = 1; 1216 continue; 1217 } 1218 1219 /* Even though there wasn't an exact match, there might be a 1220 case range which includes the enumerator's value. */ 1221 node = splay_tree_predecessor (cases, (splay_tree_key) value); 1222 if (node && CASE_HIGH ((tree) node->value)) 1223 { 1224 tree label = (tree) node->value; 1225 int cmp = tree_int_cst_compare (CASE_HIGH (label), value); 1226 if (cmp >= 0) 1227 { 1228 /* If we match the upper bound exactly, mark the CASE_HIGH 1229 part of the case entry as seen. */ 1230 if (cmp == 0) 1231 CASE_HIGH_SEEN (label) = 1; 1232 continue; 1233 } 1234 } 1235 1236 /* We've now determined that this enumerated literal isn't 1237 handled by the case labels of the switch statement. */ 1238 1239 /* If the switch expression is a constant, we only really care 1240 about whether that constant is handled by the switch. */ 1241 if (cond && tree_int_cst_compare (cond, value)) 1242 continue; 1243 1244 /* If there is a default_node, the only relevant option is 1245 Wswitch-enum. Otherwise, if both are enabled then we prefer 1246 to warn using -Wswitch because -Wswitch is enabled by -Wall 1247 while -Wswitch-enum is explicit. */ 1248 warning_at (switch_location, 1249 (default_node || !warn_switch 1250 ? OPT_Wswitch_enum 1251 : OPT_Wswitch), 1252 "enumeration value %qE not handled in switch", 1253 TREE_PURPOSE (chain)); 1254 } 1255 1256 /* Warn if there are case expressions that don't correspond to 1257 enumerators. This can occur since C and C++ don't enforce 1258 type-checking of assignments to enumeration variables. 1259 1260 The time complexity here is now always O(N) worst case, since 1261 we should have marked both the lower bound and upper bound of 1262 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN 1263 above. This scan also resets those fields. */ 1264 1265 splay_tree_foreach (cases, match_case_to_enum, type); 1266} 1267 1268/* Warn for A ?: C expressions (with B omitted) where A is a boolean 1269 expression, because B will always be true. */ 1270 1271void 1272warn_for_omitted_condop (location_t location, tree cond) 1273{ 1274 /* In C++ template declarations it can happen that the type is dependent 1275 and not yet known, thus TREE_TYPE (cond) == NULL_TREE. */ 1276 if (truth_value_p (TREE_CODE (cond)) 1277 || (TREE_TYPE (cond) != NULL_TREE 1278 && TREE_CODE (TREE_TYPE (cond)) == BOOLEAN_TYPE)) 1279 warning_at (location, OPT_Wparentheses, 1280 "the omitted middle operand in ?: will always be %<true%>, " 1281 "suggest explicit middle operand"); 1282} 1283 1284/* Give an error for storing into ARG, which is 'const'. USE indicates 1285 how ARG was being used. */ 1286 1287void 1288readonly_error (location_t loc, tree arg, enum lvalue_use use) 1289{ 1290 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement 1291 || use == lv_asm); 1292 /* Using this macro rather than (for example) arrays of messages 1293 ensures that all the format strings are checked at compile 1294 time. */ 1295#define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \ 1296 : (use == lv_increment ? (I) \ 1297 : (use == lv_decrement ? (D) : (AS)))) 1298 if (TREE_CODE (arg) == COMPONENT_REF) 1299 { 1300 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0)))) 1301 error_at (loc, READONLY_MSG (G_("assignment of member " 1302 "%qD in read-only object"), 1303 G_("increment of member " 1304 "%qD in read-only object"), 1305 G_("decrement of member " 1306 "%qD in read-only object"), 1307 G_("member %qD in read-only object " 1308 "used as %<asm%> output")), 1309 TREE_OPERAND (arg, 1)); 1310 else 1311 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"), 1312 G_("increment of read-only member %qD"), 1313 G_("decrement of read-only member %qD"), 1314 G_("read-only member %qD used as %<asm%> output")), 1315 TREE_OPERAND (arg, 1)); 1316 } 1317 else if (VAR_P (arg)) 1318 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"), 1319 G_("increment of read-only variable %qD"), 1320 G_("decrement of read-only variable %qD"), 1321 G_("read-only variable %qD used as %<asm%> output")), 1322 arg); 1323 else if (TREE_CODE (arg) == PARM_DECL) 1324 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"), 1325 G_("increment of read-only parameter %qD"), 1326 G_("decrement of read-only parameter %qD"), 1327 G_("read-only parameter %qD use as %<asm%> output")), 1328 arg); 1329 else if (TREE_CODE (arg) == RESULT_DECL) 1330 { 1331 gcc_assert (c_dialect_cxx ()); 1332 error_at (loc, READONLY_MSG (G_("assignment of " 1333 "read-only named return value %qD"), 1334 G_("increment of " 1335 "read-only named return value %qD"), 1336 G_("decrement of " 1337 "read-only named return value %qD"), 1338 G_("read-only named return value %qD " 1339 "used as %<asm%>output")), 1340 arg); 1341 } 1342 else if (TREE_CODE (arg) == FUNCTION_DECL) 1343 error_at (loc, READONLY_MSG (G_("assignment of function %qD"), 1344 G_("increment of function %qD"), 1345 G_("decrement of function %qD"), 1346 G_("function %qD used as %<asm%> output")), 1347 arg); 1348 else 1349 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"), 1350 G_("increment of read-only location %qE"), 1351 G_("decrement of read-only location %qE"), 1352 G_("read-only location %qE used as %<asm%> output")), 1353 arg); 1354} 1355 1356/* Print an error message for an invalid lvalue. USE says 1357 how the lvalue is being used and so selects the error message. LOC 1358 is the location for the error. */ 1359 1360void 1361lvalue_error (location_t loc, enum lvalue_use use) 1362{ 1363 switch (use) 1364 { 1365 case lv_assign: 1366 error_at (loc, "lvalue required as left operand of assignment"); 1367 break; 1368 case lv_increment: 1369 error_at (loc, "lvalue required as increment operand"); 1370 break; 1371 case lv_decrement: 1372 error_at (loc, "lvalue required as decrement operand"); 1373 break; 1374 case lv_addressof: 1375 error_at (loc, "lvalue required as unary %<&%> operand"); 1376 break; 1377 case lv_asm: 1378 error_at (loc, "lvalue required in asm statement"); 1379 break; 1380 default: 1381 gcc_unreachable (); 1382 } 1383} 1384 1385/* Print an error message for an invalid indirection of type TYPE. 1386 ERRSTRING is the name of the operator for the indirection. */ 1387 1388void 1389invalid_indirection_error (location_t loc, tree type, ref_operator errstring) 1390{ 1391 switch (errstring) 1392 { 1393 case RO_NULL: 1394 gcc_assert (c_dialect_cxx ()); 1395 error_at (loc, "invalid type argument (have %qT)", type); 1396 break; 1397 case RO_ARRAY_INDEXING: 1398 error_at (loc, 1399 "invalid type argument of array indexing (have %qT)", 1400 type); 1401 break; 1402 case RO_UNARY_STAR: 1403 error_at (loc, 1404 "invalid type argument of unary %<*%> (have %qT)", 1405 type); 1406 break; 1407 case RO_ARROW: 1408 error_at (loc, 1409 "invalid type argument of %<->%> (have %qT)", 1410 type); 1411 break; 1412 case RO_ARROW_STAR: 1413 error_at (loc, 1414 "invalid type argument of %<->*%> (have %qT)", 1415 type); 1416 break; 1417 case RO_IMPLICIT_CONVERSION: 1418 error_at (loc, 1419 "invalid type argument of implicit conversion (have %qT)", 1420 type); 1421 break; 1422 default: 1423 gcc_unreachable (); 1424 } 1425} 1426 1427/* Subscripting with type char is likely to lose on a machine where 1428 chars are signed. So warn on any machine, but optionally. Don't 1429 warn for unsigned char since that type is safe. Don't warn for 1430 signed char because anyone who uses that must have done so 1431 deliberately. Furthermore, we reduce the false positive load by 1432 warning only for non-constant value of type char. */ 1433 1434void 1435warn_array_subscript_with_type_char (location_t loc, tree index) 1436{ 1437 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node 1438 && TREE_CODE (index) != INTEGER_CST) 1439 warning_at (loc, OPT_Wchar_subscripts, 1440 "array subscript has type %<char%>"); 1441} 1442 1443/* Implement -Wparentheses for the unexpected C precedence rules, to 1444 cover cases like x + y << z which readers are likely to 1445 misinterpret. We have seen an expression in which CODE is a binary 1446 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which 1447 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and 1448 CODE_RIGHT may be ERROR_MARK, which means that that side of the 1449 expression was not formed using a binary or unary operator, or it 1450 was enclosed in parentheses. */ 1451 1452void 1453warn_about_parentheses (location_t loc, enum tree_code code, 1454 enum tree_code code_left, tree arg_left, 1455 enum tree_code code_right, tree arg_right) 1456{ 1457 if (!warn_parentheses) 1458 return; 1459 1460 /* This macro tests that the expression ARG with original tree code 1461 CODE appears to be a boolean expression. or the result of folding a 1462 boolean expression. */ 1463#define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \ 1464 (truth_value_p (TREE_CODE (ARG)) \ 1465 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \ 1466 /* Folding may create 0 or 1 integers from other expressions. */ \ 1467 || ((CODE) != INTEGER_CST \ 1468 && (integer_onep (ARG) || integer_zerop (ARG)))) 1469 1470 switch (code) 1471 { 1472 case LSHIFT_EXPR: 1473 if (code_left == PLUS_EXPR) 1474 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1475 "suggest parentheses around %<+%> inside %<<<%>"); 1476 else if (code_right == PLUS_EXPR) 1477 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1478 "suggest parentheses around %<+%> inside %<<<%>"); 1479 else if (code_left == MINUS_EXPR) 1480 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1481 "suggest parentheses around %<-%> inside %<<<%>"); 1482 else if (code_right == MINUS_EXPR) 1483 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1484 "suggest parentheses around %<-%> inside %<<<%>"); 1485 return; 1486 1487 case RSHIFT_EXPR: 1488 if (code_left == PLUS_EXPR) 1489 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1490 "suggest parentheses around %<+%> inside %<>>%>"); 1491 else if (code_right == PLUS_EXPR) 1492 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1493 "suggest parentheses around %<+%> inside %<>>%>"); 1494 else if (code_left == MINUS_EXPR) 1495 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1496 "suggest parentheses around %<-%> inside %<>>%>"); 1497 else if (code_right == MINUS_EXPR) 1498 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1499 "suggest parentheses around %<-%> inside %<>>%>"); 1500 return; 1501 1502 case TRUTH_ORIF_EXPR: 1503 if (code_left == TRUTH_ANDIF_EXPR) 1504 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1505 "suggest parentheses around %<&&%> within %<||%>"); 1506 else if (code_right == TRUTH_ANDIF_EXPR) 1507 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1508 "suggest parentheses around %<&&%> within %<||%>"); 1509 return; 1510 1511 case BIT_IOR_EXPR: 1512 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR 1513 || code_left == PLUS_EXPR || code_left == MINUS_EXPR) 1514 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1515 "suggest parentheses around arithmetic in operand of %<|%>"); 1516 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR 1517 || code_right == PLUS_EXPR || code_right == MINUS_EXPR) 1518 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1519 "suggest parentheses around arithmetic in operand of %<|%>"); 1520 /* Check cases like x|y==z */ 1521 else if (TREE_CODE_CLASS (code_left) == tcc_comparison) 1522 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1523 "suggest parentheses around comparison in operand of %<|%>"); 1524 else if (TREE_CODE_CLASS (code_right) == tcc_comparison) 1525 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1526 "suggest parentheses around comparison in operand of %<|%>"); 1527 /* Check cases like !x | y */ 1528 else if (code_left == TRUTH_NOT_EXPR 1529 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right)) 1530 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1531 "suggest parentheses around operand of " 1532 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>"); 1533 return; 1534 1535 case BIT_XOR_EXPR: 1536 if (code_left == BIT_AND_EXPR 1537 || code_left == PLUS_EXPR || code_left == MINUS_EXPR) 1538 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1539 "suggest parentheses around arithmetic in operand of %<^%>"); 1540 else if (code_right == BIT_AND_EXPR 1541 || code_right == PLUS_EXPR || code_right == MINUS_EXPR) 1542 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1543 "suggest parentheses around arithmetic in operand of %<^%>"); 1544 /* Check cases like x^y==z */ 1545 else if (TREE_CODE_CLASS (code_left) == tcc_comparison) 1546 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1547 "suggest parentheses around comparison in operand of %<^%>"); 1548 else if (TREE_CODE_CLASS (code_right) == tcc_comparison) 1549 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1550 "suggest parentheses around comparison in operand of %<^%>"); 1551 return; 1552 1553 case BIT_AND_EXPR: 1554 if (code_left == PLUS_EXPR) 1555 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1556 "suggest parentheses around %<+%> in operand of %<&%>"); 1557 else if (code_right == PLUS_EXPR) 1558 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1559 "suggest parentheses around %<+%> in operand of %<&%>"); 1560 else if (code_left == MINUS_EXPR) 1561 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1562 "suggest parentheses around %<-%> in operand of %<&%>"); 1563 else if (code_right == MINUS_EXPR) 1564 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1565 "suggest parentheses around %<-%> in operand of %<&%>"); 1566 /* Check cases like x&y==z */ 1567 else if (TREE_CODE_CLASS (code_left) == tcc_comparison) 1568 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1569 "suggest parentheses around comparison in operand of %<&%>"); 1570 else if (TREE_CODE_CLASS (code_right) == tcc_comparison) 1571 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1572 "suggest parentheses around comparison in operand of %<&%>"); 1573 /* Check cases like !x & y */ 1574 else if (code_left == TRUTH_NOT_EXPR 1575 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right)) 1576 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1577 "suggest parentheses around operand of " 1578 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>"); 1579 return; 1580 1581 case EQ_EXPR: 1582 if (TREE_CODE_CLASS (code_left) == tcc_comparison) 1583 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1584 "suggest parentheses around comparison in operand of %<==%>"); 1585 else if (TREE_CODE_CLASS (code_right) == tcc_comparison) 1586 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1587 "suggest parentheses around comparison in operand of %<==%>"); 1588 return; 1589 case NE_EXPR: 1590 if (TREE_CODE_CLASS (code_left) == tcc_comparison) 1591 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1592 "suggest parentheses around comparison in operand of %<!=%>"); 1593 else if (TREE_CODE_CLASS (code_right) == tcc_comparison) 1594 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1595 "suggest parentheses around comparison in operand of %<!=%>"); 1596 return; 1597 1598 default: 1599 if (TREE_CODE_CLASS (code) == tcc_comparison) 1600 { 1601 if (TREE_CODE_CLASS (code_left) == tcc_comparison 1602 && code_left != NE_EXPR && code_left != EQ_EXPR 1603 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left))) 1604 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, 1605 "comparisons like %<X<=Y<=Z%> do not " 1606 "have their mathematical meaning"); 1607 else if (TREE_CODE_CLASS (code_right) == tcc_comparison 1608 && code_right != NE_EXPR && code_right != EQ_EXPR 1609 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right))) 1610 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, 1611 "comparisons like %<X<=Y<=Z%> do not " 1612 "have their mathematical meaning"); 1613 } 1614 return; 1615 } 1616#undef NOT_A_BOOLEAN_EXPR_P 1617} 1618 1619/* If LABEL (a LABEL_DECL) has not been used, issue a warning. */ 1620 1621void 1622warn_for_unused_label (tree label) 1623{ 1624 if (!TREE_USED (label)) 1625 { 1626 if (DECL_INITIAL (label)) 1627 warning (OPT_Wunused_label, "label %q+D defined but not used", label); 1628 else 1629 warning (OPT_Wunused_label, "label %q+D declared but not defined", label); 1630 } 1631 else if (asan_sanitize_use_after_scope ()) 1632 { 1633 if (asan_used_labels == NULL) 1634 asan_used_labels = new hash_set<tree> (16); 1635 1636 asan_used_labels->add (label); 1637 } 1638} 1639 1640/* Warn for division by zero according to the value of DIVISOR. LOC 1641 is the location of the division operator. */ 1642 1643void 1644warn_for_div_by_zero (location_t loc, tree divisor) 1645{ 1646 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning 1647 about division by zero. Do not issue a warning if DIVISOR has a 1648 floating-point type, since we consider 0.0/0.0 a valid way of 1649 generating a NaN. */ 1650 if (c_inhibit_evaluation_warnings == 0 1651 && (integer_zerop (divisor) || fixed_zerop (divisor))) 1652 warning_at (loc, OPT_Wdiv_by_zero, "division by zero"); 1653} 1654 1655/* Warn for patterns where memset appears to be used incorrectly. The 1656 warning location should be LOC. ARG0, and ARG2 are the first and 1657 last arguments to the call, while LITERAL_ZERO_MASK has a 1 bit for 1658 each argument that was a literal zero. */ 1659 1660void 1661warn_for_memset (location_t loc, tree arg0, tree arg2, 1662 int literal_zero_mask) 1663{ 1664 if (warn_memset_transposed_args 1665 && integer_zerop (arg2) 1666 && (literal_zero_mask & (1 << 2)) != 0 1667 && (literal_zero_mask & (1 << 1)) == 0) 1668 warning_at (loc, OPT_Wmemset_transposed_args, 1669 "%<memset%> used with constant zero length " 1670 "parameter; this could be due to transposed " 1671 "parameters"); 1672 1673 if (warn_memset_elt_size && TREE_CODE (arg2) == INTEGER_CST) 1674 { 1675 STRIP_NOPS (arg0); 1676 if (TREE_CODE (arg0) == ADDR_EXPR) 1677 arg0 = TREE_OPERAND (arg0, 0); 1678 tree type = TREE_TYPE (arg0); 1679 if (type != NULL_TREE && TREE_CODE (type) == ARRAY_TYPE) 1680 { 1681 tree elt_type = TREE_TYPE (type); 1682 tree domain = TYPE_DOMAIN (type); 1683 if (!integer_onep (TYPE_SIZE_UNIT (elt_type)) 1684 && domain != NULL_TREE 1685 && TYPE_MAXVAL (domain) 1686 && TYPE_MINVAL (domain) 1687 && integer_zerop (TYPE_MINVAL (domain)) 1688 && integer_onep (fold_build2 (MINUS_EXPR, domain, 1689 arg2, 1690 TYPE_MAXVAL (domain)))) 1691 warning_at (loc, OPT_Wmemset_elt_size, 1692 "%<memset%> used with length equal to " 1693 "number of elements without multiplication " 1694 "by element size"); 1695 } 1696 } 1697} 1698 1699/* Subroutine of build_binary_op. Give warnings for comparisons 1700 between signed and unsigned quantities that may fail. Do the 1701 checking based on the original operand trees ORIG_OP0 and ORIG_OP1, 1702 so that casts will be considered, but default promotions won't 1703 be. 1704 1705 LOCATION is the location of the comparison operator. 1706 1707 The arguments of this function map directly to local variables 1708 of build_binary_op. */ 1709 1710void 1711warn_for_sign_compare (location_t location, 1712 tree orig_op0, tree orig_op1, 1713 tree op0, tree op1, 1714 tree result_type, enum tree_code resultcode) 1715{ 1716 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0)); 1717 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1)); 1718 int unsignedp0, unsignedp1; 1719 1720 /* In C++, check for comparison of different enum types. */ 1721 if (c_dialect_cxx() 1722 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE 1723 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE 1724 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0)) 1725 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1))) 1726 { 1727 warning_at (location, 1728 OPT_Wsign_compare, "comparison between types %qT and %qT", 1729 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1)); 1730 } 1731 1732 /* Do not warn if the comparison is being done in a signed type, 1733 since the signed type will only be chosen if it can represent 1734 all the values of the unsigned type. */ 1735 if (!TYPE_UNSIGNED (result_type)) 1736 /* OK */; 1737 /* Do not warn if both operands are unsigned. */ 1738 else if (op0_signed == op1_signed) 1739 /* OK */; 1740 else 1741 { 1742 tree sop, uop, base_type; 1743 bool ovf; 1744 1745 if (op0_signed) 1746 sop = orig_op0, uop = orig_op1; 1747 else 1748 sop = orig_op1, uop = orig_op0; 1749 1750 STRIP_TYPE_NOPS (sop); 1751 STRIP_TYPE_NOPS (uop); 1752 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE 1753 ? TREE_TYPE (result_type) : result_type); 1754 1755 /* Do not warn if the signed quantity is an unsuffixed integer 1756 literal (or some static constant expression involving such 1757 literals or a conditional expression involving such literals) 1758 and it is non-negative. */ 1759 if (tree_expr_nonnegative_warnv_p (sop, &ovf)) 1760 /* OK */; 1761 /* Do not warn if the comparison is an equality operation, the 1762 unsigned quantity is an integral constant, and it would fit 1763 in the result if the result were signed. */ 1764 else if (TREE_CODE (uop) == INTEGER_CST 1765 && (resultcode == EQ_EXPR || resultcode == NE_EXPR) 1766 && int_fits_type_p (uop, c_common_signed_type (base_type))) 1767 /* OK */; 1768 /* In C, do not warn if the unsigned quantity is an enumeration 1769 constant and its maximum value would fit in the result if the 1770 result were signed. */ 1771 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST 1772 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE 1773 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)), 1774 c_common_signed_type (base_type))) 1775 /* OK */; 1776 else 1777 warning_at (location, 1778 OPT_Wsign_compare, 1779 "comparison between signed and unsigned integer expressions"); 1780 } 1781 1782 /* Warn if two unsigned values are being compared in a size larger 1783 than their original size, and one (and only one) is the result of 1784 a `~' operator. This comparison will always fail. 1785 1786 Also warn if one operand is a constant, and the constant does not 1787 have all bits set that are set in the ~ operand when it is 1788 extended. */ 1789 1790 op0 = c_common_get_narrower (op0, &unsignedp0); 1791 op1 = c_common_get_narrower (op1, &unsignedp1); 1792 1793 if ((TREE_CODE (op0) == BIT_NOT_EXPR) 1794 ^ (TREE_CODE (op1) == BIT_NOT_EXPR)) 1795 { 1796 if (TREE_CODE (op0) == BIT_NOT_EXPR) 1797 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0); 1798 if (TREE_CODE (op1) == BIT_NOT_EXPR) 1799 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1); 1800 1801 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1)) 1802 { 1803 tree primop; 1804 HOST_WIDE_INT constant, mask; 1805 int unsignedp; 1806 unsigned int bits; 1807 1808 if (tree_fits_shwi_p (op0)) 1809 { 1810 primop = op1; 1811 unsignedp = unsignedp1; 1812 constant = tree_to_shwi (op0); 1813 } 1814 else 1815 { 1816 primop = op0; 1817 unsignedp = unsignedp0; 1818 constant = tree_to_shwi (op1); 1819 } 1820 1821 bits = TYPE_PRECISION (TREE_TYPE (primop)); 1822 if (bits < TYPE_PRECISION (result_type) 1823 && bits < HOST_BITS_PER_LONG && unsignedp) 1824 { 1825 mask = HOST_WIDE_INT_M1U << bits; 1826 if ((mask & constant) != mask) 1827 { 1828 if (constant == 0) 1829 warning_at (location, OPT_Wsign_compare, 1830 "promoted ~unsigned is always non-zero"); 1831 else 1832 warning_at (location, OPT_Wsign_compare, 1833 "comparison of promoted ~unsigned with constant"); 1834 } 1835 } 1836 } 1837 else if (unsignedp0 && unsignedp1 1838 && (TYPE_PRECISION (TREE_TYPE (op0)) 1839 < TYPE_PRECISION (result_type)) 1840 && (TYPE_PRECISION (TREE_TYPE (op1)) 1841 < TYPE_PRECISION (result_type))) 1842 warning_at (location, OPT_Wsign_compare, 1843 "comparison of promoted ~unsigned with unsigned"); 1844 } 1845} 1846 1847/* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common 1848 type via c_common_type. If -Wdouble-promotion is in use, and the 1849 conditions for warning have been met, issue a warning. GMSGID is 1850 the warning message. It must have two %T specifiers for the type 1851 that was converted (generally "float") and the type to which it was 1852 converted (generally "double), respectively. LOC is the location 1853 to which the warning should refer. */ 1854 1855void 1856do_warn_double_promotion (tree result_type, tree type1, tree type2, 1857 const char *gmsgid, location_t loc) 1858{ 1859 tree source_type; 1860 1861 if (!warn_double_promotion) 1862 return; 1863 /* If the conversion will not occur at run-time, there is no need to 1864 warn about it. */ 1865 if (c_inhibit_evaluation_warnings) 1866 return; 1867 /* If an invalid conversion has occured, don't warn. */ 1868 if (result_type == error_mark_node) 1869 return; 1870 if (TYPE_MAIN_VARIANT (result_type) != double_type_node 1871 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node) 1872 return; 1873 if (TYPE_MAIN_VARIANT (type1) == float_type_node 1874 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node) 1875 source_type = type1; 1876 else if (TYPE_MAIN_VARIANT (type2) == float_type_node 1877 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node) 1878 source_type = type2; 1879 else 1880 return; 1881 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type); 1882} 1883 1884/* Possibly warn about unused parameters. */ 1885 1886void 1887do_warn_unused_parameter (tree fn) 1888{ 1889 tree decl; 1890 1891 for (decl = DECL_ARGUMENTS (fn); 1892 decl; decl = DECL_CHAIN (decl)) 1893 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL 1894 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl) 1895 && !TREE_NO_WARNING (decl)) 1896 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter, 1897 "unused parameter %qD", decl); 1898} 1899 1900/* If DECL is a typedef that is declared in the current function, 1901 record it for the purpose of -Wunused-local-typedefs. */ 1902 1903void 1904record_locally_defined_typedef (tree decl) 1905{ 1906 struct c_language_function *l; 1907 1908 if (!warn_unused_local_typedefs 1909 || cfun == NULL 1910 /* if this is not a locally defined typedef then we are not 1911 interested. */ 1912 || !is_typedef_decl (decl) 1913 || !decl_function_context (decl)) 1914 return; 1915 1916 l = (struct c_language_function *) cfun->language; 1917 vec_safe_push (l->local_typedefs, decl); 1918} 1919 1920/* If T is a TYPE_DECL declared locally, mark it as used. */ 1921 1922void 1923maybe_record_typedef_use (tree t) 1924{ 1925 if (!is_typedef_decl (t)) 1926 return; 1927 1928 TREE_USED (t) = true; 1929} 1930 1931/* Warn if there are some unused locally defined typedefs in the 1932 current function. */ 1933 1934void 1935maybe_warn_unused_local_typedefs (void) 1936{ 1937 int i; 1938 tree decl; 1939 /* The number of times we have emitted -Wunused-local-typedefs 1940 warnings. If this is different from errorcount, that means some 1941 unrelated errors have been issued. In which case, we'll avoid 1942 emitting "unused-local-typedefs" warnings. */ 1943 static int unused_local_typedefs_warn_count; 1944 struct c_language_function *l; 1945 1946 if (cfun == NULL) 1947 return; 1948 1949 if ((l = (struct c_language_function *) cfun->language) == NULL) 1950 return; 1951 1952 if (warn_unused_local_typedefs 1953 && errorcount == unused_local_typedefs_warn_count) 1954 { 1955 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl) 1956 if (!TREE_USED (decl)) 1957 warning_at (DECL_SOURCE_LOCATION (decl), 1958 OPT_Wunused_local_typedefs, 1959 "typedef %qD locally defined but not used", decl); 1960 unused_local_typedefs_warn_count = errorcount; 1961 } 1962 1963 vec_free (l->local_typedefs); 1964} 1965 1966/* If we're creating an if-else-if condition chain, first see if we 1967 already have this COND in the CHAIN. If so, warn and don't add COND 1968 into the vector, otherwise add the COND there. LOC is the location 1969 of COND. */ 1970 1971void 1972warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain) 1973{ 1974 /* No chain has been created yet. Do nothing. */ 1975 if (*chain == NULL) 1976 return; 1977 1978 if (TREE_SIDE_EFFECTS (cond)) 1979 { 1980 /* Uh-oh! This condition has a side-effect, thus invalidates 1981 the whole chain. */ 1982 delete *chain; 1983 *chain = NULL; 1984 return; 1985 } 1986 1987 unsigned int ix; 1988 tree t; 1989 bool found = false; 1990 FOR_EACH_VEC_ELT (**chain, ix, t) 1991 if (operand_equal_p (cond, t, 0)) 1992 { 1993 if (warning_at (loc, OPT_Wduplicated_cond, 1994 "duplicated %<if%> condition")) 1995 inform (EXPR_LOCATION (t), "previously used here"); 1996 found = true; 1997 break; 1998 } 1999 2000 if (!found 2001 && !CONSTANT_CLASS_P (cond) 2002 /* Don't infinitely grow the chain. */ 2003 && (*chain)->length () < 512) 2004 (*chain)->safe_push (cond); 2005} 2006 2007/* Check and possibly warn if two declarations have contradictory 2008 attributes, such as always_inline vs. noinline. */ 2009 2010bool 2011diagnose_mismatched_attributes (tree olddecl, tree newdecl) 2012{ 2013 bool warned = false; 2014 2015 tree a1 = lookup_attribute ("optimize", DECL_ATTRIBUTES (olddecl)); 2016 tree a2 = lookup_attribute ("optimize", DECL_ATTRIBUTES (newdecl)); 2017 /* An optimization attribute applied on a declaration after the 2018 definition is likely not what the user wanted. */ 2019 if (a2 != NULL_TREE 2020 && DECL_SAVED_TREE (olddecl) != NULL_TREE 2021 && (a1 == NULL_TREE || !attribute_list_equal (a1, a2))) 2022 warned |= warning (OPT_Wattributes, 2023 "optimization attribute on %qD follows " 2024 "definition but the attribute doesn%'t match", 2025 newdecl); 2026 2027 /* Diagnose inline __attribute__ ((noinline)) which is silly. */ 2028 if (DECL_DECLARED_INLINE_P (newdecl) 2029 && DECL_UNINLINABLE (olddecl) 2030 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl))) 2031 warned |= warning (OPT_Wattributes, "inline declaration of %qD follows " 2032 "declaration with attribute noinline", newdecl); 2033 else if (DECL_DECLARED_INLINE_P (olddecl) 2034 && DECL_UNINLINABLE (newdecl) 2035 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))) 2036 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute " 2037 "noinline follows inline declaration ", newdecl); 2038 else if (lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)) 2039 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (olddecl))) 2040 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute " 2041 "%qs follows declaration with attribute %qs", 2042 newdecl, "noinline", "always_inline"); 2043 else if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (newdecl)) 2044 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl))) 2045 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute " 2046 "%qs follows declaration with attribute %qs", 2047 newdecl, "always_inline", "noinline"); 2048 else if (lookup_attribute ("cold", DECL_ATTRIBUTES (newdecl)) 2049 && lookup_attribute ("hot", DECL_ATTRIBUTES (olddecl))) 2050 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute " 2051 "%qs follows declaration with attribute %qs", 2052 newdecl, "cold", "hot"); 2053 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (newdecl)) 2054 && lookup_attribute ("cold", DECL_ATTRIBUTES (olddecl))) 2055 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute " 2056 "%qs follows declaration with attribute %qs", 2057 newdecl, "hot", "cold"); 2058 return warned; 2059} 2060 2061/* Warn if signed left shift overflows. We don't warn 2062 about left-shifting 1 into the sign bit in C++14; cf. 2063 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457> 2064 LOC is a location of the shift; OP0 and OP1 are the operands. 2065 Return true if an overflow is detected, false otherwise. */ 2066 2067bool 2068maybe_warn_shift_overflow (location_t loc, tree op0, tree op1) 2069{ 2070 if (TREE_CODE (op0) != INTEGER_CST 2071 || TREE_CODE (op1) != INTEGER_CST) 2072 return false; 2073 2074 tree type0 = TREE_TYPE (op0); 2075 unsigned int prec0 = TYPE_PRECISION (type0); 2076 2077 /* Left-hand operand must be signed. */ 2078 if (TYPE_UNSIGNED (type0)) 2079 return false; 2080 2081 unsigned int min_prec = (wi::min_precision (op0, SIGNED) 2082 + TREE_INT_CST_LOW (op1)); 2083 /* Handle the case of left-shifting 1 into the sign bit. 2084 * However, shifting 1 _out_ of the sign bit, as in 2085 * INT_MIN << 1, is considered an overflow. 2086 */ 2087 if (!tree_int_cst_sign_bit(op0) && min_prec == prec0 + 1) 2088 { 2089 /* Never warn for C++14 onwards. */ 2090 if (cxx_dialect >= cxx14) 2091 return false; 2092 /* Otherwise only if -Wshift-overflow=2. But return 2093 true to signal an overflow for the sake of integer 2094 constant expressions. */ 2095 if (warn_shift_overflow < 2) 2096 return true; 2097 } 2098 2099 bool overflowed = min_prec > prec0; 2100 if (overflowed && c_inhibit_evaluation_warnings == 0) 2101 warning_at (loc, OPT_Wshift_overflow_, 2102 "result of %qE requires %u bits to represent, " 2103 "but %qT only has %u bits", 2104 build2_loc (loc, LSHIFT_EXPR, type0, op0, op1), 2105 min_prec, type0, prec0); 2106 2107 return overflowed; 2108} 2109 2110/* Warn about boolean expression compared with an integer value different 2111 from true/false. Warns also e.g. about "(i1 == i2) == 2". 2112 LOC is the location of the comparison, CODE is its code, OP0 and OP1 2113 are the operands of the comparison. The caller must ensure that 2114 either operand is a boolean expression. */ 2115 2116void 2117maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0, 2118 tree op1) 2119{ 2120 if (TREE_CODE_CLASS (code) != tcc_comparison) 2121 return; 2122 2123 tree f, cst; 2124 if (f = fold_for_warn (op0), 2125 TREE_CODE (f) == INTEGER_CST) 2126 cst = op0 = f; 2127 else if (f = fold_for_warn (op1), 2128 TREE_CODE (f) == INTEGER_CST) 2129 cst = op1 = f; 2130 else 2131 return; 2132 2133 if (!integer_zerop (cst) && !integer_onep (cst)) 2134 { 2135 int sign = (TREE_CODE (op0) == INTEGER_CST 2136 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst)); 2137 if (code == EQ_EXPR 2138 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0) 2139 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0)) 2140 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE " 2141 "with boolean expression is always false", cst); 2142 else 2143 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE " 2144 "with boolean expression is always true", cst); 2145 } 2146 else if (integer_zerop (cst) || integer_onep (cst)) 2147 { 2148 /* If the non-constant operand isn't of a boolean type, we 2149 don't want to warn here. */ 2150 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0; 2151 /* Handle booleans promoted to integers. */ 2152 if (bool_promoted_to_int_p (noncst)) 2153 /* Warn. */; 2154 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE 2155 && !truth_value_p (TREE_CODE (noncst))) 2156 return; 2157 /* Do some magic to get the right diagnostics. */ 2158 bool flag = TREE_CODE (op0) == INTEGER_CST; 2159 flag = integer_zerop (cst) ? flag : !flag; 2160 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag)) 2161 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE " 2162 "with boolean expression is always true", cst); 2163 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag)) 2164 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE " 2165 "with boolean expression is always false", cst); 2166 } 2167} 2168 2169/* Warn if an argument at position param_pos is passed to a 2170 restrict-qualified param, and it aliases with another argument. */ 2171 2172void 2173warn_for_restrict (unsigned param_pos, tree *argarray, unsigned nargs) 2174{ 2175 tree arg = argarray[param_pos]; 2176 if (TREE_VISITED (arg) || integer_zerop (arg)) 2177 return; 2178 2179 location_t loc = EXPR_LOC_OR_LOC (arg, input_location); 2180 gcc_rich_location richloc (loc); 2181 2182 unsigned i; 2183 auto_vec<int, 16> arg_positions; 2184 2185 for (i = 0; i < nargs; i++) 2186 { 2187 if (i == param_pos) 2188 continue; 2189 2190 tree current_arg = argarray[i]; 2191 if (operand_equal_p (arg, current_arg, 0)) 2192 { 2193 TREE_VISITED (current_arg) = 1; 2194 arg_positions.safe_push (i + 1); 2195 } 2196 } 2197 2198 if (arg_positions.is_empty ()) 2199 return; 2200 2201 int pos; 2202 FOR_EACH_VEC_ELT (arg_positions, i, pos) 2203 { 2204 arg = argarray[pos - 1]; 2205 if (EXPR_HAS_LOCATION (arg)) 2206 richloc.add_range (EXPR_LOCATION (arg), false); 2207 } 2208 2209 warning_at_rich_loc_n (&richloc, OPT_Wrestrict, arg_positions.length (), 2210 "passing argument %i to restrict-qualified parameter" 2211 " aliases with argument %Z", 2212 "passing argument %i to restrict-qualified parameter" 2213 " aliases with arguments %Z", 2214 param_pos + 1, arg_positions.address (), 2215 arg_positions.length ()); 2216} 2217 2218/* Callback function to determine whether an expression TP or one of its 2219 subexpressions comes from macro expansion. Used to suppress bogus 2220 warnings. */ 2221 2222static tree 2223expr_from_macro_expansion_r (tree *tp, int *, void *) 2224{ 2225 if (CAN_HAVE_LOCATION_P (*tp) 2226 && from_macro_expansion_at (EXPR_LOCATION (*tp))) 2227 return integer_zero_node; 2228 2229 return NULL_TREE; 2230} 2231 2232/* Possibly warn when an if-else has identical branches. */ 2233 2234static void 2235do_warn_duplicated_branches (tree expr) 2236{ 2237 tree thenb = COND_EXPR_THEN (expr); 2238 tree elseb = COND_EXPR_ELSE (expr); 2239 2240 /* Don't bother if there's no else branch. */ 2241 if (elseb == NULL_TREE) 2242 return; 2243 2244 /* And don't warn for empty statements. */ 2245 if (TREE_CODE (thenb) == NOP_EXPR 2246 && TREE_TYPE (thenb) == void_type_node 2247 && TREE_OPERAND (thenb, 0) == size_zero_node) 2248 return; 2249 2250 /* ... or empty branches. */ 2251 if (TREE_CODE (thenb) == STATEMENT_LIST 2252 && STATEMENT_LIST_HEAD (thenb) == NULL) 2253 return; 2254 2255 /* Compute the hash of the then branch. */ 2256 inchash::hash hstate0 (0); 2257 inchash::add_expr (thenb, hstate0); 2258 hashval_t h0 = hstate0.end (); 2259 2260 /* Compute the hash of the else branch. */ 2261 inchash::hash hstate1 (0); 2262 inchash::add_expr (elseb, hstate1); 2263 hashval_t h1 = hstate1.end (); 2264 2265 /* Compare the hashes. */ 2266 if (h0 == h1 2267 && operand_equal_p (thenb, elseb, OEP_LEXICOGRAPHIC) 2268 /* Don't warn if any of the branches or their subexpressions comes 2269 from a macro. */ 2270 && !walk_tree_without_duplicates (&thenb, expr_from_macro_expansion_r, 2271 NULL) 2272 && !walk_tree_without_duplicates (&elseb, expr_from_macro_expansion_r, 2273 NULL)) 2274 warning_at (EXPR_LOCATION (expr), OPT_Wduplicated_branches, 2275 "this condition has identical branches"); 2276} 2277 2278/* Callback for c_genericize to implement -Wduplicated-branches. */ 2279 2280tree 2281do_warn_duplicated_branches_r (tree *tp, int *, void *) 2282{ 2283 if (TREE_CODE (*tp) == COND_EXPR) 2284 do_warn_duplicated_branches (*tp); 2285 return NULL_TREE; 2286} 2287