]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c: add name hints to c_parser_declspecs [PR107583]
authorDavid Malcolm <dmalcolm@redhat.com>
Fri, 16 Jun 2023 00:56:41 +0000 (20:56 -0400)
committerDavid Malcolm <dmalcolm@redhat.com>
Fri, 16 Jun 2023 00:56:41 +0000 (20:56 -0400)
PR c/107583 notes that we weren't issuing a hint for

  struct foo {
    time_t mytime; /* missing <time.h> include should trigger fixit */
  };

in the C frontend.

The root cause is that one of the "unknown type name" diagnostics
was missing logic to emit hints, which this patch fixes.

gcc/c/ChangeLog:
PR c/107583
* c-parser.cc (c_parser_declspecs): Add hints to "unknown type
name" error.

gcc/testsuite/ChangeLog:
PR c/107583
* c-c++-common/spellcheck-pr107583.c: New test.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
gcc/c/c-parser.cc
gcc/testsuite/c-c++-common/spellcheck-pr107583.c [new file with mode: 0644]

index 61487cc7481b16d5610bfad365b64952efdb1050..24a6eb6e4596f32c477e3f1c3f98b9792f7bc92c 100644 (file)
@@ -3182,7 +3182,19 @@ c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
          attrs_ok = true;
          if (kind == C_ID_ID)
            {
-             error_at (loc, "unknown type name %qE", value);
+             auto_diagnostic_group d;
+             name_hint hint = lookup_name_fuzzy (value, FUZZY_LOOKUP_TYPENAME,
+                                                 loc);
+             if (const char *suggestion = hint.suggestion ())
+               {
+                 gcc_rich_location richloc (loc);
+                 richloc.add_fixit_replace (suggestion);
+                 error_at (&richloc,
+                           "unknown type name %qE; did you mean %qs?",
+                           value, suggestion);
+               }
+             else
+               error_at (loc, "unknown type name %qE", value);
              t.kind = ctsk_typedef;
              t.spec = error_mark_node;
            }
diff --git a/gcc/testsuite/c-c++-common/spellcheck-pr107583.c b/gcc/testsuite/c-c++-common/spellcheck-pr107583.c
new file mode 100644 (file)
index 0000000..86a9e7d
--- /dev/null
@@ -0,0 +1,10 @@
+struct s1 {
+  time_t mytime; /* { dg-error "unknown type name 'time_t'" "c error" { target c } } */
+  /* { dg-error "'time_t' does not name a type" "c++ error" { target c++ } .-1 } */
+  /* { dg-message "'time_t' is defined in header" "hint" { target *-*-* } .-2 } */
+};
+
+struct s2 {
+  unsinged i; /* { dg-error "unknown type name 'unsinged'; did you mean 'unsigned'." "c error" { target c } } */
+  /* { dg-error "'unsinged' does not name a type; did you mean 'unsigned'." "c++ error" { target c++ } .-1 } */
+};