Skip to content

Commit b877c81

Browse files
authored
Use C++-style casts (#1576)
1 parent e66da4c commit b877c81

33 files changed

+197
-173
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ else()
6161
if(MORE_WARNINGS)
6262
add_compile_options(-Werror -Wextra
6363
-Walloc-zero -Wcast-align -Wcast-qual -Wduplicated-branches -Wduplicated-cond
64-
-Wfloat-equal -Wlogical-op -Wnull-dereference -Wshift-overflow=2
64+
-Wfloat-equal -Wlogical-op -Wnull-dereference -Wold-style-cast -Wshift-overflow=2
6565
-Wstringop-overflow=4 -Wundef -Wuninitialized -Wunused
6666
-Wshadow # TODO: -Wshadow=compatible-local?
6767
-Wformat=2 -Wformat-overflow=2 -Wformat-truncation=1

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ checkdiff:
201201
develop:
202202
$Q${MAKE} WARNFLAGS="${WARNFLAGS} -Werror -Wextra \
203203
-Walloc-zero -Wcast-align -Wcast-qual -Wduplicated-branches -Wduplicated-cond \
204-
-Wfloat-equal -Wlogical-op -Wnull-dereference -Wshift-overflow=2 \
204+
-Wfloat-equal -Wlogical-op -Wnull-dereference -Wold-style-cast -Wshift-overflow=2 \
205205
-Wstringop-overflow=4 -Wundef -Wuninitialized -Wunused -Wshadow \
206206
-Wformat=2 -Wformat-overflow=2 -Wformat-truncation=1 \
207207
-Wno-format-nonliteral -Wno-strict-overflow -Wno-unused-but-set-variable \

include/asm/fstack.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ struct FileStackNode {
3030
// Meaningless at the root level, but gets written to the object file anyway, so init it
3131
uint32_t lineNo = 0;
3232

33-
// Set only if referenced: ID within the object file, -1 if not output yet
34-
uint32_t ID = -1;
33+
// Set only if referenced: ID within the object file, `UINT32_MAX` if not output yet
34+
uint32_t ID = UINT32_MAX;
3535

3636
// REPT iteration counts since last named node, in reverse depth order
3737
std::vector<uint32_t> &iters() { return data.get<std::vector<uint32_t>>(); }

include/asm/symbol.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct Symbol {
4545
>
4646
data;
4747

48-
uint32_t ID; // ID of the symbol in the object file (-1 if none)
48+
uint32_t ID; // ID of the symbol in the object file (`UINT32_MAX` if none)
4949
uint32_t defIndex; // Ordering of the symbol in the state file
5050

5151
bool isDefined() const { return type != SYM_REF; }
@@ -55,7 +55,7 @@ struct Symbol {
5555
bool isConstant() const {
5656
if (type == SYM_LABEL) {
5757
Section const *sect = getSection();
58-
return sect && sect->org != (uint32_t)-1;
58+
return sect && sect->org != UINT32_MAX;
5959
}
6060
return type == SYM_EQU || type == SYM_VAR;
6161
}

include/either.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ union Either {
4343
// Generic field accessors; for internal use only.
4444
template<typename T>
4545
auto &field() {
46-
return pick((T *)nullptr);
46+
return pick(static_cast<T *>(nullptr));
4747
}
4848
template<typename T>
4949
auto const &field() const {
50-
return pick((T *)nullptr);
50+
return pick(static_cast<T *>(nullptr));
5151
}
5252

5353
public:

include/gfx/rgba.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct Rgba {
2828
_5to8(cgbColor),
2929
_5to8(cgbColor >> 5),
3030
_5to8(cgbColor >> 10),
31-
(uint8_t)(cgbColor & 0x8000 ? 0x00 : 0xFF),
31+
static_cast<uint8_t>(cgbColor & 0x8000 ? 0x00 : 0xFF),
3232
};
3333
}
3434

include/itertools.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class EnumSeq {
1818
explicit Iterator(T value) : _value(value) {}
1919

2020
Iterator &operator++() {
21-
_value = (T)(_value + 1);
21+
_value = static_cast<T>(_value + 1);
2222
return *this;
2323
}
2424

@@ -29,7 +29,7 @@ class EnumSeq {
2929
};
3030

3131
public:
32-
explicit EnumSeq(T stop) : _start((T)0), _stop(stop) {}
32+
explicit EnumSeq(T stop) : _start(static_cast<T>(0)), _stop(stop) {}
3333
explicit EnumSeq(T start, T stop) : _start(start), _stop(stop) {}
3434

3535
Iterator begin() { return Iterator(_start); }

src/asm/charmap.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ bool charmap_ForEach(
5353
mappings[nodeIdx] = mapping;
5454
for (unsigned c = 0; c < 256; c++) {
5555
if (size_t nextIdx = node.next[c]; nextIdx)
56-
prefixes.push({nextIdx, mapping + (char)c});
56+
prefixes.push({nextIdx, mapping + static_cast<char>(c)});
5757
}
5858
}
5959
mapFunc(charmap.name);
@@ -64,7 +64,7 @@ bool charmap_ForEach(
6464
}
6565

6666
void charmap_New(std::string const &name, std::string const *baseName) {
67-
size_t baseIdx = (size_t)-1;
67+
size_t baseIdx = SIZE_MAX;
6868

6969
if (baseName != nullptr) {
7070
if (auto search = charmapMap.find(*baseName); search == charmapMap.end())
@@ -82,7 +82,7 @@ void charmap_New(std::string const &name, std::string const *baseName) {
8282
charmapMap[name] = charmapList.size();
8383
Charmap &charmap = charmapList.emplace_back();
8484

85-
if (baseIdx != (size_t)-1)
85+
if (baseIdx != SIZE_MAX)
8686
charmap.nodes = charmapList[baseIdx].nodes; // Copies `charmapList[baseIdx].nodes`
8787
else
8888
charmap.nodes.emplace_back(); // Zero-init the root node
@@ -129,7 +129,7 @@ void charmap_Add(std::string const &mapping, std::vector<int32_t> &&value) {
129129
size_t nodeIdx = 0;
130130

131131
for (char c : mapping) {
132-
size_t &nextIdxRef = charmap.nodes[nodeIdx].next[(uint8_t)c];
132+
size_t &nextIdxRef = charmap.nodes[nodeIdx].next[static_cast<uint8_t>(c)];
133133
size_t nextIdx = nextIdxRef;
134134

135135
if (!nextIdx) {
@@ -157,7 +157,7 @@ bool charmap_HasChar(std::string const &input) {
157157
size_t nodeIdx = 0;
158158

159159
for (char c : input) {
160-
nodeIdx = charmap.nodes[nodeIdx].next[(uint8_t)c];
160+
nodeIdx = charmap.nodes[nodeIdx].next[static_cast<uint8_t>(c)];
161161

162162
if (!nodeIdx)
163163
return false;
@@ -184,7 +184,7 @@ size_t charmap_ConvertNext(std::string_view &input, std::vector<int32_t> *output
184184
size_t inputIdx = 0;
185185

186186
for (size_t nodeIdx = 0; inputIdx < input.length();) {
187-
nodeIdx = charmap.nodes[nodeIdx].next[(uint8_t)input[inputIdx]];
187+
nodeIdx = charmap.nodes[nodeIdx].next[static_cast<uint8_t>(input[inputIdx])];
188188

189189
if (!nodeIdx)
190190
break;

src/asm/fixpoint.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static int32_t double2fix(double d, int32_t q) {
2929
return 0;
3030
if (isinf(d))
3131
return d < 0 ? INT32_MIN : INT32_MAX;
32-
return (int32_t)round(d * pow(2.0, q));
32+
return static_cast<int32_t>(round(d * pow(2.0, q)));
3333
}
3434

3535
static double turn2rad(double t) {

src/asm/format.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,16 @@ void FormatSpec::appendNumber(std::string &str, uint32_t value) const {
250250
}
251251

252252
double fval = fabs(value / pow(2.0, usePrec));
253-
if (useExact)
254-
snprintf(valueBuf, sizeof(valueBuf), "%.*fq%zu", (int)useFracWidth, fval, usePrec);
253+
if (int fracWidthArg = static_cast<int>(useFracWidth); useExact)
254+
snprintf(valueBuf, sizeof(valueBuf), "%.*fq%zu", fracWidthArg, fval, usePrec);
255255
else
256-
snprintf(valueBuf, sizeof(valueBuf), "%.*f", (int)useFracWidth, fval);
256+
snprintf(valueBuf, sizeof(valueBuf), "%.*f", fracWidthArg, fval);
257257
} else if (useType == 'd') {
258258
// Decimal numbers may be formatted with a '-' sign by `snprintf`, so `abs` prevents that,
259259
// with a special case for `INT32_MIN` since `labs(INT32_MIN)` is UB. The sign will be
260260
// printed later from `signChar`.
261-
uint32_t uval = value != (uint32_t)INT32_MIN ? labs((int32_t)value) : value;
261+
uint32_t uval =
262+
value != static_cast<uint32_t>(INT32_MIN) ? labs(static_cast<int32_t>(value)) : value;
262263
snprintf(valueBuf, sizeof(valueBuf), "%" PRIu32, uval);
263264
} else {
264265
char const *spec = useType == 'u' ? "%" PRIu32

0 commit comments

Comments
 (0)