Skip to content

Commit 3d26ee2

Browse files
committedJan 18, 2017
[libcxx] [test] Fix MSVC warnings C4127 and C6326 about constants.
MSVC has compiler warnings C4127 "conditional expression is constant" (enabled by /W4) and C6326 "Potential comparison of a constant with another constant" (enabled by /analyze). They're potentially useful, although they're slightly annoying to library devs who know what they're doing. In the latest version of the compiler, C4127 is suppressed when the compiler sees simple tests like "if (name_of_thing)", so extracting comparison expressions into named constants is a workaround. At the same time, using std::integral_constant avoids C6326, which doesn't look at template arguments. test/std/containers/sequences/vector.bool/emplace.pass.cpp Replace 1 == 1 with true, which is the same as far as the library is concerned. Fixes D28837. llvm-svn: 292432
1 parent 20a0093 commit 3d26ee2

File tree

12 files changed

+43
-20
lines changed

12 files changed

+43
-20
lines changed
 

‎libcxx/test/std/containers/sequences/vector.bool/emplace.pass.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ int main()
3434
assert(c.front() == false);
3535
assert(c.back() == true);
3636

37-
i = c.emplace(c.cbegin()+1, 1 == 1);
37+
i = c.emplace(c.cbegin()+1, true);
3838
assert(i == c.begin()+1);
3939
assert(c.size() == 3);
4040
assert(c.front() == false);
@@ -56,7 +56,7 @@ int main()
5656
assert(c.front() == false);
5757
assert(c.back() == true);
5858

59-
i = c.emplace(c.cbegin()+1, 1 == 1);
59+
i = c.emplace(c.cbegin()+1, true);
6060
assert(i == c.begin()+1);
6161
assert(c.size() == 3);
6262
assert(c.size() == 3);

