diff options
author | David Malcolm <dmalcolm@redhat.com> | 2015-11-20 20:08:47 +0000 |
---|---|---|
committer | David Malcolm <dmalcolm@gcc.gnu.org> | 2015-11-20 20:08:47 +0000 |
commit | a87a86e1e97610650fb4305edc76a8f4a399a46e (patch) | |
tree | f65f921f35a1b100513886cbde83b0f1b31e72d9 /gcc/testsuite | |
parent | 48a78aee68fe0d88f01fcdef61782b4f8008f651 (diff) | |
download | gcc-a87a86e1e97610650fb4305edc76a8f4a399a46e.zip gcc-a87a86e1e97610650fb4305edc76a8f4a399a46e.tar.gz gcc-a87a86e1e97610650fb4305edc76a8f4a399a46e.tar.bz2 |
PR 62314: add ability to add fixit-hints to a diagnostic
This is the combination of two patches:
[PATCH 01/02] PR/62314: add ability to add fixit-hints
[PATCH 02/02] C FE: add fix-it hint for . vs ->
gcc/ChangeLog:
PR 62314
* diagnostic-show-locus.c (colorizer::set_fixit_hint): New.
(class layout): Update comment
(layout::print_any_fixits): New method.
(layout::move_to_column): New method.
(diagnostic_show_locus): Add call to layout.print_any_fixits.
gcc/c/ChangeLog:
PR 62314
* c-typeck.c (should_suggest_deref_p): New function.
(build_component_ref): Special-case POINTER_TYPE when
generating a "not a structure of union" error message, and
suggest a "->" rather than a ".", providing a fix-it hint.
gcc/testsuite/ChangeLog:
PR 62314
* gcc.dg/fixits.c: New file.
* gcc.dg/plugin/diagnostic-test-show-locus-ascii-bw.c
(test_fixit_insert): New.
(test_fixit_remove): New.
(test_fixit_replace): New.
* gcc.dg/plugin/diagnostic-test-show-locus-ascii-color.c
(test_fixit_insert): New.
(test_fixit_remove): New.
(test_fixit_replace): New.
* gcc.dg/plugin/diagnostic_plugin_test_show_locus.c
(test_show_locus): Add tests of rendering fixit hints.
libcpp/ChangeLog:
PR 62314
* include/line-map.h (source_range::intersects_line_p): New
method.
(rich_location::~rich_location): New.
(rich_location::add_fixit_insert): New method.
(rich_location::add_fixit_remove): New method.
(rich_location::add_fixit_replace): New method.
(rich_location::get_num_fixit_hints): New accessor.
(rich_location::get_fixit_hint): New accessor.
(rich_location::MAX_FIXIT_HINTS): New constant.
(rich_location::m_num_fixit_hints): New field.
(rich_location::m_fixit_hints): New field.
(class fixit_hint): New class.
(class fixit_insert): New class.
(class fixit_remove): New class.
(class fixit_replace): New class.
* line-map.c (source_range::intersects_line_p): New method.
(rich_location::rich_location): Add initialization of
m_num_fixit_hints to both ctors.
(rich_location::~rich_location): New.
(rich_location::add_fixit_insert): New method.
(rich_location::add_fixit_remove): New method.
(rich_location::add_fixit_replace): New method.
(fixit_insert::fixit_insert): New.
(fixit_insert::~fixit_insert): New.
(fixit_insert::affects_line_p): New.
(fixit_remove::fixit_remove): New.
(fixit_remove::affects_line_p): New.
(fixit_replace::fixit_replace): New.
(fixit_replace::~fixit_replace): New.
(fixit_replace::affects_line_p): New.
From-SVN: r230674
Diffstat (limited to 'gcc/testsuite')
-rw-r--r-- | gcc/testsuite/ChangeLog | 15 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/fixits.c | 41 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw.c | 43 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-color.c | 43 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_show_locus.c | 35 |
5 files changed, 177 insertions, 0 deletions
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index de70aae..84659a55 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,18 @@ +2015-11-20 David Malcolm <dmalcolm@redhat.com> + + PR 62314 + * gcc.dg/fixits.c: New file. + * gcc.dg/plugin/diagnostic-test-show-locus-ascii-bw.c + (test_fixit_insert): New. + (test_fixit_remove): New. + (test_fixit_replace): New. + * gcc.dg/plugin/diagnostic-test-show-locus-ascii-color.c + (test_fixit_insert): New. + (test_fixit_remove): New. + (test_fixit_replace): New. + * gcc.dg/plugin/diagnostic_plugin_test_show_locus.c + (test_show_locus): Add tests of rendering fixit hints. + 2015-11-20 Jakub Jelinek <jakub@redhat.com> PR middle-end/68339 diff --git a/gcc/testsuite/gcc.dg/fixits.c b/gcc/testsuite/gcc.dg/fixits.c new file mode 100644 index 0000000..06c9995 --- /dev/null +++ b/gcc/testsuite/gcc.dg/fixits.c @@ -0,0 +1,41 @@ +/* { dg-do compile } */ +/* { dg-options "-fdiagnostics-show-caret" } */ + +struct foo { int x; }; +union u { int x; }; + +/* Verify that we issue a hint for "." used with a ptr to a struct. */ + +int test_1 (struct foo *ptr) +{ + return ptr.x; /* { dg-error "'ptr' is a pointer; did you mean to use '->'?" } */ +/* { dg-begin-multiline-output "" } + return ptr.x; + ^ + -> + { dg-end-multiline-output "" } */ +} + +/* Likewise for a ptr to a union. */ + +int test_2 (union u *ptr) +{ + return ptr.x; /* { dg-error "'ptr' is a pointer; did you mean to use '->'?" } */ +/* { dg-begin-multiline-output "" } + return ptr.x; + ^ + -> + { dg-end-multiline-output "" } */ +} + +/* Verify that we don't issue a hint for a ptr to something that isn't a + struct or union. */ + +int test_3 (void **ptr) +{ + return ptr.x; /* { dg-error "request for member 'x' in something not a structure or union" } */ +/* { dg-begin-multiline-output "" } + return ptr.x; + ^ + { dg-end-multiline-output "" } */ +} diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw.c b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw.c index a4b16da..44b47e0 100644 --- a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw.c +++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw.c @@ -147,3 +147,46 @@ void test_caret_on_leading_whitespace (void) { dg-end-multiline-output "" } */ #endif } + +/* Unit test for rendering of insertion fixit hints + (example taken from PR 62316). */ + +void test_fixit_insert (void) +{ +#if 0 + int a[2][2] = { 0, 1 , 2, 3 }; /* { dg-warning "insertion hints" } */ +/* { dg-begin-multiline-output "" } + int a[2][2] = { 0, 1 , 2, 3 }; + ^~~~ + { } + { dg-end-multiline-output "" } */ +#endif +} + +/* Unit test for rendering of "remove" fixit hints. */ + +void test_fixit_remove (void) +{ +#if 0 + int a;; /* { dg-warning "example of a removal hint" } */ +/* { dg-begin-multiline-output "" } + int a;; + ^ + - + { dg-end-multiline-output "" } */ +#endif +} + +/* Unit test for rendering of "replace" fixit hints. */ + +void test_fixit_replace (void) +{ +#if 0 + gtk_widget_showall (dlg); /* { dg-warning "example of a replacement hint" } */ +/* { dg-begin-multiline-output "" } + gtk_widget_showall (dlg); + ^~~~~~~~~~~~~~~~~~ + gtk_widget_show_all + { dg-end-multiline-output "" } */ +#endif +} diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-color.c b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-color.c index 47639b2..199e0b2 100644 --- a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-color.c +++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-color.c @@ -156,3 +156,46 @@ void test_caret_on_leading_whitespace (void) { dg-end-multiline-output "" } */ #endif } + +/* Unit test for rendering of insertion fixit hints + (example taken from PR 62316). */ + +void test_fixit_insert (void) +{ +#if 0 + int a[2][2] = { 0, 1 , 2, 3 }; /* { dg-warning "insertion hints" } */ +/* { dg-begin-multiline-output "" } + int a[2][2] = { [01;35m[K0, 1[m[K , 2, 3 }; + [01;35m[K^~~~ + {[m[K [01;35m[K}[m[K + { dg-end-multiline-output "" } */ +#endif +} + +/* Unit test for rendering of "remove" fixit hints. */ + +void test_fixit_remove (void) +{ +#if 0 + int a;; /* { dg-warning "example of a removal hint" } */ +/* { dg-begin-multiline-output "" } + int a;[01;35m[K;[m[K + [01;35m[K^ + -[m[K + { dg-end-multiline-output "" } */ +#endif +} + +/* Unit test for rendering of "replace" fixit hints. */ + +void test_fixit_replace (void) +{ +#if 0 + gtk_widget_showall (dlg); /* { dg-warning "example of a replacement hint" } */ +/* { dg-begin-multiline-output "" } + [01;35m[Kgtk_widget_showall[m[K (dlg); + [01;35m[K^~~~~~~~~~~~~~~~~~ + gtk_widget_show_all[m[K + { dg-end-multiline-output "" } */ +#endif +} diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_show_locus.c b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_show_locus.c index 158c612..7ff2cff 100644 --- a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_show_locus.c +++ b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_show_locus.c @@ -258,6 +258,41 @@ test_show_locus (function *fun) global_dc->caret_chars[1] = '^'; } + /* Tests of rendering fixit hints. */ + if (0 == strcmp (fnname, "test_fixit_insert")) + { + const int line = fnstart_line + 2; + source_range src_range; + src_range.m_start = get_loc (line, 19); + src_range.m_finish = get_loc (line, 22); + rich_location richloc (src_range); + richloc.add_fixit_insert (src_range.m_start, "{"); + richloc.add_fixit_insert (get_loc (line, 23), "}"); + warning_at_rich_loc (&richloc, 0, "example of insertion hints"); + } + + if (0 == strcmp (fnname, "test_fixit_remove")) + { + const int line = fnstart_line + 2; + source_range src_range; + src_range.m_start = get_loc (line, 8); + src_range.m_finish = get_loc (line, 8); + rich_location richloc (src_range); + richloc.add_fixit_remove (src_range); + warning_at_rich_loc (&richloc, 0, "example of a removal hint"); + } + + if (0 == strcmp (fnname, "test_fixit_replace")) + { + const int line = fnstart_line + 2; + source_range src_range; + src_range.m_start = get_loc (line, 2); + src_range.m_finish = get_loc (line, 19); + rich_location richloc (src_range); + richloc.add_fixit_replace (src_range, "gtk_widget_show_all"); + warning_at_rich_loc (&richloc, 0, "example of a replacement hint"); + } + /* Example of two carets where both carets appear to have an off-by-one error appearing one column early. Seen with gfortran.dg/associate_5.f03. |