1 /* Diagnostic routines shared by all languages that are variants of C.
2 Copyright (C) 1992-2024 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #define INCLUDE_STRING
23 #include "coretypes.h"
30 #include "diagnostic.h"
32 #include "stringpool.h"
35 #include "gcc-rich-location.h"
37 #include "c-family/c-indentation.h"
38 #include "c-family/c-spellcheck.h"
40 #include "stor-layout.h"
41 #include "tree-pretty-print.h"
42 #include "langhooks.h"
44 /* Print a warning if a constant expression had overflow in folding.
45 Invoke this function on every expression that the language
46 requires to be a constant expression.
47 Note the ANSI C standard says it is erroneous for a
48 constant expression to overflow. */
51 constant_expression_warning (tree value
)
53 if (warn_overflow
&& pedantic
54 && (TREE_CODE (value
) == INTEGER_CST
|| TREE_CODE (value
) == REAL_CST
55 || TREE_CODE (value
) == FIXED_CST
56 || TREE_CODE (value
) == VECTOR_CST
57 || TREE_CODE (value
) == COMPLEX_CST
)
58 && TREE_OVERFLOW (value
))
59 pedwarn (input_location
, OPT_Woverflow
, "overflow in constant expression");
62 /* The same as above but print an unconditional error. */
65 constant_expression_error (tree value
)
67 if ((TREE_CODE (value
) == INTEGER_CST
|| TREE_CODE (value
) == REAL_CST
68 || TREE_CODE (value
) == FIXED_CST
69 || TREE_CODE (value
) == VECTOR_CST
70 || TREE_CODE (value
) == COMPLEX_CST
)
71 && TREE_OVERFLOW (value
))
72 error ("overflow in constant expression");
75 /* Print a warning if an expression result VALUE had an overflow
76 in folding and its operands hadn't. EXPR, which may be null, is
77 the operand of the expression.
79 Invoke this function on every expression that
80 (1) appears in the source code, and
81 (2) is a constant expression that overflowed, and
82 (3) is not already checked by convert_and_check;
83 however, do not invoke this function on operands of explicit casts
84 or when the expression is the result of an operator and any operand
85 already overflowed. */
88 overflow_warning (location_t loc
, tree value
, tree expr
)
90 if (c_inhibit_evaluation_warnings
!= 0)
93 const char *warnfmt
= NULL
;
95 switch (TREE_CODE (value
))
99 ? G_("integer overflow in expression %qE of type %qT "
101 : G_("integer overflow in expression of type %qT "
107 ? G_("floating point overflow in expression %qE "
108 "of type %qT results in %qE")
109 : G_("floating point overflow in expression of type %qT "
115 ? G_("fixed-point overflow in expression %qE of type %qT "
117 : G_("fixed-point overflow in expression of type %qT "
123 ? G_("vector overflow in expression %qE of type %qT "
125 : G_("vector overflow in expression of type %qT "
130 if (TREE_CODE (TREE_REALPART (value
)) == INTEGER_CST
)
132 ? G_("complex integer overflow in expression %qE "
133 "of type %qT results in %qE")
134 : G_("complex integer overflow in expression of type %qT "
136 else if (TREE_CODE (TREE_REALPART (value
)) == REAL_CST
)
138 ? G_("complex floating point overflow in expression %qE "
139 "of type %qT results in %qE")
140 : G_("complex floating point overflow in expression "
141 "of type %qT results in %qE"));
152 warned
= warning_at (loc
, OPT_Woverflow
, warnfmt
, expr
, TREE_TYPE (expr
),
155 warned
= warning_at (loc
, OPT_Woverflow
, warnfmt
, TREE_TYPE (value
),
159 suppress_warning (value
, OPT_Woverflow
);
162 /* Helper function for walk_tree. Unwrap C_MAYBE_CONST_EXPRs in an expression
166 unwrap_c_maybe_const (tree
*tp
, int *walk_subtrees
, void *)
168 if (TREE_CODE (*tp
) == C_MAYBE_CONST_EXPR
)
170 *tp
= C_MAYBE_CONST_EXPR_EXPR (*tp
);
171 /* C_MAYBE_CONST_EXPRs don't nest. */
172 *walk_subtrees
= false;
177 /* Warn about uses of logical || / && operator in a context where it
178 is likely that the bitwise equivalent was intended by the
179 programmer. We have seen an expression in which CODE is a binary
180 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
181 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
184 warn_logical_operator (location_t location
, enum tree_code code
, tree type
,
185 enum tree_code code_left
, tree op_left
,
186 enum tree_code
ARG_UNUSED (code_right
), tree op_right
)
188 int or_op
= (code
== TRUTH_ORIF_EXPR
|| code
== TRUTH_OR_EXPR
);
189 int in0_p
, in1_p
, in_p
;
190 tree low0
, low1
, low
, high0
, high1
, high
, lhs
, rhs
, tem
;
191 bool strict_overflow_p
= false;
193 if (!warn_logical_op
)
196 if (code
!= TRUTH_ANDIF_EXPR
197 && code
!= TRUTH_AND_EXPR
198 && code
!= TRUTH_ORIF_EXPR
199 && code
!= TRUTH_OR_EXPR
)
202 /* We don't want to warn if either operand comes from a macro
203 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
205 if (from_macro_expansion_at (EXPR_LOCATION (op_left
))
206 || from_macro_expansion_at (EXPR_LOCATION (op_right
)))
209 /* Warn if &&/|| are being used in a context where it is
210 likely that the bitwise equivalent was intended by the
211 programmer. That is, an expression such as op && MASK
212 where op should not be any boolean expression, nor a
213 constant, and mask seems to be a non-boolean integer constant. */
214 STRIP_ANY_LOCATION_WRAPPER (op_right
);
215 if (TREE_CODE (op_right
) == CONST_DECL
)
216 /* An enumerator counts as a constant. */
217 op_right
= DECL_INITIAL (op_right
);
218 tree stripped_op_left
= tree_strip_any_location_wrapper (op_left
);
219 if (!truth_value_p (code_left
)
220 && INTEGRAL_TYPE_P (TREE_TYPE (op_left
))
221 && !CONSTANT_CLASS_P (stripped_op_left
)
222 && TREE_CODE (stripped_op_left
) != CONST_DECL
223 && !warning_suppressed_p (op_left
, OPT_Wlogical_op
)
224 && TREE_CODE (op_right
) == INTEGER_CST
225 && !integer_zerop (op_right
)
226 && !integer_onep (op_right
))
231 = warning_at (location
, OPT_Wlogical_op
,
232 "logical %<or%> applied to non-boolean constant");
235 = warning_at (location
, OPT_Wlogical_op
,
236 "logical %<and%> applied to non-boolean constant");
238 suppress_warning (op_left
, OPT_Wlogical_op
);
242 /* We do not warn for constants because they are typical of macro
243 expansions that test for features. */
244 if (CONSTANT_CLASS_P (fold_for_warn (op_left
))
245 || CONSTANT_CLASS_P (fold_for_warn (op_right
)))
248 /* This warning only makes sense with logical operands. */
249 if (!(truth_value_p (TREE_CODE (op_left
))
250 || INTEGRAL_TYPE_P (TREE_TYPE (op_left
)))
251 || !(truth_value_p (TREE_CODE (op_right
))
252 || INTEGRAL_TYPE_P (TREE_TYPE (op_right
))))
255 /* The range computations only work with scalars. */
256 if (VECTOR_TYPE_P (TREE_TYPE (op_left
))
257 || VECTOR_TYPE_P (TREE_TYPE (op_right
)))
260 /* We first test whether either side separately is trivially true
261 (with OR) or trivially false (with AND). If so, do not warn.
262 This is a common idiom for testing ranges of data types in
264 op_left
= unshare_expr (op_left
);
265 walk_tree_without_duplicates (&op_left
, unwrap_c_maybe_const
, NULL
);
266 lhs
= make_range (op_left
, &in0_p
, &low0
, &high0
, &strict_overflow_p
);
270 /* If this is an OR operation, invert both sides; now, the result
271 should be always false to get a warning. */
275 tem
= build_range_check (UNKNOWN_LOCATION
, type
, lhs
, in0_p
, low0
, high0
);
276 if (tem
&& integer_zerop (tem
))
279 op_right
= unshare_expr (op_right
);
280 walk_tree_without_duplicates (&op_right
, unwrap_c_maybe_const
, NULL
);
281 rhs
= make_range (op_right
, &in1_p
, &low1
, &high1
, &strict_overflow_p
);
285 /* If this is an OR operation, invert both sides; now, the result
286 should be always false to get a warning. */
290 tem
= build_range_check (UNKNOWN_LOCATION
, type
, rhs
, in1_p
, low1
, high1
);
291 if (tem
&& integer_zerop (tem
))
294 /* If both expressions have the same operand, if we can merge the
296 if (operand_equal_p (lhs
, rhs
, 0)
297 && merge_ranges (&in_p
, &low
, &high
, in0_p
, low0
, high0
,
300 tem
= build_range_check (UNKNOWN_LOCATION
, type
, lhs
, in_p
, low
, high
);
301 /* ... and if the range test is always false, then warn. */
302 if (tem
&& integer_zerop (tem
))
305 warning_at (location
, OPT_Wlogical_op
,
306 "logical %<or%> of collectively exhaustive tests is "
309 warning_at (location
, OPT_Wlogical_op
,
310 "logical %<and%> of mutually exclusive tests is "
313 /* Or warn if the operands have exactly the same range, e.g.
315 else if (tree_int_cst_equal (low0
, low1
)
316 && tree_int_cst_equal (high0
, high1
))
319 warning_at (location
, OPT_Wlogical_op
,
320 "logical %<or%> of equal expressions");
322 warning_at (location
, OPT_Wlogical_op
,
323 "logical %<and%> of equal expressions");
328 /* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
329 with constant indices. */
332 find_array_ref_with_const_idx_r (tree
*expr_p
, int *, void *)
336 if ((TREE_CODE (expr
) == ARRAY_REF
337 || TREE_CODE (expr
) == ARRAY_RANGE_REF
)
338 && (TREE_CODE (fold_for_warn (TREE_OPERAND (expr
, 1)))
340 return integer_type_node
;
345 /* Subroutine of warn_tautological_cmp. Warn about bitwise comparison
346 that always evaluate to true or false. LOC is the location of the
347 ==/!= comparison specified by CODE; LHS and RHS are the usual operands
348 of this comparison. */
351 warn_tautological_bitwise_comparison (const op_location_t
&loc
, tree_code code
,
354 if (code
!= EQ_EXPR
&& code
!= NE_EXPR
)
357 /* Extract the operands from e.g. (x & 8) == 4. */
360 tree stripped_lhs
= tree_strip_any_location_wrapper (lhs
);
361 tree stripped_rhs
= tree_strip_any_location_wrapper (rhs
);
362 if ((TREE_CODE (lhs
) == BIT_AND_EXPR
363 || TREE_CODE (lhs
) == BIT_IOR_EXPR
)
364 && TREE_CODE (stripped_rhs
) == INTEGER_CST
)
365 bitop
= lhs
, cst
= stripped_rhs
;
366 else if ((TREE_CODE (rhs
) == BIT_AND_EXPR
367 || TREE_CODE (rhs
) == BIT_IOR_EXPR
)
368 && TREE_CODE (stripped_lhs
) == INTEGER_CST
)
369 bitop
= rhs
, cst
= stripped_lhs
;
374 tree bitop_op0
= fold_for_warn (TREE_OPERAND (bitop
, 0));
375 if (TREE_CODE (bitop_op0
) == INTEGER_CST
)
376 bitopcst
= bitop_op0
;
378 tree bitop_op1
= fold_for_warn (TREE_OPERAND (bitop
, 1));
379 if (TREE_CODE (bitop_op1
) == INTEGER_CST
)
380 bitopcst
= bitop_op1
;
385 /* Note that the two operands are from before the usual integer
386 conversions, so their types might not be the same.
387 Use the larger of the two precisions and ignore bits outside
389 int prec
= MAX (TYPE_PRECISION (TREE_TYPE (cst
)),
390 TYPE_PRECISION (TREE_TYPE (bitopcst
)));
392 wide_int bitopcstw
= wi::to_wide (bitopcst
, prec
);
393 wide_int cstw
= wi::to_wide (cst
, prec
);
396 if (TREE_CODE (bitop
) == BIT_AND_EXPR
)
397 res
= bitopcstw
& cstw
;
399 res
= bitopcstw
| cstw
;
401 /* For BIT_AND only warn if (CST2 & CST1) != CST1, and
402 for BIT_OR only if (CST2 | CST1) != CST1. */
406 binary_op_rich_location
richloc (loc
, lhs
, rhs
, false);
408 warning_at (&richloc
, OPT_Wtautological_compare
,
409 "bitwise comparison always evaluates to false");
411 warning_at (&richloc
, OPT_Wtautological_compare
,
412 "bitwise comparison always evaluates to true");
415 /* Given LOC from a macro expansion, return the map for the outermost
416 macro in the nest of expansions. */
418 static const line_map_macro
*
419 get_outermost_macro_expansion (location_t loc
)
421 gcc_assert (from_macro_expansion_at (loc
));
423 const line_map
*map
= linemap_lookup (line_table
, loc
);
424 const line_map_macro
*macro_map
;
427 macro_map
= linemap_check_macro (map
);
428 loc
= linemap_unwind_toward_expansion (line_table
, loc
, &map
);
429 } while (linemap_macro_expansion_map_p (map
));
434 /* Given LOC_A and LOC_B from macro expansions, return true if
435 they are "spelled the same" i.e. if they are both directly from
436 expansion of the same non-function-like macro. */
439 spelled_the_same_p (location_t loc_a
, location_t loc_b
)
441 gcc_assert (from_macro_expansion_at (loc_a
));
442 gcc_assert (from_macro_expansion_at (loc_b
));
444 const line_map_macro
*map_a
= get_outermost_macro_expansion (loc_a
);
445 const line_map_macro
*map_b
= get_outermost_macro_expansion (loc_b
);
447 if (map_a
->macro
== map_b
->macro
)
448 if (!cpp_fun_like_macro_p (map_a
->macro
))
454 /* Warn if a self-comparison always evaluates to true or false. LOC
455 is the location of the comparison with code CODE, LHS and RHS are
456 operands of the comparison. */
459 warn_tautological_cmp (const op_location_t
&loc
, enum tree_code code
,
462 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
465 /* Don't warn for various macro expansions. */
466 if (from_macro_expansion_at (loc
))
468 bool lhs_in_macro
= from_macro_expansion_at (EXPR_LOCATION (lhs
));
469 bool rhs_in_macro
= from_macro_expansion_at (EXPR_LOCATION (rhs
));
470 if (lhs_in_macro
|| rhs_in_macro
)
472 /* Don't warn if exactly one is from a macro. */
473 if (!(lhs_in_macro
&& rhs_in_macro
))
476 /* If both are in a macro, only warn if they're spelled the same. */
477 if (!spelled_the_same_p (EXPR_LOCATION (lhs
), EXPR_LOCATION (rhs
)))
481 warn_tautological_bitwise_comparison (loc
, code
, lhs
, rhs
);
483 /* We do not warn for constants because they are typical of macro
484 expansions that test for features, sizeof, and similar. */
485 if (CONSTANT_CLASS_P (fold_for_warn (lhs
))
486 || CONSTANT_CLASS_P (fold_for_warn (rhs
)))
489 /* Don't warn for e.g.
492 if (n == (long) n) ...
494 if ((CONVERT_EXPR_P (lhs
) || TREE_CODE (lhs
) == NON_LVALUE_EXPR
)
495 || (CONVERT_EXPR_P (rhs
) || TREE_CODE (rhs
) == NON_LVALUE_EXPR
))
498 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
499 It could be a NaN, and NaN never compares equal to anything, even
501 if (FLOAT_TYPE_P (TREE_TYPE (lhs
)) || FLOAT_TYPE_P (TREE_TYPE (rhs
)))
504 if (operand_equal_p (lhs
, rhs
, 0))
506 /* Don't warn about array references with constant indices;
507 these are likely to come from a macro. */
508 if (walk_tree_without_duplicates (&lhs
, find_array_ref_with_const_idx_r
,
511 const bool always_true
= (code
== EQ_EXPR
|| code
== LE_EXPR
512 || code
== GE_EXPR
|| code
== UNLE_EXPR
513 || code
== UNGE_EXPR
|| code
== UNEQ_EXPR
);
514 binary_op_rich_location
richloc (loc
, lhs
, rhs
, false);
516 warning_at (&richloc
, OPT_Wtautological_compare
,
517 "self-comparison always evaluates to true");
519 warning_at (&richloc
, OPT_Wtautological_compare
,
520 "self-comparison always evaluates to false");
524 /* Return true iff EXPR only contains boolean operands, or comparisons. */
527 expr_has_boolean_operands_p (tree expr
)
531 if (CONVERT_EXPR_P (expr
))
532 return bool_promoted_to_int_p (expr
);
533 else if (UNARY_CLASS_P (expr
))
534 return expr_has_boolean_operands_p (TREE_OPERAND (expr
, 0));
535 else if (BINARY_CLASS_P (expr
))
536 return (expr_has_boolean_operands_p (TREE_OPERAND (expr
, 0))
537 && expr_has_boolean_operands_p (TREE_OPERAND (expr
, 1)));
538 else if (COMPARISON_CLASS_P (expr
))
544 /* Warn about logical not used on the left hand side operand of a comparison.
545 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
546 Do not warn if RHS is of a boolean type, a logical operator, or
550 warn_logical_not_parentheses (location_t location
, enum tree_code code
,
553 if (TREE_CODE_CLASS (code
) != tcc_comparison
554 || TREE_TYPE (rhs
) == NULL_TREE
555 || TREE_CODE (TREE_TYPE (rhs
)) == BOOLEAN_TYPE
556 || truth_value_p (TREE_CODE (rhs
)))
559 /* Don't warn for expression like !x == ~(bool1 | bool2). */
560 if (expr_has_boolean_operands_p (rhs
))
563 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
564 !(x == 0) or !(y != 0). */
565 if ((code
== EQ_EXPR
|| code
== NE_EXPR
)
566 && integer_zerop (rhs
))
569 auto_diagnostic_group d
;
570 if (warning_at (location
, OPT_Wlogical_not_parentheses
,
571 "logical not is only applied to the left hand side of "
573 && EXPR_HAS_LOCATION (lhs
))
575 location_t lhs_loc
= EXPR_LOCATION (lhs
);
576 rich_location
richloc (line_table
, lhs_loc
);
577 richloc
.add_fixit_insert_before (lhs_loc
, "(");
578 richloc
.add_fixit_insert_after (lhs_loc
, ")");
579 inform (&richloc
, "add parentheses around left hand side "
580 "expression to silence this warning");
584 /* Warn if EXP contains any computations whose results are not used.
585 Return true if a warning is printed; false otherwise. LOCUS is the
586 (potential) location of the expression. */
589 warn_if_unused_value (const_tree exp
, location_t locus
, bool quiet
)
592 if (TREE_USED (exp
) || warning_suppressed_p (exp
, OPT_Wunused_value
))
595 /* Don't warn about void constructs. This includes casting to void,
596 void function calls, and statement expressions with a final cast
598 if (VOID_TYPE_P (TREE_TYPE (exp
)))
601 if (EXPR_HAS_LOCATION (exp
))
602 locus
= EXPR_LOCATION (exp
);
604 switch (TREE_CODE (exp
))
606 case PREINCREMENT_EXPR
:
607 case POSTINCREMENT_EXPR
:
608 case PREDECREMENT_EXPR
:
609 case POSTDECREMENT_EXPR
:
620 /* For a binding, warn if no side effect within it. */
621 exp
= BIND_EXPR_BODY (exp
);
625 case NON_LVALUE_EXPR
:
627 exp
= TREE_OPERAND (exp
, 0);
630 case TRUTH_ORIF_EXPR
:
631 case TRUTH_ANDIF_EXPR
:
632 /* In && or ||, warn if 2nd operand has no side effect. */
633 exp
= TREE_OPERAND (exp
, 1);
637 if (warn_if_unused_value (TREE_OPERAND (exp
, 0), locus
, quiet
))
639 /* Let people do `(foo (), 0)' without a warning. */
640 if (TREE_CONSTANT (TREE_OPERAND (exp
, 1)))
642 exp
= TREE_OPERAND (exp
, 1);
646 /* If this is an expression with side effects, don't warn; this
647 case commonly appears in macro expansions. */
648 if (TREE_SIDE_EFFECTS (exp
))
653 /* Warn only if both operands are unused. */
654 if (warn_if_unused_value (TREE_OPERAND (exp
, 0), locus
, true)
655 && warn_if_unused_value (TREE_OPERAND (exp
, 1), locus
, true))
660 /* Don't warn about automatic dereferencing of references, since
661 the user cannot control it. */
662 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp
, 0))) == REFERENCE_TYPE
)
664 exp
= TREE_OPERAND (exp
, 0);
670 /* Referencing a volatile value is a side effect, so don't warn. */
671 if ((DECL_P (exp
) || REFERENCE_CLASS_P (exp
))
672 && TREE_THIS_VOLATILE (exp
))
675 /* If this is an expression which has no operands, there is no value
676 to be unused. There are no such language-independent codes,
677 but front ends may define such. */
678 if (EXPRESSION_CLASS_P (exp
) && TREE_OPERAND_LENGTH (exp
) == 0)
684 return warning_at (locus
, OPT_Wunused_value
, "value computed is not used");
688 /* Print a warning about casts that might indicate violation of strict
689 aliasing rules if -Wstrict-aliasing is used and strict aliasing
690 mode is in effect. LOC is the location of the expression being
691 cast, EXPR might be from inside it. TYPE is the type we're casting
695 strict_aliasing_warning (location_t loc
, tree type
, tree expr
)
697 if (loc
== UNKNOWN_LOCATION
)
698 loc
= input_location
;
700 /* Strip pointer conversion chains and get to the correct original type. */
702 tree otype
= TREE_TYPE (expr
);
704 if (!(flag_strict_aliasing
705 && POINTER_TYPE_P (type
)
706 && POINTER_TYPE_P (otype
)
707 && !VOID_TYPE_P (TREE_TYPE (type
)))
708 /* If the type we are casting to is a ref-all pointer
709 dereferencing it is always valid. */
710 || TYPE_REF_CAN_ALIAS_ALL (type
))
713 if ((warn_strict_aliasing
> 1) && TREE_CODE (expr
) == ADDR_EXPR
714 && (DECL_P (TREE_OPERAND (expr
, 0))
715 || handled_component_p (TREE_OPERAND (expr
, 0))))
717 /* Casting the address of an object to non void pointer. Warn
718 if the cast breaks type based aliasing. */
719 if (!COMPLETE_TYPE_P (TREE_TYPE (type
)) && warn_strict_aliasing
== 2)
721 warning_at (loc
, OPT_Wstrict_aliasing
,
722 "type-punning to incomplete type "
723 "might break strict-aliasing rules");
728 /* warn_strict_aliasing >= 3. This includes the default (3).
729 Only warn if the cast is dereferenced immediately. */
731 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr
, 0)));
732 alias_set_type set2
= get_alias_set (TREE_TYPE (type
));
736 && !alias_set_subset_of (set2
, set1
)
737 && !alias_sets_conflict_p (set1
, set2
))
739 warning_at (loc
, OPT_Wstrict_aliasing
,
740 "dereferencing type-punned "
741 "pointer will break strict-aliasing rules");
744 else if (warn_strict_aliasing
== 2
745 && !alias_sets_must_conflict_p (set1
, set2
))
747 warning_at (loc
, OPT_Wstrict_aliasing
,
748 "dereferencing type-punned "
749 "pointer might break strict-aliasing rules");
754 else if ((warn_strict_aliasing
== 1) && !VOID_TYPE_P (TREE_TYPE (otype
)))
756 /* At this level, warn for any conversions, even if an address is
757 not taken in the same statement. This will likely produce many
758 false positives, but could be useful to pinpoint problems that
759 are not revealed at higher levels. */
760 alias_set_type set1
= get_alias_set (TREE_TYPE (otype
));
761 alias_set_type set2
= get_alias_set (TREE_TYPE (type
));
762 if (!COMPLETE_TYPE_P (TREE_TYPE (type
))
763 || !alias_sets_must_conflict_p (set1
, set2
))
765 warning_at (loc
, OPT_Wstrict_aliasing
,
766 "dereferencing type-punned "
767 "pointer might break strict-aliasing rules");
775 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
776 sizeof as last operand of certain builtins. */
779 sizeof_pointer_memaccess_warning (location_t
*sizeof_arg_loc
, tree callee
,
780 vec
<tree
, va_gc
> *params
, tree
*sizeof_arg
,
781 bool (*comp_types
) (tree
, tree
))
783 tree type
, dest
= NULL_TREE
, src
= NULL_TREE
, tem
;
784 bool strop
= false, cmp
= false;
785 unsigned int idx
= ~0;
788 if (TREE_CODE (callee
) != FUNCTION_DECL
789 || !fndecl_built_in_p (callee
, BUILT_IN_NORMAL
)
790 || vec_safe_length (params
) <= 1)
793 enum built_in_function fncode
= DECL_FUNCTION_CODE (callee
);
796 case BUILT_IN_STRNCMP
:
797 case BUILT_IN_STRNCASECMP
:
800 case BUILT_IN_STRNCPY
:
801 case BUILT_IN_STRNCPY_CHK
:
802 case BUILT_IN_STRNCAT
:
803 case BUILT_IN_STRNCAT_CHK
:
804 case BUILT_IN_STPNCPY
:
805 case BUILT_IN_STPNCPY_CHK
:
808 case BUILT_IN_MEMCPY
:
809 case BUILT_IN_MEMCPY_CHK
:
810 case BUILT_IN_MEMMOVE
:
811 case BUILT_IN_MEMMOVE_CHK
:
812 if (params
->length () < 3)
819 if (params
->length () < 3)
825 case BUILT_IN_MEMCMP
:
827 if (params
->length () < 3)
834 case BUILT_IN_MEMSET
:
835 case BUILT_IN_MEMSET_CHK
:
836 if (params
->length () < 3)
845 case BUILT_IN_STRNDUP
:
850 case BUILT_IN_MEMCHR
:
851 if (params
->length () < 3)
856 case BUILT_IN_SNPRINTF
:
857 case BUILT_IN_SNPRINTF_CHK
:
858 case BUILT_IN_VSNPRINTF
:
859 case BUILT_IN_VSNPRINTF_CHK
:
871 /* Use error_operand_p to detect non-error arguments with an error
872 type that the C++ front-end constructs. */
873 if (error_operand_p (src
)
874 || error_operand_p (dest
)
876 || error_operand_p (sizeof_arg
[idx
]))
879 type
= TYPE_P (sizeof_arg
[idx
])
880 ? sizeof_arg
[idx
] : TREE_TYPE (sizeof_arg
[idx
]);
882 if (!POINTER_TYPE_P (type
))
884 /* The argument type may be an array. Diagnose bounded string
885 copy functions that specify the bound in terms of the source
886 argument rather than the destination unless they are equal
887 to one another. Handle constant sizes and also try to handle
888 sizeof expressions involving VLAs. */
889 if (strop
&& !cmp
&& fncode
!= BUILT_IN_STRNDUP
&& src
)
891 tem
= tree_strip_nop_conversions (src
);
892 if (TREE_CODE (tem
) == ADDR_EXPR
)
893 tem
= TREE_OPERAND (tem
, 0);
895 /* Avoid diagnosing sizeof SRC when SRC is declared with
896 attribute nonstring. */
898 if (get_attr_nonstring_decl (tem
, &dummy
))
901 tree d
= tree_strip_nop_conversions (dest
);
902 if (TREE_CODE (d
) == ADDR_EXPR
)
903 d
= TREE_OPERAND (d
, 0);
905 tree dstsz
= TYPE_SIZE_UNIT (TREE_TYPE (d
));
906 tree srcsz
= TYPE_SIZE_UNIT (TREE_TYPE (tem
));
910 || !operand_equal_p (dstsz
, srcsz
, OEP_LEXICOGRAPHIC
))
911 && operand_equal_p (tem
, sizeof_arg
[idx
], OEP_ADDRESS_OF
))
912 warning_at (sizeof_arg_loc
[idx
], OPT_Wsizeof_pointer_memaccess
,
913 "argument to %<sizeof%> in %qD call is the same "
914 "expression as the source; did you mean to use "
915 "the size of the destination?",
923 && (tem
= tree_strip_nop_conversions (dest
))
924 && POINTER_TYPE_P (TREE_TYPE (tem
))
925 && comp_types (TREE_TYPE (TREE_TYPE (tem
)), type
))
929 && (tem
= tree_strip_nop_conversions (src
))
930 && POINTER_TYPE_P (TREE_TYPE (tem
))
931 && comp_types (TREE_TYPE (TREE_TYPE (tem
)), type
))
934 loc
= sizeof_arg_loc
[idx
];
938 if (!TYPE_P (sizeof_arg
[idx
])
939 && operand_equal_p (dest
, sizeof_arg
[idx
], 0)
940 && comp_types (TREE_TYPE (dest
), type
))
942 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
943 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
944 "argument to %<sizeof%> in %qD call is the same "
945 "expression as the destination; did you mean to "
946 "remove the addressof?", callee
);
947 else if ((TYPE_PRECISION (TREE_TYPE (type
))
948 == TYPE_PRECISION (char_type_node
))
950 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
951 "argument to %<sizeof%> in %qD call is the same "
952 "expression as the destination; did you mean to "
953 "provide an explicit length?", callee
);
955 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
956 "argument to %<sizeof%> in %qD call is the same "
957 "expression as the destination; did you mean to "
958 "dereference it?", callee
);
962 if (POINTER_TYPE_P (TREE_TYPE (dest
))
964 && comp_types (TREE_TYPE (dest
), type
)
965 && !VOID_TYPE_P (TREE_TYPE (type
)))
967 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
968 "argument to %<sizeof%> in %qD call is the same "
969 "pointer type %qT as the destination; expected %qT "
970 "or an explicit length", callee
, TREE_TYPE (dest
),
971 TREE_TYPE (TREE_TYPE (dest
)));
978 if (!TYPE_P (sizeof_arg
[idx
])
979 && operand_equal_p (src
, sizeof_arg
[idx
], 0)
980 && comp_types (TREE_TYPE (src
), type
))
982 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
983 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
984 "argument to %<sizeof%> in %qD call is the same "
985 "expression as the source; did you mean to "
986 "remove the addressof?", callee
);
987 else if ((TYPE_PRECISION (TREE_TYPE (type
))
988 == TYPE_PRECISION (char_type_node
))
990 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
991 "argument to %<sizeof%> in %qD call is the same "
992 "expression as the source; did you mean to "
993 "provide an explicit length?", callee
);
995 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
996 "argument to %<sizeof%> in %qD call is the same "
997 "expression as the source; did you mean to "
998 "dereference it?", callee
);
1002 if (POINTER_TYPE_P (TREE_TYPE (src
))
1004 && comp_types (TREE_TYPE (src
), type
)
1005 && !VOID_TYPE_P (TREE_TYPE (type
)))
1007 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
1008 "argument to %<sizeof%> in %qD call is the same "
1009 "pointer type %qT as the source; expected %qT "
1010 "or an explicit length", callee
, TREE_TYPE (src
),
1011 TREE_TYPE (TREE_TYPE (src
)));
1018 if (!TYPE_P (sizeof_arg
[idx
])
1019 && operand_equal_p (dest
, sizeof_arg
[idx
], 0)
1020 && comp_types (TREE_TYPE (dest
), type
))
1022 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
1023 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
1024 "argument to %<sizeof%> in %qD call is the same "
1025 "expression as the first source; did you mean to "
1026 "remove the addressof?", callee
);
1027 else if ((TYPE_PRECISION (TREE_TYPE (type
))
1028 == TYPE_PRECISION (char_type_node
))
1030 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
1031 "argument to %<sizeof%> in %qD call is the same "
1032 "expression as the first source; did you mean to "
1033 "provide an explicit length?", callee
);
1035 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
1036 "argument to %<sizeof%> in %qD call is the same "
1037 "expression as the first source; did you mean to "
1038 "dereference it?", callee
);
1042 if (POINTER_TYPE_P (TREE_TYPE (dest
))
1044 && comp_types (TREE_TYPE (dest
), type
)
1045 && !VOID_TYPE_P (TREE_TYPE (type
)))
1047 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
1048 "argument to %<sizeof%> in %qD call is the same "
1049 "pointer type %qT as the first source; expected %qT "
1050 "or an explicit length", callee
, TREE_TYPE (dest
),
1051 TREE_TYPE (TREE_TYPE (dest
)));
1058 if (!TYPE_P (sizeof_arg
[idx
])
1059 && operand_equal_p (src
, sizeof_arg
[idx
], 0)
1060 && comp_types (TREE_TYPE (src
), type
))
1062 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
1063 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
1064 "argument to %<sizeof%> in %qD call is the same "
1065 "expression as the second source; did you mean to "
1066 "remove the addressof?", callee
);
1067 else if ((TYPE_PRECISION (TREE_TYPE (type
))
1068 == TYPE_PRECISION (char_type_node
))
1070 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
1071 "argument to %<sizeof%> in %qD call is the same "
1072 "expression as the second source; did you mean to "
1073 "provide an explicit length?", callee
);
1075 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
1076 "argument to %<sizeof%> in %qD call is the same "
1077 "expression as the second source; did you mean to "
1078 "dereference it?", callee
);
1082 if (POINTER_TYPE_P (TREE_TYPE (src
))
1084 && comp_types (TREE_TYPE (src
), type
)
1085 && !VOID_TYPE_P (TREE_TYPE (type
)))
1087 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
1088 "argument to %<sizeof%> in %qD call is the same "
1089 "pointer type %qT as the second source; expected %qT "
1090 "or an explicit length", callee
, TREE_TYPE (src
),
1091 TREE_TYPE (TREE_TYPE (src
)));
1098 /* Warn for unlikely, improbable, or stupid DECL declarations
1102 check_main_parameter_types (tree decl
)
1104 function_args_iterator iter
;
1108 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl
), type
, iter
)
1110 /* XXX void_type_node belies the abstraction. */
1111 if (type
== void_type_node
|| type
== error_mark_node
)
1115 if (TYPE_ATOMIC (t
))
1116 pedwarn (input_location
, OPT_Wmain
,
1117 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1119 while (POINTER_TYPE_P (t
))
1122 if (TYPE_ATOMIC (t
))
1123 pedwarn (input_location
, OPT_Wmain
,
1124 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1132 if (TYPE_MAIN_VARIANT (type
) != integer_type_node
)
1133 pedwarn (input_location
, OPT_Wmain
,
1134 "first argument of %q+D should be %<int%>", decl
);
1138 if (TREE_CODE (type
) != POINTER_TYPE
1139 || TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
1140 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type
)))
1142 pedwarn (input_location
, OPT_Wmain
,
1143 "second argument of %q+D should be %<char **%>", decl
);
1147 if (TREE_CODE (type
) != POINTER_TYPE
1148 || TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
1149 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type
)))
1151 pedwarn (input_location
, OPT_Wmain
,
1152 "third argument of %q+D should probably be "
1153 "%<char **%>", decl
);
1158 /* It is intentional that this message does not mention the third
1159 argument because it's only mentioned in an appendix of the
1161 if (argct
> 0 && (argct
< 2 || argct
> 3))
1162 pedwarn (input_location
, OPT_Wmain
,
1163 "%q+D takes only zero or two arguments", decl
);
1165 if (stdarg_p (TREE_TYPE (decl
)))
1166 pedwarn (input_location
, OPT_Wmain
,
1167 "%q+D declared as variadic function", decl
);
1170 /* Warns and returns true if the conversion of EXPR to TYPE may alter a value.
1171 This is a helper function for warnings_for_convert_and_check. */
1174 conversion_warning (location_t loc
, tree type
, tree expr
, tree result
)
1176 tree expr_type
= TREE_TYPE (expr
);
1177 enum conversion_safety conversion_kind
;
1180 if (!warn_conversion
&& !warn_sign_conversion
&& !warn_float_conversion
)
1183 /* This may happen, because for LHS op= RHS we preevaluate
1184 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
1185 means we could no longer see the code of the EXPR. */
1186 if (TREE_CODE (expr
) == C_MAYBE_CONST_EXPR
)
1187 expr
= C_MAYBE_CONST_EXPR_EXPR (expr
);
1188 if (TREE_CODE (expr
) == SAVE_EXPR
)
1189 expr
= TREE_OPERAND (expr
, 0);
1191 switch (TREE_CODE (expr
))
1199 case TRUTH_ANDIF_EXPR
:
1200 case TRUTH_ORIF_EXPR
:
1201 case TRUTH_AND_EXPR
:
1203 case TRUTH_XOR_EXPR
:
1204 case TRUTH_NOT_EXPR
:
1205 /* Conversion from boolean to a signed:1 bit-field (which only
1206 can hold the values 0 and -1) doesn't lose information - but
1207 it does change the value. */
1208 if (TYPE_PRECISION (type
) == 1 && !TYPE_UNSIGNED (type
))
1209 warning_at (loc
, OPT_Wconversion
,
1210 "conversion to %qT from boolean expression", type
);
1217 conversion_kind
= unsafe_conversion_p (type
, expr
, result
, true);
1219 if (conversion_kind
== UNSAFE_REAL
)
1220 warnopt
= OPT_Wfloat_conversion
;
1221 else if (conversion_kind
)
1222 warnopt
= OPT_Wconversion
;
1226 if (conversion_kind
== UNSAFE_SIGN
)
1230 && CONSTANT_CLASS_P (result
));
1231 if (TYPE_UNSIGNED (type
))
1234 warning_at (loc
, OPT_Wsign_conversion
,
1235 "unsigned conversion from %qT to %qT "
1236 "changes value from %qE to %qE",
1237 expr_type
, type
, expr
, result
);
1239 warning_at (loc
, OPT_Wsign_conversion
,
1240 "unsigned conversion from %qT to %qT "
1241 "changes the value of %qE",
1242 expr_type
, type
, expr
);
1247 warning_at (loc
, OPT_Wsign_conversion
,
1248 "signed conversion from %qT to %qT changes "
1249 "value from %qE to %qE",
1250 expr_type
, type
, expr
, result
);
1252 warning_at (loc
, OPT_Wsign_conversion
,
1253 "signed conversion from %qT to %qT changes "
1255 expr_type
, type
, expr
);
1258 else if (CONSTANT_CLASS_P (result
))
1259 warning_at (loc
, warnopt
,
1260 "conversion from %qT to %qT changes value from %qE to %qE",
1261 expr_type
, type
, expr
, result
);
1263 warning_at (loc
, warnopt
,
1264 "conversion from %qT to %qT changes the value of %qE",
1265 expr_type
, type
, expr
);
1274 case TRUNC_MOD_EXPR
:
1275 case FLOOR_MOD_EXPR
:
1276 case TRUNC_DIV_EXPR
:
1277 case FLOOR_DIV_EXPR
:
1279 case EXACT_DIV_EXPR
:
1284 case PREDECREMENT_EXPR
:
1285 case PREINCREMENT_EXPR
:
1286 case POSTDECREMENT_EXPR
:
1287 case POSTINCREMENT_EXPR
:
1290 case FIX_TRUNC_EXPR
:
1291 case NON_LVALUE_EXPR
:
1299 /* In case of COND_EXPR, we do not care about the type of
1300 COND_EXPR, only about the conversion of each operand. */
1301 tree op1
= TREE_OPERAND (expr
, 1);
1302 tree op2
= TREE_OPERAND (expr
, 2);
1304 return ((op1
&& conversion_warning (loc
, type
, op1
, result
))
1305 || conversion_warning (loc
, type
, op2
, result
));
1309 if ((TREE_CODE (expr_type
) == INTEGER_TYPE
1310 || TREE_CODE (expr_type
) == BITINT_TYPE
)
1311 && (TREE_CODE (type
) == INTEGER_TYPE
1312 || TREE_CODE (type
) == BITINT_TYPE
))
1313 for (int i
= 0; i
< 2; ++i
)
1315 tree op
= TREE_OPERAND (expr
, i
);
1316 if (TREE_CODE (op
) != INTEGER_CST
)
1319 /* If one of the operands is a non-negative constant
1320 that fits in the target type, then the type of the
1321 other operand does not matter. */
1322 if (int_fits_type_p (op
, c_common_signed_type (type
))
1323 && int_fits_type_p (op
, c_common_unsigned_type (type
)))
1326 /* If constant is unsigned and fits in the target
1327 type, then the result will also fit. */
1328 if (TYPE_UNSIGNED (TREE_TYPE (op
)) && int_fits_type_p (op
, type
))
1334 return (conversion_warning (loc
, type
, TREE_OPERAND (expr
, 0), result
)
1335 || conversion_warning (loc
, type
, TREE_OPERAND (expr
, 1),
1340 conversion_kind
= unsafe_conversion_p (type
, expr
, result
, true);
1343 if (conversion_kind
== UNSAFE_REAL
)
1344 warnopt
= OPT_Wfloat_conversion
;
1345 else if (conversion_kind
== UNSAFE_SIGN
)
1346 warnopt
= OPT_Wsign_conversion
;
1347 else if (conversion_kind
)
1348 warnopt
= OPT_Wconversion
;
1353 && global_dc
->option_enabled_p (warnopt
))
1355 for (int i
= 0; i
< arith_ops
; ++i
)
1357 tree op
= TREE_OPERAND (expr
, i
);
1358 /* Avoid -Wsign-conversion for (unsigned)(x + (-1)). */
1359 if (TREE_CODE (expr
) == PLUS_EXPR
&& i
== 1
1360 && INTEGRAL_TYPE_P (type
) && TYPE_UNSIGNED (type
)
1361 && TREE_CODE (op
) == INTEGER_CST
1362 && tree_int_cst_sgn (op
) < 0)
1363 op
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (op
), op
);
1364 tree opr
= convert (type
, op
);
1365 if (unsafe_conversion_p (type
, op
, opr
, true))
1368 /* The operands seem safe, we might still want to warn if
1369 -Warith-conversion. */
1370 warnopt
= OPT_Warith_conversion
;
1374 if (conversion_kind
== UNSAFE_SIGN
)
1375 warning_at (loc
, warnopt
, "conversion to %qT from %qT "
1376 "may change the sign of the result",
1378 else if (conversion_kind
== UNSAFE_IMAGINARY
)
1379 warning_at (loc
, warnopt
,
1380 "conversion from %qT to %qT discards imaginary component",
1383 warning_at (loc
, warnopt
,
1384 "conversion from %qT to %qT may change value",
1392 /* Produce warnings after a conversion. RESULT is the result of
1393 converting EXPR to TYPE. This is a helper function for
1394 convert_and_check and cp_convert_and_check. */
1397 warnings_for_convert_and_check (location_t loc
, tree type
, tree expr
,
1400 loc
= expansion_point_location_if_in_system_header (loc
);
1402 while (TREE_CODE (expr
) == COMPOUND_EXPR
)
1403 expr
= TREE_OPERAND (expr
, 1);
1404 while (TREE_CODE (result
) == COMPOUND_EXPR
)
1405 result
= TREE_OPERAND (result
, 1);
1407 bool cst
= CONSTANT_CLASS_P (result
);
1408 tree exprtype
= TREE_TYPE (expr
);
1410 /* We're interested in the actual numerical value here, not its ASCII
1412 if (cst
&& TYPE_MAIN_VARIANT (TREE_TYPE (result
)) == char_type_node
)
1413 result_diag
= fold_convert (integer_type_node
, result
);
1415 result_diag
= result
;
1417 if (TREE_CODE (expr
) == INTEGER_CST
1418 && (TREE_CODE (type
) == INTEGER_TYPE
1419 || TREE_CODE (type
) == BITINT_TYPE
1420 || (TREE_CODE (type
) == ENUMERAL_TYPE
1421 && TREE_CODE (ENUM_UNDERLYING_TYPE (type
)) != BOOLEAN_TYPE
))
1422 && !int_fits_type_p (expr
, type
))
1424 /* Do not diagnose overflow in a constant expression merely
1425 because a conversion overflowed. */
1426 if (TREE_OVERFLOW (result
))
1427 TREE_OVERFLOW (result
) = TREE_OVERFLOW (expr
);
1429 if (TYPE_UNSIGNED (type
))
1431 /* This detects cases like converting -129 or 256 to
1433 if (!int_fits_type_p (expr
, c_common_signed_type (type
)))
1436 warning_at (loc
, OPT_Woverflow
,
1437 (TYPE_UNSIGNED (exprtype
)
1438 ? G_("conversion from %qT to %qT "
1439 "changes value from %qE to %qE")
1440 : G_("unsigned conversion from %qT to %qT "
1441 "changes value from %qE to %qE")),
1442 exprtype
, type
, expr
, result_diag
);
1444 warning_at (loc
, OPT_Woverflow
,
1445 (TYPE_UNSIGNED (exprtype
)
1446 ? G_("conversion from %qT to %qT "
1447 "changes the value of %qE")
1448 : G_("unsigned conversion from %qT to %qT "
1449 "changes the value of %qE")),
1450 exprtype
, type
, expr
);
1453 conversion_warning (loc
, type
, expr
, result
);
1455 else if (!int_fits_type_p (expr
, c_common_unsigned_type (type
)))
1458 warning_at (loc
, OPT_Woverflow
,
1459 "overflow in conversion from %qT to %qT "
1460 "changes value from %qE to %qE",
1461 exprtype
, type
, expr
, result_diag
);
1463 warning_at (loc
, OPT_Woverflow
,
1464 "overflow in conversion from %qT to %qT "
1465 "changes the value of %qE",
1466 exprtype
, type
, expr
);
1468 /* No warning for converting 0x80000000 to int. */
1470 && ((TREE_CODE (exprtype
) != INTEGER_TYPE
1471 && TREE_CODE (exprtype
) != BITINT_TYPE
)
1472 || (TYPE_PRECISION (exprtype
)
1473 != TYPE_PRECISION (type
))))
1476 warning_at (loc
, OPT_Woverflow
,
1477 "overflow in conversion from %qT to %qT "
1478 "changes value from %qE to %qE",
1479 exprtype
, type
, expr
, result_diag
);
1481 warning_at (loc
, OPT_Woverflow
,
1482 "overflow in conversion from %qT to %qT "
1483 "changes the value of %qE",
1484 exprtype
, type
, expr
);
1487 conversion_warning (loc
, type
, expr
, result
);
1489 else if ((TREE_CODE (result
) == INTEGER_CST
1490 || TREE_CODE (result
) == FIXED_CST
) && TREE_OVERFLOW (result
))
1493 warning_at (loc
, OPT_Woverflow
,
1494 "overflow in conversion from %qT to %qT "
1495 "changes value from %qE to %qE",
1496 exprtype
, type
, expr
, result_diag
);
1498 warning_at (loc
, OPT_Woverflow
,
1499 "overflow in conversion from %qT to %qT "
1500 "changes the value of %qE",
1501 exprtype
, type
, expr
);
1504 conversion_warning (loc
, type
, expr
, result
);
1507 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
1508 Used to verify that case values match up with enumerator values. */
1511 match_case_to_enum_1 (tree key
, tree type
, tree label
)
1513 /* Avoid warning about enums that have no enumerators. */
1514 if (TYPE_VALUES (type
) == NULL_TREE
)
1517 char buf
[WIDE_INT_PRINT_BUFFER_SIZE
];
1518 wide_int w
= wi::to_wide (key
);
1520 gcc_assert (w
.get_precision () <= WIDE_INT_MAX_INL_PRECISION
);
1521 if (tree_fits_uhwi_p (key
))
1522 print_dec (w
, buf
, UNSIGNED
);
1523 else if (tree_fits_shwi_p (key
))
1524 print_dec (w
, buf
, SIGNED
);
1528 if (TYPE_NAME (type
) == NULL_TREE
)
1529 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label
)),
1530 warn_switch
? OPT_Wswitch
: OPT_Wswitch_enum
,
1531 "case value %qs not in enumerated type",
1534 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label
)),
1535 warn_switch
? OPT_Wswitch
: OPT_Wswitch_enum
,
1536 "case value %qs not in enumerated type %qT",
1540 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
1541 Used to verify that case values match up with enumerator values. */
1544 match_case_to_enum (splay_tree_node node
, void *data
)
1546 tree label
= (tree
) node
->value
;
1547 tree type
= (tree
) data
;
1549 /* Skip default case. */
1550 if (!CASE_LOW (label
))
1553 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
1554 when we did our enum->case scan. Reset our scratch bit after. */
1555 if (!CASE_LOW_SEEN (label
))
1556 match_case_to_enum_1 (CASE_LOW (label
), type
, label
);
1558 CASE_LOW_SEEN (label
) = 0;
1560 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
1561 not set, that means that CASE_HIGH did not appear when we did our
1562 enum->case scan. Reset our scratch bit after. */
1563 if (CASE_HIGH (label
))
1565 if (!CASE_HIGH_SEEN (label
))
1566 match_case_to_enum_1 (CASE_HIGH (label
), type
, label
);
1568 CASE_HIGH_SEEN (label
) = 0;
1574 /* Handle -Wswitch*. Called from the front end after parsing the
1575 switch construct. */
1576 /* ??? Should probably be somewhere generic, since other languages
1577 besides C and C++ would want this. At the moment, however, C/C++
1578 are the only tree-ssa languages that support enumerations at all,
1579 so the point is moot. */
1582 c_do_switch_warnings (splay_tree cases
, location_t switch_location
,
1583 tree type
, tree cond
, bool bool_cond_p
)
1585 splay_tree_node default_node
;
1586 splay_tree_node node
;
1588 bool outside_range_p
= false;
1590 if (type
!= error_mark_node
1591 && type
!= TREE_TYPE (cond
)
1592 && INTEGRAL_TYPE_P (type
)
1593 && INTEGRAL_TYPE_P (TREE_TYPE (cond
))
1594 && (!tree_int_cst_equal (TYPE_MIN_VALUE (type
),
1595 TYPE_MIN_VALUE (TREE_TYPE (cond
)))
1596 || !tree_int_cst_equal (TYPE_MAX_VALUE (type
),
1597 TYPE_MAX_VALUE (TREE_TYPE (cond
)))))
1599 tree min_value
= TYPE_MIN_VALUE (type
);
1600 tree max_value
= TYPE_MAX_VALUE (type
);
1602 node
= splay_tree_predecessor (cases
, (splay_tree_key
) min_value
);
1603 if (node
&& node
->key
)
1605 outside_range_p
= true;
1606 /* There is at least one case smaller than TYPE's minimum value.
1607 NODE itself could be still a range overlapping the valid values,
1608 but any predecessors thereof except the default case will be
1609 completely outside of range. */
1610 if (CASE_HIGH ((tree
) node
->value
)
1611 && tree_int_cst_compare (CASE_HIGH ((tree
) node
->value
),
1614 location_t loc
= EXPR_LOCATION ((tree
) node
->value
);
1615 warning_at (loc
, OPT_Wswitch_outside_range
,
1616 "lower value in case label range less than minimum"
1618 CASE_LOW ((tree
) node
->value
) = convert (TREE_TYPE (cond
),
1620 node
->key
= (splay_tree_key
) CASE_LOW ((tree
) node
->value
);
1622 /* All the following ones are completely outside of range. */
1625 node
= splay_tree_predecessor (cases
,
1626 (splay_tree_key
) min_value
);
1627 if (node
== NULL
|| !node
->key
)
1629 location_t loc
= EXPR_LOCATION ((tree
) node
->value
);
1630 warning_at (loc
, OPT_Wswitch_outside_range
, "case label value is"
1631 " less than minimum value for type");
1632 splay_tree_remove (cases
, node
->key
);
1636 node
= splay_tree_lookup (cases
, (splay_tree_key
) max_value
);
1638 node
= splay_tree_predecessor (cases
, (splay_tree_key
) max_value
);
1639 /* Handle a single node that might partially overlap the range. */
1642 && CASE_HIGH ((tree
) node
->value
)
1643 && tree_int_cst_compare (CASE_HIGH ((tree
) node
->value
),
1646 location_t loc
= EXPR_LOCATION ((tree
) node
->value
);
1647 warning_at (loc
, OPT_Wswitch_outside_range
, "upper value in case"
1648 " label range exceeds maximum value for type");
1649 CASE_HIGH ((tree
) node
->value
)
1650 = convert (TREE_TYPE (cond
), max_value
);
1651 outside_range_p
= true;
1653 /* And any nodes that are completely outside of the range. */
1654 while ((node
= splay_tree_successor (cases
,
1655 (splay_tree_key
) max_value
))
1658 location_t loc
= EXPR_LOCATION ((tree
) node
->value
);
1659 warning_at (loc
, OPT_Wswitch_outside_range
,
1660 "case label value exceeds maximum value for type");
1661 splay_tree_remove (cases
, node
->key
);
1662 outside_range_p
= true;
1666 if (!warn_switch
&& !warn_switch_enum
&& !warn_switch_default
1667 && !warn_switch_bool
)
1670 default_node
= splay_tree_lookup (cases
, (splay_tree_key
) NULL
);
1672 warning_at (switch_location
, OPT_Wswitch_default
,
1673 "switch missing default case");
1675 /* There are certain cases where -Wswitch-bool warnings aren't
1682 so be careful here. */
1683 if (warn_switch_bool
&& bool_cond_p
)
1685 splay_tree_node min_node
;
1686 /* If there's a default node, it's also the value with the minimal
1687 key. So look at the penultimate key (if any). */
1689 min_node
= splay_tree_successor (cases
, (splay_tree_key
) NULL
);
1691 min_node
= splay_tree_min (cases
);
1692 tree min
= min_node
? (tree
) min_node
->key
: NULL_TREE
;
1694 splay_tree_node max_node
= splay_tree_max (cases
);
1695 /* This might be a case range, so look at the value with the
1696 maximal key and then check CASE_HIGH. */
1697 tree max
= max_node
? (tree
) max_node
->value
: NULL_TREE
;
1699 max
= CASE_HIGH (max
) ? CASE_HIGH (max
) : CASE_LOW (max
);
1701 /* If there's a case value > 1 or < 0, that is outside bool
1704 || (max
&& wi::gts_p (wi::to_wide (max
), 1))
1705 || (min
&& wi::lts_p (wi::to_wide (min
), 0))
1713 case, where we want to warn. */
1715 && max
&& wi::to_wide (max
) == 1
1716 && min
&& wi::to_wide (min
) == 0))
1717 warning_at (switch_location
, OPT_Wswitch_bool
,
1718 "switch condition has boolean value");
1721 /* From here on, we only care about enumerated types. */
1722 if (!type
|| TREE_CODE (type
) != ENUMERAL_TYPE
)
1725 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
1726 if (!warn_switch_enum
&& !warn_switch
)
1729 /* Check the cases. Warn about case values which are not members of
1730 the enumerated type. For -Wswitch-enum, or for -Wswitch when
1731 there is no default case, check that exactly all enumeration
1732 literals are covered by the cases. */
1734 /* Clearing COND if it is not an integer constant simplifies
1735 the tests inside the loop below. */
1736 if (TREE_CODE (cond
) != INTEGER_CST
)
1739 /* The time complexity here is O(N*lg(N)) worst case, but for the
1740 common case of monotonically increasing enumerators, it is
1741 O(N), since the nature of the splay tree will keep the next
1742 element adjacent to the root at all times. */
1744 for (chain
= TYPE_VALUES (type
); chain
; chain
= TREE_CHAIN (chain
))
1746 tree value
= TREE_VALUE (chain
);
1747 tree attrs
= DECL_ATTRIBUTES (value
);
1748 value
= DECL_INITIAL (value
);
1749 node
= splay_tree_lookup (cases
, (splay_tree_key
) value
);
1752 /* Mark the CASE_LOW part of the case entry as seen. */
1753 tree label
= (tree
) node
->value
;
1754 CASE_LOW_SEEN (label
) = 1;
1758 /* Even though there wasn't an exact match, there might be a
1759 case range which includes the enumerator's value. */
1760 node
= splay_tree_predecessor (cases
, (splay_tree_key
) value
);
1761 if (node
&& CASE_HIGH ((tree
) node
->value
))
1763 tree label
= (tree
) node
->value
;
1764 int cmp
= tree_int_cst_compare (CASE_HIGH (label
), value
);
1767 /* If we match the upper bound exactly, mark the CASE_HIGH
1768 part of the case entry as seen. */
1770 CASE_HIGH_SEEN (label
) = 1;
1775 /* We've now determined that this enumerated literal isn't
1776 handled by the case labels of the switch statement. */
1778 /* Don't warn if the enumerator was marked as unused. We can't use
1779 TREE_USED here: it could have been set on the enumerator if the
1780 enumerator was used earlier. */
1781 if (lookup_attribute ("unused", attrs
)
1782 || lookup_attribute ("maybe_unused", attrs
))
1785 /* If the switch expression is a constant, we only really care
1786 about whether that constant is handled by the switch. */
1787 if (cond
&& tree_int_cst_compare (cond
, value
))
1790 /* If the enumerator is defined in a system header and uses a reserved
1791 name, then we continue to avoid throwing a warning. */
1792 location_t loc
= DECL_SOURCE_LOCATION
1793 (TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type
)));
1794 if (in_system_header_at (loc
)
1795 && name_reserved_for_implementation_p
1796 (IDENTIFIER_POINTER (TREE_PURPOSE (chain
))))
1799 /* If there is a default_node, the only relevant option is
1800 Wswitch-enum. Otherwise, if both are enabled then we prefer
1801 to warn using -Wswitch because -Wswitch is enabled by -Wall
1802 while -Wswitch-enum is explicit. */
1803 warning_at (switch_location
,
1804 (default_node
|| !warn_switch
1807 "enumeration value %qE not handled in switch",
1808 TREE_PURPOSE (chain
));
1811 /* Warn if there are case expressions that don't correspond to
1812 enumerators. This can occur since C and C++ don't enforce
1813 type-checking of assignments to enumeration variables.
1815 The time complexity here is now always O(N) worst case, since
1816 we should have marked both the lower bound and upper bound of
1817 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
1818 above. This scan also resets those fields. */
1820 splay_tree_foreach (cases
, match_case_to_enum
, type
);
1823 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
1824 expression, because B will always be true. */
1827 warn_for_omitted_condop (location_t location
, tree cond
)
1829 /* In C++ template declarations it can happen that the type is dependent
1830 and not yet known, thus TREE_TYPE (cond) == NULL_TREE. */
1831 if (truth_value_p (TREE_CODE (cond
))
1832 || (TREE_TYPE (cond
) != NULL_TREE
1833 && TREE_CODE (TREE_TYPE (cond
)) == BOOLEAN_TYPE
))
1834 warning_at (location
, OPT_Wparentheses
,
1835 "the omitted middle operand in %<?:%> will always be %<true%>, "
1836 "suggest explicit middle operand");
1839 /* Give an error for storing into ARG, which is 'const'. USE indicates
1840 how ARG was being used. */
1843 readonly_error (location_t loc
, tree arg
, enum lvalue_use use
)
1845 gcc_assert (use
== lv_assign
|| use
== lv_increment
|| use
== lv_decrement
1847 STRIP_ANY_LOCATION_WRAPPER (arg
);
1848 /* Using this macro rather than (for example) arrays of messages
1849 ensures that all the format strings are checked at compile
1851 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
1852 : (use == lv_increment ? (I) \
1853 : (use == lv_decrement ? (D) : (AS))))
1854 if (TREE_CODE (arg
) == COMPONENT_REF
)
1856 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg
, 0))))
1857 error_at (loc
, READONLY_MSG (G_("assignment of member "
1858 "%qD in read-only object"),
1859 G_("increment of member "
1860 "%qD in read-only object"),
1861 G_("decrement of member "
1862 "%qD in read-only object"),
1863 G_("member %qD in read-only object "
1864 "used as %<asm%> output")),
1865 TREE_OPERAND (arg
, 1));
1867 error_at (loc
, READONLY_MSG (G_("assignment of read-only member %qD"),
1868 G_("increment of read-only member %qD"),
1869 G_("decrement of read-only member %qD"),
1870 G_("read-only member %qD used as %<asm%> output")),
1871 TREE_OPERAND (arg
, 1));
1873 else if (VAR_P (arg
))
1874 error_at (loc
, READONLY_MSG (G_("assignment of read-only variable %qD"),
1875 G_("increment of read-only variable %qD"),
1876 G_("decrement of read-only variable %qD"),
1877 G_("read-only variable %qD used as %<asm%> output")),
1879 else if (TREE_CODE (arg
) == PARM_DECL
)
1880 error_at (loc
, READONLY_MSG (G_("assignment of read-only parameter %qD"),
1881 G_("increment of read-only parameter %qD"),
1882 G_("decrement of read-only parameter %qD"),
1883 G_("read-only parameter %qD use as %<asm%> output")),
1885 else if (TREE_CODE (arg
) == RESULT_DECL
)
1887 gcc_assert (c_dialect_cxx ());
1888 error_at (loc
, READONLY_MSG (G_("assignment of "
1889 "read-only named return value %qD"),
1891 "read-only named return value %qD"),
1893 "read-only named return value %qD"),
1894 G_("read-only named return value %qD "
1895 "used as %<asm%>output")),
1898 else if (TREE_CODE (arg
) == FUNCTION_DECL
)
1899 error_at (loc
, READONLY_MSG (G_("assignment of function %qD"),
1900 G_("increment of function %qD"),
1901 G_("decrement of function %qD"),
1902 G_("function %qD used as %<asm%> output")),
1905 error_at (loc
, READONLY_MSG (G_("assignment of read-only location %qE"),
1906 G_("increment of read-only location %qE"),
1907 G_("decrement of read-only location %qE"),
1908 G_("read-only location %qE used as %<asm%> output")),
1912 /* Print an error message for an invalid lvalue. USE says
1913 how the lvalue is being used and so selects the error message. LOC
1914 is the location for the error. */
1917 lvalue_error (location_t loc
, enum lvalue_use use
)
1922 error_at (loc
, "lvalue required as left operand of assignment");
1925 error_at (loc
, "lvalue required as increment operand");
1928 error_at (loc
, "lvalue required as decrement operand");
1931 error_at (loc
, "lvalue required as unary %<&%> operand");
1934 error_at (loc
, "lvalue required in %<asm%> statement");
1941 /* Print an error message for an invalid indirection of type TYPE.
1942 ERRSTRING is the name of the operator for the indirection. */
1945 invalid_indirection_error (location_t loc
, tree type
, ref_operator errstring
)
1950 gcc_assert (c_dialect_cxx ());
1951 error_at (loc
, "invalid type argument (have %qT)", type
);
1953 case RO_ARRAY_INDEXING
:
1955 "invalid type argument of array indexing (have %qT)",
1960 "invalid type argument of unary %<*%> (have %qT)",
1965 "invalid type argument of %<->%> (have %qT)",
1970 "invalid type argument of %<->*%> (have %qT)",
1973 case RO_IMPLICIT_CONVERSION
:
1975 "invalid type argument of implicit conversion (have %qT)",
1983 /* Subscripting with type char is likely to lose on a machine where
1984 chars are signed. So warn on any machine, but optionally. Don't
1985 warn for unsigned char since that type is safe. Don't warn for
1986 signed char because anyone who uses that must have done so
1987 deliberately. Furthermore, we reduce the false positive load by
1988 warning only for non-constant value of type char.
1989 LOC is the location of the subscripting expression. */
1992 warn_array_subscript_with_type_char (location_t loc
, tree index
)
1994 if (TYPE_MAIN_VARIANT (TREE_TYPE (index
)) == char_type_node
)
1996 /* If INDEX has a location, use it; otherwise use LOC (the location
1997 of the subscripting expression as a whole). */
1998 loc
= EXPR_LOC_OR_LOC (index
, loc
);
1999 STRIP_ANY_LOCATION_WRAPPER (index
);
2000 if (TREE_CODE (index
) != INTEGER_CST
)
2001 warning_at (loc
, OPT_Wchar_subscripts
,
2002 "array subscript has type %<char%>");
2006 /* Implement -Wparentheses for the unexpected C precedence rules, to
2007 cover cases like x + y << z which readers are likely to
2008 misinterpret. We have seen an expression in which CODE is a binary
2009 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
2010 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
2011 CODE_RIGHT may be ERROR_MARK, which means that that side of the
2012 expression was not formed using a binary or unary operator, or it
2013 was enclosed in parentheses. */
2016 warn_about_parentheses (location_t loc
, enum tree_code code
,
2017 enum tree_code code_left
, tree arg_left
,
2018 enum tree_code code_right
, tree arg_right
)
2020 if (!warn_parentheses
)
2023 /* This macro tests that the expression ARG with original tree code
2024 CODE appears to be a boolean expression. or the result of folding a
2025 boolean expression. */
2026 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
2027 (truth_value_p (TREE_CODE (ARG)) \
2028 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
2029 /* Folding may create 0 or 1 integers from other expressions. */ \
2030 || ((CODE) != INTEGER_CST \
2031 && (integer_onep (ARG) || integer_zerop (ARG))))
2036 if (code_left
== PLUS_EXPR
)
2037 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
2038 "suggest parentheses around %<+%> inside %<<<%>");
2039 else if (code_right
== PLUS_EXPR
)
2040 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
2041 "suggest parentheses around %<+%> inside %<<<%>");
2042 else if (code_left
== MINUS_EXPR
)
2043 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
2044 "suggest parentheses around %<-%> inside %<<<%>");
2045 else if (code_right
== MINUS_EXPR
)
2046 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
2047 "suggest parentheses around %<-%> inside %<<<%>");
2051 if (code_left
== PLUS_EXPR
)
2052 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
2053 "suggest parentheses around %<+%> inside %<>>%>");
2054 else if (code_right
== PLUS_EXPR
)
2055 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
2056 "suggest parentheses around %<+%> inside %<>>%>");
2057 else if (code_left
== MINUS_EXPR
)
2058 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
2059 "suggest parentheses around %<-%> inside %<>>%>");
2060 else if (code_right
== MINUS_EXPR
)
2061 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
2062 "suggest parentheses around %<-%> inside %<>>%>");
2065 case TRUTH_ORIF_EXPR
:
2066 if (code_left
== TRUTH_ANDIF_EXPR
)
2067 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
2068 "suggest parentheses around %<&&%> within %<||%>");
2069 else if (code_right
== TRUTH_ANDIF_EXPR
)
2070 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
2071 "suggest parentheses around %<&&%> within %<||%>");
2075 if (code_left
== BIT_AND_EXPR
|| code_left
== BIT_XOR_EXPR
2076 || code_left
== PLUS_EXPR
|| code_left
== MINUS_EXPR
)
2077 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
2078 "suggest parentheses around arithmetic in operand of %<|%>");
2079 else if (code_right
== BIT_AND_EXPR
|| code_right
== BIT_XOR_EXPR
2080 || code_right
== PLUS_EXPR
|| code_right
== MINUS_EXPR
)
2081 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
2082 "suggest parentheses around arithmetic in operand of %<|%>");
2083 /* Check cases like x|y==z */
2084 else if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
2085 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
2086 "suggest parentheses around comparison in operand of %<|%>");
2087 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
2088 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
2089 "suggest parentheses around comparison in operand of %<|%>");
2090 /* Check cases like !x | y */
2091 else if (code_left
== TRUTH_NOT_EXPR
2092 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right
, arg_right
))
2093 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
2094 "suggest parentheses around operand of "
2095 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
2099 if (code_left
== BIT_AND_EXPR
2100 || code_left
== PLUS_EXPR
|| code_left
== MINUS_EXPR
)
2101 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
2102 "suggest parentheses around arithmetic in operand of %<^%>");
2103 else if (code_right
== BIT_AND_EXPR
2104 || code_right
== PLUS_EXPR
|| code_right
== MINUS_EXPR
)
2105 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
2106 "suggest parentheses around arithmetic in operand of %<^%>");
2107 /* Check cases like x^y==z */
2108 else if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
2109 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
2110 "suggest parentheses around comparison in operand of %<^%>");
2111 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
2112 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
2113 "suggest parentheses around comparison in operand of %<^%>");
2117 if (code_left
== PLUS_EXPR
)
2118 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
2119 "suggest parentheses around %<+%> in operand of %<&%>");
2120 else if (code_right
== PLUS_EXPR
)
2121 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
2122 "suggest parentheses around %<+%> in operand of %<&%>");
2123 else if (code_left
== MINUS_EXPR
)
2124 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
2125 "suggest parentheses around %<-%> in operand of %<&%>");
2126 else if (code_right
== MINUS_EXPR
)
2127 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
2128 "suggest parentheses around %<-%> in operand of %<&%>");
2129 /* Check cases like x&y==z */
2130 else if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
2131 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
2132 "suggest parentheses around comparison in operand of %<&%>");
2133 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
2134 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
2135 "suggest parentheses around comparison in operand of %<&%>");
2136 /* Check cases like !x & y */
2137 else if (code_left
== TRUTH_NOT_EXPR
2138 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right
, arg_right
))
2139 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
2140 "suggest parentheses around operand of "
2141 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
2145 if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
2146 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
2147 "suggest parentheses around comparison in operand of %<==%>");
2148 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
2149 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
2150 "suggest parentheses around comparison in operand of %<==%>");
2153 if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
2154 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
2155 "suggest parentheses around comparison in operand of %<!=%>");
2156 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
2157 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
2158 "suggest parentheses around comparison in operand of %<!=%>");
2162 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
2164 if (TREE_CODE_CLASS (code_left
) == tcc_comparison
2165 && code_left
!= NE_EXPR
&& code_left
!= EQ_EXPR
2166 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left
)))
2167 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
2168 "comparisons like %<X<=Y<=Z%> do not "
2169 "have their mathematical meaning");
2170 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
2171 && code_right
!= NE_EXPR
&& code_right
!= EQ_EXPR
2172 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right
)))
2173 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
2174 "comparisons like %<X<=Y<=Z%> do not "
2175 "have their mathematical meaning");
2179 #undef NOT_A_BOOLEAN_EXPR_P
2182 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
2185 warn_for_unused_label (tree label
)
2187 if (!TREE_USED (label
))
2189 if (DECL_INITIAL (label
))
2190 warning (OPT_Wunused_label
, "label %q+D defined but not used", label
);
2192 warning (OPT_Wunused_label
, "label %q+D declared but not defined", label
);
2194 else if (asan_sanitize_use_after_scope ())
2196 if (asan_used_labels
== NULL
)
2197 asan_used_labels
= new hash_set
<tree
> (16);
2199 asan_used_labels
->add (label
);
2203 /* Warn for division by zero according to the value of DIVISOR. LOC
2204 is the location of the division operator. */
2207 warn_for_div_by_zero (location_t loc
, tree divisor
)
2209 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
2210 about division by zero. Do not issue a warning if DIVISOR has a
2211 floating-point type, since we consider 0.0/0.0 a valid way of
2212 generating a NaN. */
2213 if (c_inhibit_evaluation_warnings
== 0
2214 && (integer_zerop (divisor
) || fixed_zerop (divisor
)))
2215 warning_at (loc
, OPT_Wdiv_by_zero
, "division by zero");
2218 /* Warn for patterns where memset appears to be used incorrectly. The
2219 warning location should be LOC. ARG0, and ARG2 are the first and
2220 last arguments to the call, while LITERAL_ZERO_MASK has a 1 bit for
2221 each argument that was a literal zero. */
2224 warn_for_memset (location_t loc
, tree arg0
, tree arg2
,
2225 int literal_zero_mask
)
2227 arg0
= fold_for_warn (arg0
);
2228 arg2
= fold_for_warn (arg2
);
2230 if (warn_memset_transposed_args
2231 && integer_zerop (arg2
)
2232 && (literal_zero_mask
& (1 << 2)) != 0
2233 && (literal_zero_mask
& (1 << 1)) == 0)
2234 warning_at (loc
, OPT_Wmemset_transposed_args
,
2235 "%<memset%> used with constant zero length "
2236 "parameter; this could be due to transposed "
2239 if (warn_memset_elt_size
&& TREE_CODE (arg2
) == INTEGER_CST
)
2242 if (TREE_CODE (arg0
) == ADDR_EXPR
)
2243 arg0
= TREE_OPERAND (arg0
, 0);
2244 tree type
= TREE_TYPE (arg0
);
2245 if (type
!= NULL_TREE
&& TREE_CODE (type
) == ARRAY_TYPE
)
2247 tree elt_type
= TREE_TYPE (type
);
2248 tree domain
= TYPE_DOMAIN (type
);
2249 if (COMPLETE_TYPE_P (elt_type
)
2250 && !integer_onep (TYPE_SIZE_UNIT (elt_type
))
2251 && domain
!= NULL_TREE
2252 && TYPE_MAX_VALUE (domain
)
2253 && TYPE_MIN_VALUE (domain
)
2254 && integer_zerop (TYPE_MIN_VALUE (domain
))
2255 && integer_onep (fold_build2 (MINUS_EXPR
, domain
,
2257 TYPE_MAX_VALUE (domain
))))
2258 warning_at (loc
, OPT_Wmemset_elt_size
,
2259 "%<memset%> used with length equal to "
2260 "number of elements without multiplication "
2266 /* Warn for calloc (sizeof (X), n). */
2269 warn_for_calloc (location_t
*sizeof_arg_loc
, tree callee
,
2270 vec
<tree
, va_gc
> *params
, tree
*sizeof_arg
, tree attr
)
2272 if (!TREE_VALUE (attr
) || !TREE_CHAIN (TREE_VALUE (attr
)))
2275 int arg1
= TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr
))) - 1;
2277 = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (TREE_VALUE (attr
)))) - 1;
2279 || (unsigned) arg1
>= vec_safe_length (params
)
2282 || (unsigned) arg2
>= vec_safe_length (params
)
2287 if (sizeof_arg
[arg1
] == NULL_TREE
|| sizeof_arg
[arg2
] != NULL_TREE
)
2290 if (warning_at (sizeof_arg_loc
[arg1
], OPT_Wcalloc_transposed_args
,
2291 "%qD sizes specified with %<sizeof%> in the earlier "
2292 "argument and not in the later argument", callee
))
2293 inform (sizeof_arg_loc
[arg1
], "earlier argument should specify number "
2294 "of elements, later size of each element");
2297 /* Warn for allocator calls where the constant allocated size is smaller
2298 than sizeof (TYPE). */
2301 warn_for_alloc_size (location_t loc
, tree type
, tree call
, tree alloc_size
)
2303 if (!TREE_VALUE (alloc_size
))
2306 tree arg1
= TREE_VALUE (TREE_VALUE (alloc_size
));
2307 int idx1
= TREE_INT_CST_LOW (arg1
) - 1;
2308 if (idx1
< 0 || idx1
>= call_expr_nargs (call
))
2310 arg1
= CALL_EXPR_ARG (call
, idx1
);
2311 if (TREE_CODE (arg1
) != INTEGER_CST
)
2313 if (TREE_CHAIN (TREE_VALUE (alloc_size
)))
2315 tree arg2
= TREE_VALUE (TREE_CHAIN (TREE_VALUE (alloc_size
)));
2316 int idx2
= TREE_INT_CST_LOW (arg2
) - 1;
2317 if (idx2
< 0 || idx2
>= call_expr_nargs (call
))
2319 arg2
= CALL_EXPR_ARG (call
, idx2
);
2320 if (TREE_CODE (arg2
) != INTEGER_CST
)
2322 arg1
= int_const_binop (MULT_EXPR
, fold_convert (sizetype
, arg1
),
2323 fold_convert (sizetype
, arg2
));
2324 if (TREE_CODE (arg1
) != INTEGER_CST
)
2327 if (!VOID_TYPE_P (type
)
2328 && TYPE_SIZE_UNIT (type
)
2329 && TREE_CODE (TYPE_SIZE_UNIT (type
)) == INTEGER_CST
2330 && tree_int_cst_lt (arg1
, TYPE_SIZE_UNIT (type
)))
2331 warning_at (loc
, OPT_Walloc_size
,
2332 "allocation of insufficient size %qE for type %qT with "
2333 "size %qE", arg1
, type
, TYPE_SIZE_UNIT (type
));
2336 /* Subroutine of build_binary_op. Give warnings for comparisons
2337 between signed and unsigned quantities that may fail. Do the
2338 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
2339 so that casts will be considered, but default promotions won't
2342 LOCATION is the location of the comparison operator.
2344 The arguments of this function map directly to local variables
2345 of build_binary_op. */
2348 warn_for_sign_compare (location_t location
,
2349 tree orig_op0
, tree orig_op1
,
2351 tree result_type
, enum tree_code resultcode
)
2353 if (error_operand_p (orig_op0
) || error_operand_p (orig_op1
))
2356 int op0_signed
= !TYPE_UNSIGNED (TREE_TYPE (orig_op0
));
2357 int op1_signed
= !TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
2358 int unsignedp0
, unsignedp1
;
2360 /* Do not warn if the comparison is being done in a signed type,
2361 since the signed type will only be chosen if it can represent
2362 all the values of the unsigned type. */
2363 if (!TYPE_UNSIGNED (result_type
))
2365 /* Do not warn if both operands are unsigned. */
2366 else if (op0_signed
== op1_signed
)
2370 tree sop
, uop
, base_type
;
2374 sop
= orig_op0
, uop
= orig_op1
;
2376 sop
= orig_op1
, uop
= orig_op0
;
2378 sop
= fold_for_warn (sop
);
2379 uop
= fold_for_warn (uop
);
2381 STRIP_TYPE_NOPS (sop
);
2382 STRIP_TYPE_NOPS (uop
);
2383 base_type
= (TREE_CODE (result_type
) == COMPLEX_TYPE
2384 ? TREE_TYPE (result_type
) : result_type
);
2386 /* Do not warn if the signed quantity is an unsuffixed integer
2387 literal (or some static constant expression involving such
2388 literals or a conditional expression involving such literals)
2389 and it is non-negative. */
2390 if (tree_expr_nonnegative_warnv_p (sop
, &ovf
))
2392 /* Do not warn if the comparison is an equality operation, the
2393 unsigned quantity is an integral constant, and it would fit
2394 in the result if the result were signed. */
2395 else if (TREE_CODE (uop
) == INTEGER_CST
2396 && (resultcode
== EQ_EXPR
|| resultcode
== NE_EXPR
)
2397 && int_fits_type_p (uop
, c_common_signed_type (base_type
)))
2399 /* In C, do not warn if the unsigned quantity is an enumeration
2400 constant and its maximum value would fit in the result if the
2401 result were signed. */
2402 else if (!c_dialect_cxx() && TREE_CODE (uop
) == INTEGER_CST
2403 && TREE_CODE (TREE_TYPE (uop
)) == ENUMERAL_TYPE
2404 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop
)),
2405 c_common_signed_type (base_type
)))
2408 warning_at (location
, OPT_Wsign_compare
,
2409 "comparison of integer expressions of different "
2410 "signedness: %qT and %qT", TREE_TYPE (orig_op0
),
2411 TREE_TYPE (orig_op1
));
2414 /* Warn if two unsigned values are being compared in a size larger
2415 than their original size, and one (and only one) is the result of
2416 a `~' operator. This comparison will always fail.
2418 Also warn if one operand is a constant, and the constant does not
2419 have all bits set that are set in the ~ operand when it is
2422 /* bits0 is the bit index of op0 extended to result_type, which will
2423 be always 0 and so all bits above it. If there is a BIT_NOT_EXPR
2424 in that operand possibly sign or zero extended to op0 and then
2425 possibly further sign or zero extended to result_type, bits0 will
2426 be the precision of result type if all the extensions involved
2427 if any are sign extensions, and will be the place of the innermost
2428 zero extension otherwise. We warn only if BIT_NOT_EXPR's operand is
2429 zero extended from some even smaller precision, in that case after
2430 BIT_NOT_EXPR some bits below bits0 will be guaranteed to be set.
2431 Similarly for bits1. */
2432 int bits0
= TYPE_PRECISION (result_type
);
2433 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
2434 bits0
= TYPE_PRECISION (TREE_TYPE (op0
));
2435 tree arg0
= c_common_get_narrower (op0
, &unsignedp0
);
2436 if (TYPE_PRECISION (TREE_TYPE (arg0
)) == TYPE_PRECISION (TREE_TYPE (op0
)))
2437 unsignedp0
= TYPE_UNSIGNED (TREE_TYPE (op0
));
2438 else if (unsignedp0
)
2439 bits0
= TYPE_PRECISION (TREE_TYPE (arg0
));
2441 int bits1
= TYPE_PRECISION (result_type
);
2442 if (TYPE_UNSIGNED (TREE_TYPE (op1
)))
2443 bits1
= TYPE_PRECISION (TREE_TYPE (op1
));
2444 tree arg1
= c_common_get_narrower (op1
, &unsignedp1
);
2445 if (TYPE_PRECISION (TREE_TYPE (arg1
)) == TYPE_PRECISION (TREE_TYPE (op1
)))
2446 unsignedp1
= TYPE_UNSIGNED (TREE_TYPE (op1
));
2447 else if (unsignedp1
)
2448 bits1
= TYPE_PRECISION (TREE_TYPE (arg1
));
2451 if ((TREE_CODE (op0
) == BIT_NOT_EXPR
)
2452 ^ (TREE_CODE (op1
) == BIT_NOT_EXPR
))
2454 if (TREE_CODE (op1
) == BIT_NOT_EXPR
)
2456 std::swap (op0
, op1
);
2457 std::swap (unsignedp0
, unsignedp1
);
2458 std::swap (bits0
, bits1
);
2462 arg0
= c_common_get_narrower (TREE_OPERAND (op0
, 0), &unsignedp
);
2464 /* For these warnings, we need BIT_NOT_EXPR operand to be
2465 zero extended from narrower type to BIT_NOT_EXPR's type.
2466 In that case, all those bits above the narrower's type
2467 are after BIT_NOT_EXPR set to 1. */
2468 if (tree_fits_shwi_p (op1
))
2470 HOST_WIDE_INT constant
= tree_to_shwi (op1
);
2471 unsigned int bits
= TYPE_PRECISION (TREE_TYPE (arg0
));
2473 && bits
< TYPE_PRECISION (TREE_TYPE (op0
))
2474 && bits
< HOST_BITS_PER_WIDE_INT
)
2476 HOST_WIDE_INT mask
= HOST_WIDE_INT_M1U
<< bits
;
2477 if (bits0
< HOST_BITS_PER_WIDE_INT
)
2478 mask
&= ~(HOST_WIDE_INT_M1U
<< bits0
);
2479 if ((mask
& constant
) != mask
)
2482 warning_at (location
, OPT_Wsign_compare
,
2483 "promoted bitwise complement of an unsigned "
2484 "value is always nonzero");
2486 warning_at (location
, OPT_Wsign_compare
,
2487 "comparison of promoted bitwise complement "
2488 "of an unsigned value with constant");
2492 else if ((TYPE_PRECISION (TREE_TYPE (arg0
))
2493 < TYPE_PRECISION (TREE_TYPE (op0
)))
2496 && TYPE_PRECISION (TREE_TYPE (op1
)) < bits0
)
2497 warning_at (location
, OPT_Wsign_compare
,
2498 "comparison of promoted bitwise complement "
2499 "of an unsigned value with unsigned");
2503 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
2504 type via c_common_type. If -Wdouble-promotion is in use, and the
2505 conditions for warning have been met, issue a warning. GMSGID is
2506 the warning message. It must have two %T specifiers for the type
2507 that was converted (generally "float") and the type to which it was
2508 converted (generally "double), respectively. LOC is the location
2509 to which the warning should refer. */
2512 do_warn_double_promotion (tree result_type
, tree type1
, tree type2
,
2513 const char *gmsgid
, location_t loc
)
2517 if (!warn_double_promotion
)
2519 /* If the conversion will not occur at run-time, there is no need to
2521 if (c_inhibit_evaluation_warnings
)
2523 /* If an invalid conversion has occurred, don't warn. */
2524 if (result_type
== error_mark_node
)
2526 if (TYPE_MAIN_VARIANT (result_type
) != double_type_node
2527 && TYPE_MAIN_VARIANT (result_type
) != complex_double_type_node
)
2529 if (TYPE_MAIN_VARIANT (type1
) == float_type_node
2530 || TYPE_MAIN_VARIANT (type1
) == complex_float_type_node
)
2531 source_type
= type1
;
2532 else if (TYPE_MAIN_VARIANT (type2
) == float_type_node
2533 || TYPE_MAIN_VARIANT (type2
) == complex_float_type_node
)
2534 source_type
= type2
;
2537 warning_at (loc
, OPT_Wdouble_promotion
, gmsgid
, source_type
, result_type
);
2540 /* Possibly warn about unused parameters. */
2543 do_warn_unused_parameter (tree fn
)
2547 for (decl
= DECL_ARGUMENTS (fn
);
2548 decl
; decl
= DECL_CHAIN (decl
))
2549 if (!TREE_USED (decl
) && TREE_CODE (decl
) == PARM_DECL
2550 && DECL_NAME (decl
) && !DECL_ARTIFICIAL (decl
)
2551 && !warning_suppressed_p (decl
, OPT_Wunused_parameter
))
2552 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wunused_parameter
,
2553 "unused parameter %qD", decl
);
2556 /* If DECL is a typedef that is declared in the current function,
2557 record it for the purpose of -Wunused-local-typedefs. */
2560 record_locally_defined_typedef (tree decl
)
2562 struct c_language_function
*l
;
2564 if (!warn_unused_local_typedefs
2566 /* if this is not a locally defined typedef then we are not
2568 || !is_typedef_decl (decl
)
2569 || !decl_function_context (decl
))
2572 l
= (struct c_language_function
*) cfun
->language
;
2573 vec_safe_push (l
->local_typedefs
, decl
);
2576 /* If T is a TYPE_DECL declared locally, mark it as used. */
2579 maybe_record_typedef_use (tree t
)
2581 if (!is_typedef_decl (t
))
2584 TREE_USED (t
) = true;
2587 /* Warn if there are some unused locally defined typedefs in the
2588 current function. */
2591 maybe_warn_unused_local_typedefs (void)
2595 /* The number of times we have emitted -Wunused-local-typedefs
2596 warnings. If this is different from errorcount, that means some
2597 unrelated errors have been issued. In which case, we'll avoid
2598 emitting "unused-local-typedefs" warnings. */
2599 static int unused_local_typedefs_warn_count
;
2600 struct c_language_function
*l
;
2605 if ((l
= (struct c_language_function
*) cfun
->language
) == NULL
)
2608 if (warn_unused_local_typedefs
2609 && errorcount
== unused_local_typedefs_warn_count
)
2611 FOR_EACH_VEC_SAFE_ELT (l
->local_typedefs
, i
, decl
)
2612 if (!TREE_USED (decl
))
2613 warning_at (DECL_SOURCE_LOCATION (decl
),
2614 OPT_Wunused_local_typedefs
,
2615 "typedef %qD locally defined but not used", decl
);
2616 unused_local_typedefs_warn_count
= errorcount
;
2619 vec_free (l
->local_typedefs
);
2622 /* If we're creating an if-else-if condition chain, first see if we
2623 already have this COND in the CHAIN. If so, warn and don't add COND
2624 into the vector, otherwise add the COND there. LOC is the location
2628 warn_duplicated_cond_add_or_warn (location_t loc
, tree cond
, vec
<tree
> **chain
)
2630 /* No chain has been created yet. Do nothing. */
2634 if (TREE_SIDE_EFFECTS (cond
) || instantiation_dependent_expression_p (cond
))
2636 /* Uh-oh! This condition has a side-effect, thus invalidates
2646 FOR_EACH_VEC_ELT (**chain
, ix
, t
)
2647 if (operand_equal_p (cond
, t
, 0))
2649 auto_diagnostic_group d
;
2650 if (warning_at (loc
, OPT_Wduplicated_cond
,
2651 "duplicated %<if%> condition"))
2652 inform (EXPR_LOCATION (t
), "previously used here");
2658 && !CONSTANT_CLASS_P (cond
)
2659 /* Don't infinitely grow the chain. */
2660 && (*chain
)->length () < 512)
2661 (*chain
)->safe_push (cond
);
2664 /* Check and possibly warn if two declarations have contradictory
2665 attributes, such as always_inline vs. noinline. */
2668 diagnose_mismatched_attributes (tree olddecl
, tree newdecl
)
2670 bool warned
= false;
2672 tree a1
= lookup_attribute ("optimize", DECL_ATTRIBUTES (olddecl
));
2673 tree a2
= lookup_attribute ("optimize", DECL_ATTRIBUTES (newdecl
));
2674 /* An optimization attribute applied on a declaration after the
2675 definition is likely not what the user wanted. */
2677 && DECL_SAVED_TREE (olddecl
) != NULL_TREE
2678 && (a1
== NULL_TREE
|| !attribute_list_equal (a1
, a2
)))
2679 warned
|= warning (OPT_Wattributes
,
2680 "optimization attribute on %qD follows "
2681 "definition but the attribute doesn%'t match",
2684 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2685 if (DECL_DECLARED_INLINE_P (newdecl
)
2686 && DECL_UNINLINABLE (olddecl
)
2687 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl
)))
2688 warned
|= warning (OPT_Wattributes
, "inline declaration of %qD follows "
2689 "declaration with attribute %<noinline%>", newdecl
);
2690 else if (DECL_DECLARED_INLINE_P (olddecl
)
2691 && DECL_UNINLINABLE (newdecl
)
2692 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl
)))
2693 warned
|= warning (OPT_Wattributes
, "declaration of %q+D with attribute "
2694 "%<noinline%> follows inline declaration", newdecl
);
2699 /* Warn if signed left shift overflows. We don't warn
2700 about left-shifting 1 into the sign bit in C++14; cf.
2701 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
2702 and don't warn for C++20 at all, as signed left shifts never
2704 LOC is a location of the shift; OP0 and OP1 are the operands.
2705 Return true if an overflow is detected, false otherwise. */
2708 maybe_warn_shift_overflow (location_t loc
, tree op0
, tree op1
)
2710 if (TREE_CODE (op0
) != INTEGER_CST
2711 || TREE_CODE (op1
) != INTEGER_CST
)
2714 /* match.pd could have narrowed the left shift already,
2715 take type promotion into account. */
2716 tree type0
= lang_hooks
.types
.type_promotes_to (TREE_TYPE (op0
));
2717 unsigned int prec0
= TYPE_PRECISION (type0
);
2719 /* Left-hand operand must be signed. */
2720 if (TYPE_OVERFLOW_WRAPS (type0
) || cxx_dialect
>= cxx20
)
2723 signop sign
= SIGNED
;
2724 if (TYPE_PRECISION (TREE_TYPE (op0
)) < TYPE_PRECISION (type0
))
2725 sign
= TYPE_SIGN (TREE_TYPE (op0
));
2726 unsigned int min_prec
= (wi::min_precision (wi::to_wide (op0
), sign
)
2727 + TREE_INT_CST_LOW (op1
));
2728 /* Handle the case of left-shifting 1 into the sign bit.
2729 * However, shifting 1 _out_ of the sign bit, as in
2730 * INT_MIN << 1, is considered an overflow.
2732 if (!tree_int_cst_sign_bit (op0
) && min_prec
== prec0
+ 1)
2734 /* Never warn for C++14 onwards. */
2735 if (cxx_dialect
>= cxx14
)
2737 /* Otherwise only if -Wshift-overflow=2. But return
2738 true to signal an overflow for the sake of integer
2739 constant expressions. */
2740 if (warn_shift_overflow
< 2)
2744 bool overflowed
= min_prec
> prec0
;
2745 if (overflowed
&& c_inhibit_evaluation_warnings
== 0)
2746 warning_at (loc
, OPT_Wshift_overflow_
,
2747 "result of %qE requires %u bits to represent, "
2748 "but %qT only has %u bits",
2749 build2_loc (loc
, LSHIFT_EXPR
, type0
,
2750 fold_convert (type0
, op0
), op1
),
2751 min_prec
, type0
, prec0
);
2756 /* Warn about boolean expression compared with an integer value different
2757 from true/false. Warns also e.g. about "(i1 == i2) == 2".
2758 LOC is the location of the comparison, CODE is its code, OP0 and OP1
2759 are the operands of the comparison. The caller must ensure that
2760 either operand is a boolean expression. */
2763 maybe_warn_bool_compare (location_t loc
, enum tree_code code
, tree op0
,
2766 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
2770 if (f
= fold_for_warn (op0
),
2771 TREE_CODE (f
) == INTEGER_CST
)
2773 else if (f
= fold_for_warn (op1
),
2774 TREE_CODE (f
) == INTEGER_CST
)
2779 if (!integer_zerop (cst
) && !integer_onep (cst
))
2781 int sign
= (TREE_CODE (op0
) == INTEGER_CST
2782 ? tree_int_cst_sgn (cst
) : -tree_int_cst_sgn (cst
));
2784 || ((code
== GT_EXPR
|| code
== GE_EXPR
) && sign
< 0)
2785 || ((code
== LT_EXPR
|| code
== LE_EXPR
) && sign
> 0))
2786 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2787 "with boolean expression is always false", cst
);
2789 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2790 "with boolean expression is always true", cst
);
2792 else if (integer_zerop (cst
) || integer_onep (cst
))
2794 /* If the non-constant operand isn't of a boolean type, we
2795 don't want to warn here. */
2796 tree noncst
= TREE_CODE (op0
) == INTEGER_CST
? op1
: op0
;
2797 /* Handle booleans promoted to integers. */
2798 if (bool_promoted_to_int_p (noncst
))
2800 else if (TREE_CODE (TREE_TYPE (noncst
)) != BOOLEAN_TYPE
2801 && !truth_value_p (TREE_CODE (noncst
)))
2803 /* Do some magic to get the right diagnostics. */
2804 bool flag
= TREE_CODE (op0
) == INTEGER_CST
;
2805 flag
= integer_zerop (cst
) ? flag
: !flag
;
2806 if ((code
== GE_EXPR
&& !flag
) || (code
== LE_EXPR
&& flag
))
2807 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2808 "with boolean expression is always true", cst
);
2809 else if ((code
== LT_EXPR
&& !flag
) || (code
== GT_EXPR
&& flag
))
2810 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2811 "with boolean expression is always false", cst
);
2815 /* Warn if an argument at position param_pos is passed to a
2816 restrict-qualified param, and it aliases with another argument.
2817 Return true if a warning has been issued. */
2820 warn_for_restrict (unsigned param_pos
, tree
*argarray
, unsigned nargs
)
2822 tree arg
= argarray
[param_pos
];
2823 if (TREE_VISITED (arg
) || integer_zerop (arg
))
2826 location_t loc
= EXPR_LOC_OR_LOC (arg
, input_location
);
2827 gcc_rich_location
richloc (loc
);
2830 auto_vec
<int, 16> arg_positions
;
2832 for (i
= 0; i
< nargs
; i
++)
2837 tree current_arg
= argarray
[i
];
2838 if (operand_equal_p (arg
, current_arg
, 0))
2840 TREE_VISITED (current_arg
) = 1;
2841 arg_positions
.safe_push (i
+ 1);
2845 if (arg_positions
.is_empty ())
2849 FOR_EACH_VEC_ELT (arg_positions
, i
, pos
)
2851 arg
= argarray
[pos
- 1];
2852 if (EXPR_HAS_LOCATION (arg
))
2853 richloc
.add_range (EXPR_LOCATION (arg
));
2856 return warning_n (&richloc
, OPT_Wrestrict
, arg_positions
.length (),
2857 "passing argument %i to %qs-qualified parameter"
2858 " aliases with argument %Z",
2859 "passing argument %i to %qs-qualified parameter"
2860 " aliases with arguments %Z",
2861 param_pos
+ 1, "restrict", arg_positions
.address (),
2862 arg_positions
.length ());
2865 /* Callback function to determine whether an expression TP or one of its
2866 subexpressions comes from macro expansion. Used to suppress bogus
2870 expr_from_macro_expansion_r (tree
*tp
, int *, void *)
2872 if (CAN_HAVE_LOCATION_P (*tp
)
2873 && from_macro_expansion_at (EXPR_LOCATION (*tp
)))
2874 return integer_zero_node
;
2879 /* Possibly warn when an if-else has identical branches. */
2882 do_warn_duplicated_branches (tree expr
)
2884 tree thenb
= COND_EXPR_THEN (expr
);
2885 tree elseb
= COND_EXPR_ELSE (expr
);
2887 /* Don't bother if any of the branches is missing. */
2888 if (thenb
== NULL_TREE
|| elseb
== NULL_TREE
)
2891 /* And don't warn for empty statements. */
2892 if (TREE_CODE (thenb
) == NOP_EXPR
2893 && TREE_TYPE (thenb
) == void_type_node
2894 && TREE_OPERAND (thenb
, 0) == size_zero_node
)
2897 /* ... or empty branches. */
2898 if (TREE_CODE (thenb
) == STATEMENT_LIST
2899 && STATEMENT_LIST_HEAD (thenb
) == NULL
)
2902 /* Compute the hash of the then branch. */
2903 inchash::hash
hstate0 (0);
2904 inchash::add_expr (thenb
, hstate0
);
2905 hashval_t h0
= hstate0
.end ();
2907 /* Compute the hash of the else branch. */
2908 inchash::hash
hstate1 (0);
2909 inchash::add_expr (elseb
, hstate1
);
2910 hashval_t h1
= hstate1
.end ();
2912 /* Compare the hashes. */
2914 && operand_equal_p (thenb
, elseb
, OEP_LEXICOGRAPHIC
2915 | OEP_ADDRESS_OF_SAME_FIELD
)
2916 /* Don't warn if any of the branches or their subexpressions comes
2918 && !walk_tree_without_duplicates (&thenb
, expr_from_macro_expansion_r
,
2920 && !walk_tree_without_duplicates (&elseb
, expr_from_macro_expansion_r
,
2922 warning_at (EXPR_LOCATION (expr
), OPT_Wduplicated_branches
,
2923 "this condition has identical branches");
2926 /* Callback for c_genericize to implement -Wduplicated-branches. */
2929 do_warn_duplicated_branches_r (tree
*tp
, int *, void *)
2931 if (TREE_CODE (*tp
) == COND_EXPR
)
2932 do_warn_duplicated_branches (*tp
);
2936 /* Implementation of -Wmultistatement-macros. This warning warns about
2937 cases when a macro expands to multiple statements not wrapped in
2938 do {} while (0) or ({ }) and is used as a body of if/else/for/while
2939 conditionals. For example,
2941 #define DOIT x++; y++
2946 will increment y unconditionally.
2948 BODY_LOC is the location of the first token in the body after labels
2949 have been parsed, NEXT_LOC is the location of the next token after the
2950 body of the conditional has been parsed, and GUARD_LOC is the location
2951 of the conditional. */
2954 warn_for_multistatement_macros (location_t body_loc
, location_t next_loc
,
2955 location_t guard_loc
, enum rid keyword
)
2957 if (!warn_multistatement_macros
)
2960 /* Ain't got time to waste. We only care about macros here. */
2961 if (!from_macro_expansion_at (body_loc
)
2962 || !from_macro_expansion_at (next_loc
))
2965 /* Let's skip macros defined in system headers. */
2966 if (in_system_header_at (body_loc
)
2967 || in_system_header_at (next_loc
))
2970 /* Find the actual tokens in the macro definition. BODY_LOC and
2971 NEXT_LOC have to come from the same spelling location, but they
2972 will resolve to different locations in the context of the macro
2974 location_t body_loc_exp
2975 = linemap_resolve_location (line_table
, body_loc
,
2976 LRK_MACRO_DEFINITION_LOCATION
, NULL
);
2977 location_t next_loc_exp
2978 = linemap_resolve_location (line_table
, next_loc
,
2979 LRK_MACRO_DEFINITION_LOCATION
, NULL
);
2980 location_t guard_loc_exp
2981 = linemap_resolve_location (line_table
, guard_loc
,
2982 LRK_MACRO_DEFINITION_LOCATION
, NULL
);
2984 /* These are some funky cases we don't want to warn about. */
2985 if (body_loc_exp
== guard_loc_exp
2986 || next_loc_exp
== guard_loc_exp
2987 || body_loc_exp
== next_loc_exp
)
2990 /* Find the macro maps for the macro expansions. */
2991 const line_map
*body_map
= linemap_lookup (line_table
, body_loc
);
2992 const line_map
*next_map
= linemap_lookup (line_table
, next_loc
);
2993 const line_map
*guard_map
= linemap_lookup (line_table
, guard_loc
);
2995 /* Now see if the following token (after the body) is coming from the
2996 same macro expansion. If it is, it might be a problem. */
2997 if (body_map
!= next_map
)
3000 /* The conditional itself must not come from the same expansion, because
3001 we don't want to warn about
3002 #define IF if (x) x++; y++
3004 if (guard_map
== body_map
)
3007 /* Handle the case where NEXT and BODY come from the same expansion while
3008 GUARD doesn't, yet we shouldn't warn. E.g.
3010 #define GUARD if (...)
3011 #define GUARD2 GUARD
3013 and in the definition of another macro:
3019 while (linemap_macro_expansion_map_p (guard_map
))
3021 const line_map_macro
*mm
= linemap_check_macro (guard_map
);
3022 guard_loc_exp
= mm
->get_expansion_point_location ();
3023 guard_map
= linemap_lookup (line_table
, guard_loc_exp
);
3024 if (guard_map
== body_map
)
3028 auto_diagnostic_group d
;
3029 if (warning_at (body_loc
, OPT_Wmultistatement_macros
,
3030 "macro expands to multiple statements"))
3031 inform (guard_loc
, "some parts of macro expansion are not guarded by "
3032 "this %qs clause", guard_tinfo_to_string (keyword
));
3035 /* Return struct or union type if the alignment of data member, FIELD,
3036 is less than the alignment of TYPE. Otherwise, return NULL_TREE.
3037 If RVALUE is true, only arrays evaluate to pointers. */
3040 check_alignment_of_packed_member (tree type
, tree field
, bool rvalue
)
3042 /* Check alignment of the data member. */
3043 if (TREE_CODE (field
) == FIELD_DECL
3044 && (DECL_PACKED (field
) || TYPE_PACKED (TREE_TYPE (field
)))
3045 /* Ignore FIELDs not laid out yet. */
3046 && DECL_FIELD_OFFSET (field
)
3047 && (!rvalue
|| TREE_CODE (TREE_TYPE (field
)) == ARRAY_TYPE
))
3049 /* Check the expected alignment against the field alignment. */
3050 unsigned int type_align
= min_align_of_type (type
);
3051 tree context
= DECL_CONTEXT (field
);
3052 unsigned int record_align
= min_align_of_type (context
);
3053 if (record_align
< type_align
)
3055 tree field_off
= byte_position (field
);
3056 if (!multiple_of_p (TREE_TYPE (field_off
), field_off
,
3057 size_int (type_align
)))
3064 /* Return struct or union type if the right hand value, RHS,
3065 is an address which takes the unaligned address of packed member
3066 of struct or union when assigning to TYPE.
3067 Otherwise, return NULL_TREE. */
3070 check_address_of_packed_member (tree type
, tree rhs
)
3073 bool indirect
= false;
3075 if (INDIRECT_REF_P (rhs
))
3077 rhs
= TREE_OPERAND (rhs
, 0);
3082 if (TREE_CODE (rhs
) == ADDR_EXPR
)
3084 rhs
= TREE_OPERAND (rhs
, 0);
3088 if (!POINTER_TYPE_P (type
))
3091 type
= TREE_TYPE (type
);
3093 tree context
= NULL_TREE
;
3095 /* Check alignment of the object. */
3096 while (handled_component_p (rhs
))
3098 if (TREE_CODE (rhs
) == COMPONENT_REF
)
3100 tree field
= TREE_OPERAND (rhs
, 1);
3101 context
= check_alignment_of_packed_member (type
, field
, rvalue
);
3105 if (TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
)
3109 rhs
= TREE_OPERAND (rhs
, 0);
3115 /* Check and warn if the right hand value, RHS,
3116 is an address which takes the unaligned address of packed member
3117 of struct or union when assigning to TYPE. */
3120 check_and_warn_address_of_packed_member (tree type
, tree rhs
)
3127 while (TREE_CODE (rhs
) == COMPOUND_EXPR
)
3128 rhs
= TREE_OPERAND (rhs
, 1);
3131 nop_p
|= orig_rhs
!= rhs
;
3133 while (orig_rhs
!= rhs
);
3135 if (TREE_CODE (rhs
) == COND_EXPR
)
3137 /* Check the THEN path. */
3138 check_and_warn_address_of_packed_member
3139 (type
, TREE_OPERAND (rhs
, 1));
3141 /* Check the ELSE path. */
3142 check_and_warn_address_of_packed_member
3143 (type
, TREE_OPERAND (rhs
, 2));
3149 switch (TREE_CODE (rhs
))
3152 /* Address is taken. */
3155 /* Pointer conversion. */
3158 /* Function call. */
3166 = check_address_of_packed_member (type
, rhs
);
3169 location_t loc
= EXPR_LOC_OR_LOC (rhs
, input_location
);
3170 warning_at (loc
, OPT_Waddress_of_packed_member
,
3171 "taking address of packed member of %qT may result "
3172 "in an unaligned pointer value",
3178 /* Warn if the right hand value, RHS,
3179 is an address which takes the unaligned address of packed member
3180 of struct or union when assigning to TYPE. */
3183 warn_for_address_of_packed_member (tree type
, tree rhs
)
3185 if (!warn_address_of_packed_member
)
3188 /* Don't warn if we don't assign RHS to a pointer. */
3189 if (!POINTER_TYPE_P (type
))
3192 check_and_warn_address_of_packed_member (type
, rhs
);
3195 /* Return EXPR + 1. Convenience helper used below. */
3198 plus_one (tree expr
)
3200 tree type
= TREE_TYPE (expr
);
3201 return fold_build2 (PLUS_EXPR
, type
, expr
, build_int_cst (type
, 1));
3204 /* Try to strip the expressions from around a VLA bound added internally
3205 to make it fit the domain mold, including any casts, and return
3206 the result. The goal is to obtain the PARM_DECL the VLA bound may
3210 vla_bound_parm_decl (tree expr
)
3215 if (TREE_CODE (expr
) == NOP_EXPR
)
3216 expr
= TREE_OPERAND (expr
, 0);
3217 if (TREE_CODE (expr
) == PLUS_EXPR
3218 && integer_all_onesp (TREE_OPERAND (expr
, 1)))
3220 expr
= TREE_OPERAND (expr
, 0);
3221 if (TREE_CODE (expr
) == NOP_EXPR
)
3222 expr
= TREE_OPERAND (expr
, 0);
3224 if (TREE_CODE (expr
) == SAVE_EXPR
)
3226 expr
= TREE_OPERAND (expr
, 0);
3227 if (TREE_CODE (expr
) == NOP_EXPR
)
3228 expr
= TREE_OPERAND (expr
, 0);
3233 /* Diagnose mismatches in VLA bounds between function parameters NEWPARMS
3234 of pointer types on a redeclaration of a function previously declared
3235 with CURPARMS at ORIGLOC. */
3238 warn_parm_ptrarray_mismatch (location_t origloc
, tree curparms
, tree newparms
)
3240 /* Maps each named integral parameter seen so far to its position
3241 in the argument list; used to associate VLA sizes with arguments. */
3242 hash_map
<tree
, unsigned> curparm2pos
;
3243 hash_map
<tree
, unsigned> newparm2pos
;
3245 unsigned parmpos
= 1;
3246 for (tree curp
= curparms
, newp
= newparms
; curp
&& newp
;
3247 curp
= TREE_CHAIN (curp
), newp
= TREE_CHAIN (newp
), ++parmpos
)
3249 tree curtyp
= TREE_TYPE (curp
), newtyp
= TREE_TYPE (newp
);
3250 if (INTEGRAL_TYPE_P (curtyp
))
3252 /* Only add named parameters; unnamed ones cannot be referred
3253 to in VLA bounds. */
3254 if (DECL_NAME (curp
))
3255 curparm2pos
.put (curp
, parmpos
);
3256 if (DECL_NAME (newp
))
3257 newparm2pos
.put (newp
, parmpos
);
3262 /* The parameter types should match at this point so only test one. */
3263 if (TREE_CODE (curtyp
) != POINTER_TYPE
)
3268 curtyp
= TREE_TYPE (curtyp
);
3269 newtyp
= TREE_TYPE (newtyp
);
3272 /* Bail on error. */
3275 while (TREE_CODE (curtyp
) == POINTER_TYPE
3276 && TREE_CODE (newtyp
) == POINTER_TYPE
);
3278 if (TREE_CODE (curtyp
) != ARRAY_TYPE
3279 || TREE_CODE (newtyp
) != ARRAY_TYPE
)
3281 if (curtyp
== error_mark_node
3282 || newtyp
== error_mark_node
)
3283 /* Bail on error. */
3289 tree curdom
= TYPE_DOMAIN (curtyp
), newdom
= TYPE_DOMAIN (newtyp
);
3290 tree curbnd
= curdom
? TYPE_MAX_VALUE (curdom
) : NULL_TREE
;
3291 tree newbnd
= newdom
? TYPE_MAX_VALUE (newdom
) : NULL_TREE
;
3294 origloc
= DECL_SOURCE_LOCATION (curp
);
3295 else if (EXPR_P (curp
) && EXPR_HAS_LOCATION (curp
))
3296 origloc
= EXPR_LOCATION (curp
);
3298 /* The location of the parameter in the current redeclaration. */
3299 location_t newloc
= DECL_SOURCE_LOCATION (newp
);
3300 if (origloc
== UNKNOWN_LOCATION
)
3303 /* Issue -Warray-parameter unless one or more mismatches involves
3304 a VLA bound; then issue -Wvla-parameter. */
3305 int opt
= OPT_Warray_parameter_
;
3306 /* Traverse the two array types looking for variable bounds and
3307 comparing the two in each pair for mismatches either in their
3308 positions in the function parameter list or lexicographically
3309 for others. Record the 1-based parameter position of each
3310 mismatch in BNDVEC, and the location of each parameter in
3311 the mismatch in WARNLOC (for the new parameter list) and
3312 NOTELOC (for the current parameter list). */
3313 unsigned bndpos
= 1;
3314 auto_vec
<int> bndvec
;
3315 gcc_rich_location
warnloc (newloc
);
3316 gcc_rich_location
noteloc (origloc
);
3317 for ( ; curtyp
|| newtyp
;
3319 curbnd
= curdom
? TYPE_MAX_VALUE (curdom
) : NULL_TREE
,
3320 newbnd
= newdom
? TYPE_MAX_VALUE (newdom
) : NULL_TREE
)
3322 /* Try to strip each bound down to the PARM_DECL if it does
3323 correspond to one. Either bound can be null if it's
3324 unspecified (i.e., has the [*] form). */
3325 curbnd
= vla_bound_parm_decl (curbnd
);
3326 newbnd
= vla_bound_parm_decl (newbnd
);
3328 /* Peel the current bound off CURTYP and NEWTYP, skipping
3329 over any subsequent pointer types. */
3330 if (curtyp
&& TREE_CODE (curtyp
) == ARRAY_TYPE
)
3333 curtyp
= TREE_TYPE (curtyp
);
3334 while (TREE_CODE (curtyp
) == POINTER_TYPE
);
3335 if (TREE_CODE (curtyp
) == ARRAY_TYPE
)
3336 curdom
= TYPE_DOMAIN (curtyp
);
3343 if (newtyp
&& TREE_CODE (newtyp
) == ARRAY_TYPE
)
3346 newtyp
= TREE_TYPE (newtyp
);
3347 while (TREE_CODE (newtyp
) == POINTER_TYPE
);
3348 if (TREE_CODE (newtyp
) == ARRAY_TYPE
)
3349 newdom
= TYPE_DOMAIN (newtyp
);
3356 /* Move on to the next bound if this one is unspecified. */
3357 if (!curbnd
&& !newbnd
)
3360 /* Try to find each bound in the parameter list. */
3361 const unsigned* const pcurbndpos
= curparm2pos
.get (curbnd
);
3362 const unsigned* const pnewbndpos
= newparm2pos
.get (newbnd
);
3363 /* Move on if both bounds refer to the same parameter. */
3364 if (pcurbndpos
&& pnewbndpos
&& *pcurbndpos
== *pnewbndpos
)
3367 /* Move on if the bounds look the same. */
3368 if (!pcurbndpos
&& !pnewbndpos
3370 && operand_equal_p (curbnd
, newbnd
,
3371 OEP_DECL_NAME
| OEP_LEXICOGRAPHIC
))
3374 if ((curbnd
&& TREE_CODE (curbnd
) != INTEGER_CST
)
3375 || (newbnd
&& TREE_CODE (newbnd
) != INTEGER_CST
))
3376 opt
= OPT_Wvla_parameter
;
3378 /* Record the mismatch. */
3379 bndvec
.safe_push (bndpos
);
3380 /* Underline the bounding parameter in the declaration. */
3381 if (curbnd
&& TREE_CODE (curbnd
) == PARM_DECL
)
3382 noteloc
.add_range (DECL_SOURCE_LOCATION (curbnd
));
3383 if (newbnd
&& TREE_CODE (newbnd
) == PARM_DECL
)
3384 warnloc
.add_range (DECL_SOURCE_LOCATION (newbnd
));
3387 const unsigned nbnds
= bndvec
.length ();
3391 /* Use attr_access to format the parameter types. */
3392 attr_access spec
= { };
3393 const std::string newparmstr
= spec
.array_as_string (TREE_TYPE (newp
));
3394 const std::string curparmstr
= spec
.array_as_string (TREE_TYPE (curp
));
3396 if (warning_n (&warnloc
, opt
, nbnds
,
3397 "mismatch in bound %Z of argument %u declared as %s",
3398 "mismatch in bounds %Z of argument %u declared as %s",
3399 bndvec
.address (), nbnds
, parmpos
, newparmstr
.c_str ()))
3400 inform (¬eloc
, "previously declared as %s", curparmstr
.c_str ());
3404 /* Format EXPR if nonnull and return the formatted string. If EXPR is
3405 null return DFLT. */
3407 static inline const char*
3408 expr_to_str (pretty_printer
&pp
, tree expr
, const char *dflt
)
3413 dump_generic_node (&pp
, expr
, 0, TDF_VOPS
| TDF_MEMSYMS
, false);
3414 return pp_formatted_text (&pp
);
3417 /* Detect and diagnose a mismatch between an attribute access specification
3418 on the original declaration of FNDECL and that on the parameters NEWPARMS
3419 from its redeclaration. ORIGLOC is the location of the first declaration
3420 (FNDECL's is set to the location of the redeclaration). */
3423 warn_parm_array_mismatch (location_t origloc
, tree fndecl
, tree newparms
)
3425 /* The original parameter list (copied from the original declaration
3426 into the current [re]declaration, FNDECL)). The two are equal if
3427 and only if FNDECL is the first declaration. */
3428 tree curparms
= DECL_ARGUMENTS (fndecl
);
3429 if (!curparms
|| !newparms
|| curparms
== newparms
)
3432 if (TREE_CODE (curparms
) != PARM_DECL
3433 || TREE_CODE (newparms
) != PARM_DECL
)
3435 /* Extract the (possibly empty) attribute access specification from
3436 the declaration and its type (it doesn't yet reflect those created
3437 in response to NEWPARMS). */
3439 tree fntype
= TREE_TYPE (fndecl
);
3440 init_attr_rdwr_indices (&cur_idx
, TYPE_ATTRIBUTES (fntype
));
3442 /* Build a (possibly null) chain of access attributes corresponding
3444 const bool builtin
= fndecl_built_in_p (fndecl
);
3445 tree newattrs
= build_attr_access_from_parms (newparms
, builtin
);
3447 /* Extract the (possibly empty) attribute access specification from
3450 init_attr_rdwr_indices (&new_idx
, newattrs
);
3452 if (cur_idx
.is_empty () && new_idx
.is_empty ())
3454 /* If both specs are empty check pointers to VLAs for mismatches. */
3455 warn_parm_ptrarray_mismatch (origloc
, curparms
, newparms
);
3458 /* ...otherwise, if at least one spec isn't empty there may be mismatches,
3459 such as between f(T*) and f(T[1]), where the former mapping would be
3462 /* Create an empty access specification and use it for pointers with
3463 no spec of their own. */
3464 attr_access ptr_spec
= { };
3466 /* Iterate over the two lists of function parameters, comparing their
3467 respective mappings and diagnosing mismatches. */
3468 unsigned parmpos
= 0;
3469 for (tree curp
= curparms
, newp
= newparms
; curp
;
3470 curp
= TREE_CHAIN (curp
), newp
= TREE_CHAIN (newp
), ++parmpos
)
3473 /* Bail on invalid redeclarations with fewer arguments. */
3476 /* Only check pointers and C++ references. */
3477 tree curptype
= TREE_TYPE (curp
);
3478 tree newptype
= TREE_TYPE (newp
);
3479 if (!POINTER_TYPE_P (curptype
) || !POINTER_TYPE_P (newptype
))
3482 /* Skip mismatches in __builtin_va_list that is commonly
3483 an array but that in declarations of built-ins decays
3485 if (builtin
&& TREE_TYPE (newptype
) == TREE_TYPE (va_list_type_node
))
3488 /* Access specs for the argument on the current (previous) and
3489 new (to replace the current) declarations. Either may be null,
3490 indicating the parameter is an ordinary pointer with no size
3491 associated with it. */
3492 attr_access
*cura
= cur_idx
.get (parmpos
);
3493 attr_access
*newa
= new_idx
.get (parmpos
);
3497 /* Continue of both parameters are pointers with no size
3498 associated with it. */
3502 /* Otherwise point at PTR_SPEC and set its parameter pointer
3506 newa
->ptrarg
= parmpos
;
3512 cura
->ptrarg
= parmpos
;
3515 /* Set if the parameter is [re]declared as a VLA. */
3516 const bool cur_vla_p
= cura
->size
|| cura
->minsize
== HOST_WIDE_INT_M1U
;
3517 const bool new_vla_p
= newa
->size
|| newa
->minsize
== HOST_WIDE_INT_M1U
;
3520 origloc
= DECL_SOURCE_LOCATION (curp
);
3521 else if (EXPR_P (curp
) && EXPR_HAS_LOCATION (curp
))
3522 origloc
= EXPR_LOCATION (curp
);
3524 /* The location of the parameter in the current redeclaration. */
3525 location_t newloc
= DECL_SOURCE_LOCATION (newp
);
3526 if (origloc
== UNKNOWN_LOCATION
)
3529 const std::string newparmstr
= newa
->array_as_string (newptype
);
3530 const std::string curparmstr
= cura
->array_as_string (curptype
);
3531 if (new_vla_p
&& !cur_vla_p
)
3533 if (warning_at (newloc
, OPT_Wvla_parameter
,
3534 "argument %u of type %s declared as "
3535 "a variable length array",
3536 parmpos
+ 1, newparmstr
.c_str ()))
3539 ? G_("previously declared as a pointer %s")
3540 : G_("previously declared as an ordinary array %s")),
3541 curparmstr
.c_str ());
3545 if (newa
== &ptr_spec
)
3547 /* The new declaration uses the pointer form. Detect mismatches
3548 between the pointer and a previous array or VLA forms. */
3549 if (cura
->minsize
== HOST_WIDE_INT_M1U
)
3551 /* Diagnose a pointer/VLA mismatch. */
3552 if (warning_at (newloc
, OPT_Wvla_parameter
,
3553 "argument %u of type %s declared "
3555 parmpos
+ 1, newparmstr
.c_str ()))
3557 "previously declared as a variable length array %s",
3558 curparmstr
.c_str ());
3562 if (cura
->minsize
&& cura
->minsize
!= HOST_WIDE_INT_M1U
)
3564 /* Diagnose mismatches between arrays with a constant
3565 bound and pointers. */
3566 if (warning_at (newloc
, OPT_Warray_parameter_
,
3567 "argument %u of type %s declared "
3569 parmpos
+ 1, newparmstr
.c_str ()))
3570 inform (origloc
, "previously declared as an array %s",
3571 curparmstr
.c_str ());
3576 if (!new_vla_p
&& cur_vla_p
)
3578 if (warning_at (newloc
, OPT_Wvla_parameter
,
3579 "argument %u of type %s declared "
3580 "as an ordinary array",
3581 parmpos
+ 1, newparmstr
.c_str ()))
3583 "previously declared as a variable length array %s",
3584 curparmstr
.c_str ());
3588 /* Move on to the next pair of parameters if both of the current
3589 pair are VLAs with a single variable bound that refers to
3590 a parameter at the same position. */
3591 if (newa
->size
&& cura
->size
3592 && newa
->sizarg
!= UINT_MAX
3593 && newa
->sizarg
== cura
->sizarg
3594 && newa
->minsize
== cura
->minsize
3595 && !TREE_PURPOSE (newa
->size
) && !TREE_PURPOSE (cura
->size
))
3598 if (newa
->size
|| cura
->size
)
3600 unsigned newunspec
, curunspec
;
3601 unsigned newbnds
= newa
->vla_bounds (&newunspec
) + newunspec
;
3602 unsigned curbnds
= cura
->vla_bounds (&curunspec
) + curunspec
;
3604 if (newbnds
!= curbnds
)
3606 if (warning_n (newloc
, OPT_Wvla_parameter
, newbnds
,
3607 "argument %u of type %s declared with "
3608 "%u variable bound",
3609 "argument %u of type %s declared with "
3610 "%u variable bounds",
3611 parmpos
+ 1, newparmstr
.c_str (),
3613 inform_n (origloc
, curbnds
,
3614 "previously declared as %s with %u variable bound",
3615 "previously declared as %s with %u variable bounds",
3616 curparmstr
.c_str (), curbnds
);
3620 if (newunspec
> curunspec
)
3622 location_t warnloc
= newloc
, noteloc
= origloc
;
3623 const char *warnparmstr
= newparmstr
.c_str ();
3624 const char *noteparmstr
= curparmstr
.c_str ();
3625 unsigned warnunspec
= newunspec
, noteunspec
= curunspec
;
3627 if (warning_n (warnloc
, OPT_Wvla_parameter
, warnunspec
,
3628 "argument %u of type %s declared with "
3629 "%u unspecified variable bound",
3630 "argument %u of type %s declared with "
3631 "%u unspecified variable bounds",
3632 parmpos
+ 1, warnparmstr
, warnunspec
))
3634 if (warnloc
== newloc
)
3635 inform_n (noteloc
, noteunspec
,
3636 "previously declared as %s with %u unspecified "
3638 "previously declared as %s with %u unspecified "
3640 noteparmstr
, noteunspec
);
3642 inform_n (noteloc
, noteunspec
,
3643 "subsequently declared as %s with %u unspecified "
3645 "subsequently declared as %s with %u unspecified "
3647 noteparmstr
, noteunspec
);
3653 /* Iterate over the lists of VLA variable bounds, comparing each
3654 pair for equality, and diagnosing mismatches. */
3655 for (tree newvbl
= newa
->size
, curvbl
= cura
->size
; newvbl
&& curvbl
;
3656 newvbl
= TREE_CHAIN (newvbl
), curvbl
= TREE_CHAIN (curvbl
))
3658 tree newpos
= TREE_PURPOSE (newvbl
);
3659 tree curpos
= TREE_PURPOSE (curvbl
);
3661 tree newbnd
= vla_bound_parm_decl (TREE_VALUE (newvbl
));
3662 tree curbnd
= vla_bound_parm_decl (TREE_VALUE (curvbl
));
3664 if (newpos
== curpos
&& newbnd
== curbnd
)
3665 /* In the expected case when both bounds either refer to
3666 the same positional parameter or when neither does,
3667 and both are the same expression they are necessarily
3671 pretty_printer pp1
, pp2
;
3672 const char* const newbndstr
= expr_to_str (pp1
, newbnd
, "*");
3673 const char* const curbndstr
= expr_to_str (pp2
, curbnd
, "*");
3675 if (!newpos
!= !curpos
3676 || (newpos
&& !tree_int_cst_equal (newpos
, curpos
)))
3678 /* Diagnose a mismatch between a specified VLA bound and
3679 an unspecified one. This can only happen in the most
3682 Distinguish between the common case of bounds that are
3683 other function parameters such as in
3687 gcc_rich_location
richloc (newloc
);
3691 /* Also underline the VLA bound argument. */
3692 richloc
.add_range (DECL_SOURCE_LOCATION (newbnd
));
3693 warned
= warning_at (&richloc
, OPT_Wvla_parameter
,
3694 "argument %u of type %s declared "
3695 "with mismatched bound argument %E",
3696 parmpos
+ 1, newparmstr
.c_str (),
3700 warned
= warning_at (&richloc
, OPT_Wvla_parameter
,
3701 "argument %u of type %s declared "
3702 "with mismatched bound %<%s%>",
3703 parmpos
+ 1, newparmstr
.c_str (),
3708 gcc_rich_location
richloc (origloc
);
3711 /* Also underline the VLA bound argument. */
3712 richloc
.add_range (DECL_SOURCE_LOCATION (curbnd
));
3713 inform (&richloc
, "previously declared as %s with "
3714 "bound argument %E",
3715 curparmstr
.c_str (), plus_one (curpos
));
3718 inform (&richloc
, "previously declared as %s with bound "
3719 "%<%s%>", curparmstr
.c_str (), curbndstr
);
3725 if (!newpos
&& newbnd
&& curbnd
)
3727 /* The VLA bounds don't refer to other function parameters.
3728 Compare them lexicographically to detect gross mismatches
3729 such as between T[foo()] and T[bar()]. */
3730 if (operand_equal_p (newbnd
, curbnd
,
3731 OEP_DECL_NAME
| OEP_LEXICOGRAPHIC
))
3734 if (warning_at (newloc
, OPT_Wvla_parameter
,
3735 "argument %u of type %s declared with "
3736 "mismatched bound %<%s%>",
3737 parmpos
+ 1, newparmstr
.c_str (), newbndstr
))
3738 inform (origloc
, "previously declared as %s with bound %qs",
3739 curparmstr
.c_str (), curbndstr
);
3744 if (newa
->minsize
== cura
->minsize
3745 || (((newa
->minsize
== 0 && newa
->mode
!= access_deferred
)
3746 || (cura
->minsize
== 0 && cura
->mode
!= access_deferred
))
3747 && newa
!= &ptr_spec
3748 && cura
!= &ptr_spec
))
3751 if (!newa
->static_p
&& !cura
->static_p
&& warn_array_parameter
< 2)
3752 /* Avoid warning about mismatches in ordinary (non-static) arrays
3753 at levels below 2. */
3756 if (warning_at (newloc
, OPT_Warray_parameter_
,
3757 "argument %u of type %s with mismatched bound",
3758 parmpos
+ 1, newparmstr
.c_str ()))
3759 inform (origloc
, "previously declared as %s", curparmstr
.c_str ());
3763 /* Warn about divisions of two sizeof operators when the first one is applied
3764 to an array and the divisor does not equal the size of the array element.
3767 sizeof (ARR) / sizeof (OP)
3769 ARR is the array argument of the first sizeof, ARR_TYPE is its ARRAY_TYPE.
3770 OP1 is the whole second SIZEOF_EXPR, or its argument; TYPE1 is the type
3771 of the second argument. */
3774 maybe_warn_sizeof_array_div (location_t loc
, tree arr
, tree arr_type
,
3775 tree op1
, tree type1
)
3777 tree elt_type
= TREE_TYPE (arr_type
);
3779 if (!warn_sizeof_array_div
3780 /* Don't warn on multidimensional arrays. */
3781 || TREE_CODE (elt_type
) == ARRAY_TYPE
)
3784 if (!tree_int_cst_equal (TYPE_SIZE (elt_type
), TYPE_SIZE (type1
)))
3786 auto_diagnostic_group d
;
3787 if (warning_at (loc
, OPT_Wsizeof_array_div
,
3788 "expression does not compute the number of "
3789 "elements in this array; element type is "
3790 "%qT, not %qT", elt_type
, type1
))
3792 if (EXPR_HAS_LOCATION (op1
))
3794 location_t op1_loc
= EXPR_LOCATION (op1
);
3795 gcc_rich_location
richloc (op1_loc
);
3796 richloc
.add_fixit_insert_before (op1_loc
, "(");
3797 richloc
.add_fixit_insert_after (op1_loc
, ")");
3798 inform (&richloc
, "add parentheses around %qE to "
3799 "silence this warning", op1
);
3802 inform (loc
, "add parentheses around the second %<sizeof%> "
3803 "to silence this warning");
3805 inform (DECL_SOURCE_LOCATION (arr
), "array %qD declared here", arr
);
3810 /* Warn about C++20 [depr.array.comp] array comparisons: "Equality
3811 and relational comparisons between two operands of array type are
3812 deprecated." We also warn in C and earlier C++ standards. CODE is
3813 the code for this comparison, OP0 and OP1 are the operands. */
3816 do_warn_array_compare (location_t location
, tree_code code
, tree op0
, tree op1
)
3820 if (TREE_CODE (op0
) == ADDR_EXPR
)
3821 op0
= TREE_OPERAND (op0
, 0);
3822 if (TREE_CODE (op1
) == ADDR_EXPR
)
3823 op1
= TREE_OPERAND (op1
, 0);
3825 auto_diagnostic_group d
;
3826 if (warning_at (location
, OPT_Warray_compare
,
3827 (c_dialect_cxx () && cxx_dialect
>= cxx20
)
3828 ? G_("comparison between two arrays is deprecated in C++20")
3829 : G_("comparison between two arrays")))
3831 /* C doesn't allow +arr. */
3832 if (c_dialect_cxx ())
3833 inform (location
, "use unary %<+%> which decays operands to pointers "
3834 "or %<&%D[0] %s &%D[0]%> to compare the addresses",
3835 op0
, op_symbol_code (code
), op1
);
3837 inform (location
, "use %<&%D[0] %s &%D[0]%> to compare the addresses",
3838 op0
, op_symbol_code (code
), op1
);
3842 /* Given LHS_VAL ^ RHS_VAL, where LHS_LOC is the location of the LHS,
3843 OPERATOR_LOC is the location of the ^, and RHS_LOC the location of the
3844 RHS, complain with -Wxor-used-as-pow if it looks like the user meant
3845 exponentiation rather than xor. */
3848 check_for_xor_used_as_pow (location_t lhs_loc
, tree lhs_val
,
3849 location_t operator_loc
,
3850 location_t rhs_loc
, tree rhs_val
)
3852 /* Only complain if both args are non-negative integer constants that fit
3854 if (!tree_fits_uhwi_p (lhs_val
) || !tree_fits_uhwi_p (rhs_val
))
3857 /* Only complain if the LHS is 2 or 10. */
3858 unsigned HOST_WIDE_INT lhs_uhwi
= tree_to_uhwi (lhs_val
);
3859 if (lhs_uhwi
!= 2 && lhs_uhwi
!= 10)
3862 unsigned HOST_WIDE_INT rhs_uhwi
= tree_to_uhwi (rhs_val
);
3863 unsigned HOST_WIDE_INT xor_result
= lhs_uhwi
^ rhs_uhwi
;
3864 binary_op_rich_location
loc (operator_loc
,
3865 lhs_val
, rhs_val
, false);
3867 /* Reject cases where we don't have 3 distinct locations.
3868 This can happen e.g. due to macro expansion with
3869 -ftrack-macro-expansion=0 */
3870 if (!(lhs_loc
!= operator_loc
3871 && lhs_loc
!= rhs_loc
3872 && operator_loc
!= rhs_loc
))
3875 /* Reject cases in which any of the locations came from a macro. */
3876 if (from_macro_expansion_at (lhs_loc
)
3877 || from_macro_expansion_at (operator_loc
)
3878 || from_macro_expansion_at (rhs_loc
))
3881 /* If we issue fix-it hints with the warning then we will also issue a
3882 note suggesting how to suppress the warning with a different change.
3883 These proposed changes are incompatible. */
3884 loc
.fixits_cannot_be_auto_applied ();
3886 auto_diagnostic_group d
;
3887 bool warned
= false;
3890 /* Would exponentiation fit in int, in long long, or not at all? */
3891 if (rhs_uhwi
< (INT_TYPE_SIZE
- 1))
3893 unsigned HOST_WIDE_INT suggested_result
= 1 << rhs_uhwi
;
3894 loc
.add_fixit_replace (lhs_loc
, "1");
3895 loc
.add_fixit_replace (operator_loc
, "<<");
3896 warned
= warning_at (&loc
, OPT_Wxor_used_as_pow
,
3897 "result of %<%wu^%wu%> is %wu;"
3898 " did you mean %<1 << %wu%> (%wu)?",
3899 lhs_uhwi
, rhs_uhwi
, xor_result
,
3900 rhs_uhwi
, suggested_result
);
3902 else if (rhs_uhwi
< (LONG_LONG_TYPE_SIZE
- 1))
3904 loc
.add_fixit_replace (lhs_loc
, "1LL");
3905 loc
.add_fixit_replace (operator_loc
, "<<");
3906 warned
= warning_at (&loc
, OPT_Wxor_used_as_pow
,
3907 "result of %<%wu^%wu%> is %wu;"
3908 " did you mean %<1LL << %wu%>?",
3909 lhs_uhwi
, rhs_uhwi
, xor_result
,
3912 else if (rhs_uhwi
<= LONG_LONG_TYPE_SIZE
)
3913 warned
= warning_at (&loc
, OPT_Wxor_used_as_pow
,
3914 "result of %<%wu^%wu%> is %wu;"
3915 " did you mean exponentiation?",
3916 lhs_uhwi
, rhs_uhwi
, xor_result
);
3917 /* Otherwise assume it's an xor. */
3921 gcc_assert (lhs_uhwi
== 10);
3922 loc
.add_fixit_replace (lhs_loc
, "1");
3923 loc
.add_fixit_replace (operator_loc
, "e");
3924 warned
= warning_at (&loc
, OPT_Wxor_used_as_pow
,
3925 "result of %<%wu^%wu%> is %wu;"
3926 " did you mean %<1e%wu%>?",
3927 lhs_uhwi
, rhs_uhwi
, xor_result
,
3932 gcc_rich_location
note_loc (lhs_loc
);
3934 note_loc
.add_fixit_replace (lhs_loc
, "0x2");
3937 gcc_assert (lhs_uhwi
== 10);
3938 note_loc
.add_fixit_replace (lhs_loc
, "0xa");
3940 note_loc
.fixits_cannot_be_auto_applied ();
3942 "you can silence this warning by using a hexadecimal constant"
3943 " (%wx rather than %wd)",
3944 lhs_uhwi
, lhs_uhwi
);