[llvm] [clang] [Clang] Correct __builtin_dynamic_object_size for subobject types (PR #78526)

Eli Friedman via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 24 17:24:54 PST 2024


efriedma-quic wrote:

> ```c
> struct x {
>     int a;
>     char foo[2][40];
>     int b;
>     int c;
> };
> 
> size_t f(struct x *p, int idx) {
>     return __builtin_dynamic_object_size(&p->foo[idx], 1);
> }
> ```

If I'm following correctly, the return here is 0, 40, or 80, depending on the value of idx?  That's not a constant, but the computation is entirely syntactic; it doesn't matter what "p" actually points to.  So clang can lower the builtin itself.  Currently it doesn't, I think, because all the relevant code is in ExprConstant, but the code could be adapted.

The problem, really, is that we can't easily extend that approach to stuff like the following:

```c
size_t f(struct x *p, int idx) {
    char *c = &p->foo[idx];
    return __builtin_dynamic_object_size(c, 1);
}
```

https://github.com/llvm/llvm-project/pull/78526


More information about the cfe-commits mailing list