From: Pavel Labath Date: Mon, 11 Mar 2019 09:06:18 +0000 (+0000) Subject: [Support/Endian] Add support for endian-specific enums X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fbbf45ee832884375edd9e19f90eab4065ab2091;p=llvm [Support/Endian] Add support for endian-specific enums Summary: Binary formats often include various enumerations or bitsets, but using endian-specific types for accessing them is tricky because they currently only support integral types. This is particularly true for scoped enums (enum class), as these are not implicitly convertible to integral types, and so one has to perform two casts just to read the enum value. This fixes that support by adding first-class support for enumeration types to endian-specific types. The support for them was already almost working -- all I needed to do was overload getSwappedBytes for enumeration types (which casts the enum to its underlying type and performs the conversion there). I also add some convenience template aliases to simplify declaring endian-specific enums. Reviewers: Bigcheese, zturner Subscribers: kristina, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D59141 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@355812 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Support/Endian.h b/include/llvm/Support/Endian.h index 5c1e89ae793..d8be94427d7 100644 --- a/include/llvm/Support/Endian.h +++ b/include/llvm/Support/Endian.h @@ -338,6 +338,17 @@ using unaligned_int32_t = using unaligned_int64_t = detail::packed_endian_specific_integral; +template +using little_t = detail::packed_endian_specific_integral; +template +using big_t = detail::packed_endian_specific_integral; + +template +using aligned_little_t = + detail::packed_endian_specific_integral; +template +using aligned_big_t = detail::packed_endian_specific_integral; + namespace endian { template inline T read(const void *P, endianness E) { diff --git a/include/llvm/Support/SwapByteOrder.h b/include/llvm/Support/SwapByteOrder.h index f58b4d911d4..380ca0a781f 100644 --- a/include/llvm/Support/SwapByteOrder.h +++ b/include/llvm/Support/SwapByteOrder.h @@ -115,6 +115,13 @@ inline double getSwappedBytes(double C) { 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); diff --git a/unittests/Support/EndianTest.cpp b/unittests/Support/EndianTest.cpp index c2b29288537..b5e4a9c8d14 100644 --- a/unittests/Support/EndianTest.cpp +++ b/unittests/Support/EndianTest.cpp @@ -200,4 +200,13 @@ TEST(Endian, PackedEndianSpecificIntegral) { 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_t; + using BigTest = big_t; + EXPECT_EQ(Test::TWOONE, *reinterpret_cast(bytes)); + EXPECT_EQ(Test::ONETWO, *reinterpret_cast(bytes)); +} + } // end anon namespace