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