public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* C PATCH to reject va_arg (ap, void) (PR c/65901)
@ 2015-04-27 17:30 Marek Polacek
  2015-04-27 17:56 ` Jeff Law
  2015-04-29  3:50 ` Martin Sebor
  0 siblings, 2 replies; 5+ messages in thread
From: Marek Polacek @ 2015-04-27 17:30 UTC (permalink / raw)
  To: GCC Patches, Joseph Myers

As reported in the PR, va_arg (ap, void) probably doesn't make any sense and
should be rejected thus.  cc1plus and clang reject va_arg with an incomplete
type.  This patch makes cc1 reject it as well.

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

2015-04-27  Marek Polacek  <polacek@redhat.com>

	PR c/65901
	* c-typeck.c (c_build_va_arg): Require TYPE be a complete type.

	* gcc.c-torture/compile/pr48767.c (foo): Add dg-error.
	* gcc.dg/pr65901.c: New test.

diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c
index 91735b5..c58e918 100644
--- gcc/c/c-typeck.c
+++ gcc/c/c-typeck.c
@@ -12648,6 +12648,11 @@ c_build_va_arg (location_t loc, tree expr, tree type)
   if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
     warning_at (loc, OPT_Wc___compat,
 		"C++ requires promoted type, not enum type, in %<va_arg%>");
+  if (type == error_mark_node || !COMPLETE_TYPE_P (type))
+    {
+      c_incomplete_type_error (NULL_TREE, type);
+      return error_mark_node;
+    }
   return build_va_arg (loc, expr, type);
 }
 
diff --git gcc/testsuite/gcc.c-torture/compile/pr48767.c gcc/testsuite/gcc.c-torture/compile/pr48767.c
index 66cb348..c8fef35 100644
--- gcc/testsuite/gcc.c-torture/compile/pr48767.c
+++ gcc/testsuite/gcc.c-torture/compile/pr48767.c
@@ -3,5 +3,5 @@
 void
 foo (__builtin_va_list ap)
 {
-  __builtin_va_arg (ap, void);
+  __builtin_va_arg (ap, void);  /* { dg-error "invalid use of void expression" } */
 }
diff --git gcc/testsuite/gcc.dg/pr65901.c gcc/testsuite/gcc.dg/pr65901.c
index e69de29..8708a1e 100644
--- gcc/testsuite/gcc.dg/pr65901.c
+++ gcc/testsuite/gcc.dg/pr65901.c
@@ -0,0 +1,16 @@
+/* PR c/65901 */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+struct S;
+enum E;
+union U;
+
+void
+foo (__builtin_va_list ap)
+{
+  __builtin_va_arg (ap, void);  /* { dg-error "invalid use of void expression" } */
+  __builtin_va_arg (ap, struct S);  /* { dg-error "invalid use of undefined type" } */
+  __builtin_va_arg (ap, enum E);  /* { dg-error "invalid use of undefined type" } */
+  __builtin_va_arg (ap, union U);  /* { dg-error "invalid use of undefined type" } */
+}

	Marek

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: C PATCH to reject va_arg (ap, void) (PR c/65901)
  2015-04-27 17:30 C PATCH to reject va_arg (ap, void) (PR c/65901) Marek Polacek
@ 2015-04-27 17:56 ` Jeff Law
  2015-04-29  3:50 ` Martin Sebor
  1 sibling, 0 replies; 5+ messages in thread
From: Jeff Law @ 2015-04-27 17:56 UTC (permalink / raw)
  To: Marek Polacek, GCC Patches, Joseph Myers

