Skip to content

[libc++][test] Fix more MSVC and Clang warnings #74965

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 13 commits into from
Dec 10, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ void test_complexity() {
const int debug_elements = std::min(100, n);
// Multiplier 2 because of comp(a,b) comp(b, a) checks.
const int debug_comparisons = 2 * (debug_elements + 1) * debug_elements;
(void)debug_comparisons;
std::shuffle(first, last, g);
std::make_heap(first, last, &MyInt::Comp);
// The exact stats of our current implementation are recorded here.
Expand All @@ -247,7 +248,6 @@ void test_complexity() {
LIBCPP_ASSERT(stats.moved <= 2 * n + n * logn);
#if _LIBCPP_HARDENING_MODE != _LIBCPP_HARDENING_MODE_DEBUG
LIBCPP_ASSERT(stats.compared <= n * logn);
(void)debug_comparisons;
#else
LIBCPP_ASSERT(stats.compared <= 2 * n * logn + debug_comparisons);
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

template <class BufferPolicy>
void test_read(BufferPolicy policy, const std::vector<std::streamsize>& payload_sizes) {
std::streamsize total_size = std::accumulate(payload_sizes.begin(), payload_sizes.end(), 0);
std::streamsize total_size = std::accumulate(payload_sizes.begin(), payload_sizes.end(), std::streamsize{0});
std::vector<char> data(total_size);
for (std::size_t i = 0; i < data.size(); ++i) {
data[i] = static_cast<char>(i % (1 << 8 * sizeof(char)));
Expand Down Expand Up @@ -99,7 +99,7 @@ void test_read(BufferPolicy policy, const std::vector<std::streamsize>& payload_
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
template <class BufferPolicy>
void test_read_codecvt(BufferPolicy policy, const std::vector<std::streamsize>& payload_sizes) {
std::streamsize total_size = std::accumulate(payload_sizes.begin(), payload_sizes.end(), 0);
std::streamsize total_size = std::accumulate(payload_sizes.begin(), payload_sizes.end(), std::streamsize{0});
std::vector<wchar_t> data(total_size);
for (std::size_t i = 0; i < data.size(); ++i) {
data[i] = static_cast<wchar_t>(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
template <class BufferPolicy>
void test_write(BufferPolicy policy, const std::vector<std::streamsize>& payload_sizes) {
std::size_t previously_written = 0;
std::streamsize total_size = std::accumulate(payload_sizes.begin(), payload_sizes.end(), 0);
std::streamsize total_size = std::accumulate(payload_sizes.begin(), payload_sizes.end(), std::streamsize{0});
std::vector<char> data(total_size);
for (std::size_t i = 0; i < data.size(); ++i) {
data[i] = static_cast<char>(i % (1 << 8 * sizeof(char)));
Expand Down Expand Up @@ -97,7 +97,7 @@ void test_write(BufferPolicy policy, const std::vector<std::streamsize>& payload
template <class BufferPolicy>
void test_write_codecvt(BufferPolicy policy, const std::vector<std::streamsize>& payload_sizes) {
std::size_t previously_written = 0;
std::streamsize total_size = std::accumulate(payload_sizes.begin(), payload_sizes.end(), 0);
std::streamsize total_size = std::accumulate(payload_sizes.begin(), payload_sizes.end(), std::streamsize{0});
std::vector<wchar_t> data(total_size);
for (std::size_t i = 0; i < data.size(); ++i) {
data[i] = static_cast<wchar_t>(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ int main(int, char**) {
typedef fs::path::format E;
static_assert(std::is_enum<E>::value, "");

typedef std::underlying_type<E>::type UT;

LIBCPP_ONLY(static_assert(std::is_same<UT, unsigned char>::value, "")); // Implementation detail
LIBCPP_STATIC_ASSERT(std::is_same<std::underlying_type<E>::type, unsigned char>::value, ""); // Implementation detail

static_assert(
E::auto_format != E::native_format &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ static void test_short_write_after_swap() {
sync_buf2.sputn(expected.data(), expected.size());

sync_buf1.swap(sync_buf2);
expected.push_back(sync_buf1.sputc(CharT('B')));
sync_buf1.sputc(CharT('B'));
expected.push_back(CharT('B'));
sync_buf2.sputc(CharT('Z'));

assert(sstr1.str().empty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ TEST_WORKAROUND_BUG_109234844_WEAK
void* operator new(std::size_t s) TEST_THROW_SPEC(std::bad_alloc) {
++new_called;
void* ret = std::malloc(s);
if (!ret) std::abort(); // placate MSVC's unchecked malloc warning
if (!ret) {
std::abort(); // placate MSVC's unchecked malloc warning (assert() won't silence it)
}
return ret;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ int delete_called = 0;
void* operator new[](std::size_t s) TEST_THROW_SPEC(std::bad_alloc) {
++new_called;
void* ret = std::malloc(s);
if (!ret) std::abort(); // placate MSVC's unchecked malloc warning
if (!ret) {
std::abort(); // placate MSVC's unchecked malloc warning (assert() won't silence it)
}
return ret;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ TEST_WORKAROUND_BUG_109234844_WEAK
void* operator new[](std::size_t s) TEST_THROW_SPEC(std::bad_alloc) {
++new_called;
void* ret = std::malloc(s);
if (!ret) std::abort(); // placate MSVC's unchecked malloc warning
if (!ret) {
std::abort(); // placate MSVC's unchecked malloc warning (assert() won't silence it)
}
return ret;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ int delete_called = 0;
void* operator new(std::size_t s) TEST_THROW_SPEC(std::bad_alloc) {
++new_called;
void* ret = std::malloc(s);
if (!ret) std::abort(); // placate MSVC's unchecked malloc warning
if (!ret) {
std::abort(); // placate MSVC's unchecked malloc warning (assert() won't silence it)
}
return ret;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ int main(int, char**) {
assert(reinterpret_cast<std::uintptr_t>(x) % alignof(TrackLifetimeOverAligned) == 0);
assert(info.address_constructed == x);

const auto old_x = x;
delete x;
assert(info.address_destroyed == x);
assert(info.address_destroyed == old_x);
}

return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ int main(int, char**) {
assert(x != nullptr);
assert(info.address_constructed == x);

const auto old_x = x;
delete x;
assert(info.address_destroyed == x);
assert(info.address_destroyed == old_x);
}

return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ TEST_WORKAROUND_BUG_109234844_WEAK
void* operator new(std::size_t s) TEST_THROW_SPEC(std::bad_alloc) {
++new_called;
void* ret = std::malloc(s);
if (!ret) std::abort(); // placate MSVC's unchecked malloc warning
if (!ret) {
std::abort(); // placate MSVC's unchecked malloc warning (assert() won't silence it)
}
return ret;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ int main(int, char**) {

// tuple
{
std::tuple<short> tps[] = {{1}, {2}, {3}};
std::tuple<short> tps[] = {{short{1}}, {short{2}}, {short{3}}};
auto ev = tps | std::views::elements<0>;
auto expected = {1, 2, 3};
assert(std::ranges::equal(ev, expected));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ constexpr void testValue(T t) {
constexpr bool test() {
// test tuple
{
std::tuple<int, short, long> ts[] = {{1, 2, 3}, {4, 5, 6}};
std::tuple<int, short, long> ts[] = {{1, short{2}, 3}, {4, short{5}, 6}};
testReference<0>(ts);
testReference<1>(ts);
testReference<2>(ts);
Expand All @@ -61,7 +61,7 @@ constexpr bool test() {

// test pair
{
std::pair<int, short> ps[] = {{1, 2}, {4, 5}};
std::pair<int, short> ps[] = {{1, short{2}}, {4, short{5}}};
testReference<0>(ps);
testReference<1>(ps);
testValue<0>(ps[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ static_assert(std::same_as<ElementsIter<Range<contiguous_iterator<std::tuple<int
static_assert(std::same_as<ElementsIter<Range<std::tuple<int>*>>::iterator_category, //
std::random_access_iterator_tag>);

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

static_assert(std::same_as<ElementsIter<Generator>::iterator_category, //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ void* operator new(std::size_t s) TEST_THROW_SPEC(std::bad_alloc)
} while (!throw_one.compare_exchange_weak(expected, expected - 1));
++outstanding_new;
void* ret = std::malloc(s);
if (!ret) std::abort(); // placate MSVC's unchecked malloc warning
if (!ret) {
std::abort(); // placate MSVC's unchecked malloc warning (assert() won't silence it)
}
return ret;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ struct convertible_from_int {
void meow(std::reference_wrapper<int>) {}
void meow(convertible_from_int) {}

int main(int, char**)
{
std::reference_wrapper<int> purr();

int main(int, char**) {
{
convertible_to_int_ref t;
std::reference_wrapper<convertible_to_int_ref> r(t);
Expand All @@ -54,21 +55,18 @@ int main(int, char**)
ASSERT_NOEXCEPT(Ref(nothrow_convertible<true>()));
ASSERT_NOT_NOEXCEPT(Ref(nothrow_convertible<false>()));
}
{
meow(0);
}
{
extern std::reference_wrapper<int> purr();
ASSERT_SAME_TYPE(decltype(true ? purr() : 0), int);
}
meow(0);
ASSERT_SAME_TYPE(decltype(true ? purr() : 0), int);
#if TEST_STD_VER > 14
{
int i = 0;
std::reference_wrapper ri(i);
static_assert((std::is_same<decltype(ri), std::reference_wrapper<int>>::value), "" );
static_assert((std::is_same<decltype(ri), std::reference_wrapper<int>>::value), "");
}
{
const int j = 0;
std::reference_wrapper rj(j);
static_assert((std::is_same<decltype(rj), std::reference_wrapper<const int>>::value), "" );
static_assert((std::is_same<decltype(rj), std::reference_wrapper<const int>>::value), "");
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ constexpr char pattern = 0xDE;

void* operator new(std::size_t count) {
void* ptr = std::malloc(count);
if (!ptr) {
std::abort(); // placate MSVC's unchecked malloc warning (assert() won't silence it)
}
for (std::size_t i = 0; i < count; ++i) {
*(reinterpret_cast<char*>(ptr) + i) = pattern;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ constexpr char pattern = 0xDE;

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