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