Skip to content

Commit

Permalink
[Clang] Update 'counted_by' documentation
Browse files Browse the repository at this point in the history
Describe a limitation of the 'counted_by' attribute when used in unions.
Also fix a errant typo.
  • Loading branch information
bwendling committed Jan 10, 2024
1 parent 8ae8ae9 commit 4a3fb9c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
28 changes: 28 additions & 0 deletions clang/include/clang/Basic/AttrDocs.td
Original file line number Diff line number Diff line change
Expand Up @@ -7825,5 +7825,33 @@ requirement:
--p->count;
p->array[index] = val;
}

Flexible array members, with the ``counted_by`` attribute, in unions are
supported with one limitation. If multiple flexible array members have the
``counted_by`` attribute, ``__builtin_dynamic_object_size`` won't be able to
calculate the object's size. For instance, in this example:

.. code-block:: c

struct union_of_fams {
int flags;
union {
unsigned long normal_field;
struct {
int count1;
int arr1[] __counted_by(count1);
};
struct {
signed char count2;
int arr2[] __counted_by(count2);
};
};
};

size_t get_size(struct union_of_fams *p) {
return __builtin_dynamic_object_size(p, 1);
}

a call to ``get_size`` will return ``-1``.
}];
}
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ CodeGenFunction::emitFlexibleArrayMemberSize(const Expr *E, unsigned Type,
// };
// };
//
// We don't konw which 'count' to use in this scenario:
// We don't know which 'count' to use in this scenario:
//
// size_t get_size(struct union_of_fams *p) {
// return __builtin_dynamic_object_size(p, 1);
Expand Down

0 comments on commit 4a3fb9c

Please sign in to comment.