Refactor FileStackNode::dump to not need a helper function

This commit is contained in:
Rangi42
2024-03-10 13:21:28 -04:00
parent 58fdaa8747
commit 846a9411b9
5 changed files with 54 additions and 53 deletions

View File

@@ -42,7 +42,7 @@ struct FileStackNode {
FileStackNode(FileStackNodeType type_, std::variant<std::vector<uint32_t>, std::string> data_) FileStackNode(FileStackNodeType type_, std::variant<std::vector<uint32_t>, std::string> data_)
: type(type_), data(data_){}; : type(type_), data(data_){};
void dump(uint32_t curLineNo) const; std::string const &dump(uint32_t curLineNo) const;
}; };
#define DEFAULT_MAX_DEPTH 64 #define DEFAULT_MAX_DEPTH 64

View File

@@ -56,7 +56,7 @@ struct FileStackNode {
std::string &name(); std::string &name();
std::string const &name() const; std::string const &name() const;
std::string const *dumpFileStack() const; std::string const &dump(uint32_t curLineNo) const;
}; };
void warning(FileStackNode const *where, uint32_t lineNo, char const *fmt, ...) void warning(FileStackNode const *where, uint32_t lineNo, char const *fmt, ...)

View File

@@ -12,6 +12,7 @@
#include <string_view> #include <string_view>
#include "error.hpp" #include "error.hpp"
#include "helpers.hpp"
#include "platform.hpp" // S_ISDIR (stat macro) #include "platform.hpp" // S_ISDIR (stat macro)
#include "asm/fstack.hpp" #include "asm/fstack.hpp"
@@ -60,32 +61,28 @@ std::string const &FileStackNode::name() const {
return std::get<std::string>(data); return std::get<std::string>(data);
} }
static char const *dumpNodeAndParents(FileStackNode const &node) { std::string const &FileStackNode::dump(uint32_t curLineNo) const {
char const *name; std::string const &topName = std::visit(Visitor{
[this](std::vector<uint32_t> const &iters) -> std::string const & {
if (node.type == NODE_REPT) { assert(this->parent); // REPT nodes use their parent's name
assert(node.parent); // REPT nodes should always have a parent std::string const &lastName = this->parent->dump(this->lineNo);
std::vector<uint32_t> const &nodeIters = node.iters(); fprintf(stderr, " -> %s", lastName.c_str());
for (uint32_t i = iters.size(); i--;)
name = dumpNodeAndParents(*node.parent); fprintf(stderr, "::REPT~%" PRIu32, iters[i]);
fprintf(stderr, "(%" PRIu32 ") -> %s", node.lineNo, name); return lastName;
for (uint32_t i = nodeIters.size(); i--;) },
fprintf(stderr, "::REPT~%" PRIu32, nodeIters[i]); [this](std::string const &name) -> std::string const & {
if (this->parent) {
this->parent->dump(this->lineNo);
fprintf(stderr, " -> %s", name.c_str());
} else { } else {
name = node.name().c_str(); fputs(name.c_str(), stderr);
if (node.parent) {
dumpNodeAndParents(*node.parent);
fprintf(stderr, "(%" PRIu32 ") -> %s", node.lineNo, name);
} else {
fputs(name, stderr);
}
} }
return name; return name;
} },
}, data);
void FileStackNode::dump(uint32_t curLineNo) const {
dumpNodeAndParents(*this);
fprintf(stderr, "(%" PRIu32 ")", curLineNo); fprintf(stderr, "(%" PRIu32 ")", curLineNo);
return topName;
} }
void fstk_DumpCurrent() { void fstk_DumpCurrent() {

View File

@@ -65,27 +65,31 @@ std::string const &FileStackNode::name() const {
return std::get<std::string>(data); return std::get<std::string>(data);
} }
// Helper function to dump a file stack to stderr std::string const &FileStackNode::dump(uint32_t curLineNo) const {
std::string const *FileStackNode::dumpFileStack() const { std::string const &topName = std::visit(Visitor{
std::string const *lastName; [this](std::vector<uint32_t> const &iters) -> std::string const & {
assert(this->parent); // REPT nodes use their parent's name
if (parent) { std::string const &lastName = this->parent->dump(this->lineNo);
lastName = parent->dumpFileStack(); fprintf(stderr, " -> %s", lastName.c_str());
// REPT nodes use their parent's name for (uint32_t iter : iters)
if (type != NODE_REPT)
lastName = &name();
fprintf(stderr, "(%" PRIu32 ") -> %s", lineNo, lastName->c_str());
if (type == NODE_REPT) {
for (uint32_t iter : iters())
fprintf(stderr, "::REPT~%" PRIu32, iter); fprintf(stderr, "::REPT~%" PRIu32, iter);
}
} else {
assert(type != NODE_REPT);
lastName = &name();
fputs(lastName->c_str(), stderr);
}
return lastName; return lastName;
},
[this](std::string const &name) -> std::string const & {
if (this->parent) {
this->parent->dump(this->lineNo);
fprintf(stderr, " -> %s", name.c_str());
} else {
fputs(name.c_str(), stderr);
}
return name;
},
[](std::monostate) -> std::string const & {
unreachable_(); // This should not be possible
},
}, data);
fprintf(stderr, "(%" PRIu32 ")", curLineNo);
return topName;
} }
void printDiag( void printDiag(
@@ -94,8 +98,8 @@ void printDiag(
fputs(type, stderr); fputs(type, stderr);
fputs(": ", stderr); fputs(": ", stderr);
if (where) { if (where) {
where->dumpFileStack(); where->dump(lineNo);
fprintf(stderr, "(%" PRIu32 "): ", lineNo); fputs(": ", stderr);
} }
vfprintf(stderr, fmt, args); vfprintf(stderr, fmt, args);
putc('\n', stderr); putc('\n', stderr);

View File

@@ -29,10 +29,10 @@ void sym_AddSymbol(Symbol &symbol) {
// Check if the symbol already exists // Check if the symbol already exists
if (Symbol *other = sym_GetSymbol(symbol.name); other) { if (Symbol *other = sym_GetSymbol(symbol.name); other) {
fprintf(stderr, "error: \"%s\" both in %s from ", symbol.name.c_str(), symbol.objFileName); fprintf(stderr, "error: \"%s\" both in %s from ", symbol.name.c_str(), symbol.objFileName);
symbol.src->dumpFileStack(); symbol.src->dump(symbol.lineNo);
fprintf(stderr, "(%" PRIu32 ") and in %s from ", symbol.lineNo, other->objFileName); fprintf(stderr, " and in %s from ", other->objFileName);
other->src->dumpFileStack(); other->src->dump(other->lineNo);
fprintf(stderr, "(%" PRIu32 ")\n", other->lineNo); fputc('\n', stderr);
exit(1); exit(1);
} }