diff options
author | Marc Mutz <marc.mutz@qt.io> | 2022-09-22 11:39:44 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2022-10-15 22:11:47 +0200 |
commit | fc7676769251a27cbbc6d40d68f04bfe38511a5b (patch) | |
tree | 93427f2f2ca5ec328ff0c84ff061ca507796337f /src/gui/rhi | |
parent | 16dbbc8f8c93f28194b8b440b9616119ea2f7b45 (diff) |
Long live Q_UNREACHABLE_RETURN()!
This is a combination of Q_UNREACHABLE() with a return statement.
ATM, the return statement is unconditionally included. If we notice
that some compilers warn about return after __builtin_unreachable(),
then we can map Q_UNREACHABLE_RETURN(...) to Q_UNREACHABLE() without
having to touch all the code that uses explicit Q_UNREACHABLE() +
return.
The fact that Boost has BOOST_UNREACHABLE_RETURN() indicates that
there are compilers that complain about a lack of return after
Q_UNREACHABLE (we know that MSVC, ICC, and GHS are among them), as
well as compilers that complained about a return being present
(Coverity). Take this opportunity to properly adapt to Coverity, by
leaving out the return statement on this compiler.
Apply the macro around the code base, using a clang-tidy transformer
rule:
const std::string unr = "unr", val = "val", ret = "ret";
auto makeUnreachableReturn = cat("Q_UNREACHABLE_RETURN(",
ifBound(val, cat(node(val)), cat("")),
")");
auto ignoringSwitchCases = [](auto stmt) {
return anyOf(stmt, switchCase(subStmt(stmt)));
};
makeRule(
stmt(ignoringSwitchCases(stmt(isExpandedFromMacro("Q_UNREACHABLE")).bind(unr)),
nextStmt(returnStmt(optionally(hasReturnValue(expr().bind(val)))).bind(ret))),
{changeTo(node(unr), cat(makeUnreachableReturn,
";")), // TODO: why is the ; lost w/o this?
changeTo(node(ret), cat(""))},
cat("use ", makeUnreachableReturn))
);
where nextStmt() is copied from some upstream clang-tidy check's
private implementation and subStmt() is a private matcher that gives
access to SwitchCase's SubStmt.
A.k.a. qt-use-unreachable-return.
There were some false positives, suppressed them with NOLINTNEXTLINE.
They're not really false positiives, it's just that Clang sees the
world in one way and if conditonal compilation (#if) differs for other
compilers, Clang doesn't know better. This is an artifact of matching
two consecutive statements.
I haven't figured out how to remove the empty line left by the
deletion of the return statement, if it, indeed, was on a separate
line, so post-processed the patch to remove all the lines matching
^\+ *$ from the diff:
git commit -am meep
git reset --hard HEAD^
git diff HEAD..HEAD@{1} | sed '/^\+ *$/d' | recountdiff - | patch -p1
[ChangeLog][QtCore][QtAssert] Added Q_UNREACHABLE_RETURN() macro.
Change-Id: I9782939f16091c964f25b7826e1c0dbd13a71305
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/gui/rhi')
-rw-r--r-- | src/gui/rhi/qrhi.cpp | 24 | ||||
-rw-r--r-- | src/gui/rhi/qrhigles2.cpp | 51 | ||||
-rw-r--r-- | src/gui/rhi/qrhinull.cpp | 3 | ||||
-rw-r--r-- | src/gui/rhi/qrhivulkan.cpp | 57 |
4 files changed, 45 insertions, 90 deletions
diff --git a/src/gui/rhi/qrhi.cpp b/src/gui/rhi/qrhi.cpp index 3821062c8e..a52b953a37 100644 --- a/src/gui/rhi/qrhi.cpp +++ b/src/gui/rhi/qrhi.cpp @@ -1435,8 +1435,7 @@ QRhiVertexInputAttribute::Format QRhiImplementation::shaderDescVariableFormatToV return QRhiVertexInputAttribute::UInt; default: - Q_UNREACHABLE(); - return QRhiVertexInputAttribute::Float; + Q_UNREACHABLE_RETURN(QRhiVertexInputAttribute::Float); } } @@ -1478,8 +1477,7 @@ quint32 QRhiImplementation::byteSizePerVertexForVertexInputFormat(QRhiVertexInpu return sizeof(qint32); default: - Q_UNREACHABLE(); - return 1; + Q_UNREACHABLE_RETURN(1); } } @@ -4120,8 +4118,7 @@ bool operator==(const QRhiShaderResourceBinding &a, const QRhiShaderResourceBind } break; default: - Q_UNREACHABLE(); - return false; + Q_UNREACHABLE_RETURN(false); } return true; @@ -5100,8 +5097,7 @@ static const char *resourceTypeStr(QRhiResource *res) return "CommandBuffer"; } - Q_UNREACHABLE(); - return ""; + Q_UNREACHABLE_RETURN(""); } QRhiImplementation::~QRhiImplementation() @@ -5629,8 +5625,7 @@ const char *QRhi::backendName(Implementation impl) return "Metal"; } - Q_UNREACHABLE(); - return "Unknown"; + Q_UNREACHABLE_RETURN("Unknown"); } /*! @@ -5691,8 +5686,7 @@ static inline const char *deviceTypeStr(QRhiDriverInfo::DeviceType type) return "Cpu"; } - Q_UNREACHABLE(); - return nullptr; + Q_UNREACHABLE_RETURN(nullptr); } QDebug operator<<(QDebug dbg, const QRhiDriverInfo &info) { @@ -7768,8 +7762,7 @@ QRhiPassResourceTracker::BufferStage QRhiPassResourceTracker::toPassTrackerBuffe if (stages.testFlag(QRhiShaderResourceBinding::GeometryStage)) return QRhiPassResourceTracker::BufGeometryStage; - Q_UNREACHABLE(); - return QRhiPassResourceTracker::BufVertexStage; + Q_UNREACHABLE_RETURN(QRhiPassResourceTracker::BufVertexStage); } QRhiPassResourceTracker::TextureStage QRhiPassResourceTracker::toPassTrackerTextureStage(QRhiShaderResourceBinding::StageFlags stages) @@ -7788,8 +7781,7 @@ QRhiPassResourceTracker::TextureStage QRhiPassResourceTracker::toPassTrackerText if (stages.testFlag(QRhiShaderResourceBinding::GeometryStage)) return QRhiPassResourceTracker::TexGeometryStage; - Q_UNREACHABLE(); - return QRhiPassResourceTracker::TexVertexStage; + Q_UNREACHABLE_RETURN(QRhiPassResourceTracker::TexVertexStage); } QT_END_NAMESPACE diff --git a/src/gui/rhi/qrhigles2.cpp b/src/gui/rhi/qrhigles2.cpp index a45915495e..a8b7919647 100644 --- a/src/gui/rhi/qrhigles2.cpp +++ b/src/gui/rhi/qrhigles2.cpp @@ -1232,8 +1232,7 @@ bool QRhiGles2::isFeatureSupported(QRhi::Feature feature) const case QRhi::NonFillPolygonMode: return !caps.gles; default: - Q_UNREACHABLE(); - return false; + Q_UNREACHABLE_RETURN(false); } } @@ -1271,8 +1270,7 @@ int QRhiGles2::resourceLimit(QRhi::ResourceLimit limit) const case QRhi::MaxVertexOutputs: return caps.maxVertexOutputs; default: - Q_UNREACHABLE(); - return 0; + Q_UNREACHABLE_RETURN(0); } } @@ -2356,8 +2354,7 @@ static inline GLenum toGlTopology(QRhiGraphicsPipeline::Topology t) case QRhiGraphicsPipeline::Patches: return GL_PATCHES; default: - Q_UNREACHABLE(); - return GL_TRIANGLES; + Q_UNREACHABLE_RETURN(GL_TRIANGLES); } } @@ -2369,8 +2366,7 @@ static inline GLenum toGlCullMode(QRhiGraphicsPipeline::CullMode c) case QRhiGraphicsPipeline::Back: return GL_BACK; default: - Q_UNREACHABLE(); - return GL_BACK; + Q_UNREACHABLE_RETURN(GL_BACK); } } @@ -2382,8 +2378,7 @@ static inline GLenum toGlFrontFace(QRhiGraphicsPipeline::FrontFace f) case QRhiGraphicsPipeline::CW: return GL_CW; default: - Q_UNREACHABLE(); - return GL_CCW; + Q_UNREACHABLE_RETURN(GL_CCW); } } @@ -2427,8 +2422,7 @@ static inline GLenum toGlBlendFactor(QRhiGraphicsPipeline::BlendFactor f) qWarning("Unsupported blend factor %d", f); return GL_ZERO; default: - Q_UNREACHABLE(); - return GL_ZERO; + Q_UNREACHABLE_RETURN(GL_ZERO); } } @@ -2446,8 +2440,7 @@ static inline GLenum toGlBlendOp(QRhiGraphicsPipeline::BlendOp op) case QRhiGraphicsPipeline::Max: return GL_MAX; default: - Q_UNREACHABLE(); - return GL_FUNC_ADD; + Q_UNREACHABLE_RETURN(GL_FUNC_ADD); } } @@ -2471,8 +2464,7 @@ static inline GLenum toGlCompareOp(QRhiGraphicsPipeline::CompareOp op) case QRhiGraphicsPipeline::Always: return GL_ALWAYS; default: - Q_UNREACHABLE(); - return GL_ALWAYS; + Q_UNREACHABLE_RETURN(GL_ALWAYS); } } @@ -2496,8 +2488,7 @@ static inline GLenum toGlStencilOp(QRhiGraphicsPipeline::StencilOp op) case QRhiGraphicsPipeline::DecrementAndWrap: return GL_DECR_WRAP; default: - Q_UNREACHABLE(); - return GL_KEEP; + Q_UNREACHABLE_RETURN(GL_KEEP); } } @@ -2509,8 +2500,7 @@ static inline GLenum toGlPolygonMode(QRhiGraphicsPipeline::PolygonMode mode) case QRhiGraphicsPipeline::PolygonMode::Line: return GL_LINE; default: - Q_UNREACHABLE(); - return GL_FILL; + Q_UNREACHABLE_RETURN(GL_FILL); } } @@ -2528,8 +2518,7 @@ static inline GLenum toGlMinFilter(QRhiSampler::Filter f, QRhiSampler::Filter m) else return m == QRhiSampler::Nearest ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR_MIPMAP_LINEAR; default: - Q_UNREACHABLE(); - return GL_LINEAR; + Q_UNREACHABLE_RETURN(GL_LINEAR); } } @@ -2541,8 +2530,7 @@ static inline GLenum toGlMagFilter(QRhiSampler::Filter f) case QRhiSampler::Linear: return GL_LINEAR; default: - Q_UNREACHABLE(); - return GL_LINEAR; + Q_UNREACHABLE_RETURN(GL_LINEAR); } } @@ -2556,8 +2544,7 @@ static inline GLenum toGlWrapMode(QRhiSampler::AddressMode m) case QRhiSampler::Mirror: return GL_MIRRORED_REPEAT; default: - Q_UNREACHABLE(); - return GL_CLAMP_TO_EDGE; + Q_UNREACHABLE_RETURN(GL_CLAMP_TO_EDGE); } } @@ -2581,8 +2568,7 @@ static inline GLenum toGlTextureCompareFunc(QRhiSampler::CompareOp op) case QRhiSampler::Always: return GL_ALWAYS; default: - Q_UNREACHABLE(); - return GL_NEVER; + Q_UNREACHABLE_RETURN(GL_NEVER); } } @@ -4275,8 +4261,7 @@ static inline GLenum toGlShaderType(QRhiShaderStage::Type type) case QRhiShaderStage::Compute: return GL_COMPUTE_SHADER; default: - Q_UNREACHABLE(); - return GL_VERTEX_SHADER; + Q_UNREACHABLE_RETURN(GL_VERTEX_SHADER); } } @@ -4558,8 +4543,7 @@ static inline QShader::Stage toShaderStage(QRhiShaderStage::Type type) case QRhiShaderStage::Compute: return QShader::ComputeStage; default: - Q_UNREACHABLE(); - return QShader::VertexStage; + Q_UNREACHABLE_RETURN(QShader::VertexStage); } } @@ -5526,8 +5510,7 @@ bool QGles2GraphicsPipeline::create() default: break; } - Q_UNREACHABLE(); - return VtxIdx; + Q_UNREACHABLE_RETURN(VtxIdx); }; QShaderDescription desc[LastIdx]; QShader::SeparateToCombinedImageSamplerMappingList samplerMappingList[LastIdx]; diff --git a/src/gui/rhi/qrhinull.cpp b/src/gui/rhi/qrhinull.cpp index b0c3c559c4..88afb004f7 100644 --- a/src/gui/rhi/qrhinull.cpp +++ b/src/gui/rhi/qrhinull.cpp @@ -135,8 +135,7 @@ int QRhiNull::resourceLimit(QRhi::ResourceLimit limit) const return 32; } - Q_UNREACHABLE(); - return 0; + Q_UNREACHABLE_RETURN(0); } const QRhiNativeHandles *QRhiNull::nativeHandles() diff --git a/src/gui/rhi/qrhivulkan.cpp b/src/gui/rhi/qrhivulkan.cpp index daf934f1d3..a04b9e8e0a 100644 --- a/src/gui/rhi/qrhivulkan.cpp +++ b/src/gui/rhi/qrhivulkan.cpp @@ -1016,8 +1016,7 @@ static inline VkFormat toVkTextureFormat(QRhiTexture::Format format, QRhiTexture return srgb ? VK_FORMAT_ASTC_12x12_SRGB_BLOCK : VK_FORMAT_ASTC_12x12_UNORM_BLOCK; default: - Q_UNREACHABLE(); - return VK_FORMAT_R8G8B8A8_UNORM; + Q_UNREACHABLE_RETURN(VK_FORMAT_R8G8B8A8_UNORM); } } @@ -3813,8 +3812,7 @@ VkSampleCountFlagBits QRhiVulkan::effectiveSampleCount(int sampleCount) return qvk_sampleCount.mask; } - Q_UNREACHABLE(); - return VK_SAMPLE_COUNT_1_BIT; + Q_UNREACHABLE_RETURN(VK_SAMPLE_COUNT_1_BIT); } void QRhiVulkan::enqueueTransitionPassResources(QVkCommandBuffer *cbD) @@ -4336,8 +4334,7 @@ bool QRhiVulkan::isFeatureSupported(QRhi::Feature feature) const case QRhi::NonFillPolygonMode: return caps.nonFillPolygonMode; default: - Q_UNREACHABLE(); - return false; + Q_UNREACHABLE_RETURN(false); } } @@ -4375,8 +4372,7 @@ int QRhiVulkan::resourceLimit(QRhi::ResourceLimit limit) const case QRhi::MaxVertexOutputs: return physDevProperties.limits.maxVertexOutputComponents / 4; default: - Q_UNREACHABLE(); - return 0; + Q_UNREACHABLE_RETURN(0); } } @@ -5258,8 +5254,7 @@ static inline VkFilter toVkFilter(QRhiSampler::Filter f) case QRhiSampler::Linear: return VK_FILTER_LINEAR; default: - Q_UNREACHABLE(); - return VK_FILTER_NEAREST; + Q_UNREACHABLE_RETURN(VK_FILTER_NEAREST); } } @@ -5273,8 +5268,7 @@ static inline VkSamplerMipmapMode toVkMipmapMode(QRhiSampler::Filter f) case QRhiSampler::Linear: return VK_SAMPLER_MIPMAP_MODE_LINEAR; default: - Q_UNREACHABLE(); - return VK_SAMPLER_MIPMAP_MODE_NEAREST; + Q_UNREACHABLE_RETURN(VK_SAMPLER_MIPMAP_MODE_NEAREST); } } @@ -5288,8 +5282,7 @@ static inline VkSamplerAddressMode toVkAddressMode(QRhiSampler::AddressMode m) case QRhiSampler::Mirror: return VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT; default: - Q_UNREACHABLE(); - return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; + Q_UNREACHABLE_RETURN(VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE); } } @@ -5309,8 +5302,7 @@ static inline VkShaderStageFlagBits toVkShaderStage(QRhiShaderStage::Type type) case QRhiShaderStage::Geometry: return VK_SHADER_STAGE_GEOMETRY_BIT; default: - Q_UNREACHABLE(); - return VK_SHADER_STAGE_VERTEX_BIT; + Q_UNREACHABLE_RETURN(VK_SHADER_STAGE_VERTEX_BIT); } } @@ -5348,8 +5340,7 @@ static inline VkFormat toVkAttributeFormat(QRhiVertexInputAttribute::Format form case QRhiVertexInputAttribute::SInt: return VK_FORMAT_R32_SINT; default: - Q_UNREACHABLE(); - return VK_FORMAT_R32G32B32A32_SFLOAT; + Q_UNREACHABLE_RETURN(VK_FORMAT_R32G32B32A32_SFLOAT); } } @@ -5371,8 +5362,7 @@ static inline VkPrimitiveTopology toVkTopology(QRhiGraphicsPipeline::Topology t) case QRhiGraphicsPipeline::Patches: return VK_PRIMITIVE_TOPOLOGY_PATCH_LIST; default: - Q_UNREACHABLE(); - return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; + Q_UNREACHABLE_RETURN(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST); } } @@ -5386,8 +5376,7 @@ static inline VkCullModeFlags toVkCullMode(QRhiGraphicsPipeline::CullMode c) case QRhiGraphicsPipeline::Back: return VK_CULL_MODE_BACK_BIT; default: - Q_UNREACHABLE(); - return VK_CULL_MODE_NONE; + Q_UNREACHABLE_RETURN(VK_CULL_MODE_NONE); } } @@ -5399,8 +5388,7 @@ static inline VkFrontFace toVkFrontFace(QRhiGraphicsPipeline::FrontFace f) case QRhiGraphicsPipeline::CW: return VK_FRONT_FACE_CLOCKWISE; default: - Q_UNREACHABLE(); - return VK_FRONT_FACE_COUNTER_CLOCKWISE; + Q_UNREACHABLE_RETURN(VK_FRONT_FACE_COUNTER_CLOCKWISE); } } @@ -5460,8 +5448,7 @@ static inline VkBlendFactor toVkBlendFactor(QRhiGraphicsPipeline::BlendFactor f) case QRhiGraphicsPipeline::OneMinusSrc1Alpha: return VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA; default: - Q_UNREACHABLE(); - return VK_BLEND_FACTOR_ZERO; + Q_UNREACHABLE_RETURN(VK_BLEND_FACTOR_ZERO); } } @@ -5479,8 +5466,7 @@ static inline VkBlendOp toVkBlendOp(QRhiGraphicsPipeline::BlendOp op) case QRhiGraphicsPipeline::Max: return VK_BLEND_OP_MAX; default: - Q_UNREACHABLE(); - return VK_BLEND_OP_ADD; + Q_UNREACHABLE_RETURN(VK_BLEND_OP_ADD); } } @@ -5504,8 +5490,7 @@ static inline VkCompareOp toVkCompareOp(QRhiGraphicsPipeline::CompareOp op) case QRhiGraphicsPipeline::Always: return VK_COMPARE_OP_ALWAYS; default: - Q_UNREACHABLE(); - return VK_COMPARE_OP_ALWAYS; + Q_UNREACHABLE_RETURN(VK_COMPARE_OP_ALWAYS); } } @@ -5529,8 +5514,7 @@ static inline VkStencilOp toVkStencilOp(QRhiGraphicsPipeline::StencilOp op) case QRhiGraphicsPipeline::DecrementAndWrap: return VK_STENCIL_OP_DECREMENT_AND_WRAP; default: - Q_UNREACHABLE(); - return VK_STENCIL_OP_KEEP; + Q_UNREACHABLE_RETURN(VK_STENCIL_OP_KEEP); } } @@ -5542,8 +5526,7 @@ static inline VkPolygonMode toVkPolygonMode(QRhiGraphicsPipeline::PolygonMode mo case QRhiGraphicsPipeline::Line: return VK_POLYGON_MODE_LINE; default: - Q_UNREACHABLE(); - return VK_POLYGON_MODE_FILL; + Q_UNREACHABLE_RETURN(VK_POLYGON_MODE_FILL); } } @@ -5582,8 +5565,7 @@ static inline VkDescriptorType toVkDescriptorType(const QRhiShaderResourceBindin return VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; default: - Q_UNREACHABLE(); - return VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + Q_UNREACHABLE_RETURN(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER); } } @@ -5625,8 +5607,7 @@ static inline VkCompareOp toVkTextureCompareOp(QRhiSampler::CompareOp op) case QRhiSampler::Always: return VK_COMPARE_OP_ALWAYS; default: - Q_UNREACHABLE(); - return VK_COMPARE_OP_NEVER; + Q_UNREACHABLE_RETURN(VK_COMPARE_OP_NEVER); } } |