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,7 +1,7 @@
/* SPDX-License-Identifier: MIT */
#include <sys/stat.h>
#include <new>
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
@@ -15,13 +15,14 @@
#include <variant>
#include <vector>
#include "error.hpp"
#include "platform.hpp" // S_ISDIR (stat macro)
#include "asm/fstack.hpp"
#include "asm/macro.hpp"
#include "asm/main.hpp"
#include "asm/symbol.hpp"
#include "asm/warning.hpp"
#include "error.hpp"
#include "platform.hpp" // S_ISDIR (stat macro)
struct Context {
FileStackNode *fileInfo;
@@ -39,7 +40,7 @@ static std::stack<Context> contextStack;
size_t maxRecursionDepth;
// The first include path for `fstk_FindFile` to try is none at all
static std::vector<std::string> includePaths = { "" };
static std::vector<std::string> includePaths = {""};
static const char *preIncludeName;
@@ -63,8 +64,7 @@ std::string const &FileStackNode::name() const {
return std::get<std::string>(data);
}
static const char *dumpNodeAndParents(FileStackNode const &node)
{
static const char *dumpNodeAndParents(FileStackNode const &node) {
char const *name;
if (node.type == NODE_REPT) {
@@ -73,7 +73,7 @@ static const char *dumpNodeAndParents(FileStackNode const &node)
name = dumpNodeAndParents(*node.parent);
fprintf(stderr, "(%" PRIu32 ") -> %s", node.lineNo, name);
for (uint32_t i = nodeIters.size(); i--; )
for (uint32_t i = nodeIters.size(); i--;)
fprintf(stderr, "::REPT~%" PRIu32, nodeIters[i]);
} else {
name = node.name().c_str();
@@ -87,14 +87,12 @@ static const char *dumpNodeAndParents(FileStackNode const &node)
return name;
}
void FileStackNode::dump(uint32_t curLineNo) const
{
void FileStackNode::dump(uint32_t curLineNo) const {
dumpNodeAndParents(*this);
fprintf(stderr, "(%" PRIu32 ")", curLineNo);
}
void fstk_DumpCurrent()
{
void fstk_DumpCurrent() {
if (contextStack.empty()) {
fputs("at top level", stderr);
return;
@@ -102,8 +100,7 @@ void fstk_DumpCurrent()
contextStack.top().fileInfo->dump(lexer_GetLineNo());
}
FileStackNode *fstk_GetFileStack()
{
FileStackNode *fstk_GetFileStack() {
if (contextStack.empty())
return nullptr;
@@ -117,8 +114,7 @@ FileStackNode *fstk_GetFileStack()
return topNode;
}
char const *fstk_GetFileName()
{
char const *fstk_GetFileName() {
// Iterating via the nodes themselves skips nested REPTs
FileStackNode const *node = contextStack.top().fileInfo;
@@ -127,8 +123,7 @@ char const *fstk_GetFileName()
return node->name().c_str();
}
void fstk_AddIncludePath(char const *path)
{
void fstk_AddIncludePath(char const *path) {
if (path[0] == '\0')
return;
@@ -138,8 +133,7 @@ void fstk_AddIncludePath(char const *path)
str += '/';
}
void fstk_SetPreIncludeFile(char const *path)
{
void fstk_SetPreIncludeFile(char const *path) {
if (preIncludeName)
warnx("Overriding pre-included filename %s", preIncludeName);
preIncludeName = path;
@@ -147,8 +141,7 @@ void fstk_SetPreIncludeFile(char const *path)
printf("Pre-included filename %s\n", preIncludeName);
}
static void printDep(char const *path)
{
static void printDep(char const *path) {
if (dependfile) {
fprintf(dependfile, "%s: %s\n", targetFileName.c_str(), path);
if (generatePhonyDeps)
@@ -156,8 +149,7 @@ static void printDep(char const *path)
}
}
static bool isPathValid(char const *path)
{
static bool isPathValid(char const *path) {
struct stat statbuf;
if (stat(path, &statbuf) != 0)
@@ -167,9 +159,8 @@ static bool isPathValid(char const *path)
return !S_ISDIR(statbuf.st_mode);
}
std::string *fstk_FindFile(char const *path)
{
std::string *fullPath = new(std::nothrow) std::string();
std::string *fstk_FindFile(char const *path) {
std::string *fullPath = new (std::nothrow) std::string();
if (!fullPath) {
error("Failed to allocate string during include path search: %s\n", strerror(errno));
@@ -189,20 +180,22 @@ std::string *fstk_FindFile(char const *path)
return nullptr;
}
bool yywrap()
{
bool yywrap() {
uint32_t ifDepth = lexer_GetIFDepth();
if (ifDepth != 0)
fatalerror("Ended block with %" PRIu32 " unterminated IF construct%s\n",
ifDepth, ifDepth == 1 ? "" : "s");
fatalerror(
"Ended block with %" PRIu32 " unterminated IF construct%s\n",
ifDepth,
ifDepth == 1 ? "" : "s"
);
if (Context &context = contextStack.top(); context.fileInfo->type == NODE_REPT) {
// The context is a REPT or FOR block, which may loop
// If the node is referenced, we can't edit it; duplicate it
if (context.fileInfo->referenced) {
context.fileInfo = new(std::nothrow) FileStackNode(*context.fileInfo);
context.fileInfo = new (std::nothrow) FileStackNode(*context.fileInfo);
if (!context.fileInfo)
fatalerror("Failed to duplicate REPT file node: %s\n", strerror(errno));
// Copy all info but the referencing
@@ -255,8 +248,7 @@ bool yywrap()
// Make sure not to switch the lexer state before calling this, so the saved line no is correct.
// BE CAREFUL! This modifies the file stack directly, you should have set up the file info first.
// Callers should set `contextStack.top().lexerState` after this so it is not `nullptr`.
static Context &newContext(FileStackNode &fileInfo)
{
static Context &newContext(FileStackNode &fileInfo) {
if (contextStack.size() > maxRecursionDepth)
fatalerror("Recursion limit (%zu) exceeded\n", maxRecursionDepth);
@@ -277,15 +269,13 @@ static Context &newContext(FileStackNode &fileInfo)
return context;
}
void fstk_RunInclude(char const *path)
{
void fstk_RunInclude(char const *path) {
std::string *fullPath = fstk_FindFile(path);
if (!fullPath) {
if (generatedMissingIncludes) {
if (verbose)
printf("Aborting (-MG) on INCLUDE file '%s' (%s)\n",
path, strerror(errno));
printf("Aborting (-MG) on INCLUDE file '%s' (%s)\n", path, strerror(errno));
failedOnMissingInclude = true;
} else {
error("Unable to open included file '%s': %s\n", path, strerror(errno));
@@ -293,7 +283,7 @@ void fstk_RunInclude(char const *path)
return;
}
FileStackNode *fileInfo = new(std::nothrow) FileStackNode();
FileStackNode *fileInfo = new (std::nothrow) FileStackNode();
if (!fileInfo) {
error("Failed to alloc file info for INCLUDE: %s\n", strerror(errno));
@@ -317,8 +307,7 @@ void fstk_RunInclude(char const *path)
// Similar to `fstk_RunInclude`, but not subject to `-MG`, and
// calling `lexer_SetState` instead of `lexer_SetStateAtEOL`.
static void runPreIncludeFile()
{
static void runPreIncludeFile() {
if (!preIncludeName)
return;
@@ -329,7 +318,7 @@ static void runPreIncludeFile()
return;
}
FileStackNode *fileInfo = new(std::nothrow) FileStackNode();
FileStackNode *fileInfo = new (std::nothrow) FileStackNode();
if (!fileInfo) {
error("Failed to alloc file info for pre-include: %s\n", strerror(errno));
@@ -348,8 +337,7 @@ static void runPreIncludeFile()
context.uniqueID = macro_UndefUniqueID();
}
void fstk_RunMacro(char const *macroName, MacroArgs &args)
{
void fstk_RunMacro(char const *macroName, MacroArgs &args) {
Symbol *macro = sym_FindExactSymbol(macroName);
if (!macro) {
@@ -362,7 +350,7 @@ void fstk_RunMacro(char const *macroName, MacroArgs &args)
}
contextStack.top().macroArgs = macro_GetCurrentArgs();
FileStackNode *fileInfo = new(std::nothrow) FileStackNode();
FileStackNode *fileInfo = new (std::nothrow) FileStackNode();
if (!fileInfo) {
error("Failed to alloc file info for \"%s\": %s\n", macro->name, strerror(errno));
@@ -383,12 +371,11 @@ void fstk_RunMacro(char const *macroName, MacroArgs &args)
if (macro->src->type == NODE_REPT) {
std::vector<uint32_t> const &srcIters = macro->src->iters();
for (uint32_t i = srcIters.size(); i--; ) {
for (uint32_t i = srcIters.size(); i--;) {
char buf[sizeof("::REPT~4294967295")]; // UINT32_MAX
if (sprintf(buf, "::REPT~%" PRIu32, srcIters[i]) < 0)
fatalerror("Failed to write macro invocation info: %s\n",
strerror(errno));
fatalerror("Failed to write macro invocation info: %s\n", strerror(errno));
fileInfoName.append(buf);
}
}
@@ -398,19 +385,19 @@ void fstk_RunMacro(char const *macroName, MacroArgs &args)
Context &context = newContext(*fileInfo);
std::string_view *macroView = macro->getMacro();
lexer_OpenFileView(context.lexerState, "MACRO", macroView->data(), macroView->size(),
macro->fileLine);
lexer_OpenFileView(
context.lexerState, "MACRO", macroView->data(), macroView->size(), macro->fileLine
);
lexer_SetStateAtEOL(&context.lexerState);
context.uniqueID = macro_UseNewUniqueID();
macro_UseNewArgs(&args);
}
static bool newReptContext(int32_t reptLineNo, char const *body, size_t size)
{
static bool newReptContext(int32_t reptLineNo, char const *body, size_t size) {
uint32_t reptDepth = contextStack.top().fileInfo->type == NODE_REPT
? contextStack.top().fileInfo->iters().size()
: 0;
FileStackNode *fileInfo = new(std::nothrow) FileStackNode();
? contextStack.top().fileInfo->iters().size()
: 0;
FileStackNode *fileInfo = new (std::nothrow) FileStackNode();
if (!fileInfo) {
error("Failed to alloc file info for REPT: %s\n", strerror(errno));
@@ -420,7 +407,9 @@ static bool newReptContext(int32_t reptLineNo, char const *body, size_t size)
fileInfo->data = std::vector<uint32_t>{1};
if (reptDepth) {
// Append all parent iter counts
fileInfo->iters().insert(fileInfo->iters().end(), RANGE(contextStack.top().fileInfo->iters()));
fileInfo->iters().insert(
fileInfo->iters().end(), RANGE(contextStack.top().fileInfo->iters())
);
}
Context &context = newContext(*fileInfo);
@@ -434,8 +423,7 @@ static bool newReptContext(int32_t reptLineNo, char const *body, size_t size)
return true;
}
void fstk_RunRept(uint32_t count, int32_t reptLineNo, char const *body, size_t size)
{
void fstk_RunRept(uint32_t count, int32_t reptLineNo, char const *body, size_t size) {
if (count == 0)
return;
if (!newReptContext(reptLineNo, body, size))
@@ -444,9 +432,15 @@ void fstk_RunRept(uint32_t count, int32_t reptLineNo, char const *body, size_t s
contextStack.top().nbReptIters = count;
}
void fstk_RunFor(char const *symName, int32_t start, int32_t stop, int32_t step,
int32_t reptLineNo, char const *body, size_t size)
{
void fstk_RunFor(
char const *symName,
int32_t start,
int32_t stop,
int32_t step,
int32_t reptLineNo,
char const *body,
size_t size
) {
Symbol *sym = sym_AddVar(symName, start);
if (sym->type != SYM_VAR)
@@ -462,8 +456,9 @@ void fstk_RunFor(char const *symName, int32_t start, int32_t stop, int32_t step,
error("FOR cannot have a step value of 0\n");
if ((step > 0 && start > stop) || (step < 0 && start < stop))
warning(WARNING_BACKWARDS_FOR, "FOR goes backwards from %d to %d by %d\n",
start, stop, step);
warning(
WARNING_BACKWARDS_FOR, "FOR goes backwards from %d to %d by %d\n", start, stop, step
);
if (count == 0)
return;
@@ -479,14 +474,12 @@ void fstk_RunFor(char const *symName, int32_t start, int32_t stop, int32_t step,
context.forName = symName;
}
void fstk_StopRept()
{
void fstk_StopRept() {
// Prevent more iterations
contextStack.top().nbReptIters = 0;
}
bool fstk_Break()
{
bool fstk_Break() {
if (contextStack.top().fileInfo->type != NODE_REPT) {
error("BREAK can only be used inside a REPT/FOR block\n");
return false;
@@ -496,22 +489,20 @@ bool fstk_Break()
return true;
}
void fstk_NewRecursionDepth(size_t newDepth)
{
void fstk_NewRecursionDepth(size_t newDepth) {
if (contextStack.size() > newDepth + 1)
fatalerror("Recursion limit (%zu) exceeded\n", newDepth);
maxRecursionDepth = newDepth;
}
void fstk_Init(char const *mainPath, size_t maxDepth)
{
void fstk_Init(char const *mainPath, size_t maxDepth) {
Context &context = contextStack.emplace();
if (!lexer_OpenFile(context.lexerState, mainPath))
fatalerror("Failed to open main file\n");
lexer_SetState(&context.lexerState);
FileStackNode *fileInfo = new(std::nothrow) FileStackNode();
FileStackNode *fileInfo = new (std::nothrow) FileStackNode();
if (!fileInfo)
fatalerror("Failed to allocate memory for main file info: %s\n", strerror(errno));