Repository navigation
tree-wide: introduce ENUM_BIT() macro - #9224
Conversation
|
Hmm, this certainly doesn't hurt. OTOH, I'm not convinced that we should introduce such macros that don't really change much. What do other people think? |
|
While somewhat orthogonal to this change, I think it's at least a little strange to have bit masks implemented as enums. What is the advantage of doing it this way over #define? Clearly the compiler is not policing the values assigned to variables of the enum type if we're allowed to OR them together into values not represented within them... |
the big benefit is that the enum definitions are part of the language itself, and thus the compiler and rest of the toolchain can make sense of them. This fact is visible at one place in particular: gdb is able to decode such bit masks for you (yes, it really can decode bitmasks of enums...), and it can't do that for macros. That is incredibly useful when debugging. And it's not just about displaying stuff, but if its an enum, you can also use these definitions in gdb expressions, i.e. do "pring flags & FOOBAR" and it will check that for you... |
There was a problem hiding this comment.
Why is this named ENUM_BIT() rather than simply BIT()? The chosen name would suggest that it does something specific to enum types, but all it really does is to set a bit. Not that consistency maters much across projects, but the Linux kernel has a BIT() macro with the same definition.
There was a problem hiding this comment.
it is specific to enum types, as doing the ULL suffix is not a good idea otherwise, as in other contexts it will upgrade everything to an unsigned long long. Only in enum context this is a safe choice, as only then the type of the field actually does not matter, as the enum type will be derived from the values, not the types...
The primary motivation is to catch enum values created through a shift that is
too big:
../src/test/test-sizeof.c:26:29: error: left shift count >= width of type [-Werror=shift-count-overflow]
enum_with_shift = 1 << 32,
^~
cc1: some warnings being treated as errors
The compiler will now reject those.
This is an alternative to systemd#9224.
The primary motivation is to catch enum values created through a shift that is
too big:
../src/test/test-sizeof.c:26:29: error: left shift count >= width of type [-Werror=shift-count-overflow]
enum_with_shift = 1 << 32,
^~
cc1: some warnings being treated as errors
The compiler will now reject those.
This is an alternative to #9224.
|
I'm still a +1 to merge this one. Even though #9245 (and its follow up) will have the compiler warn when the field doesn't fit into the enum, whenever it does, the enum still needs to be fixed. Fixing those using |
|
Ah I missed @filbranden's comment... Hmm, so I reworked this PR earlier today. Instead of adding the macro it now simply cleans up all enum definitions to use a simple "1 << 0" syntax everywhere, except for the one case where we actually need more than 31 bit, which is where UINT64_C(1) is used. That said, I probably could resurrect the macro version if there's strong desire for it. PTAL! |
Let's always write "1 << 0", "1 << 1" and so on, except where we need more than 31 flag bits, where we write "UINT64(1) << 0", and so on to force 64bit values.
|
I merged the latest version. I like this one, but anyway this cleanup does not preclude adding a macro later.
Possibly, maybe, I guess it depends. An analogy is the |
|
Also I realized it turns out when we overflow the 32-bit int, then to use that constant from the enum we need an explicit cast anyways (which we're doing with a #define on that place we need it) so ENUM_BIT doesn't make this fully transparent either... Oh well, here's hoping a future C standard would fix this! |
The macro is useful for defining bit masks as enum. It's whole purpose
is to to the shifting of 1 properly and avoid sign extension and
overflows, by shifting "1ULL" rather than "1".
The various flavours we did this with before are now unified to the
ENUM_BIT() based syntax.