Skip to content

Commit 0456d9a

Browse files
CDRIVER-4158: Fix bson-atomic.h for compatibility with C++ (#869)
Fixes CDRIVER-4158. There was some implicit cross-casting going on in the headers that is not valid C++ code. This also adds a C++ utility target that is only compiled to check that the public headers are valid C++ code.
1 parent 35c0eee commit 0456d9a

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ option (ENABLE_DEBUG_ASSERTIONS "Turn on runtime debug assertions" OFF)
5454

5555
project (mongo-c-driver C)
5656

57+
# Optionally enable C++ to do some C++-specific tests
58+
enable_language (CXX OPTIONAL)
59+
5760
if (NOT CMAKE_BUILD_TYPE)
5861
set (CMAKE_BUILD_TYPE "RelWithDebInfo")
5962
message (

src/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ file (GLOB_RECURSE src_kms_message_DIST RELATIVE "${SOURCE_DIR}" kms-message/*)
2929

3030
set_local_dist (src_DIST_local
3131
CMakeLists.txt
32+
cpp-check.cpp
3233
)
3334

3435
set (src_DIST
@@ -39,3 +40,15 @@ set (src_DIST
3940
${src_kms_message_DIST}
4041
PARENT_SCOPE
4142
)
43+
44+
45+
if (CMAKE_CXX_COMPILER)
46+
# Add a C++ source file that will #include the main C headers. This "library"
47+
# does nothing other than validate that the C headers are valid C++ headers.
48+
add_library (mongoc-cxx-check STATIC cpp-check.cpp)
49+
if (TARGET mongoc_static)
50+
target_link_libraries (mongoc-cxx-check PRIVATE mongoc_static)
51+
else ()
52+
target_link_libraries (mongoc-cxx-check PRIVATE mongoc_shared)
53+
endif ()
54+
endif ()

src/cpp-check.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* The sole purpose of this file is to check that the mongoc headers can be
3+
* included in a C++ file without error.
4+
*/
5+
#include <bson/bson.h>
6+
#include <mongoc/mongoc.h>

src/libbson/src/bson/bson-atomic.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ enum bson_memory_order {
146146
Type volatile const *a, enum bson_memory_order order) \
147147
{ \
148148
/* MSVC doesn't have a load intrinsic, so just add zero */ \
149-
BSON_IF_MSVC ( \
150-
return bson_atomic_##NamePart##_fetch_add ((void *) a, 0, order);) \
149+
BSON_IF_MSVC (return bson_atomic_##NamePart##_fetch_add ( \
150+
(Type volatile *) a, 0, order);) \
151151
/* GNU doesn't want RELEASE order for the fetch operation, so we can't \
152152
* just use DEF_ATOMIC_OP. */ \
153153
BSON_IF_GNU_LIKE (switch (order) { \
@@ -285,9 +285,19 @@ enum bson_memory_order {
285285
#define DECL_ATOMIC_STDINT(Name, VCSuffix) \
286286
DECL_ATOMIC_INTEGRAL (Name, Name##_t, VCSuffix)
287287

288+
#ifdef _MSC_VER
289+
/* MSVC expects precise types for their atomic intrinsics. */
290+
DECL_ATOMIC_INTEGRAL (int8, char, 8);
291+
DECL_ATOMIC_INTEGRAL (int16, short, 16)
292+
DECL_ATOMIC_INTEGRAL (int32, long, )
293+
DECL_ATOMIC_INTEGRAL (int, long, )
294+
#else
295+
/* Other compilers that we support provide generic intrinsics */
288296
DECL_ATOMIC_STDINT (int8, 8)
289297
DECL_ATOMIC_STDINT (int16, 16)
290298
DECL_ATOMIC_STDINT (int32, )
299+
DECL_ATOMIC_INTEGRAL (int, int, )
300+
#endif
291301

292302
extern int64_t
293303
_bson_emul_atomic_int64_fetch_add (int64_t volatile *val,
@@ -314,7 +324,11 @@ bson_thrd_yield (void);
314324

315325
#if (defined(_MSC_VER) && !defined(_M_IX86)) || (defined(__LP64__) && __LP64__)
316326
/* (64-bit intrinsics are only available in x64) */
327+
#ifdef _MSC_VER
328+
DECL_ATOMIC_INTEGRAL (int64, __int64, 64)
329+
#else
317330
DECL_ATOMIC_STDINT (int64, 64)
331+
#endif
318332
#else
319333
static BSON_INLINE int64_t
320334
bson_atomic_int64_fetch (int64_t volatile *val, enum bson_memory_order order)
@@ -367,8 +381,6 @@ bson_atomic_int64_compare_exchange_weak (int64_t volatile *val,
367381
}
368382
#endif
369383

370-
DECL_ATOMIC_INTEGRAL (int, int, )
371-
372384
static BSON_INLINE void *
373385
bson_atomic_ptr_exchange (void *volatile *ptr,
374386
void *new_value,

0 commit comments

Comments
 (0)