‎libcxx/test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters.arithmetic/minus1.pass.cpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <sstream>
2525
#include <ios>
26+
#include <type_traits>
2627
#include <cctype>
2728
#include <cstdint>
2829
#include <cassert>
@@ -66,11 +67,15 @@ int main()
6667
test_octal<uint64_t>("1777777777777777777777");
6768
test_octal< int64_t>("1777777777777777777777");
6869
test_octal<uint64_t>("1777777777777777777777");
69-
if (sizeof(long) == sizeof(int64_t)) {
70+
71+
const bool long_is_64 = std::integral_constant<bool, sizeof(long) == sizeof(int64_t)>::value; // avoid compiler warnings
72+
const bool long_long_is_64 = std::integral_constant<bool, sizeof(long long) == sizeof(int64_t)>::value; // avoid compiler warnings
73+
74+
if (long_is_64) {
7075
test_octal< unsigned long>("1777777777777777777777");
7176
test_octal< long>("1777777777777777777777");
7277
}
73-
if (sizeof(long long) == sizeof(int64_t)) {
78+
if (long_long_is_64) {
7479
test_octal< unsigned long long>("1777777777777777777777");
7580
test_octal< long long>("1777777777777777777777");
7681
}
@@ -81,11 +86,11 @@ int main()
8186
test_dec< int32_t>( "-1");
8287
test_dec<uint64_t>("18446744073709551615");
8388
test_dec< int64_t>( "-1");
84-
if (sizeof(long) == sizeof(int64_t)) {
89+
if (long_is_64) {
8590
test_dec<unsigned long>("18446744073709551615");
8691
test_dec< long>( "-1");
8792
}
88-
if (sizeof(long long) == sizeof(int64_t)) {
93+
if (long_long_is_64) {
8994
test_dec<unsigned long long>("18446744073709551615");
9095
test_dec< long long>( "-1");
9196
}
@@ -96,11 +101,11 @@ int main()
96101
test_hex< int32_t>( "FFFFFFFF");
97102
test_hex<uint64_t>("FFFFFFFFFFFFFFFF");
98103
test_hex< int64_t>("FFFFFFFFFFFFFFFF");
99-
if (sizeof(long) == sizeof(int64_t)) {
104+
if (long_is_64) {
100105
test_hex<unsigned long>("FFFFFFFFFFFFFFFF");
101106
test_hex< long>("FFFFFFFFFFFFFFFF");
102107
}
103-
if (sizeof(long long) == sizeof(int64_t)) {
108+
if (long_long_is_64) {
104109
test_hex<unsigned long long>("FFFFFFFFFFFFFFFF");
105110
test_hex< long long>("FFFFFFFFFFFFFFFF");
106111
}

‎libcxx/test/std/utilities/function.objects/unord.hash/enum.pass.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ test()
4343
for (int i = 0; i <= 5; ++i)
4444
{
4545
T t(static_cast<T> (i));
46-
if (sizeof(T) <= sizeof(std::size_t))
46+
const bool small = std::integral_constant<bool, sizeof(T) <= sizeof(std::size_t)>::value; // avoid compiler warnings
47+
if (small)
4748
assert(h1(t) == h2(static_cast<under_type>(i)));
4849
}
4950
}

‎libcxx/test/std/utilities/function.objects/unord.hash/integral.pass.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ test()
3636
for (int i = 0; i <= 5; ++i)
3737
{
3838
T t(static_cast<T>(i));
39-
if (sizeof(T) <= sizeof(std::size_t))
39+
const bool small = std::integral_constant<bool, sizeof(T) <= sizeof(std::size_t)>::value; // avoid compiler warnings
40+
if (small)
4041
{
4142
const std::size_t result = h(t);
4243
LIBCPP_ASSERT(result == static_cast<size_t>(t));

‎libcxx/test/std/utilities/template.bitset/bitset.members/all.pass.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// test bool all() const;
1111

1212
#include <bitset>
13+
#include <type_traits>
1314
#include <cassert>
1415

1516
template <std::size_t N>
@@ -20,7 +21,8 @@ void test_all()
2021
assert(v.all() == (N == 0));
2122
v.set();
2223
assert(v.all() == true);
23-
if (N > 1)
24+
const bool greater_than_1 = std::integral_constant<bool, (N > 1)>::value; // avoid compiler warnings
25+
if (greater_than_1)
2426
{
2527
v[N/2] = false;
2628
assert(v.all() == false);

‎libcxx/test/std/utilities/template.bitset/bitset.members/any.pass.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// test bool any() const;
1111

1212
#include <bitset>
13+
#include <type_traits>
1314
#include <cassert>
1415

1516
template <std::size_t N>
@@ -20,7 +21,8 @@ void test_any()
2021
assert(v.any() == false);
2122
v.set();
2223
assert(v.any() == (N != 0));
23-
if (N > 1)
24+
const bool greater_than_1 = std::integral_constant<bool, (N > 1)>::value; // avoid compiler warnings
25+
if (greater_than_1)
2426
{
2527
v[N/2] = false;
2628
assert(v.any() == true);

‎libcxx/test/std/utilities/template.bitset/bitset.members/index.pass.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// test bitset<N>::reference operator[](size_t pos);
1111

1212
#include <bitset>
13+
#include <type_traits>
1314
#include <cstdlib>
1415
#include <cassert>
1516

@@ -31,7 +32,8 @@ template <std::size_t N>
3132
void test_index_const()
3233
{
3334
std::bitset<N> v1 = make_bitset<N>();
34-
if (N > 0)
35+
const bool greater_than_0 = std::integral_constant<bool, (N > 0)>::value; // avoid compiler warnings
36+
if (greater_than_0)
3537
{
3638
assert(v1[N/2] == v1.test(N/2));
3739
typename std::bitset<N>::reference r = v1[N/2];

‎libcxx/test/std/utilities/template.bitset/bitset.members/index_const.pass.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// test constexpr bool operator[](size_t pos) const;
1111

1212
#include <bitset>
13+
#include <type_traits>
1314
#include <cstdlib>
1415
#include <cassert>
1516

@@ -31,7 +32,8 @@ template <std::size_t N>
3132
void test_index_const()
3233
{
3334
const std::bitset<N> v1 = make_bitset<N>();
34-
if (N > 0)
35+
const bool greater_than_0 = std::integral_constant<bool, (N > 0)>::value; // avoid compiler warnings
36+
if (greater_than_0)
3537
{
3638
assert(v1[N/2] == v1.test(N/2));
3739
}

‎libcxx/test/std/utilities/template.bitset/bitset.members/none.pass.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// test bool none() const;
1111

1212
#include <bitset>
13+
#include <type_traits>
1314
#include <cassert>
1415

1516
template <std::size_t N>
@@ -20,7 +21,8 @@ void test_none()
2021
assert(v.none() == true);
2122
v.set();
2223
assert(v.none() == (N == 0));
23-
if (N > 1)
24+
const bool greater_than_1 = std::integral_constant<bool, (N > 1)>::value; // avoid compiler warnings
25+
if (greater_than_1)
2426
{
2527
v[N/2] = false;
2628
assert(v.none() == false);

‎libcxx/test/std/utilities/template.bitset/bitset.members/op_eq_eq.pass.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// bool operator!=(const bitset<N>& rhs) const;
1414

1515
#include <bitset>
16+
#include <type_traits>
1617
#include <cstdlib>
1718
#include <cassert>
1819

@@ -36,7 +37,8 @@ void test_equality()
3637
const std::bitset<N> v1 = make_bitset<N>();
3738
std::bitset<N> v2 = v1;
3839
assert(v1 == v2);
39-
if (N > 0)
40+
const bool greater_than_0 = std::integral_constant<bool, (N > 0)>::value; // avoid compiler warnings
41+
if (greater_than_0)
4042
{
4143
v2[N/2].flip();
4244
assert(v1 != v2);

‎libcxx/test/std/utilities/template.bitset/bitset.members/to_ullong.pass.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@
1111

1212
#include <bitset>
1313
#include <algorithm>
14+
#include <type_traits>
1415
#include <climits>
1516
#include <cassert>
1617

1718
template <std::size_t N>
1819
void test_to_ullong()
1920
{
2021
const std::size_t M = sizeof(unsigned long long) * CHAR_BIT < N ? sizeof(unsigned long long) * CHAR_BIT : N;
21-
const std::size_t X = M == 0 ? sizeof(unsigned long long) * CHAR_BIT - 1 : sizeof(unsigned long long) * CHAR_BIT - M;
22-
const unsigned long long max = M == 0 ? 0 : (unsigned long long)(-1) >> X;
22+
const bool is_M_zero = std::integral_constant<bool, M == 0>::value; // avoid compiler warnings
23+
const std::size_t X = is_M_zero ? sizeof(unsigned long long) * CHAR_BIT - 1 : sizeof(unsigned long long) * CHAR_BIT - M;
24+
const unsigned long long max = is_M_zero ? 0 : (unsigned long long)(-1) >> X;
2325
unsigned long long tests[] = {0,
2426
std::min<unsigned long long>(1, max),
2527
std::min<unsigned long long>(2, max),

‎libcxx/test/std/utilities/template.bitset/bitset.members/to_ulong.pass.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <bitset>
1313
#include <algorithm>
14+
#include <type_traits>
1415
#include <limits>
1516
#include <climits>
1617
#include <cassert>
@@ -19,8 +20,9 @@ template <std::size_t N>
1920
void test_to_ulong()
2021
{
2122
const std::size_t M = sizeof(unsigned long) * CHAR_BIT < N ? sizeof(unsigned long) * CHAR_BIT : N;
22-
const std::size_t X = M == 0 ? sizeof(unsigned long) * CHAR_BIT - 1 : sizeof(unsigned long) * CHAR_BIT - M;
23-
const std::size_t max = M == 0 ? 0 : std::size_t(std::numeric_limits<unsigned long>::max()) >> X;
23+
const bool is_M_zero = std::integral_constant<bool, M == 0>::value; // avoid compiler warnings
24+
const std::size_t X = is_M_zero ? sizeof(unsigned long) * CHAR_BIT - 1 : sizeof(unsigned long) * CHAR_BIT - M;
25+
const std::size_t max = is_M_zero ? 0 : std::size_t(std::numeric_limits<unsigned long>::max()) >> X;
2426
std::size_t tests[] = {0,
2527
std::min<std::size_t>(1, max),
2628
std::min<std::size_t>(2, max),

0 commit comments

Comments
 (0)
Please sign in to comment.