From 1bec3fb337b86f87e7600fc6b6072ded1a7d4927 Mon Sep 17 00:00:00 2001 From: Ben Burdette Date: Fri, 25 Mar 2022 18:15:31 -0600 Subject: [PATCH] add DebugTrace for error --- src/libcmd/command.cc | 54 +++++++++++++++++++++++++++--------------- src/libexpr/eval.cc | 3 ++- src/libexpr/eval.hh | 1 + src/libexpr/nixexpr.hh | 40 +++++++++++++++---------------- 4 files changed, 58 insertions(+), 40 deletions(-) diff --git a/src/libcmd/command.cc b/src/libcmd/command.cc index 209c455fc..34535802f 100644 --- a/src/libcmd/command.cc +++ b/src/libcmd/command.cc @@ -122,29 +122,45 @@ ref EvalCommand::getEvalState() debuggerHook = [evalState{ref(evalState)}](const Error * error, const Env & env, const Expr & expr) { // clear the screen. // std::cout << "\033[2J\033[1;1H"; - + + auto dts = + error && expr.getPos() ? + std::unique_ptr( + new DebugTraceStacker( + *evalState, + DebugTrace + {.pos = *expr.getPos(), + .expr = expr, + .env = env, + .hint = error->info().msg, + .is_error = true + })) + : nullptr; + + if (error) printError("%s\n\n" ANSI_BOLD "Starting REPL to allow you to inspect the current state of the evaluator.\n" ANSI_NORMAL, error->what()); - else - { - auto iter = evalState->debugTraces.begin(); - if (iter != evalState->debugTraces.end()) { - std::cout << "\n" << "… " << iter->hint.str() << "\n"; - if (iter->pos.has_value() && (*iter->pos)) { - auto pos = iter->pos.value(); - std::cout << "\n"; - printAtPos(pos, std::cout); + // else + // { + // auto iter = evalState->debugTraces.begin(); + // if (iter != evalState->debugTraces.end()) { + // std::cout << "\n" << "… " << iter->hint.str() << "\n"; - auto loc = getCodeLines(pos); - if (loc.has_value()) { - std::cout << "\n"; - printCodeLines(std::cout, "", pos, *loc); - std::cout << "\n"; - } - } - } - } + // if (iter->pos.has_value() && (*iter->pos)) { + // auto pos = iter->pos.value(); + // std::cout << "\n"; + // printAtPos(pos, std::cout); + + // auto loc = getCodeLines(pos); + // if (loc.has_value()) { + // std::cout << "\n"; + // printCodeLines(std::cout, "", pos, *loc); + // std::cout << "\n"; + // } + // } + // } + // } if (expr.staticenv) { diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index 6758677ca..f162f3e0b 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -983,7 +983,8 @@ LocalNoInline(std::unique_ptr {.pos = pos, .expr = expr, .env = env, - .hint = hintfmt(s, s2) + .hint = hintfmt(s, s2), + .is_error = false })); } diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh index 64c3dfac0..9de3475e8 100644 --- a/src/libexpr/eval.hh +++ b/src/libexpr/eval.hh @@ -77,6 +77,7 @@ struct DebugTrace { const Expr &expr; const Env &env; hintformat hint; + bool is_error; }; class EvalState diff --git a/src/libexpr/nixexpr.hh b/src/libexpr/nixexpr.hh index 64375b5ab..6419f882a 100644 --- a/src/libexpr/nixexpr.hh +++ b/src/libexpr/nixexpr.hh @@ -84,7 +84,7 @@ struct Expr virtual void setName(Symbol & name); std::shared_ptr staticenv; - virtual Pos* getPos() = 0; + virtual const Pos* getPos() const = 0; }; std::ostream & operator << (std::ostream & str, const Expr & e); @@ -100,7 +100,7 @@ struct ExprInt : Expr Value v; ExprInt(NixInt n) : n(n) { v.mkInt(n); }; Value * maybeThunk(EvalState & state, Env & env); - Pos* getPos() { return 0; } + const Pos* getPos() const { return 0; } COMMON_METHODS }; @@ -110,7 +110,7 @@ struct ExprFloat : Expr Value v; ExprFloat(NixFloat nf) : nf(nf) { v.mkFloat(nf); }; Value * maybeThunk(EvalState & state, Env & env); - Pos* getPos() { return 0; } + const Pos* getPos() const { return 0; } COMMON_METHODS }; @@ -120,7 +120,7 @@ struct ExprString : Expr Value v; ExprString(std::string s) : s(std::move(s)) { v.mkString(this->s.data()); }; Value * maybeThunk(EvalState & state, Env & env); - Pos* getPos() { return 0; } + const Pos* getPos() const { return 0; } COMMON_METHODS }; @@ -130,7 +130,7 @@ struct ExprPath : Expr Value v; ExprPath(const string & s) : s(s) { v.mkPath(this->s.c_str()); }; Value * maybeThunk(EvalState & state, Env & env); - Pos* getPos() { return 0; } + const Pos* getPos() const { return 0; } COMMON_METHODS }; @@ -158,7 +158,7 @@ struct ExprVar : Expr ExprVar(const Symbol & name) : name(name) { }; ExprVar(const Pos & pos, const Symbol & name) : pos(pos), name(name) { }; Value * maybeThunk(EvalState & state, Env & env); - Pos* getPos() { return &pos; } + const Pos* getPos() const { return &pos; } COMMON_METHODS }; @@ -169,7 +169,7 @@ struct ExprSelect : Expr AttrPath attrPath; ExprSelect(const Pos & pos, Expr * e, const AttrPath & attrPath, Expr * def) : pos(pos), e(e), def(def), attrPath(attrPath) { }; ExprSelect(const Pos & pos, Expr * e, const Symbol & name) : pos(pos), e(e), def(0) { attrPath.push_back(AttrName(name)); }; - Pos* getPos() { return &pos; } + const Pos* getPos() const { return &pos; } COMMON_METHODS }; @@ -178,7 +178,7 @@ struct ExprOpHasAttr : Expr Expr * e; AttrPath attrPath; ExprOpHasAttr(Expr * e, const AttrPath & attrPath) : e(e), attrPath(attrPath) { }; - Pos* getPos() { return e->getPos(); } + const Pos* getPos() const { return e->getPos(); } COMMON_METHODS }; @@ -207,7 +207,7 @@ struct ExprAttrs : Expr DynamicAttrDefs dynamicAttrs; ExprAttrs(const Pos &pos) : recursive(false), pos(pos) { }; ExprAttrs() : recursive(false), pos(noPos) { }; - Pos* getPos() { return &pos; } + const Pos* getPos() const { return &pos; } COMMON_METHODS }; @@ -215,7 +215,7 @@ struct ExprList : Expr { std::vector elems; ExprList() { }; - Pos* getPos() { return 0; } + const Pos* getPos() const { return 0; } COMMON_METHODS }; @@ -264,7 +264,7 @@ struct ExprLambda : Expr void setName(Symbol & name); string showNamePos() const; inline bool hasFormals() const { return formals != nullptr; } - Pos* getPos() { return &pos; } + const Pos* getPos() const { return &pos; } COMMON_METHODS }; @@ -276,7 +276,7 @@ struct ExprCall : Expr ExprCall(const Pos & pos, Expr * fun, std::vector && args) : fun(fun), args(args), pos(pos) { } - Pos* getPos() { return &pos; } + const Pos* getPos() const { return &pos; } COMMON_METHODS }; @@ -285,7 +285,7 @@ struct ExprLet : Expr ExprAttrs * attrs; Expr * body; ExprLet(ExprAttrs * attrs, Expr * body) : attrs(attrs), body(body) { }; - Pos* getPos() { return 0; } + const Pos* getPos() const { return 0; } COMMON_METHODS }; @@ -295,7 +295,7 @@ struct ExprWith : Expr Expr * attrs, * body; size_t prevWith; ExprWith(const Pos & pos, Expr * attrs, Expr * body) : pos(pos), attrs(attrs), body(body) { }; - Pos* getPos() { return &pos; } + const Pos* getPos() const { return &pos; } COMMON_METHODS }; @@ -304,7 +304,7 @@ struct ExprIf : Expr Pos pos; Expr * cond, * then, * else_; ExprIf(const Pos & pos, Expr * cond, Expr * then, Expr * else_) : pos(pos), cond(cond), then(then), else_(else_) { }; - Pos* getPos() { return &pos; } + const Pos* getPos() const { return &pos; } COMMON_METHODS }; @@ -313,7 +313,7 @@ struct ExprAssert : Expr Pos pos; Expr * cond, * body; ExprAssert(const Pos & pos, Expr * cond, Expr * body) : pos(pos), cond(cond), body(body) { }; - Pos* getPos() { return &pos; } + const Pos* getPos() const { return &pos; } COMMON_METHODS }; @@ -321,7 +321,7 @@ struct ExprOpNot : Expr { Expr * e; ExprOpNot(Expr * e) : e(e) { }; - Pos* getPos() { return 0; } + const Pos* getPos() const { return 0; } COMMON_METHODS }; @@ -341,7 +341,7 @@ struct ExprOpNot : Expr e1->bindVars(env); e2->bindVars(env); \ } \ void eval(EvalState & state, Env & env, Value & v); \ - Pos* getPos() { return &pos; } \ + const Pos* getPos() const { return &pos; } \ }; MakeBinOp(ExprOpEq, "==") @@ -359,7 +359,7 @@ struct ExprConcatStrings : Expr vector > * es; ExprConcatStrings(const Pos & pos, bool forceString, vector > * es) : pos(pos), forceString(forceString), es(es) { }; - Pos* getPos() { return &pos; } + const Pos* getPos() const { return &pos; } COMMON_METHODS }; @@ -367,7 +367,7 @@ struct ExprPos : Expr { Pos pos; ExprPos(const Pos & pos) : pos(pos) { }; - Pos* getPos() { return &pos; } + const Pos* getPos() const { return &pos; } COMMON_METHODS };