Hi,

Changes in v7 compared to v5 (v6 never existed):

-  Add tests to make sure that _Countof() works reasonably well with
   forward declarations of array parameters.
-  Rebase on top of master.

See the range-diff at the bottom.

Joseph, as we agreed recently, I want to make _Countof() support array
parameters where the length is declared only in the forward declaration.
However, that seems non-trivial, so it might be interesting to do a
first release that doesn't yet compile for that.  I've added tests to
make sure that it never produces UB, but rather just compiler errors or
reasonable behavior (one of the specified lengths).

I think it would be interesting to release this feature already, because
it works decently well for most reasonable code.  If we have this
feature already in GCC 17, then programs could start using this feature
in some (~5 for many projects) years from now, while delaying it until
we have better support for forward declarations could delay it by one or
two years, which would be bad for bounds safety in general.

Do you think we could release this, with the acknowledgement that we
want to eventually fix the test which has a 'FIXME'?  I plan to work on
that, but can't promise when I'll have it done.

The current patch set is robust to be released, IMO.


Have a lovely night!
Alex


Alejandro Colomar (2):
  c: Refactor implementation of _Countof
  c: Add support for array parameters in _Countof

 gcc/c/c-typeck.cc                             |  45 +++++++-
 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, 191 insertions(+), 13 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

Range-diff:
1:  31191ae061c2 ! 1:  53c196b9667d c: Refactor implementation of _Countof()
    @@ Metadata
     Author: Alejandro Colomar <[email protected]>
     
      ## Commit message ##
    -    c: Refactor implementation of _Countof()
    +    c: Refactor implementation of _Countof
     
         No changes intended.  This refactor prepares the code so that a future
    -    patch extending the semantic of _Countof() will be smaller.
    +    patch extending the semantics of _Countof() will be smaller.
     
         gcc/c/ChangeLog:
     
    @@ gcc/c/c-typeck.cc: 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);
     -      ret.value = c_countof_type (loc, TREE_TYPE (folded_expr));
    --      c_last_sizeof_arg = expr.value;
    --      c_last_sizeof_loc = loc;
    +       c_last_sizeof_arg = expr.value;
    +       c_last_sizeof_loc = loc;
            ret.original_code = COUNTOF_EXPR;
            ret.original_type = NULL;
            ret.m_decimal = 0;
    --      if (is_top_array_vla (TREE_TYPE (folded_expr)))
    +-      if (top_array_vla_p (TREE_TYPE (folded_expr)))
     +
     +      tree folded_expr = c_fully_fold (expr.value, require_constant_value,
     +                                 &expr_const_operands);
    -+
     +      tree type = TREE_TYPE (folded_expr);
     +      ret.value = c_countof_type (loc, type);
    -+      c_last_sizeof_arg = expr.value;
    -+      c_last_sizeof_loc = loc;
     +
    -+      if (is_top_array_vla (type))
    ++      if (top_array_vla_p (type))
        {
          /* countof is evaluated when given a vla.  */
          ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
    @@ gcc/c/c-typeck.cc: c_expr_countof_expr (location_t loc, struct c_expr 
expr)
          C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
          SET_EXPR_LOCATION (ret.value, loc);
        }
    --      pop_maybe_used (is_top_array_vla (TREE_TYPE (folded_expr)));
    -+      pop_maybe_used (is_top_array_vla (type));
    +-      pop_maybe_used (top_array_vla_p (TREE_TYPE (folded_expr)));
    ++      pop_maybe_used (top_array_vla_p (type));
          }
        return ret;
      }
2:  b1ca7e6c7135 ! 2:  026b76ce72ec c: Add support for array parameters in 
_Countof
    @@ Commit message
         This extension will allow using _Countof() in more places, reducing the
         chances of specifying an incorrect size accidentally.
     
    -    We should eventually add -farray-parameters-are-const to make array
    -    parameters be implicitly const-qualified (similar to -Wwrite-strings).
    +    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>
     
    @@ Commit message
         Signed-off-by: Alejandro Colomar <[email protected]>
     
      ## gcc/c/c-typeck.cc ##
    -@@ gcc/c/c-typeck.cc: is_top_array_vla (tree type)
    -   return var;
    +@@ gcc/c/c-typeck.cc: top_array_vla_p (const_tree type)
    +    || TREE_CODE (TYPE_MIN_VALUE (d)) != INTEGER_CST;
      }
      
     +
    @@ gcc/c/c-typeck.cc: is_top_array_vla (tree type)
      
      struct c_expr
     @@ gcc/c/c-typeck.cc: 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 rv = c_array_parm_orig_type (expr.value))
    ++    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 = rv;
    ++        type = t;
     +      }
     +  }
     +
            ret.value = c_countof_type (loc, type);
    -       c_last_sizeof_arg = expr.value;
    -       c_last_sizeof_loc = loc;
    + 
    +       if (top_array_vla_p (type))
     
      ## gcc/doc/extend.texi ##
     @@ gcc/doc/extend.texi: _Countof (int [7][n++]);  // integer constant 
expression
    @@ gcc/testsuite/gcc.dg/countof-param-compile.c (new)
     +{
     +  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);
     +}
     
      ## gcc/testsuite/gcc.dg/countof-param-pedantic.c (new) ##

base-commit: a440b382e43203857de9195eb526c4a16f21ceb1
-- 
2.55.0

Attachment: signature.asc
Description: PGP signature

Reply via email to