diff options
author | Théophane Hufschmitt <theophane.hufschmitt@tweag.io> | 2023-01-27 09:46:46 +0100 |
---|---|---|
committer | Théophane Hufschmitt <theophane.hufschmitt@tweag.io> | 2023-01-27 09:46:46 +0100 |
commit | ab424a39a966e2e3bfb2a34ba5cf4f1c49f86d2d (patch) | |
tree | 30209c669865c452207b780f328daa0b26731ed6 /src/libutil/error.hh | |
parent | 6da4cc92d8c546939818b65ba4f1b4ce65d88d6e (diff) | |
parent | ed479aafdc03f2e7428f182549cedab947824300 (diff) |
Merge remote-tracking branch 'nixos/master' into pr-flake-show-foreign
Diffstat (limited to 'src/libutil/error.hh')
-rw-r--r-- | src/libutil/error.hh | 70 |
1 files changed, 29 insertions, 41 deletions
diff --git a/src/libutil/error.hh b/src/libutil/error.hh index 3d1479c54..0ebeaba61 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,40 @@ 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; + + virtual ~AbstractPos() = default; }; -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; + bool frame; }; struct ErrorInfo { Verbosity level; hintformat msg; - std::optional<ErrPos> errPos; + std::shared_ptr<AbstractPos> errPos; std::list<Trace> traces; Suggestions suggestions; @@ -138,6 +117,8 @@ protected: public: unsigned int status = 1; // exit status + BaseError(const BaseError &) = default; + template<typename... Args> BaseError(unsigned int status, const Args & ... args) : err { .level = lvlError, .msg = hintfmt(args...) } @@ -176,15 +157,22 @@ public: const std::string & msg() const { return calcWhat(); } const ErrorInfo & info() const { calcWhat(); return err; } + void pushTrace(Trace trace) + { + err.traces.push_front(trace); + } + template<typename... Args> - void addTrace(std::optional<ErrPos> e, const std::string & fs, const Args & ... args) + void addTrace(std::shared_ptr<AbstractPos> && e, std::string_view fs, const Args & ... args) { - addTrace(e, hintfmt(fs, args...)); + addTrace(std::move(e), hintfmt(std::string(fs), args...)); } - void addTrace(std::optional<ErrPos> e, hintformat hint); + void addTrace(std::shared_ptr<AbstractPos> && e, hintformat hint, bool frame = false); bool hasTrace() const { return !err.traces.empty(); } + + const ErrorInfo & info() { return err; }; }; #define MakeError(newClass, superClass) \ |