Coverage Report

Created: 2025-07-01 06:18

/src/WasmEdge/include/common/enum_errcode.hpp
Line
Count
Source (jump to first uncovered line)
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: 2019-2024 Second State INC
3
4
//===-- wasmedge/common/enum_errcode.h - Error code C++ enumerations ------===//
5
//
6
// Part of the WasmEdge Project.
7
//
8
//===----------------------------------------------------------------------===//
9
///
10
/// \file
11
/// This file contains the C++ enumerations of WasmEdge error code.
12
///
13
//===----------------------------------------------------------------------===//
14
15
#pragma once
16
17
#include "common/dense_enum_map.h"
18
#include "common/spare_enum_map.h"
19
#include "common/spdlog.h"
20
21
#include <cstdint>
22
#include <string_view>
23
24
namespace WasmEdge {
25
26
/// WasmEdge runtime phasing C++ enumeration class.
27
enum class WasmPhase : uint8_t {
28
#define UseWasmPhase
29
#define Line(NAME, VALUE, STRING) NAME = VALUE,
30
#include "enum.inc"
31
#undef Line
32
#undef UseWasmPhase
33
};
34
35
static inline constexpr auto WasmPhaseStr = []() constexpr {
36
  using namespace std::literals::string_view_literals;
37
  std::pair<WasmPhase, std::string_view> Array[] = {
38
#define UseWasmPhase
39
#define Line(NAME, VALUE, STRING) {WasmPhase::NAME, STRING},
40
#include "enum.inc"
41
#undef Line
42
#undef UseWasmPhase
43
  };
44
  return DenseEnumMap(Array);
45
}();
46
47
/// Error category C++ enumeration class.
48
enum class ErrCategory : uint8_t {
49
#define UseErrCategory
50
#define Line(NAME, VALUE) NAME = VALUE,
51
#include "enum.inc"
52
#undef Line
53
#undef UseErrCategory
54
};
55
56
class ErrCode {
57
public:
58
  /// Error code C++ enumeration class.
59
  enum class Value : uint32_t {
60
#define UseErrCode
61
#define Line(NAME, VALUE, STRING) NAME = VALUE,
62
#include "enum.inc"
63
#undef Line
64
#undef UseErrCode
65
  };
66
67
0
  constexpr ErrCategory getCategory() const noexcept {
68
0
    return static_cast<ErrCategory>(static_cast<uint8_t>(Inner.Num >> 24));
69
0
  }
70
0
  constexpr uint32_t getCode() const noexcept {
71
0
    return Inner.Num & 0x00FFFFFFU;
72
0
  }
73
0
  constexpr ErrCode::Value getEnum() const noexcept {
74
0
    return getCategory() != ErrCategory::WASM
75
0
               ? ErrCode::Value::UserDefError
76
0
               : static_cast<ErrCode::Value>(getCode());
77
0
  }
78
0
  constexpr WasmPhase getErrCodePhase() const noexcept {
79
0
    return getCategory() != ErrCategory::WASM
80
0
               ? WasmPhase::UserDefined
81
0
               : static_cast<WasmPhase>((getCode() >> 8) & 0x0FU);
82
0
  }
83
84
0
  constexpr ErrCode() noexcept : Inner(0) {}
85
210k
  constexpr ErrCode(const ErrCode &E) noexcept : Inner(E.Inner.Num) {}
86
11.0k
  constexpr ErrCode(const ErrCode::Value E) noexcept : Inner(E) {}
87
  constexpr ErrCode(const uint32_t N) noexcept
88
0
      : Inner((static_cast<uint32_t>(ErrCategory::UserLevelError) << 24) +
89
0
              (N & 0x00FFFFFFU)) {}
90
  constexpr ErrCode(const ErrCategory C, const uint32_t N) noexcept
91
0
      : Inner((static_cast<uint32_t>(C) << 24) + (N & 0x00FFFFFFU)) {}
92
93
  friend constexpr bool operator==(const ErrCode &LHS,
94
3.82k
                                   const ErrCode::Value &RHS) noexcept {
95
3.82k
    return LHS.Inner.Code == RHS;
96
3.82k
  }
97
  friend constexpr bool operator==(const ErrCode::Value &LHS,
98
0
                                   const ErrCode &RHS) noexcept {
99
0
    return RHS.Inner.Code == LHS;
100
0
  }
101
  friend constexpr bool operator==(const ErrCode &LHS,
102
0
                                   const ErrCode &RHS) noexcept {
103
0
    return LHS.Inner.Num == RHS.Inner.Num;
104
0
  }
105
  friend constexpr bool operator!=(const ErrCode &LHS,
106
0
                                   const ErrCode::Value &RHS) noexcept {
107
0
    return !(LHS == RHS);
108
0
  }
109
  friend constexpr bool operator!=(const ErrCode::Value &LHS,
110
0
                                   const ErrCode &RHS) noexcept {
111
0
    return !(LHS == RHS);
112
0
  }
113
  friend constexpr bool operator!=(const ErrCode &LHS,
114
0
                                   const ErrCode &RHS) noexcept {
115
0
    return !(LHS == RHS);
116
0
  }
117
  constexpr ErrCode &operator=(const ErrCode &) noexcept = default;
118
7.16k
  constexpr operator uint32_t() const noexcept { return Inner.Num; }
119
120
private:
121
  union InnerT {
122
210k
    constexpr InnerT(uint32_t Num) : Num(Num) {}
123
11.0k
    constexpr InnerT(ErrCode::Value Code) : Code(Code) {}
124
    uint32_t Num;
125
    ErrCode::Value Code;
126
  } Inner;
127
};
128
129
static inline constexpr const auto ErrCodeStr = []() constexpr {
130
  using namespace std::literals::string_view_literals;
131
  std::pair<ErrCode::Value, std::string_view> Array[] = {
132
#define UseErrCode
133
#define Line(NAME, VALUE, STRING) {ErrCode::Value::NAME, STRING},
134
#include "enum.inc"
135
#undef Line
136
#undef UseErrCode
137
  };
138
  return SpareEnumMap(Array);
139
}();
140
141
} // namespace WasmEdge
142
143
template <>
144
struct fmt::formatter<WasmEdge::WasmPhase> : fmt::formatter<std::string_view> {
145
  fmt::format_context::iterator
146
  format(const WasmEdge::WasmPhase &Phase,
147
0
         fmt::format_context &Ctx) const noexcept {
148
0
    return formatter<std::string_view>::format(WasmEdge::WasmPhaseStr[Phase],
149
0
                                               Ctx);
150
0
  }
151
};