Skip to content

Commit b2cc4b9

Browse files
[libc++][test] Fix more MSVC and Clang warnings (#74965)
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
1 parent 774295c commit b2cc4b9

File tree

19 files changed

+49
-34
lines changed

19 files changed

+49
-34
lines changed

libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/sort.heap/ranges_sort_heap.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ void test_complexity() {
238238
const int debug_elements = std::min(100, n);
239239
// Multiplier 2 because of comp(a,b) comp(b, a) checks.
240240
const int debug_comparisons = 2 * (debug_elements + 1) * debug_elements;
241+
(void)debug_comparisons;
241242
std::shuffle(first, last, g);
242243
std::make_heap(first, last, &MyInt::Comp);
243244
// The exact stats of our current implementation are recorded here.
@@ -247,7 +248,6 @@ void test_complexity() {
247248
LIBCPP_ASSERT(stats.moved <= 2 * n + n * logn);
248249
#if _LIBCPP_HARDENING_MODE != _LIBCPP_HARDENING_MODE_DEBUG
249250
LIBCPP_ASSERT(stats.compared <= n * logn);
250-
(void)debug_comparisons;
251251
#else
252252
LIBCPP_ASSERT(stats.compared <= 2 * n * logn + debug_comparisons);
253253
#endif

libcxx/test/std/input.output/file.streams/fstreams/ifstream.members/buffered_reads.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
template <class BufferPolicy>
4646
void test_read(BufferPolicy policy, const std::vector<std::streamsize>& payload_sizes) {
47-
std::streamsize total_size = std::accumulate(payload_sizes.begin(), payload_sizes.end(), 0);
47+
std::streamsize total_size = std::accumulate(payload_sizes.begin(), payload_sizes.end(), std::streamsize{0});
4848
std::vector<char> data(total_size);
4949
for (std::size_t i = 0; i < data.size(); ++i) {
5050
data[i] = static_cast<char>(i % (1 << 8 * sizeof(char)));
@@ -99,7 +99,7 @@ void test_read(BufferPolicy policy, const std::vector<std::streamsize>& payload_
9999
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
100100
template <class BufferPolicy>
101101
void test_read_codecvt(BufferPolicy policy, const std::vector<std::streamsize>& payload_sizes) {
102-
std::streamsize total_size = std::accumulate(payload_sizes.begin(), payload_sizes.end(), 0);
102+
std::streamsize total_size = std::accumulate(payload_sizes.begin(), payload_sizes.end(), std::streamsize{0});
103103
std::vector<wchar_t> data(total_size);
104104
for (std::size_t i = 0; i < data.size(); ++i) {
105105
data[i] = static_cast<wchar_t>(i);

libcxx/test/std/input.output/file.streams/fstreams/ofstream.members/buffered_writes.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
template <class BufferPolicy>
4646
void test_write(BufferPolicy policy, const std::vector<std::streamsize>& payload_sizes) {
4747
std::size_t previously_written = 0;
48-
std::streamsize total_size = std::accumulate(payload_sizes.begin(), payload_sizes.end(), 0);
48+
std::streamsize total_size = std::accumulate(payload_sizes.begin(), payload_sizes.end(), std::streamsize{0});
4949
std::vector<char> data(total_size);
5050
for (std::size_t i = 0; i < data.size(); ++i) {
5151
data[i] = static_cast<char>(i % (1 << 8 * sizeof(char)));
@@ -97,7 +97,7 @@ void test_write(BufferPolicy policy, const std::vector<std::streamsize>& payload
9797
template <class BufferPolicy>
9898
void test_write_codecvt(BufferPolicy policy, const std::vector<std::streamsize>& payload_sizes) {
9999
std::size_t previously_written = 0;
100-
std::streamsize total_size = std::accumulate(payload_sizes.begin(), payload_sizes.end(), 0);
100+
std::streamsize total_size = std::accumulate(payload_sizes.begin(), payload_sizes.end(), std::streamsize{0});
101101
std::vector<wchar_t> data(total_size);
102102
for (std::size_t i = 0; i < data.size(); ++i) {
103103
data[i] = static_cast<wchar_t>(i);

libcxx/test/std/input.output/filesystems/fs.enum/enum.path.format.pass.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ int main(int, char**) {
2525
typedef fs::path::format E;
2626
static_assert(std::is_enum<E>::value, "");
2727

28-
typedef std::underlying_type<E>::type UT;
29-
30-
LIBCPP_ONLY(static_assert(std::is_same<UT, unsigned char>::value, "")); // Implementation detail
28+
LIBCPP_STATIC_ASSERT(std::is_same<std::underlying_type<E>::type, unsigned char>::value, ""); // Implementation detail
3129

3230
static_assert(
3331
E::auto_format != E::native_format &&

libcxx/test/std/input.output/syncstream/syncbuf/syncstream.syncbuf.assign/swap.pass.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ static void test_short_write_after_swap() {
7575
sync_buf2.sputn(expected.data(), expected.size());
7676

7777
sync_buf1.swap(sync_buf2);
78-
expected.push_back(sync_buf1.sputc(CharT('B')));
78+
sync_buf1.sputc(CharT('B'));
79+
expected.push_back(CharT('B'));
7980
sync_buf2.sputc(CharT('Z'));
8081

8182
assert(sstr1.str().empty());

libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size.replace.indirect.pass.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ TEST_WORKAROUND_BUG_109234844_WEAK
2929
void* operator new(std::size_t s) TEST_THROW_SPEC(std::bad_alloc) {
3030
++new_called;
3131
void* ret = std::malloc(s);
32-
if (!ret) std::abort(); // placate MSVC's unchecked malloc warning
32+
if (!ret) {
33+
std::abort(); // placate MSVC's unchecked malloc warning (assert() won't silence it)
34+
}
3335
return ret;
3436
}
3537

libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size.replace.pass.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ int delete_called = 0;
2727
void* operator new[](std::size_t s) TEST_THROW_SPEC(std::bad_alloc) {
2828
++new_called;
2929
void* ret = std::malloc(s);
30-
if (!ret) std::abort(); // placate MSVC's unchecked malloc warning
30+
if (!ret) {
31+
std::abort(); // placate MSVC's unchecked malloc warning (assert() won't silence it)
32+
}
3133
return ret;
3234
}
3335

libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_nothrow.replace.indirect.pass.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ TEST_WORKAROUND_BUG_109234844_WEAK
3333
void* operator new[](std::size_t s) TEST_THROW_SPEC(std::bad_alloc) {
3434
++new_called;
3535
void* ret = std::malloc(s);
36-
if (!ret) std::abort(); // placate MSVC's unchecked malloc warning
36+
if (!ret) {
37+
std::abort(); // placate MSVC's unchecked malloc warning (assert() won't silence it)
38+
}
3739
return ret;
3840
}
3941

libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size.replace.pass.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ int delete_called = 0;
2525
void* operator new(std::size_t s) TEST_THROW_SPEC(std::bad_alloc) {
2626
++new_called;
2727
void* ret = std::malloc(s);
28-
if (!ret) std::abort(); // placate MSVC's unchecked malloc warning
28+
if (!ret) {
29+
std::abort(); // placate MSVC's unchecked malloc warning (assert() won't silence it)
30+
}
2931
return ret;
3032
}
3133

libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.pass.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ int main(int, char**) {
6060
assert(reinterpret_cast<std::uintptr_t>(x) % alignof(TrackLifetimeOverAligned) == 0);
6161
assert(info.address_constructed == x);
6262

63+
const auto old_x = x;
6364
delete x;
64-
assert(info.address_destroyed == x);
65+
assert(info.address_destroyed == old_x);
6566
}
6667

6768
return 0;

libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_nothrow.pass.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ int main(int, char**) {
5050
assert(x != nullptr);
5151
assert(info.address_constructed == x);
5252

53+
const auto old_x = x;
5354
delete x;
54-
assert(info.address_destroyed == x);
55+
assert(info.address_destroyed == old_x);
5556
}
5657

5758
return 0;

libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_nothrow.replace.indirect.pass.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ TEST_WORKAROUND_BUG_109234844_WEAK
2828
void* operator new(std::size_t s) TEST_THROW_SPEC(std::bad_alloc) {
2929
++new_called;
3030
void* ret = std::malloc(s);
31-
if (!ret) std::abort(); // placate MSVC's unchecked malloc warning
31+
if (!ret) {
32+
std::abort(); // placate MSVC's unchecked malloc warning (assert() won't silence it)
33+
}
3234
return ret;
3335
}
3436

libcxx/test/std/ranges/range.adaptors/range.elements/general.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ int main(int, char**) {
7070

7171
// tuple
7272
{
73-
std::tuple<short> tps[] = {{1}, {2}, {3}};
73+
std::tuple<short> tps[] = {{short{1}}, {short{2}}, {short{3}}};
7474
auto ev = tps | std::views::elements<0>;
7575
auto expected = {1, 2, 3};
7676
assert(std::ranges::equal(ev, expected));

libcxx/test/std/ranges/range.adaptors/range.elements/iterator/deref.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ constexpr void testValue(T t) {
5050
constexpr bool test() {
5151
// test tuple
5252
{
53-
std::tuple<int, short, long> ts[] = {{1, 2, 3}, {4, 5, 6}};
53+
std::tuple<int, short, long> ts[] = {{1, short{2}, 3}, {4, short{5}, 6}};
5454
testReference<0>(ts);
5555
testReference<1>(ts);
5656
testReference<2>(ts);
@@ -61,7 +61,7 @@ constexpr bool test() {
6161

6262
// test pair
6363
{
64-
std::pair<int, short> ps[] = {{1, 2}, {4, 5}};
64+
std::pair<int, short> ps[] = {{1, short{2}}, {4, short{5}}};
6565
testReference<0>(ps);
6666
testReference<1>(ps);
6767
testValue<0>(ps[0]);

libcxx/test/std/ranges/range.adaptors/range.elements/iterator/member_types.compile.pass.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ static_assert(std::same_as<ElementsIter<Range<contiguous_iterator<std::tuple<int
6464
static_assert(std::same_as<ElementsIter<Range<std::tuple<int>*>>::iterator_category, //
6565
std::random_access_iterator_tag>);
6666

67-
using Generator = decltype(std::views::iota(0, 1) | std::views::transform([](int) {
68-
return std::pair<int, short>{1, 1};
69-
}));
67+
using Generator = decltype(std::views::iota(0, 1) | std::views::transform([](int) { return std::pair{1, short{1}}; }));
7068
static_assert(std::ranges::random_access_range<Generator>);
7169

7270
static_assert(std::same_as<ElementsIter<Generator>::iterator_category, //

libcxx/test/std/thread/thread.threads/thread.thread.class/thread.thread.constr/F.pass.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ void* operator new(std::size_t s) TEST_THROW_SPEC(std::bad_alloc)
3636
} while (!throw_one.compare_exchange_weak(expected, expected - 1));
3737
++outstanding_new;
3838
void* ret = std::malloc(s);
39-
if (!ret) std::abort(); // placate MSVC's unchecked malloc warning
39+
if (!ret) {
40+
std::abort(); // placate MSVC's unchecked malloc warning (assert() won't silence it)
41+
}
4042
return ret;
4143
}
4244

libcxx/test/std/utilities/function.objects/refwrap/refwrap.const/type_conv_ctor.pass.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ struct convertible_from_int {
3737
void meow(std::reference_wrapper<int>) {}
3838
void meow(convertible_from_int) {}
3939

40-
int main(int, char**)
41-
{
40+
std::reference_wrapper<int> purr();
41+
42+
int main(int, char**) {
4243
{
4344
convertible_to_int_ref t;
4445
std::reference_wrapper<convertible_to_int_ref> r(t);
@@ -54,21 +55,18 @@ int main(int, char**)
5455
ASSERT_NOEXCEPT(Ref(nothrow_convertible<true>()));
5556
ASSERT_NOT_NOEXCEPT(Ref(nothrow_convertible<false>()));
5657
}
57-
{
58-
meow(0);
59-
}
60-
{
61-
extern std::reference_wrapper<int> purr();
62-
ASSERT_SAME_TYPE(decltype(true ? purr() : 0), int);
63-
}
58+
meow(0);
59+
ASSERT_SAME_TYPE(decltype(true ? purr() : 0), int);
6460
#if TEST_STD_VER > 14
6561
{
6662
int i = 0;
6763
std::reference_wrapper ri(i);
68-
static_assert((std::is_same<decltype(ri), std::reference_wrapper<int>>::value), "" );
64+
static_assert((std::is_same<decltype(ri), std::reference_wrapper<int>>::value), "");
65+
}
66+
{
6967
const int j = 0;
7068
std::reference_wrapper rj(j);
71-
static_assert((std::is_same<decltype(rj), std::reference_wrapper<const int>>::value), "" );
69+
static_assert((std::is_same<decltype(rj), std::reference_wrapper<const int>>::value), "");
7270
}
7371
#endif
7472

libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/make_shared_for_overwrite.pass.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ constexpr char pattern = static_cast<char>(0xDE);
6060

6161
void* operator new(std::size_t count) {
6262
void* ptr = std::malloc(count);
63+
if (!ptr) {
64+
std::abort(); // placate MSVC's unchecked malloc warning (assert() won't silence it)
65+
}
6366
for (std::size_t i = 0; i < count; ++i) {
6467
*(reinterpret_cast<char*>(ptr) + i) = pattern;
6568
}

libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.create/make_unique_for_overwrite.default_init.pass.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ constexpr char pattern = static_cast<char>(0xDE);
2727

2828
void* operator new(std::size_t count) {
2929
void* ptr = std::malloc(count);
30+
if (!ptr) {
31+
std::abort(); // placate MSVC's unchecked malloc warning (assert() won't silence it)
32+
}
3033
for (std::size_t i = 0; i < count; ++i) {
3134
*(reinterpret_cast<char*>(ptr) + i) = pattern;
3235
}

0 commit comments

Comments
 (0)