Factor out shared backtrace code (#1793)

This commit is contained in:
Rangi
2025-08-12 17:56:54 -04:00
committed by GitHub
parent 50d0b101c3
commit 9c3ce69180
14 changed files with 118 additions and 173 deletions

View File

@@ -9,9 +9,9 @@
#include <stdio.h>
#include <vector>
#include "backtrace.hpp"
#include "helpers.hpp"
#include "itertools.hpp"
#include "style.hpp"
#include "util.hpp"
#include "link/warning.hpp"
@@ -28,52 +28,12 @@ struct LexerStackEntry {
static std::vector<LexerStackEntry> lexerStack;
static void printStackEntry(LexerStackEntry const &context) {
style_Set(stderr, STYLE_CYAN, true);
fputs(context.path.c_str(), stderr);
style_Set(stderr, STYLE_CYAN, false);
fprintf(stderr, "(%" PRIu32 ")", context.lineNo);
}
void lexer_TraceCurrent() {
size_t n = lexerStack.size();
if (warnings.traceDepth == TRACE_COLLAPSE) {
fputs(" ", stderr); // Just three spaces; the fourth will be handled by the loop
for (size_t i = 0; i < n; ++i) {
style_Reset(stderr);
fprintf(stderr, " %s ", i == 0 ? "at" : "<-");
printStackEntry(lexerStack[n - i - 1]);
}
putc('\n', stderr);
} else if (warnings.traceDepth == 0 || static_cast<size_t>(warnings.traceDepth) >= n) {
for (size_t i = 0; i < n; ++i) {
style_Reset(stderr);
fprintf(stderr, " %s ", i == 0 ? "at" : "<-");
printStackEntry(lexerStack[n - i - 1]);
putc('\n', stderr);
}
} else {
size_t last = warnings.traceDepth / 2;
size_t first = warnings.traceDepth - last;
size_t skipped = n - warnings.traceDepth;
for (size_t i = 0; i < first; ++i) {
style_Reset(stderr);
fprintf(stderr, " %s ", i == 0 ? "at" : "<-");
printStackEntry(lexerStack[n - i - 1]);
putc('\n', stderr);
}
style_Reset(stderr);
fprintf(stderr, " ...%zu more%s\n", skipped, last ? "..." : "");
for (size_t i = n - last; i < n; ++i) {
style_Reset(stderr);
fputs(" <- ", stderr);
printStackEntry(lexerStack[n - i - 1]);
putc('\n', stderr);
}
}
style_Reset(stderr);
trace_PrintBacktrace(
lexerStack,
[](LexerStackEntry const &context) { return context.path.c_str(); },
[](LexerStackEntry const &context) { return context.lineNo; }
);
}
void lexer_IncludeFile(std::string &&path) {