Run clang-format on everything (#1332)

This commit is contained in:
Sylvie
2024-03-04 14:22:49 -05:00
committed by GitHub
parent b004648a13
commit e74073e480
66 changed files with 6091 additions and 4957 deletions

View File

@@ -1,30 +1,31 @@
/* SPDX-License-Identifier: MIT */
#include "link/object.hpp"
#include <algorithm>
#include <deque>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <new>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include "link/assign.hpp"
#include "link/main.hpp"
#include "link/object.hpp"
#include "link/patch.hpp"
#include "link/sdas_obj.hpp"
#include "link/section.hpp"
#include "link/symbol.hpp"
#include "error.hpp"
#include "helpers.hpp"
#include "linkdefs.hpp"
#include "version.hpp"
#include "link/assign.hpp"
#include "link/main.hpp"
#include "link/patch.hpp"
#include "link/sdas_obj.hpp"
#include "link/section.hpp"
#include "link/symbol.hpp"
static std::deque<std::vector<Symbol>> symbolLists;
static std::vector<std::vector<FileStackNode>> nodes;
static std::deque<Assertion> assertions;
@@ -33,25 +34,23 @@ static std::deque<Assertion> assertions;
// Internal, DO NOT USE.
// For helper wrapper macros defined below, such as `tryReadlong`
#define tryRead(func, type, errval, vartype, var, file, ...) do { \
FILE *tmpFile = file; \
type tmpVal = func(tmpFile); \
/* TODO: maybe mark the condition as `unlikely`; how to do that portably? */ \
if (tmpVal == (errval)) { \
errx(__VA_ARGS__, feof(tmpFile) \
? "Unexpected end of file" \
: strerror(errno)); \
} \
var = (vartype)tmpVal; \
} while (0)
#define tryRead(func, type, errval, vartype, var, file, ...) \
do { \
FILE *tmpFile = file; \
type tmpVal = func(tmpFile); \
/* TODO: maybe mark the condition as `unlikely`; how to do that portably? */ \
if (tmpVal == (errval)) { \
errx(__VA_ARGS__, feof(tmpFile) ? "Unexpected end of file" : strerror(errno)); \
} \
var = (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.
*/
static int64_t readlong(FILE *file)
{
static int64_t readlong(FILE *file) {
uint32_t value = 0;
// Read the little-endian value byte by byte
@@ -92,8 +91,7 @@ static int64_t readlong(FILE *file)
* @param ... A format string and related arguments; note that an extra string
* argument is provided, the reason for failure
*/
#define tryGetc(type, var, file, ...) \
tryRead(getc, int, EOF, type, var, file, __VA_ARGS__)
#define tryGetc(type, var, file, ...) tryRead(getc, int, EOF, type, var, file, __VA_ARGS__)
/*
* Helper macro for readings '\0'-terminated strings from a file, and errors out if it fails to.
@@ -103,19 +101,18 @@ static int64_t readlong(FILE *file)
* @param ... A format string and related arguments; note that an extra string
* argument is provided, the reason for failure
*/
#define tryReadstring(var, file, ...) do { \
FILE *tmpFile = file; \
std::string &tmpVal = var; \
for (int tmpByte = getc(tmpFile); tmpByte != '\0'; tmpByte = getc(tmpFile)) { \
if (tmpByte == EOF) { \
errx(__VA_ARGS__, feof(tmpFile) \
? "Unexpected end of file" \
: strerror(errno)); \
} else { \
tmpVal.push_back(tmpByte); \
} \
}; \
} while (0)
#define tryReadstring(var, file, ...) \
do { \
FILE *tmpFile = file; \
std::string &tmpVal = var; \
for (int tmpByte = getc(tmpFile); tmpByte != '\0'; tmpByte = getc(tmpFile)) { \
if (tmpByte == EOF) { \
errx(__VA_ARGS__, feof(tmpFile) ? "Unexpected end of file" : strerror(errno)); \
} else { \
tmpVal.push_back(tmpByte); \
} \
}; \
} while (0)
// Functions to parse object files
@@ -126,39 +123,55 @@ static int64_t readlong(FILE *file)
* @param i The ID of the node in the array
* @param fileName The filename to report in errors
*/
static void readFileStackNode(FILE *file, std::vector<FileStackNode> &fileNodes, uint32_t i,
char const *fileName)
{
static void readFileStackNode(
FILE *file, std::vector<FileStackNode> &fileNodes, uint32_t i, char const *fileName
) {
FileStackNode &node = fileNodes[i];
uint32_t parentID;
tryReadlong(parentID, file,
"%s: Cannot read node #%" PRIu32 "'s parent ID: %s", fileName, i);
tryReadlong(parentID, file, "%s: Cannot read node #%" PRIu32 "'s parent ID: %s", fileName, i);
node.parent = parentID != (uint32_t)-1 ? &fileNodes[parentID] : nullptr;
tryReadlong(node.lineNo, file,
"%s: Cannot read node #%" PRIu32 "'s line number: %s", fileName, i);
tryGetc(enum FileStackNodeType, node.type, file,
"%s: Cannot read node #%" PRIu32 "'s type: %s", fileName, i);
tryReadlong(
node.lineNo, file, "%s: Cannot read node #%" PRIu32 "'s line number: %s", fileName, i
);
tryGetc(
enum FileStackNodeType,
node.type,
file,
"%s: Cannot read node #%" PRIu32 "'s type: %s",
fileName,
i
);
switch (node.type) {
case NODE_FILE:
case NODE_MACRO:
node.data = "";
tryReadstring(node.name(), file,
"%s: Cannot read node #%" PRIu32 "'s file name: %s", fileName, i);
tryReadstring(
node.name(), file, "%s: Cannot read node #%" PRIu32 "'s file name: %s", fileName, i
);
break;
uint32_t depth;
case NODE_REPT:
tryReadlong(depth, file,
"%s: Cannot read node #%" PRIu32 "'s rept depth: %s", fileName, i);
tryReadlong(depth, file, "%s: Cannot read node #%" PRIu32 "'s rept depth: %s", fileName, i);
node.data = std::vector<uint32_t>(depth);
for (uint32_t k = 0; k < depth; k++)
tryReadlong(node.iters()[k], file,
"%s: Cannot read node #%" PRIu32 "'s iter #%" PRIu32 ": %s",
fileName, i, k);
tryReadlong(
node.iters()[k],
file,
"%s: Cannot read node #%" PRIu32 "'s iter #%" PRIu32 ": %s",
fileName,
i,
k
);
if (!node.parent)
fatal(nullptr, 0, "%s is not a valid object file: root node (#%"
PRIu32 ") may not be REPT", fileName, i);
fatal(
nullptr,
0,
"%s is not a valid object file: root node (#%" PRIu32 ") may not be REPT",
fileName,
i
);
}
}
@@ -168,34 +181,52 @@ static void readFileStackNode(FILE *file, std::vector<FileStackNode> &fileNodes,
* @param symbol The symbol to fill
* @param fileName The filename to report in errors
*/
static void readSymbol(FILE *file, Symbol &symbol, char const *fileName,
std::vector<FileStackNode> const &fileNodes)
{
static void readSymbol(
FILE *file, Symbol &symbol, char const *fileName, std::vector<FileStackNode> const &fileNodes
) {
tryReadstring(symbol.name, file, "%s: Cannot read symbol name: %s", fileName);
tryGetc(enum ExportLevel, symbol.type, file, "%s: Cannot read \"%s\"'s type: %s",
fileName, symbol.name.c_str());
tryGetc(
enum ExportLevel,
symbol.type,
file,
"%s: Cannot read \"%s\"'s type: %s",
fileName,
symbol.name.c_str()
);
// If the symbol is defined in this file, read its definition
if (symbol.type != SYMTYPE_IMPORT) {
symbol.objFileName = fileName;
uint32_t nodeID;
tryReadlong(nodeID, file, "%s: Cannot read \"%s\"'s node ID: %s",
fileName, symbol.name.c_str());
tryReadlong(
nodeID, file, "%s: Cannot read \"%s\"'s node ID: %s", fileName, symbol.name.c_str()
);
symbol.src = &fileNodes[nodeID];
tryReadlong(symbol.lineNo, file, "%s: Cannot read \"%s\"'s line number: %s",
fileName, symbol.name.c_str());
tryReadlong(
symbol.lineNo,
file,
"%s: Cannot read \"%s\"'s line number: %s",
fileName,
symbol.name.c_str()
);
int32_t sectionID, value;
tryReadlong(sectionID, file, "%s: Cannot read \"%s\"'s section ID: %s",
fileName, symbol.name.c_str());
tryReadlong(value, file, "%s: Cannot read \"%s\"'s value: %s",
fileName, symbol.name.c_str());
tryReadlong(
sectionID,
file,
"%s: Cannot read \"%s\"'s section ID: %s",
fileName,
symbol.name.c_str()
);
tryReadlong(
value, file, "%s: Cannot read \"%s\"'s value: %s", fileName, symbol.name.c_str()
);
if (sectionID == -1) {
symbol.data = value;
} else {
symbol.data = Label{
.sectionID = sectionID,
.offset = value,
// Set the `.section` later based on the `.sectionID`
.section = nullptr,
.sectionID = sectionID,
.offset = value,
// Set the `.section` later based on the `.sectionID`
.section = nullptr,
};
}
} else {
@@ -210,53 +241,96 @@ static void readSymbol(FILE *file, Symbol &symbol, char const *fileName,
* @param fileName The filename to report in errors
* @param i The number of the patch to report in errors
*/
static void readPatch(FILE *file, Patch &patch, char const *fileName, std::string const &sectName,
uint32_t i, std::vector<FileStackNode> const &fileNodes)
{
static void readPatch(
FILE *file,
Patch &patch,
char const *fileName,
std::string const &sectName,
uint32_t i,
std::vector<FileStackNode> const &fileNodes
) {
uint32_t nodeID, rpnSize;
enum PatchType type;
tryReadlong(nodeID, file,
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s node ID: %s",
fileName, sectName.c_str(), i);
tryReadlong(
nodeID,
file,
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s node ID: %s",
fileName,
sectName.c_str(),
i
);
patch.src = &fileNodes[nodeID];
tryReadlong(patch.lineNo, file,
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s line number: %s",
fileName, sectName.c_str(), i);
tryReadlong(patch.offset, file,
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s offset: %s",
fileName, sectName.c_str(), i);
tryReadlong(patch.pcSectionID, file,
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s PC offset: %s",
fileName, sectName.c_str(), i);
tryReadlong(patch.pcOffset, file,
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s PC offset: %s",
fileName, sectName.c_str(), i);
tryGetc(enum PatchType, type, file,
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s type: %s",
fileName, sectName.c_str(), i);
tryReadlong(
patch.lineNo,
file,
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s line number: %s",
fileName,
sectName.c_str(),
i
);
tryReadlong(
patch.offset,
file,
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s offset: %s",
fileName,
sectName.c_str(),
i
);
tryReadlong(
patch.pcSectionID,
file,
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s PC offset: %s",
fileName,
sectName.c_str(),
i
);
tryReadlong(
patch.pcOffset,
file,
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s PC offset: %s",
fileName,
sectName.c_str(),
i
);
tryGetc(
enum PatchType,
type,
file,
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s type: %s",
fileName,
sectName.c_str(),
i
);
patch.type = type;
tryReadlong(rpnSize, file,
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s RPN size: %s",
fileName, sectName.c_str(), i);
tryReadlong(
rpnSize,
file,
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s RPN size: %s",
fileName,
sectName.c_str(),
i
);
patch.rpnExpression.resize(rpnSize);
size_t nbElementsRead = fread(patch.rpnExpression.data(), 1, rpnSize, file);
if (nbElementsRead != rpnSize)
errx("%s: Cannot read \"%s\"'s patch #%" PRIu32 "'s RPN expression: %s",
fileName, sectName.c_str(), i,
feof(file) ? "Unexpected end of file" : strerror(errno));
errx(
"%s: Cannot read \"%s\"'s patch #%" PRIu32 "'s RPN expression: %s",
fileName,
sectName.c_str(),
i,
feof(file) ? "Unexpected end of file" : strerror(errno)
);
}
/*
* Sets a patch's pcSection from its pcSectionID.
* @param patch The patch to fix
*/
static void linkPatchToPCSect(Patch &patch, std::vector<Section *> const &fileSections)
{
patch.pcSection = patch.pcSectionID != (uint32_t)-1 ? fileSections[patch.pcSectionID]
: nullptr;
static void linkPatchToPCSect(Patch &patch, std::vector<Section *> const &fileSections) {
patch.pcSection = patch.pcSectionID != (uint32_t)-1 ? fileSections[patch.pcSectionID] : nullptr;
}
/*
@@ -265,9 +339,9 @@ static void linkPatchToPCSect(Patch &patch, std::vector<Section *> const &fileSe
* @param section The section to fill
* @param fileName The filename to report in errors
*/
static void readSection(FILE *file, Section &section, char const *fileName,
std::vector<FileStackNode> const &fileNodes)
{
static void readSection(
FILE *file, Section &section, char const *fileName, std::vector<FileStackNode> const &fileNodes
) {
int32_t tmp;
uint8_t byte;
@@ -277,8 +351,9 @@ static void readSection(FILE *file, Section &section, char const *fileName,
errx("\"%s\"'s section size (%" PRId32 ") is invalid", section.name.c_str(), tmp);
section.size = tmp;
section.offset = 0;
tryGetc(uint8_t, byte, file, "%s: Cannot read \"%s\"'s type: %s", fileName,
section.name.c_str());
tryGetc(
uint8_t, byte, file, "%s: Cannot read \"%s\"'s type: %s", fileName, section.name.c_str()
);
section.type = (enum SectionType)(byte & 0x3F);
if (byte >> 7)
section.modifier = SECTION_UNION;
@@ -296,17 +371,29 @@ static void readSection(FILE *file, Section &section, char const *fileName,
tryReadlong(tmp, file, "%s: Cannot read \"%s\"'s bank: %s", fileName, section.name.c_str());
section.isBankFixed = tmp >= 0;
section.bank = tmp;
tryGetc(uint8_t, byte, file, "%s: Cannot read \"%s\"'s alignment: %s", fileName,
section.name.c_str());
tryGetc(
uint8_t,
byte,
file,
"%s: Cannot read \"%s\"'s alignment: %s",
fileName,
section.name.c_str()
);
if (byte > 16)
byte = 16;
section.isAlignFixed = byte != 0;
section.alignMask = (1 << byte) - 1;
tryReadlong(tmp, file, "%s: Cannot read \"%s\"'s alignment offset: %s", fileName,
section.name.c_str());
tryReadlong(
tmp, file, "%s: Cannot read \"%s\"'s alignment offset: %s", fileName, section.name.c_str()
);
if (tmp > UINT16_MAX) {
error(nullptr, 0, "\"%s\"'s alignment offset is too large (%" PRId32 ")",
section.name.c_str(), tmp);
error(
nullptr,
0,
"\"%s\"'s alignment offset is too large (%" PRId32 ")",
section.name.c_str(),
tmp
);
tmp = UINT16_MAX;
}
section.alignOfs = tmp;
@@ -316,15 +403,23 @@ static void readSection(FILE *file, Section &section, char const *fileName,
section.data.resize(section.size);
if (size_t nbRead = fread(section.data.data(), 1, section.size, file);
nbRead != section.size)
errx("%s: Cannot read \"%s\"'s data: %s", fileName, section.name.c_str(),
feof(file) ? "Unexpected end of file" : strerror(errno));
errx(
"%s: Cannot read \"%s\"'s data: %s",
fileName,
section.name.c_str(),
feof(file) ? "Unexpected end of file" : strerror(errno)
);
}
uint32_t nbPatches;
tryReadlong(nbPatches, file,
"%s: Cannot read \"%s\"'s number of patches: %s", fileName,
section.name.c_str());
tryReadlong(
nbPatches,
file,
"%s: Cannot read \"%s\"'s number of patches: %s",
fileName,
section.name.c_str()
);
section.patches.resize(nbPatches);
for (uint32_t i = 0; i < nbPatches; i++)
@@ -337,8 +432,7 @@ static void readSection(FILE *file, Section &section, char const *fileName,
* @param symbol The symbol to link
* @param section The section to link
*/
static void linkSymToSect(Symbol &symbol, Section &section)
{
static void linkSymToSect(Symbol &symbol, Section &section) {
uint32_t a = 0, b = section.symbols.size();
int32_t symbolOffset = symbol.label().offset;
@@ -361,9 +455,13 @@ static void linkSymToSect(Symbol &symbol, Section &section)
* @param assert The assertion to fill
* @param fileName The filename to report in errors
*/
static void readAssertion(FILE *file, Assertion &assert, char const *fileName, uint32_t i,
std::vector<FileStackNode> const &fileNodes)
{
static void readAssertion(
FILE *file,
Assertion &assert,
char const *fileName,
uint32_t i,
std::vector<FileStackNode> const &fileNodes
) {
char assertName[sizeof("Assertion #4294967295")]; // UINT32_MAX
snprintf(assertName, sizeof(assertName), "Assertion #%" PRIu32, i);
@@ -372,13 +470,11 @@ static void readAssertion(FILE *file, Assertion &assert, char const *fileName, u
tryReadstring(assert.message, file, "%s: Cannot read assertion's message: %s", fileName);
}
static Section *getMainSection(Section &section)
{
static Section *getMainSection(Section &section) {
return section.modifier != SECTION_NORMAL ? sect_GetSection(section.name) : &section;
}
void obj_ReadFile(char const *fileName, unsigned int fileID)
{
void obj_ReadFile(char const *fileName, unsigned int fileID) {
FILE *file;
if (strcmp(fileName, "-")) {
@@ -405,12 +501,9 @@ void obj_ReadFile(char const *fileName, unsigned int fileID)
default: // This is (probably) a SDCC object file, defer the rest of detection to it
// Since SDCC does not provide line info, everything will be reported as coming from the
// object file. It's better than nothing.
nodes[fileID].push_back({
.parent = nullptr,
.lineNo = 0,
.type = NODE_FILE,
.data = fileName
});
nodes[fileID].push_back(
{.parent = nullptr, .lineNo = 0, .type = NODE_FILE, .data = fileName}
);
std::vector<Symbol> &fileSymbols = symbolLists.emplace_front();
@@ -422,7 +515,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID)
int matchedElems;
if (fscanf(file, RGBDS_OBJECT_VERSION_STRING "%n", &matchedElems) == 1
&& matchedElems != strlen(RGBDS_OBJECT_VERSION_STRING))
&& matchedElems != strlen(RGBDS_OBJECT_VERSION_STRING))
errx("%s: Not a RGBDS object file", fileName);
verbosePrint("Reading object file %s\n", fileName);
@@ -431,10 +524,16 @@ void obj_ReadFile(char const *fileName, unsigned int fileID)
tryReadlong(revNum, file, "%s: Cannot read revision number: %s", fileName);
if (revNum != RGBDS_OBJECT_REV)
errx("%s: Unsupported object file for rgblink %s; try rebuilding \"%s\"%s"
" (expected revision %d, got %d)", fileName, get_package_version_string(),
fileName, revNum > RGBDS_OBJECT_REV ? " or updating rgblink" : "",
RGBDS_OBJECT_REV, revNum);
errx(
"%s: Unsupported object file for rgblink %s; try rebuilding \"%s\"%s"
" (expected revision %d, got %d)",
fileName,
get_package_version_string(),
fileName,
revNum > RGBDS_OBJECT_REV ? " or updating rgblink" : "",
RGBDS_OBJECT_REV,
revNum
);
uint32_t nbNodes;
uint32_t nbSymbols;
@@ -448,7 +547,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID)
tryReadlong(nbNodes, file, "%s: Cannot read number of nodes: %s", fileName);
nodes[fileID].resize(nbNodes);
verbosePrint("Reading %u nodes...\n", nbNodes);
for (uint32_t i = nbNodes; i--; )
for (uint32_t i = nbNodes; i--;)
readFileStackNode(file, nodes[fileID], i, fileName);
// This file's symbols, kept to link sections to them
@@ -474,7 +573,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID)
verbosePrint("Reading %" PRIu32 " sections...\n", nbSections);
for (uint32_t i = 0; i < nbSections; i++) {
// Read section
fileSections[i] = new(std::nothrow) Section();
fileSections[i] = new (std::nothrow) Section();
if (!fileSections[i])
err("%s: Failed to create new section", fileName);
@@ -527,12 +626,10 @@ void obj_ReadFile(char const *fileName, unsigned int fileID)
fclose(file);
}
void obj_CheckAssertions()
{
void obj_CheckAssertions() {
patch_CheckAssertions(assertions);
}
void obj_Setup(unsigned int nbFiles)
{
void obj_Setup(unsigned int nbFiles) {
nodes.resize(nbFiles);
}