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]

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


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


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