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