From 18a6f8b12e65eefe6e5160e3d7d50ec9d9a7eb2b Mon Sep 17 00:00:00 2001 From: Rangi <35663410+Rangi42@users.noreply.github.com> Date: Tue, 24 Nov 2026 12:52:38 -0500 Subject: [PATCH] Unify RGBASM and RGBLINK fstack node backtrace printing methods into one generic function (#2253) Remove now-trivial `printBacktrace` methods; just call `trace_PrintFstackNodeBacktrace` directly. --- Makefile | 1 - include/asm/fstack.hpp | 3 +- include/backtrace.hpp | 63 +++++++++++++++++++++++++++++++++++- include/link/fstack.hpp | 3 +- src/CMakeLists.txt | 1 - src/asm/fstack.cpp | 60 +--------------------------------- src/asm/section.cpp | 3 +- src/asm/symbol.cpp | 3 +- src/link/fstack.cpp | 71 ----------------------------------------- src/link/object.cpp | 2 +- src/link/symbol.cpp | 5 +-- src/link/warning.cpp | 13 ++++---- 12 files changed, 82 insertions(+), 146 deletions(-) delete mode 100644 src/link/fstack.cpp diff --git a/Makefile b/Makefile index 1c492bad..b69a473c 100644 --- a/Makefile +++ b/Makefile @@ -87,7 +87,6 @@ src/asm/lexer.o src/asm/main.o: src/asm/parser.hpp rgblink_obj := \ ${common_obj} \ src/link/assign.o \ - src/link/fstack.o \ src/link/lexer.o \ src/link/layout.o \ src/link/main.o \ diff --git a/include/asm/fstack.hpp b/include/asm/fstack.hpp index aa8eadaa..bda96605 100644 --- a/include/asm/fstack.hpp +++ b/include/asm/fstack.hpp @@ -49,7 +49,8 @@ struct FileStackNode { ) : type(type_), data(data_), isQuiet(isQuiet_) {} - void printBacktrace(uint32_t curLineNo) const; + // For use inside `trace_PrintFstackNodeBacktrace` + FileStackNode const *parentNode() const { return parent.get(); } }; struct MacroArgs; diff --git a/include/backtrace.hpp b/include/backtrace.hpp index 95710898..ca0cc9ed 100644 --- a/include/backtrace.hpp +++ b/include/backtrace.hpp @@ -7,9 +7,14 @@ #include #include #include +#include +#include +#include #include -#include "helpers.hpp" // InvocableR +#include "helpers.hpp" // InvocableR +#include "itertools.hpp" // reversed +#include "linkdefs.hpp" // NODE_REPT #include "style.hpp" #define TRACE_SEPARATOR "<-" @@ -91,4 +96,60 @@ void trace_PrintBacktrace( style_Reset(stderr); } +template +void trace_PrintFstackNodeBacktrace(NodeT const &fstackNode, uint32_t curLineNo) { + using TraceItem = std::pair; + std::vector items; + for (TraceItem item{&fstackNode, curLineNo};;) { + auto &[node, itemLineNo] = item; + bool loud = !node->isQuiet || tracing.loud; + if (loud) { + items.emplace_back(node, itemLineNo); + } + if (!node->parentNode()) { + assume(node->type != NODE_REPT && std::holds_alternative(node->data)); + break; + } + if (loud || node->type != NODE_REPT) { + // Quiet REPT nodes will pass their interior line number up to their parent, + // which is more precise than the parent's own line number (since that will be + // the line number of the "REPT?" or "FOR?" itself). + itemLineNo = node->lineNo; + } + node = &*node->parentNode(); + } + + using TraceNode = std::pair; + std::vector traceNodes; + traceNodes.reserve(items.size()); + for (auto &[node, itemLineNo] : reversed(items)) { + if (std::holds_alternative>(node->data)) { + // REPT nodes use their first non-REPT parent's name (which may be a quiet node and + // thus not in `traceNodes`), followed by their REPT parents' iteration counts, + // followed by their own iteration count. + std::string reptName; + for (NodeT const *ancestor = node->parentNode();; ancestor = ancestor->parentNode()) { + assume(ancestor != nullptr); // The top level context cannot be a REPT + if (ancestor->type != NODE_REPT) { + reptName.append(ancestor->name()); + break; + } + } + for (uint32_t iter : reversed(node->iters())) { + reptName.append(NODE_SEPARATOR REPT_NODE_PREFIX); + reptName.append(std::to_string(iter)); + } + traceNodes.emplace_back(std::move(reptName), itemLineNo); + } else { + traceNodes.emplace_back(node->name(), itemLineNo); + } + } + + trace_PrintBacktrace( + traceNodes, + [](TraceNode const &node) { return node.first.c_str(); }, + [](TraceNode const &node) { return node.second; } + ); +} + #endif // RGBDS_BACKTRACE_HPP diff --git a/include/link/fstack.hpp b/include/link/fstack.hpp index 9057fe75..b794f5e0 100644 --- a/include/link/fstack.hpp +++ b/include/link/fstack.hpp @@ -32,7 +32,8 @@ struct FileStackNode { std::string &name() { return std::get(data); } std::string const &name() const { return std::get(data); } - void printBacktrace(uint32_t curLineNo) const; + // For use inside `trace_PrintFstackNodeBacktrace` + FileStackNode const *parentNode() const { return parent; } }; #endif // RGBDS_LINK_FSTACK_HPP diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 90b4d6a5..14953919 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -63,7 +63,6 @@ bison_target(linker_script_parser "link/script.y" add_executable(rgblink $ "${BISON_linker_script_parser_OUTPUT_SOURCE}" "link/assign.cpp" - "link/fstack.cpp" "link/lexer.cpp" "link/layout.cpp" "link/main.cpp" diff --git a/src/asm/fstack.cpp b/src/asm/fstack.cpp index f2c99798..7ae222fb 100644 --- a/src/asm/fstack.cpp +++ b/src/asm/fstack.cpp @@ -16,8 +16,6 @@ #include #include #include -#include -#include #include #include "backtrace.hpp" @@ -59,66 +57,10 @@ static std::vector includePaths = {""}; // -I static std::deque preIncludeStack; // -P static bool failedOnMissingInclude = false; -void FileStackNode::printBacktrace(uint32_t curLineNo) const { - using TraceItem = std::pair; - std::vector items; - for (TraceItem item{this, curLineNo};;) { - auto &[node, itemLineNo] = item; - bool loud = !node->isQuiet || tracing.loud; - if (loud) { - items.emplace_back(node, itemLineNo); - } - if (!node->parent) { - assume(node->type != NODE_REPT && std::holds_alternative(node->data)); - break; - } - if (loud || node->type != NODE_REPT) { - // Quiet REPT nodes will pass their interior line number up to their parent, - // which is more precise than the parent's own line number (since that will be - // the line number of the "REPT?" or "FOR?" itself). - itemLineNo = node->lineNo; - } - node = &*node->parent; - } - - using TraceNode = std::pair; - std::vector traceNodes; - traceNodes.reserve(items.size()); - for (auto &[node, itemLineNo] : reversed(items)) { - if (std::holds_alternative>(node->data)) { - // REPT nodes use their first non-REPT parent's name (which may be a quiet node and - // thus not in `traceNodes`), followed by their REPT parents' iteration counts, - // followed by their own iteration count. - std::string reptName; - for (FileStackNode const *ancestor = node->parent.get();; - ancestor = ancestor->parent.get()) { - assume(ancestor != nullptr); // The top level context cannot be a REPT - if (ancestor->type != NODE_REPT) { - reptName.append(ancestor->name()); - break; - } - } - for (uint32_t iter : reversed(node->iters())) { - reptName.append(NODE_SEPARATOR REPT_NODE_PREFIX); - reptName.append(std::to_string(iter)); - } - traceNodes.emplace_back(std::move(reptName), itemLineNo); - } else { - traceNodes.emplace_back(node->name(), itemLineNo); - } - } - - trace_PrintBacktrace( - traceNodes, - [](TraceNode const &node) { return node.first.c_str(); }, - [](TraceNode const &node) { return node.second; } - ); -} - void fstk_TraceCurrent() { if (!lexer_AtTopLevel()) { assume(!contextStack.empty()); - contextStack.top().fileInfo->printBacktrace(lexer_GetLineNo()); + trace_PrintFstackNodeBacktrace(*contextStack.top().fileInfo, lexer_GetLineNo()); } lexer_TraceStringExpansions(); } diff --git a/src/asm/section.cpp b/src/asm/section.cpp index 24ba30a8..b29f0855 100644 --- a/src/asm/section.cpp +++ b/src/asm/section.cpp @@ -20,6 +20,7 @@ #include #include +#include "backtrace.hpp" #include "helpers.hpp" #include "itertools.hpp" // InsertionOrderedMap #include "linkdefs.hpp" @@ -301,7 +302,7 @@ static void mergeSections( fprintf(stderr, "Section \"%s\" already defined\n", sect.name.c_str()); fstk_TraceCurrent(); fputs(" and also:\n", stderr); - sect.src->printBacktrace(sect.fileLine); + trace_PrintFstackNodeBacktrace(*sect.src, sect.fileLine); }); case SECTION_UNION: diff --git a/src/asm/symbol.cpp b/src/asm/symbol.cpp index 673e5623..d61745f7 100644 --- a/src/asm/symbol.cpp +++ b/src/asm/symbol.cpp @@ -16,6 +16,7 @@ #include #include +#include "backtrace.hpp" #include "diagnostics.hpp" #include "helpers.hpp" // assume #include "util.hpp" @@ -152,7 +153,7 @@ static void printBacktraces(Symbol const &sym) { fstk_TraceCurrent(); fputs(" and also:\n", stderr); if (sym.src) { - sym.src->printBacktrace(sym.fileLine); + trace_PrintFstackNodeBacktrace(*sym.src, sym.fileLine); } else { fprintf(stderr, " at <%s>\n", sym.isBuiltin ? "builtin" : "command-line"); } diff --git a/src/link/fstack.cpp b/src/link/fstack.cpp deleted file mode 100644 index 560ba35f..00000000 --- a/src/link/fstack.cpp +++ /dev/null @@ -1,71 +0,0 @@ -// SPDX-License-Identifier: MIT - -#include "link/fstack.hpp" - -#include -#include -#include -#include -#include - -#include "backtrace.hpp" -#include "helpers.hpp" -#include "linkdefs.hpp" - -#include "link/warning.hpp" - -void FileStackNode::printBacktrace(uint32_t curLineNo) const { - using TraceItem = std::pair; - std::vector items; - for (TraceItem item{this, curLineNo};;) { - auto &[node, itemLineNo] = item; - bool loud = !node->isQuiet || tracing.loud; - if (loud) { - items.emplace_back(node, itemLineNo); - } - if (!node->parent) { - assume(node->type != NODE_REPT && std::holds_alternative(node->data)); - break; - } - if (loud || node->type != NODE_REPT) { - // Quiet REPT nodes will pass their interior line number up to their parent, - // which is more precise than the parent's own line number (since that will be - // the line number of the "REPT?" or "FOR?" itself). - itemLineNo = node->lineNo; - } - node = &*node->parent; - } - - using TraceNode = std::pair; - std::vector traceNodes; - traceNodes.reserve(items.size()); - for (auto &[node, itemLineNo] : reversed(items)) { - if (std::holds_alternative>(node->data)) { - // REPT nodes use their first non-REPT parent's name (which may be a quiet node and - // thus not in `traceNodes`), followed by their REPT parents' iteration counts, - // followed by their own iteration count. - std::string reptName; - for (FileStackNode const *ancestor = node->parent;; ancestor = ancestor->parent) { - // The top level context cannot be a REPT (guaranteed by `readFileStackNode`) - assume(ancestor != nullptr); - if (ancestor->type != NODE_REPT) { - reptName.append(ancestor->name()); - break; - } - } - for (uint32_t iter : node->iters()) { - reptName.append(NODE_SEPARATOR REPT_NODE_PREFIX); - reptName.append(std::to_string(iter)); - } - traceNodes.emplace_back(std::move(reptName), itemLineNo); - } else { - traceNodes.emplace_back(node->name(), itemLineNo); - } - } - - trace_PrintBacktrace( - traceNodes, - [](TraceNode const &node) { return node.first.c_str(); }, - [](TraceNode const &node) { return node.second; } - ); -} diff --git a/src/link/object.cpp b/src/link/object.cpp index be0133f8..336b176b 100644 --- a/src/link/object.cpp +++ b/src/link/object.cpp @@ -129,7 +129,7 @@ static void readFileStackNode( node.data = std::vector(depth); for (uint32_t i = 0; i < depth; ++i) { tryReadLong( - node.iters()[i], + node.iters()[depth - i - 1], file, "%s: Cannot read node #%" PRIu32 "'s iter #%" PRIu32 ": %s", fileName, diff --git a/src/link/symbol.cpp b/src/link/symbol.cpp index b8b58f09..39e9505b 100644 --- a/src/link/symbol.cpp +++ b/src/link/symbol.cpp @@ -11,7 +11,8 @@ #include #include -#include "helpers.hpp" // assume +#include "backtrace.hpp" // assume +#include "helpers.hpp" // assume #include "linkdefs.hpp" #include "link/fstack.hpp" @@ -87,7 +88,7 @@ void sym_TraceLocalAliasedSymbols(std::string const &name) { break; } assume(local->src); - local->src->printBacktrace(local->lineNo); + trace_PrintFstackNodeBacktrace(*local->src, local->lineNo); ++nbListed; } } diff --git a/src/link/warning.cpp b/src/link/warning.cpp index 6097e424..44867d8f 100644 --- a/src/link/warning.cpp +++ b/src/link/warning.cpp @@ -8,6 +8,7 @@ #include #include +#include "backtrace.hpp" #include "diagnostics.hpp" #include "style.hpp" @@ -59,7 +60,7 @@ void warning(FileStackNode const *src, uint32_t lineNo, char const *fmt, ...) { vwarnx(fmt, args); va_end(args); if (src) { - src->printBacktrace(lineNo); + trace_PrintFstackNodeBacktrace(*src, lineNo); } } @@ -77,7 +78,7 @@ void error(FileStackNode const *src, uint32_t lineNo, char const *fmt, ...) { va_end(args); if (src) { - src->printBacktrace(lineNo); + trace_PrintFstackNodeBacktrace(*src, lineNo); } warnings.incrementErrors(); } @@ -109,7 +110,7 @@ void fatal(FileStackNode const *src, uint32_t lineNo, char const *fmt, ...) { va_end(args); if (src) { - src->printBacktrace(lineNo); + trace_PrintFstackNodeBacktrace(*src, lineNo); } warnings.incrementErrors(); abortLinking(nullptr); @@ -140,9 +141,9 @@ void fatalTwo( vfatalx(fmt, args); va_end(args); - src1.printBacktrace(lineNo1); + trace_PrintFstackNodeBacktrace(src1, lineNo1); fputs(" and also:\n", stderr); - src2.printBacktrace(lineNo2); + trace_PrintFstackNodeBacktrace(src2, lineNo2); warnings.incrementErrors(); abortLinking(nullptr); } @@ -161,7 +162,7 @@ void warning(FileStackNode const *src, uint32_t lineNo, WarningID id, char const if (behavior != WarningBehavior::DISABLED) { if (src) { - src->printBacktrace(lineNo); + trace_PrintFstackNodeBacktrace(*src, lineNo); } if (behavior == WarningBehavior::ERROR) { warnings.incrementErrors();