diff mbox

C/C++ PATCH to fix ICEs with enumerators in attribute list (PR c++/79962, PR c++/79984)

Message ID 20170309222549.GG3172@redhat.com
State New
Headers show

Commit Message

Marek Polacek March 9, 2017, 10:25 p.m. UTC
We are crashing left and right with enumerators in attribute nonnull arguments.
Albeit handle_nonnull_attribute calls default_conversion to accept various
integer constants (default_conversion calls scalar_constant_value which can
turn CONST_DECLs into INTEGER_CSTs), it doesn't save the converted result to
the attribute list, so those CONST_DECLs will stay in the list.  But the code
later in the game assumes that the list only contains INTEGER_CSTs, which
causes trouble (e.g. get_nonnull_operand).

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

2017-03-09  Marek Polacek  <polacek@redhat.com>

	PR c++/79962
	PR c++/79984
	* c-attribs.c (handle_nonnull_attribute): Save the result of default
	conversion to the attribute list.

	* c-c++-common/nonnull-3.c: New test.
	* g++.dg/Wnonnull3.C: New test.


	Marek

Comments

Jason Merrill March 9, 2017, 10:35 p.m. UTC | #1
OK.

On Thu, Mar 9, 2017 at 5:25 PM, Marek Polacek <polacek@redhat.com> wrote:
> We are crashing left and right with enumerators in attribute nonnull arguments.
> Albeit handle_nonnull_attribute calls default_conversion to accept various
> integer constants (default_conversion calls scalar_constant_value which can
> turn CONST_DECLs into INTEGER_CSTs), it doesn't save the converted result to
> the attribute list, so those CONST_DECLs will stay in the list.  But the code
> later in the game assumes that the list only contains INTEGER_CSTs, which
> causes trouble (e.g. get_nonnull_operand).
>
> Bootstrapped/regtested on x86_64-linux, ok for trunk?
>
> 2017-03-09  Marek Polacek  <polacek@redhat.com>
>
>         PR c++/79962
>         PR c++/79984
>         * c-attribs.c (handle_nonnull_attribute): Save the result of default
>         conversion to the attribute list.
>
>         * c-c++-common/nonnull-3.c: New test.
>         * g++.dg/Wnonnull3.C: New test.
>
> diff --git gcc/c-family/c-attribs.c gcc/c-family/c-attribs.c
> index 8058a65..f2a88e1 100644
> --- gcc/c-family/c-attribs.c
> +++ gcc/c-family/c-attribs.c
> @@ -2813,7 +2813,7 @@ handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
>        tree arg = TREE_VALUE (args);
>        if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
>           && TREE_CODE (arg) != FUNCTION_DECL)
> -       arg = default_conversion (arg);
> +       TREE_VALUE (args) = arg = default_conversion (arg);
>
>        if (!get_nonnull_operand (arg, &arg_num))
>         {
> diff --git gcc/testsuite/c-c++-common/nonnull-3.c gcc/testsuite/c-c++-common/nonnull-3.c
> index e69de29..d2ccb24 100644
> --- gcc/testsuite/c-c++-common/nonnull-3.c
> +++ gcc/testsuite/c-c++-common/nonnull-3.c
> @@ -0,0 +1,11 @@
> +/* PR c++/79984 */
> +/* { dg-do compile } */
> +/* { dg-options "-Wnonnull-compare" } */
> +
> +enum { r = 1 };
> +
> +__attribute__ ((nonnull (r))) int
> +f (int *p)
> +{
> +  return p == 0; /* { dg-warning "nonnull argument 'p' compared to NULL" } */
> +}
> diff --git gcc/testsuite/g++.dg/Wnonnull3.C gcc/testsuite/g++.dg/Wnonnull3.C
> index e69de29..d1918ef 100644
> --- gcc/testsuite/g++.dg/Wnonnull3.C
> +++ gcc/testsuite/g++.dg/Wnonnull3.C
> @@ -0,0 +1,15 @@
> +// PR c++/79962
> +// { dg-options "-Wnonnull" }
> +
> +template <class T>
> +__attribute__ ((__nonnull__ (T::i))) void f (typename T::U) { }
> +
> +struct S1 { enum { i = 1 }; typedef void* U; };
> +struct S2 { static const int i = 1; typedef void* U; };
> +
> +void
> +g ()
> +{
> +  f<S1>(0); // { dg-warning "null argument where non-null required" }
> +  f<S2>(0); // { dg-warning "null argument where non-null required" }
> +}
>
>         Marek
Marek Polacek March 9, 2017, 10:40 p.m. UTC | #2
On Thu, Mar 09, 2017 at 11:25:49PM +0100, Marek Polacek wrote:
> We are crashing left and right with enumerators in attribute nonnull arguments.
> Albeit handle_nonnull_attribute calls default_conversion to accept various
> integer constants (default_conversion calls scalar_constant_value which can
> turn CONST_DECLs into INTEGER_CSTs), it doesn't save the converted result to
> the attribute list, so those CONST_DECLs will stay in the list.  But the code
> later in the game assumes that the list only contains INTEGER_CSTs, which
> causes trouble (e.g. get_nonnull_operand).
> 
> Bootstrapped/regtested on x86_64-linux, ok for trunk?
> 
> 2017-03-09  Marek Polacek  <polacek@redhat.com>
> 
> 	PR c++/79962
> 	PR c++/79984
> 	* c-attribs.c (handle_nonnull_attribute): Save the result of default
> 	conversion to the attribute list.
> 
> 	* c-c++-common/nonnull-3.c: New test.
> 	* g++.dg/Wnonnull3.C: New test.

And this was meant to go to g++.dg/warn/.  I'll fix it up before committing.

	Marek
diff mbox

Patch

diff --git gcc/c-family/c-attribs.c gcc/c-family/c-attribs.c
index 8058a65..f2a88e1 100644
--- gcc/c-family/c-attribs.c
+++ gcc/c-family/c-attribs.c
@@ -2813,7 +2813,7 @@  handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
       tree arg = TREE_VALUE (args);
       if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
 	  && TREE_CODE (arg) != FUNCTION_DECL)
