Skip to content

Commit 3bbdd8f

Browse files
[CXX-2625] Bring our very own string_view (plus: Some iterators + ranges backports) (#1062)
* Bring our own std::string_view * Shorthand for trailing return type, noexcept, and single-return-statement * Some additional traits: rank<> * New operators.hpp utilities * We don't need to start_mongod for the simple compile tasks * A new set of macros for concise diagnostic controls * Reorganize macro definitions * Exclude some files from Doxygen coverage * Qualify bsoncxx::detail references
1 parent 605ada5 commit 3bbdd8f

File tree

15 files changed

+1246
-134
lines changed

15 files changed

+1246
-134
lines changed

.mci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,6 @@ tasks:
636636
- name: compile_with_shared_libs
637637
commands:
638638
- func: "setup"
639-
- func: "start_mongod"
640639
- func: "fetch_c_driver_source"
641640
- func: "compile"
642641

Doxyfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,8 @@ RECURSIVE = YES
900900
# Note that relative paths are relative to the directory from which doxygen is
901901
# run.
902902

903-
EXCLUDE =
903+
EXCLUDE = src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/stdx/operators.hpp \
904+
src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/stdx/type_traits.hpp
904905

905906
# The EXCLUDE_SYMLINKS tag can be used to select whether or not files or
906907
# directories that are symbolic links (a Unix file system feature) are excluded

src/bsoncxx/include/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ set_dist_list(src_bsoncxx_include_DIST
6363
bsoncxx/v_noabi/bsoncxx/config/compiler.hpp
6464
bsoncxx/v_noabi/bsoncxx/config/postlude.hpp
6565
bsoncxx/v_noabi/bsoncxx/config/prelude.hpp
66+
bsoncxx/v_noabi/bsoncxx/config/util.hpp
6667
bsoncxx/v_noabi/bsoncxx/decimal128-fwd.hpp
6768
bsoncxx/v_noabi/bsoncxx/decimal128.hpp
6869
bsoncxx/v_noabi/bsoncxx/document/element-fwd.hpp
@@ -83,6 +84,7 @@ set_dist_list(src_bsoncxx_include_DIST
8384
bsoncxx/v_noabi/bsoncxx/oid-fwd.hpp
8485
bsoncxx/v_noabi/bsoncxx/oid.hpp
8586
bsoncxx/v_noabi/bsoncxx/stdx/make_unique.hpp
87+
bsoncxx/v_noabi/bsoncxx/stdx/operators.hpp
8688
bsoncxx/v_noabi/bsoncxx/stdx/optional.hpp
8789
bsoncxx/v_noabi/bsoncxx/stdx/string_view.hpp
8890
bsoncxx/v_noabi/bsoncxx/stdx/type_traits.hpp

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

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,35 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#if defined(_MSC_VER)
15+
// clang-format off
16+
17+
#define BSONCXX_IF_MSVC(...)
18+
#define BSONCXX_IF_GCC(...)
19+
#define BSONCXX_IF_CLANG(...)
20+
#define BSONCXX_IF_GNU_LIKE(...) \
21+
BSONCXX_IF_GCC(__VA_ARGS__) \
22+
BSONCXX_IF_CLANG(__VA_ARGS__)
23+
24+
#ifdef __GNUC__
25+
#ifdef __clang__
26+
#undef BSONCXX_IF_CLANG
27+
#define BSONCXX_IF_CLANG(...) __VA_ARGS__
28+
#else
29+
#undef BSONCXX_IF_GCC
30+
#define BSONCXX_IF_GCC(...) __VA_ARGS__
31+
#endif
32+
#elif defined(_MSC_VER)
33+
#undef BSONCXX_IF_MSVC
34+
#define BSONCXX_IF_MSVC(...) __VA_ARGS__
35+
#endif
36+
37+
// clang-format on
1638

1739
// Disable MSVC warnings that cause a lot of noise related to DLL visibility
1840
// for types that we don't control (like std::unique_ptr).
19-
#pragma warning(push)
20-
#pragma warning(disable : 4251 4275)
41+
BSONCXX_PUSH_WARNINGS();
42+
BSONCXX_DISABLE_WARNING(MSVC(4251));
43+
BSONCXX_DISABLE_WARNING(MSVC(5275));
2144

2245
#define BSONCXX_INLINE inline BSONCXX_PRIVATE
23-
24-
#define BSONCXX_CALL __cdecl
25-
26-
#else
27-
28-
#define BSONCXX_INLINE inline BSONCXX_PRIVATE
29-
30-
#define BSONCXX_CALL
31-
32-
#endif
46+
#define BSONCXX_CALL BSONCXX_IF_MSVC(__cdecl)

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
// compiler.hpp
1616
#undef BSONCXX_INLINE
1717
#pragma pop_macro("BSONCXX_INLINE")
18-
#if defined(_MSC_VER)
19-
#pragma warning(pop)
20-
#endif
18+
BSONCXX_POP_WARNINGS();
2119
#undef BSONCXX_CALL
2220
#pragma pop_macro("BSONCXX_CALL")
2321

@@ -67,8 +65,35 @@
6765
#undef BSONCXX_UNREACHABLE
6866
#pragma pop_macro("BSONCXX_UNREACHABLE")
6967

68+
#pragma pop_macro("bsoncxx_cxx14_constexpr")
69+
#pragma pop_macro("BSONCXX_RETURNS")
70+
7071
// CXX-2769: out-of-place, but remains for backward compatibility.
7172
#ifdef BSONCXX_ENUM
7273
static_assert(false, "BSONCXX_ENUM must be undef'ed");
7374
#endif
7475
#pragma pop_macro("BSONCXX_ENUM")
76+
77+
// util.hpp
78+
#pragma pop_macro("BSONCXX_PUSH_WARNINGS")
79+
#pragma pop_macro("BSONCXX_POP_WARNINGS")
80+
#pragma pop_macro("BSONCXX_DISABLE_WARNING")
81+
82+
#pragma pop_macro("_bsoncxxDisableWarningImpl_for_MSVC")
83+
#pragma pop_macro("_bsoncxxDisableWarningImpl_for_GCC")
84+
#pragma pop_macro("_bsoncxxDisableWarningImpl_for_GNU")
85+
#pragma pop_macro("_bsoncxxDisableWarningImpl_for_Clang")
86+
87+
#pragma pop_macro("BSONCXX_CONCAT")
88+
#pragma pop_macro("BSONCXX_CONCAT_IMPL")
89+
90+
#pragma pop_macro("BSONCXX_PRAGMA")
91+
#pragma pop_macro("_bsoncxxPragma")
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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,53 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// util.hpp
16+
#pragma push_macro("BSONCXX_CONCAT")
17+
#undef BSONCXX_CONCAT
18+
#pragma push_macro("BSONCXX_CONCAT_IMPL")
19+
#undef BSONCXX_CONCAT_IMPL
20+
#pragma push_macro("BSONCXX_STRINGIFY")
21+
#undef BSONCXX_STRINGIFY
22+
#pragma push_macro("BSONCXX_STRINGIFY_IMPL")
23+
#undef BSONCXX_STRINGIFY_IMPL
24+
#pragma push_macro("BSONCXX_PRAGMA")
25+
#undef BSONCXX_PRAGMA
26+
#pragma push_macro("_bsoncxxPragma")
27+
#undef _bsoncxxPragma
28+
#pragma push_macro("BSONCXX_FORCE_SEMICOLON")
29+
#undef BSONCXX_FORCE_SEMICOLON
30+
#pragma push_macro("BSONCXX_RETURNS")
31+
#undef BSONCXX_RETURNS
32+
#pragma push_macro("bsoncxx_cxx14_constexpr")
33+
#undef bsoncxx_cxx14_constexpr
34+
#pragma push_macro("BSONCXX_DISABLE_WARNING")
35+
#undef BSONCXX_DISABLE_WARNING
36+
#pragma push_macro("BSONCXX_PUSH_WARNINGS")
37+
#undef BSONCXX_PUSH_WARNINGS
38+
#pragma push_macro("BSONCXX_POP_WARNINGS")
39+
#undef BSONCXX_POP_WARNINGS
40+
#pragma push_macro("_bsoncxxDisableWarningImpl_for_GCC")
41+
#undef _bsoncxxDisableWarningImpl_for_GCC
42+
#pragma push_macro("_bsoncxxDisableWarningImpl_for_Clang")
43+
#undef _bsoncxxDisableWarningImpl_for_Clang
44+
#pragma push_macro("_bsoncxxDisableWarningImpl_for_MSVC")
45+
#undef _bsoncxxDisableWarningImpl_for_MSVC
46+
#pragma push_macro("_bsoncxxDisableWarningImpl_for_GNU")
47+
#undef _bsoncxxDisableWarningImpl_for_GNU
48+
1549
// compiler.hpp
1650
#pragma push_macro("BSONCXX_INLINE")
1751
#undef BSONCXX_INLINE
1852
#pragma push_macro("BSONCXX_CALL")
1953
#undef BSONCXX_CALL
54+
#pragma push_macro("BSONCXX_IF_MSVC")
55+
#undef BSONCXX_IF_MSVC
56+
#pragma push_macro("BSONCXX_IF_GCC")
57+
#undef BSONCXX_IF_GCC
58+
#pragma push_macro("BSONCXX_IF_CLANG")
59+
#undef BSONCXX_IF_CLANG
60+
#pragma push_macro("BSONCXX_IF_GNU_LIKE")
61+
#undef BSONCXX_IF_GNU_LIKE
2062

2163
// config.hpp (generated by CMake)
2264
#pragma push_macro("BSONCXX_INLINE_NAMESPACE_BEGIN")
@@ -60,6 +102,8 @@
60102
#pragma push_macro("BSONCXX_NO_DEPRECATED")
61103
#undef BSONCXX_NO_DEPRECATED
62104

105+
#include <bsoncxx/config/util.hpp>
106+
//
63107
#include <bsoncxx/config/compiler.hpp>
64108
#include <bsoncxx/config/config.hpp>
65109
#include <bsoncxx/config/export.hpp>
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// clang-format off
2+
/**
3+
* @internal
4+
* @brief Convert the given macro argument to a string literal, after macro expansion
5+
*/
6+
#define BSONCXX_STRINGIFY(...) BSONCXX_STRINGIFY_IMPL(__VA_ARGS__)
7+
#define BSONCXX_STRINGIFY_IMPL(...) #__VA_ARGS__
8+
9+
/**
10+
* @brief Token-paste two macro arguments, after macro expansion
11+
*/
12+
#define BSONCXX_CONCAT(A, ...) BSONCXX_CONCAT_IMPL(A, __VA_ARGS__)
13+
#define BSONCXX_CONCAT_IMPL(A, ...) A##__VA_ARGS__
14+
15+
/**
16+
* @internal
17+
* @brief Expands to a _Pragma() preprocessor directive, after macro expansion
18+
*
19+
* The arguments an arbitrary "token soup", and should not be quoted like a regular
20+
* _Pragma. This macro will stringify-them itself.
21+
*
22+
* Example:
23+
*
24+
* BSONCXX_PRAGMA(GCC diagnostic ignore "-Wconversion")
25+
*
26+
* will become:
27+
*
28+
* _Pragma("GCC diagnostic ignore \"-Wconversion\"")
29+
*
30+
*/
31+
#define BSONCXX_PRAGMA(...) _bsoncxxPragma(__VA_ARGS__)
32+
#ifdef _MSC_VER
33+
// Old MSVC doesn't recognize C++11 _Pragma(), but it always recognized __pragma
34+
#define _bsoncxxPragma(...) __pragma(__VA_ARGS__)
35+
#else
36+
#define _bsoncxxPragma(...) _Pragma(BSONCXX_STRINGIFY(__VA_ARGS__))
37+
#endif
38+
39+
/**
40+
* @internal
41+
* @brief Use in a declaration position to force the appearence of a semicolon
42+
* as the next token. Use this for statement-like or declaration-like macros to
43+
* enforce that their call sites are followed by a semicolon
44+
*/
45+
#define BSONCXX_FORCE_SEMICOLON static_assert(true, "")
46+
47+
/**
48+
* @internal
49+
* @brief Add a trailing noexcept, decltype-return, and return-body to a
50+
* function definition. (Not compatible with lambda expressions.)
51+
*
52+
* Example:
53+
*
54+
* template <typename T>
55+
* auto foo(T x, T y) BSONCXX_RETURNS(x + y);
56+
*
57+
* Becomes:
58+
*
59+
* template <typename T>
60+
* auto foo(T x, T y) noexcept(noexcept(x + y))
61+
* -> decltype(x + y)
62+
* { return x + y };
63+
*
64+
*/
65+
#define BSONCXX_RETURNS(...) \
66+
noexcept(noexcept(__VA_ARGS__))->decltype(__VA_ARGS__) { \
67+
return __VA_ARGS__; \
68+
} \
69+
BSONCXX_FORCE_SEMICOLON
70+
71+
/**
72+
* @internal
73+
* @macro mongocxx_cxx14_constexpr
74+
* @brief Expands to `constexpr` if compiling as c++14 or greater, otherwise
75+
* expands to `inline`.
76+
*
77+
* Use this on functions that can only be constexpr in C++14 or newer, including
78+
* non-const member functions.
79+
*/
80+
#if __cplusplus >= 201402L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201402L && _MSC_VER > 1910)
81+
#define bsoncxx_cxx14_constexpr constexpr
82+
#else
83+
#define bsoncxx_cxx14_constexpr inline
84+
#endif
85+
86+
/**
87+
* @internal
88+
* @brief Disable a warning for a particular compiler.
89+
*
90+
* The argument should be of the form:
91+
*
92+
* - Clang(<flag-string-literal>)
93+
* - GCC(<flag-string-literal>)
94+
* - GNU(<flag-string-literal>)
95+
* - MSVC(<id-integer-literal>)
96+
*
97+
* The "GNU" form applies to both GCC and Clang
98+
*/
99+
#define BSONCXX_DISABLE_WARNING(Spec) \
100+
BSONCXX_CONCAT(_bsoncxxDisableWarningImpl_for_, Spec) \
101+
BSONCXX_FORCE_SEMICOLON
102+
103+
/**
104+
* @internal
105+
* @brief Push the current compiler diagnostics settings state
106+
*/
107+
#define BSONCXX_PUSH_WARNINGS() \
108+
BSONCXX_IF_GNU_LIKE(BSONCXX_PRAGMA(GCC diagnostic push)) \
109+
BSONCXX_IF_MSVC(BSONCXX_PRAGMA(warning(push))) \
110+
BSONCXX_FORCE_SEMICOLON
111+
112+
/**
113+
* @internal
114+
* @brief Restore prior compiler diagnostics settings from before the most
115+
* recent BSONCXX_PUSH_WARNINGS()
116+
*/
117+
#define BSONCXX_POP_WARNINGS() \
118+
BSONCXX_IF_GNU_LIKE(BSONCXX_PRAGMA(GCC diagnostic pop)) \
119+
BSONCXX_IF_MSVC(BSONCXX_PRAGMA(warning(pop))) \
120+
BSONCXX_FORCE_SEMICOLON
121+
122+
#define _bsoncxxDisableWarningImpl_for_GCC(...) \
123+
BSONCXX_IF_GCC(BSONCXX_PRAGMA(GCC diagnostic ignored __VA_ARGS__))
124+
125+
#define _bsoncxxDisableWarningImpl_for_Clang(...) \
126+
BSONCXX_IF_CLANG(BSONCXX_PRAGMA(GCC diagnostic ignored __VA_ARGS__))
127+
128+
#define _bsoncxxDisableWarningImpl_for_GNU(...) \
129+
_bsoncxxDisableWarningImpl_for_GCC(__VA_ARGS__) \
130+
_bsoncxxDisableWarningImpl_for_Clang(__VA_ARGS__)
131+
132+
#define _bsoncxxDisableWarningImpl_for_MSVC(...) \
133+
BSONCXX_IF_MSVC(BSONCXX_PRAGMA(warning(disable : __VA_ARGS__)))

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,17 +235,13 @@ class value {
235235
std::size_t _length{0};
236236
};
237237

238-
#if !defined(__clang__) && defined(__GNUC__) && (__cplusplus >= 201709L)
239-
// Silence false positive with g++ 10.2.1 on Debian 11.
240-
#pragma GCC diagnostic push
241-
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
242-
#endif
243238
BSONCXX_INLINE document::view value::view() const noexcept {
239+
// Silence false positive with g++ 10.2.1 on Debian 11.
240+
BSONCXX_PUSH_WARNINGS();
241+
BSONCXX_DISABLE_WARNING(GCC("-Wmaybe-uninitialized"));
244242
return document::view{static_cast<uint8_t*>(_data.get()), _length};
243+
BSONCXX_POP_WARNINGS();
245244
}
246-
#if !defined(__clang__) && defined(__GNUC__) && (__cplusplus >= 201709L)
247-
#pragma GCC diagnostic pop
248-
#endif
249245

250246
BSONCXX_INLINE value::operator document::view() const noexcept {
251247
return view();

0 commit comments

Comments
 (0)