/src/WasmEdge/include/common/errcode.h
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: Copyright The WasmEdge Authors |
3 | | |
4 | | //===-- wasmedge/common/errcode.h - Error code definition -----------------===// |
5 | | // |
6 | | // Part of the WasmEdge Project. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | /// |
10 | | /// \file |
11 | | /// This file contains the `Expected` wrappers. |
12 | | /// |
13 | | //===----------------------------------------------------------------------===// |
14 | | #pragma once |
15 | | |
16 | | #include "common/enum_errcode.hpp" |
17 | | #include "common/expected.h" |
18 | | #include "common/fmt.h" |
19 | | #include "common/spdlog.h" |
20 | | |
21 | | #include <cassert> |
22 | | #include <ostream> |
23 | | #include <string> |
24 | | #include <string_view> |
25 | | |
26 | | #if defined(_MSC_VER) && !defined(__clang__) |
27 | | #define __builtin_unreachable() __assume(0) |
28 | | #endif |
29 | | |
30 | | #ifdef NDEBUG |
31 | | #define assuming(R) \ |
32 | 40.0M | (static_cast<bool>(R) ? static_cast<void>(0) : __builtin_unreachable()) |
33 | 0 | #define assumingUnreachable() __builtin_unreachable() |
34 | | #else |
35 | | #define assuming(expr) assert(expr) |
36 | | #define assumingUnreachable() \ |
37 | | (assert(false && "unreachable"), __builtin_unreachable()) |
38 | | #endif |
39 | | |
40 | | namespace WasmEdge { |
41 | | |
42 | | #if defined(__GNUC__) || defined(__clang__) |
43 | 1.01G | #define likely(X) (__builtin_expect(!!(X), 1)) |
44 | 424M | #define unlikely(X) (__builtin_expect(!!(X), 0)) |
45 | | #else |
46 | | #define likely(X) (!!(X)) |
47 | | #define unlikely(X) (!!(X)) |
48 | | #endif |
49 | | |
50 | | /// Type aliasing for Expected<T, ErrCode>. |
51 | | template <typename T> using Expect = Expected<T, ErrCode>; |
52 | | |
53 | | /// Helper function for Unexpected<ErrCode>. |
54 | 18.3k | constexpr auto Unexpect(const ErrCode &Val) { return Unexpected<ErrCode>(Val); } |
55 | 36.7k | template <typename... ArgsT> constexpr auto Unexpect(ArgsT... Args) { |
56 | 36.7k | return Unexpected<ErrCode>(ErrCode(Args...)); |
57 | 36.7k | } auto WasmEdge::Unexpect<WasmEdge::ErrCode::Value>(WasmEdge::ErrCode::Value) Line | Count | Source | 55 | 36.7k | template <typename... ArgsT> constexpr auto Unexpect(ArgsT... Args) { | 56 | 36.7k | return Unexpected<ErrCode>(ErrCode(Args...)); | 57 | 36.7k | } |
Unexecuted instantiation: auto WasmEdge::Unexpect<WasmEdge::ErrCategory, unsigned int>(WasmEdge::ErrCategory, unsigned int) |
58 | 143 | template <typename T> constexpr auto Unexpect(const Expect<T> &Val) { |
59 | 143 | return Unexpected<ErrCode>(Val.error()); |
60 | 143 | } auto WasmEdge::Unexpect<void>(cxx20::expected<void, WasmEdge::ErrCode> const&) Line | Count | Source | 58 | 143 | template <typename T> constexpr auto Unexpect(const Expect<T> &Val) { | 59 | 143 | return Unexpected<ErrCode>(Val.error()); | 60 | 143 | } |
Unexecuted instantiation: auto WasmEdge::Unexpect<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >(cxx20::expected<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >, WasmEdge::ErrCode> const&) |
61 | | |
62 | | } // namespace WasmEdge |
63 | | |
64 | | template <> |
65 | | struct fmt::formatter<WasmEdge::ErrCode> : fmt::formatter<std::string_view> { |
66 | | template <typename FmtCtx> |
67 | | auto format(const WasmEdge::ErrCode &Code, |
68 | 19.8k | FmtCtx &Ctx) WASMEDGE_FMT_CONST noexcept -> decltype(Ctx.out()) { |
69 | 19.8k | using namespace std::literals; |
70 | 19.8k | std::string Output = |
71 | 19.8k | fmt::format("{} failed: {}, Code: 0x{:03x}"sv, Code.getErrCodePhase(), |
72 | 19.8k | WasmEdge::ErrCodeStr[Code.getEnum()], Code.getCode()); |
73 | 19.8k | return formatter<std::string_view>::format(Output, Ctx); |
74 | 19.8k | } |
75 | | }; |
76 | | |
77 | | template <> |
78 | | struct fmt::formatter<WasmEdge::ErrCode::Value> |
79 | | : fmt::formatter<WasmEdge::ErrCode> { |
80 | | template <typename FmtCtx> |
81 | | auto format(const WasmEdge::ErrCode::Value &Value, |
82 | 6.83k | FmtCtx &Ctx) WASMEDGE_FMT_CONST noexcept -> decltype(Ctx.out()) { |
83 | 6.83k | return formatter<WasmEdge::ErrCode>::format(WasmEdge::ErrCode(Value), Ctx); |
84 | 6.83k | } |
85 | | }; |