diff options
author | David Malcolm <dmalcolm@redhat.com> | 2023-11-14 11:01:40 -0500 |
---|---|---|
committer | David Malcolm <dmalcolm@redhat.com> | 2023-11-14 11:01:40 -0500 |
commit | 1bdd665a025a74f4f1b641600a8c0bbca3355aa7 (patch) | |
tree | 5954da6c5e393727cbf262d6682cc60a0a873fc1 /gcc/input.cc | |
parent | 070944fdd6394c992885f33f5bde8a231d37fa7f (diff) | |
download | gcc-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/input.cc')
-rw-r--r-- | gcc/input.cc | 125 |
1 files changed, 43 insertions, 82 deletions
diff --git a/gcc/input.cc b/gcc/input.cc index 1b22922..b520264 100644 --- a/gcc/input.cc +++ b/gcc/input.cc @@ -288,23 +288,6 @@ expand_location_1 (const line_maps *set, return xloc; } -/* Initialize the set of cache used for files accessed by caret - diagnostic. */ - -static void -diagnostic_file_cache_init (void) -{ - gcc_assert (global_dc); - global_dc->file_cache_init (); -} - -void -diagnostic_context::file_cache_init () -{ - if (m_file_cache == nullptr) - m_file_cache = new file_cache (); -} - /* Return the total lines number that have been read so far by the line map (in the preprocessor) so far. For languages like C++ that entirely preprocess the input file before starting to parse, this @@ -356,17 +339,6 @@ file_cache::lookup_file (const char *file_path) with tempfiles. */ void -diagnostics_file_cache_forcibly_evict_file (const char *file_path) -{ - gcc_assert (file_path); - - auto file_cache = global_dc->get_file_cache (); - if (!file_cache) - return; - file_cache->forcibly_evict_file (file_path); -} - -void file_cache::forcibly_evict_file (const char *file_path) { gcc_assert (file_path); @@ -379,6 +351,19 @@ file_cache::forcibly_evict_file (const char *file_path) r->evict (); } +/* Determine if FILE_PATH missing a trailing newline on its final line. + Only valid to call once all of the file has been loaded, by + requesting a line number beyond the end of the file. */ + +bool +file_cache::missing_trailing_newline_p (const char *file_path) +{ + gcc_assert (file_path); + + file_cache_slot *r = lookup_or_add_file (file_path); + return r->missing_trailing_newline_p (); +} + void file_cache_slot::evict () { @@ -403,8 +388,6 @@ file_cache_slot::evict () file_cache_slot* file_cache::evicted_cache_tab_entry (unsigned *highest_use_count) { - diagnostic_file_cache_init (); - file_cache_slot *to_evict = &m_file_slots[0]; unsigned huc = to_evict->get_use_count (); for (unsigned i = 1; i < num_file_slots; ++i) @@ -968,19 +951,12 @@ file_cache::get_source_line (const char *file_path, int line) return char_span (buffer, len); } -char_span -location_get_source_line (const char *file_path, int line) -{ - diagnostic_file_cache_init (); - return global_dc->get_file_cache ()->get_source_line (file_path, line); -} - /* Return a NUL-terminated copy of the source text between two locations, or NULL if the arguments are invalid. The caller is responsible for freeing the return value. */ char * -get_source_text_between (location_t start, location_t end) +get_source_text_between (file_cache &fc, location_t start, location_t end) { expanded_location expstart = expand_location_to_spelling_point (start, LOCATION_ASPECT_START); @@ -1005,7 +981,7 @@ get_source_text_between (location_t start, location_t end) /* For a single line we need to trim both edges. */ if (expstart.line == expend.line) { - char_span line = location_get_source_line (expstart.file, expstart.line); + char_span line = fc.get_source_line (expstart.file, expstart.line); if (line.length () < 1) return NULL; int s = expstart.column - 1; @@ -1022,7 +998,7 @@ get_source_text_between (location_t start, location_t end) parts of the start and end lines off depending on column values. */ for (int lnum = expstart.line; lnum <= expend.line; ++lnum) { - char_span line = location_get_source_line (expstart.file, lnum); + char_span line = fc.get_source_line (expstart.file, lnum); if (line.length () < 1 && (lnum != expstart.line && lnum != expend.line)) continue; @@ -1077,33 +1053,6 @@ file_cache::get_source_file_content (const char *file_path) return c->get_full_file_content (); } - -/* Get a borrowed char_span to the full content of FILE_PATH - as decoded according to the input charset, encoded as UTF-8. */ - -char_span -get_source_file_content (const char *file_path) -{ - diagnostic_file_cache_init (); - return global_dc->get_file_cache ()->get_source_file_content (file_path); -} - -/* Determine if FILE_PATH missing a trailing newline on its final line. - Only valid to call once all of the file has been loaded, by - requesting a line number beyond the end of the file. */ - -bool -location_missing_trailing_newline (const char *file_path) -{ - diagnostic_file_cache_init (); - - file_cache_slot *c = global_dc->get_file_cache ()->lookup_or_add_file (file_path); - if (c == NULL) - return false; - - return c->missing_trailing_newline_p (); -} - /* Test if the location originates from the spelling location of a builtin-tokens. That is, return TRUE if LOC is a (possibly virtual) location of a built-in token that appears in the expansion @@ -1236,12 +1185,13 @@ make_location (location_t caret, source_range src_range) source line in order to calculate the display width. If that cannot be done for any reason, then returns the byte column as a fallback. */ int -location_compute_display_column (expanded_location exploc, +location_compute_display_column (file_cache &fc, + expanded_location exploc, const cpp_char_column_policy &policy) { if (!(exploc.file && *exploc.file && exploc.line && exploc.column)) return exploc.column; - char_span line = location_get_source_line (exploc.file, exploc.line); + char_span line = fc.get_source_line (exploc.file, exploc.line); /* If line is NULL, this function returns exploc.column which is the desired fallback. */ return cpp_byte_column_to_display_column (line.get_buffer (), line.length (), @@ -1386,6 +1336,8 @@ dump_labelled_location_range (FILE *stream, void dump_location_info (FILE *stream) { + file_cache fc; + /* Visualize the reserved locations. */ dump_labelled_location_range (stream, "RESERVED LOCATIONS", 0, RESERVED_LOCATION_COUNT); @@ -1456,8 +1408,8 @@ dump_location_info (FILE *stream) { /* Beginning of a new source line: draw the line. */ - char_span line_text = location_get_source_line (exploc.file, - exploc.line); + char_span line_text = fc.get_source_line (exploc.file, + exploc.line); if (!line_text) break; fprintf (stream, @@ -1705,6 +1657,7 @@ class auto_cpp_string_vec : public auto_vec <cpp_string> static const char * get_substring_ranges_for_loc (cpp_reader *pfile, + file_cache &fc, string_concat_db *concats, location_t strloc, enum cpp_ttype type, @@ -1784,7 +1737,7 @@ get_substring_ranges_for_loc (cpp_reader *pfile, if (start.column > finish.column) return "range endpoints are reversed"; - char_span line = location_get_source_line (start.file, start.line); + char_span line = fc.get_source_line (start.file, start.line); if (!line) return "unable to read source line"; @@ -1872,6 +1825,7 @@ get_substring_ranges_for_loc (cpp_reader *pfile, const char * get_location_within_string (cpp_reader *pfile, + file_cache &fc, string_concat_db *concats, location_t strloc, enum cpp_ttype type, @@ -1885,7 +1839,7 @@ get_location_within_string (cpp_reader *pfile, cpp_substring_ranges ranges; const char *err - = get_substring_ranges_for_loc (pfile, concats, strloc, type, ranges); + = get_substring_ranges_for_loc (pfile, fc, concats, strloc, type, ranges); if (err) return err; @@ -1954,6 +1908,7 @@ namespace selftest { static const char * get_source_range_for_char (cpp_reader *pfile, + file_cache &fc, string_concat_db *concats, location_t strloc, enum cpp_ttype type, @@ -1965,7 +1920,7 @@ get_source_range_for_char (cpp_reader *pfile, cpp_substring_ranges ranges; const char *err - = get_substring_ranges_for_loc (pfile, concats, strloc, type, ranges); + = get_substring_ranges_for_loc (pfile, fc, concats, strloc, type, ranges); if (err) return err; @@ -1981,6 +1936,7 @@ get_source_range_for_char (cpp_reader *pfile, static const char * get_num_source_ranges_for_substring (cpp_reader *pfile, + file_cache &fc, string_concat_db *concats, location_t strloc, enum cpp_ttype type, @@ -1990,7 +1946,7 @@ get_num_source_ranges_for_substring (cpp_reader *pfile, cpp_substring_ranges ranges; const char *err - = get_substring_ranges_for_loc (pfile, concats, strloc, type, ranges); + = get_substring_ranges_for_loc (pfile, fc, concats, strloc, type, ranges); if (err) return err; @@ -2313,23 +2269,24 @@ test_reading_source_line () "01234567890123456789\n" "This is the test text\n" "This is the 3rd line"); + file_cache fc; /* Read back a specific line from the tempfile. */ - char_span source_line = location_get_source_line (tmp.get_filename (), 3); + char_span source_line = fc.get_source_line (tmp.get_filename (), 3); ASSERT_TRUE (source_line); ASSERT_TRUE (source_line.get_buffer () != NULL); ASSERT_EQ (20, source_line.length ()); ASSERT_TRUE (!strncmp ("This is the 3rd line", source_line.get_buffer (), source_line.length ())); - source_line = location_get_source_line (tmp.get_filename (), 2); + source_line = fc.get_source_line (tmp.get_filename (), 2); ASSERT_TRUE (source_line); ASSERT_TRUE (source_line.get_buffer () != NULL); ASSERT_EQ (21, source_line.length ()); ASSERT_TRUE (!strncmp ("This is the test text", source_line.get_buffer (), source_line.length ())); - source_line = location_get_source_line (tmp.get_filename (), 4); + source_line = fc.get_source_line (tmp.get_filename (), 4); ASSERT_FALSE (source_line); ASSERT_TRUE (source_line.get_buffer () == NULL); } @@ -2490,6 +2447,7 @@ public: line_table_test m_ltt; cpp_reader_ptr m_parser; temp_source_file m_tempfile; + file_cache m_file_cache; string_concat_db m_concats; bool m_implicitly_expect_EOF; }; @@ -2681,7 +2639,8 @@ assert_char_at_range (const location &loc, source_range actual_range = source_range(); const char *err - = get_source_range_for_char (pfile, concats, strloc, type, idx, + = get_source_range_for_char (pfile, test.m_file_cache, + concats, strloc, type, idx, &actual_range); if (should_have_column_data_p (strloc)) ASSERT_EQ_AT (loc, NULL, err); @@ -2736,7 +2695,8 @@ assert_num_substring_ranges (const location &loc, int actual_num_ranges = -1; const char *err - = get_num_source_ranges_for_substring (pfile, concats, strloc, type, + = get_num_source_ranges_for_substring (pfile, test.m_file_cache, + concats, strloc, type, &actual_num_ranges); if (should_have_column_data_p (strloc)) ASSERT_EQ_AT (loc, NULL, err); @@ -2773,7 +2733,7 @@ assert_has_no_substring_ranges (const location &loc, string_concat_db *concats = &test.m_concats; cpp_substring_ranges ranges; const char *actual_err - = get_substring_ranges_for_loc (pfile, concats, strloc, + = get_substring_ranges_for_loc (pfile, test.m_file_cache, concats, strloc, type, ranges); if (should_have_column_data_p (strloc)) ASSERT_STREQ_AT (loc, expected_err, actual_err); @@ -3542,7 +3502,8 @@ test_lexer_string_locations_concatenation_2 (const line_table_case &case_) this case. */ source_range actual_range; const char *err - = get_source_range_for_char (test.m_parser, &test.m_concats, + = get_source_range_for_char (test.m_parser, test.m_file_cache, + &test.m_concats, initial_loc, type, 0, &actual_range); ASSERT_STREQ ("range starts after LINE_MAP_MAX_LOCATION_WITH_COLS", err); return; |