Skip to content

Commit c67d538

Browse files
authored
Fix C4702 warning and extend MSVC CI job (#4749)
* ⚗️ try matrix for latest Signed-off-by: Niels Lohmann <[email protected]> * ♻️ refactor from #4745 (comment) Signed-off-by: Niels Lohmann <[email protected]> * 👷 simplify CI Signed-off-by: Niels Lohmann <[email protected]> * 👷 simplify CI Signed-off-by: Niels Lohmann <[email protected]> * 🚨 fix cpplint warning Signed-off-by: Niels Lohmann <[email protected]> * 👷 simplify CI Signed-off-by: Niels Lohmann <[email protected]> --------- Signed-off-by: Niels Lohmann <[email protected]>
1 parent 88c92e6 commit c67d538

File tree

3 files changed

+56
-67
lines changed

3 files changed

+56
-67
lines changed

.github/workflows/windows.yml

Lines changed: 36 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -37,69 +37,48 @@ jobs:
3737
- name: Test
3838
run: cd build ; ctest -j 10 -C Debug --output-on-failure
3939

40-
msvc2019:
41-
runs-on: windows-2019
40+
msvc:
4241
strategy:
4342
matrix:
43+
runs_on: [windows-2019, windows-2022]
4444
build_type: [Debug, Release]
4545
architecture: [Win32, x64]
46+
std_version: [default, latest]
4647

47-
steps:
48-
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
49-
- name: Run CMake
50-
run: cmake -S . -B build -G "Visual Studio 16 2019" -A ${{ matrix.architecture }} -DJSON_BuildTests=On -DCMAKE_CXX_FLAGS="/W4 /WX"
51-
if: matrix.build_type == 'Release'
52-
- name: Run CMake
53-
run: cmake -S . -B build -G "Visual Studio 16 2019" -A ${{ matrix.architecture }} -DJSON_BuildTests=On -DJSON_FastTests=ON -DCMAKE_CXX_FLAGS="/W4 /WX"
54-
if: matrix.build_type == 'Debug'
55-
- name: Build
56-
run: cmake --build build --config ${{ matrix.build_type }} --parallel 10
57-
- name: Test
58-
run: cd build ; ctest -j 10 -C ${{ matrix.build_type }} --output-on-failure
59-
60-
msvc2019_latest:
61-
runs-on: windows-2019
48+
runs-on: ${{ matrix.runs_on }}
6249

6350
steps:
64-
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
65-
- name: Run CMake
66-
run: cmake -S . -B build -G "Visual Studio 16 2019" -DJSON_BuildTests=On -DCMAKE_CXX_FLAGS="/permissive- /std:c++latest /utf-8 /W4 /WX"
67-
- name: Build
68-
run: cmake --build build --config Release --parallel 10
69-
- name: Test
70-
run: cd build ; ctest -j 10 -C Release --output-on-failure
71-
72-
msvc2022:
73-
runs-on: windows-2022
74-
strategy:
75-
matrix:
76-
build_type: [Debug, Release]
77-
architecture: [Win32, x64]
78-
79-
steps:
80-
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
81-
- name: Run CMake
82-
run: cmake -S . -B build -G "Visual Studio 17 2022" -A ${{ matrix.architecture }} -DJSON_BuildTests=On -DCMAKE_CXX_FLAGS="/W4 /WX"
83-
if: matrix.build_type == 'Release'
84-
- name: Run CMake
85-
run: cmake -S . -B build -G "Visual Studio 17 2022" -A ${{ matrix.architecture }} -DJSON_BuildTests=On -DJSON_FastTests=ON -DCMAKE_CXX_FLAGS="/W4 /WX"
86-
if: matrix.build_type == 'Debug'
87-
- name: Build
88-
run: cmake --build build --config ${{ matrix.build_type }} --parallel 10
89-
- name: Test
90-
run: cd build ; ctest -j 10 -C ${{ matrix.build_type }} --output-on-failure
91-
92-
msvc2022_latest:
93-
runs-on: windows-2022
94-
95-
steps:
96-
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
97-
- name: Run CMake
98-
run: cmake -S . -B build -G "Visual Studio 17 2022" -DJSON_BuildTests=On -DCMAKE_CXX_FLAGS="/permissive- /std:c++latest /utf-8 /W4 /WX"
99-
- name: Build
100-
run: cmake --build build --config Release --parallel 10
101-
- name: Test
102-
run: cd build ; ctest -j 10 -C Release --output-on-failure
51+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
52+
- name: Set generator
53+
id: generator
54+
run: |
55+
if [ "${{ matrix.runs_on }}" = "windows-2019" ]; then
56+
echo "generator=Visual Studio 16 2019" >> $GITHUB_ENV
57+
else
58+
echo "generator=Visual Studio 17 2022" >> $GITHUB_ENV
59+
fi
60+
shell: bash
61+
- name: Set extra CXX_FLAGS for latest std_version
62+
id: cxxflags
63+
run: |
64+
if [ "${{ matrix.std_version }}" = "latest" ]; then
65+
echo "flags=/permissive- /std:c++latest /utf-8 /W4 /WX" >> $GITHUB_ENV
66+
else
67+
echo "flags=/W4 /WX" >> $GITHUB_ENV
68+
fi
69+
shell: bash
70+
- name: Run CMake (Release)
71+
run: cmake -S . -B build -G "$env:generator" -A ${{ matrix.architecture }} -DJSON_BuildTests=On -DCMAKE_CXX_FLAGS="$env:flags"
72+
if: matrix.build_type == 'Release'
73+
shell: pwsh
74+
- name: Run CMake (Debug)
75+
run: cmake -S . -B build -G "$env:generator" -A ${{ matrix.architecture }} -DJSON_BuildTests=On -DJSON_FastTests=ON -DCMAKE_CXX_FLAGS="$env:flags"
76+
if: matrix.build_type == 'Debug'
77+
shell: pwsh
78+
- name: Build
79+
run: cmake --build build --config ${{ matrix.build_type }} --parallel 10
80+
- name: Test
81+
run: cd build ; ctest -j 10 -C ${{ matrix.build_type }} --output-on-failure
10382

10483
clang:
10584
runs-on: windows-2019
@@ -118,7 +97,7 @@ jobs:
11897
- name: Test
11998
run: cd build ; ctest -j 10 -C Debug --exclude-regex "test-unicode" --output-on-failure
12099

121-
clang-cl-11:
100+
clang-cl-12:
122101
runs-on: windows-2019
123102
strategy:
124103
matrix:

include/nlohmann/detail/input/binary_reader.hpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2826,17 +2826,22 @@ class binary_reader
28262826
{
28272827
return;
28282828
}
2829-
if constexpr(std::is_integral_v<NumberType>)
2829+
else if constexpr(std::is_integral_v<NumberType>)
28302830
{
28312831
number = std::byteswap(number);
28322832
return;
28332833
}
2834-
#endif
2835-
auto* ptr = reinterpret_cast<std::uint8_t*>(&number);
2836-
for (std::size_t i = 0; i < sz / 2; ++i)
2834+
else
28372835
{
2838-
std::swap(ptr[i], ptr[sz - i - 1]);
2836+
#endif
2837+
auto* ptr = reinterpret_cast<std::uint8_t*>(&number);
2838+
for (std::size_t i = 0; i < sz / 2; ++i)
2839+
{
2840+
std::swap(ptr[i], ptr[sz - i - 1]);
2841+
}
2842+
#ifdef __cpp_lib_byteswap
28392843
}
2844+
#endif
28402845
}
28412846

28422847
/*

single_include/nlohmann/json.hpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12601,17 +12601,22 @@ class binary_reader
1260112601
{
1260212602
return;
1260312603
}
12604-
if constexpr(std::is_integral_v<NumberType>)
12604+
else if constexpr(std::is_integral_v<NumberType>)
1260512605
{
1260612606
number = std::byteswap(number);
1260712607
return;
1260812608
}
12609-
#endif
12610-
auto* ptr = reinterpret_cast<std::uint8_t*>(&number);
12611-
for (std::size_t i = 0; i < sz / 2; ++i)
12609+
else
1261212610
{
12613-
std::swap(ptr[i], ptr[sz - i - 1]);
12611+
#endif
12612+
auto* ptr = reinterpret_cast<std::uint8_t*>(&number);
12613+
for (std::size_t i = 0; i < sz / 2; ++i)
12614+
{
12615+
std::swap(ptr[i], ptr[sz - i - 1]);
12616+
}
12617+
#ifdef __cpp_lib_byteswap
1261412618
}
12619+
#endif
1261512620
}
1261612621

1261712622
/*

0 commit comments

Comments
 (0)