https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116016

--- Comment #22 from qinzhao at gcc dot gnu.org ---
the following is the user documentation I came up based on all the discussion
so far, let me know any comment and suggestion. (refer to GCC's
__builtin_clear_padding doc on the prototype of the new builtin:
https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fclear_005fpadding)


Builtin-in Function: void __builtin_set_counted_by (ptr, type expr) 

The built-in function __builtin_set_counted_by checks whether the array object
pointed by the pointer PTR has another object associated with it that
represents the number of elements in the array object through counted_by
attribute (i.e, the counted-by object). If so, sets the corresponding
counted-by object to EXPR. If such counted-by object does not exist, do
nothing.

The first argument must be a pointer to an array.
The TYPE of the second argument may be any integral type.

This built-in never evaluates its argument for side effects. If there are any
side effects in them, the compiler does not set the counted-by object if there
is one and issues warnings at the same time. 

For example:

for the following:

  struct foo1 {
    int counter1;
    struct bar1 array[] __attribute__((counted_by(counter)));
  } *p;

  struct foo2 {
    int other;
    struct bar2 array[];
  } *q;

  __builtin_set_counted_by (p->array, COUNT)

behaves like:

  p->counter1 = COUNT;

However, 

  __builtin_set_counted_by (q->array, COUNT)

behaves like a no-op since q->array does not have any associated counted-by
object through counted-by attribute.

Reply via email to