As a GNU extension. I'll propose it for standardization, but first we'd need to make array parameters meaningful in the standard.
This extension will allow using _Countof() in more places, reducing the chances of specifying an incorrect size accidentally. If an array parameter has a forward declaration, it is unspecified which of the lengths is taken. It is generally unwise to specify the length in the forward declarations, and it is even more unwise to specify it more than once. Currently, the implementation takes the length from the actual declaration, and if it doesn't specify the length, it won't compile. In the future, we want to improve this so that the length from a forward declaration is taken if the actual declaration lacks one. But that's more complex, so let's keep it as is for now. Link: <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121271> gcc/ChangeLog: * doc/extend.texi (_Countof): Document this extension. gcc/c/ChangeLog: * c-typeck.cc (c_array_parm_orig_type): New function. (c_expr_countof_expr): Add support for array parameters. gcc/testsuite/ChangeLog: * gcc.dg/countof-compile.c (incomplete): Remove test. (param): Adapt test. * gcc.dg/countof-param-compile.c: New test. * gcc.dg/countof-param-pedantic.c: New test. * gcc.dg/countof-param.c: New test. Co-authored-by: Martin Uecker <[email protected]> Signed-off-by: Alejandro Colomar <[email protected]> --- gcc/c/c-typeck.cc | 31 ++++++ gcc/doc/extend.texi | 11 ++ gcc/testsuite/gcc.dg/countof-compile.c | 8 +- gcc/testsuite/gcc.dg/countof-param-compile.c | 104 ++++++++++++++++++ gcc/testsuite/gcc.dg/countof-param-pedantic.c | 11 ++ gcc/testsuite/gcc.dg/countof-param.c | 25 +++++ 6 files changed, 183 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/countof-param-compile.c create mode 100644 gcc/testsuite/gcc.dg/countof-param-pedantic.c create mode 100644 gcc/testsuite/gcc.dg/countof-param.c diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 61abace966da..b624b804ffd1 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -4142,6 +4142,26 @@ top_array_vla_p (const_tree type) || TREE_CODE (TYPE_MIN_VALUE (d)) != INTEGER_CST; } + +tree +c_array_parm_orig_type (tree expr) +{ + gcc_assert (POINTER_TYPE_P (TREE_TYPE (expr))); + + if (TREE_CODE (expr) == PARM_DECL + && C_ARRAY_PARAMETER (expr)) + { + if (tree attr = lookup_attribute ("arg spec", DECL_ATTRIBUTES (expr))) + { + tree arg = TREE_CHAIN (TREE_VALUE (attr)); + if (arg) + return TREE_VALUE (arg); + } + } + + return NULL_TREE; +} + /* Return the result of countof applied to EXPR. */ struct c_expr @@ -4168,6 +4188,17 @@ c_expr_countof_expr (location_t loc, struct c_expr expr) tree folded_expr = c_fully_fold (expr.value, require_constant_value, &expr_const_operands); tree type = TREE_TYPE (folded_expr); + if (POINTER_TYPE_P (type)) + { + if (tree t = c_array_parm_orig_type (expr.value)) + { + pedwarn (input_location, OPT_Wpedantic, + "ISO C does not support array parameters in %qs", + "_Countof"); + type = t; + } + } + ret.value = c_countof_type (loc, type); if (top_array_vla_p (type)) diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 961fa27ba0eb..1016b474565a 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -14377,6 +14377,17 @@ _Countof (int [7][n++]); // integer constant expression _Countof (int [n++][7]); // run-time value; n++ is evaluated @end smallexample +It also works with array parameters. +For example: + +@smallexample +void +f (int n, int a[n]) +@{ + assert(n == _Countof(a)); +@} +@end smallexample + @node _Maxof and _Minof @subsection The maximum and minimum representable values of a type @findex _Maxof diff --git a/gcc/testsuite/gcc.dg/countof-compile.c b/gcc/testsuite/gcc.dg/countof-compile.c index ebbac16ed38d..d992dd8de0ee 100644 --- a/gcc/testsuite/gcc.dg/countof-compile.c +++ b/gcc/testsuite/gcc.dg/countof-compile.c @@ -21,10 +21,6 @@ void incomplete (int p[]) { _Countof (x); /* { dg-error "incomplete" } */ - - /* We want to support array parameters in the future, - which should change this from "invalid" to "incomplete". */ - _Countof (p); /* { dg-error "invalid" } */ } void @@ -41,9 +37,7 @@ fam (void) void param (int n, int p[n]) { - /* We want to support array parameters in the future, - which would make this work. */ - _Countof (p); /* { dg-error "invalid" } */ + _Countof (p); /* { dg-error "ISO C does not support array parameters" } */ } void fix_fix (int i, char (*a)[3][5], int (*x)[_Countof (*a)], diff --git a/gcc/testsuite/gcc.dg/countof-param-compile.c b/gcc/testsuite/gcc.dg/countof-param-compile.c new file mode 100644 index 000000000000..802168a04114 --- /dev/null +++ b/gcc/testsuite/gcc.dg/countof-param-compile.c @@ -0,0 +1,104 @@ +/* { dg-do compile } */ +/* { dg-options "-Wvla-parameter -Warray-parameter=2" } */ + +#define NULL ((void *) 0) + +int f1 (int , int a[2]) { return _Countof (a); } +int f2 (int n, int a[n]) { return _Countof (a); } +int f3 (int , int a[*]) { return _Countof (a); } /* { dg-error "not allowed in other than function prototype scope" } */ +int f4 (int , int a[ ]) { return _Countof (a); } /* { dg-error "incomplete" } */ +int f5 (int , int *a) { return _Countof (a); } /* { dg-error "invalid" } */ + +int g1 (int , int a[2], int b[_Countof (a)]); +int g2 (int n, int a[n], int b[_Countof (a)]); +int g3 (int , int a[*], int b[_Countof (a)]); + +int g1 (int , int a[2], int b[_Countof (a)]) { return _Countof (b); } +int g2 (int n, int a[n], int b[_Countof (a)]) { return _Countof (b); } +int g3 (int , int a[*], int b[_Countof (a)]) { return _Countof (b); } /* { dg-error "not allowed in other than function prototype scope" } */ + +int h1 (int , int a[2], int b[(_Static_assert (_Countof (a) == 2), 7)]); +int h2 (int n, int a[n], int b[(_Static_assert (_Countof (a) == 2), 7)]); /* { dg-error "expression in static assertion is not constant" } */ +int h3 (int , int a[*], int b[(_Static_assert (_Countof (a) == 2), 7)]); /* { dg-error "expression in static assertion is not constant" } */ + +void +decay_obvious (int a[2]) +{ + _Countof (typeof (a)); /* { dg-error "invalid" } */ + _Countof (a + 1); /* { dg-error "invalid" } */ + _Countof (42 ? NULL : a); /* { dg-error "invalid" } */ + _Countof (a + 0); /* { dg-error "invalid" } */ + _Countof (&*a); /* { dg-error "invalid" } */ + _Countof (42, a); /* { dg-error "invalid" } */ + _Countof (42 ? a : a); /* { dg-error "invalid" } */ + _Countof (42 ? a : NULL); /* { dg-error "invalid" } */ + + int *p = a; + + _Countof (p); /* { dg-error "invalid" } */ + + typeof(a) a2; + + _Countof (a2); /* { dg-error "invalid" } */ +} + +void +no_decay_obvious (int a[2]) +{ + _Static_assert (2 == _Countof (a)); + _Static_assert (2 == _Countof (*&a)); + _Static_assert (2 == _Countof (*(&a + 0))); +} + +/* It would not decay if it were an array, but being a parameter, it decays. */ +void +decay_surprising (int a[2]) +{ + _Countof (*(&a + 1)); /* { dg-error "invalid" } */ +} + +/* We should warn: <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121271>. */ +void +non_const (int a[2]) +{ + a++; + _Countof (a); +} + +void +fwd_a_first (char a[6]; char a[]) +{ + _Countof (a); /* { dg-error "incomplete" } */ + // FIXME: This should pass: + //_Static_assert (_Countof (a) == 6); +} + +void +fwd_ap_first (char (*a)[6]; char (*a)[]) +{ + _Static_assert (_Countof (*a) == 6); +} + +void +fwd_a_second (char a[]; char a[7]) +{ + _Static_assert (_Countof (a) == 7); +} + +void +fwd_ap_second (char (*a)[]; char (*a)[7]) +{ + _Static_assert (_Countof (*a) == 7); +} + +void +fwd_a_both (char a[6]; char a[7]) +{ + _Static_assert (_Countof (a) == 6 || _Countof (a) == 7); +} + +void +fwd_ap_both (char (*a)[6]; char (*a)[6]) +{ + _Static_assert (_Countof (*a) == 6); +} diff --git a/gcc/testsuite/gcc.dg/countof-param-pedantic.c b/gcc/testsuite/gcc.dg/countof-param-pedantic.c new file mode 100644 index 000000000000..7d52f27a72bb --- /dev/null +++ b/gcc/testsuite/gcc.dg/countof-param-pedantic.c @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-std=c2y -pedantic" } */ + +#include <stdcountof.h> + +int +f (int a[1]) +{ + countof(a); /* { dg-warning "ISO C does not support array parameters" } */ + _Countof(a); /* { dg-warning "ISO C does not support array parameters" } */ +} diff --git a/gcc/testsuite/gcc.dg/countof-param.c b/gcc/testsuite/gcc.dg/countof-param.c new file mode 100644 index 000000000000..e03f8b9389db --- /dev/null +++ b/gcc/testsuite/gcc.dg/countof-param.c @@ -0,0 +1,25 @@ +/* { dg-do run } */ +/* { dg-options "-Wvla-parameter -Warray-parameter=2" } */ + +#define assert(e) ((e) ? (void) 0 : __builtin_abort ()) + +int f1 (int , int a[3]) { return _Countof(a); } +int f2 (int , int a[2]) { return _Countof(a); } + +int f1 (int , int a[2]); /* { dg-warning "with mismatched bound" } */ +int f2 (int n, int a[n]); /* { dg-warning "declared as a variable length array" } */ + +int g1 (int , int a[3]); +int g2 (int , int a[2]); + +int g1 (int , int a[2]) { return _Countof(a); } // { dg-warning "with mismatched bound" } */ +int g2 (int n, int a[n]) { return _Countof(a); } // { dg-warning "declared as a variable length array" } */ + +int +main (void) +{ + assert (3 == f1 (42, (int [42]){})); + assert (2 == f2 (42, (int [42]){})); + assert (2 == g1 (42, (int [42]){})); + assert (42 == g2 (42, (int [42]){})); +} -- 2.55.0
signature.asc
Description: PGP signature
