LCOV - code coverage report
Current view: top level - gcc/c-family - c-warn.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 95.4 % 1724 1644
Test Date: 2024-04-13 14:00:49 Functions: 98.3 % 59 58
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Diagnostic routines shared by all languages that are variants of C.
       2                 :             :    Copyright (C) 1992-2024 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                 :             : #define INCLUDE_STRING
      21                 :             : #include "config.h"
      22                 :             : #include "system.h"
      23                 :             : #include "coretypes.h"
      24                 :             : #include "target.h"
      25                 :             : #include "function.h"
      26                 :             : #include "tree.h"
      27                 :             : #include "c-common.h"
      28                 :             : #include "memmodel.h"
      29                 :             : #include "tm_p.h"
      30                 :             : #include "diagnostic.h"
      31                 :             : #include "intl.h"
      32                 :             : #include "stringpool.h"
      33                 :             : #include "attribs.h"
      34                 :             : #include "asan.h"
      35                 :             : #include "gcc-rich-location.h"
      36                 :             : #include "gimplify.h"
      37                 :             : #include "c-family/c-indentation.h"
      38                 :             : #include "c-family/c-spellcheck.h"
      39                 :             : #include "calls.h"
      40                 :             : #include "stor-layout.h"
      41                 :             : #include "tree-pretty-print.h"
      42                 :             : #include "langhooks.h"
      43                 :             : 
      44                 :             : /* Print a warning if a constant expression had overflow in folding.
      45                 :             :    Invoke this function on every expression that the language
      46                 :             :    requires to be a constant expression.
      47                 :             :    Note the ANSI C standard says it is erroneous for a
      48                 :             :    constant expression to overflow.  */
      49                 :             : 
      50                 :             : void
      51                 :    10635323 : constant_expression_warning (tree value)
      52                 :             : {
      53                 :    10634644 :   if (warn_overflow && pedantic
      54                 :       80829 :       && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
      55                 :        3517 :           || TREE_CODE (value) == FIXED_CST
      56                 :        3517 :           || TREE_CODE (value) == VECTOR_CST
      57                 :        3514 :           || TREE_CODE (value) == COMPLEX_CST)
      58                 :    10714435 :       && TREE_OVERFLOW (value))
      59                 :          47 :     pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
      60                 :    10635323 : }
      61                 :             : 
      62                 :             : /* The same as above but print an unconditional error.  */
      63                 :             : 
      64                 :             : void
      65                 :           0 : constant_expression_error (tree value)
      66                 :             : {
      67                 :           0 :   if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
      68                 :           0 :        || TREE_CODE (value) == FIXED_CST
      69                 :           0 :        || TREE_CODE (value) == VECTOR_CST
      70                 :           0 :        || TREE_CODE (value) == COMPLEX_CST)
      71                 :           0 :       && TREE_OVERFLOW (value))
      72                 :           0 :     error ("overflow in constant expression");
      73                 :           0 : }
      74                 :             : 
      75                 :             : /* Print a warning if an expression result VALUE had an overflow
      76                 :             :    in folding and its operands hadn't.  EXPR, which may be null, is
      77                 :             :    the operand of the expression.
      78                 :             : 
      79                 :             :    Invoke this function on every expression that
      80                 :             :    (1) appears in the source code, and
      81                 :             :    (2) is a constant expression that overflowed, and
      82                 :             :    (3) is not already checked by convert_and_check;
      83                 :             :    however, do not invoke this function on operands of explicit casts
      84                 :             :    or when the expression is the result of an operator and any operand
      85                 :             :    already overflowed.  */
      86                 :             : 
      87                 :             : void
      88                 :         507 : overflow_warning (location_t loc, tree value, tree expr)
      89                 :             : {
      90                 :         507 :   if (c_inhibit_evaluation_warnings != 0)
      91                 :             :     return;
      92                 :             : 
      93                 :         440 :   const char *warnfmt = NULL;
      94                 :             : 
      95                 :         440 :   switch (TREE_CODE (value))
      96                 :             :     {
      97                 :         440 :     case INTEGER_CST:
      98                 :         440 :       warnfmt = (expr
      99                 :         440 :                  ? G_("integer overflow in expression %qE of type %qT "
     100                 :             :                       "results in %qE")
     101                 :             :                  : G_("integer overflow in expression of type %qT "
     102                 :             :                       "results in %qE"));
     103                 :             :       break;
     104                 :             : 
     105                 :           0 :     case REAL_CST:
     106                 :           0 :       warnfmt = (expr
     107                 :           0 :                  ? G_("floating point overflow in expression %qE "
     108                 :             :                       "of type %qT results in %qE")
     109                 :             :                  : G_("floating point overflow in expression of type %qT "
     110                 :             :                       "results in %qE"));
     111                 :             :       break;
     112                 :             : 
     113                 :           0 :     case FIXED_CST:
     114                 :           0 :       warnfmt = (expr
     115                 :           0 :                  ? G_("fixed-point overflow in expression %qE of type %qT "
     116                 :             :                       "results in %qE")
     117                 :             :                  : G_("fixed-point overflow in expression of type %qT "
     118                 :             :                       "results in %qE"));
     119                 :             :       break;
     120                 :             : 
     121                 :           0 :     case VECTOR_CST:
     122                 :           0 :       warnfmt = (expr
     123                 :           0 :                  ? G_("vector overflow in expression %qE of type %qT "
     124                 :             :                       "results in %qE")
     125                 :             :                  : G_("vector overflow in expression of type %qT "
     126                 :             :                       "results in %qE"));
     127                 :             :       break;
     128                 :             : 
     129                 :           0 :     case COMPLEX_CST:
     130                 :           0 :       if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
     131                 :           0 :         warnfmt = (expr
     132                 :           0 :                    ? G_("complex integer overflow in expression %qE "
     133                 :             :                         "of type %qT results in %qE")
     134                 :             :                    : G_("complex integer overflow in expression of type %qT "
     135                 :             :                         "results in %qE"));
     136                 :           0 :       else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
     137                 :           0 :         warnfmt = (expr
     138                 :           0 :                    ? G_("complex floating point overflow in expression %qE "
     139                 :             :                         "of type %qT results in %qE")
     140                 :             :                    : G_("complex floating point overflow in expression "
     141                 :             :                         "of type %qT results in %qE"));
     142                 :             :       else
     143                 :             :         return;
     144                 :             :       break;
     145                 :             : 
     146                 :             :     default:
     147                 :             :       return;
     148                 :             :     }
     149                 :             : 
     150                 :             :   bool warned;
     151                 :             :   if (expr)
     152                 :          14 :     warned = warning_at (loc, OPT_Woverflow, warnfmt, expr, TREE_TYPE (expr),
     153                 :             :                          value);
     154                 :             :   else
     155                 :         426 :     warned = warning_at (loc, OPT_Woverflow, warnfmt, TREE_TYPE (value),
     156                 :             :                          value);
     157                 :             : 
     158                 :         439 :   if (warned)
     159                 :         258 :     suppress_warning (value, OPT_Woverflow);
     160                 :             : }
     161                 :             : 
     162                 :             : /* Helper function for walk_tree.  Unwrap C_MAYBE_CONST_EXPRs in an expression
     163                 :             :    pointed to by TP.  */
     164                 :             : 
     165                 :             : static tree
     166                 :        4002 : unwrap_c_maybe_const (tree *tp, int *walk_subtrees, void *)
     167                 :             : {
     168                 :        4002 :   if (TREE_CODE (*tp) == C_MAYBE_CONST_EXPR)
     169                 :             :     {
     170                 :         207 :       *tp = C_MAYBE_CONST_EXPR_EXPR (*tp);
     171                 :             :       /* C_MAYBE_CONST_EXPRs don't nest.  */
     172                 :         207 :       *walk_subtrees = false;
     173                 :             :     }
     174                 :        4002 :   return NULL_TREE;
     175                 :             : }
     176                 :             : 
     177                 :             : /* Warn about uses of logical || / && operator in a context where it
     178                 :             :    is likely that the bitwise equivalent was intended by the
     179                 :             :    programmer.  We have seen an expression in which CODE is a binary
     180                 :             :    operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
     181                 :             :    had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE.  */
     182                 :             : 
     183                 :             : void
     184                 :     4001995 : warn_logical_operator (location_t location, enum tree_code code, tree type,
     185                 :             :                        enum tree_code code_left, tree op_left,
     186                 :             :                        enum tree_code ARG_UNUSED (code_right), tree op_right)
     187                 :             : {
     188                 :     4001995 :   int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
     189                 :     4001995 :   int in0_p, in1_p, in_p;
     190                 :     4001995 :   tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
     191                 :     4001995 :   bool strict_overflow_p = false;
     192                 :             : 
     193                 :     4001995 :   if (!warn_logical_op)
     194                 :     4001367 :     return;
     195                 :             : 
     196                 :        1018 :   if (code != TRUTH_ANDIF_EXPR
     197                 :        1018 :       && code != TRUTH_AND_EXPR
     198                 :         534 :       && code != TRUTH_ORIF_EXPR
     199                 :         534 :       && code != TRUTH_OR_EXPR)
     200                 :             :     return;
     201                 :             : 
     202                 :             :   /* We don't want to warn if either operand comes from a macro
     203                 :             :      expansion.  ??? This doesn't work with e.g. NEGATE_EXPR yet;
     204                 :             :      see PR61534.  */
     205                 :         861 :   if (from_macro_expansion_at (EXPR_LOCATION (op_left))
     206                 :        1717 :       || from_macro_expansion_at (EXPR_LOCATION (op_right)))
     207                 :           5 :     return;
     208                 :             : 
     209                 :             :   /* Warn if &&/|| are being used in a context where it is
     210                 :             :      likely that the bitwise equivalent was intended by the
     211                 :             :      programmer. That is, an expression such as op && MASK
     212                 :             :      where op should not be any boolean expression, nor a
     213                 :             :      constant, and mask seems to be a non-boolean integer constant.  */
     214                 :         856 :   STRIP_ANY_LOCATION_WRAPPER (op_right);
     215                 :         856 :   if (TREE_CODE (op_right) == CONST_DECL)
     216                 :             :     /* An enumerator counts as a constant.  */
     217                 :          24 :     op_right = DECL_INITIAL (op_right);
     218                 :         856 :   tree stripped_op_left = tree_strip_any_location_wrapper (op_left);
     219                 :         856 :   if (!truth_value_p (code_left)
     220                 :         465 :       && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
     221                 :         457 :       && !CONSTANT_CLASS_P (stripped_op_left)
     222                 :         351 :       && TREE_CODE (stripped_op_left) != CONST_DECL
     223                 :         327 :       && !warning_suppressed_p (op_left, OPT_Wlogical_op)
     224                 :         303 :       && TREE_CODE (op_right) == INTEGER_CST
     225                 :          24 :       && !integer_zerop (op_right)
     226                 :         880 :       && !integer_onep (op_right))
     227                 :             :     {
     228                 :          24 :       bool warned;
     229                 :          24 :       if (or_op)
     230                 :          12 :         warned
     231                 :          12 :           = warning_at (location, OPT_Wlogical_op,
     232                 :             :                         "logical %<or%> applied to non-boolean constant");
     233                 :             :       else
     234                 :          12 :         warned
     235                 :          12 :           = warning_at (location, OPT_Wlogical_op,
     236                 :             :                         "logical %<and%> applied to non-boolean constant");
     237                 :          24 :       if (warned)
     238                 :          24 :         suppress_warning (op_left, OPT_Wlogical_op);
     239                 :          24 :       return;
     240                 :             :     }
     241                 :             : 
     242                 :             :   /* We do not warn for constants because they are typical of macro
     243                 :             :      expansions that test for features.  */
     244                 :         832 :   if (CONSTANT_CLASS_P (fold_for_warn (op_left))
     245                 :         832 :       || CONSTANT_CLASS_P (fold_for_warn (op_right)))
     246                 :         192 :     return;
     247                 :             : 
     248                 :             :   /* This warning only makes sense with logical operands.  */
     249                 :         880 :   if (!(truth_value_p (TREE_CODE (op_left))
     250                 :         240 :         || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
     251                 :         872 :       || !(truth_value_p (TREE_CODE (op_right))
     252                 :         285 :            || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
     253                 :             :     return;
     254                 :             : 
     255                 :             :   /* The range computations only work with scalars.  */
     256                 :         632 :   if (VECTOR_TYPE_P (TREE_TYPE (op_left))
     257                 :         632 :       || VECTOR_TYPE_P (TREE_TYPE (op_right)))
     258                 :             :     return;
     259                 :             : 
     260                 :             :   /* We first test whether either side separately is trivially true
     261                 :             :      (with OR) or trivially false (with AND).  If so, do not warn.
     262                 :             :      This is a common idiom for testing ranges of data types in
     263                 :             :      portable code.  */
     264                 :         632 :   op_left = unshare_expr (op_left);
     265                 :         632 :   walk_tree_without_duplicates (&op_left, unwrap_c_maybe_const, NULL);
     266                 :         632 :   lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
     267                 :         632 :   if (!lhs)
     268                 :             :     return;
     269                 :             : 
     270                 :             :   /* If this is an OR operation, invert both sides; now, the result
     271                 :             :      should be always false to get a warning.  */
     272                 :         630 :   if (or_op)
     273                 :         277 :     in0_p = !in0_p;
     274                 :             : 
     275                 :         630 :   tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
     276                 :         630 :   if (tem && integer_zerop (tem))
     277                 :             :     return;
     278                 :             : 
     279                 :         628 :   op_right = unshare_expr (op_right);
     280                 :         628 :   walk_tree_without_duplicates (&op_right, unwrap_c_maybe_const, NULL);
     281                 :         628 :   rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
     282                 :         628 :   if (!rhs)
     283                 :             :     return;
     284                 :             : 
     285                 :             :   /* If this is an OR operation, invert both sides; now, the result
     286                 :             :      should be always false to get a warning.  */
     287                 :         628 :   if (or_op)
     288                 :         277 :     in1_p = !in1_p;
     289                 :             : 
     290                 :         628 :   tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
     291                 :         628 :   if (tem && integer_zerop (tem))
     292                 :             :     return;
     293                 :             : 
     294                 :             :   /* If both expressions have the same operand, if we can merge the
     295                 :             :      ranges, ...  */
     296                 :         628 :   if (operand_equal_p (lhs, rhs, 0)
     297                 :         628 :       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
     298                 :             :                        in1_p, low1, high1))
     299                 :             :     {
     300                 :         437 :       tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
     301                 :             :       /* ... and if the range test is always false, then warn.  */
     302                 :         437 :       if (tem && integer_zerop (tem))
     303                 :             :         {
     304                 :         183 :           if (or_op)
     305                 :          61 :             warning_at (location, OPT_Wlogical_op,
     306                 :             :                         "logical %<or%> of collectively exhaustive tests is "
     307                 :             :                         "always true");
     308                 :             :           else
     309                 :         122 :             warning_at (location, OPT_Wlogical_op,
     310                 :             :                         "logical %<and%> of mutually exclusive tests is "
     311                 :             :                         "always false");
     312                 :             :         }
     313                 :             :       /* Or warn if the operands have exactly the same range, e.g.
     314                 :             :          A > 0 && A > 0.  */
     315                 :         254 :       else if (tree_int_cst_equal (low0, low1)
     316                 :         254 :                && tree_int_cst_equal (high0, high1))
     317                 :             :         {
     318                 :         230 :           if (or_op)
     319                 :         115 :             warning_at (location, OPT_Wlogical_op,
     320                 :             :                         "logical %<or%> of equal expressions");
     321                 :             :           else
     322                 :         115 :             warning_at (location, OPT_Wlogical_op,
     323                 :             :                         "logical %<and%> of equal expressions");
     324                 :             :         }
     325                 :             :     }
     326                 :             : }
     327                 :             : 
     328                 :             : /* Helper function for warn_tautological_cmp.  Look for ARRAY_REFs
     329                 :             :    with constant indices.  */
     330                 :             : 
     331                 :             : static tree
     332                 :         286 : find_array_ref_with_const_idx_r (tree *expr_p, int *, void *)
     333                 :             : {
     334                 :         286 :   tree expr = *expr_p;
     335                 :             : 
     336                 :         286 :   if ((TREE_CODE (expr) == ARRAY_REF
     337                 :         286 :        || TREE_CODE (expr) == ARRAY_RANGE_REF)
     338                 :         286 :       && (TREE_CODE (fold_for_warn (TREE_OPERAND (expr, 1)))
     339                 :             :           == INTEGER_CST))
     340                 :          40 :     return integer_type_node;
     341                 :             : 
     342                 :             :   return NULL_TREE;
     343                 :             : }
     344                 :             : 
     345                 :             : /* Subroutine of warn_tautological_cmp.  Warn about bitwise comparison
     346                 :             :    that always evaluate to true or false.  LOC is the location of the
     347                 :             :    ==/!= comparison specified by CODE; LHS and RHS are the usual operands
     348                 :             :    of this comparison.  */
     349                 :             : 
     350                 :             : static void
     351                 :      424720 : warn_tautological_bitwise_comparison (const op_location_t &loc, tree_code code,
     352                 :             :                                       tree lhs, tree rhs)
     353                 :             : {
     354                 :      424720 :   if (code != EQ_EXPR && code != NE_EXPR)
     355                 :      424545 :     return;
     356                 :             : 
     357                 :             :   /* Extract the operands from e.g. (x & 8) == 4.  */
     358                 :      227792 :   tree bitop;
     359                 :      227792 :   tree cst;
     360                 :      227792 :   tree stripped_lhs = tree_strip_any_location_wrapper (lhs);
     361                 :      227792 :   tree stripped_rhs = tree_strip_any_location_wrapper (rhs);
     362                 :      227792 :   if ((TREE_CODE (lhs) == BIT_AND_EXPR
     363                 :      215559 :        || TREE_CODE (lhs) == BIT_IOR_EXPR)
     364                 :       12298 :       && TREE_CODE (stripped_rhs) == INTEGER_CST)
     365                 :             :     bitop = lhs, cst = stripped_rhs;
     366                 :      215956 :   else if ((TREE_CODE (rhs) == BIT_AND_EXPR
     367                 :      215797 :             || TREE_CODE (rhs) == BIT_IOR_EXPR)
     368                 :         201 :            && TREE_CODE (stripped_lhs) == INTEGER_CST)
     369                 :             :     bitop = rhs, cst = stripped_lhs;
     370                 :             :   else
     371                 :             :     return;
     372                 :             : 
     373                 :       11920 :   tree bitopcst;
     374                 :       11920 :   tree bitop_op0 = fold_for_warn (TREE_OPERAND (bitop, 0));
     375                 :       11920 :   if (TREE_CODE (bitop_op0) == INTEGER_CST)
     376                 :             :     bitopcst = bitop_op0;
     377                 :             :   else {
     378                 :       11816 :     tree bitop_op1 = fold_for_warn (TREE_OPERAND (bitop, 1));
     379                 :       11816 :     if (TREE_CODE (bitop_op1) == INTEGER_CST)
     380                 :             :       bitopcst = bitop_op1;
     381                 :             :     else
     382                 :             :       return;
     383                 :             :   }
     384                 :             : 
     385                 :             :   /* Note that the two operands are from before the usual integer
     386                 :             :      conversions, so their types might not be the same.
     387                 :             :      Use the larger of the two precisions and ignore bits outside
     388                 :             :      of that.  */
     389                 :       10326 :   int prec = MAX (TYPE_PRECISION (TREE_TYPE (cst)),
     390                 :             :                   TYPE_PRECISION (TREE_TYPE (bitopcst)));
     391                 :             : 
     392                 :       10326 :   wide_int bitopcstw = wi::to_wide (bitopcst, prec);
     393                 :       10326 :   wide_int cstw = wi::to_wide (cst, prec);
     394                 :             : 
     395                 :       10326 :   wide_int res;
     396                 :       10326 :   if (TREE_CODE (bitop) == BIT_AND_EXPR)
     397                 :       10236 :     res = bitopcstw & cstw;
     398                 :             :   else
     399                 :          90 :     res = bitopcstw | cstw;
     400                 :             : 
     401                 :             :   /* For BIT_AND only warn if (CST2 & CST1) != CST1, and
     402                 :             :      for BIT_OR only if (CST2 | CST1) != CST1.  */
     403                 :       10326 :   if (res == cstw)
     404                 :       10151 :     return;
     405                 :             : 
     406                 :         175 :   binary_op_rich_location richloc (loc, lhs, rhs, false);
     407                 :         175 :   if (code == EQ_EXPR)
     408                 :          95 :     warning_at (&richloc, OPT_Wtautological_compare,
     409                 :             :                 "bitwise comparison always evaluates to false");
     410                 :             :   else
     411                 :          80 :     warning_at (&richloc, OPT_Wtautological_compare,
     412                 :             :                 "bitwise comparison always evaluates to true");
     413                 :       10326 : }
     414                 :             : 
     415                 :             : /* Given LOC from a macro expansion, return the map for the outermost
     416                 :             :    macro in the nest of expansions.  */
     417                 :             : 
     418                 :             : static const line_map_macro *
     419                 :        1072 : get_outermost_macro_expansion (location_t loc)
     420                 :             : {
     421                 :        1072 :   gcc_assert (from_macro_expansion_at (loc));
     422                 :             : 
     423                 :        1072 :   const line_map *map = linemap_lookup (line_table, loc);
     424                 :        1528 :   const line_map_macro *macro_map;
     425                 :        1528 :   do
     426                 :             :     {
     427                 :        1528 :       macro_map = linemap_check_macro (map);
     428                 :        1528 :       loc = linemap_unwind_toward_expansion (line_table, loc, &map);
     429                 :        1528 :     } while (linemap_macro_expansion_map_p (map));
     430                 :             : 
     431                 :        1072 :   return macro_map;
     432                 :             : }
     433                 :             : 
     434                 :             : /* Given LOC_A and LOC_B from macro expansions, return true if
     435                 :             :    they are "spelled the same" i.e. if they are both directly from
     436                 :             :    expansion of the same non-function-like macro.  */
     437                 :             : 
     438                 :             : static bool
     439                 :         536 : spelled_the_same_p (location_t loc_a, location_t loc_b)
     440                 :             : {
     441                 :         536 :   gcc_assert (from_macro_expansion_at (loc_a));
     442                 :         536 :   gcc_assert (from_macro_expansion_at (loc_b));
     443                 :             : 
     444                 :         536 :   const line_map_macro *map_a = get_outermost_macro_expansion (loc_a);
     445                 :         536 :   const line_map_macro *map_b = get_outermost_macro_expansion (loc_b);
     446                 :             : 
     447                 :         536 :   if (map_a->macro == map_b->macro)
     448                 :          64 :     if (!cpp_fun_like_macro_p (map_a->macro))
     449                 :          24 :       return true;
     450                 :             : 
     451                 :             :   return false;
     452                 :             : }
     453                 :             : 
     454                 :             : /* Warn if a self-comparison always evaluates to true or false.  LOC
     455                 :             :    is the location of the comparison with code CODE, LHS and RHS are
     456                 :             :    operands of the comparison.  */
     457                 :             : 
     458                 :             : void
     459                 :     2310199 : warn_tautological_cmp (const op_location_t &loc, enum tree_code code,
     460                 :             :                        tree lhs, tree rhs)
     461                 :             : {
     462                 :     2310199 :   if (TREE_CODE_CLASS (code) != tcc_comparison)
     463                 :             :     return;
     464                 :             : 
     465                 :             :   /* Don't warn for various macro expansions.  */
     466                 :      702649 :   if (from_macro_expansion_at (loc))
     467                 :             :     return;
     468                 :      440356 :   bool lhs_in_macro = from_macro_expansion_at (EXPR_LOCATION (lhs));
     469                 :      440356 :   bool rhs_in_macro = from_macro_expansion_at (EXPR_LOCATION (rhs));
     470                 :      440356 :   if (lhs_in_macro || rhs_in_macro)
     471                 :             :     {
     472                 :             :       /* Don't warn if exactly one is from a macro.  */
     473                 :       15660 :       if (!(lhs_in_macro && rhs_in_macro))
     474                 :             :         return;
     475                 :             : 
     476                 :             :       /* If both are in a macro, only warn if they're spelled the same.  */
     477                 :         536 :       if (!spelled_the_same_p (EXPR_LOCATION (lhs), EXPR_LOCATION (rhs)))
     478                 :             :         return;
     479                 :             :     }
     480                 :             : 
     481                 :      424720 :   warn_tautological_bitwise_comparison (loc, code, lhs, rhs);
     482                 :             : 
     483                 :             :   /* We do not warn for constants because they are typical of macro
     484                 :             :      expansions that test for features, sizeof, and similar.  */
     485                 :      424720 :   if (CONSTANT_CLASS_P (fold_for_warn (lhs))
     486                 :      424720 :       || CONSTANT_CLASS_P (fold_for_warn (rhs)))
     487                 :      248346 :     return;
     488                 :             : 
     489                 :             :   /* Don't warn for e.g.
     490                 :             :      HOST_WIDE_INT n;
     491                 :             :      ...
     492                 :             :      if (n == (long) n) ...
     493                 :             :    */
     494                 :      176374 :   if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR)
     495                 :      172563 :       || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR))
     496                 :             :     return;
     497                 :             : 
     498                 :             :   /* Don't warn if either LHS or RHS has an IEEE floating-point type.
     499                 :             :      It could be a NaN, and NaN never compares equal to anything, even
     500                 :             :      itself.  */
     501                 :      144743 :   if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
     502                 :             :     return;
     503                 :             : 
     504                 :      143398 :   if (operand_equal_p (lhs, rhs, 0))
     505                 :             :     {
     506                 :             :       /* Don't warn about array references with constant indices;
     507                 :             :          these are likely to come from a macro.  */
     508                 :         129 :       if (walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r,
     509                 :             :                                         NULL))
     510                 :          40 :         return;
     511                 :          89 :       const bool always_true = (code == EQ_EXPR || code == LE_EXPR
     512                 :             :                                 || code == GE_EXPR || code == UNLE_EXPR
     513                 :             :                                 || code == UNGE_EXPR || code == UNEQ_EXPR);
     514                 :          89 :       binary_op_rich_location richloc (loc, lhs, rhs, false);
     515                 :          89 :       if (always_true)
     516                 :          61 :         warning_at (&richloc, OPT_Wtautological_compare,
     517                 :             :                     "self-comparison always evaluates to true");
     518                 :             :       else
     519                 :          28 :         warning_at (&richloc, OPT_Wtautological_compare,
     520                 :             :                     "self-comparison always evaluates to false");
     521                 :          89 :     }
     522                 :             : }
     523                 :             : 
     524                 :             : /* Return true iff EXPR only contains boolean operands, or comparisons.  */
     525                 :             : 
     526                 :             : static bool
     527                 :         562 : expr_has_boolean_operands_p (tree expr)
     528                 :             : {
     529                 :         602 :   STRIP_NOPS (expr);
     530                 :             : 
     531                 :         602 :   if (CONVERT_EXPR_P (expr))
     532                 :         125 :     return bool_promoted_to_int_p (expr);
     533                 :         477 :   else if (UNARY_CLASS_P (expr))
     534                 :          40 :     return expr_has_boolean_operands_p (TREE_OPERAND (expr, 0));
     535                 :         437 :   else if (BINARY_CLASS_P (expr))
     536                 :          85 :     return (expr_has_boolean_operands_p (TREE_OPERAND (expr, 0))
     537                 :          85 :             && expr_has_boolean_operands_p (TREE_OPERAND (expr, 1)));
     538                 :         352 :   else if (COMPARISON_CLASS_P (expr))
     539                 :             :     return true;
     540                 :             :   else
     541                 :             :     return false;
     542                 :             : }
     543                 :             : 
     544                 :             : /* Warn about logical not used on the left hand side operand of a comparison.
     545                 :             :    This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
     546                 :             :    Do not warn if RHS is of a boolean type, a logical operator, or
     547                 :             :    a comparison.  */
     548                 :             : 
     549                 :             : void
     550                 :         460 : warn_logical_not_parentheses (location_t location, enum tree_code code,
     551                 :             :                               tree lhs, tree rhs)
     552                 :             : {
     553                 :         460 :   if (TREE_CODE_CLASS (code) != tcc_comparison
     554                 :         460 :       || TREE_TYPE (rhs) == NULL_TREE
     555                 :         460 :       || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE
     556                 :         876 :       || truth_value_p (TREE_CODE (rhs)))
     557                 :         138 :     return;
     558                 :             : 
     559                 :             :   /* Don't warn for expression like !x == ~(bool1 | bool2).  */
     560                 :         407 :   if (expr_has_boolean_operands_p (rhs))
     561                 :             :     return;
     562                 :             : 
     563                 :             :   /* Don't warn for !x == 0 or !y != 0, those are equivalent to
     564                 :             :      !(x == 0) or !(y != 0).  */
     565                 :         342 :   if ((code == EQ_EXPR || code == NE_EXPR)
     566                 :         342 :       && integer_zerop (rhs))
     567                 :             :     return;
     568                 :             : 
     569                 :         322 :   auto_diagnostic_group d;
     570                 :         322 :   if (warning_at (location, OPT_Wlogical_not_parentheses,
     571                 :             :                   "logical not is only applied to the left hand side of "
     572                 :             :                   "comparison")
     573                 :         322 :       && EXPR_HAS_LOCATION (lhs))
     574                 :             :     {
     575                 :         281 :       location_t lhs_loc = EXPR_LOCATION (lhs);
     576                 :         281 :       rich_location richloc (line_table, lhs_loc);
     577                 :         281 :       richloc.add_fixit_insert_before (lhs_loc, "(");
     578                 :         281 :       richloc.add_fixit_insert_after (lhs_loc, ")");
     579                 :         281 :       inform (&richloc, "add parentheses around left hand side "
     580                 :             :               "expression to silence this warning");
     581                 :         281 :     }
     582                 :         322 : }
     583                 :             : 
     584                 :             : /* Warn if EXP contains any computations whose results are not used.
     585                 :             :    Return true if a warning is printed; false otherwise.  LOCUS is the
     586                 :             :    (potential) location of the expression.  */
     587                 :             : 
     588                 :             : bool
     589                 :     1584971 : warn_if_unused_value (const_tree exp, location_t locus, bool quiet)
     590                 :             : {
     591                 :     1584993 :  restart:
     592                 :     1584993 :   if (TREE_USED (exp) || warning_suppressed_p (exp, OPT_Wunused_value))
     593                 :        5772 :     return false;
     594                 :             : 
     595                 :             :   /* Don't warn about void constructs.  This includes casting to void,
     596                 :             :      void function calls, and statement expressions with a final cast
     597                 :             :      to void.  */
     598                 :     1579221 :   if (VOID_TYPE_P (TREE_TYPE (exp)))
     599                 :             :     return false;
     600                 :             : 
     601                 :      921078 :   if (EXPR_HAS_LOCATION (exp))
     602                 :      921027 :     locus = EXPR_LOCATION (exp);
     603                 :             : 
     604                 :      921078 :   switch (TREE_CODE (exp))
     605                 :             :     {
     606                 :             :     case PREINCREMENT_EXPR:
     607                 :             :     case POSTINCREMENT_EXPR:
     608                 :             :     case PREDECREMENT_EXPR:
     609                 :             :     case POSTDECREMENT_EXPR:
     610                 :             :     case MODIFY_EXPR:
     611                 :             :     case INIT_EXPR:
     612                 :             :     case TARGET_EXPR:
     613                 :             :     case CALL_EXPR:
     614                 :             :     case TRY_CATCH_EXPR:
     615                 :             :     case EXIT_EXPR:
     616                 :             :     case VA_ARG_EXPR:
     617                 :             :       return false;
     618                 :             : 
     619                 :           0 :     case BIND_EXPR:
     620                 :             :       /* For a binding, warn if no side effect within it.  */
     621                 :           0 :       exp = BIND_EXPR_BODY (exp);
     622                 :           0 :       goto restart;
     623                 :             : 
     624                 :           5 :     case SAVE_EXPR:
     625                 :           5 :     case NON_LVALUE_EXPR:
     626                 :           5 :     case NOP_EXPR:
     627                 :           5 :       exp = TREE_OPERAND (exp, 0);
     628                 :           5 :       goto restart;
     629                 :             : 
     630                 :          17 :     case TRUTH_ORIF_EXPR:
     631                 :          17 :     case TRUTH_ANDIF_EXPR:
     632                 :             :       /* In && or ||, warn if 2nd operand has no side effect.  */
     633                 :          17 :       exp = TREE_OPERAND (exp, 1);
     634                 :          17 :       goto restart;
     635                 :             : 
     636                 :          17 :     case COMPOUND_EXPR:
     637                 :          17 :       if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus, quiet))
     638                 :             :         return true;
     639                 :             :       /* Let people do `(foo (), 0)' without a warning.  */
     640                 :          17 :       if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
     641                 :             :         return false;
     642                 :           0 :       exp = TREE_OPERAND (exp, 1);
     643                 :           0 :       goto restart;
     644                 :             : 
     645                 :          64 :     case COND_EXPR:
     646                 :             :       /* If this is an expression with side effects, don't warn; this
     647                 :             :          case commonly appears in macro expansions.  */
     648                 :          64 :       if (TREE_SIDE_EFFECTS (exp))
     649                 :             :         return false;
     650                 :           0 :       goto warn;
     651                 :             : 
     652                 :          45 :     case COMPLEX_EXPR:
     653                 :             :       /* Warn only if both operands are unused.  */
     654                 :          45 :       if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus, true)
     655                 :          45 :           && warn_if_unused_value (TREE_OPERAND (exp, 1), locus, true))
     656                 :          20 :         goto warn;
     657                 :             :       return false;
     658                 :             : 
     659                 :           2 :     case INDIRECT_REF:
     660                 :             :       /* Don't warn about automatic dereferencing of references, since
     661                 :             :          the user cannot control it.  */
     662                 :           2 :       if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
     663                 :             :         {
     664                 :           0 :           exp = TREE_OPERAND (exp, 0);
     665                 :           0 :           goto restart;
     666                 :             :         }
     667                 :             :       /* Fall through.  */
     668                 :             : 
     669                 :         122 :     default:
     670                 :             :       /* Referencing a volatile value is a side effect, so don't warn.  */
     671                 :         122 :       if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
     672                 :          22 :           && TREE_THIS_VOLATILE (exp))
     673                 :             :         return false;
     674                 :             : 
     675                 :             :       /* If this is an expression which has no operands, there is no value
     676                 :             :          to be unused.  There are no such language-independent codes,
     677                 :             :          but front ends may define such.  */
     678                 :         122 :       if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
     679                 :             :         return false;
     680                 :             : 
     681                 :         142 :     warn:
     682                 :         142 :       if (quiet)
     683                 :             :         return true;
     684                 :         102 :       return warning_at (locus, OPT_Wunused_value, "value computed is not used");
     685                 :             :     }
     686                 :             : }
     687                 :             : 
     688                 :             : /* Print a warning about casts that might indicate violation of strict
     689                 :             :    aliasing rules if -Wstrict-aliasing is used and strict aliasing
     690                 :             :    mode is in effect.  LOC is the location of the expression being
     691                 :             :    cast, EXPR might be from inside it.  TYPE is the type we're casting
     692                 :             :    to.  */
     693                 :             : 
     694                 :             : bool
     695                 :    81909247 : strict_aliasing_warning (location_t loc, tree type, tree expr)
     696                 :             : {
     697                 :    81909247 :   if (loc == UNKNOWN_LOCATION)
     698                 :    52743673 :     loc = input_location;
     699                 :             : 
     700                 :             :   /* Strip pointer conversion chains and get to the correct original type.  */
     701                 :    81909247 :   STRIP_NOPS (expr);
     702                 :    81909247 :   tree otype = TREE_TYPE (expr);
     703                 :             : 
     704                 :    84421262 :   if (!(flag_strict_aliasing
     705                 :    78865316 :         && POINTER_TYPE_P (type)
     706                 :     2772438 :         && POINTER_TYPE_P (otype)
     707                 :     2512015 :         && !VOID_TYPE_P (TREE_TYPE (type)))
     708                 :             :       /* If the type we are casting to is a ref-all pointer
     709                 :             :          dereferencing it is always valid.  */
     710                 :    84402407 :       || TYPE_REF_CAN_ALIAS_ALL (type))
     711                 :             :     return false;
     712                 :             : 
     713                 :      343484 :   if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
     714                 :     2177480 :       && (DECL_P (TREE_OPERAND (expr, 0))
     715                 :       10014 :           || handled_component_p (TREE_OPERAND (expr, 0))))
     716                 :             :     {
     717                 :             :       /* Casting the address of an object to non void pointer. Warn
     718                 :             :          if the cast breaks type based aliasing.  */
     719                 :       41206 :       if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
     720                 :             :         {
     721                 :           1 :           warning_at (loc, OPT_Wstrict_aliasing,
     722                 :             :                       "type-punning to incomplete type "
     723                 :             :                       "might break strict-aliasing rules");
     724                 :           1 :           return true;
     725                 :             :         }
     726                 :             :       else
     727                 :             :         {
     728                 :             :           /* warn_strict_aliasing >= 3.   This includes the default (3).
     729                 :             :              Only warn if the cast is dereferenced immediately.  */
     730                 :       41205 :           alias_set_type set1
     731                 :       41205 :             = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
     732                 :       41205 :           alias_set_type set2 = get_alias_set (TREE_TYPE (type));
     733                 :             : 
     734                 :       41205 :           if (set2 != 0
     735                 :       41205 :               && set1 != set2
     736                 :         709 :               && !alias_set_subset_of (set2, set1)
     737                 :       41266 :               && !alias_sets_conflict_p (set1, set2))
     738                 :             :             {
     739                 :          60 :               warning_at (loc, OPT_Wstrict_aliasing,
     740                 :             :                           "dereferencing type-punned "
     741                 :             :                           "pointer will break strict-aliasing rules");
     742                 :          60 :               return true;
     743                 :             :             }
     744                 :       41145 :           else if (warn_strict_aliasing == 2
     745                 :       41145 :                    && !alias_sets_must_conflict_p (set1, set2))
     746                 :             :             {
     747                 :           1 :               warning_at (loc, OPT_Wstrict_aliasing,
     748                 :             :                           "dereferencing type-punned "
     749                 :             :                           "pointer might break strict-aliasing rules");
     750                 :           1 :               return true;
     751                 :             :             }
     752                 :             :         }
     753                 :             :     }
     754                 :     2092824 :   else if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
     755                 :             :     {
     756                 :             :       /* At this level, warn for any conversions, even if an address is
     757                 :             :          not taken in the same statement.  This will likely produce many
     758                 :             :          false positives, but could be useful to pinpoint problems that
     759                 :             :          are not revealed at higher levels.  */
     760                 :          29 :       alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
     761                 :          29 :       alias_set_type set2 = get_alias_set (TREE_TYPE (type));
     762                 :          29 :       if (!COMPLETE_TYPE_P (TREE_TYPE (type))
     763                 :          29 :           || !alias_sets_must_conflict_p (set1, set2))
     764                 :             :         {
     765                 :          21 :           warning_at (loc, OPT_Wstrict_aliasing,
     766                 :             :                       "dereferencing type-punned "
     767                 :             :                       "pointer might break strict-aliasing rules");
     768                 :          21 :           return true;
     769                 :             :         }
     770                 :             :     }
     771                 :             : 
     772                 :             :   return false;
     773                 :             : }
     774                 :             : 
     775                 :             : /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
     776                 :             :    sizeof as last operand of certain builtins.  */
     777                 :             : 
     778                 :             : void
     779                 :     5010099 : sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
     780                 :             :                                   vec<tree, va_gc> *params, tree *sizeof_arg,
     781                 :             :                                   bool (*comp_types) (tree, tree))
     782                 :             : {
     783                 :     5010099 :   tree type, dest = NULL_TREE, src = NULL_TREE, tem;
     784                 :     5010099 :   bool strop = false, cmp = false;
     785                 :     5010099 :   unsigned int idx = ~0;
     786                 :     5010099 :   location_t loc;
     787                 :             : 
     788                 :     5010099 :   if (TREE_CODE (callee) != FUNCTION_DECL
     789                 :     4867005 :       || !fndecl_built_in_p (callee, BUILT_IN_NORMAL)
     790                 :     5311025 :       || vec_safe_length (params) <= 1)
     791                 :             :     return;
     792                 :             : 
     793                 :      174135 :   enum built_in_function fncode = DECL_FUNCTION_CODE (callee);
     794                 :      174135 :   switch (fncode)
     795                 :             :     {
     796                 :        1099 :     case BUILT_IN_STRNCMP:
     797                 :        1099 :     case BUILT_IN_STRNCASECMP:
     798                 :        1099 :       cmp = true;
     799                 :             :       /* FALLTHRU */
     800                 :             :     case BUILT_IN_STRNCPY:
     801                 :             :     case BUILT_IN_STRNCPY_CHK:
     802                 :             :     case BUILT_IN_STRNCAT:
     803                 :             :     case BUILT_IN_STRNCAT_CHK:
     804                 :             :     case BUILT_IN_STPNCPY:
     805                 :             :     case BUILT_IN_STPNCPY_CHK:
     806                 :             :       strop = true;
     807                 :             :       /* FALLTHRU */
     808                 :       11576 :     case BUILT_IN_MEMCPY:
     809                 :       11576 :     case BUILT_IN_MEMCPY_CHK:
     810                 :       11576 :     case BUILT_IN_MEMMOVE:
     811                 :       11576 :     case BUILT_IN_MEMMOVE_CHK:
     812                 :       11576 :       if (params->length () < 3)
     813                 :             :         return;
     814                 :       11575 :       src = (*params)[1];
     815                 :       11575 :       dest = (*params)[0];
     816                 :       11575 :       idx = 2;
     817                 :       11575 :       break;
     818                 :         146 :     case BUILT_IN_BCOPY:
     819                 :         146 :       if (params->length () < 3)
     820                 :             :         return;
     821                 :         146 :       src = (*params)[0];
     822                 :         146 :       dest = (*params)[1];
     823                 :         146 :       idx = 2;
     824                 :         146 :       break;
     825                 :       10558 :     case BUILT_IN_MEMCMP:
     826                 :       10558 :     case BUILT_IN_BCMP:
     827                 :       10558 :       if (params->length () < 3)
     828                 :             :         return;
     829                 :       10558 :       src = (*params)[1];
     830                 :       10558 :       dest = (*params)[0];
     831                 :       10558 :       idx = 2;
     832                 :       10558 :       cmp = true;
     833                 :       10558 :       break;
     834                 :        8817 :     case BUILT_IN_MEMSET:
     835                 :        8817 :     case BUILT_IN_MEMSET_CHK:
     836                 :        8817 :       if (params->length () < 3)
     837                 :             :         return;
     838                 :        8817 :       dest = (*params)[0];
     839                 :        8817 :       idx = 2;
     840                 :        8817 :       break;
     841                 :          72 :     case BUILT_IN_BZERO:
     842                 :          72 :       dest = (*params)[0];
     843                 :          72 :       idx = 1;
     844                 :          72 :       break;
     845                 :          62 :     case BUILT_IN_STRNDUP:
     846                 :          62 :       src = (*params)[0];
     847                 :          62 :       strop = true;
     848                 :          62 :       idx = 1;
     849                 :          62 :       break;
     850                 :        1142 :     case BUILT_IN_MEMCHR:
     851                 :        1142 :       if (params->length () < 3)
     852                 :             :         return;
     853                 :        1142 :       src = (*params)[0];
     854                 :        1142 :       idx = 2;
     855                 :        1142 :       break;
     856                 :        2406 :     case BUILT_IN_SNPRINTF:
     857                 :        2406 :     case BUILT_IN_SNPRINTF_CHK:
     858                 :        2406 :     case BUILT_IN_VSNPRINTF:
     859                 :        2406 :     case BUILT_IN_VSNPRINTF_CHK:
     860                 :        2406 :       dest = (*params)[0];
     861                 :        2406 :       idx = 1;
     862                 :        2406 :       strop = true;
     863                 :        2406 :       break;
     864                 :             :     default:
     865                 :             :       break;
     866                 :             :     }
     867                 :             : 
     868                 :      174134 :   if (idx >= 3)
     869                 :             :     return;
     870                 :             : 
     871                 :             :   /* Use error_operand_p to detect non-error arguments with an error
     872                 :             :      type that the C++ front-end constructs.  */
     873                 :       34778 :   if (error_operand_p (src)
     874                 :       34777 :       || error_operand_p (dest)
     875                 :       34771 :       || !sizeof_arg[idx]
     876                 :       51800 :       || error_operand_p (sizeof_arg[idx]))
     877                 :             :     return;
     878                 :             : 
     879                 :       34044 :   type = TYPE_P (sizeof_arg[idx])
     880                 :       17022 :          ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
     881                 :             : 
     882                 :       17022 :   if (!POINTER_TYPE_P (type))
     883                 :             :     {
     884                 :             :       /* The argument type may be an array.  Diagnose bounded string
     885                 :             :          copy functions that specify the bound in terms of the source
     886                 :             :          argument rather than the destination unless they are equal
     887                 :             :          to one another.  Handle constant sizes and also try to handle
     888                 :             :          sizeof expressions involving VLAs.  */
     889                 :       14374 :       if (strop && !cmp && fncode != BUILT_IN_STRNDUP && src)
     890                 :             :         {
     891                 :         257 :           tem = tree_strip_nop_conversions (src);
     892                 :         257 :           if (TREE_CODE (tem) == ADDR_EXPR)
     893                 :          40 :             tem = TREE_OPERAND (tem, 0);
     894                 :             : 
     895                 :             :           /* Avoid diagnosing sizeof SRC when SRC is declared with
     896                 :             :              attribute nonstring.  */
     897                 :         257 :           tree dummy;
     898                 :         257 :           if (get_attr_nonstring_decl (tem, &dummy))
     899                 :          15 :             return;
     900                 :             : 
     901                 :         242 :           tree d = tree_strip_nop_conversions (dest);
     902                 :         242 :           if (TREE_CODE (d) == ADDR_EXPR)
     903                 :         133 :             d = TREE_OPERAND (d, 0);
     904                 :             : 
     905                 :         242 :           tree dstsz = TYPE_SIZE_UNIT (TREE_TYPE (d));
     906                 :         242 :           tree srcsz = TYPE_SIZE_UNIT (TREE_TYPE (tem));
     907                 :             : 
     908                 :         242 :           if ((!dstsz
     909                 :         242 :                || !srcsz
     910                 :         238 :                || !operand_equal_p (dstsz, srcsz, OEP_LEXICOGRAPHIC))
     911                 :         475 :               && operand_equal_p (tem, sizeof_arg[idx], OEP_ADDRESS_OF))
     912                 :          95 :             warning_at (sizeof_arg_loc[idx], OPT_Wsizeof_pointer_memaccess,
     913                 :             :                         "argument to %<sizeof%> in %qD call is the same "
     914                 :             :                         "expression as the source; did you mean to use "
     915                 :             :                         "the size of the destination?",
     916                 :             :                         callee);
     917                 :             :         }
     918                 :             : 
     919                 :       14359 :       return;
     920                 :             :     }
     921                 :             : 
     922                 :        2648 :   if (dest
     923                 :        2487 :       && (tem = tree_strip_nop_conversions (dest))
     924                 :        2487 :       && POINTER_TYPE_P (TREE_TYPE (tem))
     925                 :        5135 :       && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
     926                 :             :     return;
     927                 :             : 
     928                 :        2584 :   if (src
     929                 :        2188 :       && (tem = tree_strip_nop_conversions (src))
     930                 :        2188 :       && POINTER_TYPE_P (TREE_TYPE (tem))
     931                 :        4772 :       && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
     932                 :             :     return;
     933                 :             : 
     934                 :        2524 :   loc = sizeof_arg_loc[idx];
     935                 :             : 
     936                 :        2524 :   if (dest && !cmp)
     937                 :             :     {
     938                 :        1909 :       if (!TYPE_P (sizeof_arg[idx])
     939                 :        1429 :           && operand_equal_p (dest, sizeof_arg[idx], 0)
     940                 :        2736 :           && comp_types (TREE_TYPE (dest), type))
     941                 :             :         {
     942                 :         582 :           if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
     943                 :         105 :             warning_at (loc, OPT_Wsizeof_pointer_memaccess,
     944                 :             :                         "argument to %<sizeof%> in %qD call is the same "
     945                 :             :                         "expression as the destination; did you mean to "
     946                 :             :                         "remove the addressof?", callee);
     947                 :         477 :           else if ((TYPE_PRECISION (TREE_TYPE (type))
     948                 :         477 :                     == TYPE_PRECISION (char_type_node))
     949                 :         477 :                    || strop)
     950                 :         162 :             warning_at (loc, OPT_Wsizeof_pointer_memaccess,
     951                 :             :                         "argument to %<sizeof%> in %qD call is the same "
     952                 :             :                         "expression as the destination; did you mean to "
     953                 :             :                         "provide an explicit length?", callee);
     954                 :             :           else
     955                 :         315 :             warning_at (loc, OPT_Wsizeof_pointer_memaccess,
     956                 :             :                         "argument to %<sizeof%> in %qD call is the same "
     957                 :             :                         "expression as the destination; did you mean to "
     958                 :             :                         "dereference it?", callee);
     959                 :         582 :           return;
     960                 :             :         }
     961                 :             : 
     962                 :        1327 :       if (POINTER_TYPE_P (TREE_TYPE (dest))
     963                 :        1327 :           && !strop
     964                 :        1289 :           && comp_types (TREE_TYPE (dest), type)
     965                 :        1607 :           && !VOID_TYPE_P (TREE_TYPE (type)))
     966                 :             :         {
     967                 :         840 :           warning_at (loc, OPT_Wsizeof_pointer_memaccess,
     968                 :             :                       "argument to %<sizeof%> in %qD call is the same "
     969                 :             :                       "pointer type %qT as the destination; expected %qT "
     970                 :         280 :                       "or an explicit length", callee, TREE_TYPE (dest),
     971                 :         280 :                       TREE_TYPE (TREE_TYPE (dest)));
     972                 :         280 :           return;
     973                 :             :         }
     974                 :             :     }
     975                 :             : 
     976                 :        1662 :   if (src && !cmp)
     977                 :             :     {
     978                 :        1112 :       if (!TYPE_P (sizeof_arg[idx])
     979                 :         872 :           && operand_equal_p (src, sizeof_arg[idx], 0)
     980                 :        1772 :           && comp_types (TREE_TYPE (src), type))
     981                 :             :         {
     982                 :         464 :           if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
     983                 :          84 :             warning_at (loc, OPT_Wsizeof_pointer_memaccess,
     984                 :             :                         "argument to %<sizeof%> in %qD call is the same "
     985                 :             :                         "expression as the source; did you mean to "
     986                 :             :                         "remove the addressof?", callee);
     987                 :         380 :           else if ((TYPE_PRECISION (TREE_TYPE (type))
     988                 :         380 :                     == TYPE_PRECISION (char_type_node))
     989                 :         380 :                    || strop)
     990                 :         128 :             warning_at (loc, OPT_Wsizeof_pointer_memaccess,
     991                 :             :                         "argument to %<sizeof%> in %qD call is the same "
     992                 :             :                         "expression as the source; did you mean to "
     993                 :             :                         "provide an explicit length?", callee);
     994                 :             :           else
     995                 :         252 :             warning_at (loc, OPT_Wsizeof_pointer_memaccess,
     996                 :             :                         "argument to %<sizeof%> in %qD call is the same "
     997                 :             :                         "expression as the source; did you mean to "
     998                 :             :                         "dereference it?", callee);
     999                 :         464 :           return;
    1000                 :             :         }
    1001                 :             : 
    1002                 :         648 :       if (POINTER_TYPE_P (TREE_TYPE (src))
    1003                 :         648 :           && !strop
    1004                 :         648 :           && comp_types (TREE_TYPE (src), type)
    1005                 :         876 :           && !VOID_TYPE_P (TREE_TYPE (type)))
    1006                 :             :         {
    1007                 :         672 :           warning_at (loc, OPT_Wsizeof_pointer_memaccess,
    1008                 :             :                       "argument to %<sizeof%> in %qD call is the same "
    1009                 :             :                       "pointer type %qT as the source; expected %qT "
    1010                 :         224 :                       "or an explicit length", callee, TREE_TYPE (src),
    1011                 :         224 :                       TREE_TYPE (TREE_TYPE (src)));
    1012                 :         224 :           return;
    1013                 :             :         }
    1014                 :             :     }
    1015                 :             : 
    1016                 :         974 :   if (dest)
    1017                 :             :     {
    1018                 :         934 :       if (!TYPE_P (sizeof_arg[idx])
    1019                 :         825 :           && operand_equal_p (dest, sizeof_arg[idx], 0)
    1020                 :        1345 :           && comp_types (TREE_TYPE (dest), type))
    1021                 :             :         {
    1022                 :         117 :           if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
    1023                 :          21 :             warning_at (loc, OPT_Wsizeof_pointer_memaccess,
    1024                 :             :                         "argument to %<sizeof%> in %qD call is the same "
    1025                 :             :                         "expression as the first source; did you mean to "
    1026                 :             :                         "remove the addressof?", callee);
    1027                 :          96 :           else if ((TYPE_PRECISION (TREE_TYPE (type))
    1028                 :          96 :                     == TYPE_PRECISION (char_type_node))
    1029                 :          96 :                    || strop)
    1030                 :          33 :             warning_at (loc, OPT_Wsizeof_pointer_memaccess,
    1031                 :             :                         "argument to %<sizeof%> in %qD call is the same "
    1032                 :             :                         "expression as the first source; did you mean to "
    1033                 :             :                         "provide an explicit length?", callee);
    1034                 :             :           else
    1035                 :          63 :             warning_at (loc, OPT_Wsizeof_pointer_memaccess,
    1036                 :             :                         "argument to %<sizeof%> in %qD call is the same "
    1037                 :             :                         "expression as the first source; did you mean to "
    1038                 :             :                         "dereference it?", callee);
    1039                 :         117 :           return;
    1040                 :             :         }
    1041                 :             : 
    1042                 :         817 :       if (POINTER_TYPE_P (TREE_TYPE (dest))
    1043                 :         817 :           && !strop
    1044                 :         805 :           && comp_types (TREE_TYPE (dest), type)
    1045                 :         859 :           && !VOID_TYPE_P (TREE_TYPE (type)))
    1046                 :             :         {
    1047                 :         126 :           warning_at (loc, OPT_Wsizeof_pointer_memaccess,
    1048                 :             :                       "argument to %<sizeof%> in %qD call is the same "
    1049                 :             :                       "pointer type %qT as the first source; expected %qT "
    1050                 :          42 :                       "or an explicit length", callee, TREE_TYPE (dest),
    1051                 :          42 :                       TREE_TYPE (TREE_TYPE (dest)));
    1052                 :          42 :           return;
    1053                 :             :         }
    1054                 :             :     }
    1055                 :             : 
    1056                 :         815 :   if (src)
    1057                 :             :     {
    1058                 :         719 :       if (!TYPE_P (sizeof_arg[idx])
    1059                 :         652 :           && operand_equal_p (src, sizeof_arg[idx], 0)
    1060                 :        1096 :           && comp_types (TREE_TYPE (src), type))
    1061                 :             :         {
    1062                 :         132 :           if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
    1063                 :          24 :             warning_at (loc, OPT_Wsizeof_pointer_memaccess,
    1064                 :             :                         "argument to %<sizeof%> in %qD call is the same "
    1065                 :             :                         "expression as the second source; did you mean to "
    1066                 :             :                         "remove the addressof?", callee);
    1067                 :         108 :           else if ((TYPE_PRECISION (TREE_TYPE (type))
    1068                 :         108 :                     == TYPE_PRECISION (char_type_node))
    1069                 :         108 :                    || strop)
    1070                 :          33 :             warning_at (loc, OPT_Wsizeof_pointer_memaccess,
    1071                 :             :                         "argument to %<sizeof%> in %qD call is the same "
    1072                 :             :                         "expression as the second source; did you mean to "
    1073                 :             :                         "provide an explicit length?", callee);
    1074                 :             :           else
    1075                 :          75 :             warning_at (loc, OPT_Wsizeof_pointer_memaccess,
    1076                 :             :                         "argument to %<sizeof%> in %qD call is the same "
    1077                 :             :                         "expression as the second source; did you mean to "
    1078                 :             :                         "dereference it?", callee);
    1079                 :         132 :           return;
    1080                 :             :         }
    1081                 :             : 
    1082                 :         587 :       if (POINTER_TYPE_P (TREE_TYPE (src))
    1083                 :         587 :           && !strop
    1084                 :         587 :           && comp_types (TREE_TYPE (src), type)
    1085                 :         642 :           && !VOID_TYPE_P (TREE_TYPE (type)))
    1086                 :             :         {
    1087                 :         153 :           warning_at (loc, OPT_Wsizeof_pointer_memaccess,
    1088                 :             :                       "argument to %<sizeof%> in %qD call is the same "
    1089                 :             :                       "pointer type %qT as the second source; expected %qT "
    1090                 :          51 :                       "or an explicit length", callee, TREE_TYPE (src),
    1091                 :          51 :                       TREE_TYPE (TREE_TYPE (src)));
    1092                 :          51 :           return;
    1093                 :             :         }
    1094                 :             :     }
    1095                 :             : 
    1096                 :             : }
    1097                 :             : 
    1098                 :             : /* Warn for unlikely, improbable, or stupid DECL declarations
    1099                 :             :    of `main'.  */
    1100                 :             : 
    1101                 :             : void
    1102                 :       37923 : check_main_parameter_types (tree decl)
    1103                 :             : {
    1104                 :       37923 :   function_args_iterator iter;
    1105                 :       37923 :   tree type;
    1106                 :       37923 :   int argct = 0;
    1107                 :             : 
    1108                 :       42774 :   FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
    1109                 :             :     {
    1110                 :             :       /* XXX void_type_node belies the abstraction.  */
    1111                 :       42028 :       if (type == void_type_node || type == error_mark_node)
    1112                 :             :         break;
    1113                 :             : 
    1114                 :        4851 :       tree t = type;
    1115                 :        4851 :       if (TYPE_ATOMIC (t))
    1116                 :           1 :           pedwarn (input_location, OPT_Wmain,
    1117                 :             :                    "%<_Atomic%>-qualified parameter type %qT of %q+D",
    1118                 :             :                    type, decl);
    1119                 :        9699 :       while (POINTER_TYPE_P (t))
    1120                 :             :         {
    1121                 :        4848 :           t = TREE_TYPE (t);
    1122                 :        4848 :           if (TYPE_ATOMIC (t))
    1123                 :           1 :             pedwarn (input_location, OPT_Wmain,
    1124                 :             :                      "%<_Atomic%>-qualified parameter type %qT of %q+D",
    1125                 :             :                      type, decl);
    1126                 :             :         }
    1127                 :             : 
    1128                 :        4851 :       ++argct;
    1129                 :        4851 :       switch (argct)
    1130                 :             :         {
    1131                 :        2427 :         case 1:
    1132                 :        2427 :           if (TYPE_MAIN_VARIANT (type) != integer_type_node)
    1133                 :          11 :             pedwarn (input_location, OPT_Wmain,
    1134                 :             :                      "first argument of %q+D should be %<int%>", decl);
    1135                 :             :           break;
    1136                 :             : 
    1137                 :        2412 :         case 2:
    1138                 :        2412 :           if (TREE_CODE (type) != POINTER_TYPE
    1139                 :        2412 :               || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
    1140                 :        4824 :               || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
    1141                 :        2412 :                   != char_type_node))
    1142                 :           0 :             pedwarn (input_location, OPT_Wmain,
    1143                 :             :                      "second argument of %q+D should be %<char **%>", decl);
    1144                 :             :           break;
    1145                 :             : 
    1146                 :          12 :         case 3:
    1147                 :          12 :           if (TREE_CODE (type) != POINTER_TYPE
    1148                 :          12 :               || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
    1149                 :          24 :               || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
    1150                 :          12 :                   != char_type_node))
    1151                 :           0 :             pedwarn (input_location, OPT_Wmain,
    1152                 :             :                      "third argument of %q+D should probably be "
    1153                 :             :                      "%<char **%>", decl);
    1154                 :             :           break;
    1155                 :             :         }
    1156                 :             :     }
    1157                 :             : 
    1158                 :             :   /* It is intentional that this message does not mention the third
    1159                 :             :     argument because it's only mentioned in an appendix of the
    1160                 :             :     standard.  */
    1161                 :       37923 :   if (argct > 0 && (argct < 2 || argct > 3))
    1162                 :          15 :     pedwarn (input_location, OPT_Wmain,
    1163                 :             :              "%q+D takes only zero or two arguments", decl);
    1164                 :             : 
    1165                 :       37923 :   if (stdarg_p (TREE_TYPE (decl)))
    1166                 :           5 :     pedwarn (input_location, OPT_Wmain,
    1167                 :             :              "%q+D declared as variadic function", decl);
    1168                 :       37923 : }
    1169                 :             : 
    1170                 :             : /* Warns and returns true if the conversion of EXPR to TYPE may alter a value.
    1171                 :             :    This is a helper function for warnings_for_convert_and_check.  */
    1172                 :             : 
    1173                 :             : static bool
    1174                 :    60491037 : conversion_warning (location_t loc, tree type, tree expr, tree result)
    1175                 :             : {
    1176                 :    60491037 :   tree expr_type = TREE_TYPE (expr);
    1177                 :    60491037 :   enum conversion_safety conversion_kind;
    1178                 :    60491037 :   int arith_ops = 0;
    1179                 :             : 
    1180                 :    60491037 :   if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
    1181                 :             :     return false;
    1182                 :             : 
    1183                 :             :   /* This may happen, because for LHS op= RHS we preevaluate
    1184                 :             :      RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
    1185                 :             :      means we could no longer see the code of the EXPR.  */
    1186                 :        7415 :   if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
    1187                 :           0 :     expr = C_MAYBE_CONST_EXPR_EXPR (expr);
    1188                 :        7415 :   if (TREE_CODE (expr) == SAVE_EXPR)
    1189                 :          17 :     expr = TREE_OPERAND (expr, 0);
    1190                 :             : 
    1191                 :        7415 :   switch (TREE_CODE (expr))
    1192                 :             :     {
    1193                 :         162 :     case EQ_EXPR:
    1194                 :         162 :     case NE_EXPR:
    1195                 :         162 :     case LE_EXPR:
    1196                 :         162 :     case GE_EXPR:
    1197                 :         162 :     case LT_EXPR:
    1198                 :         162 :     case GT_EXPR:
    1199                 :         162 :     case TRUTH_ANDIF_EXPR:
    1200                 :         162 :     case TRUTH_ORIF_EXPR:
    1201                 :         162 :     case TRUTH_AND_EXPR:
    1202                 :         162 :     case TRUTH_OR_EXPR:
    1203                 :         162 :     case TRUTH_XOR_EXPR:
    1204                 :         162 :     case TRUTH_NOT_EXPR:
    1205                 :             :       /* Conversion from boolean to a signed:1 bit-field (which only
    1206                 :             :          can hold the values 0 and -1) doesn't lose information - but
    1207                 :             :          it does change the value.  */
    1208                 :         162 :       if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
    1209                 :          38 :         warning_at (loc, OPT_Wconversion,
    1210                 :             :                     "conversion to %qT from boolean expression", type);
    1211                 :             :       return true;
    1212                 :             : 
    1213                 :        3686 :     case REAL_CST:
    1214                 :        3686 :     case INTEGER_CST:
    1215                 :        3686 :     case COMPLEX_CST:
    1216                 :        3686 :       {
    1217                 :        3686 :         conversion_kind = unsafe_conversion_p (type, expr, result, true);
    1218                 :        3686 :         int warnopt;
    1219                 :        3686 :         if (conversion_kind == UNSAFE_REAL)
    1220                 :             :           warnopt = OPT_Wfloat_conversion;
    1221                 :        3320 :         else if (conversion_kind)
    1222                 :             :           warnopt = OPT_Wconversion;
    1223                 :             :         else
    1224                 :             :           break;
    1225                 :             : 
    1226                 :         715 :         if (conversion_kind == UNSAFE_SIGN)
    1227                 :             :           {
    1228                 :        1296 :             bool cstresult
    1229                 :             :               = (result
    1230                 :         648 :                  && CONSTANT_CLASS_P (result));
    1231                 :         648 :             if (TYPE_UNSIGNED (type))
    1232                 :             :               {
    1233                 :         294 :                 if (cstresult)
    1234                 :         240 :                   warning_at (loc, OPT_Wsign_conversion,
    1235                 :             :                               "unsigned conversion from %qT to %qT "
    1236                 :             :                               "changes value from %qE to %qE",
    1237                 :             :                               expr_type, type, expr, result);
    1238                 :             :                 else
    1239                 :          54 :                   warning_at (loc, OPT_Wsign_conversion,
    1240                 :             :                               "unsigned conversion from %qT to %qT "
    1241                 :             :                               "changes the value of %qE",
    1242                 :             :                               expr_type, type, expr);
    1243                 :             :               }
    1244                 :             :             else
    1245                 :             :               {
    1246                 :         354 :                 if (cstresult)
    1247                 :         354 :                   warning_at (loc, OPT_Wsign_conversion,
    1248                 :             :                               "signed conversion from %qT to %qT changes "
    1249                 :             :                               "value from %qE to %qE",
    1250                 :             :                               expr_type, type, expr, result);
    1251                 :             :                 else
    1252                 :           0 :                   warning_at (loc, OPT_Wsign_conversion,
    1253                 :             :                               "signed conversion from %qT to %qT changes "
    1254                 :             :                               "the value of %qE",
    1255                 :             :                               expr_type, type, expr);
    1256                 :             :               }
    1257                 :             :           }
    1258                 :         433 :         else if (CONSTANT_CLASS_P (result))
    1259                 :         371 :           warning_at (loc, warnopt,
    1260                 :             :                       "conversion from %qT to %qT changes value from %qE to %qE",
    1261                 :             :                       expr_type, type, expr, result);
    1262                 :             :         else
    1263                 :          62 :           warning_at (loc, warnopt,
    1264                 :             :                       "conversion from %qT to %qT changes the value of %qE",
    1265                 :             :                       expr_type, type, expr);
    1266                 :             :         return true;
    1267                 :             :       }
    1268                 :             : 
    1269                 :         224 :     case PLUS_EXPR:
    1270                 :         224 :     case MINUS_EXPR:
    1271                 :         224 :     case MULT_EXPR:
    1272                 :         224 :     case MAX_EXPR:
    1273                 :         224 :     case MIN_EXPR:
    1274                 :         224 :     case TRUNC_MOD_EXPR:
    1275                 :         224 :     case FLOOR_MOD_EXPR:
    1276                 :         224 :     case TRUNC_DIV_EXPR:
    1277                 :         224 :     case FLOOR_DIV_EXPR:
    1278                 :         224 :     case CEIL_DIV_EXPR:
    1279                 :         224 :     case EXACT_DIV_EXPR:
    1280                 :         224 :     case RDIV_EXPR:
    1281                 :         224 :       arith_ops = 2;
    1282                 :         224 :       goto default_;
    1283                 :             : 
    1284                 :         249 :     case PREDECREMENT_EXPR:
    1285                 :         249 :     case PREINCREMENT_EXPR:
    1286                 :         249 :     case POSTDECREMENT_EXPR:
    1287                 :         249 :     case POSTINCREMENT_EXPR:
    1288                 :         249 :     case LSHIFT_EXPR:
    1289                 :         249 :     case RSHIFT_EXPR:
    1290                 :         249 :     case FIX_TRUNC_EXPR:
    1291                 :         249 :     case NON_LVALUE_EXPR:
    1292                 :         249 :     case NEGATE_EXPR:
    1293                 :         249 :     case BIT_NOT_EXPR:
    1294                 :         249 :       arith_ops = 1;
    1295                 :         249 :       goto default_;
    1296                 :             : 
    1297                 :         221 :     case COND_EXPR:
    1298                 :         221 :       {
    1299                 :             :         /* In case of COND_EXPR, we do not care about the type of
    1300                 :             :            COND_EXPR, only about the conversion of each operand.  */
    1301                 :         221 :         tree op1 = TREE_OPERAND (expr, 1);
    1302                 :         221 :         tree op2 = TREE_OPERAND (expr, 2);
    1303                 :             : 
    1304                 :         218 :         return ((op1 && conversion_warning (loc, type, op1, result))
    1305                 :         373 :                 || conversion_warning (loc, type, op2, result));
    1306                 :             :       }
    1307                 :             : 
    1308                 :          74 :     case BIT_AND_EXPR:
    1309                 :          74 :       if ((TREE_CODE (expr_type) == INTEGER_TYPE
    1310                 :          74 :            || TREE_CODE (expr_type) == BITINT_TYPE)
    1311                 :          74 :           && (TREE_CODE (type) == INTEGER_TYPE
    1312                 :          74 :               || TREE_CODE (type) == BITINT_TYPE))
    1313                 :         174 :         for (int i = 0; i < 2; ++i)
    1314                 :             :           {
    1315                 :         148 :             tree op = TREE_OPERAND (expr, i);
    1316                 :         148 :             if (TREE_CODE (op) != INTEGER_CST)
    1317                 :          85 :               continue;
    1318                 :             : 
    1319                 :             :             /* If one of the operands is a non-negative constant
    1320                 :             :                that fits in the target type, then the type of the
    1321                 :             :                other operand does not matter.  */
    1322                 :          63 :             if (int_fits_type_p (op, c_common_signed_type (type))
    1323                 :          63 :                 && int_fits_type_p (op, c_common_unsigned_type (type)))
    1324                 :             :               return false;
    1325                 :             : 
    1326                 :             :             /* If constant is unsigned and fits in the target
    1327                 :             :                type, then the result will also fit.  */
    1328                 :          23 :             if (TYPE_UNSIGNED (TREE_TYPE (op)) && int_fits_type_p (op, type))
    1329                 :             :               return false;
    1330                 :             :           }
    1331                 :             :       /* FALLTHRU */
    1332                 :         167 :     case BIT_IOR_EXPR:
    1333                 :         167 :     case BIT_XOR_EXPR:
    1334                 :         167 :       return (conversion_warning (loc, type, TREE_OPERAND (expr, 0), result)
    1335                 :         167 :               || conversion_warning (loc, type, TREE_OPERAND (expr, 1),
    1336                 :             :                                      result));
    1337                 :             : 
    1338                 :        3131 :     default_:
    1339                 :        3131 :     default:
    1340                 :        3131 :       conversion_kind = unsafe_conversion_p (type, expr, result, true);
    1341                 :        3131 :       {
    1342                 :        3131 :         int warnopt;
    1343                 :        3131 :         if (conversion_kind == UNSAFE_REAL)
    1344                 :             :           warnopt = OPT_Wfloat_conversion;
    1345                 :        3028 :         else if (conversion_kind == UNSAFE_SIGN)
    1346                 :             :           warnopt = OPT_Wsign_conversion;
    1347                 :        1915 :         else if (conversion_kind)
    1348                 :             :           warnopt = OPT_Wconversion;
    1349                 :             :         else
    1350                 :             :           break;
    1351                 :             : 
    1352                 :        1812 :         if (arith_ops
    1353                 :        1812 :             && global_dc->option_enabled_p (warnopt))
    1354                 :             :           {
    1355                 :         792 :             for (int i = 0; i < arith_ops; ++i)
    1356                 :             :               {
    1357                 :         622 :                 tree op = TREE_OPERAND (expr, i);
    1358                 :             :                 /* Avoid -Wsign-conversion for (unsigned)(x + (-1)).  */
    1359                 :         140 :                 if (TREE_CODE (expr) == PLUS_EXPR && i == 1
    1360                 :          70 :                     && INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
    1361                 :          10 :                     && TREE_CODE (op) == INTEGER_CST
    1362                 :         632 :                     && tree_int_cst_sgn (op) < 0)
    1363                 :           0 :                   op = fold_build1 (NEGATE_EXPR, TREE_TYPE (op), op);
    1364                 :         622 :                 tree opr = convert (type, op);
    1365                 :         622 :                 if (unsafe_conversion_p (type, op, opr, true))
    1366                 :         262 :                   goto op_unsafe;
    1367                 :             :               }
    1368                 :             :             /* The operands seem safe, we might still want to warn if
    1369                 :             :                -Warith-conversion.  */
    1370                 :             :             warnopt = OPT_Warith_conversion;
    1371                 :        1812 :           op_unsafe:;
    1372                 :             :           }
    1373                 :             : 
    1374                 :        1812 :         if (conversion_kind == UNSAFE_SIGN)
    1375                 :        1113 :           warning_at (loc, warnopt, "conversion to %qT from %qT "
    1376                 :             :                       "may change the sign of the result",
    1377                 :             :                       type, expr_type);
    1378                 :         699 :         else if (conversion_kind == UNSAFE_IMAGINARY)
    1379                 :          22 :           warning_at (loc, warnopt,
    1380                 :             :                       "conversion from %qT to %qT discards imaginary component",
    1381                 :             :                       expr_type, type);
    1382                 :             :         else
    1383                 :         677 :           warning_at (loc, warnopt,
    1384                 :             :                       "conversion from %qT to %qT may change value",
    1385                 :             :                       expr_type, type);
    1386                 :             :         return true;
    1387                 :             :       }
    1388                 :             :     }
    1389                 :             :   return false;
    1390                 :             : }
    1391                 :             : 
    1392                 :             : /* Produce warnings after a conversion. RESULT is the result of
    1393                 :             :    converting EXPR to TYPE.  This is a helper function for
    1394                 :             :    convert_and_check and cp_convert_and_check.  */
    1395                 :             : 
    1396                 :             : void
    1397                 :    60493464 : warnings_for_convert_and_check (location_t loc, tree type, tree expr,
    1398                 :             :                                 tree result)
    1399                 :             : {
    1400                 :    60493464 :   loc = expansion_point_location_if_in_system_header (loc);
    1401                 :             : 
    1402                 :   121018679 :   while (TREE_CODE (expr) == COMPOUND_EXPR)
    1403                 :       31751 :     expr = TREE_OPERAND (expr, 1);
    1404                 :    60514709 :   while (TREE_CODE (result) == COMPOUND_EXPR)
    1405                 :       21245 :     result = TREE_OPERAND (result, 1);
    1406                 :             : 
    1407                 :    60493464 :   bool cst = CONSTANT_CLASS_P (result);
    1408                 :    60493464 :   tree exprtype = TREE_TYPE (expr);
    1409                 :    60493464 :   tree result_diag;
    1410                 :             :   /* We're interested in the actual numerical value here, not its ASCII
    1411                 :             :      representation.  */
    1412                 :    60493464 :   if (cst && TYPE_MAIN_VARIANT (TREE_TYPE (result)) == char_type_node)
    1413                 :      643549 :     result_diag = fold_convert (integer_type_node, result);
    1414                 :             :   else
    1415                 :             :     result_diag = result;
    1416                 :             : 
    1417                 :    60493464 :   if (TREE_CODE (expr) == INTEGER_CST
    1418                 :    44652343 :       && (TREE_CODE (type) == INTEGER_TYPE
    1419                 :    44652343 :           || TREE_CODE (type) == BITINT_TYPE
    1420                 :     4281727 :           || (TREE_CODE (type) == ENUMERAL_TYPE
    1421                 :       77103 :               && TREE_CODE (ENUM_UNDERLYING_TYPE (type)) != BOOLEAN_TYPE))
    1422                 :   100941179 :       && !int_fits_type_p (expr, type))
    1423                 :             :     {
    1424                 :             :       /* Do not diagnose overflow in a constant expression merely
    1425                 :             :          because a conversion overflowed.  */
    1426                 :      751907 :       if (TREE_OVERFLOW (result))
    1427                 :        1891 :         TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
    1428                 :             : 
    1429                 :      751907 :       if (TYPE_UNSIGNED (type))
    1430                 :             :         {
    1431                 :             :           /* This detects cases like converting -129 or 256 to
    1432                 :             :              unsigned char.  */
    1433                 :      111057 :           if (!int_fits_type_p (expr, c_common_signed_type (type)))
    1434                 :             :             {
    1435                 :        1954 :               if (cst)
    1436                 :        1954 :                 warning_at (loc, OPT_Woverflow,
    1437                 :        1954 :                             (TYPE_UNSIGNED (exprtype)
    1438                 :             :                              ? G_("conversion from %qT to %qT "
    1439                 :             :                                   "changes value from %qE to %qE")
    1440                 :             :                              : G_("unsigned conversion from %qT to %qT "
    1441                 :             :                                   "changes value from %qE to %qE")),
    1442                 :             :                             exprtype, type, expr, result_diag);
    1443                 :             :               else
    1444                 :           0 :                 warning_at (loc, OPT_Woverflow,
    1445                 :           0 :                             (TYPE_UNSIGNED (exprtype)
    1446                 :             :                              ? G_("conversion from %qT to %qT "
    1447                 :             :                                   "changes the value of %qE")
    1448                 :             :                              : G_("unsigned conversion from %qT to %qT "
    1449                 :             :                                   "changes the value of %qE")),
    1450                 :             :                             exprtype, type, expr);
    1451                 :             :             }
    1452                 :             :           else
    1453                 :      109103 :             conversion_warning (loc, type, expr, result);
    1454                 :             :         }
    1455                 :      640850 :       else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
    1456                 :             :         {
    1457                 :         414 :           if (cst)
    1458                 :         414 :             warning_at (loc, OPT_Woverflow,
    1459                 :             :                         "overflow in conversion from %qT to %qT "
    1460                 :             :                         "changes value from %qE to %qE",
    1461                 :             :                         exprtype, type, expr, result_diag);
    1462                 :             :           else
    1463                 :           0 :             warning_at (loc, OPT_Woverflow,
    1464                 :             :                         "overflow in conversion from %qT to %qT "
    1465                 :             :                         "changes the value of %qE",
    1466                 :             :                         exprtype, type, expr);
    1467                 :             :         }
    1468                 :             :       /* No warning for converting 0x80000000 to int.  */
    1469                 :      640436 :       else if (pedantic
    1470                 :      640436 :                && ((TREE_CODE (exprtype) != INTEGER_TYPE
    1471                 :        1429 :                     && TREE_CODE (exprtype) != BITINT_TYPE)
    1472                 :        1429 :                    || (TYPE_PRECISION (exprtype)
    1473                 :        1429 :                        != TYPE_PRECISION (type))))
    1474                 :             :         {
    1475                 :          76 :           if (cst)
    1476                 :          76 :             warning_at (loc, OPT_Woverflow,
    1477                 :             :                         "overflow in conversion from %qT to %qT "
    1478                 :             :                         "changes value from %qE to %qE",
    1479                 :             :                         exprtype, type, expr, result_diag);
    1480                 :             :           else
    1481                 :           0 :             warning_at (loc, OPT_Woverflow,
    1482                 :             :                         "overflow in conversion from %qT to %qT "
    1483                 :             :                         "changes the value of %qE",
    1484                 :             :                         exprtype, type, expr);
    1485                 :             :         }
    1486                 :             :       else
    1487                 :      640360 :         conversion_warning (loc, type, expr, result);
    1488                 :             :     }
    1489                 :    59741557 :   else if ((TREE_CODE (result) == INTEGER_CST
    1490                 :    59741557 :             || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
    1491                 :             :     {
    1492                 :         559 :       if (cst)
    1493                 :         559 :         warning_at (loc, OPT_Woverflow,
    1494                 :             :                     "overflow in conversion from %qT to %qT "
    1495                 :             :                     "changes value from %qE to %qE",
    1496                 :             :                     exprtype, type, expr, result_diag);
    1497                 :             :       else
    1498                 :           0 :         warning_at (loc, OPT_Woverflow,
    1499                 :             :                     "overflow in conversion from %qT to %qT "
    1500                 :             :                     "changes the value of %qE",
    1501                 :             :                     exprtype, type, expr);
    1502                 :             :     }
    1503                 :             :   else
    1504                 :    59740998 :     conversion_warning (loc, type, expr, result);
    1505                 :    60493464 : }
    1506                 :             : 
    1507                 :             : /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
    1508                 :             :    Used to verify that case values match up with enumerator values.  */
    1509                 :             : 
    1510                 :             : static void
    1511                 :          40 : match_case_to_enum_1 (tree key, tree type, tree label)
    1512                 :             : {
    1513                 :             :   /* Avoid warning about enums that have no enumerators.  */
    1514                 :          40 :   if (TYPE_VALUES (type) == NULL_TREE)
    1515                 :          10 :     return;
    1516                 :             : 
    1517                 :          30 :   char buf[WIDE_INT_PRINT_BUFFER_SIZE];
    1518                 :          30 :   wide_int w = wi::to_wide (key);
    1519                 :             : 
    1520                 :          30 :   gcc_assert (w.get_precision () <= WIDE_INT_MAX_INL_PRECISION);
    1521                 :          30 :   if (tree_fits_uhwi_p (key))
    1522                 :          30 :     print_dec (w, buf, UNSIGNED);
    1523                 :           0 :   else if (tree_fits_shwi_p (key))
    1524                 :           0 :     print_dec (w, buf, SIGNED);
    1525                 :             :   else
    1526                 :           0 :     print_hex (w, buf);
    1527                 :             : 
    1528                 :          30 :   if (TYPE_NAME (type) == NULL_TREE)
    1529                 :           1 :     warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
    1530                 :           1 :                 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
    1531                 :             :                 "case value %qs not in enumerated type",
    1532                 :             :                 buf);
    1533                 :             :   else
    1534                 :          29 :     warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
    1535                 :          29 :                 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
    1536                 :             :                 "case value %qs not in enumerated type %qT",
    1537                 :             :                 buf, type);
    1538                 :          30 : }
    1539                 :             : 
    1540                 :             : /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
    1541                 :             :    Used to verify that case values match up with enumerator values.  */
    1542                 :             : 
    1543                 :             : static int
    1544                 :       31833 : match_case_to_enum (splay_tree_node node, void *data)
    1545                 :             : {
    1546                 :       31833 :   tree label = (tree) node->value;
    1547                 :       31833 :   tree type = (tree) data;
    1548                 :             : 
    1549                 :             :   /* Skip default case.  */
    1550                 :       31833 :   if (!CASE_LOW (label))
    1551                 :             :     return 0;
    1552                 :             : 
    1553                 :             :   /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
    1554                 :             :      when we did our enum->case scan.  Reset our scratch bit after.  */
    1555                 :       29962 :   if (!CASE_LOW_SEEN (label))
    1556                 :          39 :     match_case_to_enum_1 (CASE_LOW (label), type, label);
    1557                 :             :   else
    1558                 :       29923 :     CASE_LOW_SEEN (label) = 0;
    1559                 :             : 
    1560                 :             :   /* If CASE_HIGH is non-null, we have a range.  If CASE_HIGH_SEEN is
    1561                 :             :      not set, that means that CASE_HIGH did not appear when we did our
    1562                 :             :      enum->case scan.  Reset our scratch bit after.  */
    1563                 :       29962 :   if (CASE_HIGH (label))
    1564                 :             :     {
    1565                 :           3 :       if (!CASE_HIGH_SEEN (label))
    1566                 :           1 :         match_case_to_enum_1 (CASE_HIGH (label), type, label);
    1567                 :             :       else
    1568                 :           2 :         CASE_HIGH_SEEN (label) = 0;
    1569                 :             :     }
    1570                 :             : 
    1571                 :             :   return 0;
    1572                 :             : }
    1573                 :             : 
    1574                 :             : /* Handle -Wswitch*.  Called from the front end after parsing the
    1575                 :             :    switch construct.  */
    1576                 :             : /* ??? Should probably be somewhere generic, since other languages
    1577                 :             :    besides C and C++ would want this.  At the moment, however, C/C++
    1578                 :             :    are the only tree-ssa languages that support enumerations at all,
    1579                 :             :    so the point is moot.  */
    1580                 :             : 
    1581                 :             : void
    1582                 :      294668 : c_do_switch_warnings (splay_tree cases, location_t switch_location,
    1583                 :             :                       tree type, tree cond, bool bool_cond_p)
    1584                 :             : {
    1585                 :      294668 :   splay_tree_node default_node;
    1586                 :      294668 :   splay_tree_node node;
    1587                 :      294668 :   tree chain;
    1588                 :      294668 :   bool outside_range_p = false;
    1589                 :             : 
    1590                 :      294668 :   if (type != error_mark_node
    1591                 :      294636 :       && type != TREE_TYPE (cond)
    1592                 :      140764 :       && INTEGRAL_TYPE_P (type)
    1593                 :      140755 :       && INTEGRAL_TYPE_P (TREE_TYPE (cond))
    1594                 :      435422 :       && (!tree_int_cst_equal (TYPE_MIN_VALUE (type),
    1595                 :      140754 :                                TYPE_MIN_VALUE (TREE_TYPE (cond)))
    1596                 :       28531 :           || !tree_int_cst_equal (TYPE_MAX_VALUE (type),
    1597                 :       28531 :                                   TYPE_MAX_VALUE (TREE_TYPE (cond)))))
    1598                 :             :     {
    1599                 :      112227 :       tree min_value = TYPE_MIN_VALUE (type);
    1600                 :      112227 :       tree max_value = TYPE_MAX_VALUE (type);
    1601                 :             : 
    1602                 :      112227 :       node = splay_tree_predecessor (cases, (splay_tree_key) min_value);
    1603                 :      112227 :       if (node && node->key)
    1604                 :             :         {
    1605                 :          81 :           outside_range_p = true;
    1606                 :             :           /* There is at least one case smaller than TYPE's minimum value.
    1607                 :             :              NODE itself could be still a range overlapping the valid values,
    1608                 :             :              but any predecessors thereof except the default case will be
    1609                 :             :              completely outside of range.  */
    1610                 :          81 :           if (CASE_HIGH ((tree) node->value)
    1611                 :          81 :               && tree_int_cst_compare (CASE_HIGH ((tree) node->value),
    1612                 :             :                                        min_value) >= 0)
    1613                 :             :             {
    1614                 :          34 :               location_t loc = EXPR_LOCATION ((tree) node->value);
    1615                 :          34 :               warning_at (loc, OPT_Wswitch_outside_range,
    1616                 :             :                           "lower value in case label range less than minimum"
    1617                 :             :                           " value for type");
    1618                 :          34 :               CASE_LOW ((tree) node->value) = convert (TREE_TYPE (cond),
    1619                 :             :                                                        min_value);
    1620                 :          34 :               node->key = (splay_tree_key) CASE_LOW ((tree) node->value);
    1621                 :             :             }
    1622                 :             :           /* All the following ones are completely outside of range.  */
    1623                 :         195 :           do
    1624                 :             :             {
    1625                 :         138 :               node = splay_tree_predecessor (cases,
    1626                 :             :                                              (splay_tree_key) min_value);
    1627                 :         138 :               if (node == NULL || !node->key)
    1628                 :             :                 break;
    1629                 :          57 :               location_t loc = EXPR_LOCATION ((tree) node->value);
    1630                 :          57 :               warning_at (loc, OPT_Wswitch_outside_range, "case label value is"
    1631                 :             :                           " less than minimum value for type");
    1632                 :          57 :               splay_tree_remove (cases, node->key);
    1633                 :          57 :             }
    1634                 :             :           while (1);
    1635                 :             :         }
    1636                 :      112227 :       node = splay_tree_lookup (cases, (splay_tree_key) max_value);
    1637                 :      112227 :       if (node == NULL)
    1638                 :      112168 :         node = splay_tree_predecessor (cases, (splay_tree_key) max_value);
    1639                 :             :       /* Handle a single node that might partially overlap the range.  */
    1640                 :      112168 :       if (node
    1641                 :      109657 :           && node->key
    1642                 :      109617 :           && CASE_HIGH ((tree) node->value)
    1643                 :      112238 :           && tree_int_cst_compare (CASE_HIGH ((tree) node->value),
    1644                 :             :                                    max_value) > 0)
    1645                 :             :         {
    1646                 :          34 :           location_t loc = EXPR_LOCATION ((tree) node->value);
    1647                 :          34 :           warning_at (loc, OPT_Wswitch_outside_range, "upper value in case"
    1648                 :             :                       " label range exceeds maximum value for type");
    1649                 :          34 :           CASE_HIGH ((tree) node->value)
    1650                 :          34 :             = convert (TREE_TYPE (cond), max_value);
    1651                 :          34 :           outside_range_p = true;
    1652                 :             :         }
    1653                 :             :       /* And any nodes that are completely outside of the range.  */
    1654                 :      224744 :       while ((node = splay_tree_successor (cases,
    1655                 :             :                                            (splay_tree_key) max_value))
    1656                 :      112372 :              != NULL)
    1657                 :             :         {
    1658                 :         145 :           location_t loc = EXPR_LOCATION ((tree) node->value);
    1659                 :         145 :           warning_at (loc, OPT_Wswitch_outside_range,
    1660                 :             :                       "case label value exceeds maximum value for type");
    1661                 :         145 :           splay_tree_remove (cases, node->key);
    1662                 :         145 :           outside_range_p = true;
    1663                 :             :         }
    1664                 :             :     }
    1665                 :             : 
    1666                 :      294668 :   if (!warn_switch && !warn_switch_enum && !warn_switch_default
    1667                 :      286490 :       && !warn_switch_bool)
    1668                 :             :     return;
    1669                 :             : 
    1670                 :      294668 :   default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
    1671                 :      294668 :   if (!default_node)
    1672                 :       42065 :     warning_at (switch_location, OPT_Wswitch_default,
    1673                 :             :                 "switch missing default case");
    1674                 :             : 
    1675                 :             :   /* There are certain cases where -Wswitch-bool warnings aren't
    1676                 :             :      desirable, such as
    1677                 :             :      switch (boolean)
    1678                 :             :        {
    1679                 :             :        case true: ...
    1680                 :             :        case false: ...
    1681                 :             :        }
    1682                 :             :      so be careful here.  */
    1683                 :      294668 :   if (warn_switch_bool && bool_cond_p)
    1684                 :             :     {
    1685                 :         200 :       splay_tree_node min_node;
    1686                 :             :       /* If there's a default node, it's also the value with the minimal
    1687                 :             :          key.  So look at the penultimate key (if any).  */
    1688                 :         200 :       if (default_node)
    1689                 :          66 :         min_node = splay_tree_successor (cases, (splay_tree_key) NULL);
    1690                 :             :       else
    1691                 :         134 :         min_node = splay_tree_min (cases);
    1692                 :         200 :       tree min = min_node ? (tree) min_node->key : NULL_TREE;
    1693                 :             : 
    1694                 :         200 :       splay_tree_node max_node = splay_tree_max (cases);
    1695                 :             :       /* This might be a case range, so look at the value with the
    1696                 :             :          maximal key and then check CASE_HIGH.  */
    1697                 :         200 :       tree max = max_node ? (tree) max_node->value : NULL_TREE;
    1698                 :         113 :       if (max)
    1699                 :         113 :         max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max);
    1700                 :             : 
    1701                 :             :       /* If there's a case value > 1 or < 0, that is outside bool
    1702                 :             :          range, warn.  */
    1703                 :         200 :       if (outside_range_p
    1704                 :          94 :           || (max && wi::gts_p (wi::to_wide (max), 1))
    1705                 :          79 :           || (min && wi::lts_p (wi::to_wide (min), 0))
    1706                 :             :           /* And handle the
    1707                 :             :              switch (boolean)
    1708                 :             :                {
    1709                 :             :                case true: ...
    1710                 :             :                case false: ...
    1711                 :             :                default: ...
    1712                 :             :                }
    1713                 :             :              case, where we want to warn.  */
    1714                 :         200 :           || (default_node
    1715                 :          75 :               && max && wi::to_wide (max) == 1
    1716                 :          75 :               && min && wi::to_wide (min) == 0))
    1717                 :         135 :         warning_at (switch_location, OPT_Wswitch_bool,
    1718                 :             :                     "switch condition has boolean value");
    1719                 :             :     }
    1720                 :             : 
    1721                 :             :   /* From here on, we only care about enumerated types.  */
    1722                 :      294668 :   if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
    1723                 :             :     return;
    1724                 :             : 
    1725                 :             :   /* From here on, we only care about -Wswitch and -Wswitch-enum.  */
    1726                 :      112663 :   if (!warn_switch_enum && !warn_switch)
    1727                 :             :     return;
    1728                 :             : 
    1729                 :             :   /* Check the cases.  Warn about case values which are not members of
    1730                 :             :      the enumerated type.  For -Wswitch-enum, or for -Wswitch when
    1731                 :             :      there is no default case, check that exactly all enumeration
    1732                 :             :      literals are covered by the cases.  */
    1733                 :             : 
    1734                 :             :   /* Clearing COND if it is not an integer constant simplifies
    1735                 :             :      the tests inside the loop below.  */
    1736                 :        2747 :   if (TREE_CODE (cond) != INTEGER_CST)
    1737                 :        2746 :     cond = NULL_TREE;
    1738                 :             : 
    1739                 :             :   /* The time complexity here is O(N*lg(N)) worst case, but for the
    1740                 :             :       common case of monotonically increasing enumerators, it is
    1741                 :             :       O(N), since the nature of the splay tree will keep the next
    1742                 :             :       element adjacent to the root at all times.  */
    1743                 :             : 
    1744                 :       59873 :   for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
    1745                 :             :     {
    1746                 :       57126 :       tree value = TREE_VALUE (chain);
    1747                 :       57126 :       tree attrs = DECL_ATTRIBUTES (value);
    1748                 :       57126 :       value = DECL_INITIAL (value);
    1749                 :       57126 :       node = splay_tree_lookup (cases, (splay_tree_key) value);
    1750                 :       57126 :       if (node)
    1751                 :             :         {
    1752                 :             :           /* Mark the CASE_LOW part of the case entry as seen.  */
    1753                 :       31484 :           tree label = (tree) node->value;
    1754                 :       31484 :           CASE_LOW_SEEN (label) = 1;
    1755                 :       31484 :           continue;
    1756                 :       31484 :         }
    1757                 :             : 
    1758                 :             :       /* Even though there wasn't an exact match, there might be a
    1759                 :             :          case range which includes the enumerator's value.  */
    1760                 :       25642 :       node = splay_tree_predecessor (cases, (splay_tree_key) value);
    1761                 :       25642 :       if (node && CASE_HIGH ((tree) node->value))
    1762                 :             :         {
    1763                 :           4 :           tree label = (tree) node->value;
    1764                 :           4 :           int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
    1765                 :           4 :           if (cmp >= 0)
    1766                 :             :             {
    1767                 :             :               /* If we match the upper bound exactly, mark the CASE_HIGH
    1768                 :             :                  part of the case entry as seen.  */
    1769                 :           4 :               if (cmp == 0)
    1770                 :           2 :                 CASE_HIGH_SEEN (label) = 1;
    1771                 :           4 :               continue;
    1772                 :             :             }
    1773                 :             :         }
    1774                 :             : 
    1775                 :             :       /* We've now determined that this enumerated literal isn't
    1776                 :             :          handled by the case labels of the switch statement.  */
    1777                 :             : 
    1778                 :             :       /* Don't warn if the enumerator was marked as unused.  We can't use
    1779                 :             :          TREE_USED here: it could have been set on the enumerator if the
    1780                 :             :          enumerator was used earlier.  */
    1781                 :       25638 :       if (lookup_attribute ("unused", attrs)
    1782                 :       25638 :           || lookup_attribute ("maybe_unused", attrs))
    1783                 :          11 :         continue;
    1784                 :             : 
    1785                 :             :       /* If the switch expression is a constant, we only really care
    1786                 :             :          about whether that constant is handled by the switch.  */
    1787                 :       25627 :       if (cond && tree_int_cst_compare (cond, value))
    1788                 :           0 :         continue;
    1789                 :             : 
    1790                 :             :       /* If the enumerator is defined in a system header and uses a reserved
    1791                 :             :          name, then we continue to avoid throwing a warning.  */
    1792                 :       25627 :       location_t loc = DECL_SOURCE_LOCATION
    1793                 :             :             (TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type)));
    1794                 :       25627 :       if (in_system_header_at (loc)
    1795                 :       26656 :           && name_reserved_for_implementation_p
    1796                 :        1029 :               (IDENTIFIER_POINTER (TREE_PURPOSE (chain))))
    1797                 :        1021 :         continue;
    1798                 :             : 
    1799                 :             :       /* If there is a default_node, the only relevant option is
    1800                 :             :          Wswitch-enum.  Otherwise, if both are enabled then we prefer
    1801                 :             :          to warn using -Wswitch because -Wswitch is enabled by -Wall
    1802                 :             :          while -Wswitch-enum is explicit.  */
    1803                 :       49212 :       warning_at (switch_location,
    1804                 :          58 :                   (default_node || !warn_switch
    1805                 :             :                    ? OPT_Wswitch_enum
    1806                 :             :                    : OPT_Wswitch),
    1807                 :             :                   "enumeration value %qE not handled in switch",
    1808                 :       24606 :                   TREE_PURPOSE (chain));
    1809                 :             :     }
    1810                 :             : 
    1811                 :             :   /* Warn if there are case expressions that don't correspond to
    1812                 :             :      enumerators.  This can occur since C and C++ don't enforce
    1813                 :             :      type-checking of assignments to enumeration variables.
    1814                 :             : 
    1815                 :             :      The time complexity here is now always O(N) worst case, since
    1816                 :             :      we should have marked both the lower bound and upper bound of
    1817                 :             :      every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
    1818                 :             :      above.  This scan also resets those fields.  */
    1819                 :             : 
    1820                 :        2747 :   splay_tree_foreach (cases, match_case_to_enum, type);
    1821                 :             : }
    1822                 :             : 
    1823                 :             : /* Warn for A ?: C expressions (with B omitted) where A is a boolean
    1824                 :             :    expression, because B will always be true. */
    1825                 :             : 
    1826                 :             : void
    1827                 :        1415 : warn_for_omitted_condop (location_t location, tree cond)
    1828                 :             : {
    1829                 :             :   /* In C++ template declarations it can happen that the type is dependent
    1830                 :             :      and not yet known, thus TREE_TYPE (cond) == NULL_TREE.  */
    1831                 :        1415 :   if (truth_value_p (TREE_CODE (cond))
    1832                 :        1415 :       || (TREE_TYPE (cond) != NULL_TREE
    1833                 :        1123 :           && TREE_CODE (TREE_TYPE (cond)) == BOOLEAN_TYPE))
    1834                 :         352 :       warning_at (location, OPT_Wparentheses,
    1835                 :             :                 "the omitted middle operand in %<?:%> will always be %<true%>, "
    1836                 :             :                 "suggest explicit middle operand");
    1837                 :        1415 : }
    1838                 :             : 
    1839                 :             : /* Give an error for storing into ARG, which is 'const'.  USE indicates
    1840                 :             :    how ARG was being used.  */
    1841                 :             : 
    1842                 :             : void
    1843                 :         233 : readonly_error (location_t loc, tree arg, enum lvalue_use use)
    1844                 :             : {
    1845                 :         233 :   gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
    1846                 :             :               || use == lv_asm);
    1847                 :         233 :   STRIP_ANY_LOCATION_WRAPPER (arg);
    1848                 :             :   /* Using this macro rather than (for example) arrays of messages
    1849                 :             :      ensures that all the format strings are checked at compile
    1850                 :             :      time.  */
    1851                 :             : #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A)               \
    1852                 :             :                                    : (use == lv_increment ? (I)         \
    1853                 :             :                                    : (use == lv_decrement ? (D) : (AS))))
    1854                 :         233 :   if (TREE_CODE (arg) == COMPONENT_REF)
    1855                 :             :     {
    1856                 :          52 :       if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
    1857                 :          84 :         error_at (loc, READONLY_MSG (G_("assignment of member "
    1858                 :             :                                         "%qD in read-only object"),
    1859                 :             :                                      G_("increment of member "
    1860                 :             :                                         "%qD in read-only object"),
    1861                 :             :                                      G_("decrement of member "
    1862                 :             :                                         "%qD in read-only object"),
    1863                 :             :                                      G_("member %qD in read-only object "
    1864                 :             :                                         "used as %<asm%> output")),
    1865                 :          42 :                   TREE_OPERAND (arg, 1));
    1866                 :             :       else
    1867                 :          20 :         error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
    1868                 :             :                                      G_("increment of read-only member %qD"),
    1869                 :             :                                      G_("decrement of read-only member %qD"),
    1870                 :             :                                      G_("read-only member %qD used as %<asm%> output")),
    1871                 :          10 :                   TREE_OPERAND (arg, 1));
    1872                 :             :     }
    1873                 :             :   else if (VAR_P (arg))
    1874                 :         157 :     error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
    1875                 :             :                                  G_("increment of read-only variable %qD"),
    1876                 :             :                                  G_("decrement of read-only variable %qD"),
    1877                 :             :                                  G_("read-only variable %qD used as %<asm%> output")),
    1878                 :             :               arg);
    1879                 :             :   else if (TREE_CODE (arg) == PARM_DECL)
    1880                 :          15 :     error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
    1881                 :             :                                  G_("increment of read-only parameter %qD"),
    1882                 :             :                                  G_("decrement of read-only parameter %qD"),
    1883                 :             :                                  G_("read-only parameter %qD use as %<asm%> output")),
    1884                 :             :               arg);
    1885                 :             :   else if (TREE_CODE (arg) == RESULT_DECL)
    1886                 :             :     {
    1887                 :           0 :       gcc_assert (c_dialect_cxx ());
    1888                 :           0 :       error_at (loc, READONLY_MSG (G_("assignment of "
    1889                 :             :                                       "read-only named return value %qD"),
    1890                 :             :                                    G_("increment of "
    1891                 :             :                                       "read-only named return value %qD"),
    1892                 :             :                                    G_("decrement of "
    1893                 :             :                                       "read-only named return value %qD"),
    1894                 :             :                                    G_("read-only named return value %qD "
    1895                 :             :                                       "used as %<asm%>output")),
    1896                 :             :                 arg);
    1897                 :             :     }
    1898                 :             :   else if (TREE_CODE (arg) == FUNCTION_DECL)
    1899                 :           8 :     error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
    1900                 :             :                                  G_("increment of function %qD"),
    1901                 :             :                                  G_("decrement of function %qD"),
    1902                 :             :                                  G_("function %qD used as %<asm%> output")),
    1903                 :             :               arg);
    1904                 :             :   else
    1905                 :         172 :     error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
    1906                 :             :                                  G_("increment of read-only location %qE"),
    1907                 :             :                                  G_("decrement of read-only location %qE"),
    1908                 :             :                                  G_("read-only location %qE used as %<asm%> output")),
    1909                 :             :               arg);
    1910                 :         233 : }
    1911                 :             : 
    1912                 :             : /* Print an error message for an invalid lvalue.  USE says
    1913                 :             :    how the lvalue is being used and so selects the error message.  LOC
    1914                 :             :    is the location for the error.  */
    1915                 :             : 
    1916                 :             : void
    1917                 :         224 : lvalue_error (location_t loc, enum lvalue_use use)
    1918                 :             : {
    1919                 :         224 :   switch (use)
    1920                 :             :     {
    1921                 :         114 :     case lv_assign:
    1922                 :         114 :       error_at (loc, "lvalue required as left operand of assignment");
    1923                 :         114 :       break;
    1924                 :          37 :     case lv_increment:
    1925                 :          37 :       error_at (loc, "lvalue required as increment operand");
    1926                 :          37 :       break;
    1927                 :          27 :     case lv_decrement:
    1928                 :          27 :       error_at (loc, "lvalue required as decrement operand");
    1929                 :          27 :       break;
    1930                 :          45 :     case lv_addressof:
    1931                 :          45 :       error_at (loc, "lvalue required as unary %<&%> operand");
    1932                 :          45 :       break;
    1933                 :           1 :     case lv_asm:
    1934                 :           1 :       error_at (loc, "lvalue required in %<asm%> statement");
    1935                 :           1 :       break;
    1936                 :           0 :     default:
    1937                 :           0 :       gcc_unreachable ();
    1938                 :             :     }
    1939                 :         224 : }
    1940                 :             : 
    1941                 :             : /* Print an error message for an invalid indirection of type TYPE.
    1942                 :             :    ERRSTRING is the name of the operator for the indirection.  */
    1943                 :             : 
    1944                 :             : void
    1945                 :         299 : invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
    1946                 :             : {
    1947                 :         299 :   switch (errstring)
    1948                 :             :     {
    1949                 :           0 :     case RO_NULL:
    1950                 :           0 :       gcc_assert (c_dialect_cxx ());
    1951                 :           0 :       error_at (loc, "invalid type argument (have %qT)", type);
    1952                 :           0 :       break;
    1953                 :           0 :     case RO_ARRAY_INDEXING:
    1954                 :           0 :       error_at (loc,
    1955                 :             :                 "invalid type argument of array indexing (have %qT)",
    1956                 :             :                 type);
    1957                 :           0 :       break;
    1958                 :         293 :     case RO_UNARY_STAR:
    1959                 :         293 :       error_at (loc,
    1960                 :             :                 "invalid type argument of unary %<*%> (have %qT)",
    1961                 :             :                 type);
    1962                 :         293 :       break;
    1963                 :           2 :     case RO_ARROW:
    1964                 :           2 :       error_at (loc,
    1965                 :             :                 "invalid type argument of %<->%> (have %qT)",
    1966                 :             :                 type);
    1967                 :           2 :       break;
    1968                 :           4 :     case RO_ARROW_STAR:
    1969                 :           4 :       error_at (loc,
    1970                 :             :                 "invalid type argument of %<->*%> (have %qT)",
    1971                 :             :                 type);
    1972                 :           4 :       break;
    1973                 :           0 :     case RO_IMPLICIT_CONVERSION:
    1974                 :           0 :       error_at (loc,
    1975                 :             :                 "invalid type argument of implicit conversion (have %qT)",
    1976                 :             :                 type);
    1977                 :           0 :       break;
    1978                 :           0 :     default:
    1979                 :           0 :       gcc_unreachable ();
    1980                 :             :     }
    1981                 :         299 : }
    1982                 :             : 
    1983                 :             : /* Subscripting with type char is likely to lose on a machine where
    1984                 :             :    chars are signed.  So warn on any machine, but optionally.  Don't
    1985                 :             :    warn for unsigned char since that type is safe.  Don't warn for
    1986                 :             :    signed char because anyone who uses that must have done so
    1987                 :             :    deliberately. Furthermore, we reduce the false positive load by
    1988                 :             :    warning only for non-constant value of type char.
    1989                 :             :    LOC is the location of the subscripting expression.  */
    1990                 :             : 
    1991                 :             : void
    1992                 :     8241781 : warn_array_subscript_with_type_char (location_t loc, tree index)
    1993                 :             : {
    1994                 :     8241781 :   if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
    1995                 :             :     {
    1996                 :             :       /* If INDEX has a location, use it; otherwise use LOC (the location
    1997                 :             :          of the subscripting expression as a whole).  */
    1998                 :      230903 :       loc = EXPR_LOC_OR_LOC (index, loc);
    1999                 :      230903 :       STRIP_ANY_LOCATION_WRAPPER (index);
    2000                 :      230903 :       if (TREE_CODE (index) != INTEGER_CST)
    2001                 :         277 :         warning_at (loc, OPT_Wchar_subscripts,
    2002                 :             :                     "array subscript has type %<char%>");
    2003                 :             :     }
    2004                 :     8241781 : }
    2005                 :             : 
    2006                 :             : /* Implement -Wparentheses for the unexpected C precedence rules, to
    2007                 :             :    cover cases like x + y << z which readers are likely to
    2008                 :             :    misinterpret.  We have seen an expression in which CODE is a binary
    2009                 :             :    operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
    2010                 :             :    before folding had CODE_LEFT and CODE_RIGHT.  CODE_LEFT and
    2011                 :             :    CODE_RIGHT may be ERROR_MARK, which means that that side of the
    2012                 :             :    expression was not formed using a binary or unary operator, or it
    2013                 :             :    was enclosed in parentheses.  */
    2014                 :             : 
    2015                 :             : void
    2016                 :     2611580 : warn_about_parentheses (location_t loc, enum tree_code code,
    2017                 :             :                         enum tree_code code_left, tree arg_left,
    2018                 :             :                         enum tree_code code_right, tree arg_right)
    2019                 :             : {
    2020                 :     2611580 :   if (!warn_parentheses)
    2021                 :             :     return;
    2022                 :             : 
    2023                 :             :   /* This macro tests that the expression ARG with original tree code
    2024                 :             :      CODE appears to be a boolean expression. or the result of folding a
    2025                 :             :      boolean expression.  */
    2026                 :             : #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG)                             \
    2027                 :             :         (truth_value_p (TREE_CODE (ARG))                                    \
    2028                 :             :          || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE                     \
    2029                 :             :          /* Folding may create 0 or 1 integers from other expressions.  */  \
    2030                 :             :          || ((CODE) != INTEGER_CST                                          \
    2031                 :             :              && (integer_onep (ARG) || integer_zerop (ARG))))
    2032                 :             : 
    2033                 :     2611580 :   switch (code)
    2034                 :             :     {
    2035                 :      241640 :     case LSHIFT_EXPR:
    2036                 :      241640 :       if (code_left == PLUS_EXPR)
    2037                 :          29 :         warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
    2038                 :             :                     "suggest parentheses around %<+%> inside %<<<%>");
    2039                 :      241613 :       else if (code_right == PLUS_EXPR)
    2040                 :          28 :         warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
    2041                 :             :                     "suggest parentheses around %<+%> inside %<<<%>");
    2042                 :      241586 :       else if (code_left == MINUS_EXPR)
    2043                 :          29 :         warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
    2044                 :             :                     "suggest parentheses around %<-%> inside %<<<%>");
    2045                 :      241559 :       else if (code_right == MINUS_EXPR)
    2046                 :          28 :         warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
    2047                 :             :                     "suggest parentheses around %<-%> inside %<<<%>");
    2048                 :             :       return;
    2049                 :             : 
    2050                 :       91459 :     case RSHIFT_EXPR:
    2051                 :       91459 :       if (code_left == PLUS_EXPR)
    2052                 :          33 :         warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
    2053                 :             :                     "suggest parentheses around %<+%> inside %<>>%>");
    2054                 :       91428 :       else if (code_right == PLUS_EXPR)
    2055                 :          32 :         warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
    2056                 :             :                     "suggest parentheses around %<+%> inside %<>>%>");
    2057                 :       91397 :       else if (code_left == MINUS_EXPR)
    2058                 :          29 :         warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
    2059                 :             :                     "suggest parentheses around %<-%> inside %<>>%>");
    2060                 :       91370 :       else if (code_right == MINUS_EXPR)
    2061                 :          28 :         warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
    2062                 :             :                     "suggest parentheses around %<-%> inside %<>>%>");
    2063                 :             :       return;
    2064                 :             : 
    2065                 :       56200 :     case TRUTH_ORIF_EXPR:
    2066                 :       56200 :       if (code_left == TRUTH_ANDIF_EXPR)
    2067                 :          34 :         warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
    2068                 :             :                     "suggest parentheses around %<&&%> within %<||%>");
    2069                 :       56168 :       else if (code_right == TRUTH_ANDIF_EXPR)
    2070                 :          28 :         warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
    2071                 :             :                     "suggest parentheses around %<&&%> within %<||%>");
    2072                 :             :       return;
    2073                 :             : 
    2074                 :      123480 :     case BIT_IOR_EXPR:
    2075                 :      123480 :       if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
    2076                 :      123480 :           || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
    2077                 :         124 :         warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
    2078                 :             :                  "suggest parentheses around arithmetic in operand of %<|%>");
    2079                 :      123364 :       else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
    2080                 :      123364 :                || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
    2081                 :         120 :         warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
    2082                 :             :                  "suggest parentheses around arithmetic in operand of %<|%>");
    2083                 :             :       /* Check cases like x|y==z */
    2084                 :      123248 :       else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
    2085                 :          58 :         warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
    2086                 :             :                  "suggest parentheses around comparison in operand of %<|%>");
    2087                 :      123194 :       else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
    2088                 :          56 :         warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
    2089                 :             :                  "suggest parentheses around comparison in operand of %<|%>");
    2090                 :             :       /* Check cases like !x | y */
    2091                 :      123140 :       else if (code_left == TRUTH_NOT_EXPR
    2092                 :      123140 :                && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
    2093                 :         180 :         warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
    2094                 :             :                     "suggest parentheses around operand of "
    2095                 :             :                     "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
    2096                 :             :       return;
    2097                 :             : 
    2098                 :       10435 :     case BIT_XOR_EXPR:
    2099                 :       10435 :       if (code_left == BIT_AND_EXPR
    2100                 :       10435 :           || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
    2101                 :          87 :         warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
    2102                 :             :                  "suggest parentheses around arithmetic in operand of %<^%>");
    2103                 :       10354 :       else if (code_right == BIT_AND_EXPR
    2104                 :       10354 :                || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
    2105                 :          84 :         warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
    2106                 :             :                  "suggest parentheses around arithmetic in operand of %<^%>");
    2107                 :             :       /* Check cases like x^y==z */
    2108                 :       10273 :       else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
    2109                 :          87 :         warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
    2110                 :             :                  "suggest parentheses around comparison in operand of %<^%>");
    2111                 :       10192 :       else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
    2112                 :          84 :         warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
    2113                 :             :                  "suggest parentheses around comparison in operand of %<^%>");
    2114                 :             :       return;
    2115                 :             : 
    2116                 :      110902 :     case BIT_AND_EXPR:
    2117                 :      110902 :       if (code_left == PLUS_EXPR)
    2118                 :          29 :         warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
    2119                 :             :                  "suggest parentheses around %<+%> in operand of %<&%>");
    2120                 :      110875 :       else if (code_right == PLUS_EXPR)
    2121                 :          28 :         warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
    2122                 :             :                  "suggest parentheses around %<+%> in operand of %<&%>");
    2123                 :      110848 :       else if (code_left == MINUS_EXPR)
    2124                 :          29 :         warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
    2125                 :             :                  "suggest parentheses around %<-%> in operand of %<&%>");
    2126                 :      110821 :       else if (code_right == MINUS_EXPR)
    2127                 :          28 :         warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
    2128                 :             :                  "suggest parentheses around %<-%> in operand of %<&%>");
    2129                 :             :       /* Check cases like x&y==z */
    2130                 :      110794 :       else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
    2131                 :          87 :         warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
    2132                 :             :                  "suggest parentheses around comparison in operand of %<&%>");
    2133                 :      110713 :       else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
    2134                 :          84 :         warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
    2135                 :             :                  "suggest parentheses around comparison in operand of %<&%>");
    2136                 :             :       /* Check cases like !x & y */
    2137                 :      110632 :       else if (code_left == TRUTH_NOT_EXPR
    2138                 :      110632 :                && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
    2139                 :         185 :         warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
    2140                 :             :                     "suggest parentheses around operand of "
    2141                 :             :                     "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
    2142                 :             :       return;
    2143                 :             : 
    2144                 :      222443 :     case EQ_EXPR:
    2145                 :      222443 :       if (TREE_CODE_CLASS (code_left) == tcc_comparison)
    2146                 :          38 :         warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
    2147                 :             :                  "suggest parentheses around comparison in operand of %<==%>");
    2148                 :      222410 :       else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
    2149                 :          10 :         warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
    2150                 :             :                  "suggest parentheses around comparison in operand of %<==%>");
    2151                 :             :       return;
    2152                 :       92667 :     case NE_EXPR:
    2153                 :       92667 :       if (TREE_CODE_CLASS (code_left) == tcc_comparison)
    2154                 :          39 :         warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
    2155                 :             :                  "suggest parentheses around comparison in operand of %<!=%>");
    2156                 :       92632 :       else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
    2157                 :           6 :         warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
    2158                 :             :                  "suggest parentheses around comparison in operand of %<!=%>");
    2159                 :             :       return;
    2160                 :             : 
    2161                 :     1662354 :     default:
    2162                 :     1662354 :       if (TREE_CODE_CLASS (code) == tcc_comparison)
    2163                 :             :         {
    2164                 :      320746 :           if (TREE_CODE_CLASS (code_left) == tcc_comparison
    2165                 :         140 :                 && code_left != NE_EXPR && code_left != EQ_EXPR
    2166                 :      320886 :                 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
    2167                 :         132 :             warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
    2168                 :             :                         "comparisons like %<X<=Y<=Z%> do not "
    2169                 :             :                         "have their mathematical meaning");
    2170                 :      320622 :           else if (TREE_CODE_CLASS (code_right) == tcc_comparison
    2171                 :           0 :                    && code_right != NE_EXPR && code_right != EQ_EXPR
    2172                 :      320622 :                    && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
    2173                 :           0 :             warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
    2174                 :             :                         "comparisons like %<X<=Y<=Z%> do not "
    2175                 :             :                         "have their mathematical meaning");
    2176                 :             :         }
    2177                 :             :       return;
    2178                 :             :     }
    2179                 :             : }
    2180                 :             : 
    2181                 :             : /* If LABEL (a LABEL_DECL) has not been used, issue a warning.  */
    2182                 :             : 
    2183                 :             : void
    2184                 :       38447 : warn_for_unused_label (tree label)
    2185                 :             : {
    2186                 :       38447 :   if (!TREE_USED (label))
    2187                 :             :     {
    2188                 :         973 :       if (DECL_INITIAL (label))
    2189                 :         956 :         warning (OPT_Wunused_label, "label %q+D defined but not used", label);
    2190                 :             :       else
    2191                 :          17 :         warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
    2192                 :             :     }
    2193                 :       37474 :   else if (asan_sanitize_use_after_scope ())
    2194                 :             :     {
    2195                 :         172 :       if (asan_used_labels == NULL)
    2196                 :          81 :         asan_used_labels = new hash_set<tree> (16);
    2197                 :             : 
    2198                 :         172 :       asan_used_labels->add (label);
    2199                 :             :     }
    2200                 :       38447 : }
    2201                 :             : 
    2202                 :             : /* Warn for division by zero according to the value of DIVISOR.  LOC
    2203                 :             :    is the location of the division operator.  */
    2204                 :             : 
    2205                 :             : void
    2206                 :    10220882 : warn_for_div_by_zero (location_t loc, tree divisor)
    2207                 :             : {
    2208                 :             :   /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
    2209                 :             :      about division by zero.  Do not issue a warning if DIVISOR has a
    2210                 :             :      floating-point type, since we consider 0.0/0.0 a valid way of
    2211                 :             :      generating a NaN.  */
    2212                 :    10220882 :   if (c_inhibit_evaluation_warnings == 0
    2213                 :    10220882 :       && (integer_zerop (divisor) || fixed_zerop (divisor)))
    2214                 :         901 :     warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
    2215                 :    10220881 : }
    2216                 :             : 
    2217                 :             : /* Warn for patterns where memset appears to be used incorrectly.  The
    2218                 :             :    warning location should be LOC.  ARG0, and ARG2 are the first and
    2219                 :             :    last arguments to the call, while LITERAL_ZERO_MASK has a 1 bit for
    2220                 :             :    each argument that was a literal zero.  */
    2221                 :             : 
    2222                 :             : void
    2223                 :      127062 : warn_for_memset (location_t loc, tree arg0, tree arg2,
    2224                 :             :                  int literal_zero_mask)
    2225                 :             : {
    2226                 :      127062 :   arg0 = fold_for_warn (arg0);
    2227                 :      127062 :   arg2 = fold_for_warn (arg2);
    2228                 :             : 
    2229                 :      127062 :   if (warn_memset_transposed_args
    2230                 :        8776 :       && integer_zerop (arg2)
    2231                 :             :       && (literal_zero_mask & (1 << 2)) != 0
    2232                 :      127434 :       && (literal_zero_mask & (1 << 1)) == 0)
    2233                 :         201 :     warning_at (loc, OPT_Wmemset_transposed_args,
    2234                 :             :                 "%<memset%> used with constant zero length "
    2235                 :             :                 "parameter; this could be due to transposed "
    2236                 :             :                 "parameters");
    2237                 :             : 
    2238                 :      127062 :   if (warn_memset_elt_size && TREE_CODE (arg2) == INTEGER_CST)
    2239                 :             :     {
    2240                 :        7365 :       STRIP_NOPS (arg0);
    2241                 :        7365 :       if (TREE_CODE (arg0) == ADDR_EXPR)
    2242                 :        6160 :         arg0 = TREE_OPERAND (arg0, 0);
    2243                 :        7365 :       tree type = TREE_TYPE (arg0);
    2244                 :        7365 :       if (type != NULL_TREE && TREE_CODE (type) == ARRAY_TYPE)
    2245                 :             :         {
    2246                 :        2885 :           tree elt_type = TREE_TYPE (type);
    2247                 :        2885 :           tree domain = TYPE_DOMAIN (type);
    2248                 :        2885 :           if (COMPLETE_TYPE_P (elt_type)
    2249                 :        2881 :               && !integer_onep (TYPE_SIZE_UNIT (elt_type))
    2250                 :        2354 :               && domain != NULL_TREE
    2251                 :        2353 :               && TYPE_MAX_VALUE (domain)
    2252                 :        2353 :               && TYPE_MIN_VALUE (domain)
    2253                 :        2353 :               && integer_zerop (TYPE_MIN_VALUE (domain))
    2254                 :        5238 :               && integer_onep (fold_build2 (MINUS_EXPR, domain,
    2255                 :             :                                             arg2,
    2256                 :             :                                             TYPE_MAX_VALUE (domain))))
    2257                 :          39 :             warning_at (loc, OPT_Wmemset_elt_size,
    2258                 :             :                         "%<memset%> used with length equal to "
    2259                 :             :                         "number of elements without multiplication "
    2260                 :             :                         "by element size");
    2261                 :             :         }
    2262                 :             :     }
    2263                 :      127062 : }
    2264                 :             : 
    2265                 :             : /* Warn for calloc (sizeof (X), n).  */
    2266                 :             : 
    2267                 :             : void
    2268                 :         206 : warn_for_calloc (location_t *sizeof_arg_loc, tree callee,
    2269                 :             :                  vec<tree, va_gc> *params, tree *sizeof_arg, tree attr)
    2270                 :             : {
    2271                 :         206 :   if (!TREE_VALUE (attr) || !TREE_CHAIN (TREE_VALUE (attr)))
    2272                 :             :     return;
    2273                 :             : 
    2274                 :         206 :   int arg1 = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr))) - 1;
    2275                 :         206 :   int arg2
    2276                 :         206 :     = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (TREE_VALUE (attr)))) - 1;
    2277                 :         206 :   if (arg1 < 0
    2278                 :         206 :       || (unsigned) arg1 >= vec_safe_length (params)
    2279                 :         206 :       || arg1 >= 6
    2280                 :         206 :       || arg2 < 0
    2281                 :         206 :       || (unsigned) arg2 >= vec_safe_length (params)
    2282                 :         206 :       || arg2 >= 6
    2283                 :         412 :       || arg1 >= arg2)
    2284                 :             :     return;
    2285                 :             : 
    2286                 :         206 :   if (sizeof_arg[arg1] == NULL_TREE || sizeof_arg[arg2] != NULL_TREE)
    2287                 :             :     return;
    2288                 :             : 
    2289                 :          35 :   if (warning_at (sizeof_arg_loc[arg1], OPT_Wcalloc_transposed_args,
    2290                 :             :                   "%qD sizes specified with %<sizeof%> in the earlier "
    2291                 :             :                   "argument and not in the later argument", callee))
    2292                 :          35 :     inform (sizeof_arg_loc[arg1], "earlier argument should specify number "
    2293                 :             :             "of elements, later size of each element");
    2294                 :             : }
    2295                 :             : 
    2296                 :             : /* Warn for allocator calls where the constant allocated size is smaller
    2297                 :             :    than sizeof (TYPE).  */
    2298                 :             : 
    2299                 :             : void
    2300                 :        1767 : warn_for_alloc_size (location_t loc, tree type, tree call, tree alloc_size)
    2301                 :             : {
    2302                 :        1767 :   if (!TREE_VALUE (alloc_size))
    2303                 :             :     return;
    2304                 :             : 
    2305                 :        1767 :   tree arg1 = TREE_VALUE (TREE_VALUE (alloc_size));
    2306                 :        1767 :   int idx1 = TREE_INT_CST_LOW (arg1) - 1;
    2307                 :        1767 :   if (idx1 < 0 || idx1 >= call_expr_nargs (call))
    2308                 :             :     return;
    2309                 :        1763 :   arg1 = CALL_EXPR_ARG (call, idx1);
    2310                 :        1763 :   if (TREE_CODE (arg1) != INTEGER_CST)
    2311                 :             :     return;
    2312                 :         283 :   if (TREE_CHAIN (TREE_VALUE (alloc_size)))
    2313                 :             :     {
    2314                 :         109 :       tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_VALUE (alloc_size)));
    2315                 :         109 :       int idx2 = TREE_INT_CST_LOW (arg2) - 1;
    2316                 :         109 :       if (idx2 < 0 || idx2 >= call_expr_nargs (call))
    2317                 :             :         return;
    2318                 :         109 :       arg2 = CALL_EXPR_ARG (call, idx2);
    2319                 :         109 :       if (TREE_CODE (arg2) != INTEGER_CST)
    2320                 :             :         return;
    2321                 :          53 :       arg1 = int_const_binop (MULT_EXPR, fold_convert (sizetype, arg1),
    2322                 :          53 :                               fold_convert (sizetype, arg2));
    2323                 :          53 :       if (TREE_CODE (arg1) != INTEGER_CST)
    2324                 :             :         return;
    2325                 :             :     }
    2326                 :         227 :   if (!VOID_TYPE_P (type)
    2327                 :         226 :       && TYPE_SIZE_UNIT (type)
    2328                 :         225 :       && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
    2329                 :         452 :       && tree_int_cst_lt (arg1, TYPE_SIZE_UNIT (type)))
    2330                 :          54 :     warning_at (loc, OPT_Walloc_size,
    2331                 :             :                 "allocation of insufficient size %qE for type %qT with "
    2332                 :          54 :                 "size %qE", arg1, type, TYPE_SIZE_UNIT (type));
    2333                 :             : }
    2334                 :             : 
    2335                 :             : /* Subroutine of build_binary_op. Give warnings for comparisons
    2336                 :             :    between signed and unsigned quantities that may fail. Do the
    2337                 :             :    checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
    2338                 :             :    so that casts will be considered, but default promotions won't
    2339                 :             :    be.
    2340                 :             : 
    2341                 :             :    LOCATION is the location of the comparison operator.
    2342                 :             : 
    2343                 :             :    The arguments of this function map directly to local variables
    2344                 :             :    of build_binary_op.  */
    2345                 :             : 
    2346                 :             : void
    2347                 :      630450 : warn_for_sign_compare (location_t location,
    2348                 :             :                        tree orig_op0, tree orig_op1,
    2349                 :             :                        tree op0, tree op1,
    2350                 :             :                        tree result_type, enum tree_code resultcode)
    2351                 :             : {
    2352                 :      630450 :   if (error_operand_p (orig_op0) || error_operand_p (orig_op1))
    2353                 :           0 :     return;
    2354                 :             : 
    2355                 :      630450 :   int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
    2356                 :      630450 :   int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
    2357                 :      630450 :   int unsignedp0, unsignedp1;
    2358                 :             : 
    2359                 :             :   /* Do not warn if the comparison is being done in a signed type,
    2360                 :             :      since the signed type will only be chosen if it can represent
    2361                 :             :      all the values of the unsigned type.  */
    2362                 :      630450 :   if (!TYPE_UNSIGNED (result_type))
    2363                 :             :     /* OK */;
    2364                 :             :   /* Do not warn if both operands are unsigned.  */
    2365                 :      237469 :   else if (op0_signed == op1_signed)
    2366                 :             :     /* OK */;
    2367                 :             :   else
    2368                 :             :     {
    2369                 :      130621 :       tree sop, uop, base_type;
    2370                 :      130621 :       bool ovf;
    2371                 :             : 
    2372                 :      130621 :       if (op0_signed)
    2373                 :             :         sop = orig_op0, uop = orig_op1;
    2374                 :             :       else
    2375                 :      129844 :         sop = orig_op1, uop = orig_op0;
    2376                 :             : 
    2377                 :      130621 :       sop = fold_for_warn (sop);
    2378                 :      130621 :       uop = fold_for_warn (uop);
    2379                 :             : 
    2380                 :      261242 :       STRIP_TYPE_NOPS (sop);
    2381                 :      130621 :       STRIP_TYPE_NOPS (uop);
    2382                 :      261242 :       base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
    2383                 :      130621 :                    ? TREE_TYPE (result_type) : result_type);
    2384                 :             : 
    2385                 :             :       /* Do not warn if the signed quantity is an unsuffixed integer
    2386                 :             :          literal (or some static constant expression involving such
    2387                 :             :          literals or a conditional expression involving such literals)
    2388                 :             :          and it is non-negative.  */
    2389                 :      130621 :       if (tree_expr_nonnegative_warnv_p (sop, &ovf))
    2390                 :             :         /* OK */;
    2391                 :             :       /* Do not warn if the comparison is an equality operation, the
    2392                 :             :          unsigned quantity is an integral constant, and it would fit
    2393                 :             :          in the result if the result were signed.  */
    2394                 :         321 :       else if (TREE_CODE (uop) == INTEGER_CST
    2395                 :         112 :                && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
    2396                 :         429 :                && int_fits_type_p (uop, c_common_signed_type (base_type)))
    2397                 :             :         /* OK */;
    2398                 :             :       /* In C, do not warn if the unsigned quantity is an enumeration
    2399                 :             :          constant and its maximum value would fit in the result if the
    2400                 :             :          result were signed.  */
    2401                 :          87 :       else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
    2402                 :           1 :                && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
    2403                 :         218 :                && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
    2404                 :           0 :                                    c_common_signed_type (base_type)))
    2405                 :             :         /* OK */;
    2406                 :             :       else
    2407                 :         218 :         warning_at (location, OPT_Wsign_compare,
    2408                 :             :                     "comparison of integer expressions of different "
    2409                 :         218 :                     "signedness: %qT and %qT", TREE_TYPE (orig_op0),
    2410                 :         218 :                     TREE_TYPE (orig_op1));
    2411                 :             :     }
    2412                 :             : 
    2413                 :             :   /* Warn if two unsigned values are being compared in a size larger
    2414                 :             :      than their original size, and one (and only one) is the result of
    2415                 :             :      a `~' operator.  This comparison will always fail.
    2416                 :             : 
    2417                 :             :      Also warn if one operand is a constant, and the constant does not
    2418                 :             :      have all bits set that are set in the ~ operand when it is
    2419                 :             :      extended.  */
    2420                 :             : 
    2421                 :             :   /* bits0 is the bit index of op0 extended to result_type, which will
    2422                 :             :      be always 0 and so all bits above it.  If there is a BIT_NOT_EXPR
    2423                 :             :      in that operand possibly sign or zero extended to op0 and then
    2424                 :             :      possibly further sign or zero extended to result_type, bits0 will
    2425                 :             :      be the precision of result type if all the extensions involved
    2426                 :             :      if any are sign extensions, and will be the place of the innermost
    2427                 :             :      zero extension otherwise.  We warn only if BIT_NOT_EXPR's operand is
    2428                 :             :      zero extended from some even smaller precision, in that case after
    2429                 :             :      BIT_NOT_EXPR some bits below bits0 will be guaranteed to be set.
    2430                 :             :      Similarly for bits1.  */
    2431                 :      630450 :   int bits0 = TYPE_PRECISION (result_type);
    2432                 :      630450 :   if (TYPE_UNSIGNED (TREE_TYPE (op0)))
    2433                 :      238342 :     bits0 = TYPE_PRECISION (TREE_TYPE (op0));
    2434                 :      630450 :   tree arg0 = c_common_get_narrower (op0, &unsignedp0);
    2435                 :      630450 :   if (TYPE_PRECISION (TREE_TYPE (arg0)) == TYPE_PRECISION (TREE_TYPE (op0)))
    2436                 :      614480 :     unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
    2437                 :       15970 :   else if (unsignedp0)
    2438                 :        5482 :     bits0 = TYPE_PRECISION (TREE_TYPE (arg0));
    2439                 :      630450 :   op0 = arg0;
    2440                 :      630450 :   int bits1 = TYPE_PRECISION (result_type);
    2441                 :      630450 :   if (TYPE_UNSIGNED (TREE_TYPE (op1)))
    2442                 :      172044 :     bits1 = TYPE_PRECISION (TREE_TYPE (op1));
    2443                 :      630450 :   tree arg1 = c_common_get_narrower (op1, &unsignedp1);
    2444                 :      630450 :   if (TYPE_PRECISION (TREE_TYPE (arg1)) == TYPE_PRECISION (TREE_TYPE (op1)))
    2445                 :      618124 :     unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
    2446                 :       12326 :   else if (unsignedp1)
    2447                 :        3357 :     bits1 = TYPE_PRECISION (TREE_TYPE (arg1));
    2448                 :      630450 :   op1 = arg1;
    2449                 :             : 
    2450                 :      630450 :   if ((TREE_CODE (op0) == BIT_NOT_EXPR)
    2451                 :      630450 :       ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
    2452                 :             :     {
    2453                 :         198 :       if (TREE_CODE (op1) == BIT_NOT_EXPR)
    2454                 :             :         {
    2455                 :         135 :           std::swap (op0, op1);
    2456                 :         135 :           std::swap (unsignedp0, unsignedp1);
    2457                 :         135 :           std::swap (bits0, bits1);
    2458                 :             :         }
    2459                 :             : 
    2460                 :         198 :       int unsignedp;
    2461                 :         198 :       arg0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp);
    2462                 :             : 
    2463                 :             :       /* For these warnings, we need BIT_NOT_EXPR operand to be
    2464                 :             :          zero extended from narrower type to BIT_NOT_EXPR's type.
    2465                 :             :          In that case, all those bits above the narrower's type
    2466                 :             :          are after BIT_NOT_EXPR set to 1.  */
    2467                 :         198 :       if (tree_fits_shwi_p (op1))
    2468                 :             :         {
    2469                 :          43 :           HOST_WIDE_INT constant = tree_to_shwi (op1);
    2470                 :          43 :           unsigned int bits = TYPE_PRECISION (TREE_TYPE (arg0));
    2471                 :          43 :           if (unsignedp
    2472                 :          38 :               && bits < TYPE_PRECISION (TREE_TYPE (op0))
    2473                 :          78 :               && bits < HOST_BITS_PER_WIDE_INT)
    2474                 :             :             {
    2475                 :          35 :               HOST_WIDE_INT mask = HOST_WIDE_INT_M1U << bits;
    2476                 :          35 :               if (bits0 < HOST_BITS_PER_WIDE_INT)
    2477                 :          35 :                 mask &= ~(HOST_WIDE_INT_M1U << bits0);
    2478                 :          35 :               if ((mask & constant) != mask)
    2479                 :             :                 {
    2480                 :          23 :                   if (constant == 0)
    2481                 :          11 :                     warning_at (location, OPT_Wsign_compare,
    2482                 :             :                                 "promoted bitwise complement of an unsigned "
    2483                 :             :                                 "value is always nonzero");
    2484                 :             :                   else
    2485                 :          12 :                     warning_at (location, OPT_Wsign_compare,
    2486                 :             :                                 "comparison of promoted bitwise complement "
    2487                 :             :                                 "of an unsigned value with constant");
    2488                 :             :                 }
    2489                 :             :             }
    2490                 :             :         }
    2491                 :         155 :       else if ((TYPE_PRECISION (TREE_TYPE (arg0))
    2492                 :         155 :                 < TYPE_PRECISION (TREE_TYPE (op0)))
    2493                 :          19 :                && unsignedp
    2494                 :          19 :                && unsignedp1
    2495                 :         174 :                && TYPE_PRECISION (TREE_TYPE (op1)) < bits0)
    2496                 :          13 :         warning_at (location, OPT_Wsign_compare,
    2497                 :             :                     "comparison of promoted bitwise complement "
    2498                 :             :                     "of an unsigned value with unsigned");
    2499                 :             :     }
    2500                 :             : }
    2501                 :             : 
    2502                 :             : /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
    2503                 :             :    type via c_common_type.  If -Wdouble-promotion is in use, and the
    2504                 :             :    conditions for warning have been met, issue a warning.  GMSGID is
    2505                 :             :    the warning message.  It must have two %T specifiers for the type
    2506                 :             :    that was converted (generally "float") and the type to which it was
    2507                 :             :    converted (generally "double), respectively.  LOC is the location
    2508                 :             :    to which the warning should refer.  */
    2509                 :             : 
    2510                 :             : void
    2511                 :    96932696 : do_warn_double_promotion (tree result_type, tree type1, tree type2,
    2512                 :             :                          const char *gmsgid, location_t loc)
    2513                 :             : {
    2514                 :    96932696 :   tree source_type;
    2515                 :             : 
    2516                 :    96932696 :   if (!warn_double_promotion)
    2517                 :             :     return;
    2518                 :             :   /* If the conversion will not occur at run-time, there is no need to
    2519                 :             :      warn about it.  */
    2520                 :         159 :   if (c_inhibit_evaluation_warnings)
    2521                 :             :     return;
    2522                 :             :   /* If an invalid conversion has occurred, don't warn.  */
    2523                 :         134 :   if (result_type == error_mark_node)
    2524                 :             :     return;
    2525                 :         133 :   if (TYPE_MAIN_VARIANT (result_type) != double_type_node
    2526                 :         133 :       && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
    2527                 :             :     return;
    2528                 :         115 :   if (TYPE_MAIN_VARIANT (type1) == float_type_node
    2529                 :         115 :       || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
    2530                 :             :     source_type = type1;
    2531                 :          65 :   else if (TYPE_MAIN_VARIANT (type2) == float_type_node
    2532                 :          65 :            || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
    2533                 :             :     source_type = type2;
    2534                 :             :   else
    2535                 :             :     return;
    2536                 :          55 :   warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
    2537                 :             : }
    2538                 :             : 
    2539                 :             : /* Possibly warn about unused parameters.  */
    2540                 :             : 
    2541                 :             : void
    2542                 :     2884243 : do_warn_unused_parameter (tree fn)
    2543                 :             : {
    2544                 :     2884243 :   tree decl;
    2545                 :             : 
    2546                 :     2884243 :   for (decl = DECL_ARGUMENTS (fn);
    2547                 :    10427726 :        decl; decl = DECL_CHAIN (decl))
    2548                 :     7543483 :     if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
    2549                 :       68102 :         && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
    2550                 :     7543716 :         && !warning_suppressed_p (decl, OPT_Wunused_parameter))
    2551                 :         132 :       warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter,
    2552                 :             :                   "unused parameter %qD", decl);
    2553                 :     2884243 : }
    2554                 :             : 
    2555                 :             : /* If DECL is a typedef that is declared in the current function,
    2556                 :             :    record it for the purpose of -Wunused-local-typedefs.  */
    2557                 :             : 
    2558                 :             : void
    2559                 :   218867750 : record_locally_defined_typedef (tree decl)
    2560                 :             : {
    2561                 :   218867750 :   struct c_language_function *l;
    2562                 :             : 
    2563                 :   218867750 :   if (!warn_unused_local_typedefs
    2564                 :     3155930 :       || cfun == NULL
    2565                 :             :       /* if this is not a locally defined typedef then we are not
    2566                 :             :          interested.  */
    2567                 :       48988 :       || !is_typedef_decl (decl)
    2568                 :   218913977 :       || !decl_function_context (decl))
    2569                 :   218821523 :     return;
    2570                 :             : 
    2571                 :       46227 :   l = (struct c_language_function *) cfun->language;
    2572                 :       46227 :   vec_safe_push (l->local_typedefs, decl);
    2573                 :             : }
    2574                 :             : 
    2575                 :             : /* If T is a TYPE_DECL declared locally, mark it as used.  */
    2576                 :             : 
    2577                 :             : void
    2578                 :  4690856728 : maybe_record_typedef_use (tree t)
    2579                 :             : {
    2580                 :  4690856728 :   if (!is_typedef_decl (t))
    2581                 :             :     return;
    2582                 :             : 
    2583                 :   794067944 :   TREE_USED (t) = true;
    2584                 :             : }
    2585                 :             : 
    2586                 :             : /* Warn if there are some unused locally defined typedefs in the
    2587                 :             :    current function. */
    2588                 :             : 
    2589                 :             : void
    2590                 :   160173538 : maybe_warn_unused_local_typedefs (void)
    2591                 :             : {
    2592                 :   160173538 :   int i;
    2593                 :   160173538 :   tree decl;
    2594                 :             :   /* The number of times we have emitted -Wunused-local-typedefs
    2595                 :             :      warnings.  If this is different from errorcount, that means some
    2596                 :             :      unrelated errors have been issued.  In which case, we'll avoid
    2597                 :             :      emitting "unused-local-typedefs" warnings.  */
    2598                 :   160173538 :   static int unused_local_typedefs_warn_count;
    2599                 :   160173538 :   struct c_language_function *l;
    2600                 :             : 
    2601                 :   160173538 :   if (cfun == NULL)
    2602                 :   160173538 :     return;
    2603                 :             : 
    2604                 :   160173538 :   if ((l = (struct c_language_function *) cfun->language) == NULL)
    2605                 :             :     return;
    2606                 :             : 
    2607                 :   131206501 :   if (warn_unused_local_typedefs
    2608                 :   131206501 :       && errorcount == unused_local_typedefs_warn_count)
    2609                 :             :     {
    2610                 :     4559320 :       FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
    2611                 :       46216 :         if (!TREE_USED (decl))
    2612                 :         219 :           warning_at (DECL_SOURCE_LOCATION (decl),
    2613                 :             :                       OPT_Wunused_local_typedefs,
    2614                 :             :                       "typedef %qD locally defined but not used", decl);
    2615                 :     4513104 :       unused_local_typedefs_warn_count = errorcount;
    2616                 :             :     }
    2617                 :             : 
    2618                 :   131232803 :   vec_free (l->local_typedefs);
    2619                 :             : }
    2620                 :             : 
    2621                 :             : /* If we're creating an if-else-if condition chain, first see if we
    2622                 :             :    already have this COND in the CHAIN.  If so, warn and don't add COND
    2623                 :             :    into the vector, otherwise add the COND there.  LOC is the location
    2624                 :             :    of COND.  */
    2625                 :             : 
    2626                 :             : void
    2627                 :         386 : warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain)
    2628                 :             : {
    2629                 :             :   /* No chain has been created yet.  Do nothing.  */
    2630                 :         386 :   if (*chain == NULL)
    2631                 :         386 :     return;
    2632                 :             : 
    2633                 :         233 :   if (TREE_SIDE_EFFECTS (cond) || instantiation_dependent_expression_p (cond))
    2634                 :             :     {
    2635                 :             :       /* Uh-oh!  This condition has a side-effect, thus invalidates
    2636                 :             :          the whole chain.  */
    2637                 :          40 :       delete *chain;
    2638                 :          40 :       *chain = NULL;
    2639                 :          40 :       return;
    2640                 :             :     }
    2641                 :             : 
    2642                 :             :   unsigned int ix;
    2643                 :             :   tree t;
    2644                 :         496 :   bool found = false;
    2645                 :         496 :   FOR_EACH_VEC_ELT (**chain, ix, t)
    2646                 :         418 :     if (operand_equal_p (cond, t, 0))
    2647                 :             :       {
    2648                 :         115 :         auto_diagnostic_group d;
    2649                 :         115 :         if (warning_at (loc, OPT_Wduplicated_cond,
    2650                 :             :                         "duplicated %<if%> condition"))
    2651                 :         115 :           inform (EXPR_LOCATION (t), "previously used here");
    2652                 :         115 :         found = true;
    2653                 :         115 :         break;
    2654                 :         115 :       }
    2655                 :             : 
    2656                 :         115 :   if (!found
    2657                 :          78 :       && !CONSTANT_CLASS_P (cond)
    2658                 :             :       /* Don't infinitely grow the chain.  */
    2659                 :          68 :       && (*chain)->length () < 512)
    2660                 :          68 :     (*chain)->safe_push (cond);
    2661                 :             : }
    2662                 :             : 
    2663                 :             : /* Check and possibly warn if two declarations have contradictory
    2664                 :             :    attributes, such as always_inline vs. noinline.  */
    2665                 :             : 
    2666                 :             : bool
    2667                 :    16670959 : diagnose_mismatched_attributes (tree olddecl, tree newdecl)
    2668                 :             : {
    2669                 :    16670959 :   bool warned = false;
    2670                 :             : 
    2671                 :    16670959 :   tree a1 = lookup_attribute ("optimize", DECL_ATTRIBUTES (olddecl));
    2672                 :    16670959 :   tree a2 = lookup_attribute ("optimize", DECL_ATTRIBUTES (newdecl));
    2673                 :             :   /* An optimization attribute applied on a declaration after the
    2674                 :             :      definition is likely not what the user wanted.  */
    2675                 :    16670959 :   if (a2 != NULL_TREE
    2676                 :      100309 :       && DECL_SAVED_TREE (olddecl) != NULL_TREE
    2677                 :    16671000 :       && (a1 == NULL_TREE || !attribute_list_equal (a1, a2)))
    2678                 :          31 :     warned |= warning (OPT_Wattributes,
    2679                 :             :                        "optimization attribute on %qD follows "
    2680                 :             :                        "definition but the attribute doesn%'t match",
    2681                 :             :                        newdecl);
    2682                 :             : 
    2683                 :             :   /* Diagnose inline __attribute__ ((noinline)) which is silly.  */
    2684                 :    16670959 :   if (DECL_DECLARED_INLINE_P (newdecl)
    2685                 :     3764944 :       && DECL_UNINLINABLE (olddecl)
    2686                 :    16670976 :       && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
    2687                 :          17 :     warned |= warning (OPT_Wattributes, "inline declaration of %qD follows "
    2688                 :             :                        "declaration with attribute %<noinline%>", newdecl);
    2689                 :    16670942 :   else if (DECL_DECLARED_INLINE_P (olddecl)
    2690                 :      559596 :            && DECL_UNINLINABLE (newdecl)
    2691                 :    16670957 :            && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
    2692                 :          15 :     warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
    2693                 :             :                        "%<noinline%> follows inline declaration", newdecl);
    2694                 :             : 
    2695                 :    16670959 :   return warned;
    2696                 :             : }
    2697                 :             : 
    2698                 :             : /* Warn if signed left shift overflows.  We don't warn
    2699                 :             :    about left-shifting 1 into the sign bit in C++14; cf.
    2700                 :             :    <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
    2701                 :             :    and don't warn for C++20 at all, as signed left shifts never
    2702                 :             :    overflow.
    2703                 :             :    LOC is a location of the shift; OP0 and OP1 are the operands.
    2704                 :             :    Return true if an overflow is detected, false otherwise.  */
    2705                 :             : 
    2706                 :             : bool
    2707                 :     2528125 : maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
    2708                 :             : {
    2709                 :     2528125 :   if (TREE_CODE (op0) != INTEGER_CST
    2710                 :     2528092 :       || TREE_CODE (op1) != INTEGER_CST)
    2711                 :             :     return false;
    2712                 :             : 
    2713                 :             :   /* match.pd could have narrowed the left shift already,
    2714                 :             :      take type promotion into account.  */
    2715                 :     2528062 :   tree type0 = lang_hooks.types.type_promotes_to (TREE_TYPE (op0));
    2716                 :     2528062 :   unsigned int prec0 = TYPE_PRECISION (type0);
    2717                 :             : 
    2718                 :             :   /* Left-hand operand must be signed.  */
    2719                 :     2528062 :   if (TYPE_OVERFLOW_WRAPS (type0) || cxx_dialect >= cxx20)
    2720                 :             :     return false;
    2721                 :             : 
    2722                 :     1245694 :   signop sign = SIGNED;
    2723                 :     1245694 :   if (TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (type0))
    2724                 :          16 :     sign = TYPE_SIGN (TREE_TYPE (op0));
    2725                 :     1245694 :   unsigned int min_prec = (wi::min_precision (wi::to_wide (op0), sign)
    2726                 :     1245694 :                            + TREE_INT_CST_LOW (op1));
    2727                 :             :   /* Handle the case of left-shifting 1 into the sign bit.
    2728                 :             :    * However, shifting 1 _out_ of the sign bit, as in
    2729                 :             :    * INT_MIN << 1, is considered an overflow.
    2730                 :             :    */
    2731                 :     1245694 :   if (!tree_int_cst_sign_bit (op0) && min_prec == prec0 + 1)
    2732                 :             :     {
    2733                 :             :       /* Never warn for C++14 onwards.  */
    2734                 :       15157 :       if (cxx_dialect >= cxx14)
    2735                 :             :         return false;
    2736                 :             :       /* Otherwise only if -Wshift-overflow=2.  But return
    2737                 :             :          true to signal an overflow for the sake of integer
    2738                 :             :          constant expressions.  */
    2739                 :       14699 :       if (warn_shift_overflow < 2)
    2740                 :             :         return true;
    2741                 :             :     }
    2742                 :             : 
    2743                 :     1230605 :   bool overflowed = min_prec > prec0;
    2744                 :     1230605 :   if (overflowed && c_inhibit_evaluation_warnings == 0)
    2745                 :         553 :     warning_at (loc, OPT_Wshift_overflow_,
    2746                 :             :                 "result of %qE requires %u bits to represent, "
    2747                 :             :                 "but %qT only has %u bits",
    2748                 :             :                 build2_loc (loc, LSHIFT_EXPR, type0,
    2749                 :             :                             fold_convert (type0, op0), op1),
    2750                 :             :                 min_prec, type0, prec0);
    2751                 :             : 
    2752                 :             :   return overflowed;
    2753                 :             : }
    2754                 :             : 
    2755                 :             : /* Warn about boolean expression compared with an integer value different
    2756                 :             :    from true/false.  Warns also e.g. about "(i1 == i2) == 2".
    2757                 :             :    LOC is the location of the comparison, CODE is its code, OP0 and OP1
    2758                 :             :    are the operands of the comparison.  The caller must ensure that
    2759                 :             :    either operand is a boolean expression.  */
    2760                 :             : 
    2761                 :             : void
    2762                 :       79047 : maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
    2763                 :             :                          tree op1)
    2764                 :             : {
    2765                 :       79047 :   if (TREE_CODE_CLASS (code) != tcc_comparison)
    2766                 :             :     return;
    2767                 :             : 
    2768                 :       79047 :   tree f, cst;
    2769                 :       79047 :   if (f = fold_for_warn (op0),
    2770                 :       79047 :       TREE_CODE (f) == INTEGER_CST)
    2771                 :             :     cst = op0 = f;
    2772                 :       73588 :   else if (f = fold_for_warn (op1),
    2773                 :       73588 :            TREE_CODE (f) == INTEGER_CST)
    2774                 :             :     cst = op1 = f;
    2775                 :             :   else
    2776                 :             :     return;
    2777                 :             : 
    2778                 :       74819 :   if (!integer_zerop (cst) && !integer_onep (cst))
    2779                 :             :     {
    2780                 :         344 :       int sign = (TREE_CODE (op0) == INTEGER_CST
    2781                 :         344 :                  ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
    2782                 :         344 :       if (code == EQ_EXPR
    2783                 :         294 :           || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
    2784                 :         221 :           || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
    2785                 :         171 :         warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
    2786                 :             :                     "with boolean expression is always false", cst);
    2787                 :             :       else
    2788                 :         173 :         warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
    2789                 :             :                     "with boolean expression is always true", cst);
    2790                 :             :     }
    2791                 :       74475 :   else if (integer_zerop (cst) || integer_onep (cst))
    2792                 :             :     {
    2793                 :             :       /* If the non-constant operand isn't of a boolean type, we
    2794                 :             :          don't want to warn here.  */
    2795                 :       74475 :       tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
    2796                 :             :       /* Handle booleans promoted to integers.  */
    2797                 :       74475 :       if (bool_promoted_to_int_p (noncst))
    2798                 :             :         /* Warn.  */;
    2799                 :       74475 :       else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
    2800                 :       74475 :                && !truth_value_p (TREE_CODE (noncst)))
    2801                 :             :         return;
    2802                 :             :       /* Do some magic to get the right diagnostics.  */
    2803                 :       71282 :       bool flag = TREE_CODE (op0) == INTEGER_CST;
    2804                 :       71282 :       flag = integer_zerop (cst) ? flag : !flag;
    2805                 :       71282 :       if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
    2806                 :          89 :         warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
    2807                 :             :                     "with boolean expression is always true", cst);
    2808                 :       71193 :       else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
    2809                 :         115 :         warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
    2810                 :             :                     "with boolean expression is always false", cst);
    2811                 :             :     }
    2812                 :             : }
    2813                 :             : 
    2814                 :             : /* Warn if an argument at position param_pos is passed to a
    2815                 :             :    restrict-qualified param, and it aliases with another argument.
    2816                 :             :    Return true if a warning has been issued.  */
    2817                 :             : 
    2818                 :             : bool
    2819                 :      117961 : warn_for_restrict (unsigned param_pos, tree *argarray, unsigned nargs)
    2820                 :             : {
    2821                 :      117961 :   tree arg = argarray[param_pos];
    2822                 :      117961 :   if (TREE_VISITED (arg) || integer_zerop (arg))
    2823                 :       53898 :     return false;
    2824                 :             : 
    2825                 :       64063 :   location_t loc = EXPR_LOC_OR_LOC (arg, input_location);
    2826                 :       64063 :   gcc_rich_location richloc (loc);
    2827                 :             : 
    2828                 :       64063 :   unsigned i;
    2829                 :       64063 :   auto_vec<int, 16> arg_positions;
    2830                 :             : 
    2831                 :      268152 :   for (i = 0; i < nargs; i++)
    2832                 :             :     {
    2833                 :      204089 :       if (i == param_pos)
    2834                 :       64063 :         continue;
    2835                 :             : 
    2836                 :      140026 :       tree current_arg = argarray[i];
    2837                 :      140026 :       if (operand_equal_p (arg, current_arg, 0))
    2838                 :             :         {
    2839                 :          82 :           TREE_VISITED (current_arg) = 1;
    2840                 :          82 :           arg_positions.safe_push (i + 1);
    2841                 :             :         }
    2842                 :             :     }
    2843                 :             : 
    2844                 :      128126 :   if (arg_positions.is_empty ())
    2845                 :             :     return false;
    2846                 :             : 
    2847                 :             :   int pos;
    2848                 :         159 :   FOR_EACH_VEC_ELT (arg_positions, i, pos)
    2849                 :             :     {
    2850                 :          82 :       arg = argarray[pos - 1];
    2851                 :          82 :       if (EXPR_HAS_LOCATION (arg))
    2852                 :          59 :         richloc.add_range (EXPR_LOCATION (arg));
    2853                 :             :     }
    2854                 :             : 
    2855                 :         231 :   return warning_n (&richloc, OPT_Wrestrict, arg_positions.length (),
    2856                 :             :                     "passing argument %i to %qs-qualified parameter"
    2857                 :             :                     " aliases with argument %Z",
    2858                 :             :                     "passing argument %i to %qs-qualified parameter"
    2859                 :             :                     " aliases with arguments %Z",
    2860                 :             :                     param_pos + 1, "restrict", arg_positions.address (),
    2861                 :          77 :                     arg_positions.length ());
    2862                 :       64063 : }
    2863                 :             : 
    2864                 :             : /* Callback function to determine whether an expression TP or one of its
    2865                 :             :    subexpressions comes from macro expansion.  Used to suppress bogus
    2866                 :             :    warnings.  */
    2867                 :             : 
    2868                 :             : static tree
    2869                 :        3051 : expr_from_macro_expansion_r (tree *tp, int *, void *)
    2870                 :             : {
    2871                 :        3051 :   if (CAN_HAVE_LOCATION_P (*tp)
    2872                 :        4885 :       && from_macro_expansion_at (EXPR_LOCATION (*tp)))
    2873                 :          25 :     return integer_zero_node;
    2874                 :             : 
    2875                 :             :   return NULL_TREE;
    2876                 :             : }
    2877                 :             : 
    2878                 :             : /* Possibly warn when an if-else has identical branches.  */
    2879                 :             : 
    2880                 :             : static void
    2881                 :         560 : do_warn_duplicated_branches (tree expr)
    2882                 :             : {
    2883                 :         560 :   tree thenb = COND_EXPR_THEN (expr);
    2884                 :         560 :   tree elseb = COND_EXPR_ELSE (expr);
    2885                 :             : 
    2886                 :             :   /* Don't bother if any of the branches is missing.  */
    2887                 :         560 :   if (thenb == NULL_TREE || elseb == NULL_TREE)
    2888                 :          38 :     return;
    2889                 :             : 
    2890                 :             :   /* And don't warn for empty statements.  */
    2891                 :         551 :   if (TREE_CODE (thenb) == NOP_EXPR
    2892                 :          14 :       && TREE_TYPE (thenb) == void_type_node
    2893                 :         565 :       && TREE_OPERAND (thenb, 0) == size_zero_node)
    2894                 :             :     return;
    2895                 :             : 
    2896                 :             :   /* ... or empty branches.  */
    2897                 :         537 :   if (TREE_CODE (thenb) == STATEMENT_LIST
    2898                 :         537 :       && STATEMENT_LIST_HEAD (thenb) == NULL)
    2899                 :             :     return;
    2900                 :             : 
    2901                 :             :   /* Compute the hash of the then branch.  */
    2902                 :         522 :   inchash::hash hstate0 (0);
    2903                 :         522 :   inchash::add_expr (thenb, hstate0);
    2904                 :         522 :   hashval_t h0 = hstate0.end ();
    2905                 :             : 
    2906                 :             :   /* Compute the hash of the else branch.  */
    2907                 :         522 :   inchash::hash hstate1 (0);
    2908                 :         522 :   inchash::add_expr (elseb, hstate1);
    2909                 :         522 :   hashval_t h1 = hstate1.end ();
    2910                 :             : 
    2911                 :             :   /* Compare the hashes.  */
    2912                 :         522 :   if (h0 == h1
    2913                 :         223 :       && operand_equal_p (thenb, elseb, OEP_LEXICOGRAPHIC
    2914                 :             :                                         | OEP_ADDRESS_OF_SAME_FIELD)
    2915                 :             :       /* Don't warn if any of the branches or their subexpressions comes
    2916                 :             :          from a macro.  */
    2917                 :         223 :       && !walk_tree_without_duplicates (&thenb, expr_from_macro_expansion_r,
    2918                 :             :                                         NULL)
    2919                 :         725 :       && !walk_tree_without_duplicates (&elseb, expr_from_macro_expansion_r,
    2920                 :             :                                         NULL))
    2921                 :         198 :     warning_at (EXPR_LOCATION (expr), OPT_Wduplicated_branches,
    2922                 :             :                 "this condition has identical branches");
    2923                 :             : }
    2924                 :             : 
    2925                 :             : /* Callback for c_genericize to implement -Wduplicated-branches.  */
    2926                 :             : 
    2927                 :             : tree
    2928                 :        7496 : do_warn_duplicated_branches_r (tree *tp, int *, void *)
    2929                 :             : {
    2930                 :        7496 :   if (TREE_CODE (*tp) == COND_EXPR)
    2931                 :         560 :     do_warn_duplicated_branches (*tp);
    2932                 :        7496 :   return NULL_TREE;
    2933                 :             : }
    2934                 :             : 
    2935                 :             : /* Implementation of -Wmultistatement-macros.  This warning warns about
    2936                 :             :    cases when a macro expands to multiple statements not wrapped in
    2937                 :             :    do {} while (0) or ({ }) and is used as a body of if/else/for/while
    2938                 :             :    conditionals.  For example,
    2939                 :             : 
    2940                 :             :    #define DOIT x++; y++
    2941                 :             : 
    2942                 :             :    if (c)
    2943                 :             :      DOIT;
    2944                 :             : 
    2945                 :             :    will increment y unconditionally.
    2946                 :             : 
    2947                 :             :    BODY_LOC is the location of the first token in the body after labels
    2948                 :             :    have been parsed, NEXT_LOC is the location of the next token after the
    2949                 :             :    body of the conditional has been parsed, and GUARD_LOC is the location
    2950                 :             :    of the conditional.  */
    2951                 :             : 
    2952                 :             : void
    2953                 :    21434185 : warn_for_multistatement_macros (location_t body_loc, location_t next_loc,
    2954                 :             :                                 location_t guard_loc, enum rid keyword)
    2955                 :             : {
    2956                 :    21434185 :   if (!warn_multistatement_macros)
    2957                 :    21434050 :     return;
    2958                 :             : 
    2959                 :             :   /* Ain't got time to waste.  We only care about macros here.  */
    2960                 :      378626 :   if (!from_macro_expansion_at (body_loc)
    2961                 :      493067 :       || !from_macro_expansion_at (next_loc))
    2962                 :      271867 :     return;
    2963                 :             : 
    2964                 :             :   /* Let's skip macros defined in system headers.  */
    2965                 :      106759 :   if (in_system_header_at (body_loc)
    2966                 :      204523 :       || in_system_header_at (next_loc))
    2967                 :        9049 :     return;
    2968                 :             : 
    2969                 :             :   /* Find the actual tokens in the macro definition.  BODY_LOC and
    2970                 :             :      NEXT_LOC have to come from the same spelling location, but they
    2971                 :             :      will resolve to different locations in the context of the macro
    2972                 :             :      definition.  */
    2973                 :       97710 :   location_t body_loc_exp
    2974                 :       97710 :     = linemap_resolve_location (line_table, body_loc,
    2975                 :             :                                 LRK_MACRO_DEFINITION_LOCATION, NULL);
    2976                 :       97710 :   location_t next_loc_exp
    2977                 :       97710 :     = linemap_resolve_location (line_table, next_loc,
    2978                 :             :                                 LRK_MACRO_DEFINITION_LOCATION, NULL);
    2979                 :       97710 :   location_t guard_loc_exp
    2980                 :       97710 :     = linemap_resolve_location (line_table, guard_loc,
    2981                 :             :                                 LRK_MACRO_DEFINITION_LOCATION, NULL);
    2982                 :             : 
    2983                 :             :   /* These are some funky cases we don't want to warn about.  */
    2984                 :       97710 :   if (body_loc_exp == guard_loc_exp
    2985                 :       97710 :       || next_loc_exp == guard_loc_exp
    2986                 :       79543 :       || body_loc_exp == next_loc_exp)
    2987                 :             :     return;
    2988                 :             : 
    2989                 :             :   /* Find the macro maps for the macro expansions.  */
    2990                 :       79451 :   const line_map *body_map = linemap_lookup (line_table, body_loc);
    2991                 :       79451 :   const line_map *next_map = linemap_lookup (line_table, next_loc);
    2992                 :       79451 :   const line_map *guard_map = linemap_lookup (line_table, guard_loc);
    2993                 :             : 
    2994                 :             :   /* Now see if the following token (after the body) is coming from the
    2995                 :             :      same macro expansion.  If it is, it might be a problem.  */
    2996                 :       79451 :   if (body_map != next_map)
    2997                 :             :     return;
    2998                 :             : 
    2999                 :             :   /* The conditional itself must not come from the same expansion, because
    3000                 :             :      we don't want to warn about
    3001                 :             :      #define IF if (x) x++; y++
    3002                 :             :      and similar.  */
    3003                 :       63183 :   if (guard_map == body_map)
    3004                 :             :     return;
    3005                 :             : 
    3006                 :             :   /* Handle the case where NEXT and BODY come from the same expansion while
    3007                 :             :      GUARD doesn't, yet we shouldn't warn.  E.g.
    3008                 :             : 
    3009                 :             :        #define GUARD if (...)
    3010                 :             :        #define GUARD2 GUARD
    3011                 :             : 
    3012                 :             :      and in the definition of another macro:
    3013                 :             : 
    3014                 :             :        GUARD2
    3015                 :             :         foo ();
    3016                 :             :        return 1;
    3017                 :             :    */
    3018                 :         265 :   while (linemap_macro_expansion_map_p (guard_map))
    3019                 :             :     {
    3020                 :         130 :       const line_map_macro *mm = linemap_check_macro (guard_map);
    3021                 :         130 :       guard_loc_exp = mm->get_expansion_point_location ();
    3022                 :         130 :       guard_map = linemap_lookup (line_table, guard_loc_exp);
    3023                 :         130 :       if (guard_map == body_map)
    3024                 :             :         return;
    3025                 :             :     }
    3026                 :             : 
    3027                 :         135 :   auto_diagnostic_group d;
    3028                 :         135 :   if (warning_at (body_loc, OPT_Wmultistatement_macros,
    3029                 :             :                   "macro expands to multiple statements"))
    3030                 :         135 :     inform (guard_loc, "some parts of macro expansion are not guarded by "
    3031                 :             :             "this %qs clause", guard_tinfo_to_string (keyword));
    3032                 :         135 : }
    3033                 :             : 
    3034                 :             : /* Return struct or union type if the alignment of data member, FIELD,
    3035                 :             :    is less than the alignment of TYPE.  Otherwise, return NULL_TREE.
    3036                 :             :    If RVALUE is true, only arrays evaluate to pointers.  */
    3037                 :             : 
    3038                 :             : static tree
    3039                 :     6304205 : check_alignment_of_packed_member (tree type, tree field, bool rvalue)
    3040                 :             : {
    3041                 :             :   /* Check alignment of the data member.  */
    3042                 :     6304205 :   if (TREE_CODE (field) == FIELD_DECL
    3043                 :     5997005 :       && (DECL_PACKED (field) || TYPE_PACKED (TREE_TYPE (field)))
    3044                 :             :       /* Ignore FIELDs not laid out yet.  */
    3045                 :        1719 :       && DECL_FIELD_OFFSET (field)
    3046                 :     6305912 :       && (!rvalue || TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE))
    3047                 :             :     {
    3048                 :             :       /* Check the expected alignment against the field alignment.  */
    3049                 :        1576 :       unsigned int type_align = min_align_of_type (type);
    3050                 :        1576 :       tree context = DECL_CONTEXT (field);
    3051                 :        1576 :       unsigned int record_align = min_align_of_type (context);
    3052                 :        1576 :       if (record_align < type_align)
    3053                 :             :         return context;
    3054                 :        1201 :       tree field_off = byte_position (field);
    3055                 :        1201 :       if (!multiple_of_p (TREE_TYPE (field_off), field_off,
    3056                 :        2402 :                           size_int (type_align)))
    3057                 :             :         return context;
    3058                 :             :     }
    3059                 :             : 
    3060                 :             :   return NULL_TREE;
    3061                 :             : }
    3062                 :             : 
    3063                 :             : /* Return struct or union type if the right hand value, RHS,
    3064                 :             :    is an address which takes the unaligned address of packed member
    3065                 :             :    of struct or union when assigning to TYPE.
    3066                 :             :    Otherwise, return NULL_TREE.  */
    3067                 :             : 
    3068                 :             : static tree
    3069                 :    54223786 : check_address_of_packed_member (tree type, tree rhs)
    3070                 :             : {
    3071                 :    54223786 :   bool rvalue = true;
    3072                 :    54223786 :   bool indirect = false;
    3073                 :             : 
    3074                 :    54223786 :   if (INDIRECT_REF_P (rhs))
    3075                 :             :     {
    3076                 :      462809 :       rhs = TREE_OPERAND (rhs, 0);
    3077                 :      462809 :       STRIP_NOPS (rhs);
    3078                 :      462809 :       indirect = true;
    3079                 :             :     }
    3080                 :             : 
    3081                 :    54223786 :   if (TREE_CODE (rhs) == ADDR_EXPR)
    3082                 :             :     {
    3083                 :    19501510 :       rhs = TREE_OPERAND (rhs, 0);
    3084                 :    19501510 :       rvalue = indirect;
    3085                 :             :     }
    3086                 :             : 
    3087                 :    54223786 :   if (!POINTER_TYPE_P (type))
    3088                 :             :     return NULL_TREE;
    3089                 :             : 
    3090                 :    54223786 :   type = TREE_TYPE (type);
    3091                 :             : 
    3092                 :    54223786 :   tree context = NULL_TREE;
    3093                 :             : 
    3094                 :             :   /* Check alignment of the object.  */
    3095                 :    59438142 :   while (handled_component_p (rhs))
    3096                 :             :     {
    3097                 :     6667970 :       if (TREE_CODE (rhs) == COMPONENT_REF)
    3098                 :             :         {
    3099                 :     6304205 :           tree field = TREE_OPERAND (rhs, 1);
    3100                 :     6304205 :           context = check_alignment_of_packed_member (type, field, rvalue);
    3101                 :     6304205 :           if (context)
    3102                 :             :             break;
    3103                 :             :         }
    3104                 :     6667475 :       if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE)
    3105                 :             :         rvalue = false;
    3106                 :     6289378 :       if (rvalue)
    3107                 :             :         return NULL_TREE;
    3108                 :     5214356 :       rhs = TREE_OPERAND (rhs, 0);
    3109                 :             :     }
    3110                 :             : 
    3111                 :             :   return context;
    3112                 :             : }
    3113                 :             : 
    3114                 :             : /* Check and warn if the right hand value, RHS,
    3115                 :             :    is an address which takes the unaligned address of packed member
    3116                 :             :    of struct or union when assigning to TYPE.  */
    3117                 :             : 
    3118                 :             : static void
    3119                 :    57082070 : check_and_warn_address_of_packed_member (tree type, tree rhs)
    3120                 :             : {
    3121                 :    57355285 :   bool nop_p = false;
    3122                 :             :   tree orig_rhs;
    3123                 :             : 
    3124                 :             :   do
    3125                 :             :     {
    3126                 :   100343166 :       while (TREE_CODE (rhs) == COMPOUND_EXPR)
    3127                 :      227307 :         rhs = TREE_OPERAND (rhs, 1);
    3128                 :   100115859 :       orig_rhs = rhs;
    3129                 :   100115859 :       STRIP_NOPS (rhs);
    3130                 :   100115859 :       nop_p |= orig_rhs != rhs;
    3131                 :             :     }
    3132                 :   100115859 :   while (orig_rhs != rhs);
    3133                 :             : 
    3134                 :    57355285 :   if (TREE_CODE (rhs) == COND_EXPR)
    3135                 :             :     {
    3136                 :             :       /* Check the THEN path.  */
    3137                 :      273215 :       check_and_warn_address_of_packed_member
    3138                 :      273215 :         (type, TREE_OPERAND (rhs, 1));
    3139                 :             : 
    3140                 :             :       /* Check the ELSE path.  */
    3141                 :      273215 :       check_and_warn_address_of_packed_member
    3142                 :      273215 :         (type, TREE_OPERAND (rhs, 2));
    3143                 :             :     }
    3144                 :             :   else
    3145                 :             :     {
    3146                 :    57082070 :       if (nop_p)
    3147                 :             :         {
    3148                 :    42542214 :           switch (TREE_CODE (rhs))
    3149                 :             :             {
    3150                 :             :             case ADDR_EXPR:
    3151                 :             :               /* Address is taken.   */
    3152                 :             :             case PARM_DECL:
    3153                 :             :             case VAR_DECL:
    3154                 :             :               /* Pointer conversion.  */
    3155                 :             :               break;
    3156                 :             :             case CALL_EXPR:
    3157                 :             :               /* Function call. */
    3158                 :             :               break;
    3159                 :             :             default:
    3160                 :             :               return;
    3161                 :             :             }
    3162                 :             :         }
    3163                 :             : 
    3164                 :    54223786 :       tree context
    3165                 :    54223786 :         = check_address_of_packed_member (type, rhs);
    3166                 :    54223786 :       if (context)
    3167                 :             :         {
    3168                 :         495 :           location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
    3169                 :         495 :           warning_at (loc, OPT_Waddress_of_packed_member,
    3170                 :             :                       "taking address of packed member of %qT may result "
    3171                 :             :                       "in an unaligned pointer value",
    3172                 :             :                       context);
    3173                 :             :         }
    3174                 :             :     }
    3175                 :             : }
    3176                 :             : 
    3177                 :             : /* Warn if the right hand value, RHS,
    3178                 :             :    is an address which takes the unaligned address of packed member
    3179                 :             :    of struct or union when assigning to TYPE.  */
    3180                 :             : 
    3181                 :             : void
    3182                 :   308887080 : warn_for_address_of_packed_member (tree type, tree rhs)
    3183                 :             : {
    3184                 :   308887080 :   if (!warn_address_of_packed_member)
    3185                 :             :     return;
    3186                 :             : 
    3187                 :             :   /* Don't warn if we don't assign RHS to a pointer.  */
    3188                 :   308724030 :   if (!POINTER_TYPE_P (type))
    3189                 :             :     return;
    3190                 :             : 
    3191                 :    56808855 :   check_and_warn_address_of_packed_member (type, rhs);
    3192                 :             : }
    3193                 :             : 
    3194                 :             : /* Return EXPR + 1.  Convenience helper used below.  */
    3195                 :             : 
    3196                 :             : static inline tree
    3197                 :          22 : plus_one (tree expr)
    3198                 :             : {
    3199                 :          22 :   tree type = TREE_TYPE (expr);
    3200                 :          22 :   return fold_build2 (PLUS_EXPR, type, expr, build_int_cst (type, 1));
    3201                 :             : }
    3202                 :             : 
    3203                 :             : /* Try to strip the expressions from around a VLA bound added internally
    3204                 :             :    to make it fit the domain mold, including any casts, and return
    3205                 :             :    the result.  The goal is to obtain the PARM_DECL the VLA bound may
    3206                 :             :    refer to.  */
    3207                 :             : 
    3208                 :             : static tree
    3209                 :        9240 : vla_bound_parm_decl (tree expr)
    3210                 :             : {
    3211                 :        9240 :   if (!expr)
    3212                 :             :     return NULL_TREE;
    3213                 :             : 
    3214                 :        9109 :   if (TREE_CODE (expr) == NOP_EXPR)
    3215                 :         996 :     expr = TREE_OPERAND (expr, 0);
    3216                 :        9109 :   if (TREE_CODE (expr) == PLUS_EXPR
    3217                 :        9109 :       && integer_all_onesp (TREE_OPERAND (expr, 1)))
    3218                 :             :     {
    3219                 :         982 :       expr = TREE_OPERAND (expr, 0);
    3220                 :         982 :       if (TREE_CODE (expr) == NOP_EXPR)
    3221                 :         982 :         expr = TREE_OPERAND (expr, 0);
    3222                 :             :     }
    3223                 :        9109 :   if (TREE_CODE (expr) == SAVE_EXPR)
    3224                 :             :     {
    3225                 :         982 :       expr = TREE_OPERAND (expr, 0);
    3226                 :         982 :       if (TREE_CODE (expr) == NOP_EXPR)
    3227                 :           0 :         expr = TREE_OPERAND (expr, 0);
    3228                 :             :     }
    3229                 :             :   return expr;
    3230                 :             : }
    3231                 :             : 
    3232                 :             : /* Diagnose mismatches in VLA bounds between function parameters NEWPARMS
    3233                 :             :    of pointer types on a redeclaration of a function previously declared
    3234                 :             :    with CURPARMS at ORIGLOC.  */
    3235                 :             : 
    3236                 :             : static void
    3237                 :      273797 : warn_parm_ptrarray_mismatch (location_t origloc, tree curparms, tree newparms)
    3238                 :             : {
    3239                 :             :   /* Maps each named integral parameter seen so far to its position
    3240                 :             :      in the argument list; used to associate VLA sizes with arguments.  */
    3241                 :      273797 :   hash_map<tree, unsigned> curparm2pos;
    3242                 :      273797 :   hash_map<tree, unsigned> newparm2pos;
    3243                 :             : 
    3244                 :      273797 :   unsigned parmpos = 1;
    3245                 :      820490 :   for (tree curp = curparms, newp = newparms; curp && newp;
    3246                 :      546693 :        curp = TREE_CHAIN (curp), newp = TREE_CHAIN (newp), ++parmpos)
    3247                 :             :     {
    3248                 :      546695 :       tree curtyp = TREE_TYPE (curp), newtyp = TREE_TYPE (newp);
    3249                 :      546695 :       if (INTEGRAL_TYPE_P (curtyp))
    3250                 :             :         {
    3251                 :             :           /* Only add named parameters; unnamed ones cannot be referred
    3252                 :             :              to in VLA bounds.  */
    3253                 :      166208 :           if (DECL_NAME (curp))
    3254                 :      155398 :             curparm2pos.put (curp, parmpos);
    3255                 :      166208 :           if (DECL_NAME (newp))
    3256                 :      154378 :             newparm2pos.put (newp, parmpos);
    3257                 :             : 
    3258                 :      712867 :           continue;
    3259                 :             :         }
    3260                 :             : 
    3261                 :             :       /* The parameter types should match at this point so only test one.  */
    3262                 :      380487 :       if (TREE_CODE (curtyp) != POINTER_TYPE)
    3263                 :        7915 :         continue;
    3264                 :             : 
    3265                 :      417936 :       do
    3266                 :             :         {
    3267                 :      417936 :           curtyp = TREE_TYPE (curtyp);
    3268                 :      417936 :           newtyp = TREE_TYPE (newtyp);
    3269                 :             : 
    3270                 :      417936 :           if (!newtyp)
    3271                 :             :             /* Bail on error.  */
    3272                 :           2 :             return;
    3273                 :             :         }
    3274                 :      417934 :       while (TREE_CODE (curtyp) == POINTER_TYPE
    3275                 :      417934 :              && TREE_CODE (newtyp) == POINTER_TYPE);
    3276                 :             : 
    3277                 :      372570 :       if (TREE_CODE (curtyp) != ARRAY_TYPE
    3278                 :          53 :           || TREE_CODE (newtyp) != ARRAY_TYPE)
    3279                 :             :         {
    3280                 :      372517 :           if (curtyp == error_mark_node
    3281                 :      372517 :               || newtyp == error_mark_node)
    3282                 :             :             /* Bail on error.  */
    3283                 :             :             return;
    3284                 :             : 
    3285                 :      372517 :           continue;
    3286                 :             :         }
    3287                 :             : 
    3288                 :          53 :       tree curdom = TYPE_DOMAIN (curtyp), newdom = TYPE_DOMAIN (newtyp);
    3289                 :          53 :       tree curbnd = curdom ? TYPE_MAX_VALUE (curdom) : NULL_TREE;
    3290                 :          53 :       tree newbnd = newdom ? TYPE_MAX_VALUE (newdom) : NULL_TREE;
    3291                 :             : 
    3292                 :          53 :       if (DECL_P (curp))
    3293                 :          53 :         origloc = DECL_SOURCE_LOCATION (curp);
    3294                 :           0 :       else if (EXPR_P (curp) && EXPR_HAS_LOCATION (curp))
    3295                 :           0 :         origloc = EXPR_LOCATION (curp);
    3296                 :             : 
    3297                 :             :       /* The location of the parameter in the current redeclaration.  */
    3298                 :          53 :       location_t newloc = DECL_SOURCE_LOCATION (newp);
    3299                 :          53 :       if (origloc == UNKNOWN_LOCATION)
    3300                 :           0 :         origloc = newloc;
    3301                 :             : 
    3302                 :             :       /* Issue -Warray-parameter unless one or more mismatches involves
    3303                 :             :          a VLA bound; then issue -Wvla-parameter.  */
    3304                 :          53 :       int opt = OPT_Warray_parameter_;
    3305                 :             :       /* Traverse the two array types looking for variable bounds and
    3306                 :             :          comparing the two in each pair for mismatches either in their
    3307                 :             :          positions in the function parameter list or lexicographically
    3308                 :             :          for others.  Record the 1-based parameter position of each
    3309                 :             :          mismatch in BNDVEC, and the location of each parameter in
    3310                 :             :          the mismatch in WARNLOC (for the new parameter list) and
    3311                 :             :          NOTELOC (for the current parameter list).  */
    3312                 :          53 :       unsigned bndpos = 1;
    3313                 :          53 :       auto_vec<int> bndvec;
    3314                 :          53 :       gcc_rich_location warnloc (newloc);
    3315                 :          53 :       gcc_rich_location noteloc (origloc);
    3316                 :         252 :       for ( ; curtyp || newtyp;
    3317                 :             :             ++bndpos,
    3318                 :         146 :               curbnd = curdom ? TYPE_MAX_VALUE (curdom) : NULL_TREE,
    3319                 :         146 :               newbnd = newdom ? TYPE_MAX_VALUE (newdom) : NULL_TREE)
    3320                 :             :         {
    3321                 :             :           /* Try to strip each bound down to the PARM_DECL if it does
    3322                 :             :              correspond to one.  Either bound can be null if it's
    3323                 :             :              unspecified (i.e., has the [*] form).  */
    3324                 :         146 :           curbnd = vla_bound_parm_decl (curbnd);
    3325                 :         146 :           newbnd = vla_bound_parm_decl (newbnd);
    3326                 :             : 
    3327                 :             :           /* Peel the current bound off CURTYP and NEWTYP, skipping
    3328                 :             :              over any subsequent pointer types.  */
    3329                 :         146 :           if (curtyp && TREE_CODE (curtyp) == ARRAY_TYPE)
    3330                 :             :             {
    3331                 :         103 :               do
    3332                 :         103 :                 curtyp = TREE_TYPE (curtyp);
    3333                 :         103 :               while (TREE_CODE (curtyp) == POINTER_TYPE);
    3334                 :          93 :               if (TREE_CODE (curtyp) == ARRAY_TYPE)
    3335                 :          40 :                 curdom = TYPE_DOMAIN (curtyp);
    3336                 :             :               else
    3337                 :             :                 curdom = NULL_TREE;
    3338                 :             :             }
    3339                 :             :           else
    3340                 :             :             curtyp = NULL_TREE;
    3341                 :             : 
    3342                 :         146 :           if (newtyp && TREE_CODE (newtyp) == ARRAY_TYPE)
    3343                 :             :             {
    3344                 :         103 :               do
    3345                 :         103 :                 newtyp = TREE_TYPE (newtyp);
    3346                 :         103 :               while (TREE_CODE (newtyp) == POINTER_TYPE);
    3347                 :          93 :               if (TREE_CODE (newtyp) == ARRAY_TYPE)
    3348                 :          40 :                 newdom = TYPE_DOMAIN (newtyp);
    3349                 :             :               else
    3350                 :             :                 newdom = NULL_TREE;
    3351                 :             :             }
    3352                 :             :           else
    3353                 :             :             newtyp = NULL_TREE;
    3354                 :             : 
    3355                 :             :           /* Move on to the next bound if this one is unspecified.  */
    3356                 :         146 :           if (!curbnd && !newbnd)
    3357                 :          53 :             continue;
    3358                 :             : 
    3359                 :             :           /* Try to find each bound in the parameter list.  */
    3360                 :          93 :           const unsigned* const pcurbndpos = curparm2pos.get (curbnd);
    3361                 :          93 :           const unsigned* const pnewbndpos = newparm2pos.get (newbnd);
    3362                 :             :           /* Move on if both bounds refer to the same parameter.  */
    3363                 :          93 :           if (pcurbndpos && pnewbndpos && *pcurbndpos == *pnewbndpos)
    3364                 :           0 :             continue;
    3365                 :             : 
    3366                 :             :           /* Move on if the bounds look the same.  */
    3367                 :         148 :           if (!pcurbndpos && !pnewbndpos
    3368                 :          93 :               && curbnd && newbnd
    3369                 :         161 :               && operand_equal_p (curbnd, newbnd,
    3370                 :             :                                   OEP_DECL_NAME | OEP_LEXICOGRAPHIC))
    3371                 :          55 :             continue;
    3372                 :             : 
    3373                 :          38 :           if ((curbnd && TREE_CODE (curbnd) != INTEGER_CST)
    3374                 :          29 :               || (newbnd && TREE_CODE (newbnd) != INTEGER_CST))
    3375                 :          38 :             opt = OPT_Wvla_parameter;
    3376                 :             : 
    3377                 :             :           /* Record the mismatch.  */
    3378                 :          38 :           bndvec.safe_push (bndpos);
    3379                 :             :           /* Underline the bounding parameter in the declaration.  */
    3380                 :          38 :           if (curbnd && TREE_CODE (curbnd) == PARM_DECL)
    3381                 :           0 :             noteloc.add_range (DECL_SOURCE_LOCATION (curbnd));
    3382                 :          38 :           if (newbnd && TREE_CODE (newbnd) == PARM_DECL)
    3383                 :           0 :             warnloc.add_range (DECL_SOURCE_LOCATION (newbnd));
    3384                 :             :         }
    3385                 :             : 
    3386                 :          53 :       const unsigned nbnds = bndvec.length ();
    3387                 :          34 :       if (!nbnds)
    3388                 :          19 :         continue;
    3389                 :             : 
    3390                 :             :       /* Use attr_access to format the parameter types.  */
    3391                 :          34 :       attr_access spec = { };
    3392                 :          34 :       const std::string newparmstr = spec.array_as_string (TREE_TYPE (newp));
    3393                 :          34 :       const std::string curparmstr = spec.array_as_string (TREE_TYPE (curp));
    3394                 :             : 
    3395                 :          34 :       if (warning_n (&warnloc, opt, nbnds,
    3396                 :             :                      "mismatch in bound %Z of argument %u declared as %s",
    3397                 :             :                      "mismatch in bounds %Z of argument %u declared as %s",
    3398                 :             :                      bndvec.address (), nbnds, parmpos, newparmstr.c_str ()))
    3399                 :          32 :         inform (&noteloc, "previously declared as %s",    curparmstr.c_str ());
    3400                 :          53 :     }
    3401                 :      273797 : }
    3402                 :             : 
    3403                 :             : /* Format EXPR if nonnull and return the formatted string.  If EXPR is
    3404                 :             :    null return DFLT.  */
    3405                 :             : 
    3406                 :             : static inline const char*
    3407                 :         304 : expr_to_str (pretty_printer &pp, tree expr, const char *dflt)
    3408                 :             : {
    3409                 :         304 :   if (!expr)
    3410                 :             :     return dflt;
    3411                 :             : 
    3412                 :         304 :   dump_generic_node (&pp, expr, 0, TDF_VOPS | TDF_MEMSYMS, false);
    3413                 :         304 :   return pp_formatted_text (&pp);
    3414                 :             : }
    3415                 :             : 
    3416                 :             : /* Detect and diagnose a mismatch between an attribute access specification
    3417                 :             :    on the original declaration of FNDECL and that on the parameters NEWPARMS
    3418                 :             :    from its redeclaration.  ORIGLOC is the location of the first declaration
    3419                 :             :    (FNDECL's is set to the location of the redeclaration).  */
    3420                 :             : 
    3421                 :             : void
    3422                 :    12095069 : warn_parm_array_mismatch (location_t origloc, tree fndecl, tree newparms)
    3423                 :             : {
    3424                 :             :   /* The original parameter list (copied from the original declaration
    3425                 :             :      into the current [re]declaration, FNDECL)).  The two are equal if
    3426                 :             :      and only if FNDECL is the first declaration.  */
    3427                 :    12095069 :   tree curparms = DECL_ARGUMENTS (fndecl);
    3428                 :    12095069 :   if (!curparms || !newparms || curparms == newparms)
    3429                 :    12057942 :     return;
    3430                 :             : 
    3431                 :      311097 :   if (TREE_CODE (curparms) != PARM_DECL
    3432                 :      311097 :       || TREE_CODE (newparms) != PARM_DECL)
    3433                 :             :     return;
    3434                 :             :   /* Extract the (possibly empty) attribute access specification from
    3435                 :             :      the declaration and its type (it doesn't yet reflect those created
    3436                 :             :      in response to NEWPARMS).  */
    3437                 :      310926 :   rdwr_map cur_idx;
    3438                 :      310926 :   tree fntype = TREE_TYPE (fndecl);
    3439                 :      310926 :   init_attr_rdwr_indices (&cur_idx, TYPE_ATTRIBUTES (fntype));
    3440                 :             : 
    3441                 :             :   /* Build a (possibly null) chain of access attributes corresponding
    3442                 :             :      to NEWPARMS.  */
    3443                 :      310926 :   const bool builtin = fndecl_built_in_p (fndecl);
    3444                 :      310926 :   tree newattrs = build_attr_access_from_parms (newparms, builtin);
    3445                 :             : 
    3446                 :             :   /* Extract the (possibly empty) attribute access specification from
    3447                 :             :      NEWATTRS.  */
    3448                 :      310926 :   rdwr_map new_idx;
    3449                 :      310926 :   init_attr_rdwr_indices (&new_idx, newattrs);
    3450                 :             : 
    3451                 :      310926 :   if (cur_idx.is_empty () && new_idx.is_empty ())
    3452                 :             :     {
    3453                 :             :       /* If both specs are empty check pointers to VLAs for mismatches. */
    3454                 :      273797 :       warn_parm_ptrarray_mismatch (origloc, curparms, newparms);
    3455                 :      273797 :       return;
    3456                 :             :     }
    3457                 :             :   /* ...otherwise, if at least one spec isn't empty there may be mismatches,
    3458                 :             :      such as between f(T*) and f(T[1]), where the former mapping would be
    3459                 :             :      empty.  */
    3460                 :             : 
    3461                 :             :   /* Create an empty access specification and use it for pointers with
    3462                 :             :      no spec of their own.  */
    3463                 :       37129 :   attr_access ptr_spec = { };
    3464                 :             : 
    3465                 :             :   /* Iterate over the two lists of function parameters, comparing their
    3466                 :             :      respective mappings and diagnosing mismatches.  */
    3467                 :       37129 :   unsigned parmpos = 0;
    3468                 :      131918 :   for (tree curp = curparms, newp = newparms; curp;
    3469                 :       94789 :        curp = TREE_CHAIN (curp), newp = TREE_CHAIN (newp), ++parmpos)
    3470                 :             :     {
    3471                 :       94791 :       if (!newp)
    3472                 :             :         /* Bail on invalid redeclarations with fewer arguments.  */
    3473                 :           2 :         return;
    3474                 :             : 
    3475                 :             :       /* Only check pointers and C++ references.  */
    3476                 :       94789 :       tree curptype = TREE_TYPE (curp);
    3477                 :       94789 :       tree newptype = TREE_TYPE (newp);
    3478                 :       94789 :       if (!POINTER_TYPE_P (curptype) || !POINTER_TYPE_P (newptype))
    3479                 :       95971 :         continue;
    3480                 :             : 
    3481                 :             :       /* Skip mismatches in __builtin_va_list that is commonly
    3482                 :             :          an array but that in declarations of built-ins decays
    3483                 :             :          to a pointer.  */
    3484                 :       93545 :       if (builtin && TREE_TYPE (newptype) == TREE_TYPE (va_list_type_node))
    3485                 :       35171 :         continue;
    3486                 :             : 
    3487                 :             :       /* Access specs for the argument on the current (previous) and
    3488                 :             :          new (to replace the current) declarations.  Either may be null,
    3489                 :             :          indicating the parameter is an ordinary pointer with no size
    3490                 :             :          associated with it.  */
    3491                 :       58374 :       attr_access *cura = cur_idx.get (parmpos);
    3492                 :       58374 :       attr_access *newa = new_idx.get (parmpos);
    3493                 :             : 
    3494                 :       58374 :       if (!newa)
    3495                 :             :         {
    3496                 :             :           /* Continue of both parameters are pointers with no size
    3497                 :             :              associated with it.  */
    3498                 :       54157 :           if (!cura)
    3499                 :       54034 :             continue;
    3500                 :             : 
    3501                 :             :           /* Otherwise point at PTR_SPEC and set its parameter pointer
    3502                 :             :              and number.  */
    3503                 :         123 :           newa = &ptr_spec;
    3504                 :         123 :           newa->ptr = newp;
    3505                 :         123 :           newa->ptrarg = parmpos;
    3506                 :             :         }
    3507                 :        4217 :       else if (!cura)
    3508                 :             :         {
    3509                 :          25 :           cura = &ptr_spec;
    3510                 :          25 :           cura->ptr = curp;
    3511                 :          25 :           cura->ptrarg = parmpos;
    3512                 :             :         }
    3513                 :             : 
    3514                 :             :       /* Set if the parameter is [re]declared as a VLA.  */
    3515                 :        4340 :       const bool cur_vla_p = cura->size || cura->minsize == HOST_WIDE_INT_M1U;
    3516                 :        4340 :       const bool new_vla_p = newa->size || newa->minsize == HOST_WIDE_INT_M1U;
    3517                 :             : 
    3518                 :        4340 :       if (DECL_P (curp))
    3519                 :        4340 :         origloc = DECL_SOURCE_LOCATION (curp);
    3520                 :           0 :       else if (EXPR_P (curp) && EXPR_HAS_LOCATION (curp))
    3521                 :           0 :         origloc = EXPR_LOCATION (curp);
    3522                 :             : 
    3523                 :             :       /* The location of the parameter in the current redeclaration.  */
    3524                 :        4340 :       location_t newloc = DECL_SOURCE_LOCATION (newp);
    3525                 :        4340 :       if (origloc == UNKNOWN_LOCATION)
    3526                 :           0 :         origloc = newloc;
    3527                 :             : 
    3528                 :        4340 :       const std::string newparmstr = newa->array_as_string (newptype);
    3529                 :        4340 :       const std::string curparmstr = cura->array_as_string (curptype);
    3530                 :        4340 :       if (new_vla_p && !cur_vla_p)
    3531                 :             :         {
    3532                 :          29 :           if (warning_at (newloc, OPT_Wvla_parameter,
    3533                 :             :                           "argument %u of type %s declared as "
    3534                 :             :                           "a variable length array",
    3535                 :             :                           parmpos + 1, newparmstr.c_str ()))
    3536                 :          53 :             inform (origloc,
    3537                 :             :                     (cura == &ptr_spec
    3538                 :             :                      ? G_("previously declared as a pointer %s")
    3539                 :             :                      : G_("previously declared as an ordinary array %s")),
    3540                 :             :                     curparmstr.c_str ());
    3541                 :          29 :           continue;
    3542                 :             :         }
    3543                 :             : 
    3544                 :        4311 :       if (newa == &ptr_spec)
    3545                 :             :         {
    3546                 :             :           /* The new declaration uses the pointer form.  Detect mismatches
    3547                 :             :              between the pointer and a previous array or VLA forms.  */
    3548                 :         123 :           if (cura->minsize == HOST_WIDE_INT_M1U)
    3549                 :             :             {
    3550                 :             :               /* Diagnose a pointer/VLA mismatch.  */
    3551                 :           0 :               if (warning_at (newloc, OPT_Wvla_parameter,
    3552                 :             :                               "argument %u of type %s declared "
    3553                 :             :                               "as a pointer",
    3554                 :             :                               parmpos + 1, newparmstr.c_str ()))
    3555                 :           0 :                 inform (origloc,
    3556                 :             :                         "previously declared as a variable length array %s",
    3557                 :             :                         curparmstr.c_str ());
    3558                 :           0 :               continue;
    3559                 :             :             }
    3560                 :             : 
    3561                 :         123 :           if (cura->minsize && cura->minsize != HOST_WIDE_INT_M1U)
    3562                 :             :             {
    3563                 :             :               /* Diagnose mismatches between arrays with a constant
    3564                 :             :                  bound and pointers.  */
    3565                 :           4 :               if (warning_at (newloc, OPT_Warray_parameter_,
    3566                 :             :                               "argument %u of type %s declared "
    3567                 :             :                               "as a pointer",
    3568                 :             :                               parmpos + 1, newparmstr.c_str ()))
    3569                 :           2 :                 inform (origloc, "previously declared as an array %s",
    3570                 :             :                         curparmstr.c_str ());
    3571                 :           4 :               continue;
    3572                 :             :             }
    3573                 :             :         }
    3574                 :             : 
    3575                 :        4307 :       if (!new_vla_p && cur_vla_p)
    3576                 :             :         {
    3577                 :           6 :           if (warning_at (newloc, OPT_Wvla_parameter,
    3578                 :             :                           "argument %u of type %s declared "
    3579                 :             :                           "as an ordinary array",
    3580                 :             :                           parmpos + 1, newparmstr.c_str ()))
    3581                 :           2 :             inform (origloc,
    3582                 :             :                     "previously declared as a variable length array %s",
    3583                 :             :                     curparmstr.c_str ());
    3584                 :           6 :           continue;
    3585                 :             :         }
    3586                 :             : 
    3587                 :             :       /* Move on to the next pair of parameters if both of the current
    3588                 :             :          pair are VLAs with a single variable bound that refers to
    3589                 :             :          a parameter at the same position.  */
    3590                 :         373 :       if (newa->size && cura->size
    3591                 :         372 :           && newa->sizarg != UINT_MAX
    3592                 :          76 :           && newa->sizarg == cura->sizarg
    3593                 :          70 :           && newa->minsize == cura->minsize
    3594                 :        4357 :           && !TREE_PURPOSE (newa->size) && !TREE_PURPOSE (cura->size))
    3595                 :           0 :         continue;
    3596                 :             : 
    3597                 :        4301 :       if (newa->size || cura->size)
    3598                 :             :         {
    3599                 :         375 :           unsigned newunspec, curunspec;
    3600                 :         375 :           unsigned newbnds = newa->vla_bounds (&newunspec) + newunspec;
    3601                 :         375 :           unsigned curbnds = cura->vla_bounds (&curunspec) + curunspec;
    3602                 :             : 
    3603                 :         375 :           if (newbnds != curbnds)
    3604                 :             :             {
    3605                 :           5 :               if (warning_n (newloc, OPT_Wvla_parameter, newbnds,
    3606                 :             :                              "argument %u of type %s declared with "
    3607                 :             :                              "%u variable bound",
    3608                 :             :                              "argument %u of type %s declared with "
    3609                 :             :                              "%u variable bounds",
    3610                 :             :                              parmpos + 1, newparmstr.c_str (),
    3611                 :             :                              newbnds))
    3612                 :           5 :                 inform_n (origloc, curbnds,
    3613                 :             :                           "previously declared as %s with %u variable bound",
    3614                 :             :                           "previously declared as %s with %u variable bounds",
    3615                 :             :                           curparmstr.c_str (), curbnds);
    3616                 :          14 :               continue;
    3617                 :             :             }
    3618                 :             : 
    3619                 :         370 :           if (newunspec > curunspec)
    3620                 :             :             {
    3621                 :           9 :               location_t warnloc = newloc, noteloc = origloc;
    3622                 :           9 :               const char *warnparmstr = newparmstr.c_str ();
    3623                 :           9 :               const char *noteparmstr = curparmstr.c_str ();
    3624                 :           9 :               unsigned warnunspec = newunspec, noteunspec = curunspec;
    3625                 :             : 
    3626                 :           9 :               if (warning_n (warnloc, OPT_Wvla_parameter, warnunspec,
    3627                 :             :                              "argument %u of type %s declared with "
    3628                 :             :                              "%u unspecified variable bound",
    3629                 :             :                              "argument %u of type %s declared with "
    3630                 :             :                              "%u unspecified variable bounds",
    3631                 :             :                              parmpos + 1, warnparmstr, warnunspec))
    3632                 :             :                 {
    3633                 :           8 :                   if (warnloc == newloc)
    3634                 :           8 :                     inform_n (noteloc, noteunspec,
    3635                 :             :                               "previously declared as %s with %u unspecified "
    3636                 :             :                               "variable bound",
    3637                 :             :                               "previously declared as %s with %u unspecified "
    3638                 :             :                               "variable bounds",
    3639                 :             :                               noteparmstr, noteunspec);
    3640                 :             :                   else
    3641                 :             :                     inform_n (noteloc, noteunspec,
    3642                 :             :                               "subsequently declared as %s with %u unspecified "
    3643                 :             :                               "variable bound",
    3644                 :             :                               "subsequently declared as %s with %u unspecified "
    3645                 :             :                               "variable bounds",
    3646                 :             :                               noteparmstr, noteunspec);
    3647                 :             :                 }
    3648                 :           9 :               continue;
    3649                 :           9 :             }
    3650                 :             :         }
    3651                 :             : 
    3652                 :             :       /* Iterate over the lists of VLA variable bounds, comparing each
    3653                 :             :          pair for equality, and diagnosing mismatches.  */
    3654                 :        8761 :       for (tree newvbl = newa->size, curvbl = cura->size; newvbl && curvbl;
    3655                 :        4474 :            newvbl = TREE_CHAIN (newvbl), curvbl = TREE_CHAIN (curvbl))
    3656                 :             :         {
    3657                 :        4474 :           tree newpos = TREE_PURPOSE (newvbl);
    3658                 :        4474 :           tree curpos = TREE_PURPOSE (curvbl);
    3659                 :             : 
    3660                 :        4474 :           tree newbnd = vla_bound_parm_decl (TREE_VALUE (newvbl));
    3661                 :        4474 :           tree curbnd = vla_bound_parm_decl (TREE_VALUE (curvbl));
    3662                 :             : 
    3663                 :        4474 :           if (newpos == curpos && newbnd == curbnd)
    3664                 :             :             /* In the expected case when both bounds either refer to
    3665                 :             :                the same positional parameter or when neither does,
    3666                 :             :                and both are the same expression they are necessarily
    3667                 :             :                the same.  */
    3668                 :        4398 :             continue;
    3669                 :             : 
    3670                 :         152 :           pretty_printer pp1, pp2;
    3671                 :         152 :           const char* const newbndstr = expr_to_str (pp1, newbnd, "*");
    3672                 :         152 :           const char* const curbndstr = expr_to_str (pp2, curbnd, "*");
    3673                 :             : 
    3674                 :         152 :           if (!newpos != !curpos
    3675                 :         152 :               || (newpos && !tree_int_cst_equal (newpos, curpos)))
    3676                 :             :             {
    3677                 :             :               /* Diagnose a mismatch between a specified VLA bound and
    3678                 :             :                  an unspecified one.  This can only happen in the most
    3679                 :             :                  significant bound.
    3680                 :             : 
    3681                 :             :                  Distinguish between the common case of bounds that are
    3682                 :             :                  other function parameters such as in
    3683                 :             :                    f (int n, int[n]);
    3684                 :             :                  and others.  */
    3685                 :             : 
    3686                 :          21 :               gcc_rich_location richloc (newloc);
    3687                 :          21 :               bool warned;
    3688                 :          21 :               if (newpos)
    3689                 :             :                 {
    3690                 :             :                   /* Also underline the VLA bound argument.  */
    3691                 :           4 :                   richloc.add_range (DECL_SOURCE_LOCATION (newbnd));
    3692                 :           4 :                   warned = warning_at (&richloc, OPT_Wvla_parameter,
    3693                 :             :                                        "argument %u of type %s declared "
    3694                 :             :                                        "with mismatched bound argument %E",
    3695                 :             :                                        parmpos + 1, newparmstr.c_str (),
    3696                 :             :                                        plus_one (newpos));
    3697                 :             :                 }
    3698                 :             :               else
    3699                 :          17 :                 warned = warning_at (&richloc, OPT_Wvla_parameter,
    3700                 :             :                                      "argument %u of type %s declared "
    3701                 :             :                                      "with mismatched bound %<%s%>",
    3702                 :             :                                      parmpos + 1, newparmstr.c_str (),
    3703                 :             :                                      newbndstr);
    3704                 :             : 
    3705                 :          21 :               if (warned)
    3706                 :             :                 {
    3707                 :          21 :                   gcc_rich_location richloc (origloc);
    3708                 :          21 :                   if (curpos)
    3709                 :             :                     {
    3710                 :             :                       /* Also underline the VLA bound argument.  */
    3711                 :          18 :                       richloc.add_range (DECL_SOURCE_LOCATION (curbnd));
    3712                 :          18 :                       inform (&richloc, "previously declared as %s with "
    3713                 :             :                               "bound argument %E",
    3714                 :             :                               curparmstr.c_str (), plus_one (curpos));
    3715                 :             :                     }
    3716                 :             :                   else
    3717                 :           3 :                     inform (&richloc, "previously declared as %s with bound "
    3718                 :             :                             "%<%s%>", curparmstr.c_str (), curbndstr);
    3719                 :             : 
    3720                 :          21 :                   continue;
    3721                 :          21 :                 }
    3722                 :          21 :             }
    3723                 :             : 
    3724                 :         131 :           if (!newpos && newbnd && curbnd)
    3725                 :             :             {
    3726                 :             :               /* The VLA bounds don't refer to other function parameters.
    3727                 :             :                  Compare them lexicographically to detect gross mismatches
    3728                 :             :                  such as between T[foo()] and T[bar()].  */
    3729                 :          55 :               if (operand_equal_p (newbnd, curbnd,
    3730                 :             :                                    OEP_DECL_NAME | OEP_LEXICOGRAPHIC))
    3731                 :          24 :                 continue;
    3732                 :             : 
    3733                 :          31 :               if (warning_at (newloc, OPT_Wvla_parameter,
    3734                 :             :                               "argument %u of type %s declared with "
    3735                 :             :                               "mismatched bound %<%s%>",
    3736                 :             :                               parmpos + 1, newparmstr.c_str (), newbndstr))
    3737                 :          29 :                 inform (origloc, "previously declared as %s with bound %qs",
    3738                 :             :                         curparmstr.c_str (), curbndstr);
    3739                 :          31 :               continue;
    3740                 :             :             }
    3741                 :         152 :         }
    3742                 :             : 
    3743                 :        4287 :       if (newa->minsize == cura->minsize
    3744                 :          88 :           || (((newa->minsize == 0 && newa->mode != access_deferred)
    3745                 :          88 :                || (cura->minsize == 0 && cura->mode != access_deferred))
    3746                 :           7 :               && newa != &ptr_spec
    3747                 :           7 :               && cura != &ptr_spec))
    3748                 :        4201 :         continue;
    3749                 :             : 
    3750                 :          86 :       if (!newa->static_p && !cura->static_p && warn_array_parameter < 2)
    3751                 :             :         /* Avoid warning about mismatches in ordinary (non-static) arrays
    3752                 :             :            at levels below 2.  */
    3753                 :          24 :         continue;
    3754                 :             : 
    3755                 :          62 :       if (warning_at (newloc, OPT_Warray_parameter_,
    3756                 :             :                       "argument %u of type %s with mismatched bound",
    3757                 :             :                       parmpos + 1, newparmstr.c_str ()))
    3758                 :          62 :         inform (origloc, "previously declared as %s", curparmstr.c_str ());
    3759                 :        4340 :     }
    3760                 :      310926 : }
    3761                 :             : 
    3762                 :             : /* Warn about divisions of two sizeof operators when the first one is applied
    3763                 :             :    to an array and the divisor does not equal the size of the array element.
    3764                 :             :    For instance:
    3765                 :             : 
    3766                 :             :      sizeof (ARR) / sizeof (OP)
    3767                 :             : 
    3768                 :             :    ARR is the array argument of the first sizeof, ARR_TYPE is its ARRAY_TYPE.
    3769                 :             :    OP1 is the whole second SIZEOF_EXPR, or its argument; TYPE1 is the type
    3770                 :             :    of the second argument.  */
    3771                 :             : 
    3772                 :             : void
    3773                 :      103521 : maybe_warn_sizeof_array_div (location_t loc, tree arr, tree arr_type,
    3774                 :             :                              tree op1, tree type1)
    3775                 :             : {
    3776                 :      103521 :   tree elt_type = TREE_TYPE (arr_type);
    3777                 :             : 
    3778                 :      103521 :   if (!warn_sizeof_array_div
    3779                 :             :       /* Don't warn on multidimensional arrays.  */
    3780                 :        1083 :       || TREE_CODE (elt_type) == ARRAY_TYPE)
    3781                 :             :     return;
    3782                 :             : 
    3783                 :        1053 :   if (!tree_int_cst_equal (TYPE_SIZE (elt_type), TYPE_SIZE (type1)))
    3784                 :             :     {
    3785                 :          45 :       auto_diagnostic_group d;
    3786                 :          45 :       if (warning_at (loc, OPT_Wsizeof_array_div,
    3787                 :             :                       "expression does not compute the number of "
    3788                 :             :                       "elements in this array; element type is "
    3789                 :             :                       "%qT, not %qT", elt_type, type1))
    3790                 :             :         {
    3791                 :          45 :           if (EXPR_HAS_LOCATION (op1))
    3792                 :             :             {
    3793                 :          38 :               location_t op1_loc = EXPR_LOCATION (op1);
    3794                 :          38 :               gcc_rich_location richloc (op1_loc);
    3795                 :          38 :               richloc.add_fixit_insert_before (op1_loc, "(");
    3796                 :          38 :               richloc.add_fixit_insert_after (op1_loc, ")");
    3797                 :          38 :               inform (&richloc, "add parentheses around %qE to "
    3798                 :             :                       "silence this warning", op1);
    3799                 :          38 :             }
    3800                 :             :           else
    3801                 :           7 :             inform (loc, "add parentheses around the second %<sizeof%> "
    3802                 :             :                     "to silence this warning");
    3803                 :          45 :           if (DECL_P (arr))
    3804                 :          39 :             inform (DECL_SOURCE_LOCATION (arr), "array %qD declared here", arr);
    3805                 :             :         }
    3806                 :          45 :     }
    3807                 :             : }
    3808                 :             : 
    3809                 :             : /* Warn about C++20 [depr.array.comp] array comparisons: "Equality
    3810                 :             :    and relational comparisons between two operands of array type are
    3811                 :             :    deprecated."  We also warn in C and earlier C++ standards.  CODE is
    3812                 :             :    the code for this comparison, OP0 and OP1 are the operands.  */
    3813                 :             : 
    3814                 :             : void
    3815                 :          35 : do_warn_array_compare (location_t location, tree_code code, tree op0, tree op1)
    3816                 :             : {
    3817                 :          35 :   STRIP_NOPS (op0);
    3818                 :          35 :   STRIP_NOPS (op1);
    3819                 :          35 :   if (TREE_CODE (op0) == ADDR_EXPR)
    3820                 :           7 :     op0 = TREE_OPERAND (op0, 0);
    3821                 :          35 :   if (TREE_CODE (op1) == ADDR_EXPR)
    3822                 :           7 :     op1 = TREE_OPERAND (op1, 0);
    3823                 :             : 
    3824                 :          35 :   auto_diagnostic_group d;
    3825                 :          35 :   if (warning_at (location, OPT_Warray_compare,
    3826                 :          35 :                   (c_dialect_cxx () && cxx_dialect >= cxx20)
    3827                 :             :                   ? G_("comparison between two arrays is deprecated in C++20")
    3828                 :             :                   : G_("comparison between two arrays")))
    3829                 :             :     {
    3830                 :             :       /* C doesn't allow +arr.  */
    3831                 :          35 :       if (c_dialect_cxx ())
    3832                 :          28 :         inform (location, "use unary %<+%> which decays operands to pointers "
    3833                 :             :                 "or %<&%D[0] %s &%D[0]%> to compare the addresses",
    3834                 :             :                 op0, op_symbol_code (code), op1);
    3835                 :             :       else
    3836                 :           7 :         inform (location, "use %<&%D[0] %s &%D[0]%> to compare the addresses",
    3837                 :             :                 op0, op_symbol_code (code), op1);
    3838                 :             :     }
    3839                 :          35 : }
    3840                 :             : 
    3841                 :             : /* Given LHS_VAL ^ RHS_VAL, where LHS_LOC is the location of the LHS,
    3842                 :             :    OPERATOR_LOC is the location of the ^, and RHS_LOC the location of the
    3843                 :             :    RHS, complain with -Wxor-used-as-pow if it looks like the user meant
    3844                 :             :    exponentiation rather than xor.  */
    3845                 :             : 
    3846                 :             : void
    3847                 :         696 : check_for_xor_used_as_pow (location_t lhs_loc, tree lhs_val,
    3848                 :             :                            location_t operator_loc,
    3849                 :             :                            location_t rhs_loc, tree rhs_val)
    3850                 :             : {
    3851                 :             :   /* Only complain if both args are non-negative integer constants that fit
    3852                 :             :      in uhwi.  */
    3853                 :         696 :   if (!tree_fits_uhwi_p (lhs_val) || !tree_fits_uhwi_p (rhs_val))
    3854                 :         598 :     return;
    3855                 :             : 
    3856                 :             :   /* Only complain if the LHS is 2 or 10.  */
    3857                 :         696 :   unsigned HOST_WIDE_INT lhs_uhwi = tree_to_uhwi (lhs_val);
    3858                 :         696 :   if (lhs_uhwi != 2 && lhs_uhwi != 10)
    3859                 :             :     return;
    3860                 :             : 
    3861                 :         118 :   unsigned HOST_WIDE_INT rhs_uhwi = tree_to_uhwi (rhs_val);
    3862                 :         118 :   unsigned HOST_WIDE_INT xor_result = lhs_uhwi ^ rhs_uhwi;
    3863                 :         118 :   binary_op_rich_location loc (operator_loc,
    3864                 :         118 :                                lhs_val, rhs_val, false);
    3865                 :             : 
    3866                 :             :   /* Reject cases where we don't have 3 distinct locations.
    3867                 :             :      This can happen e.g. due to macro expansion with
    3868                 :             :      -ftrack-macro-expansion=0 */
    3869                 :         113 :   if (!(lhs_loc != operator_loc
    3870                 :         118 :         && lhs_loc != rhs_loc
    3871                 :             :         && operator_loc != rhs_loc))
    3872                 :             :     return;
    3873                 :             : 
    3874                 :             :   /* Reject cases in which any of the locations came from a macro.  */
    3875                 :         113 :   if (from_macro_expansion_at (lhs_loc)
    3876                 :          98 :       || from_macro_expansion_at (operator_loc)
    3877                 :         211 :       || from_macro_expansion_at (rhs_loc))
    3878                 :          15 :     return;
    3879                 :             : 
    3880                 :             :   /* If we issue fix-it hints with the warning then we will also issue a
    3881                 :             :      note suggesting how to suppress the warning with a different change.
    3882                 :             :      These proposed changes are incompatible.  */
    3883                 :          98 :   loc.fixits_cannot_be_auto_applied ();
    3884                 :             : 
    3885                 :          98 :   auto_diagnostic_group d;
    3886                 :          98 :   bool warned = false;
    3887                 :          98 :   if (lhs_uhwi == 2)
    3888                 :             :     {
    3889                 :             :       /* Would exponentiation fit in int, in long long, or not at all?  */
    3890                 :          69 :       if (rhs_uhwi < (INT_TYPE_SIZE - 1))
    3891                 :             :         {
    3892                 :          24 :           unsigned HOST_WIDE_INT suggested_result = 1 << rhs_uhwi;
    3893                 :          24 :           loc.add_fixit_replace (lhs_loc, "1");
    3894                 :          24 :           loc.add_fixit_replace (operator_loc, "<<");
    3895                 :          24 :           warned = warning_at (&loc, OPT_Wxor_used_as_pow,
    3896                 :             :                                "result of %<%wu^%wu%> is %wu;"
    3897                 :             :                                " did you mean %<1 << %wu%> (%wu)?",
    3898                 :             :                                lhs_uhwi, rhs_uhwi, xor_result,
    3899                 :             :                                rhs_uhwi, suggested_result);
    3900                 :             :         }
    3901                 :          45 :       else if (rhs_uhwi < (LONG_LONG_TYPE_SIZE - 1))
    3902                 :             :         {
    3903                 :          15 :           loc.add_fixit_replace (lhs_loc, "1LL");
    3904                 :          15 :           loc.add_fixit_replace (operator_loc, "<<");
    3905                 :          15 :           warned = warning_at (&loc, OPT_Wxor_used_as_pow,
    3906                 :             :                                "result of %<%wu^%wu%> is %wu;"
    3907                 :             :                                " did you mean %<1LL << %wu%>?",
    3908                 :             :                                lhs_uhwi, rhs_uhwi, xor_result,
    3909                 :             :                                rhs_uhwi);
    3910                 :             :         }
    3911                 :          30 :       else if (rhs_uhwi <= LONG_LONG_TYPE_SIZE)
    3912                 :          10 :         warned = warning_at (&loc, OPT_Wxor_used_as_pow,
    3913                 :             :                              "result of %<%wu^%wu%> is %wu;"
    3914                 :             :                              " did you mean exponentiation?",
    3915                 :             :                              lhs_uhwi, rhs_uhwi, xor_result);
    3916                 :             :       /* Otherwise assume it's an xor.  */
    3917                 :             :     }
    3918                 :             :   else
    3919                 :             :     {
    3920                 :          29 :       gcc_assert (lhs_uhwi == 10);
    3921                 :          29 :       loc.add_fixit_replace (lhs_loc, "1");
    3922                 :          29 :       loc.add_fixit_replace (operator_loc, "e");
    3923                 :          29 :       warned = warning_at (&loc, OPT_Wxor_used_as_pow,
    3924                 :             :                            "result of %<%wu^%wu%> is %wu;"
    3925                 :             :                            " did you mean %<1e%wu%>?",
    3926                 :             :                            lhs_uhwi, rhs_uhwi, xor_result,
    3927                 :             :                            rhs_uhwi);
    3928                 :             :     }
    3929                 :          78 :   if (warned)
    3930                 :             :     {
    3931                 :          78 :       gcc_rich_location note_loc (lhs_loc);
    3932                 :          78 :       if (lhs_uhwi == 2)
    3933                 :          49 :         note_loc.add_fixit_replace (lhs_loc, "0x2");
    3934                 :             :       else
    3935                 :             :         {
    3936                 :          29 :           gcc_assert (lhs_uhwi == 10);
    3937                 :          29 :           note_loc.add_fixit_replace (lhs_loc, "0xa");
    3938                 :             :         }
    3939                 :          78 :       note_loc.fixits_cannot_be_auto_applied ();
    3940                 :          78 :       inform (&note_loc,
    3941                 :             :               "you can silence this warning by using a hexadecimal constant"
    3942                 :             :               " (%wx rather than %wd)",
    3943                 :             :               lhs_uhwi, lhs_uhwi);
    3944                 :          78 :     }
    3945                 :         118 : }
        

Generated by: LCOV version 2.1-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.