PR c++/62314: C++: add fixit hint to misspelled member names
authorDavid Malcolm <dmalcolm@redhat.com>
Mon, 2 May 2016 19:09:30 +0000 (19:09 +0000)
committerDavid Malcolm <dmalcolm@gcc.gnu.org>
Mon, 2 May 2016 19:09:30 +0000 (19:09 +0000)
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

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.

From-SVN: r235785

gcc/cp/ChangeLog
gcc/cp/typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/spellcheck-fields-2.C [new file with mode: 0644]

index 30dddeb765ce821410a562b5aa30c82f1c79707c..990ba0fb719fad746f3af60ebc8c125e47d0ff0d 100644 (file)
@@ -1,3 +1,10 @@
+2016-05-02  David Malcolm  <dmalcolm@redhat.com>
+
+       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.
+
 2016-05-02  Cesar Philippidis  <cesar@codesourcery.com>
 
        * cp-tree.h (finish_omp_clauses): Update prototype.
index 7e12009a5e5fe0da53dfa1e3af6550579f0b158b..95c777d7721ceca690f10d167563ca51de97fe7e 100644 (file)
@@ -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
index f8baf2285a13e1f0f311dd149a800eed293d4fc4..4bd9f88af496000eb729ebc9dc116399b248cecc 100644 (file)
@@ -1,3 +1,8 @@
+2016-05-02  David Malcolm  <dmalcolm@redhat.com>
+
+       PR c++/62314
+       * g++.dg/spellcheck-fields-2.C: New test case.
+
 2016-05-02  David Malcolm  <dmalcolm@redhat.com>
 
        PR c++/62314
diff --git a/gcc/testsuite/g++.dg/spellcheck-fields-2.C b/gcc/testsuite/g++.dg/spellcheck-fields-2.C
new file mode 100644 (file)
index 0000000..eb10b44
--- /dev/null
@@ -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 "" } */