Skip to content

CXX-3296 fix build if GCC/GNU/Clang/MSVC macro defined #1409

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 13 commits into from
May 28, 2025

Conversation

kevinAlbs
Copy link
Collaborator

@kevinAlbs kevinAlbs commented May 27, 2025

Summary

Fix build if GCC/GNU/Clang/MSVC macros are already defined.

Tested with this patch build.

Background

https://www.mongodb.com/community/forums/t/error-compiling-some-c-classes-with-driver-v4-0/321847 reports a build error due to the macro GCC being defined. I can reproduce the error by adding #define GCC 1 before including C++ driver headers:

/Users/kevin.albertson/code/cxx-bootstrap/install/mongo-cxx-driver-r4.1.0/include/bsoncxx/v_noabi/bsoncxx/document/value.hpp:185:9: error: use of undeclared identifier 'BSONCXX_PRIVATE_WARNINGS_DISABLE_IMPL_FOR_1'
        BSONCXX_PRIVATE_WARNINGS_DISABLE(GCC("-Wmaybe-uninitialized"));
        ^

This PR changes the BSONCXX_PRIVATE_WARNINGS_DISABLE* macros to avoid assuming GCC/Clang/MSVC are not already defined:

// Before:
BSONCXX_PRIVATE_WARNINGS_DISABLE(GCC("-Wmaybe-uninitialized"));
// After:
BSONCXX_PRIVATE_WARNINGS_DISABLE_FOR_GCC("-Wmaybe-uninitialized");

This PR assumes the BSONCXX_PRIVATE_* macros are OK to rename in a non-major API release. The macros are in a public header, but documented as private (refer: API Versioning).

@kevinAlbs kevinAlbs marked this pull request as ready for review May 27, 2025 20:01
@kevinAlbs kevinAlbs requested a review from a team as a code owner May 27, 2025 20:01
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.

Was adding macro selection suffixes (Clang, GCC, GNU, and MSVC) to the list of guarded macros considered?

// #pragma push_macro("GCC")
// #pragma undef GCC
#include <bsoncxx/v1/detail/prelude.hpp>

... // Public header code.

// #pragma pop_macro("GCC")
#include <bsoncxx/v1/detail/postlude.hpp>

I do not think we reasonably expect any platforms or third party libraries to define any of these macros (as they are usually prefixed with at least one underscore _) in such a way that guarding them within our public headers would change library behavior. I would consider support for users building the C++ Driver libraries with such macros defined to be out-of-scope (they should be user-defining such macros in their code, not ours), but that doesn't seem to be the case for the referenced user report (diagnostic points to public header code which should be guarded).

@kevinAlbs kevinAlbs changed the title CXX-3296 fix build if GCC/Clang/MSVC macro defined CXX-3296 fix build if GCC/GNU/Clang/MSVC macro defined May 27, 2025
@kevinAlbs
Copy link
Collaborator Author

Was adding macro selection suffixes (Clang, GCC, GNU, and MSVC) to the list of guarded macros considered?

Thank you for the suggestion. That seems to fit the purpose of the macro guards and is a smaller change. Updated.

@kevinAlbs kevinAlbs requested a review from eramongodb May 28, 2025 15:22
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.

Minor suggestion; otherwise, LGTM.

@@ -139,9 +139,13 @@ if(ENABLE_MACRO_GUARD_TESTS)
BSONCXX_PRIVATE_STRINGIFY_IMPL
BSONCXX_PRIVATE_UNREACHABLE
BSONCXX_PRIVATE_WARNINGS_DISABLE
Clang
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggest keeping this list alphabetically sorted (+ consistent with the order of pragmas in macro guard headers).

Copy link
Contributor

@vector-of-bool vector-of-bool left a comment

Choose a reason for hiding this comment

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

LGTM

@kevinAlbs kevinAlbs merged commit b5264f2 into mongodb:master May 28, 2025
18 of 21 checks passed
kevinAlbs added a commit to kevinAlbs/mongo-cxx-driver that referenced this pull request May 28, 2025
kevinAlbs added a commit that referenced this pull request May 29, 2025
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.

3 participants