-	arg = default_conversion (arg);
+	TREE_VALUE (args) = arg = default_conversion (arg);
 
       if (!get_nonnull_operand (arg, &arg_num))
 	{
diff --git gcc/testsuite/c-c++-common/nonnull-3.c gcc/testsuite/c-c++-common/nonnull-3.c
index e69de29..d2ccb24 100644
--- gcc/testsuite/c-c++-common/nonnull-3.c
+++ gcc/testsuite/c-c++-common/nonnull-3.c
@@ -0,0 +1,11 @@ 
+/* PR c++/79984 */
+/* { dg-do compile } */
+/* { dg-options "-Wnonnull-compare" } */
+
+enum { r = 1 };
+
+__attribute__ ((nonnull (r))) int
+f (int *p)
+{
+  return p == 0; /* { dg-warning "nonnull argument 'p' compared to NULL" } */
+}
diff --git gcc/testsuite/g++.dg/Wnonnull3.C gcc/testsuite/g++.dg/Wnonnull3.C
index e69de29..d1918ef 100644
--- gcc/testsuite/g++.dg/Wnonnull3.C
+++ gcc/testsuite/g++.dg/Wnonnull3.C
@@ -0,0 +1,15 @@ 
+// PR c++/79962
+// { dg-options "-Wnonnull" }
+
+template <class T>
+__attribute__ ((__nonnull__ (T::i))) void f (typename T::U) { }
+
+struct S1 { enum { i = 1 }; typedef void* U; };
+struct S2 { static const int i = 1; typedef void* U; };
+
+void
+g ()
+{
+  f<S1>(0); // { dg-warning "null argument where non-null required" }
+  f<S2>(0); // { dg-warning "null argument where non-null required" }
+}