On Wed, 18 Nov 2020, Jakub Jelinek wrote:

> Hi!
> 
> The C and C++ FEs handle zero sized arrays differently, C uses
> NULL TYPE_MAX_VALUE on non-NULL TYPE_DOMAIN on complete ARRAY_TYPEs
> with bitsize_zero_node TYPE_SIZE, while C++ FE likes to set
> TYPE_MAX_VALUE to the largest value (and min to the lowest).
> 
> Martin has used array_type_nelts in get_parm_array_spec where the
> function on the C form of [0] arrays returns error_mark_node and the code
> crashes soon afterwards.  The following patch teaches array_type_nelts about
> this (e.g. dwarf2out already handles that as [0]).  While it will change
> what is_empty_type returns for certain types (e.g. struct S { int a[0]; };),
> as those types occupy zero bits in C, it should make an ABI difference.
> 
> So, the tree.c change makes the c-decl.c code handle the [0] arrays
> like any other constant extents, and the c-decl.c change just makes sure
> that if we'd run into error_mark_node e.g. from the VLA expressions, we
> don't crash on those.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

Looks OK to me.  I wonder if we want to change the C FE to follow the
C++ FE here.  Note I also chickened out making the domain signed
(the C++ variant is to be interpreted as [0, -1] for zero-size arrays).

In principle [0,] (NULL TYPE_MAX_VALUE) is a flex-array domain but
I guess a COMPLETE_TYPE_P clearly says it isn't.  Still it would be
nice to fix this inconsistency.  (layout_type could even ICE on
complete array types with "incomplete" domain)

Thanks,
Richard.

> 2020-11-18  Jakub Jelinek  <ja...@redhat.com>
> 
>       PR c/97860
>       * tree.c (array_type_nelts): For complete arrays with zero min
>       and NULL max and zero size return -1.
> 
>       * c-decl.c (get_parm_array_spec): Bail out of nelts is
>       error_operand_p.
> 
>       * gcc.dg/pr97860.c: New test.
> 
> --- gcc/tree.c.jj     2020-11-18 09:40:09.798660999 +0100
> +++ gcc/tree.c        2020-11-18 20:02:41.655398514 +0100
> @@ -3483,7 +3483,17 @@ array_type_nelts (const_tree type)
>  
>    /* TYPE_MAX_VALUE may not be set if the array has unknown length.  */
>    if (!max)
> -    return error_mark_node;
> +    {
> +      /* zero sized arrays are represented from C FE as complete types with
> +      NULL TYPE_MAX_VALUE and zero TYPE_SIZE, while C++ FE represents
> +      them as min 0, max -1.  */
> +      if (COMPLETE_TYPE_P (type)
> +       && integer_zerop (TYPE_SIZE (type))
> +       && integer_zerop (min))
> +     return build_int_cst (TREE_TYPE (min), -1);
> +
> +      return error_mark_node;
> +    }
>  
>    return (integer_zerop (min)
>         ? max
> --- gcc/c/c-decl.c.jj 2020-11-11 01:46:03.245697697 +0100
> +++ gcc/c/c-decl.c    2020-11-18 20:03:53.053602265 +0100
> @@ -5775,6 +5775,8 @@ get_parm_array_spec (const struct c_parm
>              type = TREE_TYPE (type))
>           {
>             tree nelts = array_type_nelts (type);
> +           if (error_operand_p (nelts))
> +             return attrs;
>             if (TREE_CODE (nelts) != INTEGER_CST)
>               {
>                 /* Each variable VLA bound is represented by the dollar
> --- gcc/testsuite/gcc.dg/pr97860.c.jj 2020-11-18 15:15:08.858931877 +0100
> +++ gcc/testsuite/gcc.dg/pr97860.c    2020-11-18 15:14:50.751135430 +0100
> @@ -0,0 +1,11 @@
> +/* PR c/97860 */
> +/* { dg-do compile } */
> +/* { dg-options "" } */
> +
> +void
> +foo (int n)
> +{
> +  typedef int T[0];
> +  typedef T V[n];
> +  void bar (V);
> +}
> 
>       Jakub
> 
> 

-- 
Richard Biener <rguent...@suse.de>
SUSE Software Solutions Germany GmbH, Maxfeldstrasse 5, 90409 Nuernberg,
Germany; GF: Felix Imend

Reply via email to