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