From f54bb68f47ec718ab9175a9956e44959d2976301 Mon Sep 17 00:00:00 2001 From: Rafael Cintron Date: Fri, 3 May 2019 00:58:27 +0000 Subject: [PATCH] Improve usage of static_casts over reinterpret_casts Static_casts are prefered over reinterpret_casts for better type safety Change-Id: I190cbee293591ebf8ab8035e900c081848eb1f30 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/6921 Commit-Queue: Kai Ninomiya Reviewed-by: Kai Ninomiya --- src/common/Math.h | 4 ++-- src/common/SwapChainUtils.h | 8 ++++---- src/dawn_native/BindGroup.cpp | 6 +++--- src/dawn_native/CommandAllocator.cpp | 2 +- src/dawn_native/CommandAllocator.h | 4 ++-- src/dawn_native/RenderPassEncoder.h | 3 +-- src/dawn_native/d3d12/SwapChainD3D12.cpp | 2 +- src/dawn_native/opengl/BackendGL.cpp | 2 +- src/dawn_native/opengl/ShaderModuleGL.cpp | 2 +- src/dawn_native/vulkan/MemoryAllocator.cpp | 2 +- src/dawn_wire/server/ServerBuffer.cpp | 2 +- src/tests/DawnTest.cpp | 4 ++-- 12 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/common/Math.h b/src/common/Math.h index 377f857be1..16cf2a15ae 100644 --- a/src/common/Math.h +++ b/src/common/Math.h @@ -34,12 +34,12 @@ uint32_t Align(uint32_t value, size_t alignment); template T* AlignPtr(T* ptr, size_t alignment) { - return reinterpret_cast(AlignVoidPtr(ptr, alignment)); + return static_cast(AlignVoidPtr(ptr, alignment)); } template const T* AlignPtr(const T* ptr, size_t alignment) { - return reinterpret_cast(AlignVoidPtr(const_cast(ptr), alignment)); + return static_cast(AlignVoidPtr(const_cast(ptr), alignment)); } template diff --git a/src/common/SwapChainUtils.h b/src/common/SwapChainUtils.h index 4010c3b3d3..a736227d79 100644 --- a/src/common/SwapChainUtils.h +++ b/src/common/SwapChainUtils.h @@ -22,18 +22,18 @@ DawnSwapChainImplementation CreateSwapChainImplementation(T* swapChain) { DawnSwapChainImplementation impl = {}; impl.userData = swapChain; impl.Init = [](void* userData, void* wsiContext) { - auto* ctx = reinterpret_cast(wsiContext); + auto* ctx = static_cast(wsiContext); reinterpret_cast(userData)->Init(ctx); }; impl.Destroy = [](void* userData) { delete reinterpret_cast(userData); }; impl.Configure = [](void* userData, DawnTextureFormat format, DawnTextureUsageBit allowedUsage, uint32_t width, uint32_t height) { - return reinterpret_cast(userData)->Configure(format, allowedUsage, width, height); + return static_cast(userData)->Configure(format, allowedUsage, width, height); }; impl.GetNextTexture = [](void* userData, DawnSwapChainNextTexture* nextTexture) { - return reinterpret_cast(userData)->GetNextTexture(nextTexture); + return static_cast(userData)->GetNextTexture(nextTexture); }; - impl.Present = [](void* userData) { return reinterpret_cast(userData)->Present(); }; + impl.Present = [](void* userData) { return static_cast(userData)->Present(); }; return impl; } diff --git a/src/dawn_native/BindGroup.cpp b/src/dawn_native/BindGroup.cpp index b776c911ca..8bc56c52b4 100644 --- a/src/dawn_native/BindGroup.cpp +++ b/src/dawn_native/BindGroup.cpp @@ -210,7 +210,7 @@ namespace dawn_native { ASSERT(mLayout->GetBindingInfo().mask[binding]); ASSERT(mLayout->GetBindingInfo().types[binding] == dawn::BindingType::UniformBuffer || mLayout->GetBindingInfo().types[binding] == dawn::BindingType::StorageBuffer); - BufferBase* buffer = reinterpret_cast(mBindings[binding].Get()); + BufferBase* buffer = static_cast(mBindings[binding].Get()); return {buffer, mOffsets[binding], mSizes[binding]}; } @@ -219,7 +219,7 @@ namespace dawn_native { ASSERT(binding < kMaxBindingsPerGroup); ASSERT(mLayout->GetBindingInfo().mask[binding]); ASSERT(mLayout->GetBindingInfo().types[binding] == dawn::BindingType::Sampler); - return reinterpret_cast(mBindings[binding].Get()); + return static_cast(mBindings[binding].Get()); } TextureViewBase* BindGroupBase::GetBindingAsTextureView(size_t binding) { @@ -227,7 +227,7 @@ namespace dawn_native { ASSERT(binding < kMaxBindingsPerGroup); ASSERT(mLayout->GetBindingInfo().mask[binding]); ASSERT(mLayout->GetBindingInfo().types[binding] == dawn::BindingType::SampledTexture); - return reinterpret_cast(mBindings[binding].Get()); + return static_cast(mBindings[binding].Get()); } } // namespace dawn_native diff --git a/src/dawn_native/CommandAllocator.cpp b/src/dawn_native/CommandAllocator.cpp index 6a09c1c876..990c1c5990 100644 --- a/src/dawn_native/CommandAllocator.cpp +++ b/src/dawn_native/CommandAllocator.cpp @@ -237,7 +237,7 @@ namespace dawn_native { mLastAllocationSize = std::max(minimumSize, std::min(mLastAllocationSize * 2, size_t(16384))); - uint8_t* block = reinterpret_cast(malloc(mLastAllocationSize)); + uint8_t* block = static_cast(malloc(mLastAllocationSize)); if (DAWN_UNLIKELY(block == nullptr)) { return false; } diff --git a/src/dawn_native/CommandAllocator.h b/src/dawn_native/CommandAllocator.h index 39446ceba8..ecede3ccbb 100644 --- a/src/dawn_native/CommandAllocator.h +++ b/src/dawn_native/CommandAllocator.h @@ -76,11 +76,11 @@ namespace dawn_native { } template T* NextCommand() { - return reinterpret_cast(NextCommand(sizeof(T), alignof(T))); + return static_cast(NextCommand(sizeof(T), alignof(T))); } template T* NextData(size_t count) { - return reinterpret_cast(NextData(sizeof(T) * count, alignof(T))); + return static_cast(NextData(sizeof(T) * count, alignof(T))); } // Needs to be called if iteration was stopped early. diff --git a/src/dawn_native/RenderPassEncoder.h b/src/dawn_native/RenderPassEncoder.h index 7e8243e26f..dc7b54fedc 100644 --- a/src/dawn_native/RenderPassEncoder.h +++ b/src/dawn_native/RenderPassEncoder.h @@ -55,8 +55,7 @@ namespace dawn_native { T* const* buffers, uint64_t const* offsets) { static_assert(std::is_base_of::value, ""); - SetVertexBuffers(startSlot, count, reinterpret_cast(buffers), - offsets); + SetVertexBuffers(startSlot, count, buffers, offsets); } void SetVertexBuffers(uint32_t startSlot, uint32_t count, diff --git a/src/dawn_native/d3d12/SwapChainD3D12.cpp b/src/dawn_native/d3d12/SwapChainD3D12.cpp index 2bfa789161..f02a79e30f 100644 --- a/src/dawn_native/d3d12/SwapChainD3D12.cpp +++ b/src/dawn_native/d3d12/SwapChainD3D12.cpp @@ -44,7 +44,7 @@ namespace dawn_native { namespace d3d12 { return nullptr; } - ID3D12Resource* nativeTexture = reinterpret_cast(next.texture.ptr); + ID3D12Resource* nativeTexture = static_cast(next.texture.ptr); return new Texture(ToBackend(GetDevice()), descriptor, nativeTexture); } diff --git a/src/dawn_native/opengl/BackendGL.cpp b/src/dawn_native/opengl/BackendGL.cpp index a74fef2db4..3233c5cd3a 100644 --- a/src/dawn_native/opengl/BackendGL.cpp +++ b/src/dawn_native/opengl/BackendGL.cpp @@ -67,7 +67,7 @@ namespace dawn_native { namespace opengl { ASSERT(optionsBase->backendType == BackendType::OpenGL); const AdapterDiscoveryOptions* options = - reinterpret_cast(optionsBase); + static_cast(optionsBase); if (options->getProc == nullptr) { return DAWN_VALIDATION_ERROR("AdapterDiscoveryOptions::getProc must be set"); diff --git a/src/dawn_native/opengl/ShaderModuleGL.cpp b/src/dawn_native/opengl/ShaderModuleGL.cpp index ae3d1f9ba2..0166b3f2d9 100644 --- a/src/dawn_native/opengl/ShaderModuleGL.cpp +++ b/src/dawn_native/opengl/ShaderModuleGL.cpp @@ -132,7 +132,7 @@ namespace dawn_native { namespace opengl { } const char* ShaderModule::GetSource() const { - return reinterpret_cast(mGlslSource.data()); + return mGlslSource.c_str(); } const ShaderModule::CombinedSamplerInfo& ShaderModule::GetCombinedSamplerInfo() const { diff --git a/src/dawn_native/vulkan/MemoryAllocator.cpp b/src/dawn_native/vulkan/MemoryAllocator.cpp index 6d7cf07d52..66a779e6f4 100644 --- a/src/dawn_native/vulkan/MemoryAllocator.cpp +++ b/src/dawn_native/vulkan/MemoryAllocator.cpp @@ -121,7 +121,7 @@ namespace dawn_native { namespace vulkan { allocation->mMemory = allocatedMemory; allocation->mOffset = 0; - allocation->mMappedPointer = reinterpret_cast(mappedPointer); + allocation->mMappedPointer = static_cast(mappedPointer); return true; } diff --git a/src/dawn_wire/server/ServerBuffer.cpp b/src/dawn_wire/server/ServerBuffer.cpp index 7899227fee..b5411773be 100644 --- a/src/dawn_wire/server/ServerBuffer.cpp +++ b/src/dawn_wire/server/ServerBuffer.cpp @@ -114,7 +114,7 @@ namespace dawn_wire { namespace server { cmd.requestSerial = data->requestSerial; cmd.status = status; cmd.dataLength = 0; - cmd.data = reinterpret_cast(ptr); + cmd.data = static_cast(ptr); if (status == DAWN_BUFFER_MAP_ASYNC_STATUS_SUCCESS) { cmd.dataLength = dataLength; diff --git a/src/tests/DawnTest.cpp b/src/tests/DawnTest.cpp index ee8c3e7877..1d3c8ff376 100644 --- a/src/tests/DawnTest.cpp +++ b/src/tests/DawnTest.cpp @@ -545,7 +545,7 @@ void DawnTest::ResolveExpectations() { // Get a pointer to the mapped copy of the data for the expectation. const char* data = - reinterpret_cast(mReadbackSlots[expectation.readbackSlot].mappedData); + static_cast(mReadbackSlots[expectation.readbackSlot].mappedData); data += expectation.readbackOffset; uint32_t size; @@ -652,7 +652,7 @@ namespace detail { testing::AssertionResult ExpectEq::Check(const void* data, size_t size) { DAWN_ASSERT(size == sizeof(T) * mExpected.size()); - const T* actual = reinterpret_cast(data); + const T* actual = static_cast(data); testing::AssertionResult failure = testing::AssertionFailure(); for (size_t i = 0; i < mExpected.size(); ++i) {