On 04/27/2015 11:30 AM, Marek Polacek wrote:
> As reported in the PR, va_arg (ap, void) probably doesn't make any sense and
> should be rejected thus.  cc1plus and clang reject va_arg with an incomplete
> type.  This patch makes cc1 reject it as well.
>
> Bootstrapped/regtested on x86_64-linux, ok for trunk?
>
> 2015-04-27  Marek Polacek  <polacek@redhat.com>
>
> 	PR c/65901
> 	* c-typeck.c (c_build_va_arg): Require TYPE be a complete type.
>
> 	* gcc.c-torture/compile/pr48767.c (foo): Add dg-error.
> 	* gcc.dg/pr65901.c: New test.
OK.
jeff

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: C PATCH to reject va_arg (ap, void) (PR c/65901)
  2015-04-27 17:30 C PATCH to reject va_arg (ap, void) (PR c/65901) Marek Polacek
  2015-04-27 17:56 ` Jeff Law
@ 2015-04-29  3:50 ` Martin Sebor
  2015-04-29 16:49   ` Marek Polacek
  1 sibling, 1 reply; 5+ messages in thread
From: Martin Sebor @ 2015-04-29  3:50 UTC (permalink / raw)
  To: Marek Polacek, GCC Patches, Joseph Myers

On 04/27/2015 11:30 AM, Marek Polacek wrote:
> As reported in the PR, va_arg (ap, void) probably doesn't make any sense and
> should be rejected thus.  cc1plus and clang reject va_arg with an incomplete
> type.  This patch makes cc1 reject it as well.
>
> Bootstrapped/regtested on x86_64-linux, ok for trunk?
>
> 2015-04-27  Marek Polacek  <polacek@redhat.com>
>
> 	PR c/65901
> 	* c-typeck.c (c_build_va_arg): Require TYPE be a complete type.
>
> 	* gcc.c-torture/compile/pr48767.c (foo): Add dg-error.
> 	* gcc.dg/pr65901.c: New test.
>
> diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c
> index 91735b5..c58e918 100644
> --- gcc/c/c-typeck.c
> +++ gcc/c/c-typeck.c
> @@ -12648,6 +12648,11 @@ c_build_va_arg (location_t loc, tree expr, tree type)
>     if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
>       warning_at (loc, OPT_Wc___compat,
>   		"C++ requires promoted type, not enum type, in %<va_arg%>");
> +  if (type == error_mark_node || !COMPLETE_TYPE_P (type))
> +    {
> +      c_incomplete_type_error (NULL_TREE, type);

The error message in the test cases below isn't quite right.
The type of the aggregates isn't undefined, it's incomplete.
Looking at the function, I wonder if the first argument
should be EXPR rather than than NULL_TREE? Alternatively,
experimenting with other cases where GCC diagnoses invalid
uses of incomplete type, I see that it issues:

   "invalid application of %qs to incomplete type %qT"

which might work even better here since we could name the
expression (va_arg).

Martin

> +      return error_mark_node;
> +    }
>     return build_va_arg (loc, expr, type);
>   }
>
> diff --git gcc/testsuite/gcc.c-torture/compile/pr48767.c gcc/testsuite/gcc.c-torture/compile/pr48767.c
> index 66cb348..c8fef35 100644
> --- gcc/testsuite/gcc.c-torture/compile/pr48767.c
> +++ gcc/testsuite/gcc.c-torture/compile/pr48767.c
> @@ -3,5 +3,5 @@
>   void
>   foo (__builtin_va_list ap)
>   {
> -  __builtin_va_arg (ap, void);
> +  __builtin_va_arg (ap, void);  /* { dg-error "invalid use of void expression" } */
>   }
> diff --git gcc/testsuite/gcc.dg/pr65901.c gcc/testsuite/gcc.dg/pr65901.c
> index e69de29..8708a1e 100644
> --- gcc/testsuite/gcc.dg/pr65901.c
> +++ gcc/testsuite/gcc.dg/pr65901.c
> @@ -0,0 +1,16 @@
> +/* PR c/65901 */
> +/* { dg-do compile } */
> +/* { dg-options "" } */
> +
> +struct S;
> +enum E;
> +union U;
> +
> +void
> +foo (__builtin_va_list ap)
> +{
> +  __builtin_va_arg (ap, void);  /* { dg-error "invalid use of void expression" } */
> +  __builtin_va_arg (ap, struct S);  /* { dg-error "invalid use of undefined type" } */
> +  __builtin_va_arg (ap, enum E);  /* { dg-error "invalid use of undefined type" } */
> +  __builtin_va_arg (ap, union U);  /* { dg-error "invalid use of undefined type" } */
> +}
>
> 	Marek
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: C PATCH to reject va_arg (ap, void) (PR c/65901)
  2015-04-29  3:50 ` Martin Sebor
@ 2015-04-29 16:49   ` Marek Polacek
  2015-04-30 14:17     ` Marek Polacek
  0 siblings, 1 reply; 5+ messages in thread
