LCOV - code coverage report
Current view: top level - gcc/diagnostics - markup.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 90.2 % 336 303
Test Date: 2026-11-14 16:39:50 Functions: 87.8 % 41 36
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* A tree-like representation for use when building diagnostic messages.
       2              :    Copyright (C) 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 "config.h"
      22              : #define INCLUDE_MAP
      23              : #define INCLUDE_STRING
      24              : #define INCLUDE_VECTOR
      25              : #include "system.h"
      26              : #include "coretypes.h"
      27              : 
      28              : #include "diagnostics/markup.h"
      29              : 
      30              : #include "text-art/dump-widget-info.h"
      31              : 
      32              : #include "selftest.h"
      33              : 
      34              : namespace diagnostics {
      35              : namespace markup {
      36              : 
      37              : static bool
      38          496 : child_needs_parens_p (const node &parent, const node &child)
      39              : {
      40          496 :   if (parent.get_kind () == kind::inline_code)
      41              :     return false;
      42              : 
      43          496 :   switch (child.get_kind ())
      44              :     {
      45              :     case kind::text:
      46              :       return false;
      47              :     case kind::compound:
      48              :     case kind::math_negation:
      49              :     case kind::math_subtraction:
      50              :     case kind::math_sum:
      51              :     case kind::math_product:
      52              :     case kind::math_half_open_interval:
      53              :       return true;
      54              : 
      55              :     case kind::code_element_expr:
      56              :     case kind::code_field_expr:
      57              :     case kind::code_infix_binary_op:
      58              :       return false;
      59              : 
      60              :     case kind::inline_code:
      61              :       return false;
      62              :     }
      63              : 
      64              :   return true;
      65              : }
      66              : 
      67              : /* Lower CHILD of PARENT and add to DST_NODE,
      68              :    wrapping lowered node in parentheses if necessary.  */
      69              : 
      70              : static void
      71          496 : lower_and_append_with_any_parens (compound_node &dst_node,
      72              :                                   const node &parent,
      73              :                                   const node &child,
      74              :                                   const lowering_options &opts)
      75              : {
      76          496 :   bool needs_parens = child_needs_parens_p (parent, child);
      77              : 
      78          496 :   if (needs_parens)
      79           36 :     dst_node.add_child ("(");
      80              : 
      81          496 :   dst_node.add_child (child.lower (opts));
      82              : 
      83          496 :   if (needs_parens)
      84           36 :     dst_node.add_child (")");
      85          496 : }
      86              : 
      87              : 
      88              : // class node
      89              : 
      90              : static void
      91            4 : dump_node_tree_to_pp (const node &n,
      92              :                       text_art::theme *theme,
      93              :                       pretty_printer *pp)
      94              : {
      95            4 :   if (!theme)
      96            0 :     return;
      97              : 
      98            4 :   using namespace text_art;
      99              : 
     100            4 :   style_manager sm;
     101            4 :   style tree_style (get_style_from_color_cap_name ("note"));
     102              : 
     103            4 :   style::id_t tree_style_id (sm.get_or_create_id (tree_style));
     104              : 
     105            4 :   dump_widget_info dwi  (sm, *theme, tree_style_id);
     106            4 :   if (std::unique_ptr<widget> w = n.make_dump_widget (dwi))
     107              :     {
     108            4 :       text_art::canvas c (w->to_canvas (sm));
     109            4 :       c.print_to_pp (pp);
     110            4 :     }
     111            4 : }
     112              : 
     113              : void
     114            0 : node::dump () const
     115              : {
     116            0 :   pretty_printer pp;
     117            0 :   pp.set_output_stream (stderr);
     118            0 :   text_art::ascii_theme theme;
     119            0 :   dump_node_tree_to_pp (*this, &theme, &pp);
     120            0 :   pp_flush (&pp);
     121            0 : }
     122              : 
     123              : // class text : public node
     124              : 
     125              : void
     126         1105 : text::print_to_pp (pretty_printer *pp) const
     127              : {
     128         1105 :   pp_string (pp, m_str.c_str ());
     129         1105 : }
     130              : 
     131              : std::unique_ptr<text_art::tree_widget>
     132           16 : text::make_dump_widget (text_art::dump_widget_info &dwi) const
     133              : {
     134           16 :   return text_art::tree_widget::from_fmt (dwi, nullptr,
     135           16 :                                           "text: %qs", m_str.c_str ());
     136              : }
     137              : 
     138              : std::unique_ptr<node>
     139          677 : text::lower (const lowering_options &opts) const
     140              : {
     141          677 :   return std::make_unique<text> (m_str);
     142              : }
     143              : 
     144              : // class compound_node : public node
     145              : 
     146              : void
     147          348 : compound_node::print_to_pp (pretty_printer *pp) const
     148              : {
     149         1488 :   for (auto &iter : m_children)
     150         1140 :     iter->print_to_pp (pp);
     151          348 : }
     152              : 
     153              : std::unique_ptr<text_art::tree_widget>
     154            8 : compound_node::make_dump_widget (text_art::dump_widget_info &dwi) const
     155              : {
     156            8 :   std::unique_ptr<text_art::tree_widget> w
     157              :     (text_art::tree_widget::from_fmt (dwi, nullptr,
     158              :                                       "%s",
     159            8 :                                       get_class_name ()));
     160              : 
     161           28 :   for (auto &iter : m_children)
     162           40 :     w->add_child (iter->make_dump_widget (dwi));
     163              : 
     164            8 :   return w;
     165              : }
     166              : 
     167              : std::unique_ptr<node>
     168           28 : compound_node::lower (const lowering_options &opts) const
     169              : {
     170           28 :   auto result = std::make_unique<compound_node> ();
     171              : 
     172          156 :   for (size_t idx = 0; idx < get_num_children (); ++idx)
     173          100 :     result->add_child (get_child (idx).lower (opts));
     174              : 
     175           28 :   return result;
     176           28 : }
     177              : 
     178              : void
     179          544 : compound_node::add_child (std::string str)
     180              : {
     181          544 :   add_child (std::make_unique<text> (std::move (str)));
     182          544 : }
     183              : 
     184              : // class unary_node : public node
     185              : 
     186              : void
     187            0 : unary_node::print_to_pp (pretty_printer *pp) const
     188              : {
     189            0 :   pp_string (pp, get_class_name ());
     190            0 :   pp_string (pp, " (");
     191            0 :   m_operand->print_to_pp (pp);
     192            0 :   pp_string (pp, ")");
     193            0 : }
     194              : 
     195              : std::unique_ptr<text_art::tree_widget>
     196            0 : unary_node::make_dump_widget (text_art::dump_widget_info &dwi) const
     197              : {
     198            0 :   std::unique_ptr<text_art::tree_widget> w
     199              :     (text_art::tree_widget::from_fmt (dwi, nullptr,
     200              :                                       "%qs",
     201            0 :                                       get_class_name ()));
     202              : 
     203            0 :   w->add_child (m_operand->make_dump_widget (dwi));
     204              : 
     205            0 :   return w;
     206              : }
     207              : 
     208              : // class binary_node : public node
     209              : 
     210              : void
     211            0 : binary_node::print_to_pp (pretty_printer *pp) const
     212              : {
     213            0 :   pp_string (pp, get_class_name ());
     214            0 :   pp_string (pp, " (");
     215            0 :   m_op0->print_to_pp (pp);
     216            0 :   pp_string (pp, ", ");
     217            0 :   m_op1->print_to_pp (pp);
     218            0 :   pp_string (pp, ")");
     219            0 : }
     220              : 
     221              : std::unique_ptr<text_art::tree_widget>
     222            0 : binary_node::make_dump_widget (text_art::dump_widget_info &dwi) const
     223              : {
     224            0 :   std::unique_ptr<text_art::tree_widget> w
     225              :     (text_art::tree_widget::from_fmt (dwi, nullptr,
     226              :                                       "%qs",
     227            0 :                                       get_class_name ()));
     228              : 
     229            0 :   w->add_child (m_op0->make_dump_widget (dwi));
     230            0 :   w->add_child (m_op1->make_dump_widget (dwi));
     231              : 
     232            0 :   return w;
     233              : }
     234              : 
     235              : // class math::associative_op : public compound_node
     236              : 
     237              : std::unique_ptr<node>
     238          160 : math::associative_op::lower (const lowering_options &opts) const
     239              : {
     240          160 :   gcc_assert (get_num_children () >= 2);
     241              : 
     242          160 :   auto result = std::make_unique<compound_node> ();
     243              : 
     244          160 :   pretty_printer pp_infix_symbol;
     245          320 :   pp_printf (&pp_infix_symbol, " %s ",
     246          160 :              get_symbol (opts.m_use_unicode_symbols));
     247              : 
     248          672 :   for (size_t idx = 0; idx < get_num_children (); ++idx)
     249              :     {
     250          352 :       if (idx > 0)
     251          192 :         result->add_child (pp_formatted_text (&pp_infix_symbol));
     252          352 :       lower_and_append_with_any_parens (*result,
     253              :                                         *this,
     254              :                                         get_child (idx),
     255              :                                         opts);
     256              :     }
     257              : 
     258          160 :   return result;
     259          160 : }
     260              : 
     261              : // class math::sum : public associative_op
     262              : 
     263              : const char *
     264           94 : math::sum::get_symbol (bool /* use_unicode */) const
     265              : {
     266           94 :   return "+";
     267              : }
     268              : 
     269              : // class math::product : public associative_op
     270              : 
     271              : const char *
     272           66 : math::product::get_symbol (bool use_unicode) const
     273              : {
     274           66 :   if (use_unicode)
     275              :     return MARKUP_UNICODE_SYMBOL_MULTIPLICATION;
     276              :   else
     277           42 :     return "*";
     278              : }
     279              : 
     280              : // class math::negation : public unary_node
     281              : 
     282              : std::unique_ptr<node>
     283           32 : math::negation::lower (const lowering_options &opts) const
     284              : {
     285           32 :   auto result = std::make_unique<compound_node> ();
     286           16 :   const char *symbol = (opts.m_use_unicode_symbols
     287           32 :                         ? MARKUP_UNICODE_SYMBOL_UNARY_NEGATION
     288              :                         : "-");
     289           32 :   result->add_child (symbol);
     290           32 :   lower_and_append_with_any_parens (*result, *this, get_operand (), opts);
     291           32 :   return result;
     292           32 : }
     293              : 
     294              : // class math::subtraction : public binary_node
     295              : 
     296              : std::unique_ptr<node>
     297           44 : math::subtraction::lower (const lowering_options &opts) const
     298              : {
     299           44 :   auto result = std::make_unique<compound_node> ();
     300              : 
     301           44 :   lower_and_append_with_any_parens (*result, *this, get_operand_0 (), opts);
     302              : 
     303           28 :   const char *padded_symbol
     304           44 :     = (opts.m_use_unicode_symbols
     305           44 :        ? (" " MARKUP_UNICODE_SYMBOL_SUBTRACTION " ")
     306              :        : " - ");
     307           44 :   result->add_child (padded_symbol);
     308           44 :   lower_and_append_with_any_parens (*result, *this, get_operand_1 (), opts);
     309              : 
     310           44 :   return result;
     311           44 : }
     312              : 
     313              : // class math::half_open_interval : public binary_node
     314              : 
     315              : std::unique_ptr<node>
     316           12 : math::half_open_interval::lower (const lowering_options &opts) const
     317              : {
     318           12 :   auto result = std::make_unique<compound_node> ();
     319              : 
     320           12 :   result->add_child ("[");
     321           12 :   lower_and_append_with_any_parens (*result, *this, get_operand_0 (), opts);
     322           12 :   result->add_child (", ");
     323           12 :   lower_and_append_with_any_parens (*result, *this, get_operand_1 (), opts);
     324           12 :   result->add_child (")");
     325           12 :   return result;
     326           12 : }
     327              : 
     328              : // class code::element_expr : public binary_node
     329              : 
     330              : std::unique_ptr<node>
     331            8 : code::element_expr::lower (const lowering_options &opts) const
     332              : {
     333            8 :   auto result = std::make_unique<compound_node> ();
     334            8 :   result->add_child (get_operand_0 ().lower (opts));
     335            8 :   result->add_child ("[");
     336            8 :   result->add_child (get_operand_1 ().lower (opts));
     337            8 :   result->add_child ("]");
     338            8 :   return result;
     339            8 : }
     340              : 
     341              : // class code::field_expr : public binary_node
     342              : 
     343              : std::unique_ptr<node>
     344           16 : code::field_expr::lower (const lowering_options &opts) const
     345              : {
     346           16 :   auto result = std::make_unique<compound_node> ();
     347           16 :   result->add_child (get_operand_0 ().lower (opts));
     348           16 :   result->add_child (".");
     349           16 :   result->add_child (get_operand_1 ().lower (opts));
     350           16 :   return result;
     351           16 : }
     352              : 
     353              : // class code::infix_binary_op : public binary_node
     354              : 
     355              : std::unique_ptr<node>
     356           20 : code::infix_binary_op::lower (const lowering_options &opts) const
     357              : {
     358           20 :   pretty_printer pp_infix_symbol;
     359           20 :   pp_printf (&pp_infix_symbol, " %s ", m_symbol);
     360              : 
     361           20 :   auto result = std::make_unique<compound_node> ();
     362           20 :   result->add_child (get_operand_0 ().lower (opts));
     363           20 :   result->add_child (pp_formatted_text (&pp_infix_symbol));
     364           20 :   result->add_child (get_operand_1 ().lower (opts));
     365           20 :   return result;
     366           20 : }
     367              : 
     368              : // class inline_code : public compound_node
     369              : 
     370              : void
     371           28 : inline_code::print_to_pp (pretty_printer *pp) const
     372              : {
     373           28 :   pp_begin_quote (pp, pp_show_color (pp));
     374           28 :   compound_node::print_to_pp (pp);
     375           28 :   pp_end_quote (pp, pp_show_color (pp));
     376           28 : }
     377              : 
     378              : std::unique_ptr<node>
     379           28 : inline_code::lower (const lowering_options &opts) const
     380              : {
     381           28 :   auto result = std::make_unique<inline_code> ();
     382              : 
     383           84 :   for (size_t idx = 0; idx < get_num_children (); ++idx)
     384           28 :     result->add_child (get_child (idx).lower (opts));
     385              : 
     386           28 :   return result;
     387           28 : }
     388              : 
     389              : } // namespace markup
     390              : 
     391              : #if CHECKING_P
     392              : 
     393              : namespace selftest {
     394              : 
     395              : /* Implementation detail of ASSERT_SYMBOL_MARKUP_TEXT_EQ.  */
     396              : 
     397              : void
     398          268 : assert_markup_text_eq (const ::selftest::location &loc,
     399              :                        const markup::node &node,
     400              :                        const markup::lowering_options &opts,
     401              :                        const char *expected_str)
     402              : {
     403          268 :   auto lowered = node.lower (opts);
     404          268 :   pretty_printer pp;
     405          268 :   lowered->print_to_pp (&pp);
     406          268 :   ASSERT_STREQ_AT (loc, pp_formatted_text (&pp), expected_str);
     407          268 : }
     408              : 
     409              : /* Verify that dumping markup::node hierarchies works as expected.  */
     410              : 
     411              : static void
     412            4 : test_dumping ()
     413              : {
     414            4 :   using namespace diagnostics::markup;
     415              : 
     416              :   /* "a + b + (c * d)".  */
     417            4 :   auto sum = std::make_unique<math::sum> ();
     418            4 :   sum->add_child ("a");
     419            4 :   sum->add_child ("b");
     420            4 :   {
     421            4 :     auto nested = std::make_unique<math::product> ();
     422            4 :     nested->add_child ("c");
     423            4 :     nested->add_child ("d");
     424            4 :     sum->add_child (std::move (nested));
     425            4 :   }
     426              : 
     427            4 :   pretty_printer pp;
     428            4 :   text_art::ascii_theme theme;
     429            4 :   dump_node_tree_to_pp (*sum, &theme, &pp);
     430            4 :   ASSERT_STREQ
     431              :     (pp_formatted_text (&pp),
     432              :      ("math::sum\n"
     433              :       "+- text: 'a'\n"
     434              :       "+- text: 'b'\n"
     435              :       "`- math::product\n"
     436              :       "   +- text: 'c'\n"
     437              :       "   `- text: 'd'\n"));
     438            4 : }
     439              : 
     440              : static void
     441            4 : test_math_nodes ()
     442              : {
     443            4 :   using namespace diagnostics::markup;
     444              : 
     445            4 :   {
     446            4 :     auto negation = std::make_unique<math::negation> (make_text_node ("a"));
     447            4 :     ASSERT_MARKUP_TEXT_EQ
     448              :       (*negation,
     449              :        get_ascii_markup_opts (),
     450              :        "-a");
     451            4 :     ASSERT_MARKUP_TEXT_EQ
     452              :       (*negation,
     453              :        get_unicode_markup_opts (),
     454              :        MARKUP_UNICODE_SYMBOL_UNARY_NEGATION "a");
     455            4 :   }
     456              : 
     457            4 :   {
     458            4 :     auto subtraction
     459            8 :       = std::make_unique<math::subtraction> (make_text_node ("a"),
     460           12 :                                              make_text_node ("b"));
     461            4 :     ASSERT_MARKUP_TEXT_EQ
     462              :       (*subtraction,
     463              :        get_ascii_markup_opts (),
     464              :        "a - b");
     465            4 :     ASSERT_MARKUP_TEXT_EQ
     466              :       (*subtraction,
     467              :        get_unicode_markup_opts (),
     468              :        "a " MARKUP_UNICODE_SYMBOL_SUBTRACTION " b");
     469            4 :   }
     470              : 
     471            4 :   {
     472            4 :     auto sum = std::make_unique<math::sum> ();
     473            4 :     sum->add_child ("a");
     474            4 :     sum->add_child ("b");
     475            4 :     sum->add_child ("c");
     476            4 :     ASSERT_MARKUP_TEXT_EQ
     477              :       (*sum,
     478              :        get_ascii_markup_opts (),
     479              :        "a + b + c");
     480            4 :   }
     481              : 
     482            4 :   {
     483            4 :     auto product = std::make_unique<math::product> ();
     484            4 :     product->add_child ("a");
     485            4 :     product->add_child ("b");
     486            4 :     product->add_child ("c");
     487            4 :     ASSERT_MARKUP_TEXT_EQ
     488              :       (*product,
     489              :        get_ascii_markup_opts (),
     490              :        "a * b * c");
     491            4 :     ASSERT_MARKUP_TEXT_EQ
     492              :       (*product,
     493              :        get_unicode_markup_opts (),
     494              :        ("a "
     495              :         MARKUP_UNICODE_SYMBOL_MULTIPLICATION
     496              :         " b "
     497              :         MARKUP_UNICODE_SYMBOL_MULTIPLICATION
     498              :         " c"));
     499            4 :   }
     500              : 
     501            4 :   {
     502            4 :     auto half_open_interval
     503            8 :       = std::make_unique<math::half_open_interval> (make_text_node ("m"),
     504           12 :                                                     make_text_node ("n"));
     505            4 :     ASSERT_MARKUP_TEXT_EQ
     506              :       (*half_open_interval,
     507              :        get_ascii_markup_opts (),
     508              :        "[m, n)");
     509            4 :   }
     510            4 : }
     511              : 
     512              : static void
     513            4 : test_code_nodes ()
     514              : {
     515            4 :   using namespace diagnostics::markup;
     516              : 
     517            4 :   {
     518            4 :     auto element_expr
     519            8 :       = std::make_unique<code::element_expr> (make_text_node ("arr"),
     520           12 :                                               make_text_node ("idx"));
     521            4 :     ASSERT_MARKUP_TEXT_EQ
     522              :       (*element_expr,
     523              :        get_ascii_markup_opts (),
     524              :        "arr[idx]");
     525            4 :   }
     526            4 :   {
     527            4 :     auto field_expr
     528            8 :       = std::make_unique<code::field_expr> (make_text_node ("foo"),
     529           12 :                                             make_text_node ("m_bar"));
     530            4 :     ASSERT_MARKUP_TEXT_EQ
     531              :       (*field_expr,
     532              :        get_ascii_markup_opts (),
     533              :        "foo.m_bar");
     534            4 :   }
     535            4 :   {
     536            4 :     auto subtraction
     537            8 :       = std::make_unique<code::infix_binary_op> (make_text_node ("a"),
     538              :                                                  "-",
     539           12 :                                                  make_text_node ("b"));
     540            4 :     ASSERT_MARKUP_TEXT_EQ
     541              :       (*subtraction,
     542              :        get_ascii_markup_opts (),
     543              :        "a - b");
     544              :     /* Verify that a code subtraction doesn't use the
     545              :        unicode minus character.  */
     546            4 :     ASSERT_MARKUP_TEXT_EQ
     547              :       (*subtraction,
     548              :        get_unicode_markup_opts (),
     549              :        "a - b");
     550            4 :   }
     551            4 : }
     552              : 
     553              : static void
     554            4 : test_inline_code ()
     555              : {
     556            4 :   using namespace diagnostics::markup;
     557              : 
     558            4 :   auto node = std::make_unique<inline_code> ();
     559            4 :   node->add_child ("sizeof (foo)");
     560            4 :   ASSERT_MARKUP_TEXT_EQ
     561              :     (*node,
     562              :      get_ascii_markup_opts (),
     563              :      "'sizeof (foo)'");
     564            4 : }
     565              : 
     566              : /* Verify that we add parentheses correctly when lowering
     567              :    math expressions to text.  */
     568              : 
     569              : static void
     570            4 : test_parens ()
     571              : {
     572            4 :   using namespace diagnostics::markup;
     573              : 
     574              :   /* math::sum ("a", "b") --> "a + b".  */
     575            4 :   {
     576            4 :     auto sum = std::make_unique<math::sum> ();
     577            4 :     sum->add_child ("a");
     578            4 :     sum->add_child ("b");
     579            4 :     ASSERT_MARKUP_TEXT_EQ
     580              :       (*sum,
     581              :        get_ascii_markup_opts (),
     582              :        "a + b");
     583            4 :   }
     584              : 
     585              :   /* math::sum (math::product ("2", "a"), "b") --> "(2 * a) + b".  */
     586            4 :   {
     587            4 :     auto sum = std::make_unique<math::sum> ();
     588            4 :     {
     589            4 :       auto two_times_a = std::make_unique<math::product> ();
     590            4 :       two_times_a->add_child ("2");
     591            4 :       two_times_a->add_child ("a");
     592            4 :       sum->add_child (std::move (two_times_a));
     593            4 :     }
     594            4 :     sum->add_child ("b");
     595            4 :     ASSERT_MARKUP_TEXT_EQ
     596              :       (*sum,
     597              :        get_ascii_markup_opts (),
     598              :        "(2 * a) + b");
     599            4 :     ASSERT_MARKUP_TEXT_EQ
     600              :       (*sum,
     601              :        get_unicode_markup_opts (),
     602              :        "(2 " MARKUP_UNICODE_SYMBOL_MULTIPLICATION " a) + b");
     603            4 :   }
     604              : 
     605              :   /* math::product ("2", math::sum ("a", "b")) --> "(2 * a) + b".  */
     606            4 :   {
     607            4 :     auto product = std::make_unique<math::product> ();
     608            4 :     product->add_child ("2");
     609            4 :     {
     610            4 :       auto a_plus_b = std::make_unique<math::sum> ();
     611            4 :       a_plus_b->add_child ("a");
     612            4 :       a_plus_b->add_child ("b");
     613            4 :       product->add_child (std::move (a_plus_b));
     614            4 :     }
     615            4 :     ASSERT_MARKUP_TEXT_EQ
     616              :       (*product,
     617              :        get_ascii_markup_opts (),
     618              :        "2 * (a + b)");
     619            4 :     ASSERT_MARKUP_TEXT_EQ
     620              :       (*product,
     621              :        get_unicode_markup_opts (),
     622              :        "2 " MARKUP_UNICODE_SYMBOL_MULTIPLICATION " (a + b)");
     623            4 :   }
     624            4 : }
     625              : 
     626              : /* Consider marking up this message:
     627              :    | If 'x' * 'sizeof(foo)' (64 bytes) > 'SIZE_MAX' (2**64 - 1),
     628              :    |    ^^^ ^ ^^^^^^^^^^^^^            ~  ^^^^^^^^   ^^^^^^^^^
     629              :    |     |  |     |                    |     |         |
     630              :    |     |  |     code                 math  code      math
     631              :    |     |  math
     632              :    |     code
     633              :    | then 'x * sizeof(foo)' overflows.
     634              :    |      ^^^^^^^^^^^^^^^^^
     635              :    |       |
     636              :    |       code
     637              : */
     638              : static std::unique_ptr<markup::node>
     639            4 : make_overflow_example ()
     640              : {
     641            4 :   using namespace diagnostics::markup;
     642              : 
     643            8 :   auto make_condition = [] () -> auto {
     644            8 :     auto make_lhs = [] () -> auto {
     645            4 :       auto product = std::make_unique<math::product> ();
     646            4 :       auto x = std::make_unique<inline_code> ();
     647            4 :       x->add_child ("x");
     648            4 :       product->add_child (std::move (x));
     649            4 :       product->add_child ("'sizeof(foo)' (64 bytes)"); // TODO: partially code
     650            8 :       return product;
     651            4 :     };
     652            8 :     auto make_rhs = [] () -> auto {
     653            4 :       auto term = std::make_unique<compound_node> ();
     654            4 :       term->add_child
     655            4 :         (std::make_unique<inline_code> (make_text_node ("SIZE_MAX")));
     656            4 :       term->add_child (" (2**64 - 1)"); // TODO: math
     657            4 :       return term;
     658              :     };
     659            4 :     auto cond = std::make_unique<compound_node> ();
     660            4 :     cond->add_child (make_lhs ());
     661            4 :     cond->add_child (" > ");
     662            4 :     cond->add_child (make_rhs ());
     663            4 :     return cond;
     664              :   };
     665              : 
     666            8 :   auto make_overflowing_expr = [] () -> auto {
     667            4 :     auto product = std::make_unique<code::infix_binary_op>
     668            8 :       (make_text_node ("x"),
     669              :        "*",
     670           12 :        make_text_node ("sizeof(foo)"));
     671            4 :     return std::make_unique<inline_code> (std::move (product));
     672            4 :   };
     673              : 
     674            4 :   auto msg = std::make_unique<compound_node> ();
     675            4 :   msg->add_child ("If ");
     676            4 :   msg->add_child (make_condition ());
     677            4 :   msg->add_child (", then ");
     678            4 :   msg->add_child (make_overflowing_expr ());
     679            4 :   msg->add_child (" overflows.");
     680            4 :   return msg;
     681            4 : }
     682              : 
     683              : static void
     684            4 : test_overflow_example ()
     685              : {
     686            4 :   auto markup = make_overflow_example ();
     687              : 
     688            4 :   ASSERT_MARKUP_TEXT_EQ
     689              :     (*markup, get_ascii_markup_opts (),
     690              :      ("If 'x' * 'sizeof(foo)' (64 bytes) > 'SIZE_MAX' (2**64 - 1),"
     691              :       " then 'x * sizeof(foo)' overflows."));
     692              : 
     693            4 :   ASSERT_MARKUP_TEXT_EQ
     694              :     (*markup, get_unicode_markup_opts (),
     695              :      ("If "
     696              :       "'x' " MARKUP_UNICODE_SYMBOL_MULTIPLICATION " 'sizeof(foo)' (64 bytes)"
     697              :       " > 'SIZE_MAX' (2**64 - 1),"
     698              :       " then 'x * sizeof(foo)' overflows."));
     699              : 
     700              :   // TODO: other formats
     701            4 : }
     702              : 
     703              : /* Run all of the selftests within this file.  */
     704              : 
     705              : void
     706            4 : markup_cc_tests ()
     707              : {
     708            4 :   test_dumping ();
     709            4 :   test_math_nodes ();
     710            4 :   test_code_nodes ();
     711            4 :   test_inline_code ();
     712            4 :   test_parens ();
     713            4 :   test_overflow_example ();
     714            4 : }
     715              : 
     716              : markup::lowering_options
     717           84 : get_ascii_markup_opts ()
     718              : {
     719           84 :   return markup::lowering_options ();
     720              : }
     721              : 
     722              : markup::lowering_options
     723           56 : get_unicode_markup_opts ()
     724              : {
     725           56 :   markup::lowering_options opts;
     726           56 :   opts.m_use_unicode_symbols = true;
     727           56 :   return opts;
     728              : }
     729              : 
     730              : } // namespace selftest
     731              : 
     732              : #endif /* CHECKING_P */
     733              : 
     734              : } // namespace diagnostics
        

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.