Skip to content

Fix weak-vtables warning #4500

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 7 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions cmake/ci.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ file(GLOB_RECURSE SRC_FILES ${PROJECT_SOURCE_DIR}/include/nlohmann/*.hpp)
# -Wno-padded We do not care about padding warnings.
# -Wno-covered-switch-default All switches list all cases and a default case.
# -Wno-unsafe-buffer-usage Otherwise Doctest would not compile.
# -Wno-weak-vtables The library is header-only.
# -Wreserved-identifier See https://github.com/onqtam/doctest/issues/536.

set(CLANG_CXXFLAGS
Expand All @@ -109,7 +108,6 @@ set(CLANG_CXXFLAGS
-Wno-padded
-Wno-covered-switch-default
-Wno-unsafe-buffer-usage
-Wno-weak-vtables
-Wno-reserved-identifier
)

Expand Down
16 changes: 16 additions & 0 deletions include/nlohmann/detail/exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@
#include <nlohmann/detail/meta/type_traits.hpp>
#include <nlohmann/detail/string_concat.hpp>

// With -Wweak-vtables, Clang will complain about the exception classes as they
// have no out-of-line virtual method definitions and their vtable will be
// emitted in every translation unit. This issue cannot be fixed with a
// header-only library as there is no implementation file to move these
// functions to. As a result, we suppress this warning here to avoid client
// code to stumble over this. See https://github.com/nlohmann/json/issues/4087
// for a discussion.
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wweak-vtables"
#endif

NLOHMANN_JSON_NAMESPACE_BEGIN
namespace detail
{
Expand Down Expand Up @@ -255,3 +267,7 @@ class other_error : public exception

} // namespace detail
NLOHMANN_JSON_NAMESPACE_END

#if defined(__clang__)
#pragma clang diagnostic pop
#endif
16 changes: 16 additions & 0 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4392,6 +4392,18 @@ inline OutStringType concat(Args && ... args)
NLOHMANN_JSON_NAMESPACE_END


// With -Wweak-vtables, Clang will complain about the exception classes as they
// have no out-of-line virtual method definitions and their vtable will be
// emitted in every translation unit. This issue cannot be fixed with a
// header-only library as there is no implementation file to move these
// functions to. As a result, we suppress this warning here to avoid client
// code to stumble over this. See https://github.com/nlohmann/json/issues/4087
// for a discussion.
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wweak-vtables"
#endif

NLOHMANN_JSON_NAMESPACE_BEGIN
namespace detail
{
Expand Down Expand Up @@ -4623,6 +4635,10 @@ class other_error : public exception
} // namespace detail
NLOHMANN_JSON_NAMESPACE_END

#if defined(__clang__)
#pragma clang diagnostic pop
#endif

// #include <nlohmann/detail/macro_scope.hpp>

// #include <nlohmann/detail/meta/cpp_future.hpp>
Expand Down
59 changes: 58 additions & 1 deletion tests/src/unit-regression2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,33 @@ inline void from_json(const nlohmann::json& j, FooBar& fb) // NOLINT(misc-use-in
struct for_3171_base // NOLINT(cppcoreguidelines-special-member-functions)
{
for_3171_base(const std::string& /*unused*/ = {}) {}
virtual ~for_3171_base() = default;
virtual ~for_3171_base();

for_3171_base(const for_3171_base& other) // NOLINT(hicpp-use-equals-default,modernize-use-equals-default)
: str(other.str)
{}

for_3171_base& operator=(const for_3171_base& other)
{
if (this != &other)
{
str = other.str;
}
return *this;
}

for_3171_base(for_3171_base&& other) noexcept
: str(std::move(other.str))
{}

for_3171_base& operator=(for_3171_base&& other) noexcept
{
if (this != &other)
{
str = std::move(other.str);
}
return *this;
}

virtual void _from_json(const json& j)
{
Expand All @@ -243,12 +269,43 @@ struct for_3171_base // NOLINT(cppcoreguidelines-special-member-functions)
std::string str{}; // NOLINT(readability-redundant-member-init)
};

for_3171_base::~for_3171_base() = default;

struct for_3171_derived : public for_3171_base
{
for_3171_derived() = default;
~for_3171_derived() override;
explicit for_3171_derived(const std::string& /*unused*/) { }

for_3171_derived(const for_3171_derived& other) // NOLINT(hicpp-use-equals-default,modernize-use-equals-default)
: for_3171_base(other)
{}

for_3171_derived& operator=(const for_3171_derived& other)
{
if (this != &other)
{
for_3171_base::operator=(other); // Call base class assignment operator
}
return *this;
}

for_3171_derived(for_3171_derived&& other) noexcept
: for_3171_base(std::move(other))
{}

for_3171_derived& operator=(for_3171_derived&& other) noexcept
{
if (this != &other)
{
for_3171_base::operator=(std::move(other)); // Call base class move assignment operator
}
return *this;
}
};

for_3171_derived::~for_3171_derived() = default;

inline void from_json(const json& j, for_3171_base& tb) // NOLINT(misc-use-internal-linkage)
{
tb._from_json(j);
Expand Down
Loading