[clang] [clang] Add clang::preferred_type attribute for bitfields (PR #69104)

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 19 08:17:38 PDT 2023


erichkeane wrote:

> > > @erichkeane I guess you'd be pretty happy if our enums were declared the following way:
> 
> > Not really? That doesn't gain us the 'we must change where this is stored' situation like a preferred_type attribute would/could. We MIGHT be able to get away with a comment on top of that with a list of places that needs to ALSO be changed, but we end up missing similar comments today anyway.
> 
> I meant `unsigned _BitInt(N)` underlying type to complement `preferred_type`'s check that a bit-field is wide enough. Otherwise such a check will have to incorporate heuristics to prevent false-positives for cases like the following:
> 
> https://github.com/llvm/llvm-project/blob/180eae1f1e5a08595ed2278d93f01fb321284649/clang/include/clang/AST/DeclarationName.h#L174-L185
> 
> One might argue that this particular case might be resolved by transition to `PointerIntPair`, which is true. It's just the only case I was able to find. I have a recollection of an enum that ends with `Size` enumerator, possibly TableGen'ed, but I can't find it.

We actually DO a similar kind of diagnostic in one of our assignments already I think, which figures out that a value we're storing into doesn't have the number of bits required for the 'highest' and 'lowest' value of an Enum (though I'm having trouble finding it ATM).  This obviously doesn't 'work' in the case of bitmasks, but to the point that it is 'quiet' instead of false-positive.  I'm just suggesting that we can extend this warning to the declarations of bitfield that use this to specify an enumeration type.

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


More information about the cfe-commits mailing list