From: Marek Polacek @ 2015-04-29 16:49 UTC (permalink / raw)
  To: Martin Sebor; +Cc: GCC Patches, Joseph Myers

On Tue, Apr 28, 2015 at 09:07:09PM -0600, Martin Sebor wrote:
> The error message in the test cases below isn't quite right.
> The type of the aggregates isn't undefined, it's incomplete.
> Looking at the function, I wonder if the first argument
> should be EXPR rather than than NULL_TREE? Alternatively,
> experimenting with other cases where GCC diagnoses invalid
> uses of incomplete type, I see that it issues:
> 
>   "invalid application of %qs to incomplete type %qT"
> 
> which might work even better here since we could name the
> expression (va_arg).

Yeah, I haven't concerned myself with the exact wording of the error
message much, and I agree it could be improved.  But passing down the
EXPR would mean that the compiler outputs "'ap' has an incomplete type"
and that looks wrong as well.  I think I'm going to apply the following
tomorrow if I hear no objections (and it passes testing).  Thanks for noticing.

(And I think c_incomplete_type_error deserves some TLC; I'll post a separate
patch.)

2015-04-29  Marek Polacek  <polacek@redhat.com>

	* c-typeck.c (c_build_va_arg): Clarify the error message.

	* gcc.dg/pr65901.c (foo): Adjust dg-error.

diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c
index c58e918..028d2f81 100644
--- gcc/c/c-typeck.c
+++ gcc/c/c-typeck.c
@@ -12645,14 +12645,17 @@ c_build_qualified_type (tree type, int type_quals)
 tree
 c_build_va_arg (location_t loc, tree expr, tree type)
 {
-  if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
-    warning_at (loc, OPT_Wc___compat,
-		"C++ requires promoted type, not enum type, in %<va_arg%>");
-  if (type == error_mark_node || !COMPLETE_TYPE_P (type))
+  if (error_operand_p (type))
+    return error_mark_node;
+  else if (!COMPLETE_TYPE_P (type))
     {
-      c_incomplete_type_error (NULL_TREE, type);
+      error_at (loc, "second argument to %<va_arg%> is of incomplete "
+		"type %qT", type);
       return error_mark_node;
     }
+  else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
+    warning_at (loc, OPT_Wc___compat,
+		"C++ requires promoted type, not enum type, in %<va_arg%>");
   return build_va_arg (loc, expr, type);
 }
 
diff --git gcc/testsuite/gcc.dg/pr65901.c gcc/testsuite/gcc.dg/pr65901.c
index 8708a1e..b40eea3 100644
--- gcc/testsuite/gcc.dg/pr65901.c
+++ gcc/testsuite/gcc.dg/pr65901.c
@@ -9,8 +9,8 @@ union U;
 void
 foo (__builtin_va_list ap)
 {
-  __builtin_va_arg (ap, void);  /* { dg-error "invalid use of void expression" } */
-  __builtin_va_arg (ap, struct S);  /* { dg-error "invalid use of undefined type" } */
-  __builtin_va_arg (ap, enum E);  /* { dg-error "invalid use of undefined type" } */
-  __builtin_va_arg (ap, union U);  /* { dg-error "invalid use of undefined type" } */
+  __builtin_va_arg (ap, void);  /* { dg-error "second argument to .va_arg. is of incomplete type .void." } */
+  __builtin_va_arg (ap, struct S);  /* { dg-error "second argument to .va_arg. is of incomplete type .struct S." } */
+  __builtin_va_arg (ap, enum E);  /* { dg-error "second argument to .va_arg. is of incomplete type .enum E." } */
+  __builtin_va_arg (ap, union U);  /* { dg-error "second argument to .va_arg. is of incomplete type .union U." } */
 }

	Marek

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: C PATCH to reject va_arg (ap, void) (PR c/65901)
  2015-04-29 16:49   ` Marek Polacek
@ 2015-04-30 14:17     ` Marek Polacek
  0 siblings, 0 replies; 5+ messages in thread
From: Marek Polacek @ 2015-04-30 14:17 UTC (permalink / raw)
  To: Martin Sebor; +Cc: GCC Patches, Joseph Myers

