/
config.h
140 lines (112 loc) · 3.85 KB
/
config.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
/*
* Copyright (c) 2016-2022 Morwenn
* SPDX-License-Identifier: MIT
*/
#ifndef CPPSORT_DETAIL_CONFIG_H_
#define CPPSORT_DETAIL_CONFIG_H_
////////////////////////////////////////////////////////////
// Make <version> available when possible
// config.h is what should be included to get configuration
// information, which includes standard library feature-test
// macros when available
#if __has_include(<version>)
# include <version>
#endif
////////////////////////////////////////////////////////////
// Check for __has_* macros
#ifndef __has_include
# define __has_include(x) 0
#endif
#ifndef __has_cpp_attribute
# define __has_cpp_attribute(x) 0
#endif
////////////////////////////////////////////////////////////
// Check for C++17 features
// Make sure that there is a single variable before C++17,
// default to static in C++14 to avoid ODR issues
#if defined(__cpp_inline_variables)
# define CPPSORT_INLINE_VARIABLE inline
#else
# define CPPSORT_INLINE_VARIABLE static
#endif
////////////////////////////////////////////////////////////
// Check for C++20 features
// There is no feature-test macro for std::identity that can
// be used reliably, so we have to fall back to checking
// compiler and standard versions
#if defined(__cpp_lib_ranges)
# define CPPSORT_STD_IDENTITY_AVAILABLE 1
#elif defined(__GNUC__)
# if __GNUC__ > 9 && __cplusplus > 201703L
# define CPPSORT_STD_IDENTITY_AVAILABLE 1
# else
# define CPPSORT_STD_IDENTITY_AVAILABLE 0
# endif
#else
# define CPPSORT_STD_IDENTITY_AVAILABLE 0
#endif
////////////////////////////////////////////////////////////
// CPPSORT_ASSUME
// Assumptions may help the compiler to remove unnecessary code;
// some parts of the library may be significantly slower if this
// assumption mechanism isn't supported
#if defined(__GNUC__)
# define CPPSORT_ASSUME(expression) do { if (!(expression)) __builtin_unreachable(); } while(0)
#elif defined(__clang__)
# define CPPSORT_ASSUME(expression) __builtin_assume(expression)
#elif defined(_MSC_VER)
# define CPPSORT_ASSUME(expression) __assume(expression)
#else
# define CPPSORT_ASSUME(cond)
#endif
////////////////////////////////////////////////////////////
// CPPSORT_UNREACHABLE
// Mostly useful to silence compiler warnings in the default
// clause of a switch when we know the default can never be
// reached
#if defined(__GNUC__) || defined(__clang__)
# define CPPSORT_UNREACHABLE __builtin_unreachable()
#elif defined(_MSC_VER)
# define CPPSORT_UNREACHABLE __assume(false)
#else
# define CPPSORT_UNREACHABLE
#endif
////////////////////////////////////////////////////////////
// CPPSORT_ASSERT
// We want a slightly finer-grain control over assertions
// than just relying on NDEBUG, so assertions have to be
// explicitly enabled in cpp-sort
#ifndef CPPSORT_ASSERT
# ifdef CPPSORT_ENABLE_ASSERTIONS
# include <cassert>
# define CPPSORT_ASSERT(...) assert((__VA_ARGS__))
# else
# define CPPSORT_ASSERT(...)
# endif
#endif
////////////////////////////////////////////////////////////
// CPPSORT_AUDIT
// Some debug checks might be way too expensive for most
// scenarios, but still of great help when debugging tough
// problems, hence this audit feature
#ifndef CPPSORT_AUDIT
# ifdef CPPSORT_ENABLE_AUDITS
# include <cassert>
# define CPPSORT_AUDIT(...) assert((__VA_ARGS__))
# else
# define CPPSORT_AUDIT(...)
# endif
#endif
////////////////////////////////////////////////////////////
// CPPSORT_DEPRECATED
// [[deprecated]] is available since C++14, hence why this
// macro is not in attributes.h: we hide it behind a macro
// to make sure that it can be silenced
#ifndef CPPSORT_DEPRECATED
# ifndef CPPSORT_DISABLE_DEPRECATION_WARNINGS
# define CPPSORT_DEPRECATED(message) [[deprecated(message)]]
# else
# define CPPSORT_DEPRECATED(message)
# endif
#endif
#endif // CPPSORT_DETAIL_CONFIG_H_