LCOV - code coverage report
Current view: top level - gcc/diagnostics - lazy-paths.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 95.7 % 92 88
Test Date: 2026-05-11 19:44:49 Functions: 85.7 % 14 12
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Helper class for deferring path creation until a diagnostic is emitted.
       2              :    Copyright (C) 2019-2026 Free Software Foundation, Inc.
       3              :    Contributed by David Malcolm <dmalcolm@redhat.com>
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify it under
       8              : the terms of the GNU General Public License as published by the Free
       9              : Software Foundation; either version 3, or (at your option) any later
      10              : version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      13              : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      14              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      15              : 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              : 
      22              : #include "config.h"
      23              : #define INCLUDE_VECTOR
      24              : #include "system.h"
      25              : #include "coretypes.h"
      26              : #include "diagnostics/lazy-paths.h"
      27              : #include "selftest.h"
      28              : #include "diagnostics/selftest-context.h"
      29              : #include "diagnostics/selftest-paths.h"
      30              : #include "diagnostics/text-sink.h"
      31              : 
      32              : using namespace diagnostics::paths;
      33              : 
      34              : /* class lazy_path : public path.  */
      35              : 
      36              : /* Implementation of path vfuncs in terms of a lazily-generated
      37              :    path.  */
      38              : 
      39              : unsigned
      40         5797 : lazy_path::num_events () const
      41              : {
      42         5797 :   lazily_generate_path ();
      43         5797 :   return m_inner_path->num_events ();
      44              : }
      45              : 
      46              : const event &
      47          166 : lazy_path::get_event (int idx) const
      48              : {
      49          166 :   lazily_generate_path ();
      50          166 :   return m_inner_path->get_event (idx);
      51              : }
      52              : 
      53              : unsigned
      54            4 : lazy_path::num_threads () const
      55              : {
      56            4 :   lazily_generate_path ();
      57            4 :   return m_inner_path->num_threads ();
      58              : }
      59              : 
      60              : const thread &
      61           19 : lazy_path::get_thread (thread_id_t idx) const
      62              : {
      63           19 :   lazily_generate_path ();
      64           19 :   return m_inner_path->get_thread (idx);
      65              : }
      66              : 
      67              : bool
      68           56 : lazy_path::same_function_p (int event_idx_a,
      69              :                             int event_idx_b) const
      70              : {
      71           56 :   lazily_generate_path ();
      72           56 :   return m_inner_path->same_function_p (event_idx_a, event_idx_b);
      73              : }
      74              : 
      75              : void
      76         6042 : lazy_path::lazily_generate_path () const
      77              : {
      78         6042 :   if (!m_inner_path)
      79         5761 :     m_inner_path = make_inner_path ();
      80         6042 :   gcc_assert (m_inner_path != nullptr);
      81         6042 : }
      82              : 
      83              : #if CHECKING_P
      84              : 
      85              : namespace diagnostics {
      86              : namespace selftest {
      87              : 
      88              : using auto_fix_quotes = ::selftest::auto_fix_quotes;
      89              : 
      90              : class test_lazy_path : public lazy_path
      91              : {
      92              : public:
      93           12 :   test_lazy_path (pretty_printer &pp)
      94           12 :   : lazy_path (m_logical_loc_mgr),
      95           24 :     m_pp (pp)
      96              :   {
      97              :   }
      98            8 :   std::unique_ptr<path> make_inner_path () const final override
      99              :   {
     100            8 :     auto path
     101            8 :       = std::make_unique<paths::selftest::test_path> (m_logical_loc_mgr,
     102            8 :                                                       &m_pp);
     103            8 :     path->add_event (UNKNOWN_LOCATION, "foo", 0, "first %qs", "free");
     104            8 :     path->add_event (UNKNOWN_LOCATION, "foo", 0, "double %qs", "free");
     105            8 :     return path;
     106            8 :   }
     107              : private:
     108              :   mutable logical_locations::selftest::test_manager m_logical_loc_mgr;
     109              :   pretty_printer &m_pp;
     110              : };
     111              : 
     112              : static void
     113            4 : test_intraprocedural_path (pretty_printer *event_pp)
     114              : {
     115            4 :   test_lazy_path path (*event_pp);
     116            4 :   ASSERT_FALSE (path.generated_p ());
     117            4 :   ASSERT_EQ (path.num_events (), 2);
     118            4 :   ASSERT_TRUE (path.generated_p ());
     119            4 :   ASSERT_EQ (path.num_threads (), 1);
     120            4 :   ASSERT_FALSE (path.interprocedural_p ());
     121            4 :   ASSERT_STREQ (path.get_event (0).get_desc (*event_pp).get (),
     122              :                 "first `free'");
     123            4 :   ASSERT_STREQ (path.get_event (1).get_desc (*event_pp).get (),
     124              :                 "double `free'");
     125            4 : }
     126              : 
     127              : /* Implementation of diagnostics::option_id_manager for which all
     128              :    options are disabled, for use in selftests.
     129              :    Note that this is *not* called for option_id (0), which
     130              :    means "always warn"  */
     131              : 
     132            4 : class all_warnings_disabled : public diagnostics::option_id_manager
     133              : {
     134              : public:
     135            4 :   int option_enabled_p (diagnostics::option_id) const final override
     136              :   {
     137              :     /* Treat all options as disabled.  */
     138            4 :     return 0;
     139              :   }
     140              : 
     141              :   label_text
     142            0 :   get_option_name (diagnostics::option_id,
     143              :                    enum kind,
     144              :                    enum kind) const final override
     145              :   {
     146            0 :     return label_text ();
     147              :   }
     148              : 
     149              :   label_text
     150            0 :   get_option_url (diagnostics::option_id) const final override
     151              :   {
     152            0 :     return label_text ();
     153              :   }
     154              : };
     155              : 
     156              : static void
     157            4 : test_emission (pretty_printer *event_pp)
     158              : {
     159            8 :   struct test_rich_location : public rich_location
     160              :   {
     161            8 :     test_rich_location (pretty_printer &event_pp)
     162            8 :     : rich_location (line_table, UNKNOWN_LOCATION),
     163            8 :       m_path (event_pp)
     164              :     {
     165            8 :       set_path (&m_path);
     166            8 :     }
     167              :     test_lazy_path m_path;
     168              :   };
     169              : 
     170              :   /* Verify that we don't bother generating the inner path if the warning
     171              :      is skipped.  */
     172            4 :   {
     173            4 :     test_context dc;
     174            4 :     dc.set_option_id_manager (std::make_unique<all_warnings_disabled> (), 0);
     175              : 
     176            4 :     test_rich_location rich_loc (*event_pp);
     177            4 :     ASSERT_FALSE (rich_loc.m_path.generated_p ());
     178              : 
     179            4 :     diagnostics::option_id opt_id (42); // has to be non-zero
     180            4 :     bool emitted
     181            4 :       = dc.emit_diagnostic_with_group (kind::warning, rich_loc, nullptr,
     182              :                                        opt_id,
     183              :                                        "this warning should be skipped");
     184            4 :     ASSERT_FALSE (emitted);
     185            4 :     ASSERT_FALSE (rich_loc.m_path.generated_p ());
     186            4 :   }
     187              : 
     188              :   /* Verify that we *do* generate the inner path for a diagnostic that
     189              :      is emitted, such as an error.  */
     190            4 :   {
     191            4 :     test_context dc;
     192              : 
     193            4 :     test_rich_location rich_loc (*event_pp);
     194            4 :     ASSERT_FALSE (rich_loc.m_path.generated_p ());
     195              : 
     196            4 :     bool emitted
     197            4 :       = dc.emit_diagnostic_with_group (kind::error, rich_loc, nullptr, 0,
     198              :                                        "this is a test");
     199            4 :     ASSERT_TRUE (emitted);
     200            4 :     ASSERT_TRUE (rich_loc.m_path.generated_p ());
     201              : 
     202              :     /* Verify that the path works as expected.  */
     203            4 :     dc.set_path_format (DPF_INLINE_EVENTS);
     204            4 :     diagnostics::text_sink sink_ (dc);
     205            4 :     pp_buffer (sink_.get_printer ())->m_flush_p = false;
     206            4 :     sink_.print_path (rich_loc.m_path);
     207            4 :     ASSERT_STREQ (pp_formatted_text (sink_.get_printer ()),
     208              :                   "  `foo': event 1\n"
     209              :                   " (1): first `free'\n"
     210              :                   "  `foo': event 2\n"
     211              :                   " (2): double `free'\n");
     212            8 :   }
     213            4 : }
     214              : 
     215              : /* Run all of the selftests within this file.  */
     216              : 
     217              : void
     218            4 : lazy_paths_cc_tests ()
     219              : {
     220              :   /* In a few places we use the global dc's printer to determine
     221              :      colorization so ensure this off during the tests.  */
     222            4 :   pretty_printer *global_pp = global_dc->get_reference_printer ();
     223            4 :   const bool saved_show_color = pp_show_color (global_pp);
     224            4 :   pp_show_color (global_pp) = false;
     225              : 
     226            4 :   auto_fix_quotes fix_quotes;
     227            4 :   std::unique_ptr<pretty_printer> event_pp
     228            4 :     = std::unique_ptr<pretty_printer> (global_pp->clone ());
     229              : 
     230            4 :   test_intraprocedural_path (event_pp.get ());
     231            4 :   test_emission (event_pp.get ());
     232              : 
     233            4 :   pp_show_color (global_pp) = saved_show_color;
     234            4 : }
     235              : 
     236              : } // namespace diagnostics::selftest
     237              : } // namespace diagnostics
     238              : 
     239              : #endif /* #if CHECKING_P */
        

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