Skip to content

Commit 4a3fb9c

Browse files
committed
[Clang] Update 'counted_by' documentation
Describe a limitation of the 'counted_by' attribute when used in unions. Also fix a errant typo.
1 parent 8ae8ae9 commit 4a3fb9c

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

clang/include/clang/Basic/AttrDocs.td

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7825,5 +7825,33 @@ requirement:
78257825
--p->count;
78267826
p->array[index] = val;
78277827
}
7828+
7829+
Flexible array members, with the ``counted_by`` attribute, in unions are
7830+
supported with one limitation. If multiple flexible array members have the
7831+
``counted_by`` attribute, ``__builtin_dynamic_object_size`` won't be able to
7832+
calculate the object's size. For instance, in this example:
7833+
7834+
.. code-block:: c
7835+
7836+
struct union_of_fams {
7837+
int flags;
7838+
union {
7839+
unsigned long normal_field;
7840+
struct {
7841+
int count1;
7842+
int arr1[] __counted_by(count1);
7843+
};
7844+
struct {
7845+
signed char count2;
7846+
int arr2[] __counted_by(count2);
7847+
};
7848+
};
7849+
};
7850+
7851+
size_t get_size(struct union_of_fams *p) {
7852+
return __builtin_dynamic_object_size(p, 1);
7853+
}
7854+
7855+
a call to ``get_size`` will return ``-1``.
78287856
}];
78297857
}

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ CodeGenFunction::emitFlexibleArrayMemberSize(const Expr *E, unsigned Type,
955955
// };
956956
// };
957957
//
958-
// We don't konw which 'count' to use in this scenario:
958+
// We don't know which 'count' to use in this scenario:
959959
//
960960
// size_t get_size(struct union_of_fams *p) {
961961
// return __builtin_dynamic_object_size(p, 1);

0 commit comments

Comments
 (0)