Skip to content

Commit 526633f

Browse files
committed
Move from union to variant
Signed-off-by: Arvind Sudarsanam <[email protected]>
1 parent 7350a52 commit 526633f

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

llvm/include/llvm/Support/Base64.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ llvm::Error decodeBase64(llvm::StringRef Input, std::vector<char> &Output);
5959
// General-purpose Base64 encoder/decoder class wrapper.
6060
class Base64 {
6161
public:
62-
using byte = uint8_t;
62+
using byte = std::byte;
6363

6464
// Get the size of the encoded byte sequence of given size.
6565
static size_t getEncodedSize(size_t SrcSize);

llvm/include/llvm/Support/PropertySetIO.h

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "llvm/Support/MemoryBuffer.h"
4141
#include "llvm/Support/raw_ostream.h"
4242
#include "llvm/Support/xxhash.h"
43+
#include <variant>
4344

4445
namespace llvm {
4546
namespace util {
@@ -52,7 +53,7 @@ class PropertyValue {
5253
// value data in some cases for later reading at runtime, so size_t is not
5354
// suitable as its size varies.
5455
using SizeTy = uint64_t;
55-
using byte = uint8_t;
56+
using byte = std::byte;
5657

5758
// Defines supported property types
5859
enum Type { first = 0, NONE = first, UINT32, BYTE_ARRAY, last = BYTE_ARRAY };
@@ -68,8 +69,10 @@ class PropertyValue {
6869
}
6970

7071
~PropertyValue() {
71-
if ((getType() == BYTE_ARRAY) && Val.ByteArrayVal)
72-
delete[] Val.ByteArrayVal;
72+
if (std::holds_alternative<byte *>(Val)) {
73+
byte *ByteArrayVal = std::get<byte *>(Val);
74+
delete ByteArrayVal;
75+
}
7376
}
7477

7578
PropertyValue() = default;
@@ -95,7 +98,7 @@ class PropertyValue {
9598
uint32_t asUint32() const {
9699
if (Ty != UINT32)
97100
llvm_unreachable("must be UINT32 value");
98-
return Val.UInt32Val;
101+
return std::get<uint32_t>(Val);
99102
}
100103

101104
// Get raw data size in bits.
@@ -104,8 +107,10 @@ class PropertyValue {
104107
llvm_unreachable("must be BYTE_ARRAY value");
105108
SizeTy Res = 0;
106109

107-
for (size_t I = 0; I < sizeof(SizeTy); ++I)
108-
Res |= (SizeTy)Val.ByteArrayVal[I] << (8 * I);
110+
for (size_t I = 0; I < sizeof(SizeTy); ++I) {
111+
auto ByteArrayVal = std::get<byte *>(Val);
112+
Res |= (SizeTy)ByteArrayVal[I] << (8 * I);
113+
}
109114
return Res;
110115
}
111116

@@ -126,14 +131,17 @@ class PropertyValue {
126131
const byte *asRawByteArray() const {
127132
if (Ty != BYTE_ARRAY)
128133
llvm_unreachable("must be BYTE_ARRAY value");
129-
return Val.ByteArrayVal;
134+
auto &ByteArrayVal = std::get<byte *>(Val);
135+
return ByteArrayVal;
130136
}
131137

132138
// Get byte array data excluding the leading bytes encoding the size.
133139
const byte *asByteArray() const {
134140
if (Ty != BYTE_ARRAY)
135141
llvm_unreachable("must be BYTE_ARRAY value");
136-
return Val.ByteArrayVal + sizeof(SizeTy);
142+
143+
auto ByteArrayVal = std::get<byte *>(Val);
144+
return ByteArrayVal + sizeof(SizeTy);
137145
}
138146

139147
bool isValid() const { return getType() != NONE; }
@@ -150,7 +158,7 @@ class PropertyValue {
150158
SizeTy size() const {
151159
switch (Ty) {
152160
case UINT32:
153-
return sizeof(Val.UInt32Val);
161+
return sizeof(std::get<uint32_t>(Val));
154162
case BYTE_ARRAY:
155163
return getRawByteArraySize();
156164
default:
@@ -163,12 +171,7 @@ class PropertyValue {
163171
void copy(const PropertyValue &P);
164172

165173
Type Ty = NONE;
166-
// TODO: replace this union with std::variant when uplifting to C++17
167-
union {
168-
uint32_t UInt32Val;
169-
// Holds first sizeof(size_t) bytes of size followed by actual raw data.
170-
byte *ByteArrayVal;
171-
} Val;
174+
std::variant<uint32_t, byte *> Val;
172175
};
173176

174177
/// Structure for specialization of DenseMap in PropertySetRegistry.

llvm/lib/Support/PropertySetIO.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ using namespace llvm;
2222

2323
namespace {
2424

25-
using byte = Base64::byte;
25+
using byte = std::byte;
2626

2727
::llvm::Error makeError(const Twine &Msg) {
2828
return createStringError(std::error_code{}, Msg);
@@ -135,11 +135,12 @@ namespace llvm {
135135
namespace util {
136136

137137
template <> uint32_t &PropertyValue::getValueRef<uint32_t>() {
138-
return Val.UInt32Val;
138+
return std::get<uint32_t>(Val);
139139
}
140140

141141
template <> byte *&PropertyValue::getValueRef<byte *>() {
142-
return Val.ByteArrayVal;
142+
auto &ByteArrayVal = std::get<byte *>(Val);
143+
return ByteArrayVal;
143144
}
144145

145146
template <> PropertyValue::Type PropertyValue::getTypeTag<uint32_t>() {
@@ -157,15 +158,17 @@ PropertyValue::PropertyValue(const byte *Data, SizeTy DataBitSize) {
157158
constexpr size_t SizeFieldSize = sizeof(SizeTy);
158159

159160
// Allocate space for size and data.
160-
Val.ByteArrayVal = new byte[SizeFieldSize + DataSize];
161+
Val = new byte[SizeFieldSize + DataSize];
161162

162163
// Write the size into first bytes.
163164
for (size_t I = 0; I < SizeFieldSize; ++I) {
164-
Val.ByteArrayVal[I] = (byte)DataBitSize;
165+
auto ByteArrayVal = std::get<byte *>(Val);
166+
ByteArrayVal[I] = (byte)DataBitSize;
165167
DataBitSize >>= ByteSizeInBits;
166168
}
167169
// Append data.
168-
std::memcpy(Val.ByteArrayVal + SizeFieldSize, Data, DataSize);
170+
auto ByteArrayVal = std::get<byte *>(Val);
171+
std::memcpy(ByteArrayVal + SizeFieldSize, Data, DataSize);
169172
}
170173

171174
PropertyValue::PropertyValue(const PropertyValue &P) { *this = P; }
@@ -175,8 +178,10 @@ PropertyValue::PropertyValue(PropertyValue &&P) { *this = std::move(P); }
175178
PropertyValue &PropertyValue::operator=(PropertyValue &&P) {
176179
copy(P);
177180

178-
if (P.getType() == BYTE_ARRAY)
179-
P.Val.ByteArrayVal = nullptr;
181+
if (P.getType() == BYTE_ARRAY) {
182+
auto &ByteArrayVal = std::get<byte *>(P.Val);
183+
ByteArrayVal = nullptr;
184+
}
180185
P.Ty = NONE;
181186
return *this;
182187
}

0 commit comments

Comments
 (0)