Cthulhu
0.2.10
Cthulhu compiler collection
compiler.h
Go to the documentation of this file.
1
// SPDX-License-Identifier: LGPL-3.0-only
2
3
#pragma once
4
5
#include <
ctu_config.h
>
6
7
#include "
core/cxx_compat.h
"
8
11
#if defined(__has_include)
12
# define CT_HAS_INCLUDE(x) __has_include(x)
13
#else
14
# define CT_HAS_INCLUDE(x) 0
15
#endif
16
21
24
#if defined(__has_c_attribute)
25
# define CT_HAS_C_ATTRIBUTE(x) __has_c_attribute(x)
26
#else
27
# define CT_HAS_C_ATTRIBUTE(x) 0
28
#endif
29
32
#define CT_HAS_ATTRIBUTE(x) (CT_HAS_CXX_ATTRIBUTE(x) || CT_HAS_C_ATTRIBUTE(x))
33
36
#if defined(__has_builtin)
37
# define CT_HAS_BUILTIN(x) __has_builtin(x)
38
#else
39
# define CT_HAS_BUILTIN(x) 0
40
#endif
41
42
// always detect clang first because it pretends to be gcc and msvc
43
// note: i hate that clang does this
44
#if defined(__clang__)
45
# define CT_CC_CLANG 1
46
#elif defined(__GNUC__)
47
# define CT_CC_GNU 1
48
#elif defined(_MSC_VER)
49
# define CT_CC_MSVC 1
50
#else
51
# error "unknown compiler"
52
#endif
53
54
#if defined(_MSC_VER)
55
# define CT_PRAGMA(x) __pragma(x)
56
#elif defined(__GNUC__)
57
# define CT_PRAGMA(x) _Pragma(#x)
58
#else
59
# define CT_PRAGMA(x)
60
#endif
61
62
#if defined(__linux__)
63
# define CT_OS_LINUX 1
64
#elif defined(_WIN32)
65
# define CT_OS_WINDOWS 1
66
#elif defined(__APPLE__)
67
# define CT_OS_APPLE 1
68
#elif defined(__EMSCRIPTEN__)
69
# define CT_OS_WASM 1
70
#else
71
# error "unknown platform"
72
#endif
73
74
#if defined(__GNUC__)
75
# define CT_NORETURN_IMPL __attribute__((noreturn)) void
76
#elif defined(_MSC_VER)
77
# define CT_NORETURN_IMPL __declspec(noreturn) void
78
#elif CT_HAS_ATTRIBUTE(noreturn)
79
# define CT_NORETURN_IMPL [[noreturn]] void
80
#else
81
# define CT_NORETURN_IMPL _Noreturn void
82
#endif
83
84
#ifdef CT_OS_WINDOWS
85
# define CT_NATIVE_PATH_SEPARATOR "\\"
86
# define CT_PATH_SEPERATORS "\\/"
87
#else
88
# define CT_NATIVE_PATH_SEPARATOR "/"
89
# define CT_PATH_SEPERATORS "/"
90
#endif
91
94
95
#if __cpp_lib_unreachable >= 202202L
96
# define CT_UNREACHABLE() std::unreachable()
97
#elif CT_HAS_BUILTIN(__builtin_unreachable)
98
# define CT_UNREACHABLE() __builtin_unreachable()
99
#elif defined(_MSC_VER)
100
# define CT_UNREACHABLE() __assume(0)
101
#else
102
# define CT_UNREACHABLE() ((void)0)
103
#endif
104
109
110
#if CT_HAS_CXX_ATTRIBUTE(assume)
111
# define CT_ASSUME(expr) [[assume(expr)]]
112
#elif CT_HAS_BUILTIN(__builtin_assume)
113
# define CT_ASSUME(expr) __builtin_assume(expr)
114
#elif CT_HAS_ATTRIBUTE(__assume__)
115
# define CT_ASSUME(expr) __attribute__((__assume__(expr)))
116
#elif defined(_MSC_VER)
117
# define CT_ASSUME(expr) __assume(expr)
118
#else
119
# define CT_ASSUME(expr) do { if (!(expr)) { CT_UNREACHABLE(); } } while (0)
120
#endif
121
122
// clang-format off
123
#if CT_CPLUSPLUS
124
# define CT_LINKAGE_C extern "C"
125
# define CT_BEGIN_API extern "C"
{
126
# define CT_END_API }
127
#else
128
# define CT_LINKAGE_C
129
# define CT_BEGIN_API
130
# define CT_END_API
131
#endif
132
// clang-format on
133
137
138
#if defined(__PRETTY_FUNCTION__)
139
# define CT_FUNCTION_NAME __PRETTY_FUNCTION__
140
#else
141
# define CT_FUNCTION_NAME __func__
142
#endif
143
144
// byteswapping
145
#if defined(_MSC_VER) && !defined(__clang__)
146
# define CT_BSWAP_U16(x) _byteswap_ushort(x)
147
# define CT_BSWAP_U32(x) _byteswap_ulong(x)
148
# define CT_BSWAP_U64(x) _byteswap_uint64(x)
149
#else
150
# define CT_BSWAP_U16(x) __builtin_bswap16(x)
151
# define CT_BSWAP_U32(x) __builtin_bswap32(x)
152
# define CT_BSWAP_U64(x) __builtin_bswap64(x)
153
#endif
154
155
#if defined(_MSC_VER)
156
# define CT_EXPORT __declspec(dllexport)
157
# define CT_IMPORT __declspec(dllimport)
158
# define CT_LOCAL
159
#elif __GNUC__ >= 4
160
# define CT_EXPORT __attribute__((visibility("default"
)))
161
# define CT_IMPORT
162
# define CT_LOCAL __attribute__((visibility("internal"
)))
163
#else
164
# define CT_EXPORT
165
# define CT_IMPORT
166
# define CT_LOCAL
167
#endif
168
169
#if CT_CPLUSPLUS >= 202002L
170
# define CT_ENUM_FLAGS(X, T) \
171
constexpr X operator|(X lhs, X rhs) { return X((T)rhs | (T)lhs); } \
172
constexpr X operator&(X lhs, X rhs) { return X((T)rhs & (T)lhs); } \
173
constexpr X operator^(X lhs, X rhs) { return X((T)rhs ^ (T)lhs); } \
174
constexpr X operator~(X rhs) { return X(~(T)rhs); } \
175
constexpr X& operator|=(X& lhs, X rhs) { return lhs = lhs | rhs; } \
176
constexpr X& operator&=(X& lhs, X rhs) { return lhs = lhs & rhs; } \
177
constexpr X& operator^=(X& lhs, X rhs) { return lhs = lhs ^ rhs; }
178
#else
179
# define CT_ENUM_FLAGS(X, T)
180
#endif
181
ctu_config.h
cxx_compat.h
common
core
include
core
compiler.h
Generated by
1.9.1