On Wed, Apr 29, 2015 at 06:41:22PM +0200, Marek Polacek wrote:
> On Tue, Apr 28, 2015 at 09:07:09PM -0600, Martin Sebor wrote:
> > The error message in the test cases below isn't quite right.
> > The type of the aggregates isn't undefined, it's incomplete.
> > Looking at the function, I wonder if the first argument
> > should be EXPR rather than than NULL_TREE? Alternatively,
> > experimenting with other cases where GCC diagnoses invalid
> > uses of incomplete type, I see that it issues:
> > 
> >   "invalid application of %qs to incomplete type %qT"
> > 
> > which might work even better here since we could name the
> > expression (va_arg).
> 
> Yeah, I haven't concerned myself with the exact wording of the error
> message much, and I agree it could be improved.  But passing down the
> EXPR would mean that the compiler outputs "'ap' has an incomplete type"
> and that looks wrong as well.  I think I'm going to apply the following
> tomorrow if I hear no objections (and it passes testing).  Thanks for noticing.

Committed now.
 
> (And I think c_incomplete_type_error deserves some TLC; I'll post a separate
> patch.)
> 
> 2015-04-29  Marek Polacek  <polacek@redhat.com>
> 
> 	* c-typeck.c (c_build_va_arg): Clarify the error message.
> 
> 	* gcc.dg/pr65901.c (foo): Adjust dg-error.
> 
> diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c
> index c58e918..028d2f81 100644
> --- gcc/c/c-typeck.c
> +++ gcc/c/c-typeck.c
> @@ -12645,14 +12645,17 @@ c_build_qualified_type (tree type, int type_quals)
>  tree
>  c_build_va_arg (location_t loc, tree expr, tree type)
>  {
> -  if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
> -    warning_at (loc, OPT_Wc___compat,
> -		"C++ requires promoted type, not enum type, in %<va_arg%>");
> -  if (type == error_mark_node || !COMPLETE_TYPE_P (type))
> +  if (error_operand_p (type))
> +    return error_mark_node;
> +  else if (!COMPLETE_TYPE_P (type))
>      {
> -      c_incomplete_type_error (NULL_TREE, type);
> +      error_at (loc, "second argument to %<va_arg%> is of incomplete "
> +		"type %qT", type);
>        return error_mark_node;
>      }
> +  else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
> +    warning_at (loc, OPT_Wc___compat,
> +		"C++ requires promoted type, not enum type, in %<va_arg%>");
>    return build_va_arg (loc, expr, type);
>  }
>  
> diff --git gcc/testsuite/gcc.dg/pr65901.c gcc/testsuite/gcc.dg/pr65901.c
> index 8708a1e..b40eea3 100644
> --- gcc/testsuite/gcc.dg/pr65901.c
> +++ gcc/testsuite/gcc.dg/pr65901.c
> @@ -9,8 +9,8 @@ union U;
>  void
>  foo (__builtin_va_list ap)
>  {
> -  __builtin_va_arg (ap, void);  /* { dg-error "invalid use of void expression" } */
> -  __builtin_va_arg (ap, struct S);  /* { dg-error "invalid use of undefined type" } */
> -  __builtin_va_arg (ap, enum E);  /* { dg-error "invalid use of undefined type" } */
> -  __builtin_va_arg (ap, union U);  /* { dg-error "invalid use of undefined type" } */
> +  __builtin_va_arg (ap, void);  /* { dg-error "second argument to .va_arg. is of incomplete type .void." } */
> +  __builtin_va_arg (ap, struct S);  /* { dg-error "second argument to .va_arg. is of incomplete type .struct S." } */
> +  __builtin_va_arg (ap, enum E);  /* { dg-error "second argument to .va_arg. is of incomplete type .enum E." } */
> +  __builtin_va_arg (ap, union U);  /* { dg-error "second argument to .va_arg. is of incomplete type .union U." } */
>  }

	Marek

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-04-30 13:37 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-27 17:30 C PATCH to reject va_arg (ap, void) (PR c/65901) Marek Polacek
2015-04-27 17:56 ` Jeff Law
2015-04-29  3:50 ` Martin Sebor
2015-04-29 16:49   ` Marek Polacek
2015-04-30 14:17     ` Marek Polacek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).