gcc/libcpp
David Malcolm ad53f12355 Support fix-it hints that add new lines
Previously fix-it hints couldn't contain newlines.  This is
due to the need to print something user-readable for them
within diagnostic-show-locus, and for handling them within
edit-context for printing diffs and regenerating content.

This patch enables limited support for fix-it hints with newlines,
for suggesting adding new lines.
Such a fix-it hint must have exactly one newline character, at the
end of the content.  It must be an insertion at the beginning of
a line (so that e.g. fix-its that split a pre-existing line are
still rejected).

They are printed by diagnostic-show-locus with a '+' in the
left-hand margin, like this:

test.c:42:4: note: suggest adding 'break;' here
+      break;
     case 'b':
     ^~~~~~~~~

and the printer injects "spans" if the insertion location is not
near the primary range of the diagnostic e.g.:

test.c:4:2: note: unrecognized 'putchar'; suggest including '<stdio.h>'
test.c:1:1:
+#include <stdio.h>

test.c:4:2:
  putchar (ch);
  ^~~~~~~

gcc/ChangeLog:
	* diagnostic-show-locus.c
	(layout::should_print_annotation_line_p): Make private.
	(layout::print_annotation_line): Make private.
	(layout::annotation_line_showed_range_p): Make private.
	(layout::show_ruler): Make private.
	(layout::print_source_line): Make private.  Pass in line and
	line_width, rather than calling location_get_source_line.  Drop
	returned value.
	(layout::print_leading_fixits): New method.
	(layout::print_any_fixits): Rename to...
	(layout::print_trailing_fixits): ...this, and make private.
	Don't print newline fixits.
	(diagnostic_show_locus): Move logic for printing one row into...
	(layout::print_line): ...this new function.  Move the
	location_get_source_line call and error-handling from
	print_source_line to here.  Call print_leading_fixits, and rename
	print_any_fixits to print_trailing_fixits.
	(selftest::test_fixit_insert_containing_newline): Update now that
	newlines are partially supported.
	(selftest::test_fixit_insert_containing_newline_2): New test.
	(selftest::test_fixit_replace_containing_newline): Update comments.
	(selftest::diagnostic_show_locus_c_tests): Call the new test.
	* edit-context.c (class added_line): New class.
	(class edited_line): Describe newline handling in comment.
	(edited_line::actually_edited_p): New method.
	(edited_line::print_content): Delete redundant decl.
	(edited_line::m_predecessors): New field.
	(edited_file::print_content): Call edited_line::print_content.
	(edited_file::print_diff): Update to support newlines.
	(edited_file::print_diff_hunk): Likewise.
	(edited_file::print_run_of_changed_lines): New function.
	(edited_file::print_diff_line): Convert to...
	(print_diff_line): ...this.
	(edited_file::get_effective_line_count): New function.
	(edited_line::edited_line): Initialize new field m_predecessors.
	(edited_line::~edited_line): Clean up m_predecessors.
	(edited_line::apply_fixit): Handle newlines.
	(edited_line::get_effective_line_count): New function.
	(edited_line::print_content): New function.
	(edited_line::print_diff_lines): New function.
	(selftest::test_applying_fixits_insert_containing_newline): New
	test.
	(selftest::test_applying_fixits_replace_containing_newline): New
	test.
	(selftest::insert_line): New function.
	(selftest::test_applying_fixits_multiple_lines): Add example of
	inserting a line.
	(selftest::edit_context_c_tests): Call the new tests.

gcc/testsuite/ChangeLog:
	* gcc.dg/plugin/diagnostic-test-show-locus-bw.c
	(test_fixit_insert_newline): New function.
	* gcc.dg/plugin/diagnostic-test-show-locus-color.c
	(test_fixit_insert_newline): New function.
	* gcc.dg/plugin/diagnostic-test-show-locus-generate-patch.c
	(test_fixit_insert_newline): New function.
	* gcc.dg/plugin/diagnostic-test-show-locus-parseable-fixits.c
	(test_fixit_insert_newline): New function.
	* gcc.dg/plugin/diagnostic_plugin_test_show_locus.c
	(test_show_locus): Handle test_fixit_insert_newline.

libcpp/ChangeLog:
	* include/line-map.h (class rich_location): Update description of
	newline handling.
	(class fixit_hint): Likewise.
	(fixit_hint::ends_with_newline_p): New decl.
	* line-map.c (rich_location::maybe_add_fixit): Support newlines
	in fix-it hints that are insertions of single lines at the start
	of a line.  Don't consolidate into such fix-it hints.
	(fixit_hint::ends_with_newline_p): New method.

From-SVN: r247522
2017-05-02 19:03:56 +00:00
..
include Support fix-it hints that add new lines 2017-05-02 19:03:56 +00:00
po Regenerate .pot files. 2017-05-01 23:28:02 +01:00
ChangeLog Support fix-it hints that add new lines 2017-05-02 19:03:56 +00:00
ChangeLog.jit
Makefile.in * Makefile.in (po/$(PACKAGE).pot): Adjust bug reporting URL. 2017-02-09 08:55:46 +00:00
aclocal.m4
charset.c Update copyright years. 2017-01-01 13:07:43 +01:00
config.in re PR bootstrap/72823 (r239175 causes build failure) 2016-11-16 21:10:27 +01:00
configure re PR bootstrap/72823 (r239175 causes build failure) 2016-11-16 21:10:27 +01:00
configure.ac re PR bootstrap/72823 (r239175 causes build failure) 2016-11-16 21:10:27 +01:00
directives-only.c Update copyright years. 2017-01-01 13:07:43 +01:00
directives.c Update copyright years. 2017-01-01 13:07:43 +01:00
errors.c Update copyright years. 2017-01-01 13:07:43 +01:00
expr.c Update copyright years. 2017-01-01 13:07:43 +01:00
files.c Update copyright years. 2017-01-01 13:07:43 +01:00
identifiers.c Update copyright years. 2017-01-01 13:07:43 +01:00
init.c * init.c (cpp_init_builtins): Update __cplusplus for C++17. 2017-03-16 17:16:39 -04:00
internal.h Update copyright years. 2017-01-01 13:07:43 +01:00
lex.c Fix numerous typos in comments 2017-04-03 23:30:56 +01:00
line-map.c Support fix-it hints that add new lines 2017-05-02 19:03:56 +00:00
location-example.txt
macro.c Update copyright years. 2017-01-01 13:07:43 +01:00
makeucnid.c Update copyright years. 2017-01-01 13:07:43 +01:00
mkdeps.c Update copyright years. 2017-01-01 13:07:43 +01:00
pch.c Fix numerous typos in comments 2017-04-03 23:30:56 +01:00
symtab.c Update copyright years. 2017-01-01 13:07:43 +01:00
system.h Update copyright years. 2017-01-01 13:07:43 +01:00
traditional.c Update copyright years. 2017-01-01 13:07:43 +01:00
ucnid.h Update copyright years. 2017-01-01 13:07:43 +01:00
ucnid.tab Update copyright years. 2017-01-01 13:07:43 +01:00