Skip to content

Commit e3575fa

Browse files
A new set of macros for concise diagnostic controls
1 parent 155f528 commit e3575fa

File tree

7 files changed

+149
-32
lines changed

7 files changed

+149
-32
lines changed

src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/config/postlude.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,25 @@
7575
static_assert(false, "BSONCXX_ENUM must be undef'ed");
7676
#endif
7777
#pragma pop_macro("BSONCXX_ENUM")
78+
79+
#pragma pop_macro("bsoncxx_push_warnings")
80+
#pragma pop_macro("bsoncxx_pop_warnings")
81+
#pragma pop_macro("bsoncxx_disable_warning")
82+
83+
#pragma pop_macro("BSONCXX_DISABLE_WARNING_IMPL_MSVC")
84+
#pragma pop_macro("BSONCXX_DISABLE_WARNING_IMPL_GCC")
85+
#pragma pop_macro("BSONCXX_DISABLE_WARNING_IMPL_GNU")
86+
#pragma pop_macro("BSONCXX_DISABLE_WARNING_IMPL_Clang")
87+
88+
#pragma pop_macro("bsoncxx_concat")
89+
#pragma pop_macro("bsoncxx_concat_impl")
90+
91+
#pragma pop_macro("bsoncxx_pragma")
92+
#pragma pop_macro("bsoncxx_stringify_impl")
93+
#pragma pop_macro("bsoncxx_stringify")
94+
#pragma pop_macro("bsoncxx_force_semicolon")
95+
96+
#pragma pop_macro("bsoncxx_if_msvc")
97+
#pragma pop_macro("bsoncxx_if_gcc")
98+
#pragma pop_macro("bsoncxx_if_clang")
99+
#pragma pop_macro("bsoncxx_if_gnu_like")

