diff mbox series

[C] Avoid aka types that just add tags

Message ID mptsgod9b6k.fsf@arm.com
State New
Headers show
Series [C] Avoid aka types that just add tags | expand

Commit Message

Richard Sandiford Sept. 30, 2019, 1:16 p.m. UTC
diag-aka-1.c tests that:

  struct T { int i; } T;
  void *a;
  T *t = a;

produces:

  request for implicit conversion from 'void *' to 'T *' {aka 'struct T *'} ...

But printing an aka for the tag seems a bit redundant when the tag name
is the same as the typedef name.  It's probably not going to be telling
the user anything they don't already know, and can be distracting if "T"
rather than "struct T" is the preferred choice for an exported interface.
This is even more true if the tag is anonymous; e.g.:

  struct { int i; } T;
  void *a;
  T *t = a;

gives:

  request for implicit conversion from 'void *' to 'T *' {aka 'struct <anonymous> *'}

[FWIW, clang also drops the aka for the errors in the test, except for
the function pointer ones.]

Rather than just drop the test above, the patch instead tests for:

  struct T { int i; } *T;

where seeing the tag definitely helps.

Tested on aarch64-linux-gnu and x86_64-linux-gnu.  OK to install?

Richard


2019-09-30  Richard Sandiford  <richard.sandiford@arm.com>

gcc/c/
	* c-objc-common.c (useful_aka_type_p): New function.
	(print_type): Use it to decide whether an aka type is worth printing.

gcc/testsuite/
	* gcc.dg/diag-aka-1.c (T): Turn into a pointer typedef.
	(foo): Update accordingly.
	* gcc.dg/diag-aka-4.c: New test.

Comments

Joseph Myers Sept. 30, 2019, 9:47 p.m. UTC | #1
On Mon, 30 Sep 2019, Richard Sandiford wrote:

> 2019-09-30  Richard Sandiford  <richard.sandiford@arm.com>
> 
> gcc/c/
> 	* c-objc-common.c (useful_aka_type_p): New function.
> 	(print_type): Use it to decide whether an aka type is worth printing.
> 
> gcc/testsuite/
> 	* gcc.dg/diag-aka-1.c (T): Turn into a pointer typedef.
> 	(foo): Update accordingly.
> 	* gcc.dg/diag-aka-4.c: New test.

OK.
diff mbox series

Patch

Index: gcc/c/c-objc-common.c
===================================================================
--- gcc/c/c-objc-common.c	2019-09-30 13:54:16.000000000 +0100
+++ gcc/c/c-objc-common.c	2019-09-30 14:08:26.597671509 +0100
@@ -62,6 +62,73 @@  c_objc_common_init (void)
   return c_common_init ();
 }
 
