S/390: Add static OSC breaker if necessary.
[official-gcc.git] / gcc / c-family / c-warn.c
blob904f6d3cf4f854080fbd4fbcec687c4fdcffa5e1
1 /* Diagnostic routines shared by all languages that are variants of C.
2 Copyright (C) 1992-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
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"
33 /* Print a warning if a constant expression had overflow in folding.
34 Invoke this function on every expression that the language
35 requires to be a constant expression.
36 Note the ANSI C standard says it is erroneous for a
37 constant expression to overflow. */
39 void
40 constant_expression_warning (tree value)
42 if (warn_overflow && pedantic
43 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
44 || TREE_CODE (value) == FIXED_CST
45 || TREE_CODE (value) == VECTOR_CST
46 || TREE_CODE (value) == COMPLEX_CST)
47 && TREE_OVERFLOW (value))
48 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
51 /* The same as above but print an unconditional error. */
53 void
54 constant_expression_error (tree value)
56 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
57 || TREE_CODE (value) == FIXED_CST
58 || TREE_CODE (value) == VECTOR_CST
59 || TREE_CODE (value) == COMPLEX_CST)
60 && TREE_OVERFLOW (value))
61 error ("overflow in constant expression");
64 /* Print a warning if an expression had overflow in folding and its
65 operands hadn't.
67 Invoke this function on every expression that
68 (1) appears in the source code, and
69 (2) is a constant expression that overflowed, and
70 (3) is not already checked by convert_and_check;
71 however, do not invoke this function on operands of explicit casts
72 or when the expression is the result of an operator and any operand
73 already overflowed. */
75 void
76 overflow_warning (location_t loc, tree value)
78 if (c_inhibit_evaluation_warnings != 0)
79 return;
81 switch (TREE_CODE (value))
83 case INTEGER_CST:
84 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
85 break;
87 case REAL_CST:
88 warning_at (loc, OPT_Woverflow,
89 "floating point overflow in expression");
90 break;
92 case FIXED_CST:
93 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
94 break;
96 case VECTOR_CST:
97 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
98 break;
100 case COMPLEX_CST:
101 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
102 warning_at (loc, OPT_Woverflow,
103 "complex integer overflow in expression");
104 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
105 warning_at (loc, OPT_Woverflow,
106 "complex floating point overflow in expression");
107 break;
109 default:
110 break;
114 /* Warn about uses of logical || / && operator in a context where it
115 is likely that the bitwise equivalent was intended by the
116 programmer. We have seen an expression in which CODE is a binary
117 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
118 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
120 void
121 warn_logical_operator (location_t location, enum tree_code code, tree type,
122 enum tree_code code_left, tree op_left,
123 enum tree_code ARG_UNUSED (code_right), tree op_right)
125 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
126 int in0_p, in1_p, in_p;
127 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
128 bool strict_overflow_p = false;
130 if (code != TRUTH_ANDIF_EXPR
131 && code != TRUTH_AND_EXPR
132 && code != TRUTH_ORIF_EXPR
133 && code != TRUTH_OR_EXPR)
134 return;
136 /* We don't want to warn if either operand comes from a macro
137 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
138 see PR61534. */
139 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
140 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
141 return;
143 /* Warn if &&/|| are being used in a context where it is
144 likely that the bitwise equivalent was intended by the
145 programmer. That is, an expression such as op && MASK
146 where op should not be any boolean expression, nor a
147 constant, and mask seems to be a non-boolean integer constant. */
148 if (TREE_CODE (op_right) == CONST_DECL)
149 /* An enumerator counts as a constant. */
150 op_right = DECL_INITIAL (op_right);
151 if (!truth_value_p (code_left)
152 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
153 && !CONSTANT_CLASS_P (op_left)
154 && !TREE_NO_WARNING (op_left)
155 && TREE_CODE (op_right) == INTEGER_CST
156 && !integer_zerop (op_right)
157 && !integer_onep (op_right))
159 if (or_op)
160 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
161 " applied to non-boolean constant");
162 else
163 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
164 " applied to non-boolean constant");
165 TREE_NO_WARNING (op_left) = true;
166 return;
169 /* We do not warn for constants because they are typical of macro
170 expansions that test for features. */
171 if (CONSTANT_CLASS_P (fold_for_warn (op_left))
172 || CONSTANT_CLASS_P (fold_for_warn (op_right)))
173 return;
175 /* This warning only makes sense with logical operands. */
176 if (!(truth_value_p (TREE_CODE (op_left))
177 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
178 || !(truth_value_p (TREE_CODE (op_right))
179 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
180 return;
182 /* The range computations only work with scalars. */
183 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
184 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
185 return;
187 /* We first test whether either side separately is trivially true
188 (with OR) or trivially false (with AND). If so, do not warn.
189 This is a common idiom for testing ranges of data types in
190 portable code. */
191 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
192 if (!lhs)
193 return;
194 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
195 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
197 /* If this is an OR operation, invert both sides; now, the result
198 should be always false to get a warning. */
199 if (or_op)
200 in0_p = !in0_p;
202 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
203 if (tem && integer_zerop (tem))
204 return;
206 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
207 if (!rhs)
208 return;
209 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
210 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
212 /* If this is an OR operation, invert both sides; now, the result
213 should be always false to get a warning. */
214 if (or_op)
215 in1_p = !in1_p;
217 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
218 if (tem && integer_zerop (tem))
219 return;
221 /* If both expressions have the same operand, if we can merge the
222 ranges, ... */
223 if (operand_equal_p (lhs, rhs, 0)
224 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
225 in1_p, low1, high1))
227 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
228 /* ... and if the range test is always false, then warn. */
229 if (tem && integer_zerop (tem))
231 if (or_op)
232 warning_at (location, OPT_Wlogical_op,
233 "logical %<or%> of collectively exhaustive tests is "
234 "always true");
235 else
236 warning_at (location, OPT_Wlogical_op,
237 "logical %<and%> of mutually exclusive tests is "
238 "always false");
240 /* Or warn if the operands have exactly the same range, e.g.
241 A > 0 && A > 0. */
242 else if (tree_int_cst_equal (low0, low1)
243 && tree_int_cst_equal (high0, high1))
245 if (or_op)
246 warning_at (location, OPT_Wlogical_op,
247 "logical %<or%> of equal expressions");
248 else
249 warning_at (location, OPT_Wlogical_op,
250 "logical %<and%> of equal expressions");
255 /* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
256 with constant indices. */
258 static tree
259 find_array_ref_with_const_idx_r (tree *expr_p, int *, void *)
261 tree expr = *expr_p;
263 if ((TREE_CODE (expr) == ARRAY_REF
264 || TREE_CODE (expr) == ARRAY_RANGE_REF)
265 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST)
266 return integer_type_node;
268 return NULL_TREE;
271 /* Warn if a self-comparison always evaluates to true or false. LOC
272 is the location of the comparison with code CODE, LHS and RHS are
273 operands of the comparison. */
275 void
276 warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs)
278 if (TREE_CODE_CLASS (code) != tcc_comparison)
279 return;
281 /* Don't warn for various macro expansions. */
282 if (from_macro_expansion_at (loc)
283 || from_macro_expansion_at (EXPR_LOCATION (lhs))
284 || from_macro_expansion_at (EXPR_LOCATION (rhs)))
285 return;
287 /* We do not warn for constants because they are typical of macro
288 expansions that test for features, sizeof, and similar. */
289 if (CONSTANT_CLASS_P (fold_for_warn (lhs))
290 || CONSTANT_CLASS_P (fold_for_warn (rhs)))
291 return;
293 /* Don't warn for e.g.
294 HOST_WIDE_INT n;
296 if (n == (long) n) ...
298 if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR)
299 || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR))
300 return;
302 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
303 It could be a NaN, and NaN never compares equal to anything, even
304 itself. */
305 if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
306 return;
308 if (operand_equal_p (lhs, rhs, 0))
310 /* Don't warn about array references with constant indices;
311 these are likely to come from a macro. */
312 if (walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r,
313 NULL))
314 return;
315 const bool always_true = (code == EQ_EXPR || code == LE_EXPR
316 || code == GE_EXPR || code == UNLE_EXPR
317 || code == UNGE_EXPR || code == UNEQ_EXPR);
318 if (always_true)
319 warning_at (loc, OPT_Wtautological_compare,
320 "self-comparison always evaluates to true");
321 else
322 warning_at (loc, OPT_Wtautological_compare,
323 "self-comparison always evaluates to false");
327 /* Return true iff EXPR only contains boolean operands, or comparisons. */
329 static bool
330 expr_has_boolean_operands_p (tree expr)
332 STRIP_NOPS (expr);
334 if (CONVERT_EXPR_P (expr))
335 return bool_promoted_to_int_p (expr);
336 else if (UNARY_CLASS_P (expr))
337 return expr_has_boolean_operands_p (TREE_OPERAND (expr, 0));
338 else if (BINARY_CLASS_P (expr))
339 return (expr_has_boolean_operands_p (TREE_OPERAND (expr, 0))
340 && expr_has_boolean_operands_p (TREE_OPERAND (expr, 1)));
341 else if (COMPARISON_CLASS_P (expr))
342 return true;
343 else
344 return false;
347 /* Warn about logical not used on the left hand side operand of a comparison.
348 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
349 Do not warn if RHS is of a boolean type, a logical operator, or
350 a comparison. */
352 void
353 warn_logical_not_parentheses (location_t location, enum tree_code code,
354 tree lhs, tree rhs)
356 if (TREE_CODE_CLASS (code) != tcc_comparison
357 || TREE_TYPE (rhs) == NULL_TREE
358 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE
359 || truth_value_p (TREE_CODE (rhs)))
360 return;
362 /* Don't warn for expression like !x == ~(bool1 | bool2). */
363 if (expr_has_boolean_operands_p (rhs))
364 return;
366 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
367 !(x == 0) or !(y != 0). */
368 if ((code == EQ_EXPR || code == NE_EXPR)
369 && integer_zerop (rhs))
370 return;
372 if (warning_at (location, OPT_Wlogical_not_parentheses,
373 "logical not is only applied to the left hand side of "
374 "comparison")
375 && EXPR_HAS_LOCATION (lhs))
377 location_t lhs_loc = EXPR_LOCATION (lhs);
378 rich_location richloc (line_table, lhs_loc);
379 richloc.add_fixit_insert_before (lhs_loc, "(");
380 richloc.add_fixit_insert_after (lhs_loc, ")");
381 inform_at_rich_loc (&richloc, "add parentheses around left hand side "
382 "expression to silence this warning");
386 /* Warn if EXP contains any computations whose results are not used.
387 Return true if a warning is printed; false otherwise. LOCUS is the
388 (potential) location of the expression. */
390 bool
391 warn_if_unused_value (const_tree exp, location_t locus)
393 restart:
394 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
395 return false;
397 /* Don't warn about void constructs. This includes casting to void,
398 void function calls, and statement expressions with a final cast
399 to void. */
400 if (VOID_TYPE_P (TREE_TYPE (exp)))
401 return false;
403 if (EXPR_HAS_LOCATION (exp))
404 locus = EXPR_LOCATION (exp);
406 switch (TREE_CODE (exp))
408 case PREINCREMENT_EXPR:
409 case POSTINCREMENT_EXPR:
410 case PREDECREMENT_EXPR:
411 case POSTDECREMENT_EXPR:
412 case MODIFY_EXPR:
413 case INIT_EXPR:
414 case TARGET_EXPR:
415 case CALL_EXPR:
416 case TRY_CATCH_EXPR:
417 case WITH_CLEANUP_EXPR:
418 case EXIT_EXPR:
419 case VA_ARG_EXPR:
420 return false;
422 case BIND_EXPR:
423 /* For a binding, warn if no side effect within it. */
424 exp = BIND_EXPR_BODY (exp);
425 goto restart;
427 case SAVE_EXPR:
428 case NON_LVALUE_EXPR:
429 case NOP_EXPR:
430 exp = TREE_OPERAND (exp, 0);
431 goto restart;
433 case TRUTH_ORIF_EXPR:
434 case TRUTH_ANDIF_EXPR:
435 /* In && or ||, warn if 2nd operand has no side effect. */
436 exp = TREE_OPERAND (exp, 1);
437 goto restart;
439 case COMPOUND_EXPR:
440 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
441 return true;
442 /* Let people do `(foo (), 0)' without a warning. */
443 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
444 return false;
445 exp = TREE_OPERAND (exp, 1);
446 goto restart;
448 case COND_EXPR:
449 /* If this is an expression with side effects, don't warn; this
450 case commonly appears in macro expansions. */
451 if (TREE_SIDE_EFFECTS (exp))
452 return false;
453 goto warn;
455 case INDIRECT_REF:
456 /* Don't warn about automatic dereferencing of references, since
457 the user cannot control it. */
458 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
460 exp = TREE_OPERAND (exp, 0);
461 goto restart;
463 /* Fall through. */
465 default:
466 /* Referencing a volatile value is a side effect, so don't warn. */
467 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
468 && TREE_THIS_VOLATILE (exp))
469 return false;
471 /* If this is an expression which has no operands, there is no value
472 to be unused. There are no such language-independent codes,
473 but front ends may define such. */
474 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
475 return false;
477 warn:
478 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
482 /* Print a warning about casts that might indicate violation
483 of strict aliasing rules if -Wstrict-aliasing is used and
484 strict aliasing mode is in effect. OTYPE is the original
485 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
487 bool
488 strict_aliasing_warning (tree otype, tree type, tree expr)
490 /* Strip pointer conversion chains and get to the correct original type. */
491 STRIP_NOPS (expr);
492 otype = TREE_TYPE (expr);
494 if (!(flag_strict_aliasing
495 && POINTER_TYPE_P (type)
496 && POINTER_TYPE_P (otype)
497 && !VOID_TYPE_P (TREE_TYPE (type)))
498 /* If the type we are casting to is a ref-all pointer
499 dereferencing it is always valid. */
500 || TYPE_REF_CAN_ALIAS_ALL (type))
501 return false;
503 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
504 && (DECL_P (TREE_OPERAND (expr, 0))
505 || handled_component_p (TREE_OPERAND (expr, 0))))
507 /* Casting the address of an object to non void pointer. Warn
508 if the cast breaks type based aliasing. */
509 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
511 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
512 "might break strict-aliasing rules");
513 return true;
515 else
517 /* warn_strict_aliasing >= 3. This includes the default (3).
518 Only warn if the cast is dereferenced immediately. */
519 alias_set_type set1
520 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
521 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
523 if (set1 != set2 && set2 != 0
524 && (set1 == 0
525 || (!alias_set_subset_of (set2, set1)
526 && !alias_sets_conflict_p (set1, set2))))
528 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
529 "pointer will break strict-aliasing rules");
530 return true;
532 else if (warn_strict_aliasing == 2
533 && !alias_sets_must_conflict_p (set1, set2))
535 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
536 "pointer might break strict-aliasing rules");
537 return true;
541 else if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
543 /* At this level, warn for any conversions, even if an address is
544 not taken in the same statement. This will likely produce many
545 false positives, but could be useful to pinpoint problems that
546 are not revealed at higher levels. */
547 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
548 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
549 if (!COMPLETE_TYPE_P (type)
550 || !alias_sets_must_conflict_p (set1, set2))
552 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
553 "pointer might break strict-aliasing rules");
554 return true;
558 return false;
561 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
562 sizeof as last operand of certain builtins. */
564 void
565 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
566 vec<tree, va_gc> *params, tree *sizeof_arg,
567 bool (*comp_types) (tree, tree))
569 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
570 bool strop = false, cmp = false;
571 unsigned int idx = ~0;
572 location_t loc;
574 if (TREE_CODE (callee) != FUNCTION_DECL
575 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
576 || vec_safe_length (params) <= 1)
577 return;
579 switch (DECL_FUNCTION_CODE (callee))
581 case BUILT_IN_STRNCMP:
582 case BUILT_IN_STRNCASECMP:
583 cmp = true;
584 /* FALLTHRU */
585 case BUILT_IN_STRNCPY:
586 case BUILT_IN_STRNCPY_CHK:
587 case BUILT_IN_STRNCAT:
588 case BUILT_IN_STRNCAT_CHK:
589 case BUILT_IN_STPNCPY:
590 case BUILT_IN_STPNCPY_CHK:
591 strop = true;
592 /* FALLTHRU */
593 case BUILT_IN_MEMCPY:
594 case BUILT_IN_MEMCPY_CHK:
595 case BUILT_IN_MEMMOVE:
596 case BUILT_IN_MEMMOVE_CHK:
597 if (params->length () < 3)
598 return;
599 src = (*params)[1];
600 dest = (*params)[0];
601 idx = 2;
602 break;
603 case BUILT_IN_BCOPY:
604 if (params->length () < 3)
605 return;
606 src = (*params)[0];
607 dest = (*params)[1];
608 idx = 2;
609 break;
610 case BUILT_IN_MEMCMP:
611 case BUILT_IN_BCMP:
612 if (params->length () < 3)
613 return;
614 src = (*params)[1];
615 dest = (*params)[0];
616 idx = 2;
617 cmp = true;
618 break;
619 case BUILT_IN_MEMSET:
620 case BUILT_IN_MEMSET_CHK:
621 if (params->length () < 3)
622 return;
623 dest = (*params)[0];
624 idx = 2;
625 break;
626 case BUILT_IN_BZERO:
627 dest = (*params)[0];
628 idx = 1;
629 break;
630 case BUILT_IN_STRNDUP:
631 src = (*params)[0];
632 strop = true;
633 idx = 1;
634 break;
635 case BUILT_IN_MEMCHR:
636 if (params->length () < 3)
637 return;
638 src = (*params)[0];
639 idx = 2;
640 break;
641 case BUILT_IN_SNPRINTF:
642 case BUILT_IN_SNPRINTF_CHK:
643 case BUILT_IN_VSNPRINTF:
644 case BUILT_IN_VSNPRINTF_CHK:
645 dest = (*params)[0];
646 idx = 1;
647 strop = true;
648 break;
649 default:
650 break;
653 if (idx >= 3)
654 return;
656 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
657 return;
659 type = TYPE_P (sizeof_arg[idx])
660 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
661 if (!POINTER_TYPE_P (type))
662 return;
664 if (dest
665 && (tem = tree_strip_nop_conversions (dest))
666 && POINTER_TYPE_P (TREE_TYPE (tem))
667 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
668 return;
670 if (src
671 && (tem = tree_strip_nop_conversions (src))
672 && POINTER_TYPE_P (TREE_TYPE (tem))
673 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
674 return;
676 loc = sizeof_arg_loc[idx];
678 if (dest && !cmp)
680 if (!TYPE_P (sizeof_arg[idx])
681 && operand_equal_p (dest, sizeof_arg[idx], 0)
682 && comp_types (TREE_TYPE (dest), type))
684 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
685 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
686 "argument to %<sizeof%> in %qD call is the same "
687 "expression as the destination; did you mean to "
688 "remove the addressof?", callee);
689 else if ((TYPE_PRECISION (TREE_TYPE (type))
690 == TYPE_PRECISION (char_type_node))
691 || strop)
692 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
693 "argument to %<sizeof%> in %qD call is the same "
694 "expression as the destination; did you mean to "
695 "provide an explicit length?", callee);
696 else
697 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
698 "argument to %<sizeof%> in %qD call is the same "
699 "expression as the destination; did you mean to "
700 "dereference it?", callee);
701 return;
704 if (POINTER_TYPE_P (TREE_TYPE (dest))
705 && !strop
706 && comp_types (TREE_TYPE (dest), type)
707 && !VOID_TYPE_P (TREE_TYPE (type)))
709 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
710 "argument to %<sizeof%> in %qD call is the same "
711 "pointer type %qT as the destination; expected %qT "
712 "or an explicit length", callee, TREE_TYPE (dest),
713 TREE_TYPE (TREE_TYPE (dest)));
714 return;
718 if (src && !cmp)
720 if (!TYPE_P (sizeof_arg[idx])
721 && operand_equal_p (src, sizeof_arg[idx], 0)
722 && comp_types (TREE_TYPE (src), type))
724 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
725 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
726 "argument to %<sizeof%> in %qD call is the same "
727 "expression as the source; did you mean to "
728 "remove the addressof?", callee);
729 else if ((TYPE_PRECISION (TREE_TYPE (type))
730 == TYPE_PRECISION (char_type_node))
731 || strop)
732 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
733 "argument to %<sizeof%> in %qD call is the same "
734 "expression as the source; did you mean to "
735 "provide an explicit length?", callee);
736 else
737 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
738 "argument to %<sizeof%> in %qD call is the same "
739 "expression as the source; did you mean to "
740 "dereference it?", callee);
741 return;
744 if (POINTER_TYPE_P (TREE_TYPE (src))
745 && !strop
746 && comp_types (TREE_TYPE (src), type)
747 && !VOID_TYPE_P (TREE_TYPE (type)))
749 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
750 "argument to %<sizeof%> in %qD call is the same "
751 "pointer type %qT as the source; expected %qT "
752 "or an explicit length", callee, TREE_TYPE (src),
753 TREE_TYPE (TREE_TYPE (src)));
754 return;
758 if (dest)
760 if (!TYPE_P (sizeof_arg[idx])
761 && operand_equal_p (dest, sizeof_arg[idx], 0)
762 && comp_types (TREE_TYPE (dest), type))
764 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
765 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
766 "argument to %<sizeof%> in %qD call is the same "
767 "expression as the first source; did you mean to "
768 "remove the addressof?", callee);
769 else if ((TYPE_PRECISION (TREE_TYPE (type))
770 == TYPE_PRECISION (char_type_node))
771 || strop)
772 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
773 "argument to %<sizeof%> in %qD call is the same "
774 "expression as the first source; did you mean to "
775 "provide an explicit length?", callee);
776 else
777 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
778 "argument to %<sizeof%> in %qD call is the same "
779 "expression as the first source; did you mean to "
780 "dereference it?", callee);
781 return;
784 if (POINTER_TYPE_P (TREE_TYPE (dest))
785 && !strop
786 && comp_types (TREE_TYPE (dest), type)
787 && !VOID_TYPE_P (TREE_TYPE (type)))
789 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
790 "argument to %<sizeof%> in %qD call is the same "
791 "pointer type %qT as the first source; expected %qT "
792 "or an explicit length", callee, TREE_TYPE (dest),
793 TREE_TYPE (TREE_TYPE (dest)));
794 return;
798 if (src)
800 if (!TYPE_P (sizeof_arg[idx])
801 && operand_equal_p (src, sizeof_arg[idx], 0)
802 && comp_types (TREE_TYPE (src), type))
804 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
805 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
806 "argument to %<sizeof%> in %qD call is the same "
807 "expression as the second source; did you mean to "
808 "remove the addressof?", callee);
809 else if ((TYPE_PRECISION (TREE_TYPE (type))
810 == TYPE_PRECISION (char_type_node))
811 || strop)
812 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
813 "argument to %<sizeof%> in %qD call is the same "
814 "expression as the second source; did you mean to "
815 "provide an explicit length?", callee);
816 else
817 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
818 "argument to %<sizeof%> in %qD call is the same "
819 "expression as the second source; did you mean to "
820 "dereference it?", callee);
821 return;
824 if (POINTER_TYPE_P (TREE_TYPE (src))
825 && !strop
826 && comp_types (TREE_TYPE (src), type)
827 && !VOID_TYPE_P (TREE_TYPE (type)))
829 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
830 "argument to %<sizeof%> in %qD call is the same "
831 "pointer type %qT as the second source; expected %qT "
832 "or an explicit length", callee, TREE_TYPE (src),
833 TREE_TYPE (TREE_TYPE (src)));
834 return;
840 /* Warn for unlikely, improbable, or stupid DECL declarations
841 of `main'. */
843 void
844 check_main_parameter_types (tree decl)
846 function_args_iterator iter;
847 tree type;
848 int argct = 0;
850 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
852 /* XXX void_type_node belies the abstraction. */
853 if (type == void_type_node || type == error_mark_node)
854 break;
856 tree t = type;
857 if (TYPE_ATOMIC (t))
858 pedwarn (input_location, OPT_Wmain,
859 "%<_Atomic%>-qualified parameter type %qT of %q+D",
860 type, decl);
861 while (POINTER_TYPE_P (t))
863 t = TREE_TYPE (t);
864 if (TYPE_ATOMIC (t))
865 pedwarn (input_location, OPT_Wmain,
866 "%<_Atomic%>-qualified parameter type %qT of %q+D",
867 type, decl);
870 ++argct;
871 switch (argct)
873 case 1:
874 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
875 pedwarn (input_location, OPT_Wmain,
876 "first argument of %q+D should be %<int%>", decl);
877 break;
879 case 2:
880 if (TREE_CODE (type) != POINTER_TYPE
881 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
882 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
883 != char_type_node))
884 pedwarn (input_location, OPT_Wmain,
885 "second argument of %q+D should be %<char **%>", decl);
886 break;
888 case 3:
889 if (TREE_CODE (type) != POINTER_TYPE
890 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
891 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
892 != char_type_node))
893 pedwarn (input_location, OPT_Wmain,
894 "third argument of %q+D should probably be "
895 "%<char **%>", decl);
896 break;
900 /* It is intentional that this message does not mention the third
901 argument because it's only mentioned in an appendix of the
902 standard. */
903 if (argct > 0 && (argct < 2 || argct > 3))
904 pedwarn (input_location, OPT_Wmain,
905 "%q+D takes only zero or two arguments", decl);
907 if (stdarg_p (TREE_TYPE (decl)))
908 pedwarn (input_location, OPT_Wmain,
909 "%q+D declared as variadic function", decl);
912 /* Warns if the conversion of EXPR to TYPE may alter a value.
913 This is a helper function for warnings_for_convert_and_check. */
915 static void
916 conversion_warning (location_t loc, tree type, tree expr)
918 tree expr_type = TREE_TYPE (expr);
919 enum conversion_safety conversion_kind;
921 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
922 return;
924 /* This may happen, because for LHS op= RHS we preevaluate
925 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
926 means we could no longer see the code of the EXPR. */
927 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
928 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
929 if (TREE_CODE (expr) == SAVE_EXPR)
930 expr = TREE_OPERAND (expr, 0);
932 switch (TREE_CODE (expr))
934 case EQ_EXPR:
935 case NE_EXPR:
936 case LE_EXPR:
937 case GE_EXPR:
938 case LT_EXPR:
939 case GT_EXPR:
940 case TRUTH_ANDIF_EXPR:
941 case TRUTH_ORIF_EXPR:
942 case TRUTH_AND_EXPR:
943 case TRUTH_OR_EXPR:
944 case TRUTH_XOR_EXPR:
945 case TRUTH_NOT_EXPR:
946 /* Conversion from boolean to a signed:1 bit-field (which only
947 can hold the values 0 and -1) doesn't lose information - but
948 it does change the value. */
949 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
950 warning_at (loc, OPT_Wconversion,
951 "conversion to %qT from boolean expression", type);
952 return;
954 case REAL_CST:
955 case INTEGER_CST:
956 case COMPLEX_CST:
957 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
958 if (conversion_kind == UNSAFE_REAL)
959 warning_at (loc, OPT_Wfloat_conversion,
960 "conversion to %qT alters %qT constant value",
961 type, expr_type);
962 else if (conversion_kind)
963 warning_at (loc, OPT_Wconversion,
964 "conversion to %qT alters %qT constant value",
965 type, expr_type);
966 return;
968 case COND_EXPR:
970 /* In case of COND_EXPR, we do not care about the type of
971 COND_EXPR, only about the conversion of each operand. */
972 tree op1 = TREE_OPERAND (expr, 1);
973 tree op2 = TREE_OPERAND (expr, 2);
975 conversion_warning (loc, type, op1);
976 conversion_warning (loc, type, op2);
977 return;
980 default: /* 'expr' is not a constant. */
981 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
982 if (conversion_kind == UNSAFE_REAL)
983 warning_at (loc, OPT_Wfloat_conversion,
984 "conversion to %qT from %qT may alter its value",
985 type, expr_type);
986 else if (conversion_kind == UNSAFE_IMAGINARY)
987 warning_at (loc, OPT_Wconversion,
988 "conversion to %qT from %qT discards imaginary component",
989 type, expr_type);
990 else if (conversion_kind)
991 warning_at (loc, OPT_Wconversion,
992 "conversion to %qT from %qT may alter its value",
993 type, expr_type);
997 /* Produce warnings after a conversion. RESULT is the result of
998 converting EXPR to TYPE. This is a helper function for
999 convert_and_check and cp_convert_and_check. */
1001 void
1002 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
1003 tree result)
1005 loc = expansion_point_location_if_in_system_header (loc);
1007 if (TREE_CODE (expr) == INTEGER_CST
1008 && (TREE_CODE (type) == INTEGER_TYPE
1009 || TREE_CODE (type) == ENUMERAL_TYPE)
1010 && !int_fits_type_p (expr, type))
1012 /* Do not diagnose overflow in a constant expression merely
1013 because a conversion overflowed. */
1014 if (TREE_OVERFLOW (result))
1015 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
1017 if (TYPE_UNSIGNED (type))
1019 /* This detects cases like converting -129 or 256 to
1020 unsigned char. */
1021 if (!int_fits_type_p (expr, c_common_signed_type (type)))
1022 warning_at (loc, OPT_Woverflow,
1023 "large integer implicitly truncated to unsigned type");
1024 else
1025 conversion_warning (loc, type, expr);
1027 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
1028 warning_at (loc, OPT_Woverflow,
1029 "overflow in implicit constant conversion");
1030 /* No warning for converting 0x80000000 to int. */
1031 else if (pedantic
1032 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
1033 || TYPE_PRECISION (TREE_TYPE (expr))
1034 != TYPE_PRECISION (type)))
1035 warning_at (loc, OPT_Woverflow,
1036 "overflow in implicit constant conversion");
1038 else
1039 conversion_warning (loc, type, expr);
1041 else if ((TREE_CODE (result) == INTEGER_CST
1042 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
1043 warning_at (loc, OPT_Woverflow,
1044 "overflow in implicit constant conversion");
1045 else
1046 conversion_warning (loc, type, expr);
1049 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
1050 Used to verify that case values match up with enumerator values. */
1052 static void
1053 match_case_to_enum_1 (tree key, tree type, tree label)
1055 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
1057 if (tree_fits_uhwi_p (key))
1058 print_dec (key, buf, UNSIGNED);
1059 else if (tree_fits_shwi_p (key))
1060 print_dec (key, buf, SIGNED);
1061 else
1062 print_hex (key, buf);
1064 if (TYPE_NAME (type) == 0)
1065 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
1066 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
1067 "case value %qs not in enumerated type",
1068 buf);
1069 else
1070 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
1071 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
1072 "case value %qs not in enumerated type %qT",
1073 buf, type);
1076 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
1077 Used to verify that case values match up with enumerator values. */
1079 static int
1080 match_case_to_enum (splay_tree_node node, void *data)
1082 tree label = (tree) node->value;
1083 tree type = (tree) data;
1085 /* Skip default case. */
1086 if (!CASE_LOW (label))
1087 return 0;
1089 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
1090 when we did our enum->case scan. Reset our scratch bit after. */
1091 if (!CASE_LOW_SEEN (label))
1092 match_case_to_enum_1 (CASE_LOW (label), type, label);
1093 else
1094 CASE_LOW_SEEN (label) = 0;
1096 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
1097 not set, that means that CASE_HIGH did not appear when we did our
1098 enum->case scan. Reset our scratch bit after. */
1099 if (CASE_HIGH (label))
1101 if (!CASE_HIGH_SEEN (label))
1102 match_case_to_enum_1 (CASE_HIGH (label), type, label);
1103 else
1104 CASE_HIGH_SEEN (label) = 0;
1107 return 0;
1110 /* Handle -Wswitch*. Called from the front end after parsing the
1111 switch construct. */
1112 /* ??? Should probably be somewhere generic, since other languages
1113 besides C and C++ would want this. At the moment, however, C/C++
1114 are the only tree-ssa languages that support enumerations at all,
1115 so the point is moot. */
1117 void
1118 c_do_switch_warnings (splay_tree cases, location_t switch_location,
1119 tree type, tree cond, bool bool_cond_p,
1120 bool outside_range_p)
1122 splay_tree_node default_node;
1123 splay_tree_node node;
1124 tree chain;
1126 if (!warn_switch && !warn_switch_enum && !warn_switch_default
1127 && !warn_switch_bool)
1128 return;
1130 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
1131 if (!default_node)
1132 warning_at (switch_location, OPT_Wswitch_default,
1133 "switch missing default case");
1135 /* There are certain cases where -Wswitch-bool warnings aren't
1136 desirable, such as
1137 switch (boolean)
1139 case true: ...
1140 case false: ...
1142 so be careful here. */
1143 if (warn_switch_bool && bool_cond_p)
1145 splay_tree_node min_node;
1146 /* If there's a default node, it's also the value with the minimal
1147 key. So look at the penultimate key (if any). */
1148 if (default_node)
1149 min_node = splay_tree_successor (cases, (splay_tree_key) NULL);
1150 else
1151 min_node = splay_tree_min (cases);
1152 tree min = min_node ? (tree) min_node->key : NULL_TREE;
1154 splay_tree_node max_node = splay_tree_max (cases);
1155 /* This might be a case range, so look at the value with the
1156 maximal key and then check CASE_HIGH. */
1157 tree max = max_node ? (tree) max_node->value : NULL_TREE;
1158 if (max)
1159 max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max);
1161 /* If there's a case value > 1 or < 0, that is outside bool
1162 range, warn. */
1163 if (outside_range_p
1164 || (max && wi::gts_p (max, 1))
1165 || (min && wi::lts_p (min, 0))
1166 /* And handle the
1167 switch (boolean)
1169 case true: ...
1170 case false: ...
1171 default: ...
1173 case, where we want to warn. */
1174 || (default_node
1175 && max && wi::eq_p (max, 1)
1176 && min && wi::eq_p (min, 0)))
1177 warning_at (switch_location, OPT_Wswitch_bool,
1178 "switch condition has boolean value");
1181 /* From here on, we only care about enumerated types. */
1182 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
1183 return;
1185 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
1186 if (!warn_switch_enum && !warn_switch)
1187 return;
1189 /* Check the cases. Warn about case values which are not members of
1190 the enumerated type. For -Wswitch-enum, or for -Wswitch when
1191 there is no default case, check that exactly all enumeration
1192 literals are covered by the cases. */
1194 /* Clearing COND if it is not an integer constant simplifies
1195 the tests inside the loop below. */
1196 if (TREE_CODE (cond) != INTEGER_CST)
1197 cond = NULL_TREE;
1199 /* The time complexity here is O(N*lg(N)) worst case, but for the
1200 common case of monotonically increasing enumerators, it is
1201 O(N), since the nature of the splay tree will keep the next
1202 element adjacent to the root at all times. */
1204 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
1206 tree value = TREE_VALUE (chain);
1207 if (TREE_CODE (value) == CONST_DECL)
1208 value = DECL_INITIAL (value);
1209 node = splay_tree_lookup (cases, (splay_tree_key) value);
1210 if (node)
1212 /* Mark the CASE_LOW part of the case entry as seen. */
1213 tree label = (tree) node->value;
1214 CASE_LOW_SEEN (label) = 1;
1215 continue;
1218 /* Even though there wasn't an exact match, there might be a
1219 case range which includes the enumerator's value. */
1220 node = splay_tree_predecessor (cases, (splay_tree_key) value);
1221 if (node && CASE_HIGH ((tree) node->value))
1223 tree label = (tree) node->value;
1224 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
1225 if (cmp >= 0)
1227 /* If we match the upper bound exactly, mark the CASE_HIGH
1228 part of the case entry as seen. */
1229 if (cmp == 0)
1230 CASE_HIGH_SEEN (label) = 1;
1231 continue;
1235 /* We've now determined that this enumerated literal isn't
1236 handled by the case labels of the switch statement. */
1238 /* If the switch expression is a constant, we only really care
1239 about whether that constant is handled by the switch. */
1240 if (cond && tree_int_cst_compare (cond, value))
1241 continue;
1243 /* If there is a default_node, the only relevant option is
1244 Wswitch-enum. Otherwise, if both are enabled then we prefer
1245 to warn using -Wswitch because -Wswitch is enabled by -Wall
1246 while -Wswitch-enum is explicit. */
1247 warning_at (switch_location,
1248 (default_node || !warn_switch
1249 ? OPT_Wswitch_enum
1250 : OPT_Wswitch),
1251 "enumeration value %qE not handled in switch",
1252 TREE_PURPOSE (chain));
1255 /* Warn if there are case expressions that don't correspond to
1256 enumerators. This can occur since C and C++ don't enforce
1257 type-checking of assignments to enumeration variables.
1259 The time complexity here is now always O(N) worst case, since
1260 we should have marked both the lower bound and upper bound of
1261 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
1262 above. This scan also resets those fields. */
1264 splay_tree_foreach (cases, match_case_to_enum, type);
1267 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
1268 expression, because B will always be true. */
1270 void
1271 warn_for_omitted_condop (location_t location, tree cond)
1273 /* In C++ template declarations it can happen that the type is dependent
1274 and not yet known, thus TREE_TYPE (cond) == NULL_TREE. */
1275 if (truth_value_p (TREE_CODE (cond))
1276 || (TREE_TYPE (cond) != NULL_TREE
1277 && TREE_CODE (TREE_TYPE (cond)) == BOOLEAN_TYPE))
1278 warning_at (location, OPT_Wparentheses,
1279 "the omitted middle operand in ?: will always be %<true%>, "
1280 "suggest explicit middle operand");
1283 /* Give an error for storing into ARG, which is 'const'. USE indicates
1284 how ARG was being used. */
1286 void
1287 readonly_error (location_t loc, tree arg, enum lvalue_use use)
1289 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
1290 || use == lv_asm);
1291 /* Using this macro rather than (for example) arrays of messages
1292 ensures that all the format strings are checked at compile
1293 time. */
1294 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
1295 : (use == lv_increment ? (I) \
1296 : (use == lv_decrement ? (D) : (AS))))
1297 if (TREE_CODE (arg) == COMPONENT_REF)
1299 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
1300 error_at (loc, READONLY_MSG (G_("assignment of member "
1301 "%qD in read-only object"),
1302 G_("increment of member "
1303 "%qD in read-only object"),
1304 G_("decrement of member "
1305 "%qD in read-only object"),
1306 G_("member %qD in read-only object "
1307 "used as %<asm%> output")),
1308 TREE_OPERAND (arg, 1));
1309 else
1310 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
1311 G_("increment of read-only member %qD"),
1312 G_("decrement of read-only member %qD"),
1313 G_("read-only member %qD used as %<asm%> output")),
1314 TREE_OPERAND (arg, 1));
1316 else if (VAR_P (arg))
1317 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
1318 G_("increment of read-only variable %qD"),
1319 G_("decrement of read-only variable %qD"),
1320 G_("read-only variable %qD used as %<asm%> output")),
1321 arg);
1322 else if (TREE_CODE (arg) == PARM_DECL)
1323 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
1324 G_("increment of read-only parameter %qD"),
1325 G_("decrement of read-only parameter %qD"),
1326 G_("read-only parameter %qD use as %<asm%> output")),
1327 arg);
1328 else if (TREE_CODE (arg) == RESULT_DECL)
1330 gcc_assert (c_dialect_cxx ());
1331 error_at (loc, READONLY_MSG (G_("assignment of "
1332 "read-only named return value %qD"),
1333 G_("increment of "
1334 "read-only named return value %qD"),
1335 G_("decrement of "
1336 "read-only named return value %qD"),
1337 G_("read-only named return value %qD "
1338 "used as %<asm%>output")),
1339 arg);
1341 else if (TREE_CODE (arg) == FUNCTION_DECL)
1342 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
1343 G_("increment of function %qD"),
1344 G_("decrement of function %qD"),
1345 G_("function %qD used as %<asm%> output")),
1346 arg);
1347 else
1348 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
1349 G_("increment of read-only location %qE"),
1350 G_("decrement of read-only location %qE"),
1351 G_("read-only location %qE used as %<asm%> output")),
1352 arg);
1355 /* Print an error message for an invalid lvalue. USE says
1356 how the lvalue is being used and so selects the error message. LOC
1357 is the location for the error. */
1359 void
1360 lvalue_error (location_t loc, enum lvalue_use use)
1362 switch (use)
1364 case lv_assign:
1365 error_at (loc, "lvalue required as left operand of assignment");
1366 break;
1367 case lv_increment:
1368 error_at (loc, "lvalue required as increment operand");
1369 break;
1370 case lv_decrement:
1371 error_at (loc, "lvalue required as decrement operand");
1372 break;
1373 case lv_addressof:
1374 error_at (loc, "lvalue required as unary %<&%> operand");
1375 break;
1376 case lv_asm:
1377 error_at (loc, "lvalue required in asm statement");
1378 break;
1379 default:
1380 gcc_unreachable ();
1384 /* Print an error message for an invalid indirection of type TYPE.
1385 ERRSTRING is the name of the operator for the indirection. */
1387 void
1388 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
1390 switch (errstring)
1392 case RO_NULL:
1393 gcc_assert (c_dialect_cxx ());
1394 error_at (loc, "invalid type argument (have %qT)", type);
1395 break;
1396 case RO_ARRAY_INDEXING:
1397 error_at (loc,
1398 "invalid type argument of array indexing (have %qT)",
1399 type);
1400 break;
1401 case RO_UNARY_STAR:
1402 error_at (loc,
1403 "invalid type argument of unary %<*%> (have %qT)",
1404 type);
1405 break;
1406 case RO_ARROW:
1407 error_at (loc,
1408 "invalid type argument of %<->%> (have %qT)",
1409 type);
1410 break;
1411 case RO_ARROW_STAR:
1412 error_at (loc,
1413 "invalid type argument of %<->*%> (have %qT)",
1414 type);
1415 break;
1416 case RO_IMPLICIT_CONVERSION:
1417 error_at (loc,
1418 "invalid type argument of implicit conversion (have %qT)",
1419 type);
1420 break;
1421 default:
1422 gcc_unreachable ();
1426 /* Subscripting with type char is likely to lose on a machine where
1427 chars are signed. So warn on any machine, but optionally. Don't
1428 warn for unsigned char since that type is safe. Don't warn for
1429 signed char because anyone who uses that must have done so
1430 deliberately. Furthermore, we reduce the false positive load by
1431 warning only for non-constant value of type char. */
1433 void
1434 warn_array_subscript_with_type_char (location_t loc, tree index)
1436 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
1437 && TREE_CODE (index) != INTEGER_CST)
1438 warning_at (loc, OPT_Wchar_subscripts,
1439 "array subscript has type %<char%>");
1442 /* Implement -Wparentheses for the unexpected C precedence rules, to
1443 cover cases like x + y << z which readers are likely to
1444 misinterpret. We have seen an expression in which CODE is a binary
1445 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
1446 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
1447 CODE_RIGHT may be ERROR_MARK, which means that that side of the
1448 expression was not formed using a binary or unary operator, or it
1449 was enclosed in parentheses. */
1451 void
1452 warn_about_parentheses (location_t loc, enum tree_code code,
1453 enum tree_code code_left, tree arg_left,
1454 enum tree_code code_right, tree arg_right)
1456 if (!warn_parentheses)
1457 return;
1459 /* This macro tests that the expression ARG with original tree code
1460 CODE appears to be a boolean expression. or the result of folding a
1461 boolean expression. */
1462 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
1463 (truth_value_p (TREE_CODE (ARG)) \
1464 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
1465 /* Folding may create 0 or 1 integers from other expressions. */ \
1466 || ((CODE) != INTEGER_CST \
1467 && (integer_onep (ARG) || integer_zerop (ARG))))
1469 switch (code)
1471 case LSHIFT_EXPR:
1472 if (code_left == PLUS_EXPR)
1473 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1474 "suggest parentheses around %<+%> inside %<<<%>");
1475 else if (code_right == PLUS_EXPR)
1476 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1477 "suggest parentheses around %<+%> inside %<<<%>");
1478 else if (code_left == MINUS_EXPR)
1479 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1480 "suggest parentheses around %<-%> inside %<<<%>");
1481 else if (code_right == MINUS_EXPR)
1482 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1483 "suggest parentheses around %<-%> inside %<<<%>");
1484 return;
1486 case RSHIFT_EXPR:
1487 if (code_left == PLUS_EXPR)
1488 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1489 "suggest parentheses around %<+%> inside %<>>%>");
1490 else if (code_right == PLUS_EXPR)
1491 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1492 "suggest parentheses around %<+%> inside %<>>%>");
1493 else if (code_left == MINUS_EXPR)
1494 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1495 "suggest parentheses around %<-%> inside %<>>%>");
1496 else if (code_right == MINUS_EXPR)
1497 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1498 "suggest parentheses around %<-%> inside %<>>%>");
1499 return;
1501 case TRUTH_ORIF_EXPR:
1502 if (code_left == TRUTH_ANDIF_EXPR)
1503 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1504 "suggest parentheses around %<&&%> within %<||%>");
1505 else if (code_right == TRUTH_ANDIF_EXPR)
1506 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1507 "suggest parentheses around %<&&%> within %<||%>");
1508 return;
1510 case BIT_IOR_EXPR:
1511 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
1512 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
1513 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1514 "suggest parentheses around arithmetic in operand of %<|%>");
1515 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
1516 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
1517 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1518 "suggest parentheses around arithmetic in operand of %<|%>");
1519 /* Check cases like x|y==z */
1520 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
1521 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1522 "suggest parentheses around comparison in operand of %<|%>");
1523 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
1524 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1525 "suggest parentheses around comparison in operand of %<|%>");
1526 /* Check cases like !x | y */
1527 else if (code_left == TRUTH_NOT_EXPR
1528 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
1529 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1530 "suggest parentheses around operand of "
1531 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
1532 return;
1534 case BIT_XOR_EXPR:
1535 if (code_left == BIT_AND_EXPR
1536 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
1537 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1538 "suggest parentheses around arithmetic in operand of %<^%>");
1539 else if (code_right == BIT_AND_EXPR
1540 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
1541 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1542 "suggest parentheses around arithmetic in operand of %<^%>");
1543 /* Check cases like x^y==z */
1544 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
1545 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1546 "suggest parentheses around comparison in operand of %<^%>");
1547 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
1548 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1549 "suggest parentheses around comparison in operand of %<^%>");
1550 return;
1552 case BIT_AND_EXPR:
1553 if (code_left == PLUS_EXPR)
1554 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1555 "suggest parentheses around %<+%> in operand of %<&%>");
1556 else if (code_right == PLUS_EXPR)
1557 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1558 "suggest parentheses around %<+%> in operand of %<&%>");
1559 else if (code_left == MINUS_EXPR)
1560 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1561 "suggest parentheses around %<-%> in operand of %<&%>");
1562 else if (code_right == MINUS_EXPR)
1563 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1564 "suggest parentheses around %<-%> in operand of %<&%>");
1565 /* Check cases like x&y==z */
1566 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
1567 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1568 "suggest parentheses around comparison in operand of %<&%>");
1569 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
1570 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1571 "suggest parentheses around comparison in operand of %<&%>");
1572 /* Check cases like !x & y */
1573 else if (code_left == TRUTH_NOT_EXPR
1574 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
1575 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1576 "suggest parentheses around operand of "
1577 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
1578 return;
1580 case EQ_EXPR:
1581 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
1582 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1583 "suggest parentheses around comparison in operand of %<==%>");
1584 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
1585 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1586 "suggest parentheses around comparison in operand of %<==%>");
1587 return;
1588 case NE_EXPR:
1589 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
1590 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1591 "suggest parentheses around comparison in operand of %<!=%>");
1592 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
1593 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1594 "suggest parentheses around comparison in operand of %<!=%>");
1595 return;
1597 default:
1598 if (TREE_CODE_CLASS (code) == tcc_comparison)
1600 if (TREE_CODE_CLASS (code_left) == tcc_comparison
1601 && code_left != NE_EXPR && code_left != EQ_EXPR
1602 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
1603 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1604 "comparisons like %<X<=Y<=Z%> do not "
1605 "have their mathematical meaning");
1606 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
1607 && code_right != NE_EXPR && code_right != EQ_EXPR
1608 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
1609 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1610 "comparisons like %<X<=Y<=Z%> do not "
1611 "have their mathematical meaning");
1613 return;
1615 #undef NOT_A_BOOLEAN_EXPR_P
1618 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
1620 void
1621 warn_for_unused_label (tree label)
1623 if (!TREE_USED (label))
1625 if (DECL_INITIAL (label))
1626 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
1627 else
1628 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
1632 /* Warn for division by zero according to the value of DIVISOR. LOC
1633 is the location of the division operator. */
1635 void
1636 warn_for_div_by_zero (location_t loc, tree divisor)
1638 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
1639 about division by zero. Do not issue a warning if DIVISOR has a
1640 floating-point type, since we consider 0.0/0.0 a valid way of
1641 generating a NaN. */
1642 if (c_inhibit_evaluation_warnings == 0
1643 && (integer_zerop (divisor) || fixed_zerop (divisor)))
1644 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
1647 /* Warn for patterns where memset appears to be used incorrectly. The
1648 warning location should be LOC. ARG0, and ARG2 are the first and
1649 last arguments to the call, while LITERAL_ZERO_MASK has a 1 bit for
1650 each argument that was a literal zero. */
1652 void
1653 warn_for_memset (location_t loc, tree arg0, tree arg2,
1654 int literal_zero_mask)
1656 if (warn_memset_transposed_args
1657 && integer_zerop (arg2)
1658 && (literal_zero_mask & (1 << 2)) != 0
1659 && (literal_zero_mask & (1 << 1)) == 0)
1660 warning_at (loc, OPT_Wmemset_transposed_args,
1661 "%<memset%> used with constant zero length "
1662 "parameter; this could be due to transposed "
1663 "parameters");
1665 if (warn_memset_elt_size && TREE_CODE (arg2) == INTEGER_CST)
1667 STRIP_NOPS (arg0);
1668 if (TREE_CODE (arg0) == ADDR_EXPR)
1669 arg0 = TREE_OPERAND (arg0, 0);
1670 tree type = TREE_TYPE (arg0);
1671 if (type != NULL_TREE && TREE_CODE (type) == ARRAY_TYPE)
1673 tree elt_type = TREE_TYPE (type);
1674 tree domain = TYPE_DOMAIN (type);
1675 if (!integer_onep (TYPE_SIZE_UNIT (elt_type))
1676 && domain != NULL_TREE
1677 && TYPE_MAXVAL (domain)
1678 && TYPE_MINVAL (domain)
1679 && integer_zerop (TYPE_MINVAL (domain))
1680 && integer_onep (fold_build2 (MINUS_EXPR, domain,
1681 arg2,
1682 TYPE_MAXVAL (domain))))
1683 warning_at (loc, OPT_Wmemset_elt_size,
1684 "%<memset%> used with length equal to "
1685 "number of elements without multiplication "
1686 "by element size");
1691 /* Subroutine of build_binary_op. Give warnings for comparisons
1692 between signed and unsigned quantities that may fail. Do the
1693 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
1694 so that casts will be considered, but default promotions won't
1697 LOCATION is the location of the comparison operator.
1699 The arguments of this function map directly to local variables
1700 of build_binary_op. */
1702 void
1703 warn_for_sign_compare (location_t location,
1704 tree orig_op0, tree orig_op1,
1705 tree op0, tree op1,
1706 tree result_type, enum tree_code resultcode)
1708 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
1709 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
1710 int unsignedp0, unsignedp1;
1712 /* In C++, check for comparison of different enum types. */
1713 if (c_dialect_cxx()
1714 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
1715 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
1716 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
1717 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
1719 warning_at (location,
1720 OPT_Wsign_compare, "comparison between types %qT and %qT",
1721 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
1724 /* Do not warn if the comparison is being done in a signed type,
1725 since the signed type will only be chosen if it can represent
1726 all the values of the unsigned type. */
1727 if (!TYPE_UNSIGNED (result_type))
1728 /* OK */;
1729 /* Do not warn if both operands are unsigned. */
1730 else if (op0_signed == op1_signed)
1731 /* OK */;
1732 else
1734 tree sop, uop, base_type;
1735 bool ovf;
1737 if (op0_signed)
1738 sop = orig_op0, uop = orig_op1;
1739 else
1740 sop = orig_op1, uop = orig_op0;
1742 STRIP_TYPE_NOPS (sop);
1743 STRIP_TYPE_NOPS (uop);
1744 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
1745 ? TREE_TYPE (result_type) : result_type);
1747 /* Do not warn if the signed quantity is an unsuffixed integer
1748 literal (or some static constant expression involving such
1749 literals or a conditional expression involving such literals)
1750 and it is non-negative. */
1751 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
1752 /* OK */;
1753 /* Do not warn if the comparison is an equality operation, the
1754 unsigned quantity is an integral constant, and it would fit
1755 in the result if the result were signed. */
1756 else if (TREE_CODE (uop) == INTEGER_CST
1757 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
1758 && int_fits_type_p (uop, c_common_signed_type (base_type)))
1759 /* OK */;
1760 /* In C, do not warn if the unsigned quantity is an enumeration
1761 constant and its maximum value would fit in the result if the
1762 result were signed. */
1763 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
1764 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
1765 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
1766 c_common_signed_type (base_type)))
1767 /* OK */;
1768 else
1769 warning_at (location,
1770 OPT_Wsign_compare,
1771 "comparison between signed and unsigned integer expressions");
1774 /* Warn if two unsigned values are being compared in a size larger
1775 than their original size, and one (and only one) is the result of
1776 a `~' operator. This comparison will always fail.
1778 Also warn if one operand is a constant, and the constant does not
1779 have all bits set that are set in the ~ operand when it is
1780 extended. */
1782 op0 = c_common_get_narrower (op0, &unsignedp0);
1783 op1 = c_common_get_narrower (op1, &unsignedp1);
1785 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
1786 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
1788 if (TREE_CODE (op0) == BIT_NOT_EXPR)
1789 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
1790 if (TREE_CODE (op1) == BIT_NOT_EXPR)
1791 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
1793 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
1795 tree primop;
1796 HOST_WIDE_INT constant, mask;
1797 int unsignedp;
1798 unsigned int bits;
1800 if (tree_fits_shwi_p (op0))
1802 primop = op1;
1803 unsignedp = unsignedp1;
1804 constant = tree_to_shwi (op0);
1806 else
1808 primop = op0;
1809 unsignedp = unsignedp0;
1810 constant = tree_to_shwi (op1);
1813 bits = TYPE_PRECISION (TREE_TYPE (primop));
1814 if (bits < TYPE_PRECISION (result_type)
1815 && bits < HOST_BITS_PER_LONG && unsignedp)
1817 mask = HOST_WIDE_INT_M1U << bits;
1818 if ((mask & constant) != mask)
1820 if (constant == 0)
1821 warning_at (location, OPT_Wsign_compare,
1822 "promoted ~unsigned is always non-zero");
1823 else
1824 warning_at (location, OPT_Wsign_compare,
1825 "comparison of promoted ~unsigned with constant");
1829 else if (unsignedp0 && unsignedp1
1830 && (TYPE_PRECISION (TREE_TYPE (op0))
1831 < TYPE_PRECISION (result_type))
1832 && (TYPE_PRECISION (TREE_TYPE (op1))
1833 < TYPE_PRECISION (result_type)))
1834 warning_at (location, OPT_Wsign_compare,
1835 "comparison of promoted ~unsigned with unsigned");
1839 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
1840 type via c_common_type. If -Wdouble-promotion is in use, and the
1841 conditions for warning have been met, issue a warning. GMSGID is
1842 the warning message. It must have two %T specifiers for the type
1843 that was converted (generally "float") and the type to which it was
1844 converted (generally "double), respectively. LOC is the location
1845 to which the awrning should refer. */
1847 void
1848 do_warn_double_promotion (tree result_type, tree type1, tree type2,
1849 const char *gmsgid, location_t loc)
1851 tree source_type;
1853 if (!warn_double_promotion)
1854 return;
1855 /* If the conversion will not occur at run-time, there is no need to
1856 warn about it. */
1857 if (c_inhibit_evaluation_warnings)
1858 return;
1859 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
1860 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
1861 return;
1862 if (TYPE_MAIN_VARIANT (type1) == float_type_node
1863 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
1864 source_type = type1;
1865 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
1866 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
1867 source_type = type2;
1868 else
1869 return;
1870 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
1873 /* Possibly warn about unused parameters. */
1875 void
1876 do_warn_unused_parameter (tree fn)
1878 tree decl;
1880 for (decl = DECL_ARGUMENTS (fn);
1881 decl; decl = DECL_CHAIN (decl))
1882 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
1883 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
1884 && !TREE_NO_WARNING (decl))
1885 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter,
1886 "unused parameter %qD", decl);
1889 /* If DECL is a typedef that is declared in the current function,
1890 record it for the purpose of -Wunused-local-typedefs. */
1892 void
1893 record_locally_defined_typedef (tree decl)
1895 struct c_language_function *l;
1897 if (!warn_unused_local_typedefs
1898 || cfun == NULL
1899 /* if this is not a locally defined typedef then we are not
1900 interested. */
1901 || !is_typedef_decl (decl)
1902 || !decl_function_context (decl))
1903 return;
1905 l = (struct c_language_function *) cfun->language;
1906 vec_safe_push (l->local_typedefs, decl);
1909 /* If T is a TYPE_DECL declared locally, mark it as used. */
1911 void
1912 maybe_record_typedef_use (tree t)
1914 if (!is_typedef_decl (t))
1915 return;
1917 TREE_USED (t) = true;
1920 /* Warn if there are some unused locally defined typedefs in the
1921 current function. */
1923 void
1924 maybe_warn_unused_local_typedefs (void)
1926 int i;
1927 tree decl;
1928 /* The number of times we have emitted -Wunused-local-typedefs
1929 warnings. If this is different from errorcount, that means some
1930 unrelated errors have been issued. In which case, we'll avoid
1931 emitting "unused-local-typedefs" warnings. */
1932 static int unused_local_typedefs_warn_count;
1933 struct c_language_function *l;
1935 if (cfun == NULL)
1936 return;
1938 if ((l = (struct c_language_function *) cfun->language) == NULL)
1939 return;
1941 if (warn_unused_local_typedefs
1942 && errorcount == unused_local_typedefs_warn_count)
1944 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
1945 if (!TREE_USED (decl))
1946 warning_at (DECL_SOURCE_LOCATION (decl),
1947 OPT_Wunused_local_typedefs,
1948 "typedef %qD locally defined but not used", decl);
1949 unused_local_typedefs_warn_count = errorcount;
1952 vec_free (l->local_typedefs);
1955 /* If we're creating an if-else-if condition chain, first see if we
1956 already have this COND in the CHAIN. If so, warn and don't add COND
1957 into the vector, otherwise add the COND there. LOC is the location
1958 of COND. */
1960 void
1961 warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain)
1963 /* No chain has been created yet. Do nothing. */
1964 if (*chain == NULL)
1965 return;
1967 if (TREE_SIDE_EFFECTS (cond))
1969 /* Uh-oh! This condition has a side-effect, thus invalidates
1970 the whole chain. */
1971 delete *chain;
1972 *chain = NULL;
1973 return;
1976 unsigned int ix;
1977 tree t;
1978 bool found = false;
1979 FOR_EACH_VEC_ELT (**chain, ix, t)
1980 if (operand_equal_p (cond, t, 0))
1982 if (warning_at (loc, OPT_Wduplicated_cond,
1983 "duplicated %<if%> condition"))
1984 inform (EXPR_LOCATION (t), "previously used here");
1985 found = true;
1986 break;
1989 if (!found
1990 && !CONSTANT_CLASS_P (cond)
1991 /* Don't infinitely grow the chain. */
1992 && (*chain)->length () < 512)
1993 (*chain)->safe_push (cond);
1996 /* Check and possibly warn if two declarations have contradictory
1997 attributes, such as always_inline vs. noinline. */
1999 bool
2000 diagnose_mismatched_attributes (tree olddecl, tree newdecl)
2002 bool warned = false;
2004 tree a1 = lookup_attribute ("optimize", DECL_ATTRIBUTES (olddecl));
2005 tree a2 = lookup_attribute ("optimize", DECL_ATTRIBUTES (newdecl));
2006 /* An optimization attribute applied on a declaration after the
2007 definition is likely not what the user wanted. */
2008 if (a2 != NULL_TREE
2009 && DECL_SAVED_TREE (olddecl) != NULL_TREE
2010 && (a1 == NULL_TREE || !attribute_list_equal (a1, a2)))
2011 warned |= warning (OPT_Wattributes,
2012 "optimization attribute on %qD follows "
2013 "definition but the attribute doesn%'t match",
2014 newdecl);
2016 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2017 if (DECL_DECLARED_INLINE_P (newdecl)
2018 && DECL_UNINLINABLE (olddecl)
2019 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2020 warned |= warning (OPT_Wattributes, "inline declaration of %qD follows "
2021 "declaration with attribute noinline", newdecl);
2022 else if (DECL_DECLARED_INLINE_P (olddecl)
2023 && DECL_UNINLINABLE (newdecl)
2024 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2025 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
2026 "noinline follows inline declaration ", newdecl);
2027 else if (lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))
2028 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (olddecl)))
2029 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
2030 "%qs follows declaration with attribute %qs",
2031 newdecl, "noinline", "always_inline");
2032 else if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (newdecl))
2033 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2034 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
2035 "%qs follows declaration with attribute %qs",
2036 newdecl, "always_inline", "noinline");
2037 else if (lookup_attribute ("cold", DECL_ATTRIBUTES (newdecl))
2038 && lookup_attribute ("hot", DECL_ATTRIBUTES (olddecl)))
2039 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
2040 "%qs follows declaration with attribute %qs",
2041 newdecl, "cold", "hot");
2042 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (newdecl))
2043 && lookup_attribute ("cold", DECL_ATTRIBUTES (olddecl)))
2044 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
2045 "%qs follows declaration with attribute %qs",
2046 newdecl, "hot", "cold");
2047 return warned;
2050 /* Warn if signed left shift overflows. We don't warn
2051 about left-shifting 1 into the sign bit in C++14; cf.
2052 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
2053 LOC is a location of the shift; OP0 and OP1 are the operands.
2054 Return true if an overflow is detected, false otherwise. */
2056 bool
2057 maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
2059 if (TREE_CODE (op0) != INTEGER_CST
2060 || TREE_CODE (op1) != INTEGER_CST)
2061 return false;
2063 tree type0 = TREE_TYPE (op0);
2064 unsigned int prec0 = TYPE_PRECISION (type0);
2066 /* Left-hand operand must be signed. */
2067 if (TYPE_UNSIGNED (type0))
2068 return false;
2070 unsigned int min_prec = (wi::min_precision (op0, SIGNED)
2071 + TREE_INT_CST_LOW (op1));
2072 /* Handle the case of left-shifting 1 into the sign bit.
2073 * However, shifting 1 _out_ of the sign bit, as in
2074 * INT_MIN << 1, is considered an overflow.
2076 if (!tree_int_cst_sign_bit(op0) && min_prec == prec0 + 1)
2078 /* Never warn for C++14 onwards. */
2079 if (cxx_dialect >= cxx14)
2080 return false;
2081 /* Otherwise only if -Wshift-overflow=2. But return
2082 true to signal an overflow for the sake of integer
2083 constant expressions. */
2084 if (warn_shift_overflow < 2)
2085 return true;
2088 bool overflowed = min_prec > prec0;
2089 if (overflowed && c_inhibit_evaluation_warnings == 0)
2090 warning_at (loc, OPT_Wshift_overflow_,
2091 "result of %qE requires %u bits to represent, "
2092 "but %qT only has %u bits",
2093 build2_loc (loc, LSHIFT_EXPR, type0, op0, op1),
2094 min_prec, type0, prec0);
2096 return overflowed;
2099 /* Warn about boolean expression compared with an integer value different
2100 from true/false. Warns also e.g. about "(i1 == i2) == 2".
2101 LOC is the location of the comparison, CODE is its code, OP0 and OP1
2102 are the operands of the comparison. The caller must ensure that
2103 either operand is a boolean expression. */
2105 void
2106 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
2107 tree op1)
2109 if (TREE_CODE_CLASS (code) != tcc_comparison)
2110 return;
2112 tree f, cst;
2113 if (f = fold_for_warn (op0),
2114 TREE_CODE (f) == INTEGER_CST)
2115 cst = op0 = f;
2116 else if (f = fold_for_warn (op1),
2117 TREE_CODE (f) == INTEGER_CST)
2118 cst = op1 = f;
2119 else
2120 return;
2122 if (!integer_zerop (cst) && !integer_onep (cst))
2124 int sign = (TREE_CODE (op0) == INTEGER_CST
2125 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
2126 if (code == EQ_EXPR
2127 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
2128 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
2129 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
2130 "with boolean expression is always false", cst);
2131 else
2132 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
2133 "with boolean expression is always true", cst);
2135 else if (integer_zerop (cst) || integer_onep (cst))
2137 /* If the non-constant operand isn't of a boolean type, we
2138 don't want to warn here. */
2139 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
2140 /* Handle booleans promoted to integers. */
2141 if (bool_promoted_to_int_p (noncst))
2142 /* Warn. */;
2143 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
2144 && !truth_value_p (TREE_CODE (noncst)))
2145 return;
2146 /* Do some magic to get the right diagnostics. */
2147 bool flag = TREE_CODE (op0) == INTEGER_CST;
2148 flag = integer_zerop (cst) ? flag : !flag;
2149 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
2150 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
2151 "with boolean expression is always true", cst);
2152 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
2153 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
2154 "with boolean expression is always false", cst);