LCOV - code coverage report
Current view: top level - gcc/diagnostics - markup.h (source / functions) Coverage Total Hit
Test: gcc.info Lines: 74.7 % 99 74
Test Date: 2026-11-14 16:39:50 Functions: 53.7 % 41 22
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              : #ifndef GCC_DIAGNOSTICS_MARKUP_H
      22              : #define GCC_DIAGNOSTICS_MARKUP_H
      23              : 
      24              : #include "selftest.h"
      25              : #include "text-art/tree-widget.h"
      26              : 
      27              : namespace diagnostics {
      28              : namespace markup {
      29              : 
      30              : struct lowering_options;
      31              : 
      32              : /* Indentation shows inheritance hierarchy.  */
      33              : class node;
      34              :   class text;
      35              :   class unary_node;
      36              :     namespace math { class negation; }
      37              :   class binary_node;
      38              :     namespace math { class subtraction; }
      39              :     namespace code { class element_expr; }
      40              :     namespace code { class field_expr; }
      41              :     namespace code { class infix_binary_op; }
      42              :   class compound_node;
      43              :     namespace math { class associative_op; }
      44              :       namespace math { class sum; }
      45              :       namespace math { class product; }
      46              :     class inline_code;
      47              : 
      48              : enum class kind {
      49              :   text,
      50              :   compound,
      51              : 
      52              :   math_half_open_interval,
      53              :   math_negation,
      54              :   math_product,
      55              :   math_subtraction,
      56              :   math_sum,
      57              : 
      58              :   code_element_expr,
      59              :   code_field_expr,
      60              :   code_infix_binary_op,
      61              : 
      62              :   inline_code
      63              : };
      64              : 
      65              : /* Abstract base class for representing text we might want to put
      66              :    in a diagnostic as an AST.
      67              : 
      68              :    Ideally eventually we could lower this in sink-specific ways
      69              :    e.g. text, markdown for SARIF, HTML, etc,
      70              :    but that's overkill for now.
      71              : 
      72              :    For now, all we need is enough for labels in the analyzer's access diagrams
      73              :    to work.  */
      74              : 
      75         2378 : class node
      76              : {
      77              : public:
      78              :   virtual ~node () = default;
      79              : 
      80              :   virtual const char *
      81              :   get_class_name () const = 0;
      82              : 
      83              :   virtual kind
      84              :   get_kind () const = 0;
      85              : 
      86              :   virtual void
      87              :   print_to_pp (pretty_printer *pp) const = 0;
      88              : 
      89              :   void
      90              :   dump () const;
      91              : 
      92              :   virtual std::unique_ptr<text_art::tree_widget>
      93              :   make_dump_widget (text_art::dump_widget_info &dwi) const = 0;
      94              : 
      95              :   virtual const text *
      96            0 :   dyn_cast_text () const { return nullptr; }
      97              : 
      98              :   virtual const compound_node *
      99            0 :   dyn_cast_compound_node () const { return nullptr; }
     100              : 
     101              :   virtual const math::associative_op *
     102            0 :   dyn_cast_math_associative_op () const { return nullptr; }
     103              : 
     104              :   virtual math::negation *
     105          136 :   dyn_cast_math_negation () { return nullptr; }
     106              : 
     107              :   virtual std::unique_ptr<node>
     108              :   lower (const lowering_options &opts) const = 0;
     109              : };
     110              : 
     111              : /* Concrete node subclass for a fully-lowered text string.  */
     112              : 
     113              : class text : public node
     114              : {
     115              : public:
     116         1702 :   text (std::string str) : m_str (std::move (str))
     117              :   {
     118              :   }
     119              : 
     120              :   const char *
     121            0 :   get_class_name () const final override { return "text"; }
     122              : 
     123              :   kind
     124          452 :   get_kind () const final override { return kind::text; }
     125              : 
     126              :   void
     127              :   print_to_pp (pretty_printer *pp) const final override;
     128              : 
     129              :   std::unique_ptr<text_art::tree_widget>
     130              :   make_dump_widget (text_art::dump_widget_info &dwi) const final override;
     131              : 
     132              :   const text *
     133            0 :   dyn_cast_text () const final override { return this; }
     134              : 
     135              :   std::unique_ptr<node>
     136              :   lower (const lowering_options &opts) const final override;
     137              : 
     138              :   const std::string &
     139              :   get_string () const { return m_str; }
     140              : 
     141              : private:
     142              :   std::string m_str;
     143              : };
     144              : 
     145              : 
     146              : /* Abstract subclass for nodes that have a single child.  */
     147              : 
     148              : class unary_node : public node
     149              : {
     150              : public:
     151              :   void
     152              :   print_to_pp (pretty_printer *pp) const final override;
     153              : 
     154              :   std::unique_ptr<text_art::tree_widget>
     155              :   make_dump_widget (text_art::dump_widget_info &dwi) const final override;
     156              : 
     157              :   const node &
     158           32 :   get_operand () const
     159              :   {
     160           32 :     return *m_operand;
     161              :   }
     162              : 
     163              :   std::unique_ptr<node>
     164           36 :   take_operand ()
     165              :   {
     166           36 :     return std::move (m_operand);
     167              :   }
     168              : 
     169              : protected:
     170           64 :   unary_node (std::unique_ptr<node> operand)
     171           64 :   : m_operand (std::move (operand))
     172              :   {
     173           64 :     gcc_assert (m_operand);
     174           64 :   }
     175              : 
     176              : private:
     177              :   std::unique_ptr<node> m_operand;
     178              : };
     179              : 
     180              : /* Abstract subclass for nodes that have two children.  */
     181              : 
     182              : class binary_node : public node
     183              : {
     184              : public:
     185              :   void
     186              :   print_to_pp (pretty_printer *pp) const final override;
     187              : 
     188              :   std::unique_ptr<text_art::tree_widget>
     189              :   make_dump_widget (text_art::dump_widget_info &dwi) const final override;
     190              : 
     191              :   const node &
     192          100 :   get_operand_0 () const
     193              :   {
     194          100 :     return *m_op0;
     195              :   }
     196              : 
     197              :   const node &
     198          100 :   get_operand_1 () const
     199              :   {
     200          100 :     return *m_op1;
     201              :   }
     202              : 
     203              : protected:
     204           88 :   binary_node (std::unique_ptr<node> op0,
     205              :                std::unique_ptr<node> op1)
     206           88 :   : m_op0 (std::move (op0)),
     207           88 :     m_op1 (std::move (op1))
     208              :   {
     209              :   }
     210              : 
     211              : private:
     212              :   std::unique_ptr<node> m_op0;
     213              :   std::unique_ptr<node> m_op1;
     214              : };
     215              : 
     216              : /* Concrete subclass for nodes that have an arbitrary number of children.  */
     217              : 
     218              : class compound_node : public node
     219              : {
     220              : public:
     221          454 :   compound_node () : m_children () {}
     222              : 
     223           70 :   compound_node (std::vector<std::unique_ptr<node>> children)
     224           70 :   : m_children (std::move (children))
     225              :   {
     226              :   }
     227              : 
     228              :   const char *
     229            0 :   get_class_name () const { return "compound_node"; }
     230              : 
     231              :   kind
     232            0 :   get_kind () const override { return kind::compound; }
     233              : 
     234              :   void
     235              :   print_to_pp (pretty_printer *pp) const override;
     236              : 
     237              :   std::unique_ptr<text_art::tree_widget>
     238              :   make_dump_widget (text_art::dump_widget_info &dwi) const final override;
     239              : 
     240              :   const compound_node *
     241            0 :   dyn_cast_compound_node () const final override { return this; }
     242              : 
     243              :   std::unique_ptr<node>
     244              :   lower (const lowering_options &opts) const override;
     245              : 
     246              :   void
     247         1376 :   add_child (std::unique_ptr<node> child)
     248              :   {
     249         1376 :     gcc_assert (child);
     250         1376 :     m_children.push_back (std::move (child));
     251         1376 :   }
     252              : 
     253              :   void
     254              :   add_child (std::string str);
     255              : 
     256              :   size_t
     257          856 :   get_num_children () const { return m_children.size (); }
     258              : 
     259              :   const node &
     260          480 :   get_child (size_t idx) const { return *m_children[idx]; }
     261              : 
     262              : private:
     263              :   std::vector<std::unique_ptr<node>> m_children;
     264              : };
     265              : 
     266              : namespace math {
     267              : 
     268              : /* Abstract subclass for nodes that represent an associative sequence
     269              :    of infix binary operations.  */
     270              : 
     271              : class associative_op : public compound_node
     272              : {
     273              : public:
     274              :   const associative_op *
     275            0 :   dyn_cast_math_associative_op () const final override { return this; }
     276              : 
     277              :   std::unique_ptr<node>
     278              :   lower (const lowering_options &opts) const final override;
     279              : 
     280              :   virtual const char *
     281              :   get_symbol (bool use_unicode) const = 0;
     282              : 
     283              : protected:
     284           74 :   associative_op ()
     285           74 :   : compound_node ()
     286              :   {
     287              :   }
     288              : 
     289           70 :   associative_op (std::vector<std::unique_ptr<node>> operands)
     290           70 :   : compound_node (std::move (operands))
     291              :   {
     292           70 :   }
     293              : };
     294              : 
     295              : /* Concrete node subclass for a mathematical sum.
     296              :    The children are the addends; there can be more than 2.  */
     297              : 
     298              : class sum : public associative_op
     299              : {
     300              : public:
     301           20 :   sum () : associative_op () {}
     302              : 
     303           70 :   sum (std::vector<std::unique_ptr<node>> addends)
     304           70 :   : associative_op (std::move (addends))
     305              :   {
     306           70 :   }
     307              : 
     308              :   const char *
     309            4 :   get_class_name () const final override
     310              :   {
     311            4 :     return "math::sum";
     312              :   }
     313              : 
     314              :   kind
     315          220 :   get_kind () const final override { return kind::math_sum; }
     316              : 
     317              :   const char *
     318              :   get_symbol (bool use_unicode) const final override;
     319              : };
     320              : 
     321              : /* Concrete node subclass for a mathematical product.
     322              :    The children are the multiplicands; there can be more than 2.  */
     323              : 
     324              : class product : public associative_op
     325              : {
     326              : public:
     327           54 :   product () : associative_op () {}
     328              : 
     329              :   product (std::vector<std::unique_ptr<node>> multiplicands)
     330              :   : associative_op (std::move (multiplicands))
     331              :   {
     332              :   }
     333              : 
     334              :   const char *
     335            4 :   get_class_name () const final override
     336              :   {
     337            4 :     return "math::product";
     338              :   }
     339              : 
     340              :   kind
     341          168 :   get_kind () const final override { return kind::math_product; }
     342              : 
     343              :   const char *
     344              :   get_symbol (bool use_unicode) const final override;
     345              : };
     346              : 
     347              : /* Concrete node subclass for mathematical unary negation.  */
     348              : 
     349              : class negation : public unary_node
     350              : {
     351              : public:
     352           64 :   negation (std::unique_ptr<node> operand) : unary_node (std::move (operand)) {}
     353              : 
     354              :   negation *
     355           36 :   dyn_cast_math_negation () final override { return this; }
     356              : 
     357              :   const char *
     358            0 :   get_class_name () const final override
     359              :   {
     360            0 :     return "math::negation";
     361              :   }
     362              : 
     363              :   kind
     364           32 :   get_kind () const final override { return kind::math_negation; }
     365              : 
     366              :   std::unique_ptr<node>
     367              :   lower (const lowering_options &opts) const final override;
     368              : };
     369              : 
     370              : /* Concrete node subclass for mathematical subtraction.  */
     371              : 
     372              : class subtraction : public binary_node
     373              : {
     374              : public:
     375           40 :   subtraction (std::unique_ptr<node> op0,
     376              :                std::unique_ptr<node> op1)
     377              :   : binary_node (std::move (op0),
     378           40 :                  std::move (op1))
     379              :   {
     380           40 :   }
     381              : 
     382              :   const char *
     383            0 :   get_class_name () const { return "math::subtraction"; }
     384              : 
     385              :   kind
     386           88 :   get_kind () const final override { return kind::math_subtraction; }
     387              : 
     388              :   std::unique_ptr<node>
     389              :   lower (const lowering_options &opts) const final override;
     390              : };
     391              : 
     392              : /* Concrete node subclass for a half-open interval
     393              :    of the form [start, end).  */
     394              : 
     395              : class half_open_interval : public binary_node
     396              : {
     397              : public:
     398           12 :   half_open_interval (std::unique_ptr<node> start,
     399              :                       std::unique_ptr<node> next)
     400              :   : binary_node (std::move (start),
     401           12 :                  std::move (next))
     402              :   {
     403           12 :   }
     404              : 
     405              :   const char *
     406            0 :   get_class_name () const final override
     407              :   {
     408            0 :     return "math::half_open_interval";
     409              :   }
     410              : 
     411              :   kind
     412           24 :   get_kind () const final override { return kind::math_half_open_interval; }
     413              : 
     414              :   std::unique_ptr<node>
     415              :   lower (const lowering_options &opts) const final override;
     416              : };
     417              : 
     418              : } // namespace diagnostics::markup::math
     419              : 
     420              : namespace code {
     421              : 
     422              : class element_expr : public binary_node
     423              : {
     424              : public:
     425            8 :   element_expr (std::unique_ptr<node> array_node,
     426              :                 std::unique_ptr<node> index_node)
     427            8 :   : binary_node (std::move (array_node), std::move (index_node))
     428              :   {
     429            8 :   }
     430              : 
     431              :   const char *
     432            0 :   get_class_name () const final override
     433              :   {
     434            0 :     return "code::element_expr";
     435              :   }
     436              : 
     437              :   kind
     438            0 :   get_kind () const final override { return kind::code_element_expr; }
     439              : 
     440              :   std::unique_ptr<node>
     441              :   lower (const lowering_options &opts) const final override;
     442              : };
     443              : 
     444              : class field_expr : public binary_node
     445              : {
     446              : public:
     447           16 :   field_expr (std::unique_ptr<node> struct_or_union_node,
     448              :               std::unique_ptr<node> field_node)
     449           16 :   : binary_node (std::move (struct_or_union_node), std::move (field_node))
     450              :   {
     451           16 :   }
     452              : 
     453              :   const char *
     454            0 :   get_class_name () const final override
     455              :   {
     456            0 :     return "code::field_expr";
     457              :   }
     458              : 
     459              :   kind
     460            0 :   get_kind () const final override { return kind::code_field_expr; }
     461              : 
     462              :   std::unique_ptr<node>
     463              :   lower (const lowering_options &opts) const final override;
     464              : };
     465              : 
     466              : class infix_binary_op : public binary_node
     467              : {
     468              : public:
     469           12 :   infix_binary_op (std::unique_ptr<node> op0,
     470              :                    const char *symbol,
     471              :                    std::unique_ptr<node> op1)
     472              :   : binary_node (std::move (op0), std::move (op1)),
     473           12 :     m_symbol (symbol)
     474              :   {
     475           12 :   }
     476              : 
     477              :   const char *
     478            0 :   get_class_name () const final override
     479              :   {
     480            0 :     return "code::infix_binary_op";
     481              :   }
     482              : 
     483              :   kind
     484            0 :   get_kind () const final override { return kind::code_infix_binary_op; }
     485              : 
     486              :   std::unique_ptr<node>
     487              :   lower (const lowering_options &opts) const final override;
     488              : 
     489              : private:
     490              :   const char *m_symbol;
     491              : };
     492              : 
     493              : } // namespace diagnostics::markup::code
     494              : 
     495              : /* Concrete subclass for a run of inline code within text.
     496              :    Adds quotes around its children when printed.  */
     497              : 
     498              : class inline_code : public compound_node
     499              : {
     500              : public:
     501           36 :   inline_code () {}
     502              : 
     503            8 :   inline_code (std::unique_ptr<node> child)
     504            8 :   : compound_node {}
     505              :   {
     506            8 :     add_child (std::move (child));
     507            8 :   }
     508              : 
     509              :   const char *
     510            0 :   get_class_name () const final override
     511              :   {
     512            0 :     return "inline_code";
     513              :   }
     514              : 
     515              :   kind
     516            8 :   get_kind () const final override { return kind::inline_code; }
     517              : 
     518              :   void
     519              :   print_to_pp (pretty_printer *pp) const override;
     520              : 
     521              :   std::unique_ptr<node>
     522              :   lower (const lowering_options &opts) const final override;
     523              : };
     524              : 
     525              : /* Helpers for making nodes.  */
     526              : 
     527              : inline std::unique_ptr<text>
     528          465 : make_text_node (std::string str)
     529              : {
     530          469 :   return std::make_unique<text> (std::move (str));
     531              : }
     532              : 
     533              : /* Lowering.  */
     534              : 
     535              : struct lowering_options
     536              : {
     537          410 :   lowering_options ()
     538          270 :   : m_use_unicode_symbols (false)
     539              :   {
     540              :   }
     541              : 
     542              :   bool m_use_unicode_symbols;
     543              : };
     544              : 
     545              : } // namespace diagnostics::markup
     546              : 
     547              : #if CHECKING_P
     548              : 
     549              : namespace selftest {
     550              : 
     551              : /* Implementation detail of ASSERT_SYMBOL_MARKUP_TEXT_EQ.  */
     552              : 
     553              : void
     554              : assert_markup_text_eq (const ::selftest::location &loc,
     555              :                        const markup::node &,
     556              :                        const markup::lowering_options &,
     557              :                        const char *expected_str);
     558              : 
     559              : /* Assert that MARKUP_NODE lowered to plain text is EXPECTED_STR.  */
     560              : 
     561              : #define ASSERT_MARKUP_TEXT_EQ_AT(LOC, MARKUP_NODE, LOWERING_OPTS, EXPECTED_STR) \
     562              :   SELFTEST_BEGIN_STMT                                                   \
     563              :   ::diagnostics::selftest::assert_markup_text_eq                        \
     564              :     (LOC, (MARKUP_NODE),                                                \
     565              :      (LOWERING_OPTS), (EXPECTED_STR));                                  \
     566              :   SELFTEST_END_STMT
     567              : 
     568              : #define ASSERT_MARKUP_TEXT_EQ(MARKUP_NODE, LOWERING_OPTS, EXPECTED_STR) \
     569              :   SELFTEST_BEGIN_STMT                                                   \
     570              :   ::diagnostics::selftest::assert_markup_text_eq                        \
     571              :     (SELFTEST_LOCATION, (MARKUP_NODE),                                  \
     572              :      (LOWERING_OPTS), (EXPECTED_STR));                                  \
     573              :   SELFTEST_END_STMT
     574              : 
     575              : extern markup::lowering_options
     576              : get_ascii_markup_opts ();
     577              : 
     578              : extern markup::lowering_options
     579              : get_unicode_markup_opts ();
     580              : 
     581              : } // namespace diagnostics::selftest
     582              : 
     583              : #endif /* #if CHECKING_P */
     584              : 
     585              : } // namespace diagnostics
     586              : 
     587              : /* Various string literals for unicode symbols.  */
     588              : 
     589              : /* "−": U+2212: MINUS SIGN. */
     590              : #define MARKUP_UNICODE_SYMBOL_UNARY_NEGATION "\u2212"
     591              : #define MARKUP_UNICODE_SYMBOL_SUBTRACTION "\u2212"
     592              : 
     593              : /* "×: U+00D7 MULTIPLICATION SIGN. */
     594              : #define MARKUP_UNICODE_SYMBOL_MULTIPLICATION "\u00D7"
     595              : 
     596              : #endif /* GCC_DIAGNOSTICS_MARKUP_H */
        

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.