aboutsummaryrefslogtreecommitdiff
path: root/gcc/c-family/c-indentation.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/c-family/c-indentation.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/c-family/c-indentation.cc')
-rw-r--r--gcc/c-family/c-indentation.cc73
1 files changed, 46 insertions, 27 deletions
diff --git a/gcc/c-family/c-indentation.cc b/gcc/c-family/c-indentation.cc
index 5b4614f..a81dffd 100644
--- a/gcc/c-family/c-indentation.cc
+++ b/gcc/c-family/c-indentation.cc
@@ -45,12 +45,13 @@ next_tab_stop (unsigned int vis_column, unsigned int tab_width)
on the line (up to or before EXPLOC). */
static bool
-get_visual_column (expanded_location exploc,
+get_visual_column (file_cache &fc,
+ expanded_location exploc,
unsigned int *out,
unsigned int *first_nws,
unsigned int tab_width)
{
- char_span line = location_get_source_line (exploc.file, exploc.line);
+ char_span line = fc.get_source_line (exploc.file, exploc.line);
if (!line)
return false;
if ((size_t)exploc.column > line.length ())
@@ -87,13 +88,14 @@ get_visual_column (expanded_location exploc,
Otherwise, return false, leaving *FIRST_NWS untouched. */
static bool
-get_first_nws_vis_column (const char *file, int line_num,
+get_first_nws_vis_column (file_cache &fc,
+ const char *file, int line_num,
unsigned int *first_nws,
unsigned int tab_width)
{
gcc_assert (first_nws);
- char_span line = location_get_source_line (file, line_num);
+ char_span line = fc.get_source_line (file, line_num);
if (!line)
return false;
unsigned int vis_column = 0;
@@ -158,7 +160,8 @@ get_first_nws_vis_column (const char *file, int line_num,
Return true if such an unindent/outdent is detected. */
static bool
-detect_intervening_unindent (const char *file,
+detect_intervening_unindent (file_cache &fc,
+ const char *file,
int body_line,
int next_stmt_line,
unsigned int vis_column,
@@ -170,7 +173,7 @@ detect_intervening_unindent (const char *file,
for (int line = body_line + 1; line < next_stmt_line; line++)
{
unsigned int line_vis_column;
- if (get_first_nws_vis_column (file, line, &line_vis_column, tab_width))
+ if (get_first_nws_vis_column (fc, file, line, &line_vis_column, tab_width))
if (line_vis_column < vis_column)
return true;
}
@@ -337,6 +340,8 @@ should_warn_for_misleading_indentation (const token_indent_info &guard_tinfo,
if (next_stmt_exploc.file != body_exploc.file)
return false;
+ file_cache &fc = global_dc->get_file_cache ();
+
/* If NEXT_STMT_LOC and BODY_LOC are on the same line, consider
the location of the guard.
@@ -376,7 +381,8 @@ should_warn_for_misleading_indentation (const token_indent_info &guard_tinfo,
gcc_assert (guard_exploc.line == next_stmt_exploc.line);
unsigned int guard_vis_column;
unsigned int guard_line_first_nws;
- if (!get_visual_column (guard_exploc,
+ if (!get_visual_column (fc,
+ guard_exploc,
&guard_vis_column,
&guard_line_first_nws, tab_width))
return false;
@@ -436,15 +442,18 @@ should_warn_for_misleading_indentation (const token_indent_info &guard_tinfo,
the case for input files containing #line directives, and these
are often for autogenerated sources (e.g. from .md files), where
it's not clear that it's meaningful to look at indentation. */
- if (!get_visual_column (next_stmt_exploc,
+ if (!get_visual_column (fc,
+ next_stmt_exploc,
&next_stmt_vis_column,
&next_stmt_line_first_nws, tab_width))
return false;
- if (!get_visual_column (body_exploc,
+ if (!get_visual_column (fc,
+ body_exploc,
&body_vis_column,
&body_line_first_nws, tab_width))
return false;
- if (!get_visual_column (guard_exploc,
+ if (!get_visual_column (fc,
+ guard_exploc,
&guard_vis_column,
&guard_line_first_nws, tab_width))
return false;
@@ -528,7 +537,8 @@ should_warn_for_misleading_indentation (const token_indent_info &guard_tinfo,
/* Don't warn if there is an unindent between the two statements. */
int vis_column = MIN (next_stmt_vis_column, body_vis_column);
- if (detect_intervening_unindent (body_exploc.file, body_exploc.line,
+ if (detect_intervening_unindent (fc,
+ body_exploc.file, body_exploc.line,
next_stmt_exploc.line,
vis_column, tab_width))
return false;
@@ -686,6 +696,7 @@ test_next_tab_stop ()
static void
assert_get_visual_column_succeeds (const location &loc,
+ file_cache &fc,
const char *file, int line, int column,
const unsigned int tab_width,
unsigned int expected_visual_column,
@@ -699,7 +710,8 @@ assert_get_visual_column_succeeds (const location &loc,
exploc.sysp = false;
unsigned int actual_visual_column;
unsigned int actual_first_nws;
- bool result = get_visual_column (exploc,
+ bool result = get_visual_column (fc,
+ exploc,
&actual_visual_column,
&actual_first_nws, tab_width);
ASSERT_TRUE_AT (loc, result);
@@ -710,12 +722,14 @@ assert_get_visual_column_succeeds (const location &loc,
/* Verify that the given call to get_visual_column succeeds, with
the given results. */
-#define ASSERT_GET_VISUAL_COLUMN_SUCCEEDS(FILENAME, LINE, COLUMN, \
+#define ASSERT_GET_VISUAL_COLUMN_SUCCEEDS(FILE_CACHE, \
+ FILENAME, LINE, COLUMN, \
TAB_WIDTH, \
EXPECTED_VISUAL_COLUMN, \
EXPECTED_FIRST_NWS) \
SELFTEST_BEGIN_STMT \
assert_get_visual_column_succeeds (SELFTEST_LOCATION, \
+ FILE_CACHE, \
FILENAME, LINE, COLUMN, \
TAB_WIDTH, \
EXPECTED_VISUAL_COLUMN, \
@@ -726,6 +740,7 @@ assert_get_visual_column_succeeds (const location &loc,
static void
assert_get_visual_column_fails (const location &loc,
+ file_cache &fc,
const char *file, int line, int column,
const unsigned int tab_width)
{
@@ -737,7 +752,8 @@ assert_get_visual_column_fails (const location &loc,
exploc.sysp = false;
unsigned int actual_visual_column;
unsigned int actual_first_nws;
- bool result = get_visual_column (exploc,
+ bool result = get_visual_column (fc,
+ exploc,
&actual_visual_column,
&actual_first_nws, tab_width);
ASSERT_FALSE_AT (loc, result);
@@ -745,10 +761,12 @@ assert_get_visual_column_fails (const location &loc,
/* Verify that the given call to get_visual_column fails gracefully. */
-#define ASSERT_GET_VISUAL_COLUMN_FAILS(FILENAME, LINE, COLUMN, \
+#define ASSERT_GET_VISUAL_COLUMN_FAILS(FILE_CACHE, \
+ FILENAME, LINE, COLUMN, \
TAB_WIDTH) \
SELFTEST_BEGIN_STMT \
assert_get_visual_column_fails (SELFTEST_LOCATION, \
+ FILE_CACHE, \
FILENAME, LINE, COLUMN, \
TAB_WIDTH); \
SELFTEST_END_STMT
@@ -770,6 +788,7 @@ test_get_visual_column ()
"\t line 2\n");
line_table_test ltt;
temp_source_file tmp (SELFTEST_LOCATION, ".txt", content);
+ file_cache fc;
const unsigned int tab_width = 8;
const char *file = tmp.get_filename ();
@@ -777,27 +796,27 @@ test_get_visual_column ()
/* Line 1 (space-based indentation). */
{
const int line = 1;
- ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (file, line, 1, tab_width, 0, 0);
- ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (file, line, 2, tab_width, 1, 1);
- ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (file, line, 3, tab_width, 2, 2);
+ ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (fc, file, line, 1, tab_width, 0, 0);
+ ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (fc, file, line, 2, tab_width, 1, 1);
+ ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (fc, file, line, 3, tab_width, 2, 2);
/* first_nws should have stopped increasing. */
- ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (file, line, 4, tab_width, 3, 2);
+ ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (fc, file, line, 4, tab_width, 3, 2);
/* Verify the end-of-line boundary. */
- ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (file, line, 8, tab_width, 7, 2);
- ASSERT_GET_VISUAL_COLUMN_FAILS (file, line, 9, tab_width);
+ ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (fc, file, line, 8, tab_width, 7, 2);
+ ASSERT_GET_VISUAL_COLUMN_FAILS (fc, file, line, 9, tab_width);
}
/* Line 2 (tab-based indentation). */
{
const int line = 2;
- ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (file, line, 1, tab_width, 0, 0);
- ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (file, line, 2, tab_width, 8, 8);
- ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (file, line, 3, tab_width, 9, 9);
+ ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (fc, file, line, 1, tab_width, 0, 0);
+ ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (fc, file, line, 2, tab_width, 8, 8);
+ ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (fc, file, line, 3, tab_width, 9, 9);
/* first_nws should have stopped increasing. */
- ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (file, line, 4, tab_width, 10, 9);
+ ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (fc, file, line, 4, tab_width, 10, 9);
/* Verify the end-of-line boundary. */
- ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (file, line, 8, tab_width, 14, 9);
- ASSERT_GET_VISUAL_COLUMN_FAILS (file, line, 9, tab_width);
+ ASSERT_GET_VISUAL_COLUMN_SUCCEEDS (fc, file, line, 8, tab_width, 14, 9);
+ ASSERT_GET_VISUAL_COLUMN_FAILS (fc, file, line, 9, tab_width);
}
}