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