Seregon/ShadPKG

A tool for deriving PKG packet encryption keys for ps4 written in c++

C++/47.3 KB/No license
common/assert.h
ShadPKG / common / assert.h
1// SPDX-FileCopyrightText: 2013 Dolphin Emulator Project
2// SPDX-FileCopyrightText: 2014 Citra Emulator Project
3// SPDX-License-Identifier: GPL-2.0-or-later
4 
5#pragma once
6 
7#include "common/logging/log.h"
8 
9// Sometimes we want to try to continue even after hitting an assert.
10// However touching this file yields a global recompilation as this header is included almost
11// everywhere. So let's just move the handling of the failed assert to a single cpp file.
12 
13void assert_fail_impl();
14[[noreturn]] void unreachable_impl();
15 
16#ifdef _MSC_VER
17#define SHAD_NO_INLINE __declspec(noinline)
18#else
19#define SHAD_NO_INLINE __attribute__((noinline))
20#endif
21 
22#define ASSERT(_a_) \
23 ([&]() SHAD_NO_INLINE { \
24 if (!(_a_)) [[unlikely]] { \
25 LOG_CRITICAL(Debug, "Assertion Failed!"); \
26 assert_fail_impl(); \
27 } \
28 }())
29 
30#define ASSERT_MSG(_a_, ...) \
31 ([&]() SHAD_NO_INLINE { \
32 if (!(_a_)) [[unlikely]] { \
33 LOG_CRITICAL(Debug, "Assertion Failed!\n" __VA_ARGS__); \
34 assert_fail_impl(); \
35 } \
36 }())
37 
38#define UNREACHABLE() \
39 do { \
40 LOG_CRITICAL(Debug, "Unreachable code!"); \
41 unreachable_impl(); \
42 } while (0)
43 
44#define UNREACHABLE_MSG(...) \
45 do { \
46 LOG_CRITICAL(Debug, "Unreachable code!\n" __VA_ARGS__); \
47 unreachable_impl(); \
48 } while (0)
49 
50#ifdef _DEBUG
51#define DEBUG_ASSERT(_a_) ASSERT(_a_)
52#define DEBUG_ASSERT_MSG(_a_, ...) ASSERT_MSG(_a_, __VA_ARGS__)
53#else // not debug
54#define DEBUG_ASSERT(_a_) \
55 do { \
56 } while (0)
57#define DEBUG_ASSERT_MSG(_a_, _desc_, ...) \
58 do { \
59 } while (0)
60#endif
61 
62#define UNIMPLEMENTED() ASSERT_MSG(false, "Unimplemented code!")
63#define UNIMPLEMENTED_MSG(...) ASSERT_MSG(false, __VA_ARGS__)
64 
65#define UNIMPLEMENTED_IF(cond) ASSERT_MSG(!(cond), "Unimplemented code!")
66#define UNIMPLEMENTED_IF_MSG(cond, ...) ASSERT_MSG(!(cond), __VA_ARGS__)
67 
68// If the assert is ignored, execute _b_
69#define ASSERT_OR_EXECUTE(_a_, _b_) \
70 do { \
71 ASSERT(_a_); \
72 if (!(_a_)) [[unlikely]] { \
73 _b_ \
74 } \
75 } while (0)
76 
77// If the assert is ignored, execute _b_
78#define ASSERT_OR_EXECUTE_MSG(_a_, _b_, ...) \
79 do { \
80 ASSERT_MSG(_a_, __VA_ARGS__); \
81 if (!(_a_)) [[unlikely]] { \
82 _b_ \
83 } \
84 } while (0)
85