Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -Wthread-safety %s |
| 2 | |
| 3 | |
| 4 | //-----------------------------------------// |
| 5 | // Helper fields |
| 6 | //-----------------------------------------// |
| 7 | |
| 8 | |
| 9 | class __attribute__((lockable)) Mutex { |
| 10 | public: |
| 11 | void Lock() __attribute__((exclusive_lock_function)); |
| 12 | void ReaderLock() __attribute__((shared_lock_function)); |
| 13 | void Unlock() __attribute__((unlock_function)); |
| 14 | bool TryLock() __attribute__((exclusive_trylock_function(true))); |
| 15 | bool ReaderTryLock() __attribute__((shared_trylock_function(true))); |
| 16 | void LockWhen(const int &cond) __attribute__((exclusive_lock_function)); |
| 17 | }; |
| 18 | |
| 19 | |
| 20 | Mutex sls_mu; |
| 21 | |
| 22 | Mutex sls_mu2 __attribute__((acquired_after(sls_mu))); |
| 23 | int sls_guard_var __attribute__((guarded_var)) = 0; |
| 24 | int sls_guardby_var __attribute__((guarded_by(sls_mu))) = 0; |
| 25 | |
| 26 | bool getBool(); |
| 27 | |
| 28 | class MutexWrapper { |
| 29 | public: |
| 30 | Mutex mu; |
Caitlin Sadowski | 194418f | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 31 | int x __attribute__((guarded_by(mu))); |
| 32 | void MyLock() __attribute__((exclusive_lock_function(mu))); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | MutexWrapper sls_mw; |
| 36 | |
| 37 | void sls_fun_0() { |
| 38 | sls_mw.mu.Lock(); |
Caitlin Sadowski | 194418f | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 39 | sls_mw.x = 5; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 40 | sls_mw.mu.Unlock(); |
| 41 | } |
| 42 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 43 | void sls_fun_2() { |
| 44 | sls_mu.Lock(); |
| 45 | int x = sls_guard_var; |
| 46 | sls_mu.Unlock(); |
| 47 | } |
| 48 | |
| 49 | void sls_fun_3() { |
| 50 | sls_mu.Lock(); |
| 51 | sls_guard_var = 2; |
| 52 | sls_mu.Unlock(); |
| 53 | } |
| 54 | |
| 55 | void sls_fun_4() { |
| 56 | sls_mu2.Lock(); |
| 57 | sls_guard_var = 2; |
| 58 | sls_mu2.Unlock(); |
| 59 | } |
| 60 | |
| 61 | void sls_fun_5() { |
| 62 | sls_mu.Lock(); |
| 63 | int x = sls_guardby_var; |
| 64 | sls_mu.Unlock(); |
| 65 | } |
| 66 | |
| 67 | void sls_fun_6() { |
| 68 | sls_mu.Lock(); |
| 69 | sls_guardby_var = 2; |
| 70 | sls_mu.Unlock(); |
| 71 | } |
| 72 | |
| 73 | void sls_fun_7() { |
| 74 | sls_mu.Lock(); |
| 75 | sls_mu2.Lock(); |
| 76 | sls_mu2.Unlock(); |
| 77 | sls_mu.Unlock(); |
| 78 | } |
| 79 | |
| 80 | void sls_fun_8() { |
| 81 | sls_mu.Lock(); |
| 82 | if (getBool()) |
| 83 | sls_mu.Unlock(); |
| 84 | else |
| 85 | sls_mu.Unlock(); |
| 86 | } |
| 87 | |
| 88 | void sls_fun_9() { |
| 89 | if (getBool()) |
| 90 | sls_mu.Lock(); |
| 91 | else |
| 92 | sls_mu.Lock(); |
| 93 | sls_mu.Unlock(); |
| 94 | } |
| 95 | |
| 96 | void sls_fun_good_6() { |
| 97 | if (getBool()) { |
| 98 | sls_mu.Lock(); |
| 99 | } else { |
| 100 | if (getBool()) { |
| 101 | getBool(); // EMPTY |
| 102 | } else { |
| 103 | getBool(); // EMPTY |
| 104 | } |
| 105 | sls_mu.Lock(); |
| 106 | } |
| 107 | sls_mu.Unlock(); |
| 108 | } |
| 109 | |
| 110 | void sls_fun_good_7() { |
| 111 | sls_mu.Lock(); |
| 112 | while (getBool()) { |
| 113 | sls_mu.Unlock(); |
| 114 | if (getBool()) { |
| 115 | if (getBool()) { |
| 116 | sls_mu.Lock(); |
| 117 | continue; |
| 118 | } |
| 119 | } |
| 120 | sls_mu.Lock(); |
| 121 | } |
| 122 | sls_mu.Unlock(); |
| 123 | } |
| 124 | |
Caitlin Sadowski | 194418f | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 125 | void sls_fun_good_8() { |
| 126 | sls_mw.MyLock(); |
| 127 | sls_mw.mu.Unlock(); |
| 128 | } |
| 129 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 130 | void sls_fun_bad_1() { |
| 131 | sls_mu.Unlock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 132 | // expected-warning{{unlocking 'sls_mu' that was not locked}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void sls_fun_bad_2() { |
| 136 | sls_mu.Lock(); |
| 137 | sls_mu.Lock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 138 | // expected-warning{{locking 'sls_mu' that is already locked}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 139 | sls_mu.Unlock(); |
| 140 | } |
| 141 | |
| 142 | void sls_fun_bad_3() { |
| 143 | sls_mu.Lock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 144 | // expected-warning{{mutex 'sls_mu' is still held at the end of function 'sls_fun_bad_3'}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void sls_fun_bad_4() { |
| 148 | if (getBool()) |
| 149 | sls_mu.Lock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 150 | // expected-warning{{mutex 'sls_mu' is still held at the end of its scope}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 151 | else |
| 152 | sls_mu2.Lock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 153 | // expected-warning{{mutex 'sls_mu2' is still held at the end of its scope}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void sls_fun_bad_5() { |
| 157 | sls_mu.Lock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 158 | // expected-warning{{mutex 'sls_mu' is still held at the end of its scope}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 159 | if (getBool()) |
| 160 | sls_mu.Unlock(); |
| 161 | } |
| 162 | |
| 163 | void sls_fun_bad_6() { |
| 164 | if (getBool()) { |
| 165 | sls_mu.Lock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 166 | // expected-warning{{mutex 'sls_mu' is still held at the end of its scope}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 167 | } else { |
| 168 | if (getBool()) { |
| 169 | getBool(); // EMPTY |
| 170 | } else { |
| 171 | getBool(); // EMPTY |
| 172 | } |
| 173 | } |
| 174 | sls_mu.Unlock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 175 | // expected-warning{{unlocking 'sls_mu' that was not locked}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | void sls_fun_bad_7() { |
Caitlin Sadowski | 4e4bc75 | 2011-09-15 17:25:19 +0000 | [diff] [blame^] | 179 | sls_mu.Lock(); // \ |
| 180 | // expected-warning{{expecting lock on 'sls_mu' to be held at start of each loop}} |
| 181 | while (getBool()) { |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 182 | sls_mu.Unlock(); |
| 183 | if (getBool()) { |
| 184 | if (getBool()) { |
| 185 | continue; |
| 186 | } |
| 187 | } |
| 188 | sls_mu.Lock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 189 | // expected-warning{{mutex 'sls_mu' is still held at the end of its scope}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 190 | } |
| 191 | sls_mu.Unlock(); |
| 192 | } |
| 193 | |
| 194 | void sls_fun_bad_8() { |
Caitlin Sadowski | 4e4bc75 | 2011-09-15 17:25:19 +0000 | [diff] [blame^] | 195 | sls_mu.Lock(); // \ |
| 196 | // expected-warning{{expecting lock on 'sls_mu' to be held at start of each loop}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 197 | do { |
Caitlin Sadowski | 4e4bc75 | 2011-09-15 17:25:19 +0000 | [diff] [blame^] | 198 | sls_mu.Unlock(); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 199 | } while (getBool()); |
| 200 | } |
| 201 | |
| 202 | void sls_fun_bad_9() { |
| 203 | do { |
| 204 | sls_mu.Lock(); // \ |
Caitlin Sadowski | 4e4bc75 | 2011-09-15 17:25:19 +0000 | [diff] [blame^] | 205 | // expected-warning{{expecting lock on 'sls_mu' to be held at start of each loop}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 206 | } while (getBool()); |
| 207 | sls_mu.Unlock(); |
| 208 | } |
| 209 | |
| 210 | void sls_fun_bad_10() { |
| 211 | sls_mu.Lock(); // \ |
Caitlin Sadowski | 4e4bc75 | 2011-09-15 17:25:19 +0000 | [diff] [blame^] | 212 | // expected-warning{{mutex 'sls_mu' is still held at the end of function 'sls_fun_bad_10'}} \ |
| 213 | // expected-warning{{expecting lock on 'sls_mu' to be held at start of each loop}} |
| 214 | while(getBool()) { |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 215 | sls_mu.Unlock(); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | void sls_fun_bad_11() { |
| 220 | while (getBool()) { |
| 221 | sls_mu.Lock(); // \ |
Caitlin Sadowski | 4e4bc75 | 2011-09-15 17:25:19 +0000 | [diff] [blame^] | 222 | // expected-warning{{expecting lock on 'sls_mu' to be held at start of each loop}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 223 | } |
| 224 | sls_mu.Unlock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 225 | // expected-warning{{unlocking 'sls_mu' that was not locked}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 228 | //-----------------------------------------// |
| 229 | // Handling lock expressions in attribute args |
| 230 | // -------------------------------------------// |
| 231 | |
| 232 | Mutex aa_mu; |
| 233 | |
| 234 | class GlobalLocker { |
| 235 | public: |
| 236 | void globalLock() __attribute__((exclusive_lock_function(aa_mu))); |
| 237 | void globalUnlock() __attribute__((unlock_function(aa_mu))); |
| 238 | }; |
| 239 | |
| 240 | GlobalLocker glock; |
| 241 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 242 | void aa_fun_1() { |
| 243 | glock.globalLock(); |
| 244 | glock.globalUnlock(); |
| 245 | } |
| 246 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 247 | void aa_fun_bad_1() { |
| 248 | glock.globalUnlock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 249 | // expected-warning{{unlocking 'aa_mu' that was not locked}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void aa_fun_bad_2() { |
| 253 | glock.globalLock(); |
| 254 | glock.globalLock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 255 | // expected-warning{{locking 'aa_mu' that is already locked}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 256 | glock.globalUnlock(); |
| 257 | } |
| 258 | |
| 259 | void aa_fun_bad_3() { |
| 260 | glock.globalLock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 261 | // expected-warning{{mutex 'aa_mu' is still held at the end of function 'aa_fun_bad_3'}} |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 262 | } |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 263 | |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 264 | //--------------------------------------------------// |
| 265 | // Regression tests for unusual method names |
| 266 | //--------------------------------------------------// |
| 267 | |
| 268 | Mutex wmu; |
| 269 | |
| 270 | // Test diagnostics for other method names. |
| 271 | class WeirdMethods { |
| 272 | WeirdMethods() { |
| 273 | wmu.Lock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 274 | // expected-warning {{mutex 'wmu' is still held at the end of function 'WeirdMethods'}} |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 275 | } |
| 276 | ~WeirdMethods() { |
| 277 | wmu.Lock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 278 | // expected-warning {{mutex 'wmu' is still held at the end of function '~WeirdMethods'}} |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 279 | } |
| 280 | void operator++() { |
| 281 | wmu.Lock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 282 | // expected-warning {{mutex 'wmu' is still held at the end of function 'operator++'}} |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 283 | } |
| 284 | operator int*() { |
| 285 | wmu.Lock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 286 | // expected-warning {{mutex 'wmu' is still held at the end of function 'operator int *'}} |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 287 | return 0; |
| 288 | } |
| 289 | }; |
| 290 | |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 291 | //-----------------------------------------------// |
| 292 | // Errors for guarded by or guarded var variables |
| 293 | // ----------------------------------------------// |
| 294 | |
| 295 | int *pgb_gvar __attribute__((pt_guarded_var)); |
| 296 | int *pgb_var __attribute__((pt_guarded_by(sls_mu))); |
| 297 | |
| 298 | class PGBFoo { |
| 299 | public: |
| 300 | int x; |
| 301 | int *pgb_field __attribute__((guarded_by(sls_mu2))) |
| 302 | __attribute__((pt_guarded_by(sls_mu))); |
| 303 | void testFoo() { |
| 304 | pgb_field = &x; // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 305 | // expected-warning {{writing variable 'pgb_field' requires lock on 'sls_mu2' to be held exclusively}} |
| 306 | *pgb_field = x; // expected-warning {{reading variable 'pgb_field' requires lock on 'sls_mu2' to be held}} \ |
| 307 | // expected-warning {{writing the value pointed to by 'pgb_field' requires lock on 'sls_mu' to be held exclusively}} |
| 308 | x = *pgb_field; // expected-warning {{reading variable 'pgb_field' requires lock on 'sls_mu2' to be held}} \ |
| 309 | // expected-warning {{reading the value pointed to by 'pgb_field' requires lock on 'sls_mu' to be held}} |
| 310 | (*pgb_field)++; // expected-warning {{reading variable 'pgb_field' requires lock on 'sls_mu2' to be held}} \ |
| 311 | // expected-warning {{writing the value pointed to by 'pgb_field' requires lock on 'sls_mu' to be held exclusively}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 312 | } |
| 313 | }; |
| 314 | |
| 315 | class GBFoo { |
| 316 | public: |
| 317 | int gb_field __attribute__((guarded_by(sls_mu))); |
| 318 | |
| 319 | void testFoo() { |
| 320 | gb_field = 0; // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 321 | // expected-warning {{writing variable 'gb_field' requires lock on 'sls_mu' to be held exclusively}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 322 | } |
Caitlin Sadowski | af37061 | 2011-09-08 18:35:21 +0000 | [diff] [blame] | 323 | |
| 324 | void testNoAnal() __attribute__((no_thread_safety_analysis)) { |
| 325 | gb_field = 0; |
| 326 | } |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 327 | }; |
| 328 | |
| 329 | GBFoo GlobalGBFoo __attribute__((guarded_by(sls_mu))); |
| 330 | |
| 331 | void gb_fun_0() { |
| 332 | sls_mu.Lock(); |
| 333 | int x = *pgb_var; |
| 334 | sls_mu.Unlock(); |
| 335 | } |
| 336 | |
| 337 | void gb_fun_1() { |
| 338 | sls_mu.Lock(); |
| 339 | *pgb_var = 2; |
| 340 | sls_mu.Unlock(); |
| 341 | } |
| 342 | |
| 343 | void gb_fun_2() { |
| 344 | int x; |
| 345 | pgb_var = &x; |
| 346 | } |
| 347 | |
| 348 | void gb_fun_3() { |
| 349 | int *x = pgb_var; |
| 350 | } |
| 351 | |
| 352 | void gb_bad_0() { |
| 353 | sls_guard_var = 1; // \ |
Caitlin Sadowski | df8327c | 2011-09-14 20:09:09 +0000 | [diff] [blame] | 354 | // expected-warning{{writing variable 'sls_guard_var' requires lock on any mutex to be held exclusively}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | void gb_bad_1() { |
| 358 | int x = sls_guard_var; // \ |
Caitlin Sadowski | df8327c | 2011-09-14 20:09:09 +0000 | [diff] [blame] | 359 | // expected-warning{{reading variable 'sls_guard_var' requires lock on any mutex to be held}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | void gb_bad_2() { |
| 363 | sls_guardby_var = 1; // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 364 | // expected-warning {{writing variable 'sls_guardby_var' requires lock on 'sls_mu' to be held exclusively}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | void gb_bad_3() { |
| 368 | int x = sls_guardby_var; // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 369 | // expected-warning {{reading variable 'sls_guardby_var' requires lock on 'sls_mu' to be held}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | void gb_bad_4() { |
| 373 | *pgb_gvar = 1; // \ |
Caitlin Sadowski | df8327c | 2011-09-14 20:09:09 +0000 | [diff] [blame] | 374 | // expected-warning {{writing the value pointed to by 'pgb_gvar' requires lock on any mutex to be held exclusively}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | void gb_bad_5() { |
| 378 | int x = *pgb_gvar; // \ |
Caitlin Sadowski | df8327c | 2011-09-14 20:09:09 +0000 | [diff] [blame] | 379 | // expected-warning {{reading the value pointed to by 'pgb_gvar' requires lock on any mutex to be held}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | void gb_bad_6() { |
| 383 | *pgb_var = 1; // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 384 | // expected-warning {{writing the value pointed to by 'pgb_var' requires lock on 'sls_mu' to be held exclusively}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | void gb_bad_7() { |
| 388 | int x = *pgb_var; // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 389 | // expected-warning {{reading the value pointed to by 'pgb_var' requires lock on 'sls_mu' to be held}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | void gb_bad_8() { |
| 393 | GBFoo G; |
| 394 | G.gb_field = 0; // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 395 | // expected-warning {{writing variable 'gb_field' requires lock on 'sls_mu'}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | void gb_bad_9() { |
| 399 | sls_guard_var++; // \ |
Caitlin Sadowski | df8327c | 2011-09-14 20:09:09 +0000 | [diff] [blame] | 400 | // expected-warning{{writing variable 'sls_guard_var' requires lock on any mutex to be held exclusively}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 401 | sls_guard_var--; // \ |
Caitlin Sadowski | df8327c | 2011-09-14 20:09:09 +0000 | [diff] [blame] | 402 | // expected-warning{{writing variable 'sls_guard_var' requires lock on any mutex to be held exclusively}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 403 | ++sls_guard_var; // \ |
Caitlin Sadowski | df8327c | 2011-09-14 20:09:09 +0000 | [diff] [blame] | 404 | // expected-warning{{writing variable 'sls_guard_var' requires lock on any mutex to be held exclusively}} |
Caitlin Sadowski | a49d1d8 | 2011-09-09 16:07:55 +0000 | [diff] [blame] | 405 | --sls_guard_var;// \ |
Caitlin Sadowski | df8327c | 2011-09-14 20:09:09 +0000 | [diff] [blame] | 406 | // expected-warning{{writing variable 'sls_guard_var' requires lock on any mutex to be held exclusively}} |
Caitlin Sadowski | 05b436e | 2011-08-29 22:27:51 +0000 | [diff] [blame] | 407 | } |
Caitlin Sadowski | b4d0a96 | 2011-08-29 17:12:27 +0000 | [diff] [blame] | 408 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 409 | //-----------------------------------------------// |
| 410 | // Warnings on variables with late parsed attributes |
| 411 | // ----------------------------------------------// |
| 412 | |
| 413 | class LateFoo { |
| 414 | public: |
| 415 | int a __attribute__((guarded_by(mu))); |
| 416 | int b; |
| 417 | |
| 418 | void foo() __attribute__((exclusive_locks_required(mu))) { } |
| 419 | |
| 420 | void test() { |
| 421 | a = 0; // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 422 | // expected-warning{{writing variable 'a' requires lock on 'mu' to be held exclusively}} |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 423 | b = a; // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 424 | // expected-warning {{reading variable 'a' requires lock on 'mu' to be held}} |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 425 | c = 0; // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 426 | // expected-warning {{writing variable 'c' requires lock on 'mu' to be held exclusively}} |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | int c __attribute__((guarded_by(mu))); |
| 430 | |
| 431 | Mutex mu; |
| 432 | }; |
| 433 | |
Caitlin Sadowski | 99107eb | 2011-09-09 16:21:55 +0000 | [diff] [blame] | 434 | class LateBar { |
| 435 | public: |
| 436 | int a_ __attribute__((guarded_by(mu1_))); |
| 437 | int b_; |
| 438 | int *q __attribute__((pt_guarded_by(mu))); |
| 439 | Mutex mu1_; |
| 440 | Mutex mu; |
| 441 | LateFoo Foo; |
| 442 | LateFoo Foo2; |
| 443 | LateFoo *FooPointer; |
| 444 | }; |
| 445 | |
| 446 | LateBar b1, *b3; |
| 447 | |
| 448 | void late_0() { |
| 449 | LateFoo FooA; |
| 450 | LateFoo FooB; |
| 451 | FooA.mu.Lock(); |
| 452 | FooA.a = 5; |
| 453 | FooA.mu.Unlock(); |
| 454 | } |
| 455 | |
| 456 | void late_1() { |
| 457 | LateBar BarA; |
| 458 | BarA.FooPointer->mu.Lock(); |
| 459 | BarA.FooPointer->a = 2; |
| 460 | BarA.FooPointer->mu.Unlock(); |
| 461 | } |
| 462 | |
| 463 | void late_bad_0() { |
| 464 | LateFoo fooA; |
| 465 | LateFoo fooB; |
| 466 | fooA.mu.Lock(); |
| 467 | fooB.a = 5; // \ |
| 468 | // expected-warning{{writing variable 'a' requires lock on 'mu' to be held exclusively}} |
| 469 | fooA.mu.Unlock(); |
| 470 | } |
| 471 | |
| 472 | void late_bad_1() { |
| 473 | Mutex mu; |
| 474 | mu.Lock(); |
| 475 | b1.mu1_.Lock(); |
| 476 | int res = b1.a_ + b3->b_; |
| 477 | b3->b_ = *b1.q; // \ |
| 478 | // expected-warning{{reading the value pointed to by 'q' requires lock on 'mu' to be held}} |
| 479 | b1.mu1_.Unlock(); |
| 480 | b1.b_ = res; |
| 481 | mu.Unlock(); |
| 482 | } |
| 483 | |
| 484 | void late_bad_2() { |
| 485 | LateBar BarA; |
| 486 | BarA.FooPointer->mu.Lock(); |
| 487 | BarA.Foo.a = 2; // \ |
| 488 | // expected-warning{{writing variable 'a' requires lock on 'mu' to be held exclusively}} |
| 489 | BarA.FooPointer->mu.Unlock(); |
| 490 | } |
| 491 | |
| 492 | void late_bad_3() { |
| 493 | LateBar BarA; |
| 494 | BarA.Foo.mu.Lock(); |
| 495 | BarA.FooPointer->a = 2; // \ |
| 496 | // expected-warning{{writing variable 'a' requires lock on 'mu' to be held exclusively}} |
| 497 | BarA.Foo.mu.Unlock(); |
| 498 | } |
| 499 | |
| 500 | void late_bad_4() { |
| 501 | LateBar BarA; |
| 502 | BarA.Foo.mu.Lock(); |
| 503 | BarA.Foo2.a = 2; // \ |
| 504 | // expected-warning{{writing variable 'a' requires lock on 'mu' to be held exclusively}} |
| 505 | BarA.Foo.mu.Unlock(); |
| 506 | } |
| 507 | |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 508 | //-----------------------------------------------// |
| 509 | // Extra warnings for shared vs. exclusive locks |
| 510 | // ----------------------------------------------// |
| 511 | |
| 512 | void shared_fun_0() { |
| 513 | sls_mu.Lock(); |
| 514 | do { |
| 515 | sls_mu.Unlock(); |
| 516 | sls_mu.Lock(); |
| 517 | } while (getBool()); |
| 518 | sls_mu.Unlock(); |
| 519 | } |
| 520 | |
| 521 | void shared_fun_1() { |
Caitlin Sadowski | 4e4bc75 | 2011-09-15 17:25:19 +0000 | [diff] [blame^] | 522 | sls_mu.ReaderLock(); // \ |
| 523 | // expected-warning {{lock 'sls_mu' is exclusive and shared in the same scope}} |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 524 | do { |
| 525 | sls_mu.Unlock(); |
Caitlin Sadowski | 4e4bc75 | 2011-09-15 17:25:19 +0000 | [diff] [blame^] | 526 | sls_mu.Lock(); // \ |
| 527 | // expected-note {{the other lock of mutex 'sls_mu' is here}} |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 528 | } while (getBool()); |
| 529 | sls_mu.Unlock(); |
| 530 | } |
| 531 | |
| 532 | void shared_fun_3() { |
| 533 | if (getBool()) |
| 534 | sls_mu.Lock(); |
| 535 | else |
| 536 | sls_mu.Lock(); |
| 537 | *pgb_var = 1; |
| 538 | sls_mu.Unlock(); |
| 539 | } |
| 540 | |
| 541 | void shared_fun_4() { |
| 542 | if (getBool()) |
| 543 | sls_mu.ReaderLock(); |
| 544 | else |
| 545 | sls_mu.ReaderLock(); |
| 546 | int x = sls_guardby_var; |
| 547 | sls_mu.Unlock(); |
| 548 | } |
| 549 | |
| 550 | void shared_fun_8() { |
| 551 | if (getBool()) |
| 552 | sls_mu.Lock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 553 | // expected-warning {{lock 'sls_mu' is exclusive and shared in the same scope}} |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 554 | else |
| 555 | sls_mu.ReaderLock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 556 | // expected-note {{the other lock of mutex 'sls_mu' is here}} |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 557 | sls_mu.Unlock(); |
| 558 | } |
| 559 | |
| 560 | void shared_bad_0() { |
Caitlin Sadowski | 4e4bc75 | 2011-09-15 17:25:19 +0000 | [diff] [blame^] | 561 | sls_mu.Lock(); // \ |
| 562 | // expected-warning {{lock 'sls_mu' is exclusive and shared in the same scope}} |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 563 | do { |
| 564 | sls_mu.Unlock(); |
Caitlin Sadowski | 4e4bc75 | 2011-09-15 17:25:19 +0000 | [diff] [blame^] | 565 | sls_mu.ReaderLock(); // \ |
| 566 | // expected-note {{the other lock of mutex 'sls_mu' is here}} |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 567 | } while (getBool()); |
| 568 | sls_mu.Unlock(); |
| 569 | } |
| 570 | |
| 571 | void shared_bad_1() { |
| 572 | if (getBool()) |
| 573 | sls_mu.Lock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 574 | // expected-warning {{lock 'sls_mu' is exclusive and shared in the same scope}} |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 575 | else |
| 576 | sls_mu.ReaderLock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 577 | // expected-note {{the other lock of mutex 'sls_mu' is here}} |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 578 | *pgb_var = 1; |
| 579 | sls_mu.Unlock(); |
| 580 | } |
| 581 | |
| 582 | void shared_bad_2() { |
| 583 | if (getBool()) |
| 584 | sls_mu.ReaderLock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 585 | // expected-warning {{lock 'sls_mu' is exclusive and shared in the same scope}} |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 586 | else |
| 587 | sls_mu.Lock(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 588 | // expected-note {{the other lock of mutex 'sls_mu' is here}} |
Caitlin Sadowski | a53257c | 2011-09-08 18:19:38 +0000 | [diff] [blame] | 589 | *pgb_var = 1; |
| 590 | sls_mu.Unlock(); |
| 591 | } |
Caitlin Sadowski | 978191e | 2011-09-08 18:27:31 +0000 | [diff] [blame] | 592 | |
| 593 | // FIXME: Add support for functions (not only methods) |
| 594 | class LRBar { |
| 595 | public: |
| 596 | void aa_elr_fun() __attribute__((exclusive_locks_required(aa_mu))); |
| 597 | void aa_elr_fun_s() __attribute__((shared_locks_required(aa_mu))); |
| 598 | void le_fun() __attribute__((locks_excluded(sls_mu))); |
| 599 | }; |
| 600 | |
| 601 | class LRFoo { |
| 602 | public: |
| 603 | void test() __attribute__((exclusive_locks_required(sls_mu))); |
| 604 | void testShared() __attribute__((shared_locks_required(sls_mu2))); |
| 605 | }; |
| 606 | |
| 607 | void elr_fun() __attribute__((exclusive_locks_required(sls_mu))); |
| 608 | void elr_fun() {} |
| 609 | |
| 610 | LRFoo MyLRFoo; |
| 611 | LRBar Bar; |
| 612 | |
| 613 | void es_fun_0() { |
| 614 | aa_mu.Lock(); |
| 615 | Bar.aa_elr_fun(); |
| 616 | aa_mu.Unlock(); |
| 617 | } |
| 618 | |
| 619 | void es_fun_1() { |
| 620 | aa_mu.Lock(); |
| 621 | Bar.aa_elr_fun_s(); |
| 622 | aa_mu.Unlock(); |
| 623 | } |
| 624 | |
| 625 | void es_fun_2() { |
| 626 | aa_mu.ReaderLock(); |
| 627 | Bar.aa_elr_fun_s(); |
| 628 | aa_mu.Unlock(); |
| 629 | } |
| 630 | |
| 631 | void es_fun_3() { |
| 632 | sls_mu.Lock(); |
| 633 | MyLRFoo.test(); |
| 634 | sls_mu.Unlock(); |
| 635 | } |
| 636 | |
| 637 | void es_fun_4() { |
| 638 | sls_mu2.Lock(); |
| 639 | MyLRFoo.testShared(); |
| 640 | sls_mu2.Unlock(); |
| 641 | } |
| 642 | |
| 643 | void es_fun_5() { |
| 644 | sls_mu2.ReaderLock(); |
| 645 | MyLRFoo.testShared(); |
| 646 | sls_mu2.Unlock(); |
| 647 | } |
| 648 | |
| 649 | void es_fun_6() { |
| 650 | Bar.le_fun(); |
| 651 | } |
| 652 | |
| 653 | void es_fun_7() { |
| 654 | sls_mu.Lock(); |
| 655 | elr_fun(); |
| 656 | sls_mu.Unlock(); |
| 657 | } |
| 658 | |
Caitlin Sadowski | af37061 | 2011-09-08 18:35:21 +0000 | [diff] [blame] | 659 | void es_fun_8() __attribute__((no_thread_safety_analysis)); |
| 660 | |
| 661 | void es_fun_8() { |
| 662 | Bar.aa_elr_fun_s(); |
| 663 | } |
| 664 | |
Caitlin Sadowski | 978191e | 2011-09-08 18:27:31 +0000 | [diff] [blame] | 665 | void es_bad_0() { |
| 666 | Bar.aa_elr_fun(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 667 | // 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] | 668 | } |
| 669 | |
| 670 | void es_bad_1() { |
| 671 | aa_mu.ReaderLock(); |
| 672 | Bar.aa_elr_fun(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 673 | // 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] | 674 | aa_mu.Unlock(); |
| 675 | } |
| 676 | |
| 677 | void es_bad_2() { |
| 678 | Bar.aa_elr_fun_s(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 679 | // 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] | 680 | } |
| 681 | |
| 682 | void es_bad_3() { |
| 683 | MyLRFoo.test(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 684 | // expected-warning {{calling function 'test' requires exclusive lock on 'sls_mu'}} |
Caitlin Sadowski | 978191e | 2011-09-08 18:27:31 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | void es_bad_4() { |
| 688 | MyLRFoo.testShared(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 689 | // expected-warning {{calling function 'testShared' requires shared lock on 'sls_mu2'}} |
Caitlin Sadowski | 978191e | 2011-09-08 18:27:31 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | void es_bad_5() { |
| 693 | sls_mu.ReaderLock(); |
| 694 | MyLRFoo.test(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 695 | // expected-warning {{calling function 'test' requires exclusive lock on 'sls_mu'}} |
Caitlin Sadowski | 978191e | 2011-09-08 18:27:31 +0000 | [diff] [blame] | 696 | sls_mu.Unlock(); |
| 697 | } |
| 698 | |
| 699 | void es_bad_6() { |
| 700 | sls_mu.Lock(); |
| 701 | Bar.le_fun(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 702 | // expected-warning {{cannot call function 'le_fun' while holding mutex 'sls_mu'}} |
Caitlin Sadowski | 978191e | 2011-09-08 18:27:31 +0000 | [diff] [blame] | 703 | sls_mu.Unlock(); |
| 704 | } |
| 705 | |
| 706 | void es_bad_7() { |
| 707 | sls_mu.ReaderLock(); |
| 708 | Bar.le_fun(); // \ |
Caitlin Sadowski | 8bccabe | 2011-09-08 21:52:50 +0000 | [diff] [blame] | 709 | // expected-warning {{cannot call function 'le_fun' while holding mutex 'sls_mu'}} |
Caitlin Sadowski | 978191e | 2011-09-08 18:27:31 +0000 | [diff] [blame] | 710 | sls_mu.Unlock(); |
| 711 | } |
Caitlin Sadowski | 194418f | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 712 | |
| 713 | //-----------------------------------------------// |
| 714 | // Unparseable lock expressions |
| 715 | // ----------------------------------------------// |
| 716 | |
| 717 | Mutex UPmu; |
| 718 | // FIXME: add support for lock expressions involving arrays. |
| 719 | Mutex mua[5]; |
| 720 | |
| 721 | int x __attribute__((guarded_by(UPmu = sls_mu))); // \ |
| 722 | // expected-warning{{cannot resolve lock expression to a specific lockable object}} |
| 723 | int y __attribute__((guarded_by(mua[0]))); // \ |
| 724 | // expected-warning{{cannot resolve lock expression to a specific lockable object}} |
| 725 | |
| 726 | |
| 727 | void testUnparse() { |
| 728 | // no errors, since the lock expressions are not resolved |
| 729 | x = 5; |
| 730 | y = 5; |
| 731 | } |
| 732 | |
| 733 | void testUnparse2() { |
| 734 | mua[0].Lock(); // \ |
| 735 | // expected-warning{{cannot resolve lock expression to a specific lockable object}} |
| 736 | (&(mua[0]) + 4)->Lock(); // \ |
| 737 | // expected-warning{{cannot resolve lock expression to a specific lockable object}} |
| 738 | } |
| 739 | |