Skip to content

Commit 24374ae

Browse files
committed
[Utility] Modernize C-style cats
Replaces the remaining C-style casts with explicit casts in Utility. The motivation is that they are (1) easier to spot and (2) don't have multiple meanings. llvm-svn: 361458
1 parent 3d68a38 commit 24374ae

File tree

16 files changed

+263
-225
lines changed

16 files changed

+263
-225
lines changed

lldb/include/lldb/Utility/Args.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,9 @@ class Args {
267267
if (total_byte_size == 8)
268268
return true;
269269

270-
const uint64_t max = ((uint64_t)1 << (uint64_t)(total_byte_size * 8)) - 1;
270+
const uint64_t max = (static_cast<uint64_t>(1)
271+
<< static_cast<uint64_t>(total_byte_size * 8)) -
272+
1;
271273
return uval64 <= max;
272274
}
273275

@@ -279,7 +281,9 @@ class Args {
279281
if (total_byte_size == 8)
280282
return true;
281283

282-
const int64_t max = ((int64_t)1 << (uint64_t)(total_byte_size * 8 - 1)) - 1;
284+
const int64_t max = (static_cast<int64_t>(1)
285+
<< static_cast<uint64_t>(total_byte_size * 8 - 1)) -
286+
1;
283287
const int64_t min = ~(max);
284288
return min <= sval64 && sval64 <= max;
285289
}

lldb/include/lldb/Utility/Endian.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static union EndianTest {
2323
} const endianTest = {0x01020304};
2424

2525
inline lldb::ByteOrder InlHostByteOrder() {
26-
return (lldb::ByteOrder)endianTest.bytes[0];
26+
return static_cast<lldb::ByteOrder>(endianTest.bytes[0]);
2727
}
2828

2929
// ByteOrder const InlHostByteOrder = (ByteOrder)endianTest.bytes[0];

