public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: David Malcolm <dmalcolm@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: David Malcolm <dmalcolm@redhat.com>
Subject: [PATCH 3/4] PR c++/62314: C++: add fixit hint to misspelled member names
Date: Thu, 28 Apr 2016 14:03:00 -0000	[thread overview]
Message-ID: <1461853698-43954-3-git-send-email-dmalcolm@redhat.com> (raw)
In-Reply-To: <1461853698-43954-1-git-send-email-dmalcolm@redhat.com>

When we emit a hint about a misspelled member name, it will slightly
aid readability if we use a fixit-hint to show the proposed
name in context within the source code (and in the future this
might support some kind of auto-apply in an IDE).

This patch adds such a hint to the C++ frontend, taking us from:

test.cc:10:15: error: 'struct foo' has no member named 'colour'; did you mean 'color'?
   return ptr->colour;
               ^~~~~~

to:

test.cc:10:15: error: 'struct foo' has no member named 'colour'; did you mean 'color'?
   return ptr->colour;
               ^~~~~~
               color

Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu.

OK for trunk?

gcc/cp/ChangeLog:
	PR c++/62314
	* typeck.c (finish_class_member_access_expr): When
	giving a hint about a possibly-misspelled member name,
	add a fix-it replacement hint.

gcc/testsuite/ChangeLog:
	PR c++/62314
	* g++.dg/spellcheck-fields-2.C: New test case.
---
 gcc/cp/typeck.c                            | 18 +++++++++++++++---
 gcc/testsuite/g++.dg/spellcheck-fields-2.C | 19 +++++++++++++++++++
 2 files changed, 34 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/spellcheck-fields-2.C

diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index 7e12009..95c777d 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -2817,9 +2817,21 @@ finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
 		  tree guessed_id = lookup_member_fuzzy (access_path, name,
 							 /*want_type=*/false);
 		  if (guessed_id)
-		    error ("%q#T has no member named %qE; did you mean %qE?",
-			   TREE_CODE (access_path) == TREE_BINFO
-			   ? TREE_TYPE (access_path) : object_type, name, guessed_id);
+		    {
+		      location_t bogus_component_loc = input_location;
+		      rich_location rich_loc (line_table, bogus_component_loc);
+		      source_range bogus_component_range =
+			get_range_from_loc (line_table, bogus_component_loc);
+		      rich_loc.add_fixit_replace
+			(bogus_component_range,
+			 IDENTIFIER_POINTER (guessed_id));
+		      error_at_rich_loc
+			(&rich_loc,
+			 "%q#T has no member named %qE; did you mean %qE?",
+			 TREE_CODE (access_path) == TREE_BINFO
+			 ? TREE_TYPE (access_path) : object_type, name,
+			 guessed_id);
+		    }
 		  else
 		    error ("%q#T has no member named %qE",
 			   TREE_CODE (access_path) == TREE_BINFO
diff --git a/gcc/testsuite/g++.dg/spellcheck-fields-2.C b/gcc/testsuite/g++.dg/spellcheck-fields-2.C
new file mode 100644
index 0000000..eb10b44
--- /dev/null
+++ b/gcc/testsuite/g++.dg/spellcheck-fields-2.C
@@ -0,0 +1,19 @@
+// { dg-options "-fdiagnostics-show-caret" }
+
+union u
+{
+  int color;
+  int shape;
+};
+
+int test (union u *ptr)
+{
+  return ptr->colour; // { dg-error "did you mean .color.?" }
+}
+
+// Verify that we get an underline and a fixit hint.
+/* { dg-begin-multiline-output "" }
+   return ptr->colour;
+               ^~~~~~
+               color
+   { dg-end-multiline-output "" } */
-- 
1.8.5.3

  parent reply	other threads:[~2016-04-28 14:03 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-28 14:04 [PATCH 1/4] PR c++/62314: add fixit hint for missing "template <> " in explicit specialization David Malcolm
2016-04-28 14:03 ` [PATCH 2/4] PR c++/62314: add fixit hint for "expected ';' after class definition" David Malcolm
2016-05-02 10:40   ` Bernd Schmidt
2016-07-01 17:40     ` David Malcolm
2016-07-04 10:15       ` Bernd Schmidt
2016-04-28 14:03 ` David Malcolm [this message]
2016-04-28 14:04 ` [PATCH 4/4] C: add fixit hint to misspelled field names David Malcolm
2016-05-31 23:37   ` David Malcolm
2016-06-06 15:17     ` Joseph Myers
2016-06-10 17:17       ` [PATCH] C: fixits for misspelled named initializers David Malcolm
2016-06-10 21:54         ` Joseph Myers
2016-04-28 14:31 ` [PATCH 1/4] PR c++/62314: add fixit hint for missing "template <> " in explicit specialization Trevor Saunders
2016-04-29 21:29   ` Jason Merrill
  -- strict thread matches above, loose matches on Subject: below --
2015-12-18 21:47 David Malcolm
2015-12-18 21:47 ` [PATCH 3/4] PR c++/62314: C++: add fixit hint to misspelled member names David Malcolm

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1461853698-43954-3-git-send-email-dmalcolm@redhat.com \
    --to=dmalcolm@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).