LCOV - code coverage report
Current view: top level - gcc/analyzer - ana-state-to-diagnostic-state.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 79.9 % 403 322
Test Date: 2026-12-05 16:10:02 Functions: 95.5 % 22 21
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Creating diagnostic state graphs from ana::program_state.
       2              :    Copyright (C) 2025-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              : #define INCLUDE_ALGORITHM
      22              : #define INCLUDE_MAP
      23              : #define INCLUDE_SET
      24              : #include "analyzer/common.h"
      25              : 
      26              : #include "diagnostics/state-graphs.h"
      27              : #include "diagnostics/sarif-sink.h"
      28              : 
      29              : #include "analyzer/region-model.h"
      30              : #include "analyzer/program-state.h"
      31              : #include "analyzer/record-layout.h"
      32              : #include "analyzer/ana-state-to-diagnostic-state.h"
      33              : 
      34              : #if ENABLE_ANALYZER
      35              : 
      36              : #if __GNUC__ >= 10
      37              : #pragma GCC diagnostic ignored "-Wformat"
      38              : #endif
      39              : 
      40              : namespace ana {
      41              : 
      42              : namespace node_properties = custom_sarif_properties::state_graphs::node;
      43              : 
      44              : static void
      45            8 : set_wi_attr (diagnostics::digraphs::node &state_node,
      46              :              const json::string_property &property,
      47              :              const wide_int_ref &w,
      48              :              signop sgn)
      49              : {
      50            8 :   pretty_printer pp;
      51            8 :   pp_wide_int (&pp, w, sgn);
      52            8 :   state_node.set_property (property, pp_formatted_text (&pp));
      53            8 : }
      54              : 
      55              : static void
      56          248 : set_type_attr (diagnostics::digraphs::node &state_node,
      57              :                const_tree type)
      58              : {
      59          248 :   gcc_assert (type);
      60          248 :   pretty_printer pp;
      61          248 :   pp_format_decoder (&pp) = default_tree_printer;
      62          248 :   pp_printf (&pp, "%T", type);
      63          248 :   state_node.set_property (node_properties::type,
      64              :                            pp_formatted_text (&pp));
      65          248 : }
      66              : 
      67              : static void
      68          272 : set_bits_attr (diagnostics::digraphs::node & state_node,
      69              :                const concrete_bit_range &bits)
      70              : {
      71          272 :   pretty_printer pp;
      72          272 :   bits.dump_to_pp (&pp);
      73          272 :   state_node.set_property (node_properties::bits,
      74              :                            pp_formatted_text (&pp));
      75          272 : }
      76              : 
      77              : static void
      78          224 : set_value_attrs (diagnostics::digraphs::node &state_node,
      79              :                  const svalue &sval)
      80              : {
      81          224 :   state_node.set_property (node_properties::value,
      82          224 :                            sval.to_json ());
      83          224 :   pretty_printer pp;
      84          224 :   pp_format_decoder (&pp) = default_tree_printer;
      85          224 :   sval.dump_to_pp (&pp, true);
      86          224 :   state_node.set_property (node_properties::value_str,
      87              :                            pp_formatted_text (&pp));
      88          224 : }
      89              : 
      90              : 
      91              : // class analyzer_state_graph : public diagnostics::digraphs::digraph
      92              : 
      93           28 : analyzer_state_graph::analyzer_state_graph (const program_state &state,
      94              :                                             const extrinsic_state &ext_state)
      95           28 : : m_state (state),
      96           28 :   m_ext_state (ext_state),
      97           28 :   m_node_mgr (ext_state.get_engine ()->get_node_manager ()),
      98           28 :   m_mgr (*ext_state.get_engine ()->get_model_manager ()),
      99           28 :   m_next_id (0)
     100              : {
     101              :   /* Find pointers to heap-allocated regions, and record their types,
     102              :      so that we have a user-friendly way of showing the memory
     103              :      (by field, rather than by byte offset).  */
     104          106 :   for (auto cluster_iter : *state.m_region_model->get_store ())
     105           78 :     cluster_iter.second->for_each_value
     106           78 :       ([](const symbolic::value_node &val,
     107              :           void *user_data) -> iter_result
     108              :       {
     109          112 :         return val.for_each_svalue ([] (const svalue &sval,
     110              :                                         void *user_data) -> iter_result
     111              :         {
     112          112 :           auto *sg = (analyzer_state_graph *)user_data;
     113          112 :           if (auto *sym_ptr = sval.maybe_get_symbolic_pointer ())
     114           48 :             if (sym_ptr->get_byte_offset ().is_zero_p ())
     115              :               {
     116           48 :                 auto &base_reg = sym_ptr->get_base_region ();
     117           48 :                 if (sval.get_type () && !base_reg.get_type ())
     118              :                   {
     119           48 :                     tree pointed_to_type = TREE_TYPE (sval.get_type ());
     120           48 :                     if (!VOID_TYPE_P (pointed_to_type))
     121           48 :                       sg->m_types_for_untyped_regions[&base_reg] = pointed_to_type;
     122              :                   }
     123              :               }
     124          112 :           return iter_result::keep_going;
     125              :         }, user_data);
     126              :       }, this);
     127              : 
     128              :   /* TODO: look for vtable pointers at the top of dynamically-allocated
     129              :      regions and use that type as a fallback.  */
     130              : 
     131              :   /* Find regions of interest.
     132              :      Create elements per region, and build into hierarchy.
     133              :      Add edges for pointers.  */
     134              : 
     135              :   /* Create stack, from top to bottom.  */
     136           67 :   for (int i = state.m_region_model->get_stack_depth () - 1; i >= 0; --i)
     137              :     {
     138           39 :       const frame_region *reg = state.m_region_model->get_frame_at_index (i);
     139           39 :       get_or_create_state_node (*reg);
     140              :     }
     141              : 
     142              :   /* Create bound memory.  */
     143          106 :   for (auto iter : *state.m_region_model->get_store ())
     144              :     {
     145           78 :       const bool create_all = false; // "true" for verbose, for debugging
     146           78 :       create_state_nodes_for_binding_cluster (*iter.second, create_all);
     147              :     }
     148              : 
     149              :   /* TODO: Constraints.  */
     150              : 
     151              :   /* Annotate with information from state machines.  */
     152           28 :   {
     153           28 :     int i;
     154           28 :     sm_state_map *smap;
     155          224 :     FOR_EACH_VEC_ELT (state.m_checker_states, i, smap)
     156              :       {
     157          196 :         auto &sm = ext_state.get_sm (i);
     158          240 :         for (const auto &iter : *smap)
     159           44 :           sm.add_state_to_state_graph (*this, *iter.first, iter.second.m_state);
     160          196 :         if (auto s = smap->get_global_state ())
     161          196 :           sm.add_global_state_to_state_graph (*this, s);
     162              :       }
     163              :   }
     164              : 
     165              :   /* Process pending edges.  */
     166           76 :   while (m_pending_edges.size () > 0)
     167              :     {
     168           48 :       pending_edge item = m_pending_edges.back ();
     169           48 :       m_pending_edges.pop_back ();
     170              : 
     171              :       /* Ensure we have a node for the dst region.  This
     172              :          could lead to additional pending edges.  */
     173           48 :       auto &dst_node = get_or_create_state_node (item.m_dst_reg);
     174           48 :       add_edge (nullptr, item.m_src_node, dst_node);
     175              :     }
     176           28 : }
     177              : 
     178              : diagnostics::digraphs::node &
     179          384 : analyzer_state_graph::get_or_create_state_node (const region &reg)
     180              : {
     181          384 :   auto existing = m_region_to_state_node_map.find (&reg);
     182          384 :   if (existing != m_region_to_state_node_map.end ())
     183          187 :     return *existing->second;
     184              : 
     185          197 :   auto &state_node = create_and_add_state_node (reg);
     186          197 :   m_region_to_state_node_map[&reg] = &state_node;
     187          197 :   return state_node;
     188              : }
     189              : 
     190              : diagnostics::digraphs::node &
     191          197 : analyzer_state_graph::create_and_add_state_node (const region &reg)
     192              : {
     193          197 :   auto node = create_state_node (reg);
     194              : 
     195          197 :   diagnostics::digraphs::node &result = *node;
     196          197 :   if (auto parent_reg = reg.get_parent_region ())
     197          197 :     if (parent_reg->get_kind () != RK_ROOT)
     198              :       {
     199          127 :         auto &parent_state_node = get_or_create_state_node (*parent_reg);
     200          127 :         parent_state_node.add_child (std::move (node));
     201          127 :         return result;
     202              :       }
     203           70 :   add_node (std::move (node));
     204           70 :   return result;
     205          197 : }
     206              : 
     207              : std::string
     208          190 : analyzer_state_graph::make_node_id (const char *prefix)
     209              : {
     210          380 :   return std::string (prefix) + "-" + std::to_string (m_next_id++);
     211              : }
     212              : 
     213              : std::string
     214          313 : analyzer_state_graph::make_node_id (const region &reg)
     215              : {
     216          313 :   const char *prefix = nullptr;
     217          313 :   switch (reg.get_kind ())
     218              :     {
     219            0 :     case RK_ROOT:
     220            0 :     default:
     221            0 :       gcc_unreachable ();
     222           70 :       break;
     223              : 
     224           70 :     case RK_MEMORY_SPACE:
     225           70 :       {
     226           70 :         auto memspace = reg.get_memory_space ();
     227           70 :         switch (memspace)
     228              :           {
     229            0 :           default:
     230            0 :             gcc_unreachable ();
     231           70 :           case memory_space::unknown:
     232           70 :           case memory_space::code:
     233           70 :           case memory_space::globals:
     234           70 :           case memory_space::stack:
     235           70 :           case memory_space::heap:
     236           70 :           case memory_space::read_only_data:
     237           70 :             return get_dump_name (memspace);
     238              : 
     239              :           case memory_space::thread_local_:
     240              :             prefix = "thread-local-region";
     241              :             break;
     242            0 :           case memory_space::private_:
     243            0 :             prefix = "private";
     244            0 :             break;
     245              :           }
     246              :       }
     247              :       break;
     248              : 
     249              :     case RK_FRAME:
     250              :       prefix = "frame-region";
     251              :       break;
     252            0 :     case RK_FUNCTION:
     253            0 :       prefix = "function-region";
     254            0 :       break;
     255            0 :     case RK_LABEL:
     256            0 :       prefix = "label-region";
     257            0 :       break;
     258            0 :     case RK_SYMBOLIC:
     259            0 :       prefix = "symbolic-region";
     260            0 :       break;
     261           44 :     case RK_DECL:
     262           44 :       prefix = "decl-region";
     263           44 :       break;
     264           74 :     case RK_FIELD:
     265           74 :       prefix = "field-region";
     266           74 :       break;
     267            8 :     case RK_ELEMENT:
     268            8 :       prefix = "element-region";
     269            8 :       break;
     270            0 :     case RK_OFFSET:
     271            0 :       prefix = "offset-region";
     272            0 :       break;
     273            0 :     case RK_SIZED:
     274            0 :       prefix = "sized-region";
     275            0 :       break;
     276            0 :     case RK_CAST:
     277            0 :       prefix = "cast-region";
     278            0 :       break;
     279           44 :     case RK_HEAP_ALLOCATED:
     280           44 :       prefix = "heap-allocated-region";
     281           44 :       break;
     282            0 :     case RK_ALLOCA:
     283            0 :       prefix = "alloca-region";
     284            0 :       break;
     285            0 :     case RK_STRING:
     286            0 :       prefix = "string-region";
     287            0 :       break;
     288           34 :     case RK_BIT_RANGE:
     289           34 :       prefix = "bit-range-region";
     290           34 :       break;
     291            0 :     case RK_VAR_ARG:
     292            0 :       prefix = "var-arg-region";
     293            0 :       break;
     294            0 :     case RK_ERRNO:
     295            0 :       prefix = "errno-region";
     296            0 :       break;
     297            0 :     case RK_PRIVATE:
     298            0 :       prefix = "private-region";
     299            0 :       break;
     300            0 :     case RK_UNKNOWN:
     301            0 :       prefix = "unknown-region";
     302            0 :       break;
     303              :     }
     304          486 :   return std::string (prefix) + "-" + std::to_string (reg.get_id ());
     305              : }
     306              : 
     307              : std::unique_ptr<diagnostics::digraphs::node>
     308          503 : analyzer_state_graph::
     309              : make_state_node (enum node_properties::kind_t kind,
     310              :                  std::string id)
     311              : {
     312          503 :   auto node = std::make_unique<diagnostics::digraphs::node> (*this, std::move (id));
     313          503 :   node->set_property (node_properties::kind_prop, kind);
     314          503 :   return node;
     315              : }
     316              : 
     317              : static node_properties::kind_t
     318           70 : get_node_properties_kind_for_memory_space (memory_space memspace)
     319              : {
     320           70 :   switch (memspace)
     321              :     {
     322            0 :     default:
     323            0 :       gcc_unreachable ();
     324              :     case memory_space::unknown:
     325              :     case memory_space::read_only_data:
     326              :     case memory_space::private_:
     327              :       return node_properties::kind_t::other;
     328              : 
     329              :     case memory_space::code:
     330              :       return node_properties::kind_t::code;
     331              : 
     332              :     case memory_space::globals:
     333              :       return node_properties::kind_t::globals;
     334              : 
     335              :     case memory_space::stack:
     336              :       return node_properties::kind_t::stack;
     337              : 
     338              :     case memory_space::heap:
     339              :       return node_properties::kind_t::heap_;
     340              : 
     341              :     case memory_space::thread_local_:
     342              :       return node_properties::kind_t::thread_local_;
     343              :     }
     344              : }
     345              : 
     346              : std::unique_ptr<diagnostics::digraphs::node>
     347          114 : analyzer_state_graph::
     348              : make_memspace_state_node (const region &reg,
     349              :                           node_properties::kind_t kind)
     350              : {
     351          114 :   return make_state_node (kind, make_node_id (reg));
     352              : }
     353              : 
     354              : std::unique_ptr<diagnostics::digraphs::node>
     355          197 : analyzer_state_graph::create_state_node (const region &reg)
     356              : {
     357          197 :   std::unique_ptr<diagnostics::digraphs::node> node;
     358              : 
     359          197 :   switch (reg.get_kind ())
     360              :     {
     361            0 :     default:
     362            0 :       gcc_unreachable ();
     363              : 
     364           39 :     case RK_FRAME:
     365           39 :       {
     366           39 :         const frame_region &frame_reg
     367              :           = static_cast<const frame_region &> (reg);
     368              : 
     369           78 :         node = make_state_node (node_properties::kind_t::stack_frame,
     370           78 :                                 make_node_id (reg));
     371           39 :         node->set_logical_loc
     372           39 :           (m_logical_loc_mgr.key_from_tree (frame_reg.get_fndecl ()));
     373           39 :         {
     374           39 :           pretty_printer pp;
     375           39 :           pp_format_decoder (&pp) = default_tree_printer;
     376           39 :           pp_printf (&pp, "%E", frame_reg.get_fndecl ());
     377           39 :           node->set_property (node_properties::function,
     378              :                               pp_formatted_text (&pp));
     379           39 :         }
     380              :       }
     381           39 :       break;
     382              : 
     383           70 :     case RK_MEMORY_SPACE:
     384           70 :       {
     385           70 :         auto memspace = reg.get_memory_space ();
     386           70 :         auto kind = get_node_properties_kind_for_memory_space (memspace);
     387           70 :         node = make_memspace_state_node (reg, kind);
     388              :       }
     389           70 :       break;
     390              : 
     391            0 :     case RK_FUNCTION:
     392            0 :       node = make_memspace_state_node (reg,
     393            0 :                                        node_properties::kind_t::function);
     394              :       // TODO
     395            0 :       break;
     396            0 :     case RK_ROOT:
     397            0 :       gcc_unreachable ();
     398            0 :       break;
     399            0 :     case RK_SYMBOLIC:
     400            0 :       node = make_memspace_state_node (reg,
     401            0 :                                        node_properties::kind_t::other);
     402            0 :       break;
     403              : 
     404           44 :     case RK_DECL:
     405           44 :       {
     406           88 :         node = make_state_node (node_properties::kind_t::variable,
     407           88 :                                 make_node_id (reg));
     408           44 :         const decl_region &decl_reg
     409              :           = static_cast<const decl_region &> (reg);
     410              : 
     411           44 :         {
     412           44 :           pretty_printer pp;
     413           44 :           pp_format_decoder (&pp) = default_tree_printer;
     414           44 :           pp_printf (&pp, "%E", decl_reg.get_decl ());
     415           44 :           node->set_property (node_properties::name,
     416              :                               pp_formatted_text (&pp));
     417           44 :         }
     418           44 :         set_type_attr (*node, TREE_TYPE (decl_reg.get_decl ()));
     419              :       }
     420           44 :       break;
     421              : 
     422            0 :     case RK_FIELD:
     423            0 :     case RK_ELEMENT:
     424              :       /* These should be handled in populate_state_node_for_typed_region.  */
     425            0 :       gcc_unreachable ();
     426            0 :       break;
     427              : 
     428            0 :     case RK_LABEL:
     429            0 :     case RK_OFFSET:
     430            0 :     case RK_SIZED:
     431            0 :     case RK_CAST:
     432            0 :     case RK_STRING:
     433            0 :     case RK_BIT_RANGE:
     434            0 :     case RK_VAR_ARG:
     435            0 :     case RK_ERRNO:
     436            0 :     case RK_PRIVATE:
     437            0 :     case RK_UNKNOWN:
     438            0 :       node = make_state_node (node_properties::kind_t::other,
     439            0 :                                 make_node_id (reg));
     440            0 :       break;
     441              : 
     442           44 :     case RK_HEAP_ALLOCATED:
     443           44 :     case RK_ALLOCA:
     444           44 :       node
     445           44 :         = make_memspace_state_node (reg,
     446           44 :                                     node_properties::kind_t::dynalloc_buffer);
     447           44 :       set_attr_for_dynamic_extents (reg, *node);
     448           44 :       break;
     449              :     }
     450          197 :   gcc_assert (node);
     451              : 
     452          197 :   if (reg.get_base_region () == &reg)
     453           88 :     if (!reg.get_type ())
     454              :       {
     455           44 :         auto search
     456           44 :           = m_types_for_untyped_regions.find (&reg);
     457           44 :         if (search != m_types_for_untyped_regions.end ())
     458              :           {
     459           44 :             tree type_to_use = search->second;
     460           44 :             set_type_attr (*node, type_to_use);
     461              :           }
     462              :       }
     463              : 
     464          197 :   return node;
     465              : }
     466              : 
     467              : void
     468           78 : analyzer_state_graph::
     469              : create_state_nodes_for_binding_cluster (const binding_cluster &cluster,
     470              :                                         bool create_all)
     471              : {
     472              :   /* TODO:
     473              :      - symbolic bindings
     474              :      - get current svalue, so as to get "zeros" and "uninitialized".  */
     475              : 
     476           78 :   concrete_bindings_t conc_bindings;
     477              : 
     478           78 :   auto &bc = cluster.get_bit_contents ();
     479           78 :   if (auto *conc_bit_overlay = bc.dyn_cast_concrete_bit_overlay ())
     480          190 :     for (auto iter : conc_bit_overlay->m_concrete_range_map)
     481              :       {
     482          112 :         auto &conc_bit_range = iter.first;
     483          112 :         auto &bv = iter.second;
     484          112 :         auto *sval = m_mgr.get_svalue_from_bit_vector (NULL_TREE, bv);
     485          112 :         conc_bindings[conc_bit_range] = sval;
     486          112 :         if (auto *sym_ptr = sval->maybe_get_symbolic_pointer ())
     487           48 :           get_or_create_state_node (sym_ptr->get_base_region ());
     488          112 :       }
     489              : 
     490           78 :   auto &ref = get_or_create_state_node (cluster.get_region ());
     491              : 
     492           78 :   ref.add_child (create_state_node_for_conc_bindings (conc_bindings));
     493              : 
     494           78 :   const region *typed_reg = &cluster.get_region ();
     495           78 :   if (!typed_reg->get_type ())
     496              :     {
     497           34 :       auto search
     498           34 :         = m_types_for_untyped_regions.find (typed_reg);
     499           34 :       if (search != m_types_for_untyped_regions.end ())
     500              :         {
     501           34 :           tree type_to_use = search->second;
     502           34 :           typed_reg = m_mgr.get_cast_region (typed_reg, type_to_use);
     503              :         }
     504              :     }
     505              : 
     506           78 :   if (typed_reg->get_type ())
     507           78 :     populate_state_node_for_typed_region (ref,
     508              :                                           *typed_reg,
     509              :                                           conc_bindings,
     510              :                                           create_all);
     511              :   else
     512              :     {
     513              :       // TODO
     514              :     }
     515           78 : }
     516              : 
     517              : std::unique_ptr<diagnostics::digraphs::node>
     518           78 : analyzer_state_graph::create_state_node_for_conc_bindings (const concrete_bindings_t &conc_bindings)
     519              : {
     520           78 :   auto node = make_state_node (node_properties::kind_t::other,
     521           78 :                                make_node_id ("concrete-bindings"));
     522          190 :   for (auto iter : conc_bindings)
     523              :     {
     524          112 :       const concrete_bit_range &bits = iter.first;
     525          112 :       const svalue *sval = iter.second;
     526          112 :       auto binding_state_node
     527              :         = make_state_node (node_properties::kind_t::other,
     528          112 :                            make_node_id ("binding"));
     529          112 :       set_bits_attr (*binding_state_node, bits);
     530          112 :       gcc_assert (sval);
     531          112 :       set_value_attrs (*binding_state_node, *sval);
     532          112 :       node->add_child (std::move (binding_state_node));
     533          224 :     }
     534           78 :   return node;
     535              : }
     536              : 
     537              : // Try to get the bit_range of REG within its base region
     538              : bool
     539          298 : analyzer_state_graph::get_bit_range_within_base_region (const region &reg,
     540              :                                                         concrete_bit_range &out)
     541              : {
     542          298 :   symbolic::memory_range mem_range
     543          298 :     = reg.get_memory_range (m_node_mgr.get_region_model_manager ());
     544              : 
     545          298 :   concrete_bit_range conc_bit_range;
     546          298 :   if (!concrete_from_symbolic (mem_range.get_bit_range (),
     547              :                                &out))
     548              :     return false;
     549              : 
     550              :   return true;
     551          298 : }
     552              : 
     553              : void
     554          160 : analyzer_state_graph::
     555              : populate_state_node_for_typed_region (diagnostics::digraphs::node &state_node,
     556              :                                       const region &reg,
     557              :                                       const concrete_bindings_t &conc_bindings,
     558              :                                       bool create_all)
     559              : {
     560          160 :   const_tree reg_type = reg.get_type ();
     561          160 :   gcc_assert (reg_type);
     562          160 :   set_type_attr (state_node, reg_type);
     563              : 
     564          160 :   concrete_bit_range bits;
     565          160 :   if (get_bit_range_within_base_region (reg, bits))
     566              :     {
     567          160 :       set_bits_attr (state_node, bits);
     568              : 
     569          160 :       auto search = conc_bindings.find (bits);
     570          160 :       if (search != conc_bindings.end ())
     571              :         {
     572          112 :           const svalue *bound_sval = search->second;
     573          112 :           gcc_assert (bound_sval);
     574          112 :           set_value_attrs (state_node, *bound_sval);
     575          112 :           if (auto *sym_ptr = bound_sval->maybe_get_symbolic_pointer ())
     576           48 :             m_pending_edges.push_back
     577           48 :               ({state_node, sym_ptr->get_base_region ()});
     578              :         }
     579              :     }
     580              : 
     581          160 :   switch (TREE_CODE (reg_type))
     582              :     {
     583              :     default:
     584              :       break;
     585              : 
     586            8 :     case ARRAY_TYPE:
     587            8 :       {
     588            8 :         tree domain = TYPE_DOMAIN (reg_type);
     589            8 :         if (!domain)
     590              :           return;
     591            8 :         const_tree max_idx = TYPE_MAX_VALUE (domain);
     592            8 :         if (!max_idx)
     593              :           return;
     594            8 :         if (TREE_CODE (max_idx) != INTEGER_CST)
     595              :           return;
     596            8 :         const_tree min_idx = TYPE_MIN_VALUE (domain);
     597            8 :         if (TREE_CODE (min_idx) != INTEGER_CST)
     598              :           return;
     599           30 :         for (offset_int idx = wi::to_offset (min_idx);
     600           30 :              idx <= wi::to_offset (max_idx);
     601           22 :              ++idx)
     602              :           {
     603           22 :             const_tree element_type = TREE_TYPE (reg_type);
     604           22 :             const svalue *sval_index
     605           22 :               = m_mgr.get_or_create_int_cst (domain, idx);
     606           22 :             const region *child_reg
     607           22 :               = m_mgr.get_element_region (&reg,
     608              :                                           const_cast<tree> (element_type),
     609              :                                           sval_index);
     610           22 :             if (show_child_state_node_for_child_region_p (*child_reg,
     611              :                                                        conc_bindings,
     612              :                                                        create_all))
     613              :               {
     614            8 :                 auto child_state_node
     615              :                   = make_state_node
     616              :                       (node_properties::kind_t::element,
     617            8 :                        make_node_id (*child_reg));
     618            8 :                 set_wi_attr (*child_state_node,
     619            8 :                              node_properties::index, idx, UNSIGNED);
     620              : 
     621              :                 // Recurse:
     622            8 :                 gcc_assert (element_type);
     623            8 :                 populate_state_node_for_typed_region (*child_state_node,
     624              :                                                       *child_reg,
     625              :                                                       conc_bindings,
     626              :                                                       create_all);
     627            8 :                 state_node.add_child (std::move (child_state_node));
     628            8 :               }
     629              :           }
     630              :       }
     631            8 :       break;
     632              : 
     633           40 :     case RECORD_TYPE:
     634           40 :       {
     635           40 :         const record_layout layout (reg_type);
     636          236 :         for (auto item : layout)
     637              :           {
     638          116 :             if (item.m_is_padding)
     639              :               {
     640           36 :                 const concrete_bit_range bits
     641           72 :                   (0_bits, item.m_bit_range.get_size ());
     642           36 :                 const region *child_reg
     643           36 :                   = m_mgr.get_bit_range (&reg, NULL_TREE, bits);
     644           36 :                 if (show_child_state_node_for_child_region_p (*child_reg,
     645              :                                                               conc_bindings,
     646              :                                                               create_all))
     647              :                   {
     648           34 :                     auto child_state_node
     649              :                       = make_state_node
     650              :                           (node_properties::kind_t::padding,
     651           34 :                            make_node_id (*child_reg));
     652           34 :                     child_state_node->set_property
     653           68 :                       (node_properties::num_bits,
     654           34 :                        item.m_bit_range.get_size ().to_json ());
     655           34 :                     state_node.add_child (std::move (child_state_node));
     656           34 :                   }
     657           36 :               }
     658              :             else
     659              :               {
     660           80 :                 const region *child_reg
     661           80 :                   = m_mgr.get_field_region (&reg,
     662              :                                             const_cast<tree> (item.m_field));
     663           80 :                 if (show_child_state_node_for_child_region_p (*child_reg,
     664              :                                                               conc_bindings,
     665              :                                                               create_all))
     666              :                   {
     667           74 :                     auto child_state_node
     668              :                       = make_state_node
     669              :                           (node_properties::kind_t::field,
     670           74 :                            make_node_id (*child_reg));
     671           74 :                     {
     672           74 :                       pretty_printer pp;
     673           74 :                       pp_format_decoder (&pp) = default_tree_printer;
     674           74 :                       pp_printf (&pp, "%D", item.m_field);
     675           74 :                       child_state_node->set_property (node_properties::name,
     676              :                                                       pp_formatted_text (&pp));
     677           74 :                     }
     678              : 
     679              :                     // Recurse:
     680           74 :                     populate_state_node_for_typed_region (*child_state_node,
     681              :                                                           *child_reg,
     682              :                                                           conc_bindings,
     683              :                                                           create_all);
     684           74 :                     state_node.add_child (std::move (child_state_node));
     685           74 :                   }
     686              :               }
     687          116 :           }
     688           40 :       }
     689           40 :       break;
     690              :     }
     691          160 : }
     692              : 
     693              : void
     694           44 : analyzer_state_graph::
     695              : set_attr_for_dynamic_extents (const region &reg,
     696              :                               diagnostics::digraphs::node &state_node)
     697              : {
     698           44 :   const svalue *sval = m_state.m_region_model->get_dynamic_extents (&reg);
     699           44 :   if (sval)
     700              :     {
     701           42 :       pretty_printer pp;
     702           42 :       pp_format_decoder (&pp) = default_tree_printer;
     703           42 :       if (auto cst = sval->maybe_get_constant ())
     704           42 :         pp_wide_int (&pp, wi::to_wide (cst), UNSIGNED);
     705              :       else
     706            0 :         sval->dump_to_pp (&pp, true);
     707           42 :       state_node.set_property (state_node_properties::dynamic_extents,
     708              :                                pp_formatted_text (&pp));
     709           42 :     }
     710           44 : }
     711              : 
     712              : bool
     713          138 : analyzer_state_graph::
     714              : show_child_state_node_for_child_region_p (const region &reg,
     715              :                                           const concrete_bindings_t &conc_bindings,
     716              :                                           bool create_all)
     717              : {
     718          138 :   if (create_all)
     719              :     return true;
     720          138 :   concrete_bit_range reg_bits;
     721          138 :   if (!get_bit_range_within_base_region (reg, reg_bits))
     722              :     return true;
     723              : 
     724              :   /* Is any of "bits" bound?
     725              :      TODO: ideally there would be a more efficient way to do this, using
     726              :      spatial relationships.  */
     727          194 :   for (auto iter : conc_bindings)
     728              :     {
     729          172 :       const concrete_bit_range &bound_bits = iter.first;
     730          172 :       if (bound_bits.intersects_p (reg_bits))
     731          116 :         return true;
     732          172 :     }
     733           22 :   return false;
     734          138 : }
     735              : 
     736              : std::unique_ptr<diagnostics::digraphs::digraph>
     737           28 : program_state::
     738              : make_diagnostic_state_graph (const extrinsic_state &ext_state) const
     739              : {
     740           28 :   return std::make_unique<analyzer_state_graph> (*this, ext_state);
     741              : }
     742              : 
     743              : void
     744            0 : program_state::dump_sarif (const extrinsic_state &ext_state) const
     745              : {
     746            0 :   auto g = make_diagnostic_state_graph (ext_state);
     747            0 :   g->dump ();
     748            0 : }
     749              : 
     750              : } // namespace ana
     751              : 
     752              : #endif /* #if ENABLE_ANALYZER */
        

Generated by: LCOV version 2.6-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.