Skip to content

Commit fee885a

Browse files
authored
Reduce Warnings: address C4127 "conditional expression is constant" warnings (#1989)
* Address false-positive -Wreturn-type warnings with GCC 11
1 parent 66f23a9 commit fee885a

14 files changed

+179
-62
lines changed

src/common/src/mlib/config.h

+5
Original file line numberDiff line numberDiff line change
@@ -350,4 +350,9 @@
350350
*/
351351
#define mlib_typeof(...) MLIB_IF_ELSE (mlib_have_typeof ()) (__typeof__) (__mlib_typeof_is_not_supported) (__VA_ARGS__)
352352

353+
/**
354+
* @brief Disable warnings for constant conditional expressions.
355+
*/
356+
#define mlib_disable_constant_conditional_expression_warnings() mlib_msvc_warning (disable : 4127)
357+
353358
#endif // MLIB_CONFIG_H_INCLUDED

src/common/tests/test-mlib.c

+1
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ _test_cmp (void)
308308
// Unintuitive result due to integer promotion:
309309
mlib_diagnostic_push ();
310310
mlib_gnu_warning_disable ("-Wsign-compare");
311+
mlib_disable_constant_conditional_expression_warnings ();
311312
ASSERT (-27 > 20u);
312313
mlib_diagnostic_pop ();
313314
// mlib_cmp produces the correct answer:

src/libbson/src/bson/bson-json.c

+20-16
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <bson/bson-iso8601-private.h>
2929

3030
#include <mlib/cmp.h>
31+
#include <mlib/config.h>
3132
#include <common-b64-private.h>
3233
#include <jsonsl/jsonsl.h>
3334

@@ -270,22 +271,25 @@ _noop (void)
270271
#define STACK_IS_DBPOINTER (STACK_FRAME_TYPE == BSON_JSON_FRAME_DBPOINTER)
271272
#define FRAME_TYPE_HAS_BSON(_type) ((_type) == BSON_JSON_FRAME_SCOPE || (_type) == BSON_JSON_FRAME_DBPOINTER)
272273
#define STACK_HAS_BSON FRAME_TYPE_HAS_BSON (STACK_FRAME_TYPE)
273-
#define STACK_PUSH(frame_type) \
274-
do { \
275-
if (bson->n >= (STACK_MAX - 1)) { \
276-
return; \
277-
} \
278-
bson->n++; \
279-
if (STACK_HAS_BSON) { \
280-
if (FRAME_TYPE_HAS_BSON (frame_type)) { \
281-
bson_reinit (STACK_BSON_CHILD); \
282-
} else { \
283-
bson_destroy (STACK_BSON_CHILD); \
284-
} \
285-
} else if (FRAME_TYPE_HAS_BSON (frame_type)) { \
286-
bson_init (STACK_BSON_CHILD); \
287-
} \
288-
STACK_FRAME_TYPE = frame_type; \
274+
#define STACK_PUSH(frame_type) \
275+
do { \
276+
if (bson->n >= (STACK_MAX - 1)) { \
277+
return; \
278+
} \
279+
bson->n++; \
280+
mlib_diagnostic_push (); \
281+
mlib_disable_constant_conditional_expression_warnings (); \
282+
if (STACK_HAS_BSON) { \
283+
if (FRAME_TYPE_HAS_BSON (frame_type)) { \
284+
bson_reinit (STACK_BSON_CHILD); \
285+
} else { \
286+
bson_destroy (STACK_BSON_CHILD); \
287+
} \
288+
} else if (FRAME_TYPE_HAS_BSON (frame_type)) { \
289+
bson_init (STACK_BSON_CHILD); \
290+
} \
291+
mlib_diagnostic_pop (); \
292+
STACK_FRAME_TYPE = frame_type; \
289293
} while (0)
290294
#define STACK_PUSH_ARRAY(statement) \
291295
do { \

src/libbson/src/bson/bson.c

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include <string.h>
2929
#include <math.h>
3030

31+
#include <mlib/config.h>
32+
3133

3234
typedef enum {
3335
BSON_VALIDATE_PHASE_START,
@@ -313,7 +315,10 @@ BSON_STATIC_ASSERT2 (max_alloc_grow_fits_min_sizet, (uint64_t) BSON_MAX_SIZE * 2
313315
// Add a bytes+length pair only if `_length > 0`.
314316
// Append failure if `n_bytes` will exceed BSON max size.
315317
#define BSON_APPEND_BYTES_ADD_ARGUMENT(_list, _bytes, _length) \
318+
mlib_diagnostic_push (); \
319+
mlib_disable_constant_conditional_expression_warnings (); \
316320
if (BSON_UNLIKELY ((_length) > BSON_MAX_SIZE - (_list).n_bytes)) { \
321+
mlib_diagnostic_pop (); \
317322
goto append_failure; \
318323
} else if ((_length) > 0) { \
319324
*(_list).current++ = (_bson_append_bytes_arg){ \

src/libbson/tests/test-oid.c

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
#include <limits.h>
3030

31+
#include <mlib/config.h>
32+
3133
#include "TestSuite.h"
3234
#include <common-macros-private.h> // BEGIN_IGNORE_DEPRECATIONS
3335
#include <common-json-private.h>
@@ -237,7 +239,10 @@ test_bson_oid_get_time_t (void)
237239

238240
/* if time_t is a signed int32, then a negative value may be interpreted
239241
* as a negative date when printing. */
242+
mlib_diagnostic_push ();
243+
mlib_disable_constant_conditional_expression_warnings ();
240244
if (sizeof (time_t) == 8) {
245+
mlib_diagnostic_pop ();
241246
bson_oid_init_from_string (&oid, "7FFFFFFF0000000000000000");
242247
str = get_time_as_string (&oid);
243248
ASSERT_CMPSTR (str, "2038-01-19T03:14:07Z");

src/libmongoc/src/mongoc/mcd-nsinfo.c

+11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <mongoc/mongoc-error-private.h>
2121
#include <mongoc/uthash.h>
2222

23+
#include <mlib/config.h>
24+
2325
typedef struct {
2426
char *ns; // Hash key.
2527
int32_t index;
@@ -79,7 +81,11 @@ mcd_nsinfo_append (mcd_nsinfo_t *self, const char *ns, bson_error_t *error)
7981
// Add to hash table.
8082
ns_to_index_t *entry = bson_malloc (sizeof (*entry));
8183
*entry = (ns_to_index_t){.index = ns_index, .ns = bson_strdup (ns), .hh = {0}};
84+
85+
mlib_diagnostic_push ();
86+
mlib_disable_constant_conditional_expression_warnings ();
8287
HASH_ADD_KEYPTR (hh, self->n2i, entry->ns, strlen (entry->ns), entry);
88+
mlib_diagnostic_pop ();
8389

8490
// Append to buffer.
8591
bson_t mcd_nsinfo_bson = BSON_INITIALIZER;
@@ -96,7 +102,12 @@ mcd_nsinfo_find (const mcd_nsinfo_t *self, const char *ns)
96102
BSON_ASSERT_PARAM (ns);
97103

98104
ns_to_index_t *found;
105+
106+
mlib_diagnostic_push ();
107+
mlib_disable_constant_conditional_expression_warnings ();
99108
HASH_FIND_STR (self->n2i, ns, found);
109+
mlib_diagnostic_pop ();
110+
100111
if (found == NULL) {
101112
return -1;
102113
}

src/libmongoc/src/mongoc/mongoc-handshake.c

+7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <common-bson-dsl-private.h>
4040
#include <common-string-private.h>
4141
#include <mlib/cmp.h>
42+
#include <mlib/config.h>
4243

4344
/*
4445
* Global handshake data instance. Initialized at startup from mongoc_init
@@ -177,9 +178,12 @@ _mongoc_handshake_get_config_hex_string (void)
177178
_set_bit (bf, byte_count, MONGOC_MD_FLAG_ENABLE_SHM_COUNTERS);
178179
#endif
179180

181+
mlib_diagnostic_push ();
182+
mlib_disable_constant_conditional_expression_warnings ();
180183
if (MONGOC_TRACE_ENABLED) {
181184
_set_bit (bf, byte_count, MONGOC_MD_FLAG_TRACE);
182185
}
186+
mlib_diagnostic_pop ();
183187

184188
#ifdef MONGOC_ENABLE_CLIENT_SIDE_ENCRYPTION
185189
_set_bit (bf, byte_count, MONGOC_MD_FLAG_ENABLE_CLIENT_SIDE_ENCRYPTION);
@@ -189,9 +193,12 @@ _mongoc_handshake_get_config_hex_string (void)
189193
_set_bit (bf, byte_count, MONGOC_MD_FLAG_ENABLE_MONGODB_AWS_AUTH);
190194
#endif
191195

196+
mlib_diagnostic_push ();
197+
mlib_disable_constant_conditional_expression_warnings ();
192198
if (MONGOC_SRV_ENABLED) {
193199
_set_bit (bf, byte_count, MONGOC_MD_FLAG_ENABLE_SRV);
194200
}
201+
mlib_diagnostic_pop ();
195202

196203
mcommon_string_append_t append;
197204
mcommon_string_set_append (mcommon_string_new_with_capacity ("0x", 2, 2 + byte_count * 2), &append);

src/libmongoc/src/mongoc/mongoc-ocsp-cache.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <mongoc/mongoc-trace-private.h>
2323
#include <bson/bson.h>
2424
#include <common-thread-private.h>
25+
#include <mlib/config.h>
2526

2627
typedef struct _cache_entry_list_t {
2728
struct _cache_entry_list_t *next;
@@ -43,8 +44,9 @@ static int
4344
cache_cmp (cache_entry_list_t *out, OCSP_CERTID *id)
4445
{
4546
ENTRY;
46-
if (!out || !out->id || !id)
47+
if (!out || !out->id || !id) {
4748
RETURN (1);
49+
}
4850
RETURN (OCSP_id_cmp (out->id, id));
4951
}
5052

src/libmongoc/src/mongoc/mongoc-server-description.c

+46-22
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include <common-atomic-private.h>
3131
#include <common-oid-private.h>
3232

33+
#include <mlib/config.h>
34+
3335
#include <stdio.h>
3436

3537
#define ALPHA 0.2
@@ -556,28 +558,34 @@ mongoc_server_description_handle_hello (mongoc_server_description_t *sd,
556558
}
557559
} else if (strcmp ("isWritablePrimary", bson_iter_key (&iter)) == 0 ||
558560
strcmp (HANDSHAKE_RESPONSE_LEGACY_HELLO, bson_iter_key (&iter)) == 0) {
559-
if (!BSON_ITER_HOLDS_BOOL (&iter))
561+
if (!BSON_ITER_HOLDS_BOOL (&iter)) {
560562
GOTO (typefailure);
563+
}
561564
is_primary = bson_iter_bool (&iter);
562565
} else if (strcmp ("helloOk", bson_iter_key (&iter)) == 0) {
563-
if (!BSON_ITER_HOLDS_BOOL (&iter))
566+
if (!BSON_ITER_HOLDS_BOOL (&iter)) {
564567
GOTO (typefailure);
568+
}
565569
sd->hello_ok = bson_iter_bool (&iter);
566570
} else if (strcmp ("me", bson_iter_key (&iter)) == 0) {
567-
if (!BSON_ITER_HOLDS_UTF8 (&iter))
571+
if (!BSON_ITER_HOLDS_UTF8 (&iter)) {
568572
GOTO (typefailure);
573+
}
569574
sd->me = bson_iter_utf8 (&iter, NULL);
570575
} else if (strcmp ("maxMessageSizeBytes", bson_iter_key (&iter)) == 0) {
571-
if (!BSON_ITER_HOLDS_INT32 (&iter))
576+
if (!BSON_ITER_HOLDS_INT32 (&iter)) {
572577
GOTO (typefailure);
578+
}
573579
sd->max_msg_size = bson_iter_int32 (&iter);
574580
} else if (strcmp ("maxBsonObjectSize", bson_iter_key (&iter)) == 0) {
575-
if (!BSON_ITER_HOLDS_INT32 (&iter))
581+
if (!BSON_ITER_HOLDS_INT32 (&iter)) {
576582
GOTO (typefailure);
583+
}
577584
sd->max_bson_obj_size = bson_iter_int32 (&iter);
578585
} else if (strcmp ("maxWriteBatchSize", bson_iter_key (&iter)) == 0) {
579-
if (!BSON_ITER_HOLDS_INT32 (&iter))
586+
if (!BSON_ITER_HOLDS_INT32 (&iter)) {
580587
GOTO (typefailure);
588+
}
581589
sd->max_write_batch_size = bson_iter_int32 (&iter);
582590
} else if (strcmp ("logicalSessionTimeoutMinutes", bson_iter_key (&iter)) == 0) {
583591
if (BSON_ITER_HOLDS_NUMBER (&iter)) {
@@ -589,68 +597,81 @@ mongoc_server_description_handle_hello (mongoc_server_description_t *sd,
589597
GOTO (typefailure);
590598
}
591599
} else if (strcmp ("minWireVersion", bson_iter_key (&iter)) == 0) {
592-
if (!BSON_ITER_HOLDS_INT32 (&iter))
600+
if (!BSON_ITER_HOLDS_INT32 (&iter)) {
593601
GOTO (typefailure);
602+
}
594603
sd->min_wire_version = bson_iter_int32 (&iter);
595604
} else if (strcmp ("maxWireVersion", bson_iter_key (&iter)) == 0) {
596-
if (!BSON_ITER_HOLDS_INT32 (&iter))
605+
if (!BSON_ITER_HOLDS_INT32 (&iter)) {
597606
GOTO (typefailure);
607+
}
598608
sd->max_wire_version = bson_iter_int32 (&iter);
599609
} else if (strcmp ("msg", bson_iter_key (&iter)) == 0) {
600610
const char *msg;
601-
if (!BSON_ITER_HOLDS_UTF8 (&iter))
611+
if (!BSON_ITER_HOLDS_UTF8 (&iter)) {
602612
GOTO (typefailure);
613+
}
603614
msg = bson_iter_utf8 (&iter, NULL);
604615
if (msg && 0 == strcmp (msg, "isdbgrid")) {
605616
is_shard = true;
606617
}
607618
} else if (strcmp ("setName", bson_iter_key (&iter)) == 0) {
608-
if (!BSON_ITER_HOLDS_UTF8 (&iter))
619+
if (!BSON_ITER_HOLDS_UTF8 (&iter)) {
609620
GOTO (typefailure);
621+
}
610622
sd->set_name = bson_iter_utf8 (&iter, NULL);
611623
} else if (strcmp ("setVersion", bson_iter_key (&iter)) == 0) {
612624
mongoc_server_description_set_set_version (sd, bson_iter_as_int64 (&iter));
613625
} else if (strcmp ("electionId", bson_iter_key (&iter)) == 0) {
614-
if (!BSON_ITER_HOLDS_OID (&iter))
626+
if (!BSON_ITER_HOLDS_OID (&iter)) {
615627
GOTO (typefailure);
628+
}
616629
mongoc_server_description_set_election_id (sd, bson_iter_oid (&iter));
617630
} else if (strcmp ("secondary", bson_iter_key (&iter)) == 0) {
618-
if (!BSON_ITER_HOLDS_BOOL (&iter))
631+
if (!BSON_ITER_HOLDS_BOOL (&iter)) {
619632
GOTO (typefailure);
633+
}
620634
is_secondary = bson_iter_bool (&iter);
621635
} else if (strcmp ("hosts", bson_iter_key (&iter)) == 0) {
622-
if (!BSON_ITER_HOLDS_ARRAY (&iter))
636+
if (!BSON_ITER_HOLDS_ARRAY (&iter)) {
623637
GOTO (typefailure);
638+
}
624639
bson_iter_array (&iter, &len, &bytes);
625640
bson_destroy (&sd->hosts);
626641
BSON_ASSERT (bson_init_static (&sd->hosts, bytes, len));
627642
} else if (strcmp ("passives", bson_iter_key (&iter)) == 0) {
628-
if (!BSON_ITER_HOLDS_ARRAY (&iter))
643+
if (!BSON_ITER_HOLDS_ARRAY (&iter)) {
629644
GOTO (typefailure);
645+
}
630646
bson_iter_array (&iter, &len, &bytes);
631647
bson_destroy (&sd->passives);
632648
BSON_ASSERT (bson_init_static (&sd->passives, bytes, len));
633649
} else if (strcmp ("arbiters", bson_iter_key (&iter)) == 0) {
634-
if (!BSON_ITER_HOLDS_ARRAY (&iter))
650+
if (!BSON_ITER_HOLDS_ARRAY (&iter)) {
635651
GOTO (typefailure);
652+
}
636653
bson_iter_array (&iter, &len, &bytes);
637654
bson_destroy (&sd->arbiters);
638655
BSON_ASSERT (bson_init_static (&sd->arbiters, bytes, len));
639656
} else if (strcmp ("primary", bson_iter_key (&iter)) == 0) {
640-
if (!BSON_ITER_HOLDS_UTF8 (&iter))
657+
if (!BSON_ITER_HOLDS_UTF8 (&iter)) {
641658
GOTO (typefailure);
659+
}
642660
sd->current_primary = bson_iter_utf8 (&iter, NULL);
643661
} else if (strcmp ("arbiterOnly", bson_iter_key (&iter)) == 0) {
644-
if (!BSON_ITER_HOLDS_BOOL (&iter))
662+
if (!BSON_ITER_HOLDS_BOOL (&iter)) {
645663
GOTO (typefailure);
664+
}
646665
is_arbiter = bson_iter_bool (&iter);
647666
} else if (strcmp ("isreplicaset", bson_iter_key (&iter)) == 0) {
648-
if (!BSON_ITER_HOLDS_BOOL (&iter))
667+
if (!BSON_ITER_HOLDS_BOOL (&iter)) {
649668
GOTO (typefailure);
669+
}
650670
is_replicaset = bson_iter_bool (&iter);
651671
} else if (strcmp ("tags", bson_iter_key (&iter)) == 0) {
652-
if (!BSON_ITER_HOLDS_DOCUMENT (&iter))
672+
if (!BSON_ITER_HOLDS_DOCUMENT (&iter)) {
653673
GOTO (typefailure);
674+
}
654675
bson_iter_document (&iter, &len, &bytes);
655676
bson_destroy (&sd->tags);
656677
BSON_ASSERT (bson_init_static (&sd->tags, bytes, len));
@@ -664,8 +685,9 @@ mongoc_server_description_handle_hello (mongoc_server_description_t *sd,
664685

665686
sd->last_write_date_ms = bson_iter_date_time (&child);
666687
} else if (strcmp ("compression", bson_iter_key (&iter)) == 0) {
667-
if (!BSON_ITER_HOLDS_ARRAY (&iter))
688+
if (!BSON_ITER_HOLDS_ARRAY (&iter)) {
668689
GOTO (typefailure);
690+
}
669691
bson_iter_array (&iter, &len, &bytes);
670692
bson_destroy (&sd->compressors);
671693
BSON_ASSERT (bson_init_static (&sd->compressors, bytes, len));
@@ -681,12 +703,14 @@ mongoc_server_description_handle_hello (mongoc_server_description_t *sd,
681703
mongoc_server_description_set_topology_version (sd, &incoming_topology_version);
682704
bson_destroy (&incoming_topology_version);
683705
} else if (strcmp ("serviceId", bson_iter_key (&iter)) == 0) {
684-
if (!BSON_ITER_HOLDS_OID (&iter))
706+
if (!BSON_ITER_HOLDS_OID (&iter)) {
685707
GOTO (typefailure);
708+
}
686709
bson_oid_copy_unsafe (bson_iter_oid (&iter), &sd->service_id);
687710
} else if (strcmp ("connectionId", bson_iter_key (&iter)) == 0) {
688-
if (!BSON_ITER_HOLDS_NUMBER (&iter))
711+
if (!BSON_ITER_HOLDS_NUMBER (&iter)) {
689712
GOTO (typefailure);
713+
}
690714
sd->server_connection_id = bson_iter_as_int64 (&iter);
691715
}
692716
}

src/libmongoc/src/mongoc/mongoc-server-monitor.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <mongoc/mongoc-trace-private.h>
3030
#include <mongoc/mongoc-structured-log-private.h>
3131
#include <common-atomic-private.h>
32+
#include <mlib/config.h>
3233

3334
#include <inttypes.h>
3435

@@ -116,11 +117,15 @@ static BSON_GNUC_PRINTF (3, 4) void _server_monitor_log (mongoc_server_monitor_t
116117
}
117118

118119
#define MONITOR_LOG(sm, ...) \
119-
do { \
120+
if (1) { \
121+
mlib_diagnostic_push (); \
122+
mlib_disable_constant_conditional_expression_warnings (); \
120123
if (MONGOC_TRACE_ENABLED) { \
121124
_server_monitor_log (sm, MONGOC_LOG_LEVEL_TRACE, __VA_ARGS__); \
122125
} \
123-
} while (0)
126+
mlib_diagnostic_pop (); \
127+
} else \
128+
((void) 0)
124129

125130
/* TODO CDRIVER-3710 use MONGOC_LOG_LEVEL_ERROR */
126131
#define MONITOR_LOG_ERROR(sm, ...) _server_monitor_log (sm, MONGOC_LOG_LEVEL_DEBUG, __VA_ARGS__)

0 commit comments

Comments
 (0)