Skip to content

Commit bc6b800

Browse files
authored
CDRIVER-3632 Address pedantic warnings for function pointer conversions (#850)
Conversion between object pointers and function pointers is undefined behavior, though permitted as a "common extension" by the C standard (see N1570 J.5.7 "Function pointer casts"). This commit defines a new TestSuite_AddFullWithTestFn macro as a convenient utility that defines an intermediate TestFuncCtx object through which a given function pointer is passed via the ctx parameter without incurring function pointer <-> object pointer conversions.
1 parent 96ed1d7 commit bc6b800

File tree

5 files changed

+169
-201
lines changed

5 files changed

+169
-201
lines changed

src/libmongoc/tests/TestSuite.c

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,6 @@
4242
#include "test-libmongoc.h"
4343
#include "TestSuite.h"
4444

45-
/*
46-
* Prevent failing on pedantic GCC/clang warning: "ISO C forbids conversion of
47-
* function pointer to object pointer type."
48-
*/
49-
#ifdef __clang__
50-
#pragma clang diagnostic warning "-Wpedantic"
51-
#elif __GNUC__ > 6
52-
#pragma GCC diagnostic warning "-Wpedantic"
53-
#elif __GNUC__ <= 6
54-
#pragma GCC diagnostic warning "-pedantic"
55-
#endif
56-
5745
static bson_once_t once = BSON_ONCE_INIT;
5846
static bson_mutex_t gTestMutex;
5947
static TestSuite *gTestSuite;
@@ -251,9 +239,9 @@ TestSuite_CheckMockServerAllowed (void)
251239
}
252240

253241
static void
254-
TestSuite_AddHelper (void *cb_)
242+
TestSuite_AddHelper (void *ctx)
255243
{
256-
TestFunc cb = (TestFunc) cb_;
244+
TestFunc cb = (TestFunc) ((TestFnCtx *) ctx)->test_fn;
257245

258246
cb ();
259247
}
@@ -263,12 +251,8 @@ TestSuite_Add (TestSuite *suite, /* IN */
263251
const char *name, /* IN */
264252
TestFunc func) /* IN */
265253
{
266-
TestSuite_AddFull (suite,
267-
name,
268-
TestSuite_AddHelper,
269-
NULL,
270-
(void *) func,
271-
TestSuite_CheckDummy);
254+
TestSuite_AddFullWithTestFn (
255+
suite, name, TestSuite_AddHelper, NULL, func, TestSuite_CheckDummy);
272256
}
273257

274258

@@ -277,12 +261,8 @@ TestSuite_AddLive (TestSuite *suite, /* IN */
277261
const char *name, /* IN */
278262
TestFunc func) /* IN */
279263
{
280-
TestSuite_AddFull (suite,
281-
name,
282-
TestSuite_AddHelper,
283-
NULL,
284-
(void *) func,
285-
TestSuite_CheckLive);
264+
TestSuite_AddFullWithTestFn (
265+
suite, name, TestSuite_AddHelper, NULL, func, TestSuite_CheckLive);
286266
}
287267

288268

src/libmongoc/tests/TestSuite.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,7 @@ typedef void (*TestFuncDtor) (void *);
643643
typedef int (*CheckFunc) (void);
644644
typedef struct _Test Test;
645645
typedef struct _TestSuite TestSuite;
646+
typedef struct _TestFnCtx TestFnCtx;
646647

647648

648649
struct _Test {
@@ -671,6 +672,11 @@ struct _TestSuite {
671672
};
672673

673674

675+
struct _TestFnCtx {
676+
TestFunc test_fn;
677+
};
678+
679+
674680
void
675681
TestSuite_Init (TestSuite *suite, const char *name, int argc, char **argv);
676682
void
@@ -710,6 +716,14 @@ _TestSuite_AddFull (TestSuite *suite,
710716
...);
711717
#define TestSuite_AddFull(_suite, _name, _func, _dtor, _ctx, ...) \
712718
_TestSuite_AddFull (_suite, _name, _func, _dtor, _ctx, __VA_ARGS__, NULL)
719+
#define TestSuite_AddFullWithTestFn( \
720+
_suite, _name, _func, _dtor, _test_fn, ...) \
721+
do { \
722+
static TestFnCtx ctx; \
723+
ctx.test_fn = (TestFunc) (_test_fn); \
724+
_TestSuite_AddFull ( \
725+
_suite, _name, _func, _dtor, &ctx, __VA_ARGS__, NULL); \
726+
} while (0)
713727
int
714728
TestSuite_Run (TestSuite *suite);
715729
void

src/libmongoc/tests/test-mongoc-client-session.c

Lines changed: 58 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,6 @@
1414
#undef MONGOC_LOG_DOMAIN
1515
#define MONGOC_LOG_DOMAIN "session-test"
1616

