https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116016
--- Comment #23 from Bill Wendling <isanbard at gmail dot com> --- (In reply to qinzhao from comment #22) > 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. The documentation for the 'counted_by' attribute says this about the field value: "The field that represents the number of the elements should have an integer type. Otherwise, the compiler reports an error and ignores the attribute." I think we should specify that the type of the second argument must (or should) be an integral type. Using "may" seems less strict. > This built-in never evaluates its argument for side effects. If there are s/argument/arguments/ > 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. Suggested alternative to the second sentence: If there are any side effects in them, the builtin becomes a no-op and the compiler issues a diagnostic. > 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.