Make vectorizable_load/store handle IFN_MASK_LOAD/STORE
[official-gcc.git] / gcc / c-family / c-warn.c
blob33cae8be7fdde4496862404450fef4712398c6f7
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
603 of strict aliasing rules if -Wstrict-aliasing is used and
604 strict aliasing mode is in effect. OTYPE is the original
605 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
607 bool
608 strict_aliasing_warning (tree otype, tree type, tree expr)
610 /* Strip pointer conversion chains and get to the correct original type. */
611 STRIP_NOPS (expr);
612 otype = TREE_TYPE (expr);
614 if (!(flag_strict_aliasing
615 && POINTER_TYPE_P (type)
616 && POINTER_TYPE_P (otype)
617 && !VOID_TYPE_P (TREE_TYPE (type)))
618 /* If the type we are casting to is a ref-all pointer
619 dereferencing it is always valid. */
620 || TYPE_REF_CAN_ALIAS_ALL (type))
621 return false;
623 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
624 && (DECL_P (TREE_OPERAND (expr, 0))
625 || handled_component_p (TREE_OPERAND (expr, 0))))
627 /* Casting the address of an object to non void pointer. Warn
628 if the cast breaks type based aliasing. */
629 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
631 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
632 "might break strict-aliasing rules");
633 return true;
635 else
637 /* warn_strict_aliasing >= 3. This includes the default (3).
638 Only warn if the cast is dereferenced immediately. */
639 alias_set_type set1
640 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
641 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
643 if (set2 != 0
644 && set1 != set2
645 && !alias_set_subset_of (set2, set1)
646 && !alias_sets_conflict_p (set1, set2))
648 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
649 "pointer will break strict-aliasing rules");
650 return true;
652 else if (warn_strict_aliasing == 2
653 && !alias_sets_must_conflict_p (set1, set2))
655 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
656 "pointer might break strict-aliasing rules");
657 return true;
661 else if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
663 /* At this level, warn for any conversions, even if an address is
664 not taken in the same statement. This will likely produce many
665 false positives, but could be useful to pinpoint problems that
666 are not revealed at higher levels. */
667 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
668 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
669 if (!COMPLETE_TYPE_P (type)
670 || !alias_sets_must_conflict_p (set1, set2))
672 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
673 "pointer might break strict-aliasing rules");
674 return true;
678 return false;
681 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
682 sizeof as last operand of certain builtins. */
684 void
685 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
686 vec<tree, va_gc> *params, tree *sizeof_arg,
687 bool (*comp_types) (tree, tree))
689 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
690 bool strop = false, cmp = false;
691 unsigned int idx = ~0;
692 location_t loc;
694 if (TREE_CODE (callee) != FUNCTION_DECL
695 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
696 || vec_safe_length (params) <= 1)
697 return;
699 enum built_in_function fncode = DECL_FUNCTION_CODE (callee);
700 switch (fncode)
702 case BUILT_IN_STRNCMP:
703 case BUILT_IN_STRNCASECMP:
704 cmp = true;
705 /* FALLTHRU */
706 case BUILT_IN_STRNCPY:
707 case BUILT_IN_STRNCPY_CHK:
708 case BUILT_IN_STRNCAT:
709 case BUILT_IN_STRNCAT_CHK:
710 case BUILT_IN_STPNCPY:
711 case BUILT_IN_STPNCPY_CHK:
712 strop = true;
713 /* FALLTHRU */
714 case BUILT_IN_MEMCPY:
715 case BUILT_IN_MEMCPY_CHK:
716 case BUILT_IN_MEMMOVE:
717 case BUILT_IN_MEMMOVE_CHK:
718 if (params->length () < 3)
719 return;
720 src = (*params)[1];
721 dest = (*params)[0];
722 idx = 2;
723 break;
724 case BUILT_IN_BCOPY:
725 if (params->length () < 3)
726 return;
727 src = (*params)[0];
728 dest = (*params)[1];
729 idx = 2;
730 break;
731 case BUILT_IN_MEMCMP:
732 case BUILT_IN_BCMP:
733 if (params->length () < 3)
734 return;
735 src = (*params)[1];
736 dest = (*params)[0];
737 idx = 2;
738 cmp = true;
739 break;
740 case BUILT_IN_MEMSET:
741 case BUILT_IN_MEMSET_CHK:
742 if (params->length () < 3)
743 return;
744 dest = (*params)[0];
745 idx = 2;
746 break;
747 case BUILT_IN_BZERO:
748 dest = (*params)[0];
749 idx = 1;
750 break;
751 case BUILT_IN_STRNDUP:
752 src = (*params)[0];
753 strop = true;
754 idx = 1;
755 break;
756 case BUILT_IN_MEMCHR:
757 if (params->length () < 3)
758 return;
759 src = (*params)[0];
760 idx = 2;
761 break;
762 case BUILT_IN_SNPRINTF:
763 case BUILT_IN_SNPRINTF_CHK:
764 case BUILT_IN_VSNPRINTF:
765 case BUILT_IN_VSNPRINTF_CHK:
766 dest = (*params)[0];
767 idx = 1;
768 strop = true;
769 break;
770 default:
771 break;
774 if (idx >= 3)
775 return;
777 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
778 return;
780 type = TYPE_P (sizeof_arg[idx])
781 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
783 if (!POINTER_TYPE_P (type))
785 /* The argument type may be an array. Diagnose bounded string
786 copy functions that specify the bound in terms of the source
787 argument rather than the destination. */
788 if (strop && !cmp && fncode != BUILT_IN_STRNDUP && src)
790 tem = tree_strip_nop_conversions (src);
791 if (TREE_CODE (tem) == ADDR_EXPR)
792 tem = TREE_OPERAND (tem, 0);
793 if (operand_equal_p (tem, sizeof_arg[idx], OEP_ADDRESS_OF))
794 warning_at (sizeof_arg_loc[idx], OPT_Wsizeof_pointer_memaccess,
795 "argument to %<sizeof%> in %qD call is the same "
796 "expression as the source; did you mean to use "
797 "the size of the destination?",
798 callee);
801 return;
804 if (dest
805 && (tem = tree_strip_nop_conversions (dest))
806 && POINTER_TYPE_P (TREE_TYPE (tem))
807 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
808 return;
810 if (src
811 && (tem = tree_strip_nop_conversions (src))
812 && POINTER_TYPE_P (TREE_TYPE (tem))
813 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
814 return;
816 loc = sizeof_arg_loc[idx];
818 if (dest && !cmp)
820 if (!TYPE_P (sizeof_arg[idx])
821 && operand_equal_p (dest, sizeof_arg[idx], 0)
822 && comp_types (TREE_TYPE (dest), type))
824 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
825 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
826 "argument to %<sizeof%> in %qD call is the same "
827 "expression as the destination; did you mean to "
828 "remove the addressof?", callee);
829 else if ((TYPE_PRECISION (TREE_TYPE (type))
830 == TYPE_PRECISION (char_type_node))
831 || strop)
832 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
833 "argument to %<sizeof%> in %qD call is the same "
834 "expression as the destination; did you mean to "
835 "provide an explicit length?", callee);
836 else
837 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
838 "argument to %<sizeof%> in %qD call is the same "
839 "expression as the destination; did you mean to "
840 "dereference it?", callee);
841 return;
844 if (POINTER_TYPE_P (TREE_TYPE (dest))
845 && !strop
846 && comp_types (TREE_TYPE (dest), type)
847 && !VOID_TYPE_P (TREE_TYPE (type)))
849 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
850 "argument to %<sizeof%> in %qD call is the same "
851 "pointer type %qT as the destination; expected %qT "
852 "or an explicit length", callee, TREE_TYPE (dest),
853 TREE_TYPE (TREE_TYPE (dest)));
854 return;
858 if (src && !cmp)
860 if (!TYPE_P (sizeof_arg[idx])
861 && operand_equal_p (src, sizeof_arg[idx], 0)
862 && comp_types (TREE_TYPE (src), type))
864 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
865 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
866 "argument to %<sizeof%> in %qD call is the same "
867 "expression as the source; did you mean to "
868 "remove the addressof?", callee);
869 else if ((TYPE_PRECISION (TREE_TYPE (type))
870 == TYPE_PRECISION (char_type_node))
871 || strop)
872 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
873 "argument to %<sizeof%> in %qD call is the same "
874 "expression as the source; did you mean to "
875 "provide an explicit length?", callee);
876 else
877 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
878 "argument to %<sizeof%> in %qD call is the same "
879 "expression as the source; did you mean to "
880 "dereference it?", callee);
881 return;
884 if (POINTER_TYPE_P (TREE_TYPE (src))
885 && !strop
886 && comp_types (TREE_TYPE (src), type)
887 && !VOID_TYPE_P (TREE_TYPE (type)))
889 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
890 "argument to %<sizeof%> in %qD call is the same "
891 "pointer type %qT as the source; expected %qT "
892 "or an explicit length", callee, TREE_TYPE (src),
893 TREE_TYPE (TREE_TYPE (src)));
894 return;
898 if (dest)
900 if (!TYPE_P (sizeof_arg[idx])
901 && operand_equal_p (dest, sizeof_arg[idx], 0)
902 && comp_types (TREE_TYPE (dest), type))
904 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
905 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
906 "argument to %<sizeof%> in %qD call is the same "
907 "expression as the first source; did you mean to "
908 "remove the addressof?", callee);
909 else if ((TYPE_PRECISION (TREE_TYPE (type))
910 == TYPE_PRECISION (char_type_node))
911 || strop)
912 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
913 "argument to %<sizeof%> in %qD call is the same "
914 "expression as the first source; did you mean to "
915 "provide an explicit length?", callee);
916 else
917 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
918 "argument to %<sizeof%> in %qD call is the same "
919 "expression as the first source; did you mean to "
920 "dereference it?", callee);
921 return;
924 if (POINTER_TYPE_P (TREE_TYPE (dest))
925 && !strop
926 && comp_types (TREE_TYPE (dest), type)
927 && !VOID_TYPE_P (TREE_TYPE (type)))
929 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
930 "argument to %<sizeof%> in %qD call is the same "
931 "pointer type %qT as the first source; expected %qT "
932 "or an explicit length", callee, TREE_TYPE (dest),
933 TREE_TYPE (TREE_TYPE (dest)));
934 return;
938 if (src)
940 if (!TYPE_P (sizeof_arg[idx])
941 && operand_equal_p (src, sizeof_arg[idx], 0)
942 && comp_types (TREE_TYPE (src), type))
944 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
945 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
946 "argument to %<sizeof%> in %qD call is the same "
947 "expression as the second source; did you mean to "
948 "remove the addressof?", callee);
949 else if ((TYPE_PRECISION (TREE_TYPE (type))
950 == TYPE_PRECISION (char_type_node))
951 || strop)
952 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
953 "argument to %<sizeof%> in %qD call is the same "
954 "expression as the second source; did you mean to "
955 "provide an explicit length?", callee);
956 else
957 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
958 "argument to %<sizeof%> in %qD call is the same "
959 "expression as the second source; did you mean to "
960 "dereference it?", callee);
961 return;
964 if (POINTER_TYPE_P (TREE_TYPE (src))
965 && !strop
966 && comp_types (TREE_TYPE (src), type)
967 && !VOID_TYPE_P (TREE_TYPE (type)))
969 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
970 "argument to %<sizeof%> in %qD call is the same "
971 "pointer type %qT as the second source; expected %qT "
972 "or an explicit length", callee, TREE_TYPE (src),
973 TREE_TYPE (TREE_TYPE (src)));
974 return;
980 /* Warn for unlikely, improbable, or stupid DECL declarations
981 of `main'. */
983 void
984 check_main_parameter_types (tree decl)
986 function_args_iterator iter;
987 tree type;
988 int argct = 0;
990 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
992 /* XXX void_type_node belies the abstraction. */
993 if (type == void_type_node || type == error_mark_node)
994 break;
996 tree t = type;
997 if (TYPE_ATOMIC (t))
998 pedwarn (input_location, OPT_Wmain,
999 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1000 type, decl);
1001 while (POINTER_TYPE_P (t))
1003 t = TREE_TYPE (t);
1004 if (TYPE_ATOMIC (t))
1005 pedwarn (input_location, OPT_Wmain,
1006 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1007 type, decl);
1010 ++argct;
1011 switch (argct)
1013 case 1:
1014 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
1015 pedwarn (input_location, OPT_Wmain,
1016 "first argument of %q+D should be %<int%>", decl);
1017 break;
1019 case 2:
1020 if (TREE_CODE (type) != POINTER_TYPE
1021 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1022 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1023 != char_type_node))
1024 pedwarn (input_location, OPT_Wmain,
1025 "second argument of %q+D should be %<char **%>", decl);
1026 break;
1028 case 3:
1029 if (TREE_CODE (type) != POINTER_TYPE
1030 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1031 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1032 != char_type_node))
1033 pedwarn (input_location, OPT_Wmain,
1034 "third argument of %q+D should probably be "
1035 "%<char **%>", decl);
1036 break;
1040 /* It is intentional that this message does not mention the third
1041 argument because it's only mentioned in an appendix of the
1042 standard. */
1043 if (argct > 0 && (argct < 2 || argct > 3))
1044 pedwarn (input_location, OPT_Wmain,
1045 "%q+D takes only zero or two arguments", decl);
1047 if (stdarg_p (TREE_TYPE (decl)))
1048 pedwarn (input_location, OPT_Wmain,
1049 "%q+D declared as variadic function", decl);
1052 /* Warns if the conversion of EXPR to TYPE may alter a value.
1053 This is a helper function for warnings_for_convert_and_check. */
1055 static void
1056 conversion_warning (location_t loc, tree type, tree expr, tree result)
1058 tree expr_type = TREE_TYPE (expr);
1059 enum conversion_safety conversion_kind;
1061 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
1062 return;
1064 /* This may happen, because for LHS op= RHS we preevaluate
1065 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
1066 means we could no longer see the code of the EXPR. */
1067 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
1068 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
1069 if (TREE_CODE (expr) == SAVE_EXPR)
1070 expr = TREE_OPERAND (expr, 0);
1072 switch (TREE_CODE (expr))
1074 case EQ_EXPR:
1075 case NE_EXPR:
1076 case LE_EXPR:
1077 case GE_EXPR:
1078 case LT_EXPR:
1079 case GT_EXPR:
1080 case TRUTH_ANDIF_EXPR:
1081 case TRUTH_ORIF_EXPR:
1082 case TRUTH_AND_EXPR:
1083 case TRUTH_OR_EXPR:
1084 case TRUTH_XOR_EXPR:
1085 case TRUTH_NOT_EXPR:
1086 /* Conversion from boolean to a signed:1 bit-field (which only
1087 can hold the values 0 and -1) doesn't lose information - but
1088 it does change the value. */
1089 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
1090 warning_at (loc, OPT_Wconversion,
1091 "conversion to %qT from boolean expression", type);
1092 return;
1094 case REAL_CST:
1095 case INTEGER_CST:
1096 case COMPLEX_CST:
1098 conversion_kind = unsafe_conversion_p (loc, type, expr, result, true);
1099 int warnopt;
1100 if (conversion_kind == UNSAFE_REAL)
1101 warnopt = OPT_Wfloat_conversion;
1102 else if (conversion_kind)
1103 warnopt = OPT_Wconversion;
1104 else
1105 break;
1107 if (TREE_CODE_CLASS (TREE_CODE (result)) == tcc_constant)
1108 warning_at (loc, warnopt,
1109 "conversion from %qT to %qT changes value from %qE to %qE",
1110 expr_type, type, expr, result);
1111 else
1112 warning_at (loc, warnopt,
1113 "conversion from %qT to %qT changes the value of %qE",
1114 expr_type, type, expr);
1115 break;
1117 case COND_EXPR:
1119 /* In case of COND_EXPR, we do not care about the type of
1120 COND_EXPR, only about the conversion of each operand. */
1121 tree op1 = TREE_OPERAND (expr, 1);
1122 tree op2 = TREE_OPERAND (expr, 2);
1124 conversion_warning (loc, type, op1, result);
1125 conversion_warning (loc, type, op2, result);
1126 return;
1129 default: /* 'expr' is not a constant. */
1130 conversion_kind = unsafe_conversion_p (loc, type, expr, result, true);
1131 if (conversion_kind == UNSAFE_IMAGINARY)
1132 warning_at (loc, OPT_Wconversion,
1133 "conversion from %qT to to %qT discards imaginary "
1134 "component",
1135 expr_type, type);
1136 else
1138 int warnopt;
1139 if (conversion_kind == UNSAFE_REAL)
1140 warnopt = OPT_Wfloat_conversion;
1141 else if (conversion_kind)
1142 warnopt = OPT_Wconversion;
1143 else
1144 break;
1145 warning_at (loc, warnopt,
1146 "conversion from %qT to %qT may change value",
1147 expr_type, type);
1152 /* Produce warnings after a conversion. RESULT is the result of
1153 converting EXPR to TYPE. This is a helper function for
1154 convert_and_check and cp_convert_and_check. */
1156 void
1157 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
1158 tree result)
1160 loc = expansion_point_location_if_in_system_header (loc);
1162 bool cst = TREE_CODE_CLASS (TREE_CODE (result)) == tcc_constant;
1164 tree exprtype = TREE_TYPE (expr);
1166 if (TREE_CODE (expr) == INTEGER_CST
1167 && (TREE_CODE (type) == INTEGER_TYPE
1168 || TREE_CODE (type) == ENUMERAL_TYPE)
1169 && !int_fits_type_p (expr, type))
1171 /* Do not diagnose overflow in a constant expression merely
1172 because a conversion overflowed. */
1173 if (TREE_OVERFLOW (result))
1174 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
1176 if (TYPE_UNSIGNED (type))
1178 /* This detects cases like converting -129 or 256 to
1179 unsigned char. */
1180 if (!int_fits_type_p (expr, c_common_signed_type (type)))
1182 if (cst)
1183 warning_at (loc, OPT_Woverflow,
1184 (TYPE_UNSIGNED (exprtype)
1185 ? G_("conversion from %qT to %qT "
1186 "changes value from %qE to %qE")
1187 : G_("unsigned conversion from %qT to %qT "
1188 "changes value from %qE to %qE")),
1189 exprtype, type, expr, result);
1190 else
1191 warning_at (loc, OPT_Woverflow,
1192 (TYPE_UNSIGNED (exprtype)
1193 ? G_("conversion from %qT to %qT "
1194 "changes the value of %qE")
1195 : G_("unsigned conversion from %qT to %qT "
1196 "changes the value of %qE")),
1197 exprtype, type, expr);
1199 else
1200 conversion_warning (loc, type, expr, result);
1202 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
1204 if (cst)
1205 warning_at (loc, OPT_Woverflow,
1206 "overflow in conversion from %qT to %qT "
1207 "changes value from %qE to %qE",
1208 exprtype, type, expr, result);
1209 else
1210 warning_at (loc, OPT_Woverflow,
1211 "overflow in conversion from %qT to %qT "
1212 "changes the value of %qE",
1213 exprtype, type, expr);
1215 /* No warning for converting 0x80000000 to int. */
1216 else if (pedantic
1217 && (TREE_CODE (exprtype) != INTEGER_TYPE
1218 || TYPE_PRECISION (exprtype)
1219 != TYPE_PRECISION (type)))
1221 if (cst)
1222 warning_at (loc, OPT_Woverflow,
1223 "overflow in conversion from %qT to %qT "
1224 "changes value from %qE to %qE",
1225 exprtype, type, expr, result);
1226 else
1227 warning_at (loc, OPT_Woverflow,
1228 "overflow in conversion from %qT to %qT "
1229 "changes the value of %qE",
1230 exprtype, type, expr);
1232 else
1233 conversion_warning (loc, type, expr, result);
1235 else if ((TREE_CODE (result) == INTEGER_CST
1236 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
1238 if (cst)
1239 warning_at (loc, OPT_Woverflow,
1240 "overflow in conversion from %qT to %qT "
1241 "changes value from %qE to %qE",
1242 exprtype, type, expr, result);
1243 else
1244 warning_at (loc, OPT_Woverflow,
1245 "overflow in conversion from %qT to %qT "
1246 "changes the value of %qE",
1247 exprtype, type, expr);
1249 else
1250 conversion_warning (loc, type, expr, result);
1253 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
1254 Used to verify that case values match up with enumerator values. */
1256 static void
1257 match_case_to_enum_1 (tree key, tree type, tree label)
1259 /* Avoid warning about enums that have no enumerators. */
1260 if (TYPE_VALUES (type) == NULL_TREE)
1261 return;
1263 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
1265 if (tree_fits_uhwi_p (key))
1266 print_dec (wi::to_wide (key), buf, UNSIGNED);
1267 else if (tree_fits_shwi_p (key))
1268 print_dec (wi::to_wide (key), buf, SIGNED);
1269 else
1270 print_hex (wi::to_wide (key), buf);
1272 if (TYPE_NAME (type) == NULL_TREE)
1273 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
1274 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
1275 "case value %qs not in enumerated type",
1276 buf);
1277 else
1278 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
1279 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
1280 "case value %qs not in enumerated type %qT",
1281 buf, type);
1284 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
1285 Used to verify that case values match up with enumerator values. */
1287 static int
1288 match_case_to_enum (splay_tree_node node, void *data)
1290 tree label = (tree) node->value;
1291 tree type = (tree) data;
1293 /* Skip default case. */
1294 if (!CASE_LOW (label))
1295 return 0;
1297 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
1298 when we did our enum->case scan. Reset our scratch bit after. */
1299 if (!CASE_LOW_SEEN (label))
1300 match_case_to_enum_1 (CASE_LOW (label), type, label);
1301 else
1302 CASE_LOW_SEEN (label) = 0;
1304 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
1305 not set, that means that CASE_HIGH did not appear when we did our
1306 enum->case scan. Reset our scratch bit after. */
1307 if (CASE_HIGH (label))
1309 if (!CASE_HIGH_SEEN (label))
1310 match_case_to_enum_1 (CASE_HIGH (label), type, label);
1311 else
1312 CASE_HIGH_SEEN (label) = 0;
1315 return 0;
1318 /* Handle -Wswitch*. Called from the front end after parsing the
1319 switch construct. */
1320 /* ??? Should probably be somewhere generic, since other languages
1321 besides C and C++ would want this. At the moment, however, C/C++
1322 are the only tree-ssa languages that support enumerations at all,
1323 so the point is moot. */
1325 void
1326 c_do_switch_warnings (splay_tree cases, location_t switch_location,
1327 tree type, tree cond, bool bool_cond_p,
1328 bool outside_range_p)
1330 splay_tree_node default_node;
1331 splay_tree_node node;
1332 tree chain;
1334 if (!warn_switch && !warn_switch_enum && !warn_switch_default
1335 && !warn_switch_bool)
1336 return;
1338 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
1339 if (!default_node)
1340 warning_at (switch_location, OPT_Wswitch_default,
1341 "switch missing default case");
1343 /* There are certain cases where -Wswitch-bool warnings aren't
1344 desirable, such as
1345 switch (boolean)
1347 case true: ...
1348 case false: ...
1350 so be careful here. */
1351 if (warn_switch_bool && bool_cond_p)
1353 splay_tree_node min_node;
1354 /* If there's a default node, it's also the value with the minimal
1355 key. So look at the penultimate key (if any). */
1356 if (default_node)
1357 min_node = splay_tree_successor (cases, (splay_tree_key) NULL);
1358 else
1359 min_node = splay_tree_min (cases);
1360 tree min = min_node ? (tree) min_node->key : NULL_TREE;
1362 splay_tree_node max_node = splay_tree_max (cases);
1363 /* This might be a case range, so look at the value with the
1364 maximal key and then check CASE_HIGH. */
1365 tree max = max_node ? (tree) max_node->value : NULL_TREE;
1366 if (max)
1367 max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max);
1369 /* If there's a case value > 1 or < 0, that is outside bool
1370 range, warn. */
1371 if (outside_range_p
1372 || (max && wi::gts_p (wi::to_wide (max), 1))
1373 || (min && wi::lts_p (wi::to_wide (min), 0))
1374 /* And handle the
1375 switch (boolean)
1377 case true: ...
1378 case false: ...
1379 default: ...
1381 case, where we want to warn. */
1382 || (default_node
1383 && max && wi::to_wide (max) == 1
1384 && min && wi::to_wide (min) == 0))
1385 warning_at (switch_location, OPT_Wswitch_bool,
1386 "switch condition has boolean value");
1389 /* From here on, we only care about enumerated types. */
1390 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
1391 return;
1393 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
1394 if (!warn_switch_enum && !warn_switch)
1395 return;
1397 /* Check the cases. Warn about case values which are not members of
1398 the enumerated type. For -Wswitch-enum, or for -Wswitch when
1399 there is no default case, check that exactly all enumeration
1400 literals are covered by the cases. */
1402 /* Clearing COND if it is not an integer constant simplifies
1403 the tests inside the loop below. */
1404 if (TREE_CODE (cond) != INTEGER_CST)
1405 cond = NULL_TREE;
1407 /* The time complexity here is O(N*lg(N)) worst case, but for the
1408 common case of monotonically increasing enumerators, it is
1409 O(N), since the nature of the splay tree will keep the next
1410 element adjacent to the root at all times. */
1412 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
1414 tree value = TREE_VALUE (chain);
1415 if (TREE_CODE (value) == CONST_DECL)
1416 value = DECL_INITIAL (value);
1417 node = splay_tree_lookup (cases, (splay_tree_key) value);
1418 if (node)
1420 /* Mark the CASE_LOW part of the case entry as seen. */
1421 tree label = (tree) node->value;
1422 CASE_LOW_SEEN (label) = 1;
1423 continue;
1426 /* Even though there wasn't an exact match, there might be a
1427 case range which includes the enumerator's value. */
1428 node = splay_tree_predecessor (cases, (splay_tree_key) value);
1429 if (node && CASE_HIGH ((tree) node->value))
1431 tree label = (tree) node->value;
1432 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
1433 if (cmp >= 0)
1435 /* If we match the upper bound exactly, mark the CASE_HIGH
1436 part of the case entry as seen. */
1437 if (cmp == 0)
1438 CASE_HIGH_SEEN (label) = 1;
1439 continue;
1443 /* We've now determined that this enumerated literal isn't
1444 handled by the case labels of the switch statement. */
1446 /* If the switch expression is a constant, we only really care
1447 about whether that constant is handled by the switch. */
1448 if (cond && tree_int_cst_compare (cond, value))
1449 continue;
1451 /* If there is a default_node, the only relevant option is
1452 Wswitch-enum. Otherwise, if both are enabled then we prefer
1453 to warn using -Wswitch because -Wswitch is enabled by -Wall
1454 while -Wswitch-enum is explicit. */
1455 warning_at (switch_location,
1456 (default_node || !warn_switch
1457 ? OPT_Wswitch_enum
1458 : OPT_Wswitch),
1459 "enumeration value %qE not handled in switch",
1460 TREE_PURPOSE (chain));
1463 /* Warn if there are case expressions that don't correspond to
1464 enumerators. This can occur since C and C++ don't enforce
1465 type-checking of assignments to enumeration variables.
1467 The time complexity here is now always O(N) worst case, since
1468 we should have marked both the lower bound and upper bound of
1469 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
1470 above. This scan also resets those fields. */
1472 splay_tree_foreach (cases, match_case_to_enum, type);
1475 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
1476 expression, because B will always be true. */
1478 void
1479 warn_for_omitted_condop (location_t location, tree cond)
1481 /* In C++ template declarations it can happen that the type is dependent
1482 and not yet known, thus TREE_TYPE (cond) == NULL_TREE. */
1483 if (truth_value_p (TREE_CODE (cond))
1484 || (TREE_TYPE (cond) != NULL_TREE
1485 && TREE_CODE (TREE_TYPE (cond)) == BOOLEAN_TYPE))
1486 warning_at (location, OPT_Wparentheses,
1487 "the omitted middle operand in ?: will always be %<true%>, "
1488 "suggest explicit middle operand");
1491 /* Give an error for storing into ARG, which is 'const'. USE indicates
1492 how ARG was being used. */
1494 void
1495 readonly_error (location_t loc, tree arg, enum lvalue_use use)
1497 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
1498 || use == lv_asm);
1499 /* Using this macro rather than (for example) arrays of messages
1500 ensures that all the format strings are checked at compile
1501 time. */
1502 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
1503 : (use == lv_increment ? (I) \
1504 : (use == lv_decrement ? (D) : (AS))))
1505 if (TREE_CODE (arg) == COMPONENT_REF)
1507 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
1508 error_at (loc, READONLY_MSG (G_("assignment of member "
1509 "%qD in read-only object"),
1510 G_("increment of member "
1511 "%qD in read-only object"),
1512 G_("decrement of member "
1513 "%qD in read-only object"),
1514 G_("member %qD in read-only object "
1515 "used as %<asm%> output")),
1516 TREE_OPERAND (arg, 1));
1517 else
1518 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
1519 G_("increment of read-only member %qD"),
1520 G_("decrement of read-only member %qD"),
1521 G_("read-only member %qD used as %<asm%> output")),
1522 TREE_OPERAND (arg, 1));
1524 else if (VAR_P (arg))
1525 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
1526 G_("increment of read-only variable %qD"),
1527 G_("decrement of read-only variable %qD"),
1528 G_("read-only variable %qD used as %<asm%> output")),
1529 arg);
1530 else if (TREE_CODE (arg) == PARM_DECL)
1531 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
1532 G_("increment of read-only parameter %qD"),
1533 G_("decrement of read-only parameter %qD"),
1534 G_("read-only parameter %qD use as %<asm%> output")),
1535 arg);
1536 else if (TREE_CODE (arg) == RESULT_DECL)
1538 gcc_assert (c_dialect_cxx ());
1539 error_at (loc, READONLY_MSG (G_("assignment of "
1540 "read-only named return value %qD"),
1541 G_("increment of "
1542 "read-only named return value %qD"),
1543 G_("decrement of "
1544 "read-only named return value %qD"),
1545 G_("read-only named return value %qD "
1546 "used as %<asm%>output")),
1547 arg);
1549 else if (TREE_CODE (arg) == FUNCTION_DECL)
1550 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
1551 G_("increment of function %qD"),
1552 G_("decrement of function %qD"),
1553 G_("function %qD used as %<asm%> output")),
1554 arg);
1555 else
1556 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
1557 G_("increment of read-only location %qE"),
1558 G_("decrement of read-only location %qE"),
1559 G_("read-only location %qE used as %<asm%> output")),
1560 arg);
1563 /* Print an error message for an invalid lvalue. USE says
1564 how the lvalue is being used and so selects the error message. LOC
1565 is the location for the error. */
1567 void
1568 lvalue_error (location_t loc, enum lvalue_use use)
1570 switch (use)
1572 case lv_assign:
1573 error_at (loc, "lvalue required as left operand of assignment");
1574 break;
1575 case lv_increment:
1576 error_at (loc, "lvalue required as increment operand");
1577 break;
1578 case lv_decrement:
1579 error_at (loc, "lvalue required as decrement operand");
1580 break;
1581 case lv_addressof:
1582 error_at (loc, "lvalue required as unary %<&%> operand");
1583 break;
1584 case lv_asm:
1585 error_at (loc, "lvalue required in asm statement");
1586 break;
1587 default:
1588 gcc_unreachable ();
1592 /* Print an error message for an invalid indirection of type TYPE.
1593 ERRSTRING is the name of the operator for the indirection. */
1595 void
1596 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
1598 switch (errstring)
1600 case RO_NULL:
1601 gcc_assert (c_dialect_cxx ());
1602 error_at (loc, "invalid type argument (have %qT)", type);
1603 break;
1604 case RO_ARRAY_INDEXING:
1605 error_at (loc,
1606 "invalid type argument of array indexing (have %qT)",
1607 type);
1608 break;
1609 case RO_UNARY_STAR:
1610 error_at (loc,
1611 "invalid type argument of unary %<*%> (have %qT)",
1612 type);
1613 break;
1614 case RO_ARROW:
1615 error_at (loc,
1616 "invalid type argument of %<->%> (have %qT)",
1617 type);
1618 break;
1619 case RO_ARROW_STAR:
1620 error_at (loc,
1621 "invalid type argument of %<->*%> (have %qT)",
1622 type);
1623 break;
1624 case RO_IMPLICIT_CONVERSION:
1625 error_at (loc,
1626 "invalid type argument of implicit conversion (have %qT)",
1627 type);
1628 break;
1629 default:
1630 gcc_unreachable ();
1634 /* Subscripting with type char is likely to lose on a machine where
1635 chars are signed. So warn on any machine, but optionally. Don't
1636 warn for unsigned char since that type is safe. Don't warn for
1637 signed char because anyone who uses that must have done so
1638 deliberately. Furthermore, we reduce the false positive load by
1639 warning only for non-constant value of type char. */
1641 void
1642 warn_array_subscript_with_type_char (location_t loc, tree index)
1644 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
1645 && TREE_CODE (index) != INTEGER_CST)
1646 warning_at (loc, OPT_Wchar_subscripts,
1647 "array subscript has type %<char%>");
1650 /* Implement -Wparentheses for the unexpected C precedence rules, to
1651 cover cases like x + y << z which readers are likely to
1652 misinterpret. We have seen an expression in which CODE is a binary
1653 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
1654 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
1655 CODE_RIGHT may be ERROR_MARK, which means that that side of the
1656 expression was not formed using a binary or unary operator, or it
1657 was enclosed in parentheses. */
1659 void
1660 warn_about_parentheses (location_t loc, enum tree_code code,
1661 enum tree_code code_left, tree arg_left,
1662 enum tree_code code_right, tree arg_right)
1664 if (!warn_parentheses)
1665 return;
1667 /* This macro tests that the expression ARG with original tree code
1668 CODE appears to be a boolean expression. or the result of folding a
1669 boolean expression. */
1670 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
1671 (truth_value_p (TREE_CODE (ARG)) \
1672 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
1673 /* Folding may create 0 or 1 integers from other expressions. */ \
1674 || ((CODE) != INTEGER_CST \
1675 && (integer_onep (ARG) || integer_zerop (ARG))))
1677 switch (code)
1679 case LSHIFT_EXPR:
1680 if (code_left == PLUS_EXPR)
1681 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1682 "suggest parentheses around %<+%> inside %<<<%>");
1683 else if (code_right == PLUS_EXPR)
1684 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1685 "suggest parentheses around %<+%> inside %<<<%>");
1686 else if (code_left == MINUS_EXPR)
1687 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1688 "suggest parentheses around %<-%> inside %<<<%>");
1689 else if (code_right == MINUS_EXPR)
1690 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1691 "suggest parentheses around %<-%> inside %<<<%>");
1692 return;
1694 case RSHIFT_EXPR:
1695 if (code_left == PLUS_EXPR)
1696 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1697 "suggest parentheses around %<+%> inside %<>>%>");
1698 else if (code_right == PLUS_EXPR)
1699 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1700 "suggest parentheses around %<+%> inside %<>>%>");
1701 else if (code_left == MINUS_EXPR)
1702 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1703 "suggest parentheses around %<-%> inside %<>>%>");
1704 else if (code_right == MINUS_EXPR)
1705 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1706 "suggest parentheses around %<-%> inside %<>>%>");
1707 return;
1709 case TRUTH_ORIF_EXPR:
1710 if (code_left == TRUTH_ANDIF_EXPR)
1711 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1712 "suggest parentheses around %<&&%> within %<||%>");
1713 else if (code_right == TRUTH_ANDIF_EXPR)
1714 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1715 "suggest parentheses around %<&&%> within %<||%>");
1716 return;
1718 case BIT_IOR_EXPR:
1719 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
1720 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
1721 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1722 "suggest parentheses around arithmetic in operand of %<|%>");
1723 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
1724 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
1725 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1726 "suggest parentheses around arithmetic in operand of %<|%>");
1727 /* Check cases like x|y==z */
1728 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
1729 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1730 "suggest parentheses around comparison in operand of %<|%>");
1731 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
1732 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1733 "suggest parentheses around comparison in operand of %<|%>");
1734 /* Check cases like !x | y */
1735 else if (code_left == TRUTH_NOT_EXPR
1736 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
1737 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1738 "suggest parentheses around operand of "
1739 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
1740 return;
1742 case BIT_XOR_EXPR:
1743 if (code_left == BIT_AND_EXPR
1744 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
1745 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1746 "suggest parentheses around arithmetic in operand of %<^%>");
1747 else if (code_right == BIT_AND_EXPR
1748 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
1749 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1750 "suggest parentheses around arithmetic in operand of %<^%>");
1751 /* Check cases like x^y==z */
1752 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
1753 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1754 "suggest parentheses around comparison in operand of %<^%>");
1755 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
1756 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1757 "suggest parentheses around comparison in operand of %<^%>");
1758 return;
1760 case BIT_AND_EXPR:
1761 if (code_left == PLUS_EXPR)
1762 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1763 "suggest parentheses around %<+%> in operand of %<&%>");
1764 else if (code_right == PLUS_EXPR)
1765 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1766 "suggest parentheses around %<+%> in operand of %<&%>");
1767 else if (code_left == MINUS_EXPR)
1768 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1769 "suggest parentheses around %<-%> in operand of %<&%>");
1770 else if (code_right == MINUS_EXPR)
1771 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1772 "suggest parentheses around %<-%> in operand of %<&%>");
1773 /* Check cases like x&y==z */
1774 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
1775 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1776 "suggest parentheses around comparison in operand of %<&%>");
1777 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
1778 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1779 "suggest parentheses around comparison in operand of %<&%>");
1780 /* Check cases like !x & y */
1781 else if (code_left == TRUTH_NOT_EXPR
1782 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
1783 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1784 "suggest parentheses around operand of "
1785 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
1786 return;
1788 case EQ_EXPR:
1789 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
1790 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1791 "suggest parentheses around comparison in operand of %<==%>");
1792 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
1793 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1794 "suggest parentheses around comparison in operand of %<==%>");
1795 return;
1796 case NE_EXPR:
1797 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
1798 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1799 "suggest parentheses around comparison in operand of %<!=%>");
1800 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
1801 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1802 "suggest parentheses around comparison in operand of %<!=%>");
1803 return;
1805 default:
1806 if (TREE_CODE_CLASS (code) == tcc_comparison)
1808 if (TREE_CODE_CLASS (code_left) == tcc_comparison
1809 && code_left != NE_EXPR && code_left != EQ_EXPR
1810 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
1811 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1812 "comparisons like %<X<=Y<=Z%> do not "
1813 "have their mathematical meaning");
1814 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
1815 && code_right != NE_EXPR && code_right != EQ_EXPR
1816 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
1817 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1818 "comparisons like %<X<=Y<=Z%> do not "
1819 "have their mathematical meaning");
1821 return;
1823 #undef NOT_A_BOOLEAN_EXPR_P
1826 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
1828 void
1829 warn_for_unused_label (tree label)
1831 if (!TREE_USED (label))
1833 if (DECL_INITIAL (label))
1834 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
1835 else
1836 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
1838 else if (asan_sanitize_use_after_scope ())
1840 if (asan_used_labels == NULL)
1841 asan_used_labels = new hash_set<tree> (16);
1843 asan_used_labels->add (label);
1847 /* Warn for division by zero according to the value of DIVISOR. LOC
1848 is the location of the division operator. */
1850 void
1851 warn_for_div_by_zero (location_t loc, tree divisor)
1853 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
1854 about division by zero. Do not issue a warning if DIVISOR has a
1855 floating-point type, since we consider 0.0/0.0 a valid way of
1856 generating a NaN. */
1857 if (c_inhibit_evaluation_warnings == 0
1858 && (integer_zerop (divisor) || fixed_zerop (divisor)))
1859 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
1862 /* Warn for patterns where memset appears to be used incorrectly. The
1863 warning location should be LOC. ARG0, and ARG2 are the first and
1864 last arguments to the call, while LITERAL_ZERO_MASK has a 1 bit for
1865 each argument that was a literal zero. */
1867 void
1868 warn_for_memset (location_t loc, tree arg0, tree arg2,
1869 int literal_zero_mask)
1871 if (warn_memset_transposed_args
1872 && integer_zerop (arg2)
1873 && (literal_zero_mask & (1 << 2)) != 0
1874 && (literal_zero_mask & (1 << 1)) == 0)
1875 warning_at (loc, OPT_Wmemset_transposed_args,
1876 "%<memset%> used with constant zero length "
1877 "parameter; this could be due to transposed "
1878 "parameters");
1880 if (warn_memset_elt_size && TREE_CODE (arg2) == INTEGER_CST)
1882 STRIP_NOPS (arg0);
1883 if (TREE_CODE (arg0) == ADDR_EXPR)
1884 arg0 = TREE_OPERAND (arg0, 0);
1885 tree type = TREE_TYPE (arg0);
1886 if (type != NULL_TREE && TREE_CODE (type) == ARRAY_TYPE)
1888 tree elt_type = TREE_TYPE (type);
1889 tree domain = TYPE_DOMAIN (type);
1890 if (!integer_onep (TYPE_SIZE_UNIT (elt_type))
1891 && domain != NULL_TREE
1892 && TYPE_MAX_VALUE (domain)
1893 && TYPE_MIN_VALUE (domain)
1894 && integer_zerop (TYPE_MIN_VALUE (domain))
1895 && integer_onep (fold_build2 (MINUS_EXPR, domain,
1896 arg2,
1897 TYPE_MAX_VALUE (domain))))
1898 warning_at (loc, OPT_Wmemset_elt_size,
1899 "%<memset%> used with length equal to "
1900 "number of elements without multiplication "
1901 "by element size");
1906 /* Subroutine of build_binary_op. Give warnings for comparisons
1907 between signed and unsigned quantities that may fail. Do the
1908 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
1909 so that casts will be considered, but default promotions won't
1912 LOCATION is the location of the comparison operator.
1914 The arguments of this function map directly to local variables
1915 of build_binary_op. */
1917 void
1918 warn_for_sign_compare (location_t location,
1919 tree orig_op0, tree orig_op1,
1920 tree op0, tree op1,
1921 tree result_type, enum tree_code resultcode)
1923 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
1924 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
1925 int unsignedp0, unsignedp1;
1927 /* In C++, check for comparison of different enum types. */
1928 if (c_dialect_cxx()
1929 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
1930 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
1931 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
1932 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
1934 warning_at (location,
1935 OPT_Wsign_compare, "comparison between types %qT and %qT",
1936 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
1939 /* Do not warn if the comparison is being done in a signed type,
1940 since the signed type will only be chosen if it can represent
1941 all the values of the unsigned type. */
1942 if (!TYPE_UNSIGNED (result_type))
1943 /* OK */;
1944 /* Do not warn if both operands are unsigned. */
1945 else if (op0_signed == op1_signed)
1946 /* OK */;
1947 else
1949 tree sop, uop, base_type;
1950 bool ovf;
1952 if (op0_signed)
1953 sop = orig_op0, uop = orig_op1;
1954 else
1955 sop = orig_op1, uop = orig_op0;
1957 STRIP_TYPE_NOPS (sop);
1958 STRIP_TYPE_NOPS (uop);
1959 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
1960 ? TREE_TYPE (result_type) : result_type);
1962 /* Do not warn if the signed quantity is an unsuffixed integer
1963 literal (or some static constant expression involving such
1964 literals or a conditional expression involving such literals)
1965 and it is non-negative. */
1966 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
1967 /* OK */;
1968 /* Do not warn if the comparison is an equality operation, the
1969 unsigned quantity is an integral constant, and it would fit
1970 in the result if the result were signed. */
1971 else if (TREE_CODE (uop) == INTEGER_CST
1972 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
1973 && int_fits_type_p (uop, c_common_signed_type (base_type)))
1974 /* OK */;
1975 /* In C, do not warn if the unsigned quantity is an enumeration
1976 constant and its maximum value would fit in the result if the
1977 result were signed. */
1978 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
1979 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
1980 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
1981 c_common_signed_type (base_type)))
1982 /* OK */;
1983 else
1984 warning_at (location, OPT_Wsign_compare,
1985 "comparison of integer expressions of different "
1986 "signedness: %qT and %qT", TREE_TYPE (orig_op0),
1987 TREE_TYPE (orig_op1));
1990 /* Warn if two unsigned values are being compared in a size larger
1991 than their original size, and one (and only one) is the result of
1992 a `~' operator. This comparison will always fail.
1994 Also warn if one operand is a constant, and the constant does not
1995 have all bits set that are set in the ~ operand when it is
1996 extended. */
1998 op0 = c_common_get_narrower (op0, &unsignedp0);
1999 op1 = c_common_get_narrower (op1, &unsignedp1);
2001 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
2002 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
2004 if (TREE_CODE (op0) == BIT_NOT_EXPR)
2005 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
2006 if (TREE_CODE (op1) == BIT_NOT_EXPR)
2007 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
2009 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
2011 tree primop;
2012 HOST_WIDE_INT constant, mask;
2013 int unsignedp;
2014 unsigned int bits;
2016 if (tree_fits_shwi_p (op0))
2018 primop = op1;
2019 unsignedp = unsignedp1;
2020 constant = tree_to_shwi (op0);
2022 else
2024 primop = op0;
2025 unsignedp = unsignedp0;
2026 constant = tree_to_shwi (op1);
2029 bits = TYPE_PRECISION (TREE_TYPE (primop));
2030 if (bits < TYPE_PRECISION (result_type)
2031 && bits < HOST_BITS_PER_LONG && unsignedp)
2033 mask = HOST_WIDE_INT_M1U << bits;
2034 if ((mask & constant) != mask)
2036 if (constant == 0)
2037 warning_at (location, OPT_Wsign_compare,
2038 "promoted ~unsigned is always non-zero");
2039 else
2040 warning_at (location, OPT_Wsign_compare,
2041 "comparison of promoted ~unsigned with constant");
2045 else if (unsignedp0 && unsignedp1
2046 && (TYPE_PRECISION (TREE_TYPE (op0))
2047 < TYPE_PRECISION (result_type))
2048 && (TYPE_PRECISION (TREE_TYPE (op1))
2049 < TYPE_PRECISION (result_type)))
2050 warning_at (location, OPT_Wsign_compare,
2051 "comparison of promoted ~unsigned with unsigned");
2055 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
2056 type via c_common_type. If -Wdouble-promotion is in use, and the
2057 conditions for warning have been met, issue a warning. GMSGID is
2058 the warning message. It must have two %T specifiers for the type
2059 that was converted (generally "float") and the type to which it was
2060 converted (generally "double), respectively. LOC is the location
2061 to which the warning should refer. */
2063 void
2064 do_warn_double_promotion (tree result_type, tree type1, tree type2,
2065 const char *gmsgid, location_t loc)
2067 tree source_type;
2069 if (!warn_double_promotion)
2070 return;
2071 /* If the conversion will not occur at run-time, there is no need to
2072 warn about it. */
2073 if (c_inhibit_evaluation_warnings)
2074 return;
2075 /* If an invalid conversion has occured, don't warn. */
2076 if (result_type == error_mark_node)
2077 return;
2078 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
2079 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
2080 return;
2081 if (TYPE_MAIN_VARIANT (type1) == float_type_node
2082 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
2083 source_type = type1;
2084 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
2085 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
2086 source_type = type2;
2087 else
2088 return;
2089 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
2092 /* Possibly warn about unused parameters. */
2094 void
2095 do_warn_unused_parameter (tree fn)
2097 tree decl;
2099 for (decl = DECL_ARGUMENTS (fn);
2100 decl; decl = DECL_CHAIN (decl))
2101 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
2102 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
2103 && !TREE_NO_WARNING (decl))
2104 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter,
2105 "unused parameter %qD", decl);
2108 /* If DECL is a typedef that is declared in the current function,
2109 record it for the purpose of -Wunused-local-typedefs. */
2111 void
2112 record_locally_defined_typedef (tree decl)
2114 struct c_language_function *l;
2116 if (!warn_unused_local_typedefs
2117 || cfun == NULL
2118 /* if this is not a locally defined typedef then we are not
2119 interested. */
2120 || !is_typedef_decl (decl)
2121 || !decl_function_context (decl))
2122 return;
2124 l = (struct c_language_function *) cfun->language;
2125 vec_safe_push (l->local_typedefs, decl);
2128 /* If T is a TYPE_DECL declared locally, mark it as used. */
2130 void
2131 maybe_record_typedef_use (tree t)
2133 if (!is_typedef_decl (t))
2134 return;
2136 TREE_USED (t) = true;
2139 /* Warn if there are some unused locally defined typedefs in the
2140 current function. */
2142 void
2143 maybe_warn_unused_local_typedefs (void)
2145 int i;
2146 tree decl;
2147 /* The number of times we have emitted -Wunused-local-typedefs
2148 warnings. If this is different from errorcount, that means some
2149 unrelated errors have been issued. In which case, we'll avoid
2150 emitting "unused-local-typedefs" warnings. */
2151 static int unused_local_typedefs_warn_count;
2152 struct c_language_function *l;
2154 if (cfun == NULL)
2155 return;
2157 if ((l = (struct c_language_function *) cfun->language) == NULL)
2158 return;
2160 if (warn_unused_local_typedefs
2161 && errorcount == unused_local_typedefs_warn_count)
2163 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
2164 if (!TREE_USED (decl))
2165 warning_at (DECL_SOURCE_LOCATION (decl),
2166 OPT_Wunused_local_typedefs,
2167 "typedef %qD locally defined but not used", decl);
2168 unused_local_typedefs_warn_count = errorcount;
2171 vec_free (l->local_typedefs);
2174 /* If we're creating an if-else-if condition chain, first see if we
2175 already have this COND in the CHAIN. If so, warn and don't add COND
2176 into the vector, otherwise add the COND there. LOC is the location
2177 of COND. */
2179 void
2180 warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain)
2182 /* No chain has been created yet. Do nothing. */
2183 if (*chain == NULL)
2184 return;
2186 if (TREE_SIDE_EFFECTS (cond))
2188 /* Uh-oh! This condition has a side-effect, thus invalidates
2189 the whole chain. */
2190 delete *chain;
2191 *chain = NULL;
2192 return;
2195 unsigned int ix;
2196 tree t;
2197 bool found = false;
2198 FOR_EACH_VEC_ELT (**chain, ix, t)
2199 if (operand_equal_p (cond, t, 0))
2201 if (warning_at (loc, OPT_Wduplicated_cond,
2202 "duplicated %<if%> condition"))
2203 inform (EXPR_LOCATION (t), "previously used here");
2204 found = true;
2205 break;
2208 if (!found
2209 && !CONSTANT_CLASS_P (cond)
2210 /* Don't infinitely grow the chain. */
2211 && (*chain)->length () < 512)
2212 (*chain)->safe_push (cond);
2215 /* Check and possibly warn if two declarations have contradictory
2216 attributes, such as always_inline vs. noinline. */
2218 bool
2219 diagnose_mismatched_attributes (tree olddecl, tree newdecl)
2221 bool warned = false;
2223 tree a1 = lookup_attribute ("optimize", DECL_ATTRIBUTES (olddecl));
2224 tree a2 = lookup_attribute ("optimize", DECL_ATTRIBUTES (newdecl));
2225 /* An optimization attribute applied on a declaration after the
2226 definition is likely not what the user wanted. */
2227 if (a2 != NULL_TREE
2228 && DECL_SAVED_TREE (olddecl) != NULL_TREE
2229 && (a1 == NULL_TREE || !attribute_list_equal (a1, a2)))
2230 warned |= warning (OPT_Wattributes,
2231 "optimization attribute on %qD follows "
2232 "definition but the attribute doesn%'t match",
2233 newdecl);
2235 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2236 const char *noinline = "noinline";
2238 if (DECL_DECLARED_INLINE_P (newdecl)
2239 && DECL_UNINLINABLE (olddecl)
2240 && lookup_attribute (noinline, DECL_ATTRIBUTES (olddecl)))
2241 warned |= warning (OPT_Wattributes, "inline declaration of %qD follows "
2242 "declaration with attribute %qs", newdecl, noinline);
2243 else if (DECL_DECLARED_INLINE_P (olddecl)
2244 && DECL_UNINLINABLE (newdecl)
2245 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2246 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
2247 "%qs follows inline declaration ", newdecl, noinline);
2249 return warned;
2252 /* Warn if signed left shift overflows. We don't warn
2253 about left-shifting 1 into the sign bit in C++14; cf.
2254 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
2255 LOC is a location of the shift; OP0 and OP1 are the operands.
2256 Return true if an overflow is detected, false otherwise. */
2258 bool
2259 maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
2261 if (TREE_CODE (op0) != INTEGER_CST
2262 || TREE_CODE (op1) != INTEGER_CST)
2263 return false;
2265 tree type0 = TREE_TYPE (op0);
2266 unsigned int prec0 = TYPE_PRECISION (type0);
2268 /* Left-hand operand must be signed. */
2269 if (TYPE_UNSIGNED (type0))
2270 return false;
2272 unsigned int min_prec = (wi::min_precision (wi::to_wide (op0), SIGNED)
2273 + TREE_INT_CST_LOW (op1));
2274 /* Handle the case of left-shifting 1 into the sign bit.
2275 * However, shifting 1 _out_ of the sign bit, as in
2276 * INT_MIN << 1, is considered an overflow.
2278 if (!tree_int_cst_sign_bit(op0) && min_prec == prec0 + 1)
2280 /* Never warn for C++14 onwards. */
2281 if (cxx_dialect >= cxx14)
2282 return false;
2283 /* Otherwise only if -Wshift-overflow=2. But return
2284 true to signal an overflow for the sake of integer
2285 constant expressions. */
2286 if (warn_shift_overflow < 2)
2287 return true;
2290 bool overflowed = min_prec > prec0;
2291 if (overflowed && c_inhibit_evaluation_warnings == 0)
2292 warning_at (loc, OPT_Wshift_overflow_,
2293 "result of %qE requires %u bits to represent, "
2294 "but %qT only has %u bits",
2295 build2_loc (loc, LSHIFT_EXPR, type0, op0, op1),
2296 min_prec, type0, prec0);
2298 return overflowed;
2301 /* Warn about boolean expression compared with an integer value different
2302 from true/false. Warns also e.g. about "(i1 == i2) == 2".
2303 LOC is the location of the comparison, CODE is its code, OP0 and OP1
2304 are the operands of the comparison. The caller must ensure that
2305 either operand is a boolean expression. */
2307 void
2308 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
2309 tree op1)
2311 if (TREE_CODE_CLASS (code) != tcc_comparison)
2312 return;
2314 tree f, cst;
2315 if (f = fold_for_warn (op0),
2316 TREE_CODE (f) == INTEGER_CST)
2317 cst = op0 = f;
2318 else if (f = fold_for_warn (op1),
2319 TREE_CODE (f) == INTEGER_CST)
2320 cst = op1 = f;
2321 else
2322 return;
2324 if (!integer_zerop (cst) && !integer_onep (cst))
2326 int sign = (TREE_CODE (op0) == INTEGER_CST
2327 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
2328 if (code == EQ_EXPR
2329 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
2330 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
2331 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
2332 "with boolean expression is always false", cst);
2333 else
2334 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
2335 "with boolean expression is always true", cst);
2337 else if (integer_zerop (cst) || integer_onep (cst))
2339 /* If the non-constant operand isn't of a boolean type, we
2340 don't want to warn here. */
2341 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
2342 /* Handle booleans promoted to integers. */
2343 if (bool_promoted_to_int_p (noncst))
2344 /* Warn. */;
2345 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
2346 && !truth_value_p (TREE_CODE (noncst)))
2347 return;
2348 /* Do some magic to get the right diagnostics. */
2349 bool flag = TREE_CODE (op0) == INTEGER_CST;
2350 flag = integer_zerop (cst) ? flag : !flag;
2351 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
2352 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
2353 "with boolean expression is always true", cst);
2354 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
2355 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
2356 "with boolean expression is always false", cst);
2360 /* Warn if an argument at position param_pos is passed to a
2361 restrict-qualified param, and it aliases with another argument. */
2363 void
2364 warn_for_restrict (unsigned param_pos, tree *argarray, unsigned nargs)
2366 tree arg = argarray[param_pos];
2367 if (TREE_VISITED (arg) || integer_zerop (arg))
2368 return;
2370 location_t loc = EXPR_LOC_OR_LOC (arg, input_location);
2371 gcc_rich_location richloc (loc);
2373 unsigned i;
2374 auto_vec<int, 16> arg_positions;
2376 for (i = 0; i < nargs; i++)
2378 if (i == param_pos)
2379 continue;
2381 tree current_arg = argarray[i];
2382 if (operand_equal_p (arg, current_arg, 0))
2384 TREE_VISITED (current_arg) = 1;
2385 arg_positions.safe_push (i + 1);
2389 if (arg_positions.is_empty ())
2390 return;
2392 int pos;
2393 FOR_EACH_VEC_ELT (arg_positions, i, pos)
2395 arg = argarray[pos - 1];
2396 if (EXPR_HAS_LOCATION (arg))
2397 richloc.add_range (EXPR_LOCATION (arg), false);
2400 warning_n (&richloc, OPT_Wrestrict, arg_positions.length (),
2401 "passing argument %i to restrict-qualified parameter"
2402 " aliases with argument %Z",
2403 "passing argument %i to restrict-qualified parameter"
2404 " aliases with arguments %Z",
2405 param_pos + 1, arg_positions.address (),
2406 arg_positions.length ());
2409 /* Callback function to determine whether an expression TP or one of its
2410 subexpressions comes from macro expansion. Used to suppress bogus
2411 warnings. */
2413 static tree
2414 expr_from_macro_expansion_r (tree *tp, int *, void *)
2416 if (CAN_HAVE_LOCATION_P (*tp)
2417 && from_macro_expansion_at (EXPR_LOCATION (*tp)))
2418 return integer_zero_node;
2420 return NULL_TREE;
2423 /* Possibly warn when an if-else has identical branches. */
2425 static void
2426 do_warn_duplicated_branches (tree expr)
2428 tree thenb = COND_EXPR_THEN (expr);
2429 tree elseb = COND_EXPR_ELSE (expr);
2431 /* Don't bother if any of the branches is missing. */
2432 if (thenb == NULL_TREE || elseb == NULL_TREE)
2433 return;
2435 /* And don't warn for empty statements. */
2436 if (TREE_CODE (thenb) == NOP_EXPR
2437 && TREE_TYPE (thenb) == void_type_node
2438 && TREE_OPERAND (thenb, 0) == size_zero_node)
2439 return;
2441 /* ... or empty branches. */
2442 if (TREE_CODE (thenb) == STATEMENT_LIST
2443 && STATEMENT_LIST_HEAD (thenb) == NULL)
2444 return;
2446 /* Compute the hash of the then branch. */
2447 inchash::hash hstate0 (0);
2448 inchash::add_expr (thenb, hstate0);
2449 hashval_t h0 = hstate0.end ();
2451 /* Compute the hash of the else branch. */
2452 inchash::hash hstate1 (0);
2453 inchash::add_expr (elseb, hstate1);
2454 hashval_t h1 = hstate1.end ();
2456 /* Compare the hashes. */
2457 if (h0 == h1
2458 && operand_equal_p (thenb, elseb, OEP_LEXICOGRAPHIC)
2459 /* Don't warn if any of the branches or their subexpressions comes
2460 from a macro. */
2461 && !walk_tree_without_duplicates (&thenb, expr_from_macro_expansion_r,
2462 NULL)
2463 && !walk_tree_without_duplicates (&elseb, expr_from_macro_expansion_r,
2464 NULL))
2465 warning_at (EXPR_LOCATION (expr), OPT_Wduplicated_branches,
2466 "this condition has identical branches");
2469 /* Callback for c_genericize to implement -Wduplicated-branches. */
2471 tree
2472 do_warn_duplicated_branches_r (tree *tp, int *, void *)
2474 if (TREE_CODE (*tp) == COND_EXPR)
2475 do_warn_duplicated_branches (*tp);
2476 return NULL_TREE;
2479 /* Implementation of -Wmultistatement-macros. This warning warns about
2480 cases when a macro expands to multiple statements not wrapped in
2481 do {} while (0) or ({ }) and is used as a body of if/else/for/while
2482 conditionals. For example,
2484 #define DOIT x++; y++
2486 if (c)
2487 DOIT;
2489 will increment y unconditionally.
2491 BODY_LOC is the location of the first token in the body after labels
2492 have been parsed, NEXT_LOC is the location of the next token after the
2493 body of the conditional has been parsed, and GUARD_LOC is the location
2494 of the conditional. */
2496 void
2497 warn_for_multistatement_macros (location_t body_loc, location_t next_loc,
2498 location_t guard_loc, enum rid keyword)
2500 if (!warn_multistatement_macros)
2501 return;
2503 /* Ain't got time to waste. We only care about macros here. */
2504 if (!from_macro_expansion_at (body_loc)
2505 || !from_macro_expansion_at (next_loc))
2506 return;
2508 /* Let's skip macros defined in system headers. */
2509 if (in_system_header_at (body_loc)
2510 || in_system_header_at (next_loc))
2511 return;
2513 /* Find the actual tokens in the macro definition. BODY_LOC and
2514 NEXT_LOC have to come from the same spelling location, but they
2515 will resolve to different locations in the context of the macro
2516 definition. */
2517 location_t body_loc_exp
2518 = linemap_resolve_location (line_table, body_loc,
2519 LRK_MACRO_DEFINITION_LOCATION, NULL);
2520 location_t next_loc_exp
2521 = linemap_resolve_location (line_table, next_loc,
2522 LRK_MACRO_DEFINITION_LOCATION, NULL);
2523 location_t guard_loc_exp
2524 = linemap_resolve_location (line_table, guard_loc,
2525 LRK_MACRO_DEFINITION_LOCATION, NULL);
2527 /* These are some funky cases we don't want to warn about. */
2528 if (body_loc_exp == guard_loc_exp
2529 || next_loc_exp == guard_loc_exp
2530 || body_loc_exp == next_loc_exp)
2531 return;
2533 /* Find the macro maps for the macro expansions. */
2534 const line_map *body_map = linemap_lookup (line_table, body_loc);
2535 const line_map *next_map = linemap_lookup (line_table, next_loc);
2536 const line_map *guard_map = linemap_lookup (line_table, guard_loc);
2538 /* Now see if the following token (after the body) is coming from the
2539 same macro expansion. If it is, it might be a problem. */
2540 if (body_map != next_map)
2541 return;
2543 /* The conditional itself must not come from the same expansion, because
2544 we don't want to warn about
2545 #define IF if (x) x++; y++
2546 and similar. */
2547 if (guard_map == body_map)
2548 return;
2550 /* Handle the case where NEXT and BODY come from the same expansion while
2551 GUARD doesn't, yet we shouldn't warn. E.g.
2553 #define GUARD if (...)
2554 #define GUARD2 GUARD
2556 and in the definition of another macro:
2558 GUARD2
2559 foo ();
2560 return 1;
2562 while (linemap_macro_expansion_map_p (guard_map))
2564 const line_map_macro *mm = linemap_check_macro (guard_map);
2565 guard_loc_exp = MACRO_MAP_EXPANSION_POINT_LOCATION (mm);
2566 guard_map = linemap_lookup (line_table, guard_loc_exp);
2567 if (guard_map == body_map)
2568 return;
2571 if (warning_at (body_loc, OPT_Wmultistatement_macros,
2572 "macro expands to multiple statements"))
2573 inform (guard_loc, "some parts of macro expansion are not guarded by "
2574 "this %qs clause", guard_tinfo_to_string (keyword));