1 /* Diagnostic routines shared by all languages that are variants of C.
2 Copyright (C) 1992-2017 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 /* Print a warning if a constant expression had overflow in folding.
39 Invoke this function on every expression that the language
40 requires to be a constant expression.
41 Note the ANSI C standard says it is erroneous for a
42 constant expression to overflow. */
45 constant_expression_warning (tree value
)
47 if (warn_overflow
&& pedantic
48 && (TREE_CODE (value
) == INTEGER_CST
|| TREE_CODE (value
) == REAL_CST
49 || TREE_CODE (value
) == FIXED_CST
50 || TREE_CODE (value
) == VECTOR_CST
51 || TREE_CODE (value
) == COMPLEX_CST
)
52 && TREE_OVERFLOW (value
))
53 pedwarn (input_location
, OPT_Woverflow
, "overflow in constant expression");
56 /* The same as above but print an unconditional error. */
59 constant_expression_error (tree value
)
61 if ((TREE_CODE (value
) == INTEGER_CST
|| TREE_CODE (value
) == REAL_CST
62 || TREE_CODE (value
) == FIXED_CST
63 || TREE_CODE (value
) == VECTOR_CST
64 || TREE_CODE (value
) == COMPLEX_CST
)
65 && TREE_OVERFLOW (value
))
66 error ("overflow in constant expression");
69 /* Print a warning if an expression result VALUE had an overflow
70 in folding and its operands hadn't. EXPR, which may be null, is
71 the operand of the expression.
73 Invoke this function on every expression that
74 (1) appears in the source code, and
75 (2) is a constant expression that overflowed, and
76 (3) is not already checked by convert_and_check;
77 however, do not invoke this function on operands of explicit casts
78 or when the expression is the result of an operator and any operand
79 already overflowed. */
82 overflow_warning (location_t loc
, tree value
, tree expr
)
84 if (c_inhibit_evaluation_warnings
!= 0)
87 const char *warnfmt
= NULL
;
89 switch (TREE_CODE (value
))
93 ? G_("integer overflow in expression %qE of type %qT "
95 : G_("integer overflow in expression of type %qT "
101 ? G_ ("floating point overflow in expression %qE "
102 "of type %qT results in %qE")
103 : G_ ("floating point overflow in expression of type %qT "
109 ? G_("fixed-point overflow in expression %qE of type %qT "
111 : G_("fixed-point overflow in expression of type %qT "
117 ? G_("vector overflow in expression %qE of type %qT "
119 : G_("vector overflow in expression of type %qT "
124 if (TREE_CODE (TREE_REALPART (value
)) == INTEGER_CST
)
126 ? G_("complex integer overflow in expression %qE "
127 "of type %qT results in %qE")
128 : G_("complex integer overflow in expression of type %qT "
130 else if (TREE_CODE (TREE_REALPART (value
)) == REAL_CST
)
132 ? G_("complex floating point overflow in expression %qE "
133 "of type %qT results in %qE")
134 : G_("complex floating point overflow in expression "
135 "of type %qT results in %qE"));
145 warning_at (loc
, OPT_Woverflow
, warnfmt
, expr
, TREE_TYPE (expr
), value
);
147 warning_at (loc
, OPT_Woverflow
, warnfmt
, TREE_TYPE (value
), value
);
149 TREE_NO_WARNING (value
) = 1;
152 /* Helper function for walk_tree. Unwrap C_MAYBE_CONST_EXPRs in an expression
156 unwrap_c_maybe_const (tree
*tp
, int *walk_subtrees
, void *)
158 if (TREE_CODE (*tp
) == C_MAYBE_CONST_EXPR
)
160 *tp
= C_MAYBE_CONST_EXPR_EXPR (*tp
);
161 /* C_MAYBE_CONST_EXPRs don't nest. */
162 *walk_subtrees
= false;
167 /* Warn about uses of logical || / && operator in a context where it
168 is likely that the bitwise equivalent was intended by the
169 programmer. We have seen an expression in which CODE is a binary
170 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
171 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
174 warn_logical_operator (location_t location
, enum tree_code code
, tree type
,
175 enum tree_code code_left
, tree op_left
,
176 enum tree_code
ARG_UNUSED (code_right
), tree op_right
)
178 int or_op
= (code
== TRUTH_ORIF_EXPR
|| code
== TRUTH_OR_EXPR
);
179 int in0_p
, in1_p
, in_p
;
180 tree low0
, low1
, low
, high0
, high1
, high
, lhs
, rhs
, tem
;
181 bool strict_overflow_p
= false;
183 if (code
!= TRUTH_ANDIF_EXPR
184 && code
!= TRUTH_AND_EXPR
185 && code
!= TRUTH_ORIF_EXPR
186 && code
!= TRUTH_OR_EXPR
)
189 /* We don't want to warn if either operand comes from a macro
190 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
192 if (from_macro_expansion_at (EXPR_LOCATION (op_left
))
193 || from_macro_expansion_at (EXPR_LOCATION (op_right
)))
196 /* Warn if &&/|| are being used in a context where it is
197 likely that the bitwise equivalent was intended by the
198 programmer. That is, an expression such as op && MASK
199 where op should not be any boolean expression, nor a
200 constant, and mask seems to be a non-boolean integer constant. */
201 if (TREE_CODE (op_right
) == CONST_DECL
)
202 /* An enumerator counts as a constant. */
203 op_right
= DECL_INITIAL (op_right
);
204 if (!truth_value_p (code_left
)
205 && INTEGRAL_TYPE_P (TREE_TYPE (op_left
))
206 && !CONSTANT_CLASS_P (op_left
)
207 && !TREE_NO_WARNING (op_left
)
208 && TREE_CODE (op_right
) == INTEGER_CST
209 && !integer_zerop (op_right
)
210 && !integer_onep (op_right
))
213 warning_at (location
, OPT_Wlogical_op
, "logical %<or%>"
214 " applied to non-boolean constant");
216 warning_at (location
, OPT_Wlogical_op
, "logical %<and%>"
217 " applied to non-boolean constant");
218 TREE_NO_WARNING (op_left
) = true;
222 /* We do not warn for constants because they are typical of macro
223 expansions that test for features. */
224 if (CONSTANT_CLASS_P (fold_for_warn (op_left
))
225 || CONSTANT_CLASS_P (fold_for_warn (op_right
)))
228 /* This warning only makes sense with logical operands. */
229 if (!(truth_value_p (TREE_CODE (op_left
))
230 || INTEGRAL_TYPE_P (TREE_TYPE (op_left
)))
231 || !(truth_value_p (TREE_CODE (op_right
))
232 || INTEGRAL_TYPE_P (TREE_TYPE (op_right
))))
235 /* The range computations only work with scalars. */
236 if (VECTOR_TYPE_P (TREE_TYPE (op_left
))
237 || VECTOR_TYPE_P (TREE_TYPE (op_right
)))
240 /* We first test whether either side separately is trivially true
241 (with OR) or trivially false (with AND). If so, do not warn.
242 This is a common idiom for testing ranges of data types in
244 op_left
= unshare_expr (op_left
);
245 walk_tree_without_duplicates (&op_left
, unwrap_c_maybe_const
, NULL
);
246 lhs
= make_range (op_left
, &in0_p
, &low0
, &high0
, &strict_overflow_p
);
250 /* If this is an OR operation, invert both sides; now, the result
251 should be always false to get a warning. */
255 tem
= build_range_check (UNKNOWN_LOCATION
, type
, lhs
, in0_p
, low0
, high0
);
256 if (tem
&& integer_zerop (tem
))
259 op_right
= unshare_expr (op_right
);
260 walk_tree_without_duplicates (&op_right
, unwrap_c_maybe_const
, NULL
);
261 rhs
= make_range (op_right
, &in1_p
, &low1
, &high1
, &strict_overflow_p
);
265 /* If this is an OR operation, invert both sides; now, the result
266 should be always false to get a warning. */
270 tem
= build_range_check (UNKNOWN_LOCATION
, type
, rhs
, in1_p
, low1
, high1
);
271 if (tem
&& integer_zerop (tem
))
274 /* If both expressions have the same operand, if we can merge the
276 if (operand_equal_p (lhs
, rhs
, 0)
277 && merge_ranges (&in_p
, &low
, &high
, in0_p
, low0
, high0
,
280 tem
= build_range_check (UNKNOWN_LOCATION
, type
, lhs
, in_p
, low
, high
);
281 /* ... and if the range test is always false, then warn. */
282 if (tem
&& integer_zerop (tem
))
285 warning_at (location
, OPT_Wlogical_op
,
286 "logical %<or%> of collectively exhaustive tests is "
289 warning_at (location
, OPT_Wlogical_op
,
290 "logical %<and%> of mutually exclusive tests is "
293 /* Or warn if the operands have exactly the same range, e.g.
295 else if (tree_int_cst_equal (low0
, low1
)
296 && tree_int_cst_equal (high0
, high1
))
299 warning_at (location
, OPT_Wlogical_op
,
300 "logical %<or%> of equal expressions");
302 warning_at (location
, OPT_Wlogical_op
,
303 "logical %<and%> of equal expressions");
308 /* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
309 with constant indices. */
312 find_array_ref_with_const_idx_r (tree
*expr_p
, int *, void *)
316 if ((TREE_CODE (expr
) == ARRAY_REF
317 || TREE_CODE (expr
) == ARRAY_RANGE_REF
)
318 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
)
319 return integer_type_node
;
324 /* Subroutine of warn_tautological_cmp. Warn about bitwise comparison
325 that always evaluate to true or false. LOC is the location of the
326 ==/!= comparison specified by CODE; LHS and RHS are the usual operands
327 of this comparison. */
330 warn_tautological_bitwise_comparison (location_t loc
, tree_code code
,
333 if (code
!= EQ_EXPR
&& code
!= NE_EXPR
)
336 /* Extract the operands from e.g. (x & 8) == 4. */
339 if ((TREE_CODE (lhs
) == BIT_AND_EXPR
340 || TREE_CODE (lhs
) == BIT_IOR_EXPR
)
341 && TREE_CODE (rhs
) == INTEGER_CST
)
342 bitop
= lhs
, cst
= rhs
;
343 else if ((TREE_CODE (rhs
) == BIT_AND_EXPR
344 || TREE_CODE (rhs
) == BIT_IOR_EXPR
)
345 && TREE_CODE (lhs
) == INTEGER_CST
)
346 bitop
= rhs
, cst
= lhs
;
351 if (TREE_CODE (TREE_OPERAND (bitop
, 0)) == INTEGER_CST
)
352 bitopcst
= TREE_OPERAND (bitop
, 0);
353 else if (TREE_CODE (TREE_OPERAND (bitop
, 1)) == INTEGER_CST
)
354 bitopcst
= TREE_OPERAND (bitop
, 1);
358 /* Note that the two operands are from before the usual integer
359 conversions, so their types might not be the same. */
361 if (TREE_CODE (bitop
) == BIT_AND_EXPR
)
362 res
= wi::to_widest (bitopcst
) & wi::to_widest (cst
);
364 res
= wi::to_widest (bitopcst
) | wi::to_widest (cst
);
366 /* For BIT_AND only warn if (CST2 & CST1) != CST1, and
367 for BIT_OR only if (CST2 | CST1) != CST1. */
368 if (res
== wi::to_widest (cst
))
372 warning_at (loc
, OPT_Wtautological_compare
,
373 "bitwise comparison always evaluates to false");
375 warning_at (loc
, OPT_Wtautological_compare
,
376 "bitwise comparison always evaluates to true");
379 /* Warn if a self-comparison always evaluates to true or false. LOC
380 is the location of the comparison with code CODE, LHS and RHS are
381 operands of the comparison. */
384 warn_tautological_cmp (location_t loc
, enum tree_code code
, tree lhs
, tree rhs
)
386 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
389 /* Don't warn for various macro expansions. */
390 if (from_macro_expansion_at (loc
)
391 || from_macro_expansion_at (EXPR_LOCATION (lhs
))
392 || from_macro_expansion_at (EXPR_LOCATION (rhs
)))
395 warn_tautological_bitwise_comparison (loc
, code
, lhs
, rhs
);
397 /* We do not warn for constants because they are typical of macro
398 expansions that test for features, sizeof, and similar. */
399 if (CONSTANT_CLASS_P (fold_for_warn (lhs
))
400 || CONSTANT_CLASS_P (fold_for_warn (rhs
)))
403 /* Don't warn for e.g.
406 if (n == (long) n) ...
408 if ((CONVERT_EXPR_P (lhs
) || TREE_CODE (lhs
) == NON_LVALUE_EXPR
)
409 || (CONVERT_EXPR_P (rhs
) || TREE_CODE (rhs
) == NON_LVALUE_EXPR
))
412 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
413 It could be a NaN, and NaN never compares equal to anything, even
415 if (FLOAT_TYPE_P (TREE_TYPE (lhs
)) || FLOAT_TYPE_P (TREE_TYPE (rhs
)))
418 if (operand_equal_p (lhs
, rhs
, 0))
420 /* Don't warn about array references with constant indices;
421 these are likely to come from a macro. */
422 if (walk_tree_without_duplicates (&lhs
, find_array_ref_with_const_idx_r
,
425 const bool always_true
= (code
== EQ_EXPR
|| code
== LE_EXPR
426 || code
== GE_EXPR
|| code
== UNLE_EXPR
427 || code
== UNGE_EXPR
|| code
== UNEQ_EXPR
);
429 warning_at (loc
, OPT_Wtautological_compare
,
430 "self-comparison always evaluates to true");
432 warning_at (loc
, OPT_Wtautological_compare
,
433 "self-comparison always evaluates to false");
437 /* Return true iff EXPR only contains boolean operands, or comparisons. */
440 expr_has_boolean_operands_p (tree expr
)
444 if (CONVERT_EXPR_P (expr
))
445 return bool_promoted_to_int_p (expr
);
446 else if (UNARY_CLASS_P (expr
))
447 return expr_has_boolean_operands_p (TREE_OPERAND (expr
, 0));
448 else if (BINARY_CLASS_P (expr
))
449 return (expr_has_boolean_operands_p (TREE_OPERAND (expr
, 0))
450 && expr_has_boolean_operands_p (TREE_OPERAND (expr
, 1)));
451 else if (COMPARISON_CLASS_P (expr
))
457 /* Warn about logical not used on the left hand side operand of a comparison.
458 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
459 Do not warn if RHS is of a boolean type, a logical operator, or
463 warn_logical_not_parentheses (location_t location
, enum tree_code code
,
466 if (TREE_CODE_CLASS (code
) != tcc_comparison
467 || TREE_TYPE (rhs
) == NULL_TREE
468 || TREE_CODE (TREE_TYPE (rhs
)) == BOOLEAN_TYPE
469 || truth_value_p (TREE_CODE (rhs
)))
472 /* Don't warn for expression like !x == ~(bool1 | bool2). */
473 if (expr_has_boolean_operands_p (rhs
))
476 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
477 !(x == 0) or !(y != 0). */
478 if ((code
== EQ_EXPR
|| code
== NE_EXPR
)
479 && integer_zerop (rhs
))
482 if (warning_at (location
, OPT_Wlogical_not_parentheses
,
483 "logical not is only applied to the left hand side of "
485 && EXPR_HAS_LOCATION (lhs
))
487 location_t lhs_loc
= EXPR_LOCATION (lhs
);
488 rich_location
richloc (line_table
, lhs_loc
);
489 richloc
.add_fixit_insert_before (lhs_loc
, "(");
490 richloc
.add_fixit_insert_after (lhs_loc
, ")");
491 inform_at_rich_loc (&richloc
, "add parentheses around left hand side "
492 "expression to silence this warning");
496 /* Warn if EXP contains any computations whose results are not used.
497 Return true if a warning is printed; false otherwise. LOCUS is the
498 (potential) location of the expression. */
501 warn_if_unused_value (const_tree exp
, location_t locus
)
504 if (TREE_USED (exp
) || TREE_NO_WARNING (exp
))
507 /* Don't warn about void constructs. This includes casting to void,
508 void function calls, and statement expressions with a final cast
510 if (VOID_TYPE_P (TREE_TYPE (exp
)))
513 if (EXPR_HAS_LOCATION (exp
))
514 locus
= EXPR_LOCATION (exp
);
516 switch (TREE_CODE (exp
))
518 case PREINCREMENT_EXPR
:
519 case POSTINCREMENT_EXPR
:
520 case PREDECREMENT_EXPR
:
521 case POSTDECREMENT_EXPR
:
532 /* For a binding, warn if no side effect within it. */
533 exp
= BIND_EXPR_BODY (exp
);
537 case NON_LVALUE_EXPR
:
539 exp
= TREE_OPERAND (exp
, 0);
542 case TRUTH_ORIF_EXPR
:
543 case TRUTH_ANDIF_EXPR
:
544 /* In && or ||, warn if 2nd operand has no side effect. */
545 exp
= TREE_OPERAND (exp
, 1);
549 if (warn_if_unused_value (TREE_OPERAND (exp
, 0), locus
))
551 /* Let people do `(foo (), 0)' without a warning. */
552 if (TREE_CONSTANT (TREE_OPERAND (exp
, 1)))
554 exp
= TREE_OPERAND (exp
, 1);
558 /* If this is an expression with side effects, don't warn; this
559 case commonly appears in macro expansions. */
560 if (TREE_SIDE_EFFECTS (exp
))
565 /* Don't warn about automatic dereferencing of references, since
566 the user cannot control it. */
567 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp
, 0))) == REFERENCE_TYPE
)
569 exp
= TREE_OPERAND (exp
, 0);
575 /* Referencing a volatile value is a side effect, so don't warn. */
576 if ((DECL_P (exp
) || REFERENCE_CLASS_P (exp
))
577 && TREE_THIS_VOLATILE (exp
))
580 /* If this is an expression which has no operands, there is no value
581 to be unused. There are no such language-independent codes,
582 but front ends may define such. */
583 if (EXPRESSION_CLASS_P (exp
) && TREE_OPERAND_LENGTH (exp
) == 0)
587 return warning_at (locus
, OPT_Wunused_value
, "value computed is not used");
591 /* Print a warning about casts that might indicate violation
592 of strict aliasing rules if -Wstrict-aliasing is used and
593 strict aliasing mode is in effect. OTYPE is the original
594 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
597 strict_aliasing_warning (tree otype
, tree type
, tree expr
)
599 /* Strip pointer conversion chains and get to the correct original type. */
601 otype
= TREE_TYPE (expr
);
603 if (!(flag_strict_aliasing
604 && POINTER_TYPE_P (type
)
605 && POINTER_TYPE_P (otype
)
606 && !VOID_TYPE_P (TREE_TYPE (type
)))
607 /* If the type we are casting to is a ref-all pointer
608 dereferencing it is always valid. */
609 || TYPE_REF_CAN_ALIAS_ALL (type
))
612 if ((warn_strict_aliasing
> 1) && TREE_CODE (expr
) == ADDR_EXPR
613 && (DECL_P (TREE_OPERAND (expr
, 0))
614 || handled_component_p (TREE_OPERAND (expr
, 0))))
616 /* Casting the address of an object to non void pointer. Warn
617 if the cast breaks type based aliasing. */
618 if (!COMPLETE_TYPE_P (TREE_TYPE (type
)) && warn_strict_aliasing
== 2)
620 warning (OPT_Wstrict_aliasing
, "type-punning to incomplete type "
621 "might break strict-aliasing rules");
626 /* warn_strict_aliasing >= 3. This includes the default (3).
627 Only warn if the cast is dereferenced immediately. */
629 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr
, 0)));
630 alias_set_type set2
= get_alias_set (TREE_TYPE (type
));
634 && !alias_set_subset_of (set2
, set1
)
635 && !alias_sets_conflict_p (set1
, set2
))
637 warning (OPT_Wstrict_aliasing
, "dereferencing type-punned "
638 "pointer will break strict-aliasing rules");
641 else if (warn_strict_aliasing
== 2
642 && !alias_sets_must_conflict_p (set1
, set2
))
644 warning (OPT_Wstrict_aliasing
, "dereferencing type-punned "
645 "pointer might break strict-aliasing rules");
650 else if ((warn_strict_aliasing
== 1) && !VOID_TYPE_P (TREE_TYPE (otype
)))
652 /* At this level, warn for any conversions, even if an address is
653 not taken in the same statement. This will likely produce many
654 false positives, but could be useful to pinpoint problems that
655 are not revealed at higher levels. */
656 alias_set_type set1
= get_alias_set (TREE_TYPE (otype
));
657 alias_set_type set2
= get_alias_set (TREE_TYPE (type
));
658 if (!COMPLETE_TYPE_P (type
)
659 || !alias_sets_must_conflict_p (set1
, set2
))
661 warning (OPT_Wstrict_aliasing
, "dereferencing type-punned "
662 "pointer might break strict-aliasing rules");
670 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
671 sizeof as last operand of certain builtins. */
674 sizeof_pointer_memaccess_warning (location_t
*sizeof_arg_loc
, tree callee
,
675 vec
<tree
, va_gc
> *params
, tree
*sizeof_arg
,
676 bool (*comp_types
) (tree
, tree
))
678 tree type
, dest
= NULL_TREE
, src
= NULL_TREE
, tem
;
679 bool strop
= false, cmp
= false;
680 unsigned int idx
= ~0;
683 if (TREE_CODE (callee
) != FUNCTION_DECL
684 || DECL_BUILT_IN_CLASS (callee
) != BUILT_IN_NORMAL
685 || vec_safe_length (params
) <= 1)
688 switch (DECL_FUNCTION_CODE (callee
))
690 case BUILT_IN_STRNCMP
:
691 case BUILT_IN_STRNCASECMP
:
694 case BUILT_IN_STRNCPY
:
695 case BUILT_IN_STRNCPY_CHK
:
696 case BUILT_IN_STRNCAT
:
697 case BUILT_IN_STRNCAT_CHK
:
698 case BUILT_IN_STPNCPY
:
699 case BUILT_IN_STPNCPY_CHK
:
702 case BUILT_IN_MEMCPY
:
703 case BUILT_IN_MEMCPY_CHK
:
704 case BUILT_IN_MEMMOVE
:
705 case BUILT_IN_MEMMOVE_CHK
:
706 if (params
->length () < 3)
713 if (params
->length () < 3)
719 case BUILT_IN_MEMCMP
:
721 if (params
->length () < 3)
728 case BUILT_IN_MEMSET
:
729 case BUILT_IN_MEMSET_CHK
:
730 if (params
->length () < 3)
739 case BUILT_IN_STRNDUP
:
744 case BUILT_IN_MEMCHR
:
745 if (params
->length () < 3)
750 case BUILT_IN_SNPRINTF
:
751 case BUILT_IN_SNPRINTF_CHK
:
752 case BUILT_IN_VSNPRINTF
:
753 case BUILT_IN_VSNPRINTF_CHK
:
765 if (sizeof_arg
[idx
] == NULL
|| sizeof_arg
[idx
] == error_mark_node
)
768 type
= TYPE_P (sizeof_arg
[idx
])
769 ? sizeof_arg
[idx
] : TREE_TYPE (sizeof_arg
[idx
]);
770 if (!POINTER_TYPE_P (type
))
774 && (tem
= tree_strip_nop_conversions (dest
))
775 && POINTER_TYPE_P (TREE_TYPE (tem
))
776 && comp_types (TREE_TYPE (TREE_TYPE (tem
)), type
))
780 && (tem
= tree_strip_nop_conversions (src
))
781 && POINTER_TYPE_P (TREE_TYPE (tem
))
782 && comp_types (TREE_TYPE (TREE_TYPE (tem
)), type
))
785 loc
= sizeof_arg_loc
[idx
];
789 if (!TYPE_P (sizeof_arg
[idx
])
790 && operand_equal_p (dest
, sizeof_arg
[idx
], 0)
791 && comp_types (TREE_TYPE (dest
), type
))
793 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
794 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
795 "argument to %<sizeof%> in %qD call is the same "
796 "expression as the destination; did you mean to "
797 "remove the addressof?", callee
);
798 else if ((TYPE_PRECISION (TREE_TYPE (type
))
799 == TYPE_PRECISION (char_type_node
))
801 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
802 "argument to %<sizeof%> in %qD call is the same "
803 "expression as the destination; did you mean to "
804 "provide an explicit length?", callee
);
806 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
807 "argument to %<sizeof%> in %qD call is the same "
808 "expression as the destination; did you mean to "
809 "dereference it?", callee
);
813 if (POINTER_TYPE_P (TREE_TYPE (dest
))
815 && comp_types (TREE_TYPE (dest
), type
)
816 && !VOID_TYPE_P (TREE_TYPE (type
)))
818 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
819 "argument to %<sizeof%> in %qD call is the same "
820 "pointer type %qT as the destination; expected %qT "
821 "or an explicit length", callee
, TREE_TYPE (dest
),
822 TREE_TYPE (TREE_TYPE (dest
)));
829 if (!TYPE_P (sizeof_arg
[idx
])
830 && operand_equal_p (src
, sizeof_arg
[idx
], 0)
831 && comp_types (TREE_TYPE (src
), type
))
833 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
834 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
835 "argument to %<sizeof%> in %qD call is the same "
836 "expression as the source; did you mean to "
837 "remove the addressof?", callee
);
838 else if ((TYPE_PRECISION (TREE_TYPE (type
))
839 == TYPE_PRECISION (char_type_node
))
841 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
842 "argument to %<sizeof%> in %qD call is the same "
843 "expression as the source; did you mean to "
844 "provide an explicit length?", callee
);
846 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
847 "argument to %<sizeof%> in %qD call is the same "
848 "expression as the source; did you mean to "
849 "dereference it?", callee
);
853 if (POINTER_TYPE_P (TREE_TYPE (src
))
855 && comp_types (TREE_TYPE (src
), type
)
856 && !VOID_TYPE_P (TREE_TYPE (type
)))
858 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
859 "argument to %<sizeof%> in %qD call is the same "
860 "pointer type %qT as the source; expected %qT "
861 "or an explicit length", callee
, TREE_TYPE (src
),
862 TREE_TYPE (TREE_TYPE (src
)));
869 if (!TYPE_P (sizeof_arg
[idx
])
870 && operand_equal_p (dest
, sizeof_arg
[idx
], 0)
871 && comp_types (TREE_TYPE (dest
), type
))
873 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
874 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
875 "argument to %<sizeof%> in %qD call is the same "
876 "expression as the first source; did you mean to "
877 "remove the addressof?", callee
);
878 else if ((TYPE_PRECISION (TREE_TYPE (type
))
879 == TYPE_PRECISION (char_type_node
))
881 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
882 "argument to %<sizeof%> in %qD call is the same "
883 "expression as the first source; did you mean to "
884 "provide an explicit length?", callee
);
886 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
887 "argument to %<sizeof%> in %qD call is the same "
888 "expression as the first source; did you mean to "
889 "dereference it?", callee
);
893 if (POINTER_TYPE_P (TREE_TYPE (dest
))
895 && comp_types (TREE_TYPE (dest
), type
)
896 && !VOID_TYPE_P (TREE_TYPE (type
)))
898 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
899 "argument to %<sizeof%> in %qD call is the same "
900 "pointer type %qT as the first source; expected %qT "
901 "or an explicit length", callee
, TREE_TYPE (dest
),
902 TREE_TYPE (TREE_TYPE (dest
)));
909 if (!TYPE_P (sizeof_arg
[idx
])
910 && operand_equal_p (src
, sizeof_arg
[idx
], 0)
911 && comp_types (TREE_TYPE (src
), type
))
913 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
914 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
915 "argument to %<sizeof%> in %qD call is the same "
916 "expression as the second source; did you mean to "
917 "remove the addressof?", callee
);
918 else if ((TYPE_PRECISION (TREE_TYPE (type
))
919 == TYPE_PRECISION (char_type_node
))
921 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
922 "argument to %<sizeof%> in %qD call is the same "
923 "expression as the second source; did you mean to "
924 "provide an explicit length?", callee
);
926 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
927 "argument to %<sizeof%> in %qD call is the same "
928 "expression as the second source; did you mean to "
929 "dereference it?", callee
);
933 if (POINTER_TYPE_P (TREE_TYPE (src
))
935 && comp_types (TREE_TYPE (src
), type
)
936 && !VOID_TYPE_P (TREE_TYPE (type
)))
938 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
939 "argument to %<sizeof%> in %qD call is the same "
940 "pointer type %qT as the second source; expected %qT "
941 "or an explicit length", callee
, TREE_TYPE (src
),
942 TREE_TYPE (TREE_TYPE (src
)));
949 /* Warn for unlikely, improbable, or stupid DECL declarations
953 check_main_parameter_types (tree decl
)
955 function_args_iterator iter
;
959 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl
), type
, iter
)
961 /* XXX void_type_node belies the abstraction. */
962 if (type
== void_type_node
|| type
== error_mark_node
)
967 pedwarn (input_location
, OPT_Wmain
,
968 "%<_Atomic%>-qualified parameter type %qT of %q+D",
970 while (POINTER_TYPE_P (t
))
974 pedwarn (input_location
, OPT_Wmain
,
975 "%<_Atomic%>-qualified parameter type %qT of %q+D",
983 if (TYPE_MAIN_VARIANT (type
) != integer_type_node
)
984 pedwarn (input_location
, OPT_Wmain
,
985 "first argument of %q+D should be %<int%>", decl
);
989 if (TREE_CODE (type
) != POINTER_TYPE
990 || TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
991 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type
)))
993 pedwarn (input_location
, OPT_Wmain
,
994 "second argument of %q+D should be %<char **%>", decl
);
998 if (TREE_CODE (type
) != POINTER_TYPE
999 || TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
1000 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type
)))
1002 pedwarn (input_location
, OPT_Wmain
,
1003 "third argument of %q+D should probably be "
1004 "%<char **%>", decl
);
1009 /* It is intentional that this message does not mention the third
1010 argument because it's only mentioned in an appendix of the
1012 if (argct
> 0 && (argct
< 2 || argct
> 3))
1013 pedwarn (input_location
, OPT_Wmain
,
1014 "%q+D takes only zero or two arguments", decl
);
1016 if (stdarg_p (TREE_TYPE (decl
)))
1017 pedwarn (input_location
, OPT_Wmain
,
1018 "%q+D declared as variadic function", decl
);
1021 /* Warns if the conversion of EXPR to TYPE may alter a value.
1022 This is a helper function for warnings_for_convert_and_check. */
1025 conversion_warning (location_t loc
, tree type
, tree expr
, tree result
)
1027 tree expr_type
= TREE_TYPE (expr
);
1028 enum conversion_safety conversion_kind
;
1030 if (!warn_conversion
&& !warn_sign_conversion
&& !warn_float_conversion
)
1033 /* This may happen, because for LHS op= RHS we preevaluate
1034 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
1035 means we could no longer see the code of the EXPR. */
1036 if (TREE_CODE (expr
) == C_MAYBE_CONST_EXPR
)
1037 expr
= C_MAYBE_CONST_EXPR_EXPR (expr
);
1038 if (TREE_CODE (expr
) == SAVE_EXPR
)
1039 expr
= TREE_OPERAND (expr
, 0);
1041 switch (TREE_CODE (expr
))
1049 case TRUTH_ANDIF_EXPR
:
1050 case TRUTH_ORIF_EXPR
:
1051 case TRUTH_AND_EXPR
:
1053 case TRUTH_XOR_EXPR
:
1054 case TRUTH_NOT_EXPR
:
1055 /* Conversion from boolean to a signed:1 bit-field (which only
1056 can hold the values 0 and -1) doesn't lose information - but
1057 it does change the value. */
1058 if (TYPE_PRECISION (type
) == 1 && !TYPE_UNSIGNED (type
))
1059 warning_at (loc
, OPT_Wconversion
,
1060 "conversion to %qT from boolean expression", type
);
1067 conversion_kind
= unsafe_conversion_p (loc
, type
, expr
, result
, true);
1069 if (conversion_kind
== UNSAFE_REAL
)
1070 warnopt
= OPT_Wfloat_conversion
;
1071 else if (conversion_kind
)
1072 warnopt
= OPT_Wconversion
;
1076 if (TREE_CODE_CLASS (TREE_CODE (result
)) == tcc_constant
)
1077 warning_at (loc
, warnopt
,
1078 "conversion from %qT to %qT changes value from %qE to %qE",
1079 expr_type
, type
, expr
, result
);
1081 warning_at (loc
, warnopt
,
1082 "conversion from %qT to %qT changes the value of %qE",
1083 expr_type
, type
, expr
);
1088 /* In case of COND_EXPR, we do not care about the type of
1089 COND_EXPR, only about the conversion of each operand. */
1090 tree op1
= TREE_OPERAND (expr
, 1);
1091 tree op2
= TREE_OPERAND (expr
, 2);
1093 conversion_warning (loc
, type
, op1
, result
);
1094 conversion_warning (loc
, type
, op2
, result
);
1098 default: /* 'expr' is not a constant. */
1099 conversion_kind
= unsafe_conversion_p (loc
, type
, expr
, result
, true);
1100 if (conversion_kind
== UNSAFE_IMAGINARY
)
1101 warning_at (loc
, OPT_Wconversion
,
1102 "conversion from %qT to to %qT discards imaginary "
1108 if (conversion_kind
== UNSAFE_REAL
)
1109 warnopt
= OPT_Wfloat_conversion
;
1110 else if (conversion_kind
)
1111 warnopt
= OPT_Wconversion
;
1114 warning_at (loc
, warnopt
,
1115 "conversion from %qT to %qT may change value",
1121 /* Produce warnings after a conversion. RESULT is the result of
1122 converting EXPR to TYPE. This is a helper function for
1123 convert_and_check and cp_convert_and_check. */
1126 warnings_for_convert_and_check (location_t loc
, tree type
, tree expr
,
1129 loc
= expansion_point_location_if_in_system_header (loc
);
1131 bool cst
= TREE_CODE_CLASS (TREE_CODE (result
)) == tcc_constant
;
1133 tree exprtype
= TREE_TYPE (expr
);
1135 if (TREE_CODE (expr
) == INTEGER_CST
1136 && (TREE_CODE (type
) == INTEGER_TYPE
1137 || TREE_CODE (type
) == ENUMERAL_TYPE
)
1138 && !int_fits_type_p (expr
, type
))
1140 /* Do not diagnose overflow in a constant expression merely
1141 because a conversion overflowed. */
1142 if (TREE_OVERFLOW (result
))
1143 TREE_OVERFLOW (result
) = TREE_OVERFLOW (expr
);
1145 if (TYPE_UNSIGNED (type
))
1147 /* This detects cases like converting -129 or 256 to
1149 if (!int_fits_type_p (expr
, c_common_signed_type (type
)))
1152 warning_at (loc
, OPT_Woverflow
,
1153 (TYPE_UNSIGNED (exprtype
)
1154 ? G_("conversion from %qT to %qT "
1155 "changes value from %qE to %qE")
1156 : G_("unsigned conversion from %qT to %qT "
1157 "changes value from %qE to %qE")),
1158 exprtype
, type
, expr
, result
);
1160 warning_at (loc
, OPT_Woverflow
,
1161 (TYPE_UNSIGNED (exprtype
)
1162 ? G_("conversion from %qT to %qT "
1163 "changes the value of %qE")
1164 : G_("unsigned conversion from %qT to %qT "
1165 "changes the value of %qE")),
1166 exprtype
, type
, expr
);
1169 conversion_warning (loc
, type
, expr
, result
);
1171 else if (!int_fits_type_p (expr
, c_common_unsigned_type (type
)))
1174 warning_at (loc
, OPT_Woverflow
,
1175 "overflow in conversion from %qT to %qT "
1176 "changes value from %qE to %qE",
1177 exprtype
, type
, expr
, result
);
1179 warning_at (loc
, OPT_Woverflow
,
1180 "overflow in conversion from %qT to %qT "
1181 "changes the value of %qE",
1182 exprtype
, type
, expr
);
1184 /* No warning for converting 0x80000000 to int. */
1186 && (TREE_CODE (exprtype
) != INTEGER_TYPE
1187 || TYPE_PRECISION (exprtype
)
1188 != TYPE_PRECISION (type
)))
1191 warning_at (loc
, OPT_Woverflow
,
1192 "overflow in conversion from %qT to %qT "
1193 "changes value from %qE to %qE",
1194 exprtype
, type
, expr
, result
);
1196 warning_at (loc
, OPT_Woverflow
,
1197 "overflow in conversion from %qT to %qT "
1198 "changes the value of %qE",
1199 exprtype
, type
, expr
);
1202 conversion_warning (loc
, type
, expr
, result
);
1204 else if ((TREE_CODE (result
) == INTEGER_CST
1205 || TREE_CODE (result
) == FIXED_CST
) && TREE_OVERFLOW (result
))
1208 warning_at (loc
, OPT_Woverflow
,
1209 "overflow in conversion from %qT to %qT "
1210 "chages value from %qE to %qE",
1211 exprtype
, type
, expr
, result
);
1213 warning_at (loc
, OPT_Woverflow
,
1214 "overflow in conversion from %qT to %qT "
1215 "chages the value of %qE",
1216 exprtype
, type
, expr
);
1219 conversion_warning (loc
, type
, expr
, result
);
1222 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
1223 Used to verify that case values match up with enumerator values. */
1226 match_case_to_enum_1 (tree key
, tree type
, tree label
)
1228 /* Avoid warning about enums that have no enumerators. */
1229 if (TYPE_VALUES (type
) == NULL_TREE
)
1232 char buf
[WIDE_INT_PRINT_BUFFER_SIZE
];
1234 if (tree_fits_uhwi_p (key
))
1235 print_dec (key
, buf
, UNSIGNED
);
1236 else if (tree_fits_shwi_p (key
))
1237 print_dec (key
, buf
, SIGNED
);
1239 print_hex (key
, buf
);
1241 if (TYPE_NAME (type
) == NULL_TREE
)
1242 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label
)),
1243 warn_switch
? OPT_Wswitch
: OPT_Wswitch_enum
,
1244 "case value %qs not in enumerated type",
1247 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label
)),
1248 warn_switch
? OPT_Wswitch
: OPT_Wswitch_enum
,
1249 "case value %qs not in enumerated type %qT",
1253 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
1254 Used to verify that case values match up with enumerator values. */
1257 match_case_to_enum (splay_tree_node node
, void *data
)
1259 tree label
= (tree
) node
->value
;
1260 tree type
= (tree
) data
;
1262 /* Skip default case. */
1263 if (!CASE_LOW (label
))
1266 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
1267 when we did our enum->case scan. Reset our scratch bit after. */
1268 if (!CASE_LOW_SEEN (label
))
1269 match_case_to_enum_1 (CASE_LOW (label
), type
, label
);
1271 CASE_LOW_SEEN (label
) = 0;
1273 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
1274 not set, that means that CASE_HIGH did not appear when we did our
1275 enum->case scan. Reset our scratch bit after. */
1276 if (CASE_HIGH (label
))
1278 if (!CASE_HIGH_SEEN (label
))
1279 match_case_to_enum_1 (CASE_HIGH (label
), type
, label
);
1281 CASE_HIGH_SEEN (label
) = 0;
1287 /* Handle -Wswitch*. Called from the front end after parsing the
1288 switch construct. */
1289 /* ??? Should probably be somewhere generic, since other languages
1290 besides C and C++ would want this. At the moment, however, C/C++
1291 are the only tree-ssa languages that support enumerations at all,
1292 so the point is moot. */
1295 c_do_switch_warnings (splay_tree cases
, location_t switch_location
,
1296 tree type
, tree cond
, bool bool_cond_p
,
1297 bool outside_range_p
)
1299 splay_tree_node default_node
;
1300 splay_tree_node node
;
1303 if (!warn_switch
&& !warn_switch_enum
&& !warn_switch_default
1304 && !warn_switch_bool
)
1307 default_node
= splay_tree_lookup (cases
, (splay_tree_key
) NULL
);
1309 warning_at (switch_location
, OPT_Wswitch_default
,
1310 "switch missing default case");
1312 /* There are certain cases where -Wswitch-bool warnings aren't
1319 so be careful here. */
1320 if (warn_switch_bool
&& bool_cond_p
)
1322 splay_tree_node min_node
;
1323 /* If there's a default node, it's also the value with the minimal
1324 key. So look at the penultimate key (if any). */
1326 min_node
= splay_tree_successor (cases
, (splay_tree_key
) NULL
);
1328 min_node
= splay_tree_min (cases
);
1329 tree min
= min_node
? (tree
) min_node
->key
: NULL_TREE
;
1331 splay_tree_node max_node
= splay_tree_max (cases
);
1332 /* This might be a case range, so look at the value with the
1333 maximal key and then check CASE_HIGH. */
1334 tree max
= max_node
? (tree
) max_node
->value
: NULL_TREE
;
1336 max
= CASE_HIGH (max
) ? CASE_HIGH (max
) : CASE_LOW (max
);
1338 /* If there's a case value > 1 or < 0, that is outside bool
1341 || (max
&& wi::gts_p (max
, 1))
1342 || (min
&& wi::lts_p (min
, 0))
1350 case, where we want to warn. */
1352 && max
&& wi::eq_p (max
, 1)
1353 && min
&& wi::eq_p (min
, 0)))
1354 warning_at (switch_location
, OPT_Wswitch_bool
,
1355 "switch condition has boolean value");
1358 /* From here on, we only care about enumerated types. */
1359 if (!type
|| TREE_CODE (type
) != ENUMERAL_TYPE
)
1362 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
1363 if (!warn_switch_enum
&& !warn_switch
)
1366 /* Check the cases. Warn about case values which are not members of
1367 the enumerated type. For -Wswitch-enum, or for -Wswitch when
1368 there is no default case, check that exactly all enumeration
1369 literals are covered by the cases. */
1371 /* Clearing COND if it is not an integer constant simplifies
1372 the tests inside the loop below. */
1373 if (TREE_CODE (cond
) != INTEGER_CST
)
1376 /* The time complexity here is O(N*lg(N)) worst case, but for the
1377 common case of monotonically increasing enumerators, it is
1378 O(N), since the nature of the splay tree will keep the next
1379 element adjacent to the root at all times. */
1381 for (chain
= TYPE_VALUES (type
); chain
; chain
= TREE_CHAIN (chain
))
1383 tree value
= TREE_VALUE (chain
);
1384 if (TREE_CODE (value
) == CONST_DECL
)
1385 value
= DECL_INITIAL (value
);
1386 node
= splay_tree_lookup (cases
, (splay_tree_key
) value
);
1389 /* Mark the CASE_LOW part of the case entry as seen. */
1390 tree label
= (tree
) node
->value
;
1391 CASE_LOW_SEEN (label
) = 1;
1395 /* Even though there wasn't an exact match, there might be a
1396 case range which includes the enumerator's value. */
1397 node
= splay_tree_predecessor (cases
, (splay_tree_key
) value
);
1398 if (node
&& CASE_HIGH ((tree
) node
->value
))
1400 tree label
= (tree
) node
->value
;
1401 int cmp
= tree_int_cst_compare (CASE_HIGH (label
), value
);
1404 /* If we match the upper bound exactly, mark the CASE_HIGH
1405 part of the case entry as seen. */
1407 CASE_HIGH_SEEN (label
) = 1;
1412 /* We've now determined that this enumerated literal isn't
1413 handled by the case labels of the switch statement. */
1415 /* If the switch expression is a constant, we only really care
1416 about whether that constant is handled by the switch. */
1417 if (cond
&& tree_int_cst_compare (cond
, value
))
1420 /* If there is a default_node, the only relevant option is
1421 Wswitch-enum. Otherwise, if both are enabled then we prefer
1422 to warn using -Wswitch because -Wswitch is enabled by -Wall
1423 while -Wswitch-enum is explicit. */
1424 warning_at (switch_location
,
1425 (default_node
|| !warn_switch
1428 "enumeration value %qE not handled in switch",
1429 TREE_PURPOSE (chain
));
1432 /* Warn if there are case expressions that don't correspond to
1433 enumerators. This can occur since C and C++ don't enforce
1434 type-checking of assignments to enumeration variables.
1436 The time complexity here is now always O(N) worst case, since
1437 we should have marked both the lower bound and upper bound of
1438 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
1439 above. This scan also resets those fields. */
1441 splay_tree_foreach (cases
, match_case_to_enum
, type
);
1444 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
1445 expression, because B will always be true. */
1448 warn_for_omitted_condop (location_t location
, tree cond
)
1450 /* In C++ template declarations it can happen that the type is dependent
1451 and not yet known, thus TREE_TYPE (cond) == NULL_TREE. */
1452 if (truth_value_p (TREE_CODE (cond
))
1453 || (TREE_TYPE (cond
) != NULL_TREE
1454 && TREE_CODE (TREE_TYPE (cond
)) == BOOLEAN_TYPE
))
1455 warning_at (location
, OPT_Wparentheses
,
1456 "the omitted middle operand in ?: will always be %<true%>, "
1457 "suggest explicit middle operand");
1460 /* Give an error for storing into ARG, which is 'const'. USE indicates
1461 how ARG was being used. */
1464 readonly_error (location_t loc
, tree arg
, enum lvalue_use use
)
1466 gcc_assert (use
== lv_assign
|| use
== lv_increment
|| use
== lv_decrement
1468 /* Using this macro rather than (for example) arrays of messages
1469 ensures that all the format strings are checked at compile
1471 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
1472 : (use == lv_increment ? (I) \
1473 : (use == lv_decrement ? (D) : (AS))))
1474 if (TREE_CODE (arg
) == COMPONENT_REF
)
1476 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg
, 0))))
1477 error_at (loc
, READONLY_MSG (G_("assignment of member "
1478 "%qD in read-only object"),
1479 G_("increment of member "
1480 "%qD in read-only object"),
1481 G_("decrement of member "
1482 "%qD in read-only object"),
1483 G_("member %qD in read-only object "
1484 "used as %<asm%> output")),
1485 TREE_OPERAND (arg
, 1));
1487 error_at (loc
, READONLY_MSG (G_("assignment of read-only member %qD"),
1488 G_("increment of read-only member %qD"),
1489 G_("decrement of read-only member %qD"),
1490 G_("read-only member %qD used as %<asm%> output")),
1491 TREE_OPERAND (arg
, 1));
1493 else if (VAR_P (arg
))
1494 error_at (loc
, READONLY_MSG (G_("assignment of read-only variable %qD"),
1495 G_("increment of read-only variable %qD"),
1496 G_("decrement of read-only variable %qD"),
1497 G_("read-only variable %qD used as %<asm%> output")),
1499 else if (TREE_CODE (arg
) == PARM_DECL
)
1500 error_at (loc
, READONLY_MSG (G_("assignment of read-only parameter %qD"),
1501 G_("increment of read-only parameter %qD"),
1502 G_("decrement of read-only parameter %qD"),
1503 G_("read-only parameter %qD use as %<asm%> output")),
1505 else if (TREE_CODE (arg
) == RESULT_DECL
)
1507 gcc_assert (c_dialect_cxx ());
1508 error_at (loc
, READONLY_MSG (G_("assignment of "
1509 "read-only named return value %qD"),
1511 "read-only named return value %qD"),
1513 "read-only named return value %qD"),
1514 G_("read-only named return value %qD "
1515 "used as %<asm%>output")),
1518 else if (TREE_CODE (arg
) == FUNCTION_DECL
)
1519 error_at (loc
, READONLY_MSG (G_("assignment of function %qD"),
1520 G_("increment of function %qD"),
1521 G_("decrement of function %qD"),
1522 G_("function %qD used as %<asm%> output")),
1525 error_at (loc
, READONLY_MSG (G_("assignment of read-only location %qE"),
1526 G_("increment of read-only location %qE"),
1527 G_("decrement of read-only location %qE"),
1528 G_("read-only location %qE used as %<asm%> output")),
1532 /* Print an error message for an invalid lvalue. USE says
1533 how the lvalue is being used and so selects the error message. LOC
1534 is the location for the error. */
1537 lvalue_error (location_t loc
, enum lvalue_use use
)
1542 error_at (loc
, "lvalue required as left operand of assignment");
1545 error_at (loc
, "lvalue required as increment operand");
1548 error_at (loc
, "lvalue required as decrement operand");
1551 error_at (loc
, "lvalue required as unary %<&%> operand");
1554 error_at (loc
, "lvalue required in asm statement");
1561 /* Print an error message for an invalid indirection of type TYPE.
1562 ERRSTRING is the name of the operator for the indirection. */
1565 invalid_indirection_error (location_t loc
, tree type
, ref_operator errstring
)
1570 gcc_assert (c_dialect_cxx ());
1571 error_at (loc
, "invalid type argument (have %qT)", type
);
1573 case RO_ARRAY_INDEXING
:
1575 "invalid type argument of array indexing (have %qT)",
1580 "invalid type argument of unary %<*%> (have %qT)",
1585 "invalid type argument of %<->%> (have %qT)",
1590 "invalid type argument of %<->*%> (have %qT)",
1593 case RO_IMPLICIT_CONVERSION
:
1595 "invalid type argument of implicit conversion (have %qT)",
1603 /* Subscripting with type char is likely to lose on a machine where
1604 chars are signed. So warn on any machine, but optionally. Don't
1605 warn for unsigned char since that type is safe. Don't warn for
1606 signed char because anyone who uses that must have done so
1607 deliberately. Furthermore, we reduce the false positive load by
1608 warning only for non-constant value of type char. */
1611 warn_array_subscript_with_type_char (location_t loc
, tree index
)
1613 if (TYPE_MAIN_VARIANT (TREE_TYPE (index
)) == char_type_node
1614 && TREE_CODE (index
) != INTEGER_CST
)
1615 warning_at (loc
, OPT_Wchar_subscripts
,
1616 "array subscript has type %<char%>");
1619 /* Implement -Wparentheses for the unexpected C precedence rules, to
1620 cover cases like x + y << z which readers are likely to
1621 misinterpret. We have seen an expression in which CODE is a binary
1622 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
1623 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
1624 CODE_RIGHT may be ERROR_MARK, which means that that side of the
1625 expression was not formed using a binary or unary operator, or it
1626 was enclosed in parentheses. */
1629 warn_about_parentheses (location_t loc
, enum tree_code code
,
1630 enum tree_code code_left
, tree arg_left
,
1631 enum tree_code code_right
, tree arg_right
)
1633 if (!warn_parentheses
)
1636 /* This macro tests that the expression ARG with original tree code
1637 CODE appears to be a boolean expression. or the result of folding a
1638 boolean expression. */
1639 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
1640 (truth_value_p (TREE_CODE (ARG)) \
1641 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
1642 /* Folding may create 0 or 1 integers from other expressions. */ \
1643 || ((CODE) != INTEGER_CST \
1644 && (integer_onep (ARG) || integer_zerop (ARG))))
1649 if (code_left
== PLUS_EXPR
)
1650 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1651 "suggest parentheses around %<+%> inside %<<<%>");
1652 else if (code_right
== PLUS_EXPR
)
1653 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1654 "suggest parentheses around %<+%> inside %<<<%>");
1655 else if (code_left
== MINUS_EXPR
)
1656 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1657 "suggest parentheses around %<-%> inside %<<<%>");
1658 else if (code_right
== MINUS_EXPR
)
1659 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1660 "suggest parentheses around %<-%> inside %<<<%>");
1664 if (code_left
== PLUS_EXPR
)
1665 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1666 "suggest parentheses around %<+%> inside %<>>%>");
1667 else if (code_right
== PLUS_EXPR
)
1668 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1669 "suggest parentheses around %<+%> inside %<>>%>");
1670 else if (code_left
== MINUS_EXPR
)
1671 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1672 "suggest parentheses around %<-%> inside %<>>%>");
1673 else if (code_right
== MINUS_EXPR
)
1674 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1675 "suggest parentheses around %<-%> inside %<>>%>");
1678 case TRUTH_ORIF_EXPR
:
1679 if (code_left
== TRUTH_ANDIF_EXPR
)
1680 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1681 "suggest parentheses around %<&&%> within %<||%>");
1682 else if (code_right
== TRUTH_ANDIF_EXPR
)
1683 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1684 "suggest parentheses around %<&&%> within %<||%>");
1688 if (code_left
== BIT_AND_EXPR
|| code_left
== BIT_XOR_EXPR
1689 || code_left
== PLUS_EXPR
|| code_left
== MINUS_EXPR
)
1690 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1691 "suggest parentheses around arithmetic in operand of %<|%>");
1692 else if (code_right
== BIT_AND_EXPR
|| code_right
== BIT_XOR_EXPR
1693 || code_right
== PLUS_EXPR
|| code_right
== MINUS_EXPR
)
1694 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1695 "suggest parentheses around arithmetic in operand of %<|%>");
1696 /* Check cases like x|y==z */
1697 else if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1698 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1699 "suggest parentheses around comparison in operand of %<|%>");
1700 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1701 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1702 "suggest parentheses around comparison in operand of %<|%>");
1703 /* Check cases like !x | y */
1704 else if (code_left
== TRUTH_NOT_EXPR
1705 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right
, arg_right
))
1706 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1707 "suggest parentheses around operand of "
1708 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
1712 if (code_left
== BIT_AND_EXPR
1713 || code_left
== PLUS_EXPR
|| code_left
== MINUS_EXPR
)
1714 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1715 "suggest parentheses around arithmetic in operand of %<^%>");
1716 else if (code_right
== BIT_AND_EXPR
1717 || code_right
== PLUS_EXPR
|| code_right
== MINUS_EXPR
)
1718 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1719 "suggest parentheses around arithmetic in operand of %<^%>");
1720 /* Check cases like x^y==z */
1721 else if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1722 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1723 "suggest parentheses around comparison in operand of %<^%>");
1724 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1725 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1726 "suggest parentheses around comparison in operand of %<^%>");
1730 if (code_left
== PLUS_EXPR
)
1731 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1732 "suggest parentheses around %<+%> in operand of %<&%>");
1733 else if (code_right
== PLUS_EXPR
)
1734 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1735 "suggest parentheses around %<+%> in operand of %<&%>");
1736 else if (code_left
== MINUS_EXPR
)
1737 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1738 "suggest parentheses around %<-%> in operand of %<&%>");
1739 else if (code_right
== MINUS_EXPR
)
1740 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1741 "suggest parentheses around %<-%> in operand of %<&%>");
1742 /* Check cases like x&y==z */
1743 else if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1744 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1745 "suggest parentheses around comparison in operand of %<&%>");
1746 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1747 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1748 "suggest parentheses around comparison in operand of %<&%>");
1749 /* Check cases like !x & y */
1750 else if (code_left
== TRUTH_NOT_EXPR
1751 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right
, arg_right
))
1752 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1753 "suggest parentheses around operand of "
1754 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
1758 if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1759 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1760 "suggest parentheses around comparison in operand of %<==%>");
1761 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1762 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1763 "suggest parentheses around comparison in operand of %<==%>");
1766 if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1767 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1768 "suggest parentheses around comparison in operand of %<!=%>");
1769 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1770 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1771 "suggest parentheses around comparison in operand of %<!=%>");
1775 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
1777 if (TREE_CODE_CLASS (code_left
) == tcc_comparison
1778 && code_left
!= NE_EXPR
&& code_left
!= EQ_EXPR
1779 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left
)))
1780 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1781 "comparisons like %<X<=Y<=Z%> do not "
1782 "have their mathematical meaning");
1783 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
1784 && code_right
!= NE_EXPR
&& code_right
!= EQ_EXPR
1785 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right
)))
1786 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1787 "comparisons like %<X<=Y<=Z%> do not "
1788 "have their mathematical meaning");
1792 #undef NOT_A_BOOLEAN_EXPR_P
1795 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
1798 warn_for_unused_label (tree label
)
1800 if (!TREE_USED (label
))
1802 if (DECL_INITIAL (label
))
1803 warning (OPT_Wunused_label
, "label %q+D defined but not used", label
);
1805 warning (OPT_Wunused_label
, "label %q+D declared but not defined", label
);
1807 else if (asan_sanitize_use_after_scope ())
1809 if (asan_used_labels
== NULL
)
1810 asan_used_labels
= new hash_set
<tree
> (16);
1812 asan_used_labels
->add (label
);
1816 /* Warn for division by zero according to the value of DIVISOR. LOC
1817 is the location of the division operator. */
1820 warn_for_div_by_zero (location_t loc
, tree divisor
)
1822 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
1823 about division by zero. Do not issue a warning if DIVISOR has a
1824 floating-point type, since we consider 0.0/0.0 a valid way of
1825 generating a NaN. */
1826 if (c_inhibit_evaluation_warnings
== 0
1827 && (integer_zerop (divisor
) || fixed_zerop (divisor
)))
1828 warning_at (loc
, OPT_Wdiv_by_zero
, "division by zero");
1831 /* Warn for patterns where memset appears to be used incorrectly. The
1832 warning location should be LOC. ARG0, and ARG2 are the first and
1833 last arguments to the call, while LITERAL_ZERO_MASK has a 1 bit for
1834 each argument that was a literal zero. */
1837 warn_for_memset (location_t loc
, tree arg0
, tree arg2
,
1838 int literal_zero_mask
)
1840 if (warn_memset_transposed_args
1841 && integer_zerop (arg2
)
1842 && (literal_zero_mask
& (1 << 2)) != 0
1843 && (literal_zero_mask
& (1 << 1)) == 0)
1844 warning_at (loc
, OPT_Wmemset_transposed_args
,
1845 "%<memset%> used with constant zero length "
1846 "parameter; this could be due to transposed "
1849 if (warn_memset_elt_size
&& TREE_CODE (arg2
) == INTEGER_CST
)
1852 if (TREE_CODE (arg0
) == ADDR_EXPR
)
1853 arg0
= TREE_OPERAND (arg0
, 0);
1854 tree type
= TREE_TYPE (arg0
);
1855 if (type
!= NULL_TREE
&& TREE_CODE (type
) == ARRAY_TYPE
)
1857 tree elt_type
= TREE_TYPE (type
);
1858 tree domain
= TYPE_DOMAIN (type
);
1859 if (!integer_onep (TYPE_SIZE_UNIT (elt_type
))
1860 && domain
!= NULL_TREE
1861 && TYPE_MAX_VALUE (domain
)
1862 && TYPE_MIN_VALUE (domain
)
1863 && integer_zerop (TYPE_MIN_VALUE (domain
))
1864 && integer_onep (fold_build2 (MINUS_EXPR
, domain
,
1866 TYPE_MAX_VALUE (domain
))))
1867 warning_at (loc
, OPT_Wmemset_elt_size
,
1868 "%<memset%> used with length equal to "
1869 "number of elements without multiplication "
1875 /* Subroutine of build_binary_op. Give warnings for comparisons
1876 between signed and unsigned quantities that may fail. Do the
1877 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
1878 so that casts will be considered, but default promotions won't
1881 LOCATION is the location of the comparison operator.
1883 The arguments of this function map directly to local variables
1884 of build_binary_op. */
1887 warn_for_sign_compare (location_t location
,
1888 tree orig_op0
, tree orig_op1
,
1890 tree result_type
, enum tree_code resultcode
)
1892 int op0_signed
= !TYPE_UNSIGNED (TREE_TYPE (orig_op0
));
1893 int op1_signed
= !TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
1894 int unsignedp0
, unsignedp1
;
1896 /* In C++, check for comparison of different enum types. */
1898 && TREE_CODE (TREE_TYPE (orig_op0
)) == ENUMERAL_TYPE
1899 && TREE_CODE (TREE_TYPE (orig_op1
)) == ENUMERAL_TYPE
1900 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0
))
1901 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1
)))
1903 warning_at (location
,
1904 OPT_Wsign_compare
, "comparison between types %qT and %qT",
1905 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
));
1908 /* Do not warn if the comparison is being done in a signed type,
1909 since the signed type will only be chosen if it can represent
1910 all the values of the unsigned type. */
1911 if (!TYPE_UNSIGNED (result_type
))
1913 /* Do not warn if both operands are unsigned. */
1914 else if (op0_signed
== op1_signed
)
1918 tree sop
, uop
, base_type
;
1922 sop
= orig_op0
, uop
= orig_op1
;
1924 sop
= orig_op1
, uop
= orig_op0
;
1926 STRIP_TYPE_NOPS (sop
);
1927 STRIP_TYPE_NOPS (uop
);
1928 base_type
= (TREE_CODE (result_type
) == COMPLEX_TYPE
1929 ? TREE_TYPE (result_type
) : result_type
);
1931 /* Do not warn if the signed quantity is an unsuffixed integer
1932 literal (or some static constant expression involving such
1933 literals or a conditional expression involving such literals)
1934 and it is non-negative. */
1935 if (tree_expr_nonnegative_warnv_p (sop
, &ovf
))
1937 /* Do not warn if the comparison is an equality operation, the
1938 unsigned quantity is an integral constant, and it would fit
1939 in the result if the result were signed. */
1940 else if (TREE_CODE (uop
) == INTEGER_CST
1941 && (resultcode
== EQ_EXPR
|| resultcode
== NE_EXPR
)
1942 && int_fits_type_p (uop
, c_common_signed_type (base_type
)))
1944 /* In C, do not warn if the unsigned quantity is an enumeration
1945 constant and its maximum value would fit in the result if the
1946 result were signed. */
1947 else if (!c_dialect_cxx() && TREE_CODE (uop
) == INTEGER_CST
1948 && TREE_CODE (TREE_TYPE (uop
)) == ENUMERAL_TYPE
1949 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop
)),
1950 c_common_signed_type (base_type
)))
1953 warning_at (location
, OPT_Wsign_compare
,
1954 "comparison of integer expressions of different "
1955 "signedness: %qT and %qT", TREE_TYPE (orig_op0
),
1956 TREE_TYPE (orig_op1
));
1959 /* Warn if two unsigned values are being compared in a size larger
1960 than their original size, and one (and only one) is the result of
1961 a `~' operator. This comparison will always fail.
1963 Also warn if one operand is a constant, and the constant does not
1964 have all bits set that are set in the ~ operand when it is
1967 op0
= c_common_get_narrower (op0
, &unsignedp0
);
1968 op1
= c_common_get_narrower (op1
, &unsignedp1
);
1970 if ((TREE_CODE (op0
) == BIT_NOT_EXPR
)
1971 ^ (TREE_CODE (op1
) == BIT_NOT_EXPR
))
1973 if (TREE_CODE (op0
) == BIT_NOT_EXPR
)
1974 op0
= c_common_get_narrower (TREE_OPERAND (op0
, 0), &unsignedp0
);
1975 if (TREE_CODE (op1
) == BIT_NOT_EXPR
)
1976 op1
= c_common_get_narrower (TREE_OPERAND (op1
, 0), &unsignedp1
);
1978 if (tree_fits_shwi_p (op0
) || tree_fits_shwi_p (op1
))
1981 HOST_WIDE_INT constant
, mask
;
1985 if (tree_fits_shwi_p (op0
))
1988 unsignedp
= unsignedp1
;
1989 constant
= tree_to_shwi (op0
);
1994 unsignedp
= unsignedp0
;
1995 constant
= tree_to_shwi (op1
);
1998 bits
= TYPE_PRECISION (TREE_TYPE (primop
));
1999 if (bits
< TYPE_PRECISION (result_type
)
2000 && bits
< HOST_BITS_PER_LONG
&& unsignedp
)
2002 mask
= HOST_WIDE_INT_M1U
<< bits
;
2003 if ((mask
& constant
) != mask
)
2006 warning_at (location
, OPT_Wsign_compare
,
2007 "promoted ~unsigned is always non-zero");
2009 warning_at (location
, OPT_Wsign_compare
,
2010 "comparison of promoted ~unsigned with constant");
2014 else if (unsignedp0
&& unsignedp1
2015 && (TYPE_PRECISION (TREE_TYPE (op0
))
2016 < TYPE_PRECISION (result_type
))
2017 && (TYPE_PRECISION (TREE_TYPE (op1
))
2018 < TYPE_PRECISION (result_type
)))
2019 warning_at (location
, OPT_Wsign_compare
,
2020 "comparison of promoted ~unsigned with unsigned");
2024 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
2025 type via c_common_type. If -Wdouble-promotion is in use, and the
2026 conditions for warning have been met, issue a warning. GMSGID is
2027 the warning message. It must have two %T specifiers for the type
2028 that was converted (generally "float") and the type to which it was
2029 converted (generally "double), respectively. LOC is the location
2030 to which the warning should refer. */
2033 do_warn_double_promotion (tree result_type
, tree type1
, tree type2
,
2034 const char *gmsgid
, location_t loc
)
2038 if (!warn_double_promotion
)
2040 /* If the conversion will not occur at run-time, there is no need to
2042 if (c_inhibit_evaluation_warnings
)
2044 /* If an invalid conversion has occured, don't warn. */
2045 if (result_type
== error_mark_node
)
2047 if (TYPE_MAIN_VARIANT (result_type
) != double_type_node
2048 && TYPE_MAIN_VARIANT (result_type
) != complex_double_type_node
)
2050 if (TYPE_MAIN_VARIANT (type1
) == float_type_node
2051 || TYPE_MAIN_VARIANT (type1
) == complex_float_type_node
)
2052 source_type
= type1
;
2053 else if (TYPE_MAIN_VARIANT (type2
) == float_type_node
2054 || TYPE_MAIN_VARIANT (type2
) == complex_float_type_node
)
2055 source_type
= type2
;
2058 warning_at (loc
, OPT_Wdouble_promotion
, gmsgid
, source_type
, result_type
);
2061 /* Possibly warn about unused parameters. */
2064 do_warn_unused_parameter (tree fn
)
2068 for (decl
= DECL_ARGUMENTS (fn
);
2069 decl
; decl
= DECL_CHAIN (decl
))
2070 if (!TREE_USED (decl
) && TREE_CODE (decl
) == PARM_DECL
2071 && DECL_NAME (decl
) && !DECL_ARTIFICIAL (decl
)
2072 && !TREE_NO_WARNING (decl
))
2073 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wunused_parameter
,
2074 "unused parameter %qD", decl
);
2077 /* If DECL is a typedef that is declared in the current function,
2078 record it for the purpose of -Wunused-local-typedefs. */
2081 record_locally_defined_typedef (tree decl
)
2083 struct c_language_function
*l
;
2085 if (!warn_unused_local_typedefs
2087 /* if this is not a locally defined typedef then we are not
2089 || !is_typedef_decl (decl
)
2090 || !decl_function_context (decl
))
2093 l
= (struct c_language_function
*) cfun
->language
;
2094 vec_safe_push (l
->local_typedefs
, decl
);
2097 /* If T is a TYPE_DECL declared locally, mark it as used. */
2100 maybe_record_typedef_use (tree t
)
2102 if (!is_typedef_decl (t
))
2105 TREE_USED (t
) = true;
2108 /* Warn if there are some unused locally defined typedefs in the
2109 current function. */
2112 maybe_warn_unused_local_typedefs (void)
2116 /* The number of times we have emitted -Wunused-local-typedefs
2117 warnings. If this is different from errorcount, that means some
2118 unrelated errors have been issued. In which case, we'll avoid
2119 emitting "unused-local-typedefs" warnings. */
2120 static int unused_local_typedefs_warn_count
;
2121 struct c_language_function
*l
;
2126 if ((l
= (struct c_language_function
*) cfun
->language
) == NULL
)
2129 if (warn_unused_local_typedefs
2130 && errorcount
== unused_local_typedefs_warn_count
)
2132 FOR_EACH_VEC_SAFE_ELT (l
->local_typedefs
, i
, decl
)
2133 if (!TREE_USED (decl
))
2134 warning_at (DECL_SOURCE_LOCATION (decl
),
2135 OPT_Wunused_local_typedefs
,
2136 "typedef %qD locally defined but not used", decl
);
2137 unused_local_typedefs_warn_count
= errorcount
;
2140 vec_free (l
->local_typedefs
);
2143 /* If we're creating an if-else-if condition chain, first see if we
2144 already have this COND in the CHAIN. If so, warn and don't add COND
2145 into the vector, otherwise add the COND there. LOC is the location
2149 warn_duplicated_cond_add_or_warn (location_t loc
, tree cond
, vec
<tree
> **chain
)
2151 /* No chain has been created yet. Do nothing. */
2155 if (TREE_SIDE_EFFECTS (cond
))
2157 /* Uh-oh! This condition has a side-effect, thus invalidates
2167 FOR_EACH_VEC_ELT (**chain
, ix
, t
)
2168 if (operand_equal_p (cond
, t
, 0))
2170 if (warning_at (loc
, OPT_Wduplicated_cond
,
2171 "duplicated %<if%> condition"))
2172 inform (EXPR_LOCATION (t
), "previously used here");
2178 && !CONSTANT_CLASS_P (cond
)
2179 /* Don't infinitely grow the chain. */
2180 && (*chain
)->length () < 512)
2181 (*chain
)->safe_push (cond
);
2184 /* Check and possibly warn if two declarations have contradictory
2185 attributes, such as always_inline vs. noinline. */
2188 diagnose_mismatched_attributes (tree olddecl
, tree newdecl
)
2190 bool warned
= false;
2192 tree a1
= lookup_attribute ("optimize", DECL_ATTRIBUTES (olddecl
));
2193 tree a2
= lookup_attribute ("optimize", DECL_ATTRIBUTES (newdecl
));
2194 /* An optimization attribute applied on a declaration after the
2195 definition is likely not what the user wanted. */
2197 && DECL_SAVED_TREE (olddecl
) != NULL_TREE
2198 && (a1
== NULL_TREE
|| !attribute_list_equal (a1
, a2
)))
2199 warned
|= warning (OPT_Wattributes
,
2200 "optimization attribute on %qD follows "
2201 "definition but the attribute doesn%'t match",
2204 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2205 if (DECL_DECLARED_INLINE_P (newdecl
)
2206 && DECL_UNINLINABLE (olddecl
)
2207 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl
)))
2208 warned
|= warning (OPT_Wattributes
, "inline declaration of %qD follows "
2209 "declaration with attribute noinline", newdecl
);
2210 else if (DECL_DECLARED_INLINE_P (olddecl
)
2211 && DECL_UNINLINABLE (newdecl
)
2212 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl
)))
2213 warned
|= warning (OPT_Wattributes
, "declaration of %q+D with attribute "
2214 "noinline follows inline declaration ", newdecl
);
2215 else if (lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl
))
2216 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (olddecl
)))
2217 warned
|= warning (OPT_Wattributes
, "declaration of %q+D with attribute "
2218 "%qs follows declaration with attribute %qs",
2219 newdecl
, "noinline", "always_inline");
2220 else if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (newdecl
))
2221 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl
)))
2222 warned
|= warning (OPT_Wattributes
, "declaration of %q+D with attribute "
2223 "%qs follows declaration with attribute %qs",
2224 newdecl
, "always_inline", "noinline");
2225 else if (lookup_attribute ("cold", DECL_ATTRIBUTES (newdecl
))
2226 && lookup_attribute ("hot", DECL_ATTRIBUTES (olddecl
)))
2227 warned
|= warning (OPT_Wattributes
, "declaration of %q+D with attribute "
2228 "%qs follows declaration with attribute %qs",
2229 newdecl
, "cold", "hot");
2230 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (newdecl
))
2231 && lookup_attribute ("cold", DECL_ATTRIBUTES (olddecl
)))
2232 warned
|= warning (OPT_Wattributes
, "declaration of %q+D with attribute "
2233 "%qs follows declaration with attribute %qs",
2234 newdecl
, "hot", "cold");
2238 /* Warn if signed left shift overflows. We don't warn
2239 about left-shifting 1 into the sign bit in C++14; cf.
2240 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
2241 LOC is a location of the shift; OP0 and OP1 are the operands.
2242 Return true if an overflow is detected, false otherwise. */
2245 maybe_warn_shift_overflow (location_t loc
, tree op0
, tree op1
)
2247 if (TREE_CODE (op0
) != INTEGER_CST
2248 || TREE_CODE (op1
) != INTEGER_CST
)
2251 tree type0
= TREE_TYPE (op0
);
2252 unsigned int prec0
= TYPE_PRECISION (type0
);
2254 /* Left-hand operand must be signed. */
2255 if (TYPE_UNSIGNED (type0
))
2258 unsigned int min_prec
= (wi::min_precision (op0
, SIGNED
)
2259 + TREE_INT_CST_LOW (op1
));
2260 /* Handle the case of left-shifting 1 into the sign bit.
2261 * However, shifting 1 _out_ of the sign bit, as in
2262 * INT_MIN << 1, is considered an overflow.
2264 if (!tree_int_cst_sign_bit(op0
) && min_prec
== prec0
+ 1)
2266 /* Never warn for C++14 onwards. */
2267 if (cxx_dialect
>= cxx14
)
2269 /* Otherwise only if -Wshift-overflow=2. But return
2270 true to signal an overflow for the sake of integer
2271 constant expressions. */
2272 if (warn_shift_overflow
< 2)
2276 bool overflowed
= min_prec
> prec0
;
2277 if (overflowed
&& c_inhibit_evaluation_warnings
== 0)
2278 warning_at (loc
, OPT_Wshift_overflow_
,
2279 "result of %qE requires %u bits to represent, "
2280 "but %qT only has %u bits",
2281 build2_loc (loc
, LSHIFT_EXPR
, type0
, op0
, op1
),
2282 min_prec
, type0
, prec0
);
2287 /* Warn about boolean expression compared with an integer value different
2288 from true/false. Warns also e.g. about "(i1 == i2) == 2".
2289 LOC is the location of the comparison, CODE is its code, OP0 and OP1
2290 are the operands of the comparison. The caller must ensure that
2291 either operand is a boolean expression. */
2294 maybe_warn_bool_compare (location_t loc
, enum tree_code code
, tree op0
,
2297 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
2301 if (f
= fold_for_warn (op0
),
2302 TREE_CODE (f
) == INTEGER_CST
)
2304 else if (f
= fold_for_warn (op1
),
2305 TREE_CODE (f
) == INTEGER_CST
)
2310 if (!integer_zerop (cst
) && !integer_onep (cst
))
2312 int sign
= (TREE_CODE (op0
) == INTEGER_CST
2313 ? tree_int_cst_sgn (cst
) : -tree_int_cst_sgn (cst
));
2315 || ((code
== GT_EXPR
|| code
== GE_EXPR
) && sign
< 0)
2316 || ((code
== LT_EXPR
|| code
== LE_EXPR
) && sign
> 0))
2317 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2318 "with boolean expression is always false", cst
);
2320 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2321 "with boolean expression is always true", cst
);
2323 else if (integer_zerop (cst
) || integer_onep (cst
))
2325 /* If the non-constant operand isn't of a boolean type, we
2326 don't want to warn here. */
2327 tree noncst
= TREE_CODE (op0
) == INTEGER_CST
? op1
: op0
;
2328 /* Handle booleans promoted to integers. */
2329 if (bool_promoted_to_int_p (noncst
))
2331 else if (TREE_CODE (TREE_TYPE (noncst
)) != BOOLEAN_TYPE
2332 && !truth_value_p (TREE_CODE (noncst
)))
2334 /* Do some magic to get the right diagnostics. */
2335 bool flag
= TREE_CODE (op0
) == INTEGER_CST
;
2336 flag
= integer_zerop (cst
) ? flag
: !flag
;
2337 if ((code
== GE_EXPR
&& !flag
) || (code
== LE_EXPR
&& flag
))
2338 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2339 "with boolean expression is always true", cst
);
2340 else if ((code
== LT_EXPR
&& !flag
) || (code
== GT_EXPR
&& flag
))
2341 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2342 "with boolean expression is always false", cst
);
2346 /* Warn if an argument at position param_pos is passed to a
2347 restrict-qualified param, and it aliases with another argument. */
2350 warn_for_restrict (unsigned param_pos
, tree
*argarray
, unsigned nargs
)
2352 tree arg
= argarray
[param_pos
];
2353 if (TREE_VISITED (arg
) || integer_zerop (arg
))
2356 location_t loc
= EXPR_LOC_OR_LOC (arg
, input_location
);
2357 gcc_rich_location
richloc (loc
);
2360 auto_vec
<int, 16> arg_positions
;
2362 for (i
= 0; i
< nargs
; i
++)
2367 tree current_arg
= argarray
[i
];
2368 if (operand_equal_p (arg
, current_arg
, 0))
2370 TREE_VISITED (current_arg
) = 1;
2371 arg_positions
.safe_push (i
+ 1);
2375 if (arg_positions
.is_empty ())
2379 FOR_EACH_VEC_ELT (arg_positions
, i
, pos
)
2381 arg
= argarray
[pos
- 1];
2382 if (EXPR_HAS_LOCATION (arg
))
2383 richloc
.add_range (EXPR_LOCATION (arg
), false);
2386 warning_at_rich_loc_n (&richloc
, OPT_Wrestrict
, arg_positions
.length (),
2387 "passing argument %i to restrict-qualified parameter"
2388 " aliases with argument %Z",
2389 "passing argument %i to restrict-qualified parameter"
2390 " aliases with arguments %Z",
2391 param_pos
+ 1, arg_positions
.address (),
2392 arg_positions
.length ());
2395 /* Callback function to determine whether an expression TP or one of its
2396 subexpressions comes from macro expansion. Used to suppress bogus
2400 expr_from_macro_expansion_r (tree
*tp
, int *, void *)
2402 if (CAN_HAVE_LOCATION_P (*tp
)
2403 && from_macro_expansion_at (EXPR_LOCATION (*tp
)))
2404 return integer_zero_node
;
2409 /* Possibly warn when an if-else has identical branches. */
2412 do_warn_duplicated_branches (tree expr
)
2414 tree thenb
= COND_EXPR_THEN (expr
);
2415 tree elseb
= COND_EXPR_ELSE (expr
);
2417 /* Don't bother if any of the branches is missing. */
2418 if (thenb
== NULL_TREE
|| elseb
== NULL_TREE
)
2421 /* And don't warn for empty statements. */
2422 if (TREE_CODE (thenb
) == NOP_EXPR
2423 && TREE_TYPE (thenb
) == void_type_node
2424 && TREE_OPERAND (thenb
, 0) == size_zero_node
)
2427 /* ... or empty branches. */
2428 if (TREE_CODE (thenb
) == STATEMENT_LIST
2429 && STATEMENT_LIST_HEAD (thenb
) == NULL
)
2432 /* Compute the hash of the then branch. */
2433 inchash::hash
hstate0 (0);
2434 inchash::add_expr (thenb
, hstate0
);
2435 hashval_t h0
= hstate0
.end ();
2437 /* Compute the hash of the else branch. */
2438 inchash::hash
hstate1 (0);
2439 inchash::add_expr (elseb
, hstate1
);
2440 hashval_t h1
= hstate1
.end ();
2442 /* Compare the hashes. */
2444 && operand_equal_p (thenb
, elseb
, OEP_LEXICOGRAPHIC
)
2445 /* Don't warn if any of the branches or their subexpressions comes
2447 && !walk_tree_without_duplicates (&thenb
, expr_from_macro_expansion_r
,
2449 && !walk_tree_without_duplicates (&elseb
, expr_from_macro_expansion_r
,
2451 warning_at (EXPR_LOCATION (expr
), OPT_Wduplicated_branches
,
2452 "this condition has identical branches");
2455 /* Callback for c_genericize to implement -Wduplicated-branches. */
2458 do_warn_duplicated_branches_r (tree
*tp
, int *, void *)
2460 if (TREE_CODE (*tp
) == COND_EXPR
)
2461 do_warn_duplicated_branches (*tp
);
2465 /* Implementation of -Wmultistatement-macros. This warning warns about
2466 cases when a macro expands to multiple statements not wrapped in
2467 do {} while (0) or ({ }) and is used as a body of if/else/for/while
2468 conditionals. For example,
2470 #define DOIT x++; y++
2475 will increment y unconditionally.
2477 BODY_LOC is the location of the first token in the body after labels
2478 have been parsed, NEXT_LOC is the location of the next token after the
2479 body of the conditional has been parsed, and GUARD_LOC is the location
2480 of the conditional. */
2483 warn_for_multistatement_macros (location_t body_loc
, location_t next_loc
,
2484 location_t guard_loc
, enum rid keyword
)
2486 if (!warn_multistatement_macros
)
2489 /* Ain't got time to waste. We only care about macros here. */
2490 if (!from_macro_expansion_at (body_loc
)
2491 || !from_macro_expansion_at (next_loc
))
2494 /* Let's skip macros defined in system headers. */
2495 if (in_system_header_at (body_loc
)
2496 || in_system_header_at (next_loc
))
2499 /* Find the actual tokens in the macro definition. BODY_LOC and
2500 NEXT_LOC have to come from the same spelling location, but they
2501 will resolve to different locations in the context of the macro
2503 location_t body_loc_exp
2504 = linemap_resolve_location (line_table
, body_loc
,
2505 LRK_MACRO_DEFINITION_LOCATION
, NULL
);
2506 location_t next_loc_exp
2507 = linemap_resolve_location (line_table
, next_loc
,
2508 LRK_MACRO_DEFINITION_LOCATION
, NULL
);
2509 location_t guard_loc_exp
2510 = linemap_resolve_location (line_table
, guard_loc
,
2511 LRK_MACRO_DEFINITION_LOCATION
, NULL
);
2513 /* These are some funky cases we don't want to warn about. */
2514 if (body_loc_exp
== guard_loc_exp
2515 || next_loc_exp
== guard_loc_exp
2516 || body_loc_exp
== next_loc_exp
)
2519 /* Find the macro maps for the macro expansions. */
2520 const line_map
*body_map
= linemap_lookup (line_table
, body_loc
);
2521 const line_map
*next_map
= linemap_lookup (line_table
, next_loc
);
2522 const line_map
*guard_map
= linemap_lookup (line_table
, guard_loc
);
2524 /* Now see if the following token (after the body) is coming from the
2525 same macro expansion. If it is, it might be a problem. */
2526 if (body_map
!= next_map
)
2529 /* The conditional itself must not come from the same expansion, because
2530 we don't want to warn about
2531 #define IF if (x) x++; y++
2533 if (guard_map
== body_map
)
2536 /* Handle the case where NEXT and BODY come from the same expansion while
2537 GUARD doesn't, yet we shouldn't warn. E.g.
2539 #define GUARD if (...)
2540 #define GUARD2 GUARD
2542 and in the definition of another macro:
2548 while (linemap_macro_expansion_map_p (guard_map
))
2550 const line_map_macro
*mm
= linemap_check_macro (guard_map
);
2551 guard_loc_exp
= MACRO_MAP_EXPANSION_POINT_LOCATION (mm
);
2552 guard_map
= linemap_lookup (line_table
, guard_loc_exp
);
2553 if (guard_map
== body_map
)
2557 if (warning_at (body_loc
, OPT_Wmultistatement_macros
,
2558 "macro expands to multiple statements"))
2559 inform (guard_loc
, "some parts of macro expansion are not guarded by "
2560 "this %qs clause", guard_tinfo_to_string (keyword
));