LCOV - code coverage report
Current view: top level - gcc/diagnostics - digraphs-to-dot-from-cfg.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 83.0 % 147 122
Test Date: 2026-12-05 16:10:02 Functions: 100.0 % 8 8
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Presentation tweaks for generating .dot from digraphs for GCC CFGs.
       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              : #define INCLUDE_STRING
      25              : #define INCLUDE_VECTOR
      26              : #include "config.h"
      27              : #include "system.h"
      28              : #include "coretypes.h"
      29              : 
      30              : #include "graphviz.h"
      31              : #include "xml.h"
      32              : #include "xml-printer.h"
      33              : #include "diagnostics/digraphs.h"
      34              : #include "diagnostics/digraphs-to-dot.h"
      35              : #include "diagnostics/sarif-sink.h"
      36              : #include "custom-sarif-properties/cfg.h"
      37              : 
      38              : #include "selftest.h"
      39              : 
      40              : /* Disable -Wformat-diag; pp_printf here builds dot labels, not diagnostics.  */
      41              : #if __GNUC__ >= 10
      42              : #  pragma GCC diagnostic push
      43              : #  pragma GCC diagnostic ignored "-Wformat-diag"
      44              : #endif
      45              : 
      46              : namespace diagnostics {
      47              : namespace digraphs {
      48              : namespace to_dot {
      49              : 
      50              : namespace {
      51              :   namespace node_properties = custom_sarif_properties::cfg::node;
      52              :   namespace edge_properties = custom_sarif_properties::cfg::edge;
      53              : }
      54              : 
      55           45 : class converter_from_cfg : public converter
      56              : {
      57              : public:
      58              :   std::unique_ptr<dot::graph>
      59           45 :   make_dot_graph_from_diagnostic_graph (const digraph &dg) final override
      60              :   {
      61           45 :     auto dot_graph
      62           45 :       = converter::make_dot_graph_from_diagnostic_graph (dg);
      63              : 
      64              :     /* Add an invisible edge from ENTRY to EXIT, to improve the
      65              :        graph layout.  */
      66           45 :     if (const digraphs::node *entry_node = get_entry_node (dg))
      67           45 :       if (const digraphs::node *exit_node = get_exit_node (dg))
      68              :         {
      69           45 :           auto extra_edge_stmt
      70              :             = std::make_unique<dot::edge_stmt>
      71           90 :             (get_node_id_for_node (*entry_node, "s"),
      72          135 :              get_node_id_for_node (*exit_node, "n"));
      73           45 :           extra_edge_stmt->set_attr (dot::id ("style"), dot::id ("invis"));
      74           45 :           extra_edge_stmt->set_attr (dot::id ("constraint"), dot::id ("true"));
      75           45 :           dot_graph->m_stmt_list.add_stmt (std::move (extra_edge_stmt));
      76           45 :         }
      77              : 
      78           45 :     return dot_graph;
      79              :   }
      80              : 
      81              :   void
      82           45 :   add_any_subgraph_attrs (const digraph_node &input_node,
      83              :                           dot::subgraph &output_subgraph) final override
      84              :   {
      85           45 :     if (const char *kind = input_node.get_property (node_properties::kind))
      86              :       {
      87           45 :         if (strcmp (kind, "function") == 0)
      88              :           {
      89              :           }
      90            0 :         else if (strcmp (kind, "loop") == 0)
      91              :           {
      92            0 :             namespace loop_property_names
      93              :               = custom_sarif_properties::cfg::loop;
      94            0 :             long num;
      95            0 :             if (input_node.maybe_get_property (loop_property_names::num, num))
      96              :               {
      97            0 :                 pretty_printer pp;
      98            0 :                 pp_printf (&pp, "loop %li", num);
      99            0 :                 output_subgraph.add_attr (dot::id ("label"),
     100            0 :                                           dot::id (pp_formatted_text (&pp)));
     101            0 :               }
     102            0 :             long depth;
     103            0 :             if (input_node.maybe_get_property (loop_property_names::depth,
     104              :                                                depth))
     105              :               {
     106            0 :                 const char *fillcolors[3] = { "grey88", "grey77", "grey66" };
     107            0 :                 output_subgraph.add_attr
     108            0 :                   (dot::id ("fillcolor"),
     109            0 :                    dot::id (fillcolors[(depth - 1) % 3]));
     110              :               }
     111            0 :             output_subgraph.add_attr (dot::id ("style"), dot::id ("filled"));
     112            0 :             output_subgraph.add_attr (dot::id ("color"), dot::id ("darkgreen"));
     113            0 :             output_subgraph.add_attr (dot::id ("labeljust"), dot::id ("l"));
     114            0 :             output_subgraph.add_attr (dot::id ("penwidth"), dot::id ("2"));
     115              :           }
     116              :       }
     117           45 :   }
     118              : 
     119              :   void
     120          270 :   add_any_node_attrs (const digraph_node &input_node,
     121              :                       dot::node_stmt &output_node) final override
     122              :   {
     123          270 :     if (const char *node_kind = input_node.get_property (node_properties::kind))
     124          270 :       if (strcmp (node_kind, "basic_block") == 0)
     125              :         {
     126          270 :           namespace bb_property_names
     127              :             = custom_sarif_properties::cfg::basic_block;
     128          270 :           const char *shape = nullptr;
     129          270 :           const char *fillcolor = "lightgrey";
     130          270 :           const char *label = nullptr;
     131          540 :           if (const char *bb_kind
     132          270 :                 = input_node.get_property (bb_property_names::kind))
     133              :             {
     134           90 :               if (strcmp (bb_kind, "entry") == 0)
     135              :                 {
     136              :                   shape = "Mdiamond";
     137              :                   fillcolor = "white";
     138              :                   label = "ENTRY";
     139              :                 }
     140           45 :               else if (strcmp (bb_kind, "exit") == 0)
     141              :                 {
     142              :                   shape = "Mdiamond";
     143              :                   fillcolor = "white";
     144              :                   label = "EXIT";
     145              :                 }
     146            0 :               else if (strcmp (bb_kind, "hot") == 0)
     147              :                 fillcolor = "lightpink";
     148            0 :               else if (strcmp (bb_kind, "cold") == 0)
     149              :                 fillcolor = "lightblue";
     150              :             }
     151              : 
     152              :           if (shape)
     153           90 :             output_node.set_attr (dot::id ("shape"), dot::id (shape));
     154              :           else
     155              :             {
     156          180 :               output_node.set_attr (dot::id ("shape"), dot::id ("none"));
     157          180 :               output_node.set_attr (dot::id ("margin"), dot::id ("0"));
     158              :             }
     159          270 :           output_node.set_attr (dot::id ("fillcolor"), dot::id (fillcolor));
     160          270 :           if (label)
     161           90 :             output_node.set_label (dot::id (label));
     162              :           else
     163              :             {
     164              :               // Create node with table
     165          180 :               xml::element table ("table", false);
     166          180 :               xml::printer xp (table);
     167          180 :               xp.set_attr ("border", "0");
     168          180 :               xp.set_attr ("cellborder", "1");
     169          180 :               xp.set_attr ("cellspacing", "0");
     170              : 
     171          180 :               long bb_index;
     172          180 :               if (input_node.maybe_get_property (bb_property_names::index,
     173              :                                                  bb_index))
     174              :                 {
     175           80 :                   xp.push_tag ("tr", true);
     176           80 :                   xp.push_tag ("td", true);
     177           80 :                   xp.set_attr ("align", "left");
     178           80 :                   pretty_printer pp;
     179           80 :                   pp_printf (&pp, "<bb %li>:", bb_index);
     180           80 :                   xp.add_text_from_pp (pp);
     181           80 :                   xp.pop_tag ("td");
     182           80 :                   xp.pop_tag ("tr");
     183           80 :                 }
     184              : 
     185          360 :               if (json::array *arr
     186          180 :                   = input_node.get_property (bb_property_names::gimple::phis))
     187           80 :                 print_rows_for_strings (*arr, xp);
     188              : 
     189          360 :               if (json::array *arr
     190              :                     = input_node.get_property
     191          180 :                         (bb_property_names::gimple::stmts))
     192           80 :                 print_rows_for_strings (*arr, xp);
     193              : 
     194          360 :               if (json::array *arr
     195          180 :                     = input_node.get_property (bb_property_names::rtl::insns))
     196          100 :                 print_rows_for_strings (*arr, xp);
     197              : 
     198              :               // xml must be done by now
     199              : 
     200          180 :               output_node.m_attrs.add (dot::id ("label"),
     201          360 :                                        dot::id (table));
     202          180 :             }
     203              :         }
     204          270 :   }
     205              : 
     206              :   virtual void
     207          270 :   add_any_edge_attrs (const digraph_edge &input_edge,
     208              :                       dot::edge_stmt &output_edge) final override
     209              :   {
     210          270 :     namespace edge_properties = custom_sarif_properties::cfg::edge;
     211              : 
     212          270 :     const char *style = "solid,bold";
     213          270 :     const char *color = "black";
     214          270 :     int weight = 10;
     215              : 
     216          270 :     if (edge_has_flag (input_edge, "FAKE"))
     217              :       {
     218            0 :         style = "dotted";
     219            0 :         color = "green";
     220            0 :         weight = 0;
     221              :       }
     222          270 :     if (edge_has_flag (input_edge, "DFS_BACK"))
     223              :       {
     224              :         style = "dotted,bold";
     225              :         color = "blue";
     226              :         weight = 10;
     227              :       }
     228          270 :     else if (edge_has_flag (input_edge, "FALLTHRU"))
     229              :       weight = 100;
     230          120 :     else if (edge_has_flag (input_edge, "TRUE_VALUE"))
     231              :       color = "forestgreen";
     232          100 :     else if (edge_has_flag (input_edge, "FALSE_VALUE"))
     233           20 :       color = "darkorange";
     234              : 
     235          270 :     if (edge_has_flag (input_edge, "ABNORMAL"))
     236            0 :       color = "red";
     237              : 
     238          270 :     output_edge.set_attr (dot::id ("style"), dot::id (style));
     239          270 :     output_edge.set_attr (dot::id ("color"), dot::id (color));
     240          270 :     output_edge.set_attr (dot::id ("weight"),
     241          540 :                           dot::id (std::to_string (weight)));
     242          270 :     output_edge.set_attr (dot::id ("constraint"),
     243          540 :                           dot::id ((edge_has_flag (input_edge, "FAKE")
     244          270 :                                     || edge_has_flag (input_edge, "DFS_BACK"))
     245          540 :                                    ? "false" : "true"));
     246              : 
     247          270 :     long probability_pc;
     248          270 :     if (input_edge.maybe_get_property (edge_properties::probability_pc,
     249              :                                        probability_pc))
     250              :       {
     251           48 :         pretty_printer pp;
     252           48 :         pp_printf (&pp, "[%li%%]", probability_pc);
     253           48 :         output_edge.set_label (dot::id (pp_formatted_text (&pp)));
     254           48 :       }
     255          270 :   }
     256              : 
     257              :   private:
     258              :     bool
     259         1840 :     edge_has_flag (const digraph_edge &input_edge,
     260              :                    const char *flag_name) const
     261              :     {
     262         1840 :       auto flags = input_edge.get_property (edge_properties::flags);
     263         5250 :       for (auto iter : *flags)
     264         1200 :         if (auto str = iter->dyn_cast_string ())
     265         1200 :           if (!strcmp (flag_name, str->get_string ()))
     266              :             return true;
     267              :       return false;
     268              :     }
     269              : 
     270              :     void
     271          260 :     print_rows_for_strings (json::array &arr,
     272              :                             xml::printer &xp)
     273              :     {
     274         1433 :       for (auto iter : arr)
     275          779 :         if (auto js_str = iter->dyn_cast_string ())
     276              :           {
     277          779 :             xp.push_tag ("tr", true);
     278          779 :             xp.push_tag ("td", true);
     279          779 :             xp.set_attr ("align", "left");
     280          779 :             xp.add_text (js_str->get_string ());
     281          779 :             xp.pop_tag ("td");
     282          779 :             xp.pop_tag ("tr");
     283              :           }
     284          260 :     }
     285              : 
     286              :   const node *
     287           90 :   get_bb_node_by_kind (const digraph &dg, const char *kind) const
     288              :   {
     289          135 :     for (auto &iter : dg.get_all_nodes ())
     290              :       {
     291          135 :         const node &input_node = *iter.second;
     292          135 :         if (const char *node_kind = input_node.get_property (node_properties::kind))
     293          135 :           if (strcmp (node_kind, "basic_block") == 0)
     294              :             {
     295          135 :               namespace bb_property_names
     296              :                 = custom_sarif_properties::cfg::basic_block;
     297          270 :               if (const char *bb_kind
     298          135 :                   = input_node.get_property (bb_property_names::kind))
     299              :                 {
     300          135 :                   if (strcmp (bb_kind, kind) == 0)
     301           90 :                     return &input_node;
     302              :                 }
     303              :             }
     304              :       }
     305              :     return nullptr;
     306              :   }
     307              : 
     308              :   const node *
     309           45 :   get_entry_node (const digraph &dg) const
     310              :   {
     311           45 :     return get_bb_node_by_kind (dg, "entry");
     312              :   }
     313              : 
     314              :   const node *
     315           45 :   get_exit_node (const digraph &dg) const
     316              :   {
     317           45 :     return get_bb_node_by_kind (dg, "exit");
     318              :   }
     319              : };
     320              : 
     321              : std::unique_ptr<converter>
     322           45 : make_converter_from_cfg ()
     323              : {
     324           45 :   return std::make_unique<converter_from_cfg> ();
     325              : }
     326              : 
     327              : } // namespace to_dot
     328              : } // namespace digraphs
     329              : } // namespace diagnostics
     330              : 
     331              : #if __GNUC__ >= 10
     332              : #  pragma GCC diagnostic pop
     333              : #endif
        

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.