|
| 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 <mutex> |
| 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 TextPosition |
| 25 | + { |
| 26 | + // '0' indicates uninitialized; '1' is the first row/column |
| 27 | + int row = 0; |
| 28 | + int column = 0; |
| 29 | + }; |
| 30 | + |
| 31 | + struct DiagnosticLine |
| 32 | + { |
| 33 | + DiagnosticLine(DiagKind kind, LocalizedString&& message) |
| 34 | + : m_kind(kind), m_origin(), m_position(), m_message(std::move(message)) |
| 35 | + { |
| 36 | + } |
| 37 | + |
| 38 | + DiagnosticLine(DiagKind kind, StringView origin, LocalizedString&& message) |
| 39 | + : m_kind(kind), m_origin(origin.to_string()), m_position(), m_message(std::move(message)) |
| 40 | + { |
| 41 | + } |
| 42 | + |
| 43 | + DiagnosticLine(DiagKind kind, StringView origin, TextPosition position, LocalizedString&& message) |
| 44 | + : m_kind(kind), m_origin(origin.to_string()), m_position(position), m_message(std::move(message)) |
| 45 | + { |
| 46 | + } |
| 47 | + |
| 48 | + // Prints this diagnostic to the terminal. |
| 49 | + // Not thread safe: The console DiagnosticContext must apply its own synchronization. |
| 50 | + void print(MessageSink& sink) const; |
| 51 | + // Converts this message into a string |
| 52 | + // Prefer print() if possible because it applies color |
| 53 | + std::string to_string() const; |
| 54 | + void to_string(std::string& target) const; |
| 55 | + |
| 56 | + private: |
| 57 | + DiagKind m_kind; |
| 58 | + Optional<std::string> m_origin; |
| 59 | + TextPosition m_position; |
| 60 | + LocalizedString m_message; |
| 61 | + }; |
| 62 | + |
| 63 | + struct DiagnosticContext |
| 64 | + { |
| 65 | + // Records a diagnostic. Implementations must make simultaneous calls of report() safe from multiple threads |
| 66 | + // and print entire DiagnosticLines as atomic units. Implementations are not required to synchronize with |
| 67 | + // other machinery like msg::print and friends. |
| 68 | + // |
| 69 | + // This serves to make multithreaded code that reports only via this mechanism safe. |
| 70 | + virtual void report(const DiagnosticLine& line) = 0; |
| 71 | + virtual void report(DiagnosticLine&& line) { report(line); } |
| 72 | + |
| 73 | + protected: |
| 74 | + ~DiagnosticContext() = default; |
| 75 | + }; |
| 76 | + |
| 77 | + struct BufferedDiagnosticContext final : DiagnosticContext |
| 78 | + { |
| 79 | + virtual void report(const DiagnosticLine& line) override; |
| 80 | + virtual void report(DiagnosticLine&& line) override; |
| 81 | + |
| 82 | + std::vector<DiagnosticLine> lines; |
| 83 | + |
| 84 | + // Prints all diagnostics to the terminal. |
| 85 | + // Not safe to use in the face of concurrent calls to report() |
| 86 | + void print(MessageSink& sink) const; |
| 87 | + // Converts this message into a string |
| 88 | + // Prefer print() if possible because it applies color |
| 89 | + // Not safe to use in the face of concurrent calls to report() |
| 90 | + std::string to_string() const; |
| 91 | + void to_string(std::string& target) const; |
| 92 | + |
| 93 | + private: |
| 94 | + std::mutex m_mtx; |
| 95 | + }; |
| 96 | + |
| 97 | + // If T Ty is an rvalue Optional<U>, typename UnwrapOptional<Ty>::type is the type necessary to forward U |
| 98 | + // Otherwise, there is no member UnwrapOptional<Ty>::type |
| 99 | + template<class Ty> |
| 100 | + struct UnwrapOptional |
| 101 | + { |
| 102 | + // no member ::type, SFINAEs out when the input type is: |
| 103 | + // * not Optional |
| 104 | + // * not an rvalue |
| 105 | + // * volatile |
| 106 | + }; |
| 107 | + |
| 108 | + template<class Wrapped> |
| 109 | + struct UnwrapOptional<Optional<Wrapped>> |
| 110 | + { |
| 111 | + // prvalue |
| 112 | + using type = Wrapped; |
| 113 | + using fwd = Wrapped&&; |
| 114 | + }; |
| 115 | + |
| 116 | + template<class Wrapped> |
| 117 | + struct UnwrapOptional<const Optional<Wrapped>> |
| 118 | + { |
| 119 | + // const prvalue |
| 120 | + using type = Wrapped; |
| 121 | + using fwd = const Wrapped&&; |
| 122 | + }; |
| 123 | + |
| 124 | + template<class Wrapped> |
| 125 | + struct UnwrapOptional<Optional<Wrapped>&&> |
| 126 | + { |
| 127 | + // xvalue |
| 128 | + using type = Wrapped&&; |
| 129 | + using fwd = Wrapped&&; |
| 130 | + }; |
| 131 | + |
| 132 | + template<class Wrapped> |
| 133 | + struct UnwrapOptional<const Optional<Wrapped>&&> |
| 134 | + { |
| 135 | + // const xvalue |
| 136 | + using type = const Wrapped&&; |
| 137 | + using fwd = Wrapped&&; |
| 138 | + }; |
| 139 | + |
| 140 | + template<class Fn, class... Args> |
| 141 | + auto adapt_context_to_expected(Fn functor, Args&&... args) |
| 142 | + -> ExpectedL<typename UnwrapOptional<std::invoke_result_t<Fn, BufferedDiagnosticContext&, Args...>>::type> |
| 143 | + { |
| 144 | + using Contained = typename UnwrapOptional<std::invoke_result_t<Fn, BufferedDiagnosticContext&, Args...>>::type; |
| 145 | + BufferedDiagnosticContext bdc; |
| 146 | + auto maybe_result = functor(bdc, std::forward<Args>(args)...); |
| 147 | + if (auto result = maybe_result.get()) |
| 148 | + { |
| 149 | + // N.B.: This may be a move |
| 150 | + return ExpectedL<Contained>{ |
| 151 | + static_cast< |
| 152 | + typename UnwrapOptional<std::invoke_result_t<Fn, BufferedDiagnosticContext&, Args...>>::fwd>( |
| 153 | + *result), |
| 154 | + expected_left_tag}; |
| 155 | + } |
| 156 | + |
| 157 | + return ExpectedL<Contained>{LocalizedString::from_raw(bdc.to_string()), expected_right_tag}; |
| 158 | + } |
| 159 | +} |
0 commit comments