[clang] [Clang] Add -Wcounted-by-addrof to warn when &fam discards __counted_by (PR #206760)

Yeoul Na via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 13 09:49:11 PDT 2026


rapidsna wrote:

> > > I haven't given this a thorough review, but one question (probably for @rapidsna) that comes to mind is: because `__counted_by` is a declaration or type attribute, do we have to drop the information when taking the address? I would assume that would form a pointer to a counted by FAM?
> > 
> > 
> > @AaronBallman Yes, counted_by can be attached to T (*)[]. But that would be misleading when the pointer actually gets used in most cases because the element size is still zero. Pointer arithmetic still has stride 0, and indexing does too. With __bdos it returns -1, which is likely not what the user intended.
> 
> Why is the element size zero? You can't have an array whose element type is incomplete, so I think we should always know the element size?

Sorry, I was unclear — the "element" of a pointer-to-incomplete-array (i.e. the element for pointer arithmetic) is the incomplete array `T[]`, not `T`. `__counted_by` would be misleading there: if it were respected, the stride would be `sizeof(T) * n`, but in practice the stride stays zero because `sizeof(T[])` is zero.

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


More information about the cfe-commits mailing list