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