src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/config/prelude.hpp

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,102 @@
106106
noexcept(noexcept(__VA_ARGS__))->decltype(__VA_ARGS__) { \
107107
return __VA_ARGS__; \
108108
} \
109-
static_assert(true, "")
109+
bsoncxx_force_semicolon
110+
111+
// clang-format off
112+
113+
#pragma push_macro("bsoncxx_if_msvc")
114+
#define bsoncxx_if_msvc(...)
115+
#pragma push_macro("bsoncxx_if_gcc")
116+
#define bsoncxx_if_gcc(...)
117+
#pragma push_macro("bsoncxx_if_clang")
118+
#define bsoncxx_if_clang(...)
119+
#pragma push_macro("bsoncxx_if_gnu_like")
120+
#define bsoncxx_if_gnu_like(...) \
121+
bsoncxx_if_gcc(__VA_ARGS__) \
122+
bsoncxx_if_clang(__VA_ARGS__)
123+
124+
#ifdef __GNUC__
125+
#ifdef __clang__
126+
#undef bsoncxx_if_clang
127+
#define bsoncxx_if_clang(...) __VA_ARGS__
128+
#else
129+
#undef bsoncxx_if_gcc
130+
#define bsoncxx_if_gcc(...) __VA_ARGS__
131+
#endif
132+
#elif defined(_MSC_VER)
133+
#undef bsoncxx_if_msvc
134+
#undef bsoncxx_if_msvc(...) __VA_ARGS__
135+
#endif
136+
137+
#pragma push_macro("bsoncxx_stringify")
138+
#pragma push_macro("bsoncxx_stringify_impl")
139+
#define bsoncxx_stringify(...) bsoncxx_stringify_impl(__VA_ARGS__)
140+
#define bsoncxx_stringify_impl(...) #__VA_ARGS__
141+
142+
#pragma push_macro("bsoncxx_pragma")
143+
#define bsoncxx_pragma(...) _Pragma(bsoncxx_stringify(__VA_ARGS__))
144+
145+
#pragma push_macro("bsoncxx_force_semicolon")
146+
/**
147+
* @brief Use in a declaration position to force the appearence of a semicolon as the next token
148+
*/
149+
#define bsoncxx_force_semicolon static_assert(true, "")
150+
151+
#pragma push_macro("bsoncxx_concat")
152+
#pragma push_macro("bsoncxx_concat_impl")
153+
#define bsoncxx_concat(A, ...) bsoncxx_concat_impl(A, __VA_ARGS__)
154+
#define bsoncxx_concat_impl(A, ...) A##__VA_ARGS__
155+
156+
#pragma push_macro("bsoncxx_disable_warning")
157+
/**
158+
* @brief Disable a warning for a particular compiler.
159+
*
160+
* The argument should be of the form:
161+
*
162+
* - Clang(<flag-string>)
163+
* - GCC(<flag-string>)
164+
* - GNU(<flag-string>)
165+
* - MSVC(<id-integer>)
166+
*/
167+
#define bsoncxx_disable_warning(Spec) bsoncxx_concat(BSONCXX_DISABLE_WARNING_IMPL_, Spec)
168+
169+
#pragma push_macro("bsoncxx_push_warnings")
170+
/**
171+
* @brief Push the current compiler diagnostics settings state
172+
*/
173+
#define bsoncxx_push_warnings() \
174+
bsoncxx_if_gnu_like(bsoncxx_pragma(GCC diagnostic push);) \
175+
bsoncxx_if_msvc(bsoncxx_pragma(warning(push));) \
176+
bsoncxx_force_semicolon
177+
178+
#pragma push_macro("bsoncxx_pop_warnings")
179+
/**
180+
* @brief Restore prior compiler diagnostics settings from before the most
181+
* recent bsoncxx_push_warnings()
182+
*/
183+
#define bsoncxx_pop_warnings() \
184+
bsoncxx_if_gnu_like(bsoncxx_pragma(GCC diagnostic pop);) \
185+
bsoncxx_if_msvc(bsoncxx_pragma(warning(pop));) \
186+
bsoncxx_force_semicolon
187+
188+
#pragma push_macro("BSONCXX_DISABLE_WARNING_IMPL_GCC")
189+
#define BSONCXX_DISABLE_WARNING_IMPL_GCC(...) \
190+
bsoncxx_if_gcc(bsoncxx_pragma(GCC diagnostic ignored __VA_ARGS__);) \
191+
bsoncxx_force_semicolon
192+
193+
#pragma push_macro("BSONCXX_DISABLE_WARNING_IMPL_Clang")
194+
#define BSONCXX_DISABLE_WARNING_IMPL_Clang(...) \
195+
bsoncxx_if_clang(bsoncxx_pragma(GCC diagnostic ignored __VA_ARGS__);) \
196+
bsoncxx_force_semicolon
197+
198+
#pragma push_macro("BSONCXX_DISABLE_WARNING_IMPL_GNU")
199+
#define BSONCXX_DISABLE_WARNING_IMPL_GNU(...) \
200+
BSONCXX_DISABLE_WARNING_IMPL_GCC(__VA_ARGS__); \
201+
BSONCXX_DISABLE_WARNING_IMPL_Clang(__VA_ARGS__)
202+
203+
#pragma push_macro("BSONCXX_DISABLE_WARNING_IMPL_MSVC")
204+
#define BSONCXX_DISABLE_WARNING_IMPL_MSVC(...) \
205+
bsoncxx_if_msvc(warning(disable : __VA_ARGS__))
206+
207+
// clang-format on

src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/document/value.hpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,17 +232,13 @@ class BSONCXX_API value {
232232
std::size_t _length{0};
233233
};
234234

235-
#if !defined(__clang__) && defined(__GNUC__) && (__cplusplus >= 201709L)
236-
// Silence false positive with g++ 10.2.1 on Debian 11.
237-
#pragma GCC diagnostic push
238-
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
239-
#endif
240235
BSONCXX_INLINE document::view value::view() const noexcept {
236+
// Silence false positive with g++ 10.2.1 on Debian 11.
237+
bsoncxx_push_warnings();
238+
bsoncxx_disable_warning(GCC("-Wmaybe-uninitialized"));
241239
return document::view{static_cast<uint8_t*>(_data.get()), _length};
240+
bsoncxx_pop_warnings();
242241
}
243-
#if !defined(__clang__) && defined(__GNUC__) && (__cplusplus >= 201709L)
244-
#pragma GCC diagnostic pop
245-
#endif
246242

