* tree.c (maybe_warn_parm_abi): Inform the location of the class.
[official-gcc.git] / gcc / c-family / c-warn.c
blob95ac09d76a1e7ed67a94c9dd97152e07417e046f
1 /* Diagnostic routines shared by all languages that are variants of C.
2 Copyright (C) 1992-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
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 "stringpool.h"
32 #include "attribs.h"
33 #include "asan.h"
34 #include "gcc-rich-location.h"
35 #include "gimplify.h"
36 #include "c-family/c-indentation.h"
38 /* Print a warning if a constant expression had overflow in folding.
39 Invoke this function on every expression that the language
40 requires to be a constant expression.
41 Note the ANSI C standard says it is erroneous for a
42 constant expression to overflow. */
44 void
45 constant_expression_warning (tree value)
47 if (warn_overflow && pedantic
48 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
49 || TREE_CODE (value) == FIXED_CST
50 || TREE_CODE (value) == VECTOR_CST
51 || TREE_CODE (value) == COMPLEX_CST)
52 && TREE_OVERFLOW (value))
53 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
56 /* The same as above but print an unconditional error. */
58 void
59 constant_expression_error (tree value)
61 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
62 || TREE_CODE (value) == FIXED_CST
63 || TREE_CODE (value) == VECTOR_CST
64 || TREE_CODE (value) == COMPLEX_CST)
65 && TREE_OVERFLOW (value))
66 error ("overflow in constant expression");
69 /* Print a warning if an expression result VALUE had an overflow
70 in folding and its operands hadn't. EXPR, which may be null, is
71 the operand of the expression.
73 Invoke this function on every expression that
74 (1) appears in the source code, and
75 (2) is a constant expression that overflowed, and
76 (3) is not already checked by convert_and_check;
77 however, do not invoke this function on operands of explicit casts
78 or when the expression is the result of an operator and any operand
79 already overflowed. */
81 void
82 overflow_warning (location_t loc, tree value, tree expr)
84 if (c_inhibit_evaluation_warnings != 0)
85 return;
87 const char *warnfmt = NULL;
89 switch (TREE_CODE (value))
91 case INTEGER_CST:
92 warnfmt = (expr
93 ? G_("integer overflow in expression %qE of type %qT "
94 "results in %qE")
95 : G_("integer overflow in expression of type %qT "
96 "results in %qE"));
97 break;
99 case REAL_CST:
100 warnfmt = (expr
101 ? G_("floating point overflow in expression %qE "
102 "of type %qT results in %qE")
103 : G_("floating point overflow in expression of type %qT "
104 "results in %qE"));
105 break;
107 case FIXED_CST:
108 warnfmt = (expr
109 ? G_("fixed-point overflow in expression %qE of type %qT "
110 "results in %qE")
111 : G_("fixed-point overflow in expression of type %qT "
112 "results in %qE"));
113 break;
115 case VECTOR_CST:
116 warnfmt = (expr
117 ? G_("vector overflow in expression %qE of type %qT "
118 "results in %qE")
119 : G_("vector overflow in expression of type %qT "
120 "results in %qE"));
121 break;
123 case COMPLEX_CST:
124 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
125 warnfmt = (expr
126 ? G_("complex integer overflow in expression %qE "
127 "of type %qT results in %qE")
128 : G_("complex integer overflow in expression of type %qT "
129 "results in %qE"));
130 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
131 warnfmt = (expr
132 ? G_("complex floating point overflow in expression %qE "
133 "of type %qT results in %qE")
134 : G_("complex floating point overflow in expression "
135 "of type %qT results in %qE"));
136 else
137 return;
138 break;
140 default:
141 return;
144 if (expr)
145 warning_at (loc, OPT_Woverflow, warnfmt, expr, TREE_TYPE (expr), value);
146 else
147 warning_at (loc, OPT_Woverflow, warnfmt, TREE_TYPE (value), value);
149 TREE_NO_WARNING (value) = 1;
152 /* Helper function for walk_tree. Unwrap C_MAYBE_CONST_EXPRs in an expression
153 pointed to by TP. */
155 static tree
156 unwrap_c_maybe_const (tree *tp, int *walk_subtrees, void *)
158 if (TREE_CODE (*tp) == C_MAYBE_CONST_EXPR)
160 *tp = C_MAYBE_CONST_EXPR_EXPR (*tp);
161 /* C_MAYBE_CONST_EXPRs don't nest. */
162 *walk_subtrees = false;
164 return NULL_TREE;
167 /* Warn about uses of logical || / && operator in a context where it
168 is likely that the bitwise equivalent was intended by the
169 programmer. We have seen an expression in which CODE is a binary
170 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
171 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
173 void
174 warn_logical_operator (location_t location, enum tree_code code, tree type,
175 enum tree_code code_left, tree op_left,
176 enum tree_code ARG_UNUSED (code_right), tree op_right)
178 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
179 int in0_p, in1_p, in_p;
180 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
181 bool strict_overflow_p = false;
183 if (!warn_logical_op)
184 return;
186 if (code != TRUTH_ANDIF_EXPR
187 && code != TRUTH_AND_EXPR
188 && code != TRUTH_ORIF_EXPR
189 && code != TRUTH_OR_EXPR)
190 return;
192 /* We don't want to warn if either operand comes from a macro
193 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
194 see PR61534. */
195 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
196 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
197 return;
199 /* Warn if &&/|| are being used in a context where it is
200 likely that the bitwise equivalent was intended by the
201 programmer. That is, an expression such as op && MASK
202 where op should not be any boolean expression, nor a
203 constant, and mask seems to be a non-boolean integer constant. */
204 if (TREE_CODE (op_right) == CONST_DECL)
205 /* An enumerator counts as a constant. */
206 op_right = DECL_INITIAL (op_right);
207 if (!truth_value_p (code_left)
208 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
209 && !CONSTANT_CLASS_P (op_left)
210 && !TREE_NO_WARNING (op_left)
211 && TREE_CODE (op_right) == INTEGER_CST
212 && !integer_zerop (op_right)
213 && !integer_onep (op_right))
215 if (or_op)
216 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
217 " applied to non-boolean constant");
218 else
219 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
220 " applied to non-boolean constant");
221 TREE_NO_WARNING (op_left) = true;
222 return;
225 /* We do not warn for constants because they are typical of macro
226 expansions that test for features. */
227 if (CONSTANT_CLASS_P (fold_for_warn (op_left))
228 || CONSTANT_CLASS_P (fold_for_warn (op_right)))
229 return;
231 /* This warning only makes sense with logical operands. */
232 if (!(truth_value_p (TREE_CODE (op_left))
233 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
234 || !(truth_value_p (TREE_CODE (op_right))
235 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
236 return;
238 /* The range computations only work with scalars. */
239 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
240 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
241 return;
243 /* We first test whether either side separately is trivially true
244 (with OR) or trivially false (with AND). If so, do not warn.
245 This is a common idiom for testing ranges of data types in
246 portable code. */
247 op_left = unshare_expr (op_left);
248 walk_tree_without_duplicates (&op_left, unwrap_c_maybe_const, NULL);
249 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
250 if (!lhs)
251 return;
253 /* If this is an OR operation, invert both sides; now, the result
254 should be always false to get a warning. */
255 if (or_op)
256 in0_p = !in0_p;
258 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
259 if (tem && integer_zerop (tem))
260 return;
262 op_right = unshare_expr (op_right);
263 walk_tree_without_duplicates (&op_right, unwrap_c_maybe_const, NULL);
264 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
265 if (!rhs)
266 return;
268 /* If this is an OR operation, invert both sides; now, the result
269 should be always false to get a warning. */
270 if (or_op)
271 in1_p = !in1_p;
273 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
274 if (tem && integer_zerop (tem))
275 return;
277 /* If both expressions have the same operand, if we can merge the
278 ranges, ... */
279 if (operand_equal_p (lhs, rhs, 0)
280 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
281 in1_p, low1, high1))
283 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
284 /* ... and if the range test is always false, then warn. */
285 if (tem && integer_zerop (tem))
287 if (or_op)
288 warning_at (location, OPT_Wlogical_op,
289 "logical %<or%> of collectively exhaustive tests is "
290 "always true");
291 else
292 warning_at (location, OPT_Wlogical_op,
293 "logical %<and%> of mutually exclusive tests is "
294 "always false");
296 /* Or warn if the operands have exactly the same range, e.g.
297 A > 0 && A > 0. */
298 else if (tree_int_cst_equal (low0, low1)
299 && tree_int_cst_equal (high0, high1))
301 if (or_op)
302 warning_at (location, OPT_Wlogical_op,
303 "logical %<or%> of equal expressions");
304 else
305 warning_at (location, OPT_Wlogical_op,
306 "logical %<and%> of equal expressions");
311 /* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
312 with constant indices. */
314 static tree
315 find_array_ref_with_const_idx_r (tree *expr_p, int *, void *)
317 tree expr = *expr_p;
319 if ((TREE_CODE (expr) == ARRAY_REF
320 || TREE_CODE (expr) == ARRAY_RANGE_REF)
321 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST)
322 return integer_type_node;
324 return NULL_TREE;
327 /* Subroutine of warn_tautological_cmp. Warn about bitwise comparison
328 that always evaluate to true or false. LOC is the location of the
329 ==/!= comparison specified by CODE; LHS and RHS are the usual operands
330 of this comparison. */
332 static void
333 warn_tautological_bitwise_comparison (location_t loc, tree_code code,
334 tree lhs, tree rhs)
336 if (code != EQ_EXPR && code != NE_EXPR)
337 return;
339 /* Extract the operands from e.g. (x & 8) == 4. */
340 tree bitop;
341 tree cst;
342 if ((TREE_CODE (lhs) == BIT_AND_EXPR
343 || TREE_CODE (lhs) == BIT_IOR_EXPR)
344 && TREE_CODE (rhs) == INTEGER_CST)
345 bitop = lhs, cst = rhs;
346 else if ((TREE_CODE (rhs) == BIT_AND_EXPR
347 || TREE_CODE (rhs) == BIT_IOR_EXPR)
348 && TREE_CODE (lhs) == INTEGER_CST)
349 bitop = rhs, cst = lhs;
350 else
351 return;
353 tree bitopcst;
354 if (TREE_CODE (TREE_OPERAND (bitop, 0)) == INTEGER_CST)
355 bitopcst = TREE_OPERAND (bitop, 0);
356 else if (TREE_CODE (TREE_OPERAND (bitop, 1)) == INTEGER_CST)
357 bitopcst = TREE_OPERAND (bitop, 1);
358 else
359 return;
361 /* Note that the two operands are from before the usual integer
362 conversions, so their types might not be the same.
363 Use the larger of the two precisions and ignore bits outside
364 of that. */
365 int prec = MAX (TYPE_PRECISION (TREE_TYPE (cst)),
366 TYPE_PRECISION (TREE_TYPE (bitopcst)));
368 wide_int bitopcstw = wi::to_wide (bitopcst, prec);
369 wide_int cstw = wi::to_wide (cst, prec);
371 wide_int res;
372 if (TREE_CODE (bitop) == BIT_AND_EXPR)
373 res = bitopcstw & cstw;
374 else
375 res = bitopcstw | cstw;
377 /* For BIT_AND only warn if (CST2 & CST1) != CST1, and
378 for BIT_OR only if (CST2 | CST1) != CST1. */
379 if (res == cstw)
380 return;
382 if (code == EQ_EXPR)
383 warning_at (loc, OPT_Wtautological_compare,
384 "bitwise comparison always evaluates to false");
385 else
386 warning_at (loc, OPT_Wtautological_compare,
387 "bitwise comparison always evaluates to true");
390 /* Warn if a self-comparison always evaluates to true or false. LOC
391 is the location of the comparison with code CODE, LHS and RHS are
392 operands of the comparison. */
394 void
395 warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs)
397 if (TREE_CODE_CLASS (code) != tcc_comparison)
398 return;
400 /* Don't warn for various macro expansions. */
401 if (from_macro_expansion_at (loc)
402 || from_macro_expansion_at (EXPR_LOCATION (lhs))
403 || from_macro_expansion_at (EXPR_LOCATION (rhs)))
404 return;
406 warn_tautological_bitwise_comparison (loc, code, lhs, rhs);
408 /* We do not warn for constants because they are typical of macro
409 expansions that test for features, sizeof, and similar. */
410 if (CONSTANT_CLASS_P (fold_for_warn (lhs))
411 || CONSTANT_CLASS_P (fold_for_warn (rhs)))
412 return;
414 /* Don't warn for e.g.
415 HOST_WIDE_INT n;
417 if (n == (long) n) ...
419 if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR)
420 || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR))
421 return;
423 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
424 It could be a NaN, and NaN never compares equal to anything, even
425 itself. */
426 if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
427 return;
429 if (operand_equal_p (lhs, rhs, 0))
431 /* Don't warn about array references with constant indices;
432 these are likely to come from a macro. */
433 if (walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r,
434 NULL))
435 return;
436 const bool always_true = (code == EQ_EXPR || code == LE_EXPR
437 || code == GE_EXPR || code == UNLE_EXPR
438 || code == UNGE_EXPR || code == UNEQ_EXPR);
439 if (always_true)
440 warning_at (loc, OPT_Wtautological_compare,
441 "self-comparison always evaluates to true");
442 else
443 warning_at (loc, OPT_Wtautological_compare,
444 "self-comparison always evaluates to false");
448 /* Return true iff EXPR only contains boolean operands, or comparisons. */
450 static bool
451 expr_has_boolean_operands_p (tree expr)
453 STRIP_NOPS (expr);
455 if (CONVERT_EXPR_P (expr))
456 return bool_promoted_to_int_p (expr);
457 else if (UNARY_CLASS_P (expr))
458 return expr_has_boolean_operands_p (TREE_OPERAND (expr, 0));
459 else if (BINARY_CLASS_P (expr))
460 return (expr_has_boolean_operands_p (TREE_OPERAND (expr, 0))
461 && expr_has_boolean_operands_p (TREE_OPERAND (expr, 1)));
462 else if (COMPARISON_CLASS_P (expr))
463 return true;
464 else
465 return false;
468 /* Warn about logical not used on the left hand side operand of a comparison.
469 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
470 Do not warn if RHS is of a boolean type, a logical operator, or
471 a comparison. */
473 void
474 warn_logical_not_parentheses (location_t location, enum tree_code code,
475 tree lhs, tree rhs)
477 if (TREE_CODE_CLASS (code) != tcc_comparison
478 || TREE_TYPE (rhs) == NULL_TREE
479 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE
480 || truth_value_p (TREE_CODE (rhs)))
481 return;
483 /* Don't warn for expression like !x == ~(bool1 | bool2). */
484 if (expr_has_boolean_operands_p (rhs))
485 return;
487 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
488 !(x == 0) or !(y != 0). */
489 if ((code == EQ_EXPR || code == NE_EXPR)
490 && integer_zerop (rhs))
491 return;
493 if (warning_at (location, OPT_Wlogical_not_parentheses,
494 "logical not is only applied to the left hand side of "
495 "comparison")
496 && EXPR_HAS_LOCATION (lhs))
498 location_t lhs_loc = EXPR_LOCATION (lhs);
499 rich_location richloc (line_table, lhs_loc);
500 richloc.add_fixit_insert_before (lhs_loc, "(");
501 richloc.add_fixit_insert_after (lhs_loc, ")");
502 inform (&richloc, "add parentheses around left hand side "
503 "expression to silence this warning");
507 /* Warn if EXP contains any computations whose results are not used.
508 Return true if a warning is printed; false otherwise. LOCUS is the
509 (potential) location of the expression. */
511 bool
512 warn_if_unused_value (const_tree exp, location_t locus)
514 restart:
515 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
516 return false;
518 /* Don't warn about void constructs. This includes casting to void,
519 void function calls, and statement expressions with a final cast
520 to void. */
521 if (VOID_TYPE_P (TREE_TYPE (exp)))
522 return false;
524 if (EXPR_HAS_LOCATION (exp))
525 locus = EXPR_LOCATION (exp);
527 switch (TREE_CODE (exp))
529 case PREINCREMENT_EXPR:
530 case POSTINCREMENT_EXPR:
531 case PREDECREMENT_EXPR:
532 case POSTDECREMENT_EXPR:
533 case MODIFY_EXPR:
534 case INIT_EXPR:
535 case TARGET_EXPR:
536 case CALL_EXPR:
537 case TRY_CATCH_EXPR:
538 case EXIT_EXPR:
539 case VA_ARG_EXPR:
540 return false;
542 case BIND_EXPR:
543 /* For a binding, warn if no side effect within it. */
544 exp = BIND_EXPR_BODY (exp);
545 goto restart;
547 case SAVE_EXPR:
548 case NON_LVALUE_EXPR:
549 case NOP_EXPR:
550 exp = TREE_OPERAND (exp, 0);
551 goto restart;
553 case TRUTH_ORIF_EXPR:
554 case TRUTH_ANDIF_EXPR:
555 /* In && or ||, warn if 2nd operand has no side effect. */
556 exp = TREE_OPERAND (exp, 1);
557 goto restart;
559 case COMPOUND_EXPR:
560 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
561 return true;
562 /* Let people do `(foo (), 0)' without a warning. */
563 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
564 return false;
565 exp = TREE_OPERAND (exp, 1);
566 goto restart;
568 case COND_EXPR:
569 /* If this is an expression with side effects, don't warn; this
570 case commonly appears in macro expansions. */
571 if (TREE_SIDE_EFFECTS (exp))
572 return false;
573 goto warn;
575 case INDIRECT_REF:
576 /* Don't warn about automatic dereferencing of references, since
577 the user cannot control it. */
578 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
580 exp = TREE_OPERAND (exp, 0);
581 goto restart;
583 /* Fall through. */
585 default:
586 /* Referencing a volatile value is a side effect, so don't warn. */
587 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
588 && TREE_THIS_VOLATILE (exp))
589 return false;
591 /* If this is an expression which has no operands, there is no value
592 to be unused. There are no such language-independent codes,
593 but front ends may define such. */
594 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
595 return false;
597 warn:
598 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
602 /* Print a warning about casts that might indicate violation of strict
603 aliasing rules if -Wstrict-aliasing is used and strict aliasing
604 mode is in effect. LOC is the location of the expression being
605 cast, EXPR might be from inside it. TYPE is the type we're casting
606 to. */
608 bool
609 strict_aliasing_warning (location_t loc, tree type, tree expr)
611 if (loc == UNKNOWN_LOCATION)
612 loc = input_location;
614 /* Strip pointer conversion chains and get to the correct original type. */
615 STRIP_NOPS (expr);
616 tree otype = TREE_TYPE (expr);
618 if (!(flag_strict_aliasing
619 && POINTER_TYPE_P (type)
620 && POINTER_TYPE_P (otype)
621 && !VOID_TYPE_P (TREE_TYPE (type)))
622 /* If the type we are casting to is a ref-all pointer
623 dereferencing it is always valid. */
624 || TYPE_REF_CAN_ALIAS_ALL (type))
625 return false;
627 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
628 && (DECL_P (TREE_OPERAND (expr, 0))
629 || handled_component_p (TREE_OPERAND (expr, 0))))
631 /* Casting the address of an object to non void pointer. Warn
632 if the cast breaks type based aliasing. */
633 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
635 warning_at (loc, OPT_Wstrict_aliasing,
636 "type-punning to incomplete type "
637 "might break strict-aliasing rules");
638 return true;
640 else
642 /* warn_strict_aliasing >= 3. This includes the default (3).
643 Only warn if the cast is dereferenced immediately. */
644 alias_set_type set1
645 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
646 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
648 if (set2 != 0
649 && set1 != set2
650 && !alias_set_subset_of (set2, set1)
651 && !alias_sets_conflict_p (set1, set2))
653 warning_at (loc, OPT_Wstrict_aliasing,
654 "dereferencing type-punned "
655 "pointer will break strict-aliasing rules");
656 return true;
658 else if (warn_strict_aliasing == 2
659 && !alias_sets_must_conflict_p (set1, set2))
661 warning_at (loc, OPT_Wstrict_aliasing,
662 "dereferencing type-punned "
663 "pointer might break strict-aliasing rules");
664 return true;
668 else if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
670 /* At this level, warn for any conversions, even if an address is
671 not taken in the same statement. This will likely produce many
672 false positives, but could be useful to pinpoint problems that
673 are not revealed at higher levels. */
674 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
675 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
676 if (!COMPLETE_TYPE_P (type)
677 || !alias_sets_must_conflict_p (set1, set2))
679 warning_at (loc, OPT_Wstrict_aliasing,
680 "dereferencing type-punned "
681 "pointer might break strict-aliasing rules");
682 return true;
686 return false;
689 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
690 sizeof as last operand of certain builtins. */
692 void
693 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
694 vec<tree, va_gc> *params, tree *sizeof_arg,
695 bool (*comp_types) (tree, tree))
697 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
698 bool strop = false, cmp = false;
699 unsigned int idx = ~0;
700 location_t loc;
702 if (TREE_CODE (callee) != FUNCTION_DECL
703 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
704 || vec_safe_length (params) <= 1)
705 return;
707 enum built_in_function fncode = DECL_FUNCTION_CODE (callee);
708 switch (fncode)
710 case BUILT_IN_STRNCMP:
711 case BUILT_IN_STRNCASECMP:
712 cmp = true;
713 /* FALLTHRU */
714 case BUILT_IN_STRNCPY:
715 case BUILT_IN_STRNCPY_CHK:
716 case BUILT_IN_STRNCAT:
717 case BUILT_IN_STRNCAT_CHK:
718 case BUILT_IN_STPNCPY:
719 case BUILT_IN_STPNCPY_CHK:
720 strop = true;
721 /* FALLTHRU */
722 case BUILT_IN_MEMCPY:
723 case BUILT_IN_MEMCPY_CHK:
724 case BUILT_IN_MEMMOVE:
725 case BUILT_IN_MEMMOVE_CHK:
726 if (params->length () < 3)
727 return;
728 src = (*params)[1];
729 dest = (*params)[0];
730 idx = 2;
731 break;
732 case BUILT_IN_BCOPY:
733 if (params->length () < 3)
734 return;
735 src = (*params)[0];
736 dest = (*params)[1];
737 idx = 2;
738 break;
739 case BUILT_IN_MEMCMP:
740 case BUILT_IN_BCMP:
741 if (params->length () < 3)
742 return;
743 src = (*params)[1];
744 dest = (*params)[0];
745 idx = 2;
746 cmp = true;
747 break;
748 case BUILT_IN_MEMSET:
749 case BUILT_IN_MEMSET_CHK:
750 if (params->length () < 3)
751 return;
752 dest = (*params)[0];
753 idx = 2;
754 break;
755 case BUILT_IN_BZERO:
756 dest = (*params)[0];
757 idx = 1;
758 break;
759 case BUILT_IN_STRNDUP:
760 src = (*params)[0];
761 strop = true;
762 idx = 1;
763 break;
764 case BUILT_IN_MEMCHR:
765 if (params->length () < 3)
766 return;
767 src = (*params)[0];
768 idx = 2;
769 break;
770 case BUILT_IN_SNPRINTF:
771 case BUILT_IN_SNPRINTF_CHK:
772 case BUILT_IN_VSNPRINTF:
773 case BUILT_IN_VSNPRINTF_CHK:
774 dest = (*params)[0];
775 idx = 1;
776 strop = true;
777 break;
778 default:
779 break;
782 if (idx >= 3)
783 return;
785 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
786 return;
788 type = TYPE_P (sizeof_arg[idx])
789 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
791 if (!POINTER_TYPE_P (type))
793 /* The argument type may be an array. Diagnose bounded string
794 copy functions that specify the bound in terms of the source
795 argument rather than the destination unless they are equal
796 to one another. Handle constant sizes and also try to handle
797 sizeof expressions involving VLAs. */
798 if (strop && !cmp && fncode != BUILT_IN_STRNDUP && src)
800 tem = tree_strip_nop_conversions (src);
801 if (TREE_CODE (tem) == ADDR_EXPR)
802 tem = TREE_OPERAND (tem, 0);
804 tree d = tree_strip_nop_conversions (dest);
805 if (TREE_CODE (d) == ADDR_EXPR)
806 d = TREE_OPERAND (d, 0);
808 tree dstsz = TYPE_SIZE_UNIT (TREE_TYPE (d));
809 tree srcsz = TYPE_SIZE_UNIT (TREE_TYPE (tem));
811 if ((!dstsz
812 || !srcsz
813 || !operand_equal_p (dstsz, srcsz, OEP_LEXICOGRAPHIC))
814 && operand_equal_p (tem, sizeof_arg[idx], OEP_ADDRESS_OF))
815 warning_at (sizeof_arg_loc[idx], OPT_Wsizeof_pointer_memaccess,
816 "argument to %<sizeof%> in %qD call is the same "
817 "expression as the source; did you mean to use "
818 "the size of the destination?",
819 callee);
822 return;
825 if (dest
826 && (tem = tree_strip_nop_conversions (dest))
827 && POINTER_TYPE_P (TREE_TYPE (tem))
828 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
829 return;
831 if (src
832 && (tem = tree_strip_nop_conversions (src))
833 && POINTER_TYPE_P (TREE_TYPE (tem))
834 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
835 return;
837 loc = sizeof_arg_loc[idx];
839 if (dest && !cmp)
841 if (!TYPE_P (sizeof_arg[idx])
842 && operand_equal_p (dest, sizeof_arg[idx], 0)
843 && comp_types (TREE_TYPE (dest), type))
845 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
846 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
847 "argument to %<sizeof%> in %qD call is the same "
848 "expression as the destination; did you mean to "
849 "remove the addressof?", callee);
850 else if ((TYPE_PRECISION (TREE_TYPE (type))
851 == TYPE_PRECISION (char_type_node))
852 || strop)
853 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
854 "argument to %<sizeof%> in %qD call is the same "
855 "expression as the destination; did you mean to "
856 "provide an explicit length?", callee);
857 else
858 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
859 "argument to %<sizeof%> in %qD call is the same "
860 "expression as the destination; did you mean to "
861 "dereference it?", callee);
862 return;
865 if (POINTER_TYPE_P (TREE_TYPE (dest))
866 && !strop
867 && comp_types (TREE_TYPE (dest), type)
868 && !VOID_TYPE_P (TREE_TYPE (type)))
870 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
871 "argument to %<sizeof%> in %qD call is the same "
872 "pointer type %qT as the destination; expected %qT "
873 "or an explicit length", callee, TREE_TYPE (dest),
874 TREE_TYPE (TREE_TYPE (dest)));
875 return;
879 if (src && !cmp)
881 if (!TYPE_P (sizeof_arg[idx])
882 && operand_equal_p (src, sizeof_arg[idx], 0)
883 && comp_types (TREE_TYPE (src), type))
885 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
886 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
887 "argument to %<sizeof%> in %qD call is the same "
888 "expression as the source; did you mean to "
889 "remove the addressof?", callee);
890 else if ((TYPE_PRECISION (TREE_TYPE (type))
891 == TYPE_PRECISION (char_type_node))
892 || strop)
893 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
894 "argument to %<sizeof%> in %qD call is the same "
895 "expression as the source; did you mean to "
896 "provide an explicit length?", callee);
897 else
898 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
899 "argument to %<sizeof%> in %qD call is the same "
900 "expression as the source; did you mean to "
901 "dereference it?", callee);
902 return;
905 if (POINTER_TYPE_P (TREE_TYPE (src))
906 && !strop
907 && comp_types (TREE_TYPE (src), type)
908 && !VOID_TYPE_P (TREE_TYPE (type)))
910 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
911 "argument to %<sizeof%> in %qD call is the same "
912 "pointer type %qT as the source; expected %qT "
913 "or an explicit length", callee, TREE_TYPE (src),
914 TREE_TYPE (TREE_TYPE (src)));
915 return;
919 if (dest)
921 if (!TYPE_P (sizeof_arg[idx])
922 && operand_equal_p (dest, sizeof_arg[idx], 0)
923 && comp_types (TREE_TYPE (dest), type))
925 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
926 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
927 "argument to %<sizeof%> in %qD call is the same "
928 "expression as the first source; did you mean to "
929 "remove the addressof?", callee);
930 else if ((TYPE_PRECISION (TREE_TYPE (type))
931 == TYPE_PRECISION (char_type_node))
932 || strop)
933 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
934 "argument to %<sizeof%> in %qD call is the same "
935 "expression as the first source; did you mean to "
936 "provide an explicit length?", callee);
937 else
938 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
939 "argument to %<sizeof%> in %qD call is the same "
940 "expression as the first source; did you mean to "
941 "dereference it?", callee);
942 return;
945 if (POINTER_TYPE_P (TREE_TYPE (dest))
946 && !strop
947 && comp_types (TREE_TYPE (dest), type)
948 && !VOID_TYPE_P (TREE_TYPE (type)))
950 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
951 "argument to %<sizeof%> in %qD call is the same "
952 "pointer type %qT as the first source; expected %qT "
953 "or an explicit length", callee, TREE_TYPE (dest),
954 TREE_TYPE (TREE_TYPE (dest)));
955 return;
959 if (src)
961 if (!TYPE_P (sizeof_arg[idx])
962 && operand_equal_p (src, sizeof_arg[idx], 0)
963 && comp_types (TREE_TYPE (src), type))
965 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
966 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
967 "argument to %<sizeof%> in %qD call is the same "
968 "expression as the second source; did you mean to "
969 "remove the addressof?", callee);
970 else if ((TYPE_PRECISION (TREE_TYPE (type))
971 == TYPE_PRECISION (char_type_node))
972 || strop)
973 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
974 "argument to %<sizeof%> in %qD call is the same "
975 "expression as the second source; did you mean to "
976 "provide an explicit length?", callee);
977 else
978 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
979 "argument to %<sizeof%> in %qD call is the same "
980 "expression as the second source; did you mean to "
981 "dereference it?", callee);
982 return;
985 if (POINTER_TYPE_P (TREE_TYPE (src))
986 && !strop
987 && comp_types (TREE_TYPE (src), type)
988 && !VOID_TYPE_P (TREE_TYPE (type)))
990 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
991 "argument to %<sizeof%> in %qD call is the same "
992 "pointer type %qT as the second source; expected %qT "
993 "or an explicit length", callee, TREE_TYPE (src),
994 TREE_TYPE (TREE_TYPE (src)));
995 return;
1001 /* Warn for unlikely, improbable, or stupid DECL declarations
1002 of `main'. */
1004 void
1005 check_main_parameter_types (tree decl)
1007 function_args_iterator iter;
1008 tree type;
1009 int argct = 0;
1011 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
1013 /* XXX void_type_node belies the abstraction. */
1014 if (type == void_type_node || type == error_mark_node)
1015 break;
1017 tree t = type;
1018 if (TYPE_ATOMIC (t))
1019 pedwarn (input_location, OPT_Wmain,
1020 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1021 type, decl);
1022 while (POINTER_TYPE_P (t))
1024 t = TREE_TYPE (t);
1025 if (TYPE_ATOMIC (t))
1026 pedwarn (input_location, OPT_Wmain,
1027 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1028 type, decl);
1031 ++argct;
1032 switch (argct)
1034 case 1:
1035 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
1036 pedwarn (input_location, OPT_Wmain,
1037 "first argument of %q+D should be %<int%>", decl);
1038 break;
1040 case 2:
1041 if (TREE_CODE (type) != POINTER_TYPE
1042 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1043 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1044 != char_type_node))
1045 pedwarn (input_location, OPT_Wmain,
1046 "second argument of %q+D should be %<char **%>", decl);
1047 break;
1049 case 3:
1050 if (TREE_CODE (type) != POINTER_TYPE
1051 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1052 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1053 != char_type_node))
1054 pedwarn (input_location, OPT_Wmain,
1055 "third argument of %q+D should probably be "
1056 "%<char **%>", decl);
1057 break;
1061 /* It is intentional that this message does not mention the third
1062 argument because it's only mentioned in an appendix of the
1063 standard. */
1064 if (argct > 0 && (argct < 2 || argct > 3))
1065 pedwarn (input_location, OPT_Wmain,
1066 "%q+D takes only zero or two arguments", decl);
1068 if (stdarg_p (TREE_TYPE (decl)))
1069 pedwarn (input_location, OPT_Wmain,
1070 "%q+D declared as variadic function", decl);
1073 /* Warns if the conversion of EXPR to TYPE may alter a value.
1074 This is a helper function for warnings_for_convert_and_check. */
1076 static void
1077 conversion_warning (location_t loc, tree type, tree expr, tree result)
1079 tree expr_type = TREE_TYPE (expr);
1080 enum conversion_safety conversion_kind;
1082 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
1083 return;
1085 /* This may happen, because for LHS op= RHS we preevaluate
1086 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
1087 means we could no longer see the code of the EXPR. */
1088 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
1089 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
1090 if (TREE_CODE (expr) == SAVE_EXPR)
1091 expr = TREE_OPERAND (expr, 0);
1093 switch (TREE_CODE (expr))
1095 case EQ_EXPR:
1096 case NE_EXPR:
1097 case LE_EXPR:
1098 case GE_EXPR:
1099 case LT_EXPR:
1100 case GT_EXPR:
1101 case TRUTH_ANDIF_EXPR:
1102 case TRUTH_ORIF_EXPR:
1103 case TRUTH_AND_EXPR:
1104 case TRUTH_OR_EXPR:
1105 case TRUTH_XOR_EXPR:
1106 case TRUTH_NOT_EXPR:
1107 /* Conversion from boolean to a signed:1 bit-field (which only
1108 can hold the values 0 and -1) doesn't lose information - but
1109 it does change the value. */
1110 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
1111 warning_at (loc, OPT_Wconversion,
1112 "conversion to %qT from boolean expression", type);
1113 return;
1115 case REAL_CST:
1116 case INTEGER_CST:
1117 case COMPLEX_CST:
1119 conversion_kind = unsafe_conversion_p (loc, type, expr, result, true);
1120 int warnopt;
1121 if (conversion_kind == UNSAFE_REAL)
1122 warnopt = OPT_Wfloat_conversion;
1123 else if (conversion_kind)
1124 warnopt = OPT_Wconversion;
1125 else
1126 break;
1128 if (TREE_CODE_CLASS (TREE_CODE (result)) == tcc_constant)
1129 warning_at (loc, warnopt,
1130 "conversion from %qT to %qT changes value from %qE to %qE",
1131 expr_type, type, expr, result);
1132 else
1133 warning_at (loc, warnopt,
1134 "conversion from %qT to %qT changes the value of %qE",
1135 expr_type, type, expr);
1136 break;
1138 case COND_EXPR:
1140 /* In case of COND_EXPR, we do not care about the type of
1141 COND_EXPR, only about the conversion of each operand. */
1142 tree op1 = TREE_OPERAND (expr, 1);
1143 tree op2 = TREE_OPERAND (expr, 2);
1145 conversion_warning (loc, type, op1, result);
1146 conversion_warning (loc, type, op2, result);
1147 return;
1150 default: /* 'expr' is not a constant. */
1151 conversion_kind = unsafe_conversion_p (loc, type, expr, result, true);
1152 if (conversion_kind == UNSAFE_IMAGINARY)
1153 warning_at (loc, OPT_Wconversion,
1154 "conversion from %qT to %qT discards imaginary component",
1155 expr_type, type);
1156 else
1158 int warnopt;
1159 if (conversion_kind == UNSAFE_REAL)
1160 warnopt = OPT_Wfloat_conversion;
1161 else if (conversion_kind)
1162 warnopt = OPT_Wconversion;
1163 else
1164 break;
1165 warning_at (loc, warnopt,
1166 "conversion from %qT to %qT may change value",
1167 expr_type, type);
1172 /* Produce warnings after a conversion. RESULT is the result of
1173 converting EXPR to TYPE. This is a helper function for
1174 convert_and_check and cp_convert_and_check. */
1176 void
1177 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
1178 tree result)
1180 loc = expansion_point_location_if_in_system_header (loc);
1182 bool cst = TREE_CODE_CLASS (TREE_CODE (result)) == tcc_constant;
1184 tree exprtype = TREE_TYPE (expr);
1186 if (TREE_CODE (expr) == INTEGER_CST
1187 && (TREE_CODE (type) == INTEGER_TYPE
1188 || TREE_CODE (type) == ENUMERAL_TYPE)
1189 && !int_fits_type_p (expr, type))
1191 /* Do not diagnose overflow in a constant expression merely
1192 because a conversion overflowed. */
1193 if (TREE_OVERFLOW (result))
1194 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
1196 if (TYPE_UNSIGNED (type))
1198 /* This detects cases like converting -129 or 256 to
1199 unsigned char. */
1200 if (!int_fits_type_p (expr, c_common_signed_type (type)))
1202 if (cst)
1203 warning_at (loc, OPT_Woverflow,
1204 (TYPE_UNSIGNED (exprtype)
1205 ? G_("conversion from %qT to %qT "
1206 "changes value from %qE to %qE")
1207 : G_("unsigned conversion from %qT to %qT "
1208 "changes value from %qE to %qE")),
1209 exprtype, type, expr, result);
1210 else
1211 warning_at (loc, OPT_Woverflow,
1212 (TYPE_UNSIGNED (exprtype)
1213 ? G_("conversion from %qT to %qT "
1214 "changes the value of %qE")
1215 : G_("unsigned conversion from %qT to %qT "
1216 "changes the value of %qE")),
1217 exprtype, type, expr);
1219 else
1220 conversion_warning (loc, type, expr, result);
1222 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
1224 if (cst)
1225 warning_at (loc, OPT_Woverflow,
1226 "overflow in conversion from %qT to %qT "
1227 "changes value from %qE to %qE",
1228 exprtype, type, expr, result);
1229 else
1230 warning_at (loc, OPT_Woverflow,
1231 "overflow in conversion from %qT to %qT "
1232 "changes the value of %qE",
1233 exprtype, type, expr);
1235 /* No warning for converting 0x80000000 to int. */
1236 else if (pedantic
1237 && (TREE_CODE (exprtype) != INTEGER_TYPE
1238 || TYPE_PRECISION (exprtype)
1239 != TYPE_PRECISION (type)))
1241 if (cst)
1242 warning_at (loc, OPT_Woverflow,
1243 "overflow in conversion from %qT to %qT "
1244 "changes value from %qE to %qE",
1245 exprtype, type, expr, result);
1246 else
1247 warning_at (loc, OPT_Woverflow,
1248 "overflow in conversion from %qT to %qT "
1249 "changes the value of %qE",
1250 exprtype, type, expr);
1252 else
1253 conversion_warning (loc, type, expr, result);
1255 else if ((TREE_CODE (result) == INTEGER_CST
1256 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
1258 if (cst)
1259 warning_at (loc, OPT_Woverflow,
1260 "overflow in conversion from %qT to %qT "
1261 "changes value from %qE to %qE",
1262 exprtype, type, expr, result);
1263 else
1264 warning_at (loc, OPT_Woverflow,
1265 "overflow in conversion from %qT to %qT "
1266 "changes the value of %qE",
1267 exprtype, type, expr);
1269 else
1270 conversion_warning (loc, type, expr, result);
1273 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
1274 Used to verify that case values match up with enumerator values. */
1276 static void
1277 match_case_to_enum_1 (tree key, tree type, tree label)
1279 /* Avoid warning about enums that have no enumerators. */
1280 if (TYPE_VALUES (type) == NULL_TREE)
1281 return;
1283 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
1285 if (tree_fits_uhwi_p (key))
1286 print_dec (wi::to_wide (key), buf, UNSIGNED);
1287 else if (tree_fits_shwi_p (key))
1288 print_dec (wi::to_wide (key), buf, SIGNED);
1289 else
1290 print_hex (wi::to_wide (key), buf);
1292 if (TYPE_NAME (type) == NULL_TREE)
1293 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
1294 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
1295 "case value %qs not in enumerated type",
1296 buf);
1297 else
1298 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
1299 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
1300 "case value %qs not in enumerated type %qT",
1301 buf, type);
1304 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
1305 Used to verify that case values match up with enumerator values. */
1307 static int
1308 match_case_to_enum (splay_tree_node node, void *data)
1310 tree label = (tree) node->value;
1311 tree type = (tree) data;
1313 /* Skip default case. */
1314 if (!CASE_LOW (label))
1315 return 0;
1317 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
1318 when we did our enum->case scan. Reset our scratch bit after. */
1319 if (!CASE_LOW_SEEN (label))
1320 match_case_to_enum_1 (CASE_LOW (label), type, label);
1321 else
1322 CASE_LOW_SEEN (label) = 0;
1324 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
1325 not set, that means that CASE_HIGH did not appear when we did our
1326 enum->case scan. Reset our scratch bit after. */
1327 if (CASE_HIGH (label))
1329 if (!CASE_HIGH_SEEN (label))
1330 match_case_to_enum_1 (CASE_HIGH (label), type, label);
1331 else
1332 CASE_HIGH_SEEN (label) = 0;
1335 return 0;
1338 /* Handle -Wswitch*. Called from the front end after parsing the
1339 switch construct. */
1340 /* ??? Should probably be somewhere generic, since other languages
1341 besides C and C++ would want this. At the moment, however, C/C++
1342 are the only tree-ssa languages that support enumerations at all,
1343 so the point is moot. */
1345 void
1346 c_do_switch_warnings (splay_tree cases, location_t switch_location,
1347 tree type, tree cond, bool bool_cond_p,
1348 bool outside_range_p)
1350 splay_tree_node default_node;
1351 splay_tree_node node;
1352 tree chain;
1354 if (!warn_switch && !warn_switch_enum && !warn_switch_default
1355 && !warn_switch_bool)
1356 return;
1358 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
1359 if (!default_node)
1360 warning_at (switch_location, OPT_Wswitch_default,
1361 "switch missing default case");
1363 /* There are certain cases where -Wswitch-bool warnings aren't
1364 desirable, such as
1365 switch (boolean)
1367 case true: ...
1368 case false: ...
1370 so be careful here. */
1371 if (warn_switch_bool && bool_cond_p)
1373 splay_tree_node min_node;
1374 /* If there's a default node, it's also the value with the minimal
1375 key. So look at the penultimate key (if any). */
1376 if (default_node)
1377 min_node = splay_tree_successor (cases, (splay_tree_key) NULL);
1378 else
1379 min_node = splay_tree_min (cases);
1380 tree min = min_node ? (tree) min_node->key : NULL_TREE;
1382 splay_tree_node max_node = splay_tree_max (cases);
1383 /* This might be a case range, so look at the value with the
1384 maximal key and then check CASE_HIGH. */
1385 tree max = max_node ? (tree) max_node->value : NULL_TREE;
1386 if (max)
1387 max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max);
1389 /* If there's a case value > 1 or < 0, that is outside bool
1390 range, warn. */
1391 if (outside_range_p
1392 || (max && wi::gts_p (wi::to_wide (max), 1))
1393 || (min && wi::lts_p (wi::to_wide (min), 0))
1394 /* And handle the
1395 switch (boolean)
1397 case true: ...
1398 case false: ...
1399 default: ...
1401 case, where we want to warn. */
1402 || (default_node
1403 && max && wi::to_wide (max) == 1
1404 && min && wi::to_wide (min) == 0))
1405 warning_at (switch_location, OPT_Wswitch_bool,
1406 "switch condition has boolean value");
1409 /* From here on, we only care about enumerated types. */
1410 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
1411 return;
1413 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
1414 if (!warn_switch_enum && !warn_switch)
1415 return;
1417 /* Check the cases. Warn about case values which are not members of
1418 the enumerated type. For -Wswitch-enum, or for -Wswitch when
1419 there is no default case, check that exactly all enumeration
1420 literals are covered by the cases. */
1422 /* Clearing COND if it is not an integer constant simplifies
1423 the tests inside the loop below. */
1424 if (TREE_CODE (cond) != INTEGER_CST)
1425 cond = NULL_TREE;
1427 /* The time complexity here is O(N*lg(N)) worst case, but for the
1428 common case of monotonically increasing enumerators, it is
1429 O(N), since the nature of the splay tree will keep the next
1430 element adjacent to the root at all times. */
1432 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
1434 tree value = TREE_VALUE (chain);
1435 if (TREE_CODE (value) == CONST_DECL)
1436 value = DECL_INITIAL (value);
1437 node = splay_tree_lookup (cases, (splay_tree_key) value);
1438 if (node)
1440 /* Mark the CASE_LOW part of the case entry as seen. */
1441 tree label = (tree) node->value;
1442 CASE_LOW_SEEN (label) = 1;
1443 continue;
1446 /* Even though there wasn't an exact match, there might be a
1447 case range which includes the enumerator's value. */
1448 node = splay_tree_predecessor (cases, (splay_tree_key) value);
1449 if (node && CASE_HIGH ((tree) node->value))
1451 tree label = (tree) node->value;
1452 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
1453 if (cmp >= 0)
1455 /* If we match the upper bound exactly, mark the CASE_HIGH
1456 part of the case entry as seen. */
1457 if (cmp == 0)
1458 CASE_HIGH_SEEN (label) = 1;
1459 continue;
1463 /* We've now determined that this enumerated literal isn't
1464 handled by the case labels of the switch statement. */
1466 /* If the switch expression is a constant, we only really care
1467 about whether that constant is handled by the switch. */
1468 if (cond && tree_int_cst_compare (cond, value))
1469 continue;
1471 /* If there is a default_node, the only relevant option is
1472 Wswitch-enum. Otherwise, if both are enabled then we prefer
1473 to warn using -Wswitch because -Wswitch is enabled by -Wall
1474 while -Wswitch-enum is explicit. */
1475 warning_at (switch_location,
1476 (default_node || !warn_switch
1477 ? OPT_Wswitch_enum
1478 : OPT_Wswitch),
1479 "enumeration value %qE not handled in switch",
1480 TREE_PURPOSE (chain));
1483 /* Warn if there are case expressions that don't correspond to
1484 enumerators. This can occur since C and C++ don't enforce
1485 type-checking of assignments to enumeration variables.
1487 The time complexity here is now always O(N) worst case, since
1488 we should have marked both the lower bound and upper bound of
1489 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
1490 above. This scan also resets those fields. */
1492 splay_tree_foreach (cases, match_case_to_enum, type);
1495 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
1496 expression, because B will always be true. */
1498 void
1499 warn_for_omitted_condop (location_t location, tree cond)
1501 /* In C++ template declarations it can happen that the type is dependent
1502 and not yet known, thus TREE_TYPE (cond) == NULL_TREE. */
1503 if (truth_value_p (TREE_CODE (cond))
1504 || (TREE_TYPE (cond) != NULL_TREE
1505 && TREE_CODE (TREE_TYPE (cond)) == BOOLEAN_TYPE))
1506 warning_at (location, OPT_Wparentheses,
1507 "the omitted middle operand in ?: will always be %<true%>, "
1508 "suggest explicit middle operand");
1511 /* Give an error for storing into ARG, which is 'const'. USE indicates
1512 how ARG was being used. */
1514 void
1515 readonly_error (location_t loc, tree arg, enum lvalue_use use)
1517 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
1518 || use == lv_asm);
1519 /* Using this macro rather than (for example) arrays of messages
1520 ensures that all the format strings are checked at compile
1521 time. */
1522 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
1523 : (use == lv_increment ? (I) \
1524 : (use == lv_decrement ? (D) : (AS))))
1525 if (TREE_CODE (arg) == COMPONENT_REF)
1527 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
1528 error_at (loc, READONLY_MSG (G_("assignment of member "
1529 "%qD in read-only object"),
1530 G_("increment of member "
1531 "%qD in read-only object"),
1532 G_("decrement of member "
1533 "%qD in read-only object"),
1534 G_("member %qD in read-only object "
1535 "used as %<asm%> output")),
1536 TREE_OPERAND (arg, 1));
1537 else
1538 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
1539 G_("increment of read-only member %qD"),
1540 G_("decrement of read-only member %qD"),
1541 G_("read-only member %qD used as %<asm%> output")),
1542 TREE_OPERAND (arg, 1));
1544 else if (VAR_P (arg))
1545 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
1546 G_("increment of read-only variable %qD"),
1547 G_("decrement of read-only variable %qD"),
1548 G_("read-only variable %qD used as %<asm%> output")),
1549 arg);
1550 else if (TREE_CODE (arg) == PARM_DECL)
1551 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
1552 G_("increment of read-only parameter %qD"),
1553 G_("decrement of read-only parameter %qD"),
1554 G_("read-only parameter %qD use as %<asm%> output")),
1555 arg);
1556 else if (TREE_CODE (arg) == RESULT_DECL)
1558 gcc_assert (c_dialect_cxx ());
1559 error_at (loc, READONLY_MSG (G_("assignment of "
1560 "read-only named return value %qD"),
1561 G_("increment of "
1562 "read-only named return value %qD"),
1563 G_("decrement of "
1564 "read-only named return value %qD"),
1565 G_("read-only named return value %qD "
1566 "used as %<asm%>output")),
1567 arg);
1569 else if (TREE_CODE (arg) == FUNCTION_DECL)
1570 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
1571 G_("increment of function %qD"),
1572 G_("decrement of function %qD"),
1573 G_("function %qD used as %<asm%> output")),
1574 arg);
1575 else
1576 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
1577 G_("increment of read-only location %qE"),
1578 G_("decrement of read-only location %qE"),
1579 G_("read-only location %qE used as %<asm%> output")),
1580 arg);
1583 /* Print an error message for an invalid lvalue. USE says
1584 how the lvalue is being used and so selects the error message. LOC
1585 is the location for the error. */
1587 void
1588 lvalue_error (location_t loc, enum lvalue_use use)
1590 switch (use)
1592 case lv_assign:
1593 error_at (loc, "lvalue required as left operand of assignment");
1594 break;
1595 case lv_increment:
1596 error_at (loc, "lvalue required as increment operand");
1597 break;
1598 case lv_decrement:
1599 error_at (loc, "lvalue required as decrement operand");
1600 break;
1601 case lv_addressof:
1602 error_at (loc, "lvalue required as unary %<&%> operand");
1603 break;
1604 case lv_asm:
1605 error_at (loc, "lvalue required in asm statement");
1606 break;
1607 default:
1608 gcc_unreachable ();
1612 /* Print an error message for an invalid indirection of type TYPE.
1613 ERRSTRING is the name of the operator for the indirection. */
1615 void
1616 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
1618 switch (errstring)
1620 case RO_NULL:
1621 gcc_assert (c_dialect_cxx ());
1622 error_at (loc, "invalid type argument (have %qT)", type);
1623 break;
1624 case RO_ARRAY_INDEXING:
1625 error_at (loc,
1626 "invalid type argument of array indexing (have %qT)",
1627 type);
1628 break;
1629 case RO_UNARY_STAR:
1630 error_at (loc,
1631 "invalid type argument of unary %<*%> (have %qT)",
1632 type);
1633 break;
1634 case RO_ARROW:
1635 error_at (loc,
1636 "invalid type argument of %<->%> (have %qT)",
1637 type);
1638 break;
1639 case RO_ARROW_STAR:
1640 error_at (loc,
1641 "invalid type argument of %<->*%> (have %qT)",
1642 type);
1643 break;
1644 case RO_IMPLICIT_CONVERSION:
1645 error_at (loc,
1646 "invalid type argument of implicit conversion (have %qT)",
1647 type);
1648 break;
1649 default:
1650 gcc_unreachable ();
1654 /* Subscripting with type char is likely to lose on a machine where
1655 chars are signed. So warn on any machine, but optionally. Don't
1656 warn for unsigned char since that type is safe. Don't warn for
1657 signed char because anyone who uses that must have done so
1658 deliberately. Furthermore, we reduce the false positive load by
1659 warning only for non-constant value of type char. */
1661 void
1662 warn_array_subscript_with_type_char (location_t loc, tree index)
1664 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
1665 && TREE_CODE (index) != INTEGER_CST)
1666 warning_at (loc, OPT_Wchar_subscripts,
1667 "array subscript has type %<char%>");
1670 /* Implement -Wparentheses for the unexpected C precedence rules, to
1671 cover cases like x + y << z which readers are likely to
1672 misinterpret. We have seen an expression in which CODE is a binary
1673 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
1674 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
1675 CODE_RIGHT may be ERROR_MARK, which means that that side of the
1676 expression was not formed using a binary or unary operator, or it
1677 was enclosed in parentheses. */
1679 void
1680 warn_about_parentheses (location_t loc, enum tree_code code,
1681 enum tree_code code_left, tree arg_left,
1682 enum tree_code code_right, tree arg_right)
1684 if (!warn_parentheses)
1685 return;
1687 /* This macro tests that the expression ARG with original tree code
1688 CODE appears to be a boolean expression. or the result of folding a
1689 boolean expression. */
1690 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
1691 (truth_value_p (TREE_CODE (ARG)) \
1692 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
1693 /* Folding may create 0 or 1 integers from other expressions. */ \
1694 || ((CODE) != INTEGER_CST \
1695 && (integer_onep (ARG) || integer_zerop (ARG))))
1697 switch (code)
1699 case LSHIFT_EXPR:
1700 if (code_left == PLUS_EXPR)
1701 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1702 "suggest parentheses around %<+%> inside %<<<%>");
1703 else if (code_right == PLUS_EXPR)
1704 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1705 "suggest parentheses around %<+%> inside %<<<%>");
1706 else if (code_left == MINUS_EXPR)
1707 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1708 "suggest parentheses around %<-%> inside %<<<%>");
1709 else if (code_right == MINUS_EXPR)
1710 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1711 "suggest parentheses around %<-%> inside %<<<%>");
1712 return;
1714 case RSHIFT_EXPR:
1715 if (code_left == PLUS_EXPR)
1716 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1717 "suggest parentheses around %<+%> inside %<>>%>");
1718 else if (code_right == PLUS_EXPR)
1719 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1720 "suggest parentheses around %<+%> inside %<>>%>");
1721 else if (code_left == MINUS_EXPR)
1722 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1723 "suggest parentheses around %<-%> inside %<>>%>");
1724 else if (code_right == MINUS_EXPR)
1725 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1726 "suggest parentheses around %<-%> inside %<>>%>");
1727 return;
1729 case TRUTH_ORIF_EXPR:
1730 if (code_left == TRUTH_ANDIF_EXPR)
1731 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1732 "suggest parentheses around %<&&%> within %<||%>");
1733 else if (code_right == TRUTH_ANDIF_EXPR)
1734 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1735 "suggest parentheses around %<&&%> within %<||%>");
1736 return;
1738 case BIT_IOR_EXPR:
1739 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
1740 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
1741 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1742 "suggest parentheses around arithmetic in operand of %<|%>");
1743 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
1744 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
1745 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1746 "suggest parentheses around arithmetic in operand of %<|%>");
1747 /* Check cases like x|y==z */
1748 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
1749 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1750 "suggest parentheses around comparison in operand of %<|%>");
1751 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
1752 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1753 "suggest parentheses around comparison in operand of %<|%>");
1754 /* Check cases like !x | y */
1755 else if (code_left == TRUTH_NOT_EXPR
1756 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
1757 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1758 "suggest parentheses around operand of "
1759 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
1760 return;
1762 case BIT_XOR_EXPR:
1763 if (code_left == BIT_AND_EXPR
1764 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
1765 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1766 "suggest parentheses around arithmetic in operand of %<^%>");
1767 else if (code_right == BIT_AND_EXPR
1768 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
1769 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1770 "suggest parentheses around arithmetic in operand of %<^%>");
1771 /* Check cases like x^y==z */
1772 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
1773 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1774 "suggest parentheses around comparison in operand of %<^%>");
1775 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
1776 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1777 "suggest parentheses around comparison in operand of %<^%>");
1778 return;
1780 case BIT_AND_EXPR:
1781 if (code_left == PLUS_EXPR)
1782 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1783 "suggest parentheses around %<+%> in operand of %<&%>");
1784 else if (code_right == PLUS_EXPR)
1785 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1786 "suggest parentheses around %<+%> in operand of %<&%>");
1787 else if (code_left == MINUS_EXPR)
1788 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1789 "suggest parentheses around %<-%> in operand of %<&%>");
1790 else if (code_right == MINUS_EXPR)
1791 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1792 "suggest parentheses around %<-%> in operand of %<&%>");
1793 /* Check cases like x&y==z */
1794 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
1795 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1796 "suggest parentheses around comparison in operand of %<&%>");
1797 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
1798 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1799 "suggest parentheses around comparison in operand of %<&%>");
1800 /* Check cases like !x & y */
1801 else if (code_left == TRUTH_NOT_EXPR
1802 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
1803 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1804 "suggest parentheses around operand of "
1805 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
1806 return;
1808 case EQ_EXPR:
1809 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
1810 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1811 "suggest parentheses around comparison in operand of %<==%>");
1812 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
1813 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1814 "suggest parentheses around comparison in operand of %<==%>");
1815 return;
1816 case NE_EXPR:
1817 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
1818 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1819 "suggest parentheses around comparison in operand of %<!=%>");
1820 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
1821 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1822 "suggest parentheses around comparison in operand of %<!=%>");
1823 return;
1825 default:
1826 if (TREE_CODE_CLASS (code) == tcc_comparison)
1828 if (TREE_CODE_CLASS (code_left) == tcc_comparison
1829 && code_left != NE_EXPR && code_left != EQ_EXPR
1830 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
1831 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1832 "comparisons like %<X<=Y<=Z%> do not "
1833 "have their mathematical meaning");
1834 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
1835 && code_right != NE_EXPR && code_right != EQ_EXPR
1836 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
1837 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1838 "comparisons like %<X<=Y<=Z%> do not "
1839 "have their mathematical meaning");
1841 return;
1843 #undef NOT_A_BOOLEAN_EXPR_P
1846 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
1848 void
1849 warn_for_unused_label (tree label)
1851 if (!TREE_USED (label))
1853 if (DECL_INITIAL (label))
1854 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
1855 else
1856 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
1858 else if (asan_sanitize_use_after_scope ())
1860 if (asan_used_labels == NULL)
1861 asan_used_labels = new hash_set<tree> (16);
1863 asan_used_labels->add (label);
1867 /* Warn for division by zero according to the value of DIVISOR. LOC
1868 is the location of the division operator. */
1870 void
1871 warn_for_div_by_zero (location_t loc, tree divisor)
1873 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
1874 about division by zero. Do not issue a warning if DIVISOR has a
1875 floating-point type, since we consider 0.0/0.0 a valid way of
1876 generating a NaN. */
1877 if (c_inhibit_evaluation_warnings == 0
1878 && (integer_zerop (divisor) || fixed_zerop (divisor)))
1879 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
1882 /* Warn for patterns where memset appears to be used incorrectly. The
1883 warning location should be LOC. ARG0, and ARG2 are the first and
1884 last arguments to the call, while LITERAL_ZERO_MASK has a 1 bit for
1885 each argument that was a literal zero. */
1887 void
1888 warn_for_memset (location_t loc, tree arg0, tree arg2,
1889 int literal_zero_mask)
1891 arg0 = fold_for_warn (arg0);
1892 arg2 = fold_for_warn (arg2);
1894 if (warn_memset_transposed_args
1895 && integer_zerop (arg2)
1896 && (literal_zero_mask & (1 << 2)) != 0
1897 && (literal_zero_mask & (1 << 1)) == 0)
1898 warning_at (loc, OPT_Wmemset_transposed_args,
1899 "%<memset%> used with constant zero length "
1900 "parameter; this could be due to transposed "
1901 "parameters");
1903 if (warn_memset_elt_size && TREE_CODE (arg2) == INTEGER_CST)
1905 STRIP_NOPS (arg0);
1906 if (TREE_CODE (arg0) == ADDR_EXPR)
1907 arg0 = TREE_OPERAND (arg0, 0);
1908 tree type = TREE_TYPE (arg0);
1909 if (type != NULL_TREE && TREE_CODE (type) == ARRAY_TYPE)
1911 tree elt_type = TREE_TYPE (type);
1912 tree domain = TYPE_DOMAIN (type);
1913 if (COMPLETE_TYPE_P (elt_type)
1914 && !integer_onep (TYPE_SIZE_UNIT (elt_type))
1915 && domain != NULL_TREE
1916 && TYPE_MAX_VALUE (domain)
1917 && TYPE_MIN_VALUE (domain)
1918 && integer_zerop (TYPE_MIN_VALUE (domain))
1919 && integer_onep (fold_build2 (MINUS_EXPR, domain,
1920 arg2,
1921 TYPE_MAX_VALUE (domain))))
1922 warning_at (loc, OPT_Wmemset_elt_size,
1923 "%<memset%> used with length equal to "
1924 "number of elements without multiplication "
1925 "by element size");
1930 /* Subroutine of build_binary_op. Give warnings for comparisons
1931 between signed and unsigned quantities that may fail. Do the
1932 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
1933 so that casts will be considered, but default promotions won't
1936 LOCATION is the location of the comparison operator.
1938 The arguments of this function map directly to local variables
1939 of build_binary_op. */
1941 void
1942 warn_for_sign_compare (location_t location,
1943 tree orig_op0, tree orig_op1,
1944 tree op0, tree op1,
1945 tree result_type, enum tree_code resultcode)
1947 if (error_operand_p (orig_op0) || error_operand_p (orig_op1))
1948 return;
1950 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
1951 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
1952 int unsignedp0, unsignedp1;
1954 /* In C++, check for comparison of different enum types. */
1955 if (c_dialect_cxx()
1956 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
1957 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
1958 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
1959 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
1961 warning_at (location,
1962 OPT_Wsign_compare, "comparison between types %qT and %qT",
1963 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
1966 /* Do not warn if the comparison is being done in a signed type,
1967 since the signed type will only be chosen if it can represent
1968 all the values of the unsigned type. */
1969 if (!TYPE_UNSIGNED (result_type))
1970 /* OK */;
1971 /* Do not warn if both operands are unsigned. */
1972 else if (op0_signed == op1_signed)
1973 /* OK */;
1974 else
1976 tree sop, uop, base_type;
1977 bool ovf;
1979 if (op0_signed)
1980 sop = orig_op0, uop = orig_op1;
1981 else
1982 sop = orig_op1, uop = orig_op0;
1984 STRIP_TYPE_NOPS (sop);
1985 STRIP_TYPE_NOPS (uop);
1986 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
1987 ? TREE_TYPE (result_type) : result_type);
1989 /* Do not warn if the signed quantity is an unsuffixed integer
1990 literal (or some static constant expression involving such
1991 literals or a conditional expression involving such literals)
1992 and it is non-negative. */
1993 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
1994 /* OK */;
1995 /* Do not warn if the comparison is an equality operation, the
1996 unsigned quantity is an integral constant, and it would fit
1997 in the result if the result were signed. */
1998 else if (TREE_CODE (uop) == INTEGER_CST
1999 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
2000 && int_fits_type_p (uop, c_common_signed_type (base_type)))
2001 /* OK */;
2002 /* In C, do not warn if the unsigned quantity is an enumeration
2003 constant and its maximum value would fit in the result if the
2004 result were signed. */
2005 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
2006 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
2007 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
2008 c_common_signed_type (base_type)))
2009 /* OK */;
2010 else
2011 warning_at (location, OPT_Wsign_compare,
2012 "comparison of integer expressions of different "
2013 "signedness: %qT and %qT", TREE_TYPE (orig_op0),
2014 TREE_TYPE (orig_op1));
2017 /* Warn if two unsigned values are being compared in a size larger
2018 than their original size, and one (and only one) is the result of
2019 a `~' operator. This comparison will always fail.
2021 Also warn if one operand is a constant, and the constant does not
2022 have all bits set that are set in the ~ operand when it is
2023 extended. */
2025 op0 = c_common_get_narrower (op0, &unsignedp0);
2026 op1 = c_common_get_narrower (op1, &unsignedp1);
2028 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
2029 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
2031 if (TREE_CODE (op0) == BIT_NOT_EXPR)
2032 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
2033 if (TREE_CODE (op1) == BIT_NOT_EXPR)
2034 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
2036 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
2038 tree primop;
2039 HOST_WIDE_INT constant, mask;
2040 int unsignedp;
2041 unsigned int bits;
2043 if (tree_fits_shwi_p (op0))
2045 primop = op1;
2046 unsignedp = unsignedp1;
2047 constant = tree_to_shwi (op0);
2049 else
2051 primop = op0;
2052 unsignedp = unsignedp0;
2053 constant = tree_to_shwi (op1);
2056 bits = TYPE_PRECISION (TREE_TYPE (primop));
2057 if (bits < TYPE_PRECISION (result_type)
2058 && bits < HOST_BITS_PER_LONG && unsignedp)
2060 mask = HOST_WIDE_INT_M1U << bits;
2061 if ((mask & constant) != mask)
2063 if (constant == 0)
2064 warning_at (location, OPT_Wsign_compare,
2065 "promoted ~unsigned is always non-zero");
2066 else
2067 warning_at (location, OPT_Wsign_compare,
2068 "comparison of promoted ~unsigned with constant");
2072 else if (unsignedp0 && unsignedp1
2073 && (TYPE_PRECISION (TREE_TYPE (op0))
2074 < TYPE_PRECISION (result_type))
2075 && (TYPE_PRECISION (TREE_TYPE (op1))
2076 < TYPE_PRECISION (result_type)))
2077 warning_at (location, OPT_Wsign_compare,
2078 "comparison of promoted ~unsigned with unsigned");
2082 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
2083 type via c_common_type. If -Wdouble-promotion is in use, and the
2084 conditions for warning have been met, issue a warning. GMSGID is
2085 the warning message. It must have two %T specifiers for the type
2086 that was converted (generally "float") and the type to which it was
2087 converted (generally "double), respectively. LOC is the location
2088 to which the warning should refer. */
2090 void
2091 do_warn_double_promotion (tree result_type, tree type1, tree type2,
2092 const char *gmsgid, location_t loc)
2094 tree source_type;
2096 if (!warn_double_promotion)
2097 return;
2098 /* If the conversion will not occur at run-time, there is no need to
2099 warn about it. */
2100 if (c_inhibit_evaluation_warnings)
2101 return;
2102 /* If an invalid conversion has occured, don't warn. */
2103 if (result_type == error_mark_node)
2104 return;
2105 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
2106 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
2107 return;
2108 if (TYPE_MAIN_VARIANT (type1) == float_type_node
2109 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
2110 source_type = type1;
2111 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
2112 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
2113 source_type = type2;
2114 else
2115 return;
2116 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
2119 /* Possibly warn about unused parameters. */
2121 void
2122 do_warn_unused_parameter (tree fn)
2124 tree decl;
2126 for (decl = DECL_ARGUMENTS (fn);
2127 decl; decl = DECL_CHAIN (decl))
2128 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
2129 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
2130 && !TREE_NO_WARNING (decl))
2131 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter,
2132 "unused parameter %qD", decl);
2135 /* If DECL is a typedef that is declared in the current function,
2136 record it for the purpose of -Wunused-local-typedefs. */
2138 void
2139 record_locally_defined_typedef (tree decl)
2141 struct c_language_function *l;
2143 if (!warn_unused_local_typedefs
2144 || cfun == NULL
2145 /* if this is not a locally defined typedef then we are not
2146 interested. */
2147 || !is_typedef_decl (decl)
2148 || !decl_function_context (decl))
2149 return;
2151 l = (struct c_language_function *) cfun->language;
2152 vec_safe_push (l->local_typedefs, decl);
2155 /* If T is a TYPE_DECL declared locally, mark it as used. */
2157 void
2158 maybe_record_typedef_use (tree t)
2160 if (!is_typedef_decl (t))
2161 return;
2163 TREE_USED (t) = true;
2166 /* Warn if there are some unused locally defined typedefs in the
2167 current function. */
2169 void
2170 maybe_warn_unused_local_typedefs (void)
2172 int i;
2173 tree decl;
2174 /* The number of times we have emitted -Wunused-local-typedefs
2175 warnings. If this is different from errorcount, that means some
2176 unrelated errors have been issued. In which case, we'll avoid
2177 emitting "unused-local-typedefs" warnings. */
2178 static int unused_local_typedefs_warn_count;
2179 struct c_language_function *l;
2181 if (cfun == NULL)
2182 return;
2184 if ((l = (struct c_language_function *) cfun->language) == NULL)
2185 return;
2187 if (warn_unused_local_typedefs
2188 && errorcount == unused_local_typedefs_warn_count)
2190 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
2191 if (!TREE_USED (decl))
2192 warning_at (DECL_SOURCE_LOCATION (decl),
2193 OPT_Wunused_local_typedefs,
2194 "typedef %qD locally defined but not used", decl);
2195 unused_local_typedefs_warn_count = errorcount;
2198 vec_free (l->local_typedefs);
2201 /* If we're creating an if-else-if condition chain, first see if we
2202 already have this COND in the CHAIN. If so, warn and don't add COND
2203 into the vector, otherwise add the COND there. LOC is the location
2204 of COND. */
2206 void
2207 warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain)
2209 /* No chain has been created yet. Do nothing. */
2210 if (*chain == NULL)
2211 return;
2213 if (TREE_SIDE_EFFECTS (cond))
2215 /* Uh-oh! This condition has a side-effect, thus invalidates
2216 the whole chain. */
2217 delete *chain;
2218 *chain = NULL;
2219 return;
2222 unsigned int ix;
2223 tree t;
2224 bool found = false;
2225 FOR_EACH_VEC_ELT (**chain, ix, t)
2226 if (operand_equal_p (cond, t, 0))
2228 if (warning_at (loc, OPT_Wduplicated_cond,
2229 "duplicated %<if%> condition"))
2230 inform (EXPR_LOCATION (t), "previously used here");
2231 found = true;
2232 break;
2235 if (!found
2236 && !CONSTANT_CLASS_P (cond)
2237 /* Don't infinitely grow the chain. */
2238 && (*chain)->length () < 512)
2239 (*chain)->safe_push (cond);
2242 /* Check and possibly warn if two declarations have contradictory
2243 attributes, such as always_inline vs. noinline. */
2245 bool
2246 diagnose_mismatched_attributes (tree olddecl, tree newdecl)
2248 bool warned = false;
2250 tree a1 = lookup_attribute ("optimize", DECL_ATTRIBUTES (olddecl));
2251 tree a2 = lookup_attribute ("optimize", DECL_ATTRIBUTES (newdecl));
2252 /* An optimization attribute applied on a declaration after the
2253 definition is likely not what the user wanted. */
2254 if (a2 != NULL_TREE
2255 && DECL_SAVED_TREE (olddecl) != NULL_TREE
2256 && (a1 == NULL_TREE || !attribute_list_equal (a1, a2)))
2257 warned |= warning (OPT_Wattributes,
2258 "optimization attribute on %qD follows "
2259 "definition but the attribute doesn%'t match",
2260 newdecl);
2262 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2263 if (DECL_DECLARED_INLINE_P (newdecl)
2264 && DECL_UNINLINABLE (olddecl)
2265 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2266 warned |= warning (OPT_Wattributes, "inline declaration of %qD follows "
2267 "declaration with attribute %<noinline%>", newdecl);
2268 else if (DECL_DECLARED_INLINE_P (olddecl)
2269 && DECL_UNINLINABLE (newdecl)
2270 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2271 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
2272 "%<noinline%> follows inline declaration", newdecl);
2274 return warned;
2277 /* Warn if signed left shift overflows. We don't warn
2278 about left-shifting 1 into the sign bit in C++14; cf.
2279 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
2280 LOC is a location of the shift; OP0 and OP1 are the operands.
2281 Return true if an overflow is detected, false otherwise. */
2283 bool
2284 maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
2286 if (TREE_CODE (op0) != INTEGER_CST
2287 || TREE_CODE (op1) != INTEGER_CST)
2288 return false;
2290 tree type0 = TREE_TYPE (op0);
2291 unsigned int prec0 = TYPE_PRECISION (type0);
2293 /* Left-hand operand must be signed. */
2294 if (TYPE_UNSIGNED (type0))
2295 return false;
2297 unsigned int min_prec = (wi::min_precision (wi::to_wide (op0), SIGNED)
2298 + TREE_INT_CST_LOW (op1));
2299 /* Handle the case of left-shifting 1 into the sign bit.
2300 * However, shifting 1 _out_ of the sign bit, as in
2301 * INT_MIN << 1, is considered an overflow.
2303 if (!tree_int_cst_sign_bit(op0) && min_prec == prec0 + 1)
2305 /* Never warn for C++14 onwards. */
2306 if (cxx_dialect >= cxx14)
2307 return false;
2308 /* Otherwise only if -Wshift-overflow=2. But return
2309 true to signal an overflow for the sake of integer
2310 constant expressions. */
2311 if (warn_shift_overflow < 2)
2312 return true;
2315 bool overflowed = min_prec > prec0;
2316 if (overflowed && c_inhibit_evaluation_warnings == 0)
2317 warning_at (loc, OPT_Wshift_overflow_,
2318 "result of %qE requires %u bits to represent, "
2319 "but %qT only has %u bits",
2320 build2_loc (loc, LSHIFT_EXPR, type0, op0, op1),
2321 min_prec, type0, prec0);
2323 return overflowed;
2326 /* Warn about boolean expression compared with an integer value different
2327 from true/false. Warns also e.g. about "(i1 == i2) == 2".
2328 LOC is the location of the comparison, CODE is its code, OP0 and OP1
2329 are the operands of the comparison. The caller must ensure that
2330 either operand is a boolean expression. */
2332 void
2333 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
2334 tree op1)
2336 if (TREE_CODE_CLASS (code) != tcc_comparison)
2337 return;
2339 tree f, cst;
2340 if (f = fold_for_warn (op0),
2341 TREE_CODE (f) == INTEGER_CST)
2342 cst = op0 = f;
2343 else if (f = fold_for_warn (op1),
2344 TREE_CODE (f) == INTEGER_CST)
2345 cst = op1 = f;
2346 else
2347 return;
2349 if (!integer_zerop (cst) && !integer_onep (cst))
2351 int sign = (TREE_CODE (op0) == INTEGER_CST
2352 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
2353 if (code == EQ_EXPR
2354 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
2355 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
2356 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
2357 "with boolean expression is always false", cst);
2358 else
2359 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
2360 "with boolean expression is always true", cst);
2362 else if (integer_zerop (cst) || integer_onep (cst))
2364 /* If the non-constant operand isn't of a boolean type, we
2365 don't want to warn here. */
2366 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
2367 /* Handle booleans promoted to integers. */
2368 if (bool_promoted_to_int_p (noncst))
2369 /* Warn. */;
2370 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
2371 && !truth_value_p (TREE_CODE (noncst)))
2372 return;
2373 /* Do some magic to get the right diagnostics. */
2374 bool flag = TREE_CODE (op0) == INTEGER_CST;
2375 flag = integer_zerop (cst) ? flag : !flag;
2376 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
2377 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
2378 "with boolean expression is always true", cst);
2379 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
2380 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
2381 "with boolean expression is always false", cst);
2385 /* Warn if an argument at position param_pos is passed to a
2386 restrict-qualified param, and it aliases with another argument.
2387 Return true if a warning has been issued. */
2389 bool
2390 warn_for_restrict (unsigned param_pos, tree *argarray, unsigned nargs)
2392 tree arg = argarray[param_pos];
2393 if (TREE_VISITED (arg) || integer_zerop (arg))
2394 return false;
2396 location_t loc = EXPR_LOC_OR_LOC (arg, input_location);
2397 gcc_rich_location richloc (loc);
2399 unsigned i;
2400 auto_vec<int, 16> arg_positions;
2402 for (i = 0; i < nargs; i++)
2404 if (i == param_pos)
2405 continue;
2407 tree current_arg = argarray[i];
2408 if (operand_equal_p (arg, current_arg, 0))
2410 TREE_VISITED (current_arg) = 1;
2411 arg_positions.safe_push (i + 1);
2415 if (arg_positions.is_empty ())
2416 return false;
2418 int pos;
2419 FOR_EACH_VEC_ELT (arg_positions, i, pos)
2421 arg = argarray[pos - 1];
2422 if (EXPR_HAS_LOCATION (arg))
2423 richloc.add_range (EXPR_LOCATION (arg), false);
2426 return warning_n (&richloc, OPT_Wrestrict, arg_positions.length (),
2427 "passing argument %i to restrict-qualified parameter"
2428 " aliases with argument %Z",
2429 "passing argument %i to restrict-qualified parameter"
2430 " aliases with arguments %Z",
2431 param_pos + 1, arg_positions.address (),
2432 arg_positions.length ());
2435 /* Callback function to determine whether an expression TP or one of its
2436 subexpressions comes from macro expansion. Used to suppress bogus
2437 warnings. */
2439 static tree
2440 expr_from_macro_expansion_r (tree *tp, int *, void *)
2442 if (CAN_HAVE_LOCATION_P (*tp)
2443 && from_macro_expansion_at (EXPR_LOCATION (*tp)))
2444 return integer_zero_node;
2446 return NULL_TREE;
2449 /* Possibly warn when an if-else has identical branches. */
2451 static void
2452 do_warn_duplicated_branches (tree expr)
2454 tree thenb = COND_EXPR_THEN (expr);
2455 tree elseb = COND_EXPR_ELSE (expr);
2457 /* Don't bother if any of the branches is missing. */
2458 if (thenb == NULL_TREE || elseb == NULL_TREE)
2459 return;
2461 /* And don't warn for empty statements. */
2462 if (TREE_CODE (thenb) == NOP_EXPR
2463 && TREE_TYPE (thenb) == void_type_node
2464 && TREE_OPERAND (thenb, 0) == size_zero_node)
2465 return;
2467 /* ... or empty branches. */
2468 if (TREE_CODE (thenb) == STATEMENT_LIST
2469 && STATEMENT_LIST_HEAD (thenb) == NULL)
2470 return;
2472 /* Compute the hash of the then branch. */
2473 inchash::hash hstate0 (0);
2474 inchash::add_expr (thenb, hstate0);
2475 hashval_t h0 = hstate0.end ();
2477 /* Compute the hash of the else branch. */
2478 inchash::hash hstate1 (0);
2479 inchash::add_expr (elseb, hstate1);
2480 hashval_t h1 = hstate1.end ();
2482 /* Compare the hashes. */
2483 if (h0 == h1
2484 && operand_equal_p (thenb, elseb, OEP_LEXICOGRAPHIC)
2485 /* Don't warn if any of the branches or their subexpressions comes
2486 from a macro. */
2487 && !walk_tree_without_duplicates (&thenb, expr_from_macro_expansion_r,
2488 NULL)
2489 && !walk_tree_without_duplicates (&elseb, expr_from_macro_expansion_r,
2490 NULL))
2491 warning_at (EXPR_LOCATION (expr), OPT_Wduplicated_branches,
2492 "this condition has identical branches");
2495 /* Callback for c_genericize to implement -Wduplicated-branches. */
2497 tree
2498 do_warn_duplicated_branches_r (tree *tp, int *, void *)
2500 if (TREE_CODE (*tp) == COND_EXPR)
2501 do_warn_duplicated_branches (*tp);
2502 return NULL_TREE;
2505 /* Implementation of -Wmultistatement-macros. This warning warns about
2506 cases when a macro expands to multiple statements not wrapped in
2507 do {} while (0) or ({ }) and is used as a body of if/else/for/while
2508 conditionals. For example,
2510 #define DOIT x++; y++
2512 if (c)
2513 DOIT;
2515 will increment y unconditionally.
2517 BODY_LOC is the location of the first token in the body after labels
2518 have been parsed, NEXT_LOC is the location of the next token after the
2519 body of the conditional has been parsed, and GUARD_LOC is the location
2520 of the conditional. */
2522 void
2523 warn_for_multistatement_macros (location_t body_loc, location_t next_loc,
2524 location_t guard_loc, enum rid keyword)
2526 if (!warn_multistatement_macros)
2527 return;
2529 /* Ain't got time to waste. We only care about macros here. */
2530 if (!from_macro_expansion_at (body_loc)
2531 || !from_macro_expansion_at (next_loc))
2532 return;
2534 /* Let's skip macros defined in system headers. */
2535 if (in_system_header_at (body_loc)
2536 || in_system_header_at (next_loc))
2537 return;
2539 /* Find the actual tokens in the macro definition. BODY_LOC and
2540 NEXT_LOC have to come from the same spelling location, but they
2541 will resolve to different locations in the context of the macro
2542 definition. */
2543 location_t body_loc_exp
2544 = linemap_resolve_location (line_table, body_loc,
2545 LRK_MACRO_DEFINITION_LOCATION, NULL);
2546 location_t next_loc_exp
2547 = linemap_resolve_location (line_table, next_loc,
2548 LRK_MACRO_DEFINITION_LOCATION, NULL);
2549 location_t guard_loc_exp
2550 = linemap_resolve_location (line_table, guard_loc,
2551 LRK_MACRO_DEFINITION_LOCATION, NULL);
2553 /* These are some funky cases we don't want to warn about. */
2554 if (body_loc_exp == guard_loc_exp
2555 || next_loc_exp == guard_loc_exp
2556 || body_loc_exp == next_loc_exp)
2557 return;
2559 /* Find the macro maps for the macro expansions. */
2560 const line_map *body_map = linemap_lookup (line_table, body_loc);
2561 const line_map *next_map = linemap_lookup (line_table, next_loc);
2562 const line_map *guard_map = linemap_lookup (line_table, guard_loc);
2564 /* Now see if the following token (after the body) is coming from the
2565 same macro expansion. If it is, it might be a problem. */
2566 if (body_map != next_map)
2567 return;
2569 /* The conditional itself must not come from the same expansion, because
2570 we don't want to warn about
2571 #define IF if (x) x++; y++
2572 and similar. */
2573 if (guard_map == body_map)
2574 return;
2576 /* Handle the case where NEXT and BODY come from the same expansion while
2577 GUARD doesn't, yet we shouldn't warn. E.g.
2579 #define GUARD if (...)
2580 #define GUARD2 GUARD
2582 and in the definition of another macro:
2584 GUARD2
2585 foo ();
2586 return 1;
2588 while (linemap_macro_expansion_map_p (guard_map))
2590 const line_map_macro *mm = linemap_check_macro (guard_map);
2591 guard_loc_exp = MACRO_MAP_EXPANSION_POINT_LOCATION (mm);
2592 guard_map = linemap_lookup (line_table, guard_loc_exp);
2593 if (guard_map == body_map)
2594 return;
2597 if (warning_at (body_loc, OPT_Wmultistatement_macros,
2598 "macro expands to multiple statements"))
2599 inform (guard_loc, "some parts of macro expansion are not guarded by "
2600 "this %qs clause", guard_tinfo_to_string (keyword));