Skip to content

tree-wide: introduce ENUM_BIT() macro - #9224

Merged
keszybz merged 1 commit into
systemd:masterfrom
poettering:enum-bit
Jun 12, 2018
Merged

keszybz merged 1 commit into
systemd:masterfrom
poettering:enum-bit

Conversation

@poettering

Copy link
Copy Markdown
Member

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.

@keszybz

keszybz commented Jun 7, 2018

Copy link
Copy Markdown
Member

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?

@vcaputo

vcaputo commented Jun 7, 2018

Copy link
Copy Markdown
Contributor

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...

@poettering

Copy link
Copy Markdown
Member Author

What is the advantage of doing it this way over #define?

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...

Comment thread src/basic/macro.h Outdated

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

keszybz added a commit to keszybz/systemd that referenced this pull request Jun 9, 2018
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.
filbranden pushed a commit that referenced this pull request Jun 9, 2018
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.
@filbranden

Copy link
Copy Markdown
Contributor

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 ENUM_BIT() is a good idea, I'd say. And so, using ENUM_BIT() preemptively to avoid the overflow error in first case is even better...

@poettering

Copy link
Copy Markdown
Member Author

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.
@keszybz
keszybz merged commit ef31828 into systemd:master Jun 12, 2018
@keszybz

keszybz commented Jun 12, 2018

Copy link
Copy Markdown
Member

I merged the latest version. I like this one, but anyway this cleanup does not preclude adding a macro later.

And so, using ENUM_BIT() preemptively to avoid the overflow error in first case is even better...

Possibly, maybe, I guess it depends. An analogy is the if (0 == x) syntax used in some places, that personally drives me crazy, and which we don't use in systemd. We don't need to do this, because the compiler is nice enough to warn us if we ever use if (x = 0) by mistake. Same here — if the compiler can do watch our step, we can just use the simplest syntax possible.

@filbranden

Copy link
Copy Markdown
Contributor

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

5 participants