From 8f78f6d2ee74cb65765af8894ed66f771a70cc17 Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Sun, 14 Aug 2022 15:38:46 +0700 Subject: [PATCH 1/3] remove annotation 'NullTerminated' --- stl/inc/xstring | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stl/inc/xstring b/stl/inc/xstring index 28593f1cca4..b3621282c2f 100644 --- a/stl/inc/xstring +++ b/stl/inc/xstring @@ -1223,7 +1223,7 @@ public: constexpr basic_string_view(const basic_string_view&) noexcept = default; constexpr basic_string_view& operator=(const basic_string_view&) noexcept = default; - /* implicit */ constexpr basic_string_view(_In_z_ const const_pointer _Ntcts) noexcept // strengthened + /* implicit */ constexpr basic_string_view(const const_pointer _Ntcts) noexcept // strengthened : _Mydata(_Ntcts), _Mysize(_Traits::length(_Ntcts)) {} #if _HAS_CXX23 From cef0a5121dc756fc6bf621b15fefc6f4b42b4a11 Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Mon, 15 Aug 2022 01:02:33 +0700 Subject: [PATCH 2/3] suppress the warning instead Co-authored-by: Casey Carter --- stl/inc/xstring | 8 ++++- tests/std/tests/P0220R1_string_view/test.cpp | 33 ++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/stl/inc/xstring b/stl/inc/xstring index b3621282c2f..2edb3cf74d2 100644 --- a/stl/inc/xstring +++ b/stl/inc/xstring @@ -1191,6 +1191,10 @@ struct pointer_traits<_String_view_iterator<_Traits>> { }; #endif // _HAS_CXX20 +#pragma warning(push) +// Invalid annotation: 'NullTerminated' property may only be used on buffers whose elements are of integral or pointer +// type +#pragma warning(disable : 6510) template class basic_string_view { // wrapper for any kind of contiguous character buffer @@ -1223,7 +1227,7 @@ public: constexpr basic_string_view(const basic_string_view&) noexcept = default; constexpr basic_string_view& operator=(const basic_string_view&) noexcept = default; - /* implicit */ constexpr basic_string_view(const const_pointer _Ntcts) noexcept // strengthened + /* implicit */ constexpr basic_string_view(_In_z_ const const_pointer _Ntcts) noexcept // strengthened : _Mydata(_Ntcts), _Mysize(_Traits::length(_Ntcts)) {} #if _HAS_CXX23 @@ -1656,6 +1660,8 @@ private: size_type _Mysize; }; +#pragma warning(pop) + #ifdef __cpp_lib_concepts template _Sent> basic_string_view(_Iter, _Sent) -> basic_string_view>; diff --git a/tests/std/tests/P0220R1_string_view/test.cpp b/tests/std/tests/P0220R1_string_view/test.cpp index b8b506432d5..01ae2eed37b 100644 --- a/tests/std/tests/P0220R1_string_view/test.cpp +++ b/tests/std/tests/P0220R1_string_view/test.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -1251,6 +1252,38 @@ static_assert(!is_constructible_v, "constructing string from static_assert(!is_assignable_v, "assigning nullptr_t to string is prohibited"); #endif // _HAS_CXX23 +// Also test that no C6510 warning +struct char_wrapper { + char c; +}; + +template <> +struct std::char_traits { + using char_type = char_wrapper; + + static bool eq(char_wrapper lhs, char_wrapper rhs) { + return lhs.c == rhs.c; + } + + static size_t length(const char_wrapper* a) { + static_assert(sizeof(char_wrapper) == 1, "strlen requires this"); + return std::strlen(reinterpret_cast(a)); + } + + static int compare(const char_wrapper* lhs, const char_wrapper* rhs, std::size_t count) { + return std::char_traits::compare( + reinterpret_cast(lhs), reinterpret_cast(rhs), count); + } +}; + +using WrappedSV = std::basic_string_view>; + +void test_C6510_warning() { // compile-only + char_wrapper a[] = {{'a'}, {'b'}, {'c'}, {'\0'}}; + WrappedSV sv(a); + (void) sv; +} + int main() { test_case_default_constructor(); test_case_ntcts_constructor(); From 98239b46ae19850f2588e8a92b79ccaaf343d8b6 Mon Sep 17 00:00:00 2001 From: Casey Carter Date: Tue, 16 Aug 2022 12:40:09 -0700 Subject: [PATCH 3/3] Remove extraneous `std::`-qualifications in test code --- tests/std/tests/P0220R1_string_view/test.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/std/tests/P0220R1_string_view/test.cpp b/tests/std/tests/P0220R1_string_view/test.cpp index 01ae2eed37b..120bba80022 100644 --- a/tests/std/tests/P0220R1_string_view/test.cpp +++ b/tests/std/tests/P0220R1_string_view/test.cpp @@ -1267,16 +1267,16 @@ struct std::char_traits { static size_t length(const char_wrapper* a) { static_assert(sizeof(char_wrapper) == 1, "strlen requires this"); - return std::strlen(reinterpret_cast(a)); + return strlen(reinterpret_cast(a)); } - static int compare(const char_wrapper* lhs, const char_wrapper* rhs, std::size_t count) { - return std::char_traits::compare( + static int compare(const char_wrapper* lhs, const char_wrapper* rhs, size_t count) { + return char_traits::compare( reinterpret_cast(lhs), reinterpret_cast(rhs), count); } }; -using WrappedSV = std::basic_string_view>; +using WrappedSV = basic_string_view>; void test_C6510_warning() { // compile-only char_wrapper a[] = {{'a'}, {'b'}, {'c'}, {'\0'}};