DeLesley Hutchins | 1310611 | 2012-07-10 21:47:55 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -Wthread-safety -std=c++11 %s |
| 2 | |
| 3 | // FIXME: should also run %clang_cc1 -fsyntax-only -verify -Wthread-safety -std=c++11 -Wc++98-compat %s |
| 4 | // FIXME: should also run %clang_cc1 -fsyntax-only -verify -Wthread-safety %s |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 5 | |
Caitlin Sadowski | 988b5ae | 2011-09-15 18:07:32 +0000 | [diff] [blame] | 6 | #define LOCKABLE __attribute__ ((lockable)) |
| 7 | #define SCOPED_LOCKABLE __attribute__ ((scoped_lockable)) |
| 8 | #define GUARDED_BY(x) __attribute__ ((guarded_by(x))) |
| 9 | #define GUARDED_VAR __attribute__ ((guarded_var)) |
| 10 | #define PT_GUARDED_BY(x) __attribute__ ((pt_guarded_by(x))) |
| 11 | #define PT_GUARDED_VAR __attribute__ ((pt_guarded_var)) |
| 12 | #define ACQUIRED_AFTER(...) __attribute__ ((acquired_after(__VA_ARGS__))) |
| 13 | #define ACQUIRED_BEFORE(...) __attribute__ ((acquired_before(__VA_ARGS__))) |
| 14 | #define EXCLUSIVE_LOCK_FUNCTION(...) __attribute__ ((exclusive_lock_function(__VA_ARGS__))) |
| 15 | #define SHARED_LOCK_FUNCTION(...) __attribute__ ((shared_lock_function(__VA_ARGS__))) |
| 16 | #define EXCLUSIVE_TRYLOCK_FUNCTION(...) __attribute__ ((exclusive_trylock_function(__VA_ARGS__))) |
| 17 | #define SHARED_TRYLOCK_FUNCTION(...) __attribute__ ((shared_trylock_function(__VA_ARGS__))) |
| 18 | #define UNLOCK_FUNCTION(...) __attribute__ ((unlock_function(__VA_ARGS__))) |
| 19 | #define LOCK_RETURNED(x) __attribute__ ((lock_returned(x))) |
| 20 | #define LOCKS_EXCLUDED(...) __attribute__ ((locks_excluded(__VA_ARGS__))) |
| 21 | #define EXCLUSIVE_LOCKS_REQUIRED(...) \ |
| 22 | __attribute__ ((exclusive_locks_required(__VA_ARGS__))) |
| 23 | #define SHARED_LOCKS_REQUIRED(...) \ |
| 24 | __attribute__ ((shared_locks_required(__VA_ARGS__))) |
| 25 | #define NO_THREAD_SAFETY_ANALYSIS __attribute__ ((no_thread_safety_analysis)) |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 26 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 27 | |
| 28 | class __attribute__((lockable)) Mutex { |
| 29 | public: |
| 30 | void Lock() __attribute__((exclusive_lock_function)); |
| 31 | void ReaderLock() __attribute__((shared_lock_function)); |
| 32 | void Unlock() __attribute__((unlock_function)); |
| 33 | bool TryLock() __attribute__((exclusive_trylock_function(true))); |
| 34 | bool ReaderTryLock() __attribute__((shared_trylock_function(true))); |
| 35 | void LockWhen(const int &cond) __attribute__((exclusive_lock_function)); |
| 36 | }; |
| 37 | |
DeLesley Hutchins | 1fa3c06 | 2011-12-08 20:23:06 +0000 | [diff] [blame] | 38 | class __attribute__((scoped_lockable)) MutexLock { |
| 39 | public: |
| 40 | MutexLock(Mutex *mu) __attribute__((exclusive_lock_function(mu))); |
| 41 | ~MutexLock() __attribute__((unlock_function)); |
| 42 | }; |
| 43 | |
| 44 | class __attribute__((scoped_lockable)) ReaderMutexLock { |
| 45 | public: |
| 46 | ReaderMutexLock(Mutex *mu) __attribute__((exclusive_lock_function(mu))); |
| 47 | ~ReaderMutexLock() __attribute__((unlock_function)); |
| 48 | }; |
| 49 | |
DeLesley Hutchins | c99a5d8 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 50 | class SCOPED_LOCKABLE ReleasableMutexLock { |
| 51 | public: |
| 52 | ReleasableMutexLock(Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu); |
| 53 | ~ReleasableMutexLock() UNLOCK_FUNCTION(); |
| 54 | |
| 55 | void Release() UNLOCK_FUNCTION(); |
| 56 | }; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 57 | |
DeLesley Hutchins | 9d6e7f3 | 2012-07-03 18:25:56 +0000 | [diff] [blame] | 58 | |
DeLesley Hutchins | 0b4db3e | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 59 | // The universal lock, written "*", allows checking to be selectively turned |
| 60 | // off for a particular piece of code. |
| 61 | void beginNoWarnOnReads() SHARED_LOCK_FUNCTION("*"); |
| 62 | void endNoWarnOnReads() UNLOCK_FUNCTION("*"); |
| 63 | void beginNoWarnOnWrites() EXCLUSIVE_LOCK_FUNCTION("*"); |
| 64 | void endNoWarnOnWrites() UNLOCK_FUNCTION("*"); |
| 65 | |
| 66 | |
DeLesley Hutchins | 9d6e7f3 | 2012-07-03 18:25:56 +0000 | [diff] [blame] | 67 | template<class T> |
| 68 | class SmartPtr { |
| 69 | public: |
| 70 | SmartPtr(T* p) : ptr_(p) { } |
| 71 | SmartPtr(const SmartPtr<T>& p) : ptr_(p.ptr_) { } |
| 72 | ~SmartPtr(); |
| 73 | |
| 74 | T* get() const { return ptr_; } |
| 75 | T* operator->() const { return ptr_; } |
DeLesley Hutchins | 96fac6a | 2012-07-03 19:47:18 +0000 | [diff] [blame] | 76 | T& operator*() const { return *ptr_; } |
DeLesley Hutchins | 9d6e7f3 | 2012-07-03 18:25:56 +0000 | [diff] [blame] | 77 | |
| 78 | private: |
| 79 | T* ptr_; |
| 80 | }; |
| 81 | |
| 82 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 83 | Mutex sls_mu; |
| 84 | |
| 85 | Mutex sls_mu2 __attribute__((acquired_after(sls_mu))); |
| 86 | int sls_guard_var __attribute__((guarded_var)) = 0; |
| 87 | int sls_guardby_var __attribute__((guarded_by(sls_mu))) = 0; |
| 88 | |
| 89 | bool getBool(); |
| 90 | |
| 91 | class MutexWrapper { |
| 92 | public: |
| 93 | Mutex mu; |
Caitlin Sadowski | 194418f | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 94 | int x __attribute__((guarded_by(mu))); |
| 95 | void MyLock() __attribute__((exclusive_lock_function(mu))); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | MutexWrapper sls_mw; |
| 99 | |
| 100 | void sls_fun_0() { |
| 101 | sls_mw.mu.Lock(); |
Caitlin Sadowski | 194418f | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 102 | sls_mw.x = 5; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 103 | sls_mw.mu.Unlock(); |
| 104 | } |
| 105 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 106 | void sls_fun_2() { |
| 107 | sls_mu.Lock(); |
| 108 | int x = sls_guard_var; |
| 109 | sls_mu.Unlock(); |
| 110 | } |
| 111 | |
| 112 | void sls_fun_3() { |
| 113 | sls_mu.Lock(); |
| 114 | sls_guard_var = 2; |
| 115 | sls_mu.Unlock(); |
| 116 | } |
| 117 | |
| 118 | void sls_fun_4() { |
| 119 | sls_mu2.Lock(); |
| 120 | sls_guard_var = 2; |
| 121 | sls_mu2.Unlock(); |
| 122 | } |
| 123 | |
| 124 | void sls_fun_5() { |
| 125 | sls_mu.Lock(); |
| 126 | int x = sls_guardby_var; |
| 127 | sls_mu.Unlock(); |
| 128 | } |
| 129 | |
| 130 | void sls_fun_6() { |
| 131 | sls_mu.Lock(); |
| 132 | sls_guardby_var = 2; |
| 133 | sls_mu.Unlock(); |
| 134 | } |
| 135 | |
| 136 | void sls_fun_7() { |
| 137 | sls_mu.Lock(); |
| 138 | sls_mu2.Lock(); |
| 139 | sls_mu2.Unlock(); |
| 140 | sls_mu.Unlock(); |
| 141 | } |
| 142 | |
| 143 | void sls_fun_8() { |
| 144 | sls_mu.Lock(); |
| 145 | if (getBool()) |
| 146 | sls_mu.Unlock(); |
| 147 | else |
| 148 | sls_mu.Unlock(); |
| 149 | } |
| 150 | |
| 151 | void sls_fun_9() { |
| 152 | if (getBool()) |
| 153 | sls_mu.Lock(); |
| 154 | else |
| 155 | sls_mu.Lock(); |
| 156 | sls_mu.Unlock(); |
| 157 | } |
| 158 | |
| 159 | void sls_fun_good_6() { |
| 160 | if (getBool()) { |
| 161 | sls_mu.Lock(); |
| 162 | } else { |
| 163 | if (getBool()) { |
| 164 | getBool(); // EMPTY |
| 165 | } else { |
| 166 | getBool(); // EMPTY |
| 167 | } |
| 168 | sls_mu.Lock(); |
| 169 | } |
| 170 | sls_mu.Unlock(); |
| 171 | } |
| 172 | |
| 173 | void sls_fun_good_7() { |
| 174 | sls_mu.Lock(); |
| 175 | while (getBool()) { |
| 176 | sls_mu.Unlock(); |
| 177 | if (getBool()) { |
| 178 | if (getBool()) { |
| 179 | sls_mu.Lock(); |
| 180 | continue; |
| 181 | } |
| 182 | } |
| 183 | sls_mu.Lock(); |
| 184 | } |
| 185 | sls_mu.Unlock(); |
| 186 | } |
| 187 | |
Caitlin Sadowski | 194418f | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 188 | void sls_fun_good_8() { |
| 189 | sls_mw.MyLock(); |
| 190 | sls_mw.mu.Unlock(); |
| 191 | } |
| 192 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 193 | void sls_fun_bad_1() { |
| 194 | sls_mu.Unlock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 195 | // expected-warning{{unlocking 'sls_mu' that was not locked}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void sls_fun_bad_2() { |
| 199 | sls_mu.Lock(); |
| 200 | sls_mu.Lock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 201 | // expected-warning{{locking 'sls_mu' that is already locked}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 202 | sls_mu.Unlock(); |
| 203 | } |
| 204 | |
| 205 | void sls_fun_bad_3() { |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 206 | sls_mu.Lock(); // expected-note {{mutex acquired here}} |
| 207 | } // expected-warning{{mutex 'sls_mu' is still locked at the end of function}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 208 | |
| 209 | void sls_fun_bad_4() { |
| 210 | if (getBool()) |
DeLesley Hutchins | 0da4414 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 211 | sls_mu.Lock(); // expected-note{{mutex acquired here}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 212 | else |
DeLesley Hutchins | 0da4414 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 213 | sls_mu2.Lock(); // expected-note{{mutex acquired here}} |
| 214 | } // expected-warning{{mutex 'sls_mu' is not locked on every path through here}} \ |
| 215 | // expected-warning{{mutex 'sls_mu2' is not locked on every path through here}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 216 | |
| 217 | void sls_fun_bad_5() { |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 218 | sls_mu.Lock(); // expected-note {{mutex acquired here}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 219 | if (getBool()) |
| 220 | sls_mu.Unlock(); |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 221 | } // expected-warning{{mutex 'sls_mu' is not locked on every path through here}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 222 | |
| 223 | void sls_fun_bad_6() { |
| 224 | if (getBool()) { |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 225 | sls_mu.Lock(); // expected-note {{mutex acquired here}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 226 | } else { |
| 227 | if (getBool()) { |
| 228 | getBool(); // EMPTY |
| 229 | } else { |
| 230 | getBool(); // EMPTY |
| 231 | } |
| 232 | } |
| 233 | sls_mu.Unlock(); // \ |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 234 | expected-warning{{mutex 'sls_mu' is not locked on every path through here}}\ |
| 235 | expected-warning{{unlocking 'sls_mu' that was not locked}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | void sls_fun_bad_7() { |
Richard Smith | aacde71 | 2012-02-03 03:30:07 +0000 | [diff] [blame] | 239 | sls_mu.Lock(); |
Caitlin Sadowski | 4e4bc75 | 2011-09-15 17:25:19 +0000 | [diff] [blame] | 240 | while (getBool()) { |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 241 | sls_mu.Unlock(); |
| 242 | if (getBool()) { |
| 243 | if (getBool()) { |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 244 | continue; // \ |
| 245 | expected-warning{{expecting mutex 'sls_mu' to be locked at start of each loop}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 248 | sls_mu.Lock(); // expected-note {{mutex acquired here}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 249 | } |
| 250 | sls_mu.Unlock(); |
| 251 | } |
| 252 | |
| 253 | void sls_fun_bad_8() { |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 254 | sls_mu.Lock(); // expected-note{{mutex acquired here}} |
| 255 | |
DeLesley Hutchins | 0da4414 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 256 | do { |
| 257 | sls_mu.Unlock(); // expected-warning{{expecting mutex 'sls_mu' to be locked at start of each loop}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 258 | } while (getBool()); |
| 259 | } |
| 260 | |
| 261 | void sls_fun_bad_9() { |
| 262 | do { |
DeLesley Hutchins | 0da4414 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 263 | sls_mu.Lock(); // \ |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 264 | // expected-warning{{expecting mutex 'sls_mu' to be locked at start of each loop}} \ |
| 265 | // expected-note{{mutex acquired here}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 266 | } while (getBool()); |
| 267 | sls_mu.Unlock(); |
| 268 | } |
| 269 | |
| 270 | void sls_fun_bad_10() { |
DeLesley Hutchins | 0da4414 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 271 | sls_mu.Lock(); // expected-note 2{{mutex acquired here}} |
| 272 | while(getBool()) { // expected-warning{{expecting mutex 'sls_mu' to be locked at start of each loop}} |
| 273 | sls_mu.Unlock(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 274 | } |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 275 | } // expected-warning{{mutex 'sls_mu' is still locked at the end of function}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 276 | |
| 277 | void sls_fun_bad_11() { |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 278 | while (getBool()) { // \ |
DeLesley Hutchins | 0da4414 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 279 | expected-warning{{expecting mutex 'sls_mu' to be locked at start of each loop}} |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 280 | sls_mu.Lock(); // expected-note {{mutex acquired here}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 281 | } |
| 282 | sls_mu.Unlock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 283 | // expected-warning{{unlocking 'sls_mu' that was not locked}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Richard Smith | aacde71 | 2012-02-03 03:30:07 +0000 | [diff] [blame] | 286 | void sls_fun_bad_12() { |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 287 | sls_mu.Lock(); // expected-note {{mutex acquired here}} |
Richard Smith | aacde71 | 2012-02-03 03:30:07 +0000 | [diff] [blame] | 288 | while (getBool()) { |
| 289 | sls_mu.Unlock(); |
| 290 | if (getBool()) { |
| 291 | if (getBool()) { |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 292 | break; // expected-warning{{mutex 'sls_mu' is not locked on every path through here}} |
Richard Smith | aacde71 | 2012-02-03 03:30:07 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | sls_mu.Lock(); |
| 296 | } |
| 297 | sls_mu.Unlock(); |
| 298 | } |
| 299 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 300 | //-----------------------------------------// |
| 301 | // Handling lock expressions in attribute args |
| 302 | // -------------------------------------------// |
| 303 | |
| 304 | Mutex aa_mu; |
| 305 | |
| 306 | class GlobalLocker { |
| 307 | public: |
| 308 | void globalLock() __attribute__((exclusive_lock_function(aa_mu))); |
| 309 | void globalUnlock() __attribute__((unlock_function(aa_mu))); |
| 310 | }; |
| 311 | |
| 312 | GlobalLocker glock; |
| 313 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 314 | void aa_fun_1() { |
| 315 | glock.globalLock(); |
| 316 | glock.globalUnlock(); |
| 317 | } |
| 318 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 319 | void aa_fun_bad_1() { |
| 320 | glock.globalUnlock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 321 | // expected-warning{{unlocking 'aa_mu' that was not locked}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | void aa_fun_bad_2() { |
| 325 | glock.globalLock(); |
| 326 | glock.globalLock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 327 | // expected-warning{{locking 'aa_mu' that is already locked}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 328 | glock.globalUnlock(); |
| 329 | } |
| 330 | |
| 331 | void aa_fun_bad_3() { |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 332 | glock.globalLock(); // expected-note{{mutex acquired here}} |
| 333 | } // expected-warning{{mutex 'aa_mu' is still locked at the end of function}} |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 334 | |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 335 | //--------------------------------------------------// |
| 336 | // Regression tests for unusual method names |
| 337 | //--------------------------------------------------// |
| 338 | |
| 339 | Mutex wmu; |
| 340 | |
| 341 | // Test diagnostics for other method names. |
| 342 | class WeirdMethods { |
DeLesley Hutchins | 2f13bec | 2012-02-16 17:13:43 +0000 | [diff] [blame] | 343 | // FIXME: can't currently check inside constructors and destructors. |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 344 | WeirdMethods() { |
DeLesley Hutchins | 2f13bec | 2012-02-16 17:13:43 +0000 | [diff] [blame] | 345 | wmu.Lock(); // EXPECTED-NOTE {{mutex acquired here}} |
| 346 | } // EXPECTED-WARNING {{mutex 'wmu' is still locked at the end of function}} |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 347 | ~WeirdMethods() { |
DeLesley Hutchins | 2f13bec | 2012-02-16 17:13:43 +0000 | [diff] [blame] | 348 | wmu.Lock(); // EXPECTED-NOTE {{mutex acquired here}} |
| 349 | } // EXPECTED-WARNING {{mutex 'wmu' is still locked at the end of function}} |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 350 | void operator++() { |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 351 | wmu.Lock(); // expected-note {{mutex acquired here}} |
| 352 | } // expected-warning {{mutex 'wmu' is still locked at the end of function}} |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 353 | operator int*() { |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 354 | wmu.Lock(); // expected-note {{mutex acquired here}} |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 355 | return 0; |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 356 | } // expected-warning {{mutex 'wmu' is still locked at the end of function}} |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 357 | }; |
| 358 | |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 359 | //-----------------------------------------------// |
| 360 | // Errors for guarded by or guarded var variables |
| 361 | // ----------------------------------------------// |
| 362 | |
| 363 | int *pgb_gvar __attribute__((pt_guarded_var)); |
| 364 | int *pgb_var __attribute__((pt_guarded_by(sls_mu))); |
| 365 | |
| 366 | class PGBFoo { |
| 367 | public: |
| 368 | int x; |
| 369 | int *pgb_field __attribute__((guarded_by(sls_mu2))) |
| 370 | __attribute__((pt_guarded_by(sls_mu))); |
| 371 | void testFoo() { |
| 372 | pgb_field = &x; // \ |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 373 | // expected-warning {{writing variable 'pgb_field' requires locking 'sls_mu2' exclusively}} |
| 374 | *pgb_field = x; // expected-warning {{reading variable 'pgb_field' requires locking 'sls_mu2'}} \ |
| 375 | // expected-warning {{writing the value pointed to by 'pgb_field' requires locking 'sls_mu' exclusively}} |
| 376 | x = *pgb_field; // expected-warning {{reading variable 'pgb_field' requires locking 'sls_mu2'}} \ |
| 377 | // expected-warning {{reading the value pointed to by 'pgb_field' requires locking 'sls_mu'}} |
| 378 | (*pgb_field)++; // expected-warning {{reading variable 'pgb_field' requires locking 'sls_mu2'}} \ |
| 379 | // expected-warning {{writing the value pointed to by 'pgb_field' requires locking 'sls_mu' exclusively}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 380 | } |
| 381 | }; |
| 382 | |
| 383 | class GBFoo { |
| 384 | public: |
| 385 | int gb_field __attribute__((guarded_by(sls_mu))); |
| 386 | |
| 387 | void testFoo() { |
| 388 | gb_field = 0; // \ |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 389 | // expected-warning {{writing variable 'gb_field' requires locking 'sls_mu' exclusively}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 390 | } |
Caitlin Sadowski | af37061 | 2011-09-08 18:35:21 +0000 | [diff] [blame] | 391 | |
| 392 | void testNoAnal() __attribute__((no_thread_safety_analysis)) { |
| 393 | gb_field = 0; |
| 394 | } |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 395 | }; |
| 396 | |
| 397 | GBFoo GlobalGBFoo __attribute__((guarded_by(sls_mu))); |
| 398 | |
| 399 | void gb_fun_0() { |
| 400 | sls_mu.Lock(); |
| 401 | int x = *pgb_var; |
| 402 | sls_mu.Unlock(); |
| 403 | } |
| 404 | |
| 405 | void gb_fun_1() { |
| 406 | sls_mu.Lock(); |
| 407 | *pgb_var = 2; |
| 408 | sls_mu.Unlock(); |
| 409 | } |
| 410 | |
| 411 | void gb_fun_2() { |
| 412 | int x; |
| 413 | pgb_var = &x; |
| 414 | } |
| 415 | |
| 416 | void gb_fun_3() { |
| 417 | int *x = pgb_var; |
| 418 | } |
| 419 | |
| 420 | void gb_bad_0() { |
| 421 | sls_guard_var = 1; // \ |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 422 | // expected-warning{{writing variable 'sls_guard_var' requires locking any mutex exclusively}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | void gb_bad_1() { |
| 426 | int x = sls_guard_var; // \ |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 427 | // expected-warning{{reading variable 'sls_guard_var' requires locking any mutex}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | void gb_bad_2() { |
| 431 | sls_guardby_var = 1; // \ |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 432 | // expected-warning {{writing variable 'sls_guardby_var' requires locking 'sls_mu' exclusively}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | void gb_bad_3() { |
| 436 | int x = sls_guardby_var; // \ |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 437 | // expected-warning {{reading variable 'sls_guardby_var' requires locking 'sls_mu'}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | void gb_bad_4() { |
| 441 | *pgb_gvar = 1; // \ |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 442 | // expected-warning {{writing the value pointed to by 'pgb_gvar' requires locking any mutex exclusively}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | void gb_bad_5() { |
| 446 | int x = *pgb_gvar; // \ |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 447 | // expected-warning {{reading the value pointed to by 'pgb_gvar' requires locking any mutex}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | void gb_bad_6() { |
| 451 | *pgb_var = 1; // \ |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 452 | // expected-warning {{writing the value pointed to by 'pgb_var' requires locking 'sls_mu' exclusively}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | void gb_bad_7() { |
| 456 | int x = *pgb_var; // \ |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 457 | // expected-warning {{reading the value pointed to by 'pgb_var' requires locking 'sls_mu'}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | void gb_bad_8() { |
| 461 | GBFoo G; |
| 462 | G.gb_field = 0; // \ |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 463 | // expected-warning {{writing variable 'gb_field' requires locking 'sls_mu'}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | void gb_bad_9() { |
| 467 | sls_guard_var++; // \ |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 468 | // expected-warning{{writing variable 'sls_guard_var' requires locking any mutex exclusively}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 469 | sls_guard_var--; // \ |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 470 | // expected-warning{{writing variable 'sls_guard_var' requires locking any mutex exclusively}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 471 | ++sls_guard_var; // \ |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 472 | // expected-warning{{writing variable 'sls_guard_var' requires locking any mutex exclusively}} |
Caitlin Sadowski | a49d1d8 | 2011-09-09 16:07:55 +0000 | [diff] [blame] | 473 | --sls_guard_var;// \ |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 474 | // expected-warning{{writing variable 'sls_guard_var' requires locking any mutex exclusively}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 475 | } |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 476 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 477 | //-----------------------------------------------// |
| 478 | // Warnings on variables with late parsed attributes |
| 479 | // ----------------------------------------------// |
| 480 | |
| 481 | class LateFoo { |
| 482 | public: |
| 483 | int a __attribute__((guarded_by(mu))); |
| 484 | int b; |
| 485 | |
| 486 | void foo() __attribute__((exclusive_locks_required(mu))) { } |
| 487 | |
| 488 | void test() { |
| 489 | a = 0; // \ |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 490 | // expected-warning{{writing variable 'a' requires locking 'mu' exclusively}} |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 491 | b = a; // \ |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 492 | // expected-warning {{reading variable 'a' requires locking 'mu'}} |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 493 | c = 0; // \ |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 494 | // expected-warning {{writing variable 'c' requires locking 'mu' exclusively}} |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | int c __attribute__((guarded_by(mu))); |
| 498 | |
| 499 | Mutex mu; |
| 500 | }; |
| 501 | |
Caitlin Sadowski | 99107eb | 2011-09-09 16:21:55 +0000 | [diff] [blame] | 502 | class LateBar { |
| 503 | public: |
| 504 | int a_ __attribute__((guarded_by(mu1_))); |
| 505 | int b_; |
| 506 | int *q __attribute__((pt_guarded_by(mu))); |
| 507 | Mutex mu1_; |
| 508 | Mutex mu; |
| 509 | LateFoo Foo; |
| 510 | LateFoo Foo2; |
| 511 | LateFoo *FooPointer; |
| 512 | }; |
| 513 | |
| 514 | LateBar b1, *b3; |
| 515 | |
| 516 | void late_0() { |
| 517 | LateFoo FooA; |
| 518 | LateFoo FooB; |
| 519 | FooA.mu.Lock(); |
| 520 | FooA.a = 5; |
| 521 | FooA.mu.Unlock(); |
| 522 | } |
| 523 | |
| 524 | void late_1() { |
| 525 | LateBar BarA; |
| 526 | BarA.FooPointer->mu.Lock(); |
| 527 | BarA.FooPointer->a = 2; |
| 528 | BarA.FooPointer->mu.Unlock(); |
| 529 | } |
| 530 | |
| 531 | void late_bad_0() { |
| 532 | LateFoo fooA; |
| 533 | LateFoo fooB; |
| 534 | fooA.mu.Lock(); |
| 535 | fooB.a = 5; // \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 536 | // expected-warning{{writing variable 'a' requires locking 'fooB.mu' exclusively}} |
Caitlin Sadowski | 99107eb | 2011-09-09 16:21:55 +0000 | [diff] [blame] | 537 | fooA.mu.Unlock(); |
| 538 | } |
| 539 | |
| 540 | void late_bad_1() { |
| 541 | Mutex mu; |
| 542 | mu.Lock(); |
| 543 | b1.mu1_.Lock(); |
| 544 | int res = b1.a_ + b3->b_; |
| 545 | b3->b_ = *b1.q; // \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 546 | // expected-warning{{reading the value pointed to by 'q' requires locking 'b1.mu'}} |
Caitlin Sadowski | 99107eb | 2011-09-09 16:21:55 +0000 | [diff] [blame] | 547 | b1.mu1_.Unlock(); |
| 548 | b1.b_ = res; |
| 549 | mu.Unlock(); |
| 550 | } |
| 551 | |
| 552 | void late_bad_2() { |
| 553 | LateBar BarA; |
| 554 | BarA.FooPointer->mu.Lock(); |
| 555 | BarA.Foo.a = 2; // \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 556 | // expected-warning{{writing variable 'a' requires locking 'BarA.Foo.mu' exclusively}} |
Caitlin Sadowski | 99107eb | 2011-09-09 16:21:55 +0000 | [diff] [blame] | 557 | BarA.FooPointer->mu.Unlock(); |
| 558 | } |
| 559 | |
| 560 | void late_bad_3() { |
| 561 | LateBar BarA; |
| 562 | BarA.Foo.mu.Lock(); |
| 563 | BarA.FooPointer->a = 2; // \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 564 | // expected-warning{{writing variable 'a' requires locking 'BarA.FooPointer->mu' exclusively}} |
Caitlin Sadowski | 99107eb | 2011-09-09 16:21:55 +0000 | [diff] [blame] | 565 | BarA.Foo.mu.Unlock(); |
| 566 | } |
| 567 | |
| 568 | void late_bad_4() { |
| 569 | LateBar BarA; |
| 570 | BarA.Foo.mu.Lock(); |
| 571 | BarA.Foo2.a = 2; // \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 572 | // expected-warning{{writing variable 'a' requires locking 'BarA.Foo2.mu' exclusively}} |
Caitlin Sadowski | 99107eb | 2011-09-09 16:21:55 +0000 | [diff] [blame] | 573 | BarA.Foo.mu.Unlock(); |
| 574 | } |
| 575 | |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 576 | //-----------------------------------------------// |
| 577 | // Extra warnings for shared vs. exclusive locks |
| 578 | // ----------------------------------------------// |
| 579 | |
| 580 | void shared_fun_0() { |
| 581 | sls_mu.Lock(); |
| 582 | do { |
| 583 | sls_mu.Unlock(); |
| 584 | sls_mu.Lock(); |
| 585 | } while (getBool()); |
| 586 | sls_mu.Unlock(); |
| 587 | } |
| 588 | |
| 589 | void shared_fun_1() { |
Caitlin Sadowski | 4e4bc75 | 2011-09-15 17:25:19 +0000 | [diff] [blame] | 590 | sls_mu.ReaderLock(); // \ |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 591 | // expected-warning {{mutex 'sls_mu' is locked exclusively and shared in the same scope}} |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 592 | do { |
| 593 | sls_mu.Unlock(); |
Caitlin Sadowski | 4e4bc75 | 2011-09-15 17:25:19 +0000 | [diff] [blame] | 594 | sls_mu.Lock(); // \ |
| 595 | // expected-note {{the other lock of mutex 'sls_mu' is here}} |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 596 | } while (getBool()); |
| 597 | sls_mu.Unlock(); |
| 598 | } |
| 599 | |
| 600 | void shared_fun_3() { |
| 601 | if (getBool()) |
| 602 | sls_mu.Lock(); |
| 603 | else |
| 604 | sls_mu.Lock(); |
| 605 | *pgb_var = 1; |
| 606 | sls_mu.Unlock(); |
| 607 | } |
| 608 | |
| 609 | void shared_fun_4() { |
| 610 | if (getBool()) |
| 611 | sls_mu.ReaderLock(); |
| 612 | else |
| 613 | sls_mu.ReaderLock(); |
| 614 | int x = sls_guardby_var; |
| 615 | sls_mu.Unlock(); |
| 616 | } |
| 617 | |
| 618 | void shared_fun_8() { |
| 619 | if (getBool()) |
| 620 | sls_mu.Lock(); // \ |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 621 | // expected-warning {{mutex 'sls_mu' is locked exclusively and shared in the same scope}} |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 622 | else |
| 623 | sls_mu.ReaderLock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 624 | // expected-note {{the other lock of mutex 'sls_mu' is here}} |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 625 | sls_mu.Unlock(); |
| 626 | } |
| 627 | |
| 628 | void shared_bad_0() { |
Caitlin Sadowski | 4e4bc75 | 2011-09-15 17:25:19 +0000 | [diff] [blame] | 629 | sls_mu.Lock(); // \ |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 630 | // expected-warning {{mutex 'sls_mu' is locked exclusively and shared in the same scope}} |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 631 | do { |
| 632 | sls_mu.Unlock(); |
Caitlin Sadowski | 4e4bc75 | 2011-09-15 17:25:19 +0000 | [diff] [blame] | 633 | sls_mu.ReaderLock(); // \ |
| 634 | // expected-note {{the other lock of mutex 'sls_mu' is here}} |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 635 | } while (getBool()); |
| 636 | sls_mu.Unlock(); |
| 637 | } |
| 638 | |
| 639 | void shared_bad_1() { |
| 640 | if (getBool()) |
| 641 | sls_mu.Lock(); // \ |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 642 | // expected-warning {{mutex 'sls_mu' is locked exclusively and shared in the same scope}} |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 643 | else |
| 644 | sls_mu.ReaderLock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 645 | // expected-note {{the other lock of mutex 'sls_mu' is here}} |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 646 | *pgb_var = 1; |
| 647 | sls_mu.Unlock(); |
| 648 | } |
| 649 | |
| 650 | void shared_bad_2() { |
| 651 | if (getBool()) |
| 652 | sls_mu.ReaderLock(); // \ |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 653 | // expected-warning {{mutex 'sls_mu' is locked exclusively and shared in the same scope}} |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 654 | else |
| 655 | sls_mu.Lock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 656 | // expected-note {{the other lock of mutex 'sls_mu' is here}} |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 657 | *pgb_var = 1; |
| 658 | sls_mu.Unlock(); |
| 659 | } |
Caitlin Sadowski | 978191e | 2011-09-08 18:27:31 +0000 | [diff] [blame] | 660 | |
| 661 | // FIXME: Add support for functions (not only methods) |
| 662 | class LRBar { |
| 663 | public: |
| 664 | void aa_elr_fun() __attribute__((exclusive_locks_required(aa_mu))); |
| 665 | void aa_elr_fun_s() __attribute__((shared_locks_required(aa_mu))); |
| 666 | void le_fun() __attribute__((locks_excluded(sls_mu))); |
| 667 | }; |
| 668 | |
| 669 | class LRFoo { |
| 670 | public: |
| 671 | void test() __attribute__((exclusive_locks_required(sls_mu))); |
| 672 | void testShared() __attribute__((shared_locks_required(sls_mu2))); |
| 673 | }; |
| 674 | |
| 675 | void elr_fun() __attribute__((exclusive_locks_required(sls_mu))); |
| 676 | void elr_fun() {} |
| 677 | |
| 678 | LRFoo MyLRFoo; |
| 679 | LRBar Bar; |
| 680 | |
| 681 | void es_fun_0() { |
| 682 | aa_mu.Lock(); |
| 683 | Bar.aa_elr_fun(); |
| 684 | aa_mu.Unlock(); |
| 685 | } |
| 686 | |
| 687 | void es_fun_1() { |
| 688 | aa_mu.Lock(); |
| 689 | Bar.aa_elr_fun_s(); |
| 690 | aa_mu.Unlock(); |
| 691 | } |
| 692 | |
| 693 | void es_fun_2() { |
| 694 | aa_mu.ReaderLock(); |
| 695 | Bar.aa_elr_fun_s(); |
| 696 | aa_mu.Unlock(); |
| 697 | } |
| 698 | |
| 699 | void es_fun_3() { |
| 700 | sls_mu.Lock(); |
| 701 | MyLRFoo.test(); |
| 702 | sls_mu.Unlock(); |
| 703 | } |
| 704 | |
| 705 | void es_fun_4() { |
| 706 | sls_mu2.Lock(); |
| 707 | MyLRFoo.testShared(); |
| 708 | sls_mu2.Unlock(); |
| 709 | } |
| 710 | |
| 711 | void es_fun_5() { |
| 712 | sls_mu2.ReaderLock(); |
| 713 | MyLRFoo.testShared(); |
| 714 | sls_mu2.Unlock(); |
| 715 | } |
| 716 | |
| 717 | void es_fun_6() { |
| 718 | Bar.le_fun(); |
| 719 | } |
| 720 | |
| 721 | void es_fun_7() { |
| 722 | sls_mu.Lock(); |
| 723 | elr_fun(); |
| 724 | sls_mu.Unlock(); |
| 725 | } |
| 726 | |
Caitlin Sadowski | af37061 | 2011-09-08 18:35:21 +0000 | [diff] [blame] | 727 | void es_fun_8() __attribute__((no_thread_safety_analysis)); |
| 728 | |
| 729 | void es_fun_8() { |
| 730 | Bar.aa_elr_fun_s(); |
| 731 | } |
| 732 | |
Caitlin Sadowski | cb96751 | 2011-09-15 17:43:08 +0000 | [diff] [blame] | 733 | void es_fun_9() __attribute__((shared_locks_required(aa_mu))); |
| 734 | void es_fun_9() { |
| 735 | Bar.aa_elr_fun_s(); |
| 736 | } |
| 737 | |
| 738 | void es_fun_10() __attribute__((exclusive_locks_required(aa_mu))); |
| 739 | void es_fun_10() { |
| 740 | Bar.aa_elr_fun_s(); |
| 741 | } |
| 742 | |
Caitlin Sadowski | 978191e | 2011-09-08 18:27:31 +0000 | [diff] [blame] | 743 | void es_bad_0() { |
| 744 | Bar.aa_elr_fun(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 745 | // expected-warning {{calling function 'aa_elr_fun' requires exclusive lock on 'aa_mu'}} |
Caitlin Sadowski | 978191e | 2011-09-08 18:27:31 +0000 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | void es_bad_1() { |
| 749 | aa_mu.ReaderLock(); |
| 750 | Bar.aa_elr_fun(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 751 | // expected-warning {{calling function 'aa_elr_fun' requires exclusive lock on 'aa_mu'}} |
Caitlin Sadowski | 978191e | 2011-09-08 18:27:31 +0000 | [diff] [blame] | 752 | aa_mu.Unlock(); |
| 753 | } |
| 754 | |
| 755 | void es_bad_2() { |
| 756 | Bar.aa_elr_fun_s(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 757 | // expected-warning {{calling function 'aa_elr_fun_s' requires shared lock on 'aa_mu'}} |
Caitlin Sadowski | 978191e | 2011-09-08 18:27:31 +0000 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | void es_bad_3() { |
| 761 | MyLRFoo.test(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 762 | // expected-warning {{calling function 'test' requires exclusive lock on 'sls_mu'}} |
Caitlin Sadowski | 978191e | 2011-09-08 18:27:31 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | void es_bad_4() { |
| 766 | MyLRFoo.testShared(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 767 | // expected-warning {{calling function 'testShared' requires shared lock on 'sls_mu2'}} |
Caitlin Sadowski | 978191e | 2011-09-08 18:27:31 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | void es_bad_5() { |
| 771 | sls_mu.ReaderLock(); |
| 772 | MyLRFoo.test(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 773 | // expected-warning {{calling function 'test' requires exclusive lock on 'sls_mu'}} |
Caitlin Sadowski | 978191e | 2011-09-08 18:27:31 +0000 | [diff] [blame] | 774 | sls_mu.Unlock(); |
| 775 | } |
| 776 | |
| 777 | void es_bad_6() { |
| 778 | sls_mu.Lock(); |
| 779 | Bar.le_fun(); // \ |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 780 | // expected-warning {{cannot call function 'le_fun' while mutex 'sls_mu' is locked}} |
Caitlin Sadowski | 978191e | 2011-09-08 18:27:31 +0000 | [diff] [blame] | 781 | sls_mu.Unlock(); |
| 782 | } |
| 783 | |
| 784 | void es_bad_7() { |
| 785 | sls_mu.ReaderLock(); |
| 786 | Bar.le_fun(); // \ |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 787 | // expected-warning {{cannot call function 'le_fun' while mutex 'sls_mu' is locked}} |
Caitlin Sadowski | 978191e | 2011-09-08 18:27:31 +0000 | [diff] [blame] | 788 | sls_mu.Unlock(); |
| 789 | } |
Caitlin Sadowski | 194418f | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 790 | |
DeLesley Hutchins | 8121639 | 2011-10-17 21:38:02 +0000 | [diff] [blame] | 791 | |
Caitlin Sadowski | 194418f | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 792 | //-----------------------------------------------// |
| 793 | // Unparseable lock expressions |
| 794 | // ----------------------------------------------// |
| 795 | |
DeLesley Hutchins | 0d95dfc | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 796 | // FIXME -- derive new tests for unhandled expressions |
Caitlin Sadowski | 194418f | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 797 | |
Caitlin Sadowski | 988b5ae | 2011-09-15 18:07:32 +0000 | [diff] [blame] | 798 | |
| 799 | //----------------------------------------------------------------------------// |
| 800 | // The following test cases are ported from the gcc thread safety implementation |
| 801 | // They are each wrapped inside a namespace with the test number of the gcc test |
| 802 | // |
| 803 | // FIXME: add all the gcc tests, once this analysis passes them. |
| 804 | //----------------------------------------------------------------------------// |
| 805 | |
| 806 | //-----------------------------------------// |
| 807 | // Good testcases (no errors) |
| 808 | //-----------------------------------------// |
| 809 | |
| 810 | namespace thread_annot_lock_20 { |
| 811 | class Bar { |
| 812 | public: |
| 813 | static int func1() EXCLUSIVE_LOCKS_REQUIRED(mu1_); |
| 814 | static int b_ GUARDED_BY(mu1_); |
| 815 | static Mutex mu1_; |
| 816 | static int a_ GUARDED_BY(mu1_); |
| 817 | }; |
| 818 | |
| 819 | Bar b1; |
| 820 | |
| 821 | int Bar::func1() |
| 822 | { |
| 823 | int res = 5; |
| 824 | |
| 825 | if (a_ == 4) |
| 826 | res = b_; |
| 827 | return res; |
| 828 | } |
| 829 | } // end namespace thread_annot_lock_20 |
| 830 | |
| 831 | namespace thread_annot_lock_22 { |
| 832 | // Test various usage of GUARDED_BY and PT_GUARDED_BY annotations, especially |
| 833 | // uses in class definitions. |
| 834 | Mutex mu; |
| 835 | |
| 836 | class Bar { |
| 837 | public: |
| 838 | int a_ GUARDED_BY(mu1_); |
| 839 | int b_; |
| 840 | int *q PT_GUARDED_BY(mu); |
| 841 | Mutex mu1_ ACQUIRED_AFTER(mu); |
| 842 | }; |
| 843 | |
| 844 | Bar b1, *b3; |
| 845 | int *p GUARDED_BY(mu) PT_GUARDED_BY(mu); |
| 846 | int res GUARDED_BY(mu) = 5; |
| 847 | |
| 848 | int func(int i) |
| 849 | { |
| 850 | int x; |
| 851 | mu.Lock(); |
| 852 | b1.mu1_.Lock(); |
| 853 | res = b1.a_ + b3->b_; |
| 854 | *p = i; |
| 855 | b1.a_ = res + b3->b_; |
| 856 | b3->b_ = *b1.q; |
| 857 | b1.mu1_.Unlock(); |
| 858 | b1.b_ = res; |
| 859 | x = res; |
| 860 | mu.Unlock(); |
| 861 | return x; |
| 862 | } |
| 863 | } // end namespace thread_annot_lock_22 |
| 864 | |
| 865 | namespace thread_annot_lock_27_modified { |
| 866 | // test lock annotations applied to function definitions |
| 867 | // Modified: applied annotations only to function declarations |
| 868 | Mutex mu1; |
| 869 | Mutex mu2 ACQUIRED_AFTER(mu1); |
| 870 | |
| 871 | class Foo { |
| 872 | public: |
| 873 | int method1(int i) SHARED_LOCKS_REQUIRED(mu2) EXCLUSIVE_LOCKS_REQUIRED(mu1); |
| 874 | }; |
| 875 | |
| 876 | int Foo::method1(int i) { |
| 877 | return i; |
| 878 | } |
| 879 | |
| 880 | |
| 881 | int foo(int i) EXCLUSIVE_LOCKS_REQUIRED(mu2) SHARED_LOCKS_REQUIRED(mu1); |
| 882 | int foo(int i) { |
| 883 | return i; |
| 884 | } |
| 885 | |
| 886 | static int bar(int i) EXCLUSIVE_LOCKS_REQUIRED(mu1); |
| 887 | static int bar(int i) { |
| 888 | return i; |
| 889 | } |
| 890 | |
| 891 | void main() { |
| 892 | Foo a; |
| 893 | |
| 894 | mu1.Lock(); |
| 895 | mu2.Lock(); |
| 896 | a.method1(1); |
| 897 | foo(2); |
| 898 | mu2.Unlock(); |
| 899 | bar(3); |
| 900 | mu1.Unlock(); |
| 901 | } |
| 902 | } // end namespace thread_annot_lock_27_modified |
| 903 | |
| 904 | |
| 905 | namespace thread_annot_lock_38 { |
| 906 | // Test the case where a template member function is annotated with lock |
| 907 | // attributes in a non-template class. |
| 908 | class Foo { |
| 909 | public: |
| 910 | void func1(int y) LOCKS_EXCLUDED(mu_); |
| 911 | template <typename T> void func2(T x) LOCKS_EXCLUDED(mu_); |
| 912 | private: |
| 913 | Mutex mu_; |
| 914 | }; |
| 915 | |
| 916 | Foo *foo; |
| 917 | |
| 918 | void main() |
| 919 | { |
| 920 | foo->func1(5); |
| 921 | foo->func2(5); |
| 922 | } |
| 923 | } // end namespace thread_annot_lock_38 |
| 924 | |
| 925 | namespace thread_annot_lock_43 { |
| 926 | // Tests lock canonicalization |
| 927 | class Foo { |
| 928 | public: |
| 929 | Mutex *mu_; |
| 930 | }; |
| 931 | |
| 932 | class FooBar { |
| 933 | public: |
| 934 | Foo *foo_; |
| 935 | int GetA() EXCLUSIVE_LOCKS_REQUIRED(foo_->mu_) { return a_; } |
| 936 | int a_ GUARDED_BY(foo_->mu_); |
| 937 | }; |
| 938 | |
| 939 | FooBar *fb; |
| 940 | |
| 941 | void main() |
| 942 | { |
| 943 | int x; |
| 944 | fb->foo_->mu_->Lock(); |
| 945 | x = fb->GetA(); |
| 946 | fb->foo_->mu_->Unlock(); |
| 947 | } |
| 948 | } // end namespace thread_annot_lock_43 |
| 949 | |
| 950 | namespace thread_annot_lock_49 { |
| 951 | // Test the support for use of lock expression in the annotations |
| 952 | class Foo { |
| 953 | public: |
| 954 | Mutex foo_mu_; |
| 955 | }; |
| 956 | |
| 957 | class Bar { |
| 958 | private: |
| 959 | Foo *foo; |
| 960 | Mutex bar_mu_ ACQUIRED_AFTER(foo->foo_mu_); |
| 961 | |
| 962 | public: |
| 963 | void Test1() { |
| 964 | foo->foo_mu_.Lock(); |
| 965 | bar_mu_.Lock(); |
| 966 | bar_mu_.Unlock(); |
| 967 | foo->foo_mu_.Unlock(); |
| 968 | } |
| 969 | }; |
| 970 | |
| 971 | void main() { |
| 972 | Bar bar; |
| 973 | bar.Test1(); |
| 974 | } |
| 975 | } // end namespace thread_annot_lock_49 |
| 976 | |
| 977 | namespace thread_annot_lock_61_modified { |
| 978 | // Modified to fix the compiler errors |
| 979 | // Test the fix for a bug introduced by the support of pass-by-reference |
| 980 | // paramters. |
| 981 | struct Foo { Foo &operator<< (bool) {return *this;} }; |
| 982 | Foo &getFoo(); |
| 983 | struct Bar { Foo &func () {return getFoo();} }; |
| 984 | struct Bas { void operator& (Foo &) {} }; |
| 985 | void mumble() |
| 986 | { |
| 987 | Bas() & Bar().func() << "" << ""; |
| 988 | Bas() & Bar().func() << ""; |
| 989 | } |
| 990 | } // end namespace thread_annot_lock_61_modified |
| 991 | |
| 992 | |
| 993 | namespace thread_annot_lock_65 { |
| 994 | // Test the fix for a bug in the support of allowing reader locks for |
| 995 | // non-const, non-modifying overload functions. (We didn't handle the builtin |
| 996 | // properly.) |
| 997 | enum MyFlags { |
| 998 | Zero, |
| 999 | One, |
| 1000 | Two, |
| 1001 | Three, |
| 1002 | Four, |
| 1003 | Five, |
| 1004 | Six, |
| 1005 | Seven, |
| 1006 | Eight, |
| 1007 | Nine |
| 1008 | }; |
| 1009 | |
| 1010 | inline MyFlags |
| 1011 | operator|(MyFlags a, MyFlags b) |
| 1012 | { |
| 1013 | return MyFlags(static_cast<int>(a) | static_cast<int>(b)); |
| 1014 | } |
| 1015 | |
| 1016 | inline MyFlags& |
| 1017 | operator|=(MyFlags& a, MyFlags b) |
| 1018 | { |
| 1019 | return a = a | b; |
| 1020 | } |
| 1021 | } // end namespace thread_annot_lock_65 |
| 1022 | |
| 1023 | namespace thread_annot_lock_66_modified { |
| 1024 | // Modified: Moved annotation to function defn |
| 1025 | // Test annotations on out-of-line definitions of member functions where the |
| 1026 | // annotations refer to locks that are also data members in the class. |
| 1027 | Mutex mu; |
| 1028 | |
| 1029 | class Foo { |
| 1030 | public: |
| 1031 | int method1(int i) SHARED_LOCKS_REQUIRED(mu1, mu, mu2); |
| 1032 | int data GUARDED_BY(mu1); |
| 1033 | Mutex *mu1; |
| 1034 | Mutex *mu2; |
| 1035 | }; |
| 1036 | |
| 1037 | int Foo::method1(int i) |
| 1038 | { |
| 1039 | return data + i; |
| 1040 | } |
| 1041 | |
| 1042 | void main() |
| 1043 | { |
| 1044 | Foo a; |
| 1045 | |
| 1046 | a.mu2->Lock(); |
| 1047 | a.mu1->Lock(); |
| 1048 | mu.Lock(); |
| 1049 | a.method1(1); |
| 1050 | mu.Unlock(); |
| 1051 | a.mu1->Unlock(); |
| 1052 | a.mu2->Unlock(); |
| 1053 | } |
| 1054 | } // end namespace thread_annot_lock_66_modified |
| 1055 | |
| 1056 | namespace thread_annot_lock_68_modified { |
| 1057 | // Test a fix to a bug in the delayed name binding with nested template |
| 1058 | // instantiation. We use a stack to make sure a name is not resolved to an |
| 1059 | // inner context. |
| 1060 | template <typename T> |
| 1061 | class Bar { |
| 1062 | Mutex mu_; |
| 1063 | }; |
| 1064 | |
| 1065 | template <typename T> |
| 1066 | class Foo { |
| 1067 | public: |
| 1068 | void func(T x) { |
| 1069 | mu_.Lock(); |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 1070 | count_ = x; |
Caitlin Sadowski | 988b5ae | 2011-09-15 18:07:32 +0000 | [diff] [blame] | 1071 | mu_.Unlock(); |
| 1072 | } |
| 1073 | |
| 1074 | private: |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 1075 | T count_ GUARDED_BY(mu_); |
Caitlin Sadowski | 988b5ae | 2011-09-15 18:07:32 +0000 | [diff] [blame] | 1076 | Bar<T> bar_; |
| 1077 | Mutex mu_; |
| 1078 | }; |
| 1079 | |
| 1080 | void main() |
| 1081 | { |
| 1082 | Foo<int> *foo; |
| 1083 | foo->func(5); |
| 1084 | } |
| 1085 | } // end namespace thread_annot_lock_68_modified |
| 1086 | |
| 1087 | namespace thread_annot_lock_30_modified { |
| 1088 | // Test delay parsing of lock attribute arguments with nested classes. |
| 1089 | // Modified: trylocks replaced with exclusive_lock_fun |
| 1090 | int a = 0; |
| 1091 | |
| 1092 | class Bar { |
| 1093 | struct Foo; |
| 1094 | |
| 1095 | public: |
| 1096 | void MyLock() EXCLUSIVE_LOCK_FUNCTION(mu); |
| 1097 | |
| 1098 | int func() { |
| 1099 | MyLock(); |
| 1100 | // if (foo == 0) { |
| 1101 | // return 0; |
| 1102 | // } |
| 1103 | a = 5; |
| 1104 | mu.Unlock(); |
| 1105 | return 1; |
| 1106 | } |
| 1107 | |
| 1108 | class FooBar { |
| 1109 | int x; |
| 1110 | int y; |
| 1111 | }; |
| 1112 | |
| 1113 | private: |
| 1114 | Mutex mu; |
| 1115 | }; |
| 1116 | |
| 1117 | Bar *bar; |
| 1118 | |
| 1119 | void main() |
| 1120 | { |
| 1121 | bar->func(); |
| 1122 | } |
| 1123 | } // end namespace thread_annot_lock_30_modified |
| 1124 | |
| 1125 | namespace thread_annot_lock_47 { |
| 1126 | // Test the support for annotations on virtual functions. |
| 1127 | // This is a good test case. (i.e. There should be no warning emitted by the |
| 1128 | // compiler.) |
| 1129 | class Base { |
| 1130 | public: |
| 1131 | virtual void func1() EXCLUSIVE_LOCKS_REQUIRED(mu_); |
| 1132 | virtual void func2() LOCKS_EXCLUDED(mu_); |
| 1133 | Mutex mu_; |
| 1134 | }; |
| 1135 | |
| 1136 | class Child : public Base { |
| 1137 | public: |
| 1138 | virtual void func1() EXCLUSIVE_LOCKS_REQUIRED(mu_); |
| 1139 | virtual void func2() LOCKS_EXCLUDED(mu_); |
| 1140 | }; |
| 1141 | |
| 1142 | void main() { |
| 1143 | Child *c; |
| 1144 | Base *b = c; |
| 1145 | |
| 1146 | b->mu_.Lock(); |
| 1147 | b->func1(); |
| 1148 | b->mu_.Unlock(); |
| 1149 | b->func2(); |
| 1150 | |
| 1151 | c->mu_.Lock(); |
| 1152 | c->func1(); |
| 1153 | c->mu_.Unlock(); |
| 1154 | c->func2(); |
| 1155 | } |
| 1156 | } // end namespace thread_annot_lock_47 |
| 1157 | |
| 1158 | //-----------------------------------------// |
| 1159 | // Tests which produce errors |
| 1160 | //-----------------------------------------// |
| 1161 | |
| 1162 | namespace thread_annot_lock_13 { |
| 1163 | Mutex mu1; |
| 1164 | Mutex mu2; |
| 1165 | |
| 1166 | int g GUARDED_BY(mu1); |
| 1167 | int w GUARDED_BY(mu2); |
| 1168 | |
| 1169 | class Foo { |
| 1170 | public: |
| 1171 | void bar() LOCKS_EXCLUDED(mu_, mu1); |
| 1172 | int foo() SHARED_LOCKS_REQUIRED(mu_) EXCLUSIVE_LOCKS_REQUIRED(mu2); |
| 1173 | |
| 1174 | private: |
| 1175 | int a_ GUARDED_BY(mu_); |
| 1176 | public: |
| 1177 | Mutex mu_ ACQUIRED_AFTER(mu1); |
| 1178 | }; |
| 1179 | |
| 1180 | int Foo::foo() |
| 1181 | { |
| 1182 | int res; |
David Blaikie | e31b8fb | 2012-04-05 00:16:44 +0000 | [diff] [blame] | 1183 | w = 5; |
Caitlin Sadowski | 988b5ae | 2011-09-15 18:07:32 +0000 | [diff] [blame] | 1184 | res = a_ + 5; |
| 1185 | return res; |
| 1186 | } |
| 1187 | |
| 1188 | void Foo::bar() |
| 1189 | { |
| 1190 | int x; |
| 1191 | mu_.Lock(); |
| 1192 | x = foo(); // expected-warning {{calling function 'foo' requires exclusive lock on 'mu2'}} |
| 1193 | a_ = x + 1; |
| 1194 | mu_.Unlock(); |
| 1195 | if (x > 5) { |
| 1196 | mu1.Lock(); |
David Blaikie | e31b8fb | 2012-04-05 00:16:44 +0000 | [diff] [blame] | 1197 | g = 2; |
Caitlin Sadowski | 988b5ae | 2011-09-15 18:07:32 +0000 | [diff] [blame] | 1198 | mu1.Unlock(); |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | void main() |
| 1203 | { |
| 1204 | Foo f1, *f2; |
| 1205 | f1.mu_.Lock(); |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1206 | f1.bar(); // expected-warning {{cannot call function 'bar' while mutex 'f1.mu_' is locked}} |
Caitlin Sadowski | 988b5ae | 2011-09-15 18:07:32 +0000 | [diff] [blame] | 1207 | mu2.Lock(); |
| 1208 | f1.foo(); |
| 1209 | mu2.Unlock(); |
| 1210 | f1.mu_.Unlock(); |
| 1211 | f2->mu_.Lock(); |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1212 | f2->bar(); // expected-warning {{cannot call function 'bar' while mutex 'f2->mu_' is locked}} |
Caitlin Sadowski | 988b5ae | 2011-09-15 18:07:32 +0000 | [diff] [blame] | 1213 | f2->mu_.Unlock(); |
| 1214 | mu2.Lock(); |
David Blaikie | e31b8fb | 2012-04-05 00:16:44 +0000 | [diff] [blame] | 1215 | w = 2; |
Caitlin Sadowski | 988b5ae | 2011-09-15 18:07:32 +0000 | [diff] [blame] | 1216 | mu2.Unlock(); |
| 1217 | } |
| 1218 | } // end namespace thread_annot_lock_13 |
| 1219 | |
| 1220 | namespace thread_annot_lock_18_modified { |
| 1221 | // Modified: Trylocks removed |
| 1222 | // Test the ability to distnguish between the same lock field of |
| 1223 | // different objects of a class. |
| 1224 | class Bar { |
| 1225 | public: |
| 1226 | bool MyLock() EXCLUSIVE_LOCK_FUNCTION(mu1_); |
| 1227 | void MyUnlock() UNLOCK_FUNCTION(mu1_); |
| 1228 | int a_ GUARDED_BY(mu1_); |
| 1229 | |
| 1230 | private: |
| 1231 | Mutex mu1_; |
| 1232 | }; |
| 1233 | |
| 1234 | Bar *b1, *b2; |
| 1235 | |
| 1236 | void func() |
| 1237 | { |
| 1238 | b1->MyLock(); |
| 1239 | b1->a_ = 5; |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1240 | b2->a_ = 3; // expected-warning {{writing variable 'a_' requires locking 'b2->mu1_' exclusively}} |
Caitlin Sadowski | 988b5ae | 2011-09-15 18:07:32 +0000 | [diff] [blame] | 1241 | b2->MyLock(); |
| 1242 | b2->MyUnlock(); |
| 1243 | b1->MyUnlock(); |
| 1244 | } |
| 1245 | } // end namespace thread_annot_lock_18_modified |
| 1246 | |
| 1247 | namespace thread_annot_lock_21 { |
| 1248 | // Test various usage of GUARDED_BY and PT_GUARDED_BY annotations, especially |
| 1249 | // uses in class definitions. |
| 1250 | Mutex mu; |
| 1251 | |
| 1252 | class Bar { |
| 1253 | public: |
| 1254 | int a_ GUARDED_BY(mu1_); |
| 1255 | int b_; |
| 1256 | int *q PT_GUARDED_BY(mu); |
| 1257 | Mutex mu1_ ACQUIRED_AFTER(mu); |
| 1258 | }; |
| 1259 | |
| 1260 | Bar b1, *b3; |
| 1261 | int *p GUARDED_BY(mu) PT_GUARDED_BY(mu); |
| 1262 | |
| 1263 | int res GUARDED_BY(mu) = 5; |
| 1264 | |
| 1265 | int func(int i) |
| 1266 | { |
| 1267 | int x; |
| 1268 | b3->mu1_.Lock(); |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1269 | res = b1.a_ + b3->b_; // expected-warning {{reading variable 'a_' requires locking 'b1.mu1_'}} \ |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 1270 | // expected-warning {{writing variable 'res' requires locking 'mu' exclusively}} |
| 1271 | *p = i; // expected-warning {{reading variable 'p' requires locking 'mu'}} \ |
| 1272 | // expected-warning {{writing the value pointed to by 'p' requires locking 'mu' exclusively}} |
| 1273 | b1.a_ = res + b3->b_; // expected-warning {{reading variable 'res' requires locking 'mu'}} \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1274 | // expected-warning {{writing variable 'a_' requires locking 'b1.mu1_' exclusively}} |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 1275 | b3->b_ = *b1.q; // expected-warning {{reading the value pointed to by 'q' requires locking 'mu'}} |
Caitlin Sadowski | 988b5ae | 2011-09-15 18:07:32 +0000 | [diff] [blame] | 1276 | b3->mu1_.Unlock(); |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 1277 | b1.b_ = res; // expected-warning {{reading variable 'res' requires locking 'mu'}} |
| 1278 | x = res; // expected-warning {{reading variable 'res' requires locking 'mu'}} |
Caitlin Sadowski | 988b5ae | 2011-09-15 18:07:32 +0000 | [diff] [blame] | 1279 | return x; |
| 1280 | } |
| 1281 | } // end namespace thread_annot_lock_21 |
| 1282 | |
| 1283 | namespace thread_annot_lock_35_modified { |
| 1284 | // Test the analyzer's ability to distinguish the lock field of different |
| 1285 | // objects. |
| 1286 | class Foo { |
| 1287 | private: |
| 1288 | Mutex lock_; |
| 1289 | int a_ GUARDED_BY(lock_); |
| 1290 | |
| 1291 | public: |
| 1292 | void Func(Foo* child) LOCKS_EXCLUDED(lock_) { |
| 1293 | Foo *new_foo = new Foo; |
| 1294 | |
| 1295 | lock_.Lock(); |
| 1296 | |
| 1297 | child->Func(new_foo); // There shouldn't be any warning here as the |
| 1298 | // acquired lock is not in child. |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1299 | child->bar(7); // expected-warning {{calling function 'bar' requires exclusive lock on 'child->lock_'}} |
| 1300 | child->a_ = 5; // expected-warning {{writing variable 'a_' requires locking 'child->lock_' exclusively}} |
Caitlin Sadowski | 988b5ae | 2011-09-15 18:07:32 +0000 | [diff] [blame] | 1301 | lock_.Unlock(); |
| 1302 | } |
| 1303 | |
| 1304 | void bar(int y) EXCLUSIVE_LOCKS_REQUIRED(lock_) { |
| 1305 | a_ = y; |
| 1306 | } |
| 1307 | }; |
| 1308 | |
| 1309 | Foo *x; |
| 1310 | |
| 1311 | void main() { |
| 1312 | Foo *child = new Foo; |
| 1313 | x->Func(child); |
| 1314 | } |
| 1315 | } // end namespace thread_annot_lock_35_modified |
| 1316 | |
| 1317 | namespace thread_annot_lock_36_modified { |
| 1318 | // Modified to move the annotations to function defns. |
| 1319 | // Test the analyzer's ability to distinguish the lock field of different |
| 1320 | // objects |
| 1321 | class Foo { |
| 1322 | private: |
| 1323 | Mutex lock_; |
| 1324 | int a_ GUARDED_BY(lock_); |
| 1325 | |
| 1326 | public: |
| 1327 | void Func(Foo* child) LOCKS_EXCLUDED(lock_); |
| 1328 | void bar(int y) EXCLUSIVE_LOCKS_REQUIRED(lock_); |
| 1329 | }; |
| 1330 | |
| 1331 | void Foo::Func(Foo* child) { |
| 1332 | Foo *new_foo = new Foo; |
| 1333 | |
| 1334 | lock_.Lock(); |
| 1335 | |
| 1336 | child->lock_.Lock(); |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1337 | child->Func(new_foo); // expected-warning {{cannot call function 'Func' while mutex 'child->lock_' is locked}} |
Caitlin Sadowski | 988b5ae | 2011-09-15 18:07:32 +0000 | [diff] [blame] | 1338 | child->bar(7); |
| 1339 | child->a_ = 5; |
| 1340 | child->lock_.Unlock(); |
| 1341 | |
| 1342 | lock_.Unlock(); |
| 1343 | } |
| 1344 | |
| 1345 | void Foo::bar(int y) { |
| 1346 | a_ = y; |
| 1347 | } |
| 1348 | |
| 1349 | |
| 1350 | Foo *x; |
| 1351 | |
| 1352 | void main() { |
| 1353 | Foo *child = new Foo; |
| 1354 | x->Func(child); |
| 1355 | } |
| 1356 | } // end namespace thread_annot_lock_36_modified |
| 1357 | |
| 1358 | |
| 1359 | namespace thread_annot_lock_42 { |
| 1360 | // Test support of multiple lock attributes of the same kind on a decl. |
| 1361 | class Foo { |
| 1362 | private: |
| 1363 | Mutex mu1, mu2, mu3; |
| 1364 | int x GUARDED_BY(mu1) GUARDED_BY(mu2); |
| 1365 | int y GUARDED_BY(mu2); |
| 1366 | |
| 1367 | void f2() LOCKS_EXCLUDED(mu1) LOCKS_EXCLUDED(mu2) LOCKS_EXCLUDED(mu3) { |
| 1368 | mu2.Lock(); |
| 1369 | y = 2; |
| 1370 | mu2.Unlock(); |
| 1371 | } |
| 1372 | |
| 1373 | public: |
| 1374 | void f1() EXCLUSIVE_LOCKS_REQUIRED(mu2) EXCLUSIVE_LOCKS_REQUIRED(mu1) { |
| 1375 | x = 5; |
Caitlin Sadowski | 74558b4 | 2011-09-15 18:13:32 +0000 | [diff] [blame] | 1376 | f2(); // expected-warning {{cannot call function 'f2' while mutex 'mu1' is locked}} \ |
| 1377 | // expected-warning {{cannot call function 'f2' while mutex 'mu2' is locked}} |
Caitlin Sadowski | 988b5ae | 2011-09-15 18:07:32 +0000 | [diff] [blame] | 1378 | } |
| 1379 | }; |
| 1380 | |
| 1381 | Foo *foo; |
| 1382 | |
| 1383 | void func() |
| 1384 | { |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1385 | foo->f1(); // expected-warning {{calling function 'f1' requires exclusive lock on 'foo->mu2'}} \ |
| 1386 | // expected-warning {{calling function 'f1' requires exclusive lock on 'foo->mu1'}} |
Caitlin Sadowski | 988b5ae | 2011-09-15 18:07:32 +0000 | [diff] [blame] | 1387 | } |
| 1388 | } // end namespace thread_annot_lock_42 |
| 1389 | |
| 1390 | namespace thread_annot_lock_46 { |
| 1391 | // Test the support for annotations on virtual functions. |
| 1392 | class Base { |
| 1393 | public: |
| 1394 | virtual void func1() EXCLUSIVE_LOCKS_REQUIRED(mu_); |
| 1395 | virtual void func2() LOCKS_EXCLUDED(mu_); |
| 1396 | Mutex mu_; |
| 1397 | }; |
| 1398 | |
| 1399 | class Child : public Base { |
| 1400 | public: |
| 1401 | virtual void func1() EXCLUSIVE_LOCKS_REQUIRED(mu_); |
| 1402 | virtual void func2() LOCKS_EXCLUDED(mu_); |
| 1403 | }; |
| 1404 | |
| 1405 | void main() { |
| 1406 | Child *c; |
| 1407 | Base *b = c; |
| 1408 | |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1409 | b->func1(); // expected-warning {{calling function 'func1' requires exclusive lock on 'b->mu_'}} |
Caitlin Sadowski | 988b5ae | 2011-09-15 18:07:32 +0000 | [diff] [blame] | 1410 | b->mu_.Lock(); |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1411 | b->func2(); // expected-warning {{cannot call function 'func2' while mutex 'b->mu_' is locked}} |
Caitlin Sadowski | 988b5ae | 2011-09-15 18:07:32 +0000 | [diff] [blame] | 1412 | b->mu_.Unlock(); |
| 1413 | |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1414 | c->func1(); // expected-warning {{calling function 'func1' requires exclusive lock on 'c->mu_'}} |
Caitlin Sadowski | 988b5ae | 2011-09-15 18:07:32 +0000 | [diff] [blame] | 1415 | c->mu_.Lock(); |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1416 | c->func2(); // expected-warning {{cannot call function 'func2' while mutex 'c->mu_' is locked}} |
Caitlin Sadowski | 988b5ae | 2011-09-15 18:07:32 +0000 | [diff] [blame] | 1417 | c->mu_.Unlock(); |
| 1418 | } |
| 1419 | } // end namespace thread_annot_lock_46 |
| 1420 | |
| 1421 | namespace thread_annot_lock_67_modified { |
| 1422 | // Modified: attributes on definitions moved to declarations |
| 1423 | // Test annotations on out-of-line definitions of member functions where the |
| 1424 | // annotations refer to locks that are also data members in the class. |
| 1425 | Mutex mu; |
| 1426 | Mutex mu3; |
| 1427 | |
| 1428 | class Foo { |
| 1429 | public: |
| 1430 | int method1(int i) SHARED_LOCKS_REQUIRED(mu1, mu, mu2, mu3); |
| 1431 | int data GUARDED_BY(mu1); |
| 1432 | Mutex *mu1; |
| 1433 | Mutex *mu2; |
| 1434 | }; |
| 1435 | |
| 1436 | int Foo::method1(int i) { |
| 1437 | return data + i; |
| 1438 | } |
| 1439 | |
| 1440 | void main() |
| 1441 | { |
| 1442 | Foo a; |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1443 | a.method1(1); // expected-warning {{calling function 'method1' requires shared lock on 'a.mu1'}} \ |
Caitlin Sadowski | 988b5ae | 2011-09-15 18:07:32 +0000 | [diff] [blame] | 1444 | // expected-warning {{calling function 'method1' requires shared lock on 'mu'}} \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1445 | // expected-warning {{calling function 'method1' requires shared lock on 'a.mu2'}} \ |
Caitlin Sadowski | 988b5ae | 2011-09-15 18:07:32 +0000 | [diff] [blame] | 1446 | // expected-warning {{calling function 'method1' requires shared lock on 'mu3'}} |
| 1447 | } |
| 1448 | } // end namespace thread_annot_lock_67_modified |
| 1449 | |
| 1450 | |
DeLesley Hutchins | 8121639 | 2011-10-17 21:38:02 +0000 | [diff] [blame] | 1451 | namespace substitution_test { |
| 1452 | class MyData { |
| 1453 | public: |
| 1454 | Mutex mu; |
| 1455 | |
| 1456 | void lockData() __attribute__((exclusive_lock_function(mu))) { } |
| 1457 | void unlockData() __attribute__((unlock_function(mu))) { } |
| 1458 | |
| 1459 | void doSomething() __attribute__((exclusive_locks_required(mu))) { } |
| 1460 | }; |
| 1461 | |
| 1462 | |
| 1463 | class DataLocker { |
| 1464 | public: |
| 1465 | void lockData (MyData *d) __attribute__((exclusive_lock_function(d->mu))) { } |
| 1466 | void unlockData(MyData *d) __attribute__((unlock_function(d->mu))) { } |
| 1467 | }; |
| 1468 | |
| 1469 | |
| 1470 | class Foo { |
| 1471 | public: |
| 1472 | void foo(MyData* d) __attribute__((exclusive_locks_required(d->mu))) { } |
| 1473 | |
| 1474 | void bar1(MyData* d) { |
| 1475 | d->lockData(); |
| 1476 | foo(d); |
| 1477 | d->unlockData(); |
| 1478 | } |
| 1479 | |
| 1480 | void bar2(MyData* d) { |
| 1481 | DataLocker dlr; |
| 1482 | dlr.lockData(d); |
| 1483 | foo(d); |
| 1484 | dlr.unlockData(d); |
| 1485 | } |
| 1486 | |
| 1487 | void bar3(MyData* d1, MyData* d2) { |
| 1488 | DataLocker dlr; |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1489 | dlr.lockData(d1); // expected-note {{mutex acquired here}} |
DeLesley Hutchins | 8121639 | 2011-10-17 21:38:02 +0000 | [diff] [blame] | 1490 | dlr.unlockData(d2); // \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1491 | // expected-warning {{unlocking 'd2->mu' that was not locked}} |
| 1492 | } // expected-warning {{mutex 'd1->mu' is still locked at the end of function}} |
DeLesley Hutchins | 8121639 | 2011-10-17 21:38:02 +0000 | [diff] [blame] | 1493 | |
| 1494 | void bar4(MyData* d1, MyData* d2) { |
| 1495 | DataLocker dlr; |
| 1496 | dlr.lockData(d1); |
| 1497 | foo(d2); // \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1498 | // expected-warning {{calling function 'foo' requires exclusive lock on 'd2->mu'}} |
DeLesley Hutchins | 8121639 | 2011-10-17 21:38:02 +0000 | [diff] [blame] | 1499 | dlr.unlockData(d1); |
| 1500 | } |
| 1501 | }; |
| 1502 | } // end namespace substituation_test |
| 1503 | |
DeLesley Hutchins | e0eaa85 | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 1504 | |
DeLesley Hutchins | f1ac637 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 1505 | |
DeLesley Hutchins | e0eaa85 | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 1506 | namespace constructor_destructor_tests { |
| 1507 | Mutex fooMu; |
| 1508 | int myVar GUARDED_BY(fooMu); |
| 1509 | |
| 1510 | class Foo { |
| 1511 | public: |
| 1512 | Foo() __attribute__((exclusive_lock_function(fooMu))) { } |
| 1513 | ~Foo() __attribute__((unlock_function(fooMu))) { } |
| 1514 | }; |
| 1515 | |
| 1516 | void fooTest() { |
DeLesley Hutchins | 6db51f7 | 2011-10-21 20:51:27 +0000 | [diff] [blame] | 1517 | Foo foo; |
DeLesley Hutchins | e0eaa85 | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 1518 | myVar = 0; |
| 1519 | } |
| 1520 | } |
| 1521 | |
DeLesley Hutchins | f1ac637 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 1522 | |
| 1523 | namespace invalid_lock_expression_test { |
| 1524 | |
| 1525 | class LOCKABLE MyLockable { |
| 1526 | public: |
| 1527 | MyLockable() __attribute__((exclusive_lock_function)) { } |
DeLesley Hutchins | 6db51f7 | 2011-10-21 20:51:27 +0000 | [diff] [blame] | 1528 | ~MyLockable() { } |
DeLesley Hutchins | f1ac637 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 1529 | }; |
| 1530 | |
| 1531 | // create an empty lock expression |
| 1532 | void foo() { |
| 1533 | MyLockable lock; // \ |
| 1534 | // expected-warning {{cannot resolve lock expression}} |
| 1535 | } |
| 1536 | |
| 1537 | } // end namespace invalid_lock_expression_test |
| 1538 | |
Richard Smith | 97f9fe0 | 2011-10-25 00:41:24 +0000 | [diff] [blame] | 1539 | namespace template_member_test { |
DeLesley Hutchins | f1ac637 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 1540 | |
Richard Smith | 97f9fe0 | 2011-10-25 00:41:24 +0000 | [diff] [blame] | 1541 | struct S { int n; }; |
| 1542 | struct T { |
| 1543 | Mutex m; |
| 1544 | S *s GUARDED_BY(this->m); |
| 1545 | }; |
Richard Smith | a01c711 | 2011-10-25 06:33:21 +0000 | [diff] [blame] | 1546 | Mutex m; |
| 1547 | struct U { |
| 1548 | union { |
| 1549 | int n; |
| 1550 | }; |
| 1551 | } *u GUARDED_BY(m); |
Richard Smith | 97f9fe0 | 2011-10-25 00:41:24 +0000 | [diff] [blame] | 1552 | |
| 1553 | template<typename U> |
| 1554 | struct IndirectLock { |
| 1555 | int DoNaughtyThings(T *t) { |
Richard Smith | a01c711 | 2011-10-25 06:33:21 +0000 | [diff] [blame] | 1556 | u->n = 0; // expected-warning {{reading variable 'u' requires locking 'm'}} |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1557 | return t->s->n; // expected-warning {{reading variable 's' requires locking 't->m'}} |
Richard Smith | 97f9fe0 | 2011-10-25 00:41:24 +0000 | [diff] [blame] | 1558 | } |
| 1559 | }; |
| 1560 | |
Richard Smith | f11e923 | 2011-10-25 01:05:41 +0000 | [diff] [blame] | 1561 | template struct IndirectLock<int>; // expected-note {{here}} |
Richard Smith | 97f9fe0 | 2011-10-25 00:41:24 +0000 | [diff] [blame] | 1562 | |
Richard Smith | 601d2ee | 2011-10-26 06:15:36 +0000 | [diff] [blame] | 1563 | struct V { |
| 1564 | void f(int); |
| 1565 | void f(double); |
| 1566 | |
| 1567 | Mutex m; |
| 1568 | V *p GUARDED_BY(this->m); |
| 1569 | }; |
| 1570 | template<typename U> struct W { |
| 1571 | V v; |
| 1572 | void f(U u) { |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1573 | v.p->f(u); // expected-warning {{reading variable 'p' requires locking 'v.m'}} |
Richard Smith | 601d2ee | 2011-10-26 06:15:36 +0000 | [diff] [blame] | 1574 | } |
| 1575 | }; |
| 1576 | template struct W<int>; // expected-note {{here}} |
| 1577 | |
Richard Smith | 97f9fe0 | 2011-10-25 00:41:24 +0000 | [diff] [blame] | 1578 | } |
DeLesley Hutchins | 1fa3c06 | 2011-12-08 20:23:06 +0000 | [diff] [blame] | 1579 | |
| 1580 | namespace test_scoped_lockable { |
| 1581 | |
| 1582 | struct TestScopedLockable { |
| 1583 | Mutex mu1; |
| 1584 | Mutex mu2; |
| 1585 | int a __attribute__((guarded_by(mu1))); |
| 1586 | int b __attribute__((guarded_by(mu2))); |
| 1587 | |
| 1588 | bool getBool(); |
| 1589 | |
| 1590 | void foo1() { |
| 1591 | MutexLock mulock(&mu1); |
| 1592 | a = 5; |
| 1593 | } |
| 1594 | |
| 1595 | void foo2() { |
| 1596 | ReaderMutexLock mulock1(&mu1); |
| 1597 | if (getBool()) { |
| 1598 | MutexLock mulock2a(&mu2); |
| 1599 | b = a + 1; |
| 1600 | } |
| 1601 | else { |
| 1602 | MutexLock mulock2b(&mu2); |
| 1603 | b = a + 2; |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | void foo3() { |
| 1608 | MutexLock mulock_a(&mu1); |
| 1609 | MutexLock mulock_b(&mu1); // \ |
| 1610 | // expected-warning {{locking 'mu1' that is already locked}} |
DeLesley Hutchins | c99a5d8 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 1611 | } |
DeLesley Hutchins | 1fa3c06 | 2011-12-08 20:23:06 +0000 | [diff] [blame] | 1612 | |
| 1613 | void foo4() { |
| 1614 | MutexLock mulock1(&mu1), mulock2(&mu2); |
| 1615 | a = b+1; |
| 1616 | b = a+1; |
| 1617 | } |
| 1618 | }; |
| 1619 | |
| 1620 | } // end namespace test_scoped_lockable |
| 1621 | |
| 1622 | |
DeLesley Hutchins | df49782 | 2011-12-29 00:56:48 +0000 | [diff] [blame] | 1623 | namespace FunctionAttrTest { |
| 1624 | |
| 1625 | class Foo { |
| 1626 | public: |
| 1627 | Mutex mu_; |
| 1628 | int a GUARDED_BY(mu_); |
| 1629 | }; |
| 1630 | |
| 1631 | Foo fooObj; |
| 1632 | |
| 1633 | void foo() EXCLUSIVE_LOCKS_REQUIRED(fooObj.mu_); |
| 1634 | |
| 1635 | void bar() { |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1636 | foo(); // expected-warning {{calling function 'foo' requires exclusive lock on 'fooObj.mu_'}} |
DeLesley Hutchins | df49782 | 2011-12-29 00:56:48 +0000 | [diff] [blame] | 1637 | fooObj.mu_.Lock(); |
| 1638 | foo(); |
| 1639 | fooObj.mu_.Unlock(); |
| 1640 | } |
| 1641 | |
| 1642 | }; // end namespace FunctionAttrTest |
| 1643 | |
| 1644 | |
DeLesley Hutchins | b4fa418 | 2012-01-06 19:16:50 +0000 | [diff] [blame] | 1645 | struct TestTryLock { |
| 1646 | Mutex mu; |
| 1647 | int a GUARDED_BY(mu); |
| 1648 | bool cond; |
| 1649 | |
| 1650 | void foo1() { |
| 1651 | if (mu.TryLock()) { |
| 1652 | a = 1; |
| 1653 | mu.Unlock(); |
| 1654 | } |
| 1655 | } |
| 1656 | |
| 1657 | void foo2() { |
| 1658 | if (!mu.TryLock()) return; |
| 1659 | a = 2; |
| 1660 | mu.Unlock(); |
| 1661 | } |
| 1662 | |
| 1663 | void foo3() { |
| 1664 | bool b = mu.TryLock(); |
| 1665 | if (b) { |
| 1666 | a = 3; |
| 1667 | mu.Unlock(); |
| 1668 | } |
| 1669 | } |
| 1670 | |
| 1671 | void foo4() { |
| 1672 | bool b = mu.TryLock(); |
| 1673 | if (!b) return; |
| 1674 | a = 4; |
| 1675 | mu.Unlock(); |
| 1676 | } |
| 1677 | |
| 1678 | void foo5() { |
| 1679 | while (mu.TryLock()) { |
| 1680 | a = a + 1; |
| 1681 | mu.Unlock(); |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | void foo6() { |
| 1686 | bool b = mu.TryLock(); |
| 1687 | b = !b; |
| 1688 | if (b) return; |
| 1689 | a = 6; |
| 1690 | mu.Unlock(); |
| 1691 | } |
| 1692 | |
| 1693 | void foo7() { |
| 1694 | bool b1 = mu.TryLock(); |
| 1695 | bool b2 = !b1; |
| 1696 | bool b3 = !b2; |
| 1697 | if (b3) { |
| 1698 | a = 7; |
| 1699 | mu.Unlock(); |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | // Test use-def chains: join points |
| 1704 | void foo8() { |
| 1705 | bool b = mu.TryLock(); |
| 1706 | bool b2 = b; |
| 1707 | if (cond) |
| 1708 | b = true; |
Sylvestre Ledru | 43e3dee | 2012-07-31 06:56:50 +0000 | [diff] [blame] | 1709 | if (b) { // b should be unknown at this point, because of the join point |
DeLesley Hutchins | b4fa418 | 2012-01-06 19:16:50 +0000 | [diff] [blame] | 1710 | a = 8; // expected-warning {{writing variable 'a' requires locking 'mu' exclusively}} |
| 1711 | } |
| 1712 | if (b2) { // b2 should be known at this point. |
| 1713 | a = 8; |
| 1714 | mu.Unlock(); |
| 1715 | } |
| 1716 | } |
| 1717 | |
| 1718 | // Test use-def-chains: back edges |
| 1719 | void foo9() { |
| 1720 | bool b = mu.TryLock(); |
| 1721 | |
| 1722 | for (int i = 0; i < 10; ++i); |
| 1723 | |
| 1724 | if (b) { // b is still known, because the loop doesn't alter it |
| 1725 | a = 9; |
| 1726 | mu.Unlock(); |
| 1727 | } |
| 1728 | } |
| 1729 | |
| 1730 | // Test use-def chains: back edges |
| 1731 | void foo10() { |
| 1732 | bool b = mu.TryLock(); |
| 1733 | |
| 1734 | while (cond) { |
| 1735 | if (b) { // b should be uknown at this point b/c of the loop |
| 1736 | a = 10; // expected-warning {{writing variable 'a' requires locking 'mu' exclusively}} |
| 1737 | } |
| 1738 | b = !b; |
| 1739 | } |
| 1740 | } |
| 1741 | }; // end TestTrylock |
| 1742 | |
| 1743 | |
DeLesley Hutchins | 7b9ff0c | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1744 | namespace TestTemplateAttributeInstantiation { |
| 1745 | |
| 1746 | class Foo1 { |
| 1747 | public: |
| 1748 | Mutex mu_; |
| 1749 | int a GUARDED_BY(mu_); |
| 1750 | }; |
| 1751 | |
| 1752 | class Foo2 { |
| 1753 | public: |
| 1754 | int a GUARDED_BY(mu_); |
| 1755 | Mutex mu_; |
| 1756 | }; |
| 1757 | |
| 1758 | |
| 1759 | class Bar { |
| 1760 | public: |
| 1761 | // Test non-dependent expressions in attributes on template functions |
| 1762 | template <class T> |
| 1763 | void barND(Foo1 *foo, T *fooT) EXCLUSIVE_LOCKS_REQUIRED(foo->mu_) { |
| 1764 | foo->a = 0; |
| 1765 | } |
| 1766 | |
| 1767 | // Test dependent expressions in attributes on template functions |
| 1768 | template <class T> |
| 1769 | void barD(Foo1 *foo, T *fooT) EXCLUSIVE_LOCKS_REQUIRED(fooT->mu_) { |
| 1770 | fooT->a = 0; |
| 1771 | } |
| 1772 | }; |
| 1773 | |
| 1774 | |
| 1775 | template <class T> |
| 1776 | class BarT { |
| 1777 | public: |
| 1778 | Foo1 fooBase; |
| 1779 | T fooBaseT; |
| 1780 | |
| 1781 | // Test non-dependent expression in ordinary method on template class |
| 1782 | void barND() EXCLUSIVE_LOCKS_REQUIRED(fooBase.mu_) { |
| 1783 | fooBase.a = 0; |
| 1784 | } |
| 1785 | |
| 1786 | // Test dependent expressions in ordinary methods on template class |
| 1787 | void barD() EXCLUSIVE_LOCKS_REQUIRED(fooBaseT.mu_) { |
| 1788 | fooBaseT.a = 0; |
| 1789 | } |
| 1790 | |
| 1791 | // Test dependent expressions in template method in template class |
| 1792 | template <class T2> |
| 1793 | void barTD(T2 *fooT) EXCLUSIVE_LOCKS_REQUIRED(fooBaseT.mu_, fooT->mu_) { |
| 1794 | fooBaseT.a = 0; |
| 1795 | fooT->a = 0; |
| 1796 | } |
| 1797 | }; |
| 1798 | |
| 1799 | template <class T> |
| 1800 | class Cell { |
| 1801 | public: |
| 1802 | Mutex mu_; |
| 1803 | // Test dependent guarded_by |
| 1804 | T data GUARDED_BY(mu_); |
| 1805 | |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 1806 | void fooEx() EXCLUSIVE_LOCKS_REQUIRED(mu_) { |
DeLesley Hutchins | 7b9ff0c | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1807 | data = 0; |
DeLesley Hutchins | 7b9ff0c | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1808 | } |
DeLesley Hutchins | 7b9ff0c | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1809 | |
| 1810 | void foo() { |
| 1811 | mu_.Lock(); |
| 1812 | data = 0; |
| 1813 | mu_.Unlock(); |
| 1814 | } |
DeLesley Hutchins | 7b9ff0c | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1815 | }; |
| 1816 | |
| 1817 | void test() { |
| 1818 | Bar b; |
| 1819 | BarT<Foo2> bt; |
| 1820 | Foo1 f1; |
| 1821 | Foo2 f2; |
| 1822 | |
| 1823 | f1.mu_.Lock(); |
| 1824 | f2.mu_.Lock(); |
| 1825 | bt.fooBase.mu_.Lock(); |
| 1826 | bt.fooBaseT.mu_.Lock(); |
| 1827 | |
| 1828 | b.barND(&f1, &f2); |
| 1829 | b.barD(&f1, &f2); |
| 1830 | bt.barND(); |
| 1831 | bt.barD(); |
| 1832 | bt.barTD(&f2); |
| 1833 | |
| 1834 | f1.mu_.Unlock(); |
| 1835 | bt.barTD(&f1); // \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1836 | // expected-warning {{calling function 'barTD' requires exclusive lock on 'f1.mu_'}} |
DeLesley Hutchins | 7b9ff0c | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1837 | |
| 1838 | bt.fooBase.mu_.Unlock(); |
| 1839 | bt.fooBaseT.mu_.Unlock(); |
| 1840 | f2.mu_.Unlock(); |
| 1841 | |
| 1842 | Cell<int> cell; |
| 1843 | cell.data = 0; // \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1844 | // expected-warning {{writing variable 'data' requires locking 'cell.mu_' exclusively}} |
DeLesley Hutchins | 7b9ff0c | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1845 | cell.foo(); |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 1846 | cell.mu_.Lock(); |
| 1847 | cell.fooEx(); |
| 1848 | cell.mu_.Unlock(); |
| 1849 | } |
DeLesley Hutchins | 7b9ff0c | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1850 | |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 1851 | |
| 1852 | template <class T> |
| 1853 | class CellDelayed { |
| 1854 | public: |
| 1855 | // Test dependent guarded_by |
| 1856 | T data GUARDED_BY(mu_); |
DeLesley Hutchins | dd5756c | 2012-02-16 17:30:51 +0000 | [diff] [blame] | 1857 | static T static_data GUARDED_BY(static_mu_); |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 1858 | |
| 1859 | void fooEx(CellDelayed<T> *other) EXCLUSIVE_LOCKS_REQUIRED(mu_, other->mu_) { |
| 1860 | this->data = other->data; |
| 1861 | } |
| 1862 | |
| 1863 | template <class T2> |
| 1864 | void fooExT(CellDelayed<T2> *otherT) EXCLUSIVE_LOCKS_REQUIRED(mu_, otherT->mu_) { |
| 1865 | this->data = otherT->data; |
| 1866 | } |
| 1867 | |
| 1868 | void foo() { |
| 1869 | mu_.Lock(); |
| 1870 | data = 0; |
| 1871 | mu_.Unlock(); |
| 1872 | } |
| 1873 | |
| 1874 | Mutex mu_; |
DeLesley Hutchins | dd5756c | 2012-02-16 17:30:51 +0000 | [diff] [blame] | 1875 | static Mutex static_mu_; |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 1876 | }; |
| 1877 | |
| 1878 | void testDelayed() { |
| 1879 | CellDelayed<int> celld; |
| 1880 | CellDelayed<int> celld2; |
| 1881 | celld.foo(); |
| 1882 | celld.mu_.Lock(); |
| 1883 | celld2.mu_.Lock(); |
| 1884 | |
| 1885 | celld.fooEx(&celld2); |
| 1886 | celld.fooExT(&celld2); |
| 1887 | |
| 1888 | celld2.mu_.Unlock(); |
| 1889 | celld.mu_.Unlock(); |
DeLesley Hutchins | 7b9ff0c | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | }; // end namespace TestTemplateAttributeInstantiation |
| 1893 | |
| 1894 | |
DeLesley Hutchins | e03b2b3 | 2012-01-20 23:24:41 +0000 | [diff] [blame] | 1895 | namespace FunctionDeclDefTest { |
| 1896 | |
| 1897 | class Foo { |
| 1898 | public: |
| 1899 | Mutex mu_; |
| 1900 | int a GUARDED_BY(mu_); |
| 1901 | |
| 1902 | virtual void foo1(Foo *f_declared) EXCLUSIVE_LOCKS_REQUIRED(f_declared->mu_); |
| 1903 | }; |
| 1904 | |
| 1905 | // EXCLUSIVE_LOCKS_REQUIRED should be applied, and rewritten to f_defined->mu_ |
| 1906 | void Foo::foo1(Foo *f_defined) { |
| 1907 | f_defined->a = 0; |
| 1908 | }; |
| 1909 | |
| 1910 | void test() { |
| 1911 | Foo myfoo; |
| 1912 | myfoo.foo1(&myfoo); // \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1913 | // expected-warning {{calling function 'foo1' requires exclusive lock on 'myfoo.mu_'}} |
DeLesley Hutchins | e03b2b3 | 2012-01-20 23:24:41 +0000 | [diff] [blame] | 1914 | myfoo.mu_.Lock(); |
| 1915 | myfoo.foo1(&myfoo); |
| 1916 | myfoo.mu_.Unlock(); |
| 1917 | } |
| 1918 | |
| 1919 | }; |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1920 | |
| 1921 | namespace GoingNative { |
| 1922 | |
| 1923 | struct __attribute__((lockable)) mutex { |
| 1924 | void lock() __attribute__((exclusive_lock_function)); |
| 1925 | void unlock() __attribute__((unlock_function)); |
| 1926 | // ... |
| 1927 | }; |
| 1928 | bool foo(); |
| 1929 | bool bar(); |
| 1930 | mutex m; |
| 1931 | void test() { |
| 1932 | m.lock(); |
| 1933 | while (foo()) { |
| 1934 | m.unlock(); |
| 1935 | // ... |
| 1936 | if (bar()) { |
| 1937 | // ... |
| 1938 | if (foo()) |
| 1939 | continue; // expected-warning {{expecting mutex 'm' to be locked at start of each loop}} |
| 1940 | //... |
| 1941 | } |
| 1942 | // ... |
| 1943 | m.lock(); // expected-note {{mutex acquired here}} |
| 1944 | } |
| 1945 | m.unlock(); |
| 1946 | } |
| 1947 | |
| 1948 | } |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1949 | |
| 1950 | |
| 1951 | |
| 1952 | namespace FunctionDefinitionTest { |
| 1953 | |
| 1954 | class Foo { |
| 1955 | public: |
| 1956 | void foo1(); |
| 1957 | void foo2(); |
| 1958 | void foo3(Foo *other); |
| 1959 | |
| 1960 | template<class T> |
| 1961 | void fooT1(const T& dummy1); |
| 1962 | |
| 1963 | template<class T> |
| 1964 | void fooT2(const T& dummy2) EXCLUSIVE_LOCKS_REQUIRED(mu_); |
| 1965 | |
| 1966 | Mutex mu_; |
| 1967 | int a GUARDED_BY(mu_); |
| 1968 | }; |
| 1969 | |
| 1970 | template<class T> |
| 1971 | class FooT { |
| 1972 | public: |
| 1973 | void foo(); |
| 1974 | |
| 1975 | Mutex mu_; |
| 1976 | T a GUARDED_BY(mu_); |
| 1977 | }; |
| 1978 | |
| 1979 | |
| 1980 | void Foo::foo1() NO_THREAD_SAFETY_ANALYSIS { |
| 1981 | a = 1; |
| 1982 | } |
| 1983 | |
| 1984 | void Foo::foo2() EXCLUSIVE_LOCKS_REQUIRED(mu_) { |
| 1985 | a = 2; |
| 1986 | } |
| 1987 | |
| 1988 | void Foo::foo3(Foo *other) EXCLUSIVE_LOCKS_REQUIRED(other->mu_) { |
| 1989 | other->a = 3; |
| 1990 | } |
| 1991 | |
| 1992 | template<class T> |
| 1993 | void Foo::fooT1(const T& dummy1) EXCLUSIVE_LOCKS_REQUIRED(mu_) { |
| 1994 | a = dummy1; |
| 1995 | } |
| 1996 | |
| 1997 | /* TODO -- uncomment with template instantiation of attributes. |
| 1998 | template<class T> |
| 1999 | void Foo::fooT2(const T& dummy2) { |
| 2000 | a = dummy2; |
| 2001 | } |
| 2002 | */ |
| 2003 | |
| 2004 | void fooF1(Foo *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_) { |
| 2005 | f->a = 1; |
| 2006 | } |
| 2007 | |
| 2008 | void fooF2(Foo *f); |
| 2009 | void fooF2(Foo *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_) { |
| 2010 | f->a = 2; |
| 2011 | } |
| 2012 | |
| 2013 | void fooF3(Foo *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_); |
| 2014 | void fooF3(Foo *f) { |
| 2015 | f->a = 3; |
| 2016 | } |
| 2017 | |
| 2018 | template<class T> |
| 2019 | void FooT<T>::foo() EXCLUSIVE_LOCKS_REQUIRED(mu_) { |
| 2020 | a = 0; |
| 2021 | } |
| 2022 | |
| 2023 | void test() { |
| 2024 | int dummy = 0; |
| 2025 | Foo myFoo; |
| 2026 | |
| 2027 | myFoo.foo2(); // \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2028 | // expected-warning {{calling function 'foo2' requires exclusive lock on 'myFoo.mu_'}} |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 2029 | myFoo.foo3(&myFoo); // \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2030 | // expected-warning {{calling function 'foo3' requires exclusive lock on 'myFoo.mu_'}} |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 2031 | myFoo.fooT1(dummy); // \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2032 | // expected-warning {{calling function 'fooT1' requires exclusive lock on 'myFoo.mu_'}} |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 2033 | |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2034 | myFoo.fooT2(dummy); // \ |
| 2035 | // expected-warning {{calling function 'fooT2' requires exclusive lock on 'myFoo.mu_'}} |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 2036 | |
| 2037 | fooF1(&myFoo); // \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2038 | // expected-warning {{calling function 'fooF1' requires exclusive lock on 'myFoo.mu_'}} |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 2039 | fooF2(&myFoo); // \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2040 | // expected-warning {{calling function 'fooF2' requires exclusive lock on 'myFoo.mu_'}} |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 2041 | fooF3(&myFoo); // \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2042 | // expected-warning {{calling function 'fooF3' requires exclusive lock on 'myFoo.mu_'}} |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 2043 | |
| 2044 | myFoo.mu_.Lock(); |
| 2045 | myFoo.foo2(); |
| 2046 | myFoo.foo3(&myFoo); |
| 2047 | myFoo.fooT1(dummy); |
| 2048 | |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2049 | myFoo.fooT2(dummy); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 2050 | |
| 2051 | fooF1(&myFoo); |
| 2052 | fooF2(&myFoo); |
| 2053 | fooF3(&myFoo); |
| 2054 | myFoo.mu_.Unlock(); |
| 2055 | |
| 2056 | FooT<int> myFooT; |
| 2057 | myFooT.foo(); // \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2058 | // expected-warning {{calling function 'foo' requires exclusive lock on 'myFooT.mu_'}} |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
DeLesley Hutchins | 4bda3ec | 2012-02-16 17:03:24 +0000 | [diff] [blame] | 2061 | } // end namespace FunctionDefinitionTest |
| 2062 | |
| 2063 | |
| 2064 | namespace SelfLockingTest { |
| 2065 | |
| 2066 | class LOCKABLE MyLock { |
| 2067 | public: |
| 2068 | int foo GUARDED_BY(this); |
| 2069 | |
| 2070 | void lock() EXCLUSIVE_LOCK_FUNCTION(); |
| 2071 | void unlock() UNLOCK_FUNCTION(); |
| 2072 | |
| 2073 | void doSomething() { |
| 2074 | this->lock(); // allow 'this' as a lock expression |
| 2075 | foo = 0; |
| 2076 | doSomethingElse(); |
| 2077 | this->unlock(); |
| 2078 | } |
| 2079 | |
| 2080 | void doSomethingElse() EXCLUSIVE_LOCKS_REQUIRED(this) { |
| 2081 | foo = 1; |
| 2082 | }; |
| 2083 | |
| 2084 | void test() { |
| 2085 | foo = 2; // \ |
| 2086 | // expected-warning {{writing variable 'foo' requires locking 'this' exclusively}} |
| 2087 | } |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 2088 | }; |
| 2089 | |
DeLesley Hutchins | 2f13bec | 2012-02-16 17:13:43 +0000 | [diff] [blame] | 2090 | |
| 2091 | class LOCKABLE MyLock2 { |
| 2092 | public: |
| 2093 | Mutex mu_; |
| 2094 | int foo GUARDED_BY(this); |
| 2095 | |
| 2096 | // don't check inside lock and unlock functions |
| 2097 | void lock() EXCLUSIVE_LOCK_FUNCTION() { mu_.Lock(); } |
| 2098 | void unlock() UNLOCK_FUNCTION() { mu_.Unlock(); } |
| 2099 | |
| 2100 | // don't check inside constructors and destructors |
| 2101 | MyLock2() { foo = 1; } |
| 2102 | ~MyLock2() { foo = 0; } |
| 2103 | }; |
| 2104 | |
| 2105 | |
DeLesley Hutchins | 4bda3ec | 2012-02-16 17:03:24 +0000 | [diff] [blame] | 2106 | } // end namespace SelfLockingTest |
| 2107 | |
| 2108 | |
DeLesley Hutchins | d08d599 | 2012-02-25 00:11:55 +0000 | [diff] [blame] | 2109 | namespace InvalidNonstatic { |
| 2110 | |
| 2111 | // Forward decl here causes bogus "invalid use of non-static data member" |
| 2112 | // on reference to mutex_ in guarded_by attribute. |
| 2113 | class Foo; |
| 2114 | |
| 2115 | class Foo { |
| 2116 | Mutex* mutex_; |
| 2117 | |
| 2118 | int foo __attribute__((guarded_by(mutex_))); |
| 2119 | }; |
| 2120 | |
| 2121 | } // end namespace InvalidNonStatic |
| 2122 | |
DeLesley Hutchins | 2a35be8 | 2012-03-02 22:02:58 +0000 | [diff] [blame] | 2123 | |
| 2124 | namespace NoReturnTest { |
| 2125 | |
| 2126 | bool condition(); |
| 2127 | void fatal() __attribute__((noreturn)); |
| 2128 | |
| 2129 | Mutex mu_; |
| 2130 | |
| 2131 | void test1() { |
| 2132 | MutexLock lock(&mu_); |
| 2133 | if (condition()) { |
| 2134 | fatal(); |
| 2135 | return; |
| 2136 | } |
| 2137 | } |
| 2138 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2139 | } // end namespace NoReturnTest |
| 2140 | |
| 2141 | |
| 2142 | namespace TestMultiDecl { |
| 2143 | |
| 2144 | class Foo { |
| 2145 | public: |
| 2146 | int GUARDED_BY(mu_) a; |
| 2147 | int GUARDED_BY(mu_) b, c; |
| 2148 | |
| 2149 | void foo() { |
| 2150 | a = 0; // \ |
| 2151 | // expected-warning {{writing variable 'a' requires locking 'mu_' exclusively}} |
| 2152 | b = 0; // \ |
| 2153 | // expected-warning {{writing variable 'b' requires locking 'mu_' exclusively}} |
| 2154 | c = 0; // \ |
| 2155 | // expected-warning {{writing variable 'c' requires locking 'mu_' exclusively}} |
| 2156 | } |
| 2157 | |
| 2158 | private: |
| 2159 | Mutex mu_; |
DeLesley Hutchins | 2a35be8 | 2012-03-02 22:02:58 +0000 | [diff] [blame] | 2160 | }; |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2161 | |
| 2162 | } // end namespace TestMultiDecl |
| 2163 | |
DeLesley Hutchins | 7ec419a | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 2164 | |
| 2165 | namespace WarnNoDecl { |
| 2166 | |
| 2167 | class Foo { |
| 2168 | void foo(int a); __attribute__(( // \ |
| 2169 | // expected-warning {{declaration does not declare anything}} |
| 2170 | exclusive_locks_required(a))); // \ |
| 2171 | // expected-warning {{attribute exclusive_locks_required ignored}} |
| 2172 | }; |
| 2173 | |
| 2174 | } // end namespace WarnNoDecl |
| 2175 | |
| 2176 | |
DeLesley Hutchins | 0d95dfc | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 2177 | |
| 2178 | namespace MoreLockExpressions { |
| 2179 | |
| 2180 | class Foo { |
| 2181 | public: |
| 2182 | Mutex mu_; |
| 2183 | int a GUARDED_BY(mu_); |
| 2184 | }; |
| 2185 | |
| 2186 | class Bar { |
| 2187 | public: |
| 2188 | int b; |
| 2189 | Foo* f; |
| 2190 | |
| 2191 | Foo& getFoo() { return *f; } |
| 2192 | Foo& getFoo2(int c) { return *f; } |
| 2193 | Foo& getFoo3(int c, int d) { return *f; } |
| 2194 | |
| 2195 | Foo& getFooey() { return *f; } |
| 2196 | }; |
| 2197 | |
| 2198 | Foo& getBarFoo(Bar &bar, int c) { return bar.getFoo2(c); } |
| 2199 | |
| 2200 | void test() { |
| 2201 | Foo foo; |
| 2202 | Foo *fooArray; |
| 2203 | Bar bar; |
| 2204 | int a; |
| 2205 | int b; |
| 2206 | int c; |
| 2207 | |
| 2208 | bar.getFoo().mu_.Lock(); |
| 2209 | bar.getFoo().a = 0; |
| 2210 | bar.getFoo().mu_.Unlock(); |
| 2211 | |
| 2212 | (bar.getFoo().mu_).Lock(); // test parenthesis |
| 2213 | bar.getFoo().a = 0; |
| 2214 | (bar.getFoo().mu_).Unlock(); |
| 2215 | |
| 2216 | bar.getFoo2(a).mu_.Lock(); |
| 2217 | bar.getFoo2(a).a = 0; |
| 2218 | bar.getFoo2(a).mu_.Unlock(); |
| 2219 | |
| 2220 | bar.getFoo3(a, b).mu_.Lock(); |
| 2221 | bar.getFoo3(a, b).a = 0; |
| 2222 | bar.getFoo3(a, b).mu_.Unlock(); |
| 2223 | |
| 2224 | getBarFoo(bar, a).mu_.Lock(); |
| 2225 | getBarFoo(bar, a).a = 0; |
| 2226 | getBarFoo(bar, a).mu_.Unlock(); |
| 2227 | |
| 2228 | bar.getFoo2(10).mu_.Lock(); |
| 2229 | bar.getFoo2(10).a = 0; |
| 2230 | bar.getFoo2(10).mu_.Unlock(); |
| 2231 | |
| 2232 | bar.getFoo2(a + 1).mu_.Lock(); |
| 2233 | bar.getFoo2(a + 1).a = 0; |
| 2234 | bar.getFoo2(a + 1).mu_.Unlock(); |
| 2235 | |
| 2236 | (a > 0 ? fooArray[1] : fooArray[b]).mu_.Lock(); |
| 2237 | (a > 0 ? fooArray[1] : fooArray[b]).a = 0; |
| 2238 | (a > 0 ? fooArray[1] : fooArray[b]).mu_.Unlock(); |
| 2239 | |
| 2240 | bar.getFoo().mu_.Lock(); |
| 2241 | bar.getFooey().a = 0; // \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2242 | // expected-warning {{writing variable 'a' requires locking 'bar.getFooey().mu_' exclusively}} |
DeLesley Hutchins | 0d95dfc | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 2243 | bar.getFoo().mu_.Unlock(); |
| 2244 | |
| 2245 | bar.getFoo2(a).mu_.Lock(); |
| 2246 | bar.getFoo2(b).a = 0; // \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2247 | // expected-warning {{writing variable 'a' requires locking 'bar.getFoo2(b).mu_' exclusively}} |
DeLesley Hutchins | 0d95dfc | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 2248 | bar.getFoo2(a).mu_.Unlock(); |
| 2249 | |
| 2250 | bar.getFoo3(a, b).mu_.Lock(); |
| 2251 | bar.getFoo3(a, c).a = 0; // \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2252 | // expected-warning {{writing variable 'a' requires locking 'bar.getFoo3(a,c).mu_' exclusively}} |
DeLesley Hutchins | 0d95dfc | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 2253 | bar.getFoo3(a, b).mu_.Unlock(); |
| 2254 | |
| 2255 | getBarFoo(bar, a).mu_.Lock(); |
| 2256 | getBarFoo(bar, b).a = 0; // \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2257 | // expected-warning {{writing variable 'a' requires locking 'getBarFoo(bar,b).mu_' exclusively}} |
DeLesley Hutchins | 0d95dfc | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 2258 | getBarFoo(bar, a).mu_.Unlock(); |
| 2259 | |
| 2260 | (a > 0 ? fooArray[1] : fooArray[b]).mu_.Lock(); |
| 2261 | (a > 0 ? fooArray[b] : fooArray[c]).a = 0; // \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2262 | // expected-warning {{writing variable 'a' requires locking '((a#_)#_#fooArray[b]).mu_' exclusively}} |
DeLesley Hutchins | 0d95dfc | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 2263 | (a > 0 ? fooArray[1] : fooArray[b]).mu_.Unlock(); |
| 2264 | } |
| 2265 | |
| 2266 | |
DeLesley Hutchins | 0da4414 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 2267 | } // end namespace MoreLockExpressions |
| 2268 | |
| 2269 | |
| 2270 | namespace TrylockJoinPoint { |
| 2271 | |
| 2272 | class Foo { |
| 2273 | Mutex mu; |
| 2274 | bool c; |
| 2275 | |
| 2276 | void foo() { |
| 2277 | if (c) { |
| 2278 | if (!mu.TryLock()) |
| 2279 | return; |
| 2280 | } else { |
| 2281 | mu.Lock(); |
| 2282 | } |
| 2283 | mu.Unlock(); |
| 2284 | } |
| 2285 | }; |
| 2286 | |
| 2287 | } // end namespace TrylockJoinPoint |
DeLesley Hutchins | 0d95dfc | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 2288 | |
| 2289 | |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 2290 | namespace LockReturned { |
| 2291 | |
| 2292 | class Foo { |
| 2293 | public: |
| 2294 | int a GUARDED_BY(mu_); |
| 2295 | void foo() EXCLUSIVE_LOCKS_REQUIRED(mu_); |
| 2296 | void foo2(Foo* f) EXCLUSIVE_LOCKS_REQUIRED(mu_, f->mu_); |
| 2297 | |
| 2298 | static void sfoo(Foo* f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_); |
| 2299 | |
| 2300 | Mutex* getMu() LOCK_RETURNED(mu_); |
| 2301 | |
| 2302 | Mutex mu_; |
| 2303 | |
| 2304 | static Mutex* getMu(Foo* f) LOCK_RETURNED(f->mu_); |
| 2305 | }; |
| 2306 | |
| 2307 | |
| 2308 | // Calls getMu() directly to lock and unlock |
| 2309 | void test1(Foo* f1, Foo* f2) { |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2310 | f1->a = 0; // expected-warning {{writing variable 'a' requires locking 'f1->mu_' exclusively}} |
| 2311 | f1->foo(); // expected-warning {{calling function 'foo' requires exclusive lock on 'f1->mu_'}} |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 2312 | |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2313 | f1->foo2(f2); // expected-warning {{calling function 'foo2' requires exclusive lock on 'f1->mu_'}} \ |
| 2314 | // expected-warning {{calling function 'foo2' requires exclusive lock on 'f2->mu_'}} |
| 2315 | Foo::sfoo(f1); // expected-warning {{calling function 'sfoo' requires exclusive lock on 'f1->mu_'}} |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 2316 | |
| 2317 | f1->getMu()->Lock(); |
| 2318 | |
| 2319 | f1->a = 0; |
| 2320 | f1->foo(); |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2321 | f1->foo2(f2); // expected-warning {{calling function 'foo2' requires exclusive lock on 'f2->mu_'}} |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 2322 | |
| 2323 | Foo::getMu(f2)->Lock(); |
| 2324 | f1->foo2(f2); |
| 2325 | Foo::getMu(f2)->Unlock(); |
| 2326 | |
| 2327 | Foo::sfoo(f1); |
| 2328 | |
| 2329 | f1->getMu()->Unlock(); |
| 2330 | } |
| 2331 | |
| 2332 | |
| 2333 | Mutex* getFooMu(Foo* f) LOCK_RETURNED(Foo::getMu(f)); |
| 2334 | |
| 2335 | class Bar : public Foo { |
| 2336 | public: |
| 2337 | int b GUARDED_BY(getMu()); |
| 2338 | void bar() EXCLUSIVE_LOCKS_REQUIRED(getMu()); |
| 2339 | void bar2(Bar* g) EXCLUSIVE_LOCKS_REQUIRED(getMu(this), g->getMu()); |
| 2340 | |
| 2341 | static void sbar(Bar* g) EXCLUSIVE_LOCKS_REQUIRED(g->getMu()); |
| 2342 | static void sbar2(Bar* g) EXCLUSIVE_LOCKS_REQUIRED(getFooMu(g)); |
| 2343 | }; |
| 2344 | |
| 2345 | |
| 2346 | |
| 2347 | // Use getMu() within other attributes. |
| 2348 | // This requires at lest levels of substitution, more in the case of |
| 2349 | void test2(Bar* b1, Bar* b2) { |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2350 | b1->b = 0; // expected-warning {{writing variable 'b' requires locking 'b1->mu_' exclusively}} |
| 2351 | b1->bar(); // expected-warning {{calling function 'bar' requires exclusive lock on 'b1->mu_'}} |
| 2352 | b1->bar2(b2); // expected-warning {{calling function 'bar2' requires exclusive lock on 'b1->mu_'}} \ |
| 2353 | // expected-warning {{calling function 'bar2' requires exclusive lock on 'b2->mu_'}} |
| 2354 | Bar::sbar(b1); // expected-warning {{calling function 'sbar' requires exclusive lock on 'b1->mu_'}} |
| 2355 | Bar::sbar2(b1); // expected-warning {{calling function 'sbar2' requires exclusive lock on 'b1->mu_'}} |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 2356 | |
| 2357 | b1->getMu()->Lock(); |
| 2358 | |
| 2359 | b1->b = 0; |
| 2360 | b1->bar(); |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2361 | b1->bar2(b2); // expected-warning {{calling function 'bar2' requires exclusive lock on 'b2->mu_'}} |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 2362 | |
| 2363 | b2->getMu()->Lock(); |
| 2364 | b1->bar2(b2); |
| 2365 | |
| 2366 | b2->getMu()->Unlock(); |
| 2367 | |
| 2368 | Bar::sbar(b1); |
| 2369 | Bar::sbar2(b1); |
| 2370 | |
| 2371 | b1->getMu()->Unlock(); |
| 2372 | } |
| 2373 | |
| 2374 | |
| 2375 | // Sanity check -- lock the mutex directly, but use attributes that call getMu() |
| 2376 | // Also lock the mutex using getFooMu, which calls a lock_returned function. |
| 2377 | void test3(Bar* b1, Bar* b2) { |
| 2378 | b1->mu_.Lock(); |
| 2379 | b1->b = 0; |
| 2380 | b1->bar(); |
| 2381 | |
| 2382 | getFooMu(b2)->Lock(); |
| 2383 | b1->bar2(b2); |
| 2384 | getFooMu(b2)->Unlock(); |
| 2385 | |
| 2386 | Bar::sbar(b1); |
| 2387 | Bar::sbar2(b1); |
| 2388 | |
| 2389 | b1->mu_.Unlock(); |
| 2390 | } |
| 2391 | |
| 2392 | } // end namespace LockReturned |
| 2393 | |
| 2394 | |
DeLesley Hutchins | c99a5d8 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 2395 | namespace ReleasableScopedLock { |
| 2396 | |
| 2397 | class Foo { |
| 2398 | Mutex mu_; |
| 2399 | bool c; |
| 2400 | int a GUARDED_BY(mu_); |
| 2401 | |
| 2402 | void test1(); |
| 2403 | void test2(); |
| 2404 | void test3(); |
DeLesley Hutchins | 5381c05 | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2405 | void test4(); |
| 2406 | void test5(); |
DeLesley Hutchins | c99a5d8 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 2407 | }; |
| 2408 | |
| 2409 | |
| 2410 | void Foo::test1() { |
| 2411 | ReleasableMutexLock rlock(&mu_); |
| 2412 | rlock.Release(); |
| 2413 | } |
| 2414 | |
| 2415 | void Foo::test2() { |
| 2416 | ReleasableMutexLock rlock(&mu_); |
| 2417 | if (c) { // test join point -- held/not held during release |
| 2418 | rlock.Release(); |
| 2419 | } |
| 2420 | } |
| 2421 | |
| 2422 | void Foo::test3() { |
| 2423 | ReleasableMutexLock rlock(&mu_); |
| 2424 | a = 0; |
| 2425 | rlock.Release(); |
| 2426 | a = 1; // expected-warning {{writing variable 'a' requires locking 'mu_' exclusively}} |
| 2427 | } |
| 2428 | |
DeLesley Hutchins | 5381c05 | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2429 | void Foo::test4() { |
| 2430 | ReleasableMutexLock rlock(&mu_); |
| 2431 | rlock.Release(); |
| 2432 | rlock.Release(); // expected-warning {{unlocking 'mu_' that was not locked}} |
| 2433 | } |
| 2434 | |
| 2435 | void Foo::test5() { |
| 2436 | ReleasableMutexLock rlock(&mu_); |
| 2437 | if (c) { |
| 2438 | rlock.Release(); |
| 2439 | } |
| 2440 | // no warning on join point for managed lock. |
| 2441 | rlock.Release(); // expected-warning {{unlocking 'mu_' that was not locked}} |
| 2442 | } |
| 2443 | |
| 2444 | |
DeLesley Hutchins | c99a5d8 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 2445 | } // end namespace ReleasableScopedLock |
| 2446 | |
| 2447 | |
DeLesley Hutchins | 76f0a6e | 2012-07-02 21:59:24 +0000 | [diff] [blame] | 2448 | namespace TrylockFunctionTest { |
| 2449 | |
| 2450 | class Foo { |
| 2451 | public: |
| 2452 | Mutex mu1_; |
| 2453 | Mutex mu2_; |
| 2454 | bool c; |
| 2455 | |
| 2456 | bool lockBoth() EXCLUSIVE_TRYLOCK_FUNCTION(true, mu1_, mu2_); |
| 2457 | }; |
| 2458 | |
| 2459 | bool Foo::lockBoth() { |
| 2460 | if (!mu1_.TryLock()) |
| 2461 | return false; |
| 2462 | |
| 2463 | mu2_.Lock(); |
| 2464 | if (!c) { |
| 2465 | mu1_.Unlock(); |
| 2466 | mu2_.Unlock(); |
| 2467 | return false; |
| 2468 | } |
| 2469 | |
| 2470 | return true; |
| 2471 | } |
| 2472 | |
| 2473 | |
| 2474 | } // end namespace TrylockFunctionTest |
DeLesley Hutchins | c99a5d8 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 2475 | |
| 2476 | |
| 2477 | |
DeLesley Hutchins | c36eda1 | 2012-07-02 22:12:12 +0000 | [diff] [blame] | 2478 | namespace DoubleLockBug { |
| 2479 | |
| 2480 | class Foo { |
| 2481 | public: |
| 2482 | Mutex mu_; |
| 2483 | int a GUARDED_BY(mu_); |
| 2484 | |
DeLesley Hutchins | 5381c05 | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2485 | void foo1() EXCLUSIVE_LOCKS_REQUIRED(mu_); |
| 2486 | int foo2() SHARED_LOCKS_REQUIRED(mu_); |
DeLesley Hutchins | c36eda1 | 2012-07-02 22:12:12 +0000 | [diff] [blame] | 2487 | }; |
| 2488 | |
| 2489 | |
DeLesley Hutchins | 5381c05 | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2490 | void Foo::foo1() EXCLUSIVE_LOCKS_REQUIRED(mu_) { |
DeLesley Hutchins | c36eda1 | 2012-07-02 22:12:12 +0000 | [diff] [blame] | 2491 | a = 0; |
| 2492 | } |
| 2493 | |
DeLesley Hutchins | 5381c05 | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2494 | int Foo::foo2() SHARED_LOCKS_REQUIRED(mu_) { |
| 2495 | return a; |
| 2496 | } |
| 2497 | |
| 2498 | } |
| 2499 | |
DeLesley Hutchins | c36eda1 | 2012-07-02 22:12:12 +0000 | [diff] [blame] | 2500 | |
| 2501 | |
DeLesley Hutchins | 879a433 | 2012-07-02 22:16:54 +0000 | [diff] [blame] | 2502 | namespace UnlockBug { |
DeLesley Hutchins | c36eda1 | 2012-07-02 22:12:12 +0000 | [diff] [blame] | 2503 | |
DeLesley Hutchins | 879a433 | 2012-07-02 22:16:54 +0000 | [diff] [blame] | 2504 | class Foo { |
| 2505 | public: |
| 2506 | Mutex mutex_; |
| 2507 | |
| 2508 | void foo1() EXCLUSIVE_LOCKS_REQUIRED(mutex_) { // expected-note {{mutex acquired here}} |
| 2509 | mutex_.Unlock(); |
| 2510 | } // expected-warning {{expecting mutex 'mutex_' to be locked at the end of function}} |
| 2511 | |
| 2512 | |
| 2513 | void foo2() SHARED_LOCKS_REQUIRED(mutex_) { // expected-note {{mutex acquired here}} |
| 2514 | mutex_.Unlock(); |
| 2515 | } // expected-warning {{expecting mutex 'mutex_' to be locked at the end of function}} |
| 2516 | }; |
| 2517 | |
| 2518 | } // end namespace UnlockBug |
DeLesley Hutchins | c36eda1 | 2012-07-02 22:12:12 +0000 | [diff] [blame] | 2519 | |
DeLesley Hutchins | c99a5d8 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 2520 | |
DeLesley Hutchins | 5381c05 | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2521 | |
DeLesley Hutchins | bbe3341 | 2012-07-02 22:26:29 +0000 | [diff] [blame] | 2522 | namespace FoolishScopedLockableBug { |
| 2523 | |
| 2524 | class SCOPED_LOCKABLE WTF_ScopedLockable { |
| 2525 | public: |
| 2526 | WTF_ScopedLockable(Mutex* mu) EXCLUSIVE_LOCK_FUNCTION(mu); |
| 2527 | |
| 2528 | // have to call release() manually; |
| 2529 | ~WTF_ScopedLockable(); |
| 2530 | |
| 2531 | void release() UNLOCK_FUNCTION(); |
| 2532 | }; |
| 2533 | |
| 2534 | |
| 2535 | class Foo { |
| 2536 | Mutex mu_; |
| 2537 | int a GUARDED_BY(mu_); |
| 2538 | bool c; |
| 2539 | |
| 2540 | void doSomething(); |
| 2541 | |
| 2542 | void test1() { |
| 2543 | WTF_ScopedLockable wtf(&mu_); |
| 2544 | wtf.release(); |
| 2545 | } |
| 2546 | |
| 2547 | void test2() { |
| 2548 | WTF_ScopedLockable wtf(&mu_); // expected-note {{mutex acquired here}} |
| 2549 | } // expected-warning {{mutex 'mu_' is still locked at the end of function}} |
| 2550 | |
| 2551 | void test3() { |
| 2552 | if (c) { |
| 2553 | WTF_ScopedLockable wtf(&mu_); |
| 2554 | wtf.release(); |
| 2555 | } |
| 2556 | } |
| 2557 | |
| 2558 | void test4() { |
| 2559 | if (c) { |
| 2560 | doSomething(); |
| 2561 | } |
| 2562 | else { |
| 2563 | WTF_ScopedLockable wtf(&mu_); |
| 2564 | wtf.release(); |
| 2565 | } |
| 2566 | } |
| 2567 | |
| 2568 | void test5() { |
| 2569 | if (c) { |
| 2570 | WTF_ScopedLockable wtf(&mu_); // expected-note {{mutex acquired here}} |
| 2571 | } |
| 2572 | } // expected-warning {{mutex 'mu_' is not locked on every path through here}} |
| 2573 | |
| 2574 | void test6() { |
| 2575 | if (c) { |
| 2576 | doSomething(); |
| 2577 | } |
| 2578 | else { |
| 2579 | WTF_ScopedLockable wtf(&mu_); // expected-note {{mutex acquired here}} |
| 2580 | } |
| 2581 | } // expected-warning {{mutex 'mu_' is not locked on every path through here}} |
| 2582 | }; |
| 2583 | |
| 2584 | |
| 2585 | } // end namespace FoolishScopedLockableBug |
| 2586 | |
DeLesley Hutchins | 9d6e7f3 | 2012-07-03 18:25:56 +0000 | [diff] [blame] | 2587 | |
DeLesley Hutchins | 5381c05 | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2588 | |
DeLesley Hutchins | 9d6e7f3 | 2012-07-03 18:25:56 +0000 | [diff] [blame] | 2589 | namespace TemporaryCleanupExpr { |
| 2590 | |
| 2591 | class Foo { |
| 2592 | int a GUARDED_BY(getMutexPtr().get()); |
| 2593 | |
| 2594 | SmartPtr<Mutex> getMutexPtr(); |
| 2595 | |
| 2596 | void test(); |
| 2597 | }; |
| 2598 | |
| 2599 | |
| 2600 | void Foo::test() { |
| 2601 | { |
| 2602 | ReaderMutexLock lock(getMutexPtr().get()); |
| 2603 | int b = a; |
DeLesley Hutchins | 96fac6a | 2012-07-03 19:47:18 +0000 | [diff] [blame] | 2604 | } |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2605 | int b = a; // expected-warning {{reading variable 'a' requires locking 'getMutexPtr()'}} |
DeLesley Hutchins | 9d6e7f3 | 2012-07-03 18:25:56 +0000 | [diff] [blame] | 2606 | } |
| 2607 | |
| 2608 | } // end namespace TemporaryCleanupExpr |
| 2609 | |
| 2610 | |
DeLesley Hutchins | 96fac6a | 2012-07-03 19:47:18 +0000 | [diff] [blame] | 2611 | |
| 2612 | namespace SmartPointerTests { |
| 2613 | |
| 2614 | class Foo { |
| 2615 | public: |
| 2616 | SmartPtr<Mutex> mu_; |
| 2617 | int a GUARDED_BY(mu_); |
| 2618 | int b GUARDED_BY(mu_.get()); |
| 2619 | int c GUARDED_BY(*mu_); |
| 2620 | |
| 2621 | void Lock() EXCLUSIVE_LOCK_FUNCTION(mu_); |
| 2622 | void Unlock() UNLOCK_FUNCTION(mu_); |
| 2623 | |
| 2624 | void test0(); |
| 2625 | void test1(); |
| 2626 | void test2(); |
| 2627 | void test3(); |
| 2628 | void test4(); |
| 2629 | void test5(); |
| 2630 | void test6(); |
| 2631 | void test7(); |
| 2632 | void test8(); |
| 2633 | }; |
| 2634 | |
| 2635 | void Foo::test0() { |
| 2636 | a = 0; // expected-warning {{writing variable 'a' requires locking 'mu_' exclusively}} |
| 2637 | b = 0; // expected-warning {{writing variable 'b' requires locking 'mu_' exclusively}} |
| 2638 | c = 0; // expected-warning {{writing variable 'c' requires locking 'mu_' exclusively}} |
| 2639 | } |
| 2640 | |
| 2641 | void Foo::test1() { |
| 2642 | mu_->Lock(); |
| 2643 | a = 0; |
| 2644 | b = 0; |
| 2645 | c = 0; |
| 2646 | mu_->Unlock(); |
| 2647 | } |
| 2648 | |
| 2649 | void Foo::test2() { |
| 2650 | (*mu_).Lock(); |
| 2651 | a = 0; |
| 2652 | b = 0; |
| 2653 | c = 0; |
| 2654 | (*mu_).Unlock(); |
| 2655 | } |
| 2656 | |
| 2657 | |
| 2658 | void Foo::test3() { |
| 2659 | mu_.get()->Lock(); |
| 2660 | a = 0; |
| 2661 | b = 0; |
| 2662 | c = 0; |
| 2663 | mu_.get()->Unlock(); |
| 2664 | } |
| 2665 | |
| 2666 | |
| 2667 | void Foo::test4() { |
| 2668 | MutexLock lock(mu_.get()); |
| 2669 | a = 0; |
| 2670 | b = 0; |
| 2671 | c = 0; |
| 2672 | } |
| 2673 | |
| 2674 | |
| 2675 | void Foo::test5() { |
| 2676 | MutexLock lock(&(*mu_)); |
| 2677 | a = 0; |
| 2678 | b = 0; |
| 2679 | c = 0; |
| 2680 | } |
| 2681 | |
| 2682 | |
| 2683 | void Foo::test6() { |
| 2684 | Lock(); |
| 2685 | a = 0; |
| 2686 | b = 0; |
| 2687 | c = 0; |
| 2688 | Unlock(); |
| 2689 | } |
| 2690 | |
| 2691 | |
| 2692 | void Foo::test7() { |
| 2693 | { |
| 2694 | Lock(); |
| 2695 | mu_->Unlock(); |
| 2696 | } |
| 2697 | { |
| 2698 | mu_->Lock(); |
| 2699 | Unlock(); |
| 2700 | } |
| 2701 | { |
| 2702 | mu_.get()->Lock(); |
| 2703 | mu_->Unlock(); |
| 2704 | } |
| 2705 | { |
| 2706 | mu_->Lock(); |
| 2707 | mu_.get()->Unlock(); |
| 2708 | } |
| 2709 | { |
| 2710 | mu_.get()->Lock(); |
| 2711 | (*mu_).Unlock(); |
| 2712 | } |
| 2713 | { |
| 2714 | (*mu_).Lock(); |
| 2715 | mu_->Unlock(); |
| 2716 | } |
| 2717 | } |
| 2718 | |
| 2719 | |
| 2720 | void Foo::test8() { |
| 2721 | mu_->Lock(); |
| 2722 | mu_.get()->Lock(); // expected-warning {{locking 'mu_' that is already locked}} |
| 2723 | (*mu_).Lock(); // expected-warning {{locking 'mu_' that is already locked}} |
| 2724 | mu_.get()->Unlock(); |
| 2725 | Unlock(); // expected-warning {{unlocking 'mu_' that was not locked}} |
| 2726 | } |
| 2727 | |
| 2728 | |
| 2729 | class Bar { |
| 2730 | SmartPtr<Foo> foo; |
| 2731 | |
| 2732 | void test0(); |
| 2733 | void test1(); |
| 2734 | void test2(); |
| 2735 | void test3(); |
| 2736 | }; |
| 2737 | |
| 2738 | |
| 2739 | void Bar::test0() { |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2740 | foo->a = 0; // expected-warning {{writing variable 'a' requires locking 'foo->mu_' exclusively}} |
| 2741 | (*foo).b = 0; // expected-warning {{writing variable 'b' requires locking 'foo->mu_' exclusively}} |
| 2742 | foo.get()->c = 0; // expected-warning {{writing variable 'c' requires locking 'foo->mu_' exclusively}} |
DeLesley Hutchins | 96fac6a | 2012-07-03 19:47:18 +0000 | [diff] [blame] | 2743 | } |
| 2744 | |
| 2745 | |
| 2746 | void Bar::test1() { |
| 2747 | foo->mu_->Lock(); |
| 2748 | foo->a = 0; |
| 2749 | (*foo).b = 0; |
| 2750 | foo.get()->c = 0; |
| 2751 | foo->mu_->Unlock(); |
| 2752 | } |
| 2753 | |
| 2754 | |
| 2755 | void Bar::test2() { |
| 2756 | (*foo).mu_->Lock(); |
| 2757 | foo->a = 0; |
| 2758 | (*foo).b = 0; |
| 2759 | foo.get()->c = 0; |
| 2760 | foo.get()->mu_->Unlock(); |
| 2761 | } |
| 2762 | |
| 2763 | |
| 2764 | void Bar::test3() { |
| 2765 | MutexLock lock(foo->mu_.get()); |
| 2766 | foo->a = 0; |
| 2767 | (*foo).b = 0; |
| 2768 | foo.get()->c = 0; |
| 2769 | } |
| 2770 | |
| 2771 | } // end namespace SmartPointerTests |
| 2772 | |
| 2773 | |
| 2774 | |
DeLesley Hutchins | 5381c05 | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2775 | namespace DuplicateAttributeTest { |
| 2776 | |
| 2777 | class LOCKABLE Foo { |
| 2778 | public: |
| 2779 | Mutex mu1_; |
| 2780 | Mutex mu2_; |
| 2781 | Mutex mu3_; |
| 2782 | int a GUARDED_BY(mu1_); |
| 2783 | int b GUARDED_BY(mu2_); |
| 2784 | int c GUARDED_BY(mu3_); |
| 2785 | |
| 2786 | void lock() EXCLUSIVE_LOCK_FUNCTION(); |
| 2787 | void unlock() UNLOCK_FUNCTION(); |
| 2788 | |
| 2789 | void lock1() EXCLUSIVE_LOCK_FUNCTION(mu1_); |
| 2790 | void slock1() SHARED_LOCK_FUNCTION(mu1_); |
| 2791 | void lock3() EXCLUSIVE_LOCK_FUNCTION(mu1_, mu2_, mu3_); |
| 2792 | void locklots() |
| 2793 | EXCLUSIVE_LOCK_FUNCTION(mu1_) |
| 2794 | EXCLUSIVE_LOCK_FUNCTION(mu2_) |
| 2795 | EXCLUSIVE_LOCK_FUNCTION(mu1_, mu2_, mu3_); |
| 2796 | |
| 2797 | void unlock1() UNLOCK_FUNCTION(mu1_); |
| 2798 | void unlock3() UNLOCK_FUNCTION(mu1_, mu2_, mu3_); |
| 2799 | void unlocklots() |
| 2800 | UNLOCK_FUNCTION(mu1_) |
| 2801 | UNLOCK_FUNCTION(mu2_) |
| 2802 | UNLOCK_FUNCTION(mu1_, mu2_, mu3_); |
| 2803 | }; |
| 2804 | |
| 2805 | |
| 2806 | void Foo::lock() EXCLUSIVE_LOCK_FUNCTION() { } |
| 2807 | void Foo::unlock() UNLOCK_FUNCTION() { } |
| 2808 | |
| 2809 | void Foo::lock1() EXCLUSIVE_LOCK_FUNCTION(mu1_) { |
| 2810 | mu1_.Lock(); |
| 2811 | } |
| 2812 | |
| 2813 | void Foo::slock1() SHARED_LOCK_FUNCTION(mu1_) { |
| 2814 | mu1_.Lock(); |
| 2815 | } |
| 2816 | |
| 2817 | void Foo::lock3() EXCLUSIVE_LOCK_FUNCTION(mu1_, mu2_, mu3_) { |
| 2818 | mu1_.Lock(); |
| 2819 | mu2_.Lock(); |
| 2820 | mu3_.Lock(); |
| 2821 | } |
| 2822 | |
| 2823 | void Foo::locklots() |
| 2824 | EXCLUSIVE_LOCK_FUNCTION(mu1_, mu2_) |
| 2825 | EXCLUSIVE_LOCK_FUNCTION(mu2_, mu3_) { |
| 2826 | mu1_.Lock(); |
| 2827 | mu2_.Lock(); |
| 2828 | mu3_.Lock(); |
| 2829 | } |
| 2830 | |
| 2831 | void Foo::unlock1() UNLOCK_FUNCTION(mu1_) { |
| 2832 | mu1_.Unlock(); |
| 2833 | } |
| 2834 | |
| 2835 | void Foo::unlock3() UNLOCK_FUNCTION(mu1_, mu2_, mu3_) { |
| 2836 | mu1_.Unlock(); |
| 2837 | mu2_.Unlock(); |
| 2838 | mu3_.Unlock(); |
| 2839 | } |
| 2840 | |
| 2841 | void Foo::unlocklots() |
| 2842 | UNLOCK_FUNCTION(mu1_, mu2_) |
| 2843 | UNLOCK_FUNCTION(mu2_, mu3_) { |
| 2844 | mu1_.Unlock(); |
| 2845 | mu2_.Unlock(); |
| 2846 | mu3_.Unlock(); |
| 2847 | } |
| 2848 | |
| 2849 | |
| 2850 | void test0() { |
| 2851 | Foo foo; |
| 2852 | foo.lock(); |
| 2853 | foo.unlock(); |
| 2854 | |
| 2855 | foo.lock(); |
| 2856 | foo.lock(); // expected-warning {{locking 'foo' that is already locked}} |
| 2857 | foo.unlock(); |
| 2858 | foo.unlock(); // expected-warning {{unlocking 'foo' that was not locked}} |
| 2859 | } |
| 2860 | |
| 2861 | |
| 2862 | void test1() { |
| 2863 | Foo foo; |
| 2864 | foo.lock1(); |
| 2865 | foo.a = 0; |
| 2866 | foo.unlock1(); |
| 2867 | |
| 2868 | foo.lock1(); |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2869 | foo.lock1(); // expected-warning {{locking 'foo.mu1_' that is already locked}} |
DeLesley Hutchins | 5381c05 | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2870 | foo.a = 0; |
| 2871 | foo.unlock1(); |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2872 | foo.unlock1(); // expected-warning {{unlocking 'foo.mu1_' that was not locked}} |
DeLesley Hutchins | 5381c05 | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2873 | } |
| 2874 | |
| 2875 | |
| 2876 | int test2() { |
| 2877 | Foo foo; |
| 2878 | foo.slock1(); |
| 2879 | int d1 = foo.a; |
| 2880 | foo.unlock1(); |
| 2881 | |
| 2882 | foo.slock1(); |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2883 | foo.slock1(); // expected-warning {{locking 'foo.mu1_' that is already locked}} |
DeLesley Hutchins | 5381c05 | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2884 | int d2 = foo.a; |
| 2885 | foo.unlock1(); |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2886 | foo.unlock1(); // expected-warning {{unlocking 'foo.mu1_' that was not locked}} |
DeLesley Hutchins | 5381c05 | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2887 | return d1 + d2; |
| 2888 | } |
| 2889 | |
| 2890 | |
| 2891 | void test3() { |
| 2892 | Foo foo; |
| 2893 | foo.lock3(); |
| 2894 | foo.a = 0; |
| 2895 | foo.b = 0; |
| 2896 | foo.c = 0; |
| 2897 | foo.unlock3(); |
| 2898 | |
| 2899 | foo.lock3(); |
| 2900 | foo.lock3(); // \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2901 | // expected-warning {{locking 'foo.mu1_' that is already locked}} \ |
| 2902 | // expected-warning {{locking 'foo.mu2_' that is already locked}} \ |
| 2903 | // expected-warning {{locking 'foo.mu3_' that is already locked}} |
DeLesley Hutchins | 5381c05 | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2904 | foo.a = 0; |
| 2905 | foo.b = 0; |
| 2906 | foo.c = 0; |
| 2907 | foo.unlock3(); |
| 2908 | foo.unlock3(); // \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2909 | // expected-warning {{unlocking 'foo.mu1_' that was not locked}} \ |
| 2910 | // expected-warning {{unlocking 'foo.mu2_' that was not locked}} \ |
| 2911 | // expected-warning {{unlocking 'foo.mu3_' that was not locked}} |
DeLesley Hutchins | 5381c05 | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
| 2914 | |
| 2915 | void testlots() { |
| 2916 | Foo foo; |
| 2917 | foo.locklots(); |
| 2918 | foo.a = 0; |
| 2919 | foo.b = 0; |
| 2920 | foo.c = 0; |
| 2921 | foo.unlocklots(); |
| 2922 | |
| 2923 | foo.locklots(); |
| 2924 | foo.locklots(); // \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2925 | // expected-warning {{locking 'foo.mu1_' that is already locked}} \ |
| 2926 | // expected-warning {{locking 'foo.mu2_' that is already locked}} \ |
| 2927 | // expected-warning {{locking 'foo.mu3_' that is already locked}} |
DeLesley Hutchins | 5381c05 | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2928 | foo.a = 0; |
| 2929 | foo.b = 0; |
| 2930 | foo.c = 0; |
| 2931 | foo.unlocklots(); |
| 2932 | foo.unlocklots(); // \ |
DeLesley Hutchins | a74b715 | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2933 | // expected-warning {{unlocking 'foo.mu1_' that was not locked}} \ |
| 2934 | // expected-warning {{unlocking 'foo.mu2_' that was not locked}} \ |
| 2935 | // expected-warning {{unlocking 'foo.mu3_' that was not locked}} |
DeLesley Hutchins | 5381c05 | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
| 2938 | } // end namespace DuplicateAttributeTest |
| 2939 | |
| 2940 | |
| 2941 | |
DeLesley Hutchins | 1310611 | 2012-07-10 21:47:55 +0000 | [diff] [blame] | 2942 | namespace TryLockEqTest { |
| 2943 | |
| 2944 | class Foo { |
| 2945 | Mutex mu_; |
| 2946 | int a GUARDED_BY(mu_); |
| 2947 | bool c; |
| 2948 | |
| 2949 | int tryLockMutexI() EXCLUSIVE_TRYLOCK_FUNCTION(1, mu_); |
| 2950 | Mutex* tryLockMutexP() EXCLUSIVE_TRYLOCK_FUNCTION(1, mu_); |
| 2951 | void unlock() UNLOCK_FUNCTION(mu_); |
| 2952 | |
| 2953 | void test1(); |
| 2954 | void test2(); |
| 2955 | }; |
| 2956 | |
| 2957 | |
| 2958 | void Foo::test1() { |
| 2959 | if (tryLockMutexP() == 0) { |
| 2960 | a = 0; // expected-warning {{writing variable 'a' requires locking 'mu_' exclusively}} |
| 2961 | return; |
| 2962 | } |
| 2963 | a = 0; |
| 2964 | unlock(); |
| 2965 | |
| 2966 | if (tryLockMutexP() != 0) { |
| 2967 | a = 0; |
| 2968 | unlock(); |
| 2969 | } |
| 2970 | |
| 2971 | if (0 != tryLockMutexP()) { |
| 2972 | a = 0; |
| 2973 | unlock(); |
| 2974 | } |
| 2975 | |
| 2976 | if (!(tryLockMutexP() == 0)) { |
| 2977 | a = 0; |
| 2978 | unlock(); |
| 2979 | } |
| 2980 | |
| 2981 | if (tryLockMutexI() == 0) { |
| 2982 | a = 0; // expected-warning {{writing variable 'a' requires locking 'mu_' exclusively}} |
| 2983 | return; |
| 2984 | } |
| 2985 | a = 0; |
| 2986 | unlock(); |
| 2987 | |
| 2988 | if (0 == tryLockMutexI()) { |
| 2989 | a = 0; // expected-warning {{writing variable 'a' requires locking 'mu_' exclusively}} |
| 2990 | return; |
| 2991 | } |
| 2992 | a = 0; |
| 2993 | unlock(); |
| 2994 | |
| 2995 | if (tryLockMutexI() == 1) { |
| 2996 | a = 0; |
| 2997 | unlock(); |
| 2998 | } |
| 2999 | |
| 3000 | if (mu_.TryLock() == false) { |
| 3001 | a = 0; // expected-warning {{writing variable 'a' requires locking 'mu_' exclusively}} |
| 3002 | return; |
| 3003 | } |
| 3004 | a = 0; |
| 3005 | unlock(); |
| 3006 | |
| 3007 | if (mu_.TryLock() == true) { |
| 3008 | a = 0; |
| 3009 | unlock(); |
| 3010 | } |
| 3011 | else { |
| 3012 | a = 0; // expected-warning {{writing variable 'a' requires locking 'mu_' exclusively}} |
| 3013 | } |
| 3014 | |
| 3015 | #if __has_feature(cxx_nullptr) |
| 3016 | if (tryLockMutexP() == nullptr) { |
| 3017 | a = 0; // expected-warning {{writing variable 'a' requires locking 'mu_' exclusively}} |
| 3018 | return; |
| 3019 | } |
| 3020 | a = 0; |
| 3021 | unlock(); |
| 3022 | #endif |
| 3023 | } |
| 3024 | |
| 3025 | |
| 3026 | void Foo::test2() { |
| 3027 | /* FIXME: these tests depend on changes to the CFG. |
| 3028 | * |
| 3029 | if (mu_.TryLock() && c) { |
| 3030 | a = 0; |
| 3031 | unlock(); |
| 3032 | } |
| 3033 | else return; |
| 3034 | |
| 3035 | if (c && mu_.TryLock()) { |
| 3036 | a = 0; |
| 3037 | unlock(); |
| 3038 | } |
| 3039 | else return; |
| 3040 | |
| 3041 | if (!(mu_.TryLock() && c)) |
| 3042 | return; |
| 3043 | a = 0; |
| 3044 | unlock(); |
| 3045 | |
| 3046 | if (!(c && mu_.TryLock())) |
| 3047 | return; |
| 3048 | a = 0; |
| 3049 | unlock(); |
| 3050 | |
| 3051 | if (!(mu_.TryLock() == 0) && c) { |
| 3052 | a = 0; |
| 3053 | unlock(); |
| 3054 | } |
| 3055 | |
| 3056 | if (!mu_.TryLock() || c) |
| 3057 | return; |
| 3058 | a = 0; |
| 3059 | unlock(); |
| 3060 | */ |
| 3061 | } |
| 3062 | |
DeLesley Hutchins | 1310611 | 2012-07-10 21:47:55 +0000 | [diff] [blame] | 3063 | } // end namespace TryLockEqTest |
DeLesley Hutchins | 5381c05 | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 3064 | |
DeLesley Hutchins | 96fac6a | 2012-07-03 19:47:18 +0000 | [diff] [blame] | 3065 | |
DeLesley Hutchins | ee2f032 | 2012-08-10 20:29:46 +0000 | [diff] [blame] | 3066 | namespace ExistentialPatternMatching { |
| 3067 | |
| 3068 | class Graph { |
| 3069 | public: |
| 3070 | Mutex mu_; |
| 3071 | }; |
| 3072 | |
| 3073 | void LockAllGraphs() EXCLUSIVE_LOCK_FUNCTION(&Graph::mu_); |
| 3074 | void UnlockAllGraphs() UNLOCK_FUNCTION(&Graph::mu_); |
| 3075 | |
| 3076 | class Node { |
| 3077 | public: |
| 3078 | int a GUARDED_BY(&Graph::mu_); |
| 3079 | |
| 3080 | void foo() EXCLUSIVE_LOCKS_REQUIRED(&Graph::mu_) { |
| 3081 | a = 0; |
| 3082 | } |
| 3083 | void foo2() LOCKS_EXCLUDED(&Graph::mu_); |
| 3084 | }; |
| 3085 | |
| 3086 | void test() { |
| 3087 | Graph g1; |
| 3088 | Graph g2; |
| 3089 | Node n1; |
| 3090 | |
| 3091 | n1.a = 0; // expected-warning {{writing variable 'a' requires locking '&ExistentialPatternMatching::Graph::mu_' exclusively}} |
| 3092 | n1.foo(); // expected-warning {{calling function 'foo' requires exclusive lock on '&ExistentialPatternMatching::Graph::mu_'}} |
| 3093 | n1.foo2(); |
| 3094 | |
| 3095 | g1.mu_.Lock(); |
| 3096 | n1.a = 0; |
| 3097 | n1.foo(); |
| 3098 | n1.foo2(); // expected-warning {{cannot call function 'foo2' while mutex '&ExistentialPatternMatching::Graph::mu_' is locked}} |
| 3099 | g1.mu_.Unlock(); |
| 3100 | |
| 3101 | g2.mu_.Lock(); |
| 3102 | n1.a = 0; |
| 3103 | n1.foo(); |
| 3104 | n1.foo2(); // expected-warning {{cannot call function 'foo2' while mutex '&ExistentialPatternMatching::Graph::mu_' is locked}} |
| 3105 | g2.mu_.Unlock(); |
| 3106 | |
| 3107 | LockAllGraphs(); |
| 3108 | n1.a = 0; |
| 3109 | n1.foo(); |
| 3110 | n1.foo2(); // expected-warning {{cannot call function 'foo2' while mutex '&ExistentialPatternMatching::Graph::mu_' is locked}} |
| 3111 | UnlockAllGraphs(); |
| 3112 | |
| 3113 | LockAllGraphs(); |
| 3114 | g1.mu_.Unlock(); |
| 3115 | |
| 3116 | LockAllGraphs(); |
| 3117 | g2.mu_.Unlock(); |
| 3118 | |
| 3119 | LockAllGraphs(); |
| 3120 | g1.mu_.Lock(); // expected-warning {{locking 'g1.mu_' that is already locked}} |
| 3121 | g1.mu_.Unlock(); |
| 3122 | } |
| 3123 | |
| 3124 | } // end namespace ExistentialPatternMatching |
| 3125 | |
DeLesley Hutchins | 4e4c157 | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 3126 | |
| 3127 | namespace StringIgnoreTest { |
| 3128 | |
| 3129 | class Foo { |
| 3130 | public: |
| 3131 | Mutex mu_; |
| 3132 | void lock() EXCLUSIVE_LOCK_FUNCTION(""); |
| 3133 | void unlock() UNLOCK_FUNCTION(""); |
| 3134 | void goober() EXCLUSIVE_LOCKS_REQUIRED(""); |
| 3135 | void roober() SHARED_LOCKS_REQUIRED(""); |
| 3136 | }; |
| 3137 | |
| 3138 | |
| 3139 | class Bar : public Foo { |
| 3140 | public: |
| 3141 | void bar(Foo* f) { |
| 3142 | f->unlock(); |
| 3143 | f->goober(); |
| 3144 | f->roober(); |
| 3145 | f->lock(); |
| 3146 | }; |
| 3147 | }; |
| 3148 | |
| 3149 | } // end namespace StringIgnoreTest |
| 3150 | |
| 3151 | |
DeLesley Hutchins | 5408153 | 2012-08-31 22:09:53 +0000 | [diff] [blame] | 3152 | namespace LockReturnedScopeFix { |
| 3153 | |
| 3154 | class Base { |
| 3155 | protected: |
| 3156 | struct Inner; |
| 3157 | bool c; |
| 3158 | |
| 3159 | const Mutex& getLock(const Inner* i); |
| 3160 | |
| 3161 | void lockInner (Inner* i) EXCLUSIVE_LOCK_FUNCTION(getLock(i)); |
| 3162 | void unlockInner(Inner* i) UNLOCK_FUNCTION(getLock(i)); |
| 3163 | void foo(Inner* i) EXCLUSIVE_LOCKS_REQUIRED(getLock(i)); |
| 3164 | |
| 3165 | void bar(Inner* i); |
| 3166 | }; |
| 3167 | |
| 3168 | |
| 3169 | struct Base::Inner { |
| 3170 | Mutex lock_; |
| 3171 | void doSomething() EXCLUSIVE_LOCKS_REQUIRED(lock_); |
| 3172 | }; |
| 3173 | |
| 3174 | |
| 3175 | const Mutex& Base::getLock(const Inner* i) LOCK_RETURNED(i->lock_) { |
| 3176 | return i->lock_; |
| 3177 | } |
| 3178 | |
| 3179 | |
| 3180 | void Base::foo(Inner* i) { |
| 3181 | i->doSomething(); |
| 3182 | } |
| 3183 | |
| 3184 | void Base::bar(Inner* i) { |
| 3185 | if (c) { |
| 3186 | i->lock_.Lock(); |
| 3187 | unlockInner(i); |
| 3188 | } |
| 3189 | else { |
| 3190 | lockInner(i); |
| 3191 | i->lock_.Unlock(); |
| 3192 | } |
| 3193 | } |
| 3194 | |
| 3195 | } // end namespace LockReturnedScopeFix |
| 3196 | |
DeLesley Hutchins | fd0f11c | 2012-09-05 20:01:16 +0000 | [diff] [blame] | 3197 | |
| 3198 | namespace TrylockWithCleanups { |
| 3199 | |
| 3200 | class MyString { |
| 3201 | public: |
| 3202 | MyString(const char* s); |
| 3203 | ~MyString(); |
| 3204 | }; |
| 3205 | |
| 3206 | struct Foo { |
| 3207 | Mutex mu_; |
| 3208 | int a GUARDED_BY(mu_); |
| 3209 | }; |
| 3210 | |
| 3211 | Foo* GetAndLockFoo(const MyString& s) |
| 3212 | EXCLUSIVE_TRYLOCK_FUNCTION(true, &Foo::mu_); |
| 3213 | |
| 3214 | static void test() { |
| 3215 | Foo* lt = GetAndLockFoo("foo"); |
| 3216 | if (!lt) return; |
| 3217 | int a = lt->a; |
| 3218 | lt->mu_.Unlock(); |
| 3219 | } |
| 3220 | |
| 3221 | } |
| 3222 | |
| 3223 | |
DeLesley Hutchins | 0b4db3e | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 3224 | namespace UniversalLock { |
| 3225 | |
| 3226 | class Foo { |
| 3227 | Mutex mu_; |
| 3228 | bool c; |
| 3229 | |
| 3230 | int a GUARDED_BY(mu_); |
| 3231 | void r_foo() SHARED_LOCKS_REQUIRED(mu_); |
| 3232 | void w_foo() EXCLUSIVE_LOCKS_REQUIRED(mu_); |
| 3233 | |
| 3234 | void test1() { |
| 3235 | int b; |
| 3236 | |
| 3237 | beginNoWarnOnReads(); |
| 3238 | b = a; |
| 3239 | r_foo(); |
| 3240 | endNoWarnOnReads(); |
| 3241 | |
| 3242 | beginNoWarnOnWrites(); |
| 3243 | a = 0; |
| 3244 | w_foo(); |
| 3245 | endNoWarnOnWrites(); |
| 3246 | } |
| 3247 | |
| 3248 | // don't warn on joins with universal lock |
| 3249 | void test2() { |
| 3250 | if (c) { |
| 3251 | beginNoWarnOnWrites(); |
| 3252 | } |
| 3253 | a = 0; // \ |
| 3254 | // expected-warning {{writing variable 'a' requires locking 'mu_' exclusively}} |
| 3255 | endNoWarnOnWrites(); // \ |
| 3256 | // expected-warning {{unlocking '*' that was not locked}} |
| 3257 | } |
| 3258 | |
| 3259 | |
| 3260 | // make sure the universal lock joins properly |
| 3261 | void test3() { |
| 3262 | if (c) { |
| 3263 | mu_.Lock(); |
| 3264 | beginNoWarnOnWrites(); |
| 3265 | } |
| 3266 | else { |
| 3267 | beginNoWarnOnWrites(); |
| 3268 | mu_.Lock(); |
| 3269 | } |
| 3270 | a = 0; |
| 3271 | endNoWarnOnWrites(); |
| 3272 | mu_.Unlock(); |
| 3273 | } |
| 3274 | |
| 3275 | |
| 3276 | // combine universal lock with other locks |
| 3277 | void test4() { |
| 3278 | beginNoWarnOnWrites(); |
| 3279 | mu_.Lock(); |
| 3280 | mu_.Unlock(); |
| 3281 | endNoWarnOnWrites(); |
| 3282 | |
| 3283 | mu_.Lock(); |
| 3284 | beginNoWarnOnWrites(); |
| 3285 | endNoWarnOnWrites(); |
| 3286 | mu_.Unlock(); |
| 3287 | |
| 3288 | mu_.Lock(); |
| 3289 | beginNoWarnOnWrites(); |
| 3290 | mu_.Unlock(); |
| 3291 | endNoWarnOnWrites(); |
| 3292 | } |
| 3293 | }; |
| 3294 | |
| 3295 | } |