mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-24 12:02:08 +00:00
Use C++-style casts (#1576)
This commit is contained in:
@@ -248,7 +248,7 @@ static void placeSection(Section §ion) {
|
||||
bankMem.insert(
|
||||
bankMem.begin() + spaceIdx + 1,
|
||||
{.address = sectionEnd,
|
||||
.size = (uint16_t)(freeSpace.address + freeSpace.size - sectionEnd)}
|
||||
.size = static_cast<uint16_t>(freeSpace.address + freeSpace.size - sectionEnd)}
|
||||
);
|
||||
// **`freeSpace` cannot be reused from this point on, because `bankMem.insert`
|
||||
// invalidates all references to itself!**
|
||||
@@ -279,7 +279,7 @@ static void placeSection(Section §ion) {
|
||||
sizeof(where),
|
||||
"in bank $%02" PRIx32 " with align mask $%" PRIx16,
|
||||
section.bank,
|
||||
(uint16_t)~section.alignMask
|
||||
static_cast<uint16_t>(~section.alignMask)
|
||||
);
|
||||
else
|
||||
snprintf(where, sizeof(where), "in bank $%02" PRIx32, section.bank);
|
||||
@@ -291,7 +291,7 @@ static void placeSection(Section §ion) {
|
||||
where,
|
||||
sizeof(where),
|
||||
"with align mask $%" PRIx16 " and offset $%" PRIx16,
|
||||
(uint16_t)~section.alignMask,
|
||||
static_cast<uint16_t>(~section.alignMask),
|
||||
section.alignOfs
|
||||
);
|
||||
else
|
||||
|
||||
@@ -209,8 +209,8 @@ static void parseScrambleSpec(char const *spec) {
|
||||
// Remember where the region's name begins and ends
|
||||
char const *regionName = spec;
|
||||
size_t regionNameLen = strcspn(spec, "=, \t");
|
||||
// Length of region name string slice for printing, truncated if too long
|
||||
int regionNamePrintLen = regionNameLen > INT_MAX ? INT_MAX : (int)regionNameLen;
|
||||
// Length of region name string slice for print formatting, truncated if too long
|
||||
int regionNameFmtLen = regionNameLen > INT_MAX ? INT_MAX : static_cast<int>(regionNameLen);
|
||||
ScrambledRegion region = SCRAMBLE_UNK;
|
||||
|
||||
// If this trips, `spec` must be pointing at a ',' or '=' (or NUL) due to the assumption
|
||||
@@ -228,7 +228,7 @@ static void parseScrambleSpec(char const *spec) {
|
||||
spec += regionNameLen + strspn(&spec[regionNameLen], " \t");
|
||||
if (*spec != '\0' && *spec != ',' && *spec != '=') {
|
||||
argErr(
|
||||
'S', "Unexpected '%c' after region name \"%.*s\"", regionNamePrintLen, regionName
|
||||
'S', "Unexpected '%c' after region name \"%.*s\"", regionNameFmtLen, regionName
|
||||
);
|
||||
// Skip to next ',' or '=' (or NUL) and keep parsing
|
||||
spec += 1 + strcspn(&spec[1], ",=");
|
||||
@@ -246,7 +246,7 @@ static void parseScrambleSpec(char const *spec) {
|
||||
}
|
||||
|
||||
if (region == SCRAMBLE_UNK)
|
||||
argErr('S', "Unknown region \"%.*s\"", regionNamePrintLen, regionName);
|
||||
argErr('S', "Unknown region \"%.*s\"", regionNameFmtLen, regionName);
|
||||
|
||||
if (*spec == '=') {
|
||||
spec++; // `strtoul` will skip the whitespace on its own
|
||||
@@ -254,7 +254,7 @@ static void parseScrambleSpec(char const *spec) {
|
||||
char *endptr;
|
||||
|
||||
if (*spec == '\0' || *spec == ',') {
|
||||
argErr('S', "Empty limit for region \"%.*s\"", regionNamePrintLen, regionName);
|
||||
argErr('S', "Empty limit for region \"%.*s\"", regionNameFmtLen, regionName);
|
||||
goto next;
|
||||
}
|
||||
limit = strtoul(spec, &endptr, 10);
|
||||
@@ -263,7 +263,7 @@ static void parseScrambleSpec(char const *spec) {
|
||||
argErr(
|
||||
'S',
|
||||
"Invalid non-numeric limit for region \"%.*s\"",
|
||||
regionNamePrintLen,
|
||||
regionNameFmtLen,
|
||||
regionName
|
||||
);
|
||||
endptr = strchr(endptr, ',');
|
||||
@@ -274,7 +274,7 @@ static void parseScrambleSpec(char const *spec) {
|
||||
argErr(
|
||||
'S',
|
||||
"Limit for region \"%.*s\" may not exceed %" PRIu16,
|
||||
regionNamePrintLen,
|
||||
regionNameFmtLen,
|
||||
regionName,
|
||||
scrambleSpecs[region].max
|
||||
);
|
||||
@@ -298,7 +298,7 @@ static void parseScrambleSpec(char const *spec) {
|
||||
// Only WRAMX can be implied, since ROMX and SRAM size may vary
|
||||
scrambleWRAMX = 7;
|
||||
} else {
|
||||
argErr('S', "Cannot imply limit for region \"%.*s\"", regionNamePrintLen, regionName);
|
||||
argErr('S', "Cannot imply limit for region \"%.*s\"", regionNameFmtLen, regionName);
|
||||
}
|
||||
|
||||
next: // Can't `continue` a `for` loop with this nontrivial iteration logic
|
||||
|
||||
@@ -40,13 +40,13 @@ static std::vector<std::vector<FileStackNode>> nodes;
|
||||
if (tmpVal == (errval)) { \
|
||||
errx(__VA_ARGS__, feof(tmpFile) ? "Unexpected end of file" : strerror(errno)); \
|
||||
} \
|
||||
var = (vartype)tmpVal; \
|
||||
var = static_cast<vartype>(tmpVal); \
|
||||
} while (0)
|
||||
|
||||
/*
|
||||
* Reads an unsigned long (32-bit) value from a file.
|
||||
* @param file The file to read from. This will read 4 bytes from the file.
|
||||
* @return The value read, cast to a int64_t, or -1 on failure.
|
||||
* @return The value read, cast to a int64_t, or `INT64_MAX` on failure.
|
||||
*/
|
||||
static int64_t readLong(FILE *file) {
|
||||
uint32_t value = 0;
|
||||
@@ -63,7 +63,7 @@ static int64_t readLong(FILE *file) {
|
||||
// `uint8_t`, because int is large enough to hold a byte. This
|
||||
// however causes values larger than 127 to be too large when
|
||||
// shifted, potentially triggering undefined behavior.
|
||||
value |= (unsigned int)byte << shift;
|
||||
value |= static_cast<unsigned int>(byte) << shift;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
@@ -128,7 +128,7 @@ static void readFileStackNode(
|
||||
uint32_t parentID;
|
||||
|
||||
tryReadLong(parentID, file, "%s: Cannot read node #%" PRIu32 "'s parent ID: %s", fileName, i);
|
||||
node.parent = parentID != (uint32_t)-1 ? &fileNodes[parentID] : nullptr;
|
||||
node.parent = parentID != UINT32_MAX ? &fileNodes[parentID] : nullptr;
|
||||
tryReadLong(
|
||||
node.lineNo, file, "%s: Cannot read node #%" PRIu32 "'s line number: %s", fileName, i
|
||||
);
|
||||
@@ -329,7 +329,7 @@ static void readPatch(
|
||||
static void
|
||||
linkPatchToPCSect(Patch &patch, std::vector<std::unique_ptr<Section>> const &fileSections) {
|
||||
patch.pcSection =
|
||||
patch.pcSectionID != (uint32_t)-1 ? fileSections[patch.pcSectionID].get() : nullptr;
|
||||
patch.pcSectionID != UINT32_MAX ? fileSections[patch.pcSectionID].get() : nullptr;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -363,7 +363,7 @@ static void writeSymBank(SortedSections const &bankSections, SectionType type, u
|
||||
if (!sym->name.empty() && canStartSymName(sym->name[0]))
|
||||
symList.push_back({
|
||||
.sym = sym,
|
||||
.addr = (uint16_t)(sym->label().offset + sect->org),
|
||||
.addr = static_cast<uint16_t>(sym->label().offset + sect->org),
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -53,7 +53,7 @@ static uint32_t getRPNByte(uint8_t const *&expression, int32_t &size, Patch cons
|
||||
}
|
||||
|
||||
static Symbol const *getSymbol(std::vector<Symbol> const &symbolList, uint32_t index) {
|
||||
assume(index != (uint32_t)-1); // PC needs to be handled specially, not here
|
||||
assume(index != UINT32_MAX); // PC needs to be handled specially, not here
|
||||
Symbol const &symbol = symbolList[index];
|
||||
|
||||
// If the symbol is defined elsewhere...
|
||||
@@ -73,12 +73,12 @@ static Symbol const *getSymbol(std::vector<Symbol> const &symbolList, uint32_t i
|
||||
*/
|
||||
static int32_t computeRPNExpr(Patch const &patch, std::vector<Symbol> const &fileSymbols) {
|
||||
uint8_t const *expression = patch.rpnExpression.data();
|
||||
int32_t size = (int32_t)patch.rpnExpression.size();
|
||||
int32_t size = static_cast<int32_t>(patch.rpnExpression.size());
|
||||
|
||||
rpnStack.clear();
|
||||
|
||||
while (size > 0) {
|
||||
RPNCommand command = (RPNCommand)getRPNByte(expression, size, patch);
|
||||
RPNCommand command = static_cast<RPNCommand>(getRPNByte(expression, size, patch));
|
||||
int32_t value;
|
||||
|
||||
isError = false;
|
||||
@@ -150,11 +150,11 @@ static int32_t computeRPNExpr(Patch const &patch, std::vector<Symbol> const &fil
|
||||
|
||||
case RPN_BITWIDTH:
|
||||
value = popRPN(patch);
|
||||
value = value != 0 ? 32 - clz((uint32_t)value) : 0;
|
||||
value = value != 0 ? 32 - clz(static_cast<uint32_t>(value)) : 0;
|
||||
break;
|
||||
case RPN_TZCOUNT:
|
||||
value = popRPN(patch);
|
||||
value = value != 0 ? ctz((uint32_t)value) : 32;
|
||||
value = value != 0 ? ctz(static_cast<uint32_t>(value)) : 32;
|
||||
break;
|
||||
|
||||
case RPN_OR:
|
||||
@@ -249,7 +249,7 @@ static int32_t computeRPNExpr(Patch const &patch, std::vector<Symbol> const &fil
|
||||
case RPN_BANK_SECT: {
|
||||
// `expression` is not guaranteed to be '\0'-terminated. If it is not,
|
||||
// `getRPNByte` will have a fatal internal error.
|
||||
char const *name = (char const *)expression;
|
||||
char const *name = reinterpret_cast<char const *>(expression);
|
||||
while (getRPNByte(expression, size, patch))
|
||||
;
|
||||
|
||||
@@ -280,7 +280,7 @@ static int32_t computeRPNExpr(Patch const &patch, std::vector<Symbol> const &fil
|
||||
|
||||
case RPN_SIZEOF_SECT: {
|
||||
// This has assumptions commented in the `RPN_BANK_SECT` case above.
|
||||
char const *name = (char const *)expression;
|
||||
char const *name = reinterpret_cast<char const *>(expression);
|
||||
while (getRPNByte(expression, size, patch))
|
||||
;
|
||||
|
||||
@@ -301,7 +301,7 @@ static int32_t computeRPNExpr(Patch const &patch, std::vector<Symbol> const &fil
|
||||
|
||||
case RPN_STARTOF_SECT: {
|
||||
// This has assumptions commented in the `RPN_BANK_SECT` case above.
|
||||
char const *name = (char const *)expression;
|
||||
char const *name = reinterpret_cast<char const *>(expression);
|
||||
while (getRPNByte(expression, size, patch))
|
||||
;
|
||||
|
||||
@@ -428,7 +428,7 @@ void patch_CheckAssertions() {
|
||||
|
||||
for (Assertion &assert : assertions) {
|
||||
int32_t value = computeRPNExpr(assert.patch, *assert.fileSymbols);
|
||||
AssertionType type = (AssertionType)assert.patch.type;
|
||||
AssertionType type = static_cast<AssertionType>(assert.patch.type);
|
||||
|
||||
if (!isError && !value) {
|
||||
switch (type) {
|
||||
|
||||
@@ -557,8 +557,8 @@ static void alignTo(uint32_t alignment, uint32_t alignOfs) {
|
||||
"Cannot align: the next suitable address after $%04" PRIx16 " is $%04" PRIx16
|
||||
", past $%04" PRIx16,
|
||||
pc,
|
||||
(uint16_t)(pc + length),
|
||||
(uint16_t)(endaddr(activeType) + 1)
|
||||
static_cast<uint16_t>(pc + length),
|
||||
static_cast<uint16_t>(endaddr(activeType) + 1)
|
||||
);
|
||||
return;
|
||||
}
|
||||
@@ -588,7 +588,7 @@ static void pad(uint32_t length) {
|
||||
"Cannot increase the current address by %u bytes: only %u bytes to $%04" PRIx16,
|
||||
length,
|
||||
typeInfo.size - offset,
|
||||
(uint16_t)(endaddr(activeType) + 1)
|
||||
static_cast<uint16_t>(endaddr(activeType) + 1)
|
||||
);
|
||||
} else {
|
||||
pc += length;
|
||||
@@ -689,7 +689,7 @@ static void placeSection(std::string const &name, bool isOptional) {
|
||||
name.c_str(),
|
||||
org,
|
||||
alignment,
|
||||
(uint16_t)(org & section->alignMask),
|
||||
static_cast<uint16_t>(org & section->alignMask),
|
||||
alignment,
|
||||
section->alignOfs
|
||||
);
|
||||
|
||||
@@ -468,7 +468,7 @@ void sdobj_ReadFile(FileStackNode const &where, FILE *file, std::vector<Symbol>
|
||||
getToken(nullptr, "'R' line is too short");
|
||||
areaIdx = parseByte(where, lineNo, token, numberType);
|
||||
getToken(nullptr, "'R' line is too short");
|
||||
areaIdx |= (uint16_t)parseByte(where, lineNo, token, numberType) << 8;
|
||||
areaIdx |= static_cast<uint16_t>(parseByte(where, lineNo, token, numberType)) << 8;
|
||||
if (areaIdx >= fileSections.size())
|
||||
fatal(
|
||||
&where,
|
||||
@@ -532,8 +532,9 @@ void sdobj_ReadFile(FileStackNode const &where, FILE *file, std::vector<Symbol>
|
||||
|
||||
if ((flags & 0xF0) == 0xF0) {
|
||||
getToken(nullptr, "Incomplete relocation");
|
||||
flags =
|
||||
(flags & 0x0F) | (uint16_t)parseByte(where, lineNo, token, numberType) << 4;
|
||||
flags = (flags & 0x0F)
|
||||
| static_cast<uint16_t>(parseByte(where, lineNo, token, numberType))
|
||||
<< 4;
|
||||
}
|
||||
|
||||
getToken(nullptr, "Incomplete relocation");
|
||||
@@ -560,7 +561,7 @@ void sdobj_ReadFile(FileStackNode const &where, FILE *file, std::vector<Symbol>
|
||||
uint16_t idx = parseByte(where, lineNo, token, numberType);
|
||||
|
||||
getToken(nullptr, "Incomplete relocation");
|
||||
idx |= (uint16_t)parseByte(where, lineNo, token, numberType);
|
||||
idx |= static_cast<uint16_t>(parseByte(where, lineNo, token, numberType));
|
||||
|
||||
// Loudly fail on unknown flags
|
||||
if (flags & (1 << RELOC_ZPAGE | 1 << RELOC_NPAGE))
|
||||
@@ -646,7 +647,7 @@ void sdobj_ReadFile(FileStackNode const &where, FILE *file, std::vector<Symbol>
|
||||
patch.rpnExpression.resize(1 + sym.name.length() - 2 + 1);
|
||||
patch.rpnExpression[0] = RPN_SIZEOF_SECT;
|
||||
memcpy(
|
||||
(char *)&patch.rpnExpression[1],
|
||||
reinterpret_cast<char *>(&patch.rpnExpression[1]),
|
||||
&sym.name.c_str()[2],
|
||||
sym.name.length() - 2 + 1
|
||||
);
|
||||
@@ -654,7 +655,7 @@ void sdobj_ReadFile(FileStackNode const &where, FILE *file, std::vector<Symbol>
|
||||
patch.rpnExpression.resize(1 + sym.name.length() - 2 + 1);
|
||||
patch.rpnExpression[0] = RPN_STARTOF_SECT;
|
||||
memcpy(
|
||||
(char *)&patch.rpnExpression[1],
|
||||
reinterpret_cast<char *>(&patch.rpnExpression[1]),
|
||||
&sym.name.c_str()[2],
|
||||
sym.name.length() - 2 + 1
|
||||
);
|
||||
@@ -700,7 +701,11 @@ void sdobj_ReadFile(FileStackNode const &where, FILE *file, std::vector<Symbol>
|
||||
patch.rpnExpression.resize(1 + name.length() + 1);
|
||||
patch.rpnExpression[0] = RPN_STARTOF_SECT;
|
||||
// The cast is fine, it's just different signedness
|
||||
memcpy((char *)&patch.rpnExpression[1], name.c_str(), name.length() + 1);
|
||||
memcpy(
|
||||
reinterpret_cast<char *>(&patch.rpnExpression[1]),
|
||||
name.c_str(),
|
||||
name.length() + 1
|
||||
);
|
||||
}
|
||||
|
||||
patch.rpnExpression.push_back(RPN_CONST);
|
||||
|
||||
Reference in New Issue
Block a user