c: Avoid -Wenum-int-mismatch warning for redeclaration of builtin acc_on_device [PR107041]

Message ID ZD+uPQ0SZiYDfScT@tucnak
State Unresolved
Headers
Series c: Avoid -Wenum-int-mismatch warning for redeclaration of builtin acc_on_device [PR107041] |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Jakub Jelinek April 19, 2023, 9:02 a.m. UTC
  Hi!

The new -Wenum-int-mismatch warning triggers with -Wsystem-headers in
<openacc.h>, for obvious reasons the builtin acc_on_device uses int
type argument rather than enum which isn't defined yet when the builtin
is created, while the OpenACC spec requires it to have acc_device_t
enum argument.  The header makes sure it has int underlying type by using
negative and __INT_MAX__ enumerators.

I've tried to make the builtin typegeneric or just varargs, but that
changes behavior e.g. when one calls it with some C++ class which has
cast operator to acc_device_t, so the following patch instead disables
the warning for this builtin.

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

2023-04-19  Jakub Jelinek  <jakub@redhat.com>

	PR c/107041
	* c-decl.cc (diagnose_mismatched_decls): Avoid -Wenum-int-mismatch
	warning on acc_on_device declaration.

	* gcc.dg/goacc/pr107041.c: New test.


	Jakub
  

Comments

Marek Polacek April 20, 2023, 4:48 p.m. UTC | #1
On Wed, Apr 19, 2023 at 11:02:53AM +0200, Jakub Jelinek wrote:
> Hi!
> 
> The new -Wenum-int-mismatch warning triggers with -Wsystem-headers in
> <openacc.h>, for obvious reasons the builtin acc_on_device uses int
> type argument rather than enum which isn't defined yet when the builtin
> is created, while the OpenACC spec requires it to have acc_device_t
> enum argument.  The header makes sure it has int underlying type by using
> negative and __INT_MAX__ enumerators.
> 
> I've tried to make the builtin typegeneric or just varargs, but that
> changes behavior e.g. when one calls it with some C++ class which has
> cast operator to acc_device_t, so the following patch instead disables
> the warning for this builtin.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk
> and 13.2?
> 
> 2023-04-19  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR c/107041
> 	* c-decl.cc (diagnose_mismatched_decls): Avoid -Wenum-int-mismatch
> 	warning on acc_on_device declaration.
> 
> 	* gcc.dg/goacc/pr107041.c: New test.
> 
> --- gcc/c/c-decl.cc.jj	2023-03-10 10:10:17.918387120 +0100
> +++ gcc/c/c-decl.cc	2023-04-18 10:29:33.340793562 +0200
> @@ -2219,7 +2219,14 @@ diagnose_mismatched_decls (tree newdecl,
>      }
>    /* Warn about enum/integer type mismatches.  They are compatible types
>       (C2X 6.7.2.2/5), but may pose portability problems.  */
> -  else if (enum_and_int_p && TREE_CODE (newdecl) != TYPE_DECL)
> +  else if (enum_and_int_p
> +	   && TREE_CODE (newdecl) != TYPE_DECL
> +	   /* Don't warn about about acc_on_device builtin redeclaration,

"built-in"

> +	      the builtin is declared with int rather than enum because

"built-in"

> +	      the enum isn't intrinsic.  */
> +	   && !(TREE_CODE (olddecl) == FUNCTION_DECL
> +		&& fndecl_built_in_p (olddecl, BUILT_IN_ACC_ON_DEVICE)
> +		&& !C_DECL_DECLARED_BUILTIN (olddecl)))

What do you think about adding an (UN)LIKELY here?  This seems a rather
very special case.  On the other hand we're not on a hot path here so it
hardly matters.

OK either way, thanks.

