[libcxx] r292432 - [libcxx] [test] Fix MSVC warnings C4127 and C6326 about constants.

Stephan T. Lavavej via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 18 12:09:56 PST 2017


Author: stl_msft
Date: Wed Jan 18 14:09:56 2017
New Revision: 292432

URL: http://llvm.org/viewvc/llvm-project?rev=292432&view=rev
Log:
[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.

Modified:
    libcxx/trunk/test/std/containers/sequences/vector.bool/emplace.pass.cpp
    libcxx/trunk/test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters.arithmetic/minus1.pass.cpp
    libcxx/trunk/test/std/utilities/function.objects/unord.hash/enum.pass.cpp
    libcxx/trunk/test/std/utilities/function.objects/unord.hash/integral.pass.cpp
    libcxx/trunk/test/std/utilities/template.bitset/bitset.members/all.pass.cpp
    libcxx/trunk/test/std/utilities/template.bitset/bitset.members/any.pass.cpp
    libcxx/trunk/test/std/utilities/template.bitset/bitset.members/index.pass.cpp
    libcxx/trunk/test/std/utilities/template.bitset/bitset.members/index_const.pass.cpp
    libcxx/trunk/test/std/utilities/template.bitset/bitset.members/none.pass.cpp
    libcxx/trunk/test/std/utilities/template.bitset/bitset.members/op_eq_eq.pass.cpp
    libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ullong.pass.cpp
    libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ulong.pass.cpp

Modified: libcxx/trunk/test/std/containers/sequences/vector.bool/emplace.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/sequences/vector.bool/emplace.pass.cpp?rev=292432&r1=292431&r2=292432&view=diff
==============================================================================
--- libcxx/trunk/test/std/containers/sequences/vector.bool/emplace.pass.cpp (original)
+++ libcxx/trunk/test/std/containers/sequences/vector.bool/emplace.pass.cpp Wed Jan 18 14:09:56 2017
@@ -34,7 +34,7 @@ int main()
         assert(c.front() == false);
         assert(c.back() == true);
 
-        i = c.emplace(c.cbegin()+1, 1 == 1);
+        i = c.emplace(c.cbegin()+1, true);
         assert(i == c.begin()+1);
         assert(c.size() == 3);
         assert(c.front() == false);
@@ -56,7 +56,7 @@ int main()
         assert(c.front() == false);
         assert(c.back() == true);
 
-        i = c.emplace(c.cbegin()+1, 1 == 1);
+        i = c.emplace(c.cbegin()+1, true);
         assert(i == c.begin()+1);
         assert(c.size() == 3);
         assert(c.size() == 3);

Modified: libcxx/trunk/test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters.arithmetic/minus1.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters.arithmetic/minus1.pass.cpp?rev=292432&r1=292431&r2=292432&view=diff
==============================================================================
--- libcxx/trunk/test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters.arithmetic/minus1.pass.cpp (original)
+++ libcxx/trunk/test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters.arithmetic/minus1.pass.cpp Wed Jan 18 14:09:56 2017
@@ -23,6 +23,7 @@
 
 #include <sstream>
 #include <ios>
+#include <type_traits>
 #include <cctype>
 #include <cstdint>
 #include <cassert>
@@ -66,11 +67,15 @@ int main()
     test_octal<uint64_t>("1777777777777777777777");
     test_octal< int64_t>("1777777777777777777777");
     test_octal<uint64_t>("1777777777777777777777");
-    if (sizeof(long) == sizeof(int64_t)) {
+
+    const bool long_is_64 = std::integral_constant<bool, sizeof(long) == sizeof(int64_t)>::value; // avoid compiler warnings
+    const bool long_long_is_64 = std::integral_constant<bool, sizeof(long long) == sizeof(int64_t)>::value; // avoid compiler warnings
+
+    if (long_is_64) {
         test_octal< unsigned long>("1777777777777777777777");
         test_octal<          long>("1777777777777777777777");
     }
-    if (sizeof(long long) == sizeof(int64_t)) {
+    if (long_long_is_64) {
         test_octal< unsigned long long>("1777777777777777777777");
         test_octal<          long long>("1777777777777777777777");
     }
@@ -81,11 +86,11 @@ int main()
     test_dec< int32_t>(                  "-1");
     test_dec<uint64_t>("18446744073709551615");
     test_dec< int64_t>(                  "-1");
-    if (sizeof(long) == sizeof(int64_t)) {
+    if (long_is_64) {
         test_dec<unsigned long>("18446744073709551615");
         test_dec<         long>(                  "-1");
     }
-    if (sizeof(long long) == sizeof(int64_t)) {
+    if (long_long_is_64) {
         test_dec<unsigned long long>("18446744073709551615");
         test_dec<         long long>(                  "-1");
     }
@@ -96,11 +101,11 @@ int main()
     test_hex< int32_t>(        "FFFFFFFF");
     test_hex<uint64_t>("FFFFFFFFFFFFFFFF");
     test_hex< int64_t>("FFFFFFFFFFFFFFFF");
-    if (sizeof(long) == sizeof(int64_t)) {
+    if (long_is_64) {
         test_hex<unsigned long>("FFFFFFFFFFFFFFFF");
         test_hex<         long>("FFFFFFFFFFFFFFFF");
     }
-    if (sizeof(long long) == sizeof(int64_t)) {
+    if (long_long_is_64) {
         test_hex<unsigned long long>("FFFFFFFFFFFFFFFF");
         test_hex<         long long>("FFFFFFFFFFFFFFFF");
     }

Modified: libcxx/trunk/test/std/utilities/function.objects/unord.hash/enum.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/utilities/function.objects/unord.hash/enum.pass.cpp?rev=292432&r1=292431&r2=292432&view=diff
==============================================================================
--- libcxx/trunk/test/std/utilities/function.objects/unord.hash/enum.pass.cpp (original)
+++ libcxx/trunk/test/std/utilities/function.objects/unord.hash/enum.pass.cpp Wed Jan 18 14:09:56 2017
@@ -43,7 +43,8 @@ test()
     for (int i = 0; i <= 5; ++i)
     {
         T t(static_cast<T> (i));
-        if (sizeof(T) <= sizeof(std::size_t))
+        const bool small = std::integral_constant<bool, sizeof(T) <= sizeof(std::size_t)>::value; // avoid compiler warnings
+        if (small)
             assert(h1(t) == h2(static_cast<under_type>(i)));
     }
 }

Modified: libcxx/trunk/test/std/utilities/function.objects/unord.hash/integral.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/utilities/function.objects/unord.hash/integral.pass.cpp?rev=292432&r1=292431&r2=292432&view=diff
==============================================================================
--- libcxx/trunk/test/std/utilities/function.objects/unord.hash/integral.pass.cpp (original)
+++ libcxx/trunk/test/std/utilities/function.objects/unord.hash/integral.pass.cpp Wed Jan 18 14:09:56 2017
@@ -36,7 +36,8 @@ test()
     for (int i = 0; i <= 5; ++i)
     {
         T t(static_cast<T>(i));
-        if (sizeof(T) <= sizeof(std::size_t))
+        const bool small = std::integral_constant<bool, sizeof(T) <= sizeof(std::size_t)>::value; // avoid compiler warnings
+        if (small)
         {
             const std::size_t result = h(t);
             LIBCPP_ASSERT(result == static_cast<size_t>(t));

Modified: libcxx/trunk/test/std/utilities/template.bitset/bitset.members/all.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/utilities/template.bitset/bitset.members/all.pass.cpp?rev=292432&r1=292431&r2=292432&view=diff
==============================================================================
--- libcxx/trunk/test/std/utilities/template.bitset/bitset.members/all.pass.cpp (original)
+++ libcxx/trunk/test/std/utilities/template.bitset/bitset.members/all.pass.cpp Wed Jan 18 14:09:56 2017
@@ -10,6 +10,7 @@
 // test bool all() const;
 
 #include <bitset>
+#include <type_traits>
 #include <cassert>
 
 template <std::size_t N>
@@ -20,7 +21,8 @@ void test_all()
     assert(v.all() == (N == 0));
     v.set();
     assert(v.all() == true);
-    if (N > 1)
+    const bool greater_than_1 = std::integral_constant<bool, (N > 1)>::value; // avoid compiler warnings
+    if (greater_than_1)
     {
         v[N/2] = false;
         assert(v.all() == false);

Modified: libcxx/trunk/test/std/utilities/template.bitset/bitset.members/any.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/utilities/template.bitset/bitset.members/any.pass.cpp?rev=292432&r1=292431&r2=292432&view=diff
==============================================================================
--- libcxx/trunk/test/std/utilities/template.bitset/bitset.members/any.pass.cpp (original)
+++ libcxx/trunk/test/std/utilities/template.bitset/bitset.members/any.pass.cpp Wed Jan 18 14:09:56 2017
@@ -10,6 +10,7 @@
 // test bool any() const;
 
 #include <bitset>
+#include <type_traits>
 #include <cassert>
 
 template <std::size_t N>
@@ -20,7 +21,8 @@ void test_any()
     assert(v.any() == false);
     v.set();
     assert(v.any() == (N != 0));
-    if (N > 1)
+    const bool greater_than_1 = std::integral_constant<bool, (N > 1)>::value; // avoid compiler warnings
+    if (greater_than_1)
     {
         v[N/2] = false;
         assert(v.any() == true);

Modified: libcxx/trunk/test/std/utilities/template.bitset/bitset.members/index.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/utilities/template.bitset/bitset.members/index.pass.cpp?rev=292432&r1=292431&r2=292432&view=diff
==============================================================================
--- libcxx/trunk/test/std/utilities/template.bitset/bitset.members/index.pass.cpp (original)
+++ libcxx/trunk/test/std/utilities/template.bitset/bitset.members/index.pass.cpp Wed Jan 18 14:09:56 2017
@@ -10,6 +10,7 @@
 // test bitset<N>::reference operator[](size_t pos);
 
 #include <bitset>
+#include <type_traits>
 #include <cstdlib>
 #include <cassert>
 
@@ -31,7 +32,8 @@ template <std::size_t N>
 void test_index_const()
 {
     std::bitset<N> v1 = make_bitset<N>();
-    if (N > 0)
+    const bool greater_than_0 = std::integral_constant<bool, (N > 0)>::value; // avoid compiler warnings
+    if (greater_than_0)
     {
         assert(v1[N/2] == v1.test(N/2));
         typename std::bitset<N>::reference r = v1[N/2];

Modified: libcxx/trunk/test/std/utilities/template.bitset/bitset.members/index_const.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/utilities/template.bitset/bitset.members/index_const.pass.cpp?rev=292432&r1=292431&r2=292432&view=diff
==============================================================================
--- libcxx/trunk/test/std/utilities/template.bitset/bitset.members/index_const.pass.cpp (original)
+++ libcxx/trunk/test/std/utilities/template.bitset/bitset.members/index_const.pass.cpp Wed Jan 18 14:09:56 2017
@@ -10,6 +10,7 @@
 // test constexpr bool operator[](size_t pos) const;
 
 #include <bitset>
+#include <type_traits>
 #include <cstdlib>
 #include <cassert>
 
@@ -31,7 +32,8 @@ template <std::size_t N>
 void test_index_const()
 {
     const std::bitset<N> v1 = make_bitset<N>();
-    if (N > 0)
+    const bool greater_than_0 = std::integral_constant<bool, (N > 0)>::value; // avoid compiler warnings
+    if (greater_than_0)
     {
         assert(v1[N/2] == v1.test(N/2));
     }

Modified: libcxx/trunk/test/std/utilities/template.bitset/bitset.members/none.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/utilities/template.bitset/bitset.members/none.pass.cpp?rev=292432&r1=292431&r2=292432&view=diff
==============================================================================
--- libcxx/trunk/test/std/utilities/template.bitset/bitset.members/none.pass.cpp (original)
+++ libcxx/trunk/test/std/utilities/template.bitset/bitset.members/none.pass.cpp Wed Jan 18 14:09:56 2017
@@ -10,6 +10,7 @@
 // test bool none() const;
 
 #include <bitset>
+#include <type_traits>
 #include <cassert>
 
 template <std::size_t N>
@@ -20,7 +21,8 @@ void test_none()
     assert(v.none() == true);
     v.set();
     assert(v.none() == (N == 0));
-    if (N > 1)
+    const bool greater_than_1 = std::integral_constant<bool, (N > 1)>::value; // avoid compiler warnings
+    if (greater_than_1)
     {
         v[N/2] = false;
         assert(v.none() == false);

Modified: libcxx/trunk/test/std/utilities/template.bitset/bitset.members/op_eq_eq.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/utilities/template.bitset/bitset.members/op_eq_eq.pass.cpp?rev=292432&r1=292431&r2=292432&view=diff
==============================================================================
--- libcxx/trunk/test/std/utilities/template.bitset/bitset.members/op_eq_eq.pass.cpp (original)
+++ libcxx/trunk/test/std/utilities/template.bitset/bitset.members/op_eq_eq.pass.cpp Wed Jan 18 14:09:56 2017
@@ -13,6 +13,7 @@
 // bool operator!=(const bitset<N>& rhs) const;
 
 #include <bitset>
+#include <type_traits>
 #include <cstdlib>
 #include <cassert>
 
@@ -36,7 +37,8 @@ void test_equality()
     const std::bitset<N> v1 = make_bitset<N>();
     std::bitset<N> v2 = v1;
     assert(v1 == v2);
-    if (N > 0)
+    const bool greater_than_0 = std::integral_constant<bool, (N > 0)>::value; // avoid compiler warnings
+    if (greater_than_0)
     {
         v2[N/2].flip();
         assert(v1 != v2);

Modified: libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ullong.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ullong.pass.cpp?rev=292432&r1=292431&r2=292432&view=diff
==============================================================================
--- libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ullong.pass.cpp (original)
+++ libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ullong.pass.cpp Wed Jan 18 14:09:56 2017
@@ -11,6 +11,7 @@
 
 #include <bitset>
 #include <algorithm>
+#include <type_traits>
 #include <climits>
 #include <cassert>
 
@@ -18,8 +19,9 @@ template <std::size_t N>
 void test_to_ullong()
 {
     const std::size_t M = sizeof(unsigned long long) * CHAR_BIT < N ? sizeof(unsigned long long) * CHAR_BIT : N;
-    const std::size_t X = M == 0 ? sizeof(unsigned long long) * CHAR_BIT - 1 : sizeof(unsigned long long) * CHAR_BIT - M;
-    const unsigned long long max = M == 0 ? 0 : (unsigned long long)(-1) >> X;
+    const bool is_M_zero = std::integral_constant<bool, M == 0>::value; // avoid compiler warnings
+    const std::size_t X = is_M_zero ? sizeof(unsigned long long) * CHAR_BIT - 1 : sizeof(unsigned long long) * CHAR_BIT - M;
+    const unsigned long long max = is_M_zero ? 0 : (unsigned long long)(-1) >> X;
     unsigned long long tests[] = {0,
                            std::min<unsigned long long>(1, max),
                            std::min<unsigned long long>(2, max),

Modified: libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ulong.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ulong.pass.cpp?rev=292432&r1=292431&r2=292432&view=diff
==============================================================================
--- libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ulong.pass.cpp (original)
+++ libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ulong.pass.cpp Wed Jan 18 14:09:56 2017
@@ -11,6 +11,7 @@
 
 #include <bitset>
 #include <algorithm>
+#include <type_traits>
 #include <limits>
 #include <climits>
 #include <cassert>
@@ -19,8 +20,9 @@ template <std::size_t N>
 void test_to_ulong()
 {
     const std::size_t M = sizeof(unsigned long) * CHAR_BIT < N ? sizeof(unsigned long) * CHAR_BIT : N;
-    const std::size_t X = M == 0 ? sizeof(unsigned long) * CHAR_BIT - 1 : sizeof(unsigned long) * CHAR_BIT - M;
-    const std::size_t max = M == 0 ? 0 : std::size_t(std::numeric_limits<unsigned long>::max()) >> X;
+    const bool is_M_zero = std::integral_constant<bool, M == 0>::value; // avoid compiler warnings
+    const std::size_t X = is_M_zero ? sizeof(unsigned long) * CHAR_BIT - 1 : sizeof(unsigned long) * CHAR_BIT - M;
+    const std::size_t max = is_M_zero ? 0 : std::size_t(std::numeric_limits<unsigned long>::max()) >> X;
     std::size_t tests[] = {0,
                            std::min<std::size_t>(1, max),
                            std::min<std::size_t>(2, max),




More information about the cfe-commits mailing list