STL_MSFT created this revision.
STL_MSFT added reviewers: EricWF, mclow.lists.
STL_MSFT added a subscriber: cfe-commits.

Fix an MSVC x64 compiler warning, "warning C4312: 'type cast': conversion from 
'unsigned int' to 'int *' of greater size".

The warning (which is valuable) is simple to avoid: widen to uintptr_t, then 
cast. I'm additionally using C++ casts for clarity.

https://reviews.llvm.org/D22971

Files:
  
test/std/utilities/memory/allocator.traits/allocator.traits.members/allocate.pass.cpp
  
test/std/utilities/memory/allocator.traits/allocator.traits.members/allocate_hint.pass.cpp
  
test/std/utilities/memory/allocator.traits/allocator.traits.members/deallocate.pass.cpp

Index: test/std/utilities/memory/allocator.traits/allocator.traits.members/deallocate.pass.cpp
===================================================================
--- test/std/utilities/memory/allocator.traits/allocator.traits.members/deallocate.pass.cpp
+++ test/std/utilities/memory/allocator.traits/allocator.traits.members/deallocate.pass.cpp
@@ -17,6 +17,7 @@
 // };
 
 #include <memory>
+#include <cstdint>
 #include <cassert>
 
 int called = 0;
@@ -28,15 +29,15 @@
 
     void deallocate(value_type* p, std::size_t n)
     {
-        assert(p == (value_type*)0xDEADBEEF);
+        assert(p == reinterpret_cast<value_type*>(static_cast<std::uintptr_t>(0xDEADBEEF)));
         assert(n == 10);
         ++called;
     }
 };
 
 int main()
 {
     A<int> a;
-    std::allocator_traits<A<int> >::deallocate(a, (int*)0xDEADBEEF, 10);
+    std::allocator_traits<A<int> >::deallocate(a, reinterpret_cast<int*>(static_cast<std::uintptr_t>(0xDEADBEEF)), 10);
     assert(called == 1);
 }
Index: test/std/utilities/memory/allocator.traits/allocator.traits.members/allocate_hint.pass.cpp
===================================================================
--- test/std/utilities/memory/allocator.traits/allocator.traits.members/allocate_hint.pass.cpp
+++ test/std/utilities/memory/allocator.traits/allocator.traits.members/allocate_hint.pass.cpp
@@ -17,6 +17,7 @@
 // };
 
 #include <memory>
+#include <cstdint>
 #include <cassert>
 
 template <class T>
@@ -27,7 +28,7 @@
     value_type* allocate(std::size_t n)
     {
         assert(n == 10);
-        return (value_type*)0xDEADBEEF;
+        return reinterpret_cast<value_type*>(static_cast<std::uintptr_t>(0xDEADBEEF));
     }
 };
 
@@ -39,22 +40,22 @@
     value_type* allocate(std::size_t n)
     {
         assert(n == 12);
-        return (value_type*)0xEEADBEEF;
+        return reinterpret_cast<value_type*>(static_cast<std::uintptr_t>(0xEEADBEEF));
     }
     value_type* allocate(std::size_t n, const void* p)
     {
         assert(n == 11);
         assert(p == 0);
-        return (value_type*)0xFEADBEEF;
+        return reinterpret_cast<value_type*>(static_cast<std::uintptr_t>(0xFEADBEEF));
     }
 };
 
 int main()
 {
 #ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
     A<int> a;
-    assert(std::allocator_traits<A<int> >::allocate(a, 10, nullptr) == (int*)0xDEADBEEF);
+    assert(std::allocator_traits<A<int> >::allocate(a, 10, nullptr) == reinterpret_cast<int*>(static_cast<std::uintptr_t>(0xDEADBEEF)));
 #endif  // _LIBCPP_HAS_NO_ADVANCED_SFINAE
     B<int> b;
-    assert(std::allocator_traits<B<int> >::allocate(b, 11, nullptr) == (int*)0xFEADBEEF);
+    assert(std::allocator_traits<B<int> >::allocate(b, 11, nullptr) == reinterpret_cast<int*>(static_cast<std::uintptr_t>(0xFEADBEEF)));
 }
Index: test/std/utilities/memory/allocator.traits/allocator.traits.members/allocate.pass.cpp
===================================================================
--- test/std/utilities/memory/allocator.traits/allocator.traits.members/allocate.pass.cpp
+++ test/std/utilities/memory/allocator.traits/allocator.traits.members/allocate.pass.cpp
@@ -17,6 +17,7 @@
 // };
 
 #include <memory>
+#include <cstdint>
 #include <cassert>
 
 template <class T>
@@ -27,12 +28,12 @@
     value_type* allocate(std::size_t n)
     {
         assert(n == 10);
-        return (value_type*)0xDEADBEEF;
+        return reinterpret_cast<value_type*>(static_cast<std::uintptr_t>(0xDEADBEEF));
     }
 };
 
 int main()
 {
     A<int> a;
-    assert(std::allocator_traits<A<int> >::allocate(a, 10) == (int*)0xDEADBEEF);
+    assert(std::allocator_traits<A<int> >::allocate(a, 10) == reinterpret_cast<int*>(static_cast<std::uintptr_t>(0xDEADBEEF)));
 }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to