Skip to content
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

Add C++17 copies of the test binaries #3101

Merged
merged 40 commits into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
879217d
:alembic: add C++17 copies of the test binaries
nlohmann Oct 20, 2021
cf2a896
:alembic: add C++17 copies of the test binaries
nlohmann Oct 20, 2021
7fb709c
:alembic: add C++17 copies of the test binaries
nlohmann Oct 20, 2021
d90b6bc
:alembic: use proper header for filesystem
nlohmann Oct 20, 2021
03a75d5
:rotating_light: fix warnings
nlohmann Oct 20, 2021
c33bac8
:rewind: revert removal of code
nlohmann Oct 20, 2021
930e2cf
:alembic: use proper header for filesystem
nlohmann Oct 20, 2021
5cd67e7
:alembic: use proper header for filesystem
nlohmann Oct 20, 2021
12bb888
:alembic: do not use too old compilers with C++17
nlohmann Oct 20, 2021
2b431c2
:alembic: do not use too old compilers with C++17
nlohmann Oct 20, 2021
7543e17
:alembic: do not use too old compilers with C++17
nlohmann Oct 20, 2021
7e87863
:rotating_light: fix warning
nlohmann Oct 20, 2021
4c10cca
:hammer: cleanup
nlohmann Oct 20, 2021
ccd42cd
:rewind: undo cleanup
nlohmann Oct 20, 2021
d300856
:white_check_mark: add test
nlohmann Oct 21, 2021
37be287
:hammer: add more constraints #3097
nlohmann Oct 21, 2021
61829d7
:green_heart: remove leftover
nlohmann Oct 21, 2021
fed4114
:green_heart: replace "and" with "&&"
nlohmann Oct 21, 2021
cf6903e
Merge branch 'develop' of https://github.com/nlohmann/json into issue…
nlohmann Nov 21, 2021
19f75a2
:rotating_light: suppress modernize-concat-nested-namespaces warning
nlohmann Nov 21, 2021
7566b3b
Merge branch 'develop' of https://github.com/nlohmann/json into issue…
nlohmann Dec 23, 2021
62fe919
:alembic: use fix from https://github.com/nlohmann/json/pull/3101#iss…
nlohmann Dec 23, 2021
30ab9c4
:rotating_light: fix warnings
nlohmann Dec 24, 2021
1ca9510
:rotating_light: fix warnings
nlohmann Dec 24, 2021
742d85f
:rotating_light: fix warnings
nlohmann Dec 24, 2021
05874dc
:rotating_light: fix warnings
nlohmann Dec 24, 2021
69f9c41
:rotating_light: fix warnings
nlohmann Dec 24, 2021
78baaa9
:rotating_light: fix warnings
nlohmann Dec 24, 2021
4bf052a
:green_heart: fix script
nlohmann Dec 24, 2021
6e6a82f
:rotating_light: fix warnings
nlohmann Dec 24, 2021
7001266
:rotating_light: fix warnings
nlohmann Dec 24, 2021
8372e03
:rotating_light: fix warnings
nlohmann Dec 24, 2021
c4aecea
:alembic: use fix from https://gcc.gnu.org/bugzilla/show_bug.cgi?id=9…
nlohmann Dec 28, 2021
0c27b7c
:alembic: use fix from https://gcc.gnu.org/bugzilla/show_bug.cgi?id=9…
nlohmann Dec 28, 2021
5e12e56
:alembic: use fix from https://gcc.gnu.org/bugzilla/show_bug.cgi?id=9…
nlohmann Dec 28, 2021
d5a95e4
:alembic: use fix from https://gcc.gnu.org/bugzilla/show_bug.cgi?id=9…
nlohmann Dec 28, 2021
bd3270a
:alembic: use fix from https://gcc.gnu.org/bugzilla/show_bug.cgi?id=9…
nlohmann Dec 28, 2021
41a3b33
:alembic: use fix from https://gcc.gnu.org/bugzilla/show_bug.cgi?id=9…
nlohmann Dec 28, 2021
62c19f2
:alembic: use fix from https://gcc.gnu.org/bugzilla/show_bug.cgi?id=9…
nlohmann Dec 28, 2021
eba77cd
:construction_worker: use published CI image
nlohmann Dec 28, 2021
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
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Checks: '*,
-llvmlibc-*,
-misc-no-recursion,
-misc-non-private-member-variables-in-classes,
-modernize-use-nodiscard,
-modernize-use-trailing-return-type,
-readability-function-size,
-readability-function-cognitive-complexity,
Expand Down
7 changes: 7 additions & 0 deletions doc/mkdocs/docs/features/macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ The diagnostics messages can also be controlled with the CMake option `JSON_Diag

The library targets C++11, but also supports some features introduced in later C++ versions (e.g., `std::string_view` support for C++17). For these new features, the library implements some preprocessor checks to determine the C++ standard. By defining any of these symbols, the internal check is overridden and the provided C++ version is unconditionally assumed. This can be helpful for compilers that only implement parts of the standard and would be detected incorrectly.

## `JSON_HAS_FILESYSTEM`, `JSON_HAS_EXPERIMENTAL_FILESYSTEM`

When compiling with C++17, the library provides conversions from and to `std::filesystem::path`. As compiler support
for filesystem is limited, the library tries to detect whether `<filesystem>`/`std::filesystem` (`JSON_HAS_FILESYSTEM`)
or `<experimental/filesystem>`/`std::experimental::filesystem` (`JSON_HAS_EXPERIMENTAL_FILESYSTEM`) should be used.
To override the built-in check, define `JSON_HAS_FILESYSTEM` or `JSON_HAS_EXPERIMENTAL_FILESYSTEM` to `1`.

## `JSON_NOEXCEPTION`

Exceptions can be switched off by defining the symbol `JSON_NOEXCEPTION`.
Expand Down
18 changes: 14 additions & 4 deletions include/nlohmann/detail/conversions/from_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,18 @@
#include <nlohmann/detail/meta/type_traits.hpp>
#include <nlohmann/detail/value_t.hpp>

#ifdef JSON_HAS_CPP_17
#include <filesystem>
#if JSON_HAS_EXPERIMENTAL_FILESYSTEM
#include <experimental/filesystem>
namespace nlohmann::detail
{
namespace std_fs = std::experimental::filesystem;
} // namespace nlohmann::detail
#elif JSON_HAS_FILESYSTEM
#include <filesystem>
namespace nlohmann::detail
{
namespace std_fs = std::filesystem;
} // namespace nlohmann::detail
#endif

namespace nlohmann
Expand Down Expand Up @@ -448,9 +458,9 @@ void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyE
}
}

#ifdef JSON_HAS_CPP_17
#if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
template<typename BasicJsonType>
void from_json(const BasicJsonType& j, std::filesystem::path& p)
void from_json(const BasicJsonType& j, std_fs::path& p)
{
if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
{
Expand Down
18 changes: 14 additions & 4 deletions include/nlohmann/detail/conversions/to_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,18 @@
#include <nlohmann/detail/meta/type_traits.hpp>
#include <nlohmann/detail/value_t.hpp>

#ifdef JSON_HAS_CPP_17
#include <filesystem>
#if JSON_HAS_EXPERIMENTAL_FILESYSTEM
#include <experimental/filesystem>
namespace nlohmann::detail
{
namespace std_fs = std::experimental::filesystem;
} // namespace nlohmann::detail
#elif JSON_HAS_FILESYSTEM
#include <filesystem>
namespace nlohmann::detail
{
namespace std_fs = std::filesystem;
} // namespace nlohmann::detail
#endif

namespace nlohmann
Expand Down Expand Up @@ -391,9 +401,9 @@ void to_json(BasicJsonType& j, const T& t)
to_json_tuple_impl(j, t, make_index_sequence<std::tuple_size<T>::value> {});
}

#ifdef JSON_HAS_CPP_17
#if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
template<typename BasicJsonType>
void to_json(BasicJsonType& j, const std::filesystem::path& p)
void to_json(BasicJsonType& j, const std_fs::path& p)
{
j = p.string();
}
Expand Down
60 changes: 60 additions & 0 deletions include/nlohmann/detail/macro_scope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,66 @@
#define JSON_HAS_CPP_11
#endif

#if !defined(JSON_HAS_FILESYSTEM) && !defined(JSON_HAS_EXPERIMENTAL_FILESYSTEM)
#ifdef JSON_HAS_CPP_17
#if defined(__cpp_lib_filesystem)
#define JSON_HAS_FILESYSTEM 1
#elif defined(__cpp_lib_experimental_filesystem)
#define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
#elif !defined(__has_include)
#define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
#elif __has_include(<filesystem>)
#define JSON_HAS_FILESYSTEM 1
#elif __has_include(<experimental/filesystem>)
#define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
#endif

// std::filesystem does not work on MinGW GCC 8: https://sourceforge.net/p/mingw-w64/bugs/737/
#if defined(__MINGW32__) && defined(__GNUC__) && __GNUC__ == 8
#undef JSON_HAS_FILESYSTEM
#undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
#endif

// no filesystem support before GCC 8: https://en.cppreference.com/w/cpp/compiler_support
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 8
#undef JSON_HAS_FILESYSTEM
#undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
#endif

// no filesystem support before Clang 7: https://en.cppreference.com/w/cpp/compiler_support
#if defined(__clang_major__) && __clang_major__ < 7
#undef JSON_HAS_FILESYSTEM
#undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
#endif

// no filesystem support before MSVC 19.14: https://en.cppreference.com/w/cpp/compiler_support
#if defined(_MSC_VER) && _MSC_VER < 1940
#undef JSON_HAS_FILESYSTEM
#undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
#endif

// no filesystem support before iOS 13
#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED < 130000
#undef JSON_HAS_FILESYSTEM
#undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
#endif

// no filesystem support before macOS Catalina
#if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < 101500
#undef JSON_HAS_FILESYSTEM
#undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
#endif
#endif
#endif

#ifndef JSON_HAS_EXPERIMENTAL_FILESYSTEM
#define JSON_HAS_EXPERIMENTAL_FILESYSTEM 0
#endif

#ifndef JSON_HAS_FILESYSTEM
#define JSON_HAS_FILESYSTEM 0
#endif

// disable documentation warnings on clang
#if defined(__clang__)
#pragma clang diagnostic push
Expand Down
2 changes: 2 additions & 0 deletions include/nlohmann/detail/macro_unscope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#undef JSON_HAS_CPP_14
#undef JSON_HAS_CPP_17
#undef JSON_HAS_CPP_20
#undef JSON_HAS_FILESYSTEM
#undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
#undef NLOHMANN_BASIC_JSON_TPL_DECLARATION
#undef NLOHMANN_BASIC_JSON_TPL
#undef JSON_EXPLICIT
Expand Down
2 changes: 1 addition & 1 deletion include/nlohmann/detail/meta/cpp_future.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ struct static_const
};

template<typename T>
constexpr T static_const<T>::value;
constexpr T static_const<T>::value; // NOLINT(readability-redundant-declaration)

} // namespace detail
} // namespace nlohmann
100 changes: 91 additions & 9 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2325,6 +2325,66 @@ using is_detected_convertible =
#define JSON_HAS_CPP_11
#endif

#if !defined(JSON_HAS_FILESYSTEM) && !defined(JSON_HAS_EXPERIMENTAL_FILESYSTEM)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we have just #include "nlohmann/detail/macro_scope.hpp" here?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the amalgamated header - it includes all individual headers.

#ifdef JSON_HAS_CPP_17
#if defined(__cpp_lib_filesystem)
#define JSON_HAS_FILESYSTEM 1
#elif defined(__cpp_lib_experimental_filesystem)
#define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
#elif !defined(__has_include)
#define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
#elif __has_include(<filesystem>)
#define JSON_HAS_FILESYSTEM 1
#elif __has_include(<experimental/filesystem>)
#define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
#endif

// std::filesystem does not work on MinGW GCC 8: https://sourceforge.net/p/mingw-w64/bugs/737/
#if defined(__MINGW32__) && defined(__GNUC__) && __GNUC__ == 8
#undef JSON_HAS_FILESYSTEM
#undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
#endif

// no filesystem support before GCC 8: https://en.cppreference.com/w/cpp/compiler_support
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 8
#undef JSON_HAS_FILESYSTEM
#undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
#endif

// no filesystem support before Clang 7: https://en.cppreference.com/w/cpp/compiler_support
#if defined(__clang_major__) && __clang_major__ < 7
#undef JSON_HAS_FILESYSTEM
#undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
#endif

// no filesystem support before MSVC 19.14: https://en.cppreference.com/w/cpp/compiler_support
#if defined(_MSC_VER) && _MSC_VER < 1940
#undef JSON_HAS_FILESYSTEM
#undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
#endif

// no filesystem support before iOS 13
#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED < 130000
#undef JSON_HAS_FILESYSTEM
#undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
#endif

// no filesystem support before macOS Catalina
#if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < 101500
#undef JSON_HAS_FILESYSTEM
#undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
#endif
#endif
#endif

#ifndef JSON_HAS_EXPERIMENTAL_FILESYSTEM
#define JSON_HAS_EXPERIMENTAL_FILESYSTEM 0
#endif

#ifndef JSON_HAS_FILESYSTEM
#define JSON_HAS_FILESYSTEM 0
#endif

// disable documentation warnings on clang
#if defined(__clang__)
#pragma clang diagnostic push
Expand Down Expand Up @@ -3298,7 +3358,7 @@ struct static_const
};

template<typename T>
constexpr T static_const<T>::value;
constexpr T static_const<T>::value; // NOLINT(readability-redundant-declaration)

} // namespace detail
} // namespace nlohmann
Expand Down Expand Up @@ -3950,8 +4010,18 @@ T conditional_static_cast(U value)
// #include <nlohmann/detail/value_t.hpp>


#ifdef JSON_HAS_CPP_17
#include <filesystem>
#if JSON_HAS_EXPERIMENTAL_FILESYSTEM
#include <experimental/filesystem>
namespace nlohmann::detail
{
namespace std_fs = std::experimental::filesystem;
} // namespace nlohmann::detail
#elif JSON_HAS_FILESYSTEM
#include <filesystem>
namespace nlohmann::detail
{
namespace std_fs = std::filesystem;
} // namespace nlohmann::detail
#endif

namespace nlohmann
Expand Down Expand Up @@ -4379,9 +4449,9 @@ void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyE
}
}

#ifdef JSON_HAS_CPP_17
#if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
template<typename BasicJsonType>
void from_json(const BasicJsonType& j, std::filesystem::path& p)
void from_json(const BasicJsonType& j, std_fs::path& p)
{
if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
{
Expand Down Expand Up @@ -4626,8 +4696,18 @@ class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >>
// #include <nlohmann/detail/value_t.hpp>


#ifdef JSON_HAS_CPP_17
#include <filesystem>
#if JSON_HAS_EXPERIMENTAL_FILESYSTEM
#include <experimental/filesystem>
namespace nlohmann::detail
{
namespace std_fs = std::experimental::filesystem;
} // namespace nlohmann::detail
#elif JSON_HAS_FILESYSTEM
#include <filesystem>
namespace nlohmann::detail
{
namespace std_fs = std::filesystem;
} // namespace nlohmann::detail
#endif

namespace nlohmann
Expand Down Expand Up @@ -5002,9 +5082,9 @@ void to_json(BasicJsonType& j, const T& t)
to_json_tuple_impl(j, t, make_index_sequence<std::tuple_size<T>::value> {});
}

#ifdef JSON_HAS_CPP_17
#if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
template<typename BasicJsonType>
void to_json(BasicJsonType& j, const std::filesystem::path& p)
void to_json(BasicJsonType& j, const std_fs::path& p)
{
j = p.string();
}
Expand Down Expand Up @@ -26591,6 +26671,8 @@ inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std
#undef JSON_HAS_CPP_14
#undef JSON_HAS_CPP_17
#undef JSON_HAS_CPP_20
#undef JSON_HAS_FILESYSTEM
#undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
#undef NLOHMANN_BASIC_JSON_TPL_DECLARATION
#undef NLOHMANN_BASIC_JSON_TPL
#undef JSON_EXPLICIT
Expand Down
46 changes: 46 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ endif()
# one executable for each unit test file
#############################################################################

# check if compiler supports C++17
foreach(feature ${CMAKE_CXX_COMPILE_FEATURES})
if (${feature} STREQUAL cxx_std_17)
set(compiler_supports_cpp_17 TRUE)
endif()
endforeach()
# Clang only supports C++17 starting from Clang 5.0
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)
unset(compiler_supports_cpp_17)
endif()
# MSVC 2015 (14.0) does not support C++17
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 19.1)
unset(compiler_supports_cpp_17)
endif()

file(GLOB files src/unit-*.cpp)

foreach(file ${files})
Expand All @@ -68,6 +83,37 @@ foreach(file ${files})
target_include_directories(${testcase} PRIVATE ${CMAKE_BINARY_DIR}/include thirdparty/doctest thirdparty/fifo_map)
target_link_libraries(${testcase} PRIVATE ${NLOHMANN_JSON_TARGET_NAME})

# add a copy with C++17 compilation
if (compiler_supports_cpp_17)
file(READ ${file} FILE_CONTENT)
string(FIND "${FILE_CONTENT}" "JSON_HAS_CPP_17" CPP_17_FOUND)
if(NOT ${CPP_17_FOUND} EQUAL -1)
add_executable(${testcase}_cpp17 $<TARGET_OBJECTS:doctest_main> ${file})
target_compile_definitions(${testcase}_cpp17 PRIVATE DOCTEST_CONFIG_SUPER_FAST_ASSERTS)
target_compile_options(${testcase}_cpp17 PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:/EHsc;$<$<CONFIG:Release>:/Od>>
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-deprecated;-Wno-float-equal>
$<$<CXX_COMPILER_ID:GNU>:-Wno-deprecated-declarations>
)
target_include_directories(${testcase}_cpp17 PRIVATE ${CMAKE_BINARY_DIR}/include thirdparty/doctest thirdparty/fifo_map)
target_link_libraries(${testcase}_cpp17 PRIVATE ${NLOHMANN_JSON_TARGET_NAME})
target_compile_features(${testcase}_cpp17 PRIVATE cxx_std_17)

if (JSON_FastTests)
add_test(NAME "${testcase}_cpp17"
COMMAND ${testcase}_cpp17 ${DOCTEST_TEST_FILTER}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
else()
add_test(NAME "${testcase}_cpp17"
COMMAND ${testcase}_cpp17 ${DOCTEST_TEST_FILTER} --no-skip
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
endif()
set_tests_properties("${testcase}_cpp17" PROPERTIES LABELS "all" FIXTURES_REQUIRED TEST_DATA)
endif()
endif()

if (JSON_FastTests)
add_test(NAME "${testcase}"
COMMAND ${testcase} ${DOCTEST_TEST_FILTER}
Expand Down
2 changes: 1 addition & 1 deletion test/src/unit-regression1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ TEST_CASE("regression tests 1")
// check if the actual value was stored
CHECK(j2 == 102);

static_assert(std::is_same<decltype(anon_enum_value), decltype(u)>::value, "");
static_assert(std::is_same<decltype(anon_enum_value), decltype(u)>::value, "types must be the same");

j.push_back(json::object(
{
Expand Down
Loading