aboutsummaryrefslogtreecommitdiff
path: root/gcc/diagnostic.cc
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2023-11-14 11:01:40 -0500
committerDavid Malcolm <dmalcolm@redhat.com>2023-11-14 11:01:40 -0500
commit1bdd665a025a74f4f1b641600a8c0bbca3355aa7 (patch)
tree5954da6c5e393727cbf262d6682cc60a0a873fc1 /gcc/diagnostic.cc
parent070944fdd6394c992885f33f5bde8a231d37fa7f (diff)
downloadgcc-1bdd665a025a74f4f1b641600a8c0bbca3355aa7.zip
gcc-1bdd665a025a74f4f1b641600a8c0bbca3355aa7.tar.gz
gcc-1bdd665a025a74f4f1b641600a8c0bbca3355aa7.tar.bz2
input.h: eliminate implicit users of global_dc's file_cache
This patch eliminates the following functions that implicitly used global_dc's file cache: extern char_span location_get_source_line (const char *file_path, int line); extern char_span get_source_file_content (const char *file_path); extern bool location_missing_trailing_newline (const char *file_path); in favor of explicitly using a specific file_cache throughout, and only using global_dc's file_cache in gcc-specific code. Rather than creating global_dc's file_cache the first time its needed, this patch simply creates one when a diagnostic_context is initialized, and eliminates diagnostic_file_cache_init. No functional change intended. gcc/c-family/ChangeLog: * c-common.cc (c_get_substring_location): Use global_dc's file_cache. * c-format.cc (get_corrected_substring): Likewise. * c-indentation.cc (get_visual_column): Add file_cache param. (get_first_nws_vis_column): Likewise. (detect_intervening_unindent): Likewise. (should_warn_for_misleading_indentation): Use global_dc's file_cache. (assert_get_visual_column_succeeds): Add file_cache param. (ASSERT_GET_VISUAL_COLUMN_SUCCEEDS): Likewise. (assert_get_visual_column_fails): Likewise. (define ASSERT_GET_VISUAL_COLUMN_FAILS): Likewise. (selftest::test_get_visual_column): Create and use a temporary file_cache. gcc/cp/ChangeLog: * contracts.cc (build_comment): Use global_dc's file_cache. gcc/ChangeLog: * diagnostic-format-sarif.cc (sarif_builder::get_sarif_column): Use m_context's file_cache. (sarif_builder::maybe_make_artifact_content_object): Likewise. (sarif_builder::get_source_lines): Likewise. * diagnostic-show-locus.cc (exploc_with_display_col::exploc_with_display_col): Add file_cache param. (layout::m_file_cache): New field. (make_range): Add file_cache param. (selftest::test_layout_range_for_single_point): Create and use a temporary file_cache. (selftest::test_layout_range_for_single_line): Likewise. (selftest::test_layout_range_for_multiple_lines): Likewise. (layout::layout): Initialize m_file_cache from the context and use it. (layout::maybe_add_location_range): Use m_file_cache. (layout::calculate_x_offset_display): Likewise. (get_affected_range): Add file_cache param. (get_printed_columns): Likewise. (line_corrections::line_corrections): Likewwise. (line_corrections::m_file_cache): New field. (source_line::source_line): Add file_cache param. (line_corrections::add_hint): Use m_file_cache. (layout::print_trailing_fixits): Likewise. (layout::print_line): Likewise. (selftest::test_layout_x_offset_display_utf8): Create and use a temporary file_cache. (selftest::test_layout_x_offset_display_tab): Likewise. (selftest::test_diagnostic_show_locus_one_liner_utf8): Likewise. (selftest::test_add_location_if_nearby): Pass global_dc's file_cache to temp_source_file ctor. (selftest::test_overlapped_fixit_printing): Create and use a temporary file_cache. (selftest::test_overlapped_fixit_printing_utf8): Likewise. (selftest::test_overlapped_fixit_printing_2): Use dc's file_cache. * diagnostic.cc (diagnostic_context::initialize): Always create a file_cache. (diagnostic_context::initialize_input_context): Assume m_file_cache has already been created. (diagnostic_context::create_edit_context): Pass m_file_cache to edit_context. (convert_column_unit): Add file_cache param. (diagnostic_context::converted_column): Use context's file_cache. (print_parseable_fixits): Add file_cache param. (diagnostic_context::report_diagnostic): Use context's file_cache. (selftest::test_print_parseable_fixits_none): Create and use a temporary file_cache. (selftest::test_print_parseable_fixits_insert): Likewise. (selftest::test_print_parseable_fixits_remove): Likewise. (selftest::test_print_parseable_fixits_replace): Likewise. (selftest::test_print_parseable_fixits_bytes_vs_display_columns): Likewise. * diagnostic.h (diagnostic_context::file_cache_init): Delete. (diagnostic_context::get_file_cache): Convert return type from pointer to reference. * edit-context.cc (edited_file::get_file_cache): New. (edited_file::m_edit_context): New. (edit_context::edit_context): Add file_cache param. (edit_context::get_or_insert_file): Pass this to edited_file's ctor. (edited_file::edited_file): Add edit_context param. (edited_file::print_content): Use get_file_cache. (edited_file::print_diff_hunk): Likewise. (edited_file::print_run_of_changed_lines): Likewise. (edited_file::get_or_insert_line): Likewise. (edited_file::get_num_lines): Likewise. (edited_line::edited_line): Pass in file_cache and use it. (selftest::test_get_content): Create and use a temporary file_cache. (selftest::test_applying_fixits_insert_before): Likewise. (selftest::test_applying_fixits_insert_after): Likewise. (selftest::test_applying_fixits_insert_after_at_line_end): Likewise. (selftest::test_applying_fixits_insert_after_failure): Likewise. (selftest::test_applying_fixits_insert_containing_newline): Likewise. (selftest::test_applying_fixits_growing_replace): Likewise. (selftest::test_applying_fixits_shrinking_replace): Likewise. (selftest::test_applying_fixits_replace_containing_newline): Likewise. (selftest::test_applying_fixits_remove): Likewise. (selftest::test_applying_fixits_multiple): Likewise. (selftest::test_applying_fixits_multiple_lines): Likewise. (selftest::test_applying_fixits_modernize_named_init): Likewise. (selftest::test_applying_fixits_modernize_named_init): Likewise. (selftest::test_applying_fixits_unreadable_file): Likewise. (selftest::test_applying_fixits_line_out_of_range): Likewise. (selftest::test_applying_fixits_column_validation): Likewise. (selftest::test_applying_fixits_column_validation): Likewise. (selftest::test_applying_fixits_column_validation): Likewise. (selftest::test_applying_fixits_column_validation): Likewise. * edit-context.h (edit_context::edit_context): Add file_cache param. (edit_context::get_file_cache): New. (edit_context::m_file_cache): New. * final.cc: Include "diagnostic.h". (asm_show_source): Use global_dc's file_cache. * gcc-rich-location.cc (blank_line_before_p): Add file_cache param. (use_new_line): Likewise. (gcc_rich_location::add_fixit_insert_formatted): Use global dc's file_cache. * input.cc (diagnostic_file_cache_init): Delete. (diagnostic_context::file_cache_init): Delete. (diagnostics_file_cache_forcibly_evict_file): Delete. (file_cache::missing_trailing_newline_p): New. (file_cache::evicted_cache_tab_entry): Don't call diagnostic_file_cache_init. (location_get_source_line): Delete. (get_source_text_between): Add file_cache param. (get_source_file_content): Delete. (location_missing_trailing_newline): Delete. (location_compute_display_column): Add file_cache param. (dump_location_info): Create and use temporary file_cache. (get_substring_ranges_for_loc): Add file_cache param. (get_location_within_string): Likewise. (get_source_range_for_char): Likewise. (get_num_source_ranges_for_substring): Likewise. (selftest::test_reading_source_line): Create and use temporary file_cache. (selftest::lexer_test::m_file_cache): New field. (selftest::assert_char_at_range): Use test.m_file_cache. (selftest::assert_num_substring_ranges): Likewise. (selftest::assert_has_no_substring_ranges): Likewise. (selftest::test_lexer_string_locations_concatenation_2): Likewise. * input.h (class file_cache): New forward decl. (location_compute_display_column): Add file_cache param. (location_get_source_line): Delete. (get_source_text_between): Add file_cache param. (get_source_file_content): Delete. (location_missing_trailing_newline): Delete. (file_cache::missing_trailing_newline_p): New decl. (diagnostics_file_cache_forcibly_evict_file): Delete. * selftest.cc (named_temp_file::named_temp_file): Add file_cache param. (named_temp_file::~named_temp_file): Optionally evict the file from the given file_cache. (temp_source_file::temp_source_file): Add file_cache param. * selftest.h (class file_cache): New forward decl. (named_temp_file::named_temp_file): Add file_cache param. (named_temp_file::m_file_cache): New field. (temp_source_file::temp_source_file): Add file_cache param. * substring-locations.h (get_location_within_string): Add file_cache param. gcc/testsuite/ChangeLog: * gcc.dg/plugin/diagnostic_plugin_test_show_locus.c: Use global_dc's file cache. * gcc.dg/plugin/expensive_selftests_plugin.c: Likewise. Signed-off-by: David Malcolm <dmalcolm@redhat.com>
Diffstat (limited to 'gcc/diagnostic.cc')
-rw-r--r--gcc/diagnostic.cc47
1 files changed, 29 insertions, 18 deletions
diff --git a/gcc/diagnostic.cc b/gcc/diagnostic.cc
index addd660..24212b9 100644
--- a/gcc/diagnostic.cc
+++ b/gcc/diagnostic.cc
@@ -211,7 +211,7 @@ diagnostic_context::initialize (int n_opts)
this->printer = XNEW (pretty_printer);
new (this->printer) pretty_printer ();
- m_file_cache = nullptr;
+ m_file_cache = new file_cache ();
memset (m_diagnostic_count, 0, sizeof m_diagnostic_count);
m_warning_as_error_requested = false;
m_n_opts = n_opts;
@@ -352,8 +352,6 @@ diagnostic_context::
initialize_input_context (diagnostic_input_charset_callback ccb,
bool should_skip_bom)
{
- if (!m_file_cache)
- m_file_cache = new file_cache;
m_file_cache->initialize_input_context (ccb, should_skip_bom);
}
@@ -432,7 +430,8 @@ void
diagnostic_context::create_edit_context ()
{
delete m_edit_context_ptr;
- m_edit_context_ptr = new edit_context ();
+ gcc_assert (m_file_cache);
+ m_edit_context_ptr = new edit_context (*m_file_cache);
}
/* Initialize DIAGNOSTIC, where the message MSG has already been
@@ -485,7 +484,8 @@ diagnostic_get_color_for_kind (diagnostic_t kind)
Return -1 if the column is invalid (<= 0). */
static int
-convert_column_unit (enum diagnostics_column_unit column_unit,
+convert_column_unit (file_cache &fc,
+ enum diagnostics_column_unit column_unit,
int tabstop,
expanded_location s)
{
@@ -500,7 +500,7 @@ convert_column_unit (enum diagnostics_column_unit column_unit,
case DIAGNOSTICS_COLUMN_UNIT_DISPLAY:
{
cpp_char_column_policy policy (tabstop, cpp_wcwidth);
- return location_compute_display_column (s, policy);
+ return location_compute_display_column (fc, s, policy);
}
case DIAGNOSTICS_COLUMN_UNIT_BYTE:
@@ -514,7 +514,8 @@ convert_column_unit (enum diagnostics_column_unit column_unit,
int
diagnostic_context::converted_column (expanded_location s) const
{
- int one_based_col = convert_column_unit (m_column_unit, m_tabstop, s);
+ int one_based_col = convert_column_unit (get_file_cache (),
+ m_column_unit, m_tabstop, s);
if (one_based_col <= 0)
return -1;
return one_based_col + (m_column_origin - 1);
@@ -1205,7 +1206,8 @@ print_escaped_string (pretty_printer *pp, const char *text)
Use TABSTOP when handling DIAGNOSTICS_COLUMN_UNIT_DISPLAY. */
static void
-print_parseable_fixits (pretty_printer *pp, rich_location *richloc,
+print_parseable_fixits (file_cache &fc,
+ pretty_printer *pp, rich_location *richloc,
enum diagnostics_column_unit column_unit,
int tabstop)
{
@@ -1226,9 +1228,9 @@ print_parseable_fixits (pretty_printer *pp, rich_location *richloc,
location_t next_loc = hint->get_next_loc ();
expanded_location next_exploc = expand_location (next_loc);
int start_col
- = convert_column_unit (column_unit, tabstop, start_exploc);
+ = convert_column_unit (fc, column_unit, tabstop, start_exploc);
int next_col
- = convert_column_unit (column_unit, tabstop, next_exploc);
+ = convert_column_unit (fc, column_unit, tabstop, next_exploc);
pp_printf (pp, ":{%i:%i-%i:%i}:",
start_exploc.line, start_col,
next_exploc.line, next_col);
@@ -1613,13 +1615,15 @@ diagnostic_context::report_diagnostic (diagnostic_info *diagnostic)
default:
break;
case EXTRA_DIAGNOSTIC_OUTPUT_fixits_v1:
- print_parseable_fixits (this->printer, diagnostic->richloc,
+ print_parseable_fixits (get_file_cache (),
+ this->printer, diagnostic->richloc,
DIAGNOSTICS_COLUMN_UNIT_BYTE,
m_tabstop);
pp_flush (this->printer);
break;
case EXTRA_DIAGNOSTIC_OUTPUT_fixits_v2:
- print_parseable_fixits (this->printer, diagnostic->richloc,
+ print_parseable_fixits (get_file_cache (),
+ this->printer, diagnostic->richloc,
DIAGNOSTICS_COLUMN_UNIT_DISPLAY,
m_tabstop);
pp_flush (this->printer);
@@ -2687,9 +2691,10 @@ static void
test_print_parseable_fixits_none ()
{
pretty_printer pp;
+ file_cache fc;
rich_location richloc (line_table, UNKNOWN_LOCATION);
- print_parseable_fixits (&pp, &richloc, DIAGNOSTICS_COLUMN_UNIT_BYTE, 8);
+ print_parseable_fixits (fc, &pp, &richloc, DIAGNOSTICS_COLUMN_UNIT_BYTE, 8);
ASSERT_STREQ ("", pp_formatted_text (&pp));
}
@@ -2700,6 +2705,7 @@ static void
test_print_parseable_fixits_insert ()
{
pretty_printer pp;
+ file_cache fc;
rich_location richloc (line_table, UNKNOWN_LOCATION);
linemap_add (line_table, LC_ENTER, false, "test.c", 0);
@@ -2708,7 +2714,7 @@ test_print_parseable_fixits_insert ()
location_t where = linemap_position_for_column (line_table, 10);
richloc.add_fixit_insert_before (where, "added content");
- print_parseable_fixits (&pp, &richloc, DIAGNOSTICS_COLUMN_UNIT_BYTE, 8);
+ print_parseable_fixits (fc, &pp, &richloc, DIAGNOSTICS_COLUMN_UNIT_BYTE, 8);
ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:10}:\"added content\"\n",
pp_formatted_text (&pp));
}
@@ -2720,6 +2726,7 @@ static void
test_print_parseable_fixits_remove ()
{
pretty_printer pp;
+ file_cache fc;
rich_location richloc (line_table, UNKNOWN_LOCATION);
linemap_add (line_table, LC_ENTER, false, "test.c", 0);
@@ -2730,7 +2737,7 @@ test_print_parseable_fixits_remove ()
where.m_finish = linemap_position_for_column (line_table, 20);
richloc.add_fixit_remove (where);
- print_parseable_fixits (&pp, &richloc, DIAGNOSTICS_COLUMN_UNIT_BYTE, 8);
+ print_parseable_fixits (fc, &pp, &richloc, DIAGNOSTICS_COLUMN_UNIT_BYTE, 8);
ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"\"\n",
pp_formatted_text (&pp));
}
@@ -2742,6 +2749,7 @@ static void
test_print_parseable_fixits_replace ()
{
pretty_printer pp;
+ file_cache fc;
rich_location richloc (line_table, UNKNOWN_LOCATION);
linemap_add (line_table, LC_ENTER, false, "test.c", 0);
@@ -2752,7 +2760,7 @@ test_print_parseable_fixits_replace ()
where.m_finish = linemap_position_for_column (line_table, 20);
richloc.add_fixit_replace (where, "replacement");
- print_parseable_fixits (&pp, &richloc, DIAGNOSTICS_COLUMN_UNIT_BYTE, 8);
+ print_parseable_fixits (fc, &pp, &richloc, DIAGNOSTICS_COLUMN_UNIT_BYTE, 8);
ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"replacement\"\n",
pp_formatted_text (&pp));
}
@@ -2772,6 +2780,7 @@ test_print_parseable_fixits_bytes_vs_display_columns ()
const int tabstop = 8;
temp_source_file tmp (SELFTEST_LOCATION, ".c", content);
+ file_cache fc;
const char *const fname = tmp.get_filename ();
linemap_add (line_table, LC_ENTER, false, fname, 0);
@@ -2792,7 +2801,8 @@ test_print_parseable_fixits_bytes_vs_display_columns ()
{
pretty_printer pp;
- print_parseable_fixits (&pp, &richloc, DIAGNOSTICS_COLUMN_UNIT_BYTE,
+ print_parseable_fixits (fc, &pp, &richloc,
+ DIAGNOSTICS_COLUMN_UNIT_BYTE,
tabstop);
snprintf (expected, buf_len,
"fix-it:%s:{1:12-1:18}:\"color\"\n", escaped_fname);
@@ -2800,7 +2810,8 @@ test_print_parseable_fixits_bytes_vs_display_columns ()
}
{
pretty_printer pp;
- print_parseable_fixits (&pp, &richloc, DIAGNOSTICS_COLUMN_UNIT_DISPLAY,
+ print_parseable_fixits (fc, &pp, &richloc,
+ DIAGNOSTICS_COLUMN_UNIT_DISPLAY,
tabstop);
snprintf (expected, buf_len,
"fix-it:%s:{1:10-1:16}:\"color\"\n", escaped_fname);