aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/error.cc
diff options
context:
space:
mode:
authoreldritch horrors <pennae@lix.systems>2024-03-06 05:24:35 +0100
committereldritch horrors <pennae@lix.systems>2024-03-05 23:46:18 -0700
commit89e99d94e4ae492db09c0ebc0c35e4890ef7db25 (patch)
treeeab2defee911a6c0e39d4b073062690bd9a3a058 /src/libutil/error.cc
parente9b5929b22116cb714adfe88ba39a817e89b019c (diff)
Merge pull request #9634 from 9999years/combine-abstract-pos-and-pos
Combine `AbstractPos`, `PosAdapter`, and `Pos` (cherry picked from commit 113499d16fc87d53b73fb62fe6242154909756ed) === this is a bit cursed because originally it was based on InputAccessor code that we don't have and moved/patched features we likewise don't have (fetchToStore caching, all the individual accessors, ContentAddressMethod). the commit is adjusted accordingly to match (remove caching, ignore accessors, use FileIngestionMethod). note that `state.rootPath . CanonPath == abs` and computeStorePathForPath works relative to cwd, so the slight rewrite in the moved fetchToStore is legal. Change-Id: I05fd340c273f0bcc8ffabfebdc4a88b98083bce5
Diffstat (limited to 'src/libutil/error.cc')
-rw-r--r--src/libutil/error.cc55
1 files changed, 4 insertions, 51 deletions
diff --git a/src/libutil/error.cc b/src/libutil/error.cc
index 36afb22ae..a9e3b168f 100644
--- a/src/libutil/error.cc
+++ b/src/libutil/error.cc
@@ -1,4 +1,5 @@
#include "error.hh"
+#include "position.hh"
#include <iostream>
#include <optional>
@@ -9,7 +10,7 @@ namespace nix {
const std::string nativeSystem = SYSTEM;
-void BaseError::addTrace(std::shared_ptr<AbstractPos> && e, hintformat hint, bool frame)
+void BaseError::addTrace(std::shared_ptr<Pos> && e, hintformat hint, bool frame)
{
err.traces.push_front(Trace { .pos = std::move(e), .hint = hint, .frame = frame });
}
@@ -40,15 +41,6 @@ std::ostream & operator <<(std::ostream & os, const hintformat & hf)
return os << hf.str();
}
-std::ostream & operator <<(std::ostream & str, const AbstractPos & pos)
-{
- pos.print(str);
- str << ":" << pos.line;
- if (pos.column > 0)
- str << ":" << pos.column;
- return str;
-}
-
/**
* An arbitrarily defined value comparison for the purpose of using traces in the key of a sorted container.
*/
@@ -75,49 +67,10 @@ inline bool operator> (const Trace& lhs, const Trace& rhs) { return rhs < lhs; }
inline bool operator<=(const Trace& lhs, const Trace& rhs) { return !(lhs > rhs); }
inline bool operator>=(const Trace& lhs, const Trace& rhs) { return !(lhs < rhs); }
-std::optional<LinesOfCode> AbstractPos::getCodeLines() const
-{
- if (line == 0)
- return std::nullopt;
-
- if (auto source = getSource()) {
-
- std::istringstream iss(*source);
- // count the newlines.
- int count = 0;
- std::string curLine;
- int pl = line - 1;
-
- LinesOfCode loc;
-
- do {
- std::getline(iss, curLine);
- ++count;
- if (count < pl)
- ;
- else if (count == pl) {
- loc.prevLineOfCode = curLine;
- } else if (count == pl + 1) {
- loc.errLineOfCode = curLine;
- } else if (count == pl + 2) {
- loc.nextLineOfCode = curLine;
- break;
- }
-
- if (!iss.good())
- break;
- } while (true);
-
- return loc;
- }
-
- return std::nullopt;
-}
-
// print lines of code to the ostream, indicating the error column.
void printCodeLines(std::ostream & out,
const std::string & prefix,
- const AbstractPos & errPos,
+ const Pos & errPos,
const LinesOfCode & loc)
{
// previous line of code.
@@ -195,7 +148,7 @@ static bool printUnknownLocations = getEnv("_NIX_DEVELOPER_SHOW_UNKNOWN_LOCATION
*
* @return true if a position was printed.
*/
-static bool printPosMaybe(std::ostream & oss, std::string_view indent, const std::shared_ptr<AbstractPos> & pos) {
+static bool printPosMaybe(std::ostream & oss, std::string_view indent, const std::shared_ptr<Pos> & pos) {
bool hasPos = pos && *pos;
if (hasPos) {
oss << indent << ANSI_BLUE << "at " ANSI_WARNING << *pos << ANSI_NORMAL << ":";