1 /* Diagnostic routines shared by all languages that are variants of C.
2 Copyright (C) 1992-2018 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/>. */
22 #include "coretypes.h"
29 #include "diagnostic.h"
31 #include "stringpool.h"
34 #include "gcc-rich-location.h"
36 #include "c-family/c-indentation.h"
39 /* Print a warning if a constant expression had overflow in folding.
40 Invoke this function on every expression that the language
41 requires to be a constant expression.
42 Note the ANSI C standard says it is erroneous for a
43 constant expression to overflow. */
46 constant_expression_warning (tree value
)
48 if (warn_overflow
&& pedantic
49 && (TREE_CODE (value
) == INTEGER_CST
|| TREE_CODE (value
) == REAL_CST
50 || TREE_CODE (value
) == FIXED_CST
51 || TREE_CODE (value
) == VECTOR_CST
52 || TREE_CODE (value
) == COMPLEX_CST
)
53 && TREE_OVERFLOW (value
))
54 pedwarn (input_location
, OPT_Woverflow
, "overflow in constant expression");
57 /* The same as above but print an unconditional error. */
60 constant_expression_error (tree value
)
62 if ((TREE_CODE (value
) == INTEGER_CST
|| TREE_CODE (value
) == REAL_CST
63 || TREE_CODE (value
) == FIXED_CST
64 || TREE_CODE (value
) == VECTOR_CST
65 || TREE_CODE (value
) == COMPLEX_CST
)
66 && TREE_OVERFLOW (value
))
67 error ("overflow in constant expression");
70 /* Print a warning if an expression result VALUE had an overflow
71 in folding and its operands hadn't. EXPR, which may be null, is
72 the operand of the expression.
74 Invoke this function on every expression that
75 (1) appears in the source code, and
76 (2) is a constant expression that overflowed, and
77 (3) is not already checked by convert_and_check;
78 however, do not invoke this function on operands of explicit casts
79 or when the expression is the result of an operator and any operand
80 already overflowed. */
83 overflow_warning (location_t loc
, tree value
, tree expr
)
85 if (c_inhibit_evaluation_warnings
!= 0)
88 const char *warnfmt
= NULL
;
90 switch (TREE_CODE (value
))
94 ? G_("integer overflow in expression %qE of type %qT "
96 : G_("integer overflow in expression of type %qT "
102 ? G_("floating point overflow in expression %qE "
103 "of type %qT results in %qE")
104 : G_("floating point overflow in expression of type %qT "
110 ? G_("fixed-point overflow in expression %qE of type %qT "
112 : G_("fixed-point overflow in expression of type %qT "
118 ? G_("vector overflow in expression %qE of type %qT "
120 : G_("vector overflow in expression of type %qT "
125 if (TREE_CODE (TREE_REALPART (value
)) == INTEGER_CST
)
127 ? G_("complex integer overflow in expression %qE "
128 "of type %qT results in %qE")
129 : G_("complex integer overflow in expression of type %qT "
131 else if (TREE_CODE (TREE_REALPART (value
)) == REAL_CST
)
133 ? G_("complex floating point overflow in expression %qE "
134 "of type %qT results in %qE")
135 : G_("complex floating point overflow in expression "
136 "of type %qT results in %qE"));
146 warning_at (loc
, OPT_Woverflow
, warnfmt
, expr
, TREE_TYPE (expr
), value
);
148 warning_at (loc
, OPT_Woverflow
, warnfmt
, TREE_TYPE (value
), value
);
150 TREE_NO_WARNING (value
) = 1;
153 /* Helper function for walk_tree. Unwrap C_MAYBE_CONST_EXPRs in an expression
157 unwrap_c_maybe_const (tree
*tp
, int *walk_subtrees
, void *)
159 if (TREE_CODE (*tp
) == C_MAYBE_CONST_EXPR
)
161 *tp
= C_MAYBE_CONST_EXPR_EXPR (*tp
);
162 /* C_MAYBE_CONST_EXPRs don't nest. */
163 *walk_subtrees
= false;
168 /* Warn about uses of logical || / && operator in a context where it
169 is likely that the bitwise equivalent was intended by the
170 programmer. We have seen an expression in which CODE is a binary
171 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
172 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
175 warn_logical_operator (location_t location
, enum tree_code code
, tree type
,
176 enum tree_code code_left
, tree op_left
,
177 enum tree_code
ARG_UNUSED (code_right
), tree op_right
)
179 int or_op
= (code
== TRUTH_ORIF_EXPR
|| code
== TRUTH_OR_EXPR
);
180 int in0_p
, in1_p
, in_p
;
181 tree low0
, low1
, low
, high0
, high1
, high
, lhs
, rhs
, tem
;
182 bool strict_overflow_p
= false;
184 if (!warn_logical_op
)
187 if (code
!= TRUTH_ANDIF_EXPR
188 && code
!= TRUTH_AND_EXPR
189 && code
!= TRUTH_ORIF_EXPR
190 && code
!= TRUTH_OR_EXPR
)
193 /* We don't want to warn if either operand comes from a macro
194 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
196 if (from_macro_expansion_at (EXPR_LOCATION (op_left
))
197 || from_macro_expansion_at (EXPR_LOCATION (op_right
)))
200 /* Warn if &&/|| are being used in a context where it is
201 likely that the bitwise equivalent was intended by the
202 programmer. That is, an expression such as op && MASK
203 where op should not be any boolean expression, nor a
204 constant, and mask seems to be a non-boolean integer constant. */
205 if (TREE_CODE (op_right
) == CONST_DECL
)
206 /* An enumerator counts as a constant. */
207 op_right
= DECL_INITIAL (op_right
);
208 if (!truth_value_p (code_left
)
209 && INTEGRAL_TYPE_P (TREE_TYPE (op_left
))
210 && !CONSTANT_CLASS_P (op_left
)
211 && !TREE_NO_WARNING (op_left
))
213 tree folded_op_right
= fold_for_warn (op_right
);
214 if (TREE_CODE (folded_op_right
) == INTEGER_CST
215 && !integer_zerop (folded_op_right
)
216 && !integer_onep (folded_op_right
))
219 warning_at (location
, OPT_Wlogical_op
, "logical %<or%>"
220 " applied to non-boolean constant");
222 warning_at (location
, OPT_Wlogical_op
, "logical %<and%>"
223 " applied to non-boolean constant");
224 TREE_NO_WARNING (op_left
) = true;
229 /* We do not warn for constants because they are typical of macro
230 expansions that test for features. */
231 if (CONSTANT_CLASS_P (fold_for_warn (op_left
))
232 || CONSTANT_CLASS_P (fold_for_warn (op_right
)))
235 /* This warning only makes sense with logical operands. */
236 if (!(truth_value_p (TREE_CODE (op_left
))
237 || INTEGRAL_TYPE_P (TREE_TYPE (op_left
)))
238 || !(truth_value_p (TREE_CODE (op_right
))
239 || INTEGRAL_TYPE_P (TREE_TYPE (op_right
))))
242 /* The range computations only work with scalars. */
243 if (VECTOR_TYPE_P (TREE_TYPE (op_left
))
244 || VECTOR_TYPE_P (TREE_TYPE (op_right
)))
247 /* We first test whether either side separately is trivially true
248 (with OR) or trivially false (with AND). If so, do not warn.
249 This is a common idiom for testing ranges of data types in
251 op_left
= unshare_expr (op_left
);
252 walk_tree_without_duplicates (&op_left
, unwrap_c_maybe_const
, NULL
);
253 lhs
= make_range (op_left
, &in0_p
, &low0
, &high0
, &strict_overflow_p
);
257 /* If this is an OR operation, invert both sides; now, the result
258 should be always false to get a warning. */
262 tem
= build_range_check (UNKNOWN_LOCATION
, type
, lhs
, in0_p
, low0
, high0
);
263 if (tem
&& integer_zerop (tem
))
266 op_right
= unshare_expr (op_right
);
267 walk_tree_without_duplicates (&op_right
, unwrap_c_maybe_const
, NULL
);
268 rhs
= make_range (op_right
, &in1_p
, &low1
, &high1
, &strict_overflow_p
);
272 /* If this is an OR operation, invert both sides; now, the result
273 should be always false to get a warning. */
277 tem
= build_range_check (UNKNOWN_LOCATION
, type
, rhs
, in1_p
, low1
, high1
);
278 if (tem
&& integer_zerop (tem
))
281 /* If both expressions have the same operand, if we can merge the
283 if (operand_equal_p (lhs
, rhs
, 0)
284 && merge_ranges (&in_p
, &low
, &high
, in0_p
, low0
, high0
,
287 tem
= build_range_check (UNKNOWN_LOCATION
, type
, lhs
, in_p
, low
, high
);
288 /* ... and if the range test is always false, then warn. */
289 if (tem
&& integer_zerop (tem
))
292 warning_at (location
, OPT_Wlogical_op
,
293 "logical %<or%> of collectively exhaustive tests is "
296 warning_at (location
, OPT_Wlogical_op
,
297 "logical %<and%> of mutually exclusive tests is "
300 /* Or warn if the operands have exactly the same range, e.g.
302 else if (tree_int_cst_equal (low0
, low1
)
303 && tree_int_cst_equal (high0
, high1
))
306 warning_at (location
, OPT_Wlogical_op
,
307 "logical %<or%> of equal expressions");
309 warning_at (location
, OPT_Wlogical_op
,
310 "logical %<and%> of equal expressions");
315 /* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
316 with constant indices. */
319 find_array_ref_with_const_idx_r (tree
*expr_p
, int *, void *)
323 if ((TREE_CODE (expr
) == ARRAY_REF
324 || TREE_CODE (expr
) == ARRAY_RANGE_REF
)
325 && (TREE_CODE (fold_for_warn (TREE_OPERAND (expr
, 1)))
327 return integer_type_node
;
332 /* Subroutine of warn_tautological_cmp. Warn about bitwise comparison
333 that always evaluate to true or false. LOC is the location of the
334 ==/!= comparison specified by CODE; LHS and RHS are the usual operands
335 of this comparison. */
338 warn_tautological_bitwise_comparison (const op_location_t
&loc
, tree_code code
,
341 if (code
!= EQ_EXPR
&& code
!= NE_EXPR
)
344 /* Extract the operands from e.g. (x & 8) == 4. */
347 tree stripped_lhs
= tree_strip_any_location_wrapper (lhs
);
348 tree stripped_rhs
= tree_strip_any_location_wrapper (rhs
);
349 if ((TREE_CODE (lhs
) == BIT_AND_EXPR
350 || TREE_CODE (lhs
) == BIT_IOR_EXPR
)
351 && TREE_CODE (stripped_rhs
) == INTEGER_CST
)
352 bitop
= lhs
, cst
= stripped_rhs
;
353 else if ((TREE_CODE (rhs
) == BIT_AND_EXPR
354 || TREE_CODE (rhs
) == BIT_IOR_EXPR
)
355 && TREE_CODE (stripped_lhs
) == INTEGER_CST
)
356 bitop
= rhs
, cst
= stripped_lhs
;
361 tree bitop_op0
= fold_for_warn (TREE_OPERAND (bitop
, 0));
362 if (TREE_CODE (bitop_op0
) == INTEGER_CST
)
363 bitopcst
= bitop_op0
;
365 tree bitop_op1
= fold_for_warn (TREE_OPERAND (bitop
, 1));
366 if (TREE_CODE (bitop_op1
) == INTEGER_CST
)
367 bitopcst
= bitop_op1
;
372 /* Note that the two operands are from before the usual integer
373 conversions, so their types might not be the same.
374 Use the larger of the two precisions and ignore bits outside
376 int prec
= MAX (TYPE_PRECISION (TREE_TYPE (cst
)),
377 TYPE_PRECISION (TREE_TYPE (bitopcst
)));
379 wide_int bitopcstw
= wi::to_wide (bitopcst
, prec
);
380 wide_int cstw
= wi::to_wide (cst
, prec
);
383 if (TREE_CODE (bitop
) == BIT_AND_EXPR
)
384 res
= bitopcstw
& cstw
;
386 res
= bitopcstw
| cstw
;
388 /* For BIT_AND only warn if (CST2 & CST1) != CST1, and
389 for BIT_OR only if (CST2 | CST1) != CST1. */
393 binary_op_rich_location
richloc (loc
, lhs
, rhs
, false);
395 warning_at (&richloc
, OPT_Wtautological_compare
,
396 "bitwise comparison always evaluates to false");
398 warning_at (&richloc
, OPT_Wtautological_compare
,
399 "bitwise comparison always evaluates to true");
402 /* Given LOC from a macro expansion, return the map for the outermost
403 macro in the nest of expansions. */
405 static const line_map_macro
*
406 get_outermost_macro_expansion (location_t loc
)
408 gcc_assert (from_macro_expansion_at (loc
));
410 const line_map
*map
= linemap_lookup (line_table
, loc
);
411 const line_map_macro
*macro_map
;
414 macro_map
= linemap_check_macro (map
);
415 loc
= linemap_unwind_toward_expansion (line_table
, loc
, &map
);
416 } while (linemap_macro_expansion_map_p (map
));
421 /* Given LOC_A and LOC_B from macro expansions, return true if
422 they are "spelled the same" i.e. if they are both directly from
423 expansion of the same non-function-like macro. */
426 spelled_the_same_p (location_t loc_a
, location_t loc_b
)
428 gcc_assert (from_macro_expansion_at (loc_a
));
429 gcc_assert (from_macro_expansion_at (loc_b
));
431 const line_map_macro
*map_a
= get_outermost_macro_expansion (loc_a
);
432 const line_map_macro
*map_b
= get_outermost_macro_expansion (loc_b
);
434 if (map_a
->macro
== map_b
->macro
)
435 if (!cpp_fun_like_macro_p (map_a
->macro
))
441 /* Warn if a self-comparison always evaluates to true or false. LOC
442 is the location of the comparison with code CODE, LHS and RHS are
443 operands of the comparison. */
446 warn_tautological_cmp (const op_location_t
&loc
, enum tree_code code
,
449 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
452 /* Don't warn for various macro expansions. */
453 if (from_macro_expansion_at (loc
))
455 bool lhs_in_macro
= from_macro_expansion_at (EXPR_LOCATION (lhs
));
456 bool rhs_in_macro
= from_macro_expansion_at (EXPR_LOCATION (rhs
));
457 if (lhs_in_macro
|| rhs_in_macro
)
459 /* Don't warn if exactly one is from a macro. */
460 if (!(lhs_in_macro
&& rhs_in_macro
))
463 /* If both are in a macro, only warn if they're spelled the same. */
464 if (!spelled_the_same_p (EXPR_LOCATION (lhs
), EXPR_LOCATION (rhs
)))
468 warn_tautological_bitwise_comparison (loc
, code
, lhs
, rhs
);
470 /* We do not warn for constants because they are typical of macro
471 expansions that test for features, sizeof, and similar. */
472 if (CONSTANT_CLASS_P (fold_for_warn (lhs
))
473 || CONSTANT_CLASS_P (fold_for_warn (rhs
)))
476 /* Don't warn for e.g.
479 if (n == (long) n) ...
481 if ((CONVERT_EXPR_P (lhs
) || TREE_CODE (lhs
) == NON_LVALUE_EXPR
)
482 || (CONVERT_EXPR_P (rhs
) || TREE_CODE (rhs
) == NON_LVALUE_EXPR
))
485 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
486 It could be a NaN, and NaN never compares equal to anything, even
488 if (FLOAT_TYPE_P (TREE_TYPE (lhs
)) || FLOAT_TYPE_P (TREE_TYPE (rhs
)))
491 if (operand_equal_p (lhs
, rhs
, 0))
493 /* Don't warn about array references with constant indices;
494 these are likely to come from a macro. */
495 if (walk_tree_without_duplicates (&lhs
, find_array_ref_with_const_idx_r
,
498 const bool always_true
= (code
== EQ_EXPR
|| code
== LE_EXPR
499 || code
== GE_EXPR
|| code
== UNLE_EXPR
500 || code
== UNGE_EXPR
|| code
== UNEQ_EXPR
);
501 binary_op_rich_location
richloc (loc
, lhs
, rhs
, false);
503 warning_at (&richloc
, OPT_Wtautological_compare
,
504 "self-comparison always evaluates to true");
506 warning_at (&richloc
, OPT_Wtautological_compare
,
507 "self-comparison always evaluates to false");
511 /* Return true iff EXPR only contains boolean operands, or comparisons. */
514 expr_has_boolean_operands_p (tree expr
)
518 if (CONVERT_EXPR_P (expr
))
519 return bool_promoted_to_int_p (expr
);
520 else if (UNARY_CLASS_P (expr
))
521 return expr_has_boolean_operands_p (TREE_OPERAND (expr
, 0));
522 else if (BINARY_CLASS_P (expr
))
523 return (expr_has_boolean_operands_p (TREE_OPERAND (expr
, 0))
524 && expr_has_boolean_operands_p (TREE_OPERAND (expr
, 1)));
525 else if (COMPARISON_CLASS_P (expr
))
531 /* Warn about logical not used on the left hand side operand of a comparison.
532 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
533 Do not warn if RHS is of a boolean type, a logical operator, or
537 warn_logical_not_parentheses (location_t location
, enum tree_code code
,
540 if (TREE_CODE_CLASS (code
) != tcc_comparison
541 || TREE_TYPE (rhs
) == NULL_TREE
542 || TREE_CODE (TREE_TYPE (rhs
)) == BOOLEAN_TYPE
543 || truth_value_p (TREE_CODE (rhs
)))
546 /* Don't warn for expression like !x == ~(bool1 | bool2). */
547 if (expr_has_boolean_operands_p (rhs
))
550 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
551 !(x == 0) or !(y != 0). */
552 if ((code
== EQ_EXPR
|| code
== NE_EXPR
)
553 && integer_zerop (rhs
))
556 auto_diagnostic_group d
;
557 if (warning_at (location
, OPT_Wlogical_not_parentheses
,
558 "logical not is only applied to the left hand side of "
560 && EXPR_HAS_LOCATION (lhs
))
562 location_t lhs_loc
= EXPR_LOCATION (lhs
);
563 rich_location
richloc (line_table
, lhs_loc
);
564 richloc
.add_fixit_insert_before (lhs_loc
, "(");
565 richloc
.add_fixit_insert_after (lhs_loc
, ")");
566 inform (&richloc
, "add parentheses around left hand side "
567 "expression to silence this warning");
571 /* Warn if EXP contains any computations whose results are not used.
572 Return true if a warning is printed; false otherwise. LOCUS is the
573 (potential) location of the expression. */
576 warn_if_unused_value (const_tree exp
, location_t locus
)
579 if (TREE_USED (exp
) || TREE_NO_WARNING (exp
))
582 /* Don't warn about void constructs. This includes casting to void,
583 void function calls, and statement expressions with a final cast
585 if (VOID_TYPE_P (TREE_TYPE (exp
)))
588 if (EXPR_HAS_LOCATION (exp
))
589 locus
= EXPR_LOCATION (exp
);
591 switch (TREE_CODE (exp
))
593 case PREINCREMENT_EXPR
:
594 case POSTINCREMENT_EXPR
:
595 case PREDECREMENT_EXPR
:
596 case POSTDECREMENT_EXPR
:
607 /* For a binding, warn if no side effect within it. */
608 exp
= BIND_EXPR_BODY (exp
);
612 case NON_LVALUE_EXPR
:
614 exp
= TREE_OPERAND (exp
, 0);
617 case TRUTH_ORIF_EXPR
:
618 case TRUTH_ANDIF_EXPR
:
619 /* In && or ||, warn if 2nd operand has no side effect. */
620 exp
= TREE_OPERAND (exp
, 1);
624 if (warn_if_unused_value (TREE_OPERAND (exp
, 0), locus
))
626 /* Let people do `(foo (), 0)' without a warning. */
627 if (TREE_CONSTANT (TREE_OPERAND (exp
, 1)))
629 exp
= TREE_OPERAND (exp
, 1);
633 /* If this is an expression with side effects, don't warn; this
634 case commonly appears in macro expansions. */
635 if (TREE_SIDE_EFFECTS (exp
))
640 /* Don't warn about automatic dereferencing of references, since
641 the user cannot control it. */
642 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp
, 0))) == REFERENCE_TYPE
)
644 exp
= TREE_OPERAND (exp
, 0);
650 /* Referencing a volatile value is a side effect, so don't warn. */
651 if ((DECL_P (exp
) || REFERENCE_CLASS_P (exp
))
652 && TREE_THIS_VOLATILE (exp
))
655 /* If this is an expression which has no operands, there is no value
656 to be unused. There are no such language-independent codes,
657 but front ends may define such. */
658 if (EXPRESSION_CLASS_P (exp
) && TREE_OPERAND_LENGTH (exp
) == 0)
662 return warning_at (locus
, OPT_Wunused_value
, "value computed is not used");
666 /* Print a warning about casts that might indicate violation of strict
667 aliasing rules if -Wstrict-aliasing is used and strict aliasing
668 mode is in effect. LOC is the location of the expression being
669 cast, EXPR might be from inside it. TYPE is the type we're casting
673 strict_aliasing_warning (location_t loc
, tree type
, tree expr
)
675 if (loc
== UNKNOWN_LOCATION
)
676 loc
= input_location
;
678 /* Strip pointer conversion chains and get to the correct original type. */
680 tree otype
= TREE_TYPE (expr
);
682 if (!(flag_strict_aliasing
683 && POINTER_TYPE_P (type
)
684 && POINTER_TYPE_P (otype
)
685 && !VOID_TYPE_P (TREE_TYPE (type
)))
686 /* If the type we are casting to is a ref-all pointer
687 dereferencing it is always valid. */
688 || TYPE_REF_CAN_ALIAS_ALL (type
))
691 if ((warn_strict_aliasing
> 1) && TREE_CODE (expr
) == ADDR_EXPR
692 && (DECL_P (TREE_OPERAND (expr
, 0))
693 || handled_component_p (TREE_OPERAND (expr
, 0))))
695 /* Casting the address of an object to non void pointer. Warn
696 if the cast breaks type based aliasing. */
697 if (!COMPLETE_TYPE_P (TREE_TYPE (type
)) && warn_strict_aliasing
== 2)
699 warning_at (loc
, OPT_Wstrict_aliasing
,
700 "type-punning to incomplete type "
701 "might break strict-aliasing rules");
706 /* warn_strict_aliasing >= 3. This includes the default (3).
707 Only warn if the cast is dereferenced immediately. */
709 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr
, 0)));
710 alias_set_type set2
= get_alias_set (TREE_TYPE (type
));
714 && !alias_set_subset_of (set2
, set1
)
715 && !alias_sets_conflict_p (set1
, set2
))
717 warning_at (loc
, OPT_Wstrict_aliasing
,
718 "dereferencing type-punned "
719 "pointer will break strict-aliasing rules");
722 else if (warn_strict_aliasing
== 2
723 && !alias_sets_must_conflict_p (set1
, set2
))
725 warning_at (loc
, OPT_Wstrict_aliasing
,
726 "dereferencing type-punned "
727 "pointer might break strict-aliasing rules");
732 else if ((warn_strict_aliasing
== 1) && !VOID_TYPE_P (TREE_TYPE (otype
)))
734 /* At this level, warn for any conversions, even if an address is
735 not taken in the same statement. This will likely produce many
736 false positives, but could be useful to pinpoint problems that
737 are not revealed at higher levels. */
738 alias_set_type set1
= get_alias_set (TREE_TYPE (otype
));
739 alias_set_type set2
= get_alias_set (TREE_TYPE (type
));
740 if (!COMPLETE_TYPE_P (type
)
741 || !alias_sets_must_conflict_p (set1
, set2
))
743 warning_at (loc
, OPT_Wstrict_aliasing
,
744 "dereferencing type-punned "
745 "pointer might break strict-aliasing rules");
753 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
754 sizeof as last operand of certain builtins. */
757 sizeof_pointer_memaccess_warning (location_t
*sizeof_arg_loc
, tree callee
,
758 vec
<tree
, va_gc
> *params
, tree
*sizeof_arg
,
759 bool (*comp_types
) (tree
, tree
))
761 tree type
, dest
= NULL_TREE
, src
= NULL_TREE
, tem
;
762 bool strop
= false, cmp
= false;
763 unsigned int idx
= ~0;
766 if (TREE_CODE (callee
) != FUNCTION_DECL
767 || !fndecl_built_in_p (callee
, BUILT_IN_NORMAL
)
768 || vec_safe_length (params
) <= 1)
771 enum built_in_function fncode
= DECL_FUNCTION_CODE (callee
);
774 case BUILT_IN_STRNCMP
:
775 case BUILT_IN_STRNCASECMP
:
778 case BUILT_IN_STRNCPY
:
779 case BUILT_IN_STRNCPY_CHK
:
780 case BUILT_IN_STRNCAT
:
781 case BUILT_IN_STRNCAT_CHK
:
782 case BUILT_IN_STPNCPY
:
783 case BUILT_IN_STPNCPY_CHK
:
786 case BUILT_IN_MEMCPY
:
787 case BUILT_IN_MEMCPY_CHK
:
788 case BUILT_IN_MEMMOVE
:
789 case BUILT_IN_MEMMOVE_CHK
:
790 if (params
->length () < 3)
797 if (params
->length () < 3)
803 case BUILT_IN_MEMCMP
:
805 if (params
->length () < 3)
812 case BUILT_IN_MEMSET
:
813 case BUILT_IN_MEMSET_CHK
:
814 if (params
->length () < 3)
823 case BUILT_IN_STRNDUP
:
828 case BUILT_IN_MEMCHR
:
829 if (params
->length () < 3)
834 case BUILT_IN_SNPRINTF
:
835 case BUILT_IN_SNPRINTF_CHK
:
836 case BUILT_IN_VSNPRINTF
:
837 case BUILT_IN_VSNPRINTF_CHK
:
849 /* Use error_operand_p to detect non-error arguments with an error
850 type that the C++ front-end constructs. */
851 if (error_operand_p (src
)
852 || error_operand_p (dest
)
854 || error_operand_p (sizeof_arg
[idx
]))
857 type
= TYPE_P (sizeof_arg
[idx
])
858 ? sizeof_arg
[idx
] : TREE_TYPE (sizeof_arg
[idx
]);
860 if (!POINTER_TYPE_P (type
))
862 /* The argument type may be an array. Diagnose bounded string
863 copy functions that specify the bound in terms of the source
864 argument rather than the destination unless they are equal
865 to one another. Handle constant sizes and also try to handle
866 sizeof expressions involving VLAs. */
867 if (strop
&& !cmp
&& fncode
!= BUILT_IN_STRNDUP
&& src
)
869 tem
= tree_strip_nop_conversions (src
);
870 if (TREE_CODE (tem
) == ADDR_EXPR
)
871 tem
= TREE_OPERAND (tem
, 0);
873 /* Avoid diagnosing sizeof SRC when SRC is declared with
874 attribute nonstring. */
876 if (get_attr_nonstring_decl (tem
, &dummy
))
879 tree d
= tree_strip_nop_conversions (dest
);
880 if (TREE_CODE (d
) == ADDR_EXPR
)
881 d
= TREE_OPERAND (d
, 0);
883 tree dstsz
= TYPE_SIZE_UNIT (TREE_TYPE (d
));
884 tree srcsz
= TYPE_SIZE_UNIT (TREE_TYPE (tem
));
888 || !operand_equal_p (dstsz
, srcsz
, OEP_LEXICOGRAPHIC
))
889 && operand_equal_p (tem
, sizeof_arg
[idx
], OEP_ADDRESS_OF
))
890 warning_at (sizeof_arg_loc
[idx
], OPT_Wsizeof_pointer_memaccess
,
891 "argument to %<sizeof%> in %qD call is the same "
892 "expression as the source; did you mean to use "
893 "the size of the destination?",
901 && (tem
= tree_strip_nop_conversions (dest
))
902 && POINTER_TYPE_P (TREE_TYPE (tem
))
903 && comp_types (TREE_TYPE (TREE_TYPE (tem
)), type
))
907 && (tem
= tree_strip_nop_conversions (src
))
908 && POINTER_TYPE_P (TREE_TYPE (tem
))
909 && comp_types (TREE_TYPE (TREE_TYPE (tem
)), type
))
912 loc
= sizeof_arg_loc
[idx
];
916 if (!TYPE_P (sizeof_arg
[idx
])
917 && operand_equal_p (dest
, sizeof_arg
[idx
], 0)
918 && comp_types (TREE_TYPE (dest
), type
))
920 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
921 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
922 "argument to %<sizeof%> in %qD call is the same "
923 "expression as the destination; did you mean to "
924 "remove the addressof?", callee
);
925 else if ((TYPE_PRECISION (TREE_TYPE (type
))
926 == TYPE_PRECISION (char_type_node
))
928 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
929 "argument to %<sizeof%> in %qD call is the same "
930 "expression as the destination; did you mean to "
931 "provide an explicit length?", callee
);
933 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
934 "argument to %<sizeof%> in %qD call is the same "
935 "expression as the destination; did you mean to "
936 "dereference it?", callee
);
940 if (POINTER_TYPE_P (TREE_TYPE (dest
))
942 && comp_types (TREE_TYPE (dest
), type
)
943 && !VOID_TYPE_P (TREE_TYPE (type
)))
945 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
946 "argument to %<sizeof%> in %qD call is the same "
947 "pointer type %qT as the destination; expected %qT "
948 "or an explicit length", callee
, TREE_TYPE (dest
),
949 TREE_TYPE (TREE_TYPE (dest
)));
956 if (!TYPE_P (sizeof_arg
[idx
])
957 && operand_equal_p (src
, sizeof_arg
[idx
], 0)
958 && comp_types (TREE_TYPE (src
), type
))
960 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
961 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
962 "argument to %<sizeof%> in %qD call is the same "
963 "expression as the source; did you mean to "
964 "remove the addressof?", callee
);
965 else if ((TYPE_PRECISION (TREE_TYPE (type
))
966 == TYPE_PRECISION (char_type_node
))
968 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
969 "argument to %<sizeof%> in %qD call is the same "
970 "expression as the source; did you mean to "
971 "provide an explicit length?", callee
);
973 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
974 "argument to %<sizeof%> in %qD call is the same "
975 "expression as the source; did you mean to "
976 "dereference it?", callee
);
980 if (POINTER_TYPE_P (TREE_TYPE (src
))
982 && comp_types (TREE_TYPE (src
), type
)
983 && !VOID_TYPE_P (TREE_TYPE (type
)))
985 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
986 "argument to %<sizeof%> in %qD call is the same "
987 "pointer type %qT as the source; expected %qT "
988 "or an explicit length", callee
, TREE_TYPE (src
),
989 TREE_TYPE (TREE_TYPE (src
)));
996 if (!TYPE_P (sizeof_arg
[idx
])
997 && operand_equal_p (dest
, sizeof_arg
[idx
], 0)
998 && comp_types (TREE_TYPE (dest
), type
))
1000 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
1001 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
1002 "argument to %<sizeof%> in %qD call is the same "
1003 "expression as the first source; did you mean to "
1004 "remove the addressof?", callee
);
1005 else if ((TYPE_PRECISION (TREE_TYPE (type
))
1006 == TYPE_PRECISION (char_type_node
))
1008 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
1009 "argument to %<sizeof%> in %qD call is the same "
1010 "expression as the first source; did you mean to "
1011 "provide an explicit length?", callee
);
1013 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
1014 "argument to %<sizeof%> in %qD call is the same "
1015 "expression as the first source; did you mean to "
1016 "dereference it?", callee
);
1020 if (POINTER_TYPE_P (TREE_TYPE (dest
))
1022 && comp_types (TREE_TYPE (dest
), type
)
1023 && !VOID_TYPE_P (TREE_TYPE (type
)))
1025 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
1026 "argument to %<sizeof%> in %qD call is the same "
1027 "pointer type %qT as the first source; expected %qT "
1028 "or an explicit length", callee
, TREE_TYPE (dest
),
1029 TREE_TYPE (TREE_TYPE (dest
)));
1036 if (!TYPE_P (sizeof_arg
[idx
])
1037 && operand_equal_p (src
, sizeof_arg
[idx
], 0)
1038 && comp_types (TREE_TYPE (src
), type
))
1040 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
1041 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
1042 "argument to %<sizeof%> in %qD call is the same "
1043 "expression as the second source; did you mean to "
1044 "remove the addressof?", callee
);
1045 else if ((TYPE_PRECISION (TREE_TYPE (type
))
1046 == TYPE_PRECISION (char_type_node
))
1048 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
1049 "argument to %<sizeof%> in %qD call is the same "
1050 "expression as the second source; did you mean to "
1051 "provide an explicit length?", callee
);
1053 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
1054 "argument to %<sizeof%> in %qD call is the same "
1055 "expression as the second source; did you mean to "
1056 "dereference it?", callee
);
1060 if (POINTER_TYPE_P (TREE_TYPE (src
))
1062 && comp_types (TREE_TYPE (src
), type
)
1063 && !VOID_TYPE_P (TREE_TYPE (type
)))
1065 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
1066 "argument to %<sizeof%> in %qD call is the same "
1067 "pointer type %qT as the second source; expected %qT "
1068 "or an explicit length", callee
, TREE_TYPE (src
),
1069 TREE_TYPE (TREE_TYPE (src
)));
1076 /* Warn for unlikely, improbable, or stupid DECL declarations
1080 check_main_parameter_types (tree decl
)
1082 function_args_iterator iter
;
1086 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl
), type
, iter
)
1088 /* XXX void_type_node belies the abstraction. */
1089 if (type
== void_type_node
|| type
== error_mark_node
)
1093 if (TYPE_ATOMIC (t
))
1094 pedwarn (input_location
, OPT_Wmain
,
1095 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1097 while (POINTER_TYPE_P (t
))
1100 if (TYPE_ATOMIC (t
))
1101 pedwarn (input_location
, OPT_Wmain
,
1102 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1110 if (TYPE_MAIN_VARIANT (type
) != integer_type_node
)
1111 pedwarn (input_location
, OPT_Wmain
,
1112 "first argument of %q+D should be %<int%>", decl
);
1116 if (TREE_CODE (type
) != POINTER_TYPE
1117 || TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
1118 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type
)))
1120 pedwarn (input_location
, OPT_Wmain
,
1121 "second argument of %q+D should be %<char **%>", decl
);
1125 if (TREE_CODE (type
) != POINTER_TYPE
1126 || TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
1127 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type
)))
1129 pedwarn (input_location
, OPT_Wmain
,
1130 "third argument of %q+D should probably be "
1131 "%<char **%>", decl
);
1136 /* It is intentional that this message does not mention the third
1137 argument because it's only mentioned in an appendix of the
1139 if (argct
> 0 && (argct
< 2 || argct
> 3))
1140 pedwarn (input_location
, OPT_Wmain
,
1141 "%q+D takes only zero or two arguments", decl
);
1143 if (stdarg_p (TREE_TYPE (decl
)))
1144 pedwarn (input_location
, OPT_Wmain
,
1145 "%q+D declared as variadic function", decl
);
1148 /* Warns if the conversion of EXPR to TYPE may alter a value.
1149 This is a helper function for warnings_for_convert_and_check. */
1152 conversion_warning (location_t loc
, tree type
, tree expr
, tree result
)
1154 tree expr_type
= TREE_TYPE (expr
);
1155 enum conversion_safety conversion_kind
;
1157 if (!warn_conversion
&& !warn_sign_conversion
&& !warn_float_conversion
)
1160 /* This may happen, because for LHS op= RHS we preevaluate
1161 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
1162 means we could no longer see the code of the EXPR. */
1163 if (TREE_CODE (expr
) == C_MAYBE_CONST_EXPR
)
1164 expr
= C_MAYBE_CONST_EXPR_EXPR (expr
);
1165 if (TREE_CODE (expr
) == SAVE_EXPR
)
1166 expr
= TREE_OPERAND (expr
, 0);
1168 switch (TREE_CODE (expr
))
1176 case TRUTH_ANDIF_EXPR
:
1177 case TRUTH_ORIF_EXPR
:
1178 case TRUTH_AND_EXPR
:
1180 case TRUTH_XOR_EXPR
:
1181 case TRUTH_NOT_EXPR
:
1182 /* Conversion from boolean to a signed:1 bit-field (which only
1183 can hold the values 0 and -1) doesn't lose information - but
1184 it does change the value. */
1185 if (TYPE_PRECISION (type
) == 1 && !TYPE_UNSIGNED (type
))
1186 warning_at (loc
, OPT_Wconversion
,
1187 "conversion to %qT from boolean expression", type
);
1194 conversion_kind
= unsafe_conversion_p (loc
, type
, expr
, result
, true);
1196 if (conversion_kind
== UNSAFE_REAL
)
1197 warnopt
= OPT_Wfloat_conversion
;
1198 else if (conversion_kind
)
1199 warnopt
= OPT_Wconversion
;
1203 if (TREE_CODE_CLASS (TREE_CODE (result
)) == tcc_constant
)
1204 warning_at (loc
, warnopt
,
1205 "conversion from %qT to %qT changes value from %qE to %qE",
1206 expr_type
, type
, expr
, result
);
1208 warning_at (loc
, warnopt
,
1209 "conversion from %qT to %qT changes the value of %qE",
1210 expr_type
, type
, expr
);
1215 /* In case of COND_EXPR, we do not care about the type of
1216 COND_EXPR, only about the conversion of each operand. */
1217 tree op1
= TREE_OPERAND (expr
, 1);
1218 tree op2
= TREE_OPERAND (expr
, 2);
1220 conversion_warning (loc
, type
, op1
, result
);
1221 conversion_warning (loc
, type
, op2
, result
);
1225 default: /* 'expr' is not a constant. */
1226 conversion_kind
= unsafe_conversion_p (loc
, type
, expr
, result
, true);
1227 if (conversion_kind
== UNSAFE_IMAGINARY
)
1228 warning_at (loc
, OPT_Wconversion
,
1229 "conversion from %qT to %qT discards imaginary component",
1234 if (conversion_kind
== UNSAFE_REAL
)
1235 warnopt
= OPT_Wfloat_conversion
;
1236 else if (conversion_kind
)
1237 warnopt
= OPT_Wconversion
;
1240 warning_at (loc
, warnopt
,
1241 "conversion from %qT to %qT may change value",
1247 /* Produce warnings after a conversion. RESULT is the result of
1248 converting EXPR to TYPE. This is a helper function for
1249 convert_and_check and cp_convert_and_check. */
1252 warnings_for_convert_and_check (location_t loc
, tree type
, tree expr
,
1255 loc
= expansion_point_location_if_in_system_header (loc
);
1257 bool cst
= TREE_CODE_CLASS (TREE_CODE (result
)) == tcc_constant
;
1259 tree exprtype
= TREE_TYPE (expr
);
1261 if (TREE_CODE (expr
) == INTEGER_CST
1262 && (TREE_CODE (type
) == INTEGER_TYPE
1263 || TREE_CODE (type
) == ENUMERAL_TYPE
)
1264 && !int_fits_type_p (expr
, type
))
1266 /* Do not diagnose overflow in a constant expression merely
1267 because a conversion overflowed. */
1268 if (TREE_OVERFLOW (result
))
1269 TREE_OVERFLOW (result
) = TREE_OVERFLOW (expr
);
1271 if (TYPE_UNSIGNED (type
))
1273 /* This detects cases like converting -129 or 256 to
1275 if (!int_fits_type_p (expr
, c_common_signed_type (type
)))
1278 warning_at (loc
, OPT_Woverflow
,
1279 (TYPE_UNSIGNED (exprtype
)
1280 ? G_("conversion from %qT to %qT "
1281 "changes value from %qE to %qE")
1282 : G_("unsigned conversion from %qT to %qT "
1283 "changes value from %qE to %qE")),
1284 exprtype
, type
, expr
, result
);
1286 warning_at (loc
, OPT_Woverflow
,
1287 (TYPE_UNSIGNED (exprtype
)
1288 ? G_("conversion from %qT to %qT "
1289 "changes the value of %qE")
1290 : G_("unsigned conversion from %qT to %qT "
1291 "changes the value of %qE")),
1292 exprtype
, type
, expr
);
1295 conversion_warning (loc
, type
, expr
, result
);
1297 else if (!int_fits_type_p (expr
, c_common_unsigned_type (type
)))
1300 warning_at (loc
, OPT_Woverflow
,
1301 "overflow in conversion from %qT to %qT "
1302 "changes value from %qE to %qE",
1303 exprtype
, type
, expr
, result
);
1305 warning_at (loc
, OPT_Woverflow
,
1306 "overflow in conversion from %qT to %qT "
1307 "changes the value of %qE",
1308 exprtype
, type
, expr
);
1310 /* No warning for converting 0x80000000 to int. */
1312 && (TREE_CODE (exprtype
) != INTEGER_TYPE
1313 || TYPE_PRECISION (exprtype
)
1314 != TYPE_PRECISION (type
)))
1317 warning_at (loc
, OPT_Woverflow
,
1318 "overflow in conversion from %qT to %qT "
1319 "changes value from %qE to %qE",
1320 exprtype
, type
, expr
, result
);
1322 warning_at (loc
, OPT_Woverflow
,
1323 "overflow in conversion from %qT to %qT "
1324 "changes the value of %qE",
1325 exprtype
, type
, expr
);
1328 conversion_warning (loc
, type
, expr
, result
);
1330 else if ((TREE_CODE (result
) == INTEGER_CST
1331 || TREE_CODE (result
) == FIXED_CST
) && TREE_OVERFLOW (result
))
1334 warning_at (loc
, OPT_Woverflow
,
1335 "overflow in conversion from %qT to %qT "
1336 "changes value from %qE to %qE",
1337 exprtype
, type
, expr
, result
);
1339 warning_at (loc
, OPT_Woverflow
,
1340 "overflow in conversion from %qT to %qT "
1341 "changes the value of %qE",
1342 exprtype
, type
, expr
);
1345 conversion_warning (loc
, type
, expr
, result
);
1348 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
1349 Used to verify that case values match up with enumerator values. */
1352 match_case_to_enum_1 (tree key
, tree type
, tree label
)
1354 /* Avoid warning about enums that have no enumerators. */
1355 if (TYPE_VALUES (type
) == NULL_TREE
)
1358 char buf
[WIDE_INT_PRINT_BUFFER_SIZE
];
1360 if (tree_fits_uhwi_p (key
))
1361 print_dec (wi::to_wide (key
), buf
, UNSIGNED
);
1362 else if (tree_fits_shwi_p (key
))
1363 print_dec (wi::to_wide (key
), buf
, SIGNED
);
1365 print_hex (wi::to_wide (key
), buf
);
1367 if (TYPE_NAME (type
) == NULL_TREE
)
1368 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label
)),
1369 warn_switch
? OPT_Wswitch
: OPT_Wswitch_enum
,
1370 "case value %qs not in enumerated type",
1373 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label
)),
1374 warn_switch
? OPT_Wswitch
: OPT_Wswitch_enum
,
1375 "case value %qs not in enumerated type %qT",
1379 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
1380 Used to verify that case values match up with enumerator values. */
1383 match_case_to_enum (splay_tree_node node
, void *data
)
1385 tree label
= (tree
) node
->value
;
1386 tree type
= (tree
) data
;
1388 /* Skip default case. */
1389 if (!CASE_LOW (label
))
1392 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
1393 when we did our enum->case scan. Reset our scratch bit after. */
1394 if (!CASE_LOW_SEEN (label
))
1395 match_case_to_enum_1 (CASE_LOW (label
), type
, label
);
1397 CASE_LOW_SEEN (label
) = 0;
1399 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
1400 not set, that means that CASE_HIGH did not appear when we did our
1401 enum->case scan. Reset our scratch bit after. */
1402 if (CASE_HIGH (label
))
1404 if (!CASE_HIGH_SEEN (label
))
1405 match_case_to_enum_1 (CASE_HIGH (label
), type
, label
);
1407 CASE_HIGH_SEEN (label
) = 0;
1413 /* Handle -Wswitch*. Called from the front end after parsing the
1414 switch construct. */
1415 /* ??? Should probably be somewhere generic, since other languages
1416 besides C and C++ would want this. At the moment, however, C/C++
1417 are the only tree-ssa languages that support enumerations at all,
1418 so the point is moot. */
1421 c_do_switch_warnings (splay_tree cases
, location_t switch_location
,
1422 tree type
, tree cond
, bool bool_cond_p
,
1423 bool outside_range_p
)
1425 splay_tree_node default_node
;
1426 splay_tree_node node
;
1429 if (!warn_switch
&& !warn_switch_enum
&& !warn_switch_default
1430 && !warn_switch_bool
)
1433 default_node
= splay_tree_lookup (cases
, (splay_tree_key
) NULL
);
1435 warning_at (switch_location
, OPT_Wswitch_default
,
1436 "switch missing default case");
1438 /* There are certain cases where -Wswitch-bool warnings aren't
1445 so be careful here. */
1446 if (warn_switch_bool
&& bool_cond_p
)
1448 splay_tree_node min_node
;
1449 /* If there's a default node, it's also the value with the minimal
1450 key. So look at the penultimate key (if any). */
1452 min_node
= splay_tree_successor (cases
, (splay_tree_key
) NULL
);
1454 min_node
= splay_tree_min (cases
);
1455 tree min
= min_node
? (tree
) min_node
->key
: NULL_TREE
;
1457 splay_tree_node max_node
= splay_tree_max (cases
);
1458 /* This might be a case range, so look at the value with the
1459 maximal key and then check CASE_HIGH. */
1460 tree max
= max_node
? (tree
) max_node
->value
: NULL_TREE
;
1462 max
= CASE_HIGH (max
) ? CASE_HIGH (max
) : CASE_LOW (max
);
1464 /* If there's a case value > 1 or < 0, that is outside bool
1467 || (max
&& wi::gts_p (wi::to_wide (max
), 1))
1468 || (min
&& wi::lts_p (wi::to_wide (min
), 0))
1476 case, where we want to warn. */
1478 && max
&& wi::to_wide (max
) == 1
1479 && min
&& wi::to_wide (min
) == 0))
1480 warning_at (switch_location
, OPT_Wswitch_bool
,
1481 "switch condition has boolean value");
1484 /* From here on, we only care about enumerated types. */
1485 if (!type
|| TREE_CODE (type
) != ENUMERAL_TYPE
)
1488 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
1489 if (!warn_switch_enum
&& !warn_switch
)
1492 /* Check the cases. Warn about case values which are not members of
1493 the enumerated type. For -Wswitch-enum, or for -Wswitch when
1494 there is no default case, check that exactly all enumeration
1495 literals are covered by the cases. */
1497 /* Clearing COND if it is not an integer constant simplifies
1498 the tests inside the loop below. */
1499 if (TREE_CODE (cond
) != INTEGER_CST
)
1502 /* The time complexity here is O(N*lg(N)) worst case, but for the
1503 common case of monotonically increasing enumerators, it is
1504 O(N), since the nature of the splay tree will keep the next
1505 element adjacent to the root at all times. */
1507 for (chain
= TYPE_VALUES (type
); chain
; chain
= TREE_CHAIN (chain
))
1509 tree value
= TREE_VALUE (chain
);
1510 if (TREE_CODE (value
) == CONST_DECL
)
1511 value
= DECL_INITIAL (value
);
1512 node
= splay_tree_lookup (cases
, (splay_tree_key
) value
);
1515 /* Mark the CASE_LOW part of the case entry as seen. */
1516 tree label
= (tree
) node
->value
;
1517 CASE_LOW_SEEN (label
) = 1;
1521 /* Even though there wasn't an exact match, there might be a
1522 case range which includes the enumerator's value. */
1523 node
= splay_tree_predecessor (cases
, (splay_tree_key
) value
);
1524 if (node
&& CASE_HIGH ((tree
) node
->value
))
1526 tree label
= (tree
) node
->value
;
1527 int cmp
= tree_int_cst_compare (CASE_HIGH (label
), value
);
1530 /* If we match the upper bound exactly, mark the CASE_HIGH
1531 part of the case entry as seen. */
1533 CASE_HIGH_SEEN (label
) = 1;
1538 /* We've now determined that this enumerated literal isn't
1539 handled by the case labels of the switch statement. */
1541 /* If the switch expression is a constant, we only really care
1542 about whether that constant is handled by the switch. */
1543 if (cond
&& tree_int_cst_compare (cond
, value
))
1546 /* If there is a default_node, the only relevant option is
1547 Wswitch-enum. Otherwise, if both are enabled then we prefer
1548 to warn using -Wswitch because -Wswitch is enabled by -Wall
1549 while -Wswitch-enum is explicit. */
1550 warning_at (switch_location
,
1551 (default_node
|| !warn_switch
1554 "enumeration value %qE not handled in switch",
1555 TREE_PURPOSE (chain
));
1558 /* Warn if there are case expressions that don't correspond to
1559 enumerators. This can occur since C and C++ don't enforce
1560 type-checking of assignments to enumeration variables.
1562 The time complexity here is now always O(N) worst case, since
1563 we should have marked both the lower bound and upper bound of
1564 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
1565 above. This scan also resets those fields. */
1567 splay_tree_foreach (cases
, match_case_to_enum
, type
);
1570 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
1571 expression, because B will always be true. */
1574 warn_for_omitted_condop (location_t location
, tree cond
)
1576 /* In C++ template declarations it can happen that the type is dependent
1577 and not yet known, thus TREE_TYPE (cond) == NULL_TREE. */
1578 if (truth_value_p (TREE_CODE (cond
))
1579 || (TREE_TYPE (cond
) != NULL_TREE
1580 && TREE_CODE (TREE_TYPE (cond
)) == BOOLEAN_TYPE
))
1581 warning_at (location
, OPT_Wparentheses
,
1582 "the omitted middle operand in ?: will always be %<true%>, "
1583 "suggest explicit middle operand");
1586 /* Give an error for storing into ARG, which is 'const'. USE indicates
1587 how ARG was being used. */
1590 readonly_error (location_t loc
, tree arg
, enum lvalue_use use
)
1592 gcc_assert (use
== lv_assign
|| use
== lv_increment
|| use
== lv_decrement
1594 STRIP_ANY_LOCATION_WRAPPER (arg
);
1595 /* Using this macro rather than (for example) arrays of messages
1596 ensures that all the format strings are checked at compile
1598 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
1599 : (use == lv_increment ? (I) \
1600 : (use == lv_decrement ? (D) : (AS))))
1601 if (TREE_CODE (arg
) == COMPONENT_REF
)
1603 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg
, 0))))
1604 error_at (loc
, READONLY_MSG (G_("assignment of member "
1605 "%qD in read-only object"),
1606 G_("increment of member "
1607 "%qD in read-only object"),
1608 G_("decrement of member "
1609 "%qD in read-only object"),
1610 G_("member %qD in read-only object "
1611 "used as %<asm%> output")),
1612 TREE_OPERAND (arg
, 1));
1614 error_at (loc
, READONLY_MSG (G_("assignment of read-only member %qD"),
1615 G_("increment of read-only member %qD"),
1616 G_("decrement of read-only member %qD"),
1617 G_("read-only member %qD used as %<asm%> output")),
1618 TREE_OPERAND (arg
, 1));
1620 else if (VAR_P (arg
))
1621 error_at (loc
, READONLY_MSG (G_("assignment of read-only variable %qD"),
1622 G_("increment of read-only variable %qD"),
1623 G_("decrement of read-only variable %qD"),
1624 G_("read-only variable %qD used as %<asm%> output")),
1626 else if (TREE_CODE (arg
) == PARM_DECL
)
1627 error_at (loc
, READONLY_MSG (G_("assignment of read-only parameter %qD"),
1628 G_("increment of read-only parameter %qD"),
1629 G_("decrement of read-only parameter %qD"),
1630 G_("read-only parameter %qD use as %<asm%> output")),
1632 else if (TREE_CODE (arg
) == RESULT_DECL
)
1634 gcc_assert (c_dialect_cxx ());
1635 error_at (loc
, READONLY_MSG (G_("assignment of "
1636 "read-only named return value %qD"),
1638 "read-only named return value %qD"),
1640 "read-only named return value %qD"),
1641 G_("read-only named return value %qD "
1642 "used as %<asm%>output")),
1645 else if (TREE_CODE (arg
) == FUNCTION_DECL
)
1646 error_at (loc
, READONLY_MSG (G_("assignment of function %qD"),
1647 G_("increment of function %qD"),
1648 G_("decrement of function %qD"),
1649 G_("function %qD used as %<asm%> output")),
1652 error_at (loc
, READONLY_MSG (G_("assignment of read-only location %qE"),
1653 G_("increment of read-only location %qE"),
1654 G_("decrement of read-only location %qE"),
1655 G_("read-only location %qE used as %<asm%> output")),
1659 /* Print an error message for an invalid lvalue. USE says
1660 how the lvalue is being used and so selects the error message. LOC
1661 is the location for the error. */
1664 lvalue_error (location_t loc
, enum lvalue_use use
)
1669 error_at (loc
, "lvalue required as left operand of assignment");
1672 error_at (loc
, "lvalue required as increment operand");
1675 error_at (loc
, "lvalue required as decrement operand");
1678 error_at (loc
, "lvalue required as unary %<&%> operand");
1681 error_at (loc
, "lvalue required in asm statement");
1688 /* Print an error message for an invalid indirection of type TYPE.
1689 ERRSTRING is the name of the operator for the indirection. */
1692 invalid_indirection_error (location_t loc
, tree type
, ref_operator errstring
)
1697 gcc_assert (c_dialect_cxx ());
1698 error_at (loc
, "invalid type argument (have %qT)", type
);
1700 case RO_ARRAY_INDEXING
:
1702 "invalid type argument of array indexing (have %qT)",
1707 "invalid type argument of unary %<*%> (have %qT)",
1712 "invalid type argument of %<->%> (have %qT)",
1717 "invalid type argument of %<->*%> (have %qT)",
1720 case RO_IMPLICIT_CONVERSION
:
1722 "invalid type argument of implicit conversion (have %qT)",
1730 /* Subscripting with type char is likely to lose on a machine where
1731 chars are signed. So warn on any machine, but optionally. Don't
1732 warn for unsigned char since that type is safe. Don't warn for
1733 signed char because anyone who uses that must have done so
1734 deliberately. Furthermore, we reduce the false positive load by
1735 warning only for non-constant value of type char.
1736 LOC is the location of the subscripting expression. */
1739 warn_array_subscript_with_type_char (location_t loc
, tree index
)
1741 if (TYPE_MAIN_VARIANT (TREE_TYPE (index
)) == char_type_node
)
1743 /* If INDEX has a location, use it; otherwise use LOC (the location
1744 of the subscripting expression as a whole). */
1745 loc
= EXPR_LOC_OR_LOC (index
, loc
);
1746 STRIP_ANY_LOCATION_WRAPPER (index
);
1747 if (TREE_CODE (index
) != INTEGER_CST
)
1748 warning_at (loc
, OPT_Wchar_subscripts
,
1749 "array subscript has type %<char%>");
1753 /* Implement -Wparentheses for the unexpected C precedence rules, to
1754 cover cases like x + y << z which readers are likely to
1755 misinterpret. We have seen an expression in which CODE is a binary
1756 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
1757 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
1758 CODE_RIGHT may be ERROR_MARK, which means that that side of the
1759 expression was not formed using a binary or unary operator, or it
1760 was enclosed in parentheses. */
1763 warn_about_parentheses (location_t loc
, enum tree_code code
,
1764 enum tree_code code_left
, tree arg_left
,
1765 enum tree_code code_right
, tree arg_right
)
1767 if (!warn_parentheses
)
1770 /* This macro tests that the expression ARG with original tree code
1771 CODE appears to be a boolean expression. or the result of folding a
1772 boolean expression. */
1773 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
1774 (truth_value_p (TREE_CODE (ARG)) \
1775 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
1776 /* Folding may create 0 or 1 integers from other expressions. */ \
1777 || ((CODE) != INTEGER_CST \
1778 && (integer_onep (ARG) || integer_zerop (ARG))))
1783 if (code_left
== PLUS_EXPR
)
1784 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1785 "suggest parentheses around %<+%> inside %<<<%>");
1786 else if (code_right
== PLUS_EXPR
)
1787 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1788 "suggest parentheses around %<+%> inside %<<<%>");
1789 else if (code_left
== MINUS_EXPR
)
1790 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1791 "suggest parentheses around %<-%> inside %<<<%>");
1792 else if (code_right
== MINUS_EXPR
)
1793 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1794 "suggest parentheses around %<-%> inside %<<<%>");
1798 if (code_left
== PLUS_EXPR
)
1799 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1800 "suggest parentheses around %<+%> inside %<>>%>");
1801 else if (code_right
== PLUS_EXPR
)
1802 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1803 "suggest parentheses around %<+%> inside %<>>%>");
1804 else if (code_left
== MINUS_EXPR
)
1805 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1806 "suggest parentheses around %<-%> inside %<>>%>");
1807 else if (code_right
== MINUS_EXPR
)
1808 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1809 "suggest parentheses around %<-%> inside %<>>%>");
1812 case TRUTH_ORIF_EXPR
:
1813 if (code_left
== TRUTH_ANDIF_EXPR
)
1814 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1815 "suggest parentheses around %<&&%> within %<||%>");
1816 else if (code_right
== TRUTH_ANDIF_EXPR
)
1817 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1818 "suggest parentheses around %<&&%> within %<||%>");
1822 if (code_left
== BIT_AND_EXPR
|| code_left
== BIT_XOR_EXPR
1823 || code_left
== PLUS_EXPR
|| code_left
== MINUS_EXPR
)
1824 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1825 "suggest parentheses around arithmetic in operand of %<|%>");
1826 else if (code_right
== BIT_AND_EXPR
|| code_right
== BIT_XOR_EXPR
1827 || code_right
== PLUS_EXPR
|| code_right
== MINUS_EXPR
)
1828 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1829 "suggest parentheses around arithmetic in operand of %<|%>");
1830 /* Check cases like x|y==z */
1831 else if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1832 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1833 "suggest parentheses around comparison in operand of %<|%>");
1834 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1835 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1836 "suggest parentheses around comparison in operand of %<|%>");
1837 /* Check cases like !x | y */
1838 else if (code_left
== TRUTH_NOT_EXPR
1839 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right
, arg_right
))
1840 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1841 "suggest parentheses around operand of "
1842 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
1846 if (code_left
== BIT_AND_EXPR
1847 || code_left
== PLUS_EXPR
|| code_left
== MINUS_EXPR
)
1848 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1849 "suggest parentheses around arithmetic in operand of %<^%>");
1850 else if (code_right
== BIT_AND_EXPR
1851 || code_right
== PLUS_EXPR
|| code_right
== MINUS_EXPR
)
1852 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1853 "suggest parentheses around arithmetic in operand of %<^%>");
1854 /* Check cases like x^y==z */
1855 else if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1856 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1857 "suggest parentheses around comparison in operand of %<^%>");
1858 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1859 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1860 "suggest parentheses around comparison in operand of %<^%>");
1864 if (code_left
== PLUS_EXPR
)
1865 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1866 "suggest parentheses around %<+%> in operand of %<&%>");
1867 else if (code_right
== PLUS_EXPR
)
1868 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1869 "suggest parentheses around %<+%> in operand of %<&%>");
1870 else if (code_left
== MINUS_EXPR
)
1871 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1872 "suggest parentheses around %<-%> in operand of %<&%>");
1873 else if (code_right
== MINUS_EXPR
)
1874 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1875 "suggest parentheses around %<-%> in operand of %<&%>");
1876 /* Check cases like x&y==z */
1877 else if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1878 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1879 "suggest parentheses around comparison in operand of %<&%>");
1880 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1881 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1882 "suggest parentheses around comparison in operand of %<&%>");
1883 /* Check cases like !x & y */
1884 else if (code_left
== TRUTH_NOT_EXPR
1885 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right
, arg_right
))
1886 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1887 "suggest parentheses around operand of "
1888 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
1892 if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1893 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1894 "suggest parentheses around comparison in operand of %<==%>");
1895 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1896 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1897 "suggest parentheses around comparison in operand of %<==%>");
1900 if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1901 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1902 "suggest parentheses around comparison in operand of %<!=%>");
1903 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1904 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1905 "suggest parentheses around comparison in operand of %<!=%>");
1909 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
1911 if (TREE_CODE_CLASS (code_left
) == tcc_comparison
1912 && code_left
!= NE_EXPR
&& code_left
!= EQ_EXPR
1913 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left
)))
1914 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1915 "comparisons like %<X<=Y<=Z%> do not "
1916 "have their mathematical meaning");
1917 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
1918 && code_right
!= NE_EXPR
&& code_right
!= EQ_EXPR
1919 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right
)))
1920 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1921 "comparisons like %<X<=Y<=Z%> do not "
1922 "have their mathematical meaning");
1926 #undef NOT_A_BOOLEAN_EXPR_P
1929 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
1932 warn_for_unused_label (tree label
)
1934 if (!TREE_USED (label
))
1936 if (DECL_INITIAL (label
))
1937 warning (OPT_Wunused_label
, "label %q+D defined but not used", label
);
1939 warning (OPT_Wunused_label
, "label %q+D declared but not defined", label
);
1941 else if (asan_sanitize_use_after_scope ())
1943 if (asan_used_labels
== NULL
)
1944 asan_used_labels
= new hash_set
<tree
> (16);
1946 asan_used_labels
->add (label
);
1950 /* Warn for division by zero according to the value of DIVISOR. LOC
1951 is the location of the division operator. */
1954 warn_for_div_by_zero (location_t loc
, tree divisor
)
1956 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
1957 about division by zero. Do not issue a warning if DIVISOR has a
1958 floating-point type, since we consider 0.0/0.0 a valid way of
1959 generating a NaN. */
1960 if (c_inhibit_evaluation_warnings
== 0
1961 && (integer_zerop (divisor
) || fixed_zerop (divisor
)))
1962 warning_at (loc
, OPT_Wdiv_by_zero
, "division by zero");
1965 /* Warn for patterns where memset appears to be used incorrectly. The
1966 warning location should be LOC. ARG0, and ARG2 are the first and
1967 last arguments to the call, while LITERAL_ZERO_MASK has a 1 bit for
1968 each argument that was a literal zero. */
1971 warn_for_memset (location_t loc
, tree arg0
, tree arg2
,
1972 int literal_zero_mask
)
1974 arg0
= fold_for_warn (arg0
);
1975 arg2
= fold_for_warn (arg2
);
1977 if (warn_memset_transposed_args
1978 && integer_zerop (arg2
)
1979 && (literal_zero_mask
& (1 << 2)) != 0
1980 && (literal_zero_mask
& (1 << 1)) == 0)
1981 warning_at (loc
, OPT_Wmemset_transposed_args
,
1982 "%<memset%> used with constant zero length "
1983 "parameter; this could be due to transposed "
1986 if (warn_memset_elt_size
&& TREE_CODE (arg2
) == INTEGER_CST
)
1989 if (TREE_CODE (arg0
) == ADDR_EXPR
)
1990 arg0
= TREE_OPERAND (arg0
, 0);
1991 tree type
= TREE_TYPE (arg0
);
1992 if (type
!= NULL_TREE
&& TREE_CODE (type
) == ARRAY_TYPE
)
1994 tree elt_type
= TREE_TYPE (type
);
1995 tree domain
= TYPE_DOMAIN (type
);
1996 if (COMPLETE_TYPE_P (elt_type
)
1997 && !integer_onep (TYPE_SIZE_UNIT (elt_type
))
1998 && domain
!= NULL_TREE
1999 && TYPE_MAX_VALUE (domain
)
2000 && TYPE_MIN_VALUE (domain
)
2001 && integer_zerop (TYPE_MIN_VALUE (domain
))
2002 && integer_onep (fold_build2 (MINUS_EXPR
, domain
,
2004 TYPE_MAX_VALUE (domain
))))
2005 warning_at (loc
, OPT_Wmemset_elt_size
,
2006 "%<memset%> used with length equal to "
2007 "number of elements without multiplication "
2013 /* Subroutine of build_binary_op. Give warnings for comparisons
2014 between signed and unsigned quantities that may fail. Do the
2015 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
2016 so that casts will be considered, but default promotions won't
2019 LOCATION is the location of the comparison operator.
2021 The arguments of this function map directly to local variables
2022 of build_binary_op. */
2025 warn_for_sign_compare (location_t location
,
2026 tree orig_op0
, tree orig_op1
,
2028 tree result_type
, enum tree_code resultcode
)
2030 if (error_operand_p (orig_op0
) || error_operand_p (orig_op1
))
2033 int op0_signed
= !TYPE_UNSIGNED (TREE_TYPE (orig_op0
));
2034 int op1_signed
= !TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
2035 int unsignedp0
, unsignedp1
;
2037 /* In C++, check for comparison of different enum types. */
2039 && TREE_CODE (TREE_TYPE (orig_op0
)) == ENUMERAL_TYPE
2040 && TREE_CODE (TREE_TYPE (orig_op1
)) == ENUMERAL_TYPE
2041 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0
))
2042 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1
)))
2044 warning_at (location
,
2045 OPT_Wsign_compare
, "comparison between types %qT and %qT",
2046 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
));
2049 /* Do not warn if the comparison is being done in a signed type,
2050 since the signed type will only be chosen if it can represent
2051 all the values of the unsigned type. */
2052 if (!TYPE_UNSIGNED (result_type
))
2054 /* Do not warn if both operands are unsigned. */
2055 else if (op0_signed
== op1_signed
)
2059 tree sop
, uop
, base_type
;
2063 sop
= orig_op0
, uop
= orig_op1
;
2065 sop
= orig_op1
, uop
= orig_op0
;
2067 STRIP_TYPE_NOPS (sop
);
2068 STRIP_TYPE_NOPS (uop
);
2069 base_type
= (TREE_CODE (result_type
) == COMPLEX_TYPE
2070 ? TREE_TYPE (result_type
) : result_type
);
2072 /* Do not warn if the signed quantity is an unsuffixed integer
2073 literal (or some static constant expression involving such
2074 literals or a conditional expression involving such literals)
2075 and it is non-negative. */
2076 if (tree_expr_nonnegative_warnv_p (sop
, &ovf
))
2078 /* Do not warn if the comparison is an equality operation, the
2079 unsigned quantity is an integral constant, and it would fit
2080 in the result if the result were signed. */
2081 else if (TREE_CODE (uop
) == INTEGER_CST
2082 && (resultcode
== EQ_EXPR
|| resultcode
== NE_EXPR
)
2083 && int_fits_type_p (uop
, c_common_signed_type (base_type
)))
2085 /* In C, do not warn if the unsigned quantity is an enumeration
2086 constant and its maximum value would fit in the result if the
2087 result were signed. */
2088 else if (!c_dialect_cxx() && TREE_CODE (uop
) == INTEGER_CST
2089 && TREE_CODE (TREE_TYPE (uop
)) == ENUMERAL_TYPE
2090 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop
)),
2091 c_common_signed_type (base_type
)))
2094 warning_at (location
, OPT_Wsign_compare
,
2095 "comparison of integer expressions of different "
2096 "signedness: %qT and %qT", TREE_TYPE (orig_op0
),
2097 TREE_TYPE (orig_op1
));
2100 /* Warn if two unsigned values are being compared in a size larger
2101 than their original size, and one (and only one) is the result of
2102 a `~' operator. This comparison will always fail.
2104 Also warn if one operand is a constant, and the constant does not
2105 have all bits set that are set in the ~ operand when it is
2108 op0
= c_common_get_narrower (op0
, &unsignedp0
);
2109 op1
= c_common_get_narrower (op1
, &unsignedp1
);
2111 if ((TREE_CODE (op0
) == BIT_NOT_EXPR
)
2112 ^ (TREE_CODE (op1
) == BIT_NOT_EXPR
))
2114 if (TREE_CODE (op0
) == BIT_NOT_EXPR
)
2115 op0
= c_common_get_narrower (TREE_OPERAND (op0
, 0), &unsignedp0
);
2116 if (TREE_CODE (op1
) == BIT_NOT_EXPR
)
2117 op1
= c_common_get_narrower (TREE_OPERAND (op1
, 0), &unsignedp1
);
2119 if (tree_fits_shwi_p (op0
) || tree_fits_shwi_p (op1
))
2122 HOST_WIDE_INT constant
, mask
;
2126 if (tree_fits_shwi_p (op0
))
2129 unsignedp
= unsignedp1
;
2130 constant
= tree_to_shwi (op0
);
2135 unsignedp
= unsignedp0
;
2136 constant
= tree_to_shwi (op1
);
2139 bits
= TYPE_PRECISION (TREE_TYPE (primop
));
2140 if (bits
< TYPE_PRECISION (result_type
)
2141 && bits
< HOST_BITS_PER_LONG
&& unsignedp
)
2143 mask
= HOST_WIDE_INT_M1U
<< bits
;
2144 if ((mask
& constant
) != mask
)
2147 warning_at (location
, OPT_Wsign_compare
,
2148 "promoted ~unsigned is always non-zero");
2150 warning_at (location
, OPT_Wsign_compare
,
2151 "comparison of promoted ~unsigned with constant");
2155 else if (unsignedp0
&& unsignedp1
2156 && (TYPE_PRECISION (TREE_TYPE (op0
))
2157 < TYPE_PRECISION (result_type
))
2158 && (TYPE_PRECISION (TREE_TYPE (op1
))
2159 < TYPE_PRECISION (result_type
)))
2160 warning_at (location
, OPT_Wsign_compare
,
2161 "comparison of promoted ~unsigned with unsigned");
2165 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
2166 type via c_common_type. If -Wdouble-promotion is in use, and the
2167 conditions for warning have been met, issue a warning. GMSGID is
2168 the warning message. It must have two %T specifiers for the type
2169 that was converted (generally "float") and the type to which it was
2170 converted (generally "double), respectively. LOC is the location
2171 to which the warning should refer. */
2174 do_warn_double_promotion (tree result_type
, tree type1
, tree type2
,
2175 const char *gmsgid
, location_t loc
)
2179 if (!warn_double_promotion
)
2181 /* If the conversion will not occur at run-time, there is no need to
2183 if (c_inhibit_evaluation_warnings
)
2185 /* If an invalid conversion has occured, don't warn. */
2186 if (result_type
== error_mark_node
)
2188 if (TYPE_MAIN_VARIANT (result_type
) != double_type_node
2189 && TYPE_MAIN_VARIANT (result_type
) != complex_double_type_node
)
2191 if (TYPE_MAIN_VARIANT (type1
) == float_type_node
2192 || TYPE_MAIN_VARIANT (type1
) == complex_float_type_node
)
2193 source_type
= type1
;
2194 else if (TYPE_MAIN_VARIANT (type2
) == float_type_node
2195 || TYPE_MAIN_VARIANT (type2
) == complex_float_type_node
)
2196 source_type
= type2
;
2199 warning_at (loc
, OPT_Wdouble_promotion
, gmsgid
, source_type
, result_type
);
2202 /* Possibly warn about unused parameters. */
2205 do_warn_unused_parameter (tree fn
)
2209 for (decl
= DECL_ARGUMENTS (fn
);
2210 decl
; decl
= DECL_CHAIN (decl
))
2211 if (!TREE_USED (decl
) && TREE_CODE (decl
) == PARM_DECL
2212 && DECL_NAME (decl
) && !DECL_ARTIFICIAL (decl
)
2213 && !TREE_NO_WARNING (decl
))
2214 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wunused_parameter
,
2215 "unused parameter %qD", decl
);
2218 /* If DECL is a typedef that is declared in the current function,
2219 record it for the purpose of -Wunused-local-typedefs. */
2222 record_locally_defined_typedef (tree decl
)
2224 struct c_language_function
*l
;
2226 if (!warn_unused_local_typedefs
2228 /* if this is not a locally defined typedef then we are not
2230 || !is_typedef_decl (decl
)
2231 || !decl_function_context (decl
))
2234 l
= (struct c_language_function
*) cfun
->language
;
2235 vec_safe_push (l
->local_typedefs
, decl
);
2238 /* If T is a TYPE_DECL declared locally, mark it as used. */
2241 maybe_record_typedef_use (tree t
)
2243 if (!is_typedef_decl (t
))
2246 TREE_USED (t
) = true;
2249 /* Warn if there are some unused locally defined typedefs in the
2250 current function. */
2253 maybe_warn_unused_local_typedefs (void)
2257 /* The number of times we have emitted -Wunused-local-typedefs
2258 warnings. If this is different from errorcount, that means some
2259 unrelated errors have been issued. In which case, we'll avoid
2260 emitting "unused-local-typedefs" warnings. */
2261 static int unused_local_typedefs_warn_count
;
2262 struct c_language_function
*l
;
2267 if ((l
= (struct c_language_function
*) cfun
->language
) == NULL
)
2270 if (warn_unused_local_typedefs
2271 && errorcount
== unused_local_typedefs_warn_count
)
2273 FOR_EACH_VEC_SAFE_ELT (l
->local_typedefs
, i
, decl
)
2274 if (!TREE_USED (decl
))
2275 warning_at (DECL_SOURCE_LOCATION (decl
),
2276 OPT_Wunused_local_typedefs
,
2277 "typedef %qD locally defined but not used", decl
);
2278 unused_local_typedefs_warn_count
= errorcount
;
2281 vec_free (l
->local_typedefs
);
2284 /* If we're creating an if-else-if condition chain, first see if we
2285 already have this COND in the CHAIN. If so, warn and don't add COND
2286 into the vector, otherwise add the COND there. LOC is the location
2290 warn_duplicated_cond_add_or_warn (location_t loc
, tree cond
, vec
<tree
> **chain
)
2292 /* No chain has been created yet. Do nothing. */
2296 if (TREE_SIDE_EFFECTS (cond
))
2298 /* Uh-oh! This condition has a side-effect, thus invalidates
2308 FOR_EACH_VEC_ELT (**chain
, ix
, t
)
2309 if (operand_equal_p (cond
, t
, 0))
2311 auto_diagnostic_group d
;
2312 if (warning_at (loc
, OPT_Wduplicated_cond
,
2313 "duplicated %<if%> condition"))
2314 inform (EXPR_LOCATION (t
), "previously used here");
2320 && !CONSTANT_CLASS_P (cond
)
2321 /* Don't infinitely grow the chain. */
2322 && (*chain
)->length () < 512)
2323 (*chain
)->safe_push (cond
);
2326 /* Check and possibly warn if two declarations have contradictory
2327 attributes, such as always_inline vs. noinline. */
2330 diagnose_mismatched_attributes (tree olddecl
, tree newdecl
)
2332 bool warned
= false;
2334 tree a1
= lookup_attribute ("optimize", DECL_ATTRIBUTES (olddecl
));
2335 tree a2
= lookup_attribute ("optimize", DECL_ATTRIBUTES (newdecl
));
2336 /* An optimization attribute applied on a declaration after the
2337 definition is likely not what the user wanted. */
2339 && DECL_SAVED_TREE (olddecl
) != NULL_TREE
2340 && (a1
== NULL_TREE
|| !attribute_list_equal (a1
, a2
)))
2341 warned
|= warning (OPT_Wattributes
,
2342 "optimization attribute on %qD follows "
2343 "definition but the attribute doesn%'t match",
2346 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2347 if (DECL_DECLARED_INLINE_P (newdecl
)
2348 && DECL_UNINLINABLE (olddecl
)
2349 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl
)))
2350 warned
|= warning (OPT_Wattributes
, "inline declaration of %qD follows "
2351 "declaration with attribute %<noinline%>", newdecl
);
2352 else if (DECL_DECLARED_INLINE_P (olddecl
)
2353 && DECL_UNINLINABLE (newdecl
)
2354 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl
)))
2355 warned
|= warning (OPT_Wattributes
, "declaration of %q+D with attribute "
2356 "%<noinline%> follows inline declaration", newdecl
);
2361 /* Warn if signed left shift overflows. We don't warn
2362 about left-shifting 1 into the sign bit in C++14; cf.
2363 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
2364 and don't warn for C++2a at all, as signed left shifts never
2366 LOC is a location of the shift; OP0 and OP1 are the operands.
2367 Return true if an overflow is detected, false otherwise. */
2370 maybe_warn_shift_overflow (location_t loc
, tree op0
, tree op1
)
2372 if (TREE_CODE (op0
) != INTEGER_CST
2373 || TREE_CODE (op1
) != INTEGER_CST
)
2376 tree type0
= TREE_TYPE (op0
);
2377 unsigned int prec0
= TYPE_PRECISION (type0
);
2379 /* Left-hand operand must be signed. */
2380 if (TYPE_UNSIGNED (type0
) || cxx_dialect
>= cxx2a
)
2383 unsigned int min_prec
= (wi::min_precision (wi::to_wide (op0
), SIGNED
)
2384 + TREE_INT_CST_LOW (op1
));
2385 /* Handle the case of left-shifting 1 into the sign bit.
2386 * However, shifting 1 _out_ of the sign bit, as in
2387 * INT_MIN << 1, is considered an overflow.
2389 if (!tree_int_cst_sign_bit (op0
) && min_prec
== prec0
+ 1)
2391 /* Never warn for C++14 onwards. */
2392 if (cxx_dialect
>= cxx14
)
2394 /* Otherwise only if -Wshift-overflow=2. But return
2395 true to signal an overflow for the sake of integer
2396 constant expressions. */
2397 if (warn_shift_overflow
< 2)
2401 bool overflowed
= min_prec
> prec0
;
2402 if (overflowed
&& c_inhibit_evaluation_warnings
== 0)
2403 warning_at (loc
, OPT_Wshift_overflow_
,
2404 "result of %qE requires %u bits to represent, "
2405 "but %qT only has %u bits",
2406 build2_loc (loc
, LSHIFT_EXPR
, type0
, op0
, op1
),
2407 min_prec
, type0
, prec0
);
2412 /* Warn about boolean expression compared with an integer value different
2413 from true/false. Warns also e.g. about "(i1 == i2) == 2".
2414 LOC is the location of the comparison, CODE is its code, OP0 and OP1
2415 are the operands of the comparison. The caller must ensure that
2416 either operand is a boolean expression. */
2419 maybe_warn_bool_compare (location_t loc
, enum tree_code code
, tree op0
,
2422 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
2426 if (f
= fold_for_warn (op0
),
2427 TREE_CODE (f
) == INTEGER_CST
)
2429 else if (f
= fold_for_warn (op1
),
2430 TREE_CODE (f
) == INTEGER_CST
)
2435 if (!integer_zerop (cst
) && !integer_onep (cst
))
2437 int sign
= (TREE_CODE (op0
) == INTEGER_CST
2438 ? tree_int_cst_sgn (cst
) : -tree_int_cst_sgn (cst
));
2440 || ((code
== GT_EXPR
|| code
== GE_EXPR
) && sign
< 0)
2441 || ((code
== LT_EXPR
|| code
== LE_EXPR
) && sign
> 0))
2442 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2443 "with boolean expression is always false", cst
);
2445 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2446 "with boolean expression is always true", cst
);
2448 else if (integer_zerop (cst
) || integer_onep (cst
))
2450 /* If the non-constant operand isn't of a boolean type, we
2451 don't want to warn here. */
2452 tree noncst
= TREE_CODE (op0
) == INTEGER_CST
? op1
: op0
;
2453 /* Handle booleans promoted to integers. */
2454 if (bool_promoted_to_int_p (noncst
))
2456 else if (TREE_CODE (TREE_TYPE (noncst
)) != BOOLEAN_TYPE
2457 && !truth_value_p (TREE_CODE (noncst
)))
2459 /* Do some magic to get the right diagnostics. */
2460 bool flag
= TREE_CODE (op0
) == INTEGER_CST
;
2461 flag
= integer_zerop (cst
) ? flag
: !flag
;
2462 if ((code
== GE_EXPR
&& !flag
) || (code
== LE_EXPR
&& flag
))
2463 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2464 "with boolean expression is always true", cst
);
2465 else if ((code
== LT_EXPR
&& !flag
) || (code
== GT_EXPR
&& flag
))
2466 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2467 "with boolean expression is always false", cst
);
2471 /* Warn if an argument at position param_pos is passed to a
2472 restrict-qualified param, and it aliases with another argument.
2473 Return true if a warning has been issued. */
2476 warn_for_restrict (unsigned param_pos
, tree
*argarray
, unsigned nargs
)
2478 tree arg
= argarray
[param_pos
];
2479 if (TREE_VISITED (arg
) || integer_zerop (arg
))
2482 location_t loc
= EXPR_LOC_OR_LOC (arg
, input_location
);
2483 gcc_rich_location
richloc (loc
);
2486 auto_vec
<int, 16> arg_positions
;
2488 for (i
= 0; i
< nargs
; i
++)
2493 tree current_arg
= argarray
[i
];
2494 if (operand_equal_p (arg
, current_arg
, 0))
2496 TREE_VISITED (current_arg
) = 1;
2497 arg_positions
.safe_push (i
+ 1);
2501 if (arg_positions
.is_empty ())
2505 FOR_EACH_VEC_ELT (arg_positions
, i
, pos
)
2507 arg
= argarray
[pos
- 1];
2508 if (EXPR_HAS_LOCATION (arg
))
2509 richloc
.add_range (EXPR_LOCATION (arg
));
2512 return warning_n (&richloc
, OPT_Wrestrict
, arg_positions
.length (),
2513 "passing argument %i to restrict-qualified parameter"
2514 " aliases with argument %Z",
2515 "passing argument %i to restrict-qualified parameter"
2516 " aliases with arguments %Z",
2517 param_pos
+ 1, arg_positions
.address (),
2518 arg_positions
.length ());
2521 /* Callback function to determine whether an expression TP or one of its
2522 subexpressions comes from macro expansion. Used to suppress bogus
2526 expr_from_macro_expansion_r (tree
*tp
, int *, void *)
2528 if (CAN_HAVE_LOCATION_P (*tp
)
2529 && from_macro_expansion_at (EXPR_LOCATION (*tp
)))
2530 return integer_zero_node
;
2535 /* Possibly warn when an if-else has identical branches. */
2538 do_warn_duplicated_branches (tree expr
)
2540 tree thenb
= COND_EXPR_THEN (expr
);
2541 tree elseb
= COND_EXPR_ELSE (expr
);
2543 /* Don't bother if any of the branches is missing. */
2544 if (thenb
== NULL_TREE
|| elseb
== NULL_TREE
)
2547 /* And don't warn for empty statements. */
2548 if (TREE_CODE (thenb
) == NOP_EXPR
2549 && TREE_TYPE (thenb
) == void_type_node
2550 && TREE_OPERAND (thenb
, 0) == size_zero_node
)
2553 /* ... or empty branches. */
2554 if (TREE_CODE (thenb
) == STATEMENT_LIST
2555 && STATEMENT_LIST_HEAD (thenb
) == NULL
)
2558 /* Compute the hash of the then branch. */
2559 inchash::hash
hstate0 (0);
2560 inchash::add_expr (thenb
, hstate0
);
2561 hashval_t h0
= hstate0
.end ();
2563 /* Compute the hash of the else branch. */
2564 inchash::hash
hstate1 (0);
2565 inchash::add_expr (elseb
, hstate1
);
2566 hashval_t h1
= hstate1
.end ();
2568 /* Compare the hashes. */
2570 && operand_equal_p (thenb
, elseb
, OEP_LEXICOGRAPHIC
)
2571 /* Don't warn if any of the branches or their subexpressions comes
2573 && !walk_tree_without_duplicates (&thenb
, expr_from_macro_expansion_r
,
2575 && !walk_tree_without_duplicates (&elseb
, expr_from_macro_expansion_r
,
2577 warning_at (EXPR_LOCATION (expr
), OPT_Wduplicated_branches
,
2578 "this condition has identical branches");
2581 /* Callback for c_genericize to implement -Wduplicated-branches. */
2584 do_warn_duplicated_branches_r (tree
*tp
, int *, void *)
2586 if (TREE_CODE (*tp
) == COND_EXPR
)
2587 do_warn_duplicated_branches (*tp
);
2591 /* Implementation of -Wmultistatement-macros. This warning warns about
2592 cases when a macro expands to multiple statements not wrapped in
2593 do {} while (0) or ({ }) and is used as a body of if/else/for/while
2594 conditionals. For example,
2596 #define DOIT x++; y++
2601 will increment y unconditionally.
2603 BODY_LOC is the location of the first token in the body after labels
2604 have been parsed, NEXT_LOC is the location of the next token after the
2605 body of the conditional has been parsed, and GUARD_LOC is the location
2606 of the conditional. */
2609 warn_for_multistatement_macros (location_t body_loc
, location_t next_loc
,
2610 location_t guard_loc
, enum rid keyword
)
2612 if (!warn_multistatement_macros
)
2615 /* Ain't got time to waste. We only care about macros here. */
2616 if (!from_macro_expansion_at (body_loc
)
2617 || !from_macro_expansion_at (next_loc
))
2620 /* Let's skip macros defined in system headers. */
2621 if (in_system_header_at (body_loc
)
2622 || in_system_header_at (next_loc
))
2625 /* Find the actual tokens in the macro definition. BODY_LOC and
2626 NEXT_LOC have to come from the same spelling location, but they
2627 will resolve to different locations in the context of the macro
2629 location_t body_loc_exp
2630 = linemap_resolve_location (line_table
, body_loc
,
2631 LRK_MACRO_DEFINITION_LOCATION
, NULL
);
2632 location_t next_loc_exp
2633 = linemap_resolve_location (line_table
, next_loc
,
2634 LRK_MACRO_DEFINITION_LOCATION
, NULL
);
2635 location_t guard_loc_exp
2636 = linemap_resolve_location (line_table
, guard_loc
,
2637 LRK_MACRO_DEFINITION_LOCATION
, NULL
);
2639 /* These are some funky cases we don't want to warn about. */
2640 if (body_loc_exp
== guard_loc_exp
2641 || next_loc_exp
== guard_loc_exp
2642 || body_loc_exp
== next_loc_exp
)
2645 /* Find the macro maps for the macro expansions. */
2646 const line_map
*body_map
= linemap_lookup (line_table
, body_loc
);
2647 const line_map
*next_map
= linemap_lookup (line_table
, next_loc
);
2648 const line_map
*guard_map
= linemap_lookup (line_table
, guard_loc
);
2650 /* Now see if the following token (after the body) is coming from the
2651 same macro expansion. If it is, it might be a problem. */
2652 if (body_map
!= next_map
)
2655 /* The conditional itself must not come from the same expansion, because
2656 we don't want to warn about
2657 #define IF if (x) x++; y++
2659 if (guard_map
== body_map
)
2662 /* Handle the case where NEXT and BODY come from the same expansion while
2663 GUARD doesn't, yet we shouldn't warn. E.g.
2665 #define GUARD if (...)
2666 #define GUARD2 GUARD
2668 and in the definition of another macro:
2674 while (linemap_macro_expansion_map_p (guard_map
))
2676 const line_map_macro
*mm
= linemap_check_macro (guard_map
);
2677 guard_loc_exp
= MACRO_MAP_EXPANSION_POINT_LOCATION (mm
);
2678 guard_map
= linemap_lookup (line_table
, guard_loc_exp
);
2679 if (guard_map
== body_map
)
2683 auto_diagnostic_group d
;
2684 if (warning_at (body_loc
, OPT_Wmultistatement_macros
,
2685 "macro expands to multiple statements"))
2686 inform (guard_loc
, "some parts of macro expansion are not guarded by "
2687 "this %qs clause", guard_tinfo_to_string (keyword
));
2690 /* Return struct or union type if the alignment of data memeber, FIELD,
2691 is less than the alignment of TYPE. Otherwise, return NULL_TREE. */
2694 check_alignment_of_packed_member (tree type
, tree field
)
2696 /* Check alignment of the data member. */
2697 if (TREE_CODE (field
) == FIELD_DECL
2698 && (DECL_PACKED (field
)
2699 || TYPE_PACKED (TREE_TYPE (field
))))
2701 /* Check the expected alignment against the field alignment. */
2702 unsigned int type_align
= TYPE_ALIGN (type
);
2703 tree context
= DECL_CONTEXT (field
);
2704 unsigned int record_align
= TYPE_ALIGN (context
);
2705 if ((record_align
% type_align
) != 0)
2707 tree field_off
= byte_position (field
);
2708 if (!multiple_of_p (TREE_TYPE (field_off
), field_off
,
2709 size_int (type_align
/ BITS_PER_UNIT
)))
2716 /* Return struct or union type if the right hand value, RHS, takes the
2717 unaligned address of packed member of struct or union when assigning
2718 to TYPE. Otherwise, return NULL_TREE. */
2721 check_address_of_packed_member (tree type
, tree rhs
)
2723 if (INDIRECT_REF_P (rhs
))
2724 rhs
= TREE_OPERAND (rhs
, 0);
2726 if (TREE_CODE (rhs
) == ADDR_EXPR
)
2727 rhs
= TREE_OPERAND (rhs
, 0);
2729 tree context
= NULL_TREE
;
2731 /* Check alignment of the object. */
2732 while (handled_component_p (rhs
))
2734 if (TREE_CODE (rhs
) == COMPONENT_REF
)
2736 tree field
= TREE_OPERAND (rhs
, 1);
2737 context
= check_alignment_of_packed_member (type
, field
);
2741 rhs
= TREE_OPERAND (rhs
, 0);
2747 /* Check and warn if the right hand value, RHS, takes the unaligned
2748 address of packed member of struct or union when assigning to TYPE. */
2751 check_and_warn_address_of_packed_member (tree type
, tree rhs
)
2753 if (TREE_CODE (rhs
) != COND_EXPR
)
2755 while (TREE_CODE (rhs
) == COMPOUND_EXPR
)
2756 rhs
= TREE_OPERAND (rhs
, 1);
2758 tree context
= check_address_of_packed_member (type
, rhs
);
2761 location_t loc
= EXPR_LOC_OR_LOC (rhs
, input_location
);
2762 warning_at (loc
, OPT_Waddress_of_packed_member
,
2763 "taking address of packed member of %qT may result "
2764 "in an unaligned pointer value",
2770 /* Check the THEN path. */
2771 check_and_warn_address_of_packed_member (type
, TREE_OPERAND (rhs
, 1));
2773 /* Check the ELSE path. */
2774 check_and_warn_address_of_packed_member (type
, TREE_OPERAND (rhs
, 2));
2777 /* Warn if the right hand value, RHS:
2778 1. For CONVERT_P == true, is a pointer value which isn't aligned to a
2780 2. For CONVERT_P == false, is an address which takes the unaligned
2781 address of packed member of struct or union when assigning to TYPE.
2785 warn_for_address_or_pointer_of_packed_member (bool convert_p
, tree type
,
2788 if (!warn_address_of_packed_member
)
2791 /* Don't warn if we don't assign RHS to a pointer. */
2792 if (!POINTER_TYPE_P (type
))
2795 while (TREE_CODE (rhs
) == COMPOUND_EXPR
)
2796 rhs
= TREE_OPERAND (rhs
, 1);
2803 /* Check the original type of RHS. */
2804 switch (TREE_CODE (rhs
))
2808 rhstype
= TREE_TYPE (rhs
);
2809 rhspointer_p
= POINTER_TYPE_P (rhstype
);
2812 rhs
= TREE_OPERAND (rhs
, 0);
2813 if (TREE_CODE (rhs
) == ADDR_EXPR
)
2814 rhs
= TREE_OPERAND (rhs
, 0);
2815 rhstype
= TREE_TYPE (rhs
);
2816 rhspointer_p
= TREE_CODE (rhstype
) == ARRAY_TYPE
;
2822 if (rhspointer_p
&& TYPE_PACKED (TREE_TYPE (rhstype
)))
2824 unsigned int type_align
= TYPE_ALIGN_UNIT (TREE_TYPE (type
));
2825 unsigned int rhs_align
= TYPE_ALIGN_UNIT (TREE_TYPE (rhstype
));
2826 if ((rhs_align
% type_align
) != 0)
2828 location_t location
= EXPR_LOC_OR_LOC (rhs
, input_location
);
2829 warning_at (location
, OPT_Waddress_of_packed_member
,
2830 "converting a packed %qT pointer (alignment %d) "
2831 "to %qT (alignment %d) may may result in an "
2832 "unaligned pointer value",
2833 rhstype
, rhs_align
, type
, type_align
);
2834 tree decl
= TYPE_STUB_DECL (TREE_TYPE (rhstype
));
2835 inform (DECL_SOURCE_LOCATION (decl
), "defined here");
2836 decl
= TYPE_STUB_DECL (TREE_TYPE (type
));
2838 inform (DECL_SOURCE_LOCATION (decl
), "defined here");
2844 /* Get the type of the pointer pointing to. */
2845 type
= TREE_TYPE (type
);
2847 if (TREE_CODE (rhs
) == NOP_EXPR
)
2848 rhs
= TREE_OPERAND (rhs
, 0);
2850 check_and_warn_address_of_packed_member (type
, rhs
);