| 1 |
/* Selftest support for diagnostics. |
| 2 |
Copyright (C) 2016-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GCC. |
| 5 |
|
| 6 |
GCC is free software; you can redistribute it and/or modify it under |
| 7 |
the terms of the GNU General Public License as published by the Free |
| 8 |
Software Foundation; either version 3, or (at your option) any later |
| 9 |
version. |
| 10 |
|
| 11 |
GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 |
for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with GCC; see the file COPYING3. If not see |
| 18 |
<http://www.gnu.org/licenses/>. */ |
| 19 |
|
| 20 |
#include "config.h" |
| 21 |
#include "system.h" |
| 22 |
#include "coretypes.h" |
| 23 |
#include "diagnostic.h" |
| 24 |
#include "diagnostic-format.h" |
| 25 |
#include "selftest.h" |
| 26 |
#include "selftest-diagnostic.h" |
| 27 |
|
| 28 |
/* The selftest code should entirely disappear in a production |
| 29 |
configuration, hence we guard all of it with #if CHECKING_P. */ |
| 30 |
|
| 31 |
#if CHECKING_P |
| 32 |
|
| 33 |
namespace selftest { |
| 34 |
|
| 35 |
/* Implementation of class selftest::test_diagnostic_context. */ |
| 36 |
|
| 37 |
test_diagnostic_context::test_diagnostic_context () |
| 38 |
{ |
| 39 |
diagnostic_initialize (this, 0); |
| 40 |
pp_show_color (get_reference_printer ()) = false; |
| 41 |
m_source_printing.enabled = true; |
| 42 |
m_source_printing.show_labels_p = true; |
| 43 |
m_show_column = true; |
| 44 |
diagnostic_start_span (this) = start_span_cb; |
| 45 |
m_source_printing.min_margin_width = 6; |
| 46 |
m_source_printing.max_width = 80; |
| 47 |
pp_buffer (get_output_format (0).get_printer ())->m_flush_p = false; |
| 48 |
} |
| 49 |
|
| 50 |
test_diagnostic_context::~test_diagnostic_context () |
| 51 |
{ |
| 52 |
diagnostic_finish (this); |
| 53 |
} |
| 54 |
|
| 55 |
/* Implementation of diagnostic_start_span_fn, hiding the |
| 56 |
real filename (to avoid printing the names of tempfiles). */ |
| 57 |
|
| 58 |
void |
| 59 |
test_diagnostic_context:: |
| 60 |
start_span_cb (const diagnostic_location_print_policy &loc_policy, |
| 61 |
pretty_printer *pp, |
| 62 |
expanded_location exploc) |
| 63 |
{ |
| 64 |
exploc.file = "FILENAME"; |
| 65 |
default_diagnostic_start_span_fn (loc_policy, pp, exploc); |
| 66 |
} |
| 67 |
|
| 68 |
bool |
| 69 |
test_diagnostic_context::report (diagnostic_t kind, |
| 70 |
rich_location &richloc, |
| 71 |
const diagnostic_metadata *metadata, |
| 72 |
int option, |
| 73 |
const char * fmt, ...) |
| 74 |
{ |
| 75 |
va_list ap; |
| 76 |
va_start (ap, fmt); |
| 77 |
begin_group (); |
| 78 |
bool result = diagnostic_impl (&richloc, metadata, option, fmt, &ap, kind); |
| 79 |
end_group (); |
| 80 |
va_end (ap); |
| 81 |
return result; |
| 82 |
} |
| 83 |
|
| 84 |
/* Print RICHLOC's source and annotations to this context's m_printer. |
| 85 |
Return the text buffer from the printer. */ |
| 86 |
|
| 87 |
const char * |
| 88 |
test_diagnostic_context::test_show_locus (rich_location &richloc) |
| 89 |
{ |
| 90 |
pretty_printer *pp = get_reference_printer (); |
| 91 |
gcc_assert (pp); |
| 92 |
diagnostic_source_print_policy source_policy (*this); |
| 93 |
source_policy.print (*pp, richloc, DK_ERROR, nullptr); |
| 94 |
return pp_formatted_text (pp); |
| 95 |
} |
| 96 |
|
| 97 |
} // namespace selftest |
| 98 |
|
| 99 |
#endif /* #if CHECKING_P */ |