LEFT | RIGHT |
1 // RUN: %clang_cc1 -fsyntax-only -verify -Wthread-safety %s | 1 // RUN: %clang_cc1 -fsyntax-only -verify -Wthread-safety %s |
2 | 2 |
3 | 3 |
4 /************************************** | 4 //-----------------------------------------// |
5 * | 5 // Helper fields |
6 * Helper fields | 6 //-----------------------------------------// |
7 * | 7 |
8 *************************************/ | |
9 | |
10 // more complicated classes, taken from gcc tests | |
11 | 8 |
12 class __attribute__((lockable)) Mutex { | 9 class __attribute__((lockable)) Mutex { |
13 public: | 10 public: |
14 void Lock() __attribute__((exclusive_lock_function)); | 11 void Lock() __attribute__((exclusive_lock_function)); |
15 void ReaderLock() __attribute__((shared_lock_function)); | 12 void ReaderLock() __attribute__((shared_lock_function)); |
16 void Unlock() __attribute__((unlock_function)); | 13 void Unlock() __attribute__((unlock_function)); |
17 bool TryLock() __attribute__((exclusive_trylock_function(true))); | 14 bool TryLock() __attribute__((exclusive_trylock_function(true))); |
18 bool ReaderTryLock() __attribute__((shared_trylock_function(true))); | 15 bool ReaderTryLock() __attribute__((shared_trylock_function(true))); |
19 void LockWhen(const int &cond) __attribute__((exclusive_lock_function)); | 16 void LockWhen(const int &cond) __attribute__((exclusive_lock_function)); |
20 }; | 17 }; |
21 | 18 |
22 | 19 |
23 /*********************************** | |
24 * Simple Lock Set (sls) | |
25 ***********************************/ | |
26 | |
27 Mutex sls_mu; | 20 Mutex sls_mu; |
28 | 21 |
29 Mutex sls_mu2 __attribute__((acquired_after(sls_mu))); | 22 Mutex sls_mu2 __attribute__((acquired_after(sls_mu))); |
30 int sls_guard_var __attribute__((guarded_var)) = 0; | 23 int sls_guard_var __attribute__((guarded_var)) = 0; |
31 int sls_guardby_var __attribute__((guarded_by(sls_mu))) = 0; | 24 int sls_guardby_var __attribute__((guarded_by(sls_mu))) = 0; |
32 | 25 |
33 bool getBool(); | 26 bool getBool(); |
34 | 27 |
35 class MutexWrapper { | 28 class MutexWrapper { |
36 public: | 29 public: |
37 Mutex mu; | 30 Mutex mu; |
38 // int x __attribute__((guarded_by(mu))); // FIXME: scoping error | 31 // int x __attribute__((guarded_by(mu))); // FIXME: scoping error |
39 }; | 32 }; |
40 | 33 |
41 class Destructable { | |
42 public: | |
43 Destructable(); | |
44 ~Destructable(); | |
45 }; | |
46 | |
47 | |
48 MutexWrapper sls_mw; | 34 MutexWrapper sls_mw; |
49 | 35 |
50 void sls_fun_0() { | 36 void sls_fun_0() { |
51 sls_mw.mu.Lock(); | 37 sls_mw.mu.Lock(); |
52 // sls_mw.x = 5; // turn mu into sls_mw.mu | 38 // sls_mw.x = 5; // FIXME: turn mu into sls_mw.mu |
53 sls_mw.mu.Unlock(); | 39 sls_mw.mu.Unlock(); |
54 } | 40 } |
55 | 41 |
56 | 42 |
57 void sls_fun_2() { | 43 void sls_fun_2() { |
58 sls_mu.Lock(); | 44 sls_mu.Lock(); |
59 int x = sls_guard_var; | 45 int x = sls_guard_var; |
60 sls_mu.Unlock(); | 46 sls_mu.Unlock(); |
61 } | 47 } |
62 | 48 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 if (getBool()) | 89 if (getBool()) |
104 sls_mu.Lock(); | 90 sls_mu.Lock(); |
105 else | 91 else |
106 sls_mu.Lock(); | 92 sls_mu.Lock(); |
107 sls_mu.Unlock(); | 93 sls_mu.Unlock(); |
108 } | 94 } |
109 | 95 |
110 void sls_fun_good_6() { | 96 void sls_fun_good_6() { |
111 if (getBool()) { | 97 if (getBool()) { |
112 sls_mu.Lock(); | 98 sls_mu.Lock(); |
113 } | 99 } else { |
114 else { | 100 if (getBool()) { |
115 if (getBool()) { | 101 getBool(); // EMPTY |
116 getBool(); // EMPTY | 102 } else { |
117 } | |
118 else { | |
119 getBool(); // EMPTY | 103 getBool(); // EMPTY |
120 } | 104 } |
121 sls_mu.Lock(); | 105 sls_mu.Lock(); |
122 } | 106 } |
123 sls_mu.Unlock(); | 107 sls_mu.Unlock(); |
124 } | 108 } |
125 | 109 |
126 void sls_fun_good_7() { | 110 void sls_fun_good_7() { |
127 sls_mu.Lock(); | 111 sls_mu.Lock(); |
128 while (getBool()) { | 112 while (getBool()) { |
129 sls_mu.Unlock(); | 113 sls_mu.Unlock(); |
130 if (getBool()) { | 114 if (getBool()) { |
131 Destructable d; // same behaviour without this line | |
132 if (getBool()) { | 115 if (getBool()) { |
133 sls_mu.Lock(); | 116 sls_mu.Lock(); |
134 continue; | 117 continue; |
135 } | 118 } |
136 } | 119 } |
137 sls_mu.Lock(); | 120 sls_mu.Lock(); |
138 } | 121 } |
139 sls_mu.Unlock(); | 122 sls_mu.Unlock(); |
140 } | 123 } |
141 | 124 |
(...skipping 26 matching lines...) Expand all Loading... |
168 void sls_fun_bad_5() { | 151 void sls_fun_bad_5() { |
169 sls_mu.Lock(); // \ | 152 sls_mu.Lock(); // \ |
170 expected-warning {{lock 'sls_mu' is not released at the end of its scope}} | 153 expected-warning {{lock 'sls_mu' is not released at the end of its scope}} |
171 if (getBool()) | 154 if (getBool()) |
172 sls_mu.Unlock(); | 155 sls_mu.Unlock(); |
173 } | 156 } |
174 | 157 |
175 void sls_fun_bad_6() { | 158 void sls_fun_bad_6() { |
176 if (getBool()) { | 159 if (getBool()) { |
177 sls_mu.Lock(); // \ | 160 sls_mu.Lock(); // \ |
178 expected-warning {{lock 'sls_mu' is not released at the end of its}} | 161 expected-warning {{lock 'sls_mu' is not released at the end of its scope}} |
179 } | 162 } else { |
180 else { | 163 if (getBool()) { |
181 if (getBool()) { | 164 getBool(); // EMPTY |
182 getBool(); // EMPTY | 165 } else { |
183 } | |
184 else { | |
185 getBool(); // EMPTY | 166 getBool(); // EMPTY |
186 } | 167 } |
187 } | 168 } |
188 sls_mu.Unlock(); // \ | 169 sls_mu.Unlock(); // \ |
189 expected-warning {{unlocking 'sls_mu' that was not acquired}} | 170 expected-warning {{unlocking 'sls_mu' that was not acquired}} |
190 } | 171 } |
191 | 172 |
192 void sls_fun_bad_7() { | 173 void sls_fun_bad_7() { |
193 sls_mu.Lock(); | 174 sls_mu.Lock(); |
194 while (getBool()) { // \ | 175 while (getBool()) { // \ |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 glock.globalLock(); | 258 glock.globalLock(); |
278 glock.globalLock(); // \ | 259 glock.globalLock(); // \ |
279 expected-warning {{locking 'aa_mu' that is already acquired}} | 260 expected-warning {{locking 'aa_mu' that is already acquired}} |
280 glock.globalUnlock(); | 261 glock.globalUnlock(); |
281 } | 262 } |
282 | 263 |
283 void aa_fun_bad_3() { | 264 void aa_fun_bad_3() { |
284 glock.globalLock(); // \ | 265 glock.globalLock(); // \ |
285 expected-warning {{lock 'aa_mu' is not released at the end of function 'aa_f
un_bad_3'}} | 266 expected-warning {{lock 'aa_mu' is not released at the end of function 'aa_f
un_bad_3'}} |
286 } | 267 } |
LEFT | RIGHT |