[testsuite]
[official-gcc.git] / gcc / c-family / c-warn.c
blob7d87c455ec0c7dacd6774b41c362a5bb6a37c6a5
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 arg0 = fold_for_warn (arg0);
1872 arg2 = fold_for_warn (arg2);
1874 if (warn_memset_transposed_args
1875 && integer_zerop (arg2)
1876 && (literal_zero_mask & (1 << 2)) != 0
1877 && (literal_zero_mask & (1 << 1)) == 0)
1878 warning_at (loc, OPT_Wmemset_transposed_args,
1879 "%<memset%> used with constant zero length "
1880 "parameter; this could be due to transposed "
1881 "parameters");
1883 if (warn_memset_elt_size && TREE_CODE (arg2) == INTEGER_CST)
1885 STRIP_NOPS (arg0);
1886 if (TREE_CODE (arg0) == ADDR_EXPR)
1887 arg0 = TREE_OPERAND (arg0, 0);
1888 tree type = TREE_TYPE (arg0);
1889 if (type != NULL_TREE && TREE_CODE (type) == ARRAY_TYPE)
1891 tree elt_type = TREE_TYPE (type);
1892 tree domain = TYPE_DOMAIN (type);
1893 if (!integer_onep (TYPE_SIZE_UNIT (elt_type))
1894 && domain != NULL_TREE
1895 && TYPE_MAX_VALUE (domain)
1896 && TYPE_MIN_VALUE (domain)
1897 && integer_zerop (TYPE_MIN_VALUE (domain))
1898 && integer_onep (fold_build2 (MINUS_EXPR, domain,
1899 arg2,
1900 TYPE_MAX_VALUE (domain))))
1901 warning_at (loc, OPT_Wmemset_elt_size,
1902 "%<memset%> used with length equal to "
1903 "number of elements without multiplication "
1904 "by element size");
1909 /* Subroutine of build_binary_op. Give warnings for comparisons
1910 between signed and unsigned quantities that may fail. Do the
1911 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
1912 so that casts will be considered, but default promotions won't
1915 LOCATION is the location of the comparison operator.
1917 The arguments of this function map directly to local variables
1918 of build_binary_op. */
1920 void
1921 warn_for_sign_compare (location_t location,
1922 tree orig_op0, tree orig_op1,
1923 tree op0, tree op1,
1924 tree result_type, enum tree_code resultcode)
1926 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
1927 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
1928 int unsignedp0, unsignedp1;
1930 /* In C++, check for comparison of different enum types. */
1931 if (c_dialect_cxx()
1932 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
1933 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
1934 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
1935 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
1937 warning_at (location,
1938 OPT_Wsign_compare, "comparison between types %qT and %qT",
1939 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
1942 /* Do not warn if the comparison is being done in a signed type,
1943 since the signed type will only be chosen if it can represent
1944 all the values of the unsigned type. */
1945 if (!TYPE_UNSIGNED (result_type))
1946 /* OK */;
1947 /* Do not warn if both operands are unsigned. */
1948 else if (op0_signed == op1_signed)
1949 /* OK */;
1950 else
1952 tree sop, uop, base_type;
1953 bool ovf;
1955 if (op0_signed)
1956 sop = orig_op0, uop = orig_op1;
1957 else
1958 sop = orig_op1, uop = orig_op0;
1960 STRIP_TYPE_NOPS (sop);
1961 STRIP_TYPE_NOPS (uop);
1962 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
1963 ? TREE_TYPE (result_type) : result_type);
1965 /* Do not warn if the signed quantity is an unsuffixed integer
1966 literal (or some static constant expression involving such
1967 literals or a conditional expression involving such literals)
1968 and it is non-negative. */
1969 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
1970 /* OK */;
1971 /* Do not warn if the comparison is an equality operation, the
1972 unsigned quantity is an integral constant, and it would fit
1973 in the result if the result were signed. */
1974 else if (TREE_CODE (uop) == INTEGER_CST
1975 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
1976 && int_fits_type_p (uop, c_common_signed_type (base_type)))
1977 /* OK */;
1978 /* In C, do not warn if the unsigned quantity is an enumeration
1979 constant and its maximum value would fit in the result if the
1980 result were signed. */
1981 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
1982 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
1983 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
1984 c_common_signed_type (base_type)))
1985 /* OK */;
1986 else
1987 warning_at (location, OPT_Wsign_compare,
1988 "comparison of integer expressions of different "
1989 "signedness: %qT and %qT", TREE_TYPE (orig_op0),
1990 TREE_TYPE (orig_op1));
1993 /* Warn if two unsigned values are being compared in a size larger
1994 than their original size, and one (and only one) is the result of
1995 a `~' operator. This comparison will always fail.
1997 Also warn if one operand is a constant, and the constant does not
1998 have all bits set that are set in the ~ operand when it is
1999 extended. */
2001 op0 = c_common_get_narrower (op0, &unsignedp0);
2002 op1 = c_common_get_narrower (op1, &unsignedp1);
2004 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
2005 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
2007 if (TREE_CODE (op0) == BIT_NOT_EXPR)
2008 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
2009 if (TREE_CODE (op1) == BIT_NOT_EXPR)
2010 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
2012 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
2014 tree primop;
2015 HOST_WIDE_INT constant, mask;
2016 int unsignedp;
2017 unsigned int bits;
2019 if (tree_fits_shwi_p (op0))
2021 primop = op1;
2022 unsignedp = unsignedp1;
2023 constant = tree_to_shwi (op0);
2025 else
2027 primop = op0;
2028 unsignedp = unsignedp0;
2029 constant = tree_to_shwi (op1);
2032 bits = TYPE_PRECISION (TREE_TYPE (primop));
2033 if (bits < TYPE_PRECISION (result_type)
2034 && bits < HOST_BITS_PER_LONG && unsignedp)
2036 mask = HOST_WIDE_INT_M1U << bits;
2037 if ((mask & constant) != mask)
2039 if (constant == 0)
2040 warning_at (location, OPT_Wsign_compare,
2041 "promoted ~unsigned is always non-zero");
2042 else
2043 warning_at (location, OPT_Wsign_compare,
2044 "comparison of promoted ~unsigned with constant");
2048 else if (unsignedp0 && unsignedp1
2049 && (TYPE_PRECISION (TREE_TYPE (op0))
2050 < TYPE_PRECISION (result_type))
2051 && (TYPE_PRECISION (TREE_TYPE (op1))
2052 < TYPE_PRECISION (result_type)))
2053 warning_at (location, OPT_Wsign_compare,
2054 "comparison of promoted ~unsigned with unsigned");
2058 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
2059 type via c_common_type. If -Wdouble-promotion is in use, and the
2060 conditions for warning have been met, issue a warning. GMSGID is
2061 the warning message. It must have two %T specifiers for the type
2062 that was converted (generally "float") and the type to which it was
2063 converted (generally "double), respectively. LOC is the location
2064 to which the warning should refer. */
2066 void
2067 do_warn_double_promotion (tree result_type, tree type1, tree type2,
2068 const char *gmsgid, location_t loc)
2070 tree source_type;
2072 if (!warn_double_promotion)
2073 return;
2074 /* If the conversion will not occur at run-time, there is no need to
2075 warn about it. */
2076 if (c_inhibit_evaluation_warnings)
2077 return;
2078 /* If an invalid conversion has occured, don't warn. */
2079 if (result_type == error_mark_node)
2080 return;
2081 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
2082 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
2083 return;
2084 if (TYPE_MAIN_VARIANT (type1) == float_type_node
2085 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
2086 source_type = type1;
2087 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
2088 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
2089 source_type = type2;
2090 else
2091 return;
2092 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
2095 /* Possibly warn about unused parameters. */
2097 void
2098 do_warn_unused_parameter (tree fn)
2100 tree decl;
2102 for (decl = DECL_ARGUMENTS (fn);
2103 decl; decl = DECL_CHAIN (decl))
2104 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
2105 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
2106 && !TREE_NO_WARNING (decl))
2107 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter,
2108 "unused parameter %qD", decl);
2111 /* If DECL is a typedef that is declared in the current function,
2112 record it for the purpose of -Wunused-local-typedefs. */
2114 void
2115 record_locally_defined_typedef (tree decl)
2117 struct c_language_function *l;
2119 if (!warn_unused_local_typedefs
2120 || cfun == NULL
2121 /* if this is not a locally defined typedef then we are not
2122 interested. */
2123 || !is_typedef_decl (decl)
2124 || !decl_function_context (decl))
2125 return;
2127 l = (struct c_language_function *) cfun->language;
2128 vec_safe_push (l->local_typedefs, decl);
2131 /* If T is a TYPE_DECL declared locally, mark it as used. */
2133 void
2134 maybe_record_typedef_use (tree t)
2136 if (!is_typedef_decl (t))
2137 return;
2139 TREE_USED (t) = true;
2142 /* Warn if there are some unused locally defined typedefs in the
2143 current function. */
2145 void
2146 maybe_warn_unused_local_typedefs (void)
2148 int i;
2149 tree decl;
2150 /* The number of times we have emitted -Wunused-local-typedefs
2151 warnings. If this is different from errorcount, that means some
2152 unrelated errors have been issued. In which case, we'll avoid
2153 emitting "unused-local-typedefs" warnings. */
2154 static int unused_local_typedefs_warn_count;
2155 struct c_language_function *l;
2157 if (cfun == NULL)
2158 return;
2160 if ((l = (struct c_language_function *) cfun->language) == NULL)
2161 return;
2163 if (warn_unused_local_typedefs
2164 && errorcount == unused_local_typedefs_warn_count)
2166 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
2167 if (!TREE_USED (decl))
2168 warning_at (DECL_SOURCE_LOCATION (decl),
2169 OPT_Wunused_local_typedefs,
2170 "typedef %qD locally defined but not used", decl);
2171 unused_local_typedefs_warn_count = errorcount;
2174 vec_free (l->local_typedefs);
2177 /* If we're creating an if-else-if condition chain, first see if we
2178 already have this COND in the CHAIN. If so, warn and don't add COND
2179 into the vector, otherwise add the COND there. LOC is the location
2180 of COND. */
2182 void
2183 warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain)
2185 /* No chain has been created yet. Do nothing. */
2186 if (*chain == NULL)
2187 return;
2189 if (TREE_SIDE_EFFECTS (cond))
2191 /* Uh-oh! This condition has a side-effect, thus invalidates
2192 the whole chain. */
2193 delete *chain;
2194 *chain = NULL;
2195 return;
2198 unsigned int ix;
2199 tree t;
2200 bool found = false;
2201 FOR_EACH_VEC_ELT (**chain, ix, t)
2202 if (operand_equal_p (cond, t, 0))
2204 if (warning_at (loc, OPT_Wduplicated_cond,
2205 "duplicated %<if%> condition"))
2206 inform (EXPR_LOCATION (t), "previously used here");
2207 found = true;
2208 break;
2211 if (!found
2212 && !CONSTANT_CLASS_P (cond)
2213 /* Don't infinitely grow the chain. */
2214 && (*chain)->length () < 512)
2215 (*chain)->safe_push (cond);
2218 /* Check and possibly warn if two declarations have contradictory
2219 attributes, such as always_inline vs. noinline. */
2221 bool
2222 diagnose_mismatched_attributes (tree olddecl, tree newdecl)
2224 bool warned = false;
2226 tree a1 = lookup_attribute ("optimize", DECL_ATTRIBUTES (olddecl));
2227 tree a2 = lookup_attribute ("optimize", DECL_ATTRIBUTES (newdecl));
2228 /* An optimization attribute applied on a declaration after the
2229 definition is likely not what the user wanted. */
2230 if (a2 != NULL_TREE
2231 && DECL_SAVED_TREE (olddecl) != NULL_TREE
2232 && (a1 == NULL_TREE || !attribute_list_equal (a1, a2)))
2233 warned |= warning (OPT_Wattributes,
2234 "optimization attribute on %qD follows "
2235 "definition but the attribute doesn%'t match",
2236 newdecl);
2238 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2239 const char *noinline = "noinline";
2241 if (DECL_DECLARED_INLINE_P (newdecl)
2242 && DECL_UNINLINABLE (olddecl)
2243 && lookup_attribute (noinline, DECL_ATTRIBUTES (olddecl)))
2244 warned |= warning (OPT_Wattributes, "inline declaration of %qD follows "
2245 "declaration with attribute %qs", newdecl, noinline);
2246 else if (DECL_DECLARED_INLINE_P (olddecl)
2247 && DECL_UNINLINABLE (newdecl)
2248 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2249 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
2250 "%qs follows inline declaration ", newdecl, noinline);
2252 return warned;
2255 /* Warn if signed left shift overflows. We don't warn
2256 about left-shifting 1 into the sign bit in C++14; cf.
2257 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
2258 LOC is a location of the shift; OP0 and OP1 are the operands.
2259 Return true if an overflow is detected, false otherwise. */
2261 bool
2262 maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
2264 if (TREE_CODE (op0) != INTEGER_CST
2265 || TREE_CODE (op1) != INTEGER_CST)
2266 return false;
2268 tree type0 = TREE_TYPE (op0);
2269 unsigned int prec0 = TYPE_PRECISION (type0);
2271 /* Left-hand operand must be signed. */
2272 if (TYPE_UNSIGNED (type0))
2273 return false;
2275 unsigned int min_prec = (wi::min_precision (wi::to_wide (op0), SIGNED)
2276 + TREE_INT_CST_LOW (op1));
2277 /* Handle the case of left-shifting 1 into the sign bit.
2278 * However, shifting 1 _out_ of the sign bit, as in
2279 * INT_MIN << 1, is considered an overflow.
2281 if (!tree_int_cst_sign_bit(op0) && min_prec == prec0 + 1)
2283 /* Never warn for C++14 onwards. */
2284 if (cxx_dialect >= cxx14)
2285 return false;
2286 /* Otherwise only if -Wshift-overflow=2. But return
2287 true to signal an overflow for the sake of integer
2288 constant expressions. */
2289 if (warn_shift_overflow < 2)
2290 return true;
2293 bool overflowed = min_prec > prec0;
2294 if (overflowed && c_inhibit_evaluation_warnings == 0)
2295 warning_at (loc, OPT_Wshift_overflow_,
2296 "result of %qE requires %u bits to represent, "
2297 "but %qT only has %u bits",
2298 build2_loc (loc, LSHIFT_EXPR, type0, op0, op1),
2299 min_prec, type0, prec0);
2301 return overflowed;
2304 /* Warn about boolean expression compared with an integer value different
2305 from true/false. Warns also e.g. about "(i1 == i2) == 2".
2306 LOC is the location of the comparison, CODE is its code, OP0 and OP1
2307 are the operands of the comparison. The caller must ensure that
2308 either operand is a boolean expression. */
2310 void
2311 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
2312 tree op1)
2314 if (TREE_CODE_CLASS (code) != tcc_comparison)
2315 return;
2317 tree f, cst;
2318 if (f = fold_for_warn (op0),
2319 TREE_CODE (f) == INTEGER_CST)
2320 cst = op0 = f;
2321 else if (f = fold_for_warn (op1),
2322 TREE_CODE (f) == INTEGER_CST)
2323 cst = op1 = f;
2324 else
2325 return;
2327 if (!integer_zerop (cst) && !integer_onep (cst))
2329 int sign = (TREE_CODE (op0) == INTEGER_CST
2330 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
2331 if (code == EQ_EXPR
2332 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
2333 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
2334 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
2335 "with boolean expression is always false", cst);
2336 else
2337 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
2338 "with boolean expression is always true", cst);
2340 else if (integer_zerop (cst) || integer_onep (cst))
2342 /* If the non-constant operand isn't of a boolean type, we
2343 don't want to warn here. */
2344 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
2345 /* Handle booleans promoted to integers. */
2346 if (bool_promoted_to_int_p (noncst))
2347 /* Warn. */;
2348 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
2349 && !truth_value_p (TREE_CODE (noncst)))
2350 return;
2351 /* Do some magic to get the right diagnostics. */
2352 bool flag = TREE_CODE (op0) == INTEGER_CST;
2353 flag = integer_zerop (cst) ? flag : !flag;
2354 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
2355 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
2356 "with boolean expression is always true", cst);
2357 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
2358 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
2359 "with boolean expression is always false", cst);
2363 /* Warn if an argument at position param_pos is passed to a
2364 restrict-qualified param, and it aliases with another argument. */
2366 void
2367 warn_for_restrict (unsigned param_pos, tree *argarray, unsigned nargs)
2369 tree arg = argarray[param_pos];
2370 if (TREE_VISITED (arg) || integer_zerop (arg))
2371 return;
2373 location_t loc = EXPR_LOC_OR_LOC (arg, input_location);
2374 gcc_rich_location richloc (loc);
2376 unsigned i;
2377 auto_vec<int, 16> arg_positions;
2379 for (i = 0; i < nargs; i++)
2381 if (i == param_pos)
2382 continue;
2384 tree current_arg = argarray[i];
2385 if (operand_equal_p (arg, current_arg, 0))
2387 TREE_VISITED (current_arg) = 1;
2388 arg_positions.safe_push (i + 1);
2392 if (arg_positions.is_empty ())
2393 return;
2395 int pos;
2396 FOR_EACH_VEC_ELT (arg_positions, i, pos)
2398 arg = argarray[pos - 1];
2399 if (EXPR_HAS_LOCATION (arg))
2400 richloc.add_range (EXPR_LOCATION (arg), false);
2403 warning_n (&richloc, OPT_Wrestrict, arg_positions.length (),
2404 "passing argument %i to restrict-qualified parameter"
2405 " aliases with argument %Z",
2406 "passing argument %i to restrict-qualified parameter"
2407 " aliases with arguments %Z",
2408 param_pos + 1, arg_positions.address (),
2409 arg_positions.length ());
2412 /* Callback function to determine whether an expression TP or one of its
2413 subexpressions comes from macro expansion. Used to suppress bogus
2414 warnings. */
2416 static tree
2417 expr_from_macro_expansion_r (tree *tp, int *, void *)
2419 if (CAN_HAVE_LOCATION_P (*tp)
2420 && from_macro_expansion_at (EXPR_LOCATION (*tp)))
2421 return integer_zero_node;
2423 return NULL_TREE;
2426 /* Possibly warn when an if-else has identical branches. */
2428 static void
2429 do_warn_duplicated_branches (tree expr)
2431 tree thenb = COND_EXPR_THEN (expr);
2432 tree elseb = COND_EXPR_ELSE (expr);
2434 /* Don't bother if any of the branches is missing. */
2435 if (thenb == NULL_TREE || elseb == NULL_TREE)
2436 return;
2438 /* And don't warn for empty statements. */
2439 if (TREE_CODE (thenb) == NOP_EXPR
2440 && TREE_TYPE (thenb) == void_type_node
2441 && TREE_OPERAND (thenb, 0) == size_zero_node)
2442 return;
2444 /* ... or empty branches. */
2445 if (TREE_CODE (thenb) == STATEMENT_LIST
2446 && STATEMENT_LIST_HEAD (thenb) == NULL)
2447 return;
2449 /* Compute the hash of the then branch. */
2450 inchash::hash hstate0 (0);
2451 inchash::add_expr (thenb, hstate0);
2452 hashval_t h0 = hstate0.end ();
2454 /* Compute the hash of the else branch. */
2455 inchash::hash hstate1 (0);
2456 inchash::add_expr (elseb, hstate1);
2457 hashval_t h1 = hstate1.end ();
2459 /* Compare the hashes. */
2460 if (h0 == h1
2461 && operand_equal_p (thenb, elseb, OEP_LEXICOGRAPHIC)
2462 /* Don't warn if any of the branches or their subexpressions comes
2463 from a macro. */
2464 && !walk_tree_without_duplicates (&thenb, expr_from_macro_expansion_r,
2465 NULL)
2466 && !walk_tree_without_duplicates (&elseb, expr_from_macro_expansion_r,
2467 NULL))
2468 warning_at (EXPR_LOCATION (expr), OPT_Wduplicated_branches,
2469 "this condition has identical branches");
2472 /* Callback for c_genericize to implement -Wduplicated-branches. */
2474 tree
2475 do_warn_duplicated_branches_r (tree *tp, int *, void *)
2477 if (TREE_CODE (*tp) == COND_EXPR)
2478 do_warn_duplicated_branches (*tp);
2479 return NULL_TREE;
2482 /* Implementation of -Wmultistatement-macros. This warning warns about
2483 cases when a macro expands to multiple statements not wrapped in
2484 do {} while (0) or ({ }) and is used as a body of if/else/for/while
2485 conditionals. For example,
2487 #define DOIT x++; y++
2489 if (c)
2490 DOIT;
2492 will increment y unconditionally.
2494 BODY_LOC is the location of the first token in the body after labels
2495 have been parsed, NEXT_LOC is the location of the next token after the
2496 body of the conditional has been parsed, and GUARD_LOC is the location
2497 of the conditional. */
2499 void
2500 warn_for_multistatement_macros (location_t body_loc, location_t next_loc,
2501 location_t guard_loc, enum rid keyword)
2503 if (!warn_multistatement_macros)
2504 return;
2506 /* Ain't got time to waste. We only care about macros here. */
2507 if (!from_macro_expansion_at (body_loc)
2508 || !from_macro_expansion_at (next_loc))
2509 return;
2511 /* Let's skip macros defined in system headers. */
2512 if (in_system_header_at (body_loc)
2513 || in_system_header_at (next_loc))
2514 return;
2516 /* Find the actual tokens in the macro definition. BODY_LOC and
2517 NEXT_LOC have to come from the same spelling location, but they
2518 will resolve to different locations in the context of the macro
2519 definition. */
2520 location_t body_loc_exp
2521 = linemap_resolve_location (line_table, body_loc,
2522 LRK_MACRO_DEFINITION_LOCATION, NULL);
2523 location_t next_loc_exp
2524 = linemap_resolve_location (line_table, next_loc,
2525 LRK_MACRO_DEFINITION_LOCATION, NULL);
2526 location_t guard_loc_exp
2527 = linemap_resolve_location (line_table, guard_loc,
2528 LRK_MACRO_DEFINITION_LOCATION, NULL);
2530 /* These are some funky cases we don't want to warn about. */
2531 if (body_loc_exp == guard_loc_exp
2532 || next_loc_exp == guard_loc_exp
2533 || body_loc_exp == next_loc_exp)
2534 return;
2536 /* Find the macro maps for the macro expansions. */
2537 const line_map *body_map = linemap_lookup (line_table, body_loc);
2538 const line_map *next_map = linemap_lookup (line_table, next_loc);
2539 const line_map *guard_map = linemap_lookup (line_table, guard_loc);
2541 /* Now see if the following token (after the body) is coming from the
2542 same macro expansion. If it is, it might be a problem. */
2543 if (body_map != next_map)
2544 return;
2546 /* The conditional itself must not come from the same expansion, because
2547 we don't want to warn about
2548 #define IF if (x) x++; y++
2549 and similar. */
2550 if (guard_map == body_map)
2551 return;
2553 /* Handle the case where NEXT and BODY come from the same expansion while
2554 GUARD doesn't, yet we shouldn't warn. E.g.
2556 #define GUARD if (...)
2557 #define GUARD2 GUARD
2559 and in the definition of another macro:
2561 GUARD2
2562 foo ();
2563 return 1;
2565 while (linemap_macro_expansion_map_p (guard_map))
2567 const line_map_macro *mm = linemap_check_macro (guard_map);
2568 guard_loc_exp = MACRO_MAP_EXPANSION_POINT_LOCATION (mm);
2569 guard_map = linemap_lookup (line_table, guard_loc_exp);
2570 if (guard_map == body_map)
2571 return;
2574 if (warning_at (body_loc, OPT_Wmultistatement_macros,
2575 "macro expands to multiple statements"))
2576 inform (guard_loc, "some parts of macro expansion are not guarded by "
2577 "this %qs clause", guard_tinfo_to_string (keyword));