1 /* Diagnostic routines shared by all languages that are variants of C.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
29 #include "diagnostic.h"
32 #include "gcc-rich-location.h"
34 #include "c-family/c-indentation.h"
36 /* Print a warning if a constant expression had overflow in folding.
37 Invoke this function on every expression that the language
38 requires to be a constant expression.
39 Note the ANSI C standard says it is erroneous for a
40 constant expression to overflow. */
43 constant_expression_warning (tree value
)
45 if (warn_overflow
&& pedantic
46 && (TREE_CODE (value
) == INTEGER_CST
|| TREE_CODE (value
) == REAL_CST
47 || TREE_CODE (value
) == FIXED_CST
48 || TREE_CODE (value
) == VECTOR_CST
49 || TREE_CODE (value
) == COMPLEX_CST
)
50 && TREE_OVERFLOW (value
))
51 pedwarn (input_location
, OPT_Woverflow
, "overflow in constant expression");
54 /* The same as above but print an unconditional error. */
57 constant_expression_error (tree value
)
59 if ((TREE_CODE (value
) == INTEGER_CST
|| TREE_CODE (value
) == REAL_CST
60 || TREE_CODE (value
) == FIXED_CST
61 || TREE_CODE (value
) == VECTOR_CST
62 || TREE_CODE (value
) == COMPLEX_CST
)
63 && TREE_OVERFLOW (value
))
64 error ("overflow in constant expression");
67 /* Print a warning if an expression result VALUE had an overflow
68 in folding and its operands hadn't. EXPR, which may be null, is
69 the operand of the expression.
71 Invoke this function on every expression that
72 (1) appears in the source code, and
73 (2) is a constant expression that overflowed, and
74 (3) is not already checked by convert_and_check;
75 however, do not invoke this function on operands of explicit casts
76 or when the expression is the result of an operator and any operand
77 already overflowed. */
80 overflow_warning (location_t loc
, tree value
, tree expr
)
82 if (c_inhibit_evaluation_warnings
!= 0)
85 const char *warnfmt
= NULL
;
87 switch (TREE_CODE (value
))
91 ? G_("integer overflow in expression %qE of type %qT "
93 : G_("integer overflow in expression of type %qT "
99 ? G_ ("floating point overflow in expression %qE "
100 "of type %qT results in %qE")
101 : G_ ("floating point overflow in expression of type %qT "
107 ? G_("fixed-point overflow in expression %qE of type %qT "
109 : G_("fixed-point overflow in expression of type %qT "
115 ? G_("vector overflow in expression %qE of type %qT "
117 : G_("vector overflow in expression of type %qT "
122 if (TREE_CODE (TREE_REALPART (value
)) == INTEGER_CST
)
124 ? G_("complex integer overflow in expression %qE "
125 "of type %qT results in %qE")
126 : G_("complex integer overflow in expression of type %qT "
128 else if (TREE_CODE (TREE_REALPART (value
)) == REAL_CST
)
130 ? G_("complex floating point overflow in expression %qE "
131 "of type %qT results in %qE")
132 : G_("complex floating point overflow in expression "
133 "of type %qT results in %qE"));
143 warning_at (loc
, OPT_Woverflow
, warnfmt
, expr
, TREE_TYPE (expr
), value
);
145 warning_at (loc
, OPT_Woverflow
, warnfmt
, TREE_TYPE (value
), value
);
147 TREE_NO_WARNING (value
) = 1;
150 /* Helper function for walk_tree. Unwrap C_MAYBE_CONST_EXPRs in an expression
154 unwrap_c_maybe_const (tree
*tp
, int *walk_subtrees
, void *)
156 if (TREE_CODE (*tp
) == C_MAYBE_CONST_EXPR
)
158 *tp
= C_MAYBE_CONST_EXPR_EXPR (*tp
);
159 /* C_MAYBE_CONST_EXPRs don't nest. */
160 *walk_subtrees
= false;
165 /* Warn about uses of logical || / && operator in a context where it
166 is likely that the bitwise equivalent was intended by the
167 programmer. We have seen an expression in which CODE is a binary
168 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
169 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
172 warn_logical_operator (location_t location
, enum tree_code code
, tree type
,
173 enum tree_code code_left
, tree op_left
,
174 enum tree_code
ARG_UNUSED (code_right
), tree op_right
)
176 int or_op
= (code
== TRUTH_ORIF_EXPR
|| code
== TRUTH_OR_EXPR
);
177 int in0_p
, in1_p
, in_p
;
178 tree low0
, low1
, low
, high0
, high1
, high
, lhs
, rhs
, tem
;
179 bool strict_overflow_p
= false;
181 if (code
!= TRUTH_ANDIF_EXPR
182 && code
!= TRUTH_AND_EXPR
183 && code
!= TRUTH_ORIF_EXPR
184 && code
!= TRUTH_OR_EXPR
)
187 /* We don't want to warn if either operand comes from a macro
188 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
190 if (from_macro_expansion_at (EXPR_LOCATION (op_left
))
191 || from_macro_expansion_at (EXPR_LOCATION (op_right
)))
194 /* Warn if &&/|| are being used in a context where it is
195 likely that the bitwise equivalent was intended by the
196 programmer. That is, an expression such as op && MASK
197 where op should not be any boolean expression, nor a
198 constant, and mask seems to be a non-boolean integer constant. */
199 if (TREE_CODE (op_right
) == CONST_DECL
)
200 /* An enumerator counts as a constant. */
201 op_right
= DECL_INITIAL (op_right
);
202 if (!truth_value_p (code_left
)
203 && INTEGRAL_TYPE_P (TREE_TYPE (op_left
))
204 && !CONSTANT_CLASS_P (op_left
)
205 && !TREE_NO_WARNING (op_left
)
206 && TREE_CODE (op_right
) == INTEGER_CST
207 && !integer_zerop (op_right
)
208 && !integer_onep (op_right
))
211 warning_at (location
, OPT_Wlogical_op
, "logical %<or%>"
212 " applied to non-boolean constant");
214 warning_at (location
, OPT_Wlogical_op
, "logical %<and%>"
215 " applied to non-boolean constant");
216 TREE_NO_WARNING (op_left
) = true;
220 /* We do not warn for constants because they are typical of macro
221 expansions that test for features. */
222 if (CONSTANT_CLASS_P (fold_for_warn (op_left
))
223 || CONSTANT_CLASS_P (fold_for_warn (op_right
)))
226 /* This warning only makes sense with logical operands. */
227 if (!(truth_value_p (TREE_CODE (op_left
))
228 || INTEGRAL_TYPE_P (TREE_TYPE (op_left
)))
229 || !(truth_value_p (TREE_CODE (op_right
))
230 || INTEGRAL_TYPE_P (TREE_TYPE (op_right
))))
233 /* The range computations only work with scalars. */
234 if (VECTOR_TYPE_P (TREE_TYPE (op_left
))
235 || VECTOR_TYPE_P (TREE_TYPE (op_right
)))
238 /* We first test whether either side separately is trivially true
239 (with OR) or trivially false (with AND). If so, do not warn.
240 This is a common idiom for testing ranges of data types in
242 op_left
= unshare_expr (op_left
);
243 walk_tree_without_duplicates (&op_left
, unwrap_c_maybe_const
, NULL
);
244 lhs
= make_range (op_left
, &in0_p
, &low0
, &high0
, &strict_overflow_p
);
248 /* If this is an OR operation, invert both sides; now, the result
249 should be always false to get a warning. */
253 tem
= build_range_check (UNKNOWN_LOCATION
, type
, lhs
, in0_p
, low0
, high0
);
254 if (tem
&& integer_zerop (tem
))
257 op_right
= unshare_expr (op_right
);
258 walk_tree_without_duplicates (&op_right
, unwrap_c_maybe_const
, NULL
);
259 rhs
= make_range (op_right
, &in1_p
, &low1
, &high1
, &strict_overflow_p
);
263 /* If this is an OR operation, invert both sides; now, the result
264 should be always false to get a warning. */
268 tem
= build_range_check (UNKNOWN_LOCATION
, type
, rhs
, in1_p
, low1
, high1
);
269 if (tem
&& integer_zerop (tem
))
272 /* If both expressions have the same operand, if we can merge the
274 if (operand_equal_p (lhs
, rhs
, 0)
275 && merge_ranges (&in_p
, &low
, &high
, in0_p
, low0
, high0
,
278 tem
= build_range_check (UNKNOWN_LOCATION
, type
, lhs
, in_p
, low
, high
);
279 /* ... and if the range test is always false, then warn. */
280 if (tem
&& integer_zerop (tem
))
283 warning_at (location
, OPT_Wlogical_op
,
284 "logical %<or%> of collectively exhaustive tests is "
287 warning_at (location
, OPT_Wlogical_op
,
288 "logical %<and%> of mutually exclusive tests is "
291 /* Or warn if the operands have exactly the same range, e.g.
293 else if (tree_int_cst_equal (low0
, low1
)
294 && tree_int_cst_equal (high0
, high1
))
297 warning_at (location
, OPT_Wlogical_op
,
298 "logical %<or%> of equal expressions");
300 warning_at (location
, OPT_Wlogical_op
,
301 "logical %<and%> of equal expressions");
306 /* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
307 with constant indices. */
310 find_array_ref_with_const_idx_r (tree
*expr_p
, int *, void *)
314 if ((TREE_CODE (expr
) == ARRAY_REF
315 || TREE_CODE (expr
) == ARRAY_RANGE_REF
)
316 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
)
317 return integer_type_node
;
322 /* Warn if a self-comparison always evaluates to true or false. LOC
323 is the location of the comparison with code CODE, LHS and RHS are
324 operands of the comparison. */
327 warn_tautological_cmp (location_t loc
, enum tree_code code
, tree lhs
, tree rhs
)
329 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
332 /* Don't warn for various macro expansions. */
333 if (from_macro_expansion_at (loc
)
334 || from_macro_expansion_at (EXPR_LOCATION (lhs
))
335 || from_macro_expansion_at (EXPR_LOCATION (rhs
)))
338 /* We do not warn for constants because they are typical of macro
339 expansions that test for features, sizeof, and similar. */
340 if (CONSTANT_CLASS_P (fold_for_warn (lhs
))
341 || CONSTANT_CLASS_P (fold_for_warn (rhs
)))
344 /* Don't warn for e.g.
347 if (n == (long) n) ...
349 if ((CONVERT_EXPR_P (lhs
) || TREE_CODE (lhs
) == NON_LVALUE_EXPR
)
350 || (CONVERT_EXPR_P (rhs
) || TREE_CODE (rhs
) == NON_LVALUE_EXPR
))
353 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
354 It could be a NaN, and NaN never compares equal to anything, even
356 if (FLOAT_TYPE_P (TREE_TYPE (lhs
)) || FLOAT_TYPE_P (TREE_TYPE (rhs
)))
359 if (operand_equal_p (lhs
, rhs
, 0))
361 /* Don't warn about array references with constant indices;
362 these are likely to come from a macro. */
363 if (walk_tree_without_duplicates (&lhs
, find_array_ref_with_const_idx_r
,
366 const bool always_true
= (code
== EQ_EXPR
|| code
== LE_EXPR
367 || code
== GE_EXPR
|| code
== UNLE_EXPR
368 || code
== UNGE_EXPR
|| code
== UNEQ_EXPR
);
370 warning_at (loc
, OPT_Wtautological_compare
,
371 "self-comparison always evaluates to true");
373 warning_at (loc
, OPT_Wtautological_compare
,
374 "self-comparison always evaluates to false");
378 /* Return true iff EXPR only contains boolean operands, or comparisons. */
381 expr_has_boolean_operands_p (tree expr
)
385 if (CONVERT_EXPR_P (expr
))
386 return bool_promoted_to_int_p (expr
);
387 else if (UNARY_CLASS_P (expr
))
388 return expr_has_boolean_operands_p (TREE_OPERAND (expr
, 0));
389 else if (BINARY_CLASS_P (expr
))
390 return (expr_has_boolean_operands_p (TREE_OPERAND (expr
, 0))
391 && expr_has_boolean_operands_p (TREE_OPERAND (expr
, 1)));
392 else if (COMPARISON_CLASS_P (expr
))
398 /* Warn about logical not used on the left hand side operand of a comparison.
399 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
400 Do not warn if RHS is of a boolean type, a logical operator, or
404 warn_logical_not_parentheses (location_t location
, enum tree_code code
,
407 if (TREE_CODE_CLASS (code
) != tcc_comparison
408 || TREE_TYPE (rhs
) == NULL_TREE
409 || TREE_CODE (TREE_TYPE (rhs
)) == BOOLEAN_TYPE
410 || truth_value_p (TREE_CODE (rhs
)))
413 /* Don't warn for expression like !x == ~(bool1 | bool2). */
414 if (expr_has_boolean_operands_p (rhs
))
417 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
418 !(x == 0) or !(y != 0). */
419 if ((code
== EQ_EXPR
|| code
== NE_EXPR
)
420 && integer_zerop (rhs
))
423 if (warning_at (location
, OPT_Wlogical_not_parentheses
,
424 "logical not is only applied to the left hand side of "
426 && EXPR_HAS_LOCATION (lhs
))
428 location_t lhs_loc
= EXPR_LOCATION (lhs
);
429 rich_location
richloc (line_table
, lhs_loc
);
430 richloc
.add_fixit_insert_before (lhs_loc
, "(");
431 richloc
.add_fixit_insert_after (lhs_loc
, ")");
432 inform_at_rich_loc (&richloc
, "add parentheses around left hand side "
433 "expression to silence this warning");
437 /* Warn if EXP contains any computations whose results are not used.
438 Return true if a warning is printed; false otherwise. LOCUS is the
439 (potential) location of the expression. */
442 warn_if_unused_value (const_tree exp
, location_t locus
)
445 if (TREE_USED (exp
) || TREE_NO_WARNING (exp
))
448 /* Don't warn about void constructs. This includes casting to void,
449 void function calls, and statement expressions with a final cast
451 if (VOID_TYPE_P (TREE_TYPE (exp
)))
454 if (EXPR_HAS_LOCATION (exp
))
455 locus
= EXPR_LOCATION (exp
);
457 switch (TREE_CODE (exp
))
459 case PREINCREMENT_EXPR
:
460 case POSTINCREMENT_EXPR
:
461 case PREDECREMENT_EXPR
:
462 case POSTDECREMENT_EXPR
:
473 /* For a binding, warn if no side effect within it. */
474 exp
= BIND_EXPR_BODY (exp
);
478 case NON_LVALUE_EXPR
:
480 exp
= TREE_OPERAND (exp
, 0);
483 case TRUTH_ORIF_EXPR
:
484 case TRUTH_ANDIF_EXPR
:
485 /* In && or ||, warn if 2nd operand has no side effect. */
486 exp
= TREE_OPERAND (exp
, 1);
490 if (warn_if_unused_value (TREE_OPERAND (exp
, 0), locus
))
492 /* Let people do `(foo (), 0)' without a warning. */
493 if (TREE_CONSTANT (TREE_OPERAND (exp
, 1)))
495 exp
= TREE_OPERAND (exp
, 1);
499 /* If this is an expression with side effects, don't warn; this
500 case commonly appears in macro expansions. */
501 if (TREE_SIDE_EFFECTS (exp
))
506 /* Don't warn about automatic dereferencing of references, since
507 the user cannot control it. */
508 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp
, 0))) == REFERENCE_TYPE
)
510 exp
= TREE_OPERAND (exp
, 0);
516 /* Referencing a volatile value is a side effect, so don't warn. */
517 if ((DECL_P (exp
) || REFERENCE_CLASS_P (exp
))
518 && TREE_THIS_VOLATILE (exp
))
521 /* If this is an expression which has no operands, there is no value
522 to be unused. There are no such language-independent codes,
523 but front ends may define such. */
524 if (EXPRESSION_CLASS_P (exp
) && TREE_OPERAND_LENGTH (exp
) == 0)
528 return warning_at (locus
, OPT_Wunused_value
, "value computed is not used");
532 /* Print a warning about casts that might indicate violation
533 of strict aliasing rules if -Wstrict-aliasing is used and
534 strict aliasing mode is in effect. OTYPE is the original
535 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
538 strict_aliasing_warning (tree otype
, tree type
, tree expr
)
540 /* Strip pointer conversion chains and get to the correct original type. */
542 otype
= TREE_TYPE (expr
);
544 if (!(flag_strict_aliasing
545 && POINTER_TYPE_P (type
)
546 && POINTER_TYPE_P (otype
)
547 && !VOID_TYPE_P (TREE_TYPE (type
)))
548 /* If the type we are casting to is a ref-all pointer
549 dereferencing it is always valid. */
550 || TYPE_REF_CAN_ALIAS_ALL (type
))
553 if ((warn_strict_aliasing
> 1) && TREE_CODE (expr
) == ADDR_EXPR
554 && (DECL_P (TREE_OPERAND (expr
, 0))
555 || handled_component_p (TREE_OPERAND (expr
, 0))))
557 /* Casting the address of an object to non void pointer. Warn
558 if the cast breaks type based aliasing. */
559 if (!COMPLETE_TYPE_P (TREE_TYPE (type
)) && warn_strict_aliasing
== 2)
561 warning (OPT_Wstrict_aliasing
, "type-punning to incomplete type "
562 "might break strict-aliasing rules");
567 /* warn_strict_aliasing >= 3. This includes the default (3).
568 Only warn if the cast is dereferenced immediately. */
570 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr
, 0)));
571 alias_set_type set2
= get_alias_set (TREE_TYPE (type
));
575 && !alias_set_subset_of (set2
, set1
)
576 && !alias_sets_conflict_p (set1
, set2
))
578 warning (OPT_Wstrict_aliasing
, "dereferencing type-punned "
579 "pointer will break strict-aliasing rules");
582 else if (warn_strict_aliasing
== 2
583 && !alias_sets_must_conflict_p (set1
, set2
))
585 warning (OPT_Wstrict_aliasing
, "dereferencing type-punned "
586 "pointer might break strict-aliasing rules");
591 else if ((warn_strict_aliasing
== 1) && !VOID_TYPE_P (TREE_TYPE (otype
)))
593 /* At this level, warn for any conversions, even if an address is
594 not taken in the same statement. This will likely produce many
595 false positives, but could be useful to pinpoint problems that
596 are not revealed at higher levels. */
597 alias_set_type set1
= get_alias_set (TREE_TYPE (otype
));
598 alias_set_type set2
= get_alias_set (TREE_TYPE (type
));
599 if (!COMPLETE_TYPE_P (type
)
600 || !alias_sets_must_conflict_p (set1
, set2
))
602 warning (OPT_Wstrict_aliasing
, "dereferencing type-punned "
603 "pointer might break strict-aliasing rules");
611 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
612 sizeof as last operand of certain builtins. */
615 sizeof_pointer_memaccess_warning (location_t
*sizeof_arg_loc
, tree callee
,
616 vec
<tree
, va_gc
> *params
, tree
*sizeof_arg
,
617 bool (*comp_types
) (tree
, tree
))
619 tree type
, dest
= NULL_TREE
, src
= NULL_TREE
, tem
;
620 bool strop
= false, cmp
= false;
621 unsigned int idx
= ~0;
624 if (TREE_CODE (callee
) != FUNCTION_DECL
625 || DECL_BUILT_IN_CLASS (callee
) != BUILT_IN_NORMAL
626 || vec_safe_length (params
) <= 1)
629 switch (DECL_FUNCTION_CODE (callee
))
631 case BUILT_IN_STRNCMP
:
632 case BUILT_IN_STRNCASECMP
:
635 case BUILT_IN_STRNCPY
:
636 case BUILT_IN_STRNCPY_CHK
:
637 case BUILT_IN_STRNCAT
:
638 case BUILT_IN_STRNCAT_CHK
:
639 case BUILT_IN_STPNCPY
:
640 case BUILT_IN_STPNCPY_CHK
:
643 case BUILT_IN_MEMCPY
:
644 case BUILT_IN_MEMCPY_CHK
:
645 case BUILT_IN_MEMMOVE
:
646 case BUILT_IN_MEMMOVE_CHK
:
647 if (params
->length () < 3)
654 if (params
->length () < 3)
660 case BUILT_IN_MEMCMP
:
662 if (params
->length () < 3)
669 case BUILT_IN_MEMSET
:
670 case BUILT_IN_MEMSET_CHK
:
671 if (params
->length () < 3)
680 case BUILT_IN_STRNDUP
:
685 case BUILT_IN_MEMCHR
:
686 if (params
->length () < 3)
691 case BUILT_IN_SNPRINTF
:
692 case BUILT_IN_SNPRINTF_CHK
:
693 case BUILT_IN_VSNPRINTF
:
694 case BUILT_IN_VSNPRINTF_CHK
:
706 if (sizeof_arg
[idx
] == NULL
|| sizeof_arg
[idx
] == error_mark_node
)
709 type
= TYPE_P (sizeof_arg
[idx
])
710 ? sizeof_arg
[idx
] : TREE_TYPE (sizeof_arg
[idx
]);
711 if (!POINTER_TYPE_P (type
))
715 && (tem
= tree_strip_nop_conversions (dest
))
716 && POINTER_TYPE_P (TREE_TYPE (tem
))
717 && comp_types (TREE_TYPE (TREE_TYPE (tem
)), type
))
721 && (tem
= tree_strip_nop_conversions (src
))
722 && POINTER_TYPE_P (TREE_TYPE (tem
))
723 && comp_types (TREE_TYPE (TREE_TYPE (tem
)), type
))
726 loc
= sizeof_arg_loc
[idx
];
730 if (!TYPE_P (sizeof_arg
[idx
])
731 && operand_equal_p (dest
, sizeof_arg
[idx
], 0)
732 && comp_types (TREE_TYPE (dest
), type
))
734 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
735 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
736 "argument to %<sizeof%> in %qD call is the same "
737 "expression as the destination; did you mean to "
738 "remove the addressof?", callee
);
739 else if ((TYPE_PRECISION (TREE_TYPE (type
))
740 == TYPE_PRECISION (char_type_node
))
742 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
743 "argument to %<sizeof%> in %qD call is the same "
744 "expression as the destination; did you mean to "
745 "provide an explicit length?", callee
);
747 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
748 "argument to %<sizeof%> in %qD call is the same "
749 "expression as the destination; did you mean to "
750 "dereference it?", callee
);
754 if (POINTER_TYPE_P (TREE_TYPE (dest
))
756 && comp_types (TREE_TYPE (dest
), type
)
757 && !VOID_TYPE_P (TREE_TYPE (type
)))
759 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
760 "argument to %<sizeof%> in %qD call is the same "
761 "pointer type %qT as the destination; expected %qT "
762 "or an explicit length", callee
, TREE_TYPE (dest
),
763 TREE_TYPE (TREE_TYPE (dest
)));
770 if (!TYPE_P (sizeof_arg
[idx
])
771 && operand_equal_p (src
, sizeof_arg
[idx
], 0)
772 && comp_types (TREE_TYPE (src
), type
))
774 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
775 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
776 "argument to %<sizeof%> in %qD call is the same "
777 "expression as the source; did you mean to "
778 "remove the addressof?", callee
);
779 else if ((TYPE_PRECISION (TREE_TYPE (type
))
780 == TYPE_PRECISION (char_type_node
))
782 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
783 "argument to %<sizeof%> in %qD call is the same "
784 "expression as the source; did you mean to "
785 "provide an explicit length?", callee
);
787 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
788 "argument to %<sizeof%> in %qD call is the same "
789 "expression as the source; did you mean to "
790 "dereference it?", callee
);
794 if (POINTER_TYPE_P (TREE_TYPE (src
))
796 && comp_types (TREE_TYPE (src
), type
)
797 && !VOID_TYPE_P (TREE_TYPE (type
)))
799 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
800 "argument to %<sizeof%> in %qD call is the same "
801 "pointer type %qT as the source; expected %qT "
802 "or an explicit length", callee
, TREE_TYPE (src
),
803 TREE_TYPE (TREE_TYPE (src
)));
810 if (!TYPE_P (sizeof_arg
[idx
])
811 && operand_equal_p (dest
, sizeof_arg
[idx
], 0)
812 && comp_types (TREE_TYPE (dest
), type
))
814 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
815 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
816 "argument to %<sizeof%> in %qD call is the same "
817 "expression as the first source; did you mean to "
818 "remove the addressof?", callee
);
819 else if ((TYPE_PRECISION (TREE_TYPE (type
))
820 == TYPE_PRECISION (char_type_node
))
822 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
823 "argument to %<sizeof%> in %qD call is the same "
824 "expression as the first source; did you mean to "
825 "provide an explicit length?", callee
);
827 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
828 "argument to %<sizeof%> in %qD call is the same "
829 "expression as the first source; did you mean to "
830 "dereference it?", callee
);
834 if (POINTER_TYPE_P (TREE_TYPE (dest
))
836 && comp_types (TREE_TYPE (dest
), type
)
837 && !VOID_TYPE_P (TREE_TYPE (type
)))
839 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
840 "argument to %<sizeof%> in %qD call is the same "
841 "pointer type %qT as the first source; expected %qT "
842 "or an explicit length", callee
, TREE_TYPE (dest
),
843 TREE_TYPE (TREE_TYPE (dest
)));
850 if (!TYPE_P (sizeof_arg
[idx
])
851 && operand_equal_p (src
, sizeof_arg
[idx
], 0)
852 && comp_types (TREE_TYPE (src
), type
))
854 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
855 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
856 "argument to %<sizeof%> in %qD call is the same "
857 "expression as the second source; did you mean to "
858 "remove the addressof?", callee
);
859 else if ((TYPE_PRECISION (TREE_TYPE (type
))
860 == TYPE_PRECISION (char_type_node
))
862 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
863 "argument to %<sizeof%> in %qD call is the same "
864 "expression as the second source; did you mean to "
865 "provide an explicit length?", callee
);
867 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
868 "argument to %<sizeof%> in %qD call is the same "
869 "expression as the second source; did you mean to "
870 "dereference it?", callee
);
874 if (POINTER_TYPE_P (TREE_TYPE (src
))
876 && comp_types (TREE_TYPE (src
), type
)
877 && !VOID_TYPE_P (TREE_TYPE (type
)))
879 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
880 "argument to %<sizeof%> in %qD call is the same "
881 "pointer type %qT as the second source; expected %qT "
882 "or an explicit length", callee
, TREE_TYPE (src
),
883 TREE_TYPE (TREE_TYPE (src
)));
890 /* Warn for unlikely, improbable, or stupid DECL declarations
894 check_main_parameter_types (tree decl
)
896 function_args_iterator iter
;
900 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl
), type
, iter
)
902 /* XXX void_type_node belies the abstraction. */
903 if (type
== void_type_node
|| type
== error_mark_node
)
908 pedwarn (input_location
, OPT_Wmain
,
909 "%<_Atomic%>-qualified parameter type %qT of %q+D",
911 while (POINTER_TYPE_P (t
))
915 pedwarn (input_location
, OPT_Wmain
,
916 "%<_Atomic%>-qualified parameter type %qT of %q+D",
924 if (TYPE_MAIN_VARIANT (type
) != integer_type_node
)
925 pedwarn (input_location
, OPT_Wmain
,
926 "first argument of %q+D should be %<int%>", decl
);
930 if (TREE_CODE (type
) != POINTER_TYPE
931 || TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
932 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type
)))
934 pedwarn (input_location
, OPT_Wmain
,
935 "second argument of %q+D should be %<char **%>", decl
);
939 if (TREE_CODE (type
) != POINTER_TYPE
940 || TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
941 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type
)))
943 pedwarn (input_location
, OPT_Wmain
,
944 "third argument of %q+D should probably be "
945 "%<char **%>", decl
);
950 /* It is intentional that this message does not mention the third
951 argument because it's only mentioned in an appendix of the
953 if (argct
> 0 && (argct
< 2 || argct
> 3))
954 pedwarn (input_location
, OPT_Wmain
,
955 "%q+D takes only zero or two arguments", decl
);
957 if (stdarg_p (TREE_TYPE (decl
)))
958 pedwarn (input_location
, OPT_Wmain
,
959 "%q+D declared as variadic function", decl
);
962 /* Warns if the conversion of EXPR to TYPE may alter a value.
963 This is a helper function for warnings_for_convert_and_check. */
966 conversion_warning (location_t loc
, tree type
, tree expr
, tree result
)
968 tree expr_type
= TREE_TYPE (expr
);
969 enum conversion_safety conversion_kind
;
971 if (!warn_conversion
&& !warn_sign_conversion
&& !warn_float_conversion
)
974 /* This may happen, because for LHS op= RHS we preevaluate
975 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
976 means we could no longer see the code of the EXPR. */
977 if (TREE_CODE (expr
) == C_MAYBE_CONST_EXPR
)
978 expr
= C_MAYBE_CONST_EXPR_EXPR (expr
);
979 if (TREE_CODE (expr
) == SAVE_EXPR
)
980 expr
= TREE_OPERAND (expr
, 0);
982 switch (TREE_CODE (expr
))
990 case TRUTH_ANDIF_EXPR
:
991 case TRUTH_ORIF_EXPR
:
996 /* Conversion from boolean to a signed:1 bit-field (which only
997 can hold the values 0 and -1) doesn't lose information - but
998 it does change the value. */
999 if (TYPE_PRECISION (type
) == 1 && !TYPE_UNSIGNED (type
))
1000 warning_at (loc
, OPT_Wconversion
,
1001 "conversion to %qT from boolean expression", type
);
1008 conversion_kind
= unsafe_conversion_p (loc
, type
, expr
, result
, true);
1010 if (conversion_kind
== UNSAFE_REAL
)
1011 warnopt
= OPT_Wfloat_conversion
;
1012 else if (conversion_kind
)
1013 warnopt
= OPT_Wconversion
;
1017 if (TREE_CODE_CLASS (TREE_CODE (result
)) == tcc_constant
)
1018 warning_at (loc
, warnopt
,
1019 "conversion from %qT to %qT changes value from %qE to %qE",
1020 expr_type
, type
, expr
, result
);
1022 warning_at (loc
, warnopt
,
1023 "conversion from %qT to %qT changes the value of %qE",
1024 expr_type
, type
, expr
);
1029 /* In case of COND_EXPR, we do not care about the type of
1030 COND_EXPR, only about the conversion of each operand. */
1031 tree op1
= TREE_OPERAND (expr
, 1);
1032 tree op2
= TREE_OPERAND (expr
, 2);
1034 conversion_warning (loc
, type
, op1
, result
);
1035 conversion_warning (loc
, type
, op2
, result
);
1039 default: /* 'expr' is not a constant. */
1040 conversion_kind
= unsafe_conversion_p (loc
, type
, expr
, result
, true);
1041 if (conversion_kind
== UNSAFE_IMAGINARY
)
1042 warning_at (loc
, OPT_Wconversion
,
1043 "conversion from %qT to to %qT discards imaginary "
1049 if (conversion_kind
== UNSAFE_REAL
)
1050 warnopt
= OPT_Wfloat_conversion
;
1051 else if (conversion_kind
)
1052 warnopt
= OPT_Wconversion
;
1055 warning_at (loc
, warnopt
,
1056 "conversion from %qT to %qT may change value",
1062 /* Produce warnings after a conversion. RESULT is the result of
1063 converting EXPR to TYPE. This is a helper function for
1064 convert_and_check and cp_convert_and_check. */
1067 warnings_for_convert_and_check (location_t loc
, tree type
, tree expr
,
1070 loc
= expansion_point_location_if_in_system_header (loc
);
1072 bool cst
= TREE_CODE_CLASS (TREE_CODE (result
)) == tcc_constant
;
1074 tree exprtype
= TREE_TYPE (expr
);
1076 if (TREE_CODE (expr
) == INTEGER_CST
1077 && (TREE_CODE (type
) == INTEGER_TYPE
1078 || TREE_CODE (type
) == ENUMERAL_TYPE
)
1079 && !int_fits_type_p (expr
, type
))
1081 /* Do not diagnose overflow in a constant expression merely
1082 because a conversion overflowed. */
1083 if (TREE_OVERFLOW (result
))
1084 TREE_OVERFLOW (result
) = TREE_OVERFLOW (expr
);
1086 if (TYPE_UNSIGNED (type
))
1088 /* This detects cases like converting -129 or 256 to
1090 if (!int_fits_type_p (expr
, c_common_signed_type (type
)))
1093 warning_at (loc
, OPT_Woverflow
,
1094 (TYPE_UNSIGNED (exprtype
)
1095 ? G_("conversion from %qT to %qT "
1096 "changes value from %qE to %qE")
1097 : G_("unsigned conversion from %qT to %qT "
1098 "changes value from %qE to %qE")),
1099 exprtype
, type
, expr
, result
);
1101 warning_at (loc
, OPT_Woverflow
,
1102 (TYPE_UNSIGNED (exprtype
)
1103 ? G_("conversion from %qT to %qT "
1104 "changes the value of %qE")
1105 : G_("unsigned conversion from %qT to %qT "
1106 "changes the value of %qE")),
1107 exprtype
, type
, expr
);
1110 conversion_warning (loc
, type
, expr
, result
);
1112 else if (!int_fits_type_p (expr
, c_common_unsigned_type (type
)))
1115 warning_at (loc
, OPT_Woverflow
,
1116 "overflow in conversion from %qT to %qT "
1117 "changes value from %qE to %qE",
1118 exprtype
, type
, expr
, result
);
1120 warning_at (loc
, OPT_Woverflow
,
1121 "overflow in conversion from %qT to %qT "
1122 "changes the value of %qE",
1123 exprtype
, type
, expr
);
1125 /* No warning for converting 0x80000000 to int. */
1127 && (TREE_CODE (exprtype
) != INTEGER_TYPE
1128 || TYPE_PRECISION (exprtype
)
1129 != TYPE_PRECISION (type
)))
1132 warning_at (loc
, OPT_Woverflow
,
1133 "overflow in conversion from %qT to %qT "
1134 "changes value from %qE to %qE",
1135 exprtype
, type
, expr
, result
);
1137 warning_at (loc
, OPT_Woverflow
,
1138 "overflow in conversion from %qT to %qT "
1139 "changes the value of %qE",
1140 exprtype
, type
, expr
);
1143 conversion_warning (loc
, type
, expr
, result
);
1145 else if ((TREE_CODE (result
) == INTEGER_CST
1146 || TREE_CODE (result
) == FIXED_CST
) && TREE_OVERFLOW (result
))
1149 warning_at (loc
, OPT_Woverflow
,
1150 "overflow in conversion from %qT to %qT "
1151 "chages value from %qE to %qE",
1152 exprtype
, type
, expr
, result
);
1154 warning_at (loc
, OPT_Woverflow
,
1155 "overflow in conversion from %qT to %qT "
1156 "chages the value of %qE",
1157 exprtype
, type
, expr
);
1160 conversion_warning (loc
, type
, expr
, result
);
1163 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
1164 Used to verify that case values match up with enumerator values. */
1167 match_case_to_enum_1 (tree key
, tree type
, tree label
)
1169 /* Avoid warning about enums that have no enumerators. */
1170 if (TYPE_VALUES (type
) == NULL_TREE
)
1173 char buf
[WIDE_INT_PRINT_BUFFER_SIZE
];
1175 if (tree_fits_uhwi_p (key
))
1176 print_dec (key
, buf
, UNSIGNED
);
1177 else if (tree_fits_shwi_p (key
))
1178 print_dec (key
, buf
, SIGNED
);
1180 print_hex (key
, buf
);
1182 if (TYPE_NAME (type
) == NULL_TREE
)
1183 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label
)),
1184 warn_switch
? OPT_Wswitch
: OPT_Wswitch_enum
,
1185 "case value %qs not in enumerated type",
1188 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label
)),
1189 warn_switch
? OPT_Wswitch
: OPT_Wswitch_enum
,
1190 "case value %qs not in enumerated type %qT",
1194 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
1195 Used to verify that case values match up with enumerator values. */
1198 match_case_to_enum (splay_tree_node node
, void *data
)
1200 tree label
= (tree
) node
->value
;
1201 tree type
= (tree
) data
;
1203 /* Skip default case. */
1204 if (!CASE_LOW (label
))
1207 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
1208 when we did our enum->case scan. Reset our scratch bit after. */
1209 if (!CASE_LOW_SEEN (label
))
1210 match_case_to_enum_1 (CASE_LOW (label
), type
, label
);
1212 CASE_LOW_SEEN (label
) = 0;
1214 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
1215 not set, that means that CASE_HIGH did not appear when we did our
1216 enum->case scan. Reset our scratch bit after. */
1217 if (CASE_HIGH (label
))
1219 if (!CASE_HIGH_SEEN (label
))
1220 match_case_to_enum_1 (CASE_HIGH (label
), type
, label
);
1222 CASE_HIGH_SEEN (label
) = 0;
1228 /* Handle -Wswitch*. Called from the front end after parsing the
1229 switch construct. */
1230 /* ??? Should probably be somewhere generic, since other languages
1231 besides C and C++ would want this. At the moment, however, C/C++
1232 are the only tree-ssa languages that support enumerations at all,
1233 so the point is moot. */
1236 c_do_switch_warnings (splay_tree cases
, location_t switch_location
,
1237 tree type
, tree cond
, bool bool_cond_p
,
1238 bool outside_range_p
)
1240 splay_tree_node default_node
;
1241 splay_tree_node node
;
1244 if (!warn_switch
&& !warn_switch_enum
&& !warn_switch_default
1245 && !warn_switch_bool
)
1248 default_node
= splay_tree_lookup (cases
, (splay_tree_key
) NULL
);
1250 warning_at (switch_location
, OPT_Wswitch_default
,
1251 "switch missing default case");
1253 /* There are certain cases where -Wswitch-bool warnings aren't
1260 so be careful here. */
1261 if (warn_switch_bool
&& bool_cond_p
)
1263 splay_tree_node min_node
;
1264 /* If there's a default node, it's also the value with the minimal
1265 key. So look at the penultimate key (if any). */
1267 min_node
= splay_tree_successor (cases
, (splay_tree_key
) NULL
);
1269 min_node
= splay_tree_min (cases
);
1270 tree min
= min_node
? (tree
) min_node
->key
: NULL_TREE
;
1272 splay_tree_node max_node
= splay_tree_max (cases
);
1273 /* This might be a case range, so look at the value with the
1274 maximal key and then check CASE_HIGH. */
1275 tree max
= max_node
? (tree
) max_node
->value
: NULL_TREE
;
1277 max
= CASE_HIGH (max
) ? CASE_HIGH (max
) : CASE_LOW (max
);
1279 /* If there's a case value > 1 or < 0, that is outside bool
1282 || (max
&& wi::gts_p (max
, 1))
1283 || (min
&& wi::lts_p (min
, 0))
1291 case, where we want to warn. */
1293 && max
&& wi::eq_p (max
, 1)
1294 && min
&& wi::eq_p (min
, 0)))
1295 warning_at (switch_location
, OPT_Wswitch_bool
,
1296 "switch condition has boolean value");
1299 /* From here on, we only care about enumerated types. */
1300 if (!type
|| TREE_CODE (type
) != ENUMERAL_TYPE
)
1303 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
1304 if (!warn_switch_enum
&& !warn_switch
)
1307 /* Check the cases. Warn about case values which are not members of
1308 the enumerated type. For -Wswitch-enum, or for -Wswitch when
1309 there is no default case, check that exactly all enumeration
1310 literals are covered by the cases. */
1312 /* Clearing COND if it is not an integer constant simplifies
1313 the tests inside the loop below. */
1314 if (TREE_CODE (cond
) != INTEGER_CST
)
1317 /* The time complexity here is O(N*lg(N)) worst case, but for the
1318 common case of monotonically increasing enumerators, it is
1319 O(N), since the nature of the splay tree will keep the next
1320 element adjacent to the root at all times. */
1322 for (chain
= TYPE_VALUES (type
); chain
; chain
= TREE_CHAIN (chain
))
1324 tree value
= TREE_VALUE (chain
);
1325 if (TREE_CODE (value
) == CONST_DECL
)
1326 value
= DECL_INITIAL (value
);
1327 node
= splay_tree_lookup (cases
, (splay_tree_key
) value
);
1330 /* Mark the CASE_LOW part of the case entry as seen. */
1331 tree label
= (tree
) node
->value
;
1332 CASE_LOW_SEEN (label
) = 1;
1336 /* Even though there wasn't an exact match, there might be a
1337 case range which includes the enumerator's value. */
1338 node
= splay_tree_predecessor (cases
, (splay_tree_key
) value
);
1339 if (node
&& CASE_HIGH ((tree
) node
->value
))
1341 tree label
= (tree
) node
->value
;
1342 int cmp
= tree_int_cst_compare (CASE_HIGH (label
), value
);
1345 /* If we match the upper bound exactly, mark the CASE_HIGH
1346 part of the case entry as seen. */
1348 CASE_HIGH_SEEN (label
) = 1;
1353 /* We've now determined that this enumerated literal isn't
1354 handled by the case labels of the switch statement. */
1356 /* If the switch expression is a constant, we only really care
1357 about whether that constant is handled by the switch. */
1358 if (cond
&& tree_int_cst_compare (cond
, value
))
1361 /* If there is a default_node, the only relevant option is
1362 Wswitch-enum. Otherwise, if both are enabled then we prefer
1363 to warn using -Wswitch because -Wswitch is enabled by -Wall
1364 while -Wswitch-enum is explicit. */
1365 warning_at (switch_location
,
1366 (default_node
|| !warn_switch
1369 "enumeration value %qE not handled in switch",
1370 TREE_PURPOSE (chain
));
1373 /* Warn if there are case expressions that don't correspond to
1374 enumerators. This can occur since C and C++ don't enforce
1375 type-checking of assignments to enumeration variables.
1377 The time complexity here is now always O(N) worst case, since
1378 we should have marked both the lower bound and upper bound of
1379 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
1380 above. This scan also resets those fields. */
1382 splay_tree_foreach (cases
, match_case_to_enum
, type
);
1385 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
1386 expression, because B will always be true. */
1389 warn_for_omitted_condop (location_t location
, tree cond
)
1391 /* In C++ template declarations it can happen that the type is dependent
1392 and not yet known, thus TREE_TYPE (cond) == NULL_TREE. */
1393 if (truth_value_p (TREE_CODE (cond
))
1394 || (TREE_TYPE (cond
) != NULL_TREE
1395 && TREE_CODE (TREE_TYPE (cond
)) == BOOLEAN_TYPE
))
1396 warning_at (location
, OPT_Wparentheses
,
1397 "the omitted middle operand in ?: will always be %<true%>, "
1398 "suggest explicit middle operand");
1401 /* Give an error for storing into ARG, which is 'const'. USE indicates
1402 how ARG was being used. */
1405 readonly_error (location_t loc
, tree arg
, enum lvalue_use use
)
1407 gcc_assert (use
== lv_assign
|| use
== lv_increment
|| use
== lv_decrement
1409 /* Using this macro rather than (for example) arrays of messages
1410 ensures that all the format strings are checked at compile
1412 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
1413 : (use == lv_increment ? (I) \
1414 : (use == lv_decrement ? (D) : (AS))))
1415 if (TREE_CODE (arg
) == COMPONENT_REF
)
1417 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg
, 0))))
1418 error_at (loc
, READONLY_MSG (G_("assignment of member "
1419 "%qD in read-only object"),
1420 G_("increment of member "
1421 "%qD in read-only object"),
1422 G_("decrement of member "
1423 "%qD in read-only object"),
1424 G_("member %qD in read-only object "
1425 "used as %<asm%> output")),
1426 TREE_OPERAND (arg
, 1));
1428 error_at (loc
, READONLY_MSG (G_("assignment of read-only member %qD"),
1429 G_("increment of read-only member %qD"),
1430 G_("decrement of read-only member %qD"),
1431 G_("read-only member %qD used as %<asm%> output")),
1432 TREE_OPERAND (arg
, 1));
1434 else if (VAR_P (arg
))
1435 error_at (loc
, READONLY_MSG (G_("assignment of read-only variable %qD"),
1436 G_("increment of read-only variable %qD"),
1437 G_("decrement of read-only variable %qD"),
1438 G_("read-only variable %qD used as %<asm%> output")),
1440 else if (TREE_CODE (arg
) == PARM_DECL
)
1441 error_at (loc
, READONLY_MSG (G_("assignment of read-only parameter %qD"),
1442 G_("increment of read-only parameter %qD"),
1443 G_("decrement of read-only parameter %qD"),
1444 G_("read-only parameter %qD use as %<asm%> output")),
1446 else if (TREE_CODE (arg
) == RESULT_DECL
)
1448 gcc_assert (c_dialect_cxx ());
1449 error_at (loc
, READONLY_MSG (G_("assignment of "
1450 "read-only named return value %qD"),
1452 "read-only named return value %qD"),
1454 "read-only named return value %qD"),
1455 G_("read-only named return value %qD "
1456 "used as %<asm%>output")),
1459 else if (TREE_CODE (arg
) == FUNCTION_DECL
)
1460 error_at (loc
, READONLY_MSG (G_("assignment of function %qD"),
1461 G_("increment of function %qD"),
1462 G_("decrement of function %qD"),
1463 G_("function %qD used as %<asm%> output")),
1466 error_at (loc
, READONLY_MSG (G_("assignment of read-only location %qE"),
1467 G_("increment of read-only location %qE"),
1468 G_("decrement of read-only location %qE"),
1469 G_("read-only location %qE used as %<asm%> output")),
1473 /* Print an error message for an invalid lvalue. USE says
1474 how the lvalue is being used and so selects the error message. LOC
1475 is the location for the error. */
1478 lvalue_error (location_t loc
, enum lvalue_use use
)
1483 error_at (loc
, "lvalue required as left operand of assignment");
1486 error_at (loc
, "lvalue required as increment operand");
1489 error_at (loc
, "lvalue required as decrement operand");
1492 error_at (loc
, "lvalue required as unary %<&%> operand");
1495 error_at (loc
, "lvalue required in asm statement");
1502 /* Print an error message for an invalid indirection of type TYPE.
1503 ERRSTRING is the name of the operator for the indirection. */
1506 invalid_indirection_error (location_t loc
, tree type
, ref_operator errstring
)
1511 gcc_assert (c_dialect_cxx ());
1512 error_at (loc
, "invalid type argument (have %qT)", type
);
1514 case RO_ARRAY_INDEXING
:
1516 "invalid type argument of array indexing (have %qT)",
1521 "invalid type argument of unary %<*%> (have %qT)",
1526 "invalid type argument of %<->%> (have %qT)",
1531 "invalid type argument of %<->*%> (have %qT)",
1534 case RO_IMPLICIT_CONVERSION
:
1536 "invalid type argument of implicit conversion (have %qT)",
1544 /* Subscripting with type char is likely to lose on a machine where
1545 chars are signed. So warn on any machine, but optionally. Don't
1546 warn for unsigned char since that type is safe. Don't warn for
1547 signed char because anyone who uses that must have done so
1548 deliberately. Furthermore, we reduce the false positive load by
1549 warning only for non-constant value of type char. */
1552 warn_array_subscript_with_type_char (location_t loc
, tree index
)
1554 if (TYPE_MAIN_VARIANT (TREE_TYPE (index
)) == char_type_node
1555 && TREE_CODE (index
) != INTEGER_CST
)
1556 warning_at (loc
, OPT_Wchar_subscripts
,
1557 "array subscript has type %<char%>");
1560 /* Implement -Wparentheses for the unexpected C precedence rules, to
1561 cover cases like x + y << z which readers are likely to
1562 misinterpret. We have seen an expression in which CODE is a binary
1563 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
1564 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
1565 CODE_RIGHT may be ERROR_MARK, which means that that side of the
1566 expression was not formed using a binary or unary operator, or it
1567 was enclosed in parentheses. */
1570 warn_about_parentheses (location_t loc
, enum tree_code code
,
1571 enum tree_code code_left
, tree arg_left
,
1572 enum tree_code code_right
, tree arg_right
)
1574 if (!warn_parentheses
)
1577 /* This macro tests that the expression ARG with original tree code
1578 CODE appears to be a boolean expression. or the result of folding a
1579 boolean expression. */
1580 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
1581 (truth_value_p (TREE_CODE (ARG)) \
1582 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
1583 /* Folding may create 0 or 1 integers from other expressions. */ \
1584 || ((CODE) != INTEGER_CST \
1585 && (integer_onep (ARG) || integer_zerop (ARG))))
1590 if (code_left
== PLUS_EXPR
)
1591 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1592 "suggest parentheses around %<+%> inside %<<<%>");
1593 else if (code_right
== PLUS_EXPR
)
1594 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1595 "suggest parentheses around %<+%> inside %<<<%>");
1596 else if (code_left
== MINUS_EXPR
)
1597 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1598 "suggest parentheses around %<-%> inside %<<<%>");
1599 else if (code_right
== MINUS_EXPR
)
1600 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1601 "suggest parentheses around %<-%> inside %<<<%>");
1605 if (code_left
== PLUS_EXPR
)
1606 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1607 "suggest parentheses around %<+%> inside %<>>%>");
1608 else if (code_right
== PLUS_EXPR
)
1609 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1610 "suggest parentheses around %<+%> inside %<>>%>");
1611 else if (code_left
== MINUS_EXPR
)
1612 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1613 "suggest parentheses around %<-%> inside %<>>%>");
1614 else if (code_right
== MINUS_EXPR
)
1615 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1616 "suggest parentheses around %<-%> inside %<>>%>");
1619 case TRUTH_ORIF_EXPR
:
1620 if (code_left
== TRUTH_ANDIF_EXPR
)
1621 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1622 "suggest parentheses around %<&&%> within %<||%>");
1623 else if (code_right
== TRUTH_ANDIF_EXPR
)
1624 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1625 "suggest parentheses around %<&&%> within %<||%>");
1629 if (code_left
== BIT_AND_EXPR
|| code_left
== BIT_XOR_EXPR
1630 || code_left
== PLUS_EXPR
|| code_left
== MINUS_EXPR
)
1631 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1632 "suggest parentheses around arithmetic in operand of %<|%>");
1633 else if (code_right
== BIT_AND_EXPR
|| code_right
== BIT_XOR_EXPR
1634 || code_right
== PLUS_EXPR
|| code_right
== MINUS_EXPR
)
1635 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1636 "suggest parentheses around arithmetic in operand of %<|%>");
1637 /* Check cases like x|y==z */
1638 else if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1639 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1640 "suggest parentheses around comparison in operand of %<|%>");
1641 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1642 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1643 "suggest parentheses around comparison in operand of %<|%>");
1644 /* Check cases like !x | y */
1645 else if (code_left
== TRUTH_NOT_EXPR
1646 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right
, arg_right
))
1647 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1648 "suggest parentheses around operand of "
1649 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
1653 if (code_left
== BIT_AND_EXPR
1654 || code_left
== PLUS_EXPR
|| code_left
== MINUS_EXPR
)
1655 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1656 "suggest parentheses around arithmetic in operand of %<^%>");
1657 else if (code_right
== BIT_AND_EXPR
1658 || code_right
== PLUS_EXPR
|| code_right
== MINUS_EXPR
)
1659 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1660 "suggest parentheses around arithmetic in operand of %<^%>");
1661 /* Check cases like x^y==z */
1662 else if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1663 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1664 "suggest parentheses around comparison in operand of %<^%>");
1665 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1666 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1667 "suggest parentheses around comparison in operand of %<^%>");
1671 if (code_left
== PLUS_EXPR
)
1672 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1673 "suggest parentheses around %<+%> in operand of %<&%>");
1674 else if (code_right
== PLUS_EXPR
)
1675 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1676 "suggest parentheses around %<+%> in operand of %<&%>");
1677 else if (code_left
== MINUS_EXPR
)
1678 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1679 "suggest parentheses around %<-%> in operand of %<&%>");
1680 else if (code_right
== MINUS_EXPR
)
1681 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1682 "suggest parentheses around %<-%> in operand of %<&%>");
1683 /* Check cases like x&y==z */
1684 else if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1685 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1686 "suggest parentheses around comparison in operand of %<&%>");
1687 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1688 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1689 "suggest parentheses around comparison in operand of %<&%>");
1690 /* Check cases like !x & y */
1691 else if (code_left
== TRUTH_NOT_EXPR
1692 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right
, arg_right
))
1693 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1694 "suggest parentheses around operand of "
1695 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
1699 if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1700 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1701 "suggest parentheses around comparison in operand of %<==%>");
1702 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1703 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1704 "suggest parentheses around comparison in operand of %<==%>");
1707 if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
1708 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1709 "suggest parentheses around comparison in operand of %<!=%>");
1710 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
1711 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1712 "suggest parentheses around comparison in operand of %<!=%>");
1716 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
1718 if (TREE_CODE_CLASS (code_left
) == tcc_comparison
1719 && code_left
!= NE_EXPR
&& code_left
!= EQ_EXPR
1720 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left
)))
1721 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
1722 "comparisons like %<X<=Y<=Z%> do not "
1723 "have their mathematical meaning");
1724 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
1725 && code_right
!= NE_EXPR
&& code_right
!= EQ_EXPR
1726 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right
)))
1727 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
1728 "comparisons like %<X<=Y<=Z%> do not "
1729 "have their mathematical meaning");
1733 #undef NOT_A_BOOLEAN_EXPR_P
1736 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
1739 warn_for_unused_label (tree label
)
1741 if (!TREE_USED (label
))
1743 if (DECL_INITIAL (label
))
1744 warning (OPT_Wunused_label
, "label %q+D defined but not used", label
);
1746 warning (OPT_Wunused_label
, "label %q+D declared but not defined", label
);
1748 else if (asan_sanitize_use_after_scope ())
1750 if (asan_used_labels
== NULL
)
1751 asan_used_labels
= new hash_set
<tree
> (16);
1753 asan_used_labels
->add (label
);
1757 /* Warn for division by zero according to the value of DIVISOR. LOC
1758 is the location of the division operator. */
1761 warn_for_div_by_zero (location_t loc
, tree divisor
)
1763 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
1764 about division by zero. Do not issue a warning if DIVISOR has a
1765 floating-point type, since we consider 0.0/0.0 a valid way of
1766 generating a NaN. */
1767 if (c_inhibit_evaluation_warnings
== 0
1768 && (integer_zerop (divisor
) || fixed_zerop (divisor
)))
1769 warning_at (loc
, OPT_Wdiv_by_zero
, "division by zero");
1772 /* Warn for patterns where memset appears to be used incorrectly. The
1773 warning location should be LOC. ARG0, and ARG2 are the first and
1774 last arguments to the call, while LITERAL_ZERO_MASK has a 1 bit for
1775 each argument that was a literal zero. */
1778 warn_for_memset (location_t loc
, tree arg0
, tree arg2
,
1779 int literal_zero_mask
)
1781 if (warn_memset_transposed_args
1782 && integer_zerop (arg2
)
1783 && (literal_zero_mask
& (1 << 2)) != 0
1784 && (literal_zero_mask
& (1 << 1)) == 0)
1785 warning_at (loc
, OPT_Wmemset_transposed_args
,
1786 "%<memset%> used with constant zero length "
1787 "parameter; this could be due to transposed "
1790 if (warn_memset_elt_size
&& TREE_CODE (arg2
) == INTEGER_CST
)
1793 if (TREE_CODE (arg0
) == ADDR_EXPR
)
1794 arg0
= TREE_OPERAND (arg0
, 0);
1795 tree type
= TREE_TYPE (arg0
);
1796 if (type
!= NULL_TREE
&& TREE_CODE (type
) == ARRAY_TYPE
)
1798 tree elt_type
= TREE_TYPE (type
);
1799 tree domain
= TYPE_DOMAIN (type
);
1800 if (!integer_onep (TYPE_SIZE_UNIT (elt_type
))
1801 && domain
!= NULL_TREE
1802 && TYPE_MAXVAL (domain
)
1803 && TYPE_MINVAL (domain
)
1804 && integer_zerop (TYPE_MINVAL (domain
))
1805 && integer_onep (fold_build2 (MINUS_EXPR
, domain
,
1807 TYPE_MAXVAL (domain
))))
1808 warning_at (loc
, OPT_Wmemset_elt_size
,
1809 "%<memset%> used with length equal to "
1810 "number of elements without multiplication "
1816 /* Subroutine of build_binary_op. Give warnings for comparisons
1817 between signed and unsigned quantities that may fail. Do the
1818 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
1819 so that casts will be considered, but default promotions won't
1822 LOCATION is the location of the comparison operator.
1824 The arguments of this function map directly to local variables
1825 of build_binary_op. */
1828 warn_for_sign_compare (location_t location
,
1829 tree orig_op0
, tree orig_op1
,
1831 tree result_type
, enum tree_code resultcode
)
1833 int op0_signed
= !TYPE_UNSIGNED (TREE_TYPE (orig_op0
));
1834 int op1_signed
= !TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
1835 int unsignedp0
, unsignedp1
;
1837 /* In C++, check for comparison of different enum types. */
1839 && TREE_CODE (TREE_TYPE (orig_op0
)) == ENUMERAL_TYPE
1840 && TREE_CODE (TREE_TYPE (orig_op1
)) == ENUMERAL_TYPE
1841 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0
))
1842 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1
)))
1844 warning_at (location
,
1845 OPT_Wsign_compare
, "comparison between types %qT and %qT",
1846 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
));
1849 /* Do not warn if the comparison is being done in a signed type,
1850 since the signed type will only be chosen if it can represent
1851 all the values of the unsigned type. */
1852 if (!TYPE_UNSIGNED (result_type
))
1854 /* Do not warn if both operands are unsigned. */
1855 else if (op0_signed
== op1_signed
)
1859 tree sop
, uop
, base_type
;
1863 sop
= orig_op0
, uop
= orig_op1
;
1865 sop
= orig_op1
, uop
= orig_op0
;
1867 STRIP_TYPE_NOPS (sop
);
1868 STRIP_TYPE_NOPS (uop
);
1869 base_type
= (TREE_CODE (result_type
) == COMPLEX_TYPE
1870 ? TREE_TYPE (result_type
) : result_type
);
1872 /* Do not warn if the signed quantity is an unsuffixed integer
1873 literal (or some static constant expression involving such
1874 literals or a conditional expression involving such literals)
1875 and it is non-negative. */
1876 if (tree_expr_nonnegative_warnv_p (sop
, &ovf
))
1878 /* Do not warn if the comparison is an equality operation, the
1879 unsigned quantity is an integral constant, and it would fit
1880 in the result if the result were signed. */
1881 else if (TREE_CODE (uop
) == INTEGER_CST
1882 && (resultcode
== EQ_EXPR
|| resultcode
== NE_EXPR
)
1883 && int_fits_type_p (uop
, c_common_signed_type (base_type
)))
1885 /* In C, do not warn if the unsigned quantity is an enumeration
1886 constant and its maximum value would fit in the result if the
1887 result were signed. */
1888 else if (!c_dialect_cxx() && TREE_CODE (uop
) == INTEGER_CST
1889 && TREE_CODE (TREE_TYPE (uop
)) == ENUMERAL_TYPE
1890 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop
)),
1891 c_common_signed_type (base_type
)))
1894 warning_at (location
,
1896 "comparison between signed and unsigned integer expressions");
1899 /* Warn if two unsigned values are being compared in a size larger
1900 than their original size, and one (and only one) is the result of
1901 a `~' operator. This comparison will always fail.
1903 Also warn if one operand is a constant, and the constant does not
1904 have all bits set that are set in the ~ operand when it is
1907 op0
= c_common_get_narrower (op0
, &unsignedp0
);
1908 op1
= c_common_get_narrower (op1
, &unsignedp1
);
1910 if ((TREE_CODE (op0
) == BIT_NOT_EXPR
)
1911 ^ (TREE_CODE (op1
) == BIT_NOT_EXPR
))
1913 if (TREE_CODE (op0
) == BIT_NOT_EXPR
)
1914 op0
= c_common_get_narrower (TREE_OPERAND (op0
, 0), &unsignedp0
);
1915 if (TREE_CODE (op1
) == BIT_NOT_EXPR
)
1916 op1
= c_common_get_narrower (TREE_OPERAND (op1
, 0), &unsignedp1
);
1918 if (tree_fits_shwi_p (op0
) || tree_fits_shwi_p (op1
))
1921 HOST_WIDE_INT constant
, mask
;
1925 if (tree_fits_shwi_p (op0
))
1928 unsignedp
= unsignedp1
;
1929 constant
= tree_to_shwi (op0
);
1934 unsignedp
= unsignedp0
;
1935 constant
= tree_to_shwi (op1
);
1938 bits
= TYPE_PRECISION (TREE_TYPE (primop
));
1939 if (bits
< TYPE_PRECISION (result_type
)
1940 && bits
< HOST_BITS_PER_LONG
&& unsignedp
)
1942 mask
= HOST_WIDE_INT_M1U
<< bits
;
1943 if ((mask
& constant
) != mask
)
1946 warning_at (location
, OPT_Wsign_compare
,
1947 "promoted ~unsigned is always non-zero");
1949 warning_at (location
, OPT_Wsign_compare
,
1950 "comparison of promoted ~unsigned with constant");
1954 else if (unsignedp0
&& unsignedp1
1955 && (TYPE_PRECISION (TREE_TYPE (op0
))
1956 < TYPE_PRECISION (result_type
))
1957 && (TYPE_PRECISION (TREE_TYPE (op1
))
1958 < TYPE_PRECISION (result_type
)))
1959 warning_at (location
, OPT_Wsign_compare
,
1960 "comparison of promoted ~unsigned with unsigned");
1964 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
1965 type via c_common_type. If -Wdouble-promotion is in use, and the
1966 conditions for warning have been met, issue a warning. GMSGID is
1967 the warning message. It must have two %T specifiers for the type
1968 that was converted (generally "float") and the type to which it was
1969 converted (generally "double), respectively. LOC is the location
1970 to which the warning should refer. */
1973 do_warn_double_promotion (tree result_type
, tree type1
, tree type2
,
1974 const char *gmsgid
, location_t loc
)
1978 if (!warn_double_promotion
)
1980 /* If the conversion will not occur at run-time, there is no need to
1982 if (c_inhibit_evaluation_warnings
)
1984 /* If an invalid conversion has occured, don't warn. */
1985 if (result_type
== error_mark_node
)
1987 if (TYPE_MAIN_VARIANT (result_type
) != double_type_node
1988 && TYPE_MAIN_VARIANT (result_type
) != complex_double_type_node
)
1990 if (TYPE_MAIN_VARIANT (type1
) == float_type_node
1991 || TYPE_MAIN_VARIANT (type1
) == complex_float_type_node
)
1992 source_type
= type1
;
1993 else if (TYPE_MAIN_VARIANT (type2
) == float_type_node
1994 || TYPE_MAIN_VARIANT (type2
) == complex_float_type_node
)
1995 source_type
= type2
;
1998 warning_at (loc
, OPT_Wdouble_promotion
, gmsgid
, source_type
, result_type
);
2001 /* Possibly warn about unused parameters. */
2004 do_warn_unused_parameter (tree fn
)
2008 for (decl
= DECL_ARGUMENTS (fn
);
2009 decl
; decl
= DECL_CHAIN (decl
))
2010 if (!TREE_USED (decl
) && TREE_CODE (decl
) == PARM_DECL
2011 && DECL_NAME (decl
) && !DECL_ARTIFICIAL (decl
)
2012 && !TREE_NO_WARNING (decl
))
2013 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wunused_parameter
,
2014 "unused parameter %qD", decl
);
2017 /* If DECL is a typedef that is declared in the current function,
2018 record it for the purpose of -Wunused-local-typedefs. */
2021 record_locally_defined_typedef (tree decl
)
2023 struct c_language_function
*l
;
2025 if (!warn_unused_local_typedefs
2027 /* if this is not a locally defined typedef then we are not
2029 || !is_typedef_decl (decl
)
2030 || !decl_function_context (decl
))
2033 l
= (struct c_language_function
*) cfun
->language
;
2034 vec_safe_push (l
->local_typedefs
, decl
);
2037 /* If T is a TYPE_DECL declared locally, mark it as used. */
2040 maybe_record_typedef_use (tree t
)
2042 if (!is_typedef_decl (t
))
2045 TREE_USED (t
) = true;
2048 /* Warn if there are some unused locally defined typedefs in the
2049 current function. */
2052 maybe_warn_unused_local_typedefs (void)
2056 /* The number of times we have emitted -Wunused-local-typedefs
2057 warnings. If this is different from errorcount, that means some
2058 unrelated errors have been issued. In which case, we'll avoid
2059 emitting "unused-local-typedefs" warnings. */
2060 static int unused_local_typedefs_warn_count
;
2061 struct c_language_function
*l
;
2066 if ((l
= (struct c_language_function
*) cfun
->language
) == NULL
)
2069 if (warn_unused_local_typedefs
2070 && errorcount
== unused_local_typedefs_warn_count
)
2072 FOR_EACH_VEC_SAFE_ELT (l
->local_typedefs
, i
, decl
)
2073 if (!TREE_USED (decl
))
2074 warning_at (DECL_SOURCE_LOCATION (decl
),
2075 OPT_Wunused_local_typedefs
,
2076 "typedef %qD locally defined but not used", decl
);
2077 unused_local_typedefs_warn_count
= errorcount
;
2080 vec_free (l
->local_typedefs
);
2083 /* If we're creating an if-else-if condition chain, first see if we
2084 already have this COND in the CHAIN. If so, warn and don't add COND
2085 into the vector, otherwise add the COND there. LOC is the location
2089 warn_duplicated_cond_add_or_warn (location_t loc
, tree cond
, vec
<tree
> **chain
)
2091 /* No chain has been created yet. Do nothing. */
2095 if (TREE_SIDE_EFFECTS (cond
))
2097 /* Uh-oh! This condition has a side-effect, thus invalidates
2107 FOR_EACH_VEC_ELT (**chain
, ix
, t
)
2108 if (operand_equal_p (cond
, t
, 0))
2110 if (warning_at (loc
, OPT_Wduplicated_cond
,
2111 "duplicated %<if%> condition"))
2112 inform (EXPR_LOCATION (t
), "previously used here");
2118 && !CONSTANT_CLASS_P (cond
)
2119 /* Don't infinitely grow the chain. */
2120 && (*chain
)->length () < 512)
2121 (*chain
)->safe_push (cond
);
2124 /* Check and possibly warn if two declarations have contradictory
2125 attributes, such as always_inline vs. noinline. */
2128 diagnose_mismatched_attributes (tree olddecl
, tree newdecl
)
2130 bool warned
= false;
2132 tree a1
= lookup_attribute ("optimize", DECL_ATTRIBUTES (olddecl
));
2133 tree a2
= lookup_attribute ("optimize", DECL_ATTRIBUTES (newdecl
));
2134 /* An optimization attribute applied on a declaration after the
2135 definition is likely not what the user wanted. */
2137 && DECL_SAVED_TREE (olddecl
) != NULL_TREE
2138 && (a1
== NULL_TREE
|| !attribute_list_equal (a1
, a2
)))
2139 warned
|= warning (OPT_Wattributes
,
2140 "optimization attribute on %qD follows "
2141 "definition but the attribute doesn%'t match",
2144 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2145 if (DECL_DECLARED_INLINE_P (newdecl
)
2146 && DECL_UNINLINABLE (olddecl
)
2147 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl
)))
2148 warned
|= warning (OPT_Wattributes
, "inline declaration of %qD follows "
2149 "declaration with attribute noinline", newdecl
);
2150 else if (DECL_DECLARED_INLINE_P (olddecl
)
2151 && DECL_UNINLINABLE (newdecl
)
2152 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl
)))
2153 warned
|= warning (OPT_Wattributes
, "declaration of %q+D with attribute "
2154 "noinline follows inline declaration ", newdecl
);
2155 else if (lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl
))
2156 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (olddecl
)))
2157 warned
|= warning (OPT_Wattributes
, "declaration of %q+D with attribute "
2158 "%qs follows declaration with attribute %qs",
2159 newdecl
, "noinline", "always_inline");
2160 else if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (newdecl
))
2161 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl
)))
2162 warned
|= warning (OPT_Wattributes
, "declaration of %q+D with attribute "
2163 "%qs follows declaration with attribute %qs",
2164 newdecl
, "always_inline", "noinline");
2165 else if (lookup_attribute ("cold", DECL_ATTRIBUTES (newdecl
))
2166 && lookup_attribute ("hot", DECL_ATTRIBUTES (olddecl
)))
2167 warned
|= warning (OPT_Wattributes
, "declaration of %q+D with attribute "
2168 "%qs follows declaration with attribute %qs",
2169 newdecl
, "cold", "hot");
2170 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (newdecl
))
2171 && lookup_attribute ("cold", DECL_ATTRIBUTES (olddecl
)))
2172 warned
|= warning (OPT_Wattributes
, "declaration of %q+D with attribute "
2173 "%qs follows declaration with attribute %qs",
2174 newdecl
, "hot", "cold");
2178 /* Warn if signed left shift overflows. We don't warn
2179 about left-shifting 1 into the sign bit in C++14; cf.
2180 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
2181 LOC is a location of the shift; OP0 and OP1 are the operands.
2182 Return true if an overflow is detected, false otherwise. */
2185 maybe_warn_shift_overflow (location_t loc
, tree op0
, tree op1
)
2187 if (TREE_CODE (op0
) != INTEGER_CST
2188 || TREE_CODE (op1
) != INTEGER_CST
)
2191 tree type0
= TREE_TYPE (op0
);
2192 unsigned int prec0
= TYPE_PRECISION (type0
);
2194 /* Left-hand operand must be signed. */
2195 if (TYPE_UNSIGNED (type0
))
2198 unsigned int min_prec
= (wi::min_precision (op0
, SIGNED
)
2199 + TREE_INT_CST_LOW (op1
));
2200 /* Handle the case of left-shifting 1 into the sign bit.
2201 * However, shifting 1 _out_ of the sign bit, as in
2202 * INT_MIN << 1, is considered an overflow.
2204 if (!tree_int_cst_sign_bit(op0
) && min_prec
== prec0
+ 1)
2206 /* Never warn for C++14 onwards. */
2207 if (cxx_dialect
>= cxx14
)
2209 /* Otherwise only if -Wshift-overflow=2. But return
2210 true to signal an overflow for the sake of integer
2211 constant expressions. */
2212 if (warn_shift_overflow
< 2)
2216 bool overflowed
= min_prec
> prec0
;
2217 if (overflowed
&& c_inhibit_evaluation_warnings
== 0)
2218 warning_at (loc
, OPT_Wshift_overflow_
,
2219 "result of %qE requires %u bits to represent, "
2220 "but %qT only has %u bits",
2221 build2_loc (loc
, LSHIFT_EXPR
, type0
, op0
, op1
),
2222 min_prec
, type0
, prec0
);
2227 /* Warn about boolean expression compared with an integer value different
2228 from true/false. Warns also e.g. about "(i1 == i2) == 2".
2229 LOC is the location of the comparison, CODE is its code, OP0 and OP1
2230 are the operands of the comparison. The caller must ensure that
2231 either operand is a boolean expression. */
2234 maybe_warn_bool_compare (location_t loc
, enum tree_code code
, tree op0
,
2237 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
2241 if (f
= fold_for_warn (op0
),
2242 TREE_CODE (f
) == INTEGER_CST
)
2244 else if (f
= fold_for_warn (op1
),
2245 TREE_CODE (f
) == INTEGER_CST
)
2250 if (!integer_zerop (cst
) && !integer_onep (cst
))
2252 int sign
= (TREE_CODE (op0
) == INTEGER_CST
2253 ? tree_int_cst_sgn (cst
) : -tree_int_cst_sgn (cst
));
2255 || ((code
== GT_EXPR
|| code
== GE_EXPR
) && sign
< 0)
2256 || ((code
== LT_EXPR
|| code
== LE_EXPR
) && sign
> 0))
2257 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2258 "with boolean expression is always false", cst
);
2260 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2261 "with boolean expression is always true", cst
);
2263 else if (integer_zerop (cst
) || integer_onep (cst
))
2265 /* If the non-constant operand isn't of a boolean type, we
2266 don't want to warn here. */
2267 tree noncst
= TREE_CODE (op0
) == INTEGER_CST
? op1
: op0
;
2268 /* Handle booleans promoted to integers. */
2269 if (bool_promoted_to_int_p (noncst
))
2271 else if (TREE_CODE (TREE_TYPE (noncst
)) != BOOLEAN_TYPE
2272 && !truth_value_p (TREE_CODE (noncst
)))
2274 /* Do some magic to get the right diagnostics. */
2275 bool flag
= TREE_CODE (op0
) == INTEGER_CST
;
2276 flag
= integer_zerop (cst
) ? flag
: !flag
;
2277 if ((code
== GE_EXPR
&& !flag
) || (code
== LE_EXPR
&& flag
))
2278 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2279 "with boolean expression is always true", cst
);
2280 else if ((code
== LT_EXPR
&& !flag
) || (code
== GT_EXPR
&& flag
))
2281 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
2282 "with boolean expression is always false", cst
);
2286 /* Warn if an argument at position param_pos is passed to a
2287 restrict-qualified param, and it aliases with another argument. */
2290 warn_for_restrict (unsigned param_pos
, tree
*argarray
, unsigned nargs
)
2292 tree arg
= argarray
[param_pos
];
2293 if (TREE_VISITED (arg
) || integer_zerop (arg
))
2296 location_t loc
= EXPR_LOC_OR_LOC (arg
, input_location
);
2297 gcc_rich_location
richloc (loc
);
2300 auto_vec
<int, 16> arg_positions
;
2302 for (i
= 0; i
< nargs
; i
++)
2307 tree current_arg
= argarray
[i
];
2308 if (operand_equal_p (arg
, current_arg
, 0))
2310 TREE_VISITED (current_arg
) = 1;
2311 arg_positions
.safe_push (i
+ 1);
2315 if (arg_positions
.is_empty ())
2319 FOR_EACH_VEC_ELT (arg_positions
, i
, pos
)
2321 arg
= argarray
[pos
- 1];
2322 if (EXPR_HAS_LOCATION (arg
))
2323 richloc
.add_range (EXPR_LOCATION (arg
), false);
2326 warning_at_rich_loc_n (&richloc
, OPT_Wrestrict
, arg_positions
.length (),
2327 "passing argument %i to restrict-qualified parameter"
2328 " aliases with argument %Z",
2329 "passing argument %i to restrict-qualified parameter"
2330 " aliases with arguments %Z",
2331 param_pos
+ 1, arg_positions
.address (),
2332 arg_positions
.length ());
2335 /* Callback function to determine whether an expression TP or one of its
2336 subexpressions comes from macro expansion. Used to suppress bogus
2340 expr_from_macro_expansion_r (tree
*tp
, int *, void *)
2342 if (CAN_HAVE_LOCATION_P (*tp
)
2343 && from_macro_expansion_at (EXPR_LOCATION (*tp
)))
2344 return integer_zero_node
;
2349 /* Possibly warn when an if-else has identical branches. */
2352 do_warn_duplicated_branches (tree expr
)
2354 tree thenb
= COND_EXPR_THEN (expr
);
2355 tree elseb
= COND_EXPR_ELSE (expr
);
2357 /* Don't bother if any of the branches is missing. */
2358 if (thenb
== NULL_TREE
|| elseb
== NULL_TREE
)
2361 /* And don't warn for empty statements. */
2362 if (TREE_CODE (thenb
) == NOP_EXPR
2363 && TREE_TYPE (thenb
) == void_type_node
2364 && TREE_OPERAND (thenb
, 0) == size_zero_node
)
2367 /* ... or empty branches. */
2368 if (TREE_CODE (thenb
) == STATEMENT_LIST
2369 && STATEMENT_LIST_HEAD (thenb
) == NULL
)
2372 /* Compute the hash of the then branch. */
2373 inchash::hash
hstate0 (0);
2374 inchash::add_expr (thenb
, hstate0
);
2375 hashval_t h0
= hstate0
.end ();
2377 /* Compute the hash of the else branch. */
2378 inchash::hash
hstate1 (0);
2379 inchash::add_expr (elseb
, hstate1
);
2380 hashval_t h1
= hstate1
.end ();
2382 /* Compare the hashes. */
2384 && operand_equal_p (thenb
, elseb
, OEP_LEXICOGRAPHIC
)
2385 /* Don't warn if any of the branches or their subexpressions comes
2387 && !walk_tree_without_duplicates (&thenb
, expr_from_macro_expansion_r
,
2389 && !walk_tree_without_duplicates (&elseb
, expr_from_macro_expansion_r
,
2391 warning_at (EXPR_LOCATION (expr
), OPT_Wduplicated_branches
,
2392 "this condition has identical branches");
2395 /* Callback for c_genericize to implement -Wduplicated-branches. */
2398 do_warn_duplicated_branches_r (tree
*tp
, int *, void *)
2400 if (TREE_CODE (*tp
) == COND_EXPR
)
2401 do_warn_duplicated_branches (*tp
);
2405 /* Implementation of -Wmultistatement-macros. This warning warns about
2406 cases when a macro expands to multiple statements not wrapped in
2407 do {} while (0) or ({ }) and is used as a body of if/else/for/while
2408 conditionals. For example,
2410 #define DOIT x++; y++
2415 will increment y unconditionally.
2417 BODY_LOC is the location of the first token in the body after labels
2418 have been parsed, NEXT_LOC is the location of the next token after the
2419 body of the conditional has been parsed, and GUARD_LOC is the location
2420 of the conditional. */
2423 warn_for_multistatement_macros (location_t body_loc
, location_t next_loc
,
2424 location_t guard_loc
, enum rid keyword
)
2426 if (!warn_multistatement_macros
)
2429 /* Ain't got time to waste. We only care about macros here. */
2430 if (!from_macro_expansion_at (body_loc
)
2431 || !from_macro_expansion_at (next_loc
))
2434 /* Let's skip macros defined in system headers. */
2435 if (in_system_header_at (body_loc
)
2436 || in_system_header_at (next_loc
))
2439 /* Find the actual tokens in the macro definition. BODY_LOC and
2440 NEXT_LOC have to come from the same spelling location, but they
2441 will resolve to different locations in the context of the macro
2443 location_t body_loc_exp
2444 = linemap_resolve_location (line_table
, body_loc
,
2445 LRK_MACRO_DEFINITION_LOCATION
, NULL
);
2446 location_t next_loc_exp
2447 = linemap_resolve_location (line_table
, next_loc
,
2448 LRK_MACRO_DEFINITION_LOCATION
, NULL
);
2449 location_t guard_loc_exp
2450 = linemap_resolve_location (line_table
, guard_loc
,
2451 LRK_MACRO_DEFINITION_LOCATION
, NULL
);
2453 /* These are some funky cases we don't want to warn about. */
2454 if (body_loc_exp
== guard_loc_exp
2455 || next_loc_exp
== guard_loc_exp
2456 || body_loc_exp
== next_loc_exp
)
2459 /* Find the macro map for the macro expansion BODY_LOC. */
2460 const line_map
*map
= linemap_lookup (line_table
, body_loc
);
2461 const line_map_macro
*macro_map
= linemap_check_macro (map
);
2463 /* Now see if the following token is coming from the same macro
2464 expansion. If it is, it's a problem, because it should've been
2465 parsed at this point. We only look at odd-numbered indexes
2466 within the MACRO_MAP_LOCATIONS array, i.e. the spelling locations
2468 bool found_guard
= false;
2469 bool found_next
= false;
2470 for (unsigned int i
= 1;
2471 i
< 2 * MACRO_MAP_NUM_MACRO_TOKENS (macro_map
);
2474 if (MACRO_MAP_LOCATIONS (macro_map
)[i
] == next_loc_exp
)
2476 if (MACRO_MAP_LOCATIONS (macro_map
)[i
] == guard_loc_exp
)
2480 /* The conditional itself must not come from the same expansion, because
2481 we don't want to warn about
2482 #define IF if (x) x++; y++
2484 if (!found_next
|| found_guard
)
2487 if (warning_at (body_loc
, OPT_Wmultistatement_macros
,
2488 "macro expands to multiple statements"))
2489 inform (guard_loc
, "some parts of macro expansion are not guarded by "
2490 "this %qs clause", guard_tinfo_to_string (keyword
));