| 1 |
/* Helper class for deferring path creation until a diagnostic is emitted. |
| 2 |
Copyright (C) 2019-2025 Free Software Foundation, Inc. |
| 3 |
Contributed by David Malcolm <[email protected]> |
| 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 "tree.h" |
| 27 |
#include "version.h" |
| 28 |
#include "intl.h" |
| 29 |
#include "diagnostic.h" |
| 30 |
#include "lazy-diagnostic-path.h" |
| 31 |
#include "make-unique.h" |
| 32 |
#include "selftest.h" |
| 33 |
#include "selftest-diagnostic.h" |
| 34 |
#include "simple-diagnostic-path.h" |
| 35 |
#include "gcc-rich-location.h" |
| 36 |
#include "diagnostic-format-text.h" |
| 37 |
|
| 38 |
/* class lazy_diagnostic_path : public diagnostic_path. */ |
| 39 |
|
| 40 |
/* Implementation of diagnostic_path vfuncs in terms of a lazily-generated |
| 41 |
path. */ |
| 42 |
|
| 43 |
unsigned |
| 44 |
lazy_diagnostic_path::num_events () const |
| 45 |
{ |
| 46 |
lazily_generate_path (); |
| 47 |
return m_inner_path->num_events (); |
| 48 |
} |
| 49 |
|
| 50 |
const diagnostic_event & |
| 51 |
lazy_diagnostic_path::get_event (int idx) const |
| 52 |
{ |
| 53 |
lazily_generate_path (); |
| 54 |
return m_inner_path->get_event (idx); |
| 55 |
} |
| 56 |
|
| 57 |
unsigned |
| 58 |
lazy_diagnostic_path::num_threads () const |
| 59 |
{ |
| 60 |
lazily_generate_path (); |
| 61 |
return m_inner_path->num_threads (); |
| 62 |
} |
| 63 |
|
| 64 |
const diagnostic_thread & |
| 65 |
lazy_diagnostic_path::get_thread (diagnostic_thread_id_t idx) const |
| 66 |
{ |
| 67 |
lazily_generate_path (); |
| 68 |
return m_inner_path->get_thread (idx); |
| 69 |
} |
| 70 |
|
| 71 |
bool |
| 72 |
lazy_diagnostic_path::same_function_p (int event_idx_a, |
| 73 |
int event_idx_b) const |
| 74 |
{ |
| 75 |
lazily_generate_path (); |
| 76 |
return m_inner_path->same_function_p (event_idx_a, event_idx_b); |
| 77 |
} |
| 78 |
|
| 79 |
void |
| 80 |
lazy_diagnostic_path::lazily_generate_path () const |
| 81 |
{ |
| 82 |
if (!m_inner_path) |
| 83 |
m_inner_path = make_inner_path (); |
| 84 |
gcc_assert (m_inner_path != nullptr); |
| 85 |
} |
| 86 |
|
| 87 |
#if CHECKING_P |
| 88 |
|
| 89 |
namespace selftest { |
| 90 |
|
| 91 |
class test_lazy_path : public lazy_diagnostic_path |
| 92 |
{ |
| 93 |
public: |
| 94 |
test_lazy_path (pretty_printer &pp) |
| 95 |
: m_pp (pp) |
| 96 |
{ |
| 97 |
} |
| 98 |
std::unique_ptr<diagnostic_path> make_inner_path () const final override |
| 99 |
{ |
| 100 |
tree fntype_void_void |
| 101 |
= build_function_type_array (void_type_node, 0, NULL); |
| 102 |
tree fndecl_foo = build_fn_decl ("foo", fntype_void_void); |
| 103 |
auto path = ::make_unique<simple_diagnostic_path> (&m_pp); |
| 104 |
path->add_event (UNKNOWN_LOCATION, fndecl_foo, 0, "first %qs", "free"); |
| 105 |
path->add_event (UNKNOWN_LOCATION, fndecl_foo, 0, "double %qs", "free"); |
| 106 |
return path; |
| 107 |
} |
| 108 |
private: |
| 109 |
pretty_printer &m_pp; |
| 110 |
}; |
| 111 |
|
| 112 |
static void |
| 113 |
test_intraprocedural_path (pretty_printer *event_pp) |
| 114 |
{ |
| 115 |
test_lazy_path path (*event_pp); |
| 116 |
ASSERT_FALSE (path.generated_p ()); |
| 117 |
ASSERT_EQ (path.num_events (), 2); |
| 118 |
ASSERT_TRUE (path.generated_p ()); |
| 119 |
ASSERT_EQ (path.num_threads (), 1); |
| 120 |
ASSERT_FALSE (path.interprocedural_p ()); |
| 121 |
ASSERT_STREQ (path.get_event (0).get_desc (*event_pp).get (), |
| 122 |
"first `free'"); |
| 123 |
ASSERT_STREQ (path.get_event (1).get_desc (*event_pp).get (), |
| 124 |
"double `free'"); |
| 125 |
} |
| 126 |
|
| 127 |
/* Implementation of diagnostic_option_manager for which all |
| 128 |
options are disabled, for use in selftests. |
| 129 |
Note that this is *not* called for diagnostic_option_id (0), which |
| 130 |
means "always warn" */ |
| 131 |
|
| 132 |
class all_warnings_disabled : public diagnostic_option_manager |
| 133 |
{ |
| 134 |
public: |
| 135 |
int option_enabled_p (diagnostic_option_id) const final override |
| 136 |
{ |
| 137 |
/* Treat all options as disabled. */ |
| 138 |
return 0; |
| 139 |
} |
| 140 |
char *make_option_name (diagnostic_option_id, |
| 141 |
diagnostic_t, |
| 142 |
diagnostic_t) const final override |
| 143 |
{ |
| 144 |
return nullptr; |
| 145 |
} |
| 146 |
char *make_option_url (diagnostic_option_id) const final override |
| 147 |
{ |
| 148 |
return nullptr; |
| 149 |
} |
| 150 |
}; |
| 151 |
|
| 152 |
static void |
| 153 |
test_emission (pretty_printer *event_pp) |
| 154 |
{ |
| 155 |
struct test_rich_location : public gcc_rich_location |
| 156 |
{ |
| 157 |
test_rich_location (pretty_printer &event_pp) |
| 158 |
: gcc_rich_location (UNKNOWN_LOCATION), |
| 159 |
m_path (event_pp) |
| 160 |
{ |
| 161 |
set_path (&m_path); |
| 162 |
} |
| 163 |
test_lazy_path m_path; |
| 164 |
}; |
| 165 |
|
| 166 |
/* Verify that we don't bother generating the inner path if the warning |
| 167 |
is skipped. */ |
| 168 |
{ |
| 169 |
test_diagnostic_context dc; |
| 170 |
dc.set_option_manager (::make_unique<all_warnings_disabled> (), 0); |
| 171 |
|
| 172 |
test_rich_location rich_loc (*event_pp); |
| 173 |
ASSERT_FALSE (rich_loc.m_path.generated_p ()); |
| 174 |
|
| 175 |
diagnostic_option_id option_id (42); // has to be non-zero |
| 176 |
bool emitted |
| 177 |
= dc.emit_diagnostic_with_group (DK_WARNING, rich_loc, nullptr, |
| 178 |
option_id, |
| 179 |
"this warning should be skipped"); |
| 180 |
ASSERT_FALSE (emitted); |
| 181 |
ASSERT_FALSE (rich_loc.m_path.generated_p ()); |
| 182 |
} |
| 183 |
|
| 184 |
/* Verify that we *do* generate the inner path for a diagnostic that |
| 185 |
is emitted, such as an error. */ |
| 186 |
{ |
| 187 |
test_diagnostic_context dc; |
| 188 |
|
| 189 |
test_rich_location rich_loc (*event_pp); |
| 190 |
ASSERT_FALSE (rich_loc.m_path.generated_p ()); |
| 191 |
|
| 192 |
bool emitted |
| 193 |
= dc.emit_diagnostic_with_group (DK_ERROR, rich_loc, nullptr, 0, |
| 194 |
"this is a test"); |
| 195 |
ASSERT_TRUE (emitted); |
| 196 |
ASSERT_TRUE (rich_loc.m_path.generated_p ()); |
| 197 |
|
| 198 |
/* Verify that the path works as expected. */ |
| 199 |
dc.set_path_format (DPF_INLINE_EVENTS); |
| 200 |
diagnostic_text_output_format sink (dc); |
| 201 |
pp_buffer (sink.get_printer ())->m_flush_p = false; |
| 202 |
sink.print_path (rich_loc.m_path); |
| 203 |
ASSERT_STREQ (pp_formatted_text (sink.get_printer ()), |
| 204 |
" `foo': event 1\n" |
| 205 |
" (1): first `free'\n" |
| 206 |
" `foo': event 2\n" |
| 207 |
" (2): double `free'\n"); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
/* Run all of the selftests within this file. */ |
| 212 |
|
| 213 |
void |
| 214 |
lazy_diagnostic_path_cc_tests () |
| 215 |
{ |
| 216 |
/* In a few places we use the global dc's printer to determine |
| 217 |
colorization so ensure this off during the tests. */ |
| 218 |
pretty_printer *global_pp = global_dc->get_reference_printer (); |
| 219 |
const bool saved_show_color = pp_show_color (global_pp); |
| 220 |
pp_show_color (global_pp) = false; |
| 221 |
|
| 222 |
auto_fix_quotes fix_quotes; |
| 223 |
std::unique_ptr<pretty_printer> event_pp |
| 224 |
= std::unique_ptr<pretty_printer> (global_pp->clone ()); |
| 225 |
|
| 226 |
test_intraprocedural_path (event_pp.get ()); |
| 227 |
test_emission (event_pp.get ()); |
| 228 |
|
| 229 |
pp_show_color (global_pp) = saved_show_color; |
| 230 |
} |
| 231 |
|
| 232 |
} // namespace selftest |
| 233 |
|
| 234 |
#endif /* #if CHECKING_P */ |