<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/62022>62022</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[libc++] ordering comparisons should be marked __attribute__((pure))
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
danakj
</td>
</tr>
</table>
<pre>
https://godbolt.org/z/e4rTvMeoW
The following code generates Wassume warnings suggesting there are side effects when converting from a strong_ordering to a bool. The same is true of partial_ordering and weak_ordering.
```cpp
#include <compare>
struct S {
__attribute__((pure)) bool g() { return true; }
__attribute__((pure)) auto operator<=>(const S& rhs) const {
return std::strong_ordering::equal;
}
};
constexpr __attribute__((pure)) __attribute__((noinline)) auto f(
const S&, const S&) noexcept {
return std::strong_ordering::equal;
}
int main() {
S s1, s2;
__builtin_assume(f(s1, s2) == 0);
__builtin_assume(f(s1, s2) == std::strong_ordering::equal);
__builtin_assume(s1 <= s2);
int a, b;
__builtin_assume(a <= b); // Only this one is accepted without warning.
__builtin_assume(std::strong_ordering::equivalent == 0);
__builtin_assume(std::weak_ordering::equivalent == 0);
__builtin_assume(std::partial_ordering::equivalent == 0);
}
```
I tried to find the right place to insert `__attribute__((pure))`. I think all of these:
- https://github.com/llvm/llvm-project/blob/e3d2b584aae36a3d7d2a28b23ff1c5ec4f70974a/libcxx/include/__compare/ordering.h#L157-L213
- https://github.com/llvm/llvm-project/blob/e3d2b584aae36a3d7d2a28b23ff1c5ec4f70974a/libcxx/include/__compare/ordering.h#L72-L129
- https://github.com/llvm/llvm-project/blob/e3d2b584aae36a3d7d2a28b23ff1c5ec4f70974a/libcxx/include/__compare/ordering.h#L249-L305
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMVk1v4zYQ_TX0ZRBDHtqSdfAhiWtggRQ9ZIE9GqQ4krihSZWk4mx_fUEp_twiTve0gaDAo-H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