Found while running libc++'s tests with MSVC's STL. * `libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/sort.heap/ranges_sort_heap.pass.cpp` + Fix Clang `-Wunused-variable`, because `LIBCPP_ASSERT` expands to nothing for MSVC's STL. + This is the same "always void-cast" change that #73437 applied to the neighboring `complexity.pass.cpp`. I missed that `ranges_sort_heap.pass.cpp` was also affected because we had disabled this test. * `libcxx/test/std/input.output/file.streams/fstreams/ifstream.members/buffered_reads.pass.cpp` * `libcxx/test/std/input.output/file.streams/fstreams/ofstream.members/buffered_writes.pass.cpp` + Fix MSVC "warning C4244: '`=`': conversion from '`__int64`' to '`_Ty`', possible loss of data". + This is a valid warning, possibly the best one that MSVC found in this entire saga. We're accumulating a `std::vector<std::streamsize>` and storing the result in `std::streamsize total_size` but we actually have to start with `std::streamsize{0}` or we'll truncate. * `libcxx/test/std/input.output/filesystems/fs.enum/enum.path.format.pass.cpp` + Fix Clang `-Wunused-local-typedef` because the following usage is libc++-only. + I'm just expanding it at the point of use, and using the dedicated `LIBCPP_STATIC_ASSERT` to keep the line length down. * `libcxx/test/std/input.output/syncstream/syncbuf/syncstream.syncbuf.assign/swap.pass.cpp` + Fix MSVC "warning C4242: 'argument': conversion from '`int`' to '`const _Elem`', possible loss of data". + This is a valid warning (possibly the second-best) as `sputc()` returns `int_type`. If `sputc()` returns something unexpected, we want to know, so we should separately say `expected.push_back(CharT('B'))`. * `libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.pass.cpp` * `libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_nothrow.pass.cpp` + Fix MSVC "warning C6001: Using uninitialized memory '`x`'." + [N4964](https://wg21.link/N4964) \[new.delete.single\]/12: > *Effects:* The deallocation functions (\[basic.stc.dynamic.deallocation\]) called by a *delete-expression* (\[expr.delete\]) to render the value of `ptr` invalid. + \[basic.stc.general\]/4: > When the end of the duration of a region of storage is reached, the values of all pointers representing the address of any part of that region of storage become invalid pointer values (\[basic.compound\]). Indirection through an invalid pointer value and passing an invalid pointer value to a deallocation function have undefined behavior. Any other use of an invalid pointer value has implementation-defined behavior. + In certain configurations, after `delete x;` MSVC will consider `x` to be radioactive (and in other configurations, it'll physically null out `x` as a safety measure). We can copy it into `old_x` before deletion, which the implementation finds acceptable. * `libcxx/test/std/ranges/range.adaptors/range.elements/general.pass.cpp` * `libcxx/test/std/ranges/range.adaptors/range.elements/iterator/deref.pass.cpp` + Fix MSVC "warning C4242: 'initializing': conversion from '`_Ty`' to '`_Ty`', possible loss of data". + This was being emitted in `pair` and `tuple`'s perfect forwarding constructors. Passing `short{1}` allows MSVC to see that no truncation is happening. * `libcxx/test/std/ranges/range.adaptors/range.elements/iterator/member_types.compile.pass.cpp` + Fix MSVC "warning C4242: 'initializing': conversion from '`_Ty`' to '`_Ty2`', possible loss of data". + Similarly, this was being emitted in `pair`'s perfect forwarding constructor. After passing `short{1}`, I reduced repetition by relying on CTAD. (I can undo that cleanup if it's stylistically undesirable.) * `libcxx/test/std/utilities/function.objects/refwrap/refwrap.const/type_conv_ctor.pass.cpp` + Fix MSVC "warning C4930: '`std::reference_wrapper<int> purr(void)`': prototyped function not called (was a variable definition intended?)". + There's no reason for `purr()` to be locally declared (aside from isolating it to a narrow scope, which has minimal benefits); it can be declared like `meow()` above. 😸 * `libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/make_shared_for_overwrite.pass.cpp` * `libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.create/make_unique_for_overwrite.default_init.pass.cpp` + Fix MSVC static analysis warnings when replacing `operator new`: ``` warning C28196: The requirement that '(_Param_(1)>0)?(return!=0):(1)' is not satisfied. (The expression does not evaluate to true.) warning C6387: 'return' could be '0': this does not adhere to the specification for the function 'new'. warning C6011: Dereferencing NULL pointer 'reinterpret_cast<char *>ptr+i'. ``` + All we need is a null check, which appears in other `operator new` replacements: https://github.com/llvm/llvm-project/blob/b85f1f9b182234ba366d78ae2174a149e44d08c1/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size.replace.pass.cpp#L27-L28
276 lines
7.4 KiB
C++
276 lines
7.4 KiB
C++
//===----------------------------------------------------------------------===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// UNSUPPORTED: c++03, c++11, c++14, c++17
|
|
// UNSUPPORTED: no-localization
|
|
// UNSUPPORTED: libcpp-has-no-experimental-syncstream
|
|
|
|
// <syncstream>
|
|
|
|
// template <class charT, class traits, class Allocator>
|
|
// class basic_syncbuf;
|
|
|
|
// void swap(basic_syncbuf& other) noexcept;
|
|
|
|
#include <syncstream>
|
|
#include <sstream>
|
|
#include <cassert>
|
|
|
|
#include "test_macros.h"
|
|
|
|
#include <iostream>
|
|
|
|
template <class CharT>
|
|
static void test_basic() {
|
|
std::basic_stringbuf<CharT> sstr1;
|
|
std::basic_stringbuf<CharT> sstr2;
|
|
std::basic_string<CharT> expected(42, CharT('*')); // a long string
|
|
|
|
{
|
|
std::basic_syncbuf<CharT> sync_buf1(&sstr1);
|
|
sync_buf1.sputc(CharT('A')); // a short string
|
|
|
|
std::basic_syncbuf<CharT> sync_buf2(&sstr2);
|
|
sync_buf2.sputn(expected.data(), expected.size());
|
|
|
|
#if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
|
|
assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&sstr1) == 1);
|
|
assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&sstr2) == 1);
|
|
#endif
|
|
|
|
sync_buf1.swap(sync_buf2);
|
|
assert(sync_buf1.get_wrapped() == &sstr2);
|
|
assert(sync_buf2.get_wrapped() == &sstr1);
|
|
|
|
assert(sstr1.str().empty());
|
|
assert(sstr2.str().empty());
|
|
|
|
#if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
|
|
assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&sstr1) == 1);
|
|
assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&sstr2) == 1);
|
|
#endif
|
|
}
|
|
|
|
assert(sstr1.str().size() == 1);
|
|
assert(sstr1.str()[0] == CharT('A'));
|
|
assert(sstr2.str() == expected);
|
|
}
|
|
|
|
template <class CharT>
|
|
static void test_short_write_after_swap() {
|
|
std::basic_stringbuf<CharT> sstr1;
|
|
std::basic_stringbuf<CharT> sstr2;
|
|
std::basic_string<CharT> expected(42, CharT('*')); // a long string
|
|
|
|
{
|
|
std::basic_syncbuf<CharT> sync_buf1(&sstr1);
|
|
sync_buf1.sputc(CharT('A')); // a short string
|
|
|
|
std::basic_syncbuf<CharT> sync_buf2(&sstr2);
|
|
sync_buf2.sputn(expected.data(), expected.size());
|
|
|
|
sync_buf1.swap(sync_buf2);
|
|
sync_buf1.sputc(CharT('B'));
|
|
expected.push_back(CharT('B'));
|
|
sync_buf2.sputc(CharT('Z'));
|
|
|
|
assert(sstr1.str().empty());
|
|
assert(sstr2.str().empty());
|
|
}
|
|
|
|
assert(sstr1.str().size() == 2);
|
|
assert(sstr1.str()[0] == CharT('A'));
|
|
assert(sstr1.str()[1] == CharT('Z'));
|
|
assert(sstr2.str() == expected);
|
|
}
|
|
|
|
template <class CharT>
|
|
static void test_long_write_after_swap() {
|
|
std::basic_stringbuf<CharT> sstr1;
|
|
std::basic_stringbuf<CharT> sstr2;
|
|
std::basic_string<CharT> expected(42, CharT('*')); // a long string
|
|
|
|
{
|
|
std::basic_syncbuf<CharT> sync_buf1(&sstr1);
|
|
sync_buf1.sputc(CharT('A')); // a short string
|
|
|
|
std::basic_syncbuf<CharT> sync_buf2(&sstr2);
|
|
sync_buf2.sputn(expected.data(), expected.size());
|
|
|
|
sync_buf1.swap(sync_buf2);
|
|
sync_buf1.sputn(expected.data(), expected.size());
|
|
sync_buf2.sputn(expected.data(), expected.size());
|
|
|
|
assert(sstr1.str().empty());
|
|
assert(sstr2.str().empty());
|
|
}
|
|
|
|
assert(sstr1.str().size() == 1 + expected.size());
|
|
assert(sstr1.str()[0] == CharT('A'));
|
|
assert(sstr1.str().substr(1) == expected);
|
|
assert(sstr2.str() == expected + expected);
|
|
}
|
|
|
|
template <class CharT>
|
|
static void test_emit_on_sync() {
|
|
{ // false false
|
|
|
|
std::basic_stringbuf<CharT> sstr1;
|
|
std::basic_stringbuf<CharT> sstr2;
|
|
std::basic_string<CharT> expected(42, CharT('*')); // a long string
|
|
|
|
{
|
|
std::basic_syncbuf<CharT> sync_buf1(&sstr1);
|
|
sync_buf1.set_emit_on_sync(false);
|
|
sync_buf1.sputc(CharT('A')); // a short string
|
|
|
|
std::basic_syncbuf<CharT> sync_buf2(&sstr2);
|
|
sync_buf2.set_emit_on_sync(false);
|
|
sync_buf2.sputn(expected.data(), expected.size());
|
|
|
|
sync_buf1.swap(sync_buf2);
|
|
|
|
assert(sstr1.str().empty());
|
|
assert(sstr2.str().empty());
|
|
|
|
sync_buf1.pubsync();
|
|
assert(sstr1.str().empty());
|
|
assert(sstr2.str().empty());
|
|
|
|
sync_buf2.pubsync();
|
|
assert(sstr1.str().empty());
|
|
assert(sstr2.str().empty());
|
|
}
|
|
|
|
assert(sstr1.str().size() == 1);
|
|
assert(sstr1.str()[0] == CharT('A'));
|
|
assert(sstr2.str() == expected);
|
|
}
|
|
|
|
{ // false true
|
|
|
|
std::basic_stringbuf<CharT> sstr1;
|
|
std::basic_stringbuf<CharT> sstr2;
|
|
std::basic_string<CharT> expected(42, CharT('*')); // a long string
|
|
|
|
{
|
|
std::basic_syncbuf<CharT> sync_buf1(&sstr1);
|
|
sync_buf1.set_emit_on_sync(true);
|
|
sync_buf1.sputc(CharT('A')); // a short string
|
|
|
|
std::basic_syncbuf<CharT> sync_buf2(&sstr2);
|
|
sync_buf2.set_emit_on_sync(false);
|
|
sync_buf2.sputn(expected.data(), expected.size());
|
|
|
|
sync_buf1.swap(sync_buf2);
|
|
|
|
assert(sstr1.str().empty());
|
|
assert(sstr2.str().empty());
|
|
|
|
sync_buf1.pubsync();
|
|
assert(sstr1.str().empty());
|
|
assert(sstr2.str().empty());
|
|
|
|
sync_buf2.pubsync();
|
|
assert(sstr1.str().size() == 1);
|
|
assert(sstr1.str()[0] == CharT('A'));
|
|
assert(sstr2.str().empty());
|
|
}
|
|
|
|
assert(sstr1.str().size() == 1);
|
|
assert(sstr1.str()[0] == CharT('A'));
|
|
assert(sstr2.str() == expected);
|
|
}
|
|
|
|
{ // true false
|
|
|
|
std::basic_stringbuf<CharT> sstr1;
|
|
std::basic_stringbuf<CharT> sstr2;
|
|
std::basic_string<CharT> expected(42, CharT('*')); // a long string
|
|
|
|
{
|
|
std::basic_syncbuf<CharT> sync_buf1(&sstr1);
|
|
sync_buf1.set_emit_on_sync(false);
|
|
sync_buf1.sputc(CharT('A')); // a short string
|
|
|
|
std::basic_syncbuf<CharT> sync_buf2(&sstr2);
|
|
sync_buf2.set_emit_on_sync(true);
|
|
sync_buf2.sputn(expected.data(), expected.size());
|
|
|
|
sync_buf1.swap(sync_buf2);
|
|
|
|
assert(sstr1.str().empty());
|
|
assert(sstr2.str().empty());
|
|
|
|
sync_buf1.pubsync();
|
|
assert(sstr1.str().empty());
|
|
assert(sstr2.str() == expected);
|
|
|
|
sync_buf2.pubsync();
|
|
assert(sstr1.str().empty());
|
|
assert(sstr2.str() == expected);
|
|
}
|
|
|
|
assert(sstr1.str().size() == 1);
|
|
assert(sstr1.str()[0] == CharT('A'));
|
|
assert(sstr2.str() == expected);
|
|
}
|
|
|
|
{ // true true
|
|
|
|
std::basic_stringbuf<CharT> sstr1;
|
|
std::basic_stringbuf<CharT> sstr2;
|
|
std::basic_string<CharT> expected(42, CharT('*')); // a long string
|
|
|
|
{
|
|
std::basic_syncbuf<CharT> sync_buf1(&sstr1);
|
|
sync_buf1.set_emit_on_sync(true);
|
|
sync_buf1.sputc(CharT('A')); // a short string
|
|
|
|
std::basic_syncbuf<CharT> sync_buf2(&sstr2);
|
|
sync_buf2.set_emit_on_sync(true);
|
|
sync_buf2.sputn(expected.data(), expected.size());
|
|
|
|
sync_buf1.swap(sync_buf2);
|
|
|
|
assert(sstr1.str().empty());
|
|
assert(sstr2.str().empty());
|
|
|
|
sync_buf1.pubsync();
|
|
assert(sstr1.str().empty());
|
|
assert(sstr2.str() == expected);
|
|
|
|
sync_buf2.pubsync();
|
|
assert(sstr1.str().size() == 1);
|
|
assert(sstr1.str()[0] == CharT('A'));
|
|
assert(sstr2.str() == expected);
|
|
}
|
|
|
|
assert(sstr1.str().size() == 1);
|
|
assert(sstr1.str()[0] == CharT('A'));
|
|
assert(sstr2.str() == expected);
|
|
}
|
|
}
|
|
|
|
template <class CharT>
|
|
static void test() {
|
|
test_basic<CharT>();
|
|
test_emit_on_sync<CharT>();
|
|
test_short_write_after_swap<CharT>();
|
|
test_long_write_after_swap<CharT>();
|
|
}
|
|
|
|
int main(int, char**) {
|
|
test<char>();
|
|
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
|
|
test<wchar_t>();
|
|
#endif
|
|
|
|
return 0;
|
|
}
|