247243
BSONCXX_INLINE value::operator document::view() const noexcept {
248244
return view();

src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/stdx/operators.hpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,7 @@ class strong_ordering {
9494
#undef DEFOP
9595
};
9696

97-
#ifdef __GNUC__
98-
#define psuedo_inline [[gnu::weak]]
99-
#elif defined _MSC_VER
100-
#define psuedo_inline __declspec(selectany)
101-
#else
102-
#define pseudo_inline
103-
#endif
97+
#define psuedo_inline bsoncxx_if_gnu_like([[gnu::weak]]) bsoncxx_if_msvc(__declspec(selectany))
10498

10599
psuedo_inline const strong_ordering strong_ordering::less =
106100
strong_ordering(strong_ordering::_construct{}, -1);

src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/types.hpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,8 @@
2929
#pragma push_macro("BSONCXX_ENUM")
3030
#undef BSONCXX_ENUM
3131

32-
#if defined(__clang__)
33-
#pragma clang diagnostic push
34-
#pragma clang diagnostic ignored "-Wfloat-equal"
35-
#elif defined(__GNUC__)
36-
#pragma GCC diagnostic push
37-
#pragma GCC diagnostic ignored "-Wfloat-equal"
38-
#endif
32+
bsoncxx_push_warnings();
33+
bsoncxx_disable_warning(GNU("-Wfloat-equal"));
3934

4035
namespace bsoncxx {
4136
inline namespace v_noabi {
@@ -676,11 +671,7 @@ BSONCXX_INLINE bool operator==(const b_maxkey&, const b_maxkey&) {
676671
} // namespace v_noabi
677672
} // namespace bsoncxx
678673

679-
#if defined(__clang__)
680-
#pragma clang diagnostic pop
681-
#elif defined(__GNUC__)
682-
#pragma GCC diagnostic pop
683-
#endif
674+
bsoncxx_pop_warnings();
684675

685676
#ifdef BSONCXX_ENUM
686677
static_assert(false, "BSONCXX_ENUM must be undef'ed");

src/bsoncxx/test/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@ if(ENABLE_MACRO_GUARD_TESTS)
9494
DEFINE_NO_DEPRECATED
9595
BSONCXX_NO_DEPRECATED
9696
BSONCXX_UNREACHABLE # prelude.hpp
97+
BSONCXX_DISABLE_WARNING_IMPL_GCC
98+
BSONCXX_DISABLE_WARNING_IMPL_GNU
99+
BSONCXX_DISABLE_WARNING_IMPL_MSVC
100+
BSONCXX_DISABLE_WARNING_IMPL_Clang
101+
bsoncxx_push_warnings
102+
bsoncxx_pop_warnings
103+
bsoncxx_disable_warning
104+
bsoncxx_concat
105+
bsoncxx_concat_impl
106+
bsoncxx_force_semicolon
107+
bsoncxx_pragma
108+
bsoncxx_stringify
109+
bsoncxx_stringify_impl
110+
bsoncxx_if_msvc
111+
bsoncxx_if_gcc
112+
bsoncxx_if_clang
113+
bsoncxx_if_gnu
97114
INCLUDE_PATTERNS
98115
"include/*.hpp" # Public headers.
99116
"lib/*.hh" # Private headers.

src/bsoncxx/test/type_traits.test.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
#include <bsoncxx/stdx/type_traits.hpp>
55
#include <third_party/catch/include/catch.hpp>
66

7-
#if __GNUC__
7+
#include <bsoncxx/config/prelude.hpp>
8+
89
// We declare variables that are only used for compilation checking
9-
// (applies to Clang as well)
10-
#pragma GCC diagnostic ignored "-Wunused"
11-
#endif
10+
bsoncxx_disable_warning(GNU("-Wunused"));
1211

1312
namespace {
1413
namespace tt = bsoncxx::detail;

0 commit comments

Comments
 (0)