|
| 1 | +// |
| 2 | +// This is a derivative work. originally part of the LLVM Project. |
| 3 | +// Licensed under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +// Copyright (c) 2023 Vinnie Falco ([email protected]) |
| 8 | +// |
| 9 | +// Official repository: https://github.com/cppalliance/mrdox |
| 10 | +// |
| 11 | + |
| 12 | +#ifndef MRDOX_API_SUPPORT_ERROR_HPP |
| 13 | +#define MRDOX_API_SUPPORT_ERROR_HPP |
| 14 | + |
| 15 | +#include <mrdox/Platform.hpp> |
| 16 | +#include <llvm/Support/Error.h> |
| 17 | +#include <llvm/Support/raw_ostream.h> |
| 18 | +#include <source_location> |
| 19 | +#include <string> |
| 20 | +#include <string_view> |
| 21 | +#include <utility> |
| 22 | + |
| 23 | +namespace clang { |
| 24 | +namespace mrdox { |
| 25 | + |
| 26 | +//------------------------------------------------ |
| 27 | + |
| 28 | +class Err |
| 29 | +{ |
| 30 | + std::string text_; |
| 31 | + |
| 32 | +public: |
| 33 | + Err() = default; |
| 34 | + |
| 35 | + explicit |
| 36 | + Err(std::string text) |
| 37 | + : text_(std::move(text)) |
| 38 | + { |
| 39 | + } |
| 40 | + |
| 41 | + explicit |
| 42 | + operator bool() const noexcept |
| 43 | + { |
| 44 | + return ! text_.empty(); |
| 45 | + } |
| 46 | + |
| 47 | + std::string_view |
| 48 | + message() const noexcept |
| 49 | + { |
| 50 | + return text_; |
| 51 | + } |
| 52 | +}; |
| 53 | + |
| 54 | +//------------------------------------------------ |
| 55 | +/* |
| 56 | + nice output for variadic error functions |
| 57 | +
|
| 58 | + These are used to convert arguments to |
| 59 | + strings in makeError and Reporter members. |
| 60 | +*/ |
| 61 | + |
| 62 | +template<class T> |
| 63 | +T& nice(T& t) |
| 64 | +{ |
| 65 | + return t; |
| 66 | +} |
| 67 | + |
| 68 | +template<class T> |
| 69 | +T&& nice(T&& t) |
| 70 | +{ |
| 71 | + return std::forward<T>(t); |
| 72 | +} |
| 73 | + |
| 74 | +template<class T> |
| 75 | +auto nice(llvm::Expected<T>&& e) |
| 76 | +{ |
| 77 | + return nice(e.takeError()); |
| 78 | +} |
| 79 | + |
| 80 | +inline auto nice(std::error_code ec) |
| 81 | +{ |
| 82 | + return ec.message(); |
| 83 | +} |
| 84 | + |
| 85 | +inline auto nice(Err e) |
| 86 | +{ |
| 87 | + return e.message(); |
| 88 | +} |
| 89 | + |
| 90 | +template<class T> |
| 91 | +auto nice(llvm::ErrorOr<T>&& e) |
| 92 | +{ |
| 93 | + return nice(e.getError()); |
| 94 | +} |
| 95 | + |
| 96 | +MRDOX_DECL |
| 97 | +llvm::StringRef |
| 98 | +nice(std::source_location loc); |
| 99 | + |
| 100 | +//------------------------------------------------ |
| 101 | + |
| 102 | +/** Return an Error with descriptive information. |
| 103 | +
|
| 104 | + @param reason A phrase describing the cause of the failure. |
| 105 | +
|
| 106 | + @param loc The source location where the failure occurred. |
| 107 | +*/ |
| 108 | +MRDOX_DECL |
| 109 | +[[nodiscard]] |
| 110 | +llvm::Error |
| 111 | +makeErrorString( |
| 112 | + std::string reason, |
| 113 | + std::source_location loc = |
| 114 | + std::source_location::current()); |
| 115 | + |
| 116 | +template<class Arg0, class... Args> |
| 117 | +struct makeError : llvm::Error |
| 118 | +{ |
| 119 | + makeError( |
| 120 | + Arg0&& arg0, |
| 121 | + Args&&... args, |
| 122 | + std::source_location loc = |
| 123 | + std::source_location::current()) |
| 124 | + : llvm::Error( |
| 125 | + [&] |
| 126 | + { |
| 127 | + std::string temp; |
| 128 | + llvm::raw_string_ostream os(temp); |
| 129 | + os << nice(std::forward<Arg0>(arg0)); |
| 130 | + if constexpr(sizeof...(args) > 0) |
| 131 | + (os << ... << nice(std::forward<Args>(args))); |
| 132 | + os << ' ' << nice(loc); |
| 133 | + return makeErrorString(std::move(temp), loc); |
| 134 | + }()) |
| 135 | + { |
| 136 | + } |
| 137 | +}; |
| 138 | + |
| 139 | +template<class Arg0, class... Args> |
| 140 | +makeError(Arg0&&, Args&&...) -> makeError<Arg0, Args...>; |
| 141 | + |
| 142 | +template<class... Args> |
| 143 | +Err makeErr(Args&&... args) |
| 144 | +{ |
| 145 | + auto err = makeError(std::forward<Args>(args)...); |
| 146 | + if(! err) |
| 147 | + return Err(); |
| 148 | + std::string s; |
| 149 | + llvm::raw_string_ostream os(s); |
| 150 | + os << err; |
| 151 | + return Err(s.c_str()); |
| 152 | +} |
| 153 | + |
| 154 | + |
| 155 | +} // mrdox |
| 156 | +} // clang |
| 157 | + |
| 158 | +#endif |
0 commit comments