Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1b4d42c

Browse files
authored
Fix initialization in test (#1140)
* Suppress unsafe-buffer-usage
1 parent caae4dd commit 1b4d42c

4 files changed

Lines changed: 31 additions & 11 deletions

File tree

include/gsl/span

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@
6363
#pragma GCC diagnostic ignored "-Wsign-conversion"
6464
#endif
6565

66+
// Turn off clang unsafe buffer warnings as all accessed are guarded by runtime checks
67+
#if defined(__clang__) && __has_warning("-Wunsafe-buffer-usage")
68+
#pragma clang diagnostic push
69+
#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
70+
#endif // defined(__clang__) && __has_warning("-Wunsafe-buffer-usage")
71+
6672
namespace gsl
6773
{
6874

@@ -846,4 +852,8 @@ as_writable_bytes(span<ElementType, Extent> s) noexcept
846852
#pragma GCC diagnostic pop
847853
#endif // __GNUC__ > 6
848854

855+
#if defined(__clang__) && __has_warning("-Wunsafe-buffer-usage")
856+
#pragma clang diagnostic pop
857+
#endif
858+
849859
#endif // GSL_SPAN_H

include/gsl/util

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040

4141
#endif // _MSC_VER
4242

43+
// Turn off clang unsafe buffer warnings as all accessed are guarded by runtime checks
44+
#if defined(__clang__) && __has_warning("-Wunsafe-buffer-usage")
45+
#pragma clang diagnostic push
46+
#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
47+
#endif // defined(__clang__) && __has_warning("-Wunsafe-buffer-usage")
48+
4349
#if defined(__cplusplus) && (__cplusplus >= 201703L)
4450
#define GSL_NODISCARD [[nodiscard]]
4551
#else
@@ -154,4 +160,8 @@ constexpr auto at(std::span<T, extent> sp, const index i) -> decltype(sp[sp.size
154160

155161
#endif // _MSC_VER
156162

163+
#if defined(__clang__) && __has_warning("-Wunsafe-buffer-usage")
164+
#pragma clang diagnostic pop
165+
#endif
166+
157167
#endif // GSL_UTIL_H

tests/CMakeLists.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,6 @@ if(MSVC) # MSVC or simulating MSVC
123123
if (WARN_RESERVED_ID)
124124
target_compile_options(gsl_tests_config INTERFACE "-Wno-reserved-identifier")
125125
endif()
126-
check_cxx_compiler_flag("-Wno-unsafe-buffer-usage" WARN_UNSAFE_BUFFER)
127-
if (WARN_UNSAFE_BUFFER)
128-
# This test uses very greedy heuristics such as "no pointer arithmetic on raw buffer"
129-
target_compile_options(gsl_tests_config INTERFACE "-Wno-unsafe-buffer-usage")
130-
endif()
131126
else()
132127
target_compile_options(gsl_tests_config INTERFACE
133128
-fno-strict-aliasing
@@ -191,6 +186,11 @@ else()
191186
>
192187
)
193188
endif(MSVC)
189+
check_cxx_compiler_flag("-Wno-unsafe-buffer-usage" WARN_UNSAFE_BUFFER)
190+
if (WARN_UNSAFE_BUFFER)
191+
# This test uses very greedy heuristics such as "no pointer arithmetic on raw buffer"
192+
target_compile_options(gsl_tests_config INTERFACE "-Wno-unsafe-buffer-usage")
193+
endif()
194194

195195
# for tests to find the gtest header
196196
target_include_directories(gsl_tests_config SYSTEM INTERFACE
@@ -262,11 +262,6 @@ if(MSVC) # MSVC or simulating MSVC
262262
if (WARN_RESERVED_ID)
263263
target_compile_options(gsl_tests_config_noexcept INTERFACE "-Wno-reserved-identifier")
264264
endif()
265-
check_cxx_compiler_flag("-Wno-unsafe-buffer-usage" WARN_UNSAFE_BUFFER)
266-
if (WARN_UNSAFE_BUFFER)
267-
# This test uses very greedy heuristics such as "no pointer arithmetic on raw buffer"
268-
target_compile_options(gsl_tests_config_noexcept INTERFACE "-Wno-unsafe-buffer-usage")
269-
endif()
270265
else()
271266
target_compile_options(gsl_tests_config_noexcept INTERFACE
272267
-fno-exceptions
@@ -307,6 +302,11 @@ else()
307302
>
308303
)
309304
endif(MSVC)
305+
check_cxx_compiler_flag("-Wno-unsafe-buffer-usage" WARN_UNSAFE_BUFFER)
306+
if (WARN_UNSAFE_BUFFER)
307+
# This test uses very greedy heuristics such as "no pointer arithmetic on raw buffer"
308+
target_compile_options(gsl_tests_config_noexcept INTERFACE "-Wno-unsafe-buffer-usage")
309+
endif()
310310

311311
add_executable(gsl_noexcept_tests no_exception_ensure_tests.cpp)
312312
target_link_libraries(gsl_noexcept_tests

tests/span_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ TEST(span_test, from_array_constructor)
333333
EXPECT_TRUE(s.data() == std::addressof(arr2d[0]));
334334
}
335335

336-
int arr3d[2][3][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
336+
int arr3d[2][3][2] = { { {1, 2}, {3, 4}, {5, 6} }, { {7, 8}, {9, 10}, {11, 12} } };
337337

338338
#ifdef CONFIRM_COMPILATION_ERRORS
339339
{

0 commit comments

Comments
 (0)