MongoDB C++ Driver
current
Loading...
Searching...
No Matches
macros.hpp
Go to the documentation of this file.
1
// Copyright 2009-present MongoDB, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
// Traditional include guard is required to support v_noabi include-via-prelude.
16
#if !defined(BSONCXX_V1_DETAIL_MACROS_HPP)
17
#define BSONCXX_V1_DETAIL_MACROS_HPP
18
19
// Convert the given macro argument to a string literal, after macro expansion.
20
#define BSONCXX_PRIVATE_STRINGIFY(...) BSONCXX_PRIVATE_STRINGIFY_IMPL(__VA_ARGS__)
21
#define BSONCXX_PRIVATE_STRINGIFY_IMPL(...) #__VA_ARGS__
22
23
// Token-paste two macro arguments, after macro expansion
24
#define BSONCXX_PRIVATE_CONCAT(A, ...) BSONCXX_PRIVATE_CONCAT_IMPL(A, __VA_ARGS__)
25
#define BSONCXX_PRIVATE_CONCAT_IMPL(A, ...) A##__VA_ARGS__
26
27
// Expands to a _Pragma() preprocessor directive, after macro expansion
28
//
29
// The arguments an arbitrary "token soup", and should not be quoted like a regular
30
// _Pragma. This macro will stringify-them itself.
31
//
32
// Example:
33
//
34
// BSONCXX_PRIVATE_PRAGMA(GCC diagnostic ignore "-Wconversion")
35
//
36
// will become:
37
//
38
// _Pragma("GCC diagnostic ignore \"-Wconversion\"")
39
//
40
#define BSONCXX_PRIVATE_PRAGMA(...) BSONCXX_PRIVATE_PRAGMA_IMPL(__VA_ARGS__)
41
#ifdef _MSC_VER
42
// Old MSVC doesn't recognize C++11 _Pragma(), but it always recognized __pragma
43
#define BSONCXX_PRIVATE_PRAGMA_IMPL(...) __pragma(__VA_ARGS__)
44
#else
45
#define BSONCXX_PRIVATE_PRAGMA_IMPL(...) _Pragma(BSONCXX_PRIVATE_STRINGIFY(__VA_ARGS__))
46
#endif
47
48
// Use in a declaration position to force the appearence of a semicolon
49
// as the next token. Use this for statement-like or declaration-like macros to
50
// enforce that their call sites are followed by a semicolon
51
#define BSONCXX_PRIVATE_FORCE_SEMICOLON static_assert(true, "")
52
53
// Add a trailing noexcept, decltype-return, and return-body to a
54
// function definition. (Not compatible with lambda expressions.)
55
//
56
// Example:
57
//
58
// template <typename T>
59
// auto foo(T x, T y) BSONCXX_PRIVATE_RETURNS(x + y);
60
//
61
// Becomes:
62
//
63
// template <typename T>
64
// auto foo(T x, T y) noexcept(noexcept(x + y))
65
// -> decltype(x + y)
66
// { return x + y };
67
//
68
#define BSONCXX_PRIVATE_RETURNS(...) \
69
noexcept(noexcept(__VA_ARGS__))->decltype(__VA_ARGS__) { \
70
return __VA_ARGS__; \
71
} \
72
BSONCXX_PRIVATE_FORCE_SEMICOLON
73
74
// @macro mongocxx_cxx14_constexpr
75
// Expands to `constexpr` if compiling as c++14 or greater, otherwise
76
// expands to `inline`.
77
//
78
// Use this on functions that can only be constexpr in C++14 or newer, including
79
// non-const member functions.
80
#if __cplusplus >= 201402L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201402L && _MSC_VER > 1910)
81
#define BSONCXX_PRIVATE_CONSTEXPR_CXX14 constexpr
82
#else
83
#define BSONCXX_PRIVATE_CONSTEXPR_CXX14 inline
84
#endif
85
86
#define BSONCXX_PRIVATE_MAX_ALIGN_T std::max_align_t
87
88
#define BSONCXX_PRIVATE_IF_MSVC(...)
89
#define BSONCXX_PRIVATE_IF_GCC(...)
90
#define BSONCXX_PRIVATE_IF_CLANG(...)
91
#define BSONCXX_PRIVATE_IF_GNU_LIKE(...) \
92
BSONCXX_PRIVATE_IF_GCC(__VA_ARGS__) \
93
BSONCXX_PRIVATE_IF_CLANG(__VA_ARGS__)
94
95
// clang-format off
96
#ifdef __GNUC__
97
#ifdef __clang__
98
#undef BSONCXX_PRIVATE_IF_CLANG
99
#define BSONCXX_PRIVATE_IF_CLANG(...) __VA_ARGS__
100
#else
101
#undef BSONCXX_PRIVATE_IF_GCC
102
#define BSONCXX_PRIVATE_IF_GCC(...) __VA_ARGS__
103
#endif
104
#elif defined(_MSC_VER)
105
#undef BSONCXX_PRIVATE_IF_MSVC
106
#define BSONCXX_PRIVATE_IF_MSVC(...) __VA_ARGS__
107
#endif
108
// clang-format on
109
110
// Use this on variables that can only be inline in C++17 or newer, such as static constexpr data members.
111
//
112
// GCC does not allow __inline__ (even with __extension__) given -std=c++11 and -pedantic-errors (unconditional
113
// compilation error until GCC 12 which added -Wc++17-extensions suppression), but supports [[gnu::weak]] even with
114
// constant-evaluation and constant-folding on all currently-supported GCC versions (GCC 4.8.x and newer).
115
//
116
// Clang: does not allow using [[gnu::weak]] with constexpr, but supports __inline__ as a language extension even with
117
// -std=c++11 and -pedantic-errors via diagnostic suppression on all currently-supported Clang versions
118
// (-Wc++1z-extensions suppression since Clang 3.9 and -Wc++17-extensions suppression since Clang 5.0).
119
//
120
// MSVC: supports inline variables since 19.12 (with /std:c++latest), but behavior is broken for static data members
121
// (multiple definitions) until 19.20 even when __cpp_inline_variables is defined.
122
//
123
// mingw-w64: this is an ancient bug/issue: https://sourceware.org/bugzilla/show_bug.cgi?id=9687. Use the
124
// Windows-specific [[gnu::selectany]] attribute instead, which provides link-once semantics for global variables.
125
#if ( \
126
!defined(_MSC_VER) && \
127
(__cplusplus >= 201703L || (defined(__cpp_inline_variables) && __cpp_inline_variables >= 201606L))) || \
128
(defined(_MSC_VER) && _MSC_VER >= 1920 && defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
129
#define BSONCXX_PRIVATE_INLINE_CXX17 inline
130
#elif defined(__MINGW32__)
131
#define BSONCXX_PRIVATE_INLINE_CXX17 [[gnu::selectany]]
132
#else
133
#define BSONCXX_PRIVATE_INLINE_CXX17 \
134
BSONCXX_PRIVATE_IF_GCC([[gnu::weak]]) \
135
BSONCXX_PRIVATE_IF_CLANG(_Pragma("clang diagnostic push") _Pragma( \
136
"clang diagnostic ignored \"-Wc++17-extensions\"") __inline__ _Pragma("clang diagnostic pop")) \
137
BSONCXX_PRIVATE_IF_MSVC(__declspec(selectany))
138
#endif
139
140
// Disable a warning for a particular compiler.
141
//
142
// The argument should be of the form:
143
//
144
// - Clang(<flag-string-literal>)
145
// - GCC(<flag-string-literal>)
146
// - GNU(<flag-string-literal>)
147
// - MSVC(<id-integer-literal>)
148
//
149
// The "GNU" form applies to both GCC and Clang
150
#define BSONCXX_PRIVATE_WARNINGS_DISABLE(Spec) \
151
BSONCXX_PRIVATE_CONCAT(BSONCXX_PRIVATE_WARNINGS_DISABLE_IMPL_FOR_, Spec) \
152
BSONCXX_PRIVATE_FORCE_SEMICOLON
153
154
// Push the current compiler diagnostics settings state
155
#define BSONCXX_PRIVATE_WARNINGS_PUSH() \
156
BSONCXX_PRIVATE_IF_GNU_LIKE(BSONCXX_PRIVATE_PRAGMA(GCC diagnostic push)) \
157
BSONCXX_PRIVATE_IF_MSVC(BSONCXX_PRIVATE_PRAGMA(warning(push))) \
158
BSONCXX_PRIVATE_FORCE_SEMICOLON
159
160
// Restore prior compiler diagnostics settings from before the most
161
// recent BSONCXX_PRIVATE_WARNINGS_PUSH()
162
#define BSONCXX_PRIVATE_WARNINGS_POP() \
163
BSONCXX_PRIVATE_IF_GNU_LIKE(BSONCXX_PRIVATE_PRAGMA(GCC diagnostic pop)) \
164
BSONCXX_PRIVATE_IF_MSVC(BSONCXX_PRIVATE_PRAGMA(warning(pop))) \
165
BSONCXX_PRIVATE_FORCE_SEMICOLON
166
167
#define BSONCXX_PRIVATE_WARNINGS_DISABLE_IMPL_FOR_GCC(...) \
168
BSONCXX_PRIVATE_IF_GCC(BSONCXX_PRIVATE_PRAGMA(GCC diagnostic ignored __VA_ARGS__))
169
170
#define BSONCXX_PRIVATE_WARNINGS_DISABLE_IMPL_FOR_Clang(...) \
171
BSONCXX_PRIVATE_IF_CLANG(BSONCXX_PRIVATE_PRAGMA(GCC diagnostic ignored __VA_ARGS__))
172
173
#define BSONCXX_PRIVATE_WARNINGS_DISABLE_IMPL_FOR_GNU(...) \
174
BSONCXX_PRIVATE_WARNINGS_DISABLE_IMPL_FOR_GCC(__VA_ARGS__) \
175
BSONCXX_PRIVATE_WARNINGS_DISABLE_IMPL_FOR_Clang(__VA_ARGS__)
176
177
#define BSONCXX_PRIVATE_WARNINGS_DISABLE_IMPL_FOR_MSVC(...) \
178
BSONCXX_PRIVATE_IF_MSVC(BSONCXX_PRIVATE_PRAGMA(warning(disable : __VA_ARGS__)))
179
180
#define BSONCXX_PRIVATE_FWD(...) static_cast<decltype(__VA_ARGS__)&&>(__VA_ARGS__)
181
182
#define BSONCXX_PRIVATE_UNREACHABLE \
183
if (1) { \
184
std::abort(); \
185
} else \
186
((void)0)
187
188
#endif
// BSONCXX_V1_DETAIL_MACROS_HPP
189
bsoncxx
v1
detail
macros.hpp
Generated by
1.15.0