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.

From-SVN: r239963
This commit is contained in:
David Malcolm 2016-09-02 18:00:57 +00:00 committed by David Malcolm
parent bad9b2889a
commit c65236d682
12 changed files with 1675 additions and 11 deletions

View file

@ -1,3 +1,26 @@
2016-09-02 David Malcolm <dmalcolm@redhat.com>
* 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.
2016-09-02 Jakub Jelinek <jakub@redhat.com>
Richard Biener <rguenth@suse.de>

View file

@ -1561,6 +1561,7 @@ OBJS = \
# Objects in libcommon.a, potentially used by all host binaries and with
# no target dependencies.
OBJS-libcommon = diagnostic.o diagnostic-color.o diagnostic-show-locus.o \
edit-context.o \
pretty-print.o intl.o \
vec.o input.o version.o hash-table.o ggc-none.o memory-block.o \
selftest.o

View file

@ -170,6 +170,10 @@ static struct color_cap color_dict[] =
{ "quote", SGR_SEQ (COLOR_BOLD), 5, false },
{ "fixit-insert", SGR_SEQ (COLOR_FG_GREEN), 12, false },
{ "fixit-delete", SGR_SEQ (COLOR_FG_RED), 12, false },
{ "diff-filename", SGR_SEQ (COLOR_BOLD), 13, false },
{ "diff-hunk", SGR_SEQ (COLOR_FG_CYAN), 9, false },
{ "diff-delete", SGR_SEQ (COLOR_FG_RED), 11, false },
{ "diff-insert", SGR_SEQ (COLOR_FG_GREEN), 11, false },
{ NULL, NULL, 0, false }
};
@ -200,7 +204,8 @@ colorize_stop (bool show_color)
/* Parse GCC_COLORS. The default would look like:
GCC_COLORS='error=01;31:warning=01;35:note=01;36:\
range1=32:range2=34:locus=01:quote=01:\
fixit-insert=32:fixit-delete=31'
fixit-insert=32:fixit-delete=31'\
diff-filename=01:diff-hunk=32:diff-delete=31:diff-insert=32'
No character escaping is needed or supported. */
static bool
parse_gcc_colors (void)

View file

@ -3343,7 +3343,9 @@ for 88-color and 256-color modes background colors.
The default @env{GCC_COLORS} is
@smallexample
error=01;31:warning=01;35:note=01;36:range1=32:range2=34:locus=01:quote=01:fixit-insert=32:fixit-delete=31
error=01;31:warning=01;35:note=01;36:range1=32:range2=34:locus=01:quote=01:\
fixit-insert=32:fixit-delete=31:\
diff-filename=01:diff-hunk=32:diff-delete=31:diff-insert=32
@end smallexample
@noindent
where @samp{01;31} is bold red, @samp{01;35} is bold magenta,
@ -3391,6 +3393,22 @@ be inserted or replaced.
@vindex fixit-delete GCC_COLORS @r{capability}
SGR substring for fix-it hints suggesting text to
be deleted.
@item diff-filename=
@vindex diff-filename GCC_COLORS @r{capability}
SGR substring for filename headers within generated patches.
@item diff-hunk=
@vindex diff-hunk GCC_COLORS @r{capability}
SGR substring for the starts of hunks within generated patches.
@item diff-delete=
@vindex diff-delete GCC_COLORS @r{capability}
SGR substring for deleted lines within generated patches.
@item diff-insert=
@vindex diff-insert GCC_COLORS @r{capability}
SGR substring for inserted lines within generated patches.
@end table
@item -fno-diagnostics-show-option

1511
gcc/edit-context.c Normal file

File diff suppressed because it is too large Load diff

68
gcc/edit-context.h Normal file
View file

@ -0,0 +1,68 @@
/* Determining the results of applying fix-it hints.
Copyright (C) 2016 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#ifndef GCC_EDIT_CONTEXT_H
#define GCC_EDIT_CONTEXT_H
#include "typed-splay-tree.h"
class edit_context;
class edited_file;
/* A set of changes to the source code.
The changes are "atomic" - if any changes can't be applied,
none of them can be (tracked by the m_valid flag).
Similarly, attempts to add the changes from a rich_location flagged
as containing invalid changes mean that the whole of the edit_context
is flagged as invalid.
A complication here is that fix-its are expressed relative to coordinates
in the files when they were parsed, before any changes have been made, and
so if there's more that one fix-it to be applied, we have to adjust
later fix-its to allow for the changes made by earlier ones. This
is done by the various "get_effective_column" methods. */
class edit_context
{
public:
edit_context ();
bool valid_p () const { return m_valid; }
void add_fixits (rich_location *richloc);
char *get_content (const char *filename);
int get_effective_column (const char *filename, int line, int column);
char *generate_diff (bool show_filenames);
void print_diff (pretty_printer *pp, bool show_filenames);
private:
bool apply_insert (const fixit_insert *insert);
bool apply_replace (const fixit_replace *replace);
edited_file *get_file (const char *filename);
edited_file &get_or_insert_file (const char *filename);
bool m_valid;
typed_splay_tree<const char *, edited_file *> m_files;
};
#endif /* GCC_EDIT_CONTEXT_H. */

View file

@ -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

View file

@ -41,6 +41,7 @@ extern bool is_location_from_builtin_token (source_location);
extern expanded_location expand_location (source_location);
extern const char *location_get_source_line (const char *file_path, int line,
int *line_size);
extern bool location_missing_trailing_newline (const char *file_path);
extern expanded_location expand_location_to_spelling_point (source_location);
extern source_location expansion_point_location_if_in_system_header (source_location);
extern source_location expansion_point_location (source_location);

View file

@ -68,6 +68,7 @@ selftest::run_tests ()
rely on. */
diagnostic_show_locus_c_tests ();
diagnostic_c_tests ();
edit_context_c_tests ();
fold_const_c_tests ();
spellcheck_c_tests ();
spellcheck_tree_c_tests ();

View file

@ -151,6 +151,7 @@ for_each_line_table_case (void (*testcase) (const line_table_case &));
extern void bitmap_c_tests ();
extern void diagnostic_c_tests ();
extern void diagnostic_show_locus_c_tests ();
extern void edit_context_c_tests ();
extern void et_forest_c_tests ();
extern void fold_const_c_tests ();
extern void fibonacci_heap_c_tests ();

View file

@ -1,3 +1,8 @@
2016-09-02 David Malcolm <dmalcolm@redhat.com>
* include/line-map.h (rich_location::seen_impossible_fixit_p): New
accessor.
2016-08-31 David Malcolm <dmalcolm@redhat.com>
* include/line-map.h (class fixit_remove): Remove stray decl.

View file

@ -1567,6 +1567,7 @@ class rich_location
unsigned int get_num_fixit_hints () const { return m_fixit_hints.count (); }
fixit_hint *get_fixit_hint (int idx) const { return m_fixit_hints[idx]; }
fixit_hint *get_last_fixit_hint () const;
bool seen_impossible_fixit_p () const { return m_seen_impossible_fixit; }
private:
bool reject_impossible_fixit (source_location where);