aboutsummaryrefslogtreecommitdiff
path: root/gcc/edit-context.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/edit-context.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/edit-context.cc')
-rw-r--r--gcc/edit-context.cc108
1 files changed, 71 insertions, 37 deletions
diff --git a/gcc/edit-context.cc b/gcc/edit-context.cc
index 09b000c..ee28bf6 100644
--- a/gcc/edit-context.cc
+++ b/gcc/edit-context.cc
@@ -64,7 +64,7 @@ public:
class edited_file
{
public:
- edited_file (const char *filename);
+ edited_file (edit_context &ec, const char *filename);
static void delete_cb (edited_file *file);
const char *get_filename () const { return m_filename; }
@@ -84,6 +84,11 @@ class edited_file
return 0;
}
+ file_cache &get_file_cache () const
+ {
+ return m_edit_context.get_file_cache ();
+ }
+
private:
bool print_content (pretty_printer *pp);
void print_diff (pretty_printer *pp, bool show_filenames);
@@ -100,6 +105,7 @@ class edited_file
int start_of_run,
int end_of_run);
+ edit_context &m_edit_context;
const char *m_filename;
typed_splay_tree<int, edited_line *> m_edited_lines;
int m_num_lines;
@@ -163,7 +169,7 @@ class line_event
class edited_line
{
public:
- edited_line (const char *filename, int line_num);
+ edited_line (file_cache &fc, const char *filename, int line_num);
~edited_line ();
static void delete_cb (edited_line *el);
@@ -208,8 +214,9 @@ print_diff_line (pretty_printer *pp, char prefix_char,
/* edit_context's ctor. */
-edit_context::edit_context ()
-: m_valid (true),
+edit_context::edit_context (file_cache &fc)
+: m_file_cache (fc),
+ m_valid (true),
m_files (strcmp, NULL, edited_file::delete_cb)
{}
@@ -334,7 +341,7 @@ edit_context::get_or_insert_file (const char *filename)
return *file;
/* Not found. */
- file = new edited_file (filename);
+ file = new edited_file (*this, filename);
m_files.insert (filename, file);
return *file;
}
@@ -350,8 +357,9 @@ static int line_comparator (int a, int b)
/* edited_file's constructor. */
-edited_file::edited_file (const char *filename)
-: m_filename (filename),
+edited_file::edited_file (edit_context &ec, const char *filename)
+: m_edit_context (ec),
+ m_filename (filename),
m_edited_lines (line_comparator, NULL, edited_line::delete_cb),
m_num_lines (-1)
{
@@ -422,7 +430,8 @@ edited_file::print_content (pretty_printer *pp)
el->print_content (pp);
else
{
- char_span line = location_get_source_line (m_filename, line_num);
+ char_span line
+ = get_file_cache ().get_source_line (m_filename, line_num);
if (!line)
return false;
for (size_t i = 0; i < line.length (); i++)
@@ -547,7 +556,8 @@ edited_file::print_diff_hunk (pretty_printer *pp, int old_start_of_hunk,
else
{
/* Unchanged line. */
- char_span old_line = location_get_source_line (m_filename, line_num);
+ char_span old_line
+ = get_file_cache ().get_source_line (m_filename, line_num);
print_diff_line (pp, ' ', old_line.get_buffer (), old_line.length ());
line_num++;
}
@@ -576,7 +586,8 @@ edited_file::print_run_of_changed_lines (pretty_printer *pp,
gcc_assert (el_in_run);
if (el_in_run->actually_edited_p ())
{
- char_span old_line = location_get_source_line (m_filename, line_num);
+ char_span old_line
+ = get_file_cache ().get_source_line (m_filename, line_num);
print_diff_line (pp, '-', old_line.get_buffer (),
old_line.length ());
}
@@ -649,7 +660,7 @@ edited_file::get_or_insert_line (int line)
edited_line *el = get_line (line);
if (el)
return el;
- el = new edited_line (m_filename, line);
+ el = new edited_line (get_file_cache (), m_filename, line);
if (el->get_content () == NULL)
{
delete el;
@@ -673,14 +684,15 @@ edited_file::get_num_lines (bool *missing_trailing_newline)
while (true)
{
char_span line
- = location_get_source_line (m_filename, m_num_lines + 1);
+ = get_file_cache ().get_source_line (m_filename, m_num_lines + 1);
if (line)
m_num_lines++;
else
break;
}
}
- *missing_trailing_newline = location_missing_trailing_newline (m_filename);
+ *missing_trailing_newline
+ = get_file_cache ().missing_trailing_newline_p (m_filename);
return m_num_lines;
}
@@ -688,13 +700,13 @@ edited_file::get_num_lines (bool *missing_trailing_newline)
/* edited_line's ctor. */
-edited_line::edited_line (const char *filename, int line_num)
+edited_line::edited_line (file_cache &fc, const char *filename, int line_num)
: m_line_num (line_num),
m_content (NULL), m_len (0), m_alloc_sz (0),
m_line_events (),
m_predecessors ()
{
- char_span line = location_get_source_line (filename, line_num);
+ char_span line = fc.get_source_line (filename, line_num);
if (!line)
return;
m_len = line.length ();
@@ -913,7 +925,8 @@ test_get_content ()
{
const char *content = ("");
temp_source_file tmp (SELFTEST_LOCATION, ".c", content);
- edit_context edit;
+ file_cache fc;
+ edit_context edit (fc);
auto_free <char *> result = edit.get_content (tmp.get_filename ());
ASSERT_STREQ ("", result);
}
@@ -924,7 +937,8 @@ test_get_content ()
"foo = bar.field;\n"
"/* after */\n");
temp_source_file tmp (SELFTEST_LOCATION, ".c", content);
- edit_context edit;
+ file_cache fc;
+ edit_context edit (fc);
auto_free <char *> result = edit.get_content (tmp.get_filename ());
ASSERT_STREQ ("/* before */\n"
"foo = bar.field;\n"
@@ -937,7 +951,8 @@ test_get_content ()
"foo = bar.field;\n"
"/* after */");
temp_source_file tmp (SELFTEST_LOCATION, ".c", content);
- edit_context edit;
+ file_cache fc;
+ edit_context edit (fc);
auto_free <char *> result = edit.get_content (tmp.get_filename ());
/* We should respect the omitted trailing newline. */
ASSERT_STREQ ("/* before */\n"
@@ -970,7 +985,8 @@ test_applying_fixits_insert_before (const line_table_case &case_)
if (start > LINE_MAP_MAX_LOCATION_WITH_COLS)
return;
- edit_context edit;
+ file_cache fc;
+ edit_context edit (fc);
edit.add_fixits (&richloc);
auto_free <char *> new_content = edit.get_content (filename);
if (start <= LINE_MAP_MAX_LOCATION_WITH_COLS)
@@ -1028,7 +1044,8 @@ test_applying_fixits_insert_after (const line_table_case &case_)
return;
/* Verify that the text was inserted after the end of "field". */
- edit_context edit;
+ file_cache fc;
+ edit_context edit (fc);
edit.add_fixits (&richloc);
auto_free <char *> new_content = edit.get_content (filename);
ASSERT_STREQ ("/* before */\n"
@@ -1070,7 +1087,8 @@ test_applying_fixits_insert_after_at_line_end (const line_table_case &case_)
if (loc > LINE_MAP_MAX_LOCATION_WITH_COLS)
return;
- edit_context edit;
+ file_cache fc;
+ edit_context edit (fc);
edit.add_fixits (&richloc);
auto_free <char *> new_content = edit.get_content (filename);
ASSERT_STREQ ("/* before */\n"
@@ -1128,7 +1146,8 @@ test_applying_fixits_insert_after_failure (const line_table_case &case_)
richloc.add_fixit_insert_after ("/* inserted */");
ASSERT_TRUE (richloc.seen_impossible_fixit_p ());
- edit_context edit;
+ file_cache fc;
+ edit_context edit (fc);
edit.add_fixits (&richloc);
ASSERT_FALSE (edit.valid_p ());
ASSERT_EQ (NULL, edit.get_content (filename));
@@ -1165,7 +1184,8 @@ test_applying_fixits_insert_containing_newline (const line_table_case &case_)
if (case_finish > LINE_MAP_MAX_LOCATION_WITH_COLS)
return;
- edit_context edit;
+ file_cache fc;
+ edit_context edit (fc);
edit.add_fixits (&richloc);
auto_free <char *> new_content = edit.get_content (filename);
ASSERT_STREQ ((" case 'a':\n"
@@ -1209,7 +1229,8 @@ test_applying_fixits_growing_replace (const line_table_case &case_)
rich_location richloc (line_table, field);
richloc.add_fixit_replace ("m_field");
- edit_context edit;
+ file_cache fc;
+ edit_context edit (fc);
edit.add_fixits (&richloc);
auto_free <char *> new_content = edit.get_content (filename);
if (finish <= LINE_MAP_MAX_LOCATION_WITH_COLS)
@@ -1254,7 +1275,8 @@ test_applying_fixits_shrinking_replace (const line_table_case &case_)
rich_location richloc (line_table, m_field);
richloc.add_fixit_replace ("field");
- edit_context edit;
+ file_cache fc;
+ edit_context edit (fc);
edit.add_fixits (&richloc);
auto_free <char *> new_content = edit.get_content (filename);
if (finish <= LINE_MAP_MAX_LOCATION_WITH_COLS)
@@ -1308,7 +1330,8 @@ test_applying_fixits_replace_containing_newline (const line_table_case &case_)
if (finish > LINE_MAP_MAX_LOCATION_WITH_COLS)
return;
- edit_context edit;
+ file_cache fc;
+ edit_context edit (fc);
edit.add_fixits (&richloc);
auto_free <char *> new_content = edit.get_content (filename);
//ASSERT_STREQ ("foo\n = bar ();\n", new_content);
@@ -1339,7 +1362,8 @@ test_applying_fixits_remove (const line_table_case &case_)
range.m_finish = finish;
richloc.add_fixit_remove (range);
- edit_context edit;
+ file_cache fc;
+ edit_context edit (fc);
edit.add_fixits (&richloc);
auto_free <char *> new_content = edit.get_content (filename);
if (finish <= LINE_MAP_MAX_LOCATION_WITH_COLS)
@@ -1404,7 +1428,8 @@ test_applying_fixits_multiple (const line_table_case &case_)
replace_b.add_fixit_replace (source_range::from_locations (c11, c15),
"meadow");
- edit_context edit;
+ file_cache fc;
+ edit_context edit (fc);
edit.add_fixits (&insert_a);
ASSERT_EQ (100, edit.get_effective_column (filename, 1, 100));
ASSERT_EQ (1, edit.get_effective_column (filename, 2, 1));
@@ -1506,7 +1531,8 @@ test_applying_fixits_multiple_lines (const line_table_case &case_)
linemap_add (line_table, LC_ENTER, false, filename, 1);
linemap_position_for_column (line_table, 127);
- edit_context edit;
+ file_cache fc;
+ edit_context edit (fc);
/* A run of consecutive lines. */
change_line (edit, 2);
@@ -1594,7 +1620,8 @@ test_applying_fixits_modernize_named_init (const line_table_case &case_)
/* The order should not matter. Do r1 then r2. */
{
- edit_context edit;
+ file_cache fc;
+ edit_context edit (fc);
edit.add_fixits (&r1);
/* Verify state after first replacement. */
@@ -1627,7 +1654,8 @@ test_applying_fixits_modernize_named_init (const line_table_case &case_)
/* Try again, doing r2 then r1; the new_content should be the same. */
{
- edit_context edit;
+ file_cache fc;
+ edit_context edit (fc);
edit.add_fixits (&r2);
edit.add_fixits (&r1);
auto_free <char *> new_content = edit.get_content (tmp.get_filename ());
@@ -1655,7 +1683,8 @@ test_applying_fixits_unreadable_file ()
insert.add_fixit_insert_before ("change 1");
insert.add_fixit_insert_before ("change 2");
- edit_context edit;
+ file_cache fc;
+ edit_context edit (fc);
/* Attempting to add the fixits affecting the unreadable file
should transition the edit from valid to invalid. */
ASSERT_TRUE (edit.valid_p ());
@@ -1688,7 +1717,8 @@ test_applying_fixits_line_out_of_range ()
/* Verify that attempting the insertion puts an edit_context
into an invalid state. */
- edit_context edit;
+ file_cache fc;
+ edit_context edit (fc);
ASSERT_TRUE (edit.valid_p ());
edit.add_fixits (&insert);
ASSERT_FALSE (edit.valid_p ());
@@ -1725,7 +1755,8 @@ test_applying_fixits_column_validation (const line_table_case &case_)
/* Col 15 is at the end of the line, so the insertion
should succeed. */
- edit_context edit;
+ file_cache fc;
+ edit_context edit (fc);
edit.add_fixits (&richloc);
auto_free <char *> new_content = edit.get_content (tmp.get_filename ());
if (c15 <= LINE_MAP_MAX_LOCATION_WITH_COLS)
@@ -1741,7 +1772,8 @@ test_applying_fixits_column_validation (const line_table_case &case_)
/* Col 16 is beyond the end of the line, so the insertion
should fail gracefully. */
- edit_context edit;
+ file_cache fc;
+ edit_context edit (fc);
ASSERT_TRUE (edit.valid_p ());
edit.add_fixits (&richloc);
ASSERT_FALSE (edit.valid_p ());
@@ -1759,7 +1791,8 @@ test_applying_fixits_column_validation (const line_table_case &case_)
/* Col 14 is at the end of the line, so the replacement
should succeed. */
- edit_context edit;
+ file_cache fc;
+ edit_context edit (fc);
edit.add_fixits (&richloc);
auto_free <char *> new_content = edit.get_content (tmp.get_filename ());
if (c14 <= LINE_MAP_MAX_LOCATION_WITH_COLS)
@@ -1776,7 +1809,8 @@ test_applying_fixits_column_validation (const line_table_case &case_)
/* Col 15 is after the end of the line, so the replacement
should fail; verify that the attempt fails gracefully. */
- edit_context edit;
+ file_cache fc;
+ edit_context edit (fc);
ASSERT_TRUE (edit.valid_p ());
edit.add_fixits (&richloc);
ASSERT_FALSE (edit.valid_p ());