https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116016
--- Comment #40 from qinzhao at gcc dot gnu.org ---
(In reply to Bill Wendling from comment #39)
>
> But this all relies upon the 'counted_by' attribute existing. For this
> example:
>
> typeof(*__builtin_get_counted_by(P->FAM)) idx;
>
> for (idx = 0; idx < *__builtin_get_counted_by (P->FAM); idx++)
> do_things (P->FAM[idx]);
>
> This needs an if-then around it:
>
> if (__builtin_has_attribute (P->FAM, counted_by)) {
> typeof(*__builtin_get_counted_by (P->FAM)) idx;
>
> for (idx = 0; idx < *__builtin_get_counted_by (P->FAM); idx++)
> do_things (P->FAM[idx]);
> }
>
> Does this code need to be executed only when 'counted_by' exists? If so,
> it's far better to write this, because the 'count' needs to be at the same
> "level" as the FAM:
>
> if (__builtin_has_attribute (P->FAM, counted_by)) {
> typeof(P->COUNT) idx;
>
> for (idx = 0; idx < P->COUNT; idx++)
> do_things (P->FAM[idx]);
> }
Note, our original purpose of adding this new builtin is explicitly described
in description section as:
with the new builtin, "structs can gain the counted_by attribute without
needing additional open-coded counter assignments for each struct". that will
ease the adoption of "counted-by" attribute in the large application like linux
kernel.
the above second code segment clearly is a open-coded usage of the counted-by
field.