The attached tweak enables the -Wvla-parameter warning to also recognize
the [static] notation for VLAs, the same way it does ordinary arrays.
Tested on x86_64-linux and pushed as obvious.

Martin
commit 757ba6653c2699761c2243e0194749a6695112d8
Author: Martin Sebor <mse...@redhat.com>
Date:   Fri Oct 23 12:37:38 2020 -0600

    PR middle-end/97552 - missing waning passing null to a VLA argument declared [static]
    
    gcc/ChangeLog:
    
            PR middle-end/97552
            * attribs.c (init_attr_rdwr_indices): Handle static VLA parameters.
    
    gcc/c/ChangeLog:
    
            PR middle-end/97552
            * c-decl.c (get_parm_array_spec): Handle static VLA parameters.
    
    gcc/testsuite/ChangeLog:
    
            PR middle-end/97552
            * gcc.dg/Wvla-parameter-2.c: Adjust text of expected warning.
            * gcc.dg/Wnonnull-5.c: New test.

diff --git a/gcc/attribs.c b/gcc/attribs.c
index 3bdb2ffda81..a6f6b70e39e 100644
--- a/gcc/attribs.c
+++ b/gcc/attribs.c
@@ -2109,6 +2109,7 @@ init_attr_rdwr_indices (rdwr_map *rwm, tree attrs)
 		     is followed by a comma and a dollar sign its bound is
 		     on the list.  Otherwise it's a VLA with an unspecified
 		     bound.  */
+		  acc.static_p = p[-2] == 's';
 		  acc.minsize = HOST_WIDE_INT_M1U;
 		}
 
diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c
index 81b9adb6a33..1673b958555 100644
--- a/gcc/c/c-decl.c
+++ b/gcc/c/c-decl.c
@@ -5784,6 +5784,9 @@ get_parm_array_spec (const struct c_parm *parm, tree attrs)
 	  continue;
 	}
 
+      if (pd->u.array.static_p)
+	spec += 's';
+
       if (TREE_CODE (nelts) == INTEGER_CST)
 	{
 	  /* Skip all constant bounds except the most significant one.
@@ -5796,9 +5799,8 @@ get_parm_array_spec (const struct c_parm *parm, tree attrs)
 	    return attrs;
 
 	  char buf[40];
-	  const char *code = pd->u.array.static_p ? "s" : "";
 	  unsigned HOST_WIDE_INT n = tree_to_uhwi (nelts);
-	  sprintf (buf, "%s%llu", code, (unsigned long long)n);
+	  sprintf (buf, "%llu", (unsigned long long)n);
 	  spec += buf;
 	  break;
 	}
diff --git a/gcc/testsuite/gcc.dg/Wnonnull-5.c b/gcc/testsuite/gcc.dg/Wnonnull-5.c
new file mode 100644
index 00000000000..ef6ed54c3f3
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wnonnull-5.c
@@ -0,0 +1,53 @@
+/* PR middle-end/97552 - missing waning passing null to a VLA argument
+   declared [static]
+   { dg-do compile }
+   { dg-options "-Wall" } */
+
+#define A(...) __attribute__ ((__VA_ARGS__))
+
+void             fptr_array (int(*)[0]);
+
+void             fstatic_array (int[static 0]);
+void A (nonnull) fnonnull_static_array (int [static 0]);
+
+void             fvla (int n, int [n]);
+void A (nonnull) fnonnull_vla (int n, int [n]);
+
+void             fstatic_vla (int n, int [static n]);
+void A (nonnull) fnonnull_static_vla (int n, int [static n]);
+
+
+void test_null (void)
+{
+  fptr_array (0);
+  fptr_array (&(int[0]){ });
+
+  fstatic_array (0);                // { dg-warning "\\\[-Wnonnull" }
+  fnonnull_static_array (0);        // { dg-warning "\\\[-Wnonnull" }
+
+  fvla (0, 0);
+  fnonnull_vla (0, 0);              // { dg-warning "\\\[-Wnonnull" }
+
+  fstatic_vla (0, 0);               // { dg-warning "\\\[-Wnonnull" }
+  fnonnull_static_vla (0, 0);       // { dg-warning "\\\[-Wnonnull" }
+}
+
+
+#pragma GCC optimize ("1")
+
+void test_null_optimized (void)
+{
+  int (*pa)[0] = 0;
+  fptr_array (pa);
+
+  int *p = 0;
+
+  fstatic_array (p);                // { dg-warning "\\\[-Wnonnull" }
+  fnonnull_static_array (p);        // { dg-warning "\\\[-Wnonnull" }
+
+  fvla (0, p);
+  fnonnull_vla (0, p);              // { dg-warning "\\\[-Wnonnull" }
+
+  fstatic_vla (0, p);               // { dg-warning "\\\[-Wnonnull" }
+  fnonnull_static_vla (0, p);       // { dg-warning "\\\[-Wnonnull" }
+}
diff --git a/gcc/testsuite/gcc.dg/Wvla-parameter-2.c b/gcc/testsuite/gcc.dg/Wvla-parameter-2.c
index ba9324143b4..01728e7ebb7 100644
--- a/gcc/testsuite/gcc.dg/Wvla-parameter-2.c
+++ b/gcc/testsuite/gcc.dg/Wvla-parameter-2.c
@@ -67,9 +67,9 @@ void a2pampan (int (*(*(*[2])[n1])[n2]));
 int f2ia1_1 (int n, int [n][n]);            // { sg-message "previously declared as 'int\\\[n]\\\[n]' with bound argument 1" }
 int f2ia1_1 (int n, int[static n][n]);
 int f2ia1_1 (int n, int a[static n][n]) { return sizeof *a; }
-int f2ia1_1 (int n, int[static n + 1][n]);  // { dg-warning "argument 2 of type 'int\\\[n \\\+ 1]\\\[n]' declared with mismatched bound 'n \\\+ 1'" }
+int f2ia1_1 (int n, int[static n + 1][n]);  // { dg-warning "argument 2 of type 'int\\\[static  *n \\\+ 1]\\\[n]' declared with mismatched bound 'n \\\+ 1'" }
 
-int f2ias1_1 (int n, int [static n][n]);    // { dg-message "previously declared as 'int\\\[n]\\\[n]' with bound argument 1" }
+int f2ias1_1 (int n, int [static n][n]);    // { dg-message "previously declared as 'int\\\[static +n]\\\[n]' with bound argument 1" }
 int f2ias1_1 (int n, int[n][n]);
 int f2ias1_1 (int n, int a[++n][n])         // { dg-warning "argument 2 of type 'int\\\[\\\+\\\+n]\\\[n]' declared with mismatched bound ' ?\\+\\+n'" }
 { return sizeof *a; }

Reply via email to