MongoDB C++ Driver 4.1.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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_IF_MSVC(...)
87#define BSONCXX_PRIVATE_IF_GCC(...)
88#define BSONCXX_PRIVATE_IF_CLANG(...)
89#define BSONCXX_PRIVATE_IF_GNU_LIKE(...) \
90 BSONCXX_PRIVATE_IF_GCC(__VA_ARGS__) \
91 BSONCXX_PRIVATE_IF_CLANG(__VA_ARGS__)
92
93// clang-format off
94#ifdef __GNUC__
95 #ifdef __clang__
96 #undef BSONCXX_PRIVATE_IF_CLANG
97 #define BSONCXX_PRIVATE_IF_CLANG(...) __VA_ARGS__
98 #else
99 #undef BSONCXX_PRIVATE_IF_GCC
100 #define BSONCXX_PRIVATE_IF_GCC(...) __VA_ARGS__
101 #endif
102#elif defined(_MSC_VER)
103 #undef BSONCXX_PRIVATE_IF_MSVC
104 #define BSONCXX_PRIVATE_IF_MSVC(...) __VA_ARGS__
105#endif
106// clang-format on
107
108// Disable a warning for a particular compiler.
109//
110// The argument should be of the form:
111//
112// - Clang(<flag-string-literal>)
113// - GCC(<flag-string-literal>)
114// - GNU(<flag-string-literal>)
115// - MSVC(<id-integer-literal>)
116//
117// The "GNU" form applies to both GCC and Clang
118#define BSONCXX_PRIVATE_WARNINGS_DISABLE(Spec) \
119 BSONCXX_PRIVATE_CONCAT(BSONCXX_PRIVATE_WARNINGS_DISABLE_IMPL_FOR_, Spec) \
120 BSONCXX_PRIVATE_FORCE_SEMICOLON
121
122// Push the current compiler diagnostics settings state
123#define BSONCXX_PRIVATE_WARNINGS_PUSH() \
124 BSONCXX_PRIVATE_IF_GNU_LIKE(BSONCXX_PRIVATE_PRAGMA(GCC diagnostic push)) \
125 BSONCXX_PRIVATE_IF_MSVC(BSONCXX_PRIVATE_PRAGMA(warning(push))) \
126 BSONCXX_PRIVATE_FORCE_SEMICOLON
127
128// Restore prior compiler diagnostics settings from before the most
129// recent BSONCXX_PRIVATE_WARNINGS_PUSH()
130#define BSONCXX_PRIVATE_WARNINGS_POP() \
131 BSONCXX_PRIVATE_IF_GNU_LIKE(BSONCXX_PRIVATE_PRAGMA(GCC diagnostic pop)) \
132 BSONCXX_PRIVATE_IF_MSVC(BSONCXX_PRIVATE_PRAGMA(warning(pop))) \
133 BSONCXX_PRIVATE_FORCE_SEMICOLON
134
135#define BSONCXX_PRIVATE_WARNINGS_DISABLE_IMPL_FOR_GCC(...) \
136 BSONCXX_PRIVATE_IF_GCC(BSONCXX_PRIVATE_PRAGMA(GCC diagnostic ignored __VA_ARGS__))
137
138#define BSONCXX_PRIVATE_WARNINGS_DISABLE_IMPL_FOR_Clang(...) \
139 BSONCXX_PRIVATE_IF_CLANG(BSONCXX_PRIVATE_PRAGMA(GCC diagnostic ignored __VA_ARGS__))
140
141#define BSONCXX_PRIVATE_WARNINGS_DISABLE_IMPL_FOR_GNU(...) \
142 BSONCXX_PRIVATE_WARNINGS_DISABLE_IMPL_FOR_GCC(__VA_ARGS__) \
143 BSONCXX_PRIVATE_WARNINGS_DISABLE_IMPL_FOR_Clang(__VA_ARGS__)
144
145#define BSONCXX_PRIVATE_WARNINGS_DISABLE_IMPL_FOR_MSVC(...) \
146 BSONCXX_PRIVATE_IF_MSVC(BSONCXX_PRIVATE_PRAGMA(warning(disable : __VA_ARGS__)))
147
148#define BSONCXX_PRIVATE_FWD(...) static_cast<decltype(__VA_ARGS__)&&>(__VA_ARGS__)
149
150#define BSONCXX_PRIVATE_UNREACHABLE \
151 if (1) { \
152 std::abort(); \
153 } else \
154 ((void)0)
155
156#endif // BSONCXX_V1_DETAIL_MACROS_HPP
157