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

Generated by: LCOV version 1.0

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 --enable-host-shared. GCC test suite is run with the built compiler.