Repository navigation
logging: silence deprecated enum-enum conversion warnings - #4396
Conversation
Since C++20, bitwise operations between different enumeration types are deprecated, so compilers defaulting to a newer standard (e.g. gcc 16) emit -Wdeprecated-enum-enum-conversion for every log mask that combines LogFacilities, LogFlags, and LogLevels values. These enums intentionally occupy disjoint bit ranges of the same integer log mask. Define the six mixed-type operator| overloads centrally in logging_internal.h so the intended integer combination is spelled out once and all call sites compile warning-free in both C++17 and C++20 mode. Fixes cvmfs#4327 Assisted-by: Claude
|
I had a fix for #4327 ready when I found this PR, and the approach turned out to be identical, six The overloads themselves look right. All six orderings, But const int kLogWarning = kLogStdout | kLogShowSource | kLogNormal;Once Compiling
The constant expression part is checkable directly: char probe[kLogWarning == 9218 ? 1 : -1];compiles on To be clear about what this is and is not: the mask values are unchanged, so nothing logs differently, and nothing in the tree currently uses these three in a constant expression, so it is not a build break. It is a silent change to three exported constants that no warning reports, which is why I thought it worth raising rather than leaving to be found later. The fix is small. Keeping an explicit cast on the first operand selects the built-in operator, so those three stay foldable: const int kLogWarning =
static_cast<int>(kLogStdout) | kLogShowSource | kLogNormal; // NOLINTThat restores Happy to push that as a commit to this branch if that is easiest, or you can fold it in directly, whichever you prefer. One other thing I checked while I was in there, in case it is useful: |
The operator| overloads are ordinary functions, so kLogWarning, kLogInfoMsg and kLogVerboseMsg were no longer integral constant expressions under C++03, and every translation unit including this header picked up a static initialiser. Casting the first operand selects the built-in operator again, which restores constant folding while keeping the enum-enum conversion warnings silenced. Co-authored-by: manishpaulish <[email protected]> Assisted-by: Claude
|
Good catch, you are right. Folded it in as 5b07e09, with you credited as co-author on the commit. Before pushing I checked the rest of the surface: those three are the only Thanks for building the branch and measuring it instead of just opening a competing PR. |
vvolkl
left a comment
There was a problem hiding this comment.
This is a very nice solution, thank you!
With your permission, I would like to try this.
gcc 16 (defaulting to a newer C++ standard) warns with
-Wdeprecated-enum-enum-conversion for every log mask combining
LogFacilities, LogFlags and LogLevels, as reported in #4327. These
enums intentionally occupy disjoint bit ranges of one integer mask,
so this adds the six mixed-type operator| overloads centrally in
logging_internal.h: all ~50 call sites compile warning-free in both
C++17 and C++20 mode, with no behavior change.
Verified on Ubuntu 24.04 / gcc 13.3: a probe including the header
goes from 5 warnings to 0 with -std=gnu++20 and stays at 0 with
gnu++17; clang-format reports no changes and the T_Logging unit
tests pass.