4040#include " llvm/Support/MemoryBuffer.h"
4141#include " llvm/Support/raw_ostream.h"
4242#include " llvm/Support/xxhash.h"
43+ #include < variant>
4344
4445namespace llvm {
4546namespace 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.
0 commit comments