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