================
@@ -111,7 +114,69 @@ unsigned invalid_bithacks(integer_like w, unsigned x,
signed y, unsigned z) {
}
template <class T>
-T bithacks_generic(T x) {
- // substitution only valid for some instantiation of bithacks_generic
+T has_one_bit_bithack_generic(T x) {
+ // substitution only valid for some instantiation of
has_one_bit_bithack_generic
return x && !(x & (x - 1));
}
+
+/*
+ * popcount pattern
+ */
+namespace std {
+using size_t = decltype(sizeof(0));
+template<size_t N> class bitset {
+ public:
+ bitset(unsigned long);
+ size_t count() const;
+};
+}
+
+unsigned popcount_bitset(unsigned x) {
+ // CHECK-MESSAGES: :[[@LINE+2]]:10: warning: use 'std::popcount' instead
[modernize-use-std-bit]
+ // CHECK-FIXES: return std::popcount(x);
+ return std::bitset<sizeof(x) * 8>(x).count();
+}
+
+unsigned popcount_bitset_short(unsigned short x) {
+ // CHECK-MESSAGES: :[[@LINE+2]]:10: warning: use 'std::popcount' instead
[modernize-use-std-bit]
+ // CHECK-FIXES: return std::popcount(x);
+ return std::bitset<sizeof(x) * 8>(x).count();
+}
+
+unsigned popcount_bitset_larger(unsigned x) {
+ // CHECK-MESSAGES: :[[@LINE+2]]:10: warning: use 'std::popcount' instead
[modernize-use-std-bit]
+ // CHECK-FIXES: return std::popcount(x);
+ return std::bitset<sizeof(x) * 16>(x).count();
+}
+
+unsigned popcount_bitset_uniform_init(unsigned x) {
+ // CHECK-MESSAGES: :[[@LINE+2]]:10: warning: use 'std::popcount' instead
[modernize-use-std-bit]
+ // CHECK-FIXES: return std::popcount(x);
+ return std::bitset<sizeof(x) * 16>{x}.count();
+}
+
+unsigned popcount_bitset_expr(unsigned x) {
+ // CHECK-MESSAGES: :[[@LINE+2]]:10: warning: use 'std::popcount' instead
[modernize-use-std-bit]
+ // CHECK-FIXES: return std::popcount(x + 1);
+ return std::bitset<sizeof(x) * 8>{x + 1}.count();
+}
+
+/*
+ * Invalid has_one_bit patterns
+ */
+template<std::size_t N> class bitset {
+ public:
+ bitset(unsigned long);
+ std::size_t count() const;
+};
+
+unsigned invalid_popcount_bitset(unsigned x, signed y) {
+ std::size_t patterns[] = {
+ // truncating bitset
+ std::bitset<1>{x}.count(),
----------------
localspook wrote:
Can we do something like:
```cpp
std::bitset<7>{static_cast<unsigned char>(x)}.count() // shouldn't warn
std::bitset<8>{static_cast<unsigned char>(x)}.count() // should warn
```
So that we test the exact boundary between truncating and not-truncating.
https://github.com/llvm/llvm-project/pull/185740
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits