diff options
author | David Malcolm <dmalcolm@redhat.com> | 2018-04-30 15:01:56 +0000 |
---|---|---|
committer | David Malcolm <dmalcolm@gcc.gnu.org> | 2018-04-30 15:01:56 +0000 |
commit | 7761dfbee17cb7a4bb3539a381bec63d31af7c28 (patch) | |
tree | e583063e82b23bf43e882069fe4de7790d5e078d /gcc/edit-context.c | |
parent | b6e33d73d8aa1b7965d6b2bf08b5bbd673e63284 (diff) | |
download | gcc-7761dfbee17cb7a4bb3539a381bec63d31af7c28.zip gcc-7761dfbee17cb7a4bb3539a381bec63d31af7c28.tar.gz gcc-7761dfbee17cb7a4bb3539a381bec63d31af7c28.tar.bz2 |
Use char_span for return type of location_get_source_line
location_get_source_line returns a const char * that isn't 0-terminated,
writing back a length through an int * param.
This is error-prone, as all call-sites have to take into account the
lack of 0-termination, and respect the length of the buffer.
It's cleaner to bundle together this pointer+length state into a class,
so this patch does so, reusing the "char_span" class that I introduced
in r250187 (as part of the fix for PR c/81405).
The patch also adds assertions to all access to the char_span.
gcc/c-family/ChangeLog:
* c-format.c (get_corrected_substring): Update for
location_get_source_line returning a char_span. Use a char_span
when handling the prefix of the correction.
* c-indentation.c (get_visual_column): Update for
location_get_source_line returning a char_span.
(get_first_nws_vis_column): Likewise.
gcc/ChangeLog:
* diagnostic-show-locus.c (layout::layout): Update for
location_get_source_line returning a char_span.
(struct char_span): Move to input.h.
(struct correction): Update for fields in char_span becoming
private.
(struct source_line): Update for location_get_source_line
returning a char_span.
(layout::print_line): Likewise.
* edit-context.c (edited_file::print_content): Likewise.
(edited_file::print_diff_hunk): Likewise.
(edited_file::print_run_of_changed_lines): Likewise.
(edited_file::get_num_lines): Likewise.
(edited_line::edited_line): Likewise.
* final.c (asm_show_source): Likewise.
* input.c (location_get_source_line): Convert return type
from const char * to char_span, losing the final "line_len"
param.
(dump_location_info): Update for the above.
(get_substring_ranges_for_loc): Likewise. Use a char_span
when handling the literal within the line.
(test_reading_source_line): Update for location_get_source_line
returning a char_span.
* input.h (class char_span): Move here from
diagnostic-show-locus.c, converting from a struct to a class.
Make data members private.
(char_span::operator bool): New.
(char_span::length): New.
(char_span::get_buffer): New.
(char_span::operator[]): New.
(char_span::subspan): Make const.
(char_span::xstrdup): New.
(location_get_source_line): Convert return type from const char *
to char_span, losing the final "line_size" param.
gcc/testsuite/ChangeLog:
* gcc.dg/plugin/diagnostic_plugin_test_show_locus.c
(test_show_locus): Update for location_get_source_line returning a
char_span. Use char_span for handling words in the
"test_many_nested_locations" fix-it example.
From-SVN: r259768
Diffstat (limited to 'gcc/edit-context.c')
-rw-r--r-- | gcc/edit-context.c | 31 |
1 files changed, 12 insertions, 19 deletions
diff --git a/gcc/edit-context.c b/gcc/edit-context.c index 9ac2dfc..3cdb88d 100644 --- a/gcc/edit-context.c +++ b/gcc/edit-context.c @@ -422,12 +422,10 @@ edited_file::print_content (pretty_printer *pp) el->print_content (pp); else { - int len; - const char *line - = location_get_source_line (m_filename, line_num, &len); + char_span line = location_get_source_line (m_filename, line_num); if (!line) return false; - for (int i = 0; i < len; i++) + for (size_t i = 0; i < line.length (); i++) pp_character (pp, line[i]); } if (line_num < line_count) @@ -543,10 +541,8 @@ edited_file::print_diff_hunk (pretty_printer *pp, int old_start_of_hunk, else { /* Unchanged line. */ - int line_len; - const char *old_line - = location_get_source_line (m_filename, line_num, &line_len); - print_diff_line (pp, ' ', old_line, line_len); + char_span old_line = location_get_source_line (m_filename, line_num); + print_diff_line (pp, ' ', old_line.get_buffer (), old_line.length ()); line_num++; } } @@ -574,10 +570,9 @@ edited_file::print_run_of_changed_lines (pretty_printer *pp, gcc_assert (el_in_run); if (el_in_run->actually_edited_p ()) { - int line_len; - const char *old_line - = location_get_source_line (m_filename, line_num, &line_len); - print_diff_line (pp, '-', old_line, line_len); + char_span old_line = location_get_source_line (m_filename, line_num); + print_diff_line (pp, '-', old_line.get_buffer (), + old_line.length ()); } } pp_string (pp, colorize_stop (pp_show_color (pp))); @@ -671,10 +666,8 @@ edited_file::get_num_lines (bool *missing_trailing_newline) m_num_lines = 0; while (true) { - int line_size; - const char *line - = location_get_source_line (m_filename, m_num_lines + 1, - &line_size); + char_span line + = location_get_source_line (m_filename, m_num_lines + 1); if (line) m_num_lines++; else @@ -695,12 +688,12 @@ edited_line::edited_line (const char *filename, int line_num) m_line_events (), m_predecessors () { - const char *line = location_get_source_line (filename, line_num, - &m_len); + char_span line = location_get_source_line (filename, line_num); if (!line) return; + m_len = line.length (); ensure_capacity (m_len); - memcpy (m_content, line, m_len); + memcpy (m_content, line.get_buffer (), m_len); ensure_terminated (); } |