17-
/*
18-
* Prevent failing on pedantic GCC/clang warning: "ISO C forbids conversion of
19-
* function pointer to object pointer type."
20-
*/
21-
#ifdef __clang__
22-
#pragma clang diagnostic warning "-Wpedantic"
23-
#elif __GNUC__ > 6
24-
#pragma GCC diagnostic warning "-Wpedantic"
25-
#elif __GNUC__ <= 6
26-
#pragma GCC diagnostic warning "-pedantic"
27-
#endif
28-
2917
static void
3018
test_session_opts_clone (void)
3119
{
@@ -1627,34 +1615,36 @@ _run_session_test (session_test_fn_t test_fn, bool allow_read_concern)
16271615
static void
16281616
run_session_test (void *ctx)
16291617
{
1630-
_run_session_test ((session_test_fn_t) ctx, true);
1618+
_run_session_test ((session_test_fn_t) ((TestFnCtx *) ctx)->test_fn, true);
16311619
}
16321620

16331621

16341622
/* test a command that doesn't allow readConcern, and therefore isn't causal */
16351623
static void
16361624
run_session_test_no_rc (void *ctx)
16371625
{
1638-
_run_session_test ((session_test_fn_t) ctx, false);
1626+
_run_session_test ((session_test_fn_t) ((TestFnCtx *) ctx)->test_fn, false);
16391627
}
16401628

16411629

16421630
/* skip _test_session_from_wrong_client, which would abort with bulk op */
16431631
static void
16441632
run_session_test_bulk_operation (void *ctx)
16451633
{
1646-
_test_explicit_session_lsid ((session_test_fn_t) ctx);
1647-
_test_implicit_session_lsid ((session_test_fn_t) ctx);
1648-
_test_causal_consistency ((session_test_fn_t) ctx, false /* read concern */);
1634+
session_test_fn_t test_fn = (session_test_fn_t) ((TestFnCtx *) ctx)->test_fn;
1635+
_test_explicit_session_lsid (test_fn);
1636+
_test_implicit_session_lsid (test_fn);
1637+
_test_causal_consistency (test_fn, false /* read concern */);
16491638
}
16501639

16511640

16521641
static void
1653-
run_count_test (session_test_fn_t test_fn)
1642+
run_count_test (void *ctx)
16541643
{
16551644
/* CDRIVER-3612: mongoc_collection_estimated_document_count does not support
16561645
* explicit sessions */
1657-
_test_implicit_session_lsid (test_fn);
1646+
_test_implicit_session_lsid (
1647+
(session_test_fn_t) ((TestFnCtx *) ctx)->test_fn);
16581648
}
16591649

16601650

@@ -2530,55 +2520,59 @@ _test_unacknowledged (session_test_fn_t test_fn,
25302520
static void
25312521
test_unacknowledged_explicit_cs_inherit_wc (void *ctx)
25322522
{
2533-
_test_unacknowledged ((session_test_fn_t) ctx, true, true);
2523+
_test_unacknowledged (
2524+
(session_test_fn_t) ((TestFnCtx *) ctx)->test_fn, true, true);
25342525
}
25352526

25362527

25372528
static void
25382529
test_unacknowledged_implicit_cs_explicit_wc (void *ctx)
25392530
{
2540-
_test_unacknowledged ((session_test_fn_t) ctx, true, false);
2531+
_test_unacknowledged (
2532+
(session_test_fn_t) ((TestFnCtx *) ctx)->test_fn, true, false);
25412533
}
25422534

25432535

25442536
static void
25452537
test_unacknowledged_implicit_cs_inherit_wc (void *ctx)
25462538
{
2547-
_test_unacknowledged ((session_test_fn_t) ctx, false, true);
2539+
_test_unacknowledged (
2540+
(session_test_fn_t) ((TestFnCtx *) ctx)->test_fn, false, true);
25482541
}
25492542

25502543

25512544
static void
25522545
test_unacknowledged_explicit_cs_explicit_wc (void *ctx)
25532546
{
2554-
_test_unacknowledged ((session_test_fn_t) ctx, false, false);
2547+
_test_unacknowledged (
2548+
(session_test_fn_t) ((TestFnCtx *) ctx)->test_fn, false, false);
25552549
}
25562550

25572551

2558-
#define add_session_test(_suite, _name, _test_fn, _allow_read_concern) \
2559-
TestSuite_AddFull (_suite, \
2560-
_name, \
2561-
(_allow_read_concern) ? run_session_test \
2562-
: run_session_test_no_rc, \
2563-
NULL, \
2564-
(void *) (_test_fn), \
2565-
test_framework_skip_if_no_cluster_time, \
2566-
test_framework_skip_if_no_crypto)
2552+
#define add_session_test(_suite, _name, _test_fn, _allow_read_concern) \
2553+
TestSuite_AddFullWithTestFn ( \
2554+
_suite, \
2555+
_name, \
2556+
(_allow_read_concern) ? run_session_test : run_session_test_no_rc, \
2557+
NULL, \
2558+
_test_fn, \
2559+
test_framework_skip_if_no_cluster_time, \
2560+
test_framework_skip_if_no_crypto)
25672561

25682562
#define add_session_test_wc(_suite, _name, _test_fn, _allow_read_concern, ...) \
2569-
TestSuite_AddFull (_suite, \
2570-
_name, \
2571-
(_allow_read_concern) ? run_session_test \
2572-
: run_session_test_no_rc, \
2573-
NULL, \
2574-
(void *) (_test_fn), \
2575-
test_framework_skip_if_no_cluster_time, \
2576-
test_framework_skip_if_no_crypto, \
2577-
__VA_ARGS__)
2563+
TestSuite_AddFullWithTestFn ( \
2564+
_suite, \
2565+
_name, \
2566+
(_allow_read_concern) ? run_session_test : run_session_test_no_rc, \
2567+
NULL, \
2568+
_test_fn, \
2569+
test_framework_skip_if_no_cluster_time, \
2570+
test_framework_skip_if_no_crypto, \
2571+
__VA_ARGS__)
25782572

25792573
#define add_unacknowledged_test( \
25802574
_suite, _name, _test_fn, _explicit_cs, _inherit_wc) \
2581-
TestSuite_AddFull ( \
2575+
TestSuite_AddFullWithTestFn ( \
25822576
_suite, \
25832577
_name, \
25842578
(_explicit_cs) \
@@ -2587,7 +2581,7 @@ test_unacknowledged_explicit_cs_explicit_wc (void *ctx)
25872581
: (_inherit_wc ? test_unacknowledged_implicit_cs_inherit_wc \
25882582
: test_unacknowledged_explicit_cs_explicit_wc), \
25892583
NULL, \
2590-
(void *) (_test_fn), \
2584+
_test_fn, \
25912585
test_framework_skip_if_no_cluster_time, \
25922586
test_framework_skip_if_no_crypto)
25932587

@@ -2910,13 +2904,13 @@ test_session_install (TestSuite *suite)
29102904
add_session_test (
29112905
suite, "/Session/read_write_cmd", test_read_write_cmd, true);
29122906
add_session_test (suite, "/Session/db_cmd", test_db_cmd, false);
2913-
TestSuite_AddFull (suite,
2914-
"/Session/count",
2915-
(void *) run_count_test,
2916-
NULL,
2917-
(void *) test_count,
2918-
test_framework_skip_if_no_cluster_time,
2919-
test_framework_skip_if_no_crypto);
2907+
TestSuite_AddFullWithTestFn (suite,
2908+
"/Session/count",
2909+
(TestFuncWC) run_count_test,
2910+
NULL,
2911+
test_count,
2912+
test_framework_skip_if_no_cluster_time,
2913+
test_framework_skip_if_no_crypto);
29202914
add_session_test (suite, "/Session/cursor", test_cursor, true);
29212915
add_session_test (suite, "/Session/drop", test_drop, false);
29222916
add_session_test (suite, "/Session/drop_index", test_drop_index, false);
@@ -2951,20 +2945,20 @@ test_session_install (TestSuite *suite)
29512945
suite, "/Session/collection_names", test_collection_names, true);
29522946
add_session_test (suite, "/Session/bulk", test_bulk, false);
29532947
add_session_test (suite, "/Session/find_indexes", test_find_indexes, true);
2954-
TestSuite_AddFull (suite,
2955-
"/Session/bulk_set_session",
2956-
run_session_test_bulk_operation,
2957-
NULL,
2958-
test_bulk_set_session,
2959-
test_framework_skip_if_no_cluster_time,
2960-
test_framework_skip_if_no_crypto);
2961-
TestSuite_AddFull (suite,
2962-
"/Session/bulk_set_client",
2963-
run_session_test_bulk_operation,
2964-
NULL,
2965-
test_bulk_set_client,
2966-
test_framework_skip_if_no_cluster_time,
2967-
test_framework_skip_if_no_crypto);
2948+
TestSuite_AddFullWithTestFn (suite,
2949+
"/Session/bulk_set_session",
2950+
run_session_test_bulk_operation,
2951+
NULL,
2952+
test_bulk_set_session,
2953+
test_framework_skip_if_no_cluster_time,
2954+
test_framework_skip_if_no_crypto);
2955+
TestSuite_AddFullWithTestFn (suite,
2956+
"/Session/bulk_set_client",
2957+
run_session_test_bulk_operation,
2958+
NULL,
2959+
test_bulk_set_client,
2960+
test_framework_skip_if_no_cluster_time,
2961+
test_framework_skip_if_no_crypto);
29682962
TestSuite_AddFull (suite,
29692963
"/Session/cursor_implicit_session",
29702964
test_cursor_implicit_session,

0 commit comments

Comments
 (0)