1 /* Diagnostic routines shared by all languages that are variants of C.
2 Copyright (C) 1992-2016 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"
32 #include "gcc-rich-location.h"
34 /* Print a warning if a constant expression had overflow in folding.
35 Invoke this function on every expression that the language
36 requires to be a constant expression.
37 Note the ANSI C standard says it is erroneous for a
38 constant expression to overflow. */
41 constant_expression_warning (tree value
)
43 if (warn_overflow
&& pedantic
44 && (TREE_CODE (value
) == INTEGER_CST
|| TREE_CODE (value
) == REAL_CST
45 || TREE_CODE (value
) == FIXED_CST
46 || TREE_CODE (value
) == VECTOR_CST
47 || TREE_CODE (value
) == COMPLEX_CST
)
48 && TREE_OVERFLOW (value
))
49 pedwarn (input_location
, OPT_Woverflow
, "overflow in constant expression");
52 /* The same as above but print an unconditional error. */
55 constant_expression_error (tree value
)
57 if ((TREE_CODE (value
) == INTEGER_CST
|| TREE_CODE (value
) == REAL_CST
58 || TREE_CODE (value
) == FIXED_CST
59 || TREE_CODE (value
) == VECTOR_CST
60 || TREE_CODE (value
) == COMPLEX_CST
)
61 && TREE_OVERFLOW (value
))
62 error ("overflow in constant expression");
65 /* Print a warning if an expression had overflow in folding and its
68 Invoke this function on every expression that
69 (1) appears in the source code, and
70 (2) is a constant expression that overflowed, and
71 (3) is not already checked by convert_and_check;
72 however, do not invoke this function on operands of explicit casts
73 or when the expression is the result of an operator and any operand
74 already overflowed. */
77 overflow_warning (location_t loc
, tree value
)
79 if (c_inhibit_evaluation_warnings
!= 0)
82 switch (TREE_CODE (value
))
85 warning_at (loc
, OPT_Woverflow
, "integer overflow in expression");
89 warning_at (loc
, OPT_Woverflow
,
90 "floating point overflow in expression");
94 warning_at (loc
, OPT_Woverflow
, "fixed-point overflow in expression");
98 warning_at (loc
, OPT_Woverflow
, "vector overflow in expression");
102 if (TREE_CODE (TREE_REALPART (value
)) == INTEGER_CST
)
103 warning_at (loc
, OPT_Woverflow
,
104 "complex integer overflow in expression");
105 else if (TREE_CODE (TREE_REALPART (value
)) == REAL_CST
)
106 warning_at (loc
, OPT_Woverflow
,
107 "complex floating point overflow in expression");
115 /* Warn about uses of logical || / && operator in a context where it
116 is likely that the bitwise equivalent was intended by the
117 programmer. We have seen an expression in which CODE is a binary
118 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
119 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
122 warn_logical_operator (location_t location
, enum tree_code code
, tree type
,
123 enum tree_code code_left
, tree op_left
,
124 enum tree_code
ARG_UNUSED (code_right
), tree op_right
)
126 int or_op
= (code
== TRUTH_ORIF_EXPR
|| code
== TRUTH_OR_EXPR
);
127 int in0_p
, in1_p
, in_p
;
128 tree low0
, low1
, low
, high0
, high1
, high
, lhs
, rhs
, tem
;
129 bool strict_overflow_p
= false;
131 if (code
!= TRUTH_ANDIF_EXPR
132 && code
!= TRUTH_AND_EXPR
133 && code
!= TRUTH_ORIF_EXPR
134 && code
!= TRUTH_OR_EXPR
)
137 /* We don't want to warn if either operand comes from a macro
138 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
140 if (from_macro_expansion_at (EXPR_LOCATION (op_left
))
141 || from_macro_expansion_at (EXPR_LOCATION (op_right
)))
144 /* Warn if &&/|| are being used in a context where it is
145 likely that the bitwise equivalent was intended by the
146 programmer. That is, an expression such as op && MASK
147 where op should not be any boolean expression, nor a
148 constant, and mask seems to be a non-boolean integer constant. */
149 if (TREE_CODE (op_right
) == CONST_DECL
)
150 /* An enumerator counts as a constant. */
151 op_right
= DECL_INITIAL (op_right
);
152 if (!truth_value_p (code_left
)
153 && INTEGRAL_TYPE_P (TREE_TYPE (op_left
))
154 && !CONSTANT_CLASS_P (op_left
)
155 && !TREE_NO_WARNING (op_left
)
156 && TREE_CODE (op_right
) == INTEGER_CST
157 && !integer_zerop (op_right
)
158 && !integer_onep (op_right
))
161 warning_at (location
, OPT_Wlogical_op
, "logical %<or%>"
162 " applied to non-boolean constant");
164 warning_at (location
, OPT_Wlogical_op
, "logical %<and%>"
165 " applied to non-boolean constant");
166 TREE_NO_WARNING (op_left
) = true;
170 /* We do not warn for constants because they are typical of macro
171 expansions that test for features. */
172 if (CONSTANT_CLASS_P (fold_for_warn (op_left
))
173 || CONSTANT_CLASS_P (fold_for_warn (op_right
)))
176 /* This warning only makes sense with logical operands. */
177 if (!(truth_value_p (TREE_CODE (op_left
))
178 || INTEGRAL_TYPE_P (TREE_TYPE (op_left
)))
179 || !(truth_value_p (TREE_CODE (op_right
))
180 || INTEGRAL_TYPE_P (TREE_TYPE (op_right
))))
183 /* The range computations only work with scalars. */
184 if (VECTOR_TYPE_P (TREE_TYPE (op_left
))
185 || VECTOR_TYPE_P (TREE_TYPE (op_right
)))
188 /* We first test whether either side separately is trivially true
189 (with OR) or trivially false (with AND). If so, do not warn.
190 This is a common idiom for testing ranges of data types in
192 lhs
= make_range (op_left
, &in0_p
, &low0
, &high0
, &strict_overflow_p
);
195 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
196 lhs
= C_MAYBE_CONST_EXPR_EXPR (lhs
);
198 /* If this is an OR operation, invert both sides; now, the result
199 should be always false to get a warning. */
203 tem
= build_range_check (UNKNOWN_LOCATION
, type
, lhs
, in0_p
, low0
, high0
);
204 if (tem
&& integer_zerop (tem
))
207 rhs
= make_range (op_right
, &in1_p
, &low1
, &high1
, &strict_overflow_p
);
210 if (TREE_CODE (rhs
) == C_MAYBE_CONST_EXPR
)
211 rhs
= C_MAYBE_CONST_EXPR_EXPR (rhs
);
213 /* If this is an OR operation, invert both sides; now, the result
214 should be always false to get a warning. */
218 tem
= build_range_check (UNKNOWN_LOCATION
, type
, rhs
, in1_p
, low1
, high1
);
219 if (tem
&& integer_zerop (tem
))
222 /* If both expressions have the same operand, if we can merge the
224 if (operand_equal_p (lhs
, rhs
, 0)
225 && merge_ranges (&in_p
, &low
, &high
, in0_p
, low0
, high0
,
228 tem
= build_range_check (UNKNOWN_LOCATION
, type
, lhs
, in_p
, low
, high
);
229 /* ... and if the range test is always false, then warn. */
230 if (tem
&& integer_zerop (tem
))
233 warning_at (location
, OPT_Wlogical_op
,
234 "logical %<or%> of collectively exhaustive tests is "
237 warning_at (location
, OPT_Wlogical_op
,
238 "logical %<and%> of mutually exclusive tests is "
241 /* Or warn if the operands have exactly the same range, e.g.
243 else if (tree_int_cst_equal (low0
, low1
)
244 && tree_int_cst_equal (high0
, high1
))
247 warning_at (location
, OPT_Wlogical_op
,
248 "logical %<or%> of equal expressions");
250 warning_at (location
, OPT_Wlogical_op
,
251 "logical %<and%> of equal expressions");
256 /* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
257 with constant indices. */
260 find_array_ref_with_const_idx_r (tree
*expr_p
, int *, void *)
264 if ((TREE_CODE (expr
) == ARRAY_REF
265 || TREE_CODE (expr
) == ARRAY_RANGE_REF
)
266 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
)
267 return integer_type_node
;
272 /* Warn if a self-comparison always evaluates to true or false. LOC
273 is the location of the comparison with code CODE, LHS and RHS are
274 operands of the comparison. */
277 warn_tautological_cmp (location_t loc
, enum tree_code code
, tree lhs
, tree rhs
)
279 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
282 /* Don't warn for various macro expansions. */
283 if (from_macro_expansion_at (loc
)
284 || from_macro_expansion_at (EXPR_LOCATION (lhs
))
285 || from_macro_expansion_at (EXPR_LOCATION (rhs
)))
288 /* We do not warn for constants because they are typical of macro
289 expansions that test for features, sizeof, and similar. */
290 if (CONSTANT_CLASS_P (fold_for_warn (lhs
))
291 || CONSTANT_CLASS_P (fold_for_warn (rhs
)))
294 /* Don't warn for e.g.
297 if (n == (long) n) ...
299 if ((CONVERT_EXPR_P (lhs
) || TREE_CODE (lhs
) == NON_LVALUE_EXPR
)
300 || (CONVERT_EXPR_P (rhs
) || TREE_CODE (rhs
) == NON_LVALUE_EXPR
))
303 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
304 It could be a NaN, and NaN never compares equal to anything, even
306 if (FLOAT_TYPE_P (TREE_TYPE (lhs
)) || FLOAT_TYPE_P (TREE_TYPE (rhs
)))
309 if (operand_equal_p (lhs
, rhs
, 0))
311 /* Don't warn about array references with constant indices;
312 these are likely to come from a macro. */
313 if (walk_tree_without_duplicates (&lhs
, find_array_ref_with_const_idx_r
,
316 const bool always_true
= (code
== EQ_EXPR
|| code
== LE_EXPR
317 || code
== GE_EXPR
|| code
== UNLE_EXPR
318 || code
== UNGE_EXPR
|| code
== UNEQ_EXPR
);
320 warning_at (loc
, OPT_Wtautological_compare
,
321 "self-comparison always evaluates to true");
323 warning_at (loc
, OPT_Wtautological_compare
,
324 "self-comparison always evaluates to false");
328 /* Return true iff EXPR only contains boolean operands, or comparisons. */
331 expr_has_boolean_operands_p (tree expr
)
335 if (CONVERT_EXPR_P (expr
))
336 return bool_promoted_to_int_p (expr
);
337 else if (UNARY_CLASS_P (expr
))
338 return expr_has_boolean_operands_p (TREE_OPERAND (expr
, 0));
339 else if (BINARY_CLASS_P (expr
))
340 return (expr_has_boolean_operands_p (TREE_OPERAND (expr
, 0))
341 && expr_has_boolean_operands_p (TREE_OPERAND (expr
, 1)));
342 else if (COMPARISON_CLASS_P (expr
))
348 /* Warn about logical not used on the left hand side operand of a comparison.
349 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
350 Do not warn if RHS is of a boolean type, a logical operator, or
354 warn_logical_not_parentheses (location_t location
, enum tree_code code
,
357 if (TREE_CODE_CLASS (code
) != tcc_comparison
358 || TREE_TYPE (rhs
) == NULL_TREE
359 || TREE_CODE (TREE_TYPE (rhs
)) == BOOLEAN_TYPE
360 || truth_value_p (TREE_CODE (rhs
)))
363 /* Don't warn for expression like !x == ~(bool1 | bool2). */
364 if (expr_has_boolean_operands_p (rhs
))
367 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
368 !(x == 0) or !(y != 0). */
369 if ((code
== EQ_EXPR
|| code
== NE_EXPR
)
370 && integer_zerop (rhs
))
373 if (warning_at (location
, OPT_Wlogical_not_parentheses
,
374 "logical not is only applied to the left hand side of "
376 && EXPR_HAS_LOCATION (lhs
))
378 location_t lhs_loc
= EXPR_LOCATION (lhs
);
379 rich_location
richloc (line_table
, lhs_loc
);
380 richloc
.add_fixit_insert_before (lhs_loc
, "(");
381 richloc
.add_fixit_insert_after (lhs_loc
, ")");
382 inform_at_rich_loc (&richloc
, "add parentheses around left hand side "
383 "expression to silence this warning");
387 /* Warn if EXP contains any computations whose results are not used.
388 Return true if a warning is printed; false otherwise. LOCUS is the
389 (potential) location of the expression. */
392 warn_if_unused_value (const_tree exp
, location_t locus
)
395 if (TREE_USED (exp
) || TREE_NO_WARNING (exp
))
398 /* Don't warn about void constructs. This includes casting to void,
399 void function calls, and statement expressions with a final cast
401 if (VOID_TYPE_P (TREE_TYPE (exp
)))
404 if (EXPR_HAS_LOCATION (exp
))
405 locus
= EXPR_LOCATION (exp
);
407 switch (TREE_CODE (exp
))
409 case PREINCREMENT_EXPR
:
410 case POSTINCREMENT_EXPR
:
411 case PREDECREMENT_EXPR
:
412 case POSTDECREMENT_EXPR
:
418 case WITH_CLEANUP_EXPR
:
424 /* For a binding, warn if no side effect within it. */
425 exp
= BIND_EXPR_BODY (exp
);
429 case NON_LVALUE_EXPR
:
431 exp
= TREE_OPERAND (exp
, 0);
434 case TRUTH_ORIF_EXPR
:
435 case TRUTH_ANDIF_EXPR
:
436 /* In && or ||, warn if 2nd operand has no side effect. */
437 exp
= TREE_OPERAND (exp
, 1);
441 if (warn_if_unused_value (TREE_OPERAND (exp
, 0), locus
))
443 /* Let people do `(foo (), 0)' without a warning. */
444 if (TREE_CONSTANT (TREE_OPERAND (exp
, 1)))
446 exp
= TREE_OPERAND (exp
, 1);
450 /* If this is an expression with side effects, don't warn; this
451 case commonly appears in macro expansions. */
452 if (TREE_SIDE_EFFECTS (exp
))
457 /* Don't warn about automatic dereferencing of references, since
458 the user cannot control it. */
459 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp
, 0))) == REFERENCE_TYPE
)
461 exp
= TREE_OPERAND (exp
, 0);
467 /* Referencing a volatile value is a side effect, so don't warn. */
468 if ((DECL_P (exp
) || REFERENCE_CLASS_P (exp
))
469 && TREE_THIS_VOLATILE (exp
))
472 /* If this is an expression which has no operands, there is no value
473 to be unused. There are no such language-independent codes,
474 but front ends may define such. */
475 if (EXPRESSION_CLASS_P (exp
) && TREE_OPERAND_LENGTH (exp
) == 0)
479 return warning_at (locus
, OPT_Wunused_value
, "value computed is not used");
483 /* Print a warning about casts that might indicate violation
484 of strict aliasing rules if -Wstrict-aliasing is used and
485 strict aliasing mode is in effect. OTYPE is the original
486 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
489 strict_aliasing_warning (tree otype
, tree type
, tree expr
)
491 /* Strip pointer conversion chains and get to the correct original type. */
493 otype
= TREE_TYPE (expr
);
495 if (!(flag_strict_aliasing
496 && POINTER_TYPE_P (type
)
497 && POINTER_TYPE_P (otype
)
498 && !VOID_TYPE_P (TREE_TYPE (type
)))
499 /* If the type we are casting to is a ref-all pointer
500 dereferencing it is always valid. */
501 || TYPE_REF_CAN_ALIAS_ALL (type
))
504 if ((warn_strict_aliasing
> 1) && TREE_CODE (expr
) == ADDR_EXPR
505 && (DECL_P (TREE_OPERAND (expr
, 0))
506 || handled_component_p (TREE_OPERAND (expr
, 0))))
508 /* Casting the address of an object to non void pointer. Warn
509 if the cast breaks type based aliasing. */
510 if (!COMPLETE_TYPE_P (TREE_TYPE (type
)) && warn_strict_aliasing
== 2)
512 warning (OPT_Wstrict_aliasing
, "type-punning to incomplete type "
513 "might break strict-aliasing rules");
518 /* warn_strict_aliasing >= 3. This includes the default (3).
519 Only warn if the cast is dereferenced immediately. */
521 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr
, 0)));
522 alias_set_type set2
= get_alias_set (TREE_TYPE (type
));
524 if (set1
!= set2
&& set2
!= 0
526 || (!alias_set_subset_of (set2
, set1
)
527 && !alias_sets_conflict_p (set1
, set2
))))
529 warning (OPT_Wstrict_aliasing
, "dereferencing type-punned "
530 "pointer will break strict-aliasing rules");
533 else if (warn_strict_aliasing
== 2
534 && !alias_sets_must_conflict_p (set1
, set2
))
536 warning (OPT_Wstrict_aliasing
, "dereferencing type-punned "
537 "pointer might break strict-aliasing rules");
542 else if ((warn_strict_aliasing
== 1) && !VOID_TYPE_P (TREE_TYPE (otype
)))
544 /* At this level, warn for any conversions, even if an address is
545 not taken in the same statement. This will likely produce many
546 false positives, but could be useful to pinpoint problems that
547 are not revealed at higher levels. */
548 alias_set_type set1
= get_alias_set (TREE_TYPE (otype
));
549 alias_set_type set2
= get_alias_set (TREE_TYPE (type
));
550 if (!COMPLETE_TYPE_P (type
)
551 || !alias_sets_must_conflict_p (set1
, set2
))
553 warning (OPT_Wstrict_aliasing
, "dereferencing type-punned "
554 "pointer might break strict-aliasing rules");
562 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
563 sizeof as last operand of certain builtins. */
566 sizeof_pointer_memaccess_warning (location_t
*sizeof_arg_loc
, tree callee
,
567 vec
<tree
, va_gc
> *params
, tree
*sizeof_arg
,
568 bool (*comp_types
) (tree
, tree
))
570 tree type
, dest
= NULL_TREE
, src
= NULL_TREE
, tem
;
571 bool strop
= false, cmp
= false;
572 unsigned int idx
= ~0;
575 if (TREE_CODE (callee
) != FUNCTION_DECL
576 || DECL_BUILT_IN_CLASS (callee
) != BUILT_IN_NORMAL
577 || vec_safe_length (params
) <= 1)
580 switch (DECL_FUNCTION_CODE (callee
))
582 case BUILT_IN_STRNCMP
:
583 case BUILT_IN_STRNCASECMP
:
586 case BUILT_IN_STRNCPY
:
587 case BUILT_IN_STRNCPY_CHK
:
588 case BUILT_IN_STRNCAT
:
589 case BUILT_IN_STRNCAT_CHK
:
590 case BUILT_IN_STPNCPY
:
591 case BUILT_IN_STPNCPY_CHK
:
594 case BUILT_IN_MEMCPY
:
595 case BUILT_IN_MEMCPY_CHK
:
596 case BUILT_IN_MEMMOVE
:
597 case BUILT_IN_MEMMOVE_CHK
:
598 if (params
->length () < 3)
605 if (params
->length () < 3)
611 case BUILT_IN_MEMCMP
:
613 if (params
->length () < 3)
620 case BUILT_IN_MEMSET
:
621 case BUILT_IN_MEMSET_CHK
:
622 if (params
->length () < 3)
631 case BUILT_IN_STRNDUP
:
636 case BUILT_IN_MEMCHR
:
637 if (params
->length () < 3)
642 case BUILT_IN_SNPRINTF
:
643 case BUILT_IN_SNPRINTF_CHK
:
644 case BUILT_IN_VSNPRINTF
:
645 case BUILT_IN_VSNPRINTF_CHK
:
657 if (sizeof_arg
[idx
] == NULL
|| sizeof_arg
[idx
] == error_mark_node
)
660 type
= TYPE_P (sizeof_arg
[idx
])
661 ? sizeof_arg
[idx
] : TREE_TYPE (sizeof_arg
[idx
]);
662 if (!POINTER_TYPE_P (type
))
666 && (tem
= tree_strip_nop_conversions (dest
))
667 && POINTER_TYPE_P (TREE_TYPE (tem
))
668 && comp_types (TREE_TYPE (TREE_TYPE (tem
)), type
))
672 && (tem
= tree_strip_nop_conversions (src
))
673 && POINTER_TYPE_P (TREE_TYPE (tem
))
674 && comp_types (TREE_TYPE (TREE_TYPE (tem
)), type
))
677 loc
= sizeof_arg_loc
[idx
];
681 if (!TYPE_P (sizeof_arg
[idx
])
682 && operand_equal_p (dest
, sizeof_arg
[idx
], 0)
683 && comp_types (TREE_TYPE (dest
), type
))
685 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
686 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
687 "argument to %<sizeof%> in %qD call is the same "
688 "expression as the destination; did you mean to "
689 "remove the addressof?", callee
);
690 else if ((TYPE_PRECISION (TREE_TYPE (type
))
691 == TYPE_PRECISION (char_type_node
))
693 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
694 "argument to %<sizeof%> in %qD call is the same "
695 "expression as the destination; did you mean to "
696 "provide an explicit length?", callee
);
698 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
699 "argument to %<sizeof%> in %qD call is the same "
700 "expression as the destination; did you mean to "
701 "dereference it?", callee
);
705 if (POINTER_TYPE_P (TREE_TYPE (dest
))
707 && comp_types (TREE_TYPE (dest
), type
)
708 && !VOID_TYPE_P (TREE_TYPE (type
)))
710 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
711 "argument to %<sizeof%> in %qD call is the same "
712 "pointer type %qT as the destination; expected %qT "
713 "or an explicit length", callee
, TREE_TYPE (dest
),
714 TREE_TYPE (TREE_TYPE (dest
)));
721 if (!TYPE_P (sizeof_arg
[idx
])
722 && operand_equal_p (src
, sizeof_arg
[idx
], 0)
723 && comp_types (TREE_TYPE (src
), type
))
725 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
726 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
727 "argument to %<sizeof%> in %qD call is the same "
728 "expression as the source; did you mean to "
729 "remove the addressof?", callee
);
730 else if ((TYPE_PRECISION (TREE_TYPE (type
))
731 == TYPE_PRECISION (char_type_node
))
733 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
734 "argument to %<sizeof%> in %qD call is the same "
735 "expression as the source; did you mean to "
736 "provide an explicit length?", callee
);
738 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
739 "argument to %<sizeof%> in %qD call is the same "
740 "expression as the source; did you mean to "
741 "dereference it?", callee
);
745 if (POINTER_TYPE_P (TREE_TYPE (src
))
747 && comp_types (TREE_TYPE (src
), type
)
748 && !VOID_TYPE_P (TREE_TYPE (type
)))
750 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
751 "argument to %<sizeof%> in %qD call is the same "
752 "pointer type %qT as the source; expected %qT "
753 "or an explicit length", callee
, TREE_TYPE (src
),
754 TREE_TYPE (TREE_TYPE (src
)));
761 if (!TYPE_P (sizeof_arg
[idx
])
762 && operand_equal_p (dest
, sizeof_arg
[idx
], 0)
763 && comp_types (TREE_TYPE (dest
), type
))
765 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
766 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
767 "argument to %<sizeof%> in %qD call is the same "
768 "expression as the first source; did you mean to "
769 "remove the addressof?", callee
);
770 else if ((TYPE_PRECISION (TREE_TYPE (type
))
771 == TYPE_PRECISION (char_type_node
))
773 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
774 "argument to %<sizeof%> in %qD call is the same "
775 "expression as the first source; did you mean to "
776 "provide an explicit length?", callee
);
778 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
779 "argument to %<sizeof%> in %qD call is the same "
780 "expression as the first source; did you mean to "
781 "dereference it?", callee
);
785 if (POINTER_TYPE_P (TREE_TYPE (dest
))
787 && comp_types (TREE_TYPE (dest
), type
)
788 && !VOID_TYPE_P (TREE_TYPE (type
)))
790 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
791 "argument to %<sizeof%> in %qD call is the same "
792 "pointer type %qT as the first source; expected %qT "
793 "or an explicit length", callee
, TREE_TYPE (dest
),
794 TREE_TYPE (TREE_TYPE (dest
)));
801 if (!TYPE_P (sizeof_arg
[idx
])
802 && operand_equal_p (src
, sizeof_arg
[idx
], 0)
803 && comp_types (TREE_TYPE (src
), type
))
805 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
806 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
807 "argument to %<sizeof%> in %qD call is the same "
808 "expression as the second source; did you mean to "
809 "remove the addressof?", callee
);
810 else if ((TYPE_PRECISION (TREE_TYPE (type
))
811 == TYPE_PRECISION (char_type_node
))
813 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
814 "argument to %<sizeof%> in %qD call is the same "
815 "expression as the second source; did you mean to "
816 "provide an explicit length?", callee
);
818 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
819 "argument to %<sizeof%> in %qD call is the same "
820 "expression as the second source; did you mean to "
821 "dereference it?", callee
);
825 if (POINTER_TYPE_P (TREE_TYPE (src
))
827 && comp_types (TREE_TYPE (src
), type
)
828 && !VOID_TYPE_P (TREE_TYPE (type
)))
830 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
831 "argument to %<sizeof%> in %qD call is the same "
832 "pointer type %qT as the second source; expected %qT "
833 "or an explicit length", callee
, TREE_TYPE (src
),
834 TREE_TYPE (TREE_TYPE (src
)));
841 /* Warn for unlikely, improbable, or stupid DECL declarations
845 check_main_parameter_types (tree decl
)
847 function_args_iterator iter
;
851 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl
), type
, iter
)
853 /* XXX void_type_node belies the abstraction. */
854 if (type
== void_type_node
|| type
== error_mark_node
)
859 pedwarn (input_location
, OPT_Wmain
,
860 "%<_Atomic%>-qualified parameter type %qT of %q+D",
862 while (POINTER_TYPE_P (t
))
866 pedwarn (input_location
, OPT_Wmain
,
867 "%<_Atomic%>-qualified parameter type %qT of %q+D",
875 if (TYPE_MAIN_VARIANT (type
) != integer_type_node
)
876 pedwarn (input_location
, OPT_Wmain
,
877 "first argument of %q+D should be %<int%>", decl
);
881 if (TREE_CODE (type
) != POINTER_TYPE
882 || TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
883 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type
)))
885 pedwarn (input_location
, OPT_Wmain
,
886 "second argument of %q+D should be %<char **%>", decl
);
890 if (TREE_CODE (type
) != POINTER_TYPE
891 || TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
892 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type
)))
894 pedwarn (input_location
, OPT_Wmain
,
895 "third argument of %q+D should probably be "
896 "%<char **%>", decl
);
901 /* It is intentional that this message does not mention the third
902 argument because it's only mentioned in an appendix of the
904 if (argct
> 0 && (argct
< 2 || argct
> 3))
905 pedwarn (input_location
, OPT_Wmain
,
906 "%q+D takes only zero or two arguments", decl
);
908 if (stdarg_p (TREE_TYPE (decl
)))
909 pedwarn (input_location
, OPT_Wmain
,
910 "%q+D declared as variadic function", decl
);
913 /* Warns if the conversion of EXPR to TYPE may alter a value.
914 This is a helper function for warnings_for_convert_and_check. */
917 conversion_warning (location_t loc
, tree type
, tree expr
)
919 tree expr_type
= TREE_TYPE (expr
);
920 enum conversion_safety conversion_kind
;
922 if (!warn_conversion
&& !warn_sign_conversion
&& !warn_float_conversion
)
925 /* This may happen, because for LHS op= RHS we preevaluate
926 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
927 means we could no longer see the code of the EXPR. */
928 if (TREE_CODE (expr
) == C_MAYBE_CONST_EXPR
)
929 expr
= C_MAYBE_CONST_EXPR_EXPR (expr
);
930 if (TREE_CODE (expr
) == SAVE_EXPR
)
931 expr
= TREE_OPERAND (expr
, 0);
933 switch (TREE_CODE (expr
))
941 case TRUTH_ANDIF_EXPR
:
942 case TRUTH_ORIF_EXPR
:
947 /* Conversion from boolean to a signed:1 bit-field (which only
948 can hold the values 0 and -1) doesn't lose information - but
949 it does change the value. */
950 if (TYPE_PRECISION (type
) == 1 && !TYPE_UNSIGNED (type
))
951 warning_at (loc
, OPT_Wconversion
,
952 "conversion to %qT from boolean expression", type
);
958 conversion_kind
= unsafe_conversion_p (loc
, type
, expr
, true);
959 if (conversion_kind
== UNSAFE_REAL
)
960 warning_at (loc
, OPT_Wfloat_conversion
,
961 "conversion to %qT alters %qT constant value",
963 else if (conversion_kind
)
964 warning_at (loc
, OPT_Wconversion
,
965 "conversion to %qT alters %qT constant value",
971 /* In case of COND_EXPR, we do not care about the type of
972 COND_EXPR, only about the conversion of each operand. */
973 tree op1
= TREE_OPERAND (expr
, 1);
974 tree op2
= TREE_OPERAND (expr
, 2);
976 conversion_warning (loc
, type
, op1
);
977 conversion_warning (loc
, type
, op2
);
981 default: /* 'expr' is not a constant. */
982 conversion_kind
= unsafe_conversion_p (loc
, type
, expr
, true);
983 if (conversion_kind
== UNSAFE_REAL
)
984 warning_at (loc
, OPT_Wfloat_conversion
,
985 "conversion to %qT from %qT may alter its value",
987 else if (conversion_kind
== UNSAFE_IMAGINARY
)
988 warning_at (loc
, OPT_Wconversion
,
989 "conversion to %qT from %qT discards imaginary component",
991 else if (conversion_kind
)
992 warning_at (loc
, OPT_Wconversion
,
993 "conversion to %qT from %qT may alter its value",
998 /* Produce warnings after a conversion. RESULT is the result of
999 converting EXPR to TYPE. This is a helper function for
1000 convert_and_check and cp_convert_and_check. */
1003 warnings_for_convert_and_check (location_t loc
, tree type
, tree expr
,
1006 loc
= expansion_point_location_if_in_system_header (loc
);
1008 if (TREE_CODE (expr
) == INTEGER_CST
1009 && (TREE_CODE (type
) == INTEGER_TYPE
1010 || TREE_CODE (type
) == ENUMERAL_TYPE
)
1011 && !int_fits_type_p (expr
, type
))
1013 /* Do not diagnose overflow in a constant expression merely
1014 because a conversion overflowed. */
1015 if (TREE_OVERFLOW (result
))
1016 TREE_OVERFLOW (result
) = TREE_OVERFLOW (expr
);
1018 if (TYPE_UNSIGNED (type
))
1020 /* This detects cases like converting -129 or 256 to
1022 if (!int_fits_type_p (expr
, c_common_signed_type (type
)))
1023 warning_at (loc
, OPT_Woverflow
,
1024 "large integer implicitly truncated to unsigned type");
1026 conversion_warning (loc
, type
, expr
);
1028 else if (!int_fits_type_p (expr
, c_common_unsigned_type (type
)))
1029 warning_at (loc
, OPT_Woverflow
,
1030 "overflow in implicit constant conversion");
1031 /* No warning for converting 0x80000000 to int. */
1033 && (TREE_CODE (TREE_TYPE (expr
)) != INTEGER_TYPE
1034 || TYPE_PRECISION (TREE_TYPE (expr
))
1035 != TYPE_PRECISION (type
)))
1036 warning_at (loc
, OPT_Woverflow
,
1037 "overflow in implicit constant conversion");
1040 conversion_warning (loc
, type
, expr
);
1042 else if ((TREE_CODE (result
) == INTEGER_CST
1043 || TREE_CODE (result
) == FIXED_CST
) && TREE_OVERFLOW (result
))
1044 warning_at (loc
, OPT_Woverflow
,
1045 "overflow in implicit constant conversion");
1047 conversion_warning (loc
, type
, expr
);
1050 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
1051 Used to verify that case values match up with enumerator values. */
1054 match_case_to_enum_1 (tree key
, tree type
, tree label
)
1056 char buf
[WIDE_INT_PRINT_BUFFER_SIZE
];
1058 if (tree_fits_uhwi_p (key
))
1059 print_dec (key
, buf
, UNSIGNED
);
1060 else if (tree_fits_shwi_p (key
))
1061 print_dec (key
, buf
, SIGNED
);
1063 print_hex (key
, buf
);
1065 if (TYPE_NAME (type
) == 0)
1066 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label
)),
1067 warn_switch
? OPT_Wswitch
: OPT_Wswitch_enum
,
1068 "case value %qs not in enumerated type",
1071 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label
)),
1072 warn_switch
? OPT_Wswitch
: OPT_Wswitch_enum
,
1073 "case value %qs not in enumerated type %qT",
1077 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
1078 Used to verify that case values match up with enumerator values. */
1081 match_case_to_enum (splay_tree_node node
, void *data
)
1083 tree label
= (tree
) node
->value
;
1084 tree type
= (tree
) data
;
1086 /* Skip default case. */
1087 if (!CASE_LOW (label
))
1090 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
1091 when we did our enum->case scan. Reset our scratch bit after. */
1092 if (!CASE_LOW_SEEN (label
))
1093 match_case_to_enum_1 (CASE_LOW (label
), type
, label
);
1095 CASE_LOW_SEEN (label
) = 0;
1097 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
1098 not set, that means that CASE_HIGH did not appear when we did our
1099 enum->case scan. Reset our scratch bit after. */
1100 if (CASE_HIGH (label
))
1102 if (!CASE_HIGH_SEEN (label
))
1103 match_case_to_enum_1 (CASE_HIGH (label
), type
, label
);
1105 CASE_HIGH_SEEN (label
) = 0;
1111 /* Handle -Wswitch*. Called from the front end after parsing the
1112 switch construct. */
1113 /* ??? Should probably be somewhere generic, since other languages
1114 besides C and C++ would want this. At the moment, however, C/C++
1115 are the only tree-ssa languages that support enumerations at all,
1116 so the point is moot. */
1119 c_do_switch_warnings (splay_tree cases
, location_t switch_location
,
1120 tree type
, tree cond
, bool bool_cond_p
,
1121 bool outside_range_p
)
1123 splay_tree_node default_node
;
1124 splay_tree_node node
;
1127 if (!warn_switch
&& !warn_switch_enum
&& !warn_switch_default
1128 && !warn_switch_bool
)
1131 default_node
= splay_tree_lookup (cases
, (splay_tree_key
) NULL
);
1133 warning_at (switch_location
, OPT_Wswitch_default
,
1134 "switch missing default case");
1136 /* There are certain cases where -Wswitch-bool warnings aren't
1143 so be careful here. */
1144 if (warn_switch_bool
&& bool_cond_p
)
1146 splay_tree_node min_node
;
1147 /* If there's a default node, it's also the value with the minimal
1148 key. So look at the penultimate key (if any). */
1150 min_node
= splay_tree_successor (cases
, (splay_tree_key
) NULL
);
1152 min_node
= splay_tree_min (cases
);
1153 tree min
= min_node
? (tree
) min_node
->key
: NULL_TREE
;
1155 splay_tree_node max_node
= splay_tree_max (cases
);
1156 /* This might be a case range, so look at the value with the
1157 maximal key and then check CASE_HIGH. */
1158 tree max
= max_node
? (tree
) max_node
->value
: NULL_TREE
;
1160 max
= CASE_HIGH (max
) ? CASE_HIGH (max
) : CASE_LOW (max
);
1162 /* If there's a case value > 1 or < 0, that is outside bool
1165 || (max
&& wi::gts_p (max
, 1))
1166 || (min
&& wi::lts_p (min
, 0))
1174 case, where we want to warn. */
1176 && max
&& wi::eq_p (max
, 1)
1177 && min
&& wi::eq_p (min
, 0)))
1178 warning_at (switch_location
, OPT_Wswitch_bool
,
1179 "switch condition has boolean value");
1182 /* From here on, we only care about enumerated types. */
1183 if (!type
|| TREE_CODE (type
) != ENUMERAL_TYPE
)
1186 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
1187 if (!warn_switch_enum
&& !warn_switch
)
1190 /* Check the cases. Warn about case values which are not members of
1191 the enumerated type. For -Wswitch-enum, or for -Wswitch when
1192 there is no default case, check that exactly all enumeration
1193 literals are covered by the cases. */
1195 /* Clearing COND if it is not an integer constant simplifies
1196 the tests inside the loop below. */
1197 if (TREE_CODE (cond
) != INTEGER_CST
)
1200 /* The time complexity here is O(N*lg(N)) worst case, but for the
1201 common case of monotonically increasing enumerators, it is
1202 O(N), since the nature of the splay tree will keep the next
1203 element adjacent to the root at all times. */
1205 for (chain
= TYPE_VALUES (type
); chain
; chain
= TREE_CHAIN (chain
))
1207 tree value
= TREE_VALUE (chain
);
1208 if (TREE_CODE (value
) == CONST_DECL
)
1209 value
= DECL_INITIAL (value
);
1210 node
= splay_tree_lookup (cases
, (splay_tree_key
) value
);
1213 /* Mark the CASE_LOW part of the case entry as seen. */
1214 tree label
= (tree
) node
->value
;
1215 CASE_LOW_SEEN (label
) = 1;
1219 /* Even though there wasn't an exact match, there might be a
1220 case range which includes the enumerator's value. */
1221 node
= splay_tree_predecessor (cases
, (splay_tree_key
) value
);
1222 if (node
&& CASE_HIGH ((tree
) node
->value
))
1224 tree label
= (tree
) node
->value
;
1225 int cmp
= tree_int_cst_compare (CASE_HIGH (label
), value
);
1228 /* If we match the upper bound exactly, mark the CASE_HIGH
1229 part of the case entry as seen. */
1231 CASE_HIGH_SEEN (label
) = 1;
1236 /* We've now determined that this enumerated literal isn't
1237 handled by the case labels of the switch statement. */
1239 /* If the switch expression is a constant, we only really care
1240 about whether that constant is handled by the switch. */
1241 if (cond
&& tree_int_cst_compare (cond
, value
))
1244 /* If there is a default_node, the only relevant option is
1245 Wswitch-enum. Otherwise, if both are enabled then we prefer
1246 to warn using -Wswitch because -Wswitch is enabled by -Wall
1247 while -Wswitch-enum is explicit. */
1248 warning_at (switch_location
,
1249 (default_node
|| !warn_switch
1252 "enumeration value %qE not handled in switch",
1253 TREE_PURPOSE (chain
));
1256 /* Warn if there are case expressions that don't correspond to
1257 enumerators. This can occur since C and C++ don't enforce
1258 type-checking of assignments to enumeration variables.
1260 The time complexity here is now always O(N) worst case, since
1261 we should have marked both the lower bound and upper bound of
1262 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
1263 above. This scan also resets those fields. */
1265 splay_tree_foreach (cases
, match_case_to_enum
, type
);
1268 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
1269 expression, because B will always be true. */
1272 warn_for_omitted_condop (location_t location
, tree cond
)
1274 /* In C++ template declarations it can happen that the type is dependent
1275 and not yet known, thus TREE_TYPE (cond) == NULL_TREE. */
1276 if (truth_value_p (TREE_CODE (cond
))
1277 || (TREE_TYPE (cond
) != NULL_TREE
1278 && TREE_CODE (TREE_TYPE (cond
)) == BOOLEAN_TYPE
))
1279 warning_at (location
, OPT_Wparentheses
,
1280 "the omitted middle operand in ?: will always be %<true%>, "
1281 "suggest explicit middle operand");
1284 /* Give an error for storing into ARG, which is 'const'. USE indicates
1285 how ARG was being used. */
1288 readonly_error (location_t loc
, tree arg
, enum lvalue_use use
)
1290 gcc_assert (use
== lv_assign
|| use
== lv_increment
|| use
== lv_decrement
1292 /* Using this macro rather than (for example) arrays of messages
1293 ensures that all the format strings are checked at compile
1295 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
1296 : (use == lv_increment ? (I) \
1297 : (use == lv_decrement ? (D) : (AS))))
1298 if (TREE_CODE (arg
) == COMPONENT_REF
)
1300 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg
, 0))))
1301 error_at (loc
, READONLY_MSG (G_("assignment of member "
1302 "%qD in read-only object"),
1303 G_("increment of member "
1304 "%qD in read-only object"),
1305 G_("decrement of member "
1306 "%qD in read-only object"),
1307 G_("member %qD in read-only object "
1308 "used as %<asm%> output")),
1309 TREE_OPERAND (arg
, 1));
1311 error_at (loc
, READONLY_MSG (G_("assignment of read-only member %qD"),
1312 G_("increment of read-only member %qD"),
1313 G_("decrement of read-only member %qD"),
1314 G_("read-only member %qD used as %<asm%> output")),
1315 TREE_OPERAND (arg
, 1));
1317 else if (VAR_P (arg
))
1318 error_at (loc
, READONLY_MSG (G_("assignment of read-only variable %qD"),
1319 G_("increment of read-only variable %qD"),
1320 G_("decrement of read-only variable %qD"),
1321 G_("read-only variable %qD used as %<asm%> output")),
1323 else if (TREE_CODE (arg
) == PARM_DECL
)
1324 error_at (loc
, READONLY_MSG (G_("assignment of read-only parameter %qD"),
1325 G_("increment of read-only parameter %qD"),
1326 G_("decrement of read-only parameter %qD"),
1327 G_("read-only parameter %qD use as %<asm%> output")),
1329 else if (TREE_CODE (arg
) == RESULT_DECL
)
1331 gcc_assert (c_dialect_cxx ());
1332 error_at (loc
, READONLY_MSG (G_("assignment of "
1333 "read-only named return value %qD"),
1335 "read-only named return value %qD"),
1337 "read-only named return value %qD"),
1338 G_("read-only named return value %qD "
1339 "used as %<asm%>output")),
1342 else if (TREE_CODE (arg
) == FUNCTION_DECL
)
1343 error_at (loc
, READONLY_MSG (G_("assignment of function %qD"),
1344 G_("increment of function %qD"),
1345 G_("decrement of function %qD"),
1346 G_("function %qD used as %<asm%> output")),
1349 error_at (loc
, READONLY_MSG (G_("assignment of read-only location %qE"),
1350 G_("increment of read-only location %qE"),
1351 G_("decrement of read-only location %qE"),
1352 G_("read-only location %qE used as %<asm%> output")),
1356 /* Print an error message for an invalid lvalue. USE says
1357 how the lvalue is being used and so selects the error message. LOC
1358 is the location for the error. */
1361 lvalue_error (location_t loc
, enum lvalue_use use
)
1366 error_at (loc
, "lvalue required as left operand of assignment");
1369 error_at (loc
, "lvalue required as increment operand");
1372 error_at (loc
, "lvalue required as decrement operand");
1375 error_at (loc
, "lvalue required as unary %<&%> operand");
1378 error_at (loc
, "lvalue required in asm statement");
1385 /* Print an error message for an invalid indirection of type TYPE.
1386 ERRSTRING is the name of the operator for the indirection. */
1389 invalid_indirection_error (location_t loc
, tree type
, ref_operator errstring
)
1394 gcc_assert (c_dialect_cxx ());
1395 error_at (loc
, "invalid type argument (have %qT)", type
);
1397 case RO_ARRAY_INDEXING
:
1399 "invalid type argument of array indexing (have %qT)",
1404 "invalid type argument of unary %<*%> (have %qT)",
1409 "invalid type argument of %<->%> (have %qT)",
1414 "invalid type argument of %<->*%> (have %qT)",
1417 case RO_IMPLICIT_CONVERSION
:
1419 "invalid type argument of implicit conversion (have %qT)",
1427 /* Subscripting with type char is likely to lose on a machine where
1428 chars are signed. So warn on any machine, but optionally. Don't
1429 warn for unsigned char since that type is safe. Don't warn for
1430 signed char because anyone who uses that must have done so
1431 deliberately. Furthermore, we reduce the false positive load by
1432 warning only for non-constant value of type char. */
1435 warn_array_subscript_with_type_char (location_t loc
, tree index
)
1437 if (TYPE_MAIN_VARIANT (TREE_TYPE (index
)) == char_type_node
1438 && TREE_CODE (index
) != INTEGER_CST
)
1439 warning_at (loc
, OPT_Wchar_subscripts
,
1440 "array subscript has type %<char%>");
1443 /* Implement -Wparentheses for the unexpected C precedence rules, to
1444 cover cases like x + y << z which readers are likely to
1445 misinterpret. We have seen an expression in which CODE is a binary
1446 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
1447 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
1448 CODE_RIGHT may be ERROR_MARK, which means that that side of the
1449 expression was not formed using a binary or unary operator, or it
1450 was enclosed in parentheses. */
1453 warn_about_parentheses (location_t loc
, enum tree_code code
,
1454 enum tree_code code_left
, tree arg_left
,
1455 enum tree_code code_right
, tree arg_right
)
1457 if (!warn_parentheses
)
1460 /* This macro tests that the expression ARG with original tree code
1461 CODE appears to be a boolean expression. or the result of folding a
1462 boolean expression. */
1463 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
1464 (truth_value_p (TREE_CODE (ARG)) \
1465 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
1466 /* Folding may create 0 or 1 integers from other expressions. */ \
1467 || ((CODE) != INTEGER_CST \
1468 && (integer_onep (ARG) || integer_zerop (ARG))))
1473 if (code_left
== PLUS_EXPR
)
1474 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1475 "suggest parentheses around %<+%> inside %<<<%>");
1476 else if (code_right
== PLUS_EXPR
)
1477 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1478 "suggest parentheses around %<+%> inside %<<<%>");
1479 else if (code_left
== MINUS_EXPR
)
1480 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1481 "suggest parentheses around %<-%> inside %<<<%>");
1482 else if (code_right
== MINUS_EXPR
)
1483 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1484 "suggest parentheses around %<-%> inside %<<<%>");
1488 if (code_left
== PLUS_EXPR
)
1489 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1490 "suggest parentheses around %<+%> inside %<>>%>");
1491 else if (code_right
== PLUS_EXPR
)
1492 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1493 "suggest parentheses around %<+%> inside %<>>%>");
1494 else if (code_left
== MINUS_EXPR
)
1495 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1496 "suggest parentheses around %<-%> inside %<>>%>");
1497 else if (code_right
== MINUS_EXPR
)
1498 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1499 "suggest parentheses around %<-%> inside %<>>%>");
1502 case TRUTH_ORIF_EXPR
:
1503 if (code_left
== TRUTH_ANDIF_EXPR
)
1504 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1505 "suggest parentheses around %<&&%> within %<||%>");
1506 else if (code_right
== TRUTH_ANDIF_EXPR
)
1507 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1508 "suggest parentheses around %<&&%> within %<||%>");
1512 if (code_left
== BIT_AND_EXPR
|| code_left
== BIT_XOR_EXPR
1513 || code_left
== PLUS_EXPR
|| code_left
== MINUS_EXPR
)
1514 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1515 "suggest parentheses around arithmetic in operand of %<|%>");
1516 else if (code_right
== BIT_AND_EXPR
|| code_right
== BIT_XOR_EXPR
1517 || code_right
== PLUS_EXPR
|| code_right
== MINUS_EXPR
)
1518 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1519 "suggest parentheses around arithmetic in operand of %<|%>");
1520 /* Check cases like x|y==z */
1521 else if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1522 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1523 "suggest parentheses around comparison in operand of %<|%>");
1524 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1525 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1526 "suggest parentheses around comparison in operand of %<|%>");
1527 /* Check cases like !x | y */
1528 else if (code_left
== TRUTH_NOT_EXPR
1529 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right
, arg_right
))
1530 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1531 "suggest parentheses around operand of "
1532 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
1536 if (code_left
== BIT_AND_EXPR
1537 || code_left
== PLUS_EXPR
|| code_left
== MINUS_EXPR
)
1538 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1539 "suggest parentheses around arithmetic in operand of %<^%>");
1540 else if (code_right
== BIT_AND_EXPR
1541 || code_right
== PLUS_EXPR
|| code_right
== MINUS_EXPR
)
1542 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1543 "suggest parentheses around arithmetic in operand of %<^%>");
1544 /* Check cases like x^y==z */
1545 else if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1546 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1547 "suggest parentheses around comparison in operand of %<^%>");
1548 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1549 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1550 "suggest parentheses around comparison in operand of %<^%>");
1554 if (code_left
== PLUS_EXPR
)
1555 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1556 "suggest parentheses around %<+%> in operand of %<&%>");
1557 else if (code_right
== PLUS_EXPR
)
1558 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1559 "suggest parentheses around %<+%> in operand of %<&%>");
1560 else if (code_left
== MINUS_EXPR
)
1561 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1562 "suggest parentheses around %<-%> in operand of %<&%>");
1563 else if (code_right
== MINUS_EXPR
)
1564 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1565 "suggest parentheses around %<-%> in operand of %<&%>");
1566 /* Check cases like x&y==z */
1567 else if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1568 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1569 "suggest parentheses around comparison in operand of %<&%>");
1570 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1571 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1572 "suggest parentheses around comparison in operand of %<&%>");
1573 /* Check cases like !x & y */
1574 else if (code_left
== TRUTH_NOT_EXPR
1575 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right
, arg_right
))
1576 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1577 "suggest parentheses around operand of "
1578 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
1582 if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1583 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1584 "suggest parentheses around comparison in operand of %<==%>");
1585 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1586 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1587 "suggest parentheses around comparison in operand of %<==%>");
1590 if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1591 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1592 "suggest parentheses around comparison in operand of %<!=%>");
1593 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1594 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1595 "suggest parentheses around comparison in operand of %<!=%>");
1599 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
1601 if (TREE_CODE_CLASS (code_left
) == tcc_comparison
1602 && code_left
!= NE_EXPR
&& code_left
!= EQ_EXPR
1603 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left
)))
1604 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1605 "comparisons like %<X<=Y<=Z%> do not "
1606 "have their mathematical meaning");
1607 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
1608 && code_right
!= NE_EXPR
&& code_right
!= EQ_EXPR
1609 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right
)))
1610 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1611 "comparisons like %<X<=Y<=Z%> do not "
1612 "have their mathematical meaning");
1616 #undef NOT_A_BOOLEAN_EXPR_P
1619 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
1622 warn_for_unused_label (tree label
)
1624 if (!TREE_USED (label
))
1626 if (DECL_INITIAL (label
))
1627 warning (OPT_Wunused_label
, "label %q+D defined but not used", label
);
1629 warning (OPT_Wunused_label
, "label %q+D declared but not defined", label
);
1631 else if (asan_sanitize_use_after_scope ())
1633 if (asan_used_labels
== NULL
)
1634 asan_used_labels
= new hash_set
<tree
> (16);
1636 asan_used_labels
->add (label
);
1640 /* Warn for division by zero according to the value of DIVISOR. LOC
1641 is the location of the division operator. */
1644 warn_for_div_by_zero (location_t loc
, tree divisor
)
1646 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
1647 about division by zero. Do not issue a warning if DIVISOR has a
1648 floating-point type, since we consider 0.0/0.0 a valid way of
1649 generating a NaN. */
1650 if (c_inhibit_evaluation_warnings
== 0
1651 && (integer_zerop (divisor
) || fixed_zerop (divisor
)))
1652 warning_at (loc
, OPT_Wdiv_by_zero
, "division by zero");
1655 /* Warn for patterns where memset appears to be used incorrectly. The
1656 warning location should be LOC. ARG0, and ARG2 are the first and
1657 last arguments to the call, while LITERAL_ZERO_MASK has a 1 bit for
1658 each argument that was a literal zero. */
1661 warn_for_memset (location_t loc
, tree arg0
, tree arg2
,
1662 int literal_zero_mask
)
1664 if (warn_memset_transposed_args
1665 && integer_zerop (arg2
)
1666 && (literal_zero_mask
& (1 << 2)) != 0
1667 && (literal_zero_mask
& (1 << 1)) == 0)
1668 warning_at (loc
, OPT_Wmemset_transposed_args
,
1669 "%<memset%> used with constant zero length "
1670 "parameter; this could be due to transposed "
1673 if (warn_memset_elt_size
&& TREE_CODE (arg2
) == INTEGER_CST
)
1676 if (TREE_CODE (arg0
) == ADDR_EXPR
)
1677 arg0
= TREE_OPERAND (arg0
, 0);
1678 tree type
= TREE_TYPE (arg0
);
1679 if (type
!= NULL_TREE
&& TREE_CODE (type
) == ARRAY_TYPE
)
1681 tree elt_type
= TREE_TYPE (type
);
1682 tree domain
= TYPE_DOMAIN (type
);
1683 if (!integer_onep (TYPE_SIZE_UNIT (elt_type
))
1684 && domain
!= NULL_TREE
1685 && TYPE_MAXVAL (domain
)
1686 && TYPE_MINVAL (domain
)
1687 && integer_zerop (TYPE_MINVAL (domain
))
1688 && integer_onep (fold_build2 (MINUS_EXPR
, domain
,
1690 TYPE_MAXVAL (domain
))))
1691 warning_at (loc
, OPT_Wmemset_elt_size
,
1692 "%<memset%> used with length equal to "
1693 "number of elements without multiplication "
1699 /* Subroutine of build_binary_op. Give warnings for comparisons
1700 between signed and unsigned quantities that may fail. Do the
1701 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
1702 so that casts will be considered, but default promotions won't
1705 LOCATION is the location of the comparison operator.
1707 The arguments of this function map directly to local variables
1708 of build_binary_op. */
1711 warn_for_sign_compare (location_t location
,
1712 tree orig_op0
, tree orig_op1
,
1714 tree result_type
, enum tree_code resultcode
)
1716 int op0_signed
= !TYPE_UNSIGNED (TREE_TYPE (orig_op0
));
1717 int op1_signed
= !TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
1718 int unsignedp0
, unsignedp1
;
1720 /* In C++, check for comparison of different enum types. */
1722 && TREE_CODE (TREE_TYPE (orig_op0
)) == ENUMERAL_TYPE
1723 && TREE_CODE (TREE_TYPE (orig_op1
)) == ENUMERAL_TYPE
1724 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0
))
1725 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1
)))
1727 warning_at (location
,
1728 OPT_Wsign_compare
, "comparison between types %qT and %qT",
1729 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
));
1732 /* Do not warn if the comparison is being done in a signed type,
1733 since the signed type will only be chosen if it can represent
1734 all the values of the unsigned type. */
1735 if (!TYPE_UNSIGNED (result_type
))
1737 /* Do not warn if both operands are unsigned. */
1738 else if (op0_signed
== op1_signed
)
1742 tree sop
, uop
, base_type
;
1746 sop
= orig_op0
, uop
= orig_op1
;
1748 sop
= orig_op1
, uop
= orig_op0
;
1750 STRIP_TYPE_NOPS (sop
);
1751 STRIP_TYPE_NOPS (uop
);
1752 base_type
= (TREE_CODE (result_type
) == COMPLEX_TYPE
1753 ? TREE_TYPE (result_type
) : result_type
);
1755 /* Do not warn if the signed quantity is an unsuffixed integer
1756 literal (or some static constant expression involving such
1757 literals or a conditional expression involving such literals)
1758 and it is non-negative. */
1759 if (tree_expr_nonnegative_warnv_p (sop
, &ovf
))
1761 /* Do not warn if the comparison is an equality operation, the
1762 unsigned quantity is an integral constant, and it would fit
1763 in the result if the result were signed. */
1764 else if (TREE_CODE (uop
) == INTEGER_CST
1765 && (resultcode
== EQ_EXPR
|| resultcode
== NE_EXPR
)
1766 && int_fits_type_p (uop
, c_common_signed_type (base_type
)))
1768 /* In C, do not warn if the unsigned quantity is an enumeration
1769 constant and its maximum value would fit in the result if the
1770 result were signed. */
1771 else if (!c_dialect_cxx() && TREE_CODE (uop
) == INTEGER_CST
1772 && TREE_CODE (TREE_TYPE (uop
)) == ENUMERAL_TYPE
1773 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop
)),
1774 c_common_signed_type (base_type
)))
1777 warning_at (location
,
1779 "comparison between signed and unsigned integer expressions");
1782 /* Warn if two unsigned values are being compared in a size larger
1783 than their original size, and one (and only one) is the result of
1784 a `~' operator. This comparison will always fail.
1786 Also warn if one operand is a constant, and the constant does not
1787 have all bits set that are set in the ~ operand when it is
1790 op0
= c_common_get_narrower (op0
, &unsignedp0
);
1791 op1
= c_common_get_narrower (op1
, &unsignedp1
);
1793 if ((TREE_CODE (op0
) == BIT_NOT_EXPR
)
1794 ^ (TREE_CODE (op1
) == BIT_NOT_EXPR
))
1796 if (TREE_CODE (op0
) == BIT_NOT_EXPR
)
1797 op0
= c_common_get_narrower (TREE_OPERAND (op0
, 0), &unsignedp0
);
1798 if (TREE_CODE (op1
) == BIT_NOT_EXPR
)
1799 op1
= c_common_get_narrower (TREE_OPERAND (op1
, 0), &unsignedp1
);
1801 if (tree_fits_shwi_p (op0
) || tree_fits_shwi_p (op1
))
1804 HOST_WIDE_INT constant
, mask
;
1808 if (tree_fits_shwi_p (op0
))
1811 unsignedp
= unsignedp1
;
1812 constant
= tree_to_shwi (op0
);
1817 unsignedp
= unsignedp0
;
1818 constant
= tree_to_shwi (op1
);
1821 bits
= TYPE_PRECISION (TREE_TYPE (primop
));
1822 if (bits
< TYPE_PRECISION (result_type
)
1823 && bits
< HOST_BITS_PER_LONG
&& unsignedp
)
1825 mask
= HOST_WIDE_INT_M1U
<< bits
;
1826 if ((mask
& constant
) != mask
)
1829 warning_at (location
, OPT_Wsign_compare
,
1830 "promoted ~unsigned is always non-zero");
1832 warning_at (location
, OPT_Wsign_compare
,
1833 "comparison of promoted ~unsigned with constant");
1837 else if (unsignedp0
&& unsignedp1
1838 && (TYPE_PRECISION (TREE_TYPE (op0
))
1839 < TYPE_PRECISION (result_type
))
1840 && (TYPE_PRECISION (TREE_TYPE (op1
))
1841 < TYPE_PRECISION (result_type
)))
1842 warning_at (location
, OPT_Wsign_compare
,
1843 "comparison of promoted ~unsigned with unsigned");
1847 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
1848 type via c_common_type. If -Wdouble-promotion is in use, and the
1849 conditions for warning have been met, issue a warning. GMSGID is
1850 the warning message. It must have two %T specifiers for the type
1851 that was converted (generally "float") and the type to which it was
1852 converted (generally "double), respectively. LOC is the location
1853 to which the awrning should refer. */
1856 do_warn_double_promotion (tree result_type
, tree type1
, tree type2
,
1857 const char *gmsgid
, location_t loc
)
1861 if (!warn_double_promotion
)
1863 /* If the conversion will not occur at run-time, there is no need to
1865 if (c_inhibit_evaluation_warnings
)
1867 if (TYPE_MAIN_VARIANT (result_type
) != double_type_node
1868 && TYPE_MAIN_VARIANT (result_type
) != complex_double_type_node
)
1870 if (TYPE_MAIN_VARIANT (type1
) == float_type_node
1871 || TYPE_MAIN_VARIANT (type1
) == complex_float_type_node
)
1872 source_type
= type1
;
1873 else if (TYPE_MAIN_VARIANT (type2
) == float_type_node
1874 || TYPE_MAIN_VARIANT (type2
) == complex_float_type_node
)
1875 source_type
= type2
;
1878 warning_at (loc
, OPT_Wdouble_promotion
, gmsgid
, source_type
, result_type
);
1881 /* Possibly warn about unused parameters. */
1884 do_warn_unused_parameter (tree fn
)
1888 for (decl
= DECL_ARGUMENTS (fn
);
1889 decl
; decl
= DECL_CHAIN (decl
))
1890 if (!TREE_USED (decl
) && TREE_CODE (decl
) == PARM_DECL
1891 && DECL_NAME (decl
) && !DECL_ARTIFICIAL (decl
)
1892 && !TREE_NO_WARNING (decl
))
1893 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wunused_parameter
,
1894 "unused parameter %qD", decl
);
1897 /* If DECL is a typedef that is declared in the current function,
1898 record it for the purpose of -Wunused-local-typedefs. */
1901 record_locally_defined_typedef (tree decl
)
1903 struct c_language_function
*l
;
1905 if (!warn_unused_local_typedefs
1907 /* if this is not a locally defined typedef then we are not
1909 || !is_typedef_decl (decl
)
1910 || !decl_function_context (decl
))
1913 l
= (struct c_language_function
*) cfun
->language
;
1914 vec_safe_push (l
->local_typedefs
, decl
);
1917 /* If T is a TYPE_DECL declared locally, mark it as used. */
1920 maybe_record_typedef_use (tree t
)
1922 if (!is_typedef_decl (t
))
1925 TREE_USED (t
) = true;
1928 /* Warn if there are some unused locally defined typedefs in the
1929 current function. */
1932 maybe_warn_unused_local_typedefs (void)
1936 /* The number of times we have emitted -Wunused-local-typedefs
1937 warnings. If this is different from errorcount, that means some
1938 unrelated errors have been issued. In which case, we'll avoid
1939 emitting "unused-local-typedefs" warnings. */
1940 static int unused_local_typedefs_warn_count
;
1941 struct c_language_function
*l
;
1946 if ((l
= (struct c_language_function
*) cfun
->language
) == NULL
)
1949 if (warn_unused_local_typedefs
1950 && errorcount
== unused_local_typedefs_warn_count
)
1952 FOR_EACH_VEC_SAFE_ELT (l
->local_typedefs
, i
, decl
)
1953 if (!TREE_USED (decl
))
1954 warning_at (DECL_SOURCE_LOCATION (decl
),
1955 OPT_Wunused_local_typedefs
,
1956 "typedef %qD locally defined but not used", decl
);
1957 unused_local_typedefs_warn_count
= errorcount
;
1960 vec_free (l
->local_typedefs
);
1963 /* If we're creating an if-else-if condition chain, first see if we
1964 already have this COND in the CHAIN. If so, warn and don't add COND
1965 into the vector, otherwise add the COND there. LOC is the location
1969 warn_duplicated_cond_add_or_warn (location_t loc
, tree cond
, vec
<tree
> **chain
)
1971 /* No chain has been created yet. Do nothing. */
1975 if (TREE_SIDE_EFFECTS (cond
))
1977 /* Uh-oh! This condition has a side-effect, thus invalidates
1987 FOR_EACH_VEC_ELT (**chain
, ix
, t
)
1988 if (operand_equal_p (cond
, t
, 0))
1990 if (warning_at (loc
, OPT_Wduplicated_cond
,
1991 "duplicated %<if%> condition"))
1992 inform (EXPR_LOCATION (t
), "previously used here");
1998 && !CONSTANT_CLASS_P (cond
)
1999 /* Don't infinitely grow the chain. */
2000 && (*chain
)->length () < 512)
2001 (*chain
)->safe_push (cond
);
2004 /* Check and possibly warn if two declarations have contradictory
2005 attributes, such as always_inline vs. noinline. */
2008 diagnose_mismatched_attributes (tree olddecl
, tree newdecl
)
2010 bool warned
= false;
2012 tree a1
= lookup_attribute ("optimize", DECL_ATTRIBUTES (olddecl
));
2013 tree a2
= lookup_attribute ("optimize", DECL_ATTRIBUTES (newdecl
));
2014 /* An optimization attribute applied on a declaration after the
2015 definition is likely not what the user wanted. */
2017 && DECL_SAVED_TREE (olddecl
) != NULL_TREE
2018 && (a1
== NULL_TREE
|| !attribute_list_equal (a1
, a2
)))
2019 warned
|= warning (OPT_Wattributes
,
2020 "optimization attribute on %qD follows "
2021 "definition but the attribute doesn%'t match",
2024 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2025 if (DECL_DECLARED_INLINE_P (newdecl
)
2026 && DECL_UNINLINABLE (olddecl
)
2027 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl
)))
2028 warned
|= warning (OPT_Wattributes
, "inline declaration of %qD follows "
2029 "declaration with attribute noinline", newdecl
);
2030 else if (DECL_DECLARED_INLINE_P (olddecl
)
2031 && DECL_UNINLINABLE (newdecl
)
2032 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl
)))
2033 warned
|= warning (OPT_Wattributes
, "declaration of %q+D with attribute "
2034 "noinline follows inline declaration ", newdecl
);
2035 else if (lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl
))
2036 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (olddecl
)))
2037 warned
|= warning (OPT_Wattributes
, "declaration of %q+D with attribute "
2038 "%qs follows declaration with attribute %qs",
2039 newdecl
, "noinline", "always_inline");
2040 else if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (newdecl
))
2041 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl
)))
2042 warned
|= warning (OPT_Wattributes
, "declaration of %q+D with attribute "
2043 "%qs follows declaration with attribute %qs",
2044 newdecl
, "always_inline", "noinline");
2045 else if (lookup_attribute ("cold", DECL_ATTRIBUTES (newdecl
))
2046 && lookup_attribute ("hot", DECL_ATTRIBUTES (olddecl
)))
2047 warned
|= warning (OPT_Wattributes
, "declaration of %q+D with attribute "
2048 "%qs follows declaration with attribute %qs",
2049 newdecl
, "cold", "hot");
2050 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (newdecl
))
2051 && lookup_attribute ("cold", DECL_ATTRIBUTES (olddecl
)))
2052 warned
|= warning (OPT_Wattributes
, "declaration of %q+D with attribute "
2053 "%qs follows declaration with attribute %qs",
2054 newdecl
, "hot", "cold");
2058 /* Warn if signed left shift overflows. We don't warn
2059 about left-shifting 1 into the sign bit in C++14; cf.
2060 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
2061 LOC is a location of the shift; OP0 and OP1 are the operands.
2062 Return true if an overflow is detected, false otherwise. */
2065 maybe_warn_shift_overflow (location_t loc
, tree op0
, tree op1
)
2067 if (TREE_CODE (op0
) != INTEGER_CST
2068 || TREE_CODE (op1
) != INTEGER_CST
)
2071 tree type0
= TREE_TYPE (op0
);
2072 unsigned int prec0
= TYPE_PRECISION (type0
);
2074 /* Left-hand operand must be signed. */
2075 if (TYPE_UNSIGNED (type0
))
2078 unsigned int min_prec
= (wi::min_precision (op0
, SIGNED
)
2079 + TREE_INT_CST_LOW (op1
));
2080 /* Handle the case of left-shifting 1 into the sign bit.
2081 * However, shifting 1 _out_ of the sign bit, as in
2082 * INT_MIN << 1, is considered an overflow.
2084 if (!tree_int_cst_sign_bit(op0
) && min_prec
== prec0
+ 1)
2086 /* Never warn for C++14 onwards. */
2087 if (cxx_dialect
>= cxx14
)
2089 /* Otherwise only if -Wshift-overflow=2. But return
2090 true to signal an overflow for the sake of integer
2091 constant expressions. */
2092 if (warn_shift_overflow
< 2)
2096 bool overflowed
= min_prec
> prec0
;
2097 if (overflowed
&& c_inhibit_evaluation_warnings
== 0)
2098 warning_at (loc
, OPT_Wshift_overflow_
,
2099 "result of %qE requires %u bits to represent, "
2100 "but %qT only has %u bits",
2101 build2_loc (loc
, LSHIFT_EXPR
, type0
, op0
, op1
),
2102 min_prec
, type0
, prec0
);
2107 /* Warn about boolean expression compared with an integer value different
2108 from true/false. Warns also e.g. about "(i1 == i2) == 2".
2109 LOC is the location of the comparison, CODE is its code, OP0 and OP1
2110 are the operands of the comparison. The caller must ensure that
2111 either operand is a boolean expression. */
2114 maybe_warn_bool_compare (location_t loc
, enum tree_code code
, tree op0
,
2117 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
2121 if (f
= fold_for_warn (op0
),
2122 TREE_CODE (f
) == INTEGER_CST
)
2124 else if (f
= fold_for_warn (op1
),
2125 TREE_CODE (f
) == INTEGER_CST
)
2130 if (!integer_zerop (cst
) && !integer_onep (cst
))
2132 int sign
= (TREE_CODE (op0
) == INTEGER_CST
2133 ? tree_int_cst_sgn (cst
) : -tree_int_cst_sgn (cst
));
2135 || ((code
== GT_EXPR
|| code
== GE_EXPR
) && sign
< 0)
2136 || ((code
== LT_EXPR
|| code
== LE_EXPR
) && sign
> 0))
2137 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2138 "with boolean expression is always false", cst
);
2140 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2141 "with boolean expression is always true", cst
);
2143 else if (integer_zerop (cst
) || integer_onep (cst
))
2145 /* If the non-constant operand isn't of a boolean type, we
2146 don't want to warn here. */
2147 tree noncst
= TREE_CODE (op0
) == INTEGER_CST
? op1
: op0
;
2148 /* Handle booleans promoted to integers. */
2149 if (bool_promoted_to_int_p (noncst
))
2151 else if (TREE_CODE (TREE_TYPE (noncst
)) != BOOLEAN_TYPE
2152 && !truth_value_p (TREE_CODE (noncst
)))
2154 /* Do some magic to get the right diagnostics. */
2155 bool flag
= TREE_CODE (op0
) == INTEGER_CST
;
2156 flag
= integer_zerop (cst
) ? flag
: !flag
;
2157 if ((code
== GE_EXPR
&& !flag
) || (code
== LE_EXPR
&& flag
))
2158 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2159 "with boolean expression is always true", cst
);
2160 else if ((code
== LT_EXPR
&& !flag
) || (code
== GT_EXPR
&& flag
))
2161 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2162 "with boolean expression is always false", cst
);
2166 /* Warn if an argument at position param_pos is passed to a
2167 restrict-qualified param, and it aliases with another argument. */
2170 warn_for_restrict (unsigned param_pos
, vec
<tree
, va_gc
> *args
)
2172 tree arg
= (*args
)[param_pos
];
2173 if (TREE_VISITED (arg
) || operand_equal_p (arg
, null_pointer_node
, 0))
2176 location_t loc
= EXPR_LOC_OR_LOC (arg
, input_location
);
2177 gcc_rich_location
richloc (loc
);
2181 int *arg_positions
= XNEWVEC (int, args
->length ());
2182 unsigned arg_positions_len
= 0;
2184 FOR_EACH_VEC_ELT (*args
, i
, current_arg
)
2189 tree current_arg
= (*args
)[i
];
2190 if (operand_equal_p (arg
, current_arg
, 0))
2192 TREE_VISITED (current_arg
) = 1;
2193 arg_positions
[arg_positions_len
++] = (i
+ 1);
2197 if (arg_positions_len
== 0)
2199 free (arg_positions
);
2203 for (unsigned i
= 0; i
< arg_positions_len
; i
++)
2205 unsigned pos
= arg_positions
[i
];
2206 tree arg
= (*args
)[pos
- 1];
2207 if (EXPR_HAS_LOCATION (arg
))
2208 richloc
.add_range (EXPR_LOCATION (arg
), false);
2211 warning_at_rich_loc_n (&richloc
, OPT_Wrestrict
, arg_positions_len
,
2212 "passing argument %i to restrict-qualified parameter"
2213 " aliases with argument %Z",
2214 "passing argument %i to restrict-qualified parameter"
2215 " aliases with arguments %Z",
2216 param_pos
+ 1, arg_positions
, arg_positions_len
);
2218 free (arg_positions
);