aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/error.hh
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2022-12-13 00:48:04 +0100
committerEelco Dolstra <edolstra@gmail.com>2022-12-13 00:50:43 +0100
commitb3fdab28a216683365f7f04bfa9bbc5cd122d753 (patch)
tree54bd67dc94f2596be2d77a13b0b84a0afde8b067 /src/libutil/error.hh
parentedb54c62e6c66f3d26642c024d92bd20b30abf85 (diff)
Introduce AbstractPos
This makes the position object used in exceptions abstract, with a method getSource() to get the source code of the file in which the error originated. This is needed for lazy trees because source files don't necessarily exist in the filesystem, and we don't want to make libutil depend on the InputAccessor type in libfetcher.
Diffstat (limited to 'src/libutil/error.hh')
-rw-r--r--src/libutil/error.hh58
1 files changed, 17 insertions, 41 deletions
diff --git a/src/libutil/error.hh b/src/libutil/error.hh
index 3d1479c54..c3bb8c0df 100644
--- a/src/libutil/error.hh
+++ b/src/libutil/error.hh
@@ -54,13 +54,6 @@ typedef enum {
lvlVomit
} Verbosity;
-/* adjust Pos::origin bit width when adding stuff here */
-typedef enum {
- foFile,
- foStdin,
- foString
-} FileOrigin;
-
// the lines of code surrounding an error.
struct LinesOfCode {
std::optional<std::string> prevLineOfCode;
@@ -68,54 +61,37 @@ struct LinesOfCode {
std::optional<std::string> nextLineOfCode;
};
-// ErrPos indicates the location of an error in a nix file.
-struct ErrPos {
- int line = 0;
- int column = 0;
- std::string file;
- FileOrigin origin;
+/* An abstract type that represents a location in a source file. */
+struct AbstractPos
+{
+ uint32_t line = 0;
+ uint32_t column = 0;
- operator bool() const
- {
- return line != 0;
- }
+ /* Return the contents of the source file. */
+ virtual std::optional<std::string> getSource() const
+ { return std::nullopt; };
- // convert from the Pos struct, found in libexpr.
- template <class P>
- ErrPos & operator=(const P & pos)
- {
- origin = pos.origin;
- line = pos.line;
- column = pos.column;
- file = pos.file;
- return *this;
- }
+ virtual void print(std::ostream & out) const = 0;
- template <class P>
- ErrPos(const P & p)
- {
- *this = p;
- }
+ std::optional<LinesOfCode> getCodeLines() const;
};
-std::optional<LinesOfCode> getCodeLines(const ErrPos & errPos);
+std::ostream & operator << (std::ostream & str, const AbstractPos & pos);
void printCodeLines(std::ostream & out,
const std::string & prefix,
- const ErrPos & errPos,
+ const AbstractPos & errPos,
const LinesOfCode & loc);
-void printAtPos(const ErrPos & pos, std::ostream & out);
-
struct Trace {
- std::optional<ErrPos> pos;
+ std::shared_ptr<AbstractPos> pos;
hintformat hint;
};
struct ErrorInfo {
Verbosity level;
hintformat msg;
- std::optional<ErrPos> errPos;
+ std::shared_ptr<AbstractPos> errPos;
std::list<Trace> traces;
Suggestions suggestions;
@@ -177,12 +153,12 @@ public:
const ErrorInfo & info() const { calcWhat(); return err; }
template<typename... Args>
- void addTrace(std::optional<ErrPos> e, const std::string & fs, const Args & ... args)
+ void addTrace(std::shared_ptr<AbstractPos> && e, const std::string & fs, const Args & ... args)
{
- addTrace(e, hintfmt(fs, args...));
+ addTrace(std::move(e), hintfmt(fs, args...));
}
- void addTrace(std::optional<ErrPos> e, hintformat hint);
+ void addTrace(std::shared_ptr<AbstractPos> && e, hintformat hint);
bool hasTrace() const { return !err.traces.empty(); }
};