Skip to content

Commit b0810e8

Browse files
authored
Use private virtual key_function() to address -Wweak-vtables (#1487)
1 parent 87b54b0 commit b0810e8

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

src/bsoncxx/include/bsoncxx/v1/exception.hpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,10 @@ BSONCXX_PRIVATE_WARNINGS_DISABLE(MSVC(4275));
9797
///
9898
class exception : public std::system_error {
9999
public:
100-
~exception() override;
101-
102-
exception(exception&&) noexcept = default;
103-
exception& operator=(exception&&) noexcept = default;
104-
exception(exception const&) = default;
105-
exception& operator=(exception const&) = default;
106-
107100
using std::system_error::system_error;
101+
102+
private:
103+
BSONCXX_ABI_NO_EXPORT virtual void key_function() const;
108104
};
109105

110106
BSONCXX_PRIVATE_WARNINGS_POP();

src/bsoncxx/lib/bsoncxx/v1/exception.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,14 @@ std::error_category const& type_error_category() {
8585
namespace bsoncxx {
8686
namespace v1 {
8787

88-
exception::~exception() = default;
88+
// Prevent vague linkage of the vtable and type_info object (-Wweak-vtables).
89+
// - https://itanium-cxx-abi.github.io/cxx-abi/abi.html#vague-vtable
90+
// > The key function is the first non-pure virtual function that is not inline at the point of class definition.
91+
// - https://lld.llvm.org/missingkeyfunction:
92+
// > It’s always advisable to ensure there is at least one eligible function that can serve as the key function.
93+
// - https://gcc.gnu.org/onlinedocs/gcc/Vague-Linkage.html
94+
// > For polymorphic classes (classes with virtual functions), the ‘type_info’ object is written out along with the vtable.
95+
void exception::key_function() const {}
8996

9097
} // namespace v1
9198
} // namespace bsoncxx

src/mongocxx/include/mongocxx/v1/exception.hpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,10 @@ BSONCXX_PRIVATE_WARNINGS_DISABLE(MSVC(4275));
100100
///
101101
class exception : public std::system_error {
102102
public:
103-
~exception() override;
104-
105-
exception(exception&&) noexcept = default;
106-
exception& operator=(exception&&) noexcept = default;
107-
exception(exception const&) = default;
108-
exception& operator=(exception const&) = default;
109-
110103
using std::system_error::system_error;
104+
105+
private:
106+
MONGOCXX_ABI_NO_EXPORT virtual void key_function() const;
111107
};
112108

113109
BSONCXX_PRIVATE_WARNINGS_POP();

src/mongocxx/lib/mongocxx/v1/exception.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,14 @@ std::error_category const& type_error_category() {
8383
return instance.value();
8484
}
8585

86-
exception::~exception() = default;
86+
// Prevent vague linkage of the vtable and type_info object (-Wweak-vtables).
87+
// - https://itanium-cxx-abi.github.io/cxx-abi/abi.html#vague-vtable
88+
// > The key function is the first non-pure virtual function that is not inline at the point of class definition.
89+
// - https://lld.llvm.org/missingkeyfunction:
90+
// > It’s always advisable to ensure there is at least one eligible function that can serve as the key function.
91+
// - https://gcc.gnu.org/onlinedocs/gcc/Vague-Linkage.html
92+
// > For polymorphic classes (classes with virtual functions), the ‘type_info’ object is written out along with the vtable.
93+
void exception::key_function() const {}
8794

8895
} // namespace v1
8996
} // namespace mongocxx

0 commit comments

Comments
 (0)