LCOV - code coverage report
Current view: top level - gcc/analyzer - initializers.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 95.6 % 113 108
Test Date: 2026-11-14 16:39:50 Functions: 100.0 % 7 7
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Implementation of decl_region::calc_svalue_for_constructor.
       2              :    Copyright (C) 2019-2026 Free Software Foundation, Inc.
       3              :    Contributed by David Malcolm <dmalcolm@redhat.com>.
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify it
       8              : under the terms of the GNU General Public License as published by
       9              : the Free Software Foundation; either version 3, or (at your option)
      10              : any later version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful, but
      13              : WITHOUT ANY WARRANTY; without even the implied warranty of
      14              : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15              : General Public License for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : #include "analyzer/common.h"
      22              : 
      23              : #include "analyzer/region-model.h"
      24              : 
      25              : #if ENABLE_ANALYZER
      26              : 
      27              : namespace ana {
      28              : 
      29              : /* Handling of tree code CONSTRUCTOR for initialization of a decl_region.  */
      30              : 
      31              : static bool
      32              : apply_ctor_pair_to_child_region (symbolic::concrete_bit_overlay::key &out,
      33              :                                  const region *parent_reg,
      34              :                                  region_model_manager *mgr,
      35              :                                  tree index, tree val);
      36              : 
      37              : static bool
      38              : apply_ctor_val_to_range (symbolic::concrete_bit_overlay::key &out,
      39              :                          const region *parent_reg,
      40              :                          region_model_manager *mgr,
      41              :                          tree min_index, tree max_index,
      42              :                          tree val);
      43              : 
      44              : /* Get the child region of PARENT_REG based upon INDEX within a
      45              :    CONSTRUCTOR.   */
      46              : 
      47              : static const region *
      48          883 : get_subregion_within_ctor (const region *parent_reg, tree index,
      49              :                            region_model_manager *mgr)
      50              : {
      51          883 :   switch (TREE_CODE (index))
      52              :     {
      53            0 :     default:
      54            0 :       gcc_unreachable ();
      55          836 :     case INTEGER_CST:
      56          836 :       {
      57          836 :         const svalue *index_sval
      58          836 :           = mgr->get_or_create_constant_svalue (index);
      59          836 :         return mgr->get_element_region (parent_reg,
      60          836 :                                         TREE_TYPE (parent_reg->get_type ()),
      61          836 :                                         index_sval);
      62              :       }
      63           47 :       break;
      64           47 :     case FIELD_DECL:
      65           47 :       return mgr->get_field_region (parent_reg, index);
      66              :     }
      67              : }
      68              : 
      69              : /* Get the child region of PARENT_REG based upon (INDEX, VALUE) within a
      70              :    CONSTRUCTOR.   */
      71              : 
      72              : static const region *
      73          871 : get_subregion_within_ctor_for_ctor_pair (const region *parent_reg,
      74              :                                          tree index,
      75              :                                          tree value,
      76              :                                          region_model_manager *mgr)
      77              : {
      78          871 :   if (TREE_CODE (index) == INTEGER_CST
      79          824 :       && TREE_CODE (value) == RAW_DATA_CST)
      80              :     {
      81              :       /* Special-case; see tree.def's description of CONSTRUCTOR.
      82              :          We have RAW_DATA_LENGTH of bytes, starting at INDEX's start.  */
      83           14 :       const region *start_reg
      84           14 :         = get_subregion_within_ctor (parent_reg, index, mgr);
      85              :       /* Build a bit range, relative to PARENT_REG.  */
      86              : 
      87           14 :       auto start_offset = start_reg->get_memory_offset (*mgr);
      88           14 :       concrete_bit_offset conc_start_offset;
      89           14 :       if (!concrete_from_symbolic (start_offset.get_bit_offset (),
      90              :                                    &conc_start_offset))
      91              :         return nullptr;
      92           14 :       concrete_byte_size byte_length (RAW_DATA_LENGTH (value));
      93           14 :       concrete_bit_size bit_length = bits_from_bytes (byte_length);
      94           28 :       concrete_bit_range bits (conc_start_offset, bit_length);
      95           14 :       return mgr->get_bit_range (parent_reg, NULL_TREE, bits);
      96           28 :     }
      97              : 
      98          857 :   return get_subregion_within_ctor (parent_reg, index, mgr);
      99              : }
     100              : 
     101              : /* Get the svalue for VAL, a non-CONSTRUCTOR value within a CONSTRUCTOR.  */
     102              : 
     103              : static const svalue *
     104          835 : get_svalue_for_ctor_val (tree val, region_model_manager *mgr)
     105              : {
     106              :   /* Reuse the get_rvalue logic from region_model.  */
     107          835 :   region_model m (mgr->get_node_manager ());
     108          835 :   return m.get_rvalue (path_var (val, 0), nullptr);
     109          835 : }
     110              : 
     111              : /* Bind values from CONSTRUCTOR to OUT, relative to
     112              :    PARENT_REG's relationship to its base region.
     113              :    Return true if successful, false if there was a problem (e.g. due
     114              :    to hitting a complexity limit).  */
     115              : 
     116              : static bool
     117          194 : apply_ctor_to_region (symbolic::concrete_bit_overlay::key &out,
     118              :                       const region *parent_reg,
     119              :                       tree ctor,
     120              :                       region_model_manager *mgr)
     121              : {
     122          194 :   gcc_assert (parent_reg);
     123          194 :   gcc_assert (TREE_CODE (ctor) == CONSTRUCTOR);
     124              : 
     125          194 :   unsigned ix;
     126          194 :   tree index;
     127          194 :   tree val;
     128          194 :   tree parent_type = parent_reg->get_type ();
     129          194 :   tree field;
     130          194 :   if (TREE_CODE (parent_type) == RECORD_TYPE)
     131           42 :     field = TYPE_FIELDS (parent_type);
     132              :   else
     133              :     field = NULL_TREE;
     134         1067 :   FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), ix, index, val)
     135              :     {
     136          877 :       if (!index)
     137              :         {
     138              :           /* If index is NULL, then iterate through the fields for
     139              :              a RECORD_TYPE, or use an INTEGER_CST otherwise.
     140              :              Compare with similar logic in output_constructor.  */
     141          606 :           if (field)
     142              :             {
     143            0 :               index = field;
     144            0 :               field = DECL_CHAIN (field);
     145              :             }
     146              :           else
     147          606 :             index = build_int_cst (integer_type_node, ix);
     148              :         }
     149          271 :       else if (TREE_CODE (index) == RANGE_EXPR)
     150              :         {
     151            6 :           tree min_index = TREE_OPERAND (index, 0);
     152            6 :           tree max_index = TREE_OPERAND (index, 1);
     153            6 :           if (min_index == max_index)
     154              :             {
     155            0 :               if (!apply_ctor_pair_to_child_region (out, parent_reg, mgr,
     156              :                                                     min_index, val))
     157              :                 return false;
     158              :             }
     159              :           else
     160              :             {
     161            6 :               if (!apply_ctor_val_to_range (out, parent_reg, mgr,
     162              :                                             min_index, max_index, val))
     163              :                 return false;
     164              :             }
     165            3 :           continue;
     166            3 :         }
     167          871 :       if (!apply_ctor_pair_to_child_region (out, parent_reg, mgr, index, val))
     168              :         return false;
     169              :     }
     170              :   return true;
     171              : }
     172              : 
     173              : /* Bind the value VAL into OUT at the range of elements within PARENT_REF
     174              :    from MIN_INDEX to MAX_INDEX (including endpoints).
     175              :    For use in handling RANGE_EXPR within a CONSTRUCTOR.
     176              :    Return true if successful, false if there was a problem (e.g. due
     177              :    to hitting a complexity limit).  */
     178              : 
     179              : static bool
     180            6 : apply_ctor_val_to_range (symbolic::concrete_bit_overlay::key &out,
     181              :                          const region *parent_reg,
     182              :                          region_model_manager *mgr,
     183              :                          tree min_index, tree max_index,
     184              :                          tree val)
     185              : {
     186            6 :   gcc_assert (TREE_CODE (min_index) == INTEGER_CST);
     187            6 :   gcc_assert (TREE_CODE (max_index) == INTEGER_CST);
     188              : 
     189              :   /* Generate a binding key for the range.  */
     190            6 :   const region *min_element
     191            6 :     = get_subregion_within_ctor (parent_reg, min_index, mgr);
     192            6 :   const region *max_element
     193            6 :     = get_subregion_within_ctor (parent_reg, max_index, mgr);
     194              : 
     195            6 :   auto min_element_mem_range = min_element->get_memory_range (*mgr);
     196            6 :   auto max_element_mem_range = max_element->get_memory_range (*mgr);
     197            6 :   gcc_assert (&min_element_mem_range.get_base_region ()
     198              :               == &max_element_mem_range.get_base_region ());
     199            6 :   symbolic_bit_range overall_sym_range
     200            6 :     (min_element_mem_range.get_start_offset (),
     201            6 :      (max_element_mem_range.get_next_offset ()
     202           12 :       - min_element_mem_range.get_next_offset ()));
     203            6 :   concrete_bit_range overall_conc_range;
     204            6 :   if (!concrete_from_symbolic (overall_sym_range, &overall_conc_range))
     205              :     return false;
     206              : 
     207              :   /* Get the value.  */
     208            6 :   if (TREE_CODE (val) == CONSTRUCTOR)
     209              :     return false;
     210            3 :   const svalue *sval = get_svalue_for_ctor_val (val, mgr);
     211              : 
     212            3 :   auto &bv_mgr = mgr->get_node_manager ().get_bit_vector_manager ();
     213            3 :   auto &bv = bv_mgr.get_bit_vector_from_svalue (*sval);
     214              : 
     215            3 :   concrete_bit_size bv_size;
     216            3 :   if (!bv.maybe_get_concrete_size_in_bits (&bv_size))
     217              :     return false;
     218              : 
     219            3 :   if (overall_conc_range.get_size ().floor_mod (bv_size) != 0_bits)
     220              :     return false;
     221              : 
     222            3 :   auto num_repeats = overall_conc_range.get_size ().floor_div (bv_size);
     223              : 
     224            3 :   auto &repeated_bv = bv_mgr.get_repeated_bits (bv, num_repeats);
     225              : 
     226            3 :   out.m_concrete_range_map.insert ({overall_conc_range, repeated_bv});
     227              : 
     228            3 :   return true;
     229            6 : }
     230              : 
     231              : /* Bind the value VAL into INDEX within PARENT_REF.
     232              :    For use in handling a pair of entries within a CONSTRUCTOR.
     233              :    Return true if successful, false if there was a problem (e.g. due
     234              :    to hitting a complexity limit).  */
     235              : 
     236              : static bool
     237          871 : apply_ctor_pair_to_child_region (symbolic::concrete_bit_overlay::key &out,
     238              :                                  const region *parent_reg,
     239              :                                  region_model_manager *mgr,
     240              :                                  tree index, tree val)
     241              : {
     242          871 :   const region *child_reg
     243          871 :     = get_subregion_within_ctor_for_ctor_pair (parent_reg, index, val, mgr);
     244          871 :   if (!child_reg)
     245              :     return false;
     246          871 :   if (TREE_CODE (val) == CONSTRUCTOR)
     247           39 :     return apply_ctor_to_region (out, child_reg, val, mgr);
     248              :   else
     249              :     {
     250          832 :       const svalue *sval = get_svalue_for_ctor_val (val, mgr);
     251          832 :       if (child_reg->empty_p (*mgr))
     252              :         return false;
     253          832 :       auto &bv_mgr = mgr->get_node_manager ().get_bit_vector_manager ();
     254          832 :       auto &bv = bv_mgr.get_bit_vector_from_svalue (*sval);
     255              :       /* Use the size of bv rather than that of the region to cope
     256              :          with flexible arrays.  */
     257          832 :       auto mem_offset = child_reg->get_memory_offset (*mgr);
     258          832 :       symbolic_bit_range mem_range (mem_offset.get_bit_offset (),
     259          832 :                                     bv.get_size ());
     260          832 :       concrete_bit_range conc_bit_range;
     261          832 :       if (!concrete_from_symbolic (mem_range, &conc_bit_range))
     262              :         return false;
     263          831 :       out.m_concrete_range_map.insert ({conc_bit_range, bv});
     264          831 :       return true;
     265          832 :     }
     266              : }
     267              : 
     268              : /* Implementation of decl_region::get_bit_contents_for_constructor
     269              :    for when the cached value hasn't yet been calculated.  */
     270              : 
     271              : const symbolic::bit_contents &
     272          155 : decl_region::calc_bit_contents_for_constructor (tree ctor,
     273              :                                                 region_model_manager *mgr) const
     274              : {
     275          155 :   auto &node_mgr = mgr->get_node_manager ();
     276          155 :   auto &bit_contents_mgr = node_mgr.get_bit_contents_manager ();
     277          155 :   symbolic::concrete_bit_overlay::key bit_overlay_key
     278          155 :     {&bit_contents_mgr.get_all_zeros (), {}};
     279          155 :   if (!apply_ctor_to_region (bit_overlay_key, this, ctor, mgr))
     280            4 :     return bit_contents_mgr.get_untracked ({});
     281          151 :   return bit_contents_mgr.get_concrete_bit_overlay
     282          151 :     (std::move (bit_overlay_key), nullptr);
     283          155 : }
     284              : 
     285              : } // namespace ana
     286              : 
     287              : #endif /* #if ENABLE_ANALYZER */
        

Generated by: LCOV version 2.5.0-beta

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