1 /* Diagnostic routines shared by all languages that are variants of C.
2 Copyright (C) 1992-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
29 #include "diagnostic.h"
31 #include "stringpool.h"
34 #include "gcc-rich-location.h"
36 #include "c-family/c-indentation.h"
39 /* Print a warning if a constant expression had overflow in folding.
40 Invoke this function on every expression that the language
41 requires to be a constant expression.
42 Note the ANSI C standard says it is erroneous for a
43 constant expression to overflow. */
46 constant_expression_warning (tree value
)
48 if (warn_overflow
&& pedantic
49 && (TREE_CODE (value
) == INTEGER_CST
|| TREE_CODE (value
) == REAL_CST
50 || TREE_CODE (value
) == FIXED_CST
51 || TREE_CODE (value
) == VECTOR_CST
52 || TREE_CODE (value
) == COMPLEX_CST
)
53 && TREE_OVERFLOW (value
))
54 pedwarn (input_location
, OPT_Woverflow
, "overflow in constant expression");
57 /* The same as above but print an unconditional error. */
60 constant_expression_error (tree value
)
62 if ((TREE_CODE (value
) == INTEGER_CST
|| TREE_CODE (value
) == REAL_CST
63 || TREE_CODE (value
) == FIXED_CST
64 || TREE_CODE (value
) == VECTOR_CST
65 || TREE_CODE (value
) == COMPLEX_CST
)
66 && TREE_OVERFLOW (value
))
67 error ("overflow in constant expression");
70 /* Print a warning if an expression result VALUE had an overflow
71 in folding and its operands hadn't. EXPR, which may be null, is
72 the operand of the expression.
74 Invoke this function on every expression that
75 (1) appears in the source code, and
76 (2) is a constant expression that overflowed, and
77 (3) is not already checked by convert_and_check;
78 however, do not invoke this function on operands of explicit casts
79 or when the expression is the result of an operator and any operand
80 already overflowed. */
83 overflow_warning (location_t loc
, tree value
, tree expr
)
85 if (c_inhibit_evaluation_warnings
!= 0)
88 const char *warnfmt
= NULL
;
90 switch (TREE_CODE (value
))
94 ? G_("integer overflow in expression %qE of type %qT "
96 : G_("integer overflow in expression of type %qT "
102 ? G_("floating point overflow in expression %qE "
103 "of type %qT results in %qE")
104 : G_("floating point overflow in expression of type %qT "
110 ? G_("fixed-point overflow in expression %qE of type %qT "
112 : G_("fixed-point overflow in expression of type %qT "
118 ? G_("vector overflow in expression %qE of type %qT "
120 : G_("vector overflow in expression of type %qT "
125 if (TREE_CODE (TREE_REALPART (value
)) == INTEGER_CST
)
127 ? G_("complex integer overflow in expression %qE "
128 "of type %qT results in %qE")
129 : G_("complex integer overflow in expression of type %qT "
131 else if (TREE_CODE (TREE_REALPART (value
)) == REAL_CST
)
133 ? G_("complex floating point overflow in expression %qE "
134 "of type %qT results in %qE")
135 : G_("complex floating point overflow in expression "
136 "of type %qT results in %qE"));
146 warning_at (loc
, OPT_Woverflow
, warnfmt
, expr
, TREE_TYPE (expr
), value
);
148 warning_at (loc
, OPT_Woverflow
, warnfmt
, TREE_TYPE (value
), value
);
150 TREE_NO_WARNING (value
) = 1;
153 /* Helper function for walk_tree. Unwrap C_MAYBE_CONST_EXPRs in an expression
157 unwrap_c_maybe_const (tree
*tp
, int *walk_subtrees
, void *)
159 if (TREE_CODE (*tp
) == C_MAYBE_CONST_EXPR
)
161 *tp
= C_MAYBE_CONST_EXPR_EXPR (*tp
);
162 /* C_MAYBE_CONST_EXPRs don't nest. */
163 *walk_subtrees
= false;
168 /* Warn about uses of logical || / && operator in a context where it
169 is likely that the bitwise equivalent was intended by the
170 programmer. We have seen an expression in which CODE is a binary
171 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
172 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
175 warn_logical_operator (location_t location
, enum tree_code code
, tree type
,
176 enum tree_code code_left
, tree op_left
,
177 enum tree_code
ARG_UNUSED (code_right
), tree op_right
)
179 int or_op
= (code
== TRUTH_ORIF_EXPR
|| code
== TRUTH_OR_EXPR
);
180 int in0_p
, in1_p
, in_p
;
181 tree low0
, low1
, low
, high0
, high1
, high
, lhs
, rhs
, tem
;
182 bool strict_overflow_p
= false;
184 if (!warn_logical_op
)
187 if (code
!= TRUTH_ANDIF_EXPR
188 && code
!= TRUTH_AND_EXPR
189 && code
!= TRUTH_ORIF_EXPR
190 && code
!= TRUTH_OR_EXPR
)
193 /* We don't want to warn if either operand comes from a macro
194 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
196 if (from_macro_expansion_at (EXPR_LOCATION (op_left
))
197 || from_macro_expansion_at (EXPR_LOCATION (op_right
)))
200 /* Warn if &&/|| are being used in a context where it is
201 likely that the bitwise equivalent was intended by the
202 programmer. That is, an expression such as op && MASK
203 where op should not be any boolean expression, nor a
204 constant, and mask seems to be a non-boolean integer constant. */
205 if (TREE_CODE (op_right
) == CONST_DECL
)
206 /* An enumerator counts as a constant. */
207 op_right
= DECL_INITIAL (op_right
);
208 if (!truth_value_p (code_left
)
209 && INTEGRAL_TYPE_P (TREE_TYPE (op_left
))
210 && !CONSTANT_CLASS_P (op_left
)
211 && !TREE_NO_WARNING (op_left
)
212 && TREE_CODE (op_right
) == INTEGER_CST
213 && !integer_zerop (op_right
)
214 && !integer_onep (op_right
))
217 warning_at (location
, OPT_Wlogical_op
, "logical %<or%>"
218 " applied to non-boolean constant");
220 warning_at (location
, OPT_Wlogical_op
, "logical %<and%>"
221 " applied to non-boolean constant");
222 TREE_NO_WARNING (op_left
) = true;
226 /* We do not warn for constants because they are typical of macro
227 expansions that test for features. */
228 if (CONSTANT_CLASS_P (fold_for_warn (op_left
))
229 || CONSTANT_CLASS_P (fold_for_warn (op_right
)))
232 /* This warning only makes sense with logical operands. */
233 if (!(truth_value_p (TREE_CODE (op_left
))
234 || INTEGRAL_TYPE_P (TREE_TYPE (op_left
)))
235 || !(truth_value_p (TREE_CODE (op_right
))
236 || INTEGRAL_TYPE_P (TREE_TYPE (op_right
))))
239 /* The range computations only work with scalars. */
240 if (VECTOR_TYPE_P (TREE_TYPE (op_left
))
241 || VECTOR_TYPE_P (TREE_TYPE (op_right
)))
244 /* We first test whether either side separately is trivially true
245 (with OR) or trivially false (with AND). If so, do not warn.
246 This is a common idiom for testing ranges of data types in
248 op_left
= unshare_expr (op_left
);
249 walk_tree_without_duplicates (&op_left
, unwrap_c_maybe_const
, NULL
);
250 lhs
= make_range (op_left
, &in0_p
, &low0
, &high0
, &strict_overflow_p
);
254 /* If this is an OR operation, invert both sides; now, the result
255 should be always false to get a warning. */
259 tem
= build_range_check (UNKNOWN_LOCATION
, type
, lhs
, in0_p
, low0
, high0
);
260 if (tem
&& integer_zerop (tem
))
263 op_right
= unshare_expr (op_right
);
264 walk_tree_without_duplicates (&op_right
, unwrap_c_maybe_const
, NULL
);
265 rhs
= make_range (op_right
, &in1_p
, &low1
, &high1
, &strict_overflow_p
);
269 /* If this is an OR operation, invert both sides; now, the result
270 should be always false to get a warning. */
274 tem
= build_range_check (UNKNOWN_LOCATION
, type
, rhs
, in1_p
, low1
, high1
);
275 if (tem
&& integer_zerop (tem
))
278 /* If both expressions have the same operand, if we can merge the
280 if (operand_equal_p (lhs
, rhs
, 0)
281 && merge_ranges (&in_p
, &low
, &high
, in0_p
, low0
, high0
,
284 tem
= build_range_check (UNKNOWN_LOCATION
, type
, lhs
, in_p
, low
, high
);
285 /* ... and if the range test is always false, then warn. */
286 if (tem
&& integer_zerop (tem
))
289 warning_at (location
, OPT_Wlogical_op
,
290 "logical %<or%> of collectively exhaustive tests is "
293 warning_at (location
, OPT_Wlogical_op
,
294 "logical %<and%> of mutually exclusive tests is "
297 /* Or warn if the operands have exactly the same range, e.g.
299 else if (tree_int_cst_equal (low0
, low1
)
300 && tree_int_cst_equal (high0
, high1
))
303 warning_at (location
, OPT_Wlogical_op
,
304 "logical %<or%> of equal expressions");
306 warning_at (location
, OPT_Wlogical_op
,
307 "logical %<and%> of equal expressions");
312 /* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
313 with constant indices. */
316 find_array_ref_with_const_idx_r (tree
*expr_p
, int *, void *)
320 if ((TREE_CODE (expr
) == ARRAY_REF
321 || TREE_CODE (expr
) == ARRAY_RANGE_REF
)
322 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
)
323 return integer_type_node
;
328 /* Subroutine of warn_tautological_cmp. Warn about bitwise comparison
329 that always evaluate to true or false. LOC is the location of the
330 ==/!= comparison specified by CODE; LHS and RHS are the usual operands
331 of this comparison. */
334 warn_tautological_bitwise_comparison (location_t loc
, tree_code code
,
337 if (code
!= EQ_EXPR
&& code
!= NE_EXPR
)
340 /* Extract the operands from e.g. (x & 8) == 4. */
343 if ((TREE_CODE (lhs
) == BIT_AND_EXPR
344 || TREE_CODE (lhs
) == BIT_IOR_EXPR
)
345 && TREE_CODE (rhs
) == INTEGER_CST
)
346 bitop
= lhs
, cst
= rhs
;
347 else if ((TREE_CODE (rhs
) == BIT_AND_EXPR
348 || TREE_CODE (rhs
) == BIT_IOR_EXPR
)
349 && TREE_CODE (lhs
) == INTEGER_CST
)
350 bitop
= rhs
, cst
= lhs
;
355 if (TREE_CODE (TREE_OPERAND (bitop
, 0)) == INTEGER_CST
)
356 bitopcst
= TREE_OPERAND (bitop
, 0);
357 else if (TREE_CODE (TREE_OPERAND (bitop
, 1)) == INTEGER_CST
)
358 bitopcst
= TREE_OPERAND (bitop
, 1);
362 /* Note that the two operands are from before the usual integer
363 conversions, so their types might not be the same.
364 Use the larger of the two precisions and ignore bits outside
366 int prec
= MAX (TYPE_PRECISION (TREE_TYPE (cst
)),
367 TYPE_PRECISION (TREE_TYPE (bitopcst
)));
369 wide_int bitopcstw
= wi::to_wide (bitopcst
, prec
);
370 wide_int cstw
= wi::to_wide (cst
, prec
);
373 if (TREE_CODE (bitop
) == BIT_AND_EXPR
)
374 res
= bitopcstw
& cstw
;
376 res
= bitopcstw
| cstw
;
378 /* For BIT_AND only warn if (CST2 & CST1) != CST1, and
379 for BIT_OR only if (CST2 | CST1) != CST1. */
384 warning_at (loc
, OPT_Wtautological_compare
,
385 "bitwise comparison always evaluates to false");
387 warning_at (loc
, OPT_Wtautological_compare
,
388 "bitwise comparison always evaluates to true");
391 /* Warn if a self-comparison always evaluates to true or false. LOC
392 is the location of the comparison with code CODE, LHS and RHS are
393 operands of the comparison. */
396 warn_tautological_cmp (location_t loc
, enum tree_code code
, tree lhs
, tree rhs
)
398 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
401 /* Don't warn for various macro expansions. */
402 if (from_macro_expansion_at (loc
)
403 || from_macro_expansion_at (EXPR_LOCATION (lhs
))
404 || from_macro_expansion_at (EXPR_LOCATION (rhs
)))
407 warn_tautological_bitwise_comparison (loc
, code
, lhs
, rhs
);
409 /* We do not warn for constants because they are typical of macro
410 expansions that test for features, sizeof, and similar. */
411 if (CONSTANT_CLASS_P (fold_for_warn (lhs
))
412 || CONSTANT_CLASS_P (fold_for_warn (rhs
)))
415 /* Don't warn for e.g.
418 if (n == (long) n) ...
420 if ((CONVERT_EXPR_P (lhs
) || TREE_CODE (lhs
) == NON_LVALUE_EXPR
)
421 || (CONVERT_EXPR_P (rhs
) || TREE_CODE (rhs
) == NON_LVALUE_EXPR
))
424 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
425 It could be a NaN, and NaN never compares equal to anything, even
427 if (FLOAT_TYPE_P (TREE_TYPE (lhs
)) || FLOAT_TYPE_P (TREE_TYPE (rhs
)))
430 if (operand_equal_p (lhs
, rhs
, 0))
432 /* Don't warn about array references with constant indices;
433 these are likely to come from a macro. */
434 if (walk_tree_without_duplicates (&lhs
, find_array_ref_with_const_idx_r
,
437 const bool always_true
= (code
== EQ_EXPR
|| code
== LE_EXPR
438 || code
== GE_EXPR
|| code
== UNLE_EXPR
439 || code
== UNGE_EXPR
|| code
== UNEQ_EXPR
);
441 warning_at (loc
, OPT_Wtautological_compare
,
442 "self-comparison always evaluates to true");
444 warning_at (loc
, OPT_Wtautological_compare
,
445 "self-comparison always evaluates to false");
449 /* Return true iff EXPR only contains boolean operands, or comparisons. */
452 expr_has_boolean_operands_p (tree expr
)
456 if (CONVERT_EXPR_P (expr
))
457 return bool_promoted_to_int_p (expr
);
458 else if (UNARY_CLASS_P (expr
))
459 return expr_has_boolean_operands_p (TREE_OPERAND (expr
, 0));
460 else if (BINARY_CLASS_P (expr
))
461 return (expr_has_boolean_operands_p (TREE_OPERAND (expr
, 0))
462 && expr_has_boolean_operands_p (TREE_OPERAND (expr
, 1)));
463 else if (COMPARISON_CLASS_P (expr
))
469 /* Warn about logical not used on the left hand side operand of a comparison.
470 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
471 Do not warn if RHS is of a boolean type, a logical operator, or
475 warn_logical_not_parentheses (location_t location
, enum tree_code code
,
478 if (TREE_CODE_CLASS (code
) != tcc_comparison
479 || TREE_TYPE (rhs
) == NULL_TREE
480 || TREE_CODE (TREE_TYPE (rhs
)) == BOOLEAN_TYPE
481 || truth_value_p (TREE_CODE (rhs
)))
484 /* Don't warn for expression like !x == ~(bool1 | bool2). */
485 if (expr_has_boolean_operands_p (rhs
))
488 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
489 !(x == 0) or !(y != 0). */
490 if ((code
== EQ_EXPR
|| code
== NE_EXPR
)
491 && integer_zerop (rhs
))
494 if (warning_at (location
, OPT_Wlogical_not_parentheses
,
495 "logical not is only applied to the left hand side of "
497 && EXPR_HAS_LOCATION (lhs
))
499 location_t lhs_loc
= EXPR_LOCATION (lhs
);
500 rich_location
richloc (line_table
, lhs_loc
);
501 richloc
.add_fixit_insert_before (lhs_loc
, "(");
502 richloc
.add_fixit_insert_after (lhs_loc
, ")");
503 inform (&richloc
, "add parentheses around left hand side "
504 "expression to silence this warning");
508 /* Warn if EXP contains any computations whose results are not used.
509 Return true if a warning is printed; false otherwise. LOCUS is the
510 (potential) location of the expression. */
513 warn_if_unused_value (const_tree exp
, location_t locus
)
516 if (TREE_USED (exp
) || TREE_NO_WARNING (exp
))
519 /* Don't warn about void constructs. This includes casting to void,
520 void function calls, and statement expressions with a final cast
522 if (VOID_TYPE_P (TREE_TYPE (exp
)))
525 if (EXPR_HAS_LOCATION (exp
))
526 locus
= EXPR_LOCATION (exp
);
528 switch (TREE_CODE (exp
))
530 case PREINCREMENT_EXPR
:
531 case POSTINCREMENT_EXPR
:
532 case PREDECREMENT_EXPR
:
533 case POSTDECREMENT_EXPR
:
544 /* For a binding, warn if no side effect within it. */
545 exp
= BIND_EXPR_BODY (exp
);
549 case NON_LVALUE_EXPR
:
551 exp
= TREE_OPERAND (exp
, 0);
554 case TRUTH_ORIF_EXPR
:
555 case TRUTH_ANDIF_EXPR
:
556 /* In && or ||, warn if 2nd operand has no side effect. */
557 exp
= TREE_OPERAND (exp
, 1);
561 if (warn_if_unused_value (TREE_OPERAND (exp
, 0), locus
))
563 /* Let people do `(foo (), 0)' without a warning. */
564 if (TREE_CONSTANT (TREE_OPERAND (exp
, 1)))
566 exp
= TREE_OPERAND (exp
, 1);
570 /* If this is an expression with side effects, don't warn; this
571 case commonly appears in macro expansions. */
572 if (TREE_SIDE_EFFECTS (exp
))
577 /* Don't warn about automatic dereferencing of references, since
578 the user cannot control it. */
579 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp
, 0))) == REFERENCE_TYPE
)
581 exp
= TREE_OPERAND (exp
, 0);
587 /* Referencing a volatile value is a side effect, so don't warn. */
588 if ((DECL_P (exp
) || REFERENCE_CLASS_P (exp
))
589 && TREE_THIS_VOLATILE (exp
))
592 /* If this is an expression which has no operands, there is no value
593 to be unused. There are no such language-independent codes,
594 but front ends may define such. */
595 if (EXPRESSION_CLASS_P (exp
) && TREE_OPERAND_LENGTH (exp
) == 0)
599 return warning_at (locus
, OPT_Wunused_value
, "value computed is not used");
603 /* Print a warning about casts that might indicate violation of strict
604 aliasing rules if -Wstrict-aliasing is used and strict aliasing
605 mode is in effect. LOC is the location of the expression being
606 cast, EXPR might be from inside it. TYPE is the type we're casting
610 strict_aliasing_warning (location_t loc
, tree type
, tree expr
)
612 if (loc
== UNKNOWN_LOCATION
)
613 loc
= input_location
;
615 /* Strip pointer conversion chains and get to the correct original type. */
617 tree otype
= TREE_TYPE (expr
);
619 if (!(flag_strict_aliasing
620 && POINTER_TYPE_P (type
)
621 && POINTER_TYPE_P (otype
)
622 && !VOID_TYPE_P (TREE_TYPE (type
)))
623 /* If the type we are casting to is a ref-all pointer
624 dereferencing it is always valid. */
625 || TYPE_REF_CAN_ALIAS_ALL (type
))
628 if ((warn_strict_aliasing
> 1) && TREE_CODE (expr
) == ADDR_EXPR
629 && (DECL_P (TREE_OPERAND (expr
, 0))
630 || handled_component_p (TREE_OPERAND (expr
, 0))))
632 /* Casting the address of an object to non void pointer. Warn
633 if the cast breaks type based aliasing. */
634 if (!COMPLETE_TYPE_P (TREE_TYPE (type
)) && warn_strict_aliasing
== 2)
636 warning_at (loc
, OPT_Wstrict_aliasing
,
637 "type-punning to incomplete type "
638 "might break strict-aliasing rules");
643 /* warn_strict_aliasing >= 3. This includes the default (3).
644 Only warn if the cast is dereferenced immediately. */
646 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr
, 0)));
647 alias_set_type set2
= get_alias_set (TREE_TYPE (type
));
651 && !alias_set_subset_of (set2
, set1
)
652 && !alias_sets_conflict_p (set1
, set2
))
654 warning_at (loc
, OPT_Wstrict_aliasing
,
655 "dereferencing type-punned "
656 "pointer will break strict-aliasing rules");
659 else if (warn_strict_aliasing
== 2
660 && !alias_sets_must_conflict_p (set1
, set2
))
662 warning_at (loc
, OPT_Wstrict_aliasing
,
663 "dereferencing type-punned "
664 "pointer might break strict-aliasing rules");
669 else if ((warn_strict_aliasing
== 1) && !VOID_TYPE_P (TREE_TYPE (otype
)))
671 /* At this level, warn for any conversions, even if an address is
672 not taken in the same statement. This will likely produce many
673 false positives, but could be useful to pinpoint problems that
674 are not revealed at higher levels. */
675 alias_set_type set1
= get_alias_set (TREE_TYPE (otype
));
676 alias_set_type set2
= get_alias_set (TREE_TYPE (type
));
677 if (!COMPLETE_TYPE_P (type
)
678 || !alias_sets_must_conflict_p (set1
, set2
))
680 warning_at (loc
, OPT_Wstrict_aliasing
,
681 "dereferencing type-punned "
682 "pointer might break strict-aliasing rules");
690 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
691 sizeof as last operand of certain builtins. */
694 sizeof_pointer_memaccess_warning (location_t
*sizeof_arg_loc
, tree callee
,
695 vec
<tree
, va_gc
> *params
, tree
*sizeof_arg
,
696 bool (*comp_types
) (tree
, tree
))
698 tree type
, dest
= NULL_TREE
, src
= NULL_TREE
, tem
;
699 bool strop
= false, cmp
= false;
700 unsigned int idx
= ~0;
703 if (TREE_CODE (callee
) != FUNCTION_DECL
704 || DECL_BUILT_IN_CLASS (callee
) != BUILT_IN_NORMAL
705 || vec_safe_length (params
) <= 1)
708 enum built_in_function fncode
= DECL_FUNCTION_CODE (callee
);
711 case BUILT_IN_STRNCMP
:
712 case BUILT_IN_STRNCASECMP
:
715 case BUILT_IN_STRNCPY
:
716 case BUILT_IN_STRNCPY_CHK
:
717 case BUILT_IN_STRNCAT
:
718 case BUILT_IN_STRNCAT_CHK
:
719 case BUILT_IN_STPNCPY
:
720 case BUILT_IN_STPNCPY_CHK
:
723 case BUILT_IN_MEMCPY
:
724 case BUILT_IN_MEMCPY_CHK
:
725 case BUILT_IN_MEMMOVE
:
726 case BUILT_IN_MEMMOVE_CHK
:
727 if (params
->length () < 3)
734 if (params
->length () < 3)
740 case BUILT_IN_MEMCMP
:
742 if (params
->length () < 3)
749 case BUILT_IN_MEMSET
:
750 case BUILT_IN_MEMSET_CHK
:
751 if (params
->length () < 3)
760 case BUILT_IN_STRNDUP
:
765 case BUILT_IN_MEMCHR
:
766 if (params
->length () < 3)
771 case BUILT_IN_SNPRINTF
:
772 case BUILT_IN_SNPRINTF_CHK
:
773 case BUILT_IN_VSNPRINTF
:
774 case BUILT_IN_VSNPRINTF_CHK
:
786 if (sizeof_arg
[idx
] == NULL
|| sizeof_arg
[idx
] == error_mark_node
)
789 type
= TYPE_P (sizeof_arg
[idx
])
790 ? sizeof_arg
[idx
] : TREE_TYPE (sizeof_arg
[idx
]);
792 if (!POINTER_TYPE_P (type
))
794 /* The argument type may be an array. Diagnose bounded string
795 copy functions that specify the bound in terms of the source
796 argument rather than the destination unless they are equal
797 to one another. Handle constant sizes and also try to handle
798 sizeof expressions involving VLAs. */
799 if (strop
&& !cmp
&& fncode
!= BUILT_IN_STRNDUP
&& src
)
801 tem
= tree_strip_nop_conversions (src
);
802 if (TREE_CODE (tem
) == ADDR_EXPR
)
803 tem
= TREE_OPERAND (tem
, 0);
805 /* Avoid diagnosing sizeof SRC when SRC is declared with
806 attribute nonstring. */
808 if (get_attr_nonstring_decl (tem
, &dummy
))
811 tree d
= tree_strip_nop_conversions (dest
);
812 if (TREE_CODE (d
) == ADDR_EXPR
)
813 d
= TREE_OPERAND (d
, 0);
815 tree dstsz
= TYPE_SIZE_UNIT (TREE_TYPE (d
));
816 tree srcsz
= TYPE_SIZE_UNIT (TREE_TYPE (tem
));
820 || !operand_equal_p (dstsz
, srcsz
, OEP_LEXICOGRAPHIC
))
821 && operand_equal_p (tem
, sizeof_arg
[idx
], OEP_ADDRESS_OF
))
822 warning_at (sizeof_arg_loc
[idx
], OPT_Wsizeof_pointer_memaccess
,
823 "argument to %<sizeof%> in %qD call is the same "
824 "expression as the source; did you mean to use "
825 "the size of the destination?",
833 && (tem
= tree_strip_nop_conversions (dest
))
834 && POINTER_TYPE_P (TREE_TYPE (tem
))
835 && comp_types (TREE_TYPE (TREE_TYPE (tem
)), type
))
839 && (tem
= tree_strip_nop_conversions (src
))
840 && POINTER_TYPE_P (TREE_TYPE (tem
))
841 && comp_types (TREE_TYPE (TREE_TYPE (tem
)), type
))
844 loc
= sizeof_arg_loc
[idx
];
848 if (!TYPE_P (sizeof_arg
[idx
])
849 && operand_equal_p (dest
, sizeof_arg
[idx
], 0)
850 && comp_types (TREE_TYPE (dest
), type
))
852 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
853 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
854 "argument to %<sizeof%> in %qD call is the same "
855 "expression as the destination; did you mean to "
856 "remove the addressof?", callee
);
857 else if ((TYPE_PRECISION (TREE_TYPE (type
))
858 == TYPE_PRECISION (char_type_node
))
860 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
861 "argument to %<sizeof%> in %qD call is the same "
862 "expression as the destination; did you mean to "
863 "provide an explicit length?", callee
);
865 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
866 "argument to %<sizeof%> in %qD call is the same "
867 "expression as the destination; did you mean to "
868 "dereference it?", callee
);
872 if (POINTER_TYPE_P (TREE_TYPE (dest
))
874 && comp_types (TREE_TYPE (dest
), type
)
875 && !VOID_TYPE_P (TREE_TYPE (type
)))
877 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
878 "argument to %<sizeof%> in %qD call is the same "
879 "pointer type %qT as the destination; expected %qT "
880 "or an explicit length", callee
, TREE_TYPE (dest
),
881 TREE_TYPE (TREE_TYPE (dest
)));
888 if (!TYPE_P (sizeof_arg
[idx
])
889 && operand_equal_p (src
, sizeof_arg
[idx
], 0)
890 && comp_types (TREE_TYPE (src
), type
))
892 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
893 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
894 "argument to %<sizeof%> in %qD call is the same "
895 "expression as the source; did you mean to "
896 "remove the addressof?", callee
);
897 else if ((TYPE_PRECISION (TREE_TYPE (type
))
898 == TYPE_PRECISION (char_type_node
))
900 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
901 "argument to %<sizeof%> in %qD call is the same "
902 "expression as the source; did you mean to "
903 "provide an explicit length?", callee
);
905 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
906 "argument to %<sizeof%> in %qD call is the same "
907 "expression as the source; did you mean to "
908 "dereference it?", callee
);
912 if (POINTER_TYPE_P (TREE_TYPE (src
))
914 && comp_types (TREE_TYPE (src
), type
)
915 && !VOID_TYPE_P (TREE_TYPE (type
)))
917 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
918 "argument to %<sizeof%> in %qD call is the same "
919 "pointer type %qT as the source; expected %qT "
920 "or an explicit length", callee
, TREE_TYPE (src
),
921 TREE_TYPE (TREE_TYPE (src
)));
928 if (!TYPE_P (sizeof_arg
[idx
])
929 && operand_equal_p (dest
, sizeof_arg
[idx
], 0)
930 && comp_types (TREE_TYPE (dest
), type
))
932 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
933 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
934 "argument to %<sizeof%> in %qD call is the same "
935 "expression as the first source; did you mean to "
936 "remove the addressof?", callee
);
937 else if ((TYPE_PRECISION (TREE_TYPE (type
))
938 == TYPE_PRECISION (char_type_node
))
940 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
941 "argument to %<sizeof%> in %qD call is the same "
942 "expression as the first source; did you mean to "
943 "provide an explicit length?", callee
);
945 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
946 "argument to %<sizeof%> in %qD call is the same "
947 "expression as the first source; did you mean to "
948 "dereference it?", callee
);
952 if (POINTER_TYPE_P (TREE_TYPE (dest
))
954 && comp_types (TREE_TYPE (dest
), type
)
955 && !VOID_TYPE_P (TREE_TYPE (type
)))
957 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
958 "argument to %<sizeof%> in %qD call is the same "
959 "pointer type %qT as the first source; expected %qT "
960 "or an explicit length", callee
, TREE_TYPE (dest
),
961 TREE_TYPE (TREE_TYPE (dest
)));
968 if (!TYPE_P (sizeof_arg
[idx
])
969 && operand_equal_p (src
, sizeof_arg
[idx
], 0)
970 && comp_types (TREE_TYPE (src
), type
))
972 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
973 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
974 "argument to %<sizeof%> in %qD call is the same "
975 "expression as the second source; did you mean to "
976 "remove the addressof?", callee
);
977 else if ((TYPE_PRECISION (TREE_TYPE (type
))
978 == TYPE_PRECISION (char_type_node
))
980 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
981 "argument to %<sizeof%> in %qD call is the same "
982 "expression as the second source; did you mean to "
983 "provide an explicit length?", callee
);
985 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
986 "argument to %<sizeof%> in %qD call is the same "
987 "expression as the second source; did you mean to "
988 "dereference it?", callee
);
992 if (POINTER_TYPE_P (TREE_TYPE (src
))
994 && comp_types (TREE_TYPE (src
), type
)
995 && !VOID_TYPE_P (TREE_TYPE (type
)))
997 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
998 "argument to %<sizeof%> in %qD call is the same "
999 "pointer type %qT as the second source; expected %qT "
1000 "or an explicit length", callee
, TREE_TYPE (src
),
1001 TREE_TYPE (TREE_TYPE (src
)));
1008 /* Warn for unlikely, improbable, or stupid DECL declarations
1012 check_main_parameter_types (tree decl
)
1014 function_args_iterator iter
;
1018 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl
), type
, iter
)
1020 /* XXX void_type_node belies the abstraction. */
1021 if (type
== void_type_node
|| type
== error_mark_node
)
1025 if (TYPE_ATOMIC (t
))
1026 pedwarn (input_location
, OPT_Wmain
,
1027 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1029 while (POINTER_TYPE_P (t
))
1032 if (TYPE_ATOMIC (t
))
1033 pedwarn (input_location
, OPT_Wmain
,
1034 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1042 if (TYPE_MAIN_VARIANT (type
) != integer_type_node
)
1043 pedwarn (input_location
, OPT_Wmain
,
1044 "first argument of %q+D should be %<int%>", decl
);
1048 if (TREE_CODE (type
) != POINTER_TYPE
1049 || TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
1050 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type
)))
1052 pedwarn (input_location
, OPT_Wmain
,
1053 "second argument of %q+D should be %<char **%>", decl
);
1057 if (TREE_CODE (type
) != POINTER_TYPE
1058 || TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
1059 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type
)))
1061 pedwarn (input_location
, OPT_Wmain
,
1062 "third argument of %q+D should probably be "
1063 "%<char **%>", decl
);
1068 /* It is intentional that this message does not mention the third
1069 argument because it's only mentioned in an appendix of the
1071 if (argct
> 0 && (argct
< 2 || argct
> 3))
1072 pedwarn (input_location
, OPT_Wmain
,
1073 "%q+D takes only zero or two arguments", decl
);
1075 if (stdarg_p (TREE_TYPE (decl
)))
1076 pedwarn (input_location
, OPT_Wmain
,
1077 "%q+D declared as variadic function", decl
);
1080 /* Warns if the conversion of EXPR to TYPE may alter a value.
1081 This is a helper function for warnings_for_convert_and_check. */
1084 conversion_warning (location_t loc
, tree type
, tree expr
, tree result
)
1086 tree expr_type
= TREE_TYPE (expr
);
1087 enum conversion_safety conversion_kind
;
1089 if (!warn_conversion
&& !warn_sign_conversion
&& !warn_float_conversion
)
1092 /* This may happen, because for LHS op= RHS we preevaluate
1093 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
1094 means we could no longer see the code of the EXPR. */
1095 if (TREE_CODE (expr
) == C_MAYBE_CONST_EXPR
)
1096 expr
= C_MAYBE_CONST_EXPR_EXPR (expr
);
1097 if (TREE_CODE (expr
) == SAVE_EXPR
)
1098 expr
= TREE_OPERAND (expr
, 0);
1100 switch (TREE_CODE (expr
))
1108 case TRUTH_ANDIF_EXPR
:
1109 case TRUTH_ORIF_EXPR
:
1110 case TRUTH_AND_EXPR
:
1112 case TRUTH_XOR_EXPR
:
1113 case TRUTH_NOT_EXPR
:
1114 /* Conversion from boolean to a signed:1 bit-field (which only
1115 can hold the values 0 and -1) doesn't lose information - but
1116 it does change the value. */
1117 if (TYPE_PRECISION (type
) == 1 && !TYPE_UNSIGNED (type
))
1118 warning_at (loc
, OPT_Wconversion
,
1119 "conversion to %qT from boolean expression", type
);
1126 conversion_kind
= unsafe_conversion_p (loc
, type
, expr
, result
, true);
1128 if (conversion_kind
== UNSAFE_REAL
)
1129 warnopt
= OPT_Wfloat_conversion
;
1130 else if (conversion_kind
)
1131 warnopt
= OPT_Wconversion
;
1135 if (TREE_CODE_CLASS (TREE_CODE (result
)) == tcc_constant
)
1136 warning_at (loc
, warnopt
,
1137 "conversion from %qT to %qT changes value from %qE to %qE",
1138 expr_type
, type
, expr
, result
);
1140 warning_at (loc
, warnopt
,
1141 "conversion from %qT to %qT changes the value of %qE",
1142 expr_type
, type
, expr
);
1147 /* In case of COND_EXPR, we do not care about the type of
1148 COND_EXPR, only about the conversion of each operand. */
1149 tree op1
= TREE_OPERAND (expr
, 1);
1150 tree op2
= TREE_OPERAND (expr
, 2);
1152 conversion_warning (loc
, type
, op1
, result
);
1153 conversion_warning (loc
, type
, op2
, result
);
1157 default: /* 'expr' is not a constant. */
1158 conversion_kind
= unsafe_conversion_p (loc
, type
, expr
, result
, true);
1159 if (conversion_kind
== UNSAFE_IMAGINARY
)
1160 warning_at (loc
, OPT_Wconversion
,
1161 "conversion from %qT to %qT discards imaginary component",
1166 if (conversion_kind
== UNSAFE_REAL
)
1167 warnopt
= OPT_Wfloat_conversion
;
1168 else if (conversion_kind
)
1169 warnopt
= OPT_Wconversion
;
1172 warning_at (loc
, warnopt
,
1173 "conversion from %qT to %qT may change value",
1179 /* Produce warnings after a conversion. RESULT is the result of
1180 converting EXPR to TYPE. This is a helper function for
1181 convert_and_check and cp_convert_and_check. */
1184 warnings_for_convert_and_check (location_t loc
, tree type
, tree expr
,
1187 loc
= expansion_point_location_if_in_system_header (loc
);
1189 bool cst
= TREE_CODE_CLASS (TREE_CODE (result
)) == tcc_constant
;
1191 tree exprtype
= TREE_TYPE (expr
);
1193 if (TREE_CODE (expr
) == INTEGER_CST
1194 && (TREE_CODE (type
) == INTEGER_TYPE
1195 || TREE_CODE (type
) == ENUMERAL_TYPE
)
1196 && !int_fits_type_p (expr
, type
))
1198 /* Do not diagnose overflow in a constant expression merely
1199 because a conversion overflowed. */
1200 if (TREE_OVERFLOW (result
))
1201 TREE_OVERFLOW (result
) = TREE_OVERFLOW (expr
);
1203 if (TYPE_UNSIGNED (type
))
1205 /* This detects cases like converting -129 or 256 to
1207 if (!int_fits_type_p (expr
, c_common_signed_type (type
)))
1210 warning_at (loc
, OPT_Woverflow
,
1211 (TYPE_UNSIGNED (exprtype
)
1212 ? G_("conversion from %qT to %qT "
1213 "changes value from %qE to %qE")
1214 : G_("unsigned conversion from %qT to %qT "
1215 "changes value from %qE to %qE")),
1216 exprtype
, type
, expr
, result
);
1218 warning_at (loc
, OPT_Woverflow
,
1219 (TYPE_UNSIGNED (exprtype
)
1220 ? G_("conversion from %qT to %qT "
1221 "changes the value of %qE")
1222 : G_("unsigned conversion from %qT to %qT "
1223 "changes the value of %qE")),
1224 exprtype
, type
, expr
);
1227 conversion_warning (loc
, type
, expr
, result
);
1229 else if (!int_fits_type_p (expr
, c_common_unsigned_type (type
)))
1232 warning_at (loc
, OPT_Woverflow
,
1233 "overflow in conversion from %qT to %qT "
1234 "changes value from %qE to %qE",
1235 exprtype
, type
, expr
, result
);
1237 warning_at (loc
, OPT_Woverflow
,
1238 "overflow in conversion from %qT to %qT "
1239 "changes the value of %qE",
1240 exprtype
, type
, expr
);
1242 /* No warning for converting 0x80000000 to int. */
1244 && (TREE_CODE (exprtype
) != INTEGER_TYPE
1245 || TYPE_PRECISION (exprtype
)
1246 != TYPE_PRECISION (type
)))
1249 warning_at (loc
, OPT_Woverflow
,
1250 "overflow in conversion from %qT to %qT "
1251 "changes value from %qE to %qE",
1252 exprtype
, type
, expr
, result
);
1254 warning_at (loc
, OPT_Woverflow
,
1255 "overflow in conversion from %qT to %qT "
1256 "changes the value of %qE",
1257 exprtype
, type
, expr
);
1260 conversion_warning (loc
, type
, expr
, result
);
1262 else if ((TREE_CODE (result
) == INTEGER_CST
1263 || TREE_CODE (result
) == FIXED_CST
) && TREE_OVERFLOW (result
))
1266 warning_at (loc
, OPT_Woverflow
,
1267 "overflow in conversion from %qT to %qT "
1268 "changes value from %qE to %qE",
1269 exprtype
, type
, expr
, result
);
1271 warning_at (loc
, OPT_Woverflow
,
1272 "overflow in conversion from %qT to %qT "
1273 "changes the value of %qE",
1274 exprtype
, type
, expr
);
1277 conversion_warning (loc
, type
, expr
, result
);
1280 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
1281 Used to verify that case values match up with enumerator values. */
1284 match_case_to_enum_1 (tree key
, tree type
, tree label
)
1286 /* Avoid warning about enums that have no enumerators. */
1287 if (TYPE_VALUES (type
) == NULL_TREE
)
1290 char buf
[WIDE_INT_PRINT_BUFFER_SIZE
];
1292 if (tree_fits_uhwi_p (key
))
1293 print_dec (wi::to_wide (key
), buf
, UNSIGNED
);
1294 else if (tree_fits_shwi_p (key
))
1295 print_dec (wi::to_wide (key
), buf
, SIGNED
);
1297 print_hex (wi::to_wide (key
), buf
);
1299 if (TYPE_NAME (type
) == NULL_TREE
)
1300 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label
)),
1301 warn_switch
? OPT_Wswitch
: OPT_Wswitch_enum
,
1302 "case value %qs not in enumerated type",
1305 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label
)),
1306 warn_switch
? OPT_Wswitch
: OPT_Wswitch_enum
,
1307 "case value %qs not in enumerated type %qT",
1311 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
1312 Used to verify that case values match up with enumerator values. */
1315 match_case_to_enum (splay_tree_node node
, void *data
)
1317 tree label
= (tree
) node
->value
;
1318 tree type
= (tree
) data
;
1320 /* Skip default case. */
1321 if (!CASE_LOW (label
))
1324 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
1325 when we did our enum->case scan. Reset our scratch bit after. */
1326 if (!CASE_LOW_SEEN (label
))
1327 match_case_to_enum_1 (CASE_LOW (label
), type
, label
);
1329 CASE_LOW_SEEN (label
) = 0;
1331 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
1332 not set, that means that CASE_HIGH did not appear when we did our
1333 enum->case scan. Reset our scratch bit after. */
1334 if (CASE_HIGH (label
))
1336 if (!CASE_HIGH_SEEN (label
))
1337 match_case_to_enum_1 (CASE_HIGH (label
), type
, label
);
1339 CASE_HIGH_SEEN (label
) = 0;
1345 /* Handle -Wswitch*. Called from the front end after parsing the
1346 switch construct. */
1347 /* ??? Should probably be somewhere generic, since other languages
1348 besides C and C++ would want this. At the moment, however, C/C++
1349 are the only tree-ssa languages that support enumerations at all,
1350 so the point is moot. */
1353 c_do_switch_warnings (splay_tree cases
, location_t switch_location
,
1354 tree type
, tree cond
, bool bool_cond_p
,
1355 bool outside_range_p
)
1357 splay_tree_node default_node
;
1358 splay_tree_node node
;
1361 if (!warn_switch
&& !warn_switch_enum
&& !warn_switch_default
1362 && !warn_switch_bool
)
1365 default_node
= splay_tree_lookup (cases
, (splay_tree_key
) NULL
);
1367 warning_at (switch_location
, OPT_Wswitch_default
,
1368 "switch missing default case");
1370 /* There are certain cases where -Wswitch-bool warnings aren't
1377 so be careful here. */
1378 if (warn_switch_bool
&& bool_cond_p
)
1380 splay_tree_node min_node
;
1381 /* If there's a default node, it's also the value with the minimal
1382 key. So look at the penultimate key (if any). */
1384 min_node
= splay_tree_successor (cases
, (splay_tree_key
) NULL
);
1386 min_node
= splay_tree_min (cases
);
1387 tree min
= min_node
? (tree
) min_node
->key
: NULL_TREE
;
1389 splay_tree_node max_node
= splay_tree_max (cases
);
1390 /* This might be a case range, so look at the value with the
1391 maximal key and then check CASE_HIGH. */
1392 tree max
= max_node
? (tree
) max_node
->value
: NULL_TREE
;
1394 max
= CASE_HIGH (max
) ? CASE_HIGH (max
) : CASE_LOW (max
);
1396 /* If there's a case value > 1 or < 0, that is outside bool
1399 || (max
&& wi::gts_p (wi::to_wide (max
), 1))
1400 || (min
&& wi::lts_p (wi::to_wide (min
), 0))
1408 case, where we want to warn. */
1410 && max
&& wi::to_wide (max
) == 1
1411 && min
&& wi::to_wide (min
) == 0))
1412 warning_at (switch_location
, OPT_Wswitch_bool
,
1413 "switch condition has boolean value");
1416 /* From here on, we only care about enumerated types. */
1417 if (!type
|| TREE_CODE (type
) != ENUMERAL_TYPE
)
1420 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
1421 if (!warn_switch_enum
&& !warn_switch
)
1424 /* Check the cases. Warn about case values which are not members of
1425 the enumerated type. For -Wswitch-enum, or for -Wswitch when
1426 there is no default case, check that exactly all enumeration
1427 literals are covered by the cases. */
1429 /* Clearing COND if it is not an integer constant simplifies
1430 the tests inside the loop below. */
1431 if (TREE_CODE (cond
) != INTEGER_CST
)
1434 /* The time complexity here is O(N*lg(N)) worst case, but for the
1435 common case of monotonically increasing enumerators, it is
1436 O(N), since the nature of the splay tree will keep the next
1437 element adjacent to the root at all times. */
1439 for (chain
= TYPE_VALUES (type
); chain
; chain
= TREE_CHAIN (chain
))
1441 tree value
= TREE_VALUE (chain
);
1442 if (TREE_CODE (value
) == CONST_DECL
)
1443 value
= DECL_INITIAL (value
);
1444 node
= splay_tree_lookup (cases
, (splay_tree_key
) value
);
1447 /* Mark the CASE_LOW part of the case entry as seen. */
1448 tree label
= (tree
) node
->value
;
1449 CASE_LOW_SEEN (label
) = 1;
1453 /* Even though there wasn't an exact match, there might be a
1454 case range which includes the enumerator's value. */
1455 node
= splay_tree_predecessor (cases
, (splay_tree_key
) value
);
1456 if (node
&& CASE_HIGH ((tree
) node
->value
))
1458 tree label
= (tree
) node
->value
;
1459 int cmp
= tree_int_cst_compare (CASE_HIGH (label
), value
);
1462 /* If we match the upper bound exactly, mark the CASE_HIGH
1463 part of the case entry as seen. */
1465 CASE_HIGH_SEEN (label
) = 1;
1470 /* We've now determined that this enumerated literal isn't
1471 handled by the case labels of the switch statement. */
1473 /* If the switch expression is a constant, we only really care
1474 about whether that constant is handled by the switch. */
1475 if (cond
&& tree_int_cst_compare (cond
, value
))
1478 /* If there is a default_node, the only relevant option is
1479 Wswitch-enum. Otherwise, if both are enabled then we prefer
1480 to warn using -Wswitch because -Wswitch is enabled by -Wall
1481 while -Wswitch-enum is explicit. */
1482 warning_at (switch_location
,
1483 (default_node
|| !warn_switch
1486 "enumeration value %qE not handled in switch",
1487 TREE_PURPOSE (chain
));
1490 /* Warn if there are case expressions that don't correspond to
1491 enumerators. This can occur since C and C++ don't enforce
1492 type-checking of assignments to enumeration variables.
1494 The time complexity here is now always O(N) worst case, since
1495 we should have marked both the lower bound and upper bound of
1496 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
1497 above. This scan also resets those fields. */
1499 splay_tree_foreach (cases
, match_case_to_enum
, type
);
1502 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
1503 expression, because B will always be true. */
1506 warn_for_omitted_condop (location_t location
, tree cond
)
1508 /* In C++ template declarations it can happen that the type is dependent
1509 and not yet known, thus TREE_TYPE (cond) == NULL_TREE. */
1510 if (truth_value_p (TREE_CODE (cond
))
1511 || (TREE_TYPE (cond
) != NULL_TREE
1512 && TREE_CODE (TREE_TYPE (cond
)) == BOOLEAN_TYPE
))
1513 warning_at (location
, OPT_Wparentheses
,
1514 "the omitted middle operand in ?: will always be %<true%>, "
1515 "suggest explicit middle operand");
1518 /* Give an error for storing into ARG, which is 'const'. USE indicates
1519 how ARG was being used. */
1522 readonly_error (location_t loc
, tree arg
, enum lvalue_use use
)
1524 gcc_assert (use
== lv_assign
|| use
== lv_increment
|| use
== lv_decrement
1526 /* Using this macro rather than (for example) arrays of messages
1527 ensures that all the format strings are checked at compile
1529 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
1530 : (use == lv_increment ? (I) \
1531 : (use == lv_decrement ? (D) : (AS))))
1532 if (TREE_CODE (arg
) == COMPONENT_REF
)
1534 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg
, 0))))
1535 error_at (loc
, READONLY_MSG (G_("assignment of member "
1536 "%qD in read-only object"),
1537 G_("increment of member "
1538 "%qD in read-only object"),
1539 G_("decrement of member "
1540 "%qD in read-only object"),
1541 G_("member %qD in read-only object "
1542 "used as %<asm%> output")),
1543 TREE_OPERAND (arg
, 1));
1545 error_at (loc
, READONLY_MSG (G_("assignment of read-only member %qD"),
1546 G_("increment of read-only member %qD"),
1547 G_("decrement of read-only member %qD"),
1548 G_("read-only member %qD used as %<asm%> output")),
1549 TREE_OPERAND (arg
, 1));
1551 else if (VAR_P (arg
))
1552 error_at (loc
, READONLY_MSG (G_("assignment of read-only variable %qD"),
1553 G_("increment of read-only variable %qD"),
1554 G_("decrement of read-only variable %qD"),
1555 G_("read-only variable %qD used as %<asm%> output")),
1557 else if (TREE_CODE (arg
) == PARM_DECL
)
1558 error_at (loc
, READONLY_MSG (G_("assignment of read-only parameter %qD"),
1559 G_("increment of read-only parameter %qD"),
1560 G_("decrement of read-only parameter %qD"),
1561 G_("read-only parameter %qD use as %<asm%> output")),
1563 else if (TREE_CODE (arg
) == RESULT_DECL
)
1565 gcc_assert (c_dialect_cxx ());
1566 error_at (loc
, READONLY_MSG (G_("assignment of "
1567 "read-only named return value %qD"),
1569 "read-only named return value %qD"),
1571 "read-only named return value %qD"),
1572 G_("read-only named return value %qD "
1573 "used as %<asm%>output")),
1576 else if (TREE_CODE (arg
) == FUNCTION_DECL
)
1577 error_at (loc
, READONLY_MSG (G_("assignment of function %qD"),
1578 G_("increment of function %qD"),
1579 G_("decrement of function %qD"),
1580 G_("function %qD used as %<asm%> output")),
1583 error_at (loc
, READONLY_MSG (G_("assignment of read-only location %qE"),
1584 G_("increment of read-only location %qE"),
1585 G_("decrement of read-only location %qE"),
1586 G_("read-only location %qE used as %<asm%> output")),
1590 /* Print an error message for an invalid lvalue. USE says
1591 how the lvalue is being used and so selects the error message. LOC
1592 is the location for the error. */
1595 lvalue_error (location_t loc
, enum lvalue_use use
)
1600 error_at (loc
, "lvalue required as left operand of assignment");
1603 error_at (loc
, "lvalue required as increment operand");
1606 error_at (loc
, "lvalue required as decrement operand");
1609 error_at (loc
, "lvalue required as unary %<&%> operand");
1612 error_at (loc
, "lvalue required in asm statement");
1619 /* Print an error message for an invalid indirection of type TYPE.
1620 ERRSTRING is the name of the operator for the indirection. */
1623 invalid_indirection_error (location_t loc
, tree type
, ref_operator errstring
)
1628 gcc_assert (c_dialect_cxx ());
1629 error_at (loc
, "invalid type argument (have %qT)", type
);
1631 case RO_ARRAY_INDEXING
:
1633 "invalid type argument of array indexing (have %qT)",
1638 "invalid type argument of unary %<*%> (have %qT)",
1643 "invalid type argument of %<->%> (have %qT)",
1648 "invalid type argument of %<->*%> (have %qT)",
1651 case RO_IMPLICIT_CONVERSION
:
1653 "invalid type argument of implicit conversion (have %qT)",
1661 /* Subscripting with type char is likely to lose on a machine where
1662 chars are signed. So warn on any machine, but optionally. Don't
1663 warn for unsigned char since that type is safe. Don't warn for
1664 signed char because anyone who uses that must have done so
1665 deliberately. Furthermore, we reduce the false positive load by
1666 warning only for non-constant value of type char. */
1669 warn_array_subscript_with_type_char (location_t loc
, tree index
)
1671 if (TYPE_MAIN_VARIANT (TREE_TYPE (index
)) == char_type_node
1672 && TREE_CODE (index
) != INTEGER_CST
)
1673 warning_at (loc
, OPT_Wchar_subscripts
,
1674 "array subscript has type %<char%>");
1677 /* Implement -Wparentheses for the unexpected C precedence rules, to
1678 cover cases like x + y << z which readers are likely to
1679 misinterpret. We have seen an expression in which CODE is a binary
1680 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
1681 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
1682 CODE_RIGHT may be ERROR_MARK, which means that that side of the
1683 expression was not formed using a binary or unary operator, or it
1684 was enclosed in parentheses. */
1687 warn_about_parentheses (location_t loc
, enum tree_code code
,
1688 enum tree_code code_left
, tree arg_left
,
1689 enum tree_code code_right
, tree arg_right
)
1691 if (!warn_parentheses
)
1694 /* This macro tests that the expression ARG with original tree code
1695 CODE appears to be a boolean expression. or the result of folding a
1696 boolean expression. */
1697 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
1698 (truth_value_p (TREE_CODE (ARG)) \
1699 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
1700 /* Folding may create 0 or 1 integers from other expressions. */ \
1701 || ((CODE) != INTEGER_CST \
1702 && (integer_onep (ARG) || integer_zerop (ARG))))
1707 if (code_left
== PLUS_EXPR
)
1708 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1709 "suggest parentheses around %<+%> inside %<<<%>");
1710 else if (code_right
== PLUS_EXPR
)
1711 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1712 "suggest parentheses around %<+%> inside %<<<%>");
1713 else if (code_left
== MINUS_EXPR
)
1714 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1715 "suggest parentheses around %<-%> inside %<<<%>");
1716 else if (code_right
== MINUS_EXPR
)
1717 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1718 "suggest parentheses around %<-%> inside %<<<%>");
1722 if (code_left
== PLUS_EXPR
)
1723 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1724 "suggest parentheses around %<+%> inside %<>>%>");
1725 else if (code_right
== PLUS_EXPR
)
1726 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1727 "suggest parentheses around %<+%> inside %<>>%>");
1728 else if (code_left
== MINUS_EXPR
)
1729 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1730 "suggest parentheses around %<-%> inside %<>>%>");
1731 else if (code_right
== MINUS_EXPR
)
1732 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1733 "suggest parentheses around %<-%> inside %<>>%>");
1736 case TRUTH_ORIF_EXPR
:
1737 if (code_left
== TRUTH_ANDIF_EXPR
)
1738 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1739 "suggest parentheses around %<&&%> within %<||%>");
1740 else if (code_right
== TRUTH_ANDIF_EXPR
)
1741 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1742 "suggest parentheses around %<&&%> within %<||%>");
1746 if (code_left
== BIT_AND_EXPR
|| code_left
== BIT_XOR_EXPR
1747 || code_left
== PLUS_EXPR
|| code_left
== MINUS_EXPR
)
1748 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1749 "suggest parentheses around arithmetic in operand of %<|%>");
1750 else if (code_right
== BIT_AND_EXPR
|| code_right
== BIT_XOR_EXPR
1751 || code_right
== PLUS_EXPR
|| code_right
== MINUS_EXPR
)
1752 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1753 "suggest parentheses around arithmetic in operand of %<|%>");
1754 /* Check cases like x|y==z */
1755 else if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1756 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1757 "suggest parentheses around comparison in operand of %<|%>");
1758 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1759 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1760 "suggest parentheses around comparison in operand of %<|%>");
1761 /* Check cases like !x | y */
1762 else if (code_left
== TRUTH_NOT_EXPR
1763 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right
, arg_right
))
1764 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1765 "suggest parentheses around operand of "
1766 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
1770 if (code_left
== BIT_AND_EXPR
1771 || code_left
== PLUS_EXPR
|| code_left
== MINUS_EXPR
)
1772 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1773 "suggest parentheses around arithmetic in operand of %<^%>");
1774 else if (code_right
== BIT_AND_EXPR
1775 || code_right
== PLUS_EXPR
|| code_right
== MINUS_EXPR
)
1776 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1777 "suggest parentheses around arithmetic in operand of %<^%>");
1778 /* Check cases like x^y==z */
1779 else if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1780 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1781 "suggest parentheses around comparison in operand of %<^%>");
1782 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1783 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1784 "suggest parentheses around comparison in operand of %<^%>");
1788 if (code_left
== PLUS_EXPR
)
1789 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1790 "suggest parentheses around %<+%> in operand of %<&%>");
1791 else if (code_right
== PLUS_EXPR
)
1792 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1793 "suggest parentheses around %<+%> in operand of %<&%>");
1794 else if (code_left
== MINUS_EXPR
)
1795 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1796 "suggest parentheses around %<-%> in operand of %<&%>");
1797 else if (code_right
== MINUS_EXPR
)
1798 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1799 "suggest parentheses around %<-%> in operand of %<&%>");
1800 /* Check cases like x&y==z */
1801 else if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1802 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1803 "suggest parentheses around comparison in operand of %<&%>");
1804 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1805 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1806 "suggest parentheses around comparison in operand of %<&%>");
1807 /* Check cases like !x & y */
1808 else if (code_left
== TRUTH_NOT_EXPR
1809 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right
, arg_right
))
1810 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1811 "suggest parentheses around operand of "
1812 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
1816 if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1817 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1818 "suggest parentheses around comparison in operand of %<==%>");
1819 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1820 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1821 "suggest parentheses around comparison in operand of %<==%>");
1824 if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1825 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1826 "suggest parentheses around comparison in operand of %<!=%>");
1827 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1828 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1829 "suggest parentheses around comparison in operand of %<!=%>");
1833 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
1835 if (TREE_CODE_CLASS (code_left
) == tcc_comparison
1836 && code_left
!= NE_EXPR
&& code_left
!= EQ_EXPR
1837 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left
)))
1838 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1839 "comparisons like %<X<=Y<=Z%> do not "
1840 "have their mathematical meaning");
1841 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
1842 && code_right
!= NE_EXPR
&& code_right
!= EQ_EXPR
1843 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right
)))
1844 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1845 "comparisons like %<X<=Y<=Z%> do not "
1846 "have their mathematical meaning");
1850 #undef NOT_A_BOOLEAN_EXPR_P
1853 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
1856 warn_for_unused_label (tree label
)
1858 if (!TREE_USED (label
))
1860 if (DECL_INITIAL (label
))
1861 warning (OPT_Wunused_label
, "label %q+D defined but not used", label
);
1863 warning (OPT_Wunused_label
, "label %q+D declared but not defined", label
);
1865 else if (asan_sanitize_use_after_scope ())
1867 if (asan_used_labels
== NULL
)
1868 asan_used_labels
= new hash_set
<tree
> (16);
1870 asan_used_labels
->add (label
);
1874 /* Warn for division by zero according to the value of DIVISOR. LOC
1875 is the location of the division operator. */
1878 warn_for_div_by_zero (location_t loc
, tree divisor
)
1880 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
1881 about division by zero. Do not issue a warning if DIVISOR has a
1882 floating-point type, since we consider 0.0/0.0 a valid way of
1883 generating a NaN. */
1884 if (c_inhibit_evaluation_warnings
== 0
1885 && (integer_zerop (divisor
) || fixed_zerop (divisor
)))
1886 warning_at (loc
, OPT_Wdiv_by_zero
, "division by zero");
1889 /* Warn for patterns where memset appears to be used incorrectly. The
1890 warning location should be LOC. ARG0, and ARG2 are the first and
1891 last arguments to the call, while LITERAL_ZERO_MASK has a 1 bit for
1892 each argument that was a literal zero. */
1895 warn_for_memset (location_t loc
, tree arg0
, tree arg2
,
1896 int literal_zero_mask
)
1898 arg0
= fold_for_warn (arg0
);
1899 arg2
= fold_for_warn (arg2
);
1901 if (warn_memset_transposed_args
1902 && integer_zerop (arg2
)
1903 && (literal_zero_mask
& (1 << 2)) != 0
1904 && (literal_zero_mask
& (1 << 1)) == 0)
1905 warning_at (loc
, OPT_Wmemset_transposed_args
,
1906 "%<memset%> used with constant zero length "
1907 "parameter; this could be due to transposed "
1910 if (warn_memset_elt_size
&& TREE_CODE (arg2
) == INTEGER_CST
)
1913 if (TREE_CODE (arg0
) == ADDR_EXPR
)
1914 arg0
= TREE_OPERAND (arg0
, 0);
1915 tree type
= TREE_TYPE (arg0
);
1916 if (type
!= NULL_TREE
&& TREE_CODE (type
) == ARRAY_TYPE
)
1918 tree elt_type
= TREE_TYPE (type
);
1919 tree domain
= TYPE_DOMAIN (type
);
1920 if (COMPLETE_TYPE_P (elt_type
)
1921 && !integer_onep (TYPE_SIZE_UNIT (elt_type
))
1922 && domain
!= NULL_TREE
1923 && TYPE_MAX_VALUE (domain
)
1924 && TYPE_MIN_VALUE (domain
)
1925 && integer_zerop (TYPE_MIN_VALUE (domain
))
1926 && integer_onep (fold_build2 (MINUS_EXPR
, domain
,
1928 TYPE_MAX_VALUE (domain
))))
1929 warning_at (loc
, OPT_Wmemset_elt_size
,
1930 "%<memset%> used with length equal to "
1931 "number of elements without multiplication "
1937 /* Subroutine of build_binary_op. Give warnings for comparisons
1938 between signed and unsigned quantities that may fail. Do the
1939 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
1940 so that casts will be considered, but default promotions won't
1943 LOCATION is the location of the comparison operator.
1945 The arguments of this function map directly to local variables
1946 of build_binary_op. */
1949 warn_for_sign_compare (location_t location
,
1950 tree orig_op0
, tree orig_op1
,
1952 tree result_type
, enum tree_code resultcode
)
1954 if (error_operand_p (orig_op0
) || error_operand_p (orig_op1
))
1957 int op0_signed
= !TYPE_UNSIGNED (TREE_TYPE (orig_op0
));
1958 int op1_signed
= !TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
1959 int unsignedp0
, unsignedp1
;
1961 /* In C++, check for comparison of different enum types. */
1963 && TREE_CODE (TREE_TYPE (orig_op0
)) == ENUMERAL_TYPE
1964 && TREE_CODE (TREE_TYPE (orig_op1
)) == ENUMERAL_TYPE
1965 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0
))
1966 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1
)))
1968 warning_at (location
,
1969 OPT_Wsign_compare
, "comparison between types %qT and %qT",
1970 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
));
1973 /* Do not warn if the comparison is being done in a signed type,
1974 since the signed type will only be chosen if it can represent
1975 all the values of the unsigned type. */
1976 if (!TYPE_UNSIGNED (result_type
))
1978 /* Do not warn if both operands are unsigned. */
1979 else if (op0_signed
== op1_signed
)
1983 tree sop
, uop
, base_type
;
1987 sop
= orig_op0
, uop
= orig_op1
;
1989 sop
= orig_op1
, uop
= orig_op0
;
1991 STRIP_TYPE_NOPS (sop
);
1992 STRIP_TYPE_NOPS (uop
);
1993 base_type
= (TREE_CODE (result_type
) == COMPLEX_TYPE
1994 ? TREE_TYPE (result_type
) : result_type
);
1996 /* Do not warn if the signed quantity is an unsuffixed integer
1997 literal (or some static constant expression involving such
1998 literals or a conditional expression involving such literals)
1999 and it is non-negative. */
2000 if (tree_expr_nonnegative_warnv_p (sop
, &ovf
))
2002 /* Do not warn if the comparison is an equality operation, the
2003 unsigned quantity is an integral constant, and it would fit
2004 in the result if the result were signed. */
2005 else if (TREE_CODE (uop
) == INTEGER_CST
2006 && (resultcode
== EQ_EXPR
|| resultcode
== NE_EXPR
)
2007 && int_fits_type_p (uop
, c_common_signed_type (base_type
)))
2009 /* In C, do not warn if the unsigned quantity is an enumeration
2010 constant and its maximum value would fit in the result if the
2011 result were signed. */
2012 else if (!c_dialect_cxx() && TREE_CODE (uop
) == INTEGER_CST
2013 && TREE_CODE (TREE_TYPE (uop
)) == ENUMERAL_TYPE
2014 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop
)),
2015 c_common_signed_type (base_type
)))
2018 warning_at (location
, OPT_Wsign_compare
,
2019 "comparison of integer expressions of different "
2020 "signedness: %qT and %qT", TREE_TYPE (orig_op0
),
2021 TREE_TYPE (orig_op1
));
2024 /* Warn if two unsigned values are being compared in a size larger
2025 than their original size, and one (and only one) is the result of
2026 a `~' operator. This comparison will always fail.
2028 Also warn if one operand is a constant, and the constant does not
2029 have all bits set that are set in the ~ operand when it is
2032 op0
= c_common_get_narrower (op0
, &unsignedp0
);
2033 op1
= c_common_get_narrower (op1
, &unsignedp1
);
2035 if ((TREE_CODE (op0
) == BIT_NOT_EXPR
)
2036 ^ (TREE_CODE (op1
) == BIT_NOT_EXPR
))
2038 if (TREE_CODE (op0
) == BIT_NOT_EXPR
)
2039 op0
= c_common_get_narrower (TREE_OPERAND (op0
, 0), &unsignedp0
);
2040 if (TREE_CODE (op1
) == BIT_NOT_EXPR
)
2041 op1
= c_common_get_narrower (TREE_OPERAND (op1
, 0), &unsignedp1
);
2043 if (tree_fits_shwi_p (op0
) || tree_fits_shwi_p (op1
))
2046 HOST_WIDE_INT constant
, mask
;
2050 if (tree_fits_shwi_p (op0
))
2053 unsignedp
= unsignedp1
;
2054 constant
= tree_to_shwi (op0
);
2059 unsignedp
= unsignedp0
;
2060 constant
= tree_to_shwi (op1
);
2063 bits
= TYPE_PRECISION (TREE_TYPE (primop
));
2064 if (bits
< TYPE_PRECISION (result_type
)
2065 && bits
< HOST_BITS_PER_LONG
&& unsignedp
)
2067 mask
= HOST_WIDE_INT_M1U
<< bits
;
2068 if ((mask
& constant
) != mask
)
2071 warning_at (location
, OPT_Wsign_compare
,
2072 "promoted ~unsigned is always non-zero");
2074 warning_at (location
, OPT_Wsign_compare
,
2075 "comparison of promoted ~unsigned with constant");
2079 else if (unsignedp0
&& unsignedp1
2080 && (TYPE_PRECISION (TREE_TYPE (op0
))
2081 < TYPE_PRECISION (result_type
))
2082 && (TYPE_PRECISION (TREE_TYPE (op1
))
2083 < TYPE_PRECISION (result_type
)))
2084 warning_at (location
, OPT_Wsign_compare
,
2085 "comparison of promoted ~unsigned with unsigned");
2089 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
2090 type via c_common_type. If -Wdouble-promotion is in use, and the
2091 conditions for warning have been met, issue a warning. GMSGID is
2092 the warning message. It must have two %T specifiers for the type
2093 that was converted (generally "float") and the type to which it was
2094 converted (generally "double), respectively. LOC is the location
2095 to which the warning should refer. */
2098 do_warn_double_promotion (tree result_type
, tree type1
, tree type2
,
2099 const char *gmsgid
, location_t loc
)
2103 if (!warn_double_promotion
)
2105 /* If the conversion will not occur at run-time, there is no need to
2107 if (c_inhibit_evaluation_warnings
)
2109 /* If an invalid conversion has occured, don't warn. */
2110 if (result_type
== error_mark_node
)
2112 if (TYPE_MAIN_VARIANT (result_type
) != double_type_node
2113 && TYPE_MAIN_VARIANT (result_type
) != complex_double_type_node
)
2115 if (TYPE_MAIN_VARIANT (type1
) == float_type_node
2116 || TYPE_MAIN_VARIANT (type1
) == complex_float_type_node
)
2117 source_type
= type1
;
2118 else if (TYPE_MAIN_VARIANT (type2
) == float_type_node
2119 || TYPE_MAIN_VARIANT (type2
) == complex_float_type_node
)
2120 source_type
= type2
;
2123 warning_at (loc
, OPT_Wdouble_promotion
, gmsgid
, source_type
, result_type
);
2126 /* Possibly warn about unused parameters. */
2129 do_warn_unused_parameter (tree fn
)
2133 for (decl
= DECL_ARGUMENTS (fn
);
2134 decl
; decl
= DECL_CHAIN (decl
))
2135 if (!TREE_USED (decl
) && TREE_CODE (decl
) == PARM_DECL
2136 && DECL_NAME (decl
) && !DECL_ARTIFICIAL (decl
)
2137 && !TREE_NO_WARNING (decl
))
2138 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wunused_parameter
,
2139 "unused parameter %qD", decl
);
2142 /* If DECL is a typedef that is declared in the current function,
2143 record it for the purpose of -Wunused-local-typedefs. */
2146 record_locally_defined_typedef (tree decl
)
2148 struct c_language_function
*l
;
2150 if (!warn_unused_local_typedefs
2152 /* if this is not a locally defined typedef then we are not
2154 || !is_typedef_decl (decl
)
2155 || !decl_function_context (decl
))
2158 l
= (struct c_language_function
*) cfun
->language
;
2159 vec_safe_push (l
->local_typedefs
, decl
);
2162 /* If T is a TYPE_DECL declared locally, mark it as used. */
2165 maybe_record_typedef_use (tree t
)
2167 if (!is_typedef_decl (t
))
2170 TREE_USED (t
) = true;
2173 /* Warn if there are some unused locally defined typedefs in the
2174 current function. */
2177 maybe_warn_unused_local_typedefs (void)
2181 /* The number of times we have emitted -Wunused-local-typedefs
2182 warnings. If this is different from errorcount, that means some
2183 unrelated errors have been issued. In which case, we'll avoid
2184 emitting "unused-local-typedefs" warnings. */
2185 static int unused_local_typedefs_warn_count
;
2186 struct c_language_function
*l
;
2191 if ((l
= (struct c_language_function
*) cfun
->language
) == NULL
)
2194 if (warn_unused_local_typedefs
2195 && errorcount
== unused_local_typedefs_warn_count
)
2197 FOR_EACH_VEC_SAFE_ELT (l
->local_typedefs
, i
, decl
)
2198 if (!TREE_USED (decl
))
2199 warning_at (DECL_SOURCE_LOCATION (decl
),
2200 OPT_Wunused_local_typedefs
,
2201 "typedef %qD locally defined but not used", decl
);
2202 unused_local_typedefs_warn_count
= errorcount
;
2205 vec_free (l
->local_typedefs
);
2208 /* If we're creating an if-else-if condition chain, first see if we
2209 already have this COND in the CHAIN. If so, warn and don't add COND
2210 into the vector, otherwise add the COND there. LOC is the location
2214 warn_duplicated_cond_add_or_warn (location_t loc
, tree cond
, vec
<tree
> **chain
)
2216 /* No chain has been created yet. Do nothing. */
2220 if (TREE_SIDE_EFFECTS (cond
))
2222 /* Uh-oh! This condition has a side-effect, thus invalidates
2232 FOR_EACH_VEC_ELT (**chain
, ix
, t
)
2233 if (operand_equal_p (cond
, t
, 0))
2235 if (warning_at (loc
, OPT_Wduplicated_cond
,
2236 "duplicated %<if%> condition"))
2237 inform (EXPR_LOCATION (t
), "previously used here");
2243 && !CONSTANT_CLASS_P (cond
)
2244 /* Don't infinitely grow the chain. */
2245 && (*chain
)->length () < 512)
2246 (*chain
)->safe_push (cond
);
2249 /* Check and possibly warn if two declarations have contradictory
2250 attributes, such as always_inline vs. noinline. */
2253 diagnose_mismatched_attributes (tree olddecl
, tree newdecl
)
2255 bool warned
= false;
2257 tree a1
= lookup_attribute ("optimize", DECL_ATTRIBUTES (olddecl
));
2258 tree a2
= lookup_attribute ("optimize", DECL_ATTRIBUTES (newdecl
));
2259 /* An optimization attribute applied on a declaration after the
2260 definition is likely not what the user wanted. */
2262 && DECL_SAVED_TREE (olddecl
) != NULL_TREE
2263 && (a1
== NULL_TREE
|| !attribute_list_equal (a1
, a2
)))
2264 warned
|= warning (OPT_Wattributes
,
2265 "optimization attribute on %qD follows "
2266 "definition but the attribute doesn%'t match",
2269 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2270 if (DECL_DECLARED_INLINE_P (newdecl
)
2271 && DECL_UNINLINABLE (olddecl
)
2272 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl
)))
2273 warned
|= warning (OPT_Wattributes
, "inline declaration of %qD follows "
2274 "declaration with attribute %<noinline%>", newdecl
);
2275 else if (DECL_DECLARED_INLINE_P (olddecl
)
2276 && DECL_UNINLINABLE (newdecl
)
2277 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl
)))
2278 warned
|= warning (OPT_Wattributes
, "declaration of %q+D with attribute "
2279 "%<noinline%> follows inline declaration", newdecl
);
2284 /* Warn if signed left shift overflows. We don't warn
2285 about left-shifting 1 into the sign bit in C++14; cf.
2286 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
2287 LOC is a location of the shift; OP0 and OP1 are the operands.
2288 Return true if an overflow is detected, false otherwise. */
2291 maybe_warn_shift_overflow (location_t loc
, tree op0
, tree op1
)
2293 if (TREE_CODE (op0
) != INTEGER_CST
2294 || TREE_CODE (op1
) != INTEGER_CST
)
2297 tree type0
= TREE_TYPE (op0
);
2298 unsigned int prec0
= TYPE_PRECISION (type0
);
2300 /* Left-hand operand must be signed. */
2301 if (TYPE_UNSIGNED (type0
))
2304 unsigned int min_prec
= (wi::min_precision (wi::to_wide (op0
), SIGNED
)
2305 + TREE_INT_CST_LOW (op1
));
2306 /* Handle the case of left-shifting 1 into the sign bit.
2307 * However, shifting 1 _out_ of the sign bit, as in
2308 * INT_MIN << 1, is considered an overflow.
2310 if (!tree_int_cst_sign_bit(op0
) && min_prec
== prec0
+ 1)
2312 /* Never warn for C++14 onwards. */
2313 if (cxx_dialect
>= cxx14
)
2315 /* Otherwise only if -Wshift-overflow=2. But return
2316 true to signal an overflow for the sake of integer
2317 constant expressions. */
2318 if (warn_shift_overflow
< 2)
2322 bool overflowed
= min_prec
> prec0
;
2323 if (overflowed
&& c_inhibit_evaluation_warnings
== 0)
2324 warning_at (loc
, OPT_Wshift_overflow_
,
2325 "result of %qE requires %u bits to represent, "
2326 "but %qT only has %u bits",
2327 build2_loc (loc
, LSHIFT_EXPR
, type0
, op0
, op1
),
2328 min_prec
, type0
, prec0
);
2333 /* Warn about boolean expression compared with an integer value different
2334 from true/false. Warns also e.g. about "(i1 == i2) == 2".
2335 LOC is the location of the comparison, CODE is its code, OP0 and OP1
2336 are the operands of the comparison. The caller must ensure that
2337 either operand is a boolean expression. */
2340 maybe_warn_bool_compare (location_t loc
, enum tree_code code
, tree op0
,
2343 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
2347 if (f
= fold_for_warn (op0
),
2348 TREE_CODE (f
) == INTEGER_CST
)
2350 else if (f
= fold_for_warn (op1
),
2351 TREE_CODE (f
) == INTEGER_CST
)
2356 if (!integer_zerop (cst
) && !integer_onep (cst
))
2358 int sign
= (TREE_CODE (op0
) == INTEGER_CST
2359 ? tree_int_cst_sgn (cst
) : -tree_int_cst_sgn (cst
));
2361 || ((code
== GT_EXPR
|| code
== GE_EXPR
) && sign
< 0)
2362 || ((code
== LT_EXPR
|| code
== LE_EXPR
) && sign
> 0))
2363 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2364 "with boolean expression is always false", cst
);
2366 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2367 "with boolean expression is always true", cst
);
2369 else if (integer_zerop (cst
) || integer_onep (cst
))
2371 /* If the non-constant operand isn't of a boolean type, we
2372 don't want to warn here. */
2373 tree noncst
= TREE_CODE (op0
) == INTEGER_CST
? op1
: op0
;
2374 /* Handle booleans promoted to integers. */
2375 if (bool_promoted_to_int_p (noncst
))
2377 else if (TREE_CODE (TREE_TYPE (noncst
)) != BOOLEAN_TYPE
2378 && !truth_value_p (TREE_CODE (noncst
)))
2380 /* Do some magic to get the right diagnostics. */
2381 bool flag
= TREE_CODE (op0
) == INTEGER_CST
;
2382 flag
= integer_zerop (cst
) ? flag
: !flag
;
2383 if ((code
== GE_EXPR
&& !flag
) || (code
== LE_EXPR
&& flag
))
2384 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2385 "with boolean expression is always true", cst
);
2386 else if ((code
== LT_EXPR
&& !flag
) || (code
== GT_EXPR
&& flag
))
2387 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2388 "with boolean expression is always false", cst
);
2392 /* Warn if an argument at position param_pos is passed to a
2393 restrict-qualified param, and it aliases with another argument.
2394 Return true if a warning has been issued. */
2397 warn_for_restrict (unsigned param_pos
, tree
*argarray
, unsigned nargs
)
2399 tree arg
= argarray
[param_pos
];
2400 if (TREE_VISITED (arg
) || integer_zerop (arg
))
2403 location_t loc
= EXPR_LOC_OR_LOC (arg
, input_location
);
2404 gcc_rich_location
richloc (loc
);
2407 auto_vec
<int, 16> arg_positions
;
2409 for (i
= 0; i
< nargs
; i
++)
2414 tree current_arg
= argarray
[i
];
2415 if (operand_equal_p (arg
, current_arg
, 0))
2417 TREE_VISITED (current_arg
) = 1;
2418 arg_positions
.safe_push (i
+ 1);
2422 if (arg_positions
.is_empty ())
2426 FOR_EACH_VEC_ELT (arg_positions
, i
, pos
)
2428 arg
= argarray
[pos
- 1];
2429 if (EXPR_HAS_LOCATION (arg
))
2430 richloc
.add_range (EXPR_LOCATION (arg
), false);
2433 return warning_n (&richloc
, OPT_Wrestrict
, arg_positions
.length (),
2434 "passing argument %i to restrict-qualified parameter"
2435 " aliases with argument %Z",
2436 "passing argument %i to restrict-qualified parameter"
2437 " aliases with arguments %Z",
2438 param_pos
+ 1, arg_positions
.address (),
2439 arg_positions
.length ());
2442 /* Callback function to determine whether an expression TP or one of its
2443 subexpressions comes from macro expansion. Used to suppress bogus
2447 expr_from_macro_expansion_r (tree
*tp
, int *, void *)
2449 if (CAN_HAVE_LOCATION_P (*tp
)
2450 && from_macro_expansion_at (EXPR_LOCATION (*tp
)))
2451 return integer_zero_node
;
2456 /* Possibly warn when an if-else has identical branches. */
2459 do_warn_duplicated_branches (tree expr
)
2461 tree thenb
= COND_EXPR_THEN (expr
);
2462 tree elseb
= COND_EXPR_ELSE (expr
);
2464 /* Don't bother if any of the branches is missing. */
2465 if (thenb
== NULL_TREE
|| elseb
== NULL_TREE
)
2468 /* And don't warn for empty statements. */
2469 if (TREE_CODE (thenb
) == NOP_EXPR
2470 && TREE_TYPE (thenb
) == void_type_node
2471 && TREE_OPERAND (thenb
, 0) == size_zero_node
)
2474 /* ... or empty branches. */
2475 if (TREE_CODE (thenb
) == STATEMENT_LIST
2476 && STATEMENT_LIST_HEAD (thenb
) == NULL
)
2479 /* Compute the hash of the then branch. */
2480 inchash::hash
hstate0 (0);
2481 inchash::add_expr (thenb
, hstate0
);
2482 hashval_t h0
= hstate0
.end ();
2484 /* Compute the hash of the else branch. */
2485 inchash::hash
hstate1 (0);
2486 inchash::add_expr (elseb
, hstate1
);
2487 hashval_t h1
= hstate1
.end ();
2489 /* Compare the hashes. */
2491 && operand_equal_p (thenb
, elseb
, OEP_LEXICOGRAPHIC
)
2492 /* Don't warn if any of the branches or their subexpressions comes
2494 && !walk_tree_without_duplicates (&thenb
, expr_from_macro_expansion_r
,
2496 && !walk_tree_without_duplicates (&elseb
, expr_from_macro_expansion_r
,
2498 warning_at (EXPR_LOCATION (expr
), OPT_Wduplicated_branches
,
2499 "this condition has identical branches");
2502 /* Callback for c_genericize to implement -Wduplicated-branches. */
2505 do_warn_duplicated_branches_r (tree
*tp
, int *, void *)
2507 if (TREE_CODE (*tp
) == COND_EXPR
)
2508 do_warn_duplicated_branches (*tp
);
2512 /* Implementation of -Wmultistatement-macros. This warning warns about
2513 cases when a macro expands to multiple statements not wrapped in
2514 do {} while (0) or ({ }) and is used as a body of if/else/for/while
2515 conditionals. For example,
2517 #define DOIT x++; y++
2522 will increment y unconditionally.
2524 BODY_LOC is the location of the first token in the body after labels
2525 have been parsed, NEXT_LOC is the location of the next token after the
2526 body of the conditional has been parsed, and GUARD_LOC is the location
2527 of the conditional. */
2530 warn_for_multistatement_macros (location_t body_loc
, location_t next_loc
,
2531 location_t guard_loc
, enum rid keyword
)
2533 if (!warn_multistatement_macros
)
2536 /* Ain't got time to waste. We only care about macros here. */
2537 if (!from_macro_expansion_at (body_loc
)
2538 || !from_macro_expansion_at (next_loc
))
2541 /* Let's skip macros defined in system headers. */
2542 if (in_system_header_at (body_loc
)
2543 || in_system_header_at (next_loc
))
2546 /* Find the actual tokens in the macro definition. BODY_LOC and
2547 NEXT_LOC have to come from the same spelling location, but they
2548 will resolve to different locations in the context of the macro
2550 location_t body_loc_exp
2551 = linemap_resolve_location (line_table
, body_loc
,
2552 LRK_MACRO_DEFINITION_LOCATION
, NULL
);
2553 location_t next_loc_exp
2554 = linemap_resolve_location (line_table
, next_loc
,
2555 LRK_MACRO_DEFINITION_LOCATION
, NULL
);
2556 location_t guard_loc_exp
2557 = linemap_resolve_location (line_table
, guard_loc
,
2558 LRK_MACRO_DEFINITION_LOCATION
, NULL
);
2560 /* These are some funky cases we don't want to warn about. */
2561 if (body_loc_exp
== guard_loc_exp
2562 || next_loc_exp
== guard_loc_exp
2563 || body_loc_exp
== next_loc_exp
)
2566 /* Find the macro maps for the macro expansions. */
2567 const line_map
*body_map
= linemap_lookup (line_table
, body_loc
);
2568 const line_map
*next_map
= linemap_lookup (line_table
, next_loc
);
2569 const line_map
*guard_map
= linemap_lookup (line_table
, guard_loc
);
2571 /* Now see if the following token (after the body) is coming from the
2572 same macro expansion. If it is, it might be a problem. */
2573 if (body_map
!= next_map
)
2576 /* The conditional itself must not come from the same expansion, because
2577 we don't want to warn about
2578 #define IF if (x) x++; y++
2580 if (guard_map
== body_map
)
2583 /* Handle the case where NEXT and BODY come from the same expansion while
2584 GUARD doesn't, yet we shouldn't warn. E.g.
2586 #define GUARD if (...)
2587 #define GUARD2 GUARD
2589 and in the definition of another macro:
2595 while (linemap_macro_expansion_map_p (guard_map
))
2597 const line_map_macro
*mm
= linemap_check_macro (guard_map
);
2598 guard_loc_exp
= MACRO_MAP_EXPANSION_POINT_LOCATION (mm
);
2599 guard_map
= linemap_lookup (line_table
, guard_loc_exp
);
2600 if (guard_map
== body_map
)
2604 if (warning_at (body_loc
, OPT_Wmultistatement_macros
,
2605 "macro expands to multiple statements"))
2606 inform (guard_loc
, "some parts of macro expansion are not guarded by "
2607 "this %qs clause", guard_tinfo_to_string (keyword
));