lldb/include/lldb/Utility/Flags.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Flags {
6969
///
7070
/// \return
7171
/// The new flags after clearing all bits from \a mask.
72-
ValueType Clear(ValueType mask = ~(ValueType)0) {
72+
ValueType Clear(ValueType mask = ~static_cast<ValueType>(0)) {
7373
m_flags &= ~mask;
7474
return m_flags;
7575
}

lldb/include/lldb/Utility/RegisterValue.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ class RegisterValue {
4141
eTypeBytes
4242
};
4343

44-
RegisterValue() : m_type(eTypeInvalid), m_scalar((unsigned long)0) {}
44+
RegisterValue()
45+
: m_type(eTypeInvalid), m_scalar(static_cast<unsigned long>(0)) {}
4546

4647
explicit RegisterValue(uint8_t inst) : m_type(eTypeUInt8) { m_scalar = inst; }
4748

lldb/include/lldb/Utility/Scalar.h

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,40 +59,44 @@ class Scalar {
5959

6060
// Constructors and Destructors
6161
Scalar();
62-
Scalar(int v) : m_type(e_sint), m_float((float)0) {
62+
Scalar(int v) : m_type(e_sint), m_float(static_cast<float>(0)) {
6363
m_integer = llvm::APInt(sizeof(int) * 8, v, true);
6464
}
65-
Scalar(unsigned int v) : m_type(e_uint), m_float((float)0) {
65+
Scalar(unsigned int v) : m_type(e_uint), m_float(static_cast<float>(0)) {
6666
m_integer = llvm::APInt(sizeof(int) * 8, v);
6767
}
68-
Scalar(long v) : m_type(e_slong), m_float((float)0) {
68+
Scalar(long v) : m_type(e_slong), m_float(static_cast<float>(0)) {
6969
m_integer = llvm::APInt(sizeof(long) * 8, v, true);
7070
}
71-
Scalar(unsigned long v) : m_type(e_ulong), m_float((float)0) {
71+
Scalar(unsigned long v) : m_type(e_ulong), m_float(static_cast<float>(0)) {
7272
m_integer = llvm::APInt(sizeof(long) * 8, v);
7373
}
74-
Scalar(long long v) : m_type(e_slonglong), m_float((float)0) {
74+
Scalar(long long v) : m_type(e_slonglong), m_float(static_cast<float>(0)) {
7575
m_integer = llvm::APInt(sizeof(long long) * 8, v, true);
7676
}
77-
Scalar(unsigned long long v) : m_type(e_ulonglong), m_float((float)0) {
77+
Scalar(unsigned long long v)
78+
: m_type(e_ulonglong), m_float(static_cast<float>(0)) {
7879
m_integer = llvm::APInt(sizeof(long long) * 8, v);
7980
}
8081
Scalar(float v) : m_type(e_float), m_float(v) { m_float = llvm::APFloat(v); }
8182
Scalar(double v) : m_type(e_double), m_float(v) {
8283
m_float = llvm::APFloat(v);
8384
}
8485
Scalar(long double v, bool ieee_quad)
85-
: m_type(e_long_double), m_float((float)0), m_ieee_quad(ieee_quad) {
86+
: m_type(e_long_double), m_float(static_cast<float>(0)),
87+
m_ieee_quad(ieee_quad) {
8688
if (ieee_quad)
87-
m_float = llvm::APFloat(llvm::APFloat::IEEEquad(),
88-
llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
89-
((type128 *)&v)->x));
89+
m_float =
90+
llvm::APFloat(llvm::APFloat::IEEEquad(),
91+
llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
92+
(reinterpret_cast<type128 *>(&v))->x));
9093
else
91-
m_float = llvm::APFloat(llvm::APFloat::x87DoubleExtended(),
92-
llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
93-
((type128 *)&v)->x));
94+
m_float =
95+
llvm::APFloat(llvm::APFloat::x87DoubleExtended(),
96+
llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
97+
(reinterpret_cast<type128 *>(&v))->x));
9498
}
95-
Scalar(llvm::APInt v) : m_type(), m_float((float)0) {
99+
Scalar(llvm::APInt v) : m_type(), m_float(static_cast<float>(0)) {
96100
m_integer = llvm::APInt(v);
97101
switch (m_integer.getBitWidth()) {
98102
case 8:
@@ -252,7 +256,9 @@ class Scalar {
252256
if (total_byte_size == 8)
253257
return true;
254258

255-
const uint64_t max = ((uint64_t)1 << (uint64_t)(total_byte_size * 8)) - 1;
259+
const uint64_t max = (static_cast<uint64_t>(1)
260+
<< static_cast<uint64_t>(total_byte_size * 8)) -
261+
1;
256262
return uval64 <= max;
257263
}
258264

@@ -263,7 +269,9 @@ class Scalar {
263269
if (total_byte_size == 8)
264270
return true;
265271

266-
const int64_t max = ((int64_t)1 << (uint64_t)(total_byte_size * 8 - 1)) - 1;
272+
const int64_t max = (static_cast<int64_t>(1)
273+
<< static_cast<uint64_t>(total_byte_size * 8 - 1)) -
274+
1;
267275
const int64_t min = ~(max);
268276
return min <= sval64 && sval64 <= max;
269277
}

lldb/source/Utility/Args.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ void Args::EncodeEscapeSequences(const char *src, std::string &dst) {
541541
p += i - 1;
542542
unsigned long octal_value = ::strtoul(oct_str, nullptr, 8);
543543
if (octal_value <= UINT8_MAX) {
544-
dst.append(1, (char)octal_value);
544+
dst.append(1, static_cast<char>(octal_value));
545545
}
546546
}
547547
break;
@@ -561,7 +561,7 @@ void Args::EncodeEscapeSequences(const char *src, std::string &dst) {
561561

562562
unsigned long hex_value = strtoul(hex_str, nullptr, 16);
563563
if (hex_value <= UINT8_MAX)
564-
dst.append(1, (char)hex_value);
564+
dst.append(1, static_cast<char>(hex_value));
565565
} else {
566566
dst.append(1, 'x');
567567
}

lldb/source/Utility/DataBufferHeap.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ uint64_t DataBufferHeap::SetByteSize(uint64_t new_size) {
5252
}
5353

5454
void DataBufferHeap::CopyData(const void *src, uint64_t src_len) {
55-
const uint8_t *src_u8 = (const uint8_t *)src;
55+
const uint8_t *src_u8 = static_cast<const uint8_t *>(src);
5656
if (src && src_len > 0)
5757
m_data.assign(src_u8, src_u8 + src_len);
5858
else
5959
m_data.clear();
6060
}
6161

6262
void DataBufferHeap::AppendData(const void *src, uint64_t src_len) {
63-
m_data.insert(m_data.end(), (const uint8_t *)src,
64-
(const uint8_t *)src + src_len);
63+
m_data.insert(m_data.end(), static_cast<const uint8_t *>(src),
64+
static_cast<const uint8_t *>(src) + src_len);
6565
}
6666

6767
void DataBufferHeap::Clear() {

lldb/source/Utility/DataEncoder.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ DataEncoder::DataEncoder()
3434
// data must stay around as long as this object is valid.
3535
DataEncoder::DataEncoder(void *data, uint32_t length, ByteOrder endian,
3636
uint8_t addr_size)
37-
: m_start((uint8_t *)data), m_end((uint8_t *)data + length),
38-
m_byte_order(endian), m_addr_size(addr_size), m_data_sp() {}
37+
: m_start(static_cast<uint8_t *>(data)),
38+
m_end(static_cast<uint8_t *>(data) + length), m_byte_order(endian),
39+
m_addr_size(addr_size), m_data_sp() {}
3940

4041
// Make a shared pointer reference to the shared data in "data_sp" and set the
4142
// endian swapping setting to "swap", and the address size to "addr_size". The
@@ -90,7 +91,7 @@ uint32_t DataEncoder::SetData(void *bytes, uint32_t length, ByteOrder endian) {
9091
m_start = nullptr;
9192
m_end = nullptr;
9293
} else {
93-
m_start = (uint8_t *)bytes;
94+
m_start = static_cast<uint8_t *>(bytes);
9495
m_end = m_start + length;
9596
}
9697
return GetByteSize();

0 commit comments

Comments
 (0)