This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [C PATCH] Better diagnostics for missing struct/union (PR c/67580)


On 09/15/2015 10:52 AM, Marek Polacek wrote:
Currently, when one writes a code that is missing the struct/union/enum
tag, we get less than ideal error message.  The following patch improves
the situation a bit by giving a hint to the user.

I can't count the number of times I've made this mistake and
scratched my head wondering what I did wrong. This is going
to help a lot!

FWIW, I would like it even more if there was just one message
instead of an error and a note. Since (when) the type is known,
could the error simply say:

  error_at (here, "must use the %<struct%> keyword to refer to type %qE",
            name);

Martin


I couldn't use lookup_tag as that gives the "defined as wrong kind of tag"
error; instead I wrote a new predicate.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2015-09-15  Marek Polacek  <polacek@redhat.com>

	PR c/67580
	* c-decl.c (tag_exists_p): New function.
	* c-parser.c (c_parser_declaration_or_fndef): Give a hint when
	struct/union/enum keywords are missing.
	* c-tree.h (tag_exists_p): Declare.

	* gcc.dg/pr67580.c: New test.

diff --git gcc/c/c-decl.c gcc/c/c-decl.c
index 989ff99..a110226 100644
--- gcc/c/c-decl.c
+++ gcc/c/c-decl.c
@@ -3856,6 +3856,18 @@ lookup_tag (enum tree_code code, tree name, bool thislevel_only,
    return b->decl;
  }

+/* Return true if a definition exists for NAME with code CODE.  */
+
+bool
+tag_exists_p (enum tree_code code, tree name)
+{
+  struct c_binding *b = I_TAG_BINDING (name);
+
+  if (b == NULL || b->decl == NULL_TREE)
+    return false;
+  return TREE_CODE (b->decl) == code;
+}
+
  /* Print an error message now
     for a recent invalid struct, union or enum cross reference.
     We don't print them immediately because they are not invalid
diff --git gcc/c/c-parser.c gcc/c/c-parser.c
index 4d9cbe0..d5de102 100644
--- gcc/c/c-parser.c
+++ gcc/c/c-parser.c
@@ -1539,8 +1539,16 @@ c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
            || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
        && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
      {
-      error_at (here, "unknown type name %qE",
-                c_parser_peek_token (parser)->value);
+      tree name = c_parser_peek_token (parser)->value;
+      error_at (here, "unknown type name %qE", name);
+      /* Give a hint to the user.  This is not C++ with its implicit
+	 typedef.  */
+      if (tag_exists_p (RECORD_TYPE, name))
+	inform (here, "use %<struct%> keyword to refer to the type");
+      else if (tag_exists_p (UNION_TYPE, name))
+	inform (here, "use %<union%> keyword to refer to the type");
+      else if (tag_exists_p (ENUMERAL_TYPE, name))
+	inform (here, "use %<enum%> keyword to refer to the type");

        /* Parse declspecs normally to get a correct pointer type, but avoid
           a further "fails to be a type name" error.  Refuse nested functions
diff --git gcc/c/c-tree.h gcc/c/c-tree.h
index a3979dd..667529a 100644
--- gcc/c/c-tree.h
+++ gcc/c/c-tree.h
@@ -701,6 +701,7 @@ extern tree c_omp_reduction_lookup (tree, tree);
  extern tree c_check_omp_declare_reduction_r (tree *, int *, void *);
  extern void c_pushtag (location_t, tree, tree);
  extern void c_bind (location_t, tree, bool);
+extern bool tag_exists_p (enum tree_code, tree);

  /* In c-errors.c */
  extern void pedwarn_c90 (location_t, int opt, const char *, ...)
diff --git gcc/testsuite/gcc.dg/pr67580.c gcc/testsuite/gcc.dg/pr67580.c
index e69de29..90e4b1b 100644
--- gcc/testsuite/gcc.dg/pr67580.c
+++ gcc/testsuite/gcc.dg/pr67580.c
@@ -0,0 +1,31 @@
+/* PR c/67580 */
+/* { dg-do compile } */
+
+struct S { int s; };
+union U { int s; };
+enum E { A };
+
+void
+f (void)
+{
+  S s; /* { dg-error "unknown type name" } */
+/* { dg-message "use .struct. keyword to refer to the type" "" { target *-*-* } 11 } */
+  U u; /* { dg-error "unknown type name" } */
+/* { dg-message "use .union. keyword to refer to the type" "" { target *-*-* } 13 } */
+  E e; /* { dg-error "unknown type name" } */
+/* { dg-message "use .enum. keyword to refer to the type" "" { target *-*-* } 15 } */
+}
+
+void
+g (void)
+{
+  struct T { int i; };
+  union V { int i; };
+  enum F { J };
+  T t; /* { dg-error "unknown type name" } */
+/* { dg-message "use .struct. keyword to refer to the type" "" { target *-*-* } 25 } */
+  V v; /* { dg-error "unknown type name" } */
+/* { dg-message "use .union. keyword to refer to the type" "" { target *-*-* } 27 } */
+  F f; /* { dg-error "unknown type name" } */
+/* { dg-message "use .enum. keyword to refer to the type" "" { target *-*-* } 29 } */
+}

	Marek



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]