+/* Return true if it's worth saying that TYPE1 is also known as TYPE2.  */
+
+static bool
+useful_aka_type_p (tree type1, tree type2)
+{
+  if (type1 == type2)
+    return false;
+
+  if (type1 == error_mark_node || type2 == error_mark_node)
+    return false;
+
+  if (TREE_CODE (type1) != TREE_CODE (type2))
+    return true;
+
+  if (typedef_variant_p (type1))
+    {
+      /* Saying that "foo" is also known as "struct foo" or
+	 "struct <anonymous>" is unlikely to be useful, since users of
+	 structure-like types would already know that they're structures.
+	 The same applies to unions and enums; in general, printing the
+	 tag is only useful if it has a different name.  */
+      tree_code code = TREE_CODE (type2);
+      tree id2 = TYPE_IDENTIFIER (type2);
+      if ((code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
+	  && (!id2 || TYPE_IDENTIFIER (type1) == id2))
+	return false;
+
+      return true;
+    }
+  else
+    {
+      switch (TREE_CODE (type1))
+	{
+	case POINTER_TYPE:
+	case REFERENCE_TYPE:
+	  return useful_aka_type_p (TREE_TYPE (type1), TREE_TYPE (type2));
+
+	case ARRAY_TYPE:
+	  return (useful_aka_type_p (TYPE_DOMAIN (type1), TYPE_DOMAIN (type2))
+		  || useful_aka_type_p (TREE_TYPE (type1), TREE_TYPE (type2)));
+
+	case FUNCTION_TYPE:
+	  {
+	    tree args1 = TYPE_ARG_TYPES (type1);
+	    tree args2 = TYPE_ARG_TYPES (type2);
+	    while (args1 != args2)
+	      {
+		/* Although this shouldn't happen, it seems to wrong to assert
+		   for it in a diagnostic routine.  */
+		if (!args1 || args1 == void_type_node)
+		  return true;
+		if (!args2 || args2 == void_type_node)
+		  return true;
+		if (useful_aka_type_p (TREE_VALUE (args1), TREE_VALUE (args2)))
+		  return true;
+		args1 = TREE_CHAIN (args1);
+		args2 = TREE_CHAIN (args2);
+	      }
+	    return useful_aka_type_p (TREE_TYPE (type1), TREE_TYPE (type2));
+	  }
+
+	default:
+	  return true;
+	}
+    }
+}
+
 /* Print T to CPP.  */
 
 static void
@@ -83,7 +150,7 @@  print_type (c_pretty_printer *cpp, tree
      stripped version.  But sometimes the stripped version looks
      exactly the same, so we don't want it after all.  To avoid
      printing it in that case, we play ugly obstack games.  */
-  if (TYPE_CANONICAL (t) && t != TYPE_CANONICAL (t))
+  if (TYPE_CANONICAL (t) && useful_aka_type_p (t, TYPE_CANONICAL (t)))
     {
       c_pretty_printer cpp2;
       /* Print the stripped version into a temporary printer.  */
Index: gcc/testsuite/gcc.dg/diag-aka-1.c
===================================================================
--- gcc/testsuite/gcc.dg/diag-aka-1.c	2019-03-08 18:15:07.100852863 +0000
+++ gcc/testsuite/gcc.dg/diag-aka-1.c	2019-09-30 14:08:26.597671509 +0100
@@ -2,7 +2,7 @@ 
 /* { dg-options "-Wc++-compat" } */
 
 typedef struct A { int i; } B;
-typedef struct T { int i; } T;
+typedef struct T { int i; } *T; /* { dg-warning "using 'T' as both a typedef and a tag is invalid" } */
 typedef const float TFA;
 typedef TFA TFB;
 typedef TFB TFC;
@@ -24,6 +24,6 @@  bar (B *b, int *i)
 int
 foo (void *a)
 {
-  T *t = a; /* { dg-warning "request for implicit conversion from 'void \\*' to 'T \\*' {aka 'struct T \\*'} not" } */
+  T t = a; /* { dg-warning "request for implicit conversion from 'void \\*' to 'T' {aka 'struct T \\*'} not" } */
   return t->i;
 }
Index: gcc/testsuite/gcc.dg/diag-aka-4.c
===================================================================
--- /dev/null	2019-09-17 11:41:18.176664108 +0100
+++ gcc/testsuite/gcc.dg/diag-aka-4.c	2019-09-30 14:08:26.601671481 +0100
@@ -0,0 +1,72 @@ 
+typedef struct struct_wrapper { int i; } struct_wrapper;
+typedef struct { int i; } anon_struct_wrapper;
+
+typedef union union_wrapper { int i; } union_wrapper;
+typedef union { int i; } anon_union_wrapper;
+
+typedef enum enum_wrapper { A, B } enum_wrapper;
+typedef enum { C, D } anon_enum_wrapper;
+
+void test_struct_wrapper (struct_wrapper y, int x)
+{
+  struct_wrapper *ptr = &x; /* { dg-error {initialization of 'struct_wrapper \*' from incompatible pointer type 'int \*'} } */
+  const struct_wrapper *const_ptr = &x; /* { dg-error {initialization of 'const struct_wrapper \*' from incompatible pointer type 'int \*'} } */
+  volatile struct_wrapper *volatile_ptr = &x; /* { dg-error {initialization of 'volatile struct_wrapper \*' from incompatible pointer type 'int \*'} } */
+  struct_wrapper (*aptr)[10] = &x; /* { dg-error {initialization of 'struct_wrapper \(\*\)\[10\]' from incompatible pointer type 'int \*'} } */
+  struct_wrapper (*f1)(int) = &x; /* { dg-error {initialization of 'struct_wrapper \(\*\)\(int\)' from incompatible pointer type 'int \*'} } */
+  int (*f2)(struct_wrapper) = &x; /* { dg-error {initialization of 'int \(\*\)\(struct_wrapper\)' from incompatible pointer type 'int \*'} } */
+  y = x; /* { dg-error {incompatible types when assigning to type 'struct_wrapper' from type 'int'} } */
+}
+
+void test_anon_struct_wrapper (anon_struct_wrapper y, int x)
+{
+  anon_struct_wrapper *ptr = &x; /* { dg-error {initialization of 'anon_struct_wrapper \*' from incompatible pointer type 'int \*'} } */
+  const anon_struct_wrapper *const_ptr = &x; /* { dg-error {initialization of 'const anon_struct_wrapper \*' from incompatible pointer type 'int \*'} } */
+  volatile anon_struct_wrapper *volatile_ptr = &x; /* { dg-error {initialization of 'volatile anon_struct_wrapper \*' from incompatible pointer type 'int \*'} } */
+  anon_struct_wrapper (*aptr)[10] = &x; /* { dg-error {initialization of 'anon_struct_wrapper \(\*\)\[10\]' from incompatible pointer type 'int \*'} } */
+  anon_struct_wrapper (*f1)(int) = &x; /* { dg-error {initialization of 'anon_struct_wrapper \(\*\)\(int\)' from incompatible pointer type 'int \*'} } */
+  int (*f2)(anon_struct_wrapper) = &x; /* { dg-error {initialization of 'int \(\*\)\(anon_struct_wrapper\)' from incompatible pointer type 'int \*'} } */
+  y = x; /* { dg-error {incompatible types when assigning to type 'anon_struct_wrapper' from type 'int'} } */
+}
+
+void test_union_wrapper (union_wrapper y, int x)
+{
+  union_wrapper *ptr = &x; /* { dg-error {initialization of 'union_wrapper \*' from incompatible pointer type 'int \*'} } */
+  const union_wrapper *const_ptr = &x; /* { dg-error {initialization of 'const union_wrapper \*' from incompatible pointer type 'int \*'} } */
+  volatile union_wrapper *volatile_ptr = &x; /* { dg-error {initialization of 'volatile union_wrapper \*' from incompatible pointer type 'int \*'} } */
+  union_wrapper (*aptr)[10] = &x; /* { dg-error {initialization of 'union_wrapper \(\*\)\[10\]' from incompatible pointer type 'int \*'} } */
+  union_wrapper (*f1)(int) = &x; /* { dg-error {initialization of 'union_wrapper \(\*\)\(int\)' from incompatible pointer type 'int \*'} } */
+  int (*f2)(union_wrapper) = &x; /* { dg-error {initialization of 'int \(\*\)\(union_wrapper\)' from incompatible pointer type 'int \*'} } */
+  y = x; /* { dg-error {incompatible types when assigning to type 'union_wrapper' from type 'int'} } */
+}
+
+void test_anon_union_wrapper (anon_union_wrapper y, int x)
+{
+  anon_union_wrapper *ptr = &x; /* { dg-error {initialization of 'anon_union_wrapper \*' from incompatible pointer type 'int \*'} } */
+  const anon_union_wrapper *const_ptr = &x; /* { dg-error {initialization of 'const anon_union_wrapper \*' from incompatible pointer type 'int \*'} } */
+  volatile anon_union_wrapper *volatile_ptr = &x; /* { dg-error {initialization of 'volatile anon_union_wrapper \*' from incompatible pointer type 'int \*'} } */
+  anon_union_wrapper (*aptr)[10] = &x; /* { dg-error {initialization of 'anon_union_wrapper \(\*\)\[10\]' from incompatible pointer type 'int \*'} } */
+  anon_union_wrapper (*f1)(int) = &x; /* { dg-error {initialization of 'anon_union_wrapper \(\*\)\(int\)' from incompatible pointer type 'int \*'} } */
+  int (*f2)(anon_union_wrapper) = &x; /* { dg-error {initialization of 'int \(\*\)\(anon_union_wrapper\)' from incompatible pointer type 'int \*'} } */
+  y = x; /* { dg-error {incompatible types when assigning to type 'anon_union_wrapper' from type 'int'} } */
+}
+
+void test_enum_wrapper (enum_wrapper y, int x)
+{
+  enum_wrapper *ptr = &x; /* { dg-error {initialization of 'enum_wrapper \*' from incompatible pointer type 'int \*'} } */
+  const enum_wrapper *const_ptr = &x; /* { dg-error {initialization of 'const enum_wrapper \*' from incompatible pointer type 'int \*'} } */
+  volatile enum_wrapper *volatile_ptr = &x; /* { dg-error {initialization of 'volatile enum_wrapper \*' from incompatible pointer type 'int \*'} } */
+  enum_wrapper (*aptr)[10] = &x; /* { dg-error {initialization of 'enum_wrapper \(\*\)\[10\]' from incompatible pointer type 'int \*'} } */
+  enum_wrapper (*f1)(int) = &x; /* { dg-error {initialization of 'enum_wrapper \(\*\)\(int\)' from incompatible pointer type 'int \*'} } */
+  int (*f2)(enum_wrapper) = &x; /* { dg-error {initialization of 'int \(\*\)\(enum_wrapper\)' from incompatible pointer type 'int \*'} } */
+}
+
+void test_anon_enum_wrapper (anon_enum_wrapper y, int x)
+{
+  anon_enum_wrapper *ptr = &x; /* { dg-error {initialization of 'anon_enum_wrapper \*' from incompatible pointer type 'int \*'} } */
+  const anon_enum_wrapper *const_ptr = &x; /* { dg-error {initialization of 'const anon_enum_wrapper \*' from incompatible pointer type 'int \*'} } */
+  volatile anon_enum_wrapper *volatile_ptr = &x; /* { dg-error {initialization of 'volatile anon_enum_wrapper \*' from incompatible pointer type 'int \*'} } */
+  anon_enum_wrapper (*aptr)[10] = &x; /* { dg-error {initialization of 'anon_enum_wrapper \(\*\)\[10\]' from incompatible pointer type 'int \*'} } */
+  anon_enum_wrapper (*f1)(int) = &x; /* { dg-error {initialization of 'anon_enum_wrapper \(\*\)\(int\)' from incompatible pointer type 'int \*'} } */
+  int (*f2)(anon_enum_wrapper) = &x; /* { dg-error {initialization of 'int \(\*\)\(anon_enum_wrapper\)' from incompatible pointer type 'int \*'} } */
+}