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