Skip to content

[libc++] Implement LWG4023 #87513

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

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion libcxx/docs/Status/Cxx2cIssues.csv
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"`4012 <https://wg21.link/LWG4012>`__","``common_view::begin/end`` are missing the ``simple-view`` check","Tokyo March 2024","","","|ranges|"
"`4013 <https://wg21.link/LWG4013>`__","``lazy_split_view::outer-iterator::value_type`` should not provide default constructor","Tokyo March 2024","","","|ranges|"
"`4016 <https://wg21.link/LWG4016>`__","container-insertable checks do not match what container-inserter does","Tokyo March 2024","","",""
"`4023 <https://wg21.link/LWG4023>`__","Preconditions of ``std::basic_streambuf::setg/setp``","Tokyo March 2024","","",""
"`4023 <https://wg21.link/LWG4023>`__","Preconditions of ``std::basic_streambuf::setg/setp``","Tokyo March 2024","|Complete|","19.0",""
"`4025 <https://wg21.link/LWG4025>`__","Move assignment operator of ``std::expected<cv void, E>`` should not be conditionally deleted","Tokyo March 2024","","",""
"`4030 <https://wg21.link/LWG4030>`__","Clarify whether arithmetic expressions in ``[numeric.sat.func]`` are mathematical or C++","Tokyo March 2024","|Nothing To Do|","",""
"`4031 <https://wg21.link/LWG4031>`__","``bad_expected_access<void>`` member functions should be ``noexcept``","Tokyo March 2024","|Complete|","16.0",""
Expand Down
6 changes: 6 additions & 0 deletions libcxx/include/streambuf
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,12 @@ protected:

*/

#include <__assert>
#include <__config>
#include <__fwd/streambuf.h>
#include <__locale>
#include <__type_traits/is_same.h>
#include <__utility/is_valid_range.h>
#include <climits>
#include <ios>
#include <iosfwd>
Expand Down Expand Up @@ -234,6 +236,9 @@ protected:
inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void gbump(int __n) { __ninp_ += __n; }

inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void setg(char_type* __gbeg, char_type* __gnext, char_type* __gend) {
_LIBCPP_ASSERT_VALID_INPUT_RANGE(std::__is_valid_range(__gbeg, __gnext), "[gbeg, gnext) must be a valid range");
_LIBCPP_ASSERT_VALID_INPUT_RANGE(std::__is_valid_range(__gbeg, __gend), "[gbeg, gend) must be a valid range");
_LIBCPP_ASSERT_VALID_INPUT_RANGE(std::__is_valid_range(__gnext, __gend), "[gnext, gend) must be a valid range");
__binp_ = __gbeg;
__ninp_ = __gnext;
__einp_ = __gend;
Expand All @@ -249,6 +254,7 @@ protected:
_LIBCPP_HIDE_FROM_ABI void __pbump(streamsize __n) { __nout_ += __n; }

inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void setp(char_type* __pbeg, char_type* __pend) {
_LIBCPP_ASSERT_VALID_INPUT_RANGE(std::__is_valid_range(__pbeg, __pend), "[pbeg, pend) must be a valid range");
__bout_ = __nout_ = __pbeg;
__eout_ = __pend;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,20 @@ int main(int, char**)
test<char> t2 = t;
}
{
char g1, g2, g3, p1, p3;
char g[3];
char p[3];
test<char> t;
t.setg(&g1, &g2, &g3);
t.setp(&p1, &p3);
t.setg(&g[0], &g[1], &g[2]);
t.setp(&p[0], &p[2]);
test<char> t2 = t;
}
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
{
wchar_t g1, g2, g3, p1, p3;
wchar_t g[3];
wchar_t p[3];
test<wchar_t> t;
t.setg(&g1, &g2, &g3);
t.setp(&p1, &p3);
t.setg(&g[0], &g[1], &g[2]);
t.setp(&p[0], &p[2]);
test<wchar_t> t2 = t;
}
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ int main(int, char**)
t2 = t;
}
{
char g1, g2, g3, p1, p3;
char g[3];
char p[3];
test<char> t;
t.setg(&g1, &g2, &g3);
t.setp(&p1, &p3);
t.setg(&g[0], &g[1], &g[2]);
t.setp(&p[0], &p[2]);
test<char> t2;
t2 = t;
}
Expand All @@ -73,10 +74,11 @@ int main(int, char**)
t2 = t;
}
{
wchar_t g1, g2, g3, p1, p3;
wchar_t g[3];
wchar_t p[3];
test<wchar_t> t;
t.setg(&g1, &g2, &g3);
t.setp(&p1, &p3);
t.setg(&g[0], &g[1], &g[2]);
t.setp(&p[0], &p[2]);
test<wchar_t> t2;
t2 = t;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ int main(int, char**)
t2.swap(t);
}
{
char g1, g2, g3, p1, p3;
char g[3];
char p[3];
test<char> t;
t.setg(&g1, &g2, &g3);
t.setp(&p1, &p3);
t.setg(&g[0], &g[1], &g[2]);
t.setp(&p[0], &p[2]);
test<char> t2;
t2.swap(t);
}
Expand All @@ -82,10 +83,11 @@ int main(int, char**)
t2.swap(t);
}
{
wchar_t g1, g2, g3, p1, p3;
wchar_t g[3];
wchar_t p[3];
test<wchar_t> t;
t.setg(&g1, &g2, &g3);
t.setp(&p1, &p3);
t.setg(&g[0], &g[1], &g[2]);
t.setp(&p[0], &p[2]);
test<wchar_t> t2;
t2.swap(t);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// REQUIRES: has-unix-headers
// UNSUPPORTED: libcpp-hardening-mode=none
// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing

// <streambuf>

// template <class charT, class traits = char_traits<charT> >
// class basic_streambuf;

// void setg(char_type* gbeg, char_type* gnext, char_type* gend);

#include <algorithm>
#include <iterator>
#include <streambuf>
#include <string>

#include "check_assertion.h"
#include "make_string.h"
#include "test_macros.h"

template <class CharT>
struct streambuf : public std::basic_streambuf<CharT> {
typedef std::basic_streambuf<CharT> base;

streambuf() {}

void setg(CharT* gbeg, CharT* gnext, CharT* gend) { base::setg(gbeg, gnext, gend); }
};

template <class CharT>
void test() {
std::basic_string<CharT> str = MAKE_STRING(CharT, "ABCDEF");
CharT arr[6];
std::copy(str.begin(), str.end(), arr);

{
streambuf<CharT> buff;
TEST_LIBCPP_ASSERT_FAILURE(
buff.setg(std::begin(arr) + 1, std::begin(arr), std::end(arr)), "[gbeg, gnext) must be a valid range");
}
{
streambuf<CharT> buff;
TEST_LIBCPP_ASSERT_FAILURE(
buff.setg(std::begin(arr) + 1, std::begin(arr) + 1, std::begin(arr)), "[gbeg, gend) must be a valid range");
}
{
streambuf<CharT> buff;
TEST_LIBCPP_ASSERT_FAILURE(
buff.setg(std::begin(arr), std::begin(arr) + 3, std::begin(arr) + 2), "[gnext, gend) must be a valid range");
}
}

int main(int, char**) {
test<char>();
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
test<wchar_t>();
#endif

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// REQUIRES: has-unix-headers
// UNSUPPORTED: libcpp-hardening-mode=none
// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing

// <streambuf>

// template <class charT, class traits = char_traits<charT> >
// class basic_streambuf;

// void setp(char_type* pbeg, char_type* pend);

#include <algorithm>
#include <iterator>
#include <streambuf>
#include <string>

#include "check_assertion.h"
#include "make_string.h"
#include "test_macros.h"

template <class CharT>
struct streambuf : public std::basic_streambuf<CharT> {
typedef std::basic_streambuf<CharT> base;

streambuf() {}

void setp(CharT* pbeg, CharT* pend) { base::setp(pbeg, pend); }
};

template <class CharT>
void test() {
std::basic_string<CharT> str = MAKE_STRING(CharT, "ABCDEF");
CharT arr[6];
std::copy(str.begin(), str.end(), arr);

{
streambuf<CharT> buff;
TEST_LIBCPP_ASSERT_FAILURE(buff.setp(std::begin(arr) + 3, std::begin(arr)), "[pbeg, pend) must be a valid range");
}
}

int main(int, char**) {
test<char>();
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
test<wchar_t>();
#endif

return 0;
}