Skip to content

Set Certain Warnings as Errors Unconditionally #890

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 20 commits into from
Dec 3, 2021

Conversation

vector-of-bool
Copy link
Contributor

Some constructs that are allowed in C are well-established to be virtually always erroneous, e.g. calling a function that hasn't been declared, implicitly binding a pointer to a different type, and implicitly converting an integral to a pointer. Implicitly converting a pointer-to-const to a pointer-to-mutable is not always an error, but is problematic enough that an explicit cast should be required.

Many of these errors will be caught at a later time, but only due to test failures, linker failures, or on a build on CI that enables some warnings-as-errors (currently never on Windows).

This change instead makes such constructs always-errors, thus adding a barrier to any changes that produce such errors that might otherwise have been missed. The selected warnings are well-established and detected by all modern compilers, and are very unlikely to appear anew do to compiler upgrades.

Additional warnings that are not so definitely-wrong (etc. sign conversion/compare, unused variables) are not addressed by these changes.

# Missing return types/statements
gnu-like:-Werror=return-type msvc:/we4716
# Incompatible pointer types
gnu-like:$<${is_c_lang}:-Werror=incompatible-pointer-types> msvc:/we4113
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one might be problematic on older compilers with the new atomics. For instance, here is an excerpt of build output from RHEL 6.2 (gcc 4.4.7):

/root/mongo-c-driver.git/src/libbson/src/bson/bson-atomic.c: In function 'bson_atomic_int_add':
/root/mongo-c-driver.git/src/libbson/src/bson/bson-atomic.c:28: warning: passing argument 1 of 'bson_atomic_int32_fetch_add' from incompatible pointer type
/root/mongo-c-driver.git/src/libbson/src/bson/bson-atomic.h:323: note: expected 'volatile long int *' but argument is of type 'volatile int32_t *'
/root/mongo-c-driver.git/src/libbson/src/bson/bson-atomic.c: In function '_lock_64bit_atomic':
/root/mongo-c-driver.git/src/libbson/src/bson/bson-atomic.c:61: warning: passing argument 1 of 'bson_atomic_int_compare_exchange_weak' from incompatible pointer type
/root/mongo-c-driver.git/src/libbson/src/bson/bson-atomic.h:324: note: expected 'volatile long int *' but argument is of type 'int *'

We might want to wait on this PR until after CDRIVER-4215 is done, as that might clear some of the warnings which would turn into errors with this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's another open ticket about some related implicit conversions in the atomics code. I plan to inspect it, because it is very suspicious.

Copy link
Contributor

@chardan chardan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. I think these will generally only improve the code, although as has been pointed out we should check how they behave across all the compiler versions we support; I'm less familiar with flags on the Microsoft compiler, but from what I've read I think it's also ok.

@@ -10,7 +10,6 @@
-Wno-strict-aliasing
-Wno-uninitialized
-Wredundant-decls
-Wreturn-type
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious as to why we're removing this one, it seems to fall into the "almost always an error" category?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was moved into the MongoC-Warnings.cmake file. This just removes the redundancy.

# Incompatible pointer types
gnu-like:$<${is_c_lang}:-Werror=incompatible-pointer-types> msvc:/we4113
# Integral/pointer conversions
gnu-like:$<${is_c_lang}:-Werror=int-conversion> msvc:/we4047
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen some (usually pretty old) code where this can make things noisy-- but it is indeed "almost always an error".

@rcsanchez97
Copy link
Contributor

@vector-of-bool Now that #889 is merged, you might consider rebasing these changes and starting a new patch build. It will give us useful information about the effect of these changes on the build for older platforms.

Copy link
Contributor

@eramongodb eramongodb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The workaround for CMake versions less than 3.3 (assuming C language even for C++ targets) leaves me with an unsatisfied feeling. I feel the situation could be improved by using target_compile_options rather than add_compile_options, but I do not consider it to be a blocker for this PR.

Copy link
Collaborator

@kevinAlbs kevinAlbs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! The generator expressions make it simpler to declare equivalent warnings across compilers.

I suggest running a full patch build to check all compilers on the Evergreen waterfall. That can be done with the Evergreen CLI: evergreen patch --project=mongo-c-driver

@kevinAlbs kevinAlbs self-requested a review November 29, 2021 20:27
Copy link
Collaborator

@kevinAlbs kevinAlbs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I think some tasks are failing with warnings on zlib sources.

Copy link
Collaborator

@kevinAlbs kevinAlbs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! The effort towards fixing compiler warnings and improving developer workflow is much appreciated.


#[[
Define additional compile options, conditional on the compiler being used.
Each option should be prefixed by `gnu-like:` or `msvc:`. Those options will be
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic of the function allows for prefixes of gnu-like, gnu, clang, and msvc. Perhaps this function header comment should be updated for consistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot to update this comment, yeah.

Copy link
Contributor

@rcsanchez97 rcsanchez97 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vector-of-bool vector-of-bool merged commit df17e90 into mongodb:master Dec 3, 2021
vector-of-bool added a commit that referenced this pull request Dec 3, 2021
Warning became a hard error following #890. Mark the offending code path as unreachable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants