Index: include/llvm/Support/Endian.h =================================================================== --- include/llvm/Support/Endian.h +++ include/llvm/Support/Endian.h @@ -338,6 +338,12 @@ using unaligned_int64_t = detail::packed_endian_specific_integral; +template +using little_enum_t = + detail::packed_endian_specific_integral; +template +using big_enum_t = detail::packed_endian_specific_integral; + namespace endian { template inline T read(const void *P, endianness E) { Index: include/llvm/Support/SwapByteOrder.h =================================================================== --- include/llvm/Support/SwapByteOrder.h +++ include/llvm/Support/SwapByteOrder.h @@ -115,6 +115,13 @@ return out.d; } +template +inline typename std::enable_if::value, T>::type +getSwappedBytes(T C) { + return static_cast( + getSwappedBytes(static_cast::type>(C))); +} + template inline void swapByteOrder(T &Value) { Value = getSwappedBytes(Value); Index: unittests/Support/EndianTest.cpp =================================================================== --- unittests/Support/EndianTest.cpp +++ unittests/Support/EndianTest.cpp @@ -200,4 +200,13 @@ EXPECT_EQ(*big_val, *little_val); } +TEST(Endian, PacketEndianSpecificIntegralAsEnum) { + enum class Test : uint16_t { ONETWO = 0x0102, TWOONE = 0x0201 }; + unsigned char bytes[] = {0x01, 0x02}; + using LittleTest = little_enum_t; + using BigTest = big_enum_t; + EXPECT_EQ(Test::TWOONE, *reinterpret_cast(bytes)); + EXPECT_EQ(Test::ONETWO, *reinterpret_cast(bytes)); +} + } // end anon namespace