diff options
author | Pavel Labath <pavel@labath.sk> | 2025-07-09 09:09:57 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-07-09 09:09:57 +0200 |
commit | cd561bf1f17941a875c9319cf728ff9fc13f0a5a (patch) | |
tree | c5c36586135cbb638a5171327340e09eafcb4208 /llvm/unittests/Support | |
parent | f0bc41181c0fd03069ca63a3b8b0f85e3c7cb477 (diff) | |
download | llvm-cd561bf1f17941a875c9319cf728ff9fc13f0a5a.zip llvm-cd561bf1f17941a875c9319cf728ff9fc13f0a5a.tar.gz llvm-cd561bf1f17941a875c9319cf728ff9fc13f0a5a.tar.bz2 |
[support] Make ScopedPrinter compatible with bitmask enums (#147512)
.. produced by ADT/BitmaskEnum.h.
These aren't implicitly convertible to an integer, so I needed to tweak
a couple of bitwise operations and add an explicit constructor for
HexNumber and FlagEntry.
Motivation: I'd like to use this in the SFrame data structures (#147264)
Diffstat (limited to 'llvm/unittests/Support')
-rw-r--r-- | llvm/unittests/Support/ScopedPrinterTest.cpp | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/llvm/unittests/Support/ScopedPrinterTest.cpp b/llvm/unittests/Support/ScopedPrinterTest.cpp index 72af331..1e2b138 100644 --- a/llvm/unittests/Support/ScopedPrinterTest.cpp +++ b/llvm/unittests/Support/ScopedPrinterTest.cpp @@ -8,11 +8,21 @@ #include "llvm/Support/ScopedPrinter.h" #include "llvm/ADT/APSInt.h" +#include "llvm/ADT/BitmaskEnum.h" #include "llvm/Support/Format.h" #include "gtest/gtest.h" #include <cmath> #include <vector> +namespace { +enum class BitmaskEnum : uint8_t { + F1 = 0x1, + F2 = 0x2, + LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/F2), +}; +LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE(); +} // namespace + using namespace llvm; TEST(JSONScopedPrinterTest, PrettyPrintCtor) { @@ -261,6 +271,12 @@ TEST_F(ScopedPrinterTest, PrintFlag) { {"SecondByte2", "Second2", 0x20u}, {"SecondByte3", "Second3", 0x30u}, {"ThirdByte1", "Third1", 0x100u}, {"ThirdByte2", "Third2", 0x200u}, {"ThirdByte3", "Third3", 0x300u}}; + + const EnumEntry<BitmaskEnum> ScopedFlags[] = { + {"F1", "AltF1", BitmaskEnum::F1}, + {"F2", "AltF2", BitmaskEnum::F2}, + }; + W.printFlags("ZeroFlag", 0, ArrayRef(SingleBitFlags)); W.printFlags("NoFlag", 1 << 3, ArrayRef(SingleBitFlags)); W.printFlags("Flag1", SingleBitFlags[1].Value, ArrayRef(SingleBitFlags)); @@ -286,6 +302,7 @@ TEST_F(ScopedPrinterTest, PrintFlag) { FirstByteMask, SecondByteMask); W.printFlags("FirstSecondThirdByteMask", 0x333u, ArrayRef(EnumFlags), FirstByteMask, SecondByteMask, ThirdByteMask); + W.printFlags("BitmaskEnum::F1", BitmaskEnum::F1, ArrayRef(ScopedFlags)); }; const char *ExpectedOut = R"(ZeroFlag [ (0x0) @@ -343,6 +360,9 @@ FirstSecondThirdByteMask [ (0x333) SecondByte3 (0x30) ThirdByte3 (0x300) ] +BitmaskEnum::F1 [ (0x1) + F1 (0x1) +] )"; const char *JSONExpectedOut = R"({ @@ -504,6 +524,15 @@ FirstSecondThirdByteMask [ (0x333) "Value": 768 } ] + }, + "BitmaskEnum::F1": { + "Value": 1, + "Flags": [ + { + "Name": "F1", + "Value": 1 + } + ] } })"; verifyAll(ExpectedOut, JSONExpectedOut, PrintFunc); |