<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/141827>141827</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Allow `counted_by` argument to support simple arithmetic expressions
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
seanm
</td>
</tr>
</table>
<pre>
Consider the following C:
```c
struct mystruct
{
unsigned len;
unsigned data[] __attribute__((counted_by(len)));
};
```
This works with recent clang.
But it would be nice if simple arithmetic expressions could be used in the `counted_by` argument, like `counted_by(len - 1)` or `counted_by(len * 4)`, things like that. Trying to do so gives:
```
<source>:4:47: error: 'counted_by' argument must be a simple declaration reference
4 | unsigned data[] __attribute__((counted_by(len - 1)));
| ^~~~~~~
```
See: https://godbolt.org/z/sx6sY1fjG
The gcc folks seemed to consider this too: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108896#c36 though from godbolt it seems they don't support it either.
There are several places in the libusb codebase that would benefit from this. The most complicated case would be `counted_by((len - 2) / 2)`.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJycVMtu4zgQ_Br60oggUe-DDo4z3g_YuezJoMi2xAlFGmQzHs9hv31B-ZFBkt3DCjTcEFvdVV1FihD0ZBEHVj-z-mUjIs3ODwGFXTajU5dh52zQCj3QjHB0xrizthPsWLll-bqa_Loky7eBfJQEy-UapO32meVbAIBo114KDFpWfnqrBIkrCjgcBJHXYyQ8HBjvGO-ki5ZQHcYL410qwPvbWiux9uUW3NFcwX2fdYCz868Bzppm8CjREkgj7JRdU54jgSY4u2gUjAhWSwR9hKCXk0EQXtO8IGkJ-PPkMQTtbAB5T48BFWi7jicN4R1nk4PwU1zQEuM7MPr1Y8bKBJ6gSDyaHJz_MoHxLVTXlFSIZm2ncK1Hs6AMvvtL0oQcKAfBwaTfMHwWKMXlLrjoJbLyGyu3Vfq1rNwCeu98ChhvfwfQPjjAEgMlxuI-GoXSCC9IOwsej-jRSrzJWgFrd_9X3PtIfhMY1ifVZPW3v9fns9p_IiYKM9Fppc_3jO8np0ZnKHN-Ynz_i_F9-NmEv4rjjz_uHkGYpEzmfg0QEBdUaZby3fk6ADn3RW0ps8nGW-0xTr-0MSK1mN35MMYpk5Nm5V4rVr4Uedf1DeOlLBug2cVphqN3C9wQJhem7iF56QLKWcZbghBPJ-fXXdQ0o88esH2yJ0LAN_TCwMkIieFuRqPHGEaQTuEowtUqD5dbPGq6dk_sMkhDWFwgkG45GS0FoQKZvnscjI_WfGjFGe-B8f0asCbPNmooVV_2YoND0VZdVfclzzfzwPumqive1LKuq6Zpm7zo675ojqMqmqKoNnrgOa_zmndFn3dVntW5KgvelbIfRX9EyaocF6FNZszbkua-0SFEHIqq6Hi7MWJEE9bbjHOLZ1h3GefpcvND-uhpjFNgVW50oPBehjQZHLbpfvv3c5xscdfjP--HTfRm-OAVTXMcM-kWxvep7e3v6eTdD5TE-H4FGxjf39i8DfyfAAAA___v38l1">