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