[clang] [analyzer] Fix false positive for mutexes inheriting mutex_base (PR #106240)

Balazs Benics via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 28 00:01:11 PDT 2024


steakhal wrote:

Could you also add this test case?
```c++
namespace std {
struct __mutex_base {
  void lock();
};
struct mutex : __mutex_base {
  void unlock();
  bool try_lock();
};
template <class... MutexTypes> struct scoped_lock {
  explicit scoped_lock(MutexTypes&... m);
  ~scoped_lock();
};
template <class MutexType> class scoped_lock<MutexType> {
public:
  explicit scoped_lock(MutexType& m) : m(m) { m.lock(); }
  ~scoped_lock() { m.unlock(); }
private:
  MutexType& m;
};
} // namespace std

extern "C" unsigned int sleep(unsigned int seconds);

int magic_number;
std::mutex m;

void fixed() {
  int current;
  for (int items_processed = 0; items_processed < 100; ++items_processed) {
    {
      std::scoped_lock guard(m);
      current = magic_number;
    }
    sleep(current); // expected no warning
  }
}
```
Or is it already implied by other tests?

https://github.com/llvm/llvm-project/pull/106240


More information about the cfe-commits mailing list