>      warned = warning_at (DECL_SOURCE_LOCATION (newdecl),
>  			 OPT_Wenum_int_mismatch,
>  			 "conflicting types for %q+D due to enum/integer "
> --- gcc/testsuite/gcc.dg/goacc/pr107041.c.jj	2023-04-18 10:18:07.039754258 +0200
> +++ gcc/testsuite/gcc.dg/goacc/pr107041.c	2023-04-18 10:17:21.252418797 +0200
> @@ -0,0 +1,23 @@
> +/* PR c/107041 */
> +/* { dg-do compile } */
> +/* { dg-additional-options "-Wenum-int-mismatch" } */
> +
> +typedef enum acc_device_t {
> +  acc_device_current = -1,
> +  acc_device_none = 0,
> +  acc_device_default = 1,
> +  acc_device_host = 2,
> +  acc_device_not_host = 4,
> +  acc_device_nvidia = 5,
> +  acc_device_radeon = 8,
> +  _ACC_highest = __INT_MAX__
> +} acc_device_t;
> +
> +int acc_on_device (acc_device_t);		/* { dg-bogus "conflicting types for 'acc_on_device' due to enum/integer mismatch; have 'int\\\(acc_device_t\\\)'" } */
> +int acc_on_device (acc_device_t);
> +
> +int
> +foo (void)
> +{
> +  return acc_on_device (acc_device_host);
> +}
> 
> 	Jakub
> 

Marek
  
Jakub Jelinek April 20, 2023, 5:24 p.m. UTC | #2
On Thu, Apr 20, 2023 at 12:48:57PM -0400, Marek Polacek wrote:
> > -  else if (enum_and_int_p && TREE_CODE (newdecl) != TYPE_DECL)
> > +  else if (enum_and_int_p
> > +	   && TREE_CODE (newdecl) != TYPE_DECL
> > +	   /* Don't warn about about acc_on_device builtin redeclaration,
> 
> "built-in"
> 
> > +	      the builtin is declared with int rather than enum because
> 
> "built-in"

Changing.
> 
> > +	      the enum isn't intrinsic.  */
> > +	   && !(TREE_CODE (olddecl) == FUNCTION_DECL
> > +		&& fndecl_built_in_p (olddecl, BUILT_IN_ACC_ON_DEVICE)
> > +		&& !C_DECL_DECLARED_BUILTIN (olddecl)))
> 
> What do you think about adding an (UN)LIKELY here?  This seems a rather
> very special case.  On the other hand we're not on a hot path here so it
> hardly matters.

If anything, I'd add it either as UNLIKELY (enum_and_int_p) because that
whole thing is unlikely, or add UNLIKELY (flag_openacc) && before this
acc_on_device stuff (but then users of -fopenacc might complain that it is
likely for them).

	Jakub
  
Marek Polacek April 20, 2023, 5:28 p.m. UTC | #3
On Thu, Apr 20, 2023 at 07:24:29PM +0200, Jakub Jelinek wrote:
> On Thu, Apr 20, 2023 at 12:48:57PM -0400, Marek Polacek wrote:
> > > -  else if (enum_and_int_p && TREE_CODE (newdecl) != TYPE_DECL)
> > > +  else if (enum_and_int_p
> > > +	   && TREE_CODE (newdecl) != TYPE_DECL
> > > +	   /* Don't warn about about acc_on_device builtin redeclaration,
> > 
> > "built-in"
> > 
> > > +	      the builtin is declared with int rather than enum because
> > 
> > "built-in"
> 
> Changing.
> > 
> > > +	      the enum isn't intrinsic.  */
> > > +	   && !(TREE_CODE (olddecl) == FUNCTION_DECL
> > > +		&& fndecl_built_in_p (olddecl, BUILT_IN_ACC_ON_DEVICE)
> > > +		&& !C_DECL_DECLARED_BUILTIN (olddecl)))
> > 
> > What do you think about adding an (UN)LIKELY here?  This seems a rather
> > very special case.  On the other hand we're not on a hot path here so it
> > hardly matters.
> 
> If anything, I'd add it either as UNLIKELY (enum_and_int_p) because that
> whole thing is unlikely,

Might could as well.

> or add UNLIKELY (flag_openacc) && before this
> acc_on_device stuff (but then users of -fopenacc might complain that it is
> likely for them).

Ok.

Marek
  

Patch

--- gcc/c/c-decl.cc.jj	2023-03-10 10:10:17.918387120 +0100
+++ gcc/c/c-decl.cc	2023-04-18 10:29:33.340793562 +0200
@@ -2219,7 +2219,14 @@  diagnose_mismatched_decls (tree newdecl,
     }
   /* Warn about enum/integer type mismatches.  They are compatible types
      (C2X 6.7.2.2/5), but may pose portability problems.  */
-  else if (enum_and_int_p && TREE_CODE (newdecl) != TYPE_DECL)
+  else if (enum_and_int_p
+	   && TREE_CODE (newdecl) != TYPE_DECL
+	   /* Don't warn about about acc_on_device builtin redeclaration,
+	      the builtin is declared with int rather than enum because
+	      the enum isn't intrinsic.  */
+	   && !(TREE_CODE (olddecl) == FUNCTION_DECL
+		&& fndecl_built_in_p (olddecl, BUILT_IN_ACC_ON_DEVICE)
+		&& !C_DECL_DECLARED_BUILTIN (olddecl)))
     warned = warning_at (DECL_SOURCE_LOCATION (newdecl),
 			 OPT_Wenum_int_mismatch,
 			 "conflicting types for %q+D due to enum/integer "
--- gcc/testsuite/gcc.dg/goacc/pr107041.c.jj	2023-04-18 10:18:07.039754258 +0200
+++ gcc/testsuite/gcc.dg/goacc/pr107041.c	2023-04-18 10:17:21.252418797 +0200
@@ -0,0 +1,23 @@ 
+/* PR c/107041 */
+/* { dg-do compile } */
+/* { dg-additional-options "-Wenum-int-mismatch" } */
+
+typedef enum acc_device_t {
+  acc_device_current = -1,
+  acc_device_none = 0,
+  acc_device_default = 1,
+  acc_device_host = 2,
+  acc_device_not_host = 4,
+  acc_device_nvidia = 5,
+  acc_device_radeon = 8,
+  _ACC_highest = __INT_MAX__
+} acc_device_t;
+
+int acc_on_device (acc_device_t);		/* { dg-bogus "conflicting types for 'acc_on_device' due to enum/integer mismatch; have 'int\\\(acc_device_t\\\)'" } */
+int acc_on_device (acc_device_t);
+
+int
+foo (void)
+{
+  return acc_on_device (acc_device_host);
+}