| 1 |
From eef76c200e1d5b88a2cf0b5264756c4a020a88db Mon Sep 17 00:00:00 2001 |
| 2 |
From: Niels Lohmann <[email protected]> |
| 3 |
Date: Mon, 28 Apr 2025 21:52:26 +0200 |
| 4 |
Subject: [PATCH] Make library work with C++20 modules (#4764) |
| 5 |
|
| 6 |
* :white_check_mark: add test for C++20 modules |
| 7 |
|
| 8 |
Signed-off-by: Niels Lohmann <[email protected]> |
| 9 |
|
| 10 |
* :rotating_light: fix warning |
| 11 |
|
| 12 |
Signed-off-by: Niels Lohmann <[email protected]> |
| 13 |
|
| 14 |
* Add missing header (#4763) |
| 15 |
|
| 16 |
* :bug: add missing header |
| 17 |
|
| 18 |
Signed-off-by: Niels Lohmann <[email protected]> |
| 19 |
|
| 20 |
* :rotating_light: fix warning |
| 21 |
|
| 22 |
Signed-off-by: Niels Lohmann <[email protected]> |
| 23 |
|
| 24 |
* :rotating_light: fix warning |
| 25 |
|
| 26 |
Signed-off-by: Niels Lohmann <[email protected]> |
| 27 |
|
| 28 |
--------- |
| 29 |
|
| 30 |
Signed-off-by: Niels Lohmann <[email protected]> |
| 31 |
|
| 32 |
--------- |
| 33 |
|
| 34 |
Signed-off-by: Niels Lohmann <[email protected]> |
| 35 |
--- |
| 36 |
.github/workflows/ubuntu.yml | 17 ++++++++++++++++- |
| 37 |
cmake/ci.cmake | 11 +++++++++++ |
| 38 |
include/nlohmann/detail/input/binary_reader.hpp | 2 +- |
| 39 |
include/nlohmann/detail/string_escape.hpp | 2 +- |
| 40 |
single_include/nlohmann/json.hpp | 4 ++-- |
| 41 |
tests/module_cpp20/CMakeLists.txt | 12 ++++++++++++ |
| 42 |
tests/module_cpp20/json.cpp | 17 +++++++++++++++++ |
| 43 |
tests/module_cpp20/main.cpp | 6 ++++++ |
| 44 |
8 files changed, 66 insertions(+), 5 deletions(-) |
| 45 |
create mode 100644 tests/module_cpp20/CMakeLists.txt |
| 46 |
create mode 100644 tests/module_cpp20/json.cpp |
| 47 |
create mode 100644 tests/module_cpp20/main.cpp |
| 48 |
|
| 49 |
diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml |
| 50 |
index 6defddc2bc..179038fd62 100644 |
| 51 |
--- a/.github/workflows/ubuntu.yml |
| 52 |
+++ b/.github/workflows/ubuntu.yml |
| 53 |
@@ -143,7 +143,7 @@ jobs: |
| 54 |
strategy: |
| 55 |
matrix: |
| 56 |
# older GCC docker images (4, 5, 6) fail to check out code |
| 57 |
- compiler: ['7', '8', '9', '10', '11', '12', '13', '14', 'latest'] |
| 58 |
+ compiler: ['7', '8', '9', '10', '11', '12', '13', '14', '15', 'latest'] |
| 59 |
container: gcc:${{ matrix.compiler }} |
| 60 |
steps: |
| 61 |
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 62 |
@@ -221,6 +221,21 @@ jobs: |
| 63 |
- name: Build |
| 64 |
run: cmake --build build --target ci_cuda_example |
| 65 |
|
| 66 |
+ ci_module_cpp20: |
| 67 |
+ strategy: |
| 68 |
+ matrix: |
| 69 |
+ container: ['gcc:latest', 'silkeh/clang:latest'] |
| 70 |
+ runs-on: ubuntu-latest |
| 71 |
+ container: ${{ matrix.container }} |
| 72 |
+ steps: |
| 73 |
+ - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 74 |
+ - name: Get latest CMake and ninja |
| 75 |
+ uses: lukka/get-cmake@57c20a23a6cac5b90f31864439996e5b206df9dc # v4.0.1 |
| 76 |
+ - name: Run CMake |
| 77 |
+ run: cmake -S . -B build -DJSON_CI=On |
| 78 |
+ - name: Build |
| 79 |
+ run: cmake --build build --target ci_module_cpp20 |
| 80 |
+ |
| 81 |
ci_icpc: |
| 82 |
runs-on: ubuntu-latest |
| 83 |
container: ghcr.io/nlohmann/json-ci:v2.2.0 |
| 84 |
diff --git a/cmake/ci.cmake b/cmake/ci.cmake |
| 85 |
index ef4c257b18..8c0d2106b7 100644 |
| 86 |
--- a/cmake/ci.cmake |
| 87 |
+++ b/cmake/ci.cmake |
| 88 |
@@ -659,6 +659,17 @@ add_custom_target(ci_cuda_example |
| 89 |
COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_cuda_example |
| 90 |
) |
| 91 |
|
| 92 |
+############################################################################### |
| 93 |
+# C++ 20 modules |
| 94 |
+############################################################################### |
| 95 |
+ |
| 96 |
+add_custom_target(ci_module_cpp20 |
| 97 |
+ COMMAND ${CMAKE_COMMAND} |
| 98 |
+ -DCMAKE_BUILD_TYPE=Debug -GNinja |
| 99 |
+ -S${PROJECT_SOURCE_DIR}/tests/module_cpp20 -B${PROJECT_BINARY_DIR}/ci_module_cpp20 |
| 100 |
+ COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/ci_module_cpp20 |
| 101 |
+) |
| 102 |
+ |
| 103 |
############################################################################### |
| 104 |
# Intel C++ Compiler |
| 105 |
############################################################################### |
| 106 |
diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp |
| 107 |
index 92b85572a0..ed0ee5b7c2 100644 |
| 108 |
--- a/include/nlohmann/detail/input/binary_reader.hpp |
| 109 |
+++ b/include/nlohmann/detail/input/binary_reader.hpp |
| 110 |
@@ -53,7 +53,7 @@ enum class cbor_tag_handler_t |
| 111 |
|
| 112 |
@note from https://stackoverflow.com/a/1001328/266378 |
| 113 |
*/ |
| 114 |
-static inline bool little_endianness(int num = 1) noexcept |
| 115 |
+inline bool little_endianness(int num = 1) noexcept |
| 116 |
{ |
| 117 |
return *reinterpret_cast<char*>(&num) == 1; |
| 118 |
} |
| 119 |
diff --git a/include/nlohmann/detail/string_escape.hpp b/include/nlohmann/detail/string_escape.hpp |
| 120 |
index 7f02318193..d969a9ed44 100644 |
| 121 |
--- a/include/nlohmann/detail/string_escape.hpp |
| 122 |
+++ b/include/nlohmann/detail/string_escape.hpp |
| 123 |
@@ -62,7 +62,7 @@ inline StringType escape(StringType s) |
| 124 |
* Note the order of escaping "~1" to "/" and "~0" to "~" is important. |
| 125 |
*/ |
| 126 |
template<typename StringType> |
| 127 |
-static void unescape(StringType& s) |
| 128 |
+inline void unescape(StringType& s) |
| 129 |
{ |
| 130 |
replace_substring(s, StringType{"~1"}, StringType{"/"}); |
| 131 |
replace_substring(s, StringType{"~0"}, StringType{"~"}); |
| 132 |
diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp |
| 133 |
index dabad062d1..850cd8ebf3 100644 |
| 134 |
--- a/single_include/nlohmann/json.hpp |
| 135 |
+++ b/single_include/nlohmann/json.hpp |
| 136 |
@@ -3126,7 +3126,7 @@ inline StringType escape(StringType s) |
| 137 |
* Note the order of escaping "~1" to "/" and "~0" to "~" is important. |
| 138 |
*/ |
| 139 |
template<typename StringType> |
| 140 |
-static void unescape(StringType& s) |
| 141 |
+inline void unescape(StringType& s) |
| 142 |
{ |
| 143 |
replace_substring(s, StringType{"~1"}, StringType{"/"}); |
| 144 |
replace_substring(s, StringType{"~0"}, StringType{"~"}); |
| 145 |
@@ -9868,7 +9868,7 @@ enum class cbor_tag_handler_t |
| 146 |
|
| 147 |
@note from https://stackoverflow.com/a/1001328/266378 |
| 148 |
*/ |
| 149 |
-static inline bool little_endianness(int num = 1) noexcept |
| 150 |
+inline bool little_endianness(int num = 1) noexcept |
| 151 |
{ |
| 152 |
return *reinterpret_cast<char*>(&num) == 1; |
| 153 |
} |
| 154 |
diff --git a/tests/module_cpp20/CMakeLists.txt b/tests/module_cpp20/CMakeLists.txt |
| 155 |
new file mode 100644 |
| 156 |
index 0000000000..23884342ca |
| 157 |
--- /dev/null |
| 158 |
+++ b/tests/module_cpp20/CMakeLists.txt |
| 159 |
@@ -0,0 +1,12 @@ |
| 160 |
+cmake_minimum_required(VERSION 3.28) |
| 161 |
+ |
| 162 |
+project(json_test CXX) |
| 163 |
+ |
| 164 |
+add_executable(json_test) |
| 165 |
+ |
| 166 |
+target_sources(json_test |
| 167 |
+ PRIVATE main.cpp |
| 168 |
+ PUBLIC FILE_SET cxx_modules TYPE CXX_MODULES FILES json.cpp) |
| 169 |
+ |
| 170 |
+target_compile_features(json_test PUBLIC cxx_std_20) |
| 171 |
+target_include_directories(json_test PRIVATE ../../include) |
| 172 |
diff --git a/tests/module_cpp20/json.cpp b/tests/module_cpp20/json.cpp |
| 173 |
new file mode 100644 |
| 174 |
index 0000000000..8d5cc91476 |
| 175 |
--- /dev/null |
| 176 |
+++ b/tests/module_cpp20/json.cpp |
| 177 |
@@ -0,0 +1,17 @@ |
| 178 |
+module; |
| 179 |
+#include <nlohmann/json.hpp> |
| 180 |
+export module json; |
| 181 |
+ |
| 182 |
+export namespace nlohmann |
| 183 |
+{ |
| 184 |
+using ::nlohmann::adl_serializer; |
| 185 |
+ |
| 186 |
+using ::nlohmann::basic_json; |
| 187 |
+using ::nlohmann::json_pointer; |
| 188 |
+ |
| 189 |
+using ::nlohmann::json; |
| 190 |
+using ::nlohmann::ordered_json; |
| 191 |
+using ::nlohmann::ordered_map; |
| 192 |
+ |
| 193 |
+using ::nlohmann::json_pointer; |
| 194 |
+} // namespace nlohmann |
| 195 |
diff --git a/tests/module_cpp20/main.cpp b/tests/module_cpp20/main.cpp |
| 196 |
new file mode 100644 |
| 197 |
index 0000000000..ad7bd8b1af |
| 198 |
--- /dev/null |
| 199 |
+++ b/tests/module_cpp20/main.cpp |
| 200 |
@@ -0,0 +1,6 @@ |
| 201 |
+import json; |
| 202 |
+ |
| 203 |
+int main() |
| 204 |
+{ |
| 205 |
+ nlohmann::json j; |
| 206 |
+} |