forked from sorbet/sorbet
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommon.h
220 lines (184 loc) · 7.4 KB
/
common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#ifndef SORBET_COMMON_HPP
#define SORBET_COMMON_HPP
#if __cplusplus < 201402L
#define STRINGIZE(x) "C++ = " #x
#define SSTRINGIZE(x) STRINGIZE(x)
#pragma message(SSTRINGIZE(__cplusplus))
static_assert(false, "Need c++14 to compile this codebase");
#endif
#include "absl/algorithm/container.h"
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/container/inlined_vector.h"
#include "pdqsort.h"
#include "spdlog/fmt/fmt.h"
#include "spdlog/spdlog.h"
#include <stdint.h>
#include <string>
#include <string_view>
#include <type_traits>
#if !defined(NDEBUG)
// So you can use `cout` when debugging. Not included in production as it is a
// performance hit.
#include <iostream>
#endif
namespace sorbet {
template <class T, size_t N> using InlinedVector = absl::InlinedVector<T, N>;
template <class K, class V> using UnorderedMap = absl::flat_hash_map<K, V>;
template <class E> using UnorderedSet = absl::flat_hash_set<E>;
// Uncomment to make vectors debuggable
// template <class T, size_t N> using InlinedVector = std::vector<T>;
#if defined(NDEBUG) && !defined(FORCE_DEBUG)
constexpr bool debug_mode = false;
#undef DEBUG_MODE
#else
#define DEBUG_MODE
constexpr bool debug_mode = true;
#endif
#if !defined(EMSCRIPTEN)
constexpr bool emscripten_build = false;
#else
constexpr bool emscripten_build = true;
#endif
#if !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
constexpr bool fuzz_mode = false;
#else
constexpr bool fuzz_mode = true;
#endif
#define _MAYBE_ADD_COMMA(...) , ##__VA_ARGS__
#define ENFORCE(x, ...) \
((::sorbet::debug_mode && !(x)) ? ({ \
::sorbet::Exception::failInFuzzer(); \
if (stopInDebugger()) { \
(void)!(x); \
} \
::sorbet::Exception::enforce_handler(#x, __FILE__, __LINE__ _MAYBE_ADD_COMMA(__VA_ARGS__)); \
}) \
: false)
#define DEBUG_ONLY(X) \
if (debug_mode) { \
X; \
}
constexpr bool skip_check_memory_layout = debug_mode || emscripten_build;
template <typename ToCheck, std::size_t ExpectedSize, std::size_t RealSize = sizeof(ToCheck)> struct check_size {
static_assert(skip_check_memory_layout || ExpectedSize == RealSize, "Size is off!");
};
template <typename ToCheck, std::size_t ExpectedAlign, std::size_t RealAlign = alignof(ToCheck)> struct check_align {
static_assert(skip_check_memory_layout || ExpectedAlign == RealAlign, "Align is off!");
};
#ifdef UNUSED
#elif defined(__GNUC__)
#define UNUSED(x) UNUSED_##x __attribute__((unused))
#elif defined(__LCLINT__)
#define UNUSED(x) /*@unused@*/ x
#else
#define UNUSED(x) x
#endif
#define CheckSize(T, ExpSize, ExpAlign) \
inline void _##T##is##ExpSize##_bytes_long_() { \
sorbet::check_size<T, ExpSize> UNUSED(_##T##is##ExpSize##_bytes_long); \
sorbet::check_align<T, ExpAlign> UNUSED(_##T##is##ExpAlign##_bytes_aligned); \
}
/**
* Shorter aliases for unsigned ints of specified byte widths.
*/
typedef uint8_t u1;
CheckSize(u1, 1, 1);
typedef uint16_t u2;
CheckSize(u2, 2, 2);
typedef uint32_t u4;
CheckSize(u4, 4, 4);
typedef uint64_t u8;
CheckSize(u8, 8, 8);
template <class From, class To> To *fast_cast(From *what) {
constexpr bool isFinal = std::is_final<To>::value;
if (std::is_same<From, To>::value)
return static_cast<To *>(what);
if (what == nullptr) {
return nullptr;
}
if (isFinal) {
From &nonNull = *what;
const std::type_info &ty = typeid(nonNull);
if (ty == typeid(To))
return static_cast<To *>(what);
return nullptr;
}
return dynamic_cast<To *>(what);
};
} // namespace sorbet
std::string demangle(const char *mangled);
namespace fmt {
template <typename It, typename Char, class UnaryOp, typename UnaryOpResult> struct arg_map_join {
It begin;
It end;
basic_string_view<Char> sep;
const UnaryOp &mapper;
arg_map_join(It begin, It end, basic_string_view<Char> sep, const UnaryOp &mapper)
: begin(begin), end(end), sep(sep), mapper(mapper) {}
};
template <typename It, typename Char, class UnaryOp, typename UnaryOpResult>
struct formatter<arg_map_join<It, Char, UnaryOp, UnaryOpResult>, Char> : formatter<UnaryOpResult, Char> {
template <typename FormatContext>
auto format(const arg_map_join<It, Char, UnaryOp, UnaryOpResult> &value, FormatContext &ctx)
-> decltype(ctx.out()) {
typedef formatter<UnaryOpResult, Char> base;
auto it = value.begin;
auto out = ctx.out();
if (it != value.end) {
out = base::format(std::invoke(value.mapper, *it++), ctx);
while (it != value.end) {
out = std::copy(value.sep.begin(), value.sep.end(), out);
ctx.advance_to(out);
out = base::format(std::invoke(value.mapper, *it++), ctx);
}
}
return out;
}
};
template <typename It, class UnaryOp> auto map_join(It begin, It end, std::string_view sep, const UnaryOp &mapper) {
return arg_map_join<It, char, UnaryOp,
typename std::result_of<UnaryOp(typename std::iterator_traits<It>::value_type)>::type>(
begin, end, sep, mapper);
}
template <typename Container, class UnaryOp>
auto map_join(const Container &collection, std::string_view sep, const UnaryOp &mapper) {
return arg_map_join<typename Container::const_iterator, char, UnaryOp,
typename std::result_of<UnaryOp(
typename std::iterator_traits<typename Container::const_iterator>::value_type)>::type>(
collection.begin(), collection.end(), sep, mapper);
}
} // namespace fmt
template <class Container, class Compare> inline void fast_sort(Container &container, Compare &&comp) {
pdqsort(container.begin(), container.end(), std::forward<Compare>(comp));
};
template <class Container> inline void fast_sort(Container &container) {
pdqsort(container.begin(), container.end());
};
/* use fast_sort */
#pragma GCC poison sort c_sort
/* use absl::c_ alternatives */
#pragma GCC poison any_of find_if linear_search min_element max_element iota all_of
// I wish I could add replace and find, but those names are too generic
// accumulate upper_bound are used by <random>
// lower_bound is needed for parser
/* String handling functions. Use C++ alternatives */
#pragma GCC poison strcpy wcscpy stpcpy wcpcpy
#pragma GCC poison strdup
#pragma GCC poison gets puts
#pragma GCC poison strcat wcscat
#pragma GCC poison wcrtomb wctob
#pragma GCC poison sprintf vsprintf vfprintf
#pragma GCC poison asprintf vasprintf
#pragma GCC poison strncpy wcsncpy
#pragma GCC poison strtok wcstok
/* Signal related */
#pragma GCC poison longjmp siglongjmp
#pragma GCC poison setjmp sigsetjmp
/* File API's */
#pragma GCC poison tmpnam tempnam
/* Misc */
#pragma GCC poison cuserid
#pragma GCC poison rexec rexec_af
#include "Exception.h"
#endif