From deb1fd66e8c884937827813c079135532913ca86 Mon Sep 17 00:00:00 2001 From: Ben Burdette Date: Thu, 23 Dec 2021 09:08:41 -0700 Subject: [PATCH] makeDebugTraceStacker --- src/libexpr/eval.cc | 93 +++++++++++++++------------------------------ 1 file changed, 31 insertions(+), 62 deletions(-) diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index 15eaef667..d7deb550e 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -49,10 +49,8 @@ class DebugTraceStacker { // assert(evalState.debugTraces.front() == trace); evalState.debugTraces.pop_front(); } - EvalState &evalState; Trace trace; - }; static char * dupString(const char * s) @@ -882,8 +880,7 @@ LocalNoInlineNoReturn(void throwUndefinedVarError(const Pos & pos, const char * }); if (debuggerHook && expr) { - - std::cout << "throwUndefinedVarError debuggerHook" << std::endl; + std::cout << "throwUndefinedVarError debuggerHook" << std::endl; debuggerHook(error, env, *expr); } @@ -913,15 +910,17 @@ LocalNoInline(void addErrorTrace(Error & e, const Pos & pos, const char * s, con e.addTrace(pos, s, s2); } -// LocalNoInline(void makeErrorTrace(Error & e, const char * s, const string & s2)) -// { -// Trace { .pos = e, .hint = hint } -// } +LocalNoInline(std::unique_ptr + makeDebugTraceStacker(EvalState &state, std::optional pos, const char * s, const string & s2)) +{ + return std::unique_ptr( + new DebugTraceStacker( + state, + Trace {.pos = pos, + .hint = hintfmt(s, s2) + })); +} -// LocalNoInline(void makeErrorTrace(Error & e, const Pos & pos, const char * s, const string & s2)) -// { -// return Trace { .pos = e, .hint = hintfmt(s, s2); }; -// } void mkString(Value & v, const char * s) { @@ -1127,20 +1126,6 @@ void EvalState::resetFileCache() fileParseCache.clear(); } -// class DebugTraceStacker { -// DebugTraceStacker(std::ref evalState, std::ref t) -// :evalState(evalState), trace(t) -// { -// evalState->debugTraces.push_front(t); -// } -// ~DebugTraceStacker() { -// assert(evalState->debugTraces.pop_front() == trace); -// } - -// std::ref evalState; -// std::ref trace; - -// }; @@ -1154,15 +1139,14 @@ void EvalState::cacheFile( fileParseCache[resolvedPath] = e; try { - std::unique_ptr dts = debuggerHook ? - std::unique_ptr(new DebugTraceStacker(*this, - Trace { .pos = (e->getPos() ? std::optional(ErrPos(*e->getPos())) : std::nullopt), - .hint = hintfmt("while evaluating the file '%1%':", resolvedPath) - } - )) : std::unique_ptr(); + std::unique_ptr dts = + debuggerHook ? + makeDebugTraceStacker( + *this, + (e->getPos() ? std::optional(ErrPos(*e->getPos())) : std::nullopt), + "while evaluating the file '%1%':", resolvedPath) + : std::unique_ptr(); - // Trace( .pos = (e->getPos() ? std::optional(ErrPos(*e->getPos())): - // std::nullopt), hintfmt("while evaluating the file '%1%':", resolvedPath)); // Enforce that 'flake.nix' is a direct attrset, not a // computation. if (mustBeTrivial && @@ -1387,16 +1371,13 @@ void ExprSelect::eval(EvalState & state, Env & env, Value & v) e->eval(state, env, vTmp); try { - std::unique_ptr dts = - debuggerHook ? - std::unique_ptr( - new DebugTraceStacker( - state, - Trace { .pos = *pos2, - .hint = hintfmt( - "while evaluating the attribute '%1%'", - showAttrPath(state, env, attrPath)) - })) + std::unique_ptr dts = + debuggerHook ? + makeDebugTraceStacker( + state, + *pos2, + "while evaluating the attribute '%1%'", + showAttrPath(state, env, attrPath)) : std::unique_ptr(); for (auto & i : attrPath) { @@ -1541,21 +1522,15 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value & /* Evaluate the body. */ try { - std::unique_ptr dts = - debuggerHook ? - std::unique_ptr( - new DebugTraceStacker( - *this, - Trace {.pos = lambda.pos, - .hint = hintfmt( + std::unique_ptr dts = + debuggerHook ? + makeDebugTraceStacker(*this, lambda.pos, "while evaluating %s", (lambda.name.set() ? "'" + (string) lambda.name + "'" : "anonymous lambda")) - })) : std::unique_ptr(); - lambda.body->eval(*this, env2, vCur); } catch (Error & e) { std::cout << "eval showErrorInfo showTrace: " << loggerSettings.showTrace.get() << std::endl; @@ -1950,18 +1925,12 @@ void EvalState::forceValueDeep(Value & v) if (v.type() == nAttrs) { for (auto & i : *v.attrs) try { - std::unique_ptr dts = - debuggerHook ? - std::unique_ptr( - new DebugTraceStacker( - *this, - Trace { .pos = *i.pos, - .hint = hintfmt( + std::unique_ptr dts = + debuggerHook ? + makeDebugTraceStacker(*this, *i.pos, "while evaluating the attribute '%1%'", i.name) - })) : std::unique_ptr(); - recurse(*i.value); } catch (Error & e) { addErrorTrace(e, *i.pos, "while evaluating the attribute '%1%'", i.name);