1 /* Diagnostic routines shared by all languages that are variants of C.
2 Copyright (C) 1992-2019 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"
38 #include "stor-layout.h"
40 /* Print a warning if a constant expression had overflow in folding.
41 Invoke this function on every expression that the language
42 requires to be a constant expression.
43 Note the ANSI C standard says it is erroneous for a
44 constant expression to overflow. */
47 constant_expression_warning (tree value
)
49 if (warn_overflow
&& pedantic
50 && (TREE_CODE (value
) == INTEGER_CST
|| TREE_CODE (value
) == REAL_CST
51 || TREE_CODE (value
) == FIXED_CST
52 || TREE_CODE (value
) == VECTOR_CST
53 || TREE_CODE (value
) == COMPLEX_CST
)
54 && TREE_OVERFLOW (value
))
55 pedwarn (input_location
, OPT_Woverflow
, "overflow in constant expression");
58 /* The same as above but print an unconditional error. */
61 constant_expression_error (tree value
)
63 if ((TREE_CODE (value
) == INTEGER_CST
|| TREE_CODE (value
) == REAL_CST
64 || TREE_CODE (value
) == FIXED_CST
65 || TREE_CODE (value
) == VECTOR_CST
66 || TREE_CODE (value
) == COMPLEX_CST
)
67 && TREE_OVERFLOW (value
))
68 error ("overflow in constant expression");
71 /* Print a warning if an expression result VALUE had an overflow
72 in folding and its operands hadn't. EXPR, which may be null, is
73 the operand of the expression.
75 Invoke this function on every expression that
76 (1) appears in the source code, and
77 (2) is a constant expression that overflowed, and
78 (3) is not already checked by convert_and_check;
79 however, do not invoke this function on operands of explicit casts
80 or when the expression is the result of an operator and any operand
81 already overflowed. */
84 overflow_warning (location_t loc
, tree value
, tree expr
)
86 if (c_inhibit_evaluation_warnings
!= 0)
89 const char *warnfmt
= NULL
;
91 switch (TREE_CODE (value
))
95 ? G_("integer overflow in expression %qE of type %qT "
97 : G_("integer overflow in expression of type %qT "
103 ? G_("floating point overflow in expression %qE "
104 "of type %qT results in %qE")
105 : G_("floating point overflow in expression of type %qT "
111 ? G_("fixed-point overflow in expression %qE of type %qT "
113 : G_("fixed-point overflow in expression of type %qT "
119 ? G_("vector overflow in expression %qE of type %qT "
121 : G_("vector overflow in expression of type %qT "
126 if (TREE_CODE (TREE_REALPART (value
)) == INTEGER_CST
)
128 ? G_("complex integer overflow in expression %qE "
129 "of type %qT results in %qE")
130 : G_("complex integer overflow in expression of type %qT "
132 else if (TREE_CODE (TREE_REALPART (value
)) == REAL_CST
)
134 ? G_("complex floating point overflow in expression %qE "
135 "of type %qT results in %qE")
136 : G_("complex floating point overflow in expression "
137 "of type %qT results in %qE"));
148 warned
= warning_at (loc
, OPT_Woverflow
, warnfmt
, expr
, TREE_TYPE (expr
),
151 warned
= warning_at (loc
, OPT_Woverflow
, warnfmt
, TREE_TYPE (value
),
155 TREE_NO_WARNING (value
) = 1;
158 /* Helper function for walk_tree. Unwrap C_MAYBE_CONST_EXPRs in an expression
162 unwrap_c_maybe_const (tree
*tp
, int *walk_subtrees
, void *)
164 if (TREE_CODE (*tp
) == C_MAYBE_CONST_EXPR
)
166 *tp
= C_MAYBE_CONST_EXPR_EXPR (*tp
);
167 /* C_MAYBE_CONST_EXPRs don't nest. */
168 *walk_subtrees
= false;
173 /* Warn about uses of logical || / && operator in a context where it
174 is likely that the bitwise equivalent was intended by the
175 programmer. We have seen an expression in which CODE is a binary
176 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
177 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
180 warn_logical_operator (location_t location
, enum tree_code code
, tree type
,
181 enum tree_code code_left
, tree op_left
,
182 enum tree_code
ARG_UNUSED (code_right
), tree op_right
)
184 int or_op
= (code
== TRUTH_ORIF_EXPR
|| code
== TRUTH_OR_EXPR
);
185 int in0_p
, in1_p
, in_p
;
186 tree low0
, low1
, low
, high0
, high1
, high
, lhs
, rhs
, tem
;
187 bool strict_overflow_p
= false;
189 if (!warn_logical_op
)
192 if (code
!= TRUTH_ANDIF_EXPR
193 && code
!= TRUTH_AND_EXPR
194 && code
!= TRUTH_ORIF_EXPR
195 && code
!= TRUTH_OR_EXPR
)
198 /* We don't want to warn if either operand comes from a macro
199 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
201 if (from_macro_expansion_at (EXPR_LOCATION (op_left
))
202 || from_macro_expansion_at (EXPR_LOCATION (op_right
)))
205 /* Warn if &&/|| are being used in a context where it is
206 likely that the bitwise equivalent was intended by the
207 programmer. That is, an expression such as op && MASK
208 where op should not be any boolean expression, nor a
209 constant, and mask seems to be a non-boolean integer constant. */
210 if (TREE_CODE (op_right
) == CONST_DECL
)
211 /* An enumerator counts as a constant. */
212 op_right
= DECL_INITIAL (op_right
);
213 if (!truth_value_p (code_left
)
214 && INTEGRAL_TYPE_P (TREE_TYPE (op_left
))
215 && !CONSTANT_CLASS_P (op_left
)
216 && !TREE_NO_WARNING (op_left
))
218 tree folded_op_right
= fold_for_warn (op_right
);
219 if (TREE_CODE (folded_op_right
) == INTEGER_CST
220 && !integer_zerop (folded_op_right
)
221 && !integer_onep (folded_op_right
))
226 = warning_at (location
, OPT_Wlogical_op
,
227 "logical %<or%> applied to non-boolean constant");
230 = warning_at (location
, OPT_Wlogical_op
,
231 "logical %<and%> applied to non-boolean constant");
233 TREE_NO_WARNING (op_left
) = true;
238 /* We do not warn for constants because they are typical of macro
239 expansions that test for features. */
240 if (CONSTANT_CLASS_P (fold_for_warn (op_left
))
241 || CONSTANT_CLASS_P (fold_for_warn (op_right
)))
244 /* This warning only makes sense with logical operands. */
245 if (!(truth_value_p (TREE_CODE (op_left
))
246 || INTEGRAL_TYPE_P (TREE_TYPE (op_left
)))
247 || !(truth_value_p (TREE_CODE (op_right
))
248 || INTEGRAL_TYPE_P (TREE_TYPE (op_right
))))
251 /* The range computations only work with scalars. */
252 if (VECTOR_TYPE_P (TREE_TYPE (op_left
))
253 || VECTOR_TYPE_P (TREE_TYPE (op_right
)))
256 /* We first test whether either side separately is trivially true
257 (with OR) or trivially false (with AND). If so, do not warn.
258 This is a common idiom for testing ranges of data types in
260 op_left
= unshare_expr (op_left
);
261 walk_tree_without_duplicates (&op_left
, unwrap_c_maybe_const
, NULL
);
262 lhs
= make_range (op_left
, &in0_p
, &low0
, &high0
, &strict_overflow_p
);
266 /* If this is an OR operation, invert both sides; now, the result
267 should be always false to get a warning. */
271 tem
= build_range_check (UNKNOWN_LOCATION
, type
, lhs
, in0_p
, low0
, high0
);
272 if (tem
&& integer_zerop (tem
))
275 op_right
= unshare_expr (op_right
);
276 walk_tree_without_duplicates (&op_right
, unwrap_c_maybe_const
, NULL
);
277 rhs
= make_range (op_right
, &in1_p
, &low1
, &high1
, &strict_overflow_p
);
281 /* If this is an OR operation, invert both sides; now, the result
282 should be always false to get a warning. */
286 tem
= build_range_check (UNKNOWN_LOCATION
, type
, rhs
, in1_p
, low1
, high1
);
287 if (tem
&& integer_zerop (tem
))
290 /* If both expressions have the same operand, if we can merge the
292 if (operand_equal_p (lhs
, rhs
, 0)
293 && merge_ranges (&in_p
, &low
, &high
, in0_p
, low0
, high0
,
296 tem
= build_range_check (UNKNOWN_LOCATION
, type
, lhs
, in_p
, low
, high
);
297 /* ... and if the range test is always false, then warn. */
298 if (tem
&& integer_zerop (tem
))
301 warning_at (location
, OPT_Wlogical_op
,
302 "logical %<or%> of collectively exhaustive tests is "
305 warning_at (location
, OPT_Wlogical_op
,
306 "logical %<and%> of mutually exclusive tests is "
309 /* Or warn if the operands have exactly the same range, e.g.
311 else if (tree_int_cst_equal (low0
, low1
)
312 && tree_int_cst_equal (high0
, high1
))
315 warning_at (location
, OPT_Wlogical_op
,
316 "logical %<or%> of equal expressions");
318 warning_at (location
, OPT_Wlogical_op
,
319 "logical %<and%> of equal expressions");
324 /* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
325 with constant indices. */
328 find_array_ref_with_const_idx_r (tree
*expr_p
, int *, void *)
332 if ((TREE_CODE (expr
) == ARRAY_REF
333 || TREE_CODE (expr
) == ARRAY_RANGE_REF
)
334 && (TREE_CODE (fold_for_warn (TREE_OPERAND (expr
, 1)))
336 return integer_type_node
;
341 /* Subroutine of warn_tautological_cmp. Warn about bitwise comparison
342 that always evaluate to true or false. LOC is the location of the
343 ==/!= comparison specified by CODE; LHS and RHS are the usual operands
344 of this comparison. */
347 warn_tautological_bitwise_comparison (const op_location_t
&loc
, tree_code code
,
350 if (code
!= EQ_EXPR
&& code
!= NE_EXPR
)
353 /* Extract the operands from e.g. (x & 8) == 4. */
356 tree stripped_lhs
= tree_strip_any_location_wrapper (lhs
);
357 tree stripped_rhs
= tree_strip_any_location_wrapper (rhs
);
358 if ((TREE_CODE (lhs
) == BIT_AND_EXPR
359 || TREE_CODE (lhs
) == BIT_IOR_EXPR
)
360 && TREE_CODE (stripped_rhs
) == INTEGER_CST
)
361 bitop
= lhs
, cst
= stripped_rhs
;
362 else if ((TREE_CODE (rhs
) == BIT_AND_EXPR
363 || TREE_CODE (rhs
) == BIT_IOR_EXPR
)
364 && TREE_CODE (stripped_lhs
) == INTEGER_CST
)
365 bitop
= rhs
, cst
= stripped_lhs
;
370 tree bitop_op0
= fold_for_warn (TREE_OPERAND (bitop
, 0));
371 if (TREE_CODE (bitop_op0
) == INTEGER_CST
)
372 bitopcst
= bitop_op0
;
374 tree bitop_op1
= fold_for_warn (TREE_OPERAND (bitop
, 1));
375 if (TREE_CODE (bitop_op1
) == INTEGER_CST
)
376 bitopcst
= bitop_op1
;
381 /* Note that the two operands are from before the usual integer
382 conversions, so their types might not be the same.
383 Use the larger of the two precisions and ignore bits outside
385 int prec
= MAX (TYPE_PRECISION (TREE_TYPE (cst
)),
386 TYPE_PRECISION (TREE_TYPE (bitopcst
)));
388 wide_int bitopcstw
= wi::to_wide (bitopcst
, prec
);
389 wide_int cstw
= wi::to_wide (cst
, prec
);
392 if (TREE_CODE (bitop
) == BIT_AND_EXPR
)
393 res
= bitopcstw
& cstw
;
395 res
= bitopcstw
| cstw
;
397 /* For BIT_AND only warn if (CST2 & CST1) != CST1, and
398 for BIT_OR only if (CST2 | CST1) != CST1. */
402 binary_op_rich_location
richloc (loc
, lhs
, rhs
, false);
404 warning_at (&richloc
, OPT_Wtautological_compare
,
405 "bitwise comparison always evaluates to false");
407 warning_at (&richloc
, OPT_Wtautological_compare
,
408 "bitwise comparison always evaluates to true");
411 /* Given LOC from a macro expansion, return the map for the outermost
412 macro in the nest of expansions. */
414 static const line_map_macro
*
415 get_outermost_macro_expansion (location_t loc
)
417 gcc_assert (from_macro_expansion_at (loc
));
419 const line_map
*map
= linemap_lookup (line_table
, loc
);
420 const line_map_macro
*macro_map
;
423 macro_map
= linemap_check_macro (map
);
424 loc
= linemap_unwind_toward_expansion (line_table
, loc
, &map
);
425 } while (linemap_macro_expansion_map_p (map
));
430 /* Given LOC_A and LOC_B from macro expansions, return true if
431 they are "spelled the same" i.e. if they are both directly from
432 expansion of the same non-function-like macro. */
435 spelled_the_same_p (location_t loc_a
, location_t loc_b
)
437 gcc_assert (from_macro_expansion_at (loc_a
));
438 gcc_assert (from_macro_expansion_at (loc_b
));
440 const line_map_macro
*map_a
= get_outermost_macro_expansion (loc_a
);
441 const line_map_macro
*map_b
= get_outermost_macro_expansion (loc_b
);
443 if (map_a
->macro
== map_b
->macro
)
444 if (!cpp_fun_like_macro_p (map_a
->macro
))
450 /* Warn if a self-comparison always evaluates to true or false. LOC
451 is the location of the comparison with code CODE, LHS and RHS are
452 operands of the comparison. */
455 warn_tautological_cmp (const op_location_t
&loc
, enum tree_code code
,
458 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
461 /* Don't warn for various macro expansions. */
462 if (from_macro_expansion_at (loc
))
464 bool lhs_in_macro
= from_macro_expansion_at (EXPR_LOCATION (lhs
));
465 bool rhs_in_macro
= from_macro_expansion_at (EXPR_LOCATION (rhs
));
466 if (lhs_in_macro
|| rhs_in_macro
)
468 /* Don't warn if exactly one is from a macro. */
469 if (!(lhs_in_macro
&& rhs_in_macro
))
472 /* If both are in a macro, only warn if they're spelled the same. */
473 if (!spelled_the_same_p (EXPR_LOCATION (lhs
), EXPR_LOCATION (rhs
)))
477 warn_tautological_bitwise_comparison (loc
, code
, lhs
, rhs
);
479 /* We do not warn for constants because they are typical of macro
480 expansions that test for features, sizeof, and similar. */
481 if (CONSTANT_CLASS_P (fold_for_warn (lhs
))
482 || CONSTANT_CLASS_P (fold_for_warn (rhs
)))
485 /* Don't warn for e.g.
488 if (n == (long) n) ...
490 if ((CONVERT_EXPR_P (lhs
) || TREE_CODE (lhs
) == NON_LVALUE_EXPR
)
491 || (CONVERT_EXPR_P (rhs
) || TREE_CODE (rhs
) == NON_LVALUE_EXPR
))
494 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
495 It could be a NaN, and NaN never compares equal to anything, even
497 if (FLOAT_TYPE_P (TREE_TYPE (lhs
)) || FLOAT_TYPE_P (TREE_TYPE (rhs
)))
500 if (operand_equal_p (lhs
, rhs
, 0))
502 /* Don't warn about array references with constant indices;
503 these are likely to come from a macro. */
504 if (walk_tree_without_duplicates (&lhs
, find_array_ref_with_const_idx_r
,
507 const bool always_true
= (code
== EQ_EXPR
|| code
== LE_EXPR
508 || code
== GE_EXPR
|| code
== UNLE_EXPR
509 || code
== UNGE_EXPR
|| code
== UNEQ_EXPR
);
510 binary_op_rich_location
richloc (loc
, lhs
, rhs
, false);
512 warning_at (&richloc
, OPT_Wtautological_compare
,
513 "self-comparison always evaluates to true");
515 warning_at (&richloc
, OPT_Wtautological_compare
,
516 "self-comparison always evaluates to false");
520 /* Return true iff EXPR only contains boolean operands, or comparisons. */
523 expr_has_boolean_operands_p (tree expr
)
527 if (CONVERT_EXPR_P (expr
))
528 return bool_promoted_to_int_p (expr
);
529 else if (UNARY_CLASS_P (expr
))
530 return expr_has_boolean_operands_p (TREE_OPERAND (expr
, 0));
531 else if (BINARY_CLASS_P (expr
))
532 return (expr_has_boolean_operands_p (TREE_OPERAND (expr
, 0))
533 && expr_has_boolean_operands_p (TREE_OPERAND (expr
, 1)));
534 else if (COMPARISON_CLASS_P (expr
))
540 /* Warn about logical not used on the left hand side operand of a comparison.
541 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
542 Do not warn if RHS is of a boolean type, a logical operator, or
546 warn_logical_not_parentheses (location_t location
, enum tree_code code
,
549 if (TREE_CODE_CLASS (code
) != tcc_comparison
550 || TREE_TYPE (rhs
) == NULL_TREE
551 || TREE_CODE (TREE_TYPE (rhs
)) == BOOLEAN_TYPE
552 || truth_value_p (TREE_CODE (rhs
)))
555 /* Don't warn for expression like !x == ~(bool1 | bool2). */
556 if (expr_has_boolean_operands_p (rhs
))
559 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
560 !(x == 0) or !(y != 0). */
561 if ((code
== EQ_EXPR
|| code
== NE_EXPR
)
562 && integer_zerop (rhs
))
565 auto_diagnostic_group d
;
566 if (warning_at (location
, OPT_Wlogical_not_parentheses
,
567 "logical not is only applied to the left hand side of "
569 && EXPR_HAS_LOCATION (lhs
))
571 location_t lhs_loc
= EXPR_LOCATION (lhs
);
572 rich_location
richloc (line_table
, lhs_loc
);
573 richloc
.add_fixit_insert_before (lhs_loc
, "(");
574 richloc
.add_fixit_insert_after (lhs_loc
, ")");
575 inform (&richloc
, "add parentheses around left hand side "
576 "expression to silence this warning");
580 /* Warn if EXP contains any computations whose results are not used.
581 Return true if a warning is printed; false otherwise. LOCUS is the
582 (potential) location of the expression. */
585 warn_if_unused_value (const_tree exp
, location_t locus
)
588 if (TREE_USED (exp
) || TREE_NO_WARNING (exp
))
591 /* Don't warn about void constructs. This includes casting to void,
592 void function calls, and statement expressions with a final cast
594 if (VOID_TYPE_P (TREE_TYPE (exp
)))
597 if (EXPR_HAS_LOCATION (exp
))
598 locus
= EXPR_LOCATION (exp
);
600 switch (TREE_CODE (exp
))
602 case PREINCREMENT_EXPR
:
603 case POSTINCREMENT_EXPR
:
604 case PREDECREMENT_EXPR
:
605 case POSTDECREMENT_EXPR
:
616 /* For a binding, warn if no side effect within it. */
617 exp
= BIND_EXPR_BODY (exp
);
621 case NON_LVALUE_EXPR
:
623 exp
= TREE_OPERAND (exp
, 0);
626 case TRUTH_ORIF_EXPR
:
627 case TRUTH_ANDIF_EXPR
:
628 /* In && or ||, warn if 2nd operand has no side effect. */
629 exp
= TREE_OPERAND (exp
, 1);
633 if (warn_if_unused_value (TREE_OPERAND (exp
, 0), locus
))
635 /* Let people do `(foo (), 0)' without a warning. */
636 if (TREE_CONSTANT (TREE_OPERAND (exp
, 1)))
638 exp
= TREE_OPERAND (exp
, 1);
642 /* If this is an expression with side effects, don't warn; this
643 case commonly appears in macro expansions. */
644 if (TREE_SIDE_EFFECTS (exp
))
649 /* Don't warn about automatic dereferencing of references, since
650 the user cannot control it. */
651 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp
, 0))) == REFERENCE_TYPE
)
653 exp
= TREE_OPERAND (exp
, 0);
659 /* Referencing a volatile value is a side effect, so don't warn. */
660 if ((DECL_P (exp
) || REFERENCE_CLASS_P (exp
))
661 && TREE_THIS_VOLATILE (exp
))
664 /* If this is an expression which has no operands, there is no value
665 to be unused. There are no such language-independent codes,
666 but front ends may define such. */
667 if (EXPRESSION_CLASS_P (exp
) && TREE_OPERAND_LENGTH (exp
) == 0)
671 return warning_at (locus
, OPT_Wunused_value
, "value computed is not used");
675 /* Print a warning about casts that might indicate violation of strict
676 aliasing rules if -Wstrict-aliasing is used and strict aliasing
677 mode is in effect. LOC is the location of the expression being
678 cast, EXPR might be from inside it. TYPE is the type we're casting
682 strict_aliasing_warning (location_t loc
, tree type
, tree expr
)
684 if (loc
== UNKNOWN_LOCATION
)
685 loc
= input_location
;
687 /* Strip pointer conversion chains and get to the correct original type. */
689 tree otype
= TREE_TYPE (expr
);
691 if (!(flag_strict_aliasing
692 && POINTER_TYPE_P (type
)
693 && POINTER_TYPE_P (otype
)
694 && !VOID_TYPE_P (TREE_TYPE (type
)))
695 /* If the type we are casting to is a ref-all pointer
696 dereferencing it is always valid. */
697 || TYPE_REF_CAN_ALIAS_ALL (type
))
700 if ((warn_strict_aliasing
> 1) && TREE_CODE (expr
) == ADDR_EXPR
701 && (DECL_P (TREE_OPERAND (expr
, 0))
702 || handled_component_p (TREE_OPERAND (expr
, 0))))
704 /* Casting the address of an object to non void pointer. Warn
705 if the cast breaks type based aliasing. */
706 if (!COMPLETE_TYPE_P (TREE_TYPE (type
)) && warn_strict_aliasing
== 2)
708 warning_at (loc
, OPT_Wstrict_aliasing
,
709 "type-punning to incomplete type "
710 "might break strict-aliasing rules");
715 /* warn_strict_aliasing >= 3. This includes the default (3).
716 Only warn if the cast is dereferenced immediately. */
718 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr
, 0)));
719 alias_set_type set2
= get_alias_set (TREE_TYPE (type
));
723 && !alias_set_subset_of (set2
, set1
)
724 && !alias_sets_conflict_p (set1
, set2
))
726 warning_at (loc
, OPT_Wstrict_aliasing
,
727 "dereferencing type-punned "
728 "pointer will break strict-aliasing rules");
731 else if (warn_strict_aliasing
== 2
732 && !alias_sets_must_conflict_p (set1
, set2
))
734 warning_at (loc
, OPT_Wstrict_aliasing
,
735 "dereferencing type-punned "
736 "pointer might break strict-aliasing rules");
741 else if ((warn_strict_aliasing
== 1) && !VOID_TYPE_P (TREE_TYPE (otype
)))
743 /* At this level, warn for any conversions, even if an address is
744 not taken in the same statement. This will likely produce many
745 false positives, but could be useful to pinpoint problems that
746 are not revealed at higher levels. */
747 alias_set_type set1
= get_alias_set (TREE_TYPE (otype
));
748 alias_set_type set2
= get_alias_set (TREE_TYPE (type
));
749 if (!COMPLETE_TYPE_P (type
)
750 || !alias_sets_must_conflict_p (set1
, set2
))
752 warning_at (loc
, OPT_Wstrict_aliasing
,
753 "dereferencing type-punned "
754 "pointer might break strict-aliasing rules");
762 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
763 sizeof as last operand of certain builtins. */
766 sizeof_pointer_memaccess_warning (location_t
*sizeof_arg_loc
, tree callee
,
767 vec
<tree
, va_gc
> *params
, tree
*sizeof_arg
,
768 bool (*comp_types
) (tree
, tree
))
770 tree type
, dest
= NULL_TREE
, src
= NULL_TREE
, tem
;
771 bool strop
= false, cmp
= false;
772 unsigned int idx
= ~0;
775 if (TREE_CODE (callee
) != FUNCTION_DECL
776 || !fndecl_built_in_p (callee
, BUILT_IN_NORMAL
)
777 || vec_safe_length (params
) <= 1)
780 enum built_in_function fncode
= DECL_FUNCTION_CODE (callee
);
783 case BUILT_IN_STRNCMP
:
784 case BUILT_IN_STRNCASECMP
:
787 case BUILT_IN_STRNCPY
:
788 case BUILT_IN_STRNCPY_CHK
:
789 case BUILT_IN_STRNCAT
:
790 case BUILT_IN_STRNCAT_CHK
:
791 case BUILT_IN_STPNCPY
:
792 case BUILT_IN_STPNCPY_CHK
:
795 case BUILT_IN_MEMCPY
:
796 case BUILT_IN_MEMCPY_CHK
:
797 case BUILT_IN_MEMMOVE
:
798 case BUILT_IN_MEMMOVE_CHK
:
799 if (params
->length () < 3)
806 if (params
->length () < 3)
812 case BUILT_IN_MEMCMP
:
814 if (params
->length () < 3)
821 case BUILT_IN_MEMSET
:
822 case BUILT_IN_MEMSET_CHK
:
823 if (params
->length () < 3)
832 case BUILT_IN_STRNDUP
:
837 case BUILT_IN_MEMCHR
:
838 if (params
->length () < 3)
843 case BUILT_IN_SNPRINTF
:
844 case BUILT_IN_SNPRINTF_CHK
:
845 case BUILT_IN_VSNPRINTF
:
846 case BUILT_IN_VSNPRINTF_CHK
:
858 /* Use error_operand_p to detect non-error arguments with an error
859 type that the C++ front-end constructs. */
860 if (error_operand_p (src
)
861 || error_operand_p (dest
)
863 || error_operand_p (sizeof_arg
[idx
]))
866 type
= TYPE_P (sizeof_arg
[idx
])
867 ? sizeof_arg
[idx
] : TREE_TYPE (sizeof_arg
[idx
]);
869 if (!POINTER_TYPE_P (type
))
871 /* The argument type may be an array. Diagnose bounded string
872 copy functions that specify the bound in terms of the source
873 argument rather than the destination unless they are equal
874 to one another. Handle constant sizes and also try to handle
875 sizeof expressions involving VLAs. */
876 if (strop
&& !cmp
&& fncode
!= BUILT_IN_STRNDUP
&& src
)
878 tem
= tree_strip_nop_conversions (src
);
879 if (TREE_CODE (tem
) == ADDR_EXPR
)
880 tem
= TREE_OPERAND (tem
, 0);
882 /* Avoid diagnosing sizeof SRC when SRC is declared with
883 attribute nonstring. */
885 if (get_attr_nonstring_decl (tem
, &dummy
))
888 tree d
= tree_strip_nop_conversions (dest
);
889 if (TREE_CODE (d
) == ADDR_EXPR
)
890 d
= TREE_OPERAND (d
, 0);
892 tree dstsz
= TYPE_SIZE_UNIT (TREE_TYPE (d
));
893 tree srcsz
= TYPE_SIZE_UNIT (TREE_TYPE (tem
));
897 || !operand_equal_p (dstsz
, srcsz
, OEP_LEXICOGRAPHIC
))
898 && operand_equal_p (tem
, sizeof_arg
[idx
], OEP_ADDRESS_OF
))
899 warning_at (sizeof_arg_loc
[idx
], OPT_Wsizeof_pointer_memaccess
,
900 "argument to %<sizeof%> in %qD call is the same "
901 "expression as the source; did you mean to use "
902 "the size of the destination?",
910 && (tem
= tree_strip_nop_conversions (dest
))
911 && POINTER_TYPE_P (TREE_TYPE (tem
))
912 && comp_types (TREE_TYPE (TREE_TYPE (tem
)), type
))
916 && (tem
= tree_strip_nop_conversions (src
))
917 && POINTER_TYPE_P (TREE_TYPE (tem
))
918 && comp_types (TREE_TYPE (TREE_TYPE (tem
)), type
))
921 loc
= sizeof_arg_loc
[idx
];
925 if (!TYPE_P (sizeof_arg
[idx
])
926 && operand_equal_p (dest
, sizeof_arg
[idx
], 0)
927 && comp_types (TREE_TYPE (dest
), type
))
929 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
930 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
931 "argument to %<sizeof%> in %qD call is the same "
932 "expression as the destination; did you mean to "
933 "remove the addressof?", callee
);
934 else if ((TYPE_PRECISION (TREE_TYPE (type
))
935 == TYPE_PRECISION (char_type_node
))
937 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
938 "argument to %<sizeof%> in %qD call is the same "
939 "expression as the destination; did you mean to "
940 "provide an explicit length?", callee
);
942 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
943 "argument to %<sizeof%> in %qD call is the same "
944 "expression as the destination; did you mean to "
945 "dereference it?", callee
);
949 if (POINTER_TYPE_P (TREE_TYPE (dest
))
951 && comp_types (TREE_TYPE (dest
), type
)
952 && !VOID_TYPE_P (TREE_TYPE (type
)))
954 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
955 "argument to %<sizeof%> in %qD call is the same "
956 "pointer type %qT as the destination; expected %qT "
957 "or an explicit length", callee
, TREE_TYPE (dest
),
958 TREE_TYPE (TREE_TYPE (dest
)));
965 if (!TYPE_P (sizeof_arg
[idx
])
966 && operand_equal_p (src
, sizeof_arg
[idx
], 0)
967 && comp_types (TREE_TYPE (src
), type
))
969 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
970 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
971 "argument to %<sizeof%> in %qD call is the same "
972 "expression as the source; did you mean to "
973 "remove the addressof?", callee
);
974 else if ((TYPE_PRECISION (TREE_TYPE (type
))
975 == TYPE_PRECISION (char_type_node
))
977 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
978 "argument to %<sizeof%> in %qD call is the same "
979 "expression as the source; did you mean to "
980 "provide an explicit length?", callee
);
982 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
983 "argument to %<sizeof%> in %qD call is the same "
984 "expression as the source; did you mean to "
985 "dereference it?", callee
);
989 if (POINTER_TYPE_P (TREE_TYPE (src
))
991 && comp_types (TREE_TYPE (src
), type
)
992 && !VOID_TYPE_P (TREE_TYPE (type
)))
994 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
995 "argument to %<sizeof%> in %qD call is the same "
996 "pointer type %qT as the source; expected %qT "
997 "or an explicit length", callee
, TREE_TYPE (src
),
998 TREE_TYPE (TREE_TYPE (src
)));
1005 if (!TYPE_P (sizeof_arg
[idx
])
1006 && operand_equal_p (dest
, sizeof_arg
[idx
], 0)
1007 && comp_types (TREE_TYPE (dest
), type
))
1009 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
1010 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
1011 "argument to %<sizeof%> in %qD call is the same "
1012 "expression as the first source; did you mean to "
1013 "remove the addressof?", callee
);
1014 else if ((TYPE_PRECISION (TREE_TYPE (type
))
1015 == TYPE_PRECISION (char_type_node
))
1017 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
1018 "argument to %<sizeof%> in %qD call is the same "
1019 "expression as the first source; did you mean to "
1020 "provide an explicit length?", callee
);
1022 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
1023 "argument to %<sizeof%> in %qD call is the same "
1024 "expression as the first source; did you mean to "
1025 "dereference it?", callee
);
1029 if (POINTER_TYPE_P (TREE_TYPE (dest
))
1031 && comp_types (TREE_TYPE (dest
), type
)
1032 && !VOID_TYPE_P (TREE_TYPE (type
)))
1034 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
1035 "argument to %<sizeof%> in %qD call is the same "
1036 "pointer type %qT as the first source; expected %qT "
1037 "or an explicit length", callee
, TREE_TYPE (dest
),
1038 TREE_TYPE (TREE_TYPE (dest
)));
1045 if (!TYPE_P (sizeof_arg
[idx
])
1046 && operand_equal_p (src
, sizeof_arg
[idx
], 0)
1047 && comp_types (TREE_TYPE (src
), type
))
1049 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
1050 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
1051 "argument to %<sizeof%> in %qD call is the same "
1052 "expression as the second source; did you mean to "
1053 "remove the addressof?", callee
);
1054 else if ((TYPE_PRECISION (TREE_TYPE (type
))
1055 == TYPE_PRECISION (char_type_node
))
1057 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
1058 "argument to %<sizeof%> in %qD call is the same "
1059 "expression as the second source; did you mean to "
1060 "provide an explicit length?", callee
);
1062 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
1063 "argument to %<sizeof%> in %qD call is the same "
1064 "expression as the second source; did you mean to "
1065 "dereference it?", callee
);
1069 if (POINTER_TYPE_P (TREE_TYPE (src
))
1071 && comp_types (TREE_TYPE (src
), type
)
1072 && !VOID_TYPE_P (TREE_TYPE (type
)))
1074 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
1075 "argument to %<sizeof%> in %qD call is the same "
1076 "pointer type %qT as the second source; expected %qT "
1077 "or an explicit length", callee
, TREE_TYPE (src
),
1078 TREE_TYPE (TREE_TYPE (src
)));
1085 /* Warn for unlikely, improbable, or stupid DECL declarations
1089 check_main_parameter_types (tree decl
)
1091 function_args_iterator iter
;
1095 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl
), type
, iter
)
1097 /* XXX void_type_node belies the abstraction. */
1098 if (type
== void_type_node
|| type
== error_mark_node
)
1102 if (TYPE_ATOMIC (t
))
1103 pedwarn (input_location
, OPT_Wmain
,
1104 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1106 while (POINTER_TYPE_P (t
))
1109 if (TYPE_ATOMIC (t
))
1110 pedwarn (input_location
, OPT_Wmain
,
1111 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1119 if (TYPE_MAIN_VARIANT (type
) != integer_type_node
)
1120 pedwarn (input_location
, OPT_Wmain
,
1121 "first argument of %q+D should be %<int%>", decl
);
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 "second argument of %q+D should be %<char **%>", decl
);
1134 if (TREE_CODE (type
) != POINTER_TYPE
1135 || TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
1136 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type
)))
1138 pedwarn (input_location
, OPT_Wmain
,
1139 "third argument of %q+D should probably be "
1140 "%<char **%>", decl
);
1145 /* It is intentional that this message does not mention the third
1146 argument because it's only mentioned in an appendix of the
1148 if (argct
> 0 && (argct
< 2 || argct
> 3))
1149 pedwarn (input_location
, OPT_Wmain
,
1150 "%q+D takes only zero or two arguments", decl
);
1152 if (stdarg_p (TREE_TYPE (decl
)))
1153 pedwarn (input_location
, OPT_Wmain
,
1154 "%q+D declared as variadic function", decl
);
1157 /* Warns if the conversion of EXPR to TYPE may alter a value.
1158 This is a helper function for warnings_for_convert_and_check. */
1161 conversion_warning (location_t loc
, tree type
, tree expr
, tree result
)
1163 tree expr_type
= TREE_TYPE (expr
);
1164 enum conversion_safety conversion_kind
;
1166 if (!warn_conversion
&& !warn_sign_conversion
&& !warn_float_conversion
)
1169 /* This may happen, because for LHS op= RHS we preevaluate
1170 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
1171 means we could no longer see the code of the EXPR. */
1172 if (TREE_CODE (expr
) == C_MAYBE_CONST_EXPR
)
1173 expr
= C_MAYBE_CONST_EXPR_EXPR (expr
);
1174 if (TREE_CODE (expr
) == SAVE_EXPR
)
1175 expr
= TREE_OPERAND (expr
, 0);
1177 switch (TREE_CODE (expr
))
1185 case TRUTH_ANDIF_EXPR
:
1186 case TRUTH_ORIF_EXPR
:
1187 case TRUTH_AND_EXPR
:
1189 case TRUTH_XOR_EXPR
:
1190 case TRUTH_NOT_EXPR
:
1191 /* Conversion from boolean to a signed:1 bit-field (which only
1192 can hold the values 0 and -1) doesn't lose information - but
1193 it does change the value. */
1194 if (TYPE_PRECISION (type
) == 1 && !TYPE_UNSIGNED (type
))
1195 warning_at (loc
, OPT_Wconversion
,
1196 "conversion to %qT from boolean expression", type
);
1203 conversion_kind
= unsafe_conversion_p (loc
, type
, expr
, result
, true);
1205 if (conversion_kind
== UNSAFE_REAL
)
1206 warnopt
= OPT_Wfloat_conversion
;
1207 else if (conversion_kind
)
1208 warnopt
= OPT_Wconversion
;
1212 if (TREE_CODE_CLASS (TREE_CODE (result
)) == tcc_constant
)
1213 warning_at (loc
, warnopt
,
1214 "conversion from %qT to %qT changes value from %qE to %qE",
1215 expr_type
, type
, expr
, result
);
1217 warning_at (loc
, warnopt
,
1218 "conversion from %qT to %qT changes the value of %qE",
1219 expr_type
, type
, expr
);
1224 /* In case of COND_EXPR, we do not care about the type of
1225 COND_EXPR, only about the conversion of each operand. */
1226 tree op1
= TREE_OPERAND (expr
, 1);
1227 tree op2
= TREE_OPERAND (expr
, 2);
1229 conversion_warning (loc
, type
, op1
, result
);
1230 conversion_warning (loc
, type
, op2
, result
);
1234 default: /* 'expr' is not a constant. */
1235 conversion_kind
= unsafe_conversion_p (loc
, type
, expr
, result
, true);
1236 if (conversion_kind
== UNSAFE_IMAGINARY
)
1237 warning_at (loc
, OPT_Wconversion
,
1238 "conversion from %qT to %qT discards imaginary component",
1243 if (conversion_kind
== UNSAFE_REAL
)
1244 warnopt
= OPT_Wfloat_conversion
;
1245 else if (conversion_kind
)
1246 warnopt
= OPT_Wconversion
;
1249 warning_at (loc
, warnopt
,
1250 "conversion from %qT to %qT may change value",
1256 /* Produce warnings after a conversion. RESULT is the result of
1257 converting EXPR to TYPE. This is a helper function for
1258 convert_and_check and cp_convert_and_check. */
1261 warnings_for_convert_and_check (location_t loc
, tree type
, tree expr
,
1264 loc
= expansion_point_location_if_in_system_header (loc
);
1266 bool cst
= TREE_CODE_CLASS (TREE_CODE (result
)) == tcc_constant
;
1268 tree exprtype
= TREE_TYPE (expr
);
1270 if (TREE_CODE (expr
) == INTEGER_CST
1271 && (TREE_CODE (type
) == INTEGER_TYPE
1272 || TREE_CODE (type
) == ENUMERAL_TYPE
)
1273 && !int_fits_type_p (expr
, type
))
1275 /* Do not diagnose overflow in a constant expression merely
1276 because a conversion overflowed. */
1277 if (TREE_OVERFLOW (result
))
1278 TREE_OVERFLOW (result
) = TREE_OVERFLOW (expr
);
1280 if (TYPE_UNSIGNED (type
))
1282 /* This detects cases like converting -129 or 256 to
1284 if (!int_fits_type_p (expr
, c_common_signed_type (type
)))
1287 warning_at (loc
, OPT_Woverflow
,
1288 (TYPE_UNSIGNED (exprtype
)
1289 ? G_("conversion from %qT to %qT "
1290 "changes value from %qE to %qE")
1291 : G_("unsigned conversion from %qT to %qT "
1292 "changes value from %qE to %qE")),
1293 exprtype
, type
, expr
, result
);
1295 warning_at (loc
, OPT_Woverflow
,
1296 (TYPE_UNSIGNED (exprtype
)
1297 ? G_("conversion from %qT to %qT "
1298 "changes the value of %qE")
1299 : G_("unsigned conversion from %qT to %qT "
1300 "changes the value of %qE")),
1301 exprtype
, type
, expr
);
1304 conversion_warning (loc
, type
, expr
, result
);
1306 else if (!int_fits_type_p (expr
, c_common_unsigned_type (type
)))
1309 warning_at (loc
, OPT_Woverflow
,
1310 "overflow in conversion from %qT to %qT "
1311 "changes value from %qE to %qE",
1312 exprtype
, type
, expr
, result
);
1314 warning_at (loc
, OPT_Woverflow
,
1315 "overflow in conversion from %qT to %qT "
1316 "changes the value of %qE",
1317 exprtype
, type
, expr
);
1319 /* No warning for converting 0x80000000 to int. */
1321 && (TREE_CODE (exprtype
) != INTEGER_TYPE
1322 || TYPE_PRECISION (exprtype
)
1323 != TYPE_PRECISION (type
)))
1326 warning_at (loc
, OPT_Woverflow
,
1327 "overflow in conversion from %qT to %qT "
1328 "changes value from %qE to %qE",
1329 exprtype
, type
, expr
, result
);
1331 warning_at (loc
, OPT_Woverflow
,
1332 "overflow in conversion from %qT to %qT "
1333 "changes the value of %qE",
1334 exprtype
, type
, expr
);
1337 conversion_warning (loc
, type
, expr
, result
);
1339 else if ((TREE_CODE (result
) == INTEGER_CST
1340 || TREE_CODE (result
) == FIXED_CST
) && TREE_OVERFLOW (result
))
1343 warning_at (loc
, OPT_Woverflow
,
1344 "overflow in conversion from %qT to %qT "
1345 "changes value from %qE to %qE",
1346 exprtype
, type
, expr
, result
);
1348 warning_at (loc
, OPT_Woverflow
,
1349 "overflow in conversion from %qT to %qT "
1350 "changes the value of %qE",
1351 exprtype
, type
, expr
);
1354 conversion_warning (loc
, type
, expr
, result
);
1357 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
1358 Used to verify that case values match up with enumerator values. */
1361 match_case_to_enum_1 (tree key
, tree type
, tree label
)
1363 /* Avoid warning about enums that have no enumerators. */
1364 if (TYPE_VALUES (type
) == NULL_TREE
)
1367 char buf
[WIDE_INT_PRINT_BUFFER_SIZE
];
1369 if (tree_fits_uhwi_p (key
))
1370 print_dec (wi::to_wide (key
), buf
, UNSIGNED
);
1371 else if (tree_fits_shwi_p (key
))
1372 print_dec (wi::to_wide (key
), buf
, SIGNED
);
1374 print_hex (wi::to_wide (key
), buf
);
1376 if (TYPE_NAME (type
) == NULL_TREE
)
1377 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label
)),
1378 warn_switch
? OPT_Wswitch
: OPT_Wswitch_enum
,
1379 "case value %qs not in enumerated type",
1382 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label
)),
1383 warn_switch
? OPT_Wswitch
: OPT_Wswitch_enum
,
1384 "case value %qs not in enumerated type %qT",
1388 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
1389 Used to verify that case values match up with enumerator values. */
1392 match_case_to_enum (splay_tree_node node
, void *data
)
1394 tree label
= (tree
) node
->value
;
1395 tree type
= (tree
) data
;
1397 /* Skip default case. */
1398 if (!CASE_LOW (label
))
1401 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
1402 when we did our enum->case scan. Reset our scratch bit after. */
1403 if (!CASE_LOW_SEEN (label
))
1404 match_case_to_enum_1 (CASE_LOW (label
), type
, label
);
1406 CASE_LOW_SEEN (label
) = 0;
1408 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
1409 not set, that means that CASE_HIGH did not appear when we did our
1410 enum->case scan. Reset our scratch bit after. */
1411 if (CASE_HIGH (label
))
1413 if (!CASE_HIGH_SEEN (label
))
1414 match_case_to_enum_1 (CASE_HIGH (label
), type
, label
);
1416 CASE_HIGH_SEEN (label
) = 0;
1422 /* Handle -Wswitch*. Called from the front end after parsing the
1423 switch construct. */
1424 /* ??? Should probably be somewhere generic, since other languages
1425 besides C and C++ would want this. At the moment, however, C/C++
1426 are the only tree-ssa languages that support enumerations at all,
1427 so the point is moot. */
1430 c_do_switch_warnings (splay_tree cases
, location_t switch_location
,
1431 tree type
, tree cond
, bool bool_cond_p
)
1433 splay_tree_node default_node
;
1434 splay_tree_node node
;
1436 bool outside_range_p
= false;
1438 if (type
!= error_mark_node
1439 && type
!= TREE_TYPE (cond
)
1440 && INTEGRAL_TYPE_P (type
)
1441 && INTEGRAL_TYPE_P (TREE_TYPE (cond
))
1442 && (!tree_int_cst_equal (TYPE_MIN_VALUE (type
),
1443 TYPE_MIN_VALUE (TREE_TYPE (cond
)))
1444 || !tree_int_cst_equal (TYPE_MAX_VALUE (type
),
1445 TYPE_MAX_VALUE (TREE_TYPE (cond
)))))
1447 tree min_value
= TYPE_MIN_VALUE (type
);
1448 tree max_value
= TYPE_MAX_VALUE (type
);
1450 node
= splay_tree_predecessor (cases
, (splay_tree_key
) min_value
);
1451 if (node
&& node
->key
)
1453 outside_range_p
= true;
1454 /* There is at least one case smaller than TYPE's minimum value.
1455 NODE itself could be still a range overlapping the valid values,
1456 but any predecessors thereof except the default case will be
1457 completely outside of range. */
1458 if (CASE_HIGH ((tree
) node
->value
)
1459 && tree_int_cst_compare (CASE_HIGH ((tree
) node
->value
),
1462 location_t loc
= EXPR_LOCATION ((tree
) node
->value
);
1463 warning_at (loc
, 0, "lower value in case label range"
1464 " less than minimum value for type");
1465 CASE_LOW ((tree
) node
->value
) = convert (TREE_TYPE (cond
),
1467 node
->key
= (splay_tree_key
) CASE_LOW ((tree
) node
->value
);
1469 /* All the following ones are completely outside of range. */
1472 node
= splay_tree_predecessor (cases
,
1473 (splay_tree_key
) min_value
);
1474 if (node
== NULL
|| !node
->key
)
1476 location_t loc
= EXPR_LOCATION ((tree
) node
->value
);
1477 warning_at (loc
, 0, "case label value is less than minimum "
1479 splay_tree_remove (cases
, node
->key
);
1483 node
= splay_tree_lookup (cases
, (splay_tree_key
) max_value
);
1485 node
= splay_tree_predecessor (cases
, (splay_tree_key
) max_value
);
1486 /* Handle a single node that might partially overlap the range. */
1489 && CASE_HIGH ((tree
) node
->value
)
1490 && tree_int_cst_compare (CASE_HIGH ((tree
) node
->value
),
1493 location_t loc
= EXPR_LOCATION ((tree
) node
->value
);
1494 warning_at (loc
, 0, "upper value in case label range"
1495 " exceeds maximum value for type");
1496 CASE_HIGH ((tree
) node
->value
)
1497 = convert (TREE_TYPE (cond
), max_value
);
1498 outside_range_p
= true;
1500 /* And any nodes that are completely outside of the range. */
1501 while ((node
= splay_tree_successor (cases
,
1502 (splay_tree_key
) max_value
))
1505 location_t loc
= EXPR_LOCATION ((tree
) node
->value
);
1507 "case label value exceeds maximum value for type");
1508 splay_tree_remove (cases
, node
->key
);
1509 outside_range_p
= true;
1513 if (!warn_switch
&& !warn_switch_enum
&& !warn_switch_default
1514 && !warn_switch_bool
)
1517 default_node
= splay_tree_lookup (cases
, (splay_tree_key
) NULL
);
1519 warning_at (switch_location
, OPT_Wswitch_default
,
1520 "switch missing default case");
1522 /* There are certain cases where -Wswitch-bool warnings aren't
1529 so be careful here. */
1530 if (warn_switch_bool
&& bool_cond_p
)
1532 splay_tree_node min_node
;
1533 /* If there's a default node, it's also the value with the minimal
1534 key. So look at the penultimate key (if any). */
1536 min_node
= splay_tree_successor (cases
, (splay_tree_key
) NULL
);
1538 min_node
= splay_tree_min (cases
);
1539 tree min
= min_node
? (tree
) min_node
->key
: NULL_TREE
;
1541 splay_tree_node max_node
= splay_tree_max (cases
);
1542 /* This might be a case range, so look at the value with the
1543 maximal key and then check CASE_HIGH. */
1544 tree max
= max_node
? (tree
) max_node
->value
: NULL_TREE
;
1546 max
= CASE_HIGH (max
) ? CASE_HIGH (max
) : CASE_LOW (max
);
1548 /* If there's a case value > 1 or < 0, that is outside bool
1551 || (max
&& wi::gts_p (wi::to_wide (max
), 1))
1552 || (min
&& wi::lts_p (wi::to_wide (min
), 0))
1560 case, where we want to warn. */
1562 && max
&& wi::to_wide (max
) == 1
1563 && min
&& wi::to_wide (min
) == 0))
1564 warning_at (switch_location
, OPT_Wswitch_bool
,
1565 "switch condition has boolean value");
1568 /* From here on, we only care about enumerated types. */
1569 if (!type
|| TREE_CODE (type
) != ENUMERAL_TYPE
)
1572 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
1573 if (!warn_switch_enum
&& !warn_switch
)
1576 /* Check the cases. Warn about case values which are not members of
1577 the enumerated type. For -Wswitch-enum, or for -Wswitch when
1578 there is no default case, check that exactly all enumeration
1579 literals are covered by the cases. */
1581 /* Clearing COND if it is not an integer constant simplifies
1582 the tests inside the loop below. */
1583 if (TREE_CODE (cond
) != INTEGER_CST
)
1586 /* The time complexity here is O(N*lg(N)) worst case, but for the
1587 common case of monotonically increasing enumerators, it is
1588 O(N), since the nature of the splay tree will keep the next
1589 element adjacent to the root at all times. */
1591 for (chain
= TYPE_VALUES (type
); chain
; chain
= TREE_CHAIN (chain
))
1593 tree value
= TREE_VALUE (chain
);
1594 if (TREE_CODE (value
) == CONST_DECL
)
1595 value
= DECL_INITIAL (value
);
1596 node
= splay_tree_lookup (cases
, (splay_tree_key
) value
);
1599 /* Mark the CASE_LOW part of the case entry as seen. */
1600 tree label
= (tree
) node
->value
;
1601 CASE_LOW_SEEN (label
) = 1;
1605 /* Even though there wasn't an exact match, there might be a
1606 case range which includes the enumerator's value. */
1607 node
= splay_tree_predecessor (cases
, (splay_tree_key
) value
);
1608 if (node
&& CASE_HIGH ((tree
) node
->value
))
1610 tree label
= (tree
) node
->value
;
1611 int cmp
= tree_int_cst_compare (CASE_HIGH (label
), value
);
1614 /* If we match the upper bound exactly, mark the CASE_HIGH
1615 part of the case entry as seen. */
1617 CASE_HIGH_SEEN (label
) = 1;
1622 /* We've now determined that this enumerated literal isn't
1623 handled by the case labels of the switch statement. */
1625 /* If the switch expression is a constant, we only really care
1626 about whether that constant is handled by the switch. */
1627 if (cond
&& tree_int_cst_compare (cond
, value
))
1630 /* If there is a default_node, the only relevant option is
1631 Wswitch-enum. Otherwise, if both are enabled then we prefer
1632 to warn using -Wswitch because -Wswitch is enabled by -Wall
1633 while -Wswitch-enum is explicit. */
1634 warning_at (switch_location
,
1635 (default_node
|| !warn_switch
1638 "enumeration value %qE not handled in switch",
1639 TREE_PURPOSE (chain
));
1642 /* Warn if there are case expressions that don't correspond to
1643 enumerators. This can occur since C and C++ don't enforce
1644 type-checking of assignments to enumeration variables.
1646 The time complexity here is now always O(N) worst case, since
1647 we should have marked both the lower bound and upper bound of
1648 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
1649 above. This scan also resets those fields. */
1651 splay_tree_foreach (cases
, match_case_to_enum
, type
);
1654 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
1655 expression, because B will always be true. */
1658 warn_for_omitted_condop (location_t location
, tree cond
)
1660 /* In C++ template declarations it can happen that the type is dependent
1661 and not yet known, thus TREE_TYPE (cond) == NULL_TREE. */
1662 if (truth_value_p (TREE_CODE (cond
))
1663 || (TREE_TYPE (cond
) != NULL_TREE
1664 && TREE_CODE (TREE_TYPE (cond
)) == BOOLEAN_TYPE
))
1665 warning_at (location
, OPT_Wparentheses
,
1666 "the omitted middle operand in ?: will always be %<true%>, "
1667 "suggest explicit middle operand");
1670 /* Give an error for storing into ARG, which is 'const'. USE indicates
1671 how ARG was being used. */
1674 readonly_error (location_t loc
, tree arg
, enum lvalue_use use
)
1676 gcc_assert (use
== lv_assign
|| use
== lv_increment
|| use
== lv_decrement
1678 STRIP_ANY_LOCATION_WRAPPER (arg
);
1679 /* Using this macro rather than (for example) arrays of messages
1680 ensures that all the format strings are checked at compile
1682 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
1683 : (use == lv_increment ? (I) \
1684 : (use == lv_decrement ? (D) : (AS))))
1685 if (TREE_CODE (arg
) == COMPONENT_REF
)
1687 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg
, 0))))
1688 error_at (loc
, READONLY_MSG (G_("assignment of member "
1689 "%qD in read-only object"),
1690 G_("increment of member "
1691 "%qD in read-only object"),
1692 G_("decrement of member "
1693 "%qD in read-only object"),
1694 G_("member %qD in read-only object "
1695 "used as %<asm%> output")),
1696 TREE_OPERAND (arg
, 1));
1698 error_at (loc
, READONLY_MSG (G_("assignment of read-only member %qD"),
1699 G_("increment of read-only member %qD"),
1700 G_("decrement of read-only member %qD"),
1701 G_("read-only member %qD used as %<asm%> output")),
1702 TREE_OPERAND (arg
, 1));
1704 else if (VAR_P (arg
))
1705 error_at (loc
, READONLY_MSG (G_("assignment of read-only variable %qD"),
1706 G_("increment of read-only variable %qD"),
1707 G_("decrement of read-only variable %qD"),
1708 G_("read-only variable %qD used as %<asm%> output")),
1710 else if (TREE_CODE (arg
) == PARM_DECL
)
1711 error_at (loc
, READONLY_MSG (G_("assignment of read-only parameter %qD"),
1712 G_("increment of read-only parameter %qD"),
1713 G_("decrement of read-only parameter %qD"),
1714 G_("read-only parameter %qD use as %<asm%> output")),
1716 else if (TREE_CODE (arg
) == RESULT_DECL
)
1718 gcc_assert (c_dialect_cxx ());
1719 error_at (loc
, READONLY_MSG (G_("assignment of "
1720 "read-only named return value %qD"),
1722 "read-only named return value %qD"),
1724 "read-only named return value %qD"),
1725 G_("read-only named return value %qD "
1726 "used as %<asm%>output")),
1729 else if (TREE_CODE (arg
) == FUNCTION_DECL
)
1730 error_at (loc
, READONLY_MSG (G_("assignment of function %qD"),
1731 G_("increment of function %qD"),
1732 G_("decrement of function %qD"),
1733 G_("function %qD used as %<asm%> output")),
1736 error_at (loc
, READONLY_MSG (G_("assignment of read-only location %qE"),
1737 G_("increment of read-only location %qE"),
1738 G_("decrement of read-only location %qE"),
1739 G_("read-only location %qE used as %<asm%> output")),
1743 /* Print an error message for an invalid lvalue. USE says
1744 how the lvalue is being used and so selects the error message. LOC
1745 is the location for the error. */
1748 lvalue_error (location_t loc
, enum lvalue_use use
)
1753 error_at (loc
, "lvalue required as left operand of assignment");
1756 error_at (loc
, "lvalue required as increment operand");
1759 error_at (loc
, "lvalue required as decrement operand");
1762 error_at (loc
, "lvalue required as unary %<&%> operand");
1765 error_at (loc
, "lvalue required in asm statement");
1772 /* Print an error message for an invalid indirection of type TYPE.
1773 ERRSTRING is the name of the operator for the indirection. */
1776 invalid_indirection_error (location_t loc
, tree type
, ref_operator errstring
)
1781 gcc_assert (c_dialect_cxx ());
1782 error_at (loc
, "invalid type argument (have %qT)", type
);
1784 case RO_ARRAY_INDEXING
:
1786 "invalid type argument of array indexing (have %qT)",
1791 "invalid type argument of unary %<*%> (have %qT)",
1796 "invalid type argument of %<->%> (have %qT)",
1801 "invalid type argument of %<->*%> (have %qT)",
1804 case RO_IMPLICIT_CONVERSION
:
1806 "invalid type argument of implicit conversion (have %qT)",
1814 /* Subscripting with type char is likely to lose on a machine where
1815 chars are signed. So warn on any machine, but optionally. Don't
1816 warn for unsigned char since that type is safe. Don't warn for
1817 signed char because anyone who uses that must have done so
1818 deliberately. Furthermore, we reduce the false positive load by
1819 warning only for non-constant value of type char.
1820 LOC is the location of the subscripting expression. */
1823 warn_array_subscript_with_type_char (location_t loc
, tree index
)
1825 if (TYPE_MAIN_VARIANT (TREE_TYPE (index
)) == char_type_node
)
1827 /* If INDEX has a location, use it; otherwise use LOC (the location
1828 of the subscripting expression as a whole). */
1829 loc
= EXPR_LOC_OR_LOC (index
, loc
);
1830 STRIP_ANY_LOCATION_WRAPPER (index
);
1831 if (TREE_CODE (index
) != INTEGER_CST
)
1832 warning_at (loc
, OPT_Wchar_subscripts
,
1833 "array subscript has type %<char%>");
1837 /* Implement -Wparentheses for the unexpected C precedence rules, to
1838 cover cases like x + y << z which readers are likely to
1839 misinterpret. We have seen an expression in which CODE is a binary
1840 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
1841 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
1842 CODE_RIGHT may be ERROR_MARK, which means that that side of the
1843 expression was not formed using a binary or unary operator, or it
1844 was enclosed in parentheses. */
1847 warn_about_parentheses (location_t loc
, enum tree_code code
,
1848 enum tree_code code_left
, tree arg_left
,
1849 enum tree_code code_right
, tree arg_right
)
1851 if (!warn_parentheses
)
1854 /* This macro tests that the expression ARG with original tree code
1855 CODE appears to be a boolean expression. or the result of folding a
1856 boolean expression. */
1857 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
1858 (truth_value_p (TREE_CODE (ARG)) \
1859 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
1860 /* Folding may create 0 or 1 integers from other expressions. */ \
1861 || ((CODE) != INTEGER_CST \
1862 && (integer_onep (ARG) || integer_zerop (ARG))))
1867 if (code_left
== PLUS_EXPR
)
1868 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1869 "suggest parentheses around %<+%> inside %<<<%>");
1870 else if (code_right
== PLUS_EXPR
)
1871 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1872 "suggest parentheses around %<+%> inside %<<<%>");
1873 else if (code_left
== MINUS_EXPR
)
1874 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1875 "suggest parentheses around %<-%> inside %<<<%>");
1876 else if (code_right
== MINUS_EXPR
)
1877 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1878 "suggest parentheses around %<-%> inside %<<<%>");
1882 if (code_left
== PLUS_EXPR
)
1883 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1884 "suggest parentheses around %<+%> inside %<>>%>");
1885 else if (code_right
== PLUS_EXPR
)
1886 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1887 "suggest parentheses around %<+%> inside %<>>%>");
1888 else if (code_left
== MINUS_EXPR
)
1889 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1890 "suggest parentheses around %<-%> inside %<>>%>");
1891 else if (code_right
== MINUS_EXPR
)
1892 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1893 "suggest parentheses around %<-%> inside %<>>%>");
1896 case TRUTH_ORIF_EXPR
:
1897 if (code_left
== TRUTH_ANDIF_EXPR
)
1898 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1899 "suggest parentheses around %<&&%> within %<||%>");
1900 else if (code_right
== TRUTH_ANDIF_EXPR
)
1901 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1902 "suggest parentheses around %<&&%> within %<||%>");
1906 if (code_left
== BIT_AND_EXPR
|| code_left
== BIT_XOR_EXPR
1907 || code_left
== PLUS_EXPR
|| code_left
== MINUS_EXPR
)
1908 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1909 "suggest parentheses around arithmetic in operand of %<|%>");
1910 else if (code_right
== BIT_AND_EXPR
|| code_right
== BIT_XOR_EXPR
1911 || code_right
== PLUS_EXPR
|| code_right
== MINUS_EXPR
)
1912 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1913 "suggest parentheses around arithmetic in operand of %<|%>");
1914 /* Check cases like x|y==z */
1915 else if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1916 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1917 "suggest parentheses around comparison in operand of %<|%>");
1918 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1919 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1920 "suggest parentheses around comparison in operand of %<|%>");
1921 /* Check cases like !x | y */
1922 else if (code_left
== TRUTH_NOT_EXPR
1923 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right
, arg_right
))
1924 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1925 "suggest parentheses around operand of "
1926 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
1930 if (code_left
== BIT_AND_EXPR
1931 || code_left
== PLUS_EXPR
|| code_left
== MINUS_EXPR
)
1932 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1933 "suggest parentheses around arithmetic in operand of %<^%>");
1934 else if (code_right
== BIT_AND_EXPR
1935 || code_right
== PLUS_EXPR
|| code_right
== MINUS_EXPR
)
1936 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1937 "suggest parentheses around arithmetic in operand of %<^%>");
1938 /* Check cases like x^y==z */
1939 else if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1940 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1941 "suggest parentheses around comparison in operand of %<^%>");
1942 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1943 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1944 "suggest parentheses around comparison in operand of %<^%>");
1948 if (code_left
== PLUS_EXPR
)
1949 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1950 "suggest parentheses around %<+%> in operand of %<&%>");
1951 else if (code_right
== PLUS_EXPR
)
1952 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1953 "suggest parentheses around %<+%> in operand of %<&%>");
1954 else if (code_left
== MINUS_EXPR
)
1955 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1956 "suggest parentheses around %<-%> in operand of %<&%>");
1957 else if (code_right
== MINUS_EXPR
)
1958 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1959 "suggest parentheses around %<-%> in operand of %<&%>");
1960 /* Check cases like x&y==z */
1961 else if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1962 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1963 "suggest parentheses around comparison in operand of %<&%>");
1964 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1965 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1966 "suggest parentheses around comparison in operand of %<&%>");
1967 /* Check cases like !x & y */
1968 else if (code_left
== TRUTH_NOT_EXPR
1969 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right
, arg_right
))
1970 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1971 "suggest parentheses around operand of "
1972 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
1976 if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1977 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1978 "suggest parentheses around comparison in operand of %<==%>");
1979 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1980 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1981 "suggest parentheses around comparison in operand of %<==%>");
1984 if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1985 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1986 "suggest parentheses around comparison in operand of %<!=%>");
1987 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1988 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1989 "suggest parentheses around comparison in operand of %<!=%>");
1993 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
1995 if (TREE_CODE_CLASS (code_left
) == tcc_comparison
1996 && code_left
!= NE_EXPR
&& code_left
!= EQ_EXPR
1997 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left
)))
1998 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1999 "comparisons like %<X<=Y<=Z%> do not "
2000 "have their mathematical meaning");
2001 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
2002 && code_right
!= NE_EXPR
&& code_right
!= EQ_EXPR
2003 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right
)))
2004 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
2005 "comparisons like %<X<=Y<=Z%> do not "
2006 "have their mathematical meaning");
2010 #undef NOT_A_BOOLEAN_EXPR_P
2013 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
2016 warn_for_unused_label (tree label
)
2018 if (!TREE_USED (label
))
2020 if (DECL_INITIAL (label
))
2021 warning (OPT_Wunused_label
, "label %q+D defined but not used", label
);
2023 warning (OPT_Wunused_label
, "label %q+D declared but not defined", label
);
2025 else if (asan_sanitize_use_after_scope ())
2027 if (asan_used_labels
== NULL
)
2028 asan_used_labels
= new hash_set
<tree
> (16);
2030 asan_used_labels
->add (label
);
2034 /* Warn for division by zero according to the value of DIVISOR. LOC
2035 is the location of the division operator. */
2038 warn_for_div_by_zero (location_t loc
, tree divisor
)
2040 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
2041 about division by zero. Do not issue a warning if DIVISOR has a
2042 floating-point type, since we consider 0.0/0.0 a valid way of
2043 generating a NaN. */
2044 if (c_inhibit_evaluation_warnings
== 0
2045 && (integer_zerop (divisor
) || fixed_zerop (divisor
)))
2046 warning_at (loc
, OPT_Wdiv_by_zero
, "division by zero");
2049 /* Warn for patterns where memset appears to be used incorrectly. The
2050 warning location should be LOC. ARG0, and ARG2 are the first and
2051 last arguments to the call, while LITERAL_ZERO_MASK has a 1 bit for
2052 each argument that was a literal zero. */
2055 warn_for_memset (location_t loc
, tree arg0
, tree arg2
,
2056 int literal_zero_mask
)
2058 arg0
= fold_for_warn (arg0
);
2059 arg2
= fold_for_warn (arg2
);
2061 if (warn_memset_transposed_args
2062 && integer_zerop (arg2
)
2063 && (literal_zero_mask
& (1 << 2)) != 0
2064 && (literal_zero_mask
& (1 << 1)) == 0)
2065 warning_at (loc
, OPT_Wmemset_transposed_args
,
2066 "%<memset%> used with constant zero length "
2067 "parameter; this could be due to transposed "
2070 if (warn_memset_elt_size
&& TREE_CODE (arg2
) == INTEGER_CST
)
2073 if (TREE_CODE (arg0
) == ADDR_EXPR
)
2074 arg0
= TREE_OPERAND (arg0
, 0);
2075 tree type
= TREE_TYPE (arg0
);
2076 if (type
!= NULL_TREE
&& TREE_CODE (type
) == ARRAY_TYPE
)
2078 tree elt_type
= TREE_TYPE (type
);
2079 tree domain
= TYPE_DOMAIN (type
);
2080 if (COMPLETE_TYPE_P (elt_type
)
2081 && !integer_onep (TYPE_SIZE_UNIT (elt_type
))
2082 && domain
!= NULL_TREE
2083 && TYPE_MAX_VALUE (domain
)
2084 && TYPE_MIN_VALUE (domain
)
2085 && integer_zerop (TYPE_MIN_VALUE (domain
))
2086 && integer_onep (fold_build2 (MINUS_EXPR
, domain
,
2088 TYPE_MAX_VALUE (domain
))))
2089 warning_at (loc
, OPT_Wmemset_elt_size
,
2090 "%<memset%> used with length equal to "
2091 "number of elements without multiplication "
2097 /* Subroutine of build_binary_op. Give warnings for comparisons
2098 between signed and unsigned quantities that may fail. Do the
2099 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
2100 so that casts will be considered, but default promotions won't
2103 LOCATION is the location of the comparison operator.
2105 The arguments of this function map directly to local variables
2106 of build_binary_op. */
2109 warn_for_sign_compare (location_t location
,
2110 tree orig_op0
, tree orig_op1
,
2112 tree result_type
, enum tree_code resultcode
)
2114 if (error_operand_p (orig_op0
) || error_operand_p (orig_op1
))
2117 int op0_signed
= !TYPE_UNSIGNED (TREE_TYPE (orig_op0
));
2118 int op1_signed
= !TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
2119 int unsignedp0
, unsignedp1
;
2121 /* In C++, check for comparison of different enum types. */
2123 && TREE_CODE (TREE_TYPE (orig_op0
)) == ENUMERAL_TYPE
2124 && TREE_CODE (TREE_TYPE (orig_op1
)) == ENUMERAL_TYPE
2125 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0
))
2126 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1
)))
2128 warning_at (location
,
2129 OPT_Wsign_compare
, "comparison between types %qT and %qT",
2130 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
));
2133 /* Do not warn if the comparison is being done in a signed type,
2134 since the signed type will only be chosen if it can represent
2135 all the values of the unsigned type. */
2136 if (!TYPE_UNSIGNED (result_type
))
2138 /* Do not warn if both operands are unsigned. */
2139 else if (op0_signed
== op1_signed
)
2143 tree sop
, uop
, base_type
;
2147 sop
= orig_op0
, uop
= orig_op1
;
2149 sop
= orig_op1
, uop
= orig_op0
;
2151 sop
= fold_for_warn (sop
);
2152 uop
= fold_for_warn (uop
);
2154 STRIP_TYPE_NOPS (sop
);
2155 STRIP_TYPE_NOPS (uop
);
2156 base_type
= (TREE_CODE (result_type
) == COMPLEX_TYPE
2157 ? TREE_TYPE (result_type
) : result_type
);
2159 /* Do not warn if the signed quantity is an unsuffixed integer
2160 literal (or some static constant expression involving such
2161 literals or a conditional expression involving such literals)
2162 and it is non-negative. */
2163 if (tree_expr_nonnegative_warnv_p (sop
, &ovf
))
2165 /* Do not warn if the comparison is an equality operation, the
2166 unsigned quantity is an integral constant, and it would fit
2167 in the result if the result were signed. */
2168 else if (TREE_CODE (uop
) == INTEGER_CST
2169 && (resultcode
== EQ_EXPR
|| resultcode
== NE_EXPR
)
2170 && int_fits_type_p (uop
, c_common_signed_type (base_type
)))
2172 /* In C, do not warn if the unsigned quantity is an enumeration
2173 constant and its maximum value would fit in the result if the
2174 result were signed. */
2175 else if (!c_dialect_cxx() && TREE_CODE (uop
) == INTEGER_CST
2176 && TREE_CODE (TREE_TYPE (uop
)) == ENUMERAL_TYPE
2177 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop
)),
2178 c_common_signed_type (base_type
)))
2181 warning_at (location
, OPT_Wsign_compare
,
2182 "comparison of integer expressions of different "
2183 "signedness: %qT and %qT", TREE_TYPE (orig_op0
),
2184 TREE_TYPE (orig_op1
));
2187 /* Warn if two unsigned values are being compared in a size larger
2188 than their original size, and one (and only one) is the result of
2189 a `~' operator. This comparison will always fail.
2191 Also warn if one operand is a constant, and the constant does not
2192 have all bits set that are set in the ~ operand when it is
2195 op0
= c_common_get_narrower (op0
, &unsignedp0
);
2196 op1
= c_common_get_narrower (op1
, &unsignedp1
);
2198 if ((TREE_CODE (op0
) == BIT_NOT_EXPR
)
2199 ^ (TREE_CODE (op1
) == BIT_NOT_EXPR
))
2201 if (TREE_CODE (op0
) == BIT_NOT_EXPR
)
2202 op0
= c_common_get_narrower (TREE_OPERAND (op0
, 0), &unsignedp0
);
2203 if (TREE_CODE (op1
) == BIT_NOT_EXPR
)
2204 op1
= c_common_get_narrower (TREE_OPERAND (op1
, 0), &unsignedp1
);
2206 if (tree_fits_shwi_p (op0
) || tree_fits_shwi_p (op1
))
2209 HOST_WIDE_INT constant
, mask
;
2213 if (tree_fits_shwi_p (op0
))
2216 unsignedp
= unsignedp1
;
2217 constant
= tree_to_shwi (op0
);
2222 unsignedp
= unsignedp0
;
2223 constant
= tree_to_shwi (op1
);
2226 bits
= TYPE_PRECISION (TREE_TYPE (primop
));
2227 if (bits
< TYPE_PRECISION (result_type
)
2228 && bits
< HOST_BITS_PER_LONG
&& unsignedp
)
2230 mask
= HOST_WIDE_INT_M1U
<< bits
;
2231 if ((mask
& constant
) != mask
)
2234 warning_at (location
, OPT_Wsign_compare
,
2235 "promoted ~unsigned is always non-zero");
2237 warning_at (location
, OPT_Wsign_compare
,
2238 "comparison of promoted ~unsigned with constant");
2242 else if (unsignedp0
&& unsignedp1
2243 && (TYPE_PRECISION (TREE_TYPE (op0
))
2244 < TYPE_PRECISION (result_type
))
2245 && (TYPE_PRECISION (TREE_TYPE (op1
))
2246 < TYPE_PRECISION (result_type
)))
2247 warning_at (location
, OPT_Wsign_compare
,
2248 "comparison of promoted ~unsigned with unsigned");
2252 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
2253 type via c_common_type. If -Wdouble-promotion is in use, and the
2254 conditions for warning have been met, issue a warning. GMSGID is
2255 the warning message. It must have two %T specifiers for the type
2256 that was converted (generally "float") and the type to which it was
2257 converted (generally "double), respectively. LOC is the location
2258 to which the warning should refer. */
2261 do_warn_double_promotion (tree result_type
, tree type1
, tree type2
,
2262 const char *gmsgid
, location_t loc
)
2266 if (!warn_double_promotion
)
2268 /* If the conversion will not occur at run-time, there is no need to
2270 if (c_inhibit_evaluation_warnings
)
2272 /* If an invalid conversion has occured, don't warn. */
2273 if (result_type
== error_mark_node
)
2275 if (TYPE_MAIN_VARIANT (result_type
) != double_type_node
2276 && TYPE_MAIN_VARIANT (result_type
) != complex_double_type_node
)
2278 if (TYPE_MAIN_VARIANT (type1
) == float_type_node
2279 || TYPE_MAIN_VARIANT (type1
) == complex_float_type_node
)
2280 source_type
= type1
;
2281 else if (TYPE_MAIN_VARIANT (type2
) == float_type_node
2282 || TYPE_MAIN_VARIANT (type2
) == complex_float_type_node
)
2283 source_type
= type2
;
2286 warning_at (loc
, OPT_Wdouble_promotion
, gmsgid
, source_type
, result_type
);
2289 /* Possibly warn about unused parameters. */
2292 do_warn_unused_parameter (tree fn
)
2296 for (decl
= DECL_ARGUMENTS (fn
);
2297 decl
; decl
= DECL_CHAIN (decl
))
2298 if (!TREE_USED (decl
) && TREE_CODE (decl
) == PARM_DECL
2299 && DECL_NAME (decl
) && !DECL_ARTIFICIAL (decl
)
2300 && !TREE_NO_WARNING (decl
))
2301 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wunused_parameter
,
2302 "unused parameter %qD", decl
);
2305 /* If DECL is a typedef that is declared in the current function,
2306 record it for the purpose of -Wunused-local-typedefs. */
2309 record_locally_defined_typedef (tree decl
)
2311 struct c_language_function
*l
;
2313 if (!warn_unused_local_typedefs
2315 /* if this is not a locally defined typedef then we are not
2317 || !is_typedef_decl (decl
)
2318 || !decl_function_context (decl
))
2321 l
= (struct c_language_function
*) cfun
->language
;
2322 vec_safe_push (l
->local_typedefs
, decl
);
2325 /* If T is a TYPE_DECL declared locally, mark it as used. */
2328 maybe_record_typedef_use (tree t
)
2330 if (!is_typedef_decl (t
))
2333 TREE_USED (t
) = true;
2336 /* Warn if there are some unused locally defined typedefs in the
2337 current function. */
2340 maybe_warn_unused_local_typedefs (void)
2344 /* The number of times we have emitted -Wunused-local-typedefs
2345 warnings. If this is different from errorcount, that means some
2346 unrelated errors have been issued. In which case, we'll avoid
2347 emitting "unused-local-typedefs" warnings. */
2348 static int unused_local_typedefs_warn_count
;
2349 struct c_language_function
*l
;
2354 if ((l
= (struct c_language_function
*) cfun
->language
) == NULL
)
2357 if (warn_unused_local_typedefs
2358 && errorcount
== unused_local_typedefs_warn_count
)
2360 FOR_EACH_VEC_SAFE_ELT (l
->local_typedefs
, i
, decl
)
2361 if (!TREE_USED (decl
))
2362 warning_at (DECL_SOURCE_LOCATION (decl
),
2363 OPT_Wunused_local_typedefs
,
2364 "typedef %qD locally defined but not used", decl
);
2365 unused_local_typedefs_warn_count
= errorcount
;
2368 vec_free (l
->local_typedefs
);
2371 /* If we're creating an if-else-if condition chain, first see if we
2372 already have this COND in the CHAIN. If so, warn and don't add COND
2373 into the vector, otherwise add the COND there. LOC is the location
2377 warn_duplicated_cond_add_or_warn (location_t loc
, tree cond
, vec
<tree
> **chain
)
2379 /* No chain has been created yet. Do nothing. */
2383 if (TREE_SIDE_EFFECTS (cond
))
2385 /* Uh-oh! This condition has a side-effect, thus invalidates
2395 FOR_EACH_VEC_ELT (**chain
, ix
, t
)
2396 if (operand_equal_p (cond
, t
, 0))
2398 auto_diagnostic_group d
;
2399 if (warning_at (loc
, OPT_Wduplicated_cond
,
2400 "duplicated %<if%> condition"))
2401 inform (EXPR_LOCATION (t
), "previously used here");
2407 && !CONSTANT_CLASS_P (cond
)
2408 /* Don't infinitely grow the chain. */
2409 && (*chain
)->length () < 512)
2410 (*chain
)->safe_push (cond
);
2413 /* Check and possibly warn if two declarations have contradictory
2414 attributes, such as always_inline vs. noinline. */
2417 diagnose_mismatched_attributes (tree olddecl
, tree newdecl
)
2419 bool warned
= false;
2421 tree a1
= lookup_attribute ("optimize", DECL_ATTRIBUTES (olddecl
));
2422 tree a2
= lookup_attribute ("optimize", DECL_ATTRIBUTES (newdecl
));
2423 /* An optimization attribute applied on a declaration after the
2424 definition is likely not what the user wanted. */
2426 && DECL_SAVED_TREE (olddecl
) != NULL_TREE
2427 && (a1
== NULL_TREE
|| !attribute_list_equal (a1
, a2
)))
2428 warned
|= warning (OPT_Wattributes
,
2429 "optimization attribute on %qD follows "
2430 "definition but the attribute doesn%'t match",
2433 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2434 if (DECL_DECLARED_INLINE_P (newdecl
)
2435 && DECL_UNINLINABLE (olddecl
)
2436 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl
)))
2437 warned
|= warning (OPT_Wattributes
, "inline declaration of %qD follows "
2438 "declaration with attribute %<noinline%>", newdecl
);
2439 else if (DECL_DECLARED_INLINE_P (olddecl
)
2440 && DECL_UNINLINABLE (newdecl
)
2441 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl
)))
2442 warned
|= warning (OPT_Wattributes
, "declaration of %q+D with attribute "
2443 "%<noinline%> follows inline declaration", newdecl
);
2448 /* Warn if signed left shift overflows. We don't warn
2449 about left-shifting 1 into the sign bit in C++14; cf.
2450 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
2451 and don't warn for C++2a at all, as signed left shifts never
2453 LOC is a location of the shift; OP0 and OP1 are the operands.
2454 Return true if an overflow is detected, false otherwise. */
2457 maybe_warn_shift_overflow (location_t loc
, tree op0
, tree op1
)
2459 if (TREE_CODE (op0
) != INTEGER_CST
2460 || TREE_CODE (op1
) != INTEGER_CST
)
2463 tree type0
= TREE_TYPE (op0
);
2464 unsigned int prec0
= TYPE_PRECISION (type0
);
2466 /* Left-hand operand must be signed. */
2467 if (TYPE_UNSIGNED (type0
) || cxx_dialect
>= cxx2a
)
2470 unsigned int min_prec
= (wi::min_precision (wi::to_wide (op0
), SIGNED
)
2471 + TREE_INT_CST_LOW (op1
));
2472 /* Handle the case of left-shifting 1 into the sign bit.
2473 * However, shifting 1 _out_ of the sign bit, as in
2474 * INT_MIN << 1, is considered an overflow.
2476 if (!tree_int_cst_sign_bit (op0
) && min_prec
== prec0
+ 1)
2478 /* Never warn for C++14 onwards. */
2479 if (cxx_dialect
>= cxx14
)
2481 /* Otherwise only if -Wshift-overflow=2. But return
2482 true to signal an overflow for the sake of integer
2483 constant expressions. */
2484 if (warn_shift_overflow
< 2)
2488 bool overflowed
= min_prec
> prec0
;
2489 if (overflowed
&& c_inhibit_evaluation_warnings
== 0)
2490 warning_at (loc
, OPT_Wshift_overflow_
,
2491 "result of %qE requires %u bits to represent, "
2492 "but %qT only has %u bits",
2493 build2_loc (loc
, LSHIFT_EXPR
, type0
, op0
, op1
),
2494 min_prec
, type0
, prec0
);
2499 /* Warn about boolean expression compared with an integer value different
2500 from true/false. Warns also e.g. about "(i1 == i2) == 2".
2501 LOC is the location of the comparison, CODE is its code, OP0 and OP1
2502 are the operands of the comparison. The caller must ensure that
2503 either operand is a boolean expression. */
2506 maybe_warn_bool_compare (location_t loc
, enum tree_code code
, tree op0
,
2509 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
2513 if (f
= fold_for_warn (op0
),
2514 TREE_CODE (f
) == INTEGER_CST
)
2516 else if (f
= fold_for_warn (op1
),
2517 TREE_CODE (f
) == INTEGER_CST
)
2522 if (!integer_zerop (cst
) && !integer_onep (cst
))
2524 int sign
= (TREE_CODE (op0
) == INTEGER_CST
2525 ? tree_int_cst_sgn (cst
) : -tree_int_cst_sgn (cst
));
2527 || ((code
== GT_EXPR
|| code
== GE_EXPR
) && sign
< 0)
2528 || ((code
== LT_EXPR
|| code
== LE_EXPR
) && sign
> 0))
2529 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2530 "with boolean expression is always false", cst
);
2532 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2533 "with boolean expression is always true", cst
);
2535 else if (integer_zerop (cst
) || integer_onep (cst
))
2537 /* If the non-constant operand isn't of a boolean type, we
2538 don't want to warn here. */
2539 tree noncst
= TREE_CODE (op0
) == INTEGER_CST
? op1
: op0
;
2540 /* Handle booleans promoted to integers. */
2541 if (bool_promoted_to_int_p (noncst
))
2543 else if (TREE_CODE (TREE_TYPE (noncst
)) != BOOLEAN_TYPE
2544 && !truth_value_p (TREE_CODE (noncst
)))
2546 /* Do some magic to get the right diagnostics. */
2547 bool flag
= TREE_CODE (op0
) == INTEGER_CST
;
2548 flag
= integer_zerop (cst
) ? flag
: !flag
;
2549 if ((code
== GE_EXPR
&& !flag
) || (code
== LE_EXPR
&& flag
))
2550 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2551 "with boolean expression is always true", cst
);
2552 else if ((code
== LT_EXPR
&& !flag
) || (code
== GT_EXPR
&& flag
))
2553 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2554 "with boolean expression is always false", cst
);
2558 /* Warn if an argument at position param_pos is passed to a
2559 restrict-qualified param, and it aliases with another argument.
2560 Return true if a warning has been issued. */
2563 warn_for_restrict (unsigned param_pos
, tree
*argarray
, unsigned nargs
)
2565 tree arg
= argarray
[param_pos
];
2566 if (TREE_VISITED (arg
) || integer_zerop (arg
))
2569 location_t loc
= EXPR_LOC_OR_LOC (arg
, input_location
);
2570 gcc_rich_location
richloc (loc
);
2573 auto_vec
<int, 16> arg_positions
;
2575 for (i
= 0; i
< nargs
; i
++)
2580 tree current_arg
= argarray
[i
];
2581 if (operand_equal_p (arg
, current_arg
, 0))
2583 TREE_VISITED (current_arg
) = 1;
2584 arg_positions
.safe_push (i
+ 1);
2588 if (arg_positions
.is_empty ())
2592 FOR_EACH_VEC_ELT (arg_positions
, i
, pos
)
2594 arg
= argarray
[pos
- 1];
2595 if (EXPR_HAS_LOCATION (arg
))
2596 richloc
.add_range (EXPR_LOCATION (arg
));
2599 return warning_n (&richloc
, OPT_Wrestrict
, arg_positions
.length (),
2600 "passing argument %i to restrict-qualified parameter"
2601 " aliases with argument %Z",
2602 "passing argument %i to restrict-qualified parameter"
2603 " aliases with arguments %Z",
2604 param_pos
+ 1, arg_positions
.address (),
2605 arg_positions
.length ());
2608 /* Callback function to determine whether an expression TP or one of its
2609 subexpressions comes from macro expansion. Used to suppress bogus
2613 expr_from_macro_expansion_r (tree
*tp
, int *, void *)
2615 if (CAN_HAVE_LOCATION_P (*tp
)
2616 && from_macro_expansion_at (EXPR_LOCATION (*tp
)))
2617 return integer_zero_node
;
2622 /* Possibly warn when an if-else has identical branches. */
2625 do_warn_duplicated_branches (tree expr
)
2627 tree thenb
= COND_EXPR_THEN (expr
);
2628 tree elseb
= COND_EXPR_ELSE (expr
);
2630 /* Don't bother if any of the branches is missing. */
2631 if (thenb
== NULL_TREE
|| elseb
== NULL_TREE
)
2634 /* And don't warn for empty statements. */
2635 if (TREE_CODE (thenb
) == NOP_EXPR
2636 && TREE_TYPE (thenb
) == void_type_node
2637 && TREE_OPERAND (thenb
, 0) == size_zero_node
)
2640 /* ... or empty branches. */
2641 if (TREE_CODE (thenb
) == STATEMENT_LIST
2642 && STATEMENT_LIST_HEAD (thenb
) == NULL
)
2645 /* Compute the hash of the then branch. */
2646 inchash::hash
hstate0 (0);
2647 inchash::add_expr (thenb
, hstate0
);
2648 hashval_t h0
= hstate0
.end ();
2650 /* Compute the hash of the else branch. */
2651 inchash::hash
hstate1 (0);
2652 inchash::add_expr (elseb
, hstate1
);
2653 hashval_t h1
= hstate1
.end ();
2655 /* Compare the hashes. */
2657 && operand_equal_p (thenb
, elseb
, OEP_LEXICOGRAPHIC
)
2658 /* Don't warn if any of the branches or their subexpressions comes
2660 && !walk_tree_without_duplicates (&thenb
, expr_from_macro_expansion_r
,
2662 && !walk_tree_without_duplicates (&elseb
, expr_from_macro_expansion_r
,
2664 warning_at (EXPR_LOCATION (expr
), OPT_Wduplicated_branches
,
2665 "this condition has identical branches");
2668 /* Callback for c_genericize to implement -Wduplicated-branches. */
2671 do_warn_duplicated_branches_r (tree
*tp
, int *, void *)
2673 if (TREE_CODE (*tp
) == COND_EXPR
)
2674 do_warn_duplicated_branches (*tp
);
2678 /* Implementation of -Wmultistatement-macros. This warning warns about
2679 cases when a macro expands to multiple statements not wrapped in
2680 do {} while (0) or ({ }) and is used as a body of if/else/for/while
2681 conditionals. For example,
2683 #define DOIT x++; y++
2688 will increment y unconditionally.
2690 BODY_LOC is the location of the first token in the body after labels
2691 have been parsed, NEXT_LOC is the location of the next token after the
2692 body of the conditional has been parsed, and GUARD_LOC is the location
2693 of the conditional. */
2696 warn_for_multistatement_macros (location_t body_loc
, location_t next_loc
,
2697 location_t guard_loc
, enum rid keyword
)
2699 if (!warn_multistatement_macros
)
2702 /* Ain't got time to waste. We only care about macros here. */
2703 if (!from_macro_expansion_at (body_loc
)
2704 || !from_macro_expansion_at (next_loc
))
2707 /* Let's skip macros defined in system headers. */
2708 if (in_system_header_at (body_loc
)
2709 || in_system_header_at (next_loc
))
2712 /* Find the actual tokens in the macro definition. BODY_LOC and
2713 NEXT_LOC have to come from the same spelling location, but they
2714 will resolve to different locations in the context of the macro
2716 location_t body_loc_exp
2717 = linemap_resolve_location (line_table
, body_loc
,
2718 LRK_MACRO_DEFINITION_LOCATION
, NULL
);
2719 location_t next_loc_exp
2720 = linemap_resolve_location (line_table
, next_loc
,
2721 LRK_MACRO_DEFINITION_LOCATION
, NULL
);
2722 location_t guard_loc_exp
2723 = linemap_resolve_location (line_table
, guard_loc
,
2724 LRK_MACRO_DEFINITION_LOCATION
, NULL
);
2726 /* These are some funky cases we don't want to warn about. */
2727 if (body_loc_exp
== guard_loc_exp
2728 || next_loc_exp
== guard_loc_exp
2729 || body_loc_exp
== next_loc_exp
)
2732 /* Find the macro maps for the macro expansions. */
2733 const line_map
*body_map
= linemap_lookup (line_table
, body_loc
);
2734 const line_map
*next_map
= linemap_lookup (line_table
, next_loc
);
2735 const line_map
*guard_map
= linemap_lookup (line_table
, guard_loc
);
2737 /* Now see if the following token (after the body) is coming from the
2738 same macro expansion. If it is, it might be a problem. */
2739 if (body_map
!= next_map
)
2742 /* The conditional itself must not come from the same expansion, because
2743 we don't want to warn about
2744 #define IF if (x) x++; y++
2746 if (guard_map
== body_map
)
2749 /* Handle the case where NEXT and BODY come from the same expansion while
2750 GUARD doesn't, yet we shouldn't warn. E.g.
2752 #define GUARD if (...)
2753 #define GUARD2 GUARD
2755 and in the definition of another macro:
2761 while (linemap_macro_expansion_map_p (guard_map
))
2763 const line_map_macro
*mm
= linemap_check_macro (guard_map
);
2764 guard_loc_exp
= MACRO_MAP_EXPANSION_POINT_LOCATION (mm
);
2765 guard_map
= linemap_lookup (line_table
, guard_loc_exp
);
2766 if (guard_map
== body_map
)
2770 auto_diagnostic_group d
;
2771 if (warning_at (body_loc
, OPT_Wmultistatement_macros
,
2772 "macro expands to multiple statements"))
2773 inform (guard_loc
, "some parts of macro expansion are not guarded by "
2774 "this %qs clause", guard_tinfo_to_string (keyword
));
2777 /* Return struct or union type if the alignment of data memeber, FIELD,
2778 is less than the alignment of TYPE. Otherwise, return NULL_TREE.
2779 If RVALUE is true, only arrays evaluate to pointers. */
2782 check_alignment_of_packed_member (tree type
, tree field
, bool rvalue
)
2784 /* Check alignment of the data member. */
2785 if (TREE_CODE (field
) == FIELD_DECL
2786 && (DECL_PACKED (field
) || TYPE_PACKED (TREE_TYPE (field
)))
2787 && (!rvalue
|| TREE_CODE (TREE_TYPE (field
)) == ARRAY_TYPE
))
2789 /* Check the expected alignment against the field alignment. */
2790 unsigned int type_align
= min_align_of_type (type
);
2791 tree context
= DECL_CONTEXT (field
);
2792 unsigned int record_align
= min_align_of_type (context
);
2793 if (record_align
< type_align
)
2795 tree field_off
= byte_position (field
);
2796 if (!multiple_of_p (TREE_TYPE (field_off
), field_off
,
2797 size_int (type_align
)))
2804 /* Return struct or union type if the right hand value, RHS:
2805 1. Is a pointer value which isn't aligned to a pointer type TYPE.
2806 2. Is an address which takes the unaligned address of packed member
2807 of struct or union when assigning to TYPE.
2808 Otherwise, return NULL_TREE. */
2811 check_address_or_pointer_of_packed_member (tree type
, tree rhs
)
2814 bool indirect
= false;
2816 if (INDIRECT_REF_P (rhs
))
2818 rhs
= TREE_OPERAND (rhs
, 0);
2823 if (TREE_CODE (rhs
) == ADDR_EXPR
)
2825 rhs
= TREE_OPERAND (rhs
, 0);
2829 if (!POINTER_TYPE_P (type
))
2832 type
= TREE_TYPE (type
);
2834 if (TREE_CODE (rhs
) == PARM_DECL
2836 || TREE_CODE (rhs
) == CALL_EXPR
)
2838 tree rhstype
= TREE_TYPE (rhs
);
2839 if (TREE_CODE (rhs
) == CALL_EXPR
)
2841 rhs
= CALL_EXPR_FN (rhs
); /* Pointer expression. */
2842 if (rhs
== NULL_TREE
)
2844 rhs
= TREE_TYPE (rhs
); /* Pointer type. */
2845 rhs
= TREE_TYPE (rhs
); /* Function type. */
2846 rhstype
= TREE_TYPE (rhs
);
2847 if (!rhstype
|| !POINTER_TYPE_P (rhstype
))
2851 if (rvalue
&& POINTER_TYPE_P (rhstype
))
2852 rhstype
= TREE_TYPE (rhstype
);
2853 while (TREE_CODE (rhstype
) == ARRAY_TYPE
)
2854 rhstype
= TREE_TYPE (rhstype
);
2855 if (TYPE_PACKED (rhstype
))
2857 unsigned int type_align
= min_align_of_type (type
);
2858 unsigned int rhs_align
= min_align_of_type (rhstype
);
2859 if (rhs_align
< type_align
)
2861 auto_diagnostic_group d
;
2862 location_t location
= EXPR_LOC_OR_LOC (rhs
, input_location
);
2863 if (warning_at (location
, OPT_Waddress_of_packed_member
,
2864 "converting a packed %qT pointer (alignment %d) "
2865 "to a %qT pointer (alignment %d) may result in "
2866 "an unaligned pointer value",
2867 rhstype
, rhs_align
, type
, type_align
))
2869 tree decl
= TYPE_STUB_DECL (rhstype
);
2871 inform (DECL_SOURCE_LOCATION (decl
), "defined here");
2872 decl
= TYPE_STUB_DECL (type
);
2874 inform (DECL_SOURCE_LOCATION (decl
), "defined here");
2881 tree context
= NULL_TREE
;
2883 /* Check alignment of the object. */
2884 while (handled_component_p (rhs
))
2886 if (TREE_CODE (rhs
) == COMPONENT_REF
)
2888 tree field
= TREE_OPERAND (rhs
, 1);
2889 context
= check_alignment_of_packed_member (type
, field
, rvalue
);
2893 if (TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
)
2897 rhs
= TREE_OPERAND (rhs
, 0);
2903 /* Check and warn if the right hand value, RHS:
2904 1. Is a pointer value which isn't aligned to a pointer type TYPE.
2905 2. Is an address which takes the unaligned address of packed member
2906 of struct or union when assigning to TYPE.
2910 check_and_warn_address_or_pointer_of_packed_member (tree type
, tree rhs
)
2917 while (TREE_CODE (rhs
) == COMPOUND_EXPR
)
2918 rhs
= TREE_OPERAND (rhs
, 1);
2921 nop_p
|= orig_rhs
!= rhs
;
2923 while (orig_rhs
!= rhs
);
2925 if (TREE_CODE (rhs
) == COND_EXPR
)
2927 /* Check the THEN path. */
2928 check_and_warn_address_or_pointer_of_packed_member
2929 (type
, TREE_OPERAND (rhs
, 1));
2931 /* Check the ELSE path. */
2932 check_and_warn_address_or_pointer_of_packed_member
2933 (type
, TREE_OPERAND (rhs
, 2));
2939 switch (TREE_CODE (rhs
))
2942 /* Address is taken. */
2945 /* Pointer conversion. */
2948 /* Function call. */
2956 = check_address_or_pointer_of_packed_member (type
, rhs
);
2959 location_t loc
= EXPR_LOC_OR_LOC (rhs
, input_location
);
2960 warning_at (loc
, OPT_Waddress_of_packed_member
,
2961 "taking address of packed member of %qT may result "
2962 "in an unaligned pointer value",
2968 /* Warn if the right hand value, RHS:
2969 1. Is a pointer value which isn't aligned to a pointer type TYPE.
2970 2. Is an address which takes the unaligned address of packed member
2971 of struct or union when assigning to TYPE.
2975 warn_for_address_or_pointer_of_packed_member (tree type
, tree rhs
)
2977 if (!warn_address_of_packed_member
)
2980 /* Don't warn if we don't assign RHS to a pointer. */
2981 if (!POINTER_TYPE_P (type
))
2984 check_and_warn_address_or_pointer_of_packed_member (type
, rhs
);