Skip to content

Commit 2a35499

Browse files
authored
CDRIVER-5876 detect aligned_alloc via CMake instead of feature test macros (#1851)
* Add missing CMAKE_REQUIRED_* updates * Add BSON_HAVE_ALIGNED_ALLOC to bson-config.h * Refactor pragma diagnostic push and pop into MC_PRAGMA_DIAGNOSTIC_(PUSH|POP) * Suppress -Wimplicit-function-declaration warnings for aligned_alloc
1 parent ad6332f commit 2a35499

File tree

6 files changed

+60
-15
lines changed

6 files changed

+60
-15
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ endif ()
448448

449449
if (MSVC)
450450
add_definitions (-D_CRT_SECURE_NO_WARNINGS)
451+
list (APPEND CMAKE_REQUIRED_DEFINITIONS -D_CRT_SECURE_NO_WARNINGS)
451452
endif ()
452453

453454
if (ENABLE_MONGOC)

src/common/src/common-macros-private.h

+41-13
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,29 @@
2727
#define MONGOC_DEBUG_ASSERT(statement) ((void) 0)
2828
#endif
2929

30+
#if defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
31+
#define MC_PRAGMA_DIAGNOSTIC_PUSH _Pragma ("GCC diagnostic push")
32+
#define MC_PRAGMA_DIAGNOSTIC_POP _Pragma ("GCC diagnostic pop")
33+
#elif defined(__clang__)
34+
#define MC_PRAGMA_DIAGNOSTIC_PUSH _Pragma ("clang diagnostic push")
35+
#define MC_PRAGMA_DIAGNOSTIC_POP _Pragma ("clang diagnostic pop")
36+
#elif defined(_MSC_VER)
37+
#define MC_PRAGMA_DIAGNOSTIC_PUSH _Pragma ("warning ( push )")
38+
#define MC_PRAGMA_DIAGNOSTIC_POP _Pragma ("warning ( pop )")
39+
#else
40+
#define MC_PRAGMA_DIAGNOSTIC_PUSH
41+
#define MC_PRAGMA_DIAGNOSTIC_POP
42+
#endif
43+
3044
// `MC_ENABLE_CONVERSION_WARNING_BEGIN` enables -Wconversion to check for potentially unsafe integer conversions.
3145
// The `mcommon_in_range_*` functions can help address these warnings by ensuring a cast is within bounds.
32-
#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) // gcc 4.6 added support for "diagnostic push".
33-
#define MC_ENABLE_CONVERSION_WARNING_BEGIN \
34-
_Pragma ("GCC diagnostic push") _Pragma ("GCC diagnostic warning \"-Wconversion\"")
35-
#define MC_ENABLE_CONVERSION_WARNING_END _Pragma ("GCC diagnostic pop")
46+
#if defined(__GNUC__)
47+
#define MC_ENABLE_CONVERSION_WARNING_BEGIN MC_PRAGMA_DIAGNOSTIC_PUSH _Pragma ("GCC diagnostic warning \"-Wconversion\"")
48+
#define MC_ENABLE_CONVERSION_WARNING_END MC_PRAGMA_DIAGNOSTIC_POP
3649
#elif defined(__clang__)
3750
#define MC_ENABLE_CONVERSION_WARNING_BEGIN \
38-
_Pragma ("clang diagnostic push") _Pragma ("clang diagnostic warning \"-Wconversion\"")
39-
#define MC_ENABLE_CONVERSION_WARNING_END _Pragma ("clang diagnostic pop")
51+
MC_PRAGMA_DIAGNOSTIC_PUSH _Pragma ("clang diagnostic warning \"-Wconversion\"")
52+
#define MC_ENABLE_CONVERSION_WARNING_END MC_PRAGMA_DIAGNOSTIC_POP
4053
#else
4154
#define MC_ENABLE_CONVERSION_WARNING_BEGIN
4255
#define MC_ENABLE_CONVERSION_WARNING_END
@@ -50,22 +63,37 @@
5063
#undef MC_DISABLE_CAST_FUNCTION_TYPE_STRICT_WARNING_BEGIN
5164
#undef MC_DISABLE_CAST_FUNCTION_TYPE_STRICT_WARNING_END
5265
#define MC_DISABLE_CAST_FUNCTION_TYPE_STRICT_WARNING_BEGIN \
53-
_Pragma ("clang diagnostic push") _Pragma ("clang diagnostic ignored \"-Wcast-function-type-strict\"")
54-
#define MC_DISABLE_CAST_FUNCTION_TYPE_STRICT_WARNING_END _Pragma ("clang diagnostic pop")
66+
MC_PRAGMA_DIAGNOSTIC_PUSH _Pragma ("clang diagnostic ignored \"-Wcast-function-type-strict\"")
67+
#define MC_DISABLE_CAST_FUNCTION_TYPE_STRICT_WARNING_END MC_PRAGMA_DIAGNOSTIC_POP
5568
#endif // __has_warning("-Wcast-function-type-strict")
5669
#endif // defined(__clang__)
5770

58-
#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
71+
#if defined(__GNUC__)
5972
#define BEGIN_IGNORE_DEPRECATIONS \
60-
_Pragma ("GCC diagnostic push") _Pragma ("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
61-
#define END_IGNORE_DEPRECATIONS _Pragma ("GCC diagnostic pop")
73+
MC_PRAGMA_DIAGNOSTIC_PUSH _Pragma ("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
74+
#define END_IGNORE_DEPRECATIONS MC_PRAGMA_DIAGNOSTIC_POP
6275
#elif defined(__clang__)
6376
#define BEGIN_IGNORE_DEPRECATIONS \
64-
_Pragma ("clang diagnostic push") _Pragma ("clang diagnostic ignored \"-Wdeprecated-declarations\"")
65-
#define END_IGNORE_DEPRECATIONS _Pragma ("clang diagnostic pop")
77+
MC_PRAGMA_DIAGNOSTIC_PUSH _Pragma ("clang diagnostic ignored \"-Wdeprecated-declarations\"")
78+
#define END_IGNORE_DEPRECATIONS MC_PRAGMA_DIAGNOSTIC_PUSH
6679
#else
6780
#define BEGIN_IGNORE_DEPRECATIONS
6881
#define END_IGNORE_DEPRECATIONS
6982
#endif
7083

84+
// Disable the -Wimplicit warning (including -Wimplicit-int and -Wimplicit-function-declaration).
85+
#if defined(__GNUC__)
86+
#define MC_DISABLE_IMPLICIT_WARNING_BEGIN MC_PRAGMA_DIAGNOSTIC_PUSH _Pragma ("GCC diagnostic ignored \"-Wimplicit\"")
87+
#define MC_DISABLE_IMPLICIT_WARNING_END MC_PRAGMA_DIAGNOSTIC_POP
88+
#elif defined(__clang__)
89+
#define MC_DISABLE_IMPLICIT_WARNING_BEGIN MC_PRAGMA_DIAGNOSTIC_PUSH _Pragma ("clang diagnostic ignored \"-Wimplicit\"")
90+
#define MC_DISABLE_IMPLICIT_WARNING_END MC_PRAGMA_DIAGNOSTIC_POP
91+
#elif defined(_MSC_VER)
92+
#define MC_DISABLE_IMPLICIT_WARNING_BEGIN MC_PRAGMA_DIAGNOSTIC_PUSH _Pragma ("warning (disable : 4013 4431)")
93+
#define MC_DISABLE_IMPLICIT_WARNING_END MC_PRAGMA_DIAGNOSTIC_POP
94+
#else
95+
#define MC_DISABLE_IMPLICIT_WARNING_BEGIN
96+
#define MC_DISABLE_IMPLICIT_WARNING_END
97+
#endif
98+
7199
#endif /* MONGO_C_DRIVER_COMMON_MACROS_PRIVATE_H */

src/libbson/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ check_symbol_exists (clock_gettime time.h BSON_HAVE_CLOCK_GETTIME)
108108
mongo_bool01 (BSON_HAVE_CLOCK_GETTIME BSON_HAVE_CLOCK_GETTIME)
109109
check_symbol_exists (strnlen string.h BSON_HAVE_STRNLEN)
110110
mongo_bool01 (BSON_HAVE_STRNLEN BSON_HAVE_STRNLEN)
111+
check_symbol_exists (aligned_alloc stdlib.h BSON_HAVE_ALIGNED_ALLOC)
112+
mongo_bool01 (BSON_HAVE_ALIGNED_ALLOC BSON_HAVE_ALIGNED_ALLOC)
111113
test_big_endian (BSON_BIG_ENDIAN)
112114

113115
if (WIN32)

src/libbson/src/bson/bson-config.h.in

+9
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,13 @@
122122
# undef BSON_HAVE_STRLCPY
123123
#endif
124124

125+
126+
/*
127+
* Define to 1 if you have aligned_alloc available on your platform.
128+
*/
129+
#define BSON_HAVE_ALIGNED_ALLOC @BSON_HAVE_ALIGNED_ALLOC@
130+
#if BSON_HAVE_ALIGNED_ALLOC != 1
131+
# undef BSON_HAVE_ALIGNED_ALLOC
132+
#endif
133+
125134
#endif /* BSON_CONFIG_H */

src/libbson/src/bson/bson-memory.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <bson/bson-config.h>
2424
#include <bson/bson-memory.h>
2525

26+
#include <common-macros-private.h>
27+
2628

2729
// Ensure size of exported structs are stable.
2830
BSON_STATIC_ASSERT2 (bson_mem_vtable_t, sizeof (bson_mem_vtable_t) == sizeof (void *) * 8u);
@@ -31,10 +33,11 @@ BSON_STATIC_ASSERT2 (bson_mem_vtable_t, sizeof (bson_mem_vtable_t) == sizeof (vo
3133
// For compatibility with C standards prior to C11.
3234
static void *
3335
_aligned_alloc_impl (size_t alignment, size_t num_bytes)
34-
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(_WIN32) && !defined(__ANDROID__) && \
35-
!defined(_AIX)
36+
#if defined(BSON_HAVE_ALIGNED_ALLOC)
3637
{
38+
MC_DISABLE_IMPLICIT_WARNING_BEGIN
3739
return aligned_alloc (alignment, num_bytes);
40+
MC_DISABLE_IMPLICIT_WARNING_END
3841
}
3942
#elif defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L
4043
{

src/libmongoc/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,11 @@ if (NOT ENABLE_ZLIB STREQUAL "OFF")
112112
check_include_files ("stdarg.h" HAVE_STDARG_H)
113113
if (HAVE_UNISTD_H)
114114
add_definitions (-DHAVE_UNISTD_H)
115+
list (APPEND CMAKE_REQUIRED_DEFINITIONS -DHAVE_UNISTD_H)
115116
endif ()
116117
if (HAVE_STDARG_H)
117118
add_definitions (-DHAVE_STDARG_H)
119+
list (APPEND CMAKE_REQUIRED_DEFINITIONS -DHAVE_STDARG_H)
118120
endif ()
119121
set (MONGOC_ENABLE_COMPRESSION 1)
120122
set (MONGOC_ENABLE_COMPRESSION_ZLIB 1)

0 commit comments

Comments
 (0)