1 // Test this without pch.
2 // RUN: %clang_cc1 -include %s -fsyntax-only -verify -Wthread-safety -std=c++11 %s
5 // RUN: %clang_cc1 -emit-pch -o %t %s -std=c++11
6 // RUN: %clang_cc1 -include-pch %t -fsyntax-only -verify -Wthread-safety -std=c++11 %s
11 #define LOCKABLE __attribute__ ((lockable))
12 #define SCOPED_LOCKABLE __attribute__ ((scoped_lockable))
13 #define GUARDED_BY(x) __attribute__ ((guarded_by(x)))
14 #define GUARDED_VAR __attribute__ ((guarded_var))
15 #define PT_GUARDED_BY(x) __attribute__ ((pt_guarded_by(x)))
16 #define PT_GUARDED_VAR __attribute__ ((pt_guarded_var))
17 #define ACQUIRED_AFTER(...) __attribute__ ((acquired_after(__VA_ARGS__)))
18 #define ACQUIRED_BEFORE(...) __attribute__ ((acquired_before(__VA_ARGS__)))
19 #define EXCLUSIVE_LOCK_FUNCTION(...) __attribute__ ((exclusive_lock_function(__VA_ARGS__)))
20 #define SHARED_LOCK_FUNCTION(...) __attribute__ ((shared_lock_function(__VA_ARGS__)))
21 #define EXCLUSIVE_TRYLOCK_FUNCTION(...) __attribute__ ((exclusive_trylock_function(__VA_ARGS__)))
22 #define SHARED_TRYLOCK_FUNCTION(...) __attribute__ ((shared_trylock_function(__VA_ARGS__)))
23 #define UNLOCK_FUNCTION(...) __attribute__ ((unlock_function(__VA_ARGS__)))
24 #define LOCK_RETURNED(x) __attribute__ ((lock_returned(x)))
25 #define LOCKS_EXCLUDED(...) __attribute__ ((locks_excluded(__VA_ARGS__)))
26 #define EXCLUSIVE_LOCKS_REQUIRED(...) \
27 __attribute__ ((exclusive_locks_required(__VA_ARGS__)))
28 #define SHARED_LOCKS_REQUIRED(...) \
29 __attribute__ ((shared_locks_required(__VA_ARGS__)))
30 #define NO_THREAD_SAFETY_ANALYSIS __attribute__ ((no_thread_safety_analysis))
33 class __attribute__((lockable)) Mutex {
35 void Lock() __attribute__((exclusive_lock_function));
36 void ReaderLock() __attribute__((shared_lock_function));
37 void Unlock() __attribute__((unlock_function));
38 bool TryLock() __attribute__((exclusive_trylock_function(true)));
39 bool ReaderTryLock() __attribute__((shared_trylock_function(true)));
40 void LockWhen(const int &cond) __attribute__((exclusive_lock_function));
43 class __attribute__((scoped_lockable)) MutexLock {
45 MutexLock(Mutex *mu) __attribute__((exclusive_lock_function(mu)));
46 ~MutexLock() __attribute__((unlock_function));
49 class __attribute__((scoped_lockable)) ReaderMutexLock {
51 ReaderMutexLock(Mutex *mu) __attribute__((exclusive_lock_function(mu)));
52 ~ReaderMutexLock() __attribute__((unlock_function));
55 class SCOPED_LOCKABLE ReleasableMutexLock {
57 ReleasableMutexLock(Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu);
58 ~ReleasableMutexLock() UNLOCK_FUNCTION();
60 void Release() UNLOCK_FUNCTION();
64 // The universal lock, written "*", allows checking to be selectively turned
65 // off for a particular piece of code.
66 void beginNoWarnOnReads() SHARED_LOCK_FUNCTION("*");
67 void endNoWarnOnReads() UNLOCK_FUNCTION("*");
68 void beginNoWarnOnWrites() EXCLUSIVE_LOCK_FUNCTION("*");
69 void endNoWarnOnWrites() UNLOCK_FUNCTION("*");
72 // For testing handling of smart pointers.
76 SmartPtr(T* p) : ptr_(p) { }
77 SmartPtr(const SmartPtr<T>& p) : ptr_(p.ptr_) { }
80 T* get() const { return ptr_; }
81 T* operator->() const { return ptr_; }
82 T& operator*() const { return *ptr_; }
89 // For testing destructor calls and cleanup.
92 MyString(const char* s);
100 Mutex sls_mu2 __attribute__((acquired_after(sls_mu)));
101 int sls_guard_var __attribute__((guarded_var)) = 0;
102 int sls_guardby_var __attribute__((guarded_by(sls_mu))) = 0;
109 int x __attribute__((guarded_by(mu)));
110 void MyLock() __attribute__((exclusive_lock_function(mu)));
125 int x = sls_guard_var;
143 int x = sls_guardby_var;
176 void sls_fun_good_6() {
190 void sls_fun_good_7() {
205 void sls_fun_good_8() {
210 void sls_fun_bad_1() {
211 sls_mu.Unlock(); // \
212 // expected-warning{{releasing mutex 'sls_mu' that was not held}}
215 void sls_fun_bad_2() {
218 // expected-warning{{acquiring mutex 'sls_mu' that is already held}}
222 void sls_fun_bad_3() {
223 sls_mu.Lock(); // expected-note {{mutex acquired here}}
224 } // expected-warning{{mutex 'sls_mu' is still held at the end of function}}
226 void sls_fun_bad_4() {
228 sls_mu.Lock(); // expected-note{{mutex acquired here}}
230 sls_mu2.Lock(); // expected-note{{mutex acquired here}}
231 } // expected-warning{{mutex 'sls_mu' is not held on every path through here}} \
232 // expected-warning{{mutex 'sls_mu2' is not held on every path through here}}
234 void sls_fun_bad_5() {
235 sls_mu.Lock(); // expected-note {{mutex acquired here}}
238 } // expected-warning{{mutex 'sls_mu' is not held on every path through here}}
240 void sls_fun_bad_6() {
242 sls_mu.Lock(); // expected-note {{mutex acquired here}}
250 sls_mu.Unlock(); // \
251 expected-warning{{mutex 'sls_mu' is not held on every path through here}}\
252 expected-warning{{releasing mutex 'sls_mu' that was not held}}
255 void sls_fun_bad_7() {
262 expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}}
265 sls_mu.Lock(); // expected-note {{mutex acquired here}}
270 void sls_fun_bad_8() {
271 sls_mu.Lock(); // expected-note{{mutex acquired here}}
274 sls_mu.Unlock(); // expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}}
278 void sls_fun_bad_9() {
281 // expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}} \
282 // expected-note{{mutex acquired here}}
287 void sls_fun_bad_10() {
288 sls_mu.Lock(); // expected-note 2{{mutex acquired here}}
289 while(getBool()) { // expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}}
292 } // expected-warning{{mutex 'sls_mu' is still held at the end of function}}
294 void sls_fun_bad_11() {
295 while (getBool()) { // \
296 expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}}
297 sls_mu.Lock(); // expected-note {{mutex acquired here}}
299 sls_mu.Unlock(); // \
300 // expected-warning{{releasing mutex 'sls_mu' that was not held}}
303 void sls_fun_bad_12() {
304 sls_mu.Lock(); // expected-note {{mutex acquired here}}
309 break; // expected-warning{{mutex 'sls_mu' is not held on every path through here}}