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