Skip to content

Commit 74309fa

Browse files
authored
CXX-3072 Reduce Warnings - Code Consistency and Quality Improvements (#1178)
* Extract scoped_bson_value into its own component * Address warnings: -Wextra-semi * Address warnings: -Wextra-semi-stmt * Address warnings: -Wimplicit-fallthrough * Address warnings: -Wdeprecated-literal-operator * Address warnings: -Wswitch-enum * Address warnings: -Wundef * Address warnings: -Wused-but-marked-unused * Address warnings: -Wzero-as-null-pointer-constant * Address warnings: -Wold-style-cast
1 parent d373213 commit 74309fa

File tree

29 files changed

+284
-223
lines changed

29 files changed

+284
-223
lines changed

examples/mongocxx/mongodb.com/documentation_examples.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void check_field(const T& document,
4141
const char* field,
4242
bool should_have,
4343
int example_no,
44-
const char* example_type = NULL) {
44+
const char* example_type = nullptr) {
4545
std::string example_type_formatted = example_type ? example_type + std::string(" ") : "";
4646
if (should_have) {
4747
if (!document[field]) {

src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/stdx/operators.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class strong_ordering {
117117

118118
// nonstd: Swap greater/less values
119119
constexpr strong_ordering inverted() const noexcept {
120-
return *this < 0 ? greater : *this > 0 ? less : *this;
120+
return *this < nullptr ? greater : *this > nullptr ? less : *this;
121121
}
122122
};
123123

src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/stdx/optional.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ struct nullopt_t {
124124
explicit constexpr nullopt_t(std::nullptr_t) noexcept {}
125125
};
126126
/// Tag constant to construct or compare with an empty optional value
127-
static constexpr nullopt_t nullopt{0};
127+
static constexpr nullopt_t nullopt{nullptr};
128128
/// Tag used to call the emplacement-constructor of optional<T>
129129
static constexpr struct in_place_t {
130130
} in_place;

src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/stdx/string_view.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class basic_string_view : bsoncxx::detail::equality_operators, bsoncxx::detail::
182182
const std::basic_string<value_type, traits_type, Alloc>& str) noexcept
183183
: _begin(str.data()), _size(str.size()) {}
184184

185-
#if __cpp_lib_string_view
185+
#if defined(__cpp_lib_string_view)
186186
constexpr basic_string_view(std::basic_string_view<value_type, traits_type> sv) noexcept
187187
: _begin(sv.data()), _size(sv.size()) {}
188188
#endif
@@ -503,7 +503,7 @@ class basic_string_view : bsoncxx::detail::equality_operators, bsoncxx::detail::
503503
return std::basic_string<Char, Traits, Allocator>(data(), size());
504504
}
505505

506-
#if __cpp_lib_string_view
506+
#if defined(__cpp_lib_string_view)
507507
explicit operator std::basic_string_view<value_type, traits_type>() const noexcept {
508508
return std::basic_string_view<value_type, traits_type>(data(), size());
509509
}

src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/document/element.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525

2626
#include <bsoncxx/config/private/prelude.hh>
2727

28-
#define BSONCXX_CITER \
29-
bson_iter_t iter; \
30-
bson_iter_init_from_data_at_offset(&iter, _raw, _length, _offset, _keylen);
28+
#define BSONCXX_CITER \
29+
bson_iter_t iter; \
30+
bson_iter_init_from_data_at_offset(&iter, _raw, _length, _offset, _keylen); \
31+
((void)0)
3132

3233
namespace bsoncxx {
3334
namespace v_noabi {

src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/exception/error_code.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class error_category_impl final : public std::error_category {
5858
return "unset document::element";
5959
case error_code::k_invalid_oid:
6060
return "could not parse Object ID string";
61+
case error_code::k_failed_converting_bson_to_json:
62+
return "could not convert document to JSON";
6163
case error_code::k_json_parse_failure:
6264
return "could not parse JSON document";
6365
case error_code::k_invalid_decimal128:
@@ -72,6 +74,8 @@ class error_category_impl final : public std::error_category {
7274
return "tried to complete appending an array, but overflowed";
7375
case error_code::k_cannot_end_appending_document:
7476
return "tried to complete appending an document, but overflowed";
77+
case error_code::k_invalid_binary_subtype:
78+
return "invalid BSON binary subtype";
7579
case error_code::k_invalid_bson_type_id:
7680
return "invalid BSON type identifier";
7781
#define BSONCXX_ENUM(name, value) \

src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/json.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ document::value BSONCXX_CALL from_json(stdx::string_view json) {
9999
return document::value{buf, length, bson_free_deleter};
100100
}
101101

102-
document::value BSONCXX_CALL operator"" _bson(const char* str, size_t len) {
102+
document::value BSONCXX_CALL operator""_bson(const char* str, size_t len) {
103103
return from_json(stdx::string_view{str, len});
104104
}
105105

src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/private/b64_ntop.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ inline int ntop(std::uint8_t const* src,
185185
return -1;
186186
}
187187
target[datalength] = '\0'; /* Returned value doesn't count \0. */
188-
return (int)datalength;
188+
return static_cast<int>(datalength);
189189
}
190190

191191
} // namespace b64

src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/types/bson_value/private/value.hh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ class value::impl {
4747
impl operator=(const impl&) = delete;
4848

4949
bson_value::view view() const noexcept {
50-
return bson_value::view{(void*)&_value};
50+
// ABI backward compatibility. Const is restored in `view::_init`.
51+
return bson_value::view{const_cast<void*>(static_cast<const void*>(&_value))};
5152
}
5253

5354
bson_value_t _value;

src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/types/bson_value/value.cpp

Lines changed: 78 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ value::value(b_string v) : value(v.value) {}
5050
value::value(stdx::string_view v) : _impl{stdx::make_unique<impl>()} {
5151
_impl->_value.value_type = BSON_TYPE_UTF8;
5252
_impl->_value.value.v_utf8.str = make_copy_for_libbson(v);
53-
_impl->_value.value.v_utf8.len = (uint32_t)v.size();
53+
_impl->_value.value.v_utf8.len = static_cast<uint32_t>(v.size());
5454
}
5555

5656
value::value(b_null) : value(nullptr) {}
@@ -90,6 +90,25 @@ value::value(const type id) : _impl{stdx::make_unique<impl>()} {
9090
case type::k_undefined:
9191
_impl->_value.value_type = BSON_TYPE_UNDEFINED;
9292
break;
93+
94+
case type::k_double:
95+
case type::k_string:
96+
case type::k_document:
97+
case type::k_array:
98+
case type::k_binary:
99+
case type::k_oid:
100+
case type::k_bool:
101+
case type::k_date:
102+
case type::k_null:
103+
case type::k_regex:
104+
case type::k_dbpointer:
105+
case type::k_code:
106+
case type::k_symbol:
107+
case type::k_codewscope:
108+
case type::k_int32:
109+
case type::k_timestamp:
110+
case type::k_int64:
111+
case type::k_decimal128:
93112
default:
94113
throw bsoncxx::v_noabi::exception(error_code::k_invalid_bson_type_id);
95114
}
@@ -100,7 +119,8 @@ value::value(stdx::string_view regex, stdx::string_view options)
100119
: _impl{stdx::make_unique<impl>()} {
101120
_impl->_value.value_type = BSON_TYPE_REGEX;
102121
_impl->_value.value.v_regex.regex = make_copy_for_libbson(regex);
103-
_impl->_value.value.v_regex.options = options.empty() ? NULL : make_copy_for_libbson(options);
122+
_impl->_value.value.v_regex.options =
123+
options.empty() ? nullptr : make_copy_for_libbson(options);
104124
}
105125

106126
value::value(b_code v) : value(v.type_id, v) {}
@@ -110,18 +130,37 @@ value::value(const type id, stdx::string_view v) : _impl{stdx::make_unique<impl>
110130
case type::k_regex:
111131
_impl->_value.value_type = BSON_TYPE_REGEX;
112132
_impl->_value.value.v_regex.regex = make_copy_for_libbson(v);
113-
_impl->_value.value.v_regex.options = NULL;
133+
_impl->_value.value.v_regex.options = nullptr;
114134
break;
115135
case type::k_code:
116136
_impl->_value.value_type = BSON_TYPE_CODE;
117137
_impl->_value.value.v_code.code = make_copy_for_libbson(v);
118-
_impl->_value.value.v_code.code_len = (uint32_t)v.length();
138+
_impl->_value.value.v_code.code_len = static_cast<uint32_t>(v.length());
119139
break;
120140
case type::k_symbol:
121141
_impl->_value.value_type = BSON_TYPE_SYMBOL;
122142
_impl->_value.value.v_symbol.symbol = make_copy_for_libbson(v);
123-
_impl->_value.value.v_symbol.len = (uint32_t)v.length();
143+
_impl->_value.value.v_symbol.len = static_cast<uint32_t>(v.length());
124144
break;
145+
146+
case type::k_double:
147+
case type::k_string:
148+
case type::k_document:
149+
case type::k_array:
150+
case type::k_binary:
151+
case type::k_undefined:
152+
case type::k_oid:
153+
case type::k_bool:
154+
case type::k_date:
155+
case type::k_null:
156+
case type::k_dbpointer:
157+
case type::k_codewscope:
158+
case type::k_int32:
159+
case type::k_timestamp:
160+
case type::k_int64:
161+
case type::k_decimal128:
162+
case type::k_maxkey:
163+
case type::k_minkey:
125164
default:
126165
throw bsoncxx::v_noabi::exception(error_code::k_invalid_bson_type_id);
127166
}
@@ -139,9 +178,29 @@ value::value(type id, uint64_t a, uint64_t b) : _impl{stdx::make_unique<impl>()}
139178
break;
140179
case type::k_timestamp:
141180
_impl->_value.value_type = BSON_TYPE_TIMESTAMP;
142-
_impl->_value.value.v_timestamp.increment = (uint32_t)a;
143-
_impl->_value.value.v_timestamp.timestamp = (uint32_t)b;
181+
_impl->_value.value.v_timestamp.increment = static_cast<uint32_t>(a);
182+
_impl->_value.value.v_timestamp.timestamp = static_cast<uint32_t>(b);
144183
break;
184+
185+
case type::k_double:
186+
case type::k_string:
187+
case type::k_document:
188+
case type::k_array:
189+
case type::k_binary:
190+
case type::k_undefined:
191+
case type::k_oid:
192+
case type::k_bool:
193+
case type::k_date:
194+
case type::k_null:
195+
case type::k_regex:
196+
case type::k_dbpointer:
197+
case type::k_code:
198+
case type::k_symbol:
199+
case type::k_codewscope:
200+
case type::k_int32:
201+
case type::k_int64:
202+
case type::k_maxkey:
203+
case type::k_minkey:
145204
default:
146205
throw bsoncxx::v_noabi::exception(error_code::k_invalid_bson_type_id);
147206
}
@@ -151,7 +210,7 @@ value::value(b_dbpointer v) : value(v.collection, v.value) {}
151210
value::value(stdx::string_view collection, oid value) : _impl{stdx::make_unique<impl>()} {
152211
_impl->_value.value_type = BSON_TYPE_DBPOINTER;
153212
_impl->_value.value.v_dbpointer.collection = make_copy_for_libbson(collection);
154-
_impl->_value.value.v_dbpointer.collection_len = (uint32_t)collection.length();
213+
_impl->_value.value.v_dbpointer.collection_len = static_cast<uint32_t>(collection.length());
155214
std::memcpy(_impl->_value.value.v_dbpointer.oid.bytes, value.bytes(), value.k_oid_length);
156215
}
157216

@@ -160,9 +219,10 @@ value::value(stdx::string_view code, bsoncxx::v_noabi::document::view_or_value s
160219
: _impl{stdx::make_unique<impl>()} {
161220
_impl->_value.value_type = BSON_TYPE_CODEWSCOPE;
162221
_impl->_value.value.v_codewscope.code = make_copy_for_libbson(code);
163-
_impl->_value.value.v_codewscope.code_len = (uint32_t)code.length();
164-
_impl->_value.value.v_codewscope.scope_len = (uint32_t)scope.view().length();
165-
_impl->_value.value.v_codewscope.scope_data = (uint8_t*)bson_malloc(scope.view().length());
222+
_impl->_value.value.v_codewscope.code_len = static_cast<uint32_t>(code.length());
223+
_impl->_value.value.v_codewscope.scope_len = static_cast<uint32_t>(scope.view().length());
224+
_impl->_value.value.v_codewscope.scope_data =
225+
static_cast<uint8_t*>(bson_malloc(scope.view().length()));
166226
std::memcpy(
167227
_impl->_value.value.v_codewscope.scope_data, scope.view().data(), scope.view().length());
168228
}
@@ -174,25 +234,25 @@ value::value(const uint8_t* data, size_t size, const binary_sub_type sub_type)
174234
: _impl{stdx::make_unique<impl>()} {
175235
_impl->_value.value_type = BSON_TYPE_BINARY;
176236
_impl->_value.value.v_binary.subtype = static_cast<bson_subtype_t>(sub_type);
177-
_impl->_value.value.v_binary.data_len = (uint32_t)size;
178-
_impl->_value.value.v_binary.data = (uint8_t*)bson_malloc(size);
237+
_impl->_value.value.v_binary.data_len = static_cast<uint32_t>(size);
238+
_impl->_value.value.v_binary.data = static_cast<uint8_t*>(bson_malloc(size));
179239
if (size)
180240
std::memcpy(_impl->_value.value.v_binary.data, data, size);
181241
}
182242

183243
value::value(b_document v) : value(v.view()) {}
184244
value::value(bsoncxx::v_noabi::document::view v) : _impl{stdx::make_unique<impl>()} {
185245
_impl->_value.value_type = BSON_TYPE_DOCUMENT;
186-
_impl->_value.value.v_doc.data_len = (uint32_t)v.length();
187-
_impl->_value.value.v_doc.data = (uint8_t*)bson_malloc(v.length());
246+
_impl->_value.value.v_doc.data_len = static_cast<uint32_t>(v.length());
247+
_impl->_value.value.v_doc.data = static_cast<uint8_t*>(bson_malloc(v.length()));
188248
std::memcpy(_impl->_value.value.v_doc.data, v.data(), v.length());
189249
}
190250

191251
value::value(b_array v) : value(v.value) {}
192252
value::value(bsoncxx::v_noabi::array::view v) : _impl{stdx::make_unique<impl>()} {
193253
_impl->_value.value_type = BSON_TYPE_ARRAY;
194-
_impl->_value.value.v_doc.data_len = (uint32_t)v.length();
195-
_impl->_value.value.v_doc.data = (uint8_t*)bson_malloc(v.length());
254+
_impl->_value.value.v_doc.data_len = static_cast<uint32_t>(v.length());
255+
_impl->_value.value.v_doc.data = static_cast<uint8_t*>(bson_malloc(v.length()));
196256
std::memcpy(_impl->_value.value.v_doc.data, v.data(), v.length());
197257
}
198258

@@ -215,7 +275,7 @@ value::value(const std::uint8_t* raw,
215275
}
216276

217277
value::value(void* internal_value)
218-
: _impl(stdx::make_unique<impl>((bson_value_t*)internal_value)) {}
278+
: _impl(stdx::make_unique<impl>(static_cast<const bson_value_t*>(internal_value))) {}
219279

220280
value::value(const value& rhs) : value(&rhs._impl->_value) {}
221281

src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/types/bson_value/view.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323

2424
#include <bsoncxx/config/private/prelude.hh>
2525

26-
#define BSONCXX_CITER \
27-
bson_iter_t iter; \
28-
bson_iter_init_from_data_at_offset(&iter, raw, length, offset, keylen);
26+
#define BSONCXX_CITER \
27+
bson_iter_t iter; \
28+
bson_iter_init_from_data_at_offset(&iter, raw, length, offset, keylen); \
29+
((void)0)
2930

3031
#define BSONCXX_TYPE_CHECK(name) \
3132
do { \
@@ -126,7 +127,8 @@ view::view(const std::uint8_t* raw,
126127

127128
auto value = bson_iter_value(&iter);
128129

129-
_init((void*)value);
130+
// ABI backward compatibility. Const is restored in `view::_init`.
131+
_init(const_cast<void*>(static_cast<const void*>(value)));
130132
}
131133

132134
view::view(void* internal_value) noexcept {
@@ -140,7 +142,7 @@ void view::_init(void* internal_value) noexcept {
140142
return;
141143
}
142144

143-
bson_value_t* v = (bson_value_t*)(internal_value);
145+
auto v = static_cast<const bson_value_t*>(internal_value);
144146
_type = static_cast<bsoncxx::v_noabi::type>(v->value_type);
145147

146148
switch (_type) {

0 commit comments

Comments
 (0)