From fe066ce3d8b45a687ddb9ebba6426a3746f3a8ca Mon Sep 17 00:00:00 2001 From: dmalcolm Date: Fri, 2 Sep 2016 18:00:57 +0000 Subject: Introduce class edit_context gcc/ChangeLog: * Makefile.in (OBJS-libcommon): Add edit-context.o. * diagnostic-color.c (color_dict): Add "diff-filename", "diff-hunk", "diff-delete", and "diff-insert". (parse_gcc_colors): Update default value of GCC_COLORS in comment to reflect above changes. * doc/invoke.texi (-fdiagnostics-color): Update description of default GCC_COLORS, and of the supported capabilities. * edit-context.c: New file. * edit-context.h: New file. * input.c (struct fcache): Add field "missing_trailing_newline". (diagnostics_file_cache_forcibly_evict_file): Initialize it to true. (add_file_to_cache_tab): Likewise. (fcache::fcache): Likewise. (get_next_line): Update c->missing_trailing_newline. (location_missing_trailing_newline): New function. * input.h (location_missing_trailing_newline): New decl. * selftest-run-tests.c (selftest::run_tests): Call edit_context_c_tests. * selftest.h (edit_context_c_tests): New decl. libcpp/ChangeLog: * include/line-map.h (rich_location::seen_impossible_fixit_p): New accessor. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@239963 138bc75d-0d04-0410-961f-82ee72b054a4 --- gcc/input.c | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) (limited to 'gcc/input.c') diff --git a/gcc/input.c b/gcc/input.c index 2ff7cb8e943..55bff4891ef 100644 --- a/gcc/input.c +++ b/gcc/input.c @@ -95,6 +95,11 @@ struct fcache before the line map has seen the end of the file. */ size_t total_lines; + /* Could this file be missing a trailing newline on its final line? + Initially true (to cope with empty files), set to true/false + as each line is read. */ + bool missing_trailing_newline; + /* This is a record of the beginning and end of the lines we've seen while reading the file. This is useful to avoid walking the data from the beginning when we are asked to read a line that is @@ -280,6 +285,7 @@ diagnostics_file_cache_forcibly_evict_file (const char *file_path) r->line_record.truncate (0); r->use_count = 0; r->total_lines = 0; + r->missing_trailing_newline = true; } /* Return the file cache that has been less used, recently, or the @@ -348,6 +354,7 @@ add_file_to_cache_tab (const char *file_path) add_file_to_cache_tab is called. */ r->use_count = ++highest_use_count; r->total_lines = total_lines_num (file_path); + r->missing_trailing_newline = true; return r; } @@ -372,7 +379,7 @@ lookup_or_add_file_to_cache_tab (const char *file_path) fcache::fcache () : use_count (0), file_path (NULL), fp (NULL), data (0), size (0), nb_read (0), line_start_idx (0), line_num (0), - total_lines (0) + total_lines (0), missing_trailing_newline (true) { line_record.create (0); } @@ -507,16 +514,24 @@ get_next_line (fcache *c, char **line, ssize_t *line_len) } } if (line_end == NULL) - /* We've loadded all the file into the cache and still no - '\n'. Let's say the line ends up at one byte passed the - end of the file. This is to stay consistent with the case - of when the line ends up with a '\n' and line_end points to - that terminal '\n'. That consistency is useful below in - the len calculation. */ - line_end = c->data + c->nb_read ; + { + /* We've loadded all the file into the cache and still no + '\n'. Let's say the line ends up at one byte passed the + end of the file. This is to stay consistent with the case + of when the line ends up with a '\n' and line_end points to + that terminal '\n'. That consistency is useful below in + the len calculation. */ + line_end = c->data + c->nb_read ; + c->missing_trailing_newline = true; + } + else + c->missing_trailing_newline = false; } else - next_line_start = line_end + 1; + { + next_line_start = line_end + 1; + c->missing_trailing_newline = false; + } if (ferror (c->fp)) return -1; @@ -751,6 +766,20 @@ location_get_source_line (const char *file_path, int line, return read ? buffer : NULL; } +/* 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) +{ + fcache *c = lookup_or_add_file_to_cache_tab (file_path); + if (c == NULL) + return false; + + return c->missing_trailing_newline; +} + /* 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 -- cgit v1.2.1