[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH] hash: pacify gcc -Wsuggest-attribute=const
|
From: |
Bruno Haible |
|
Subject: |
Re: [PATCH] hash: pacify gcc -Wsuggest-attribute=const |
|
Date: |
Fri, 19 May 2023 14:12:16 +0200 |
Pádraig Brady wrote:
> In general for any tuning though, since the tuning param struct is declared
> const,
That the 'tuning' parameter is a 'const Hash_tuning *' is merely an indication
that the function is pure. If it was a 'Hash_tuning *' and if there was an
assignment to a field tuning->something inside the function, the function
could not be pure.
> can the compiler not assume successive invocations against the _same object_
> returns the same value?
No, it can't. The elements of 'struct hash_tuning' are non-const; therefore
there can be a separate compilation unit that changes the value of
tuning->is_n_buckets between the two successive invocations. This would be
a change to "the observable state of the program" and would have an effect
on the return value of compute_bucket_size. Thus compute_bucket_size is not
'const'.
> In coreutils usage tuning==NULL so the static const default_tuning is passed
> always.
These are global data flow properties, that "gcc -flto" can well exploit to
produce better code. But a function's declaration must remain valid
independently of the global data flow in the surrounding program.
> So I guess _GL_ATTRIBUTE_CONST is fine there.
No, it's not fine. You proposed a patch to gnulib, not to coreutils.
Bruno