Fix CI failures of the unity test build

- keep JSON_DISABLE_ENUM_SERIALIZATION and
  JSON_DISABLE_TUPLE_REFERENCE_CONVERSION defined with
  JSON_TEST_KEEP_MACROS, so that every file of a batch sees them, not only
  the first one
- mark JSON_TESTS_PRIVATE in the generated batch file as used, as GCC and
  Clang report an unused definition when the first file defines it again
  (-Wunused-macros)
- suppress bugprone-suspicious-include for the #include-d .cpp files
- avoid local json aliases and variables that hide a global json of
  another file in the same batch (MSVC C4459)

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-10-09 23:23:04 +02:00
1 parent 993e37e7ec
commit 769c5cde57
5 files changed
+79 -82

No files matched your search

+4 -1
View File
@@ -290,9 +290,12 @@ function(_json_test_add_unity_batch batch_name cxx_standard main private)
if(private) if(private)
string(APPEND batch_content "// at least one file of this batch needs access to private members of the library\n") string(APPEND batch_content "// at least one file of this batch needs access to private members of the library\n")
string(APPEND batch_content "#define JSON_TESTS_PRIVATE\n") string(APPEND batch_content "#define JSON_TESTS_PRIVATE\n")
# the files define the macro again; mark this definition as used, as
# -Wunused-macros reports an unused definition when it is redefined
string(APPEND batch_content "#ifdef JSON_TESTS_PRIVATE\n#endif\n")
endif() endif()
foreach(file ${ARGN}) foreach(file ${ARGN})
string(APPEND batch_content "#include \"${file}\"\n") string(APPEND batch_content "#include \"${file}\" // NOLINT(bugprone-suspicious-include)\n")
endforeach() endforeach()
# only touch the generated file if it changed to keep incremental builds incremental # only touch the generated file if it changed to keep incremental builds incremental
+2 -2
View File
@@ -19,8 +19,6 @@
#undef NLOHMANN_CAN_CALL_STD_FUNC_IMPL #undef NLOHMANN_CAN_CALL_STD_FUNC_IMPL
#undef JSON_INLINE_VARIABLE #undef JSON_INLINE_VARIABLE
#undef JSON_NO_UNIQUE_ADDRESS #undef JSON_NO_UNIQUE_ADDRESS
#undef JSON_DISABLE_ENUM_SERIALIZATION
#undef JSON_DISABLE_TUPLE_REFERENCE_CONVERSION
#ifndef JSON_TEST_KEEP_MACROS #ifndef JSON_TEST_KEEP_MACROS
#undef JSON_CATCH #undef JSON_CATCH
@@ -45,6 +43,8 @@
#undef JSON_STRICT_BINARY_UTF8 #undef JSON_STRICT_BINARY_UTF8
#undef JSON_DELETE_DEPRECATED_FUNCTIONS #undef JSON_DELETE_DEPRECATED_FUNCTIONS
#undef JSON_USE_OBJECTS_FOR_ENUM_KEYED_MAPS #undef JSON_USE_OBJECTS_FOR_ENUM_KEYED_MAPS
#undef JSON_DISABLE_ENUM_SERIALIZATION
#undef JSON_DISABLE_TUPLE_REFERENCE_CONVERSION
#endif #endif
#include <nlohmann/thirdparty/hedley/hedley_undef.hpp> #include <nlohmann/thirdparty/hedley/hedley_undef.hpp>
+2 -2
View File
@@ -35304,8 +35304,6 @@ struct formatter<nlohmann::NLOHMANN_BASIC_JSON_TPL, char> // NOLINT(cert-dcl58-c
#undef NLOHMANN_CAN_CALL_STD_FUNC_IMPL #undef NLOHMANN_CAN_CALL_STD_FUNC_IMPL
#undef JSON_INLINE_VARIABLE #undef JSON_INLINE_VARIABLE
#undef JSON_NO_UNIQUE_ADDRESS #undef JSON_NO_UNIQUE_ADDRESS
#undef JSON_DISABLE_ENUM_SERIALIZATION
#undef JSON_DISABLE_TUPLE_REFERENCE_CONVERSION
#ifndef JSON_TEST_KEEP_MACROS #ifndef JSON_TEST_KEEP_MACROS
#undef JSON_CATCH #undef JSON_CATCH
@@ -35330,6 +35328,8 @@ struct formatter<nlohmann::NLOHMANN_BASIC_JSON_TPL, char> // NOLINT(cert-dcl58-c
#undef JSON_STRICT_BINARY_UTF8 #undef JSON_STRICT_BINARY_UTF8
#undef JSON_DELETE_DEPRECATED_FUNCTIONS #undef JSON_DELETE_DEPRECATED_FUNCTIONS
#undef JSON_USE_OBJECTS_FOR_ENUM_KEYED_MAPS #undef JSON_USE_OBJECTS_FOR_ENUM_KEYED_MAPS
#undef JSON_DISABLE_ENUM_SERIALIZATION
#undef JSON_DISABLE_TUPLE_REFERENCE_CONVERSION
#endif #endif
// #include <nlohmann/thirdparty/hedley/hedley_undef.hpp> // #include <nlohmann/thirdparty/hedley/hedley_undef.hpp>
+62 -62
View File
@@ -44,10 +44,10 @@ TEST_CASE("JSON Node Metadata")
{ {
SECTION("type int") SECTION("type int")
{ {
using json = json_with_metadata<int>; using json_t = json_with_metadata<int>;
json null; json_t null;
auto obj = json::object(); auto obj = json_t::object();
auto array = json::array(); auto array = json_t::array();
null.metadata() = 1; null.metadata() = 1;
obj.metadata() = 2; obj.metadata() = 2;
@@ -61,8 +61,8 @@ TEST_CASE("JSON Node Metadata")
} }
SECTION("type vector<int>") SECTION("type vector<int>")
{ {
using json = json_with_metadata<std::vector<int>>; using json_t = json_with_metadata<std::vector<int>>;
json value; json_t value;
value.metadata().emplace_back(1); value.metadata().emplace_back(1);
auto copy = value; auto copy = value;
value.metadata().emplace_back(2); value.metadata().emplace_back(2);
@@ -75,12 +75,12 @@ TEST_CASE("JSON Node Metadata")
} }
SECTION("copy ctor") SECTION("copy ctor")
{ {
using json = json_with_metadata<std::vector<int>>; using json_t = json_with_metadata<std::vector<int>>;
json value; json_t value;
value.metadata().emplace_back(1); value.metadata().emplace_back(1);
value.metadata().emplace_back(2); value.metadata().emplace_back(2);
json copy = value; json_t copy = value;
CHECK(copy.metadata().size() == 2); CHECK(copy.metadata().size() == 2);
CHECK(copy.metadata().at(0) == 1); CHECK(copy.metadata().at(0) == 1);
@@ -95,12 +95,12 @@ TEST_CASE("JSON Node Metadata")
} }
SECTION("move ctor") SECTION("move ctor")
{ {
using json = json_with_metadata<std::vector<int>>; using json_t = json_with_metadata<std::vector<int>>;
json value; json_t value;
value.metadata().emplace_back(1); value.metadata().emplace_back(1);
value.metadata().emplace_back(2); value.metadata().emplace_back(2);
const json moved = std::move(value); const json_t moved = std::move(value);
CHECK(moved.metadata().size() == 2); CHECK(moved.metadata().size() == 2);
CHECK(moved.metadata().at(0) == 1); CHECK(moved.metadata().at(0) == 1);
@@ -108,12 +108,12 @@ TEST_CASE("JSON Node Metadata")
} }
SECTION("move assign") SECTION("move assign")
{ {
using json = json_with_metadata<std::vector<int>>; using json_t = json_with_metadata<std::vector<int>>;
json value; json_t value;
value.metadata().emplace_back(1); value.metadata().emplace_back(1);
value.metadata().emplace_back(2); value.metadata().emplace_back(2);
json moved; json_t moved;
moved = std::move(value); moved = std::move(value);
CHECK(moved.metadata().size() == 2); CHECK(moved.metadata().size() == 2);
@@ -122,12 +122,12 @@ TEST_CASE("JSON Node Metadata")
} }
SECTION("copy assign") SECTION("copy assign")
{ {
using json = json_with_metadata<std::vector<int>>; using json_t = json_with_metadata<std::vector<int>>;
json value; json_t value;
value.metadata().emplace_back(1); value.metadata().emplace_back(1);
value.metadata().emplace_back(2); value.metadata().emplace_back(2);
json copy; json_t copy;
copy = value; copy = value;
CHECK(copy.metadata().size() == 2); CHECK(copy.metadata().size() == 2);
@@ -143,8 +143,8 @@ TEST_CASE("JSON Node Metadata")
} }
SECTION("type unique_ptr<int>") SECTION("type unique_ptr<int>")
{ {
using json = json_with_metadata<std::unique_ptr<int>>; using json_t = json_with_metadata<std::unique_ptr<int>>;
json value; json_t value;
value.metadata().reset(new int(42)); // NOLINT(cppcoreguidelines-owning-memory) value.metadata().reset(new int(42)); // NOLINT(cppcoreguidelines-owning-memory)
auto moved = std::move(value); auto moved = std::move(value);
@@ -153,12 +153,12 @@ TEST_CASE("JSON Node Metadata")
} }
SECTION("type vector<int> in json array") SECTION("type vector<int> in json array")
{ {
using json = json_with_metadata<std::vector<int>>; using json_t = json_with_metadata<std::vector<int>>;
json value; json_t value;
value.metadata().emplace_back(1); value.metadata().emplace_back(1);
value.metadata().emplace_back(2); value.metadata().emplace_back(2);
json const array(10, value); json_t const array(10, value);
CHECK(value.metadata().size() == 2); CHECK(value.metadata().size() == 2);
CHECK(value.metadata().at(0) == 1); CHECK(value.metadata().at(0) == 1);
@@ -173,10 +173,10 @@ TEST_CASE("JSON Node Metadata")
} }
SECTION("member swap") SECTION("member swap")
{ {
using json = json_with_metadata<int>; using json_t = json_with_metadata<int>;
json a = 1; json_t a = 1;
a.metadata() = 100; a.metadata() = 100;
json b = 2; json_t b = 2;
b.metadata() = 200; b.metadata() = 200;
a.swap(b); a.swap(b);
@@ -188,10 +188,10 @@ TEST_CASE("JSON Node Metadata")
} }
SECTION("nonmember swap") SECTION("nonmember swap")
{ {
using json = json_with_metadata<int>; using json_t = json_with_metadata<int>;
json a = 1; json_t a = 1;
a.metadata() = 100; a.metadata() = 100;
json b = 2; json_t b = 2;
b.metadata() = 200; b.metadata() = 200;
using std::swap; using std::swap;
@@ -204,10 +204,10 @@ TEST_CASE("JSON Node Metadata")
} }
SECTION("std::swap") SECTION("std::swap")
{ {
using json = json_with_metadata<int>; using json_t = json_with_metadata<int>;
json a = 1; json_t a = 1;
a.metadata() = 100; a.metadata() = 100;
json b = 2; json_t b = 2;
b.metadata() = 200; b.metadata() = 200;
std::swap(a, b); std::swap(a, b);
@@ -221,15 +221,15 @@ TEST_CASE("JSON Node Metadata")
{ {
// std::sort mixes swap() with moves; each value's metadata must // std::sort mixes swap() with moves; each value's metadata must
// travel with it, just as it does for copy, move, and assignment // travel with it, just as it does for copy, move, and assignment
using json = json_with_metadata<int>; using json_t = json_with_metadata<int>;
std::vector<json> values; std::vector<json_t> values;
for (const int v : for (const int v :
{ {
5, 3, 9, 1, 7, 2, 8, 4, 6, 0, 15, 13, 19, 11, 17, 12, 18, 14, 16, 10, 5, 3, 9, 1, 7, 2, 8, 4, 6, 0, 15, 13, 19, 11, 17, 12, 18, 14, 16, 10,
25, 23, 29, 21, 27, 22, 28, 24, 26, 20, 35, 33 25, 23, 29, 21, 27, 22, 28, 24, 26, 20, 35, 33
}) })
{ {
json value = v; json_t value = v;
value.metadata() = v; value.metadata() = v;
values.push_back(value); values.push_back(value);
} }
@@ -267,19 +267,19 @@ template <class Ptr, class Fnc>
void visitor_adaptor::do_visit(const Ptr& ptr, const Fnc& fnc) const void visitor_adaptor::do_visit(const Ptr& ptr, const Fnc& fnc) const
{ {
using value_t = nlohmann::detail::value_t; using value_t = nlohmann::detail::value_t;
const json_with_visitor_t& json = *static_cast<const json_with_visitor_t*>(this); // NOLINT(cppcoreguidelines-pro-type-static-cast-downcast) const json_with_visitor_t& self = *static_cast<const json_with_visitor_t*>(this); // NOLINT(cppcoreguidelines-pro-type-static-cast-downcast)
switch (json.type()) switch (self.type())
{ {
case value_t::object: case value_t::object:
for (const auto& entry : json.items()) for (const auto& entry : self.items())
{ {
entry.value().do_visit(ptr / entry.key(), fnc); entry.value().do_visit(ptr / entry.key(), fnc);
} }
break; break;
case value_t::array: case value_t::array:
for (std::size_t i = 0; i < json.size(); ++i) for (std::size_t i = 0; i < self.size(); ++i)
{ {
json.at(i).do_visit(ptr / std::to_string(i), fnc); self.at(i).do_visit(ptr / std::to_string(i), fnc);
} }
break; break;
case value_t::discarded: case value_t::discarded:
@@ -292,22 +292,22 @@ void visitor_adaptor::do_visit(const Ptr& ptr, const Fnc& fnc) const
case value_t::number_float: case value_t::number_float:
case value_t::binary: case value_t::binary:
default: default:
fnc(ptr, json); fnc(ptr, self);
} }
} }
TEST_CASE("JSON Visit Node") TEST_CASE("JSON Visit Node")
{ {
json_with_visitor_t json; json_with_visitor_t root;
json["null"]; root["null"];
json["int"] = -1; root["int"] = -1;
json["uint"] = 1U; root["uint"] = 1U;
json["float"] = 1.0; root["float"] = 1.0;
json["boolean"] = true; root["boolean"] = true;
json["string"] = "string"; root["string"] = "string";
json["array"].push_back(0); root["array"].push_back(0);
json["array"].push_back(1); root["array"].push_back(1);
json["array"].push_back(json); root["array"].push_back(root);
std::set<std::string> expected std::set<std::string> expected
{ {
@@ -330,7 +330,7 @@ TEST_CASE("JSON Visit Node")
"/array/2/array/1 - number_integer - 1" "/array/2/array/1 - number_integer - 1"
}; };
json.visit( root.visit(
[&](const json_with_visitor_t::json_pointer & p, [&](const json_with_visitor_t::json_pointer & p,
const json_with_visitor_t& j) const json_with_visitor_t& j)
{ {
@@ -374,7 +374,7 @@ TEST_CASE("JSON Visit Node")
break; break;
} }
str << " - " << j.dump(); str << " - " << j.dump();
CHECK(json.at(p) == j); CHECK(root.at(p) == j);
INFO(str.str()); INFO(str.str());
CHECK(expected.count(str.str()) == 1); CHECK(expected.count(str.str()) == 1);
expected.erase(str.str()); expected.erase(str.str());
@@ -417,22 +417,22 @@ using json_with_hidden_base_members =
TEST_CASE("JSON Node as_base_class") TEST_CASE("JSON Node as_base_class")
{ {
using json = json_with_hidden_base_members; using json_t = json_with_hidden_base_members;
static_assert(std::is_same<decltype(std::declval<json&>().as_base_class()), json::json_base_class_t&>::value, ""); static_assert(std::is_same<decltype(std::declval<json_t&>().as_base_class()), json_t::json_base_class_t&>::value, "");
static_assert(std::is_same<decltype(std::declval<const json&>().as_base_class()), const json::json_base_class_t&>::value, ""); static_assert(std::is_same<decltype(std::declval<const json_t&>().as_base_class()), const json_t::json_base_class_t&>::value, "");
static_assert(noexcept(std::declval<json&>().as_base_class()), ""); static_assert(noexcept(std::declval<json_t&>().as_base_class()), "");
static_assert(noexcept(std::declval<const json&>().as_base_class()), ""); static_assert(noexcept(std::declval<const json_t&>().as_base_class()), "");
SECTION("non-const") SECTION("non-const")
{ {
json j = {1, 2, 3}; json_t j = {1, 2, 3};
CHECK(std::string(j.type_name()) == "array"); CHECK(std::string(j.type_name()) == "array");
CHECK(j.size() == 3); CHECK(j.size() == 3);
CHECK(std::string(j.as_base_class().type_name()) == "custom type_name"); CHECK(std::string(j.as_base_class().type_name()) == "custom type_name");
CHECK(j.as_base_class().size() == 42); CHECK(j.as_base_class().size() == 42);
CHECK(&j.as_base_class() == &static_cast<json::json_base_class_t&>(j)); CHECK(&j.as_base_class() == &static_cast<json_t::json_base_class_t&>(j));
j.as_base_class().m_size = 7; j.as_base_class().m_size = 7;
CHECK(j.as_base_class().size() == 7); CHECK(j.as_base_class().size() == 7);
@@ -441,13 +441,13 @@ TEST_CASE("JSON Node as_base_class")
SECTION("const") SECTION("const")
{ {
const json j = {1, 2, 3}; const json_t j = {1, 2, 3};
CHECK(std::string(j.type_name()) == "array"); CHECK(std::string(j.type_name()) == "array");
CHECK(j.size() == 3); CHECK(j.size() == 3);
CHECK(std::string(j.as_base_class().type_name()) == "custom type_name"); CHECK(std::string(j.as_base_class().type_name()) == "custom type_name");
CHECK(j.as_base_class().size() == 42); CHECK(j.as_base_class().size() == 42);
CHECK(&j.as_base_class() == &static_cast<const json::json_base_class_t&>(j)); CHECK(&j.as_base_class() == &static_cast<const json_t::json_base_class_t&>(j));
} }
} }
+9 -15
View File
@@ -876,8 +876,6 @@ TEST_CASE_TEMPLATE("element access 2 (additional value() tests) (C++17)", Json,
TEST_CASE("operator[] with user-defined std::string_view-convertible types") TEST_CASE("operator[] with user-defined std::string_view-convertible types")
{ {
using json = nlohmann::json;
class TestClass class TestClass
{ {
std::string key_data_ = "foo"; std::string key_data_ = "foo";
@@ -897,7 +895,7 @@ TEST_CASE("operator[] with user-defined std::string_view-convertible types")
} }
}; };
json j = {{"foo", "from_class"}, {"bar", "from_struct"}}; nlohmann::json j = {{"foo", "from_class"}, {"bar", "from_struct"}};
const TestClass foo_obj; const TestClass foo_obj;
const TestStruct bar_obj; const TestStruct bar_obj;
@@ -959,12 +957,10 @@ TEST_CASE("keys convertible to std::string_view work with all lookup functions (
SECTION("nlohmann::json") SECTION("nlohmann::json")
{ {
using json = nlohmann::json;
SECTION("ViewKey") SECTION("ViewKey")
{ {
json j = {{"a", 1}}; nlohmann::json j = {{"a", 1}};
const json& cj = j; const nlohmann::json& cj = j;
CHECK(j[ViewKey{}] == 1); CHECK(j[ViewKey{}] == 1);
CHECK(cj[ViewKey{}] == 1); CHECK(cj[ViewKey{}] == 1);
@@ -981,8 +977,8 @@ TEST_CASE("keys convertible to std::string_view work with all lookup functions (
SECTION("DualKey") SECTION("DualKey")
{ {
json j = {{"a", 1}}; nlohmann::json j = {{"a", 1}};
const json& cj = j; const nlohmann::json& cj = j;
CHECK(j[DualKey{}] == 1); CHECK(j[DualKey{}] == 1);
CHECK(cj[DualKey{}] == 1); CHECK(cj[DualKey{}] == 1);
@@ -1000,12 +996,10 @@ TEST_CASE("keys convertible to std::string_view work with all lookup functions (
SECTION("nlohmann::ordered_json") SECTION("nlohmann::ordered_json")
{ {
using ordered_json = nlohmann::ordered_json;
SECTION("ViewKey") SECTION("ViewKey")
{ {
ordered_json j = {{"a", 1}}; nlohmann::ordered_json j = {{"a", 1}};
const ordered_json& cj = j; const nlohmann::ordered_json& cj = j;
CHECK(j[ViewKey{}] == 1); CHECK(j[ViewKey{}] == 1);
CHECK(cj[ViewKey{}] == 1); CHECK(cj[ViewKey{}] == 1);
@@ -1022,8 +1016,8 @@ TEST_CASE("keys convertible to std::string_view work with all lookup functions (
SECTION("DualKey") SECTION("DualKey")
{ {
ordered_json j = {{"a", 1}}; nlohmann::ordered_json j = {{"a", 1}};
const ordered_json& cj = j; const nlohmann::ordered_json& cj = j;
CHECK(j[DualKey{}] == 1); CHECK(j[DualKey{}] == 1);
CHECK(cj[DualKey{}] == 1); CHECK(cj[DualKey{}] == 1);