Clang Project

clang_source_code/test/SemaTemplate/warn-thread-safety-analysis.cpp
1// RUN: %clang_cc1 -std=c++11 %s -verify -Wthread-safety-analysis
2
3class Mutex {
4public:
5  void Lock() __attribute__((exclusive_lock_function()));
6  void Unlock() __attribute__((unlock_function()));
7};
8
9class A {
10public:
11  Mutex mu1, mu2;
12
13  void foo() __attribute__((exclusive_locks_required(mu1))) __attribute__((exclusive_locks_required(mu2))) {}
14
15  template <class T> void bar() __attribute__((exclusive_locks_required(mu1))) __attribute__((exclusive_locks_required(mu2))) {
16    foo();
17  }
18};
19
20void f() {
21  A a;
22  a.mu1.Lock();
23  a.mu2.Lock();
24  a.bar<int>();
25  a.mu2.Unlock();
26  a.bar<int>(); // expected-warning {{calling function 'bar<int>' requires holding mutex 'a.mu2' exclusively}}
27  a.mu1.Unlock();
28  a.bar<int>(); // expected-warning {{calling function 'bar<int>' requires holding mutex 'a.mu1' exclusively}} \
29                   expected-warning {{calling function 'bar<int>' requires holding mutex 'a.mu2' exclusively}}
30}
31