Skip to content

CXX-3072 Reduce Warnings - Code Consistency and Quality Improvements #1178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 25, 2024
Merged
2 changes: 1 addition & 1 deletion examples/mongocxx/mongodb.com/documentation_examples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void check_field(const T& document,
const char* field,
bool should_have,
int example_no,
const char* example_type = NULL) {
const char* example_type = nullptr) {
std::string example_type_formatted = example_type ? example_type + std::string(" ") : "";
if (should_have) {
if (!document[field]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class strong_ordering {

// nonstd: Swap greater/less values
constexpr strong_ordering inverted() const noexcept {
return *this < 0 ? greater : *this > 0 ? less : *this;
return *this < nullptr ? greater : *this > nullptr ? less : *this;
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ struct nullopt_t {
explicit constexpr nullopt_t(std::nullptr_t) noexcept {}
};
/// Tag constant to construct or compare with an empty optional value
static constexpr nullopt_t nullopt{0};
static constexpr nullopt_t nullopt{nullptr};
/// Tag used to call the emplacement-constructor of optional<T>
static constexpr struct in_place_t {
} in_place;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class basic_string_view : bsoncxx::detail::equality_operators, bsoncxx::detail::
const std::basic_string<value_type, traits_type, Alloc>& str) noexcept
: _begin(str.data()), _size(str.size()) {}

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

#if __cpp_lib_string_view
#if defined(__cpp_lib_string_view)
explicit operator std::basic_string_view<value_type, traits_type>() const noexcept {
return std::basic_string_view<value_type, traits_type>(data(), size());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@

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

#define BSONCXX_CITER \
bson_iter_t iter; \
bson_iter_init_from_data_at_offset(&iter, _raw, _length, _offset, _keylen);
#define BSONCXX_CITER \
bson_iter_t iter; \
bson_iter_init_from_data_at_offset(&iter, _raw, _length, _offset, _keylen); \
((void)0)

namespace bsoncxx {
namespace v_noabi {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class error_category_impl final : public std::error_category {
return "unset document::element";
case error_code::k_invalid_oid:
return "could not parse Object ID string";
case error_code::k_failed_converting_bson_to_json:
return "could not convert document to JSON";
case error_code::k_json_parse_failure:
return "could not parse JSON document";
case error_code::k_invalid_decimal128:
Expand All @@ -72,6 +74,8 @@ class error_category_impl final : public std::error_category {
return "tried to complete appending an array, but overflowed";
case error_code::k_cannot_end_appending_document:
return "tried to complete appending an document, but overflowed";
case error_code::k_invalid_binary_subtype:
return "invalid BSON binary subtype";
case error_code::k_invalid_bson_type_id:
return "invalid BSON type identifier";
#define BSONCXX_ENUM(name, value) \
Expand Down
2 changes: 1 addition & 1 deletion src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ document::value BSONCXX_CALL from_json(stdx::string_view json) {
return document::value{buf, length, bson_free_deleter};
}

document::value BSONCXX_CALL operator"" _bson(const char* str, size_t len) {
document::value BSONCXX_CALL operator""_bson(const char* str, size_t len) {
return from_json(stdx::string_view{str, len});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ inline int ntop(std::uint8_t const* src,
return -1;
}
target[datalength] = '\0'; /* Returned value doesn't count \0. */
return (int)datalength;
return static_cast<int>(datalength);
}

} // namespace b64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class value::impl {
impl operator=(const impl&) = delete;

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

bson_value_t _value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ value::value(b_string v) : value(v.value) {}
value::value(stdx::string_view v) : _impl{stdx::make_unique<impl>()} {
_impl->_value.value_type = BSON_TYPE_UTF8;
_impl->_value.value.v_utf8.str = make_copy_for_libbson(v);
_impl->_value.value.v_utf8.len = (uint32_t)v.size();
_impl->_value.value.v_utf8.len = static_cast<uint32_t>(v.size());
}

value::value(b_null) : value(nullptr) {}
Expand Down Expand Up @@ -90,6 +90,25 @@ value::value(const type id) : _impl{stdx::make_unique<impl>()} {
case type::k_undefined:
_impl->_value.value_type = BSON_TYPE_UNDEFINED;
break;

case type::k_double:
case type::k_string:
case type::k_document:
case type::k_array:
case type::k_binary:
case type::k_oid:
case type::k_bool:
case type::k_date:
case type::k_null:
case type::k_regex:
case type::k_dbpointer:
case type::k_code:
case type::k_symbol:
case type::k_codewscope:
case type::k_int32:
case type::k_timestamp:
case type::k_int64:
case type::k_decimal128:
default:
throw bsoncxx::v_noabi::exception(error_code::k_invalid_bson_type_id);
}
Expand All @@ -100,7 +119,8 @@ value::value(stdx::string_view regex, stdx::string_view options)
: _impl{stdx::make_unique<impl>()} {
_impl->_value.value_type = BSON_TYPE_REGEX;
_impl->_value.value.v_regex.regex = make_copy_for_libbson(regex);
_impl->_value.value.v_regex.options = options.empty() ? NULL : make_copy_for_libbson(options);
_impl->_value.value.v_regex.options =
options.empty() ? nullptr : make_copy_for_libbson(options);
}

value::value(b_code v) : value(v.type_id, v) {}
Expand All @@ -110,18 +130,37 @@ value::value(const type id, stdx::string_view v) : _impl{stdx::make_unique<impl>
case type::k_regex:
_impl->_value.value_type = BSON_TYPE_REGEX;
_impl->_value.value.v_regex.regex = make_copy_for_libbson(v);
_impl->_value.value.v_regex.options = NULL;
_impl->_value.value.v_regex.options = nullptr;
break;
case type::k_code:
_impl->_value.value_type = BSON_TYPE_CODE;
_impl->_value.value.v_code.code = make_copy_for_libbson(v);
_impl->_value.value.v_code.code_len = (uint32_t)v.length();
_impl->_value.value.v_code.code_len = static_cast<uint32_t>(v.length());
break;
case type::k_symbol:
_impl->_value.value_type = BSON_TYPE_SYMBOL;
_impl->_value.value.v_symbol.symbol = make_copy_for_libbson(v);
_impl->_value.value.v_symbol.len = (uint32_t)v.length();
_impl->_value.value.v_symbol.len = static_cast<uint32_t>(v.length());
break;

case type::k_double:
case type::k_string:
case type::k_document:
case type::k_array:
case type::k_binary:
case type::k_undefined:
case type::k_oid:
case type::k_bool:
case type::k_date:
case type::k_null:
case type::k_dbpointer:
case type::k_codewscope:
case type::k_int32:
case type::k_timestamp:
case type::k_int64:
case type::k_decimal128:
case type::k_maxkey:
case type::k_minkey:
default:
throw bsoncxx::v_noabi::exception(error_code::k_invalid_bson_type_id);
}
Expand All @@ -139,9 +178,29 @@ value::value(type id, uint64_t a, uint64_t b) : _impl{stdx::make_unique<impl>()}
break;
case type::k_timestamp:
_impl->_value.value_type = BSON_TYPE_TIMESTAMP;
_impl->_value.value.v_timestamp.increment = (uint32_t)a;
_impl->_value.value.v_timestamp.timestamp = (uint32_t)b;
_impl->_value.value.v_timestamp.increment = static_cast<uint32_t>(a);
_impl->_value.value.v_timestamp.timestamp = static_cast<uint32_t>(b);
break;

case type::k_double:
case type::k_string:
case type::k_document:
case type::k_array:
case type::k_binary:
case type::k_undefined:
case type::k_oid:
case type::k_bool:
case type::k_date:
case type::k_null:
case type::k_regex:
case type::k_dbpointer:
case type::k_code:
case type::k_symbol:
case type::k_codewscope:
case type::k_int32:
case type::k_int64:
case type::k_maxkey:
case type::k_minkey:
default:
throw bsoncxx::v_noabi::exception(error_code::k_invalid_bson_type_id);
}
Expand All @@ -151,7 +210,7 @@ value::value(b_dbpointer v) : value(v.collection, v.value) {}
value::value(stdx::string_view collection, oid value) : _impl{stdx::make_unique<impl>()} {
_impl->_value.value_type = BSON_TYPE_DBPOINTER;
_impl->_value.value.v_dbpointer.collection = make_copy_for_libbson(collection);
_impl->_value.value.v_dbpointer.collection_len = (uint32_t)collection.length();
_impl->_value.value.v_dbpointer.collection_len = static_cast<uint32_t>(collection.length());
std::memcpy(_impl->_value.value.v_dbpointer.oid.bytes, value.bytes(), value.k_oid_length);
}

Expand All @@ -160,9 +219,10 @@ value::value(stdx::string_view code, bsoncxx::v_noabi::document::view_or_value s
: _impl{stdx::make_unique<impl>()} {
_impl->_value.value_type = BSON_TYPE_CODEWSCOPE;
_impl->_value.value.v_codewscope.code = make_copy_for_libbson(code);
_impl->_value.value.v_codewscope.code_len = (uint32_t)code.length();
_impl->_value.value.v_codewscope.scope_len = (uint32_t)scope.view().length();
_impl->_value.value.v_codewscope.scope_data = (uint8_t*)bson_malloc(scope.view().length());
_impl->_value.value.v_codewscope.code_len = static_cast<uint32_t>(code.length());
_impl->_value.value.v_codewscope.scope_len = static_cast<uint32_t>(scope.view().length());
_impl->_value.value.v_codewscope.scope_data =
static_cast<uint8_t*>(bson_malloc(scope.view().length()));
std::memcpy(
_impl->_value.value.v_codewscope.scope_data, scope.view().data(), scope.view().length());
}
Expand All @@ -174,25 +234,25 @@ value::value(const uint8_t* data, size_t size, const binary_sub_type sub_type)
: _impl{stdx::make_unique<impl>()} {
_impl->_value.value_type = BSON_TYPE_BINARY;
_impl->_value.value.v_binary.subtype = static_cast<bson_subtype_t>(sub_type);
_impl->_value.value.v_binary.data_len = (uint32_t)size;
_impl->_value.value.v_binary.data = (uint8_t*)bson_malloc(size);
_impl->_value.value.v_binary.data_len = static_cast<uint32_t>(size);
_impl->_value.value.v_binary.data = static_cast<uint8_t*>(bson_malloc(size));
if (size)
std::memcpy(_impl->_value.value.v_binary.data, data, size);
}

value::value(b_document v) : value(v.view()) {}
value::value(bsoncxx::v_noabi::document::view v) : _impl{stdx::make_unique<impl>()} {
_impl->_value.value_type = BSON_TYPE_DOCUMENT;
_impl->_value.value.v_doc.data_len = (uint32_t)v.length();
_impl->_value.value.v_doc.data = (uint8_t*)bson_malloc(v.length());
_impl->_value.value.v_doc.data_len = static_cast<uint32_t>(v.length());
_impl->_value.value.v_doc.data = static_cast<uint8_t*>(bson_malloc(v.length()));
std::memcpy(_impl->_value.value.v_doc.data, v.data(), v.length());
}

value::value(b_array v) : value(v.value) {}
value::value(bsoncxx::v_noabi::array::view v) : _impl{stdx::make_unique<impl>()} {
_impl->_value.value_type = BSON_TYPE_ARRAY;
_impl->_value.value.v_doc.data_len = (uint32_t)v.length();
_impl->_value.value.v_doc.data = (uint8_t*)bson_malloc(v.length());
_impl->_value.value.v_doc.data_len = static_cast<uint32_t>(v.length());
_impl->_value.value.v_doc.data = static_cast<uint8_t*>(bson_malloc(v.length()));
std::memcpy(_impl->_value.value.v_doc.data, v.data(), v.length());
}

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

value::value(void* internal_value)
: _impl(stdx::make_unique<impl>((bson_value_t*)internal_value)) {}
: _impl(stdx::make_unique<impl>(static_cast<const bson_value_t*>(internal_value))) {}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@

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

#define BSONCXX_CITER \
bson_iter_t iter; \
bson_iter_init_from_data_at_offset(&iter, raw, length, offset, keylen);
#define BSONCXX_CITER \
bson_iter_t iter; \
bson_iter_init_from_data_at_offset(&iter, raw, length, offset, keylen); \
((void)0)

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

auto value = bson_iter_value(&iter);

_init((void*)value);
// ABI backward compatibility. Const is restored in `view::_init`.
_init(const_cast<void*>(static_cast<const void*>(value)));
}

view::view(void* internal_value) noexcept {
Expand All @@ -140,7 +142,7 @@ void view::_init(void* internal_value) noexcept {
return;
}

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

switch (_type) {
Expand Down
Loading