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