Export client-facing headers from json.hpp for IWYU / include-cleaner, and check clients in CI (#5813)

* Mark json_fwd.hpp as exported by json.hpp for IWYU / include-cleaner

json_fwd.hpp is the only header declaring the nlohmann::json alias, so
include-what-you-use and clang-tidy's misc-include-cleaner attributed
the name to it and told users of <nlohmann/json.hpp> to include
json_fwd.hpp as well (or instead, which breaks compilation).

Fixes #5812

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

* Export the remaining client-facing headers from json.hpp and check clients with IWYU

Besides json_fwd.hpp, include analyzers also told clients of
<nlohmann/json.hpp> to include detail/json_ref.hpp (brace
initialization), json_literals.hpp (_json, _json_pointer),
adl_serializer.hpp, detail/macro_scope.hpp (NLOHMANN_DEFINE_*) and
detail/abi_macros.hpp. Mark those includes as exported, too.

ci_single_binaries only checked each header on its own, so it could not
notice this. It now also runs IWYU and clang-tidy's misc-include-cleaner
on small client programs in tests/iwyu_consumer, which fail on develop.
With the exports, json_literals.hpp no longer gets the json_fwd.hpp
suggestion that kept it out of the IWYU gate, so it is gated again.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

---------

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann authored and GitHub committed 2026-10-13 17:17:15 +02:00
1 parent aa9620fb15
commit e839bbd1c0
6 files changed
+152 -19

No files matched your search

+11 -6
View File
@@ -42,6 +42,11 @@
// each of these, not only the forward declarations from json_fwd.hpp, so IWYU's suggestion to
// drop them (nothing in this file otherwise names the type) would break every downstream
// translation unit that relies on basic_json<>'s defaults actually being usable.
// export: client code names what these headers declare (nlohmann::json and ordered_json from
// json_fwd.hpp, adl_serializer for specializations, json_ref behind brace initialization, the
// NLOHMANN_DEFINE_* and NLOHMANN_JSON_NAMESPACE_* macros, and the literals at the end of this file), and include analyzers must
// attribute it to this header, not tell clients to include the internal ones (issue #5812).
// tests/iwyu_consumer checks this.
// #include <nlohmann/adl_serializer.hpp>
// __ _____ _____ _____
// __| | __| | | | JSON for Modern C++
@@ -7533,7 +7538,7 @@ struct adl_serializer
};
NLOHMANN_JSON_NAMESPACE_END
// IWYU pragma: keep
// IWYU pragma: export
// #include <nlohmann/byte_container_with_subtype.hpp>
// __ _____ _____ _____
// __| | __| | | | JSON for Modern C++
@@ -7641,7 +7646,7 @@ class byte_container_with_subtype : public BinaryType
NLOHMANN_JSON_NAMESPACE_END
// #include <nlohmann/detail/abi_macros.hpp>
// IWYU pragma: export
// #include <nlohmann/detail/conversions/from_json.hpp>
// IWYU pragma: keep
// #include <nlohmann/detail/conversions/to_json.hpp>
@@ -21573,9 +21578,9 @@ class json_ref
} // namespace detail
NLOHMANN_JSON_NAMESPACE_END
// IWYU pragma: export
// #include <nlohmann/detail/macro_scope.hpp>
// IWYU pragma: export
// #include <nlohmann/detail/meta/detected.hpp>
// #include <nlohmann/detail/string_concat.hpp>
@@ -27939,7 +27944,7 @@ NLOHMANN_JSON_NAMESPACE_END
// #include <nlohmann/detail/value_t.hpp>
// #include <nlohmann/json_fwd.hpp>
// IWYU pragma: export
// #include <nlohmann/ordered_map.hpp>
// __ _____ _____ _____
// __| | __| | | | JSON for Modern C++
@@ -36372,7 +36377,7 @@ NLOHMANN_JSON_NAMESPACE_END
#undef NLOHMANN_JSON_LITERAL_OPERATOR
#endif // INCLUDE_NLOHMANN_JSON_LITERALS_HPP_
// IWYU pragma: export
#endif
#endif // INCLUDE_NLOHMANN_JSON_HPP_