Define and use BSONCXX_PRIVATE_INLINE_CXX17 for inline variables #1468
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Related to CXX-2745 and CXX-3320. This PR proposes defining a
BSONCXX_PRIVATE_INLINE_CXX17macro for use withinlinevariables to prevent their inclusion in the (stable) ABI. This also extends support for declaringconstexprvariables asinline(or equivalent) while fixing distinct behaviors across compilers, improving C++17 forward compatibility.Important
This changes in this PR remove
v1::types::b_*::type_idandv1::oid::k_oid_lengthfrom the (experimental) stable ABI.Note
The
BSONCXX_PRIVATE_INLINE_CXX17macro is for use with variables, not functions.Initially motivated by this GCC warning when compiling with certain configurations such as with
-fsanitize=address,undefinedon RHEL:This required moving the
inverted()function after the out-of-line definitions containing the[[gnu::weak]]attribute. This prompted further investigation into the potential use of[[gnu::weak]]withconstexprstatic data members. However, this effort was halted by this Clang issue:where Clang emits the following compilation error with C++ standards prior to C++17:
Clang does not support the declaration of a
constexprvariable as a "weak" symbol due to what Clang interprets as contradictory intent:constexpris "a value computable at compile-time", whereas "weak" is "a value potentially overridden at runtime". This is not the case for GCC or MSVC which appear to interpret the "potentially-overwritten" behavior as an allowance rather than explicit intent.To avoid including static data members in the stable ABI, this PR proposes making an initiative to move away from
[[gnu::weak]]with Clang and instead use its__inline__extension instead. As mentioned in theBSONCXX_PRIVATE_INLINE_CXX17documentation comment, this extension is supported by all relevant Clang compilers.Similarly, the macro definition is updated to use
inlinewith supported MSVC compiler versions instead of unconditionally using__declspec(selectany)... when it is correctly supported: versions prior to 19.20, aka before "Visual Studio 2019", produce "multiple definition" errors for inline static data members, but not for regular namespace-scope variables which appear to work as intended.Unfortunately, it seems the
strong_orderingdata members cannot be declaredconstexprdue to not being a literal type. However, given this is internal API, I believe the discrepancy from specification is acceptable so long as it is known to maintainers.