[clang] [Clang][Sema] Allow counted_by on void* in GNU mode (PR #164737)
Dan Liew via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 24 16:54:42 PDT 2025
delcypher wrote:
> > What happens with this?
> > ```
> > struct T {
> > int count;
> > void* __counted_by(count) ptr;
> > };
> >
> > void use(struct T* t) {
> > t = 0;
> > }
> > ```
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > Previously this was completely disallowed and when using the GNU language mode it will be. I don't think you're testing this.
> > @yeoul We probably need to check what happens when we lift this restriction in `-fbounds-safety`. I'm not sure our implementation will do the right thing.
>
> I don't understand what you mean? This builds with gnu and not with C11. I have tests in this PR for it? I must be misunderstanding something.
>
> ```shell
> $ clang -std=gnu11 -o wat.o -c wat.c
> $ clang -std=c11 -o wat.o -c wat.c
> wat.c:4:5: error: 'counted_by' cannot be applied to a pointer with pointee of
> unknown size because 'void' is an incomplete type
> 4 | void* __counted_by(count) ptr;
> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 1 error generated.
> ```
Sorry I should've been clearer. What I'm looking for is a Sema test that checks that the using a `void* __counted_by(...)` pointer works, not just that declaring it works. The change you've made in `Sema::CheckCountedByAttrOnField` basically means that a `void* __counted_by` pointer is treated like we treat incomplete types that might be completed later (e.g. `struct Foo;`). Those kinds of pointer are checked when they are used (e.g. as an rvalue or assigned to).
The `clang/test/Sema/attr-counted-by-void-ptr-gnu.c` test you added doesn't test this and none of the other Sema tests check this AFAICT. You do have a codegen test that checks some of this but it only tests use as an rvalue and not assignment to the pointer. Can you add a sema test that checks the behavior both in gnu11 and c11?
https://github.com/llvm/llvm-project/pull/164737
More information about the cfe-commits
mailing list