-
Notifications
You must be signed in to change notification settings - Fork 455
CDRIVER-4596 Reducing Warnings - MSVC and MinGW Warnings in libbson #1221
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Latest changes verified by this patch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. The efforts to reduce warnings are appreciated.
src/libbson/examples/bson-metrics.c
Outdated
const uint64_t a_count = ((bson_type_metrics_t *) b)->count; | ||
const uint64_t b_count = ((bson_type_metrics_t *) a)->count; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const uint64_t a_count = ((bson_type_metrics_t *) b)->count; | |
const uint64_t b_count = ((bson_type_metrics_t *) a)->count; | |
const uint64_t a_count = ((bson_type_metrics_t *) a)->count; | |
const uint64_t b_count = ((bson_type_metrics_t *) b)->count; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Description
This PR is a belated followup to #960 that addresses various warnings emitted by MSVC and MinGW on Windows distros, mostly pertaining to signedness mismatch or implicit narrowing conversions of integral types.
Verified by this patch, which adds
/WX
or-Werror
when compilingbson_shared
andbson_static
with MSVC or MinGW respectively (treat all warnings as errors). These flags are not included in this PR.The scope of this PR is limited to MSVC and MinGW warnings for the libbson library only (+ some libbson test files and misc. drive-by fixes). Followup PRs will address warnings emitted by MSVC and MinGW for the libmongoc library, example executables, and test executables. When able, warnings emitted by GCC and Clang (far more numerous) will be addressed after that.
Stats
Warning statistics were obtained using the following local toolchain on Windows 10 and Ubuntu 20.04 (within WSL2):
and the following CMake configuration for GCC/Clang:
and for MSVC:
Relative to 72cba00, this PR reduces the total number of warnings emitted by:
and the number of unique warnings (by file, line, type, and message) by:
Note: the warning counts do not include D9025 command line option override warnings generated by the
kms_message
andzlib
libraries.Methodology
C4018 Signedness Mismatch Warnings
Most signedness mismatch warnings are addressed by the use of
bson_cmp_*
utilities instead of simple comparison operators, e.g.:Otherwise, either the type of relevant variables, parameters, or data members were adjusted to avoid signedness inconsistencies, or an assertion of
bson_in_range_*
was added prior to an explicit cast to the target type, e.g.:C4028 Parameter Inconsistency Warnings
Several MSVC versions (pessimistically) warn when the type of a parameter in a function declaration differs from the definition, even when the difference is due to top-level
const
-qualifiers. The declarations were updated to match the definitions.C4146 Unsigned Unary Negation Warnings
Newer MSVC versions warn when the unary negation operator is used on an unsigned type, even if it has well-defined behavior. Added suppression of C4146 where appropriate.
C4244 Narrowing Conversion Warnings
Most of these warnings are addressed by the addition of a range-check assertion as described above in C4018. For functions that return an error code or error messages, an appropriate error and/or return value is used instead of an assertion. If range checks already exist, only the explicit cast is added.
C4267 size_t Conversion Warnings
MSVC has a separate warning type for size_t conversions than for other narrowing conversion warnings, but they are effectively the same as C4244 narrowing conversion warnings.
C4756 Constant Arithmetic Overflow Warnings
VC 2013's definition of the
INFINITY
macro triggers overflow warnings despite being intentional behavior. Furthermore, VS 2013 does not appear to support inline warning suppression with#pragma warning(suppress : ...)
or#pragma warning(disable : ...)
. Therefore, the/wd4756
compile option was added tobson_shared
andbson_static
when compiling with VS 2013 or older to disable the warning entirely, in favor of depending on other toolchain combinations to detect undesirable overflow conditions.Miscellaneous Warnings
As drive-by fixes and improvements not related to the above:
replaced
(void)expr;
withBSON_MAYBE_UNUSED int ret = expr;
in_aligned_alloc_impl
to address GCC unused result warnings that were not being silenced by(void)
. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 for context.addressed a scan-build null pointer argument warning in
_mongoc_getenv
due to repeated calls togetenv
defeating the static analyzer's ability to recognize the null pointer check.