Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-Wenum-constexpr-conversion should be a hard error, not a downgradable error #59036

Closed
carlosgalvezp opened this issue Nov 16, 2022 · 67 comments · Fixed by #102364
Closed

-Wenum-constexpr-conversion should be a hard error, not a downgradable error #59036

carlosgalvezp opened this issue Nov 16, 2022 · 67 comments · Fixed by #102364
Assignees
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer enhancement Improving things as opposed to bug fixing, e.g. new or missing feature

Comments

@carlosgalvezp
Copy link
Contributor

The -Wenum-constexpr-conversion warning was created to account for the fact that casting integers to enums outside of the valid range of the enum is UB in C++17. Constant expressions invoking UB lead to an ill-formed program.

The current behavior of Clang is that it does warn and it does error, but the message is nevertheless a warning that the user can easily suppress via -Wno-enum-constexpr-conversion.

Since the program is ill-formed, I don't think this should be a warning that can be disabled. Just like one can't disable the warning in this ill-formed code:

constexpr int x[10]{};
constexpr int y = x[10];

Therefore I propose to make the diagnostic a hard error that cannot be disabled. Additionally, this "hard error" behavior should only be active in C++17 (it's currently active in for any standard).

The warning could be repurposed to be a general warning that one can enable, and is active not only in constexpr expressions, but in any expression that involves casting an integer to an enum.

@carlosgalvezp carlosgalvezp added enhancement Improving things as opposed to bug fixing, e.g. new or missing feature clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer labels Nov 16, 2022
@thesamesam
Copy link
Member

thesamesam commented Nov 16, 2022

https://clang.llvm.org/docs/ReleaseNotes.html#potentially-breaking-changes (for Clang 16) says it's an error by default but is currently downgradable (until Clang 17). So I guess this bug is to track remembering to do so in Clang 17.

See also #50055.

@thesamesam thesamesam changed the title -Wenum-constexpr-conversion should be a hard error, not a warning -Wenum-constexpr-conversion should be a hard error, not a downgradable error Nov 16, 2022
@carlosgalvezp
Copy link
Contributor Author

Nice, I wasn't aware of that, thanks! Yes that makes a lot of sense, thanks for pointing out. Let's keep this bug as a reminder. I will open a new ticket for having a warning that works also on non-constexpr contexts.

@tbaederr
Copy link
Contributor

Did this happen?

@tbaederr tbaederr added this to the LLVM 17.0.0 Release milestone Apr 15, 2023
kdesysadmin pushed a commit to KDE/kdevelop that referenced this issue Jul 7, 2023
This fixes the following Clang 16 compilation error:
kdevelop/plugins/clang/duchain/cursorkindtraits.h:217:7: error: integer value -1 is outside the valid range of values [0, 255] for the enumeration type 'CommonIntegralTypes' [-Wenum-constexpr-conversion]
    : static_cast<IntegralType::CommonIntegralTypes>(-1);

Quote from llvm/llvm-project#59036 :
    The -Wenum-constexpr-conversion warning was created to account for
    the fact that casting integers to enums outside of the valid range
    of the enum is UB in C++17. Constant expressions invoking UB lead to
    an ill-formed program.

BUG: 471995
FIXED-IN: 5.12.230800
@github-project-automation github-project-automation bot moved this to Needs Triage in LLVM Release Status Aug 30, 2023
carlosgalvezp pushed a commit to carlosgalvezp/llvm-project that referenced this issue Sep 22, 2023
Casting an int into an enum is undefined behavior if the int is
outside of the range of the enum. UB is not allowed in constant
expressions, therefore the compiler must produce a hard error
in that case.

However, until now, the compiler produced a warning that could be
suppressed. It should instead be a hard error, since the program
is ill-formed in that case, as per the C++ Standard.

This patch turns the warning into an error. Additionally, references
to the old warning are removed since they are now meaningless.

Fixes llvm#59036
@carlosgalvezp
Copy link
Contributor Author

@carlosgalvezp
Copy link
Contributor Author

@AaronBallman @dwblaikie We are now on Clang 19, do you think it's the right time to pull the trigger and turn this into a non-downgradable error? It has been a warning also in system headers since Clang 18.

@dwblaikie
Copy link
Collaborator

Looks like binutils is still suppressing this warning: https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=include/diagnostics.h;h=8cc2b493d2c02ba7dbc5879207746107ad7143a0;hb=refs/heads/master#l81

As is openmp in the LLVM project itself (

append_if(OPENMP_HAVE_WENUM_CONSTEXPR_CONVERSION_FLAG "-Wno-enum-constexpr-conversion" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
)

And I also found this magic_enum project disabling it too: https://github.com/Neargye/magic_enum/blob/b4ada55c8cea21f3c73c5a58cebd368a227465a1/include/magic_enum/magic_enum.hpp#L71

At least for the openmp usage and probably the binutils usage, it'd be nice to see if some progress can be made removing those warning suppressions.

@AaronBallman
Copy link
Collaborator

I expect that so long as we leave it a downgradable diagnostic, people will continue to not fix their code because that's easier or because they're unaware of the issue. The OpenMP find is a wonderful example; it was disabled two years ago "to buy ourselves more time" and no attempts have been made to actually fix the issue since then: 9ff0cc7

That said, Clang 19 might be a bit too soon given that we enabled it in system headers in Clang 18, which isn't yet released. I think Clang 20/21 might be somewhat better because that gives folks another year to discover the problems with their uses, but at some point, I think we really do need to force the issue otherwise the can will be kicked down the road forever.

@carlosgalvezp
Copy link
Contributor Author

FYI I'm bringing this issue to the binutils project.
https://sourceware.org/bugzilla/show_bug.cgi?id=31331

I agree with Aaron, it's way easier to just suppress the warning and call it a compiler bug than actually do some research and understand the subtle issues when doing these tricks with enums. I think that the more we delay it, the more projects will suppress the warning as they bump their clang installation, and so the harder it will be for us to introduce it.

That said, I'm not opposed to waiting another release or two.

carlosgalvezp pushed a commit to carlosgalvezp/llvm-project that referenced this issue Feb 9, 2024
This effectively reverts commit 9ff0cc7.
For some reason "git revert" lead to "no changes" after fixing
conflicts, so a clean revert was not possible.

The original issue (llvm#57022) is no longer reproducible even with this
patch, so we can remove the suppression.

This is in line with our goal to make -Wenum-constexpr-conversion
a non-downgradeable error, see llvm#59036.
carlosgalvezp added a commit that referenced this issue Feb 11, 2024
This effectively reverts commit
9ff0cc7. For some reason "git revert"
lead to "no changes" after fixing conflicts, so a clean revert was not
possible.

The original issue (#57022) is no longer reproducible even with this
patch, so we can remove the suppression.

This is in line with our goal to make -Wenum-constexpr-conversion a
non-downgradeable error, see #59036.

Co-authored-by: Carlos Gálvez <[email protected]>
@mborgerding
Copy link

mborgerding commented Dec 3, 2024

I'm not a contributor to this fine project, so my influence is understandably small. If the LLVM maintainers value purity over usability and practicality, I respect that. Take the high road. It's your project. It's your passion. You decide your goals.

But I don't agree with breaking refusing to compile years-old code for pedantic adherence to the c++17 standard (even applied back to c++11 compilation ?), without any workaround.

It sounds like I need to caution my customers who build on RedHat Linux or older Ubuntu LTS that the workaround will stop working soon and they should avoid using upcoming LLVM compilers unless they are also willing to upgrade or fix other required libs beyond the package manager versions.

@RJVB
Copy link

RJVB commented Dec 3, 2024 via email

@AaronBallman
Copy link
Collaborator

I'm not a contributor to this fine project, so my influence is understandably small. If the LLVM maintainers value purity over usability and practicality, I respect that. Take the high road. It's your project. It's your passion. You decide your goals.

We're following the guidance from the standards committee; they thought this issue was sufficiently important to change its behavior as a defect report:

https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1766
https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#2338

But I don't agree with breaking years-old code for pedantic adherence to the c++17 standard (even applied back to c++11 compilation ?), without any workaround.

We're not breaking years-old code; we're diagnosing code that's been broken for years and was hoping the behavior from the compiler was reasonable. Enumerations in C++ are not the same as enumerations in C in that C++ enumerations use a notional power-of-two integer type as the underlying type of an unscoped enumeration, not int as in C. And compilers can (and do) take advantage of this outside of constexpr contexts: https://gcc.godbolt.org/z/evPx7oYe4

(There is an interesting question there of whether this diagnostic should be tied to -fstrict-enums or not. Currently it's not.)

It sounds like I need to caution my customers who build on RedHat Linux or older Ubuntu LTS that the workaround will stop working soon and they should avoid using upcoming LLVM compilers unless they are also willing to upgrade other required libs beyond the package manager.

If the changes are sufficiently disruptive, we can certainly walk them back and leave it as an error which can be downgraded to a warning for another release or two. However, there's not been much indication yet that this is significantly disruptive in practice. Are you finding otherwise?

@RJVB
Copy link

RJVB commented Dec 3, 2024 via email

@mborgerding
Copy link

However, there's not been much indication yet that this is significantly disruptive in practice. Are you finding otherwise?

Has the upcoming unreleased version been disruptive in practice? Shockingly no. ;)

Recent versions of intel LLVM-based icpx were slightly disruptive, but not too bad. There was a compiler flag workaround.

@AaronBallman
Copy link
Collaborator

Has the upcoming unreleased version been disruptive in practice?

Correct. The unreleased version of the compiler gets nontrivial amounts of testing in practice, so if this broke something significant, we're likely to hear about it before the final rc's get made.

@carlosgalvezp
Copy link
Contributor Author

There is an interesting question there of whether this diagnostic should be tied to -fstrict-enums or not. Currently it's not.

That could be an option. It might however open the door to allowing similar things with similar flags, e.g. don't error on signed integer overflow in constant expressions with -fwrapv.

@AaronBallman
Copy link
Collaborator

There is an interesting question there of whether this diagnostic should be tied to -fstrict-enums or not. Currently it's not.

That could be an option. It might however open the door to allowing similar things with similar flags, e.g. don't error on signed integer overflow in constant expressions with -fwrapv.

Yeah, the behavior could end up inconsistent in ways that are similarly frustrating -- e.g., a library developer who compiles with the flag disabled (which it is by default) may not get any diagnostics, ship their library, and then a user who does enable the flag consumes the library header and gets errors they can't fix.

So in terms of options I see currently, we have:

  1. status quo -- it's a hard error in Clang 20 in all C++ language modes, based on the rationale that we're following the committee's recommendations and we're diagnosing code that has undefined behavior in a constant expression, which is expected behavior for any UB in a constant expression.
  2. don't implement as a DR -- it's still a hard error in Clang 20, but only in C++17 and up. In C++11/14, it would be a warning and users would lose the property of "no UB in a constant expression" in a known case. This would be based on user disruption from the changes. It may be seen as inconsistent by users because it's UB in all C++ language modes.
  3. don't implement the changes at all -- it's back to being a warning only in Clang 20 in all language modes. This would be based on user disruption for the changes. It would be self-consistent, unfortunately diverge from what the standard requires (in a conforming way though), but it would still be more than any other C++ compiler does currently in terms of diagnosing the situation: https://godbolt.org/z/aP976TPe9 If we went with this option, I think we'd have to go back to WG21 with a paper explaining that the DRs resolved wrong and how we'd rather see them resolved, but I don't expect much appetite from WG21 for a change in this area (the only change I think the committee could make would be to stop using the power-of-two notional underlying type for unscoped enumerations, but that's been the definition in C++ since 1998 and there are compilers which take advantage of it via things like -fstrict-enums).

@RJVB
Copy link

RJVB commented Dec 4, 2024 via email

@carlosgalvezp
Copy link
Contributor Author

So in terms of options I see currently

Personally I'm happy with 1. I think option 2 is probably a good tradeoff, but ultimately is just delaying the problem.

Doesn't overflow always simply discard the overflowing bits?

No, this is simply undefined behavior, both in C and C++. Check the following example: the compiler will always emit "false" regardless of the input:
https://godbolt.org/z/3sjYTb7Y9

@RJVB
Copy link

RJVB commented Dec 4, 2024 via email

@AaronBallman
Copy link
Collaborator

I know this was discussed before, but wasn't it declared UB only in C++17?

Yes and no. In C++14 and before, it was unspecified behavior rather than undefined behavior. Unspecified behavior means "you'll get a result but it doesn't have to make sense or be consistent", whereas undefined behavior means "you'll get what you get, no promises about anything". Both are indicative of the user's code being invalid.

Additionally, there's two ways issues get resolved in WG21: either as a Defect Report (DR) or not. If an issue is not resolved as a DR, then the behavior is expected to change in that release going forward. If an issue is resolved as a DR, then the behavior is expected to change in all language modes where the issue could appear. So this was declared UB in C++17, but as a DR, which means the committee would like us to treat it as though it was always undefined rather than unspecified.

That idea must come from somewhere, and clang++ has clearly managed to avoid miscompiling code offending this principle until now. Don't mistake "users" for compiler experts, I think the vast majority won't see an inconsistency when a standard is being followed to the letter (and not more than that). After all the whole debate here is about old code that may not even follow C++11 .

There are natural tensions here. Some people want existing code to work at all costs. Some people don't care at all about existing code if they can prove that code is "broken" somehow. Most folks are somewhere in between those two extremes. WG21 wants a perfect language which never has bugs, users want their existing code to continue to work but are fine with behavioral changes so long as it makes things faster, and implementers have to find ways to work with both sets of desires.

You missed an option, I think: 4) drop support for obsolete C++ standards (arbitrarily defined as pre-C++17). That would be perfectly consistent with LLVM's, erm, forward looking approach.

That is an option, but it's a non-starter because there is far too much old code that still needs to be compiled. For example, think about a Linux distribution and how many packages it builds -- many of those are C++98/03 and quite don't see regular updates, but still need to compile with the same compiler as other packages.

@RJVB
Copy link

RJVB commented Dec 4, 2024 via email

@mborgerding
Copy link

From my limited perspective ... my and my customers' needs would be addressed by the option that differentiates at c++17:

  • for pre 17: a dire warning about UB (not suppressible?)
  • for post 17: a hard error

@RJVB
Copy link

RJVB commented Dec 4, 2024 via email

@AaronBallman
Copy link
Collaborator

From my limited perspective ... my and my customers' needs would be addressed by the option that differentiates at c++17:

* for pre 17: a dire warning about UB (not suppressible?)
* for post 17: a hard error

It would help me if I understood your needs better. I know what you want, but I don't know the severity of why you want it. Is there something preventing your customers from fixing their code? Is this impacting 1000s of customers? Something else?

@mborgerding
Copy link

The problem occurs when it's not their code that needs fixing. It's the development libraries (e.g. boost) their applications build against, which are preferably provided by package manger versions.

@AaronBallman
Copy link
Collaborator

It was tongue in cheek of course, but... Hah, consider how many people are also using older hardware that doesn't run a current version of whatever OS family they use (and how happily LLVM dropped official support for those) ... How many Linux distributions even use clang as their system compiler?

The Linux kernel itself, FreeBSD, Darwin, others...

Plus, that code gotta be "fixed" sometime, no? O:^) (I'm not really up to speed how C++ standards "work", but I'd presume they're incremental so most code that's a proper standard X will also conform to standard X+1?)

The way I'd describe it is like this: The C committee works really hard to avoid breaking existing code when updating standards, but it still happens on occasion. The C++ committee knowingly breaks existing code when updating standards, but does so in an opinionated way that aims to limit the blast radius somewhat (e.g., they'll break "bad", "ugly", or "overly clever" code, but they won't break "important" code, where all of these are value judgements).

@AaronBallman
Copy link
Collaborator

The problem occurs when it's not their code that needs fixing. It's the development libraries (e.g. boost) their applications build against, which are preferably provided by package manger versions.

Apologies if there were details elsewhere that I missed, but the issue is fixed in Boost. So if a user has a package manager which provides Clang 20 (releases mid 2025), wouldn't that package manager also already be providing the Boost (released in mid-2024) with the fix in it?

@RJVB
Copy link

RJVB commented Dec 4, 2024 via email

@mborgerding
Copy link

RedHat Enterprise Linux 9.5 is the newest. It ships with boost 1.75. The bug was not fixed until 1.81
If someone is building up a system months from now they may grab the latest clang or intel compiler based thereon and expect it to work. You might say the fault is RedHat's outdated versions. But right or wrong, compilers are expected to be backwards compatible. If you violate that condition too often, you'll lose the user.

I'm ambivalent which way you go. I appreciate that any pains from a hard error would be limited in scope and time; it would be cleaner in the long run to just "rip off the bandage". But it is still pain. Pain that could be avoided with either a workaround or limiting the hard error to >= c++17.
I appreciate the modularity and exacting nature of llvm/clang. It finds good diagnostics above what we get from g++. We'll continue to use it as a regression testing platform no matter what our customers do. But intel compilers have historically been a PITA to support. It would sometimes seem as if there was an evil genie (or lawyer) reading through the standard, carefully looking for new and inventive ways to break existing code. Now that intel has switched over to LLVM, they inherit your decisions in this regard. How do you want to proceed?

On a related note, does anyone know how to tell upon which version of LLVM a particular intel icpx is based?

@AaronBallman
Copy link
Collaborator

Is there even C++ code in the Linux kernel?

I'm not certain where this discussion landed: https://www.phoronix.com/news/CPP-Linux-Kernel-2024-Discuss

I did try to build one with clang, but quickly discovered that wasn't a good idea (IIRC it ended up not having support for dynamically loaded extensions).

You may want to check out https://www.kernel.org/doc/html/latest/kbuild/llvm.html

@AaronBallman
Copy link
Collaborator

RedHat Enterprise Linux 9.5 is the newest. It ships with boost 1.75. The bug was not fixed until 1.81 If someone is building up a system months from now they may grab the latest clang or intel compiler based thereon and expect it to work. You might say the fault is RedHat's outdated versions. But right or wrong, compilers are expected to be backwards compatible. If you violate that condition too often, you'll lose the user.

Thanks, that's helpful!

I'm ambivalent which way you go. I appreciate that any pains from a hard error would be limited in scope and time; it would be cleaner in the long run to just "rip off the bandage". But it is still pain. Pain that could be avoided with either a workaround or limiting the hard error to >= c++17. I appreciate the modularity and exacting nature of llvm/clang. It finds good diagnostics above what we get from g++. We'll continue to use it as a regression testing platform no matter what our customers do. But intel compilers have historically been a PITA to support. It would sometimes seem as if there was an evil genie (or lawyer) reading through the standard, carefully looking for new and inventive ways to break existing code. Now that intel has switched over to LLVM, they inherit your decisions in this regard. How do you want to proceed?

Intel's downstream can (and does) carry patches to deviate from the upstream behavior, as with any of the other downstream consumers of Clang. In upstream, we try to pick behaviors that will minimize the need for downstreams to patch, so downstreams still do impact the decisions we make.

@RJVB
Copy link

RJVB commented Dec 4, 2024 via email

@mborgerding
Copy link

anyone know how to tell upon which version of LLVM a particular intel icpx is based?

I did not easily find anything official, but from fiddling with godbolt,
I infer clang major.minor maps to icpx version as follows
19.0 --> 2024.2.0 up to at least 2025.0.0
18.0 --> 2024.1.0
17.0 --> 2023.2.1 through 2024.0.0
16.0 --> 2023.0.0 through 2023.1.0
...

@AaronBallman
Copy link
Collaborator

(There is an interesting question there of whether this diagnostic should be tied to -fstrict-enums or not. Currently it's not.)

Related, Jonathan filed this issue in GCC: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117963

saagarjha pushed a commit to ahjragaas/binutils-gdb that referenced this issue Dec 22, 2024
This fixes PR 31331:
https://sourceware.org/bugzilla/show_bug.cgi?id=31331

Currently, enum-flags.h is suppressing the warning
-Wenum-constexpr-conversion coming from recent versions of Clang.
This warning is intended to be made a compiler error
(non-downgradeable) in future versions of Clang:

llvm/llvm-project#59036

The rationale is that casting a value of an integral type into an
enumeration is Undefined Behavior if the value does not fit in the
range of values of the enum:
https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1766

Undefined Behavior is not allowed in constant expressions, leading to
an ill-formed program.

In this case, in enum-flags.h, we are casting the value -1 to an enum
of a positive range only, which is UB as per the Standard and thus not
allowed in a constexpr context.

The purpose of doing this instead of using std::underlying_type is
because, for C-style enums, std::underlying_type typically returns
"unsigned int". However, when operating with it arithmetically, the
enum is promoted to *signed* int, which is what we want to avoid.

This patch solves this issue as follows:

* Use std::underlying_type and remove the custom enum_underlying_type.

* Ensure that operator~ is called always on an unsigned integer. We do
  this by casting the input enum into std::size_t, which can fit any
  unsigned integer. We have the guarantee that the cast is safe,
  because we have checked that the underlying type is unsigned. If the
  enum had negative values, the underlying type would be signed.

  This solves the issue with C-style enums, but also solves a hidden
  issue: enums with underlying type of std::uint8_t or std::uint16_t are
  *also* promoted to signed int. Now they are all explicitly casted
  to the largest unsigned int type and operator~ is safe to use.

* There is one more thing that needs fix. Currently, operator~ is
  implemented as follows:

  return (enum_type) ~underlying(e);

  After applying ~underlying(e), the result is a very large value,
  which we then cast to "enum_type". This cast is Undefined Behavior
  if the large value does not fit in the range of the enum. For
  C++ enums (scoped and/or with explicit underlying type), the range
  of the enum is the entire range of the underlying type, so the cast
  is safe. However, for C-style enums, the range is the smallest
  bit-field that can hold all the values of the enumeration. So the
  range is a lot smaller and casting a large value to the enum would
  invoke Undefined Behavior.

  To solve this problem, we create a new trait
  EnumHasFixedUnderlyingType, to ensure operator~ may only be called
  on C++-style enums. This behavior is roughly the same as what we
  had on trunk, but relying on different properties of the enums.

* Once this is implemented, the following tests fail to compile:

  CHECK_VALID (true,  int,  true ? EF () : EF2 ())

  This is because it expects the enums to be promoted to signed int,
  instead of unsigned int (which is the true underlying type).

  I propose to remove these tests altogether, because:

  - The comment nearby say they are not very important.
  - Comparing 2 enums of different type like that is strange, relies
    on integer promotions and thus hurts readability. As per comments
    in the related PR, we likely don't want this type of code in gdb
    code anyway, so there's no point in testing it.
  - Most importantly, this type of comparison will be ill-formed in
    C++26 for regular enums, so enum_flags does not need to emulate
    that.

Since this is the only place where the warning was suppressed, remove
also the corresponding macro in include/diagnostics.h.

The change has been tested by running the entire gdb test suite
(make check) and comparing the results (testsuite/gdb.sum) against
trunk. No noticeable differences have been observed.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31331
Tested-by: Keith Seitz <[email protected]>
Approved-By: Tom Tromey <[email protected]>
@MaxEW707
Copy link
Contributor

MaxEW707 commented Jan 10, 2025

I am a little confused how the enum of unfixed type DR in C++17 and how that relates to C interoperability especially on Win32 APIs.
I ran into this error a couple of weeks ago. I try to keep up with the C and C++ std but time isn't always available so forgive me if anything I say below is incorrect.

On MSVC all enums of unfixed types have int as the underlying type. I am not worrying about the MSVC /Zc:enumTypes option here as it isn't used in shipping code since it breaks a lot of Win32.

A lot of Win32/DirectX/etc APIs use enums for constants instead of macros like POSIX.
The Windows SDK headers tend to add enum values over time but code in large projects need to build/ship against older Windows SDK headers. It is common to use runtime detection and your own constants to work around needing to ship against older Windows SDK headers especially in shared code that is used across different shipping products with different minimum specs.

Take the HeapQueryInformation Win32 API which uses the HEAP_INFORMATION_CLASS enum.

On Windows SDK 22621 that enum is defined as

typedef enum _HEAP_INFORMATION_CLASS {
    HeapCompatibilityInformation = 0,
    HeapEnableTerminationOnCorruption = 1

#if ((NTDDI_VERSION > NTDDI_WINBLUE) || \
     (NTDDI_VERSION == NTDDI_WINBLUE && defined(WINBLUE_KBSPRING14)))
    ,
    HeapOptimizeResources = 3
#endif
    ,
    HeapTag = 7
} HEAP_INFORMATION_CLASS;

On Windows SDK 19041 that enum is defined as

typedef enum _HEAP_INFORMATION_CLASS {
    HeapCompatibilityInformation = 0,
    HeapEnableTerminationOnCorruption = 1

#if ((NTDDI_VERSION > NTDDI_WINBLUE) || \
     (NTDDI_VERSION == NTDDI_WINBLUE && defined(WINBLUE_KBSPRING14)))
    ,
    HeapOptimizeResources = 3
#endif
} HEAP_INFORMATION_CLASS;

Having code such as

static constexpr HEAP_INFORMATION_CLASS kHeapTag = HEAP_INFORMATION_CLASS(7);

// Do runtime detection or just ignore a failure of `ERROR_INVALID_PARAMETER` since HeapTag is a debugging API
HeapQueryInformat(..., kHeapTag, ...);

now fails to compile which I understand the rationale for from the DR in C++17.

However from my understanding C allows any valid value that is representable by the underlying unfixed enum type.
Given the definition of HEAP_INFORMATION_CLASS above in 19041 and assuming MSVC compat so the enum is an int; in C any int value is valid but in C++17 only the values of 0-3 are valid.

// UB in C++17 as far as I understand when building with the definition above in 19041
HeapQueryInformat(...,  HEAP_INFORMATION_CLASS(7), ...);

If my understanding is correct one of the following would have to happen

  • Win32 headers need to be updated to make enum decls have a fixed type in C++, 'enum HEAP_INFORMATION_CLASS : int`.
  • MSVC may not follow this part of the spec and thus clang in MSVC compat mode shouldn't either. Don't treat this as UB for MSVC.

In my toy example above I could just check WDK_NTDDI_VERSION or other NTDDI macros from sdkddkver.h and not try to use HeapTag if building against older headers even if we may be running on a system that has such support but I just needed a small example.

I need to think a bit more about MSVC compat in clang here. Maybe we tie this to -fstrict-enums when in MSVC compat mode?
AFAIK MSVC does not do any optimizations based on the C++ definition of a enum with an unfixed underlying type and assuming clang only does such optimizations with -fstrict-enums I think that may be an option for MSVC compat?

rocm-ci pushed a commit to ROCm/ROCgdb that referenced this issue Jan 21, 2025
This fixes PR 31331:
https://sourceware.org/bugzilla/show_bug.cgi?id=31331

Currently, enum-flags.h is suppressing the warning
-Wenum-constexpr-conversion coming from recent versions of Clang.
This warning is intended to be made a compiler error
(non-downgradeable) in future versions of Clang:

llvm/llvm-project#59036

The rationale is that casting a value of an integral type into an
enumeration is Undefined Behavior if the value does not fit in the
range of values of the enum:
https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1766

Undefined Behavior is not allowed in constant expressions, leading to
an ill-formed program.

In this case, in enum-flags.h, we are casting the value -1 to an enum
of a positive range only, which is UB as per the Standard and thus not
allowed in a constexpr context.

The purpose of doing this instead of using std::underlying_type is
because, for C-style enums, std::underlying_type typically returns
"unsigned int". However, when operating with it arithmetically, the
enum is promoted to *signed* int, which is what we want to avoid.

This patch solves this issue as follows:

* Use std::underlying_type and remove the custom enum_underlying_type.

* Ensure that operator~ is called always on an unsigned integer. We do
  this by casting the input enum into std::size_t, which can fit any
  unsigned integer. We have the guarantee that the cast is safe,
  because we have checked that the underlying type is unsigned. If the
  enum had negative values, the underlying type would be signed.

  This solves the issue with C-style enums, but also solves a hidden
  issue: enums with underlying type of std::uint8_t or std::uint16_t are
  *also* promoted to signed int. Now they are all explicitly casted
  to the largest unsigned int type and operator~ is safe to use.

* There is one more thing that needs fix. Currently, operator~ is
  implemented as follows:

  return (enum_type) ~underlying(e);

  After applying ~underlying(e), the result is a very large value,
  which we then cast to "enum_type". This cast is Undefined Behavior
  if the large value does not fit in the range of the enum. For
  C++ enums (scoped and/or with explicit underlying type), the range
  of the enum is the entire range of the underlying type, so the cast
  is safe. However, for C-style enums, the range is the smallest
  bit-field that can hold all the values of the enumeration. So the
  range is a lot smaller and casting a large value to the enum would
  invoke Undefined Behavior.

  To solve this problem, we create a new trait
  EnumHasFixedUnderlyingType, to ensure operator~ may only be called
  on C++-style enums. This behavior is roughly the same as what we
  had on trunk, but relying on different properties of the enums.

* Once this is implemented, the following tests fail to compile:

  CHECK_VALID (true,  int,  true ? EF () : EF2 ())

  This is because it expects the enums to be promoted to signed int,
  instead of unsigned int (which is the true underlying type).

  I propose to remove these tests altogether, because:

  - The comment nearby say they are not very important.
  - Comparing 2 enums of different type like that is strange, relies
    on integer promotions and thus hurts readability. As per comments
    in the related PR, we likely don't want this type of code in gdb
    code anyway, so there's no point in testing it.
  - Most importantly, this type of comparison will be ill-formed in
    C++26 for regular enums, so enum_flags does not need to emulate
    that.

Since this is the only place where the warning was suppressed, remove
also the corresponding macro in include/diagnostics.h.

The change has been tested by running the entire gdb test suite
(make check) and comparing the results (testsuite/gdb.sum) against
trunk. No noticeable differences have been observed.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31331
Tested-by: Keith Seitz <[email protected]>
Approved-By: Tom Tromey <[email protected]>
(cherry picked from commit 4a0b2cb)
Change-Id: I5a9918859deca5f219c20d3a7215fa5adf762d91
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer enhancement Improving things as opposed to bug fixing, e.g. new or missing feature
Projects
None yet