Unify RGBASM and RGBLINK fstack node backtrace printing methods into one generic function (#2253)

Remove now-trivial `printBacktrace` methods; just call `trace_PrintFstackNodeBacktrace` directly.
This commit is contained in:
Rangi authored and GitHub committed 2026-11-24 12:52:38 -05:00
1 parent edfb9c15d5
commit 18a6f8b12e
12 files changed
+82 -146

No files matched your search

-1
View File
@@ -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 \
+2 -1
View File
@@ -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;
+62 -1
View File
@@ -7,9 +7,14 @@
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string>
#include <utility>
#include <variant>
#include <vector>
#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<typename NodeT>
void trace_PrintFstackNodeBacktrace(NodeT const &fstackNode, uint32_t curLineNo) {
using TraceItem = std::pair<NodeT const *, uint32_t>;
std::vector<TraceItem> 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<std::string>(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::string, uint32_t>;
std::vector<TraceNode> traceNodes;
traceNodes.reserve(items.size());
for (auto &[node, itemLineNo] : reversed(items)) {
if (std::holds_alternative<std::vector<uint32_t>>(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
+2 -1
View File
@@ -32,7 +32,8 @@ struct FileStackNode {
std::string &name() { return std::get<std::string>(data); }
std::string const &name() const { return std::get<std::string>(data); }
void printBacktrace(uint32_t curLineNo) const;
// For use inside `trace_PrintFstackNodeBacktrace`
FileStackNode const *parentNode() const { return parent; }
};
#endif // RGBDS_LINK_FSTACK_HPP
-1
View File
@@ -63,7 +63,6 @@ bison_target(linker_script_parser "link/script.y"
add_executable(rgblink $<TARGET_OBJECTS:common>
"${BISON_linker_script_parser_OUTPUT_SOURCE}"
"link/assign.cpp"
"link/fstack.cpp"
"link/lexer.cpp"
"link/layout.cpp"
"link/main.cpp"
+1 -59
View File
@@ -16,8 +16,6 @@
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <variant>
#include <vector>
#include "backtrace.hpp"
@@ -59,66 +57,10 @@ static std::vector<std::string> includePaths = {""}; // -I
static std::deque<std::string> preIncludeStack; // -P
static bool failedOnMissingInclude = false;
void FileStackNode::printBacktrace(uint32_t curLineNo) const {
using TraceItem = std::pair<FileStackNode const *, uint32_t>;
std::vector<TraceItem> 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<std::string>(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::string, uint32_t>;
std::vector<TraceNode> traceNodes;
traceNodes.reserve(items.size());
for (auto &[node, itemLineNo] : reversed(items)) {
if (std::holds_alternative<std::vector<uint32_t>>(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();
}
+2 -1
View File
@@ -20,6 +20,7 @@
#include <utility>
#include <vector>
#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:
+2 -1
View File
@@ -16,6 +16,7 @@
#include <utility>
#include <variant>
#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");
}
-71
View File
@@ -1,71 +0,0 @@
// SPDX-License-Identifier: MIT
#include "link/fstack.hpp"
#include <stdint.h>
#include <string>
#include <utility>
#include <variant>
#include <vector>
#include "backtrace.hpp"
#include "helpers.hpp"
#include "linkdefs.hpp"
#include "link/warning.hpp"
void FileStackNode::printBacktrace(uint32_t curLineNo) const {
using TraceItem = std::pair<FileStackNode const *, uint32_t>;
std::vector<TraceItem> 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<std::string>(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::string, uint32_t>;
std::vector<TraceNode> traceNodes;
traceNodes.reserve(items.size());
for (auto &[node, itemLineNo] : reversed(items)) {
if (std::holds_alternative<std::vector<uint32_t>>(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; }
);
}
+1 -1
View File
@@ -129,7 +129,7 @@ static void readFileStackNode(
node.data = std::vector<uint32_t>(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,
+3 -2
View File
@@ -11,7 +11,8 @@
#include <variant>
#include <vector>
#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;
}
}
+7 -6
View File
@@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdlib.h>
#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();