|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <vcpkg/base/expected.h> |
| 4 | +#include <vcpkg/base/messages.h> |
| 5 | +#include <vcpkg/base/optional.h> |
| 6 | + |
| 7 | +#include <memory> |
| 8 | +#include <string> |
| 9 | +#include <type_traits> |
| 10 | +#include <vector> |
| 11 | + |
| 12 | +namespace vcpkg |
| 13 | +{ |
| 14 | + enum class DiagKind |
| 15 | + { |
| 16 | + None, // foo.h: localized |
| 17 | + Message, // foo.h: message: localized |
| 18 | + Error, // foo.h: error: localized |
| 19 | + Warning, // foo.h: warning: localized |
| 20 | + Note, // foo.h: note: localized |
| 21 | + COUNT |
| 22 | + }; |
| 23 | + |
| 24 | + struct TextRowCol |
| 25 | + { |
| 26 | + // '0' indicates that line and column information is unknown; '1' is the first row/column |
| 27 | + int row = 0; |
| 28 | + int column = 0; |
| 29 | + }; |
| 30 | + |
| 31 | + struct DiagnosticLine |
| 32 | + { |
| 33 | + template<class MessageLike, std::enable_if_t<std::is_convertible_v<MessageLike, LocalizedString>, int> = 0> |
| 34 | + DiagnosticLine(DiagKind kind, MessageLike&& message) |
| 35 | + : m_kind(kind), m_origin(), m_position(), m_message(std::forward<MessageLike>(message)) |
| 36 | + { |
| 37 | + } |
| 38 | + |
| 39 | + template<class MessageLike, std::enable_if_t<std::is_convertible_v<MessageLike, LocalizedString>, int> = 0> |
| 40 | + DiagnosticLine(DiagKind kind, StringView origin, MessageLike&& message) |
| 41 | + : m_kind(kind), m_origin(origin.to_string()), m_position(), m_message(std::forward<MessageLike>(message)) |
| 42 | + { |
| 43 | + if (origin.empty()) |
| 44 | + { |
| 45 | + Checks::unreachable(VCPKG_LINE_INFO, "origin must not be empty"); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + template<class MessageLike, std::enable_if_t<std::is_convertible_v<MessageLike, LocalizedString>, int> = 0> |
| 50 | + DiagnosticLine(DiagKind kind, StringView origin, TextRowCol position, MessageLike&& message) |
| 51 | + : m_kind(kind) |
| 52 | + , m_origin(origin.to_string()) |
| 53 | + , m_position(position) |
| 54 | + , m_message(std::forward<MessageLike>(message)) |
| 55 | + { |
| 56 | + if (origin.empty()) |
| 57 | + { |
| 58 | + Checks::unreachable(VCPKG_LINE_INFO, "origin must not be empty"); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Prints this diagnostic to the supplied sink. |
| 63 | + void print_to(MessageSink& sink) const; |
| 64 | + // Converts this message into a string |
| 65 | + // Prefer print() if possible because it applies color |
| 66 | + std::string to_string() const; |
| 67 | + void to_string(std::string& target) const; |
| 68 | + |
| 69 | + LocalizedString to_json_reader_string(const std::string& path, const LocalizedString& type) const; |
| 70 | + |
| 71 | + DiagKind kind() const noexcept { return m_kind; } |
| 72 | + |
| 73 | + private: |
| 74 | + DiagKind m_kind; |
| 75 | + Optional<std::string> m_origin; |
| 76 | + TextRowCol m_position; |
| 77 | + LocalizedString m_message; |
| 78 | + }; |
| 79 | + |
| 80 | + struct DiagnosticContext |
| 81 | + { |
| 82 | + virtual void report(const DiagnosticLine& line) = 0; |
| 83 | + virtual void report(DiagnosticLine&& line) { report(line); } |
| 84 | + |
| 85 | + void report_error(const LocalizedString& message) { report(DiagnosticLine{DiagKind::Error, message}); } |
| 86 | + void report_error(LocalizedString&& message) { report(DiagnosticLine{DiagKind::Error, std::move(message)}); } |
| 87 | + template<VCPKG_DECL_MSG_TEMPLATE> |
| 88 | + void report_error(VCPKG_DECL_MSG_ARGS) |
| 89 | + { |
| 90 | + LocalizedString message; |
| 91 | + msg::format_to(message, VCPKG_EXPAND_MSG_ARGS); |
| 92 | + this->report_error(std::move(message)); |
| 93 | + } |
| 94 | + |
| 95 | + protected: |
| 96 | + ~DiagnosticContext() = default; |
| 97 | + }; |
| 98 | + |
| 99 | + struct BufferedDiagnosticContext final : DiagnosticContext |
| 100 | + { |
| 101 | + virtual void report(const DiagnosticLine& line) override; |
| 102 | + virtual void report(DiagnosticLine&& line) override; |
| 103 | + |
| 104 | + std::vector<DiagnosticLine> lines; |
| 105 | + |
| 106 | + // Prints all diagnostics to the supplied sink. |
| 107 | + void print_to(MessageSink& sink) const; |
| 108 | + // Converts this message into a string |
| 109 | + // Prefer print() if possible because it applies color |
| 110 | + std::string to_string() const; |
| 111 | + void to_string(std::string& target) const; |
| 112 | + |
| 113 | + bool any_errors() const noexcept; |
| 114 | + }; |
| 115 | + |
| 116 | + extern DiagnosticContext& console_diagnostic_context; |
| 117 | + extern DiagnosticContext& null_diagnostic_context; |
| 118 | + |
| 119 | + // The following overloads are implementing |
| 120 | + // adapt_context_to_expected(Fn functor, Args&&... args) |
| 121 | + // |
| 122 | + // Given: |
| 123 | + // Optional<T> functor(DiagnosticContext&, args...), adapts functor to return ExpectedL<T> |
| 124 | + // Optional<T>& functor(DiagnosticContext&, args...), adapts functor to return ExpectedL<T&> |
| 125 | + // Optional<T>&& functor(DiagnosticContext&, args...), adapts functor to return ExpectedL<T> |
| 126 | + // std::unique_ptr<T> functor(DiagnosticContext&, args...), adapts functor to return ExpectedL<std::unique_ptr<T>> |
| 127 | + |
| 128 | + // If Ty is an Optional<U>, typename AdaptContextUnwrapOptional<Ty>::type is the type necessary to return U, and fwd |
| 129 | + // is the type necessary to forward U. Otherwise, there are no members ::type or ::fwd |
| 130 | + template<class Ty> |
| 131 | + struct AdaptContextUnwrapOptional |
| 132 | + { |
| 133 | + // no member ::type, SFINAEs out when the input type is: |
| 134 | + // * not Optional |
| 135 | + // * volatile |
| 136 | + }; |
| 137 | + |
| 138 | + template<class Wrapped> |
| 139 | + struct AdaptContextUnwrapOptional<Optional<Wrapped>> |
| 140 | + { |
| 141 | + // prvalue, move from the optional into the expected |
| 142 | + using type = Wrapped; |
| 143 | + using fwd = Wrapped&&; |
| 144 | + }; |
| 145 | + |
| 146 | + template<class Wrapped> |
| 147 | + struct AdaptContextUnwrapOptional<const Optional<Wrapped>> |
| 148 | + { |
| 149 | + // const prvalue |
| 150 | + using type = Wrapped; |
| 151 | + using fwd = const Wrapped&&; |
| 152 | + }; |
| 153 | + |
| 154 | + template<class Wrapped> |
| 155 | + struct AdaptContextUnwrapOptional<Optional<Wrapped>&> |
| 156 | + { |
| 157 | + // lvalue, return an expected referencing the Wrapped inside the optional |
| 158 | + using type = Wrapped&; |
| 159 | + using fwd = Wrapped&; |
| 160 | + }; |
| 161 | + |
| 162 | + template<class Wrapped> |
| 163 | + struct AdaptContextUnwrapOptional<const Optional<Wrapped>&> |
| 164 | + { |
| 165 | + // const lvalue |
| 166 | + using type = const Wrapped&; |
| 167 | + using fwd = const Wrapped&; |
| 168 | + }; |
| 169 | + |
| 170 | + template<class Wrapped> |
| 171 | + struct AdaptContextUnwrapOptional<Optional<Wrapped>&&> |
| 172 | + { |
| 173 | + // xvalue, move from the optional into the expected |
| 174 | + using type = Wrapped; |
| 175 | + using fwd = Wrapped&&; |
| 176 | + }; |
| 177 | + |
| 178 | + template<class Wrapped> |
| 179 | + struct AdaptContextUnwrapOptional<const Optional<Wrapped>&&> |
| 180 | + { |
| 181 | + // const xvalue |
| 182 | + using type = Wrapped; |
| 183 | + using fwd = const Wrapped&&; |
| 184 | + }; |
| 185 | + |
| 186 | + // The overload for functors that return Optional<T> |
| 187 | + template<class Fn, class... Args> |
| 188 | + auto adapt_context_to_expected(Fn functor, Args&&... args) |
| 189 | + -> ExpectedL< |
| 190 | + typename AdaptContextUnwrapOptional<std::invoke_result_t<Fn, BufferedDiagnosticContext&, Args...>>::type> |
| 191 | + { |
| 192 | + using Unwrapper = AdaptContextUnwrapOptional<std::invoke_result_t<Fn, BufferedDiagnosticContext&, Args...>>; |
| 193 | + using ReturnType = ExpectedL<typename Unwrapper::type>; |
| 194 | + BufferedDiagnosticContext bdc; |
| 195 | + decltype(auto) maybe_result = functor(bdc, std::forward<Args>(args)...); |
| 196 | + if (auto result = maybe_result.get()) |
| 197 | + { |
| 198 | + // N.B.: This may be a move |
| 199 | + return ReturnType{static_cast<typename Unwrapper::fwd>(*result), expected_left_tag}; |
| 200 | + } |
| 201 | + |
| 202 | + return ReturnType{LocalizedString::from_raw(bdc.to_string()), expected_right_tag}; |
| 203 | + } |
| 204 | + |
| 205 | + // If Ty is a std::unique_ptr<U>, typename AdaptContextDetectUniquePtr<Ty>::type is the type necessary to return |
| 206 | + template<class Ty> |
| 207 | + struct AdaptContextDetectUniquePtr |
| 208 | + { |
| 209 | + // no member ::type, SFINAEs out when the input type is: |
| 210 | + // * not unique_ptr |
| 211 | + // * volatile |
| 212 | + }; |
| 213 | + |
| 214 | + template<class Wrapped, class Deleter> |
| 215 | + struct AdaptContextDetectUniquePtr<std::unique_ptr<Wrapped, Deleter>> |
| 216 | + { |
| 217 | + // prvalue, move into the Expected |
| 218 | + using type = std::unique_ptr<Wrapped, Deleter>; |
| 219 | + }; |
| 220 | + |
| 221 | + template<class Wrapped, class Deleter> |
| 222 | + struct AdaptContextDetectUniquePtr<const std::unique_ptr<Wrapped, Deleter>> |
| 223 | + { |
| 224 | + // const prvalue (not valid, can't be moved from) |
| 225 | + // no members |
| 226 | + }; |
| 227 | + |
| 228 | + template<class Wrapped, class Deleter> |
| 229 | + struct AdaptContextDetectUniquePtr<std::unique_ptr<Wrapped, Deleter>&> |
| 230 | + { |
| 231 | + // lvalue, reference the unique_ptr itself |
| 232 | + using type = std::unique_ptr<Wrapped, Deleter>&; |
| 233 | + }; |
| 234 | + |
| 235 | + template<class Wrapped, class Deleter> |
| 236 | + struct AdaptContextDetectUniquePtr<const std::unique_ptr<Wrapped, Deleter>&> |
| 237 | + { |
| 238 | + // const lvalue |
| 239 | + using type = const std::unique_ptr<Wrapped, Deleter>&; |
| 240 | + }; |
| 241 | + |
| 242 | + template<class Wrapped, class Deleter> |
| 243 | + struct AdaptContextDetectUniquePtr<std::unique_ptr<Wrapped, Deleter>&&> |
| 244 | + { |
| 245 | + // xvalue, move into the Expected |
| 246 | + using type = std::unique_ptr<Wrapped, Deleter>; |
| 247 | + }; |
| 248 | + |
| 249 | + template<class Wrapped, class Deleter> |
| 250 | + struct AdaptContextDetectUniquePtr<const std::unique_ptr<Wrapped, Deleter>&&> |
| 251 | + { |
| 252 | + // const xvalue (not valid, can't be moved from) |
| 253 | + // no members |
| 254 | + }; |
| 255 | + |
| 256 | + // The overload for functors that return std::unique_ptr<T> |
| 257 | + template<class Fn, class... Args> |
| 258 | + auto adapt_context_to_expected(Fn functor, Args&&... args) |
| 259 | + -> ExpectedL< |
| 260 | + typename AdaptContextDetectUniquePtr<std::invoke_result_t<Fn, BufferedDiagnosticContext&, Args...>>::type> |
| 261 | + { |
| 262 | + using ReturnType = ExpectedL< |
| 263 | + typename AdaptContextDetectUniquePtr<std::invoke_result_t<Fn, BufferedDiagnosticContext&, Args...>>::type>; |
| 264 | + BufferedDiagnosticContext bdc; |
| 265 | + decltype(auto) maybe_result = functor(bdc, std::forward<Args>(args)...); |
| 266 | + if (maybe_result) |
| 267 | + { |
| 268 | + return ReturnType{static_cast<decltype(maybe_result)&&>(maybe_result), expected_left_tag}; |
| 269 | + } |
| 270 | + |
| 271 | + return ReturnType{LocalizedString::from_raw(bdc.to_string()), expected_right_tag}; |
| 272 | + } |
| 273 | +} |
0 commit comments