Hi Martin,

On Thu, Aug 14, 2025 at 02:21:10PM +0200, Martin Uecker wrote:
> > > You could also add a warning when _Countof is applied to a
> > > non-constant pointer.
> > 
> > Yup, I should add that.  I wouldn't enable it in any groups flags,
> > though.  I don't want people to start writing [const n].  Instead,
> > I expect them to use -farray-parameters-are-const in the future.
> 
> Or a warning when array parameters are modified. I think this would
> make the most sense at the moment.

That would be effectively implementing -farray-parameters-are-const.
I think it makes sense to actually implement it, which will result in
simpler GCC code, and don't add any special code here.

So, I think I'll leave this without a warning, pending on
-farray-parameters-are-const.

> > > > So, I stand by this patch even with this known limitation.
> > > > 
> > > > On the other, hand, it may be a GCC existing bug?  I see that
> > > > _Countof(a+0) remembers the array number of elements but _Countof(a+1)
> > > > doesn't.  Maybe once you do a++ you should also forget the number of
> > > > elements (or update it)?
> > > 
> > > Difficult, as this is then flow-sensitive and not simply a
> > > property of 'a'.   That a + 0 works because the expression
> > > is folded before.  Maybe you could change it to take the expression
> > > before folding?
> > 
> > Hmmm, yeah, that would be good.  It would fix the _surprise() test
> > cases.  Do you expect any issues by removing the call to c_fully_fold()?
> 
> In the code path where we have a PARM_DECL, it is not needed.
> Otherwise it is.

Thanks!

I don't know much about c_fully_fold().  Would this be enough?  Or do
I need to add more conditionals?

        diff --git i/gcc/c/c-typeck.cc w/gcc/c/c-typeck.cc
        index 0080da494a9b..7f24880297de 100644
        --- i/gcc/c/c-typeck.cc
        +++ w/gcc/c/c-typeck.cc
        @@ -3853,40 +3853,43 @@ c_expr_countof_expr (location_t loc, struct 
c_expr expr)
             }
           else
             {
               bool expr_const_operands = true;
               ret.original_code = COUNTOF_EXPR;
               ret.original_type = NULL;
               ret.m_decimal = 0;
         
               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 (folded_expr))
                    {
                      pedwarn (input_location, OPT_Wpedantic,
                               "ISO C does not support array parameters in %qs",
                               "_Countof");
                      type = rv;
        +
        +             /* Don't fold in array parameters.  */
        +             folded_expr = expr.value;
                    }
                }
         
               ret.value = c_countof_type (loc, type);
               c_last_sizeof_arg = expr.value;
               c_last_sizeof_loc = loc;
         
               if (is_top_array_vla (type))
                {
                  /* countof is evaluated when given a vla.  */
                  ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
                                      folded_expr, ret.value);
                  C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = 
!expr_const_operands;
                  SET_EXPR_LOCATION (ret.value, loc);
                }
               pop_maybe_used (is_top_array_vla (type));
             }
           return ret;
         }
         

Cheers,
Alex

-- 
<https://www.alejandro-colomar.es/>

Attachment: signature.asc
Description: PGP signature

Reply via email to