/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 | | #include "experimental/assume.hpp" |
21 | | |
22 | | #include <cassert> |
23 | | #include <ostream> |
24 | | #include <string> |
25 | | #include <string_view> |
26 | | |
27 | | #if defined(_MSC_VER) && !defined(__clang__) |
28 | | #define __builtin_unreachable() __assume(0) |
29 | | #endif |
30 | | |
31 | 40.7M | #define assuming(R) CXX23_ASSUME(R) |
32 | | #ifdef NDEBUG |
33 | 0 | #define assumingUnreachable() __builtin_unreachable() |
34 | | #else |
35 | | #define assumingUnreachable() \ |
36 | | (assert(false && "unreachable"), __builtin_unreachable()) |
37 | | #endif |
38 | | |
39 | | namespace WasmEdge { |
40 | | |
41 | | #if defined(__GNUC__) || defined(__clang__) |
42 | 1.31G | #define likely(X) (__builtin_expect(!!(X), 1)) |
43 | 394M | #define unlikely(X) (__builtin_expect(!!(X), 0)) |
44 | | #else |
45 | | #define likely(X) (!!(X)) |
46 | | #define unlikely(X) (!!(X)) |
47 | | #endif |
48 | | |
49 | | /// Type aliasing for Expected<T, ErrCode>. |
50 | | template <typename T> using Expect = Expected<T, ErrCode>; |
51 | | |
52 | | /// Helper function for Unexpected<ErrCode>. |
53 | 19.5k | constexpr auto Unexpect(const ErrCode &Val) { return Unexpected<ErrCode>(Val); } |
54 | 39.7k | template <typename... ArgsT> constexpr auto Unexpect(ArgsT... Args) { |
55 | 39.7k | return Unexpected<ErrCode>(ErrCode(Args...)); |
56 | 39.7k | } auto WasmEdge::Unexpect<WasmEdge::ErrCode::Value>(WasmEdge::ErrCode::Value) Line | Count | Source | 54 | 39.7k | template <typename... ArgsT> constexpr auto Unexpect(ArgsT... Args) { | 55 | 39.7k | return Unexpected<ErrCode>(ErrCode(Args...)); | 56 | 39.7k | } |
Unexecuted instantiation: auto WasmEdge::Unexpect<WasmEdge::ErrCategory, unsigned int>(WasmEdge::ErrCategory, unsigned int) |
57 | 68 | template <typename T> constexpr auto Unexpect(const Expect<T> &Val) { |
58 | 68 | return Unexpected<ErrCode>(Val.error()); |
59 | 68 | } auto WasmEdge::Unexpect<void>(cxx20::expected<void, WasmEdge::ErrCode> const&) Line | Count | Source | 57 | 68 | template <typename T> constexpr auto Unexpect(const Expect<T> &Val) { | 58 | 68 | return Unexpected<ErrCode>(Val.error()); | 59 | 68 | } |
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&) |
60 | | |
61 | | } // namespace WasmEdge |
62 | | |
63 | | template <> |
64 | | struct fmt::formatter<WasmEdge::ErrCode> : fmt::formatter<std::string_view> { |
65 | | template <typename FmtCtx> |
66 | | auto format(const WasmEdge::ErrCode &Code, |
67 | 21.5k | FmtCtx &Ctx) WASMEDGE_FMT_CONST noexcept -> decltype(Ctx.out()) { |
68 | 21.5k | using namespace std::literals; |
69 | 21.5k | std::string Output = |
70 | 21.5k | fmt::format("{} failed: {}, Code: 0x{:03x}"sv, Code.getErrCodePhase(), |
71 | 21.5k | WasmEdge::ErrCodeStr[Code.getEnum()], Code.getCode()); |
72 | 21.5k | return formatter<std::string_view>::format(Output, Ctx); |
73 | 21.5k | } |
74 | | }; |
75 | | |
76 | | template <> |
77 | | struct fmt::formatter<WasmEdge::ErrCode::Value> |
78 | | : fmt::formatter<WasmEdge::ErrCode> { |
79 | | template <typename FmtCtx> |
80 | | auto format(const WasmEdge::ErrCode::Value &Value, |
81 | 7.29k | FmtCtx &Ctx) WASMEDGE_FMT_CONST noexcept -> decltype(Ctx.out()) { |
82 | 7.29k | return formatter<WasmEdge::ErrCode>::format(WasmEdge::ErrCode(Value), Ctx); |
83 | 7.29k | } |
84 | | }; |