|
| 1 | +/// diagnostic.hh - This file declares records that related to nix diagnostic |
| 2 | +/// |
| 3 | +/// Diagnostics are structures with a main message, |
| 4 | +/// and optionally some additional information (body). |
| 5 | +/// |
| 6 | +/// For diagnostics with a body, |
| 7 | +/// they may need a special overrided function to format the message. |
| 8 | +/// |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include <string> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +#include "error.hh" |
| 15 | +#include "nixexpr.hh" |
| 16 | + |
| 17 | +namespace nix { |
| 18 | + |
| 19 | +/** |
| 20 | + * The base class for all diagnostics. |
| 21 | + * concret diagnostic types are defined in Diagnostic*.inc |
| 22 | + */ |
| 23 | +struct Diag |
| 24 | +{ |
| 25 | + /** |
| 26 | + * The location of some diagnostic, currently it is at the beginning of tokens |
| 27 | + */ |
| 28 | + PosIdx loc; |
| 29 | + |
| 30 | + /** |
| 31 | + * Unique identifier for internal use. |
| 32 | + */ |
| 33 | + enum Kind { |
| 34 | +#define DIAG_MERGE(SNAME, CNAME, SEVERITY, MESSAGE) DK_##CNAME, |
| 35 | +#include "diagnostics/merge.inc" |
| 36 | + }; |
| 37 | + |
| 38 | + Diag() = default; |
| 39 | + Diag(PosIdx loc) |
| 40 | + : loc(loc){}; |
| 41 | + |
| 42 | + /** |
| 43 | + * Each diagnostic contains a severity field, |
| 44 | + * should be "Fatal", "Error" or "Warning" |
| 45 | + * this will affect the eval process. |
| 46 | + * |
| 47 | + * "Fatal" -- non-recoverable while parsing. |
| 48 | + * "Error" -- recoverable while parsing, but should not eval |
| 49 | + * "Warning" -- recoverable while parsing, and we can eval the AST |
| 50 | + * "Note" -- some additional information about the error. |
| 51 | + */ |
| 52 | + enum Severity { DS_Fatal, DS_Error, DS_Warning, DS_Note }; |
| 53 | + |
| 54 | + [[nodiscard]] virtual Kind kind() const = 0; |
| 55 | + |
| 56 | + /** |
| 57 | + * \brief short name. |
| 58 | + * There might be a human readable short name that controls the diagnostic |
| 59 | + * For example, one may pass -Wno-dup-formal to suppress duplicated formals. |
| 60 | + * A special case for parsing errors, generated from bison |
| 61 | + * have the sname "bison" |
| 62 | + */ |
| 63 | + [[nodiscard]] virtual std::string_view sname() const = 0; |
| 64 | + |
| 65 | + /** Get severity */ |
| 66 | + [[nodiscard]] virtual Severity severity() const = 0; |
| 67 | + |
| 68 | + /** |
| 69 | + * Format printable diagnostic, with string interpolated |
| 70 | + * e.g. "invalid integer %1%" -> "invalid integer 'bar'" |
| 71 | + */ |
| 72 | + [[nodiscard]] virtual std::string_view format() const = 0; |
| 73 | + |
| 74 | + virtual ~Diag() = default; |
| 75 | + |
| 76 | + static Verbosity getVerb(Severity s) |
| 77 | + { |
| 78 | + switch (s) { |
| 79 | + case DS_Error: |
| 80 | + case DS_Fatal: |
| 81 | + return lvlError; |
| 82 | + case DS_Warning: |
| 83 | + return lvlWarn; |
| 84 | + case DS_Note: |
| 85 | + return lvlNotice; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + [[nodiscard]] ErrorInfo getErrorInfo(const PosTable & positions) const |
| 90 | + { |
| 91 | + return ErrorInfo{.msg = std::string(format()), .errPos = positions[loc]}; |
| 92 | + } |
| 93 | + |
| 94 | + using Notes = std::vector<std::shared_ptr<Diag>>; |
| 95 | + |
| 96 | + [[nodiscard]] virtual Notes getNotes() const |
| 97 | + { |
| 98 | + return {}; |
| 99 | + } |
| 100 | +}; |
| 101 | + |
| 102 | +struct DiagWithNotes : Diag |
| 103 | +{ |
| 104 | + Diag::Notes notes; |
| 105 | + [[nodiscard]] Diag::Notes getNotes() const override |
| 106 | + { |
| 107 | + return notes; |
| 108 | + } |
| 109 | +}; |
| 110 | + |
| 111 | +#define DIAG_SIMPLE(SNAME, CNAME, SEVERITY, MESSAGE) \ |
| 112 | + struct Diag##CNAME : Diag \ |
| 113 | + { \ |
| 114 | + std::string_view format() const override \ |
| 115 | + { \ |
| 116 | + return MESSAGE; \ |
| 117 | + } \ |
| 118 | + std::string_view sname() const override \ |
| 119 | + { \ |
| 120 | + return SNAME; \ |
| 121 | + } \ |
| 122 | + Severity severity() const override \ |
| 123 | + { \ |
| 124 | + return DS_##SEVERITY; \ |
| 125 | + } \ |
| 126 | + Kind kind() const override \ |
| 127 | + { \ |
| 128 | + return DK_##CNAME; \ |
| 129 | + } \ |
| 130 | + Diag##CNAME() = default; \ |
| 131 | + Diag##CNAME(PosIdx pos) \ |
| 132 | + : Diag(pos) \ |
| 133 | + { \ |
| 134 | + } \ |
| 135 | + }; |
| 136 | +#include "diagnostics/kinds.inc" |
| 137 | +#undef DIAG_SIMPLE |
| 138 | + |
| 139 | +#define DIAG_BODY(SNAME, CNAME, SEVERITY, MESSAGE, BODY) struct Diag##CNAME : Diag BODY; |
| 140 | +#include "diagnostics/kinds.inc" |
| 141 | +#undef DIAG_BODY |
| 142 | + |
| 143 | +// Implement trivial functions except ::format |
| 144 | +#define DIAG_BODY(SNAME, CNAME, SEVERITY, MESSAGE, BODY) \ |
| 145 | + inline std::string_view Diag##CNAME::sname() const \ |
| 146 | + { \ |
| 147 | + return SNAME; \ |
| 148 | + } \ |
| 149 | + inline Diag::Severity Diag##CNAME::severity() const \ |
| 150 | + { \ |
| 151 | + return DS_##SEVERITY; \ |
| 152 | + } \ |
| 153 | + inline Diag::Kind Diag##CNAME::kind() const \ |
| 154 | + { \ |
| 155 | + return DK_##CNAME; \ |
| 156 | + } |
| 157 | +#include "diagnostics/kinds.inc" |
| 158 | +#undef DIAG_BODY |
| 159 | + |
| 160 | +inline DiagInvalidInteger::DiagInvalidInteger(PosIdx loc, std::string text) |
| 161 | + : Diag(loc) |
| 162 | + , text(std::move(text)) |
| 163 | +{ |
| 164 | +} |
| 165 | + |
| 166 | +inline DiagInvalidFloat::DiagInvalidFloat(PosIdx loc, std::string text) |
| 167 | + : Diag(loc) |
| 168 | + , text(std::move(text)) |
| 169 | +{ |
| 170 | +} |
| 171 | + |
| 172 | +inline std::string_view DiagInvalidInteger::format() const |
| 173 | +{ |
| 174 | + return text; |
| 175 | +} |
| 176 | + |
| 177 | +inline std::string_view DiagInvalidFloat::format() const |
| 178 | +{ |
| 179 | + return text; |
| 180 | +} |
| 181 | + |
| 182 | +inline std::string_view DiagBisonParse::format() const |
| 183 | +{ |
| 184 | + return err; |
| 185 | +} |
| 186 | + |
| 187 | +struct DiagnosticEngine |
| 188 | +{ |
| 189 | + std::vector<std::unique_ptr<Diag>> errors; |
| 190 | + std::vector<std::unique_ptr<Diag>> warnings; |
| 191 | + |
| 192 | + void add(std::unique_ptr<Diag> D) |
| 193 | + { |
| 194 | + switch (D->severity()) { |
| 195 | + case Diag::DS_Fatal: |
| 196 | + case Diag::DS_Error: { |
| 197 | + errors.emplace_back(std::move(D)); |
| 198 | + break; |
| 199 | + } |
| 200 | + case Diag::DS_Warning: { |
| 201 | + warnings.emplace_back(std::move(D)); |
| 202 | + break; |
| 203 | + } |
| 204 | + case Diag::DS_Note: { |
| 205 | + // todo: unreachble |
| 206 | + assert(0); |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + void checkRaise(const PosTable & positions) const |
| 212 | + { |
| 213 | + if (!errors.empty()) { |
| 214 | + const Diag * back = errors[0].get(); |
| 215 | + throw ParseError(back->getErrorInfo(positions)); |
| 216 | + } |
| 217 | + } |
| 218 | +}; |
| 219 | + |
| 220 | +} // namespace nix |
0 commit comments