Skip to content

Commit 8d90a54

Browse files
authored
[clang] Add comment about misleading alloc_size argument names (llvm#134899)
Attr.td names the first alloc_size argument "ElemSizeParam" and the second optional argument "NumElemsParam"; but the semantics of how the two-argument version is used in practice is the opposite of that. glibc declares calloc() like this, so the second alloc_size argument is the element size: ``` extern void *calloc (size_t __nmemb, size_t __size) __THROW __attribute_malloc__ __attribute_alloc_size__ ((1, 2)) __wur; ``` The Linux kernel declares array allocation functions like `kmalloc_array_noprof()` the same way. Add a comment explaining that the names used inside clang are misleading.
1 parent 320c13e commit 8d90a54

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

clang/include/clang/Basic/Attr.td

+7
Original file line numberDiff line numberDiff line change
@@ -1709,6 +1709,13 @@ def EmptyBases : InheritableAttr, TargetSpecificAttr<TargetMicrosoftCXXABI> {
17091709
def AllocSize : InheritableAttr {
17101710
let Spellings = [GCC<"alloc_size">];
17111711
let Subjects = SubjectList<[HasFunctionProto]>;
1712+
// The parameter names here are a bit misleading.
1713+
// When used with a single argument, the first argument is obviously the
1714+
// allocation size; but when used with two arguments, the first argument is
1715+
// usually the number of elements, while the second argument is usually the
1716+
// element size - the reverse of how they are named here.
1717+
// The documentation of both GCC and clang does not describe any semantic
1718+
// difference between the first and second argument.
17121719
let Args = [ParamIdxArgument<"ElemSizeParam">,
17131720
ParamIdxArgument<"NumElemsParam", /*opt*/ 1>];
17141721
let TemplateDependent = 1;

0 commit comments

Comments
 (0)