We provide several diagnostics for various undefined behaviors due to signed integer overflow outside of a constant expression context. We were missing the machinery to catch overflows due to unary minus.
Details
Details
Diff Detail
Diff Detail
Unit Tests
Unit Tests
Event Timeline
Comment Actions
Amusingly this fired on some code that used negation with the intention of getting fewer warnings: https://github.com/KhronosGroup/glslang/commit/866f67140ed98865350b53312bba1b654b59f937 :-)
Fixed in https://github.com/KhronosGroup/glslang/commit/7341a21b345e7aea1d2791db0f2d36866c434c03
Comment Actions
The warning now fires even if overflow is prevented with if constexpr:
if constexpr (width <= 64) { if constexpr (width == 64) { return 1; } return -static_cast<int64_t>(uint64_t{1} << (width - 1)); }
Comment Actions
For reference, actually placing the second return into an else block (thus making it a discarded statement) does suppress the diagnostic:
https://godbolt.org/z/Kb6Md5PrK
I also question the focus on if constexpr. Replacing with if (true) { return 1; } should be as effective in the non-else-block case in suppressing the warning in terms of QoI.