aboutsummaryrefslogtreecommitdiff
path: root/src/libexpr
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2019-11-05 11:20:53 +0100
committerEelco Dolstra <edolstra@gmail.com>2019-11-05 11:20:53 +0100
commit852554bb16c767950bace54bf44bb949608a394d (patch)
tree561d79a7dedefa8829f5558d4009e86342011bba /src/libexpr
parent78760270713992a7fcba761c8be01ccbc0214fd8 (diff)
parent9a2505965667267f03a8385926f3b31a47732ed5 (diff)
Merge branch 'nix-repl-e' of https://github.com/zimbatm/nix
Diffstat (limited to 'src/libexpr')
-rw-r--r--src/libexpr/attr-path.cc32
-rw-r--r--src/libexpr/attr-path.hh3
2 files changed, 35 insertions, 0 deletions
diff --git a/src/libexpr/attr-path.cc b/src/libexpr/attr-path.cc
index b0f80db32..06b472d8b 100644
--- a/src/libexpr/attr-path.cc
+++ b/src/libexpr/attr-path.cc
@@ -93,4 +93,36 @@ Value * findAlongAttrPath(EvalState & state, const string & attrPath,
}
+Pos findDerivationFilename(EvalState & state, Value & v, std::string what)
+{
+ Value * v2;
+ try {
+ auto dummyArgs = state.allocBindings(0);
+ v2 = findAlongAttrPath(state, "meta.position", *dummyArgs, v);
+ } catch (Error &) {
+ throw Error("package '%s' has no source location information", what);
+ }
+
+ // FIXME: is it possible to extract the Pos object instead of doing this
+ // toString + parsing?
+ auto pos = state.forceString(*v2);
+
+ auto colon = pos.rfind(':');
+ if (colon == std::string::npos)
+ throw Error("cannot parse meta.position attribute '%s'", pos);
+
+ std::string filename(pos, 0, colon);
+ unsigned int lineno;
+ try {
+ lineno = std::stoi(std::string(pos, colon + 1));
+ } catch (std::invalid_argument & e) {
+ throw Error("cannot parse line number '%s'", pos);
+ }
+
+ Symbol file = state.symbols.create(filename);
+
+ return { file, lineno, 0 };
+}
+
+
}
diff --git a/src/libexpr/attr-path.hh b/src/libexpr/attr-path.hh
index 46a341950..716e5ba27 100644
--- a/src/libexpr/attr-path.hh
+++ b/src/libexpr/attr-path.hh
@@ -10,4 +10,7 @@ namespace nix {
Value * findAlongAttrPath(EvalState & state, const string & attrPath,
Bindings & autoArgs, Value & vIn);
+/* Heuristic to find the filename and lineno or a nix value. */
+Pos findDerivationFilename(EvalState & state, Value & v, std::string what);
+
}