]> gcc.gnu.org Git - gcc.git/blob - gcc/c-family/c-warn.c
Add CASE_ADDR_EXPR
[gcc.git] / gcc / c-family / c-warn.c
1 /* Diagnostic routines shared by all languages that are variants of C.
2 Copyright (C) 1992-2020 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
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"
37 #include "c-family/c-spellcheck.h"
38 #include "calls.h"
39 #include "stor-layout.h"
40
41 /* Print a warning if a constant expression had overflow in folding.
42 Invoke this function on every expression that the language
43 requires to be a constant expression.
44 Note the ANSI C standard says it is erroneous for a
45 constant expression to overflow. */
46
47 void
48 constant_expression_warning (tree value)
49 {
50 if (warn_overflow && pedantic
51 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
52 || TREE_CODE (value) == FIXED_CST
53 || TREE_CODE (value) == VECTOR_CST
54 || TREE_CODE (value) == COMPLEX_CST)
55 && TREE_OVERFLOW (value))
56 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
57 }
58
59 /* The same as above but print an unconditional error. */
60
61 void
62 constant_expression_error (tree value)
63 {
64 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
65 || TREE_CODE (value) == FIXED_CST
66 || TREE_CODE (value) == VECTOR_CST
67 || TREE_CODE (value) == COMPLEX_CST)
68 && TREE_OVERFLOW (value))
69 error ("overflow in constant expression");
70 }
71
72 /* Print a warning if an expression result VALUE had an overflow
73 in folding and its operands hadn't. EXPR, which may be null, is
74 the operand of the expression.
75
76 Invoke this function on every expression that
77 (1) appears in the source code, and
78 (2) is a constant expression that overflowed, and
79 (3) is not already checked by convert_and_check;
80 however, do not invoke this function on operands of explicit casts
81 or when the expression is the result of an operator and any operand
82 already overflowed. */
83
84 void
85 overflow_warning (location_t loc, tree value, tree expr)
86 {
87 if (c_inhibit_evaluation_warnings != 0)
88 return;
89
90 const char *warnfmt = NULL;
91
92 switch (TREE_CODE (value))
93 {
94 case INTEGER_CST:
95 warnfmt = (expr
96 ? G_("integer overflow in expression %qE of type %qT "
97 "results in %qE")
98 : G_("integer overflow in expression of type %qT "
99 "results in %qE"));
100 break;
101
102 case REAL_CST:
103 warnfmt = (expr
104 ? G_("floating point overflow in expression %qE "
105 "of type %qT results in %qE")
106 : G_("floating point overflow in expression of type %qT "
107 "results in %qE"));
108 break;
109
110 case FIXED_CST:
111 warnfmt = (expr
112 ? G_("fixed-point overflow in expression %qE of type %qT "
113 "results in %qE")
114 : G_("fixed-point overflow in expression of type %qT "
115 "results in %qE"));
116 break;
117
118 case VECTOR_CST:
119 warnfmt = (expr
120 ? G_("vector overflow in expression %qE of type %qT "
121 "results in %qE")
122 : G_("vector overflow in expression of type %qT "
123 "results in %qE"));
124 break;
125
126 case COMPLEX_CST:
127 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
128 warnfmt = (expr
129 ? G_("complex integer overflow in expression %qE "
130 "of type %qT results in %qE")
131 : G_("complex integer overflow in expression of type %qT "
132 "results in %qE"));
133 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
134 warnfmt = (expr
135 ? G_("complex floating point overflow in expression %qE "
136 "of type %qT results in %qE")
137 : G_("complex floating point overflow in expression "
138 "of type %qT results in %qE"));
139 else
140 return;
141 break;
142
143 default:
144 return;
145 }
146
147 bool warned;
148 if (expr)
149 warned = warning_at (loc, OPT_Woverflow, warnfmt, expr, TREE_TYPE (expr),
150 value);
151 else
152 warned = warning_at (loc, OPT_Woverflow, warnfmt, TREE_TYPE (value),
153 value);
154
155 if (warned)
156 TREE_NO_WARNING (value) = 1;
157 }
158
159 /* Helper function for walk_tree. Unwrap C_MAYBE_CONST_EXPRs in an expression
160 pointed to by TP. */
161
162 static tree
163 unwrap_c_maybe_const (tree *tp, int *walk_subtrees, void *)
164 {
165 if (TREE_CODE (*tp) == C_MAYBE_CONST_EXPR)
166 {
167 *tp = C_MAYBE_CONST_EXPR_EXPR (*tp);
168 /* C_MAYBE_CONST_EXPRs don't nest. */
169 *walk_subtrees = false;
170 }
171 return NULL_TREE;
172 }
173
174 /* Warn about uses of logical || / && operator in a context where it
175 is likely that the bitwise equivalent was intended by the
176 programmer. We have seen an expression in which CODE is a binary
177 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
178 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
179
180 void
181 warn_logical_operator (location_t location, enum tree_code code, tree type,
182 enum tree_code code_left, tree op_left,
183 enum tree_code ARG_UNUSED (code_right), tree op_right)
184 {
185 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
186 int in0_p, in1_p, in_p;
187 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
188 bool strict_overflow_p = false;
189
190 if (!warn_logical_op)
191 return;
192
193 if (code != TRUTH_ANDIF_EXPR
194 && code != TRUTH_AND_EXPR
195 && code != TRUTH_ORIF_EXPR
196 && code != TRUTH_OR_EXPR)
197 return;
198
199 /* We don't want to warn if either operand comes from a macro
200 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
201 see PR61534. */
202 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
203 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
204 return;
205
206 /* Warn if &&/|| are being used in a context where it is
207 likely that the bitwise equivalent was intended by the
208 programmer. That is, an expression such as op && MASK
209 where op should not be any boolean expression, nor a
210 constant, and mask seems to be a non-boolean integer constant. */
211 STRIP_ANY_LOCATION_WRAPPER (op_right);
212 if (TREE_CODE (op_right) == CONST_DECL)
213 /* An enumerator counts as a constant. */
214 op_right = DECL_INITIAL (op_right);
215 tree stripped_op_left = tree_strip_any_location_wrapper (op_left);
216 if (!truth_value_p (code_left)
217 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
218 && !CONSTANT_CLASS_P (stripped_op_left)
219 && TREE_CODE (stripped_op_left) != CONST_DECL
220 && !TREE_NO_WARNING (op_left)
221 && TREE_CODE (op_right) == INTEGER_CST
222 && !integer_zerop (op_right)
223 && !integer_onep (op_right))
224 {
225 bool warned;
226 if (or_op)
227 warned
228 = warning_at (location, OPT_Wlogical_op,
229 "logical %<or%> applied to non-boolean constant");
230 else
231 warned
232 = warning_at (location, OPT_Wlogical_op,
233 "logical %<and%> applied to non-boolean constant");
234 if (warned)
235 TREE_NO_WARNING (op_left) = true;
236 return;
237 }
238
239 /* We do not warn for constants because they are typical of macro
240 expansions that test for features. */
241 if (CONSTANT_CLASS_P (fold_for_warn (op_left))
242 || CONSTANT_CLASS_P (fold_for_warn (op_right)))
243 return;
244
245 /* This warning only makes sense with logical operands. */
246 if (!(truth_value_p (TREE_CODE (op_left))
247 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
248 || !(truth_value_p (TREE_CODE (op_right))
249 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
250 return;
251
252 /* The range computations only work with scalars. */
253 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
254 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
255 return;
256
257 /* We first test whether either side separately is trivially true
258 (with OR) or trivially false (with AND). If so, do not warn.
259 This is a common idiom for testing ranges of data types in
260 portable code. */
261 op_left = unshare_expr (op_left);
262 walk_tree_without_duplicates (&op_left, unwrap_c_maybe_const, NULL);
263 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
264 if (!lhs)
265 return;
266
267 /* If this is an OR operation, invert both sides; now, the result
268 should be always false to get a warning. */
269 if (or_op)
270 in0_p = !in0_p;
271
272 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
273 if (tem && integer_zerop (tem))
274 return;
275
276 op_right = unshare_expr (op_right);
277 walk_tree_without_duplicates (&op_right, unwrap_c_maybe_const, NULL);
278 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
279 if (!rhs)
280 return;
281
282 /* If this is an OR operation, invert both sides; now, the result
283 should be always false to get a warning. */
284 if (or_op)
285 in1_p = !in1_p;
286
287 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
288 if (tem && integer_zerop (tem))
289 return;
290
291 /* If both expressions have the same operand, if we can merge the
292 ranges, ... */
293 if (operand_equal_p (lhs, rhs, 0)
294 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
295 in1_p, low1, high1))
296 {
297 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
298 /* ... and if the range test is always false, then warn. */
299 if (tem && integer_zerop (tem))
300 {
301 if (or_op)
302 warning_at (location, OPT_Wlogical_op,
303 "logical %<or%> of collectively exhaustive tests is "
304 "always true");
305 else
306 warning_at (location, OPT_Wlogical_op,
307 "logical %<and%> of mutually exclusive tests is "
308 "always false");
309 }
310 /* Or warn if the operands have exactly the same range, e.g.
311 A > 0 && A > 0. */
312 else if (tree_int_cst_equal (low0, low1)
313 && tree_int_cst_equal (high0, high1))
314 {
315 if (or_op)
316 warning_at (location, OPT_Wlogical_op,
317 "logical %<or%> of equal expressions");
318 else
319 warning_at (location, OPT_Wlogical_op,
320 "logical %<and%> of equal expressions");
321 }
322 }
323 }
324
325 /* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
326 with constant indices. */
327
328 static tree
329 find_array_ref_with_const_idx_r (tree *expr_p, int *, void *)
330 {
331 tree expr = *expr_p;
332
333 if ((TREE_CODE (expr) == ARRAY_REF
334 || TREE_CODE (expr) == ARRAY_RANGE_REF)
335 && (TREE_CODE (fold_for_warn (TREE_OPERAND (expr, 1)))
336 == INTEGER_CST))
337 return integer_type_node;
338
339 return NULL_TREE;
340 }
341
342 /* Subroutine of warn_tautological_cmp. Warn about bitwise comparison
343 that always evaluate to true or false. LOC is the location of the
344 ==/!= comparison specified by CODE; LHS and RHS are the usual operands
345 of this comparison. */
346
347 static void
348 warn_tautological_bitwise_comparison (const op_location_t &loc, tree_code code,
349 tree lhs, tree rhs)
350 {
351 if (code != EQ_EXPR && code != NE_EXPR)
352 return;
353
354 /* Extract the operands from e.g. (x & 8) == 4. */
355 tree bitop;
356 tree cst;
357 tree stripped_lhs = tree_strip_any_location_wrapper (lhs);
358 tree stripped_rhs = tree_strip_any_location_wrapper (rhs);
359 if ((TREE_CODE (lhs) == BIT_AND_EXPR
360 || TREE_CODE (lhs) == BIT_IOR_EXPR)
361 && TREE_CODE (stripped_rhs) == INTEGER_CST)
362 bitop = lhs, cst = stripped_rhs;
363 else if ((TREE_CODE (rhs) == BIT_AND_EXPR
364 || TREE_CODE (rhs) == BIT_IOR_EXPR)
365 && TREE_CODE (stripped_lhs) == INTEGER_CST)
366 bitop = rhs, cst = stripped_lhs;
367 else
368 return;
369
370 tree bitopcst;
371 tree bitop_op0 = fold_for_warn (TREE_OPERAND (bitop, 0));
372 if (TREE_CODE (bitop_op0) == INTEGER_CST)
373 bitopcst = bitop_op0;
374 else {
375 tree bitop_op1 = fold_for_warn (TREE_OPERAND (bitop, 1));
376 if (TREE_CODE (bitop_op1) == INTEGER_CST)
377 bitopcst = bitop_op1;
378 else
379 return;
380 }
381
382 /* Note that the two operands are from before the usual integer
383 conversions, so their types might not be the same.
384 Use the larger of the two precisions and ignore bits outside
385 of that. */
386 int prec = MAX (TYPE_PRECISION (TREE_TYPE (cst)),
387 TYPE_PRECISION (TREE_TYPE (bitopcst)));
388
389 wide_int bitopcstw = wi::to_wide (bitopcst, prec);
390 wide_int cstw = wi::to_wide (cst, prec);
391
392 wide_int res;
393 if (TREE_CODE (bitop) == BIT_AND_EXPR)
394 res = bitopcstw & cstw;
395 else
396 res = bitopcstw | cstw;
397
398 /* For BIT_AND only warn if (CST2 & CST1) != CST1, and
399 for BIT_OR only if (CST2 | CST1) != CST1. */
400 if (res == cstw)
401 return;
402
403 binary_op_rich_location richloc (loc, lhs, rhs, false);
404 if (code == EQ_EXPR)
405 warning_at (&richloc, OPT_Wtautological_compare,
406 "bitwise comparison always evaluates to false");
407 else
408 warning_at (&richloc, OPT_Wtautological_compare,
409 "bitwise comparison always evaluates to true");
410 }
411
412 /* Given LOC from a macro expansion, return the map for the outermost
413 macro in the nest of expansions. */
414
415 static const line_map_macro *
416 get_outermost_macro_expansion (location_t loc)
417 {
418 gcc_assert (from_macro_expansion_at (loc));
419
420 const line_map *map = linemap_lookup (line_table, loc);
421 const line_map_macro *macro_map;
422 do
423 {
424 macro_map = linemap_check_macro (map);
425 loc = linemap_unwind_toward_expansion (line_table, loc, &map);
426 } while (linemap_macro_expansion_map_p (map));
427
428 return macro_map;
429 }
430
431 /* Given LOC_A and LOC_B from macro expansions, return true if
432 they are "spelled the same" i.e. if they are both directly from
433 expansion of the same non-function-like macro. */
434
435 static bool
436 spelled_the_same_p (location_t loc_a, location_t loc_b)
437 {
438 gcc_assert (from_macro_expansion_at (loc_a));
439 gcc_assert (from_macro_expansion_at (loc_b));
440
441 const line_map_macro *map_a = get_outermost_macro_expansion (loc_a);
442 const line_map_macro *map_b = get_outermost_macro_expansion (loc_b);
443
444 if (map_a->macro == map_b->macro)
445 if (!cpp_fun_like_macro_p (map_a->macro))
446 return true;
447
448 return false;
449 }
450
451 /* Warn if a self-comparison always evaluates to true or false. LOC
452 is the location of the comparison with code CODE, LHS and RHS are
453 operands of the comparison. */
454
455 void
456 warn_tautological_cmp (const op_location_t &loc, enum tree_code code,
457 tree lhs, tree rhs)
458 {
459 if (TREE_CODE_CLASS (code) != tcc_comparison)
460 return;
461
462 /* Don't warn for various macro expansions. */
463 if (from_macro_expansion_at (loc))
464 return;
465 bool lhs_in_macro = from_macro_expansion_at (EXPR_LOCATION (lhs));
466 bool rhs_in_macro = from_macro_expansion_at (EXPR_LOCATION (rhs));
467 if (lhs_in_macro || rhs_in_macro)
468 {
469 /* Don't warn if exactly one is from a macro. */
470 if (!(lhs_in_macro && rhs_in_macro))
471 return;
472
473 /* If both are in a macro, only warn if they're spelled the same. */
474 if (!spelled_the_same_p (EXPR_LOCATION (lhs), EXPR_LOCATION (rhs)))
475 return;
476 }
477
478 warn_tautological_bitwise_comparison (loc, code, lhs, rhs);
479
480 /* We do not warn for constants because they are typical of macro
481 expansions that test for features, sizeof, and similar. */
482 if (CONSTANT_CLASS_P (fold_for_warn (lhs))
483 || CONSTANT_CLASS_P (fold_for_warn (rhs)))
484 return;
485
486 /* Don't warn for e.g.
487 HOST_WIDE_INT n;
488 ...
489 if (n == (long) n) ...
490 */
491 if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR)
492 || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR))
493 return;
494
495 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
496 It could be a NaN, and NaN never compares equal to anything, even
497 itself. */
498 if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
499 return;
500
501 if (operand_equal_p (lhs, rhs, 0))
502 {
503 /* Don't warn about array references with constant indices;
504 these are likely to come from a macro. */
505 if (walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r,
506 NULL))
507 return;
508 const bool always_true = (code == EQ_EXPR || code == LE_EXPR
509 || code == GE_EXPR || code == UNLE_EXPR
510 || code == UNGE_EXPR || code == UNEQ_EXPR);
511 binary_op_rich_location richloc (loc, lhs, rhs, false);
512 if (always_true)
513 warning_at (&richloc, OPT_Wtautological_compare,
514 "self-comparison always evaluates to true");
515 else
516 warning_at (&richloc, OPT_Wtautological_compare,
517 "self-comparison always evaluates to false");
518 }
519 }
520
521 /* Return true iff EXPR only contains boolean operands, or comparisons. */
522
523 static bool
524 expr_has_boolean_operands_p (tree expr)
525 {
526 STRIP_NOPS (expr);
527
528 if (CONVERT_EXPR_P (expr))
529 return bool_promoted_to_int_p (expr);
530 else if (UNARY_CLASS_P (expr))
531 return expr_has_boolean_operands_p (TREE_OPERAND (expr, 0));
532 else if (BINARY_CLASS_P (expr))
533 return (expr_has_boolean_operands_p (TREE_OPERAND (expr, 0))
534 && expr_has_boolean_operands_p (TREE_OPERAND (expr, 1)));
535 else if (COMPARISON_CLASS_P (expr))
536 return true;
537 else
538 return false;
539 }
540
541 /* Warn about logical not used on the left hand side operand of a comparison.
542 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
543 Do not warn if RHS is of a boolean type, a logical operator, or
544 a comparison. */
545
546 void
547 warn_logical_not_parentheses (location_t location, enum tree_code code,
548 tree lhs, tree rhs)
549 {
550 if (TREE_CODE_CLASS (code) != tcc_comparison
551 || TREE_TYPE (rhs) == NULL_TREE
552 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE
553 || truth_value_p (TREE_CODE (rhs)))
554 return;
555
556 /* Don't warn for expression like !x == ~(bool1 | bool2). */
557 if (expr_has_boolean_operands_p (rhs))
558 return;
559
560 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
561 !(x == 0) or !(y != 0). */
562 if ((code == EQ_EXPR || code == NE_EXPR)
563 && integer_zerop (rhs))
564 return;
565
566 auto_diagnostic_group d;
567 if (warning_at (location, OPT_Wlogical_not_parentheses,
568 "logical not is only applied to the left hand side of "
569 "comparison")
570 && EXPR_HAS_LOCATION (lhs))
571 {
572 location_t lhs_loc = EXPR_LOCATION (lhs);
573 rich_location richloc (line_table, lhs_loc);
574 richloc.add_fixit_insert_before (lhs_loc, "(");
575 richloc.add_fixit_insert_after (lhs_loc, ")");
576 inform (&richloc, "add parentheses around left hand side "
577 "expression to silence this warning");
578 }
579 }
580
581 /* Warn if EXP contains any computations whose results are not used.
582 Return true if a warning is printed; false otherwise. LOCUS is the
583 (potential) location of the expression. */
584
585 bool
586 warn_if_unused_value (const_tree exp, location_t locus)
587 {
588 restart:
589 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
590 return false;
591
592 /* Don't warn about void constructs. This includes casting to void,
593 void function calls, and statement expressions with a final cast
594 to void. */
595 if (VOID_TYPE_P (TREE_TYPE (exp)))
596 return false;
597
598 if (EXPR_HAS_LOCATION (exp))
599 locus = EXPR_LOCATION (exp);
600
601 switch (TREE_CODE (exp))
602 {
603 case PREINCREMENT_EXPR:
604 case POSTINCREMENT_EXPR:
605 case PREDECREMENT_EXPR:
606 case POSTDECREMENT_EXPR:
607 case MODIFY_EXPR:
608 case INIT_EXPR:
609 case TARGET_EXPR:
610 case CALL_EXPR:
611 case TRY_CATCH_EXPR:
612 case EXIT_EXPR:
613 case VA_ARG_EXPR:
614 return false;
615
616 case BIND_EXPR:
617 /* For a binding, warn if no side effect within it. */
618 exp = BIND_EXPR_BODY (exp);
619 goto restart;
620
621 case SAVE_EXPR:
622 case NON_LVALUE_EXPR:
623 case NOP_EXPR:
624 exp = TREE_OPERAND (exp, 0);
625 goto restart;
626
627 case TRUTH_ORIF_EXPR:
628 case TRUTH_ANDIF_EXPR:
629 /* In && or ||, warn if 2nd operand has no side effect. */
630 exp = TREE_OPERAND (exp, 1);
631 goto restart;
632
633 case COMPOUND_EXPR:
634 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
635 return true;
636 /* Let people do `(foo (), 0)' without a warning. */
637 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
638 return false;
639 exp = TREE_OPERAND (exp, 1);
640 goto restart;
641
642 case COND_EXPR:
643 /* If this is an expression with side effects, don't warn; this
644 case commonly appears in macro expansions. */
645 if (TREE_SIDE_EFFECTS (exp))
646 return false;
647 goto warn;
648
649 case INDIRECT_REF:
650 /* Don't warn about automatic dereferencing of references, since
651 the user cannot control it. */
652 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
653 {
654 exp = TREE_OPERAND (exp, 0);
655 goto restart;
656 }
657 /* Fall through. */
658
659 default:
660 /* Referencing a volatile value is a side effect, so don't warn. */
661 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
662 && TREE_THIS_VOLATILE (exp))
663 return false;
664
665 /* If this is an expression which has no operands, there is no value
666 to be unused. There are no such language-independent codes,
667 but front ends may define such. */
668 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
669 return false;
670
671 warn:
672 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
673 }
674 }
675
676 /* Print a warning about casts that might indicate violation of strict
677 aliasing rules if -Wstrict-aliasing is used and strict aliasing
678 mode is in effect. LOC is the location of the expression being
679 cast, EXPR might be from inside it. TYPE is the type we're casting
680 to. */
681
682 bool
683 strict_aliasing_warning (location_t loc, tree type, tree expr)
684 {
685 if (loc == UNKNOWN_LOCATION)
686 loc = input_location;
687
688 /* Strip pointer conversion chains and get to the correct original type. */
689 STRIP_NOPS (expr);
690 tree otype = TREE_TYPE (expr);
691
692 if (!(flag_strict_aliasing
693 && POINTER_TYPE_P (type)
694 && POINTER_TYPE_P (otype)
695 && !VOID_TYPE_P (TREE_TYPE (type)))
696 /* If the type we are casting to is a ref-all pointer
697 dereferencing it is always valid. */
698 || TYPE_REF_CAN_ALIAS_ALL (type))
699 return false;
700
701 if ((warn_strict_aliasing > 1) && ADDR_EXPR_P (expr)
702 && (DECL_P (TREE_OPERAND (expr, 0))
703 || handled_component_p (TREE_OPERAND (expr, 0))))
704 {
705 /* Casting the address of an object to non void pointer. Warn
706 if the cast breaks type based aliasing. */
707 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
708 {
709 warning_at (loc, OPT_Wstrict_aliasing,
710 "type-punning to incomplete type "
711 "might break strict-aliasing rules");
712 return true;
713 }
714 else
715 {
716 /* warn_strict_aliasing >= 3. This includes the default (3).
717 Only warn if the cast is dereferenced immediately. */
718 alias_set_type set1
719 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
720 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
721
722 if (set2 != 0
723 && set1 != set2
724 && !alias_set_subset_of (set2, set1)
725 && !alias_sets_conflict_p (set1, set2))
726 {
727 warning_at (loc, OPT_Wstrict_aliasing,
728 "dereferencing type-punned "
729 "pointer will break strict-aliasing rules");
730 return true;
731 }
732 else if (warn_strict_aliasing == 2
733 && !alias_sets_must_conflict_p (set1, set2))
734 {
735 warning_at (loc, OPT_Wstrict_aliasing,
736 "dereferencing type-punned "
737 "pointer might break strict-aliasing rules");
738 return true;
739 }
740 }
741 }
742 else if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
743 {
744 /* At this level, warn for any conversions, even if an address is
745 not taken in the same statement. This will likely produce many
746 false positives, but could be useful to pinpoint problems that
747 are not revealed at higher levels. */
748 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
749 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
750 if (!COMPLETE_TYPE_P (TREE_TYPE (type))
751 || !alias_sets_must_conflict_p (set1, set2))
752 {
753 warning_at (loc, OPT_Wstrict_aliasing,
754 "dereferencing type-punned "
755 "pointer might break strict-aliasing rules");
756 return true;
757 }
758 }
759
760 return false;
761 }
762
763 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
764 sizeof as last operand of certain builtins. */
765
766 void
767 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
768 vec<tree, va_gc> *params, tree *sizeof_arg,
769 bool (*comp_types) (tree, tree))
770 {
771 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
772 bool strop = false, cmp = false;
773 unsigned int idx = ~0;
774 location_t loc;
775
776 if (TREE_CODE (callee) != FUNCTION_DECL
777 || !fndecl_built_in_p (callee, BUILT_IN_NORMAL)
778 || vec_safe_length (params) <= 1)
779 return;
780
781 enum built_in_function fncode = DECL_FUNCTION_CODE (callee);
782 switch (fncode)
783 {
784 case BUILT_IN_STRNCMP:
785 case BUILT_IN_STRNCASECMP:
786 cmp = true;
787 /* FALLTHRU */
788 case BUILT_IN_STRNCPY:
789 case BUILT_IN_STRNCPY_CHK:
790 case BUILT_IN_STRNCAT:
791 case BUILT_IN_STRNCAT_CHK:
792 case BUILT_IN_STPNCPY:
793 case BUILT_IN_STPNCPY_CHK:
794 strop = true;
795 /* FALLTHRU */
796 case BUILT_IN_MEMCPY:
797 case BUILT_IN_MEMCPY_CHK:
798 case BUILT_IN_MEMMOVE:
799 case BUILT_IN_MEMMOVE_CHK:
800 if (params->length () < 3)
801 return;
802 src = (*params)[1];
803 dest = (*params)[0];
804 idx = 2;
805 break;
806 case BUILT_IN_BCOPY:
807 if (params->length () < 3)
808 return;
809 src = (*params)[0];
810 dest = (*params)[1];
811 idx = 2;
812 break;
813 case BUILT_IN_MEMCMP:
814 case BUILT_IN_BCMP:
815 if (params->length () < 3)
816 return;
817 src = (*params)[1];
818 dest = (*params)[0];
819 idx = 2;
820 cmp = true;
821 break;
822 case BUILT_IN_MEMSET:
823 case BUILT_IN_MEMSET_CHK:
824 if (params->length () < 3)
825 return;
826 dest = (*params)[0];
827 idx = 2;
828 break;
829 case BUILT_IN_BZERO:
830 dest = (*params)[0];
831 idx = 1;
832 break;
833 case BUILT_IN_STRNDUP:
834 src = (*params)[0];
835 strop = true;
836 idx = 1;
837 break;
838 case BUILT_IN_MEMCHR:
839 if (params->length () < 3)
840 return;
841 src = (*params)[0];
842 idx = 2;
843 break;
844 case BUILT_IN_SNPRINTF:
845 case BUILT_IN_SNPRINTF_CHK:
846 case BUILT_IN_VSNPRINTF:
847 case BUILT_IN_VSNPRINTF_CHK:
848 dest = (*params)[0];
849 idx = 1;
850 strop = true;
851 break;
852 default:
853 break;
854 }
855
856 if (idx >= 3)
857 return;
858
859 /* Use error_operand_p to detect non-error arguments with an error
860 type that the C++ front-end constructs. */
861 if (error_operand_p (src)
862 || error_operand_p (dest)
863 || !sizeof_arg[idx]
864 || error_operand_p (sizeof_arg[idx]))
865 return;
866
867 type = TYPE_P (sizeof_arg[idx])
868 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
869
870 if (!POINTER_TYPE_P (type))
871 {
872 /* The argument type may be an array. Diagnose bounded string
873 copy functions that specify the bound in terms of the source
874 argument rather than the destination unless they are equal
875 to one another. Handle constant sizes and also try to handle
876 sizeof expressions involving VLAs. */
877 if (strop && !cmp && fncode != BUILT_IN_STRNDUP && src)
878 {
879 tem = tree_strip_nop_conversions (src);
880 if (ADDR_EXPR_P (tem))
881 tem = TREE_OPERAND (tem, 0);
882
883 /* Avoid diagnosing sizeof SRC when SRC is declared with
884 attribute nonstring. */
885 tree dummy;
886 if (get_attr_nonstring_decl (tem, &dummy))
887 return;
888
889 tree d = tree_strip_nop_conversions (dest);
890 if (ADDR_EXPR_P (d))
891 d = TREE_OPERAND (d, 0);
892
893 tree dstsz = TYPE_SIZE_UNIT (TREE_TYPE (d));
894 tree srcsz = TYPE_SIZE_UNIT (TREE_TYPE (tem));
895
896 if ((!dstsz
897 || !srcsz
898 || !operand_equal_p (dstsz, srcsz, OEP_LEXICOGRAPHIC))
899 && operand_equal_p (tem, sizeof_arg[idx], OEP_ADDRESS_OF))
900 warning_at (sizeof_arg_loc[idx], OPT_Wsizeof_pointer_memaccess,
901 "argument to %<sizeof%> in %qD call is the same "
902 "expression as the source; did you mean to use "
903 "the size of the destination?",
904 callee);
905 }
906
907 return;
908 }
909
910 if (dest
911 && (tem = tree_strip_nop_conversions (dest))
912 && POINTER_TYPE_P (TREE_TYPE (tem))
913 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
914 return;
915
916 if (src
917 && (tem = tree_strip_nop_conversions (src))
918 && POINTER_TYPE_P (TREE_TYPE (tem))
919 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
920 return;
921
922 loc = sizeof_arg_loc[idx];
923
924 if (dest && !cmp)
925 {
926 if (!TYPE_P (sizeof_arg[idx])
927 && operand_equal_p (dest, sizeof_arg[idx], 0)
928 && comp_types (TREE_TYPE (dest), type))
929 {
930 if (ADDR_EXPR_P (sizeof_arg[idx]) && !strop)
931 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
932 "argument to %<sizeof%> in %qD call is the same "
933 "expression as the destination; did you mean to "
934 "remove the addressof?", callee);
935 else if ((TYPE_PRECISION (TREE_TYPE (type))
936 == TYPE_PRECISION (char_type_node))
937 || strop)
938 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
939 "argument to %<sizeof%> in %qD call is the same "
940 "expression as the destination; did you mean to "
941 "provide an explicit length?", callee);
942 else
943 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
944 "argument to %<sizeof%> in %qD call is the same "
945 "expression as the destination; did you mean to "
946 "dereference it?", callee);
947 return;
948 }
949
950 if (POINTER_TYPE_P (TREE_TYPE (dest))
951 && !strop
952 && comp_types (TREE_TYPE (dest), type)
953 && !VOID_TYPE_P (TREE_TYPE (type)))
954 {
955 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
956 "argument to %<sizeof%> in %qD call is the same "
957 "pointer type %qT as the destination; expected %qT "
958 "or an explicit length", callee, TREE_TYPE (dest),
959 TREE_TYPE (TREE_TYPE (dest)));
960 return;
961 }
962 }
963
964 if (src && !cmp)
965 {
966 if (!TYPE_P (sizeof_arg[idx])
967 && operand_equal_p (src, sizeof_arg[idx], 0)
968 && comp_types (TREE_TYPE (src), type))
969 {
970 if (ADDR_EXPR_P (sizeof_arg[idx]) && !strop)
971 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
972 "argument to %<sizeof%> in %qD call is the same "
973 "expression as the source; did you mean to "
974 "remove the addressof?", callee);
975 else if ((TYPE_PRECISION (TREE_TYPE (type))
976 == TYPE_PRECISION (char_type_node))
977 || strop)
978 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
979 "argument to %<sizeof%> in %qD call is the same "
980 "expression as the source; did you mean to "
981 "provide an explicit length?", callee);
982 else
983 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
984 "argument to %<sizeof%> in %qD call is the same "
985 "expression as the source; did you mean to "
986 "dereference it?", callee);
987 return;
988 }
989
990 if (POINTER_TYPE_P (TREE_TYPE (src))
991 && !strop
992 && comp_types (TREE_TYPE (src), type)
993 && !VOID_TYPE_P (TREE_TYPE (type)))
994 {
995 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
996 "argument to %<sizeof%> in %qD call is the same "
997 "pointer type %qT as the source; expected %qT "
998 "or an explicit length", callee, TREE_TYPE (src),
999 TREE_TYPE (TREE_TYPE (src)));
1000 return;
1001 }
1002 }
1003
1004 if (dest)
1005 {
1006 if (!TYPE_P (sizeof_arg[idx])
1007 && operand_equal_p (dest, sizeof_arg[idx], 0)
1008 && comp_types (TREE_TYPE (dest), type))
1009 {
1010 if (ADDR_EXPR_P (sizeof_arg[idx]) && !strop)
1011 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1012 "argument to %<sizeof%> in %qD call is the same "
1013 "expression as the first source; did you mean to "
1014 "remove the addressof?", callee);
1015 else if ((TYPE_PRECISION (TREE_TYPE (type))
1016 == TYPE_PRECISION (char_type_node))
1017 || strop)
1018 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1019 "argument to %<sizeof%> in %qD call is the same "
1020 "expression as the first source; did you mean to "
1021 "provide an explicit length?", callee);
1022 else
1023 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1024 "argument to %<sizeof%> in %qD call is the same "
1025 "expression as the first source; did you mean to "
1026 "dereference it?", callee);
1027 return;
1028 }
1029
1030 if (POINTER_TYPE_P (TREE_TYPE (dest))
1031 && !strop
1032 && comp_types (TREE_TYPE (dest), type)
1033 && !VOID_TYPE_P (TREE_TYPE (type)))
1034 {
1035 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1036 "argument to %<sizeof%> in %qD call is the same "
1037 "pointer type %qT as the first source; expected %qT "
1038 "or an explicit length", callee, TREE_TYPE (dest),
1039 TREE_TYPE (TREE_TYPE (dest)));
1040 return;
1041 }
1042 }
1043
1044 if (src)
1045 {
1046 if (!TYPE_P (sizeof_arg[idx])
1047 && operand_equal_p (src, sizeof_arg[idx], 0)
1048 && comp_types (TREE_TYPE (src), type))
1049 {
1050 if (ADDR_EXPR_P (sizeof_arg[idx]) && !strop)
1051 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1052 "argument to %<sizeof%> in %qD call is the same "
1053 "expression as the second source; did you mean to "
1054 "remove the addressof?", callee);
1055 else if ((TYPE_PRECISION (TREE_TYPE (type))
1056 == TYPE_PRECISION (char_type_node))
1057 || strop)
1058 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1059 "argument to %<sizeof%> in %qD call is the same "
1060 "expression as the second source; did you mean to "
1061 "provide an explicit length?", callee);
1062 else
1063 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1064 "argument to %<sizeof%> in %qD call is the same "
1065 "expression as the second source; did you mean to "
1066 "dereference it?", callee);
1067 return;
1068 }
1069
1070 if (POINTER_TYPE_P (TREE_TYPE (src))
1071 && !strop
1072 && comp_types (TREE_TYPE (src), type)
1073 && !VOID_TYPE_P (TREE_TYPE (type)))
1074 {
1075 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1076 "argument to %<sizeof%> in %qD call is the same "
1077 "pointer type %qT as the second source; expected %qT "
1078 "or an explicit length", callee, TREE_TYPE (src),
1079 TREE_TYPE (TREE_TYPE (src)));
1080 return;
1081 }
1082 }
1083
1084 }
1085
1086 /* Warn for unlikely, improbable, or stupid DECL declarations
1087 of `main'. */
1088
1089 void
1090 check_main_parameter_types (tree decl)
1091 {
1092 function_args_iterator iter;
1093 tree type;
1094 int argct = 0;
1095
1096 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
1097 {
1098 /* XXX void_type_node belies the abstraction. */
1099 if (type == void_type_node || type == error_mark_node)
1100 break;
1101
1102 tree t = type;
1103 if (TYPE_ATOMIC (t))
1104 pedwarn (input_location, OPT_Wmain,
1105 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1106 type, decl);
1107 while (POINTER_TYPE_P (t))
1108 {
1109 t = TREE_TYPE (t);
1110 if (TYPE_ATOMIC (t))
1111 pedwarn (input_location, OPT_Wmain,
1112 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1113 type, decl);
1114 }
1115
1116 ++argct;
1117 switch (argct)
1118 {
1119 case 1:
1120 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
1121 pedwarn (input_location, OPT_Wmain,
1122 "first argument of %q+D should be %<int%>", decl);
1123 break;
1124
1125 case 2:
1126 if (TREE_CODE (type) != POINTER_TYPE
1127 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1128 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1129 != char_type_node))
1130 pedwarn (input_location, OPT_Wmain,
1131 "second argument of %q+D should be %<char **%>", decl);
1132 break;
1133
1134 case 3:
1135 if (TREE_CODE (type) != POINTER_TYPE
1136 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1137 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1138 != char_type_node))
1139 pedwarn (input_location, OPT_Wmain,
1140 "third argument of %q+D should probably be "
1141 "%<char **%>", decl);
1142 break;
1143 }
1144 }
1145
1146 /* It is intentional that this message does not mention the third
1147 argument because it's only mentioned in an appendix of the
1148 standard. */
1149 if (argct > 0 && (argct < 2 || argct > 3))
1150 pedwarn (input_location, OPT_Wmain,
1151 "%q+D takes only zero or two arguments", decl);
1152
1153 if (stdarg_p (TREE_TYPE (decl)))
1154 pedwarn (input_location, OPT_Wmain,
1155 "%q+D declared as variadic function", decl);
1156 }
1157
1158 /* Warns and returns true if the conversion of EXPR to TYPE may alter a value.
1159 This is a helper function for warnings_for_convert_and_check. */
1160
1161 static bool
1162 conversion_warning (location_t loc, tree type, tree expr, tree result)
1163 {
1164 tree expr_type = TREE_TYPE (expr);
1165 enum conversion_safety conversion_kind;
1166 int arith_ops = 0;
1167
1168 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
1169 return false;
1170
1171 /* This may happen, because for LHS op= RHS we preevaluate
1172 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
1173 means we could no longer see the code of the EXPR. */
1174 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
1175 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
1176 if (TREE_CODE (expr) == SAVE_EXPR)
1177 expr = TREE_OPERAND (expr, 0);
1178
1179 switch (TREE_CODE (expr))
1180 {
1181 case EQ_EXPR:
1182 case NE_EXPR:
1183 case LE_EXPR:
1184 case GE_EXPR:
1185 case LT_EXPR:
1186 case GT_EXPR:
1187 case TRUTH_ANDIF_EXPR:
1188 case TRUTH_ORIF_EXPR:
1189 case TRUTH_AND_EXPR:
1190 case TRUTH_OR_EXPR:
1191 case TRUTH_XOR_EXPR:
1192 case TRUTH_NOT_EXPR:
1193 /* Conversion from boolean to a signed:1 bit-field (which only
1194 can hold the values 0 and -1) doesn't lose information - but
1195 it does change the value. */
1196 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
1197 warning_at (loc, OPT_Wconversion,
1198 "conversion to %qT from boolean expression", type);
1199 return true;
1200
1201 case REAL_CST:
1202 case INTEGER_CST:
1203 case COMPLEX_CST:
1204 {
1205 conversion_kind = unsafe_conversion_p (type, expr, result, true);
1206 int warnopt;
1207 if (conversion_kind == UNSAFE_REAL)
1208 warnopt = OPT_Wfloat_conversion;
1209 else if (conversion_kind)
1210 warnopt = OPT_Wconversion;
1211 else
1212 break;
1213
1214 if (conversion_kind == UNSAFE_SIGN)
1215 {
1216 bool cstresult
1217 = (result
1218 && TREE_CODE_CLASS (TREE_CODE (result)) == tcc_constant);
1219 if (TYPE_UNSIGNED (type))
1220 {
1221 if (cstresult)
1222 warning_at (loc, OPT_Wsign_conversion,
1223 "unsigned conversion from %qT to %qT "
1224 "changes value from %qE to %qE",
1225 expr_type, type, expr, result);
1226 else
1227 warning_at (loc, OPT_Wsign_conversion,
1228 "unsigned conversion from %qT to %qT "
1229 "changes the value of %qE",
1230 expr_type, type, expr);
1231 }
1232 else
1233 {
1234 if (cstresult)
1235 warning_at (loc, OPT_Wsign_conversion,
1236 "signed conversion from %qT to %qT changes "
1237 "value from %qE to %qE",
1238 expr_type, type, expr, result);
1239 else
1240 warning_at (loc, OPT_Wsign_conversion,
1241 "signed conversion from %qT to %qT changes "
1242 "the value of %qE",
1243 expr_type, type, expr);
1244 }
1245 }
1246 else if (TREE_CODE_CLASS (TREE_CODE (result)) == tcc_constant)
1247 warning_at (loc, warnopt,
1248 "conversion from %qT to %qT changes value from %qE to %qE",
1249 expr_type, type, expr, result);
1250 else
1251 warning_at (loc, warnopt,
1252 "conversion from %qT to %qT changes the value of %qE",
1253 expr_type, type, expr);
1254 return true;
1255 }
1256
1257 case PLUS_EXPR:
1258 case MINUS_EXPR:
1259 case MULT_EXPR:
1260 case MAX_EXPR:
1261 case MIN_EXPR:
1262 case TRUNC_MOD_EXPR:
1263 case FLOOR_MOD_EXPR:
1264 case TRUNC_DIV_EXPR:
1265 case FLOOR_DIV_EXPR:
1266 case CEIL_DIV_EXPR:
1267 case EXACT_DIV_EXPR:
1268 case RDIV_EXPR:
1269 arith_ops = 2;
1270 goto default_;
1271
1272 case PREDECREMENT_EXPR:
1273 case PREINCREMENT_EXPR:
1274 case POSTDECREMENT_EXPR:
1275 case POSTINCREMENT_EXPR:
1276 case LSHIFT_EXPR:
1277 case RSHIFT_EXPR:
1278 case FIX_TRUNC_EXPR:
1279 case NON_LVALUE_EXPR:
1280 case NEGATE_EXPR:
1281 case BIT_NOT_EXPR:
1282 arith_ops = 1;
1283 goto default_;
1284
1285 case COND_EXPR:
1286 {
1287 /* In case of COND_EXPR, we do not care about the type of
1288 COND_EXPR, only about the conversion of each operand. */
1289 tree op1 = TREE_OPERAND (expr, 1);
1290 tree op2 = TREE_OPERAND (expr, 2);
1291
1292 return (conversion_warning (loc, type, op1, result)
1293 || conversion_warning (loc, type, op2, result));
1294 }
1295
1296 default_:
1297 default:
1298 conversion_kind = unsafe_conversion_p (type, expr, result, true);
1299 {
1300 int warnopt;
1301 if (conversion_kind == UNSAFE_REAL)
1302 warnopt = OPT_Wfloat_conversion;
1303 else if (conversion_kind == UNSAFE_SIGN)
1304 warnopt = OPT_Wsign_conversion;
1305 else if (conversion_kind)
1306 warnopt = OPT_Wconversion;
1307 else
1308 break;
1309
1310 if (arith_ops
1311 && global_dc->option_enabled (warnopt,
1312 global_dc->lang_mask,
1313 global_dc->option_state))
1314 {
1315 for (int i = 0; i < arith_ops; ++i)
1316 {
1317 tree op = TREE_OPERAND (expr, i);
1318 /* Avoid -Wsign-conversion for (unsigned)(x + (-1)). */
1319 if (TREE_CODE (expr) == PLUS_EXPR && i == 1
1320 && INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
1321 && TREE_CODE (op) == INTEGER_CST
1322 && tree_int_cst_sgn (op) < 0)
1323 op = fold_build1 (NEGATE_EXPR, TREE_TYPE (op), op);
1324 tree opr = convert (type, op);
1325 if (unsafe_conversion_p (type, op, opr, true))
1326 goto op_unsafe;
1327 }
1328 /* The operands seem safe, we might still want to warn if
1329 -Warith-conversion. */
1330 warnopt = OPT_Warith_conversion;
1331 op_unsafe:;
1332 }
1333
1334 if (conversion_kind == UNSAFE_SIGN)
1335 warning_at (loc, warnopt, "conversion to %qT from %qT "
1336 "may change the sign of the result",
1337 type, expr_type);
1338 else if (conversion_kind == UNSAFE_IMAGINARY)
1339 warning_at (loc, warnopt,
1340 "conversion from %qT to %qT discards imaginary component",
1341 expr_type, type);
1342 else
1343 warning_at (loc, warnopt,
1344 "conversion from %qT to %qT may change value",
1345 expr_type, type);
1346 return true;
1347 }
1348 }
1349 return false;
1350 }
1351
1352 /* Produce warnings after a conversion. RESULT is the result of
1353 converting EXPR to TYPE. This is a helper function for
1354 convert_and_check and cp_convert_and_check. */
1355
1356 void
1357 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
1358 tree result)
1359 {
1360 loc = expansion_point_location_if_in_system_header (loc);
1361
1362 while (TREE_CODE (expr) == COMPOUND_EXPR)
1363 expr = TREE_OPERAND (expr, 1);
1364 while (TREE_CODE (result) == COMPOUND_EXPR)
1365 result = TREE_OPERAND (result, 1);
1366
1367 bool cst = TREE_CODE_CLASS (TREE_CODE (result)) == tcc_constant;
1368
1369 tree exprtype = TREE_TYPE (expr);
1370
1371 if (TREE_CODE (expr) == INTEGER_CST
1372 && (TREE_CODE (type) == INTEGER_TYPE
1373 || TREE_CODE (type) == ENUMERAL_TYPE)
1374 && !int_fits_type_p (expr, type))
1375 {
1376 /* Do not diagnose overflow in a constant expression merely
1377 because a conversion overflowed. */
1378 if (TREE_OVERFLOW (result))
1379 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
1380
1381 if (TYPE_UNSIGNED (type))
1382 {
1383 /* This detects cases like converting -129 or 256 to
1384 unsigned char. */
1385 if (!int_fits_type_p (expr, c_common_signed_type (type)))
1386 {
1387 if (cst)
1388 warning_at (loc, OPT_Woverflow,
1389 (TYPE_UNSIGNED (exprtype)
1390 ? G_("conversion from %qT to %qT "
1391 "changes value from %qE to %qE")
1392 : G_("unsigned conversion from %qT to %qT "
1393 "changes value from %qE to %qE")),
1394 exprtype, type, expr, result);
1395 else
1396 warning_at (loc, OPT_Woverflow,
1397 (TYPE_UNSIGNED (exprtype)
1398 ? G_("conversion from %qT to %qT "
1399 "changes the value of %qE")
1400 : G_("unsigned conversion from %qT to %qT "
1401 "changes the value of %qE")),
1402 exprtype, type, expr);
1403 }
1404 else
1405 conversion_warning (loc, type, expr, result);
1406 }
1407 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
1408 {
1409 if (cst)
1410 warning_at (loc, OPT_Woverflow,
1411 "overflow in conversion from %qT to %qT "
1412 "changes value from %qE to %qE",
1413 exprtype, type, expr, result);
1414 else
1415 warning_at (loc, OPT_Woverflow,
1416 "overflow in conversion from %qT to %qT "
1417 "changes the value of %qE",
1418 exprtype, type, expr);
1419 }
1420 /* No warning for converting 0x80000000 to int. */
1421 else if (pedantic
1422 && (TREE_CODE (exprtype) != INTEGER_TYPE
1423 || TYPE_PRECISION (exprtype)
1424 != TYPE_PRECISION (type)))
1425 {
1426 if (cst)
1427 warning_at (loc, OPT_Woverflow,
1428 "overflow in conversion from %qT to %qT "
1429 "changes value from %qE to %qE",
1430 exprtype, type, expr, result);
1431 else
1432 warning_at (loc, OPT_Woverflow,
1433 "overflow in conversion from %qT to %qT "
1434 "changes the value of %qE",
1435 exprtype, type, expr);
1436 }
1437 else
1438 conversion_warning (loc, type, expr, result);
1439 }
1440 else if ((TREE_CODE (result) == INTEGER_CST
1441 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
1442 {
1443 if (cst)
1444 warning_at (loc, OPT_Woverflow,
1445 "overflow in conversion from %qT to %qT "
1446 "changes value from %qE to %qE",
1447 exprtype, type, expr, result);
1448 else
1449 warning_at (loc, OPT_Woverflow,
1450 "overflow in conversion from %qT to %qT "
1451 "changes the value of %qE",
1452 exprtype, type, expr);
1453 }
1454 else
1455 conversion_warning (loc, type, expr, result);
1456 }
1457
1458 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
1459 Used to verify that case values match up with enumerator values. */
1460
1461 static void
1462 match_case_to_enum_1 (tree key, tree type, tree label)
1463 {
1464 /* Avoid warning about enums that have no enumerators. */
1465 if (TYPE_VALUES (type) == NULL_TREE)
1466 return;
1467
1468 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
1469
1470 if (tree_fits_uhwi_p (key))
1471 print_dec (wi::to_wide (key), buf, UNSIGNED);
1472 else if (tree_fits_shwi_p (key))
1473 print_dec (wi::to_wide (key), buf, SIGNED);
1474 else
1475 print_hex (wi::to_wide (key), buf);
1476
1477 if (TYPE_NAME (type) == NULL_TREE)
1478 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
1479 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
1480 "case value %qs not in enumerated type",
1481 buf);
1482 else
1483 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
1484 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
1485 "case value %qs not in enumerated type %qT",
1486 buf, type);
1487 }
1488
1489 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
1490 Used to verify that case values match up with enumerator values. */
1491
1492 static int
1493 match_case_to_enum (splay_tree_node node, void *data)
1494 {
1495 tree label = (tree) node->value;
1496 tree type = (tree) data;
1497
1498 /* Skip default case. */
1499 if (!CASE_LOW (label))
1500 return 0;
1501
1502 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
1503 when we did our enum->case scan. Reset our scratch bit after. */
1504 if (!CASE_LOW_SEEN (label))
1505 match_case_to_enum_1 (CASE_LOW (label), type, label);
1506 else
1507 CASE_LOW_SEEN (label) = 0;
1508
1509 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
1510 not set, that means that CASE_HIGH did not appear when we did our
1511 enum->case scan. Reset our scratch bit after. */
1512 if (CASE_HIGH (label))
1513 {
1514 if (!CASE_HIGH_SEEN (label))
1515 match_case_to_enum_1 (CASE_HIGH (label), type, label);
1516 else
1517 CASE_HIGH_SEEN (label) = 0;
1518 }
1519
1520 return 0;
1521 }
1522
1523 /* Handle -Wswitch*. Called from the front end after parsing the
1524 switch construct. */
1525 /* ??? Should probably be somewhere generic, since other languages
1526 besides C and C++ would want this. At the moment, however, C/C++
1527 are the only tree-ssa languages that support enumerations at all,
1528 so the point is moot. */
1529
1530 void
1531 c_do_switch_warnings (splay_tree cases, location_t switch_location,
1532 tree type, tree cond, bool bool_cond_p)
1533 {
1534 splay_tree_node default_node;
1535 splay_tree_node node;
1536 tree chain;
1537 bool outside_range_p = false;
1538
1539 if (type != error_mark_node
1540 && type != TREE_TYPE (cond)
1541 && INTEGRAL_TYPE_P (type)
1542 && INTEGRAL_TYPE_P (TREE_TYPE (cond))
1543 && (!tree_int_cst_equal (TYPE_MIN_VALUE (type),
1544 TYPE_MIN_VALUE (TREE_TYPE (cond)))
1545 || !tree_int_cst_equal (TYPE_MAX_VALUE (type),
1546 TYPE_MAX_VALUE (TREE_TYPE (cond)))))
1547 {
1548 tree min_value = TYPE_MIN_VALUE (type);
1549 tree max_value = TYPE_MAX_VALUE (type);
1550
1551 node = splay_tree_predecessor (cases, (splay_tree_key) min_value);
1552 if (node && node->key)
1553 {
1554 outside_range_p = true;
1555 /* There is at least one case smaller than TYPE's minimum value.
1556 NODE itself could be still a range overlapping the valid values,
1557 but any predecessors thereof except the default case will be
1558 completely outside of range. */
1559 if (CASE_HIGH ((tree) node->value)
1560 && tree_int_cst_compare (CASE_HIGH ((tree) node->value),
1561 min_value) >= 0)
1562 {
1563 location_t loc = EXPR_LOCATION ((tree) node->value);
1564 warning_at (loc, OPT_Wswitch_outside_range,
1565 "lower value in case label range less than minimum"
1566 " value for type");
1567 CASE_LOW ((tree) node->value) = convert (TREE_TYPE (cond),
1568 min_value);
1569 node->key = (splay_tree_key) CASE_LOW ((tree) node->value);
1570 }
1571 /* All the following ones are completely outside of range. */
1572 do
1573 {
1574 node = splay_tree_predecessor (cases,
1575 (splay_tree_key) min_value);
1576 if (node == NULL || !node->key)
1577 break;
1578 location_t loc = EXPR_LOCATION ((tree) node->value);
1579 warning_at (loc, OPT_Wswitch_outside_range, "case label value is"
1580 " less than minimum value for type");
1581 splay_tree_remove (cases, node->key);
1582 }
1583 while (1);
1584 }
1585 node = splay_tree_lookup (cases, (splay_tree_key) max_value);
1586 if (node == NULL)
1587 node = splay_tree_predecessor (cases, (splay_tree_key) max_value);
1588 /* Handle a single node that might partially overlap the range. */
1589 if (node
1590 && node->key
1591 && CASE_HIGH ((tree) node->value)
1592 && tree_int_cst_compare (CASE_HIGH ((tree) node->value),
1593 max_value) > 0)
1594 {
1595 location_t loc = EXPR_LOCATION ((tree) node->value);
1596 warning_at (loc, OPT_Wswitch_outside_range, "upper value in case"
1597 " label range exceeds maximum value for type");
1598 CASE_HIGH ((tree) node->value)
1599 = convert (TREE_TYPE (cond), max_value);
1600 outside_range_p = true;
1601 }
1602 /* And any nodes that are completely outside of the range. */
1603 while ((node = splay_tree_successor (cases,
1604 (splay_tree_key) max_value))
1605 != NULL)
1606 {
1607 location_t loc = EXPR_LOCATION ((tree) node->value);
1608 warning_at (loc, OPT_Wswitch_outside_range,
1609 "case label value exceeds maximum value for type");
1610 splay_tree_remove (cases, node->key);
1611 outside_range_p = true;
1612 }
1613 }
1614
1615 if (!warn_switch && !warn_switch_enum && !warn_switch_default
1616 && !warn_switch_bool)
1617 return;
1618
1619 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
1620 if (!default_node)
1621 warning_at (switch_location, OPT_Wswitch_default,
1622 "switch missing default case");
1623
1624 /* There are certain cases where -Wswitch-bool warnings aren't
1625 desirable, such as
1626 switch (boolean)
1627 {
1628 case true: ...
1629 case false: ...
1630 }
1631 so be careful here. */
1632 if (warn_switch_bool && bool_cond_p)
1633 {
1634 splay_tree_node min_node;
1635 /* If there's a default node, it's also the value with the minimal
1636 key. So look at the penultimate key (if any). */
1637 if (default_node)
1638 min_node = splay_tree_successor (cases, (splay_tree_key) NULL);
1639 else
1640 min_node = splay_tree_min (cases);
1641 tree min = min_node ? (tree) min_node->key : NULL_TREE;
1642
1643 splay_tree_node max_node = splay_tree_max (cases);
1644 /* This might be a case range, so look at the value with the
1645 maximal key and then check CASE_HIGH. */
1646 tree max = max_node ? (tree) max_node->value : NULL_TREE;
1647 if (max)
1648 max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max);
1649
1650 /* If there's a case value > 1 or < 0, that is outside bool
1651 range, warn. */
1652 if (outside_range_p
1653 || (max && wi::gts_p (wi::to_wide (max), 1))
1654 || (min && wi::lts_p (wi::to_wide (min), 0))
1655 /* And handle the
1656 switch (boolean)
1657 {
1658 case true: ...
1659 case false: ...
1660 default: ...
1661 }
1662 case, where we want to warn. */
1663 || (default_node
1664 && max && wi::to_wide (max) == 1
1665 && min && wi::to_wide (min) == 0))
1666 warning_at (switch_location, OPT_Wswitch_bool,
1667 "switch condition has boolean value");
1668 }
1669
1670 /* From here on, we only care about enumerated types. */
1671 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
1672 return;
1673
1674 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
1675 if (!warn_switch_enum && !warn_switch)
1676 return;
1677
1678 /* Check the cases. Warn about case values which are not members of
1679 the enumerated type. For -Wswitch-enum, or for -Wswitch when
1680 there is no default case, check that exactly all enumeration
1681 literals are covered by the cases. */
1682
1683 /* Clearing COND if it is not an integer constant simplifies
1684 the tests inside the loop below. */
1685 if (TREE_CODE (cond) != INTEGER_CST)
1686 cond = NULL_TREE;
1687
1688 /* The time complexity here is O(N*lg(N)) worst case, but for the
1689 common case of monotonically increasing enumerators, it is
1690 O(N), since the nature of the splay tree will keep the next
1691 element adjacent to the root at all times. */
1692
1693 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
1694 {
1695 tree value = TREE_VALUE (chain);
1696 if (TREE_CODE (value) == CONST_DECL)
1697 value = DECL_INITIAL (value);
1698 node = splay_tree_lookup (cases, (splay_tree_key) value);
1699 if (node)
1700 {
1701 /* Mark the CASE_LOW part of the case entry as seen. */
1702 tree label = (tree) node->value;
1703 CASE_LOW_SEEN (label) = 1;
1704 continue;
1705 }
1706
1707 /* Even though there wasn't an exact match, there might be a
1708 case range which includes the enumerator's value. */
1709 node = splay_tree_predecessor (cases, (splay_tree_key) value);
1710 if (node && CASE_HIGH ((tree) node->value))
1711 {
1712 tree label = (tree) node->value;
1713 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
1714 if (cmp >= 0)
1715 {
1716 /* If we match the upper bound exactly, mark the CASE_HIGH
1717 part of the case entry as seen. */
1718 if (cmp == 0)
1719 CASE_HIGH_SEEN (label) = 1;
1720 continue;
1721 }
1722 }
1723
1724 /* We've now determined that this enumerated literal isn't
1725 handled by the case labels of the switch statement. */
1726
1727 /* If the switch expression is a constant, we only really care
1728 about whether that constant is handled by the switch. */
1729 if (cond && tree_int_cst_compare (cond, value))
1730 continue;
1731
1732 /* If the enumerator is defined in a system header and uses a reserved
1733 name, then we continue to avoid throwing a warning. */
1734 location_t loc = DECL_SOURCE_LOCATION
1735 (TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type)));
1736 if (in_system_header_at (loc)
1737 && name_reserved_for_implementation_p
1738 (IDENTIFIER_POINTER (TREE_PURPOSE (chain))))
1739 continue;
1740
1741 /* If there is a default_node, the only relevant option is
1742 Wswitch-enum. Otherwise, if both are enabled then we prefer
1743 to warn using -Wswitch because -Wswitch is enabled by -Wall
1744 while -Wswitch-enum is explicit. */
1745 warning_at (switch_location,
1746 (default_node || !warn_switch
1747 ? OPT_Wswitch_enum
1748 : OPT_Wswitch),
1749 "enumeration value %qE not handled in switch",
1750 TREE_PURPOSE (chain));
1751 }
1752
1753 /* Warn if there are case expressions that don't correspond to
1754 enumerators. This can occur since C and C++ don't enforce
1755 type-checking of assignments to enumeration variables.
1756
1757 The time complexity here is now always O(N) worst case, since
1758 we should have marked both the lower bound and upper bound of
1759 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
1760 above. This scan also resets those fields. */
1761
1762 splay_tree_foreach (cases, match_case_to_enum, type);
1763 }
1764
1765 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
1766 expression, because B will always be true. */
1767
1768 void
1769 warn_for_omitted_condop (location_t location, tree cond)
1770 {
1771 /* In C++ template declarations it can happen that the type is dependent
1772 and not yet known, thus TREE_TYPE (cond) == NULL_TREE. */
1773 if (truth_value_p (TREE_CODE (cond))
1774 || (TREE_TYPE (cond) != NULL_TREE
1775 && TREE_CODE (TREE_TYPE (cond)) == BOOLEAN_TYPE))
1776 warning_at (location, OPT_Wparentheses,
1777 "the omitted middle operand in %<?:%> will always be %<true%>, "
1778 "suggest explicit middle operand");
1779 }
1780
1781 /* Give an error for storing into ARG, which is 'const'. USE indicates
1782 how ARG was being used. */
1783
1784 void
1785 readonly_error (location_t loc, tree arg, enum lvalue_use use)
1786 {
1787 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
1788 || use == lv_asm);
1789 STRIP_ANY_LOCATION_WRAPPER (arg);
1790 /* Using this macro rather than (for example) arrays of messages
1791 ensures that all the format strings are checked at compile
1792 time. */
1793 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
1794 : (use == lv_increment ? (I) \
1795 : (use == lv_decrement ? (D) : (AS))))
1796 if (TREE_CODE (arg) == COMPONENT_REF)
1797 {
1798 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
1799 error_at (loc, READONLY_MSG (G_("assignment of member "
1800 "%qD in read-only object"),
1801 G_("increment of member "
1802 "%qD in read-only object"),
1803 G_("decrement of member "
1804 "%qD in read-only object"),
1805 G_("member %qD in read-only object "
1806 "used as %<asm%> output")),
1807 TREE_OPERAND (arg, 1));
1808 else
1809 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
1810 G_("increment of read-only member %qD"),
1811 G_("decrement of read-only member %qD"),
1812 G_("read-only member %qD used as %<asm%> output")),
1813 TREE_OPERAND (arg, 1));
1814 }
1815 else if (VAR_P (arg))
1816 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
1817 G_("increment of read-only variable %qD"),
1818 G_("decrement of read-only variable %qD"),
1819 G_("read-only variable %qD used as %<asm%> output")),
1820 arg);
1821 else if (TREE_CODE (arg) == PARM_DECL)
1822 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
1823 G_("increment of read-only parameter %qD"),
1824 G_("decrement of read-only parameter %qD"),
1825 G_("read-only parameter %qD use as %<asm%> output")),
1826 arg);
1827 else if (TREE_CODE (arg) == RESULT_DECL)
1828 {
1829 gcc_assert (c_dialect_cxx ());
1830 error_at (loc, READONLY_MSG (G_("assignment of "
1831 "read-only named return value %qD"),
1832 G_("increment of "
1833 "read-only named return value %qD"),
1834 G_("decrement of "
1835 "read-only named return value %qD"),
1836 G_("read-only named return value %qD "
1837 "used as %<asm%>output")),
1838 arg);
1839 }
1840 else if (TREE_CODE (arg) == FUNCTION_DECL)
1841 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
1842 G_("increment of function %qD"),
1843 G_("decrement of function %qD"),
1844 G_("function %qD used as %<asm%> output")),
1845 arg);
1846 else
1847 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
1848 G_("increment of read-only location %qE"),
1849 G_("decrement of read-only location %qE"),
1850 G_("read-only location %qE used as %<asm%> output")),
1851 arg);
1852 }
1853
1854 /* Print an error message for an invalid lvalue. USE says
1855 how the lvalue is being used and so selects the error message. LOC
1856 is the location for the error. */
1857
1858 void
1859 lvalue_error (location_t loc, enum lvalue_use use)
1860 {
1861 switch (use)
1862 {
1863 case lv_assign:
1864 error_at (loc, "lvalue required as left operand of assignment");
1865 break;
1866 case lv_increment:
1867 error_at (loc, "lvalue required as increment operand");
1868 break;
1869 case lv_decrement:
1870 error_at (loc, "lvalue required as decrement operand");
1871 break;
1872 case lv_addressof:
1873 error_at (loc, "lvalue required as unary %<&%> operand");
1874 break;
1875 case lv_asm:
1876 error_at (loc, "lvalue required in %<asm%> statement");
1877 break;
1878 default:
1879 gcc_unreachable ();
1880 }
1881 }
1882
1883 /* Print an error message for an invalid indirection of type TYPE.
1884 ERRSTRING is the name of the operator for the indirection. */
1885
1886 void
1887 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
1888 {
1889 switch (errstring)
1890 {
1891 case RO_NULL:
1892 gcc_assert (c_dialect_cxx ());
1893 error_at (loc, "invalid type argument (have %qT)", type);
1894 break;
1895 case RO_ARRAY_INDEXING:
1896 error_at (loc,
1897 "invalid type argument of array indexing (have %qT)",
1898 type);
1899 break;
1900 case RO_UNARY_STAR:
1901 error_at (loc,
1902 "invalid type argument of unary %<*%> (have %qT)",
1903 type);
1904 break;
1905 case RO_ARROW:
1906 error_at (loc,
1907 "invalid type argument of %<->%> (have %qT)",
1908 type);
1909 break;
1910 case RO_ARROW_STAR:
1911 error_at (loc,
1912 "invalid type argument of %<->*%> (have %qT)",
1913 type);
1914 break;
1915 case RO_IMPLICIT_CONVERSION:
1916 error_at (loc,
1917 "invalid type argument of implicit conversion (have %qT)",
1918 type);
1919 break;
1920 default:
1921 gcc_unreachable ();
1922 }
1923 }
1924
1925 /* Subscripting with type char is likely to lose on a machine where
1926 chars are signed. So warn on any machine, but optionally. Don't
1927 warn for unsigned char since that type is safe. Don't warn for
1928 signed char because anyone who uses that must have done so
1929 deliberately. Furthermore, we reduce the false positive load by
1930 warning only for non-constant value of type char.
1931 LOC is the location of the subscripting expression. */
1932
1933 void
1934 warn_array_subscript_with_type_char (location_t loc, tree index)
1935 {
1936 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1937 {
1938 /* If INDEX has a location, use it; otherwise use LOC (the location
1939 of the subscripting expression as a whole). */
1940 loc = EXPR_LOC_OR_LOC (index, loc);
1941 STRIP_ANY_LOCATION_WRAPPER (index);
1942 if (TREE_CODE (index) != INTEGER_CST)
1943 warning_at (loc, OPT_Wchar_subscripts,
1944 "array subscript has type %<char%>");
1945 }
1946 }
1947
1948 /* Implement -Wparentheses for the unexpected C precedence rules, to
1949 cover cases like x + y << z which readers are likely to
1950 misinterpret. We have seen an expression in which CODE is a binary
1951 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
1952 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
1953 CODE_RIGHT may be ERROR_MARK, which means that that side of the
1954 expression was not formed using a binary or unary operator, or it
1955 was enclosed in parentheses. */
1956
1957 void
1958 warn_about_parentheses (location_t loc, enum tree_code code,
1959 enum tree_code code_left, tree arg_left,
1960 enum tree_code code_right, tree arg_right)
1961 {
1962 if (!warn_parentheses)
1963 return;
1964
1965 /* This macro tests that the expression ARG with original tree code
1966 CODE appears to be a boolean expression. or the result of folding a
1967 boolean expression. */
1968 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
1969 (truth_value_p (TREE_CODE (ARG)) \
1970 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
1971 /* Folding may create 0 or 1 integers from other expressions. */ \
1972 || ((CODE) != INTEGER_CST \
1973 && (integer_onep (ARG) || integer_zerop (ARG))))
1974
1975 switch (code)
1976 {
1977 case LSHIFT_EXPR:
1978 if (code_left == PLUS_EXPR)
1979 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1980 "suggest parentheses around %<+%> inside %<<<%>");
1981 else if (code_right == PLUS_EXPR)
1982 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1983 "suggest parentheses around %<+%> inside %<<<%>");
1984 else if (code_left == MINUS_EXPR)
1985 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1986 "suggest parentheses around %<-%> inside %<<<%>");
1987 else if (code_right == MINUS_EXPR)
1988 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1989 "suggest parentheses around %<-%> inside %<<<%>");
1990 return;
1991
1992 case RSHIFT_EXPR:
1993 if (code_left == PLUS_EXPR)
1994 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
1995 "suggest parentheses around %<+%> inside %<>>%>");
1996 else if (code_right == PLUS_EXPR)
1997 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
1998 "suggest parentheses around %<+%> inside %<>>%>");
1999 else if (code_left == MINUS_EXPR)
2000 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
2001 "suggest parentheses around %<-%> inside %<>>%>");
2002 else if (code_right == MINUS_EXPR)
2003 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
2004 "suggest parentheses around %<-%> inside %<>>%>");
2005 return;
2006
2007 case TRUTH_ORIF_EXPR:
2008 if (code_left == TRUTH_ANDIF_EXPR)
2009 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
2010 "suggest parentheses around %<&&%> within %<||%>");
2011 else if (code_right == TRUTH_ANDIF_EXPR)
2012 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
2013 "suggest parentheses around %<&&%> within %<||%>");
2014 return;
2015
2016 case BIT_IOR_EXPR:
2017 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
2018 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
2019 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
2020 "suggest parentheses around arithmetic in operand of %<|%>");
2021 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
2022 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
2023 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
2024 "suggest parentheses around arithmetic in operand of %<|%>");
2025 /* Check cases like x|y==z */
2026 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
2027 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
2028 "suggest parentheses around comparison in operand of %<|%>");
2029 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
2030 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
2031 "suggest parentheses around comparison in operand of %<|%>");
2032 /* Check cases like !x | y */
2033 else if (code_left == TRUTH_NOT_EXPR
2034 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
2035 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
2036 "suggest parentheses around operand of "
2037 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
2038 return;
2039
2040 case BIT_XOR_EXPR:
2041 if (code_left == BIT_AND_EXPR
2042 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
2043 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
2044 "suggest parentheses around arithmetic in operand of %<^%>");
2045 else if (code_right == BIT_AND_EXPR
2046 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
2047 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
2048 "suggest parentheses around arithmetic in operand of %<^%>");
2049 /* Check cases like x^y==z */
2050 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
2051 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
2052 "suggest parentheses around comparison in operand of %<^%>");
2053 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
2054 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
2055 "suggest parentheses around comparison in operand of %<^%>");
2056 return;
2057
2058 case BIT_AND_EXPR:
2059 if (code_left == PLUS_EXPR)
2060 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
2061 "suggest parentheses around %<+%> in operand of %<&%>");
2062 else if (code_right == PLUS_EXPR)
2063 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
2064 "suggest parentheses around %<+%> in operand of %<&%>");
2065 else if (code_left == MINUS_EXPR)
2066 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
2067 "suggest parentheses around %<-%> in operand of %<&%>");
2068 else if (code_right == MINUS_EXPR)
2069 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
2070 "suggest parentheses around %<-%> in operand of %<&%>");
2071 /* Check cases like x&y==z */
2072 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
2073 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
2074 "suggest parentheses around comparison in operand of %<&%>");
2075 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
2076 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
2077 "suggest parentheses around comparison in operand of %<&%>");
2078 /* Check cases like !x & y */
2079 else if (code_left == TRUTH_NOT_EXPR
2080 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
2081 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
2082 "suggest parentheses around operand of "
2083 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
2084 return;
2085
2086 case EQ_EXPR:
2087 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
2088 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
2089 "suggest parentheses around comparison in operand of %<==%>");
2090 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
2091 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
2092 "suggest parentheses around comparison in operand of %<==%>");
2093 return;
2094 case NE_EXPR:
2095 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
2096 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
2097 "suggest parentheses around comparison in operand of %<!=%>");
2098 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
2099 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
2100 "suggest parentheses around comparison in operand of %<!=%>");
2101 return;
2102
2103 default:
2104 if (TREE_CODE_CLASS (code) == tcc_comparison)
2105 {
2106 if (TREE_CODE_CLASS (code_left) == tcc_comparison
2107 && code_left != NE_EXPR && code_left != EQ_EXPR
2108 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
2109 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
2110 "comparisons like %<X<=Y<=Z%> do not "
2111 "have their mathematical meaning");
2112 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
2113 && code_right != NE_EXPR && code_right != EQ_EXPR
2114 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
2115 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
2116 "comparisons like %<X<=Y<=Z%> do not "
2117 "have their mathematical meaning");
2118 }
2119 return;
2120 }
2121 #undef NOT_A_BOOLEAN_EXPR_P
2122 }
2123
2124 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
2125
2126 void
2127 warn_for_unused_label (tree label)
2128 {
2129 if (!TREE_USED (label))
2130 {
2131 if (DECL_INITIAL (label))
2132 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
2133 else
2134 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
2135 }
2136 else if (asan_sanitize_use_after_scope ())
2137 {
2138 if (asan_used_labels == NULL)
2139 asan_used_labels = new hash_set<tree> (16);
2140
2141 asan_used_labels->add (label);
2142 }
2143 }
2144
2145 /* Warn for division by zero according to the value of DIVISOR. LOC
2146 is the location of the division operator. */
2147
2148 void
2149 warn_for_div_by_zero (location_t loc, tree divisor)
2150 {
2151 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
2152 about division by zero. Do not issue a warning if DIVISOR has a
2153 floating-point type, since we consider 0.0/0.0 a valid way of
2154 generating a NaN. */
2155 if (c_inhibit_evaluation_warnings == 0
2156 && (integer_zerop (divisor) || fixed_zerop (divisor)))
2157 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
2158 }
2159
2160 /* Warn for patterns where memset appears to be used incorrectly. The
2161 warning location should be LOC. ARG0, and ARG2 are the first and
2162 last arguments to the call, while LITERAL_ZERO_MASK has a 1 bit for
2163 each argument that was a literal zero. */
2164
2165 void
2166 warn_for_memset (location_t loc, tree arg0, tree arg2,
2167 int literal_zero_mask)
2168 {
2169 arg0 = fold_for_warn (arg0);
2170 arg2 = fold_for_warn (arg2);
2171
2172 if (warn_memset_transposed_args
2173 && integer_zerop (arg2)
2174 && (literal_zero_mask & (1 << 2)) != 0
2175 && (literal_zero_mask & (1 << 1)) == 0)
2176 warning_at (loc, OPT_Wmemset_transposed_args,
2177 "%<memset%> used with constant zero length "
2178 "parameter; this could be due to transposed "
2179 "parameters");
2180
2181 if (warn_memset_elt_size && TREE_CODE (arg2) == INTEGER_CST)
2182 {
2183 STRIP_NOPS (arg0);
2184 if (ADDR_EXPR_P (arg0))
2185 arg0 = TREE_OPERAND (arg0, 0);
2186 tree type = TREE_TYPE (arg0);
2187 if (type != NULL_TREE && TREE_CODE (type) == ARRAY_TYPE)
2188 {
2189 tree elt_type = TREE_TYPE (type);
2190 tree domain = TYPE_DOMAIN (type);
2191 if (COMPLETE_TYPE_P (elt_type)
2192 && !integer_onep (TYPE_SIZE_UNIT (elt_type))
2193 && domain != NULL_TREE
2194 && TYPE_MAX_VALUE (domain)
2195 && TYPE_MIN_VALUE (domain)
2196 && integer_zerop (TYPE_MIN_VALUE (domain))
2197 && integer_onep (fold_build2 (MINUS_EXPR, domain,
2198 arg2,
2199 TYPE_MAX_VALUE (domain))))
2200 warning_at (loc, OPT_Wmemset_elt_size,
2201 "%<memset%> used with length equal to "
2202 "number of elements without multiplication "
2203 "by element size");
2204 }
2205 }
2206 }
2207
2208 /* Subroutine of build_binary_op. Give warnings for comparisons
2209 between signed and unsigned quantities that may fail. Do the
2210 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
2211 so that casts will be considered, but default promotions won't
2212 be.
2213
2214 LOCATION is the location of the comparison operator.
2215
2216 The arguments of this function map directly to local variables
2217 of build_binary_op. */
2218
2219 void
2220 warn_for_sign_compare (location_t location,
2221 tree orig_op0, tree orig_op1,
2222 tree op0, tree op1,
2223 tree result_type, enum tree_code resultcode)
2224 {
2225 if (error_operand_p (orig_op0) || error_operand_p (orig_op1))
2226 return;
2227
2228 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
2229 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
2230 int unsignedp0, unsignedp1;
2231
2232 /* In C++, check for comparison of different enum types. */
2233 if (c_dialect_cxx()
2234 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
2235 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
2236 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
2237 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
2238 {
2239 warning_at (location,
2240 OPT_Wsign_compare, "comparison between types %qT and %qT",
2241 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
2242 }
2243
2244 /* Do not warn if the comparison is being done in a signed type,
2245 since the signed type will only be chosen if it can represent
2246 all the values of the unsigned type. */
2247 if (!TYPE_UNSIGNED (result_type))
2248 /* OK */;
2249 /* Do not warn if both operands are unsigned. */
2250 else if (op0_signed == op1_signed)
2251 /* OK */;
2252 else
2253 {
2254 tree sop, uop, base_type;
2255 bool ovf;
2256
2257 if (op0_signed)
2258 sop = orig_op0, uop = orig_op1;
2259 else
2260 sop = orig_op1, uop = orig_op0;
2261
2262 sop = fold_for_warn (sop);
2263 uop = fold_for_warn (uop);
2264
2265 STRIP_TYPE_NOPS (sop);
2266 STRIP_TYPE_NOPS (uop);
2267 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
2268 ? TREE_TYPE (result_type) : result_type);
2269
2270 /* Do not warn if the signed quantity is an unsuffixed integer
2271 literal (or some static constant expression involving such
2272 literals or a conditional expression involving such literals)
2273 and it is non-negative. */
2274 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
2275 /* OK */;
2276 /* Do not warn if the comparison is an equality operation, the
2277 unsigned quantity is an integral constant, and it would fit
2278 in the result if the result were signed. */
2279 else if (TREE_CODE (uop) == INTEGER_CST
2280 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
2281 && int_fits_type_p (uop, c_common_signed_type (base_type)))
2282 /* OK */;
2283 /* In C, do not warn if the unsigned quantity is an enumeration
2284 constant and its maximum value would fit in the result if the
2285 result were signed. */
2286 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
2287 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
2288 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
2289 c_common_signed_type (base_type)))
2290 /* OK */;
2291 else
2292 warning_at (location, OPT_Wsign_compare,
2293 "comparison of integer expressions of different "
2294 "signedness: %qT and %qT", TREE_TYPE (orig_op0),
2295 TREE_TYPE (orig_op1));
2296 }
2297
2298 /* Warn if two unsigned values are being compared in a size larger
2299 than their original size, and one (and only one) is the result of
2300 a `~' operator. This comparison will always fail.
2301
2302 Also warn if one operand is a constant, and the constant does not
2303 have all bits set that are set in the ~ operand when it is
2304 extended. */
2305
2306 op0 = c_common_get_narrower (op0, &unsignedp0);
2307 op1 = c_common_get_narrower (op1, &unsignedp1);
2308
2309 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
2310 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
2311 {
2312 if (TREE_CODE (op0) == BIT_NOT_EXPR)
2313 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
2314 if (TREE_CODE (op1) == BIT_NOT_EXPR)
2315 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
2316
2317 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
2318 {
2319 tree primop;
2320 HOST_WIDE_INT constant, mask;
2321 int unsignedp;
2322 unsigned int bits;
2323
2324 if (tree_fits_shwi_p (op0))
2325 {
2326 primop = op1;
2327 unsignedp = unsignedp1;
2328 constant = tree_to_shwi (op0);
2329 }
2330 else
2331 {
2332 primop = op0;
2333 unsignedp = unsignedp0;
2334 constant = tree_to_shwi (op1);
2335 }
2336
2337 bits = TYPE_PRECISION (TREE_TYPE (primop));
2338 if (bits < TYPE_PRECISION (result_type)
2339 && bits < HOST_BITS_PER_LONG && unsignedp)
2340 {
2341 mask = HOST_WIDE_INT_M1U << bits;
2342 if ((mask & constant) != mask)
2343 {
2344 if (constant == 0)
2345 warning_at (location, OPT_Wsign_compare,
2346 "promoted bitwise complement of an unsigned "
2347 "value is always nonzero");
2348 else
2349 warning_at (location, OPT_Wsign_compare,
2350 "comparison of promoted bitwise complement "
2351 "of an unsigned value with constant");
2352 }
2353 }
2354 }
2355 else if (unsignedp0 && unsignedp1
2356 && (TYPE_PRECISION (TREE_TYPE (op0))
2357 < TYPE_PRECISION (result_type))
2358 && (TYPE_PRECISION (TREE_TYPE (op1))
2359 < TYPE_PRECISION (result_type)))
2360 warning_at (location, OPT_Wsign_compare,
2361 "comparison of promoted bitwise complement "
2362 "of an unsigned value with unsigned");
2363 }
2364 }
2365
2366 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
2367 type via c_common_type. If -Wdouble-promotion is in use, and the
2368 conditions for warning have been met, issue a warning. GMSGID is
2369 the warning message. It must have two %T specifiers for the type
2370 that was converted (generally "float") and the type to which it was
2371 converted (generally "double), respectively. LOC is the location
2372 to which the warning should refer. */
2373
2374 void
2375 do_warn_double_promotion (tree result_type, tree type1, tree type2,
2376 const char *gmsgid, location_t loc)
2377 {
2378 tree source_type;
2379
2380 if (!warn_double_promotion)
2381 return;
2382 /* If the conversion will not occur at run-time, there is no need to
2383 warn about it. */
2384 if (c_inhibit_evaluation_warnings)
2385 return;
2386 /* If an invalid conversion has occured, don't warn. */
2387 if (result_type == error_mark_node)
2388 return;
2389 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
2390 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
2391 return;
2392 if (TYPE_MAIN_VARIANT (type1) == float_type_node
2393 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
2394 source_type = type1;
2395 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
2396 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
2397 source_type = type2;
2398 else
2399 return;
2400 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
2401 }
2402
2403 /* Possibly warn about unused parameters. */
2404
2405 void
2406 do_warn_unused_parameter (tree fn)
2407 {
2408 tree decl;
2409
2410 for (decl = DECL_ARGUMENTS (fn);
2411 decl; decl = DECL_CHAIN (decl))
2412 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
2413 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
2414 && !TREE_NO_WARNING (decl))
2415 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter,
2416 "unused parameter %qD", decl);
2417 }
2418
2419 /* If DECL is a typedef that is declared in the current function,
2420 record it for the purpose of -Wunused-local-typedefs. */
2421
2422 void
2423 record_locally_defined_typedef (tree decl)
2424 {
2425 struct c_language_function *l;
2426
2427 if (!warn_unused_local_typedefs
2428 || cfun == NULL
2429 /* if this is not a locally defined typedef then we are not
2430 interested. */
2431 || !is_typedef_decl (decl)
2432 || !decl_function_context (decl))
2433 return;
2434
2435 l = (struct c_language_function *) cfun->language;
2436 vec_safe_push (l->local_typedefs, decl);
2437 }
2438
2439 /* If T is a TYPE_DECL declared locally, mark it as used. */
2440
2441 void
2442 maybe_record_typedef_use (tree t)
2443 {
2444 if (!is_typedef_decl (t))
2445 return;
2446
2447 TREE_USED (t) = true;
2448 }
2449
2450 /* Warn if there are some unused locally defined typedefs in the
2451 current function. */
2452
2453 void
2454 maybe_warn_unused_local_typedefs (void)
2455 {
2456 int i;
2457 tree decl;
2458 /* The number of times we have emitted -Wunused-local-typedefs
2459 warnings. If this is different from errorcount, that means some
2460 unrelated errors have been issued. In which case, we'll avoid
2461 emitting "unused-local-typedefs" warnings. */
2462 static int unused_local_typedefs_warn_count;
2463 struct c_language_function *l;
2464
2465 if (cfun == NULL)
2466 return;
2467
2468 if ((l = (struct c_language_function *) cfun->language) == NULL)
2469 return;
2470
2471 if (warn_unused_local_typedefs
2472 && errorcount == unused_local_typedefs_warn_count)
2473 {
2474 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
2475 if (!TREE_USED (decl))
2476 warning_at (DECL_SOURCE_LOCATION (decl),
2477 OPT_Wunused_local_typedefs,
2478 "typedef %qD locally defined but not used", decl);
2479 unused_local_typedefs_warn_count = errorcount;
2480 }
2481
2482 vec_free (l->local_typedefs);
2483 }
2484
2485 /* If we're creating an if-else-if condition chain, first see if we
2486 already have this COND in the CHAIN. If so, warn and don't add COND
2487 into the vector, otherwise add the COND there. LOC is the location
2488 of COND. */
2489
2490 void
2491 warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain)
2492 {
2493 /* No chain has been created yet. Do nothing. */
2494 if (*chain == NULL)
2495 return;
2496
2497 if (TREE_SIDE_EFFECTS (cond))
2498 {
2499 /* Uh-oh! This condition has a side-effect, thus invalidates
2500 the whole chain. */
2501 delete *chain;
2502 *chain = NULL;
2503 return;
2504 }
2505
2506 unsigned int ix;
2507 tree t;
2508 bool found = false;
2509 FOR_EACH_VEC_ELT (**chain, ix, t)
2510 if (operand_equal_p (cond, t, 0))
2511 {
2512 auto_diagnostic_group d;
2513 if (warning_at (loc, OPT_Wduplicated_cond,
2514 "duplicated %<if%> condition"))
2515 inform (EXPR_LOCATION (t), "previously used here");
2516 found = true;
2517 break;
2518 }
2519
2520 if (!found
2521 && !CONSTANT_CLASS_P (cond)
2522 /* Don't infinitely grow the chain. */
2523 && (*chain)->length () < 512)
2524 (*chain)->safe_push (cond);
2525 }
2526
2527 /* Check and possibly warn if two declarations have contradictory
2528 attributes, such as always_inline vs. noinline. */
2529
2530 bool
2531 diagnose_mismatched_attributes (tree olddecl, tree newdecl)
2532 {
2533 bool warned = false;
2534
2535 tree a1 = lookup_attribute ("optimize", DECL_ATTRIBUTES (olddecl));
2536 tree a2 = lookup_attribute ("optimize", DECL_ATTRIBUTES (newdecl));
2537 /* An optimization attribute applied on a declaration after the
2538 definition is likely not what the user wanted. */
2539 if (a2 != NULL_TREE
2540 && DECL_SAVED_TREE (olddecl) != NULL_TREE
2541 && (a1 == NULL_TREE || !attribute_list_equal (a1, a2)))
2542 warned |= warning (OPT_Wattributes,
2543 "optimization attribute on %qD follows "
2544 "definition but the attribute doesn%'t match",
2545 newdecl);
2546
2547 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2548 if (DECL_DECLARED_INLINE_P (newdecl)
2549 && DECL_UNINLINABLE (olddecl)
2550 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2551 warned |= warning (OPT_Wattributes, "inline declaration of %qD follows "
2552 "declaration with attribute %<noinline%>", newdecl);
2553 else if (DECL_DECLARED_INLINE_P (olddecl)
2554 && DECL_UNINLINABLE (newdecl)
2555 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2556 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
2557 "%<noinline%> follows inline declaration", newdecl);
2558
2559 return warned;
2560 }
2561
2562 /* Warn if signed left shift overflows. We don't warn
2563 about left-shifting 1 into the sign bit in C++14; cf.
2564 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
2565 and don't warn for C++20 at all, as signed left shifts never
2566 overflow.
2567 LOC is a location of the shift; OP0 and OP1 are the operands.
2568 Return true if an overflow is detected, false otherwise. */
2569
2570 bool
2571 maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
2572 {
2573 if (TREE_CODE (op0) != INTEGER_CST
2574 || TREE_CODE (op1) != INTEGER_CST)
2575 return false;
2576
2577 tree type0 = TREE_TYPE (op0);
2578 unsigned int prec0 = TYPE_PRECISION (type0);
2579
2580 /* Left-hand operand must be signed. */
2581 if (TYPE_UNSIGNED (type0) || cxx_dialect >= cxx20)
2582 return false;
2583
2584 unsigned int min_prec = (wi::min_precision (wi::to_wide (op0), SIGNED)
2585 + TREE_INT_CST_LOW (op1));
2586 /* Handle the case of left-shifting 1 into the sign bit.
2587 * However, shifting 1 _out_ of the sign bit, as in
2588 * INT_MIN << 1, is considered an overflow.
2589 */
2590 if (!tree_int_cst_sign_bit (op0) && min_prec == prec0 + 1)
2591 {
2592 /* Never warn for C++14 onwards. */
2593 if (cxx_dialect >= cxx14)
2594 return false;
2595 /* Otherwise only if -Wshift-overflow=2. But return
2596 true to signal an overflow for the sake of integer
2597 constant expressions. */
2598 if (warn_shift_overflow < 2)
2599 return true;
2600 }
2601
2602 bool overflowed = min_prec > prec0;
2603 if (overflowed && c_inhibit_evaluation_warnings == 0)
2604 warning_at (loc, OPT_Wshift_overflow_,
2605 "result of %qE requires %u bits to represent, "
2606 "but %qT only has %u bits",
2607 build2_loc (loc, LSHIFT_EXPR, type0, op0, op1),
2608 min_prec, type0, prec0);
2609
2610 return overflowed;
2611 }
2612
2613 /* Warn about boolean expression compared with an integer value different
2614 from true/false. Warns also e.g. about "(i1 == i2) == 2".
2615 LOC is the location of the comparison, CODE is its code, OP0 and OP1
2616 are the operands of the comparison. The caller must ensure that
2617 either operand is a boolean expression. */
2618
2619 void
2620 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
2621 tree op1)
2622 {
2623 if (TREE_CODE_CLASS (code) != tcc_comparison)
2624 return;
2625
2626 tree f, cst;
2627 if (f = fold_for_warn (op0),
2628 TREE_CODE (f) == INTEGER_CST)
2629 cst = op0 = f;
2630 else if (f = fold_for_warn (op1),
2631 TREE_CODE (f) == INTEGER_CST)
2632 cst = op1 = f;
2633 else
2634 return;
2635
2636 if (!integer_zerop (cst) && !integer_onep (cst))
2637 {
2638 int sign = (TREE_CODE (op0) == INTEGER_CST
2639 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
2640 if (code == EQ_EXPR
2641 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
2642 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
2643 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
2644 "with boolean expression is always false", cst);
2645 else
2646 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
2647 "with boolean expression is always true", cst);
2648 }
2649 else if (integer_zerop (cst) || integer_onep (cst))
2650 {
2651 /* If the non-constant operand isn't of a boolean type, we
2652 don't want to warn here. */
2653 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
2654 /* Handle booleans promoted to integers. */
2655 if (bool_promoted_to_int_p (noncst))
2656 /* Warn. */;
2657 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
2658 && !truth_value_p (TREE_CODE (noncst)))
2659 return;
2660 /* Do some magic to get the right diagnostics. */
2661 bool flag = TREE_CODE (op0) == INTEGER_CST;
2662 flag = integer_zerop (cst) ? flag : !flag;
2663 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
2664 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
2665 "with boolean expression is always true", cst);
2666 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
2667 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
2668 "with boolean expression is always false", cst);
2669 }
2670 }
2671
2672 /* Warn if an argument at position param_pos is passed to a
2673 restrict-qualified param, and it aliases with another argument.
2674 Return true if a warning has been issued. */
2675
2676 bool
2677 warn_for_restrict (unsigned param_pos, tree *argarray, unsigned nargs)
2678 {
2679 tree arg = argarray[param_pos];
2680 if (TREE_VISITED (arg) || integer_zerop (arg))
2681 return false;
2682
2683 location_t loc = EXPR_LOC_OR_LOC (arg, input_location);
2684 gcc_rich_location richloc (loc);
2685
2686 unsigned i;
2687 auto_vec<int, 16> arg_positions;
2688
2689 for (i = 0; i < nargs; i++)
2690 {
2691 if (i == param_pos)
2692 continue;
2693
2694 tree current_arg = argarray[i];
2695 if (operand_equal_p (arg, current_arg, 0))
2696 {
2697 TREE_VISITED (current_arg) = 1;
2698 arg_positions.safe_push (i + 1);
2699 }
2700 }
2701
2702 if (arg_positions.is_empty ())
2703 return false;
2704
2705 int pos;
2706 FOR_EACH_VEC_ELT (arg_positions, i, pos)
2707 {
2708 arg = argarray[pos - 1];
2709 if (EXPR_HAS_LOCATION (arg))
2710 richloc.add_range (EXPR_LOCATION (arg));
2711 }
2712
2713 return warning_n (&richloc, OPT_Wrestrict, arg_positions.length (),
2714 "passing argument %i to %qs-qualified parameter"
2715 " aliases with argument %Z",
2716 "passing argument %i to %qs-qualified parameter"
2717 " aliases with arguments %Z",
2718 param_pos + 1, "restrict", arg_positions.address (),
2719 arg_positions.length ());
2720 }
2721
2722 /* Callback function to determine whether an expression TP or one of its
2723 subexpressions comes from macro expansion. Used to suppress bogus
2724 warnings. */
2725
2726 static tree
2727 expr_from_macro_expansion_r (tree *tp, int *, void *)
2728 {
2729 if (CAN_HAVE_LOCATION_P (*tp)
2730 && from_macro_expansion_at (EXPR_LOCATION (*tp)))
2731 return integer_zero_node;
2732
2733 return NULL_TREE;
2734 }
2735
2736 /* Possibly warn when an if-else has identical branches. */
2737
2738 static void
2739 do_warn_duplicated_branches (tree expr)
2740 {
2741 tree thenb = COND_EXPR_THEN (expr);
2742 tree elseb = COND_EXPR_ELSE (expr);
2743
2744 /* Don't bother if any of the branches is missing. */
2745 if (thenb == NULL_TREE || elseb == NULL_TREE)
2746 return;
2747
2748 /* And don't warn for empty statements. */
2749 if (TREE_CODE (thenb) == NOP_EXPR
2750 && TREE_TYPE (thenb) == void_type_node
2751 && TREE_OPERAND (thenb, 0) == size_zero_node)
2752 return;
2753
2754 /* ... or empty branches. */
2755 if (TREE_CODE (thenb) == STATEMENT_LIST
2756 && STATEMENT_LIST_HEAD (thenb) == NULL)
2757 return;
2758
2759 /* Compute the hash of the then branch. */
2760 inchash::hash hstate0 (0);
2761 inchash::add_expr (thenb, hstate0);
2762 hashval_t h0 = hstate0.end ();
2763
2764 /* Compute the hash of the else branch. */
2765 inchash::hash hstate1 (0);
2766 inchash::add_expr (elseb, hstate1);
2767 hashval_t h1 = hstate1.end ();
2768
2769 /* Compare the hashes. */
2770 if (h0 == h1
2771 && operand_equal_p (thenb, elseb, OEP_LEXICOGRAPHIC)
2772 /* Don't warn if any of the branches or their subexpressions comes
2773 from a macro. */
2774 && !walk_tree_without_duplicates (&thenb, expr_from_macro_expansion_r,
2775 NULL)
2776 && !walk_tree_without_duplicates (&elseb, expr_from_macro_expansion_r,
2777 NULL))
2778 warning_at (EXPR_LOCATION (expr), OPT_Wduplicated_branches,
2779 "this condition has identical branches");
2780 }
2781
2782 /* Callback for c_genericize to implement -Wduplicated-branches. */
2783
2784 tree
2785 do_warn_duplicated_branches_r (tree *tp, int *, void *)
2786 {
2787 if (TREE_CODE (*tp) == COND_EXPR)
2788 do_warn_duplicated_branches (*tp);
2789 return NULL_TREE;
2790 }
2791
2792 /* Implementation of -Wmultistatement-macros. This warning warns about
2793 cases when a macro expands to multiple statements not wrapped in
2794 do {} while (0) or ({ }) and is used as a body of if/else/for/while
2795 conditionals. For example,
2796
2797 #define DOIT x++; y++
2798
2799 if (c)
2800 DOIT;
2801
2802 will increment y unconditionally.
2803
2804 BODY_LOC is the location of the first token in the body after labels
2805 have been parsed, NEXT_LOC is the location of the next token after the
2806 body of the conditional has been parsed, and GUARD_LOC is the location
2807 of the conditional. */
2808
2809 void
2810 warn_for_multistatement_macros (location_t body_loc, location_t next_loc,
2811 location_t guard_loc, enum rid keyword)
2812 {
2813 if (!warn_multistatement_macros)
2814 return;
2815
2816 /* Ain't got time to waste. We only care about macros here. */
2817 if (!from_macro_expansion_at (body_loc)
2818 || !from_macro_expansion_at (next_loc))
2819 return;
2820
2821 /* Let's skip macros defined in system headers. */
2822 if (in_system_header_at (body_loc)
2823 || in_system_header_at (next_loc))
2824 return;
2825
2826 /* Find the actual tokens in the macro definition. BODY_LOC and
2827 NEXT_LOC have to come from the same spelling location, but they
2828 will resolve to different locations in the context of the macro
2829 definition. */
2830 location_t body_loc_exp
2831 = linemap_resolve_location (line_table, body_loc,
2832 LRK_MACRO_DEFINITION_LOCATION, NULL);
2833 location_t next_loc_exp
2834 = linemap_resolve_location (line_table, next_loc,
2835 LRK_MACRO_DEFINITION_LOCATION, NULL);
2836 location_t guard_loc_exp
2837 = linemap_resolve_location (line_table, guard_loc,
2838 LRK_MACRO_DEFINITION_LOCATION, NULL);
2839
2840 /* These are some funky cases we don't want to warn about. */
2841 if (body_loc_exp == guard_loc_exp
2842 || next_loc_exp == guard_loc_exp
2843 || body_loc_exp == next_loc_exp)
2844 return;
2845
2846 /* Find the macro maps for the macro expansions. */
2847 const line_map *body_map = linemap_lookup (line_table, body_loc);
2848 const line_map *next_map = linemap_lookup (line_table, next_loc);
2849 const line_map *guard_map = linemap_lookup (line_table, guard_loc);
2850
2851 /* Now see if the following token (after the body) is coming from the
2852 same macro expansion. If it is, it might be a problem. */
2853 if (body_map != next_map)
2854 return;
2855
2856 /* The conditional itself must not come from the same expansion, because
2857 we don't want to warn about
2858 #define IF if (x) x++; y++
2859 and similar. */
2860 if (guard_map == body_map)
2861 return;
2862
2863 /* Handle the case where NEXT and BODY come from the same expansion while
2864 GUARD doesn't, yet we shouldn't warn. E.g.
2865
2866 #define GUARD if (...)
2867 #define GUARD2 GUARD
2868
2869 and in the definition of another macro:
2870
2871 GUARD2
2872 foo ();
2873 return 1;
2874 */
2875 while (linemap_macro_expansion_map_p (guard_map))
2876 {
2877 const line_map_macro *mm = linemap_check_macro (guard_map);
2878 guard_loc_exp = MACRO_MAP_EXPANSION_POINT_LOCATION (mm);
2879 guard_map = linemap_lookup (line_table, guard_loc_exp);
2880 if (guard_map == body_map)
2881 return;
2882 }
2883
2884 auto_diagnostic_group d;
2885 if (warning_at (body_loc, OPT_Wmultistatement_macros,
2886 "macro expands to multiple statements"))
2887 inform (guard_loc, "some parts of macro expansion are not guarded by "
2888 "this %qs clause", guard_tinfo_to_string (keyword));
2889 }
2890
2891 /* Return struct or union type if the alignment of data memeber, FIELD,
2892 is less than the alignment of TYPE. Otherwise, return NULL_TREE.
2893 If RVALUE is true, only arrays evaluate to pointers. */
2894
2895 static tree
2896 check_alignment_of_packed_member (tree type, tree field, bool rvalue)
2897 {
2898 /* Check alignment of the data member. */
2899 if (TREE_CODE (field) == FIELD_DECL
2900 && (DECL_PACKED (field) || TYPE_PACKED (TREE_TYPE (field)))
2901 /* Ignore FIELDs not laid out yet. */
2902 && DECL_FIELD_OFFSET (field)
2903 && (!rvalue || TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE))
2904 {
2905 /* Check the expected alignment against the field alignment. */
2906 unsigned int type_align = min_align_of_type (type);
2907 tree context = DECL_CONTEXT (field);
2908 unsigned int record_align = min_align_of_type (context);
2909 if (record_align < type_align)
2910 return context;
2911 tree field_off = byte_position (field);
2912 if (!multiple_of_p (TREE_TYPE (field_off), field_off,
2913 size_int (type_align)))
2914 return context;
2915 }
2916
2917 return NULL_TREE;
2918 }
2919
2920 /* Return struct or union type if the right hand value, RHS:
2921 1. Is a pointer value which isn't aligned to a pointer type TYPE.
2922 2. Is an address which takes the unaligned address of packed member
2923 of struct or union when assigning to TYPE.
2924 Otherwise, return NULL_TREE. */
2925
2926 static tree
2927 check_address_or_pointer_of_packed_member (tree type, tree rhs)
2928 {
2929 bool rvalue = true;
2930 bool indirect = false;
2931
2932 if (INDIRECT_REF_P (rhs))
2933 {
2934 rhs = TREE_OPERAND (rhs, 0);
2935 STRIP_NOPS (rhs);
2936 indirect = true;
2937 }
2938
2939 if (ADDR_EXPR_P (rhs))
2940 {
2941 rhs = TREE_OPERAND (rhs, 0);
2942 rvalue = indirect;
2943 }
2944
2945 if (!POINTER_TYPE_P (type))
2946 return NULL_TREE;
2947
2948 type = TREE_TYPE (type);
2949
2950 if (TREE_CODE (rhs) == PARM_DECL
2951 || VAR_P (rhs)
2952 || TREE_CODE (rhs) == CALL_EXPR)
2953 {
2954 tree rhstype = TREE_TYPE (rhs);
2955 if (TREE_CODE (rhs) == CALL_EXPR)
2956 {
2957 rhs = CALL_EXPR_FN (rhs); /* Pointer expression. */
2958 if (rhs == NULL_TREE)
2959 return NULL_TREE;
2960 rhs = TREE_TYPE (rhs); /* Pointer type. */
2961 rhs = TREE_TYPE (rhs); /* Function type. */
2962 rhstype = TREE_TYPE (rhs);
2963 if (!rhstype || !POINTER_TYPE_P (rhstype))
2964 return NULL_TREE;
2965 rvalue = true;
2966 }
2967 if (rvalue && POINTER_TYPE_P (rhstype))
2968 rhstype = TREE_TYPE (rhstype);
2969 while (TREE_CODE (rhstype) == ARRAY_TYPE)
2970 rhstype = TREE_TYPE (rhstype);
2971 if (TYPE_PACKED (rhstype))
2972 {
2973 unsigned int type_align = min_align_of_type (type);
2974 unsigned int rhs_align = min_align_of_type (rhstype);
2975 if (rhs_align < type_align)
2976 {
2977 auto_diagnostic_group d;
2978 location_t location = EXPR_LOC_OR_LOC (rhs, input_location);
2979 if (warning_at (location, OPT_Waddress_of_packed_member,
2980 "converting a packed %qT pointer (alignment %d) "
2981 "to a %qT pointer (alignment %d) may result in "
2982 "an unaligned pointer value",
2983 rhstype, rhs_align, type, type_align))
2984 {
2985 tree decl = TYPE_STUB_DECL (rhstype);
2986 if (decl)
2987 inform (DECL_SOURCE_LOCATION (decl), "defined here");
2988 decl = TYPE_STUB_DECL (type);
2989 if (decl)
2990 inform (DECL_SOURCE_LOCATION (decl), "defined here");
2991 }
2992 }
2993 }
2994 return NULL_TREE;
2995 }
2996
2997 tree context = NULL_TREE;
2998
2999 /* Check alignment of the object. */
3000 while (handled_component_p (rhs))
3001 {
3002 if (TREE_CODE (rhs) == COMPONENT_REF)
3003 {
3004 tree field = TREE_OPERAND (rhs, 1);
3005 context = check_alignment_of_packed_member (type, field, rvalue);
3006 if (context)
3007 break;
3008 }
3009 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE)
3010 rvalue = false;
3011 if (rvalue)
3012 return NULL_TREE;
3013 rhs = TREE_OPERAND (rhs, 0);
3014 }
3015
3016 return context;
3017 }
3018
3019 /* Check and warn if the right hand value, RHS:
3020 1. Is a pointer value which isn't aligned to a pointer type TYPE.
3021 2. Is an address which takes the unaligned address of packed member
3022 of struct or union when assigning to TYPE.
3023 */
3024
3025 static void
3026 check_and_warn_address_or_pointer_of_packed_member (tree type, tree rhs)
3027 {
3028 bool nop_p = false;
3029 tree orig_rhs;
3030
3031 do
3032 {
3033 while (TREE_CODE (rhs) == COMPOUND_EXPR)
3034 rhs = TREE_OPERAND (rhs, 1);
3035 orig_rhs = rhs;
3036 STRIP_NOPS (rhs);
3037 nop_p |= orig_rhs != rhs;
3038 }
3039 while (orig_rhs != rhs);
3040
3041 if (TREE_CODE (rhs) == COND_EXPR)
3042 {
3043 /* Check the THEN path. */
3044 check_and_warn_address_or_pointer_of_packed_member
3045 (type, TREE_OPERAND (rhs, 1));
3046
3047 /* Check the ELSE path. */
3048 check_and_warn_address_or_pointer_of_packed_member
3049 (type, TREE_OPERAND (rhs, 2));
3050 }
3051 else
3052 {
3053 if (nop_p)
3054 {
3055 switch (TREE_CODE (rhs))
3056 {
3057 CASE_ADDR_EXPR:
3058 /* Address is taken. */
3059 case PARM_DECL:
3060 case VAR_DECL:
3061 /* Pointer conversion. */
3062 break;
3063 case CALL_EXPR:
3064 /* Function call. */
3065 break;
3066 default:
3067 return;
3068 }
3069 }
3070
3071 tree context
3072 = check_address_or_pointer_of_packed_member (type, rhs);
3073 if (context)
3074 {
3075 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
3076 warning_at (loc, OPT_Waddress_of_packed_member,
3077 "taking address of packed member of %qT may result "
3078 "in an unaligned pointer value",
3079 context);
3080 }
3081 }
3082 }
3083
3084 /* Warn if the right hand value, RHS:
3085 1. Is a pointer value which isn't aligned to a pointer type TYPE.
3086 2. Is an address which takes the unaligned address of packed member
3087 of struct or union when assigning to TYPE.
3088 */
3089
3090 void
3091 warn_for_address_or_pointer_of_packed_member (tree type, tree rhs)
3092 {
3093 if (!warn_address_of_packed_member)
3094 return;
3095
3096 /* Don't warn if we don't assign RHS to a pointer. */
3097 if (!POINTER_TYPE_P (type))
3098 return;
3099
3100 check_and_warn_address_or_pointer_of_packed_member (type, rhs);
3101 }
This page took 0.181882 seconds and 5 git commands to generate.