1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wthread-safety -Wthread-safety-beta -Wno-thread-safety-negative -fcxx-exceptions -DUSE_ASSERT_CAPABILITY=0 %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wthread-safety -Wthread-safety-beta -Wno-thread-safety-negative -fcxx-exceptions -DUSE_ASSERT_CAPABILITY=1 %s
3 
4 // FIXME: should also run  %clang_cc1 -fsyntax-only -verify -Wthread-safety -std=c++11 -Wc++98-compat %s
5 // FIXME: should also run  %clang_cc1 -fsyntax-only -verify -Wthread-safety %s
6 
7 #define LOCKABLE             __attribute__((lockable))
8 #define SCOPED_LOCKABLE      __attribute__((scoped_lockable))
9 #define GUARDED_BY(x)        __attribute__((guarded_by(x)))
10 #define GUARDED_VAR          __attribute__((guarded_var))
11 #define PT_GUARDED_BY(x)     __attribute__((pt_guarded_by(x)))
12 #define PT_GUARDED_VAR       __attribute__((pt_guarded_var))
13 #define ACQUIRED_AFTER(...)  __attribute__((acquired_after(__VA_ARGS__)))
14 #define ACQUIRED_BEFORE(...) __attribute__((acquired_before(__VA_ARGS__)))
15 #define EXCLUSIVE_LOCK_FUNCTION(...)    __attribute__((exclusive_lock_function(__VA_ARGS__)))
16 #define SHARED_LOCK_FUNCTION(...)       __attribute__((shared_lock_function(__VA_ARGS__)))
17 
18 #if USE_ASSERT_CAPABILITY
19 #define ASSERT_EXCLUSIVE_LOCK(...)      __attribute__((assert_capability(__VA_ARGS__)))
20 #define ASSERT_SHARED_LOCK(...)         __attribute__((assert_shared_capability(__VA_ARGS__)))
21 #else
22 #define ASSERT_EXCLUSIVE_LOCK(...)      __attribute__((assert_exclusive_lock(__VA_ARGS__)))
23 #define ASSERT_SHARED_LOCK(...)         __attribute__((assert_shared_lock(__VA_ARGS__)))
24 #endif
25 
26 #define EXCLUSIVE_TRYLOCK_FUNCTION(...) __attribute__((exclusive_trylock_function(__VA_ARGS__)))
27 #define SHARED_TRYLOCK_FUNCTION(...)    __attribute__((shared_trylock_function(__VA_ARGS__)))
28 #define UNLOCK_FUNCTION(...)            __attribute__((unlock_function(__VA_ARGS__)))
29 #define EXCLUSIVE_UNLOCK_FUNCTION(...)  __attribute__((release_capability(__VA_ARGS__)))
30 #define SHARED_UNLOCK_FUNCTION(...)     __attribute__((release_shared_capability(__VA_ARGS__)))
31 #define LOCK_RETURNED(x)                __attribute__((lock_returned(x)))
32 #define LOCKS_EXCLUDED(...)             __attribute__((locks_excluded(__VA_ARGS__)))
33 #define EXCLUSIVE_LOCKS_REQUIRED(...)   __attribute__((exclusive_locks_required(__VA_ARGS__)))
34 #define SHARED_LOCKS_REQUIRED(...)      __attribute__((shared_locks_required(__VA_ARGS__)))
35 #define NO_THREAD_SAFETY_ANALYSIS       __attribute__((no_thread_safety_analysis))
36 
37 
38 class LOCKABLE Mutex {
39  public:
40   void Lock() __attribute__((exclusive_lock_function));
41   void ReaderLock() __attribute__((shared_lock_function));
42   void Unlock() __attribute__((unlock_function));
43   bool TryLock() __attribute__((exclusive_trylock_function(true)));
44   bool ReaderTryLock() __attribute__((shared_trylock_function(true)));
45   void LockWhen(const int &cond) __attribute__((exclusive_lock_function));
46 
47   // for negative capabilities
48   const Mutex& operator!() const { return *this; }
49 
50   void AssertHeld()       ASSERT_EXCLUSIVE_LOCK();
51   void AssertReaderHeld() ASSERT_SHARED_LOCK();
52 };
53 
54 class SCOPED_LOCKABLE MutexLock {
55  public:
56   MutexLock(Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu);
57   MutexLock(Mutex *mu, bool adopt) EXCLUSIVE_LOCKS_REQUIRED(mu);
58   ~MutexLock() UNLOCK_FUNCTION();
59 };
60 
61 class SCOPED_LOCKABLE ReaderMutexLock {
62  public:
63   ReaderMutexLock(Mutex *mu) SHARED_LOCK_FUNCTION(mu);
64   ReaderMutexLock(Mutex *mu, bool adopt) SHARED_LOCKS_REQUIRED(mu);
65   ~ReaderMutexLock() UNLOCK_FUNCTION();
66 };
67 
68 class SCOPED_LOCKABLE ReleasableMutexLock {
69  public:
70   ReleasableMutexLock(Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu);
71   ~ReleasableMutexLock() UNLOCK_FUNCTION();
72 
73   void Release() UNLOCK_FUNCTION();
74 };
75 
76 class __attribute__((scoped_lockable)) DoubleMutexLock {
77 public:
78   DoubleMutexLock(Mutex *mu1, Mutex *mu2)
79       __attribute__((exclusive_lock_function(mu1, mu2)));
80   ~DoubleMutexLock() __attribute__((unlock_function));
81 };
82 
83 // The universal lock, written "*", allows checking to be selectively turned
84 // off for a particular piece of code.
85 void beginNoWarnOnReads()  SHARED_LOCK_FUNCTION("*");
86 void endNoWarnOnReads()    UNLOCK_FUNCTION("*");
87 void beginNoWarnOnWrites() EXCLUSIVE_LOCK_FUNCTION("*");
88 void endNoWarnOnWrites()   UNLOCK_FUNCTION("*");
89 
90 
91 // For testing handling of smart pointers.
92 template<class T>
93 class SmartPtr {
94 public:
95   SmartPtr(T* p) : ptr_(p) { }
96   SmartPtr(const SmartPtr<T>& p) : ptr_(p.ptr_) { }
97   ~SmartPtr();
98 
99   T* get()        const { return ptr_; }
100   T* operator->() const { return ptr_; }
101   T& operator*()  const { return *ptr_; }
102   T& operator[](int i) const { return ptr_[i]; }
103 
104 private:
105   T* ptr_;
106 };
107 
108 
109 // For testing destructor calls and cleanup.
110 class MyString {
111 public:
112   MyString(const char* s);
113   ~MyString();
114 };
115 
116 
117 // For testing operator overloading
118 template <class K, class T>
119 class MyMap {
120 public:
121   T& operator[](const K& k);
122 };
123 
124 
125 // For testing handling of containers.
126 template <class T>
127 class MyContainer {
128 public:
129   MyContainer();
130 
131   typedef T* iterator;
132   typedef const T* const_iterator;
133 
134   T* begin();
135   T* end();
136 
137   const T* cbegin();
138   const T* cend();
139 
140   T&       operator[](int i);
141   const T& operator[](int i) const;
142 
143 private:
144   T* ptr_;
145 };
146 
147 
148 
149 Mutex sls_mu;
150 
151 Mutex sls_mu2 __attribute__((acquired_after(sls_mu)));
152 int sls_guard_var __attribute__((guarded_var)) = 0;
153 int sls_guardby_var __attribute__((guarded_by(sls_mu))) = 0;
154 
155 bool getBool();
156 
157 class MutexWrapper {
158 public:
159    Mutex mu;
160    int x __attribute__((guarded_by(mu)));
161    void MyLock() __attribute__((exclusive_lock_function(mu)));
162 };
163 
164 MutexWrapper sls_mw;
165 
166 void sls_fun_0() {
167   sls_mw.mu.Lock();
168   sls_mw.x = 5;
169   sls_mw.mu.Unlock();
170 }
171 
172 void sls_fun_2() {
173   sls_mu.Lock();
174   int x = sls_guard_var;
175   sls_mu.Unlock();
176 }
177 
178 void sls_fun_3() {
179   sls_mu.Lock();
180   sls_guard_var = 2;
181   sls_mu.Unlock();
182 }
183 
184 void sls_fun_4() {
185   sls_mu2.Lock();
186   sls_guard_var = 2;
187   sls_mu2.Unlock();
188 }
189 
190 void sls_fun_5() {
191   sls_mu.Lock();
192   int x = sls_guardby_var;
193   sls_mu.Unlock();
194 }
195 
196 void sls_fun_6() {
197   sls_mu.Lock();
198   sls_guardby_var = 2;
199   sls_mu.Unlock();
200 }
201 
202 void sls_fun_7() {
203   sls_mu.Lock();
204   sls_mu2.Lock();
205   sls_mu2.Unlock();
206   sls_mu.Unlock();
207 }
208 
209 void sls_fun_8() {
210   sls_mu.Lock();
211   if (getBool())
212     sls_mu.Unlock();
213   else
214     sls_mu.Unlock();
215 }
216 
217 void sls_fun_9() {
218   if (getBool())
219     sls_mu.Lock();
220   else
221     sls_mu.Lock();
222   sls_mu.Unlock();
223 }
224 
225 void sls_fun_good_6() {
226   if (getBool()) {
227     sls_mu.Lock();
228   } else {
229     if (getBool()) {
230       getBool(); // EMPTY
231     } else {
232       getBool(); // EMPTY
233     }
234     sls_mu.Lock();
235   }
236   sls_mu.Unlock();
237 }
238 
239 void sls_fun_good_7() {
240   sls_mu.Lock();
241   while (getBool()) {
242     sls_mu.Unlock();
243     if (getBool()) {
244       if (getBool()) {
245         sls_mu.Lock();
246         continue;
247       }
248     }
249     sls_mu.Lock();
250   }
251   sls_mu.Unlock();
252 }
253 
254 void sls_fun_good_8() {
255   sls_mw.MyLock();
256   sls_mw.mu.Unlock();
257 }
258 
259 void sls_fun_bad_1() {
260   sls_mu.Unlock(); // \
261     // expected-warning{{releasing mutex 'sls_mu' that was not held}}
262 }
263 
264 void sls_fun_bad_2() {
265   sls_mu.Lock();
266   sls_mu.Lock(); // \
267     // expected-warning{{acquiring mutex 'sls_mu' that is already held}}
268   sls_mu.Unlock();
269 }
270 
271 void sls_fun_bad_3() {
272   sls_mu.Lock(); // expected-note {{mutex acquired here}}
273 } // expected-warning{{mutex 'sls_mu' is still held at the end of function}}
274 
275 void sls_fun_bad_4() {
276   if (getBool())
277     sls_mu.Lock();  // expected-note{{mutex acquired here}}
278   else
279     sls_mu2.Lock(); // expected-note{{mutex acquired here}}
280 } // expected-warning{{mutex 'sls_mu' is not held on every path through here}}  \
281   // expected-warning{{mutex 'sls_mu2' is not held on every path through here}}
282 
283 void sls_fun_bad_5() {
284   sls_mu.Lock(); // expected-note {{mutex acquired here}}
285   if (getBool())
286     sls_mu.Unlock();
287 } // expected-warning{{mutex 'sls_mu' is not held on every path through here}}
288 
289 void sls_fun_bad_6() {
290   if (getBool()) {
291     sls_mu.Lock(); // expected-note {{mutex acquired here}}
292   } else {
293     if (getBool()) {
294       getBool(); // EMPTY
295     } else {
296       getBool(); // EMPTY
297     }
298   }
299   sls_mu.Unlock(); // \
300     expected-warning{{mutex 'sls_mu' is not held on every path through here}}\
301     expected-warning{{releasing mutex 'sls_mu' that was not held}}
302 }
303 
304 void sls_fun_bad_7() {
305   sls_mu.Lock();
306   while (getBool()) {
307     sls_mu.Unlock();
308     if (getBool()) {
309       if (getBool()) {
310         continue; // \
311         expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}}
312       }
313     }
314     sls_mu.Lock(); // expected-note {{mutex acquired here}}
315   }
316   sls_mu.Unlock();
317 }
318 
319 void sls_fun_bad_8() {
320   sls_mu.Lock(); // expected-note{{mutex acquired here}}
321 
322   do {
323     sls_mu.Unlock(); // expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}}
324   } while (getBool());
325 }
326 
327 void sls_fun_bad_9() {
328   do {
329     sls_mu.Lock();  // \
330       // expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}} \
331       // expected-note{{mutex acquired here}}
332   } while (getBool());
333   sls_mu.Unlock();
334 }
335 
336 void sls_fun_bad_10() {
337   sls_mu.Lock();  // expected-note 2{{mutex acquired here}}
338   while(getBool()) {  // expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}}
339     sls_mu.Unlock();
340   }
341 } // expected-warning{{mutex 'sls_mu' is still held at the end of function}}
342 
343 void sls_fun_bad_11() {
344   while (getBool()) { // \
345       expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}}
346     sls_mu.Lock(); // expected-note {{mutex acquired here}}
347   }
348   sls_mu.Unlock(); // \
349     // expected-warning{{releasing mutex 'sls_mu' that was not held}}
350 }
351 
352 void sls_fun_bad_12() {
353   sls_mu.Lock(); // expected-note {{mutex acquired here}}
354   while (getBool()) {
355     sls_mu.Unlock();
356     if (getBool()) {
357       if (getBool()) {
358         break; // expected-warning{{mutex 'sls_mu' is not held on every path through here}}
359       }
360     }
361     sls_mu.Lock();
362   }
363   sls_mu.Unlock();
364 }
365 
366 //-----------------------------------------//
367 // Handling lock expressions in attribute args
368 // -------------------------------------------//
369 
370 Mutex aa_mu;
371 
372 class GlobalLocker {
373 public:
374   void globalLock() __attribute__((exclusive_lock_function(aa_mu)));
375   void globalUnlock() __attribute__((unlock_function(aa_mu)));
376 };
377 
378 GlobalLocker glock;
379 
380 void aa_fun_1() {
381   glock.globalLock();
382   glock.globalUnlock();
383 }
384 
385 void aa_fun_bad_1() {
386   glock.globalUnlock(); // \
387     // expected-warning{{releasing mutex 'aa_mu' that was not held}}
388 }
389 
390 void aa_fun_bad_2() {
391   glock.globalLock();
392   glock.globalLock(); // \
393     // expected-warning{{acquiring mutex 'aa_mu' that is already held}}
394   glock.globalUnlock();
395 }
396 
397 void aa_fun_bad_3() {
398   glock.globalLock(); // expected-note{{mutex acquired here}}
399 } // expected-warning{{mutex 'aa_mu' is still held at the end of function}}
400 
401 //--------------------------------------------------//
402 // Regression tests for unusual method names
403 //--------------------------------------------------//
404 
405 Mutex wmu;
406 
407 // Test diagnostics for other method names.
408 class WeirdMethods {
409   // FIXME: can't currently check inside constructors and destructors.
410   WeirdMethods() {
411     wmu.Lock(); // EXPECTED-NOTE {{mutex acquired here}}
412   } // EXPECTED-WARNING {{mutex 'wmu' is still held at the end of function}}
413   ~WeirdMethods() {
414     wmu.Lock(); // EXPECTED-NOTE {{mutex acquired here}}
415   } // EXPECTED-WARNING {{mutex 'wmu' is still held at the end of function}}
416   void operator++() {
417     wmu.Lock(); // expected-note {{mutex acquired here}}
418   } // expected-warning {{mutex 'wmu' is still held at the end of function}}
419   operator int*() {
420     wmu.Lock(); // expected-note {{mutex acquired here}}
421     return 0;
422   } // expected-warning {{mutex 'wmu' is still held at the end of function}}
423 };
424 
425 //-----------------------------------------------//
426 // Errors for guarded by or guarded var variables
427 // ----------------------------------------------//
428 
429 int *pgb_gvar __attribute__((pt_guarded_var));
430 int *pgb_var __attribute__((pt_guarded_by(sls_mu)));
431 
432 class PGBFoo {
433  public:
434   int x;
435   int *pgb_field __attribute__((guarded_by(sls_mu2)))
436                  __attribute__((pt_guarded_by(sls_mu)));
437   void testFoo() {
438     pgb_field = &x; // \
439       // expected-warning {{writing variable 'pgb_field' requires holding mutex 'sls_mu2' exclusively}}
440     *pgb_field = x; // expected-warning {{reading variable 'pgb_field' requires holding mutex 'sls_mu2'}} \
441       // expected-warning {{writing the value pointed to by 'pgb_field' requires holding mutex 'sls_mu' exclusively}}
442     x = *pgb_field; // expected-warning {{reading variable 'pgb_field' requires holding mutex 'sls_mu2'}} \
443       // expected-warning {{reading the value pointed to by 'pgb_field' requires holding mutex 'sls_mu'}}
444     (*pgb_field)++; // expected-warning {{reading variable 'pgb_field' requires holding mutex 'sls_mu2'}} \
445       // expected-warning {{writing the value pointed to by 'pgb_field' requires holding mutex 'sls_mu' exclusively}}
446   }
447 };
448 
449 class GBFoo {
450  public:
451   int gb_field __attribute__((guarded_by(sls_mu)));
452 
453   void testFoo() {
454     gb_field = 0; // \
455       // expected-warning {{writing variable 'gb_field' requires holding mutex 'sls_mu' exclusively}}
456   }
457 
458   void testNoAnal() __attribute__((no_thread_safety_analysis)) {
459     gb_field = 0;
460   }
461 };
462 
463 GBFoo GlobalGBFoo __attribute__((guarded_by(sls_mu)));
464 
465 void gb_fun_0() {
466   sls_mu.Lock();
467   int x = *pgb_var;
468   sls_mu.Unlock();
469 }
470 
471 void gb_fun_1() {
472   sls_mu.Lock();
473   *pgb_var = 2;
474   sls_mu.Unlock();
475 }
476 
477 void gb_fun_2() {
478   int x;
479   pgb_var = &x;
480 }
481 
482 void gb_fun_3() {
483   int *x = pgb_var;
484 }
485 
486 void gb_bad_0() {
487   sls_guard_var = 1; // \
488     // expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}}
489 }
490 
491 void gb_bad_1() {
492   int x = sls_guard_var; // \
493     // expected-warning{{reading variable 'sls_guard_var' requires holding any mutex}}
494 }
495 
496 void gb_bad_2() {
497   sls_guardby_var = 1; // \
498     // expected-warning {{writing variable 'sls_guardby_var' requires holding mutex 'sls_mu' exclusively}}
499 }
500 
501 void gb_bad_3() {
502   int x = sls_guardby_var; // \
503     // expected-warning {{reading variable 'sls_guardby_var' requires holding mutex 'sls_mu'}}
504 }
505 
506 void gb_bad_4() {
507   *pgb_gvar = 1; // \
508     // expected-warning {{writing the value pointed to by 'pgb_gvar' requires holding any mutex exclusively}}
509 }
510 
511 void gb_bad_5() {
512   int x = *pgb_gvar; // \
513     // expected-warning {{reading the value pointed to by 'pgb_gvar' requires holding any mutex}}
514 }
515 
516 void gb_bad_6() {
517   *pgb_var = 1; // \
518     // expected-warning {{writing the value pointed to by 'pgb_var' requires holding mutex 'sls_mu' exclusively}}
519 }
520 
521 void gb_bad_7() {
522   int x = *pgb_var; // \
523     // expected-warning {{reading the value pointed to by 'pgb_var' requires holding mutex 'sls_mu'}}
524 }
525 
526 void gb_bad_8() {
527   GBFoo G;
528   G.gb_field = 0; // \
529     // expected-warning {{writing variable 'gb_field' requires holding mutex 'sls_mu'}}
530 }
531 
532 void gb_bad_9() {
533   sls_guard_var++; // \
534     // expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}}
535   sls_guard_var--; // \
536     // expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}}
537   ++sls_guard_var; // \
538     // expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}}
539   --sls_guard_var;// \
540     // expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}}
541 }
542 
543 //-----------------------------------------------//
544 // Warnings on variables with late parsed attributes
545 // ----------------------------------------------//
546 
547 class LateFoo {
548 public:
549   int a __attribute__((guarded_by(mu)));
550   int b;
551 
552   void foo() __attribute__((exclusive_locks_required(mu))) { }
553 
554   void test() {
555     a = 0; // \
556       // expected-warning{{writing variable 'a' requires holding mutex 'mu' exclusively}}
557     b = a; // \
558       // expected-warning {{reading variable 'a' requires holding mutex 'mu'}}
559     c = 0; // \
560       // expected-warning {{writing variable 'c' requires holding mutex 'mu' exclusively}}
561   }
562 
563   int c __attribute__((guarded_by(mu)));
564 
565   Mutex mu;
566 };
567 
568 class LateBar {
569  public:
570   int a_ __attribute__((guarded_by(mu1_)));
571   int b_;
572   int *q __attribute__((pt_guarded_by(mu)));
573   Mutex mu1_;
574   Mutex mu;
575   LateFoo Foo;
576   LateFoo Foo2;
577   LateFoo *FooPointer;
578 };
579 
580 LateBar b1, *b3;
581 
582 void late_0() {
583   LateFoo FooA;
584   LateFoo FooB;
585   FooA.mu.Lock();
586   FooA.a = 5;
587   FooA.mu.Unlock();
588 }
589 
590 void late_1() {
591   LateBar BarA;
592   BarA.FooPointer->mu.Lock();
593   BarA.FooPointer->a = 2;
594   BarA.FooPointer->mu.Unlock();
595 }
596 
597 void late_bad_0() {
598   LateFoo fooA;
599   LateFoo fooB;
600   fooA.mu.Lock();
601   fooB.a = 5; // \
602     // expected-warning{{writing variable 'a' requires holding mutex 'fooB.mu' exclusively}} \
603     // expected-note{{found near match 'fooA.mu'}}
604   fooA.mu.Unlock();
605 }
606 
607 void late_bad_1() {
608   Mutex mu;
609   mu.Lock();
610   b1.mu1_.Lock();
611   int res = b1.a_ + b3->b_;
612   b3->b_ = *b1.q; // \
613     // expected-warning{{reading the value pointed to by 'q' requires holding mutex 'b1.mu'}}
614   b1.mu1_.Unlock();
615   b1.b_ = res;
616   mu.Unlock();
617 }
618 
619 void late_bad_2() {
620   LateBar BarA;
621   BarA.FooPointer->mu.Lock();
622   BarA.Foo.a = 2; // \
623     // expected-warning{{writing variable 'a' requires holding mutex 'BarA.Foo.mu' exclusively}} \
624     // expected-note{{found near match 'BarA.FooPointer->mu'}}
625   BarA.FooPointer->mu.Unlock();
626 }
627 
628 void late_bad_3() {
629   LateBar BarA;
630   BarA.Foo.mu.Lock();
631   BarA.FooPointer->a = 2; // \
632     // expected-warning{{writing variable 'a' requires holding mutex 'BarA.FooPointer->mu' exclusively}} \
633     // expected-note{{found near match 'BarA.Foo.mu'}}
634   BarA.Foo.mu.Unlock();
635 }
636 
637 void late_bad_4() {
638   LateBar BarA;
639   BarA.Foo.mu.Lock();
640   BarA.Foo2.a = 2; // \
641     // expected-warning{{writing variable 'a' requires holding mutex 'BarA.Foo2.mu' exclusively}} \
642     // expected-note{{found near match 'BarA.Foo.mu'}}
643   BarA.Foo.mu.Unlock();
644 }
645 
646 //-----------------------------------------------//
647 // Extra warnings for shared vs. exclusive locks
648 // ----------------------------------------------//
649 
650 void shared_fun_0() {
651   sls_mu.Lock();
652   do {
653     sls_mu.Unlock();
654     sls_mu.Lock();
655   } while (getBool());
656   sls_mu.Unlock();
657 }
658 
659 void shared_fun_1() {
660   sls_mu.ReaderLock(); // \
661     // expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}}
662   do {
663     sls_mu.Unlock();
664     sls_mu.Lock();  // \
665       // expected-note {{the other acquisition of mutex 'sls_mu' is here}}
666   } while (getBool());
667   sls_mu.Unlock();
668 }
669 
670 void shared_fun_3() {
671   if (getBool())
672     sls_mu.Lock();
673   else
674     sls_mu.Lock();
675   *pgb_var = 1;
676   sls_mu.Unlock();
677 }
678 
679 void shared_fun_4() {
680   if (getBool())
681     sls_mu.ReaderLock();
682   else
683     sls_mu.ReaderLock();
684   int x = sls_guardby_var;
685   sls_mu.Unlock();
686 }
687 
688 void shared_fun_8() {
689   if (getBool())
690     sls_mu.Lock(); // \
691       // expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}}
692   else
693     sls_mu.ReaderLock(); // \
694       // expected-note {{the other acquisition of mutex 'sls_mu' is here}}
695   sls_mu.Unlock();
696 }
697 
698 void shared_bad_0() {
699   sls_mu.Lock();  // \
700     // expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}}
701   do {
702     sls_mu.Unlock();
703     sls_mu.ReaderLock();  // \
704       // expected-note {{the other acquisition of mutex 'sls_mu' is here}}
705   } while (getBool());
706   sls_mu.Unlock();
707 }
708 
709 void shared_bad_1() {
710   if (getBool())
711     sls_mu.Lock(); // \
712       // expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}}
713   else
714     sls_mu.ReaderLock(); // \
715       // expected-note {{the other acquisition of mutex 'sls_mu' is here}}
716   *pgb_var = 1;
717   sls_mu.Unlock();
718 }
719 
720 void shared_bad_2() {
721   if (getBool())
722     sls_mu.ReaderLock(); // \
723       // expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}}
724   else
725     sls_mu.Lock(); // \
726       // expected-note {{the other acquisition of mutex 'sls_mu' is here}}
727   *pgb_var = 1;
728   sls_mu.Unlock();
729 }
730 
731 // FIXME: Add support for functions (not only methods)
732 class LRBar {
733  public:
734   void aa_elr_fun() __attribute__((exclusive_locks_required(aa_mu)));
735   void aa_elr_fun_s() __attribute__((shared_locks_required(aa_mu)));
736   void le_fun() __attribute__((locks_excluded(sls_mu)));
737 };
738 
739 class LRFoo {
740  public:
741   void test() __attribute__((exclusive_locks_required(sls_mu)));
742   void testShared() __attribute__((shared_locks_required(sls_mu2)));
743 };
744 
745 void elr_fun() __attribute__((exclusive_locks_required(sls_mu)));
746 void elr_fun() {}
747 
748 LRFoo MyLRFoo;
749 LRBar Bar;
750 
751 void es_fun_0() {
752   aa_mu.Lock();
753   Bar.aa_elr_fun();
754   aa_mu.Unlock();
755 }
756 
757 void es_fun_1() {
758   aa_mu.Lock();
759   Bar.aa_elr_fun_s();
760   aa_mu.Unlock();
761 }
762 
763 void es_fun_2() {
764   aa_mu.ReaderLock();
765   Bar.aa_elr_fun_s();
766   aa_mu.Unlock();
767 }
768 
769 void es_fun_3() {
770   sls_mu.Lock();
771   MyLRFoo.test();
772   sls_mu.Unlock();
773 }
774 
775 void es_fun_4() {
776   sls_mu2.Lock();
777   MyLRFoo.testShared();
778   sls_mu2.Unlock();
779 }
780 
781 void es_fun_5() {
782   sls_mu2.ReaderLock();
783   MyLRFoo.testShared();
784   sls_mu2.Unlock();
785 }
786 
787 void es_fun_6() {
788   Bar.le_fun();
789 }
790 
791 void es_fun_7() {
792   sls_mu.Lock();
793   elr_fun();
794   sls_mu.Unlock();
795 }
796 
797 void es_fun_8() __attribute__((no_thread_safety_analysis));
798 
799 void es_fun_8() {
800   Bar.aa_elr_fun_s();
801 }
802 
803 void es_fun_9() __attribute__((shared_locks_required(aa_mu)));
804 void es_fun_9() {
805   Bar.aa_elr_fun_s();
806 }
807 
808 void es_fun_10() __attribute__((exclusive_locks_required(aa_mu)));
809 void es_fun_10() {
810   Bar.aa_elr_fun_s();
811 }
812 
813 void es_bad_0() {
814   Bar.aa_elr_fun(); // \
815     // expected-warning {{calling function 'aa_elr_fun' requires holding mutex 'aa_mu' exclusively}}
816 }
817 
818 void es_bad_1() {
819   aa_mu.ReaderLock();
820   Bar.aa_elr_fun(); // \
821     // expected-warning {{calling function 'aa_elr_fun' requires holding mutex 'aa_mu' exclusively}}
822   aa_mu.Unlock();
823 }
824 
825 void es_bad_2() {
826   Bar.aa_elr_fun_s(); // \
827     // expected-warning {{calling function 'aa_elr_fun_s' requires holding mutex 'aa_mu'}}
828 }
829 
830 void es_bad_3() {
831   MyLRFoo.test(); // \
832     // expected-warning {{calling function 'test' requires holding mutex 'sls_mu' exclusively}}
833 }
834 
835 void es_bad_4() {
836   MyLRFoo.testShared(); // \
837     // expected-warning {{calling function 'testShared' requires holding mutex 'sls_mu2'}}
838 }
839 
840 void es_bad_5() {
841   sls_mu.ReaderLock();
842   MyLRFoo.test(); // \
843     // expected-warning {{calling function 'test' requires holding mutex 'sls_mu' exclusively}}
844   sls_mu.Unlock();
845 }
846 
847 void es_bad_6() {
848   sls_mu.Lock();
849   Bar.le_fun(); // \
850     // expected-warning {{cannot call function 'le_fun' while mutex 'sls_mu' is held}}
851   sls_mu.Unlock();
852 }
853 
854 void es_bad_7() {
855   sls_mu.ReaderLock();
856   Bar.le_fun(); // \
857     // expected-warning {{cannot call function 'le_fun' while mutex 'sls_mu' is held}}
858   sls_mu.Unlock();
859 }
860 
861 
862 //-----------------------------------------------//
863 // Unparseable lock expressions
864 // ----------------------------------------------//
865 
866 // FIXME -- derive new tests for unhandled expressions
867 
868 
869 //----------------------------------------------------------------------------//
870 // The following test cases are ported from the gcc thread safety implementation
871 // They are each wrapped inside a namespace with the test number of the gcc test
872 //
873 // FIXME: add all the gcc tests, once this analysis passes them.
874 //----------------------------------------------------------------------------//
875 
876 //-----------------------------------------//
877 // Good testcases (no errors)
878 //-----------------------------------------//
879 
880 namespace thread_annot_lock_20 {
881 class Bar {
882  public:
883   static int func1() EXCLUSIVE_LOCKS_REQUIRED(mu1_);
884   static int b_ GUARDED_BY(mu1_);
885   static Mutex mu1_;
886   static int a_ GUARDED_BY(mu1_);
887 };
888 
889 Bar b1;
890 
891 int Bar::func1()
892 {
893   int res = 5;
894 
895   if (a_ == 4)
896     res = b_;
897   return res;
898 }
899 } // end namespace thread_annot_lock_20
900 
901 namespace thread_annot_lock_22 {
902 // Test various usage of GUARDED_BY and PT_GUARDED_BY annotations, especially
903 // uses in class definitions.
904 Mutex mu;
905 
906 class Bar {
907  public:
908   int a_ GUARDED_BY(mu1_);
909   int b_;
910   int *q PT_GUARDED_BY(mu);
911   Mutex mu1_ ACQUIRED_AFTER(mu);
912 };
913 
914 Bar b1, *b3;
915 int *p GUARDED_BY(mu) PT_GUARDED_BY(mu);
916 int res GUARDED_BY(mu) = 5;
917 
918 int func(int i)
919 {
920   int x;
921   mu.Lock();
922   b1.mu1_.Lock();
923   res = b1.a_ + b3->b_;
924   *p = i;
925   b1.a_ = res + b3->b_;
926   b3->b_ = *b1.q;
927   b1.mu1_.Unlock();
928   b1.b_ = res;
929   x = res;
930   mu.Unlock();
931   return x;
932 }
933 } // end namespace thread_annot_lock_22
934 
935 namespace thread_annot_lock_27_modified {
936 // test lock annotations applied to function definitions
937 // Modified: applied annotations only to function declarations
938 Mutex mu1;
939 Mutex mu2 ACQUIRED_AFTER(mu1);
940 
941 class Foo {
942  public:
943   int method1(int i) SHARED_LOCKS_REQUIRED(mu2) EXCLUSIVE_LOCKS_REQUIRED(mu1);
944 };
945 
946 int Foo::method1(int i) {
947   return i;
948 }
949 
950 
951 int foo(int i) EXCLUSIVE_LOCKS_REQUIRED(mu2) SHARED_LOCKS_REQUIRED(mu1);
952 int foo(int i) {
953   return i;
954 }
955 
956 static int bar(int i) EXCLUSIVE_LOCKS_REQUIRED(mu1);
957 static int bar(int i) {
958   return i;
959 }
960 
961 void main() {
962   Foo a;
963 
964   mu1.Lock();
965   mu2.Lock();
966   a.method1(1);
967   foo(2);
968   mu2.Unlock();
969   bar(3);
970   mu1.Unlock();
971 }
972 } // end namespace thread_annot_lock_27_modified
973 
974 
975 namespace thread_annot_lock_38 {
976 // Test the case where a template member function is annotated with lock
977 // attributes in a non-template class.
978 class Foo {
979  public:
980   void func1(int y) LOCKS_EXCLUDED(mu_);
981   template <typename T> void func2(T x) LOCKS_EXCLUDED(mu_);
982  private:
983   Mutex mu_;
984 };
985 
986 Foo *foo;
987 
988 void main()
989 {
990   foo->func1(5);
991   foo->func2(5);
992 }
993 } // end namespace thread_annot_lock_38
994 
995 namespace thread_annot_lock_43 {
996 // Tests lock canonicalization
997 class Foo {
998  public:
999   Mutex *mu_;
1000 };
1001 
1002 class FooBar {
1003  public:
1004   Foo *foo_;
1005   int GetA() EXCLUSIVE_LOCKS_REQUIRED(foo_->mu_) { return a_; }
1006   int a_ GUARDED_BY(foo_->mu_);
1007 };
1008 
1009 FooBar *fb;
1010 
1011 void main()
1012 {
1013   int x;
1014   fb->foo_->mu_->Lock();
1015   x = fb->GetA();
1016   fb->foo_->mu_->Unlock();
1017 }
1018 } // end namespace thread_annot_lock_43
1019 
1020 namespace thread_annot_lock_49 {
1021 // Test the support for use of lock expression in the annotations
1022 class Foo {
1023  public:
1024   Mutex foo_mu_;
1025 };
1026 
1027 class Bar {
1028  private:
1029   Foo *foo;
1030   Mutex bar_mu_ ACQUIRED_AFTER(foo->foo_mu_);
1031 
1032  public:
1033   void Test1() {
1034     foo->foo_mu_.Lock();
1035     bar_mu_.Lock();
1036     bar_mu_.Unlock();
1037     foo->foo_mu_.Unlock();
1038   }
1039 };
1040 
1041 void main() {
1042   Bar bar;
1043   bar.Test1();
1044 }
1045 } // end namespace thread_annot_lock_49
1046 
1047 namespace thread_annot_lock_61_modified {
1048   // Modified to fix the compiler errors
1049   // Test the fix for a bug introduced by the support of pass-by-reference
1050   // paramters.
1051   struct Foo { Foo &operator<< (bool) {return *this;} };
1052   Foo &getFoo();
1053   struct Bar { Foo &func () {return getFoo();} };
1054   struct Bas { void operator& (Foo &) {} };
1055   void mumble()
1056   {
1057     Bas() & Bar().func() << "" << "";
1058     Bas() & Bar().func() << "";
1059   }
1060 } // end namespace thread_annot_lock_61_modified
1061 
1062 
1063 namespace thread_annot_lock_65 {
1064 // Test the fix for a bug in the support of allowing reader locks for
1065 // non-const, non-modifying overload functions. (We didn't handle the builtin
1066 // properly.)
1067 enum MyFlags {
1068   Zero,
1069   One,
1070   Two,
1071   Three,
1072   Four,
1073   Five,
1074   Six,
1075   Seven,
1076   Eight,
1077   Nine
1078 };
1079 
1080 inline MyFlags
1081 operator|(MyFlags a, MyFlags b)
1082 {
1083   return MyFlags(static_cast<int>(a) | static_cast<int>(b));
1084 }
1085 
1086 inline MyFlags&
1087 operator|=(MyFlags& a, MyFlags b)
1088 {
1089     return a = a | b;
1090 }
1091 } // end namespace thread_annot_lock_65
1092 
1093 namespace thread_annot_lock_66_modified {
1094 // Modified: Moved annotation to function defn
1095 // Test annotations on out-of-line definitions of member functions where the
1096 // annotations refer to locks that are also data members in the class.
1097 Mutex mu;
1098 
1099 class Foo {
1100  public:
1101   int method1(int i) SHARED_LOCKS_REQUIRED(mu1, mu, mu2);
1102   int data GUARDED_BY(mu1);
1103   Mutex *mu1;
1104   Mutex *mu2;
1105 };
1106 
1107 int Foo::method1(int i)
1108 {
1109   return data + i;
1110 }
1111 
1112 void main()
1113 {
1114   Foo a;
1115 
1116   a.mu2->Lock();
1117   a.mu1->Lock();
1118   mu.Lock();
1119   a.method1(1);
1120   mu.Unlock();
1121   a.mu1->Unlock();
1122   a.mu2->Unlock();
1123 }
1124 } // end namespace thread_annot_lock_66_modified
1125 
1126 namespace thread_annot_lock_68_modified {
1127 // Test a fix to a bug in the delayed name binding with nested template
1128 // instantiation. We use a stack to make sure a name is not resolved to an
1129 // inner context.
1130 template <typename T>
1131 class Bar {
1132   Mutex mu_;
1133 };
1134 
1135 template <typename T>
1136 class Foo {
1137  public:
1138   void func(T x) {
1139     mu_.Lock();
1140     count_ = x;
1141     mu_.Unlock();
1142   }
1143 
1144  private:
1145   T count_ GUARDED_BY(mu_);
1146   Bar<T> bar_;
1147   Mutex mu_;
1148 };
1149 
1150 void main()
1151 {
1152   Foo<int> *foo;
1153   foo->func(5);
1154 }
1155 } // end namespace thread_annot_lock_68_modified
1156 
1157 namespace thread_annot_lock_30_modified {
1158 // Test delay parsing of lock attribute arguments with nested classes.
1159 // Modified: trylocks replaced with exclusive_lock_fun
1160 int a = 0;
1161 
1162 class Bar {
1163   struct Foo;
1164 
1165  public:
1166   void MyLock() EXCLUSIVE_LOCK_FUNCTION(mu);
1167 
1168   int func() {
1169     MyLock();
1170 //    if (foo == 0) {
1171 //      return 0;
1172 //    }
1173     a = 5;
1174     mu.Unlock();
1175     return 1;
1176   }
1177 
1178   class FooBar {
1179     int x;
1180     int y;
1181   };
1182 
1183  private:
1184   Mutex mu;
1185 };
1186 
1187 Bar *bar;
1188 
1189 void main()
1190 {
1191   bar->func();
1192 }
1193 } // end namespace thread_annot_lock_30_modified
1194 
1195 namespace thread_annot_lock_47 {
1196 // Test the support for annotations on virtual functions.
1197 // This is a good test case. (i.e. There should be no warning emitted by the
1198 // compiler.)
1199 class Base {
1200  public:
1201   virtual void func1() EXCLUSIVE_LOCKS_REQUIRED(mu_);
1202   virtual void func2() LOCKS_EXCLUDED(mu_);
1203   Mutex mu_;
1204 };
1205 
1206 class Child : public Base {
1207  public:
1208   virtual void func1() EXCLUSIVE_LOCKS_REQUIRED(mu_);
1209   virtual void func2() LOCKS_EXCLUDED(mu_);
1210 };
1211 
1212 void main() {
1213   Child *c;
1214   Base *b = c;
1215 
1216   b->mu_.Lock();
1217   b->func1();
1218   b->mu_.Unlock();
1219   b->func2();
1220 
1221   c->mu_.Lock();
1222   c->func1();
1223   c->mu_.Unlock();
1224   c->func2();
1225 }
1226 } // end namespace thread_annot_lock_47
1227 
1228 //-----------------------------------------//
1229 // Tests which produce errors
1230 //-----------------------------------------//
1231 
1232 namespace thread_annot_lock_13 {
1233 Mutex mu1;
1234 Mutex mu2;
1235 
1236 int g GUARDED_BY(mu1);
1237 int w GUARDED_BY(mu2);
1238 
1239 class Foo {
1240  public:
1241   void bar() LOCKS_EXCLUDED(mu_, mu1);
1242   int foo() SHARED_LOCKS_REQUIRED(mu_) EXCLUSIVE_LOCKS_REQUIRED(mu2);
1243 
1244  private:
1245   int a_ GUARDED_BY(mu_);
1246  public:
1247   Mutex mu_ ACQUIRED_AFTER(mu1);
1248 };
1249 
1250 int Foo::foo()
1251 {
1252   int res;
1253   w = 5;
1254   res = a_ + 5;
1255   return res;
1256 }
1257 
1258 void Foo::bar()
1259 {
1260   int x;
1261   mu_.Lock();
1262   x = foo(); // expected-warning {{calling function 'foo' requires holding mutex 'mu2' exclusively}}
1263   a_ = x + 1;
1264   mu_.Unlock();
1265   if (x > 5) {
1266     mu1.Lock();
1267     g = 2;
1268     mu1.Unlock();
1269   }
1270 }
1271 
1272 void main()
1273 {
1274   Foo f1, *f2;
1275   f1.mu_.Lock();
1276   f1.bar(); // expected-warning {{cannot call function 'bar' while mutex 'f1.mu_' is held}}
1277   mu2.Lock();
1278   f1.foo();
1279   mu2.Unlock();
1280   f1.mu_.Unlock();
1281   f2->mu_.Lock();
1282   f2->bar(); // expected-warning {{cannot call function 'bar' while mutex 'f2->mu_' is held}}
1283   f2->mu_.Unlock();
1284   mu2.Lock();
1285   w = 2;
1286   mu2.Unlock();
1287 }
1288 } // end namespace thread_annot_lock_13
1289 
1290 namespace thread_annot_lock_18_modified {
1291 // Modified: Trylocks removed
1292 // Test the ability to distnguish between the same lock field of
1293 // different objects of a class.
1294   class Bar {
1295  public:
1296   bool MyLock() EXCLUSIVE_LOCK_FUNCTION(mu1_);
1297   void MyUnlock() UNLOCK_FUNCTION(mu1_);
1298   int a_ GUARDED_BY(mu1_);
1299 
1300  private:
1301   Mutex mu1_;
1302 };
1303 
1304 Bar *b1, *b2;
1305 
1306 void func()
1307 {
1308   b1->MyLock();
1309   b1->a_ = 5;
1310   b2->a_ = 3; // \
1311     // expected-warning {{writing variable 'a_' requires holding mutex 'b2->mu1_' exclusively}} \
1312     // expected-note {{found near match 'b1->mu1_'}}
1313   b2->MyLock();
1314   b2->MyUnlock();
1315   b1->MyUnlock();
1316 }
1317 } // end namespace thread_annot_lock_18_modified
1318 
1319 namespace thread_annot_lock_21 {
1320 // Test various usage of GUARDED_BY and PT_GUARDED_BY annotations, especially
1321 // uses in class definitions.
1322 Mutex mu;
1323 
1324 class Bar {
1325  public:
1326   int a_ GUARDED_BY(mu1_);
1327   int b_;
1328   int *q PT_GUARDED_BY(mu);
1329   Mutex mu1_ ACQUIRED_AFTER(mu);
1330 };
1331 
1332 Bar b1, *b3;
1333 int *p GUARDED_BY(mu) PT_GUARDED_BY(mu);
1334 
1335 int res GUARDED_BY(mu) = 5;
1336 
1337 int func(int i)
1338 {
1339   int x;
1340   b3->mu1_.Lock();
1341   res = b1.a_ + b3->b_; // expected-warning {{reading variable 'a_' requires holding mutex 'b1.mu1_'}} \
1342     // expected-warning {{writing variable 'res' requires holding mutex 'mu' exclusively}} \
1343     // expected-note {{found near match 'b3->mu1_'}}
1344   *p = i; // expected-warning {{reading variable 'p' requires holding mutex 'mu'}} \
1345     // expected-warning {{writing the value pointed to by 'p' requires holding mutex 'mu' exclusively}}
1346   b1.a_ = res + b3->b_; // expected-warning {{reading variable 'res' requires holding mutex 'mu'}} \
1347     // expected-warning {{writing variable 'a_' requires holding mutex 'b1.mu1_' exclusively}} \
1348     // expected-note {{found near match 'b3->mu1_'}}
1349   b3->b_ = *b1.q; // expected-warning {{reading the value pointed to by 'q' requires holding mutex 'mu'}}
1350   b3->mu1_.Unlock();
1351   b1.b_ = res; // expected-warning {{reading variable 'res' requires holding mutex 'mu'}}
1352   x = res; // expected-warning {{reading variable 'res' requires holding mutex 'mu'}}
1353   return x;
1354 }
1355 } // end namespace thread_annot_lock_21
1356 
1357 namespace thread_annot_lock_35_modified {
1358 // Test the analyzer's ability to distinguish the lock field of different
1359 // objects.
1360 class Foo {
1361  private:
1362   Mutex lock_;
1363   int a_ GUARDED_BY(lock_);
1364 
1365  public:
1366   void Func(Foo* child) LOCKS_EXCLUDED(lock_) {
1367      Foo *new_foo = new Foo;
1368 
1369      lock_.Lock();
1370 
1371      child->Func(new_foo); // There shouldn't be any warning here as the
1372                            // acquired lock is not in child.
1373      child->bar(7); // \
1374        // expected-warning {{calling function 'bar' requires holding mutex 'child->lock_' exclusively}} \
1375        // expected-note {{found near match 'lock_'}}
1376      child->a_ = 5; // \
1377        // expected-warning {{writing variable 'a_' requires holding mutex 'child->lock_' exclusively}} \
1378        // expected-note {{found near match 'lock_'}}
1379      lock_.Unlock();
1380   }
1381 
1382   void bar(int y) EXCLUSIVE_LOCKS_REQUIRED(lock_) {
1383     a_ = y;
1384   }
1385 };
1386 
1387 Foo *x;
1388 
1389 void main() {
1390   Foo *child = new Foo;
1391   x->Func(child);
1392 }
1393 } // end namespace thread_annot_lock_35_modified
1394 
1395 namespace thread_annot_lock_36_modified {
1396 // Modified to move the annotations to function defns.
1397 // Test the analyzer's ability to distinguish the lock field of different
1398 // objects
1399 class Foo {
1400  private:
1401   Mutex lock_;
1402   int a_ GUARDED_BY(lock_);
1403 
1404  public:
1405   void Func(Foo* child) LOCKS_EXCLUDED(lock_);
1406   void bar(int y) EXCLUSIVE_LOCKS_REQUIRED(lock_);
1407 };
1408 
1409 void Foo::Func(Foo* child) {
1410   Foo *new_foo = new Foo;
1411 
1412   lock_.Lock();
1413 
1414   child->lock_.Lock();
1415   child->Func(new_foo); // expected-warning {{cannot call function 'Func' while mutex 'child->lock_' is held}}
1416   child->bar(7);
1417   child->a_ = 5;
1418   child->lock_.Unlock();
1419 
1420   lock_.Unlock();
1421 }
1422 
1423 void Foo::bar(int y) {
1424   a_ = y;
1425 }
1426 
1427 
1428 Foo *x;
1429 
1430 void main() {
1431   Foo *child = new Foo;
1432   x->Func(child);
1433 }
1434 } // end namespace thread_annot_lock_36_modified
1435 
1436 
1437 namespace thread_annot_lock_42 {
1438 // Test support of multiple lock attributes of the same kind on a decl.
1439 class Foo {
1440  private:
1441   Mutex mu1, mu2, mu3;
1442   int x GUARDED_BY(mu1) GUARDED_BY(mu2);
1443   int y GUARDED_BY(mu2);
1444 
1445   void f2() LOCKS_EXCLUDED(mu1) LOCKS_EXCLUDED(mu2) LOCKS_EXCLUDED(mu3) {
1446     mu2.Lock();
1447     y = 2;
1448     mu2.Unlock();
1449   }
1450 
1451  public:
1452   void f1() EXCLUSIVE_LOCKS_REQUIRED(mu2) EXCLUSIVE_LOCKS_REQUIRED(mu1) {
1453     x = 5;
1454     f2(); // expected-warning {{cannot call function 'f2' while mutex 'mu1' is held}} \
1455       // expected-warning {{cannot call function 'f2' while mutex 'mu2' is held}}
1456   }
1457 };
1458 
1459 Foo *foo;
1460 
1461 void func()
1462 {
1463   foo->f1(); // expected-warning {{calling function 'f1' requires holding mutex 'foo->mu2' exclusively}} \
1464              // expected-warning {{calling function 'f1' requires holding mutex 'foo->mu1' exclusively}}
1465 }
1466 } // end namespace thread_annot_lock_42
1467 
1468 namespace thread_annot_lock_46 {
1469 // Test the support for annotations on virtual functions.
1470 class Base {
1471  public:
1472   virtual void func1() EXCLUSIVE_LOCKS_REQUIRED(mu_);
1473   virtual void func2() LOCKS_EXCLUDED(mu_);
1474   Mutex mu_;
1475 };
1476 
1477 class Child : public Base {
1478  public:
1479   virtual void func1() EXCLUSIVE_LOCKS_REQUIRED(mu_);
1480   virtual void func2() LOCKS_EXCLUDED(mu_);
1481 };
1482 
1483 void main() {
1484   Child *c;
1485   Base *b = c;
1486 
1487   b->func1(); // expected-warning {{calling function 'func1' requires holding mutex 'b->mu_' exclusively}}
1488   b->mu_.Lock();
1489   b->func2(); // expected-warning {{cannot call function 'func2' while mutex 'b->mu_' is held}}
1490   b->mu_.Unlock();
1491 
1492   c->func1(); // expected-warning {{calling function 'func1' requires holding mutex 'c->mu_' exclusively}}
1493   c->mu_.Lock();
1494   c->func2(); // expected-warning {{cannot call function 'func2' while mutex 'c->mu_' is held}}
1495   c->mu_.Unlock();
1496 }
1497 } // end namespace thread_annot_lock_46
1498 
1499 namespace thread_annot_lock_67_modified {
1500 // Modified: attributes on definitions moved to declarations
1501 // Test annotations on out-of-line definitions of member functions where the
1502 // annotations refer to locks that are also data members in the class.
1503 Mutex mu;
1504 Mutex mu3;
1505 
1506 class Foo {
1507  public:
1508   int method1(int i) SHARED_LOCKS_REQUIRED(mu1, mu, mu2, mu3);
1509   int data GUARDED_BY(mu1);
1510   Mutex *mu1;
1511   Mutex *mu2;
1512 };
1513 
1514 int Foo::method1(int i) {
1515   return data + i;
1516 }
1517 
1518 void main()
1519 {
1520   Foo a;
1521   a.method1(1); // expected-warning {{calling function 'method1' requires holding mutex 'a.mu1'}} \
1522     // expected-warning {{calling function 'method1' requires holding mutex 'mu'}} \
1523     // expected-warning {{calling function 'method1' requires holding mutex 'a.mu2'}} \
1524     // expected-warning {{calling function 'method1' requires holding mutex 'mu3'}}
1525 }
1526 } // end namespace thread_annot_lock_67_modified
1527 
1528 
1529 namespace substitution_test {
1530   class MyData  {
1531   public:
1532     Mutex mu;
1533 
1534     void lockData()    __attribute__((exclusive_lock_function(mu)));
1535     void unlockData()  __attribute__((unlock_function(mu)));
1536 
1537     void doSomething() __attribute__((exclusive_locks_required(mu)))  { }
1538   };
1539 
1540 
1541   class DataLocker {
1542   public:
1543     void lockData  (MyData *d) __attribute__((exclusive_lock_function(d->mu)));
1544     void unlockData(MyData *d) __attribute__((unlock_function(d->mu)));
1545   };
1546 
1547 
1548   class Foo {
1549   public:
1550     void foo(MyData* d) __attribute__((exclusive_locks_required(d->mu))) { }
1551 
1552     void bar1(MyData* d) {
1553       d->lockData();
1554       foo(d);
1555       d->unlockData();
1556     }
1557 
1558     void bar2(MyData* d) {
1559       DataLocker dlr;
1560       dlr.lockData(d);
1561       foo(d);
1562       dlr.unlockData(d);
1563     }
1564 
1565     void bar3(MyData* d1, MyData* d2) {
1566       DataLocker dlr;
1567       dlr.lockData(d1);   // expected-note {{mutex acquired here}}
1568       dlr.unlockData(d2); // \
1569         // expected-warning {{releasing mutex 'd2->mu' that was not held}}
1570     } // expected-warning {{mutex 'd1->mu' is still held at the end of function}}
1571 
1572     void bar4(MyData* d1, MyData* d2) {
1573       DataLocker dlr;
1574       dlr.lockData(d1);
1575       foo(d2); // \
1576         // expected-warning {{calling function 'foo' requires holding mutex 'd2->mu' exclusively}} \
1577         // expected-note {{found near match 'd1->mu'}}
1578       dlr.unlockData(d1);
1579     }
1580   };
1581 } // end namespace substituation_test
1582 
1583 
1584 
1585 namespace constructor_destructor_tests {
1586   Mutex fooMu;
1587   int myVar GUARDED_BY(fooMu);
1588 
1589   class Foo {
1590   public:
1591     Foo()  __attribute__((exclusive_lock_function(fooMu))) { }
1592     ~Foo() __attribute__((unlock_function(fooMu))) { }
1593   };
1594 
1595   void fooTest() {
1596     Foo foo;
1597     myVar = 0;
1598   }
1599 }
1600 
1601 
1602 namespace template_member_test {
1603 
1604   struct S { int n; };
1605   struct T {
1606     Mutex m;
1607     S *s GUARDED_BY(this->m);
1608   };
1609   Mutex m;
1610   struct U {
1611     union {
1612       int n;
1613     };
1614   } *u GUARDED_BY(m);
1615 
1616   template<typename U>
1617   struct IndirectLock {
1618     int DoNaughtyThings(T *t) {
1619       u->n = 0; // expected-warning {{reading variable 'u' requires holding mutex 'm'}}
1620       return t->s->n; // expected-warning {{reading variable 's' requires holding mutex 't->m'}}
1621     }
1622   };
1623 
1624   template struct IndirectLock<int>; // expected-note {{here}}
1625 
1626   struct V {
1627     void f(int);
1628     void f(double);
1629 
1630     Mutex m;
1631     V *p GUARDED_BY(this->m);
1632   };
1633   template<typename U> struct W {
1634     V v;
1635     void f(U u) {
1636       v.p->f(u); // expected-warning {{reading variable 'p' requires holding mutex 'v.m'}}
1637     }
1638   };
1639   template struct W<int>; // expected-note {{here}}
1640 
1641 }
1642 
1643 namespace test_scoped_lockable {
1644 
1645 struct TestScopedLockable {
1646   Mutex mu1;
1647   Mutex mu2;
1648   int a __attribute__((guarded_by(mu1)));
1649   int b __attribute__((guarded_by(mu2)));
1650 
1651   bool getBool();
1652 
1653   void foo1() {
1654     MutexLock mulock(&mu1);
1655     a = 5;
1656   }
1657 
1658   void foo2() {
1659     ReaderMutexLock mulock1(&mu1);
1660     if (getBool()) {
1661       MutexLock mulock2a(&mu2);
1662       b = a + 1;
1663     }
1664     else {
1665       MutexLock mulock2b(&mu2);
1666       b = a + 2;
1667     }
1668   }
1669 
1670   void foo3() {
1671     MutexLock mulock_a(&mu1);
1672     MutexLock mulock_b(&mu1); // \
1673       // expected-warning {{acquiring mutex 'mu1' that is already held}}
1674   }
1675 
1676   void foo4() {
1677     MutexLock mulock1(&mu1), mulock2(&mu2);
1678     a = b+1;
1679     b = a+1;
1680   }
1681 
1682   void foo5() {
1683     DoubleMutexLock mulock(&mu1, &mu2);
1684     a = b + 1;
1685     b = a + 1;
1686   }
1687 };
1688 
1689 } // end namespace test_scoped_lockable
1690 
1691 
1692 namespace FunctionAttrTest {
1693 
1694 class Foo {
1695 public:
1696   Mutex mu_;
1697   int a GUARDED_BY(mu_);
1698 };
1699 
1700 Foo fooObj;
1701 
1702 void foo() EXCLUSIVE_LOCKS_REQUIRED(fooObj.mu_);
1703 
1704 void bar() {
1705   foo();  // expected-warning {{calling function 'foo' requires holding mutex 'fooObj.mu_' exclusively}}
1706   fooObj.mu_.Lock();
1707   foo();
1708   fooObj.mu_.Unlock();
1709 }
1710 
1711 };  // end namespace FunctionAttrTest
1712 
1713 
1714 namespace TryLockTest {
1715 
1716 struct TestTryLock {
1717   Mutex mu;
1718   int a GUARDED_BY(mu);
1719   bool cond;
1720 
1721   void foo1() {
1722     if (mu.TryLock()) {
1723       a = 1;
1724       mu.Unlock();
1725     }
1726   }
1727 
1728   void foo2() {
1729     if (!mu.TryLock()) return;
1730     a = 2;
1731     mu.Unlock();
1732   }
1733 
1734   void foo3() {
1735     bool b = mu.TryLock();
1736     if (b) {
1737       a = 3;
1738       mu.Unlock();
1739     }
1740   }
1741 
1742   void foo4() {
1743     bool b = mu.TryLock();
1744     if (!b) return;
1745     a = 4;
1746     mu.Unlock();
1747   }
1748 
1749   void foo5() {
1750     while (mu.TryLock()) {
1751       a = a + 1;
1752       mu.Unlock();
1753     }
1754   }
1755 
1756   void foo6() {
1757     bool b = mu.TryLock();
1758     b = !b;
1759     if (b) return;
1760     a = 6;
1761     mu.Unlock();
1762   }
1763 
1764   void foo7() {
1765     bool b1 = mu.TryLock();
1766     bool b2 = !b1;
1767     bool b3 = !b2;
1768     if (b3) {
1769       a = 7;
1770       mu.Unlock();
1771     }
1772   }
1773 
1774   // Test use-def chains: join points
1775   void foo8() {
1776     bool b  = mu.TryLock();
1777     bool b2 = b;
1778     if (cond)
1779       b = true;
1780     if (b) {    // b should be unknown at this point, because of the join point
1781       a = 8;    // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}}
1782     }
1783     if (b2) {   // b2 should be known at this point.
1784       a = 8;
1785       mu.Unlock();
1786     }
1787   }
1788 
1789   // Test use-def-chains: back edges
1790   void foo9() {
1791     bool b = mu.TryLock();
1792 
1793     for (int i = 0; i < 10; ++i);
1794 
1795     if (b) {  // b is still known, because the loop doesn't alter it
1796       a = 9;
1797       mu.Unlock();
1798     }
1799   }
1800 
1801   // Test use-def chains: back edges
1802   void foo10() {
1803     bool b = mu.TryLock();
1804 
1805     while (cond) {
1806       if (b) {   // b should be uknown at this point b/c of the loop
1807         a = 10;  // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}}
1808       }
1809       b = !b;
1810     }
1811   }
1812 
1813   // Test merge of exclusive trylock
1814   void foo11() {
1815    if (cond) {
1816      if (!mu.TryLock())
1817        return;
1818    }
1819    else {
1820      mu.Lock();
1821    }
1822    a = 10;
1823    mu.Unlock();
1824   }
1825 
1826   // Test merge of shared trylock
1827   void foo12() {
1828    if (cond) {
1829      if (!mu.ReaderTryLock())
1830        return;
1831    }
1832    else {
1833      mu.ReaderLock();
1834    }
1835    int i = a;
1836    mu.Unlock();
1837   }
1838 };  // end TestTrylock
1839 
1840 } // end namespace TrylockTest
1841 
1842 
1843 namespace TestTemplateAttributeInstantiation {
1844 
1845 class Foo1 {
1846 public:
1847   Mutex mu_;
1848   int a GUARDED_BY(mu_);
1849 };
1850 
1851 class Foo2 {
1852 public:
1853   int a GUARDED_BY(mu_);
1854   Mutex mu_;
1855 };
1856 
1857 
1858 class Bar {
1859 public:
1860   // Test non-dependent expressions in attributes on template functions
1861   template <class T>
1862   void barND(Foo1 *foo, T *fooT) EXCLUSIVE_LOCKS_REQUIRED(foo->mu_) {
1863     foo->a = 0;
1864   }
1865 
1866   // Test dependent expressions in attributes on template functions
1867   template <class T>
1868   void barD(Foo1 *foo, T *fooT) EXCLUSIVE_LOCKS_REQUIRED(fooT->mu_) {
1869     fooT->a = 0;
1870   }
1871 };
1872 
1873 
1874 template <class T>
1875 class BarT {
1876 public:
1877   Foo1 fooBase;
1878   T    fooBaseT;
1879 
1880   // Test non-dependent expression in ordinary method on template class
1881   void barND() EXCLUSIVE_LOCKS_REQUIRED(fooBase.mu_) {
1882     fooBase.a = 0;
1883   }
1884 
1885   // Test dependent expressions in ordinary methods on template class
1886   void barD() EXCLUSIVE_LOCKS_REQUIRED(fooBaseT.mu_) {
1887     fooBaseT.a = 0;
1888   }
1889 
1890   // Test dependent expressions in template method in template class
1891   template <class T2>
1892   void barTD(T2 *fooT) EXCLUSIVE_LOCKS_REQUIRED(fooBaseT.mu_, fooT->mu_) {
1893     fooBaseT.a = 0;
1894     fooT->a = 0;
1895   }
1896 };
1897 
1898 template <class T>
1899 class Cell {
1900 public:
1901   Mutex mu_;
1902   // Test dependent guarded_by
1903   T data GUARDED_BY(mu_);
1904 
1905   void fooEx() EXCLUSIVE_LOCKS_REQUIRED(mu_) {
1906     data = 0;
1907   }
1908 
1909   void foo() {
1910     mu_.Lock();
1911     data = 0;
1912     mu_.Unlock();
1913   }
1914 };
1915 
1916 void test() {
1917   Bar b;
1918   BarT<Foo2> bt;
1919   Foo1 f1;
1920   Foo2 f2;
1921 
1922   f1.mu_.Lock();
1923   f2.mu_.Lock();
1924   bt.fooBase.mu_.Lock();
1925   bt.fooBaseT.mu_.Lock();
1926 
1927   b.barND(&f1, &f2);
1928   b.barD(&f1, &f2);
1929   bt.barND();
1930   bt.barD();
1931   bt.barTD(&f2);
1932 
1933   f1.mu_.Unlock();
1934   bt.barTD(&f1);  // \
1935     // expected-warning {{calling function 'barTD' requires holding mutex 'f1.mu_' exclusively}} \
1936     // expected-note {{found near match 'bt.fooBase.mu_'}}
1937 
1938   bt.fooBase.mu_.Unlock();
1939   bt.fooBaseT.mu_.Unlock();
1940   f2.mu_.Unlock();
1941 
1942   Cell<int> cell;
1943   cell.data = 0; // \
1944     // expected-warning {{writing variable 'data' requires holding mutex 'cell.mu_' exclusively}}
1945   cell.foo();
1946   cell.mu_.Lock();
1947   cell.fooEx();
1948   cell.mu_.Unlock();
1949 }
1950 
1951 
1952 template <class T>
1953 class CellDelayed {
1954 public:
1955   // Test dependent guarded_by
1956   T data GUARDED_BY(mu_);
1957   static T static_data GUARDED_BY(static_mu_);
1958 
1959   void fooEx(CellDelayed<T> *other) EXCLUSIVE_LOCKS_REQUIRED(mu_, other->mu_) {
1960     this->data = other->data;
1961   }
1962 
1963   template <class T2>
1964   void fooExT(CellDelayed<T2> *otherT) EXCLUSIVE_LOCKS_REQUIRED(mu_, otherT->mu_) {
1965     this->data = otherT->data;
1966   }
1967 
1968   void foo() {
1969     mu_.Lock();
1970     data = 0;
1971     mu_.Unlock();
1972   }
1973 
1974   Mutex mu_;
1975   static Mutex static_mu_;
1976 };
1977 
1978 void testDelayed() {
1979   CellDelayed<int> celld;
1980   CellDelayed<int> celld2;
1981   celld.foo();
1982   celld.mu_.Lock();
1983   celld2.mu_.Lock();
1984 
1985   celld.fooEx(&celld2);
1986   celld.fooExT(&celld2);
1987 
1988   celld2.mu_.Unlock();
1989   celld.mu_.Unlock();
1990 }
1991 
1992 };  // end namespace TestTemplateAttributeInstantiation
1993 
1994 
1995 namespace FunctionDeclDefTest {
1996 
1997 class Foo {
1998 public:
1999   Mutex mu_;
2000   int a GUARDED_BY(mu_);
2001 
2002   virtual void foo1(Foo *f_declared) EXCLUSIVE_LOCKS_REQUIRED(f_declared->mu_);
2003 };
2004 
2005 // EXCLUSIVE_LOCKS_REQUIRED should be applied, and rewritten to f_defined->mu_
2006 void Foo::foo1(Foo *f_defined) {
2007   f_defined->a = 0;
2008 };
2009 
2010 void test() {
2011   Foo myfoo;
2012   myfoo.foo1(&myfoo);  // \
2013     // expected-warning {{calling function 'foo1' requires holding mutex 'myfoo.mu_' exclusively}}
2014   myfoo.mu_.Lock();
2015   myfoo.foo1(&myfoo);
2016   myfoo.mu_.Unlock();
2017 }
2018 
2019 };
2020 
2021 namespace GoingNative {
2022 
2023   struct __attribute__((lockable)) mutex {
2024     void lock() __attribute__((exclusive_lock_function));
2025     void unlock() __attribute__((unlock_function));
2026     // ...
2027   };
2028   bool foo();
2029   bool bar();
2030   mutex m;
2031   void test() {
2032     m.lock();
2033     while (foo()) {
2034       m.unlock();
2035       // ...
2036       if (bar()) {
2037         // ...
2038         if (foo())
2039           continue; // expected-warning {{expecting mutex 'm' to be held at start of each loop}}
2040         //...
2041       }
2042       // ...
2043       m.lock(); // expected-note {{mutex acquired here}}
2044     }
2045     m.unlock();
2046   }
2047 
2048 }
2049 
2050 
2051 
2052 namespace FunctionDefinitionTest {
2053 
2054 class Foo {
2055 public:
2056   void foo1();
2057   void foo2();
2058   void foo3(Foo *other);
2059 
2060   template<class T>
2061   void fooT1(const T& dummy1);
2062 
2063   template<class T>
2064   void fooT2(const T& dummy2) EXCLUSIVE_LOCKS_REQUIRED(mu_);
2065 
2066   Mutex mu_;
2067   int a GUARDED_BY(mu_);
2068 };
2069 
2070 template<class T>
2071 class FooT {
2072 public:
2073   void foo();
2074 
2075   Mutex mu_;
2076   T a GUARDED_BY(mu_);
2077 };
2078 
2079 
2080 void Foo::foo1() NO_THREAD_SAFETY_ANALYSIS {
2081   a = 1;
2082 }
2083 
2084 void Foo::foo2() EXCLUSIVE_LOCKS_REQUIRED(mu_) {
2085   a = 2;
2086 }
2087 
2088 void Foo::foo3(Foo *other) EXCLUSIVE_LOCKS_REQUIRED(other->mu_) {
2089   other->a = 3;
2090 }
2091 
2092 template<class T>
2093 void Foo::fooT1(const T& dummy1) EXCLUSIVE_LOCKS_REQUIRED(mu_) {
2094   a = dummy1;
2095 }
2096 
2097 /* TODO -- uncomment with template instantiation of attributes.
2098 template<class T>
2099 void Foo::fooT2(const T& dummy2) {
2100   a = dummy2;
2101 }
2102 */
2103 
2104 void fooF1(Foo *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_) {
2105   f->a = 1;
2106 }
2107 
2108 void fooF2(Foo *f);
2109 void fooF2(Foo *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_) {
2110   f->a = 2;
2111 }
2112 
2113 void fooF3(Foo *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_);
2114 void fooF3(Foo *f) {
2115   f->a = 3;
2116 }
2117 
2118 template<class T>
2119 void FooT<T>::foo() EXCLUSIVE_LOCKS_REQUIRED(mu_) {
2120   a = 0;
2121 }
2122 
2123 void test() {
2124   int dummy = 0;
2125   Foo myFoo;
2126 
2127   myFoo.foo2();        // \
2128     // expected-warning {{calling function 'foo2' requires holding mutex 'myFoo.mu_' exclusively}}
2129   myFoo.foo3(&myFoo);  // \
2130     // expected-warning {{calling function 'foo3' requires holding mutex 'myFoo.mu_' exclusively}}
2131   myFoo.fooT1(dummy);  // \
2132     // expected-warning {{calling function 'fooT1' requires holding mutex 'myFoo.mu_' exclusively}}
2133 
2134   myFoo.fooT2(dummy);  // \
2135     // expected-warning {{calling function 'fooT2' requires holding mutex 'myFoo.mu_' exclusively}}
2136 
2137   fooF1(&myFoo);  // \
2138     // expected-warning {{calling function 'fooF1' requires holding mutex 'myFoo.mu_' exclusively}}
2139   fooF2(&myFoo);  // \
2140     // expected-warning {{calling function 'fooF2' requires holding mutex 'myFoo.mu_' exclusively}}
2141   fooF3(&myFoo);  // \
2142     // expected-warning {{calling function 'fooF3' requires holding mutex 'myFoo.mu_' exclusively}}
2143 
2144   myFoo.mu_.Lock();
2145   myFoo.foo2();
2146   myFoo.foo3(&myFoo);
2147   myFoo.fooT1(dummy);
2148 
2149   myFoo.fooT2(dummy);
2150 
2151   fooF1(&myFoo);
2152   fooF2(&myFoo);
2153   fooF3(&myFoo);
2154   myFoo.mu_.Unlock();
2155 
2156   FooT<int> myFooT;
2157   myFooT.foo();  // \
2158     // expected-warning {{calling function 'foo' requires holding mutex 'myFooT.mu_' exclusively}}
2159 }
2160 
2161 } // end namespace FunctionDefinitionTest
2162 
2163 
2164 namespace SelfLockingTest {
2165 
2166 class LOCKABLE MyLock {
2167 public:
2168   int foo GUARDED_BY(this);
2169 
2170   void lock()   EXCLUSIVE_LOCK_FUNCTION();
2171   void unlock() UNLOCK_FUNCTION();
2172 
2173   void doSomething() {
2174     this->lock();  // allow 'this' as a lock expression
2175     foo = 0;
2176     doSomethingElse();
2177     this->unlock();
2178   }
2179 
2180   void doSomethingElse() EXCLUSIVE_LOCKS_REQUIRED(this) {
2181     foo = 1;
2182   };
2183 
2184   void test() {
2185     foo = 2;  // \
2186       // expected-warning {{writing variable 'foo' requires holding mutex 'this' exclusively}}
2187   }
2188 };
2189 
2190 
2191 class LOCKABLE MyLock2 {
2192 public:
2193   Mutex mu_;
2194   int foo GUARDED_BY(this);
2195 
2196   // don't check inside lock and unlock functions
2197   void lock()   EXCLUSIVE_LOCK_FUNCTION() { mu_.Lock();   }
2198   void unlock() UNLOCK_FUNCTION()         { mu_.Unlock(); }
2199 
2200   // don't check inside constructors and destructors
2201   MyLock2()  { foo = 1; }
2202   ~MyLock2() { foo = 0; }
2203 };
2204 
2205 
2206 } // end namespace SelfLockingTest
2207 
2208 
2209 namespace InvalidNonstatic {
2210 
2211 // Forward decl here causes bogus "invalid use of non-static data member"
2212 // on reference to mutex_ in guarded_by attribute.
2213 class Foo;
2214 
2215 class Foo {
2216   Mutex* mutex_;
2217 
2218   int foo __attribute__((guarded_by(mutex_)));
2219 };
2220 
2221 }  // end namespace InvalidNonStatic
2222 
2223 
2224 namespace NoReturnTest {
2225 
2226 bool condition();
2227 void fatal() __attribute__((noreturn));
2228 
2229 Mutex mu_;
2230 
2231 void test1() {
2232   MutexLock lock(&mu_);
2233   if (condition()) {
2234     fatal();
2235     return;
2236   }
2237 }
2238 
2239 } // end namespace NoReturnTest
2240 
2241 
2242 namespace TestMultiDecl {
2243 
2244 class Foo {
2245 public:
2246   int GUARDED_BY(mu_) a;
2247   int GUARDED_BY(mu_) b, c;
2248 
2249   void foo() {
2250     a = 0; // \
2251       // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}}
2252     b = 0; // \
2253       // expected-warning {{writing variable 'b' requires holding mutex 'mu_' exclusively}}
2254     c = 0; // \
2255       // expected-warning {{writing variable 'c' requires holding mutex 'mu_' exclusively}}
2256   }
2257 
2258 private:
2259   Mutex mu_;
2260 };
2261 
2262 } // end namespace TestMultiDecl
2263 
2264 
2265 namespace WarnNoDecl {
2266 
2267 class Foo {
2268   void foo(int a);  __attribute__(( // \
2269     // expected-warning {{declaration does not declare anything}}
2270     exclusive_locks_required(a))); // \
2271     // expected-warning {{attribute exclusive_locks_required ignored}}
2272 };
2273 
2274 } // end namespace WarnNoDecl
2275 
2276 
2277 
2278 namespace MoreLockExpressions {
2279 
2280 class Foo {
2281 public:
2282   Mutex mu_;
2283   int a GUARDED_BY(mu_);
2284 };
2285 
2286 class Bar {
2287 public:
2288   int b;
2289   Foo* f;
2290 
2291   Foo& getFoo()              { return *f; }
2292   Foo& getFoo2(int c)        { return *f; }
2293   Foo& getFoo3(int c, int d) { return *f; }
2294 
2295   Foo& getFooey() { return *f; }
2296 };
2297 
2298 Foo& getBarFoo(Bar &bar, int c) { return bar.getFoo2(c); }
2299 
2300 void test() {
2301   Foo foo;
2302   Foo *fooArray;
2303   Bar bar;
2304   int a;
2305   int b;
2306   int c;
2307 
2308   bar.getFoo().mu_.Lock();
2309   bar.getFoo().a = 0;
2310   bar.getFoo().mu_.Unlock();
2311 
2312   (bar.getFoo().mu_).Lock();   // test parenthesis
2313   bar.getFoo().a = 0;
2314   (bar.getFoo().mu_).Unlock();
2315 
2316   bar.getFoo2(a).mu_.Lock();
2317   bar.getFoo2(a).a = 0;
2318   bar.getFoo2(a).mu_.Unlock();
2319 
2320   bar.getFoo3(a, b).mu_.Lock();
2321   bar.getFoo3(a, b).a = 0;
2322   bar.getFoo3(a, b).mu_.Unlock();
2323 
2324   getBarFoo(bar, a).mu_.Lock();
2325   getBarFoo(bar, a).a = 0;
2326   getBarFoo(bar, a).mu_.Unlock();
2327 
2328   bar.getFoo2(10).mu_.Lock();
2329   bar.getFoo2(10).a = 0;
2330   bar.getFoo2(10).mu_.Unlock();
2331 
2332   bar.getFoo2(a + 1).mu_.Lock();
2333   bar.getFoo2(a + 1).a = 0;
2334   bar.getFoo2(a + 1).mu_.Unlock();
2335 
2336   (a > 0 ? fooArray[1] : fooArray[b]).mu_.Lock();
2337   (a > 0 ? fooArray[1] : fooArray[b]).a = 0;
2338   (a > 0 ? fooArray[1] : fooArray[b]).mu_.Unlock();
2339 }
2340 
2341 
2342 void test2() {
2343   Foo *fooArray;
2344   Bar bar;
2345   int a;
2346   int b;
2347   int c;
2348 
2349   bar.getFoo().mu_.Lock();
2350   bar.getFooey().a = 0; // \
2351     // expected-warning {{writing variable 'a' requires holding mutex 'bar.getFooey().mu_' exclusively}} \
2352     // expected-note {{found near match 'bar.getFoo().mu_'}}
2353   bar.getFoo().mu_.Unlock();
2354 
2355   bar.getFoo2(a).mu_.Lock();
2356   bar.getFoo2(b).a = 0; // \
2357     // expected-warning {{writing variable 'a' requires holding mutex 'bar.getFoo2(b).mu_' exclusively}} \
2358     // expected-note {{found near match 'bar.getFoo2(a).mu_'}}
2359   bar.getFoo2(a).mu_.Unlock();
2360 
2361   bar.getFoo3(a, b).mu_.Lock();
2362   bar.getFoo3(a, c).a = 0;  // \
2363     // expected-warning {{writing variable 'a' requires holding mutex 'bar.getFoo3(a, c).mu_' exclusively}} \
2364     // expected-note {{found near match 'bar.getFoo3(a, b).mu_'}}
2365   bar.getFoo3(a, b).mu_.Unlock();
2366 
2367   getBarFoo(bar, a).mu_.Lock();
2368   getBarFoo(bar, b).a = 0;  // \
2369     // expected-warning {{writing variable 'a' requires holding mutex 'getBarFoo(bar, b).mu_' exclusively}} \
2370     // expected-note {{found near match 'getBarFoo(bar, a).mu_'}}
2371   getBarFoo(bar, a).mu_.Unlock();
2372 
2373   (a > 0 ? fooArray[1] : fooArray[b]).mu_.Lock();
2374   (a > 0 ? fooArray[b] : fooArray[c]).a = 0; // \
2375     // expected-warning {{writing variable 'a' requires holding mutex '((0 < a) ? fooArray[b] : fooArray[c]).mu_' exclusively}} \
2376     // expected-note {{found near match '((0 < a) ? fooArray[1] : fooArray[b]).mu_'}}
2377   (a > 0 ? fooArray[1] : fooArray[b]).mu_.Unlock();
2378 }
2379 
2380 
2381 } // end namespace MoreLockExpressions
2382 
2383 
2384 namespace TrylockJoinPoint {
2385 
2386 class Foo {
2387   Mutex mu;
2388   bool c;
2389 
2390   void foo() {
2391     if (c) {
2392       if (!mu.TryLock())
2393         return;
2394     } else {
2395       mu.Lock();
2396     }
2397     mu.Unlock();
2398   }
2399 };
2400 
2401 } // end namespace TrylockJoinPoint
2402 
2403 
2404 namespace LockReturned {
2405 
2406 class Foo {
2407 public:
2408   int a             GUARDED_BY(mu_);
2409   void foo()        EXCLUSIVE_LOCKS_REQUIRED(mu_);
2410   void foo2(Foo* f) EXCLUSIVE_LOCKS_REQUIRED(mu_, f->mu_);
2411 
2412   static void sfoo(Foo* f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_);
2413 
2414   Mutex* getMu() LOCK_RETURNED(mu_);
2415 
2416   Mutex mu_;
2417 
2418   static Mutex* getMu(Foo* f) LOCK_RETURNED(f->mu_);
2419 };
2420 
2421 
2422 // Calls getMu() directly to lock and unlock
2423 void test1(Foo* f1, Foo* f2) {
2424   f1->a = 0;       // expected-warning {{writing variable 'a' requires holding mutex 'f1->mu_' exclusively}}
2425   f1->foo();       // expected-warning {{calling function 'foo' requires holding mutex 'f1->mu_' exclusively}}
2426 
2427   f1->foo2(f2);    // expected-warning {{calling function 'foo2' requires holding mutex 'f1->mu_' exclusively}} \
2428                    // expected-warning {{calling function 'foo2' requires holding mutex 'f2->mu_' exclusively}}
2429   Foo::sfoo(f1);   // expected-warning {{calling function 'sfoo' requires holding mutex 'f1->mu_' exclusively}}
2430 
2431   f1->getMu()->Lock();
2432 
2433   f1->a = 0;
2434   f1->foo();
2435   f1->foo2(f2); // \
2436     // expected-warning {{calling function 'foo2' requires holding mutex 'f2->mu_' exclusively}} \
2437     // expected-note {{found near match 'f1->mu_'}}
2438 
2439   Foo::getMu(f2)->Lock();
2440   f1->foo2(f2);
2441   Foo::getMu(f2)->Unlock();
2442 
2443   Foo::sfoo(f1);
2444 
2445   f1->getMu()->Unlock();
2446 }
2447 
2448 
2449 Mutex* getFooMu(Foo* f) LOCK_RETURNED(Foo::getMu(f));
2450 
2451 class Bar : public Foo {
2452 public:
2453   int  b            GUARDED_BY(getMu());
2454   void bar()        EXCLUSIVE_LOCKS_REQUIRED(getMu());
2455   void bar2(Bar* g) EXCLUSIVE_LOCKS_REQUIRED(getMu(this), g->getMu());
2456 
2457   static void sbar(Bar* g)  EXCLUSIVE_LOCKS_REQUIRED(g->getMu());
2458   static void sbar2(Bar* g) EXCLUSIVE_LOCKS_REQUIRED(getFooMu(g));
2459 };
2460 
2461 
2462 
2463 // Use getMu() within other attributes.
2464 // This requires at lest levels of substitution, more in the case of
2465 void test2(Bar* b1, Bar* b2) {
2466   b1->b = 0;       // expected-warning {{writing variable 'b' requires holding mutex 'b1->mu_' exclusively}}
2467   b1->bar();       // expected-warning {{calling function 'bar' requires holding mutex 'b1->mu_' exclusively}}
2468   b1->bar2(b2);    // expected-warning {{calling function 'bar2' requires holding mutex 'b1->mu_' exclusively}} \
2469                    // expected-warning {{calling function 'bar2' requires holding mutex 'b2->mu_' exclusively}}
2470   Bar::sbar(b1);   // expected-warning {{calling function 'sbar' requires holding mutex 'b1->mu_' exclusively}}
2471   Bar::sbar2(b1);  // expected-warning {{calling function 'sbar2' requires holding mutex 'b1->mu_' exclusively}}
2472 
2473   b1->getMu()->Lock();
2474 
2475   b1->b = 0;
2476   b1->bar();
2477   b1->bar2(b2);  // \
2478     // expected-warning {{calling function 'bar2' requires holding mutex 'b2->mu_' exclusively}} \
2479     // // expected-note {{found near match 'b1->mu_'}}
2480 
2481   b2->getMu()->Lock();
2482   b1->bar2(b2);
2483 
2484   b2->getMu()->Unlock();
2485 
2486   Bar::sbar(b1);
2487   Bar::sbar2(b1);
2488 
2489   b1->getMu()->Unlock();
2490 }
2491 
2492 
2493 // Sanity check -- lock the mutex directly, but use attributes that call getMu()
2494 // Also lock the mutex using getFooMu, which calls a lock_returned function.
2495 void test3(Bar* b1, Bar* b2) {
2496   b1->mu_.Lock();
2497   b1->b = 0;
2498   b1->bar();
2499 
2500   getFooMu(b2)->Lock();
2501   b1->bar2(b2);
2502   getFooMu(b2)->Unlock();
2503 
2504   Bar::sbar(b1);
2505   Bar::sbar2(b1);
2506 
2507   b1->mu_.Unlock();
2508 }
2509 
2510 } // end namespace LockReturned
2511 
2512 
2513 namespace ReleasableScopedLock {
2514 
2515 class Foo {
2516   Mutex mu_;
2517   bool c;
2518   int a GUARDED_BY(mu_);
2519 
2520   void test1();
2521   void test2();
2522   void test3();
2523   void test4();
2524   void test5();
2525 };
2526 
2527 
2528 void Foo::test1() {
2529   ReleasableMutexLock rlock(&mu_);
2530   rlock.Release();
2531 }
2532 
2533 void Foo::test2() {
2534   ReleasableMutexLock rlock(&mu_);
2535   if (c) {            // test join point -- held/not held during release
2536     rlock.Release();
2537   }
2538 }
2539 
2540 void Foo::test3() {
2541   ReleasableMutexLock rlock(&mu_);
2542   a = 0;
2543   rlock.Release();
2544   a = 1;  // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}}
2545 }
2546 
2547 void Foo::test4() {
2548   ReleasableMutexLock rlock(&mu_);
2549   rlock.Release();
2550   rlock.Release();  // expected-warning {{releasing mutex 'mu_' that was not held}}
2551 }
2552 
2553 void Foo::test5() {
2554   ReleasableMutexLock rlock(&mu_);
2555   if (c) {
2556     rlock.Release();
2557   }
2558   // no warning on join point for managed lock.
2559   rlock.Release();  // expected-warning {{releasing mutex 'mu_' that was not held}}
2560 }
2561 
2562 
2563 } // end namespace ReleasableScopedLock
2564 
2565 
2566 namespace TrylockFunctionTest {
2567 
2568 class Foo {
2569 public:
2570   Mutex mu1_;
2571   Mutex mu2_;
2572   bool c;
2573 
2574   bool lockBoth() EXCLUSIVE_TRYLOCK_FUNCTION(true, mu1_, mu2_);
2575 };
2576 
2577 bool Foo::lockBoth() {
2578   if (!mu1_.TryLock())
2579     return false;
2580 
2581   mu2_.Lock();
2582   if (!c) {
2583     mu1_.Unlock();
2584     mu2_.Unlock();
2585     return false;
2586   }
2587 
2588   return true;
2589 }
2590 
2591 
2592 }  // end namespace TrylockFunctionTest
2593 
2594 
2595 
2596 namespace DoubleLockBug {
2597 
2598 class Foo {
2599 public:
2600   Mutex mu_;
2601   int a GUARDED_BY(mu_);
2602 
2603   void foo1() EXCLUSIVE_LOCKS_REQUIRED(mu_);
2604   int  foo2() SHARED_LOCKS_REQUIRED(mu_);
2605 };
2606 
2607 
2608 void Foo::foo1() EXCLUSIVE_LOCKS_REQUIRED(mu_) {
2609   a = 0;
2610 }
2611 
2612 int Foo::foo2() SHARED_LOCKS_REQUIRED(mu_) {
2613   return a;
2614 }
2615 
2616 }
2617 
2618 
2619 
2620 namespace UnlockBug {
2621 
2622 class Foo {
2623 public:
2624   Mutex mutex_;
2625 
2626   void foo1() EXCLUSIVE_LOCKS_REQUIRED(mutex_) {  // expected-note {{mutex acquired here}}
2627     mutex_.Unlock();
2628   }  // expected-warning {{expecting mutex 'mutex_' to be held at the end of function}}
2629 
2630 
2631   void foo2() SHARED_LOCKS_REQUIRED(mutex_) {   // expected-note {{mutex acquired here}}
2632     mutex_.Unlock();
2633   }  // expected-warning {{expecting mutex 'mutex_' to be held at the end of function}}
2634 };
2635 
2636 } // end namespace UnlockBug
2637 
2638 
2639 
2640 namespace FoolishScopedLockableBug {
2641 
2642 class SCOPED_LOCKABLE WTF_ScopedLockable {
2643 public:
2644   WTF_ScopedLockable(Mutex* mu) EXCLUSIVE_LOCK_FUNCTION(mu);
2645 
2646   // have to call release() manually;
2647   ~WTF_ScopedLockable();
2648 
2649   void release() UNLOCK_FUNCTION();
2650 };
2651 
2652 
2653 class Foo {
2654   Mutex mu_;
2655   int a GUARDED_BY(mu_);
2656   bool c;
2657 
2658   void doSomething();
2659 
2660   void test1() {
2661     WTF_ScopedLockable wtf(&mu_);
2662     wtf.release();
2663   }
2664 
2665   void test2() {
2666     WTF_ScopedLockable wtf(&mu_);  // expected-note {{mutex acquired here}}
2667   }  // expected-warning {{mutex 'mu_' is still held at the end of function}}
2668 
2669   void test3() {
2670     if (c) {
2671       WTF_ScopedLockable wtf(&mu_);
2672       wtf.release();
2673     }
2674   }
2675 
2676   void test4() {
2677     if (c) {
2678       doSomething();
2679     }
2680     else {
2681       WTF_ScopedLockable wtf(&mu_);
2682       wtf.release();
2683     }
2684   }
2685 
2686   void test5() {
2687     if (c) {
2688       WTF_ScopedLockable wtf(&mu_);  // expected-note {{mutex acquired here}}
2689     }
2690   } // expected-warning {{mutex 'mu_' is not held on every path through here}}
2691 
2692   void test6() {
2693     if (c) {
2694       doSomething();
2695     }
2696     else {
2697       WTF_ScopedLockable wtf(&mu_);  // expected-note {{mutex acquired here}}
2698     }
2699   } // expected-warning {{mutex 'mu_' is not held on every path through here}}
2700 };
2701 
2702 
2703 } // end namespace FoolishScopedLockableBug
2704 
2705 
2706 
2707 namespace TemporaryCleanupExpr {
2708 
2709 class Foo {
2710   int a GUARDED_BY(getMutexPtr().get());
2711 
2712   SmartPtr<Mutex> getMutexPtr();
2713 
2714   void test();
2715 };
2716 
2717 
2718 void Foo::test() {
2719   {
2720     ReaderMutexLock lock(getMutexPtr().get());
2721     int b = a;
2722   }
2723   int b = a;  // expected-warning {{reading variable 'a' requires holding mutex 'getMutexPtr()'}}
2724 }
2725 
2726 } // end namespace TemporaryCleanupExpr
2727 
2728 
2729 
2730 namespace SmartPointerTests {
2731 
2732 class Foo {
2733 public:
2734   SmartPtr<Mutex> mu_;
2735   int a GUARDED_BY(mu_);
2736   int b GUARDED_BY(mu_.get());
2737   int c GUARDED_BY(*mu_);
2738 
2739   void Lock()   EXCLUSIVE_LOCK_FUNCTION(mu_);
2740   void Unlock() UNLOCK_FUNCTION(mu_);
2741 
2742   void test0();
2743   void test1();
2744   void test2();
2745   void test3();
2746   void test4();
2747   void test5();
2748   void test6();
2749   void test7();
2750   void test8();
2751 };
2752 
2753 void Foo::test0() {
2754   a = 0;  // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}}
2755   b = 0;  // expected-warning {{writing variable 'b' requires holding mutex 'mu_' exclusively}}
2756   c = 0;  // expected-warning {{writing variable 'c' requires holding mutex 'mu_' exclusively}}
2757 }
2758 
2759 void Foo::test1() {
2760   mu_->Lock();
2761   a = 0;
2762   b = 0;
2763   c = 0;
2764   mu_->Unlock();
2765 }
2766 
2767 void Foo::test2() {
2768   (*mu_).Lock();
2769   a = 0;
2770   b = 0;
2771   c = 0;
2772   (*mu_).Unlock();
2773 }
2774 
2775 
2776 void Foo::test3() {
2777   mu_.get()->Lock();
2778   a = 0;
2779   b = 0;
2780   c = 0;
2781   mu_.get()->Unlock();
2782 }
2783 
2784 
2785 void Foo::test4() {
2786   MutexLock lock(mu_.get());
2787   a = 0;
2788   b = 0;
2789   c = 0;
2790 }
2791 
2792 
2793 void Foo::test5() {
2794   MutexLock lock(&(*mu_));
2795   a = 0;
2796   b = 0;
2797   c = 0;
2798 }
2799 
2800 
2801 void Foo::test6() {
2802   Lock();
2803   a = 0;
2804   b = 0;
2805   c = 0;
2806   Unlock();
2807 }
2808 
2809 
2810 void Foo::test7() {
2811   {
2812     Lock();
2813     mu_->Unlock();
2814   }
2815   {
2816     mu_->Lock();
2817     Unlock();
2818   }
2819   {
2820     mu_.get()->Lock();
2821     mu_->Unlock();
2822   }
2823   {
2824     mu_->Lock();
2825     mu_.get()->Unlock();
2826   }
2827   {
2828     mu_.get()->Lock();
2829     (*mu_).Unlock();
2830   }
2831   {
2832     (*mu_).Lock();
2833     mu_->Unlock();
2834   }
2835 }
2836 
2837 
2838 void Foo::test8() {
2839   mu_->Lock();
2840   mu_.get()->Lock();    // expected-warning {{acquiring mutex 'mu_' that is already held}}
2841   (*mu_).Lock();        // expected-warning {{acquiring mutex 'mu_' that is already held}}
2842   mu_.get()->Unlock();
2843   Unlock();             // expected-warning {{releasing mutex 'mu_' that was not held}}
2844 }
2845 
2846 
2847 class Bar {
2848   SmartPtr<Foo> foo;
2849 
2850   void test0();
2851   void test1();
2852   void test2();
2853   void test3();
2854 };
2855 
2856 
2857 void Bar::test0() {
2858   foo->a = 0;         // expected-warning {{writing variable 'a' requires holding mutex 'foo->mu_' exclusively}}
2859   (*foo).b = 0;       // expected-warning {{writing variable 'b' requires holding mutex 'foo->mu_' exclusively}}
2860   foo.get()->c = 0;   // expected-warning {{writing variable 'c' requires holding mutex 'foo->mu_' exclusively}}
2861 }
2862 
2863 
2864 void Bar::test1() {
2865   foo->mu_->Lock();
2866   foo->a = 0;
2867   (*foo).b = 0;
2868   foo.get()->c = 0;
2869   foo->mu_->Unlock();
2870 }
2871 
2872 
2873 void Bar::test2() {
2874   (*foo).mu_->Lock();
2875   foo->a = 0;
2876   (*foo).b = 0;
2877   foo.get()->c = 0;
2878   foo.get()->mu_->Unlock();
2879 }
2880 
2881 
2882 void Bar::test3() {
2883   MutexLock lock(foo->mu_.get());
2884   foo->a = 0;
2885   (*foo).b = 0;
2886   foo.get()->c = 0;
2887 }
2888 
2889 }  // end namespace SmartPointerTests
2890 
2891 
2892 
2893 namespace DuplicateAttributeTest {
2894 
2895 class LOCKABLE Foo {
2896 public:
2897   Mutex mu1_;
2898   Mutex mu2_;
2899   Mutex mu3_;
2900   int a GUARDED_BY(mu1_);
2901   int b GUARDED_BY(mu2_);
2902   int c GUARDED_BY(mu3_);
2903 
2904   void lock()   EXCLUSIVE_LOCK_FUNCTION();
2905   void unlock() UNLOCK_FUNCTION();
2906 
2907   void lock1()  EXCLUSIVE_LOCK_FUNCTION(mu1_);
2908   void slock1() SHARED_LOCK_FUNCTION(mu1_);
2909   void lock3()  EXCLUSIVE_LOCK_FUNCTION(mu1_, mu2_, mu3_);
2910   void locklots()
2911     EXCLUSIVE_LOCK_FUNCTION(mu1_)
2912     EXCLUSIVE_LOCK_FUNCTION(mu2_)
2913     EXCLUSIVE_LOCK_FUNCTION(mu1_, mu2_, mu3_);
2914 
2915   void unlock1() UNLOCK_FUNCTION(mu1_);
2916   void unlock3() UNLOCK_FUNCTION(mu1_, mu2_, mu3_);
2917   void unlocklots()
2918     UNLOCK_FUNCTION(mu1_)
2919     UNLOCK_FUNCTION(mu2_)
2920     UNLOCK_FUNCTION(mu1_, mu2_, mu3_);
2921 };
2922 
2923 
2924 void Foo::lock()   EXCLUSIVE_LOCK_FUNCTION() { }
2925 void Foo::unlock() UNLOCK_FUNCTION()         { }
2926 
2927 void Foo::lock1()  EXCLUSIVE_LOCK_FUNCTION(mu1_) {
2928   mu1_.Lock();
2929 }
2930 
2931 void Foo::slock1() SHARED_LOCK_FUNCTION(mu1_) {
2932   mu1_.ReaderLock();
2933 }
2934 
2935 void Foo::lock3()  EXCLUSIVE_LOCK_FUNCTION(mu1_, mu2_, mu3_) {
2936   mu1_.Lock();
2937   mu2_.Lock();
2938   mu3_.Lock();
2939 }
2940 
2941 void Foo::locklots()
2942     EXCLUSIVE_LOCK_FUNCTION(mu1_, mu2_)
2943     EXCLUSIVE_LOCK_FUNCTION(mu2_, mu3_) {
2944   mu1_.Lock();
2945   mu2_.Lock();
2946   mu3_.Lock();
2947 }
2948 
2949 void Foo::unlock1() UNLOCK_FUNCTION(mu1_) {
2950   mu1_.Unlock();
2951 }
2952 
2953 void Foo::unlock3() UNLOCK_FUNCTION(mu1_, mu2_, mu3_) {
2954   mu1_.Unlock();
2955   mu2_.Unlock();
2956   mu3_.Unlock();
2957 }
2958 
2959 void Foo::unlocklots()
2960     UNLOCK_FUNCTION(mu1_, mu2_)
2961     UNLOCK_FUNCTION(mu2_, mu3_) {
2962   mu1_.Unlock();
2963   mu2_.Unlock();
2964   mu3_.Unlock();
2965 }
2966 
2967 
2968 void test0() {
2969   Foo foo;
2970   foo.lock();
2971   foo.unlock();
2972 
2973   foo.lock();
2974   foo.lock();     // expected-warning {{acquiring mutex 'foo' that is already held}}
2975   foo.unlock();
2976   foo.unlock();   // expected-warning {{releasing mutex 'foo' that was not held}}
2977 }
2978 
2979 
2980 void test1() {
2981   Foo foo;
2982   foo.lock1();
2983   foo.a = 0;
2984   foo.unlock1();
2985 
2986   foo.lock1();
2987   foo.lock1();    // expected-warning {{acquiring mutex 'foo.mu1_' that is already held}}
2988   foo.a = 0;
2989   foo.unlock1();
2990   foo.unlock1();  // expected-warning {{releasing mutex 'foo.mu1_' that was not held}}
2991 }
2992 
2993 
2994 int test2() {
2995   Foo foo;
2996   foo.slock1();
2997   int d1 = foo.a;
2998   foo.unlock1();
2999 
3000   foo.slock1();
3001   foo.slock1();    // expected-warning {{acquiring mutex 'foo.mu1_' that is already held}}
3002   int d2 = foo.a;
3003   foo.unlock1();
3004   foo.unlock1();   // expected-warning {{releasing mutex 'foo.mu1_' that was not held}}
3005   return d1 + d2;
3006 }
3007 
3008 
3009 void test3() {
3010   Foo foo;
3011   foo.lock3();
3012   foo.a = 0;
3013   foo.b = 0;
3014   foo.c = 0;
3015   foo.unlock3();
3016 
3017   foo.lock3();
3018   foo.lock3(); // \
3019     // expected-warning {{acquiring mutex 'foo.mu1_' that is already held}} \
3020     // expected-warning {{acquiring mutex 'foo.mu2_' that is already held}} \
3021     // expected-warning {{acquiring mutex 'foo.mu3_' that is already held}}
3022   foo.a = 0;
3023   foo.b = 0;
3024   foo.c = 0;
3025   foo.unlock3();
3026   foo.unlock3(); // \
3027     // expected-warning {{releasing mutex 'foo.mu1_' that was not held}} \
3028     // expected-warning {{releasing mutex 'foo.mu2_' that was not held}} \
3029     // expected-warning {{releasing mutex 'foo.mu3_' that was not held}}
3030 }
3031 
3032 
3033 void testlots() {
3034   Foo foo;
3035   foo.locklots();
3036   foo.a = 0;
3037   foo.b = 0;
3038   foo.c = 0;
3039   foo.unlocklots();
3040 
3041   foo.locklots();
3042   foo.locklots(); // \
3043     // expected-warning {{acquiring mutex 'foo.mu1_' that is already held}} \
3044     // expected-warning {{acquiring mutex 'foo.mu2_' that is already held}} \
3045     // expected-warning {{acquiring mutex 'foo.mu3_' that is already held}}
3046   foo.a = 0;
3047   foo.b = 0;
3048   foo.c = 0;
3049   foo.unlocklots();
3050   foo.unlocklots(); // \
3051     // expected-warning {{releasing mutex 'foo.mu1_' that was not held}} \
3052     // expected-warning {{releasing mutex 'foo.mu2_' that was not held}} \
3053     // expected-warning {{releasing mutex 'foo.mu3_' that was not held}}
3054 }
3055 
3056 }  // end namespace DuplicateAttributeTest
3057 
3058 
3059 
3060 namespace TryLockEqTest {
3061 
3062 class Foo {
3063   Mutex mu_;
3064   int a GUARDED_BY(mu_);
3065   bool c;
3066 
3067   int    tryLockMutexI() EXCLUSIVE_TRYLOCK_FUNCTION(1, mu_);
3068   Mutex* tryLockMutexP() EXCLUSIVE_TRYLOCK_FUNCTION(1, mu_);
3069   void unlock() UNLOCK_FUNCTION(mu_);
3070 
3071   void test1();
3072   void test2();
3073 };
3074 
3075 
3076 void Foo::test1() {
3077   if (tryLockMutexP() == 0) {
3078     a = 0;  // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}}
3079     return;
3080   }
3081   a = 0;
3082   unlock();
3083 
3084   if (tryLockMutexP() != 0) {
3085     a = 0;
3086     unlock();
3087   }
3088 
3089   if (0 != tryLockMutexP()) {
3090     a = 0;
3091     unlock();
3092   }
3093 
3094   if (!(tryLockMutexP() == 0)) {
3095     a = 0;
3096     unlock();
3097   }
3098 
3099   if (tryLockMutexI() == 0) {
3100     a = 0;   // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}}
3101     return;
3102   }
3103   a = 0;
3104   unlock();
3105 
3106   if (0 == tryLockMutexI()) {
3107     a = 0;   // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}}
3108     return;
3109   }
3110   a = 0;
3111   unlock();
3112 
3113   if (tryLockMutexI() == 1) {
3114     a = 0;
3115     unlock();
3116   }
3117 
3118   if (mu_.TryLock() == false) {
3119     a = 0;   // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}}
3120     return;
3121   }
3122   a = 0;
3123   unlock();
3124 
3125   if (mu_.TryLock() == true) {
3126     a = 0;
3127     unlock();
3128   }
3129   else {
3130     a = 0;  // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}}
3131   }
3132 
3133 #if __has_feature(cxx_nullptr)
3134   if (tryLockMutexP() == nullptr) {
3135     a = 0;  // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}}
3136     return;
3137   }
3138   a = 0;
3139   unlock();
3140 #endif
3141 }
3142 
3143 } // end namespace TryLockEqTest
3144 
3145 
3146 namespace ExistentialPatternMatching {
3147 
3148 class Graph {
3149 public:
3150   Mutex mu_;
3151 };
3152 
3153 void LockAllGraphs()   EXCLUSIVE_LOCK_FUNCTION(&Graph::mu_);
3154 void UnlockAllGraphs() UNLOCK_FUNCTION(&Graph::mu_);
3155 
3156 class Node {
3157 public:
3158   int a GUARDED_BY(&Graph::mu_);
3159 
3160   void foo()  EXCLUSIVE_LOCKS_REQUIRED(&Graph::mu_) {
3161     a = 0;
3162   }
3163   void foo2() LOCKS_EXCLUDED(&Graph::mu_);
3164 };
3165 
3166 void test() {
3167   Graph g1;
3168   Graph g2;
3169   Node n1;
3170 
3171   n1.a = 0;   // expected-warning {{writing variable 'a' requires holding mutex '&ExistentialPatternMatching::Graph::mu_' exclusively}}
3172   n1.foo();   // expected-warning {{calling function 'foo' requires holding mutex '&ExistentialPatternMatching::Graph::mu_' exclusively}}
3173   n1.foo2();
3174 
3175   g1.mu_.Lock();
3176   n1.a = 0;
3177   n1.foo();
3178   n1.foo2();  // expected-warning {{cannot call function 'foo2' while mutex '&ExistentialPatternMatching::Graph::mu_' is held}}
3179   g1.mu_.Unlock();
3180 
3181   g2.mu_.Lock();
3182   n1.a = 0;
3183   n1.foo();
3184   n1.foo2();  // expected-warning {{cannot call function 'foo2' while mutex '&ExistentialPatternMatching::Graph::mu_' is held}}
3185   g2.mu_.Unlock();
3186 
3187   LockAllGraphs();
3188   n1.a = 0;
3189   n1.foo();
3190   n1.foo2();  // expected-warning {{cannot call function 'foo2' while mutex '&ExistentialPatternMatching::Graph::mu_' is held}}
3191   UnlockAllGraphs();
3192 
3193   LockAllGraphs();
3194   g1.mu_.Unlock();
3195 
3196   LockAllGraphs();
3197   g2.mu_.Unlock();
3198 
3199   LockAllGraphs();
3200   g1.mu_.Lock();  // expected-warning {{acquiring mutex 'g1.mu_' that is already held}}
3201   g1.mu_.Unlock();
3202 }
3203 
3204 } // end namespace ExistentialPatternMatching
3205 
3206 
3207 namespace StringIgnoreTest {
3208 
3209 class Foo {
3210 public:
3211   Mutex mu_;
3212   void lock()   EXCLUSIVE_LOCK_FUNCTION("");
3213   void unlock() UNLOCK_FUNCTION("");
3214   void goober() EXCLUSIVE_LOCKS_REQUIRED("");
3215   void roober() SHARED_LOCKS_REQUIRED("");
3216 };
3217 
3218 
3219 class Bar : public Foo {
3220 public:
3221   void bar(Foo* f) {
3222     f->unlock();
3223     f->goober();
3224     f->roober();
3225     f->lock();
3226   };
3227 };
3228 
3229 } // end namespace StringIgnoreTest
3230 
3231 
3232 namespace LockReturnedScopeFix {
3233 
3234 class Base {
3235 protected:
3236   struct Inner;
3237   bool c;
3238 
3239   const Mutex& getLock(const Inner* i);
3240 
3241   void lockInner  (Inner* i) EXCLUSIVE_LOCK_FUNCTION(getLock(i));
3242   void unlockInner(Inner* i) UNLOCK_FUNCTION(getLock(i));
3243   void foo(Inner* i) EXCLUSIVE_LOCKS_REQUIRED(getLock(i));
3244 
3245   void bar(Inner* i);
3246 };
3247 
3248 
3249 struct Base::Inner {
3250   Mutex lock_;
3251   void doSomething() EXCLUSIVE_LOCKS_REQUIRED(lock_);
3252 };
3253 
3254 
3255 const Mutex& Base::getLock(const Inner* i) LOCK_RETURNED(i->lock_) {
3256   return i->lock_;
3257 }
3258 
3259 
3260 void Base::foo(Inner* i) {
3261   i->doSomething();
3262 }
3263 
3264 void Base::bar(Inner* i) {
3265   if (c) {
3266     i->lock_.Lock();
3267     unlockInner(i);
3268   }
3269   else {
3270     lockInner(i);
3271     i->lock_.Unlock();
3272   }
3273 }
3274 
3275 } // end namespace LockReturnedScopeFix
3276 
3277 
3278 namespace TrylockWithCleanups {
3279 
3280 struct Foo {
3281   Mutex mu_;
3282   int a GUARDED_BY(mu_);
3283 };
3284 
3285 Foo* GetAndLockFoo(const MyString& s)
3286     EXCLUSIVE_TRYLOCK_FUNCTION(true, &Foo::mu_);
3287 
3288 static void test() {
3289   Foo* lt = GetAndLockFoo("foo");
3290   if (!lt) return;
3291   int a = lt->a;
3292   lt->mu_.Unlock();
3293 }
3294 
3295 }  // end namespace TrylockWithCleanups
3296 
3297 
3298 namespace UniversalLock {
3299 
3300 class Foo {
3301   Mutex mu_;
3302   bool c;
3303 
3304   int a        GUARDED_BY(mu_);
3305   void r_foo() SHARED_LOCKS_REQUIRED(mu_);
3306   void w_foo() EXCLUSIVE_LOCKS_REQUIRED(mu_);
3307 
3308   void test1() {
3309     int b;
3310 
3311     beginNoWarnOnReads();
3312     b = a;
3313     r_foo();
3314     endNoWarnOnReads();
3315 
3316     beginNoWarnOnWrites();
3317     a = 0;
3318     w_foo();
3319     endNoWarnOnWrites();
3320   }
3321 
3322   // don't warn on joins with universal lock
3323   void test2() {
3324     if (c) {
3325       beginNoWarnOnWrites();
3326     }
3327     a = 0; // \
3328       // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}}
3329     endNoWarnOnWrites();  // \
3330       // expected-warning {{releasing mutex '*' that was not held}}
3331   }
3332 
3333 
3334   // make sure the universal lock joins properly
3335   void test3() {
3336     if (c) {
3337       mu_.Lock();
3338       beginNoWarnOnWrites();
3339     }
3340     else {
3341       beginNoWarnOnWrites();
3342       mu_.Lock();
3343     }
3344     a = 0;
3345     endNoWarnOnWrites();
3346     mu_.Unlock();
3347   }
3348 
3349 
3350   // combine universal lock with other locks
3351   void test4() {
3352     beginNoWarnOnWrites();
3353     mu_.Lock();
3354     mu_.Unlock();
3355     endNoWarnOnWrites();
3356 
3357     mu_.Lock();
3358     beginNoWarnOnWrites();
3359     endNoWarnOnWrites();
3360     mu_.Unlock();
3361 
3362     mu_.Lock();
3363     beginNoWarnOnWrites();
3364     mu_.Unlock();
3365     endNoWarnOnWrites();
3366   }
3367 };
3368 
3369 }  // end namespace UniversalLock
3370 
3371 
3372 namespace TemplateLockReturned {
3373 
3374 template<class T>
3375 class BaseT {
3376 public:
3377   virtual void baseMethod() = 0;
3378   Mutex* get_mutex() LOCK_RETURNED(mutex_) { return &mutex_; }
3379 
3380   Mutex mutex_;
3381   int a GUARDED_BY(mutex_);
3382 };
3383 
3384 
3385 class Derived : public BaseT<int> {
3386 public:
3387   void baseMethod() EXCLUSIVE_LOCKS_REQUIRED(get_mutex()) {
3388     a = 0;
3389   }
3390 };
3391 
3392 }  // end namespace TemplateLockReturned
3393 
3394 
3395 namespace ExprMatchingBugFix {
3396 
3397 class Foo {
3398 public:
3399   Mutex mu_;
3400 };
3401 
3402 
3403 class Bar {
3404 public:
3405   bool c;
3406   Foo* foo;
3407   Bar(Foo* f) : foo(f) { }
3408 
3409   struct Nested {
3410     Foo* foo;
3411     Nested(Foo* f) : foo(f) { }
3412 
3413     void unlockFoo() UNLOCK_FUNCTION(&Foo::mu_);
3414   };
3415 
3416   void test();
3417 };
3418 
3419 
3420 void Bar::test() {
3421   foo->mu_.Lock();
3422   if (c) {
3423     Nested *n = new Nested(foo);
3424     n->unlockFoo();
3425   }
3426   else {
3427     foo->mu_.Unlock();
3428   }
3429 }
3430 
3431 }; // end namespace ExprMatchingBugfix
3432 
3433 
3434 namespace ComplexNameTest {
3435 
3436 class Foo {
3437 public:
3438   static Mutex mu_;
3439 
3440   Foo() EXCLUSIVE_LOCKS_REQUIRED(mu_)  { }
3441   ~Foo() EXCLUSIVE_LOCKS_REQUIRED(mu_) { }
3442 
3443   int operator[](int i) EXCLUSIVE_LOCKS_REQUIRED(mu_) { return 0; }
3444 };
3445 
3446 class Bar {
3447 public:
3448   static Mutex mu_;
3449 
3450   Bar()  LOCKS_EXCLUDED(mu_) { }
3451   ~Bar() LOCKS_EXCLUDED(mu_) { }
3452 
3453   int operator[](int i) LOCKS_EXCLUDED(mu_) { return 0; }
3454 };
3455 
3456 
3457 void test1() {
3458   Foo f;           // expected-warning {{calling function 'Foo' requires holding mutex 'mu_' exclusively}}
3459   int a = f[0];    // expected-warning {{calling function 'operator[]' requires holding mutex 'mu_' exclusively}}
3460 }                  // expected-warning {{calling function '~Foo' requires holding mutex 'mu_' exclusively}}
3461 
3462 
3463 void test2() {
3464   Bar::mu_.Lock();
3465   {
3466     Bar b;         // expected-warning {{cannot call function 'Bar' while mutex 'mu_' is held}}
3467     int a = b[0];  // expected-warning {{cannot call function 'operator[]' while mutex 'mu_' is held}}
3468   }                // expected-warning {{cannot call function '~Bar' while mutex 'mu_' is held}}
3469   Bar::mu_.Unlock();
3470 }
3471 
3472 };  // end namespace ComplexNameTest
3473 
3474 
3475 namespace UnreachableExitTest {
3476 
3477 class FemmeFatale {
3478 public:
3479   FemmeFatale();
3480   ~FemmeFatale() __attribute__((noreturn));
3481 };
3482 
3483 void exitNow() __attribute__((noreturn));
3484 void exitDestruct(const MyString& ms) __attribute__((noreturn));
3485 
3486 Mutex fatalmu_;
3487 
3488 void test1() EXCLUSIVE_LOCKS_REQUIRED(fatalmu_) {
3489   exitNow();
3490 }
3491 
3492 void test2() EXCLUSIVE_LOCKS_REQUIRED(fatalmu_) {
3493   FemmeFatale femme;
3494 }
3495 
3496 bool c;
3497 
3498 void test3() EXCLUSIVE_LOCKS_REQUIRED(fatalmu_) {
3499   if (c) {
3500     exitNow();
3501   }
3502   else {
3503     FemmeFatale femme;
3504   }
3505 }
3506 
3507 void test4() EXCLUSIVE_LOCKS_REQUIRED(fatalmu_) {
3508   exitDestruct("foo");
3509 }
3510 
3511 }   // end namespace UnreachableExitTest
3512 
3513 
3514 namespace VirtualMethodCanonicalizationTest {
3515 
3516 class Base {
3517 public:
3518   virtual Mutex* getMutex() = 0;
3519 };
3520 
3521 class Base2 : public Base {
3522 public:
3523   Mutex* getMutex();
3524 };
3525 
3526 class Base3 : public Base2 {
3527 public:
3528   Mutex* getMutex();
3529 };
3530 
3531 class Derived : public Base3 {
3532 public:
3533   Mutex* getMutex();  // overrides Base::getMutex()
3534 };
3535 
3536 void baseFun(Base *b) EXCLUSIVE_LOCKS_REQUIRED(b->getMutex()) { }
3537 
3538 void derivedFun(Derived *d) EXCLUSIVE_LOCKS_REQUIRED(d->getMutex()) {
3539   baseFun(d);
3540 }
3541 
3542 }  // end namespace VirtualMethodCanonicalizationTest
3543 
3544 
3545 namespace TemplateFunctionParamRemapTest {
3546 
3547 template <class T>
3548 struct Cell {
3549   T dummy_;
3550   Mutex* mu_;
3551 };
3552 
3553 class Foo {
3554 public:
3555   template <class T>
3556   void elr(Cell<T>* c) __attribute__((exclusive_locks_required(c->mu_)));
3557 
3558   void test();
3559 };
3560 
3561 template<class T>
3562 void Foo::elr(Cell<T>* c1) { }
3563 
3564 void Foo::test() {
3565   Cell<int> cell;
3566   elr(&cell); // \
3567     // expected-warning {{calling function 'elr' requires holding mutex 'cell.mu_' exclusively}}
3568 }
3569 
3570 
3571 template<class T>
3572 void globalELR(Cell<T>* c) __attribute__((exclusive_locks_required(c->mu_)));
3573 
3574 template<class T>
3575 void globalELR(Cell<T>* c1) { }
3576 
3577 void globalTest() {
3578   Cell<int> cell;
3579   globalELR(&cell); // \
3580     // expected-warning {{calling function 'globalELR' requires holding mutex 'cell.mu_' exclusively}}
3581 }
3582 
3583 
3584 template<class T>
3585 void globalELR2(Cell<T>* c) __attribute__((exclusive_locks_required(c->mu_)));
3586 
3587 // second declaration
3588 template<class T>
3589 void globalELR2(Cell<T>* c2);
3590 
3591 template<class T>
3592 void globalELR2(Cell<T>* c3) { }
3593 
3594 // re-declaration after definition
3595 template<class T>
3596 void globalELR2(Cell<T>* c4);
3597 
3598 void globalTest2() {
3599   Cell<int> cell;
3600   globalELR2(&cell); // \
3601     // expected-warning {{calling function 'globalELR2' requires holding mutex 'cell.mu_' exclusively}}
3602 }
3603 
3604 
3605 template<class T>
3606 class FooT {
3607 public:
3608   void elr(Cell<T>* c) __attribute__((exclusive_locks_required(c->mu_)));
3609 };
3610 
3611 template<class T>
3612 void FooT<T>::elr(Cell<T>* c1) { }
3613 
3614 void testFooT() {
3615   Cell<int> cell;
3616   FooT<int> foo;
3617   foo.elr(&cell); // \
3618     // expected-warning {{calling function 'elr' requires holding mutex 'cell.mu_' exclusively}}
3619 }
3620 
3621 }  // end namespace TemplateFunctionParamRemapTest
3622 
3623 
3624 namespace SelfConstructorTest {
3625 
3626 class SelfLock {
3627 public:
3628   SelfLock()  EXCLUSIVE_LOCK_FUNCTION(mu_);
3629   ~SelfLock() UNLOCK_FUNCTION(mu_);
3630 
3631   void foo() EXCLUSIVE_LOCKS_REQUIRED(mu_);
3632 
3633   Mutex mu_;
3634 };
3635 
3636 class LOCKABLE SelfLock2 {
3637 public:
3638   SelfLock2()  EXCLUSIVE_LOCK_FUNCTION();
3639   ~SelfLock2() UNLOCK_FUNCTION();
3640 
3641   void foo() EXCLUSIVE_LOCKS_REQUIRED(this);
3642 };
3643 
3644 
3645 void test() {
3646   SelfLock s;
3647   s.foo();
3648 }
3649 
3650 void test2() {
3651   SelfLock2 s2;
3652   s2.foo();
3653 }
3654 
3655 }  // end namespace SelfConstructorTest
3656 
3657 
3658 namespace MultipleAttributeTest {
3659 
3660 class Foo {
3661   Mutex mu1_;
3662   Mutex mu2_;
3663   int  a GUARDED_BY(mu1_);
3664   int  b GUARDED_BY(mu2_);
3665   int  c GUARDED_BY(mu1_)    GUARDED_BY(mu2_);
3666   int* d PT_GUARDED_BY(mu1_) PT_GUARDED_BY(mu2_);
3667 
3668   void foo1()          EXCLUSIVE_LOCKS_REQUIRED(mu1_)
3669                        EXCLUSIVE_LOCKS_REQUIRED(mu2_);
3670   void foo2()          SHARED_LOCKS_REQUIRED(mu1_)
3671                        SHARED_LOCKS_REQUIRED(mu2_);
3672   void foo3()          LOCKS_EXCLUDED(mu1_)
3673                        LOCKS_EXCLUDED(mu2_);
3674   void lock()          EXCLUSIVE_LOCK_FUNCTION(mu1_)
3675                        EXCLUSIVE_LOCK_FUNCTION(mu2_);
3676   void readerlock()    SHARED_LOCK_FUNCTION(mu1_)
3677                        SHARED_LOCK_FUNCTION(mu2_);
3678   void unlock()        UNLOCK_FUNCTION(mu1_)
3679                        UNLOCK_FUNCTION(mu2_);
3680   bool trylock()       EXCLUSIVE_TRYLOCK_FUNCTION(true, mu1_)
3681                        EXCLUSIVE_TRYLOCK_FUNCTION(true, mu2_);
3682   bool readertrylock() SHARED_TRYLOCK_FUNCTION(true, mu1_)
3683                        SHARED_TRYLOCK_FUNCTION(true, mu2_);
3684   void assertBoth() ASSERT_EXCLUSIVE_LOCK(mu1_)
3685                     ASSERT_EXCLUSIVE_LOCK(mu2_);
3686 
3687   void alsoAssertBoth() ASSERT_EXCLUSIVE_LOCK(mu1_, mu2_);
3688 
3689   void assertShared() ASSERT_SHARED_LOCK(mu1_)
3690                       ASSERT_SHARED_LOCK(mu2_);
3691 
3692   void alsoAssertShared() ASSERT_SHARED_LOCK(mu1_, mu2_);
3693 
3694   void test();
3695   void testAssert();
3696   void testAssertShared();
3697 };
3698 
3699 
3700 void Foo::foo1() {
3701   a = 1;
3702   b = 2;
3703 }
3704 
3705 void Foo::foo2() {
3706   int result = a + b;
3707 }
3708 
3709 void Foo::foo3() { }
3710 void Foo::lock() { mu1_.Lock();  mu2_.Lock(); }
3711 void Foo::readerlock() { mu1_.ReaderLock();  mu2_.ReaderLock(); }
3712 void Foo::unlock() { mu1_.Unlock();  mu2_.Unlock(); }
3713 bool Foo::trylock()       { return true; }
3714 bool Foo::readertrylock() { return true; }
3715 
3716 
3717 void Foo::test() {
3718   mu1_.Lock();
3719   foo1();             // expected-warning {{}}
3720   c = 0;              // expected-warning {{}}
3721   *d = 0;             // expected-warning {{}}
3722   mu1_.Unlock();
3723 
3724   mu1_.ReaderLock();
3725   foo2();             // expected-warning {{}}
3726   int x = c;          // expected-warning {{}}
3727   int y = *d;         // expected-warning {{}}
3728   mu1_.Unlock();
3729 
3730   mu2_.Lock();
3731   foo3();             // expected-warning {{}}
3732   mu2_.Unlock();
3733 
3734   lock();
3735   a = 0;
3736   b = 0;
3737   unlock();
3738 
3739   readerlock();
3740   int z = a + b;
3741   unlock();
3742 
3743   if (trylock()) {
3744     a = 0;
3745     b = 0;
3746     unlock();
3747   }
3748 
3749   if (readertrylock()) {
3750     int zz = a + b;
3751     unlock();
3752   }
3753 }
3754 
3755 // Force duplication of attributes
3756 void Foo::assertBoth() { }
3757 void Foo::alsoAssertBoth() { }
3758 void Foo::assertShared() { }
3759 void Foo::alsoAssertShared() { }
3760 
3761 void Foo::testAssert() {
3762   {
3763     assertBoth();
3764     a = 0;
3765     b = 0;
3766   }
3767   {
3768     alsoAssertBoth();
3769     a = 0;
3770     b = 0;
3771   }
3772 }
3773 
3774 void Foo::testAssertShared() {
3775   {
3776     assertShared();
3777     int zz = a + b;
3778   }
3779 
3780   {
3781     alsoAssertShared();
3782     int zz = a + b;
3783   }
3784 }
3785 
3786 
3787 }  // end namespace MultipleAttributeTest
3788 
3789 
3790 namespace GuardedNonPrimitiveTypeTest {
3791 
3792 
3793 class Data {
3794 public:
3795   Data(int i) : dat(i) { }
3796 
3797   int  getValue() const { return dat; }
3798   void setValue(int i)  { dat = i; }
3799 
3800   int  operator[](int i) const { return dat; }
3801   int& operator[](int i)       { return dat; }
3802 
3803   void operator()() { }
3804 
3805 private:
3806   int dat;
3807 };
3808 
3809 
3810 class DataCell {
3811 public:
3812   DataCell(const Data& d) : dat(d) { }
3813 
3814 private:
3815   Data dat;
3816 };
3817 
3818 
3819 void showDataCell(const DataCell& dc);
3820 
3821 
3822 class Foo {
3823 public:
3824   // method call tests
3825   void test() {
3826     data_.setValue(0);         // FIXME -- should be writing \
3827       // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}}
3828     int a = data_.getValue();  // \
3829       // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}}
3830 
3831     datap1_->setValue(0);      // FIXME -- should be writing \
3832       // expected-warning {{reading variable 'datap1_' requires holding mutex 'mu_'}}
3833     a = datap1_->getValue();   // \
3834       // expected-warning {{reading variable 'datap1_' requires holding mutex 'mu_'}}
3835 
3836     datap2_->setValue(0);      // FIXME -- should be writing \
3837       // expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}}
3838     a = datap2_->getValue();   // \
3839       // expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}}
3840 
3841     (*datap2_).setValue(0);    // FIXME -- should be writing \
3842       // expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}}
3843     a = (*datap2_).getValue(); // \
3844       // expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}}
3845 
3846     mu_.Lock();
3847     data_.setValue(1);
3848     datap1_->setValue(1);
3849     datap2_->setValue(1);
3850     mu_.Unlock();
3851 
3852     mu_.ReaderLock();
3853     a = data_.getValue();
3854     datap1_->setValue(0);  // reads datap1_, writes *datap1_
3855     a = datap1_->getValue();
3856     a = datap2_->getValue();
3857     mu_.Unlock();
3858   }
3859 
3860   // operator tests
3861   void test2() {
3862     data_    = Data(1);   // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}}
3863     *datap1_ = data_;     // expected-warning {{reading variable 'datap1_' requires holding mutex 'mu_'}} \
3864                           // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}}
3865     *datap2_ = data_;     // expected-warning {{writing the value pointed to by 'datap2_' requires holding mutex 'mu_' exclusively}} \
3866                           // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}}
3867     data_ = *datap1_;     // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} \
3868                           // expected-warning {{reading variable 'datap1_' requires holding mutex 'mu_'}}
3869     data_ = *datap2_;     // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} \
3870                           // expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}}
3871 
3872     data_[0] = 0;         // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}}
3873     (*datap2_)[0] = 0;    // expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}}
3874 
3875     data_();              // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}}
3876   }
3877 
3878   // const operator tests
3879   void test3() const {
3880     Data mydat(data_);      // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}}
3881 
3882     //FIXME
3883     //showDataCell(data_);    // xpected-warning {{reading variable 'data_' requires holding mutex 'mu_'}}
3884     //showDataCell(*datap2_); // xpected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}}
3885 
3886     int a = data_[0];       // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}}
3887   }
3888 
3889 private:
3890   Mutex mu_;
3891   Data  data_   GUARDED_BY(mu_);
3892   Data* datap1_ GUARDED_BY(mu_);
3893   Data* datap2_ PT_GUARDED_BY(mu_);
3894 };
3895 
3896 }  // end namespace GuardedNonPrimitiveTypeTest
3897 
3898 
3899 namespace GuardedNonPrimitive_MemberAccess {
3900 
3901 class Cell {
3902 public:
3903   Cell(int i);
3904 
3905   void cellMethod();
3906 
3907   int a;
3908 };
3909 
3910 
3911 class Foo {
3912 public:
3913   int   a;
3914   Cell  c  GUARDED_BY(cell_mu_);
3915   Cell* cp PT_GUARDED_BY(cell_mu_);
3916 
3917   void myMethod();
3918 
3919   Mutex cell_mu_;
3920 };
3921 
3922 
3923 class Bar {
3924 private:
3925   Mutex mu_;
3926   Foo  foo  GUARDED_BY(mu_);
3927   Foo* foop PT_GUARDED_BY(mu_);
3928 
3929   void test() {
3930     foo.myMethod();      // expected-warning {{reading variable 'foo' requires holding mutex 'mu_'}}
3931 
3932     int fa = foo.a;      // expected-warning {{reading variable 'foo' requires holding mutex 'mu_'}}
3933     foo.a  = fa;         // expected-warning {{writing variable 'foo' requires holding mutex 'mu_' exclusively}}
3934 
3935     fa = foop->a;        // expected-warning {{reading the value pointed to by 'foop' requires holding mutex 'mu_'}}
3936     foop->a = fa;        // expected-warning {{writing the value pointed to by 'foop' requires holding mutex 'mu_' exclusively}}
3937 
3938     fa = (*foop).a;      // expected-warning {{reading the value pointed to by 'foop' requires holding mutex 'mu_'}}
3939     (*foop).a = fa;      // expected-warning {{writing the value pointed to by 'foop' requires holding mutex 'mu_' exclusively}}
3940 
3941     foo.c  = Cell(0);    // expected-warning {{writing variable 'foo' requires holding mutex 'mu_'}} \
3942                          // expected-warning {{writing variable 'c' requires holding mutex 'foo.cell_mu_' exclusively}}
3943     foo.c.cellMethod();  // expected-warning {{reading variable 'foo' requires holding mutex 'mu_'}} \
3944                          // expected-warning {{reading variable 'c' requires holding mutex 'foo.cell_mu_'}}
3945 
3946     foop->c  = Cell(0);    // expected-warning {{writing the value pointed to by 'foop' requires holding mutex 'mu_'}} \
3947                            // expected-warning {{writing variable 'c' requires holding mutex 'foop->cell_mu_' exclusively}}
3948     foop->c.cellMethod();  // expected-warning {{reading the value pointed to by 'foop' requires holding mutex 'mu_'}} \
3949                            // expected-warning {{reading variable 'c' requires holding mutex 'foop->cell_mu_'}}
3950 
3951     (*foop).c  = Cell(0);    // expected-warning {{writing the value pointed to by 'foop' requires holding mutex 'mu_'}} \
3952                              // expected-warning {{writing variable 'c' requires holding mutex 'foop->cell_mu_' exclusively}}
3953     (*foop).c.cellMethod();  // expected-warning {{reading the value pointed to by 'foop' requires holding mutex 'mu_'}} \
3954                              // expected-warning {{reading variable 'c' requires holding mutex 'foop->cell_mu_'}}
3955   };
3956 };
3957 
3958 }  // namespace GuardedNonPrimitive_MemberAccess
3959 
3960 
3961 namespace TestThrowExpr {
3962 
3963 class Foo {
3964   Mutex mu_;
3965 
3966   bool hasError();
3967 
3968   void test() {
3969     mu_.Lock();
3970     if (hasError()) {
3971       throw "ugly";
3972     }
3973     mu_.Unlock();
3974   }
3975 };
3976 
3977 }  // end namespace TestThrowExpr
3978 
3979 
3980 namespace UnevaluatedContextTest {
3981 
3982 // parse attribute expressions in an unevaluated context.
3983 
3984 static inline Mutex* getMutex1();
3985 static inline Mutex* getMutex2();
3986 
3987 void bar() EXCLUSIVE_LOCKS_REQUIRED(getMutex1());
3988 
3989 void bar2() EXCLUSIVE_LOCKS_REQUIRED(getMutex1(), getMutex2());
3990 
3991 }  // end namespace UnevaluatedContextTest
3992 
3993 
3994 namespace LockUnlockFunctionTest {
3995 
3996 // Check built-in lock functions
3997 class LOCKABLE MyLockable  {
3998 public:
3999   void lock()       EXCLUSIVE_LOCK_FUNCTION() { mu_.Lock(); }
4000   void readerLock() SHARED_LOCK_FUNCTION()    { mu_.ReaderLock(); }
4001   void unlock()     UNLOCK_FUNCTION()         { mu_.Unlock(); }
4002 
4003 private:
4004   Mutex mu_;
4005 };
4006 
4007 
4008 class Foo {
4009 public:
4010   // Correct lock/unlock functions
4011   void lock() EXCLUSIVE_LOCK_FUNCTION(mu_) {
4012     mu_.Lock();
4013   }
4014 
4015   void readerLock() SHARED_LOCK_FUNCTION(mu_) {
4016     mu_.ReaderLock();
4017   }
4018 
4019   void unlock() UNLOCK_FUNCTION(mu_) {
4020     mu_.Unlock();
4021   }
4022 
4023   // Check failure to lock.
4024   void lockBad() EXCLUSIVE_LOCK_FUNCTION(mu_) {    // expected-note {{mutex acquired here}}
4025     mu2_.Lock();
4026     mu2_.Unlock();
4027   }  // expected-warning {{expecting mutex 'mu_' to be held at the end of function}}
4028 
4029   void readerLockBad() SHARED_LOCK_FUNCTION(mu_) {  // expected-note {{mutex acquired here}}
4030     mu2_.Lock();
4031     mu2_.Unlock();
4032   }  // expected-warning {{expecting mutex 'mu_' to be held at the end of function}}
4033 
4034   void unlockBad() UNLOCK_FUNCTION(mu_) {  // expected-note {{mutex acquired here}}
4035     mu2_.Lock();
4036     mu2_.Unlock();
4037   }  // expected-warning {{mutex 'mu_' is still held at the end of function}}
4038 
4039   // Check locking the wrong thing.
4040   void lockBad2() EXCLUSIVE_LOCK_FUNCTION(mu_) {   // expected-note {{mutex acquired here}}
4041     mu2_.Lock();            // expected-note {{mutex acquired here}}
4042   } // expected-warning {{expecting mutex 'mu_' to be held at the end of function}} \
4043     // expected-warning {{mutex 'mu2_' is still held at the end of function}}
4044 
4045 
4046   void readerLockBad2() SHARED_LOCK_FUNCTION(mu_) {   // expected-note {{mutex acquired here}}
4047     mu2_.ReaderLock();      // expected-note {{mutex acquired here}}
4048   } // expected-warning {{expecting mutex 'mu_' to be held at the end of function}} \
4049     // expected-warning {{mutex 'mu2_' is still held at the end of function}}
4050 
4051 
4052   void unlockBad2() UNLOCK_FUNCTION(mu_) {  // expected-note {{mutex acquired here}}
4053     mu2_.Unlock();  // expected-warning {{releasing mutex 'mu2_' that was not held}}
4054   }  // expected-warning {{mutex 'mu_' is still held at the end of function}}
4055 
4056 private:
4057   Mutex mu_;
4058   Mutex mu2_;
4059 };
4060 
4061 }  // end namespace LockUnlockFunctionTest
4062 
4063 
4064 namespace AssertHeldTest {
4065 
4066 class Foo {
4067 public:
4068   int c;
4069   int a GUARDED_BY(mu_);
4070   Mutex mu_;
4071 
4072   void test1() {
4073     mu_.AssertHeld();
4074     int b = a;
4075     a = 0;
4076   }
4077 
4078   void test2() {
4079     mu_.AssertReaderHeld();
4080     int b = a;
4081     a = 0;   // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}}
4082   }
4083 
4084   void test3() {
4085     if (c) {
4086       mu_.AssertHeld();
4087     }
4088     else {
4089       mu_.AssertHeld();
4090     }
4091     int b = a;
4092     a = 0;
4093   }
4094 
4095   void test4() EXCLUSIVE_LOCKS_REQUIRED(mu_) {
4096     mu_.AssertHeld();
4097     int b = a;
4098     a = 0;
4099   }
4100 
4101   void test5() UNLOCK_FUNCTION(mu_) {
4102     mu_.AssertHeld();
4103     mu_.Unlock();
4104   }
4105 
4106   void test6() {
4107     mu_.AssertHeld();
4108     mu_.Unlock();
4109   }  // should this be a warning?
4110 
4111   void test7() {
4112     if (c) {
4113       mu_.AssertHeld();
4114     }
4115     else {
4116       mu_.Lock();
4117     }
4118     int b = a;
4119     a = 0;
4120     mu_.Unlock();
4121   }
4122 
4123   void test8() {
4124     if (c) {
4125       mu_.Lock();
4126     }
4127     else {
4128       mu_.AssertHeld();
4129     }
4130     int b = a;
4131     a = 0;
4132     mu_.Unlock();
4133   }
4134 
4135   void test9() {
4136     if (c) {
4137       mu_.AssertHeld();
4138     }
4139     else {
4140       mu_.Lock();  // expected-note {{mutex acquired here}}
4141     }
4142   }  // expected-warning {{mutex 'mu_' is still held at the end of function}}
4143 
4144   void test10() {
4145     if (c) {
4146       mu_.Lock();  // expected-note {{mutex acquired here}}
4147     }
4148     else {
4149       mu_.AssertHeld();
4150     }
4151   }  // expected-warning {{mutex 'mu_' is still held at the end of function}}
4152 
4153   void assertMu() ASSERT_EXCLUSIVE_LOCK(mu_);
4154 
4155   void test11() {
4156     assertMu();
4157     int b = a;
4158     a = 0;
4159   }
4160 };
4161 
4162 }  // end namespace AssertHeldTest
4163 
4164 
4165 namespace LogicalConditionalTryLock {
4166 
4167 class Foo {
4168 public:
4169   Mutex mu;
4170   int a GUARDED_BY(mu);
4171   bool c;
4172 
4173   bool newc();
4174 
4175   void test1() {
4176     if (c && mu.TryLock()) {
4177       a = 0;
4178       mu.Unlock();
4179     }
4180   }
4181 
4182   void test2() {
4183     bool b = mu.TryLock();
4184     if (c && b) {
4185       a = 0;
4186       mu.Unlock();
4187     }
4188   }
4189 
4190   void test3() {
4191     if (c || !mu.TryLock())
4192       return;
4193     a = 0;
4194     mu.Unlock();
4195   }
4196 
4197   void test4() {
4198     while (c && mu.TryLock()) {
4199       a = 0;
4200       c = newc();
4201       mu.Unlock();
4202     }
4203   }
4204 
4205   void test5() {
4206     while (c) {
4207       if (newc() || !mu.TryLock())
4208         break;
4209       a = 0;
4210       mu.Unlock();
4211     }
4212   }
4213 
4214   void test6() {
4215     mu.Lock();
4216     do {
4217       a = 0;
4218       mu.Unlock();
4219     } while (newc() && mu.TryLock());
4220   }
4221 
4222   void test7() {
4223     for (bool b = mu.TryLock(); c && b;) {
4224       a = 0;
4225       mu.Unlock();
4226     }
4227   }
4228 
4229   void test8() {
4230     if (c && newc() && mu.TryLock()) {
4231       a = 0;
4232       mu.Unlock();
4233     }
4234   }
4235 
4236   void test9() {
4237     if (!(c && newc() && mu.TryLock()))
4238       return;
4239     a = 0;
4240     mu.Unlock();
4241   }
4242 
4243   void test10() {
4244     if (!(c || !mu.TryLock())) {
4245       a = 0;
4246       mu.Unlock();
4247     }
4248   }
4249 };
4250 
4251 }  // end namespace LogicalConditionalTryLock
4252 
4253 
4254 
4255 namespace PtGuardedByTest {
4256 
4257 void doSomething();
4258 
4259 class Cell {
4260   public:
4261   int a;
4262 };
4263 
4264 
4265 // This mainly duplicates earlier tests, but just to make sure...
4266 class PtGuardedBySanityTest {
4267   Mutex  mu1;
4268   Mutex  mu2;
4269   int*   a GUARDED_BY(mu1) PT_GUARDED_BY(mu2);
4270   Cell*  c GUARDED_BY(mu1) PT_GUARDED_BY(mu2);
4271   int    sa[10] GUARDED_BY(mu1);
4272   Cell   sc[10] GUARDED_BY(mu1);
4273 
4274   void test1() {
4275     mu1.Lock();
4276     if (a == 0) doSomething();  // OK, we don't dereference.
4277     a = 0;
4278     c = 0;
4279     if (sa[0] == 42) doSomething();
4280     sa[0] = 57;
4281     if (sc[0].a == 42) doSomething();
4282     sc[0].a = 57;
4283     mu1.Unlock();
4284   }
4285 
4286   void test2() {
4287     mu1.ReaderLock();
4288     if (*a == 0) doSomething();      // expected-warning {{reading the value pointed to by 'a' requires holding mutex 'mu2'}}
4289     *a = 0;                          // expected-warning {{writing the value pointed to by 'a' requires holding mutex 'mu2' exclusively}}
4290 
4291     if (c->a == 0) doSomething();    // expected-warning {{reading the value pointed to by 'c' requires holding mutex 'mu2'}}
4292     c->a = 0;                        // expected-warning {{writing the value pointed to by 'c' requires holding mutex 'mu2' exclusively}}
4293 
4294     if ((*c).a == 0) doSomething();  // expected-warning {{reading the value pointed to by 'c' requires holding mutex 'mu2'}}
4295     (*c).a = 0;                      // expected-warning {{writing the value pointed to by 'c' requires holding mutex 'mu2' exclusively}}
4296 
4297     if (a[0] == 42) doSomething();     // expected-warning {{reading the value pointed to by 'a' requires holding mutex 'mu2'}}
4298     a[0] = 57;                         // expected-warning {{writing the value pointed to by 'a' requires holding mutex 'mu2' exclusively}}
4299     if (c[0].a == 42) doSomething();   // expected-warning {{reading the value pointed to by 'c' requires holding mutex 'mu2'}}
4300     c[0].a = 57;                       // expected-warning {{writing the value pointed to by 'c' requires holding mutex 'mu2' exclusively}}
4301     mu1.Unlock();
4302   }
4303 
4304   void test3() {
4305     mu2.Lock();
4306     if (*a == 0) doSomething();      // expected-warning {{reading variable 'a' requires holding mutex 'mu1'}}
4307     *a = 0;                          // expected-warning {{reading variable 'a' requires holding mutex 'mu1'}}
4308 
4309     if (c->a == 0) doSomething();    // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}}
4310     c->a = 0;                        // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}}
4311 
4312     if ((*c).a == 0) doSomething();  // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}}
4313     (*c).a = 0;                      // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}}
4314 
4315     if (a[0] == 42) doSomething();     // expected-warning {{reading variable 'a' requires holding mutex 'mu1'}}
4316     a[0] = 57;                         // expected-warning {{reading variable 'a' requires holding mutex 'mu1'}}
4317     if (c[0].a == 42) doSomething();   // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}}
4318     c[0].a = 57;                       // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}}
4319     mu2.Unlock();
4320   }
4321 
4322   void test4() {  // Literal arrays
4323     if (sa[0] == 42) doSomething();     // expected-warning {{reading variable 'sa' requires holding mutex 'mu1'}}
4324     sa[0] = 57;                         // expected-warning {{writing variable 'sa' requires holding mutex 'mu1' exclusively}}
4325     if (sc[0].a == 42) doSomething();   // expected-warning {{reading variable 'sc' requires holding mutex 'mu1'}}
4326     sc[0].a = 57;                       // expected-warning {{writing variable 'sc' requires holding mutex 'mu1' exclusively}}
4327 
4328     if (*sa == 42) doSomething();       // expected-warning {{reading variable 'sa' requires holding mutex 'mu1'}}
4329     *sa = 57;                           // expected-warning {{writing variable 'sa' requires holding mutex 'mu1' exclusively}}
4330     if ((*sc).a == 42) doSomething();   // expected-warning {{reading variable 'sc' requires holding mutex 'mu1'}}
4331     (*sc).a = 57;                       // expected-warning {{writing variable 'sc' requires holding mutex 'mu1' exclusively}}
4332     if (sc->a == 42) doSomething();     // expected-warning {{reading variable 'sc' requires holding mutex 'mu1'}}
4333     sc->a = 57;                         // expected-warning {{writing variable 'sc' requires holding mutex 'mu1' exclusively}}
4334   }
4335 
4336   void test5() {
4337     mu1.ReaderLock();    // OK -- correct use.
4338     mu2.Lock();
4339     if (*a == 0) doSomething();
4340     *a = 0;
4341 
4342     if (c->a == 0) doSomething();
4343     c->a = 0;
4344 
4345     if ((*c).a == 0) doSomething();
4346     (*c).a = 0;
4347     mu2.Unlock();
4348     mu1.Unlock();
4349   }
4350 };
4351 
4352 
4353 class SmartPtr_PtGuardedBy_Test {
4354   Mutex mu1;
4355   Mutex mu2;
4356   SmartPtr<int>  sp GUARDED_BY(mu1) PT_GUARDED_BY(mu2);
4357   SmartPtr<Cell> sq GUARDED_BY(mu1) PT_GUARDED_BY(mu2);
4358 
4359   void test1() {
4360     mu1.ReaderLock();
4361     mu2.Lock();
4362 
4363     sp.get();
4364     if (*sp == 0) doSomething();
4365     *sp = 0;
4366     sq->a = 0;
4367 
4368     if (sp[0] == 0) doSomething();
4369     sp[0] = 0;
4370 
4371     mu2.Unlock();
4372     mu1.Unlock();
4373   }
4374 
4375   void test2() {
4376     mu2.Lock();
4377 
4378     sp.get();                      // expected-warning {{reading variable 'sp' requires holding mutex 'mu1'}}
4379     if (*sp == 0) doSomething();   // expected-warning {{reading variable 'sp' requires holding mutex 'mu1'}}
4380     *sp = 0;                       // expected-warning {{reading variable 'sp' requires holding mutex 'mu1'}}
4381     sq->a = 0;                     // expected-warning {{reading variable 'sq' requires holding mutex 'mu1'}}
4382 
4383     if (sp[0] == 0) doSomething();   // expected-warning {{reading variable 'sp' requires holding mutex 'mu1'}}
4384     sp[0] = 0;                       // expected-warning {{reading variable 'sp' requires holding mutex 'mu1'}}
4385     if (sq[0].a == 0) doSomething(); // expected-warning {{reading variable 'sq' requires holding mutex 'mu1'}}
4386     sq[0].a = 0;                     // expected-warning {{reading variable 'sq' requires holding mutex 'mu1'}}
4387 
4388     mu2.Unlock();
4389   }
4390 
4391   void test3() {
4392     mu1.Lock();
4393 
4394     sp.get();
4395     if (*sp == 0) doSomething();   // expected-warning {{reading the value pointed to by 'sp' requires holding mutex 'mu2'}}
4396     *sp = 0;                       // expected-warning {{reading the value pointed to by 'sp' requires holding mutex 'mu2'}}
4397     sq->a = 0;                     // expected-warning {{reading the value pointed to by 'sq' requires holding mutex 'mu2'}}
4398 
4399     if (sp[0] == 0) doSomething();   // expected-warning {{reading the value pointed to by 'sp' requires holding mutex 'mu2'}}
4400     sp[0] = 0;                       // expected-warning {{reading the value pointed to by 'sp' requires holding mutex 'mu2'}}
4401     if (sq[0].a == 0) doSomething(); // expected-warning {{reading the value pointed to by 'sq' requires holding mutex 'mu2'}}
4402     sq[0].a = 0;                     // expected-warning {{reading the value pointed to by 'sq' requires holding mutex 'mu2'}}
4403 
4404     mu1.Unlock();
4405   }
4406 };
4407 
4408 }  // end namespace PtGuardedByTest
4409 
4410 
4411 namespace NonMemberCalleeICETest {
4412 
4413 class A {
4414   void Run() {
4415   (RunHelper)();  // expected-warning {{calling function 'RunHelper' requires holding mutex 'M' exclusively}}
4416  }
4417 
4418  void RunHelper() __attribute__((exclusive_locks_required(M)));
4419  Mutex M;
4420 };
4421 
4422 }  // end namespace NonMemberCalleeICETest
4423 
4424 
4425 namespace pt_guard_attribute_type {
4426   int i PT_GUARDED_BY(sls_mu);  // expected-warning {{'pt_guarded_by' only applies to pointer types; type here is 'int'}}
4427   int j PT_GUARDED_VAR;  // expected-warning {{'pt_guarded_var' only applies to pointer types; type here is 'int'}}
4428 
4429   void test() {
4430     int i PT_GUARDED_BY(sls_mu);  // expected-warning {{'pt_guarded_by' attribute only applies to fields and global variables}}
4431     int j PT_GUARDED_VAR;  // expected-warning {{'pt_guarded_var' attribute only applies to fields and global variables}}
4432 
4433     typedef int PT_GUARDED_BY(sls_mu) bad1;  // expected-warning {{'pt_guarded_by' attribute only applies to fields and global variables}}
4434     typedef int PT_GUARDED_VAR bad2;  // expected-warning {{'pt_guarded_var' attribute only applies to fields and global variables}}
4435   }
4436 }  // end namespace pt_guard_attribute_type
4437 
4438 
4439 namespace ThreadAttributesOnLambdas {
4440 
4441 class Foo {
4442   Mutex mu_;
4443 
4444   void LockedFunction() EXCLUSIVE_LOCKS_REQUIRED(mu_);
4445 
4446   void test() {
4447     auto func1 = [this]() EXCLUSIVE_LOCKS_REQUIRED(mu_) {
4448       LockedFunction();
4449     };
4450 
4451     auto func2 = [this]() NO_THREAD_SAFETY_ANALYSIS {
4452       LockedFunction();
4453     };
4454 
4455     auto func3 = [this]() EXCLUSIVE_LOCK_FUNCTION(mu_) {
4456       mu_.Lock();
4457     };
4458 
4459     func1();  // expected-warning {{calling function 'operator()' requires holding mutex 'mu_' exclusively}}
4460     func2();
4461     func3();
4462     mu_.Unlock();
4463   }
4464 };
4465 
4466 }  // end namespace ThreadAttributesOnLambdas
4467 
4468 
4469 
4470 namespace AttributeExpressionCornerCases {
4471 
4472 class Foo {
4473   int a GUARDED_BY(getMu());
4474 
4475   Mutex* getMu()   LOCK_RETURNED("");
4476   Mutex* getUniv() LOCK_RETURNED("*");
4477 
4478   void test1() {
4479     a = 0;
4480   }
4481 
4482   void test2() EXCLUSIVE_LOCKS_REQUIRED(getUniv()) {
4483     a = 0;
4484   }
4485 
4486   void foo(Mutex* mu) EXCLUSIVE_LOCKS_REQUIRED(mu);
4487 
4488   void test3() {
4489     foo(nullptr);
4490   }
4491 };
4492 
4493 
4494 class MapTest {
4495   struct MuCell { Mutex* mu; };
4496 
4497   MyMap<MyString, Mutex*> map;
4498   MyMap<MyString, MuCell> mapCell;
4499 
4500   int a GUARDED_BY(map["foo"]);
4501   int b GUARDED_BY(mapCell["foo"].mu);
4502 
4503   void test() {
4504     map["foo"]->Lock();
4505     a = 0;
4506     map["foo"]->Unlock();
4507   }
4508 
4509   void test2() {
4510     mapCell["foo"].mu->Lock();
4511     b = 0;
4512     mapCell["foo"].mu->Unlock();
4513   }
4514 };
4515 
4516 
4517 class PreciseSmartPtr {
4518   SmartPtr<Mutex> mu;
4519   int val GUARDED_BY(mu);
4520 
4521   static bool compare(PreciseSmartPtr& a, PreciseSmartPtr &b) {
4522     a.mu->Lock();
4523     bool result = (a.val == b.val);   // expected-warning {{reading variable 'val' requires holding mutex 'b.mu'}} \
4524                                       // expected-note {{found near match 'a.mu'}}
4525     a.mu->Unlock();
4526     return result;
4527   }
4528 };
4529 
4530 
4531 class SmartRedeclare {
4532   SmartPtr<Mutex> mu;
4533   int val GUARDED_BY(mu);
4534 
4535   void test()  EXCLUSIVE_LOCKS_REQUIRED(mu);
4536   void test2() EXCLUSIVE_LOCKS_REQUIRED(mu.get());
4537   void test3() EXCLUSIVE_LOCKS_REQUIRED(mu.get());
4538 };
4539 
4540 
4541 void SmartRedeclare::test() EXCLUSIVE_LOCKS_REQUIRED(mu.get()) {
4542   val = 0;
4543 }
4544 
4545 void SmartRedeclare::test2() EXCLUSIVE_LOCKS_REQUIRED(mu) {
4546   val = 0;
4547 }
4548 
4549 void SmartRedeclare::test3() {
4550   val = 0;
4551 }
4552 
4553 
4554 namespace CustomMutex {
4555 
4556 
4557 class LOCKABLE BaseMutex { };
4558 class DerivedMutex : public BaseMutex { };
4559 
4560 void customLock(const BaseMutex *m)   EXCLUSIVE_LOCK_FUNCTION(m);
4561 void customUnlock(const BaseMutex *m) UNLOCK_FUNCTION(m);
4562 
4563 static struct DerivedMutex custMu;
4564 
4565 static void doSomethingRequiringLock() EXCLUSIVE_LOCKS_REQUIRED(custMu) { }
4566 
4567 void customTest() {
4568   customLock(reinterpret_cast<BaseMutex*>(&custMu));  // ignore casts
4569   doSomethingRequiringLock();
4570   customUnlock(reinterpret_cast<BaseMutex*>(&custMu));
4571 }
4572 
4573 } // end namespace CustomMutex
4574 
4575 } // end AttributeExpressionCornerCases
4576 
4577 
4578 namespace ScopedLockReturnedInvalid {
4579 
4580 class Opaque;
4581 
4582 Mutex* getMutex(Opaque* o) LOCK_RETURNED("");
4583 
4584 void test(Opaque* o) {
4585   MutexLock lock(getMutex(o));
4586 }
4587 
4588 }  // end namespace ScopedLockReturnedInvalid
4589 
4590 
4591 namespace NegativeRequirements {
4592 
4593 class Bar {
4594   Mutex mu;
4595   int a GUARDED_BY(mu);
4596 
4597 public:
4598   void baz() EXCLUSIVE_LOCKS_REQUIRED(!mu) {
4599     mu.Lock();
4600     a = 0;
4601     mu.Unlock();
4602   }
4603 };
4604 
4605 
4606 class Foo {
4607   Mutex mu;
4608   int a GUARDED_BY(mu);
4609 
4610 public:
4611   void foo() {
4612     mu.Lock();    // warning?  needs !mu?
4613     baz();        // expected-warning {{cannot call function 'baz' while mutex 'mu' is held}}
4614     bar();
4615     mu.Unlock();
4616   }
4617 
4618   void bar() {
4619     bar2();       // expected-warning {{calling function 'bar2' requires holding  '!mu'}}
4620   }
4621 
4622   void bar2() EXCLUSIVE_LOCKS_REQUIRED(!mu) {
4623     baz();
4624   }
4625 
4626   void baz() EXCLUSIVE_LOCKS_REQUIRED(!mu) {
4627     mu.Lock();
4628     a = 0;
4629     mu.Unlock();
4630   }
4631 
4632   void test() {
4633     Bar b;
4634     b.baz();     // no warning -- in different class.
4635   }
4636 };
4637 
4638 }   // end namespace NegativeRequirements
4639 
4640 
4641 namespace NegativeThreadRoles {
4642 
4643 typedef int __attribute__((capability("role"))) ThreadRole;
4644 
4645 void acquire(ThreadRole R) __attribute__((exclusive_lock_function(R))) __attribute__((no_thread_safety_analysis)) {}
4646 void release(ThreadRole R) __attribute__((unlock_function(R))) __attribute__((no_thread_safety_analysis)) {}
4647 
4648 ThreadRole FlightControl, Logger;
4649 
4650 extern void enque_log_msg(const char *msg);
4651 void log_msg(const char *msg) {
4652   enque_log_msg(msg);
4653 }
4654 
4655 void dispatch_log(const char *msg) __attribute__((requires_capability(!FlightControl))) {}
4656 void dispatch_log2(const char *msg) __attribute__((requires_capability(Logger))) {}
4657 
4658 void flight_control_entry(void) __attribute__((requires_capability(FlightControl))) {
4659   dispatch_log("wrong"); /* expected-warning {{cannot call function 'dispatch_log' while mutex 'FlightControl' is held}} */
4660   dispatch_log2("also wrong"); /* expected-warning {{calling function 'dispatch_log2' requires holding role 'Logger' exclusively}} */
4661 }
4662 
4663 void spawn_fake_flight_control_thread(void) {
4664   acquire(FlightControl);
4665   flight_control_entry();
4666   release(FlightControl);
4667 }
4668 
4669 extern const char *deque_log_msg(void) __attribute__((requires_capability(Logger)));
4670 void logger_entry(void) __attribute__((requires_capability(Logger))) {
4671   const char *msg;
4672 
4673   while ((msg = deque_log_msg())) {
4674     dispatch_log(msg);
4675   }
4676 }
4677 
4678 void spawn_fake_logger_thread(void) {
4679   acquire(Logger);
4680   logger_entry();
4681   release(Logger);
4682 }
4683 
4684 int main(void) {
4685   spawn_fake_flight_control_thread();
4686   spawn_fake_logger_thread();
4687 
4688   for (;;)
4689     ; /* Pretend to dispatch things. */
4690 
4691   return 0;
4692 }
4693 
4694 } // end namespace NegativeThreadRoles
4695 
4696 
4697 namespace AssertSharedExclusive {
4698 
4699 void doSomething();
4700 
4701 class Foo {
4702   Mutex mu;
4703   int a GUARDED_BY(mu);
4704 
4705   void test() SHARED_LOCKS_REQUIRED(mu) {
4706     mu.AssertHeld();
4707     if (a > 0)
4708       doSomething();
4709   }
4710 };
4711 
4712 } // end namespace AssertSharedExclusive
4713 
4714 
4715 namespace RangeBasedForAndReferences {
4716 
4717 class Foo {
4718   struct MyStruct {
4719     int a;
4720   };
4721 
4722   Mutex mu;
4723   int a GUARDED_BY(mu);
4724   MyContainer<int>  cntr  GUARDED_BY(mu);
4725   MyStruct s GUARDED_BY(mu);
4726   int arr[10] GUARDED_BY(mu);
4727 
4728   void nonref_test() {
4729     int b = a;             // expected-warning {{reading variable 'a' requires holding mutex 'mu'}}
4730     b = 0;                 // no warning
4731   }
4732 
4733   void auto_test() {
4734     auto b = a;            // expected-warning {{reading variable 'a' requires holding mutex 'mu'}}
4735     b = 0;                 // no warning
4736     auto &c = a;           // no warning
4737     c = 0;                 // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}}
4738   }
4739 
4740   void ref_test() {
4741     int &b = a;
4742     int &c = b;
4743     int &d = c;
4744     b = 0;                 // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}}
4745     c = 0;                 // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}}
4746     d = 0;                 // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}}
4747 
4748     MyStruct &rs = s;
4749     rs.a = 0;              // expected-warning {{writing variable 's' requires holding mutex 'mu' exclusively}}
4750 
4751     int (&rarr)[10] = arr;
4752     rarr[2] = 0;           // expected-warning {{writing variable 'arr' requires holding mutex 'mu' exclusively}}
4753   }
4754 
4755   void ptr_test() {
4756     int *b = &a;
4757     *b = 0;                // no expected warning yet
4758   }
4759 
4760   void for_test() {
4761     int total = 0;
4762     for (int i : cntr) {   // expected-warning2 {{reading variable 'cntr' requires holding mutex 'mu'}}
4763       total += i;
4764     }
4765   }
4766 };
4767 
4768 
4769 } // end namespace RangeBasedForAndReferences
4770 
4771 
4772 
4773 namespace PassByRefTest {
4774 
4775 class Foo {
4776 public:
4777   Foo() : a(0), b(0) { }
4778 
4779   int a;
4780   int b;
4781 
4782   void operator+(const Foo& f);
4783 
4784   void operator[](const Foo& g);
4785 };
4786 
4787 template<class T>
4788 T&& mymove(T& f);
4789 
4790 
4791 // test top-level functions
4792 void copy(Foo f);
4793 void write1(Foo& f);
4794 void write2(int a, Foo& f);
4795 void read1(const Foo& f);
4796 void read2(int a, const Foo& f);
4797 void destroy(Foo&& f);
4798 
4799 void operator/(const Foo& f, const Foo& g);
4800 void operator*(const Foo& f, const Foo& g);
4801 
4802 
4803 
4804 
4805 class Bar {
4806 public:
4807   Mutex mu;
4808   Foo           foo   GUARDED_BY(mu);
4809   Foo           foo2  GUARDED_BY(mu);
4810   Foo*          foop  PT_GUARDED_BY(mu);
4811   SmartPtr<Foo> foosp PT_GUARDED_BY(mu);
4812 
4813   // test methods.
4814   void mwrite1(Foo& f);
4815   void mwrite2(int a, Foo& f);
4816   void mread1(const Foo& f);
4817   void mread2(int a, const Foo& f);
4818 
4819   // static methods
4820   static void smwrite1(Foo& f);
4821   static void smwrite2(int a, Foo& f);
4822   static void smread1(const Foo& f);
4823   static void smread2(int a, const Foo& f);
4824 
4825   void operator<<(const Foo& f);
4826 
4827   void test1() {
4828     copy(foo);             // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}}
4829     write1(foo);           // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
4830     write2(10, foo);       // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
4831     read1(foo);            // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
4832     read2(10, foo);        // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
4833     destroy(mymove(foo));  // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
4834 
4835     mwrite1(foo);           // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
4836     mwrite2(10, foo);       // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
4837     mread1(foo);            // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
4838     mread2(10, foo);        // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
4839 
4840     smwrite1(foo);           // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
4841     smwrite2(10, foo);       // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
4842     smread1(foo);            // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
4843     smread2(10, foo);        // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
4844 
4845     foo + foo2;              // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} \
4846                              // expected-warning {{passing variable 'foo2' by reference requires holding mutex 'mu'}}
4847     foo / foo2;              // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} \
4848                              // expected-warning {{passing variable 'foo2' by reference requires holding mutex 'mu'}}
4849     foo * foo2;              // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} \
4850                              // expected-warning {{passing variable 'foo2' by reference requires holding mutex 'mu'}}
4851     foo[foo2];               // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} \
4852                              // expected-warning {{passing variable 'foo2' by reference requires holding mutex 'mu'}}
4853     (*this) << foo;          // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
4854 
4855     copy(*foop);             // expected-warning {{reading the value pointed to by 'foop' requires holding mutex 'mu'}}
4856     write1(*foop);           // expected-warning {{passing the value that 'foop' points to by reference requires holding mutex 'mu'}}
4857     write2(10, *foop);       // expected-warning {{passing the value that 'foop' points to by reference requires holding mutex 'mu'}}
4858     read1(*foop);            // expected-warning {{passing the value that 'foop' points to by reference requires holding mutex 'mu'}}
4859     read2(10, *foop);        // expected-warning {{passing the value that 'foop' points to by reference requires holding mutex 'mu'}}
4860     destroy(mymove(*foop));  // expected-warning {{passing the value that 'foop' points to by reference requires holding mutex 'mu'}}
4861 
4862     copy(*foosp);             // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}}
4863     write1(*foosp);           // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}}
4864     write2(10, *foosp);       // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}}
4865     read1(*foosp);            // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}}
4866     read2(10, *foosp);        // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}}
4867     destroy(mymove(*foosp));  // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}}
4868 
4869     // TODO -- these require better smart pointer handling.
4870     copy(*foosp.get());
4871     write1(*foosp.get());
4872     write2(10, *foosp.get());
4873     read1(*foosp.get());
4874     read2(10, *foosp.get());
4875     destroy(mymove(*foosp.get()));
4876   }
4877 };
4878 
4879 
4880 }  // end namespace PassByRefTest
4881 
4882 
4883 namespace AcquiredBeforeAfterText {
4884 
4885 class Foo {
4886   Mutex mu1 ACQUIRED_BEFORE(mu2, mu3);
4887   Mutex mu2;
4888   Mutex mu3;
4889 
4890   void test1() {
4891     mu1.Lock();
4892     mu2.Lock();
4893     mu3.Lock();
4894 
4895     mu3.Unlock();
4896     mu2.Unlock();
4897     mu1.Unlock();
4898   }
4899 
4900   void test2() {
4901     mu2.Lock();
4902     mu1.Lock();    // expected-warning {{mutex 'mu1' must be acquired before 'mu2'}}
4903     mu1.Unlock();
4904     mu2.Unlock();
4905   }
4906 
4907   void test3() {
4908     mu3.Lock();
4909     mu1.Lock();     // expected-warning {{mutex 'mu1' must be acquired before 'mu3'}}
4910     mu1.Unlock();
4911     mu3.Unlock();
4912   }
4913 
4914   void test4() EXCLUSIVE_LOCKS_REQUIRED(mu1) {
4915     mu2.Lock();
4916     mu2.Unlock();
4917   }
4918 
4919   void test5() EXCLUSIVE_LOCKS_REQUIRED(mu2) {
4920     mu1.Lock();    // expected-warning {{mutex 'mu1' must be acquired before 'mu2'}}
4921     mu1.Unlock();
4922   }
4923 
4924   void test6() EXCLUSIVE_LOCKS_REQUIRED(mu2) {
4925     mu1.AssertHeld();
4926   }
4927 
4928   void test7() EXCLUSIVE_LOCKS_REQUIRED(mu1, mu2, mu3) { }
4929 
4930   void test8() EXCLUSIVE_LOCKS_REQUIRED(mu3, mu2, mu1) { }
4931 };
4932 
4933 
4934 class Foo2 {
4935   Mutex mu1;
4936   Mutex mu2 ACQUIRED_AFTER(mu1);
4937   Mutex mu3 ACQUIRED_AFTER(mu1);
4938 
4939   void test1() {
4940     mu1.Lock();
4941     mu2.Lock();
4942     mu3.Lock();
4943 
4944     mu3.Unlock();
4945     mu2.Unlock();
4946     mu1.Unlock();
4947   }
4948 
4949   void test2() {
4950     mu2.Lock();
4951     mu1.Lock();     // expected-warning {{mutex 'mu1' must be acquired before 'mu2'}}
4952     mu1.Unlock();
4953     mu2.Unlock();
4954   }
4955 
4956   void test3() {
4957     mu3.Lock();
4958     mu1.Lock();     // expected-warning {{mutex 'mu1' must be acquired before 'mu3'}}
4959     mu1.Unlock();
4960     mu3.Unlock();
4961   }
4962 };
4963 
4964 
4965 class Foo3 {
4966   Mutex mu1 ACQUIRED_BEFORE(mu2);
4967   Mutex mu2;
4968   Mutex mu3 ACQUIRED_AFTER(mu2) ACQUIRED_BEFORE(mu4);
4969   Mutex mu4;
4970 
4971   void test1() {
4972     mu1.Lock();
4973     mu2.Lock();
4974     mu3.Lock();
4975     mu4.Lock();
4976 
4977     mu4.Unlock();
4978     mu3.Unlock();
4979     mu2.Unlock();
4980     mu1.Unlock();
4981   }
4982 
4983   void test2() {
4984     mu4.Lock();
4985     mu2.Lock();     // expected-warning {{mutex 'mu2' must be acquired before 'mu4'}}
4986 
4987     mu2.Unlock();
4988     mu4.Unlock();
4989   }
4990 
4991   void test3() {
4992     mu4.Lock();
4993     mu1.Lock();     // expected-warning {{mutex 'mu1' must be acquired before 'mu4'}}
4994 
4995     mu1.Unlock();
4996     mu4.Unlock();
4997   }
4998 
4999   void test4() {
5000     mu3.Lock();
5001     mu1.Lock();     // expected-warning {{mutex 'mu1' must be acquired before 'mu3'}}
5002 
5003     mu1.Unlock();
5004     mu3.Unlock();
5005   }
5006 };
5007 
5008 
5009 // Test transitive DAG traversal with AFTER
5010 class Foo4 {
5011   Mutex mu1;
5012   Mutex mu2 ACQUIRED_AFTER(mu1);
5013   Mutex mu3 ACQUIRED_AFTER(mu1);
5014   Mutex mu4 ACQUIRED_AFTER(mu2, mu3);
5015   Mutex mu5 ACQUIRED_AFTER(mu4);
5016   Mutex mu6 ACQUIRED_AFTER(mu4);
5017   Mutex mu7 ACQUIRED_AFTER(mu5, mu6);
5018   Mutex mu8 ACQUIRED_AFTER(mu7);
5019 
5020   void test() {
5021     mu8.Lock();
5022     mu1.Lock();    // expected-warning {{mutex 'mu1' must be acquired before 'mu8'}}
5023     mu1.Unlock();
5024     mu8.Unlock();
5025   }
5026 };
5027 
5028 
5029 // Test transitive DAG traversal with BEFORE
5030 class Foo5 {
5031   Mutex mu1 ACQUIRED_BEFORE(mu2, mu3);
5032   Mutex mu2 ACQUIRED_BEFORE(mu4);
5033   Mutex mu3 ACQUIRED_BEFORE(mu4);
5034   Mutex mu4 ACQUIRED_BEFORE(mu5, mu6);
5035   Mutex mu5 ACQUIRED_BEFORE(mu7);
5036   Mutex mu6 ACQUIRED_BEFORE(mu7);
5037   Mutex mu7 ACQUIRED_BEFORE(mu8);
5038   Mutex mu8;
5039 
5040   void test() {
5041     mu8.Lock();
5042     mu1.Lock();  // expected-warning {{mutex 'mu1' must be acquired before 'mu8'}}
5043     mu1.Unlock();
5044     mu8.Unlock();
5045   }
5046 };
5047 
5048 
5049 class Foo6 {
5050   Mutex mu1 ACQUIRED_AFTER(mu3);     // expected-warning {{Cycle in acquired_before/after dependencies, starting with 'mu1'}}
5051   Mutex mu2 ACQUIRED_AFTER(mu1);     // expected-warning {{Cycle in acquired_before/after dependencies, starting with 'mu2'}}
5052   Mutex mu3 ACQUIRED_AFTER(mu2);     // expected-warning {{Cycle in acquired_before/after dependencies, starting with 'mu3'}}
5053 
5054   Mutex mu_b ACQUIRED_BEFORE(mu_b);  // expected-warning {{Cycle in acquired_before/after dependencies, starting with 'mu_b'}}
5055   Mutex mu_a ACQUIRED_AFTER(mu_a);   // expected-warning {{Cycle in acquired_before/after dependencies, starting with 'mu_a'}}
5056 
5057   void test0() {
5058     mu_a.Lock();
5059     mu_b.Lock();
5060     mu_b.Unlock();
5061     mu_a.Unlock();
5062   }
5063 
5064   void test1a() {
5065     mu1.Lock();
5066     mu1.Unlock();
5067   }
5068 
5069   void test1b() {
5070     mu1.Lock();
5071     mu_a.Lock();
5072     mu_b.Lock();
5073     mu_b.Unlock();
5074     mu_a.Unlock();
5075     mu1.Unlock();
5076   }
5077 
5078   void test() {
5079     mu2.Lock();
5080     mu2.Unlock();
5081   }
5082 
5083   void test3() {
5084     mu3.Lock();
5085     mu3.Unlock();
5086   }
5087 };
5088 
5089 }  // end namespace AcquiredBeforeAfterTest
5090 
5091 
5092 namespace ScopedAdoptTest {
5093 
5094 class Foo {
5095   Mutex mu;
5096   int a GUARDED_BY(mu);
5097   int b;
5098 
5099   void test1() EXCLUSIVE_UNLOCK_FUNCTION(mu) {
5100     MutexLock slock(&mu, true);
5101     a = 0;
5102   }
5103 
5104   void test2() SHARED_UNLOCK_FUNCTION(mu) {
5105     ReaderMutexLock slock(&mu, true);
5106     b = a;
5107   }
5108 
5109   void test3() EXCLUSIVE_LOCKS_REQUIRED(mu) {  // expected-note {{mutex acquired here}}
5110     MutexLock slock(&mu, true);
5111     a = 0;
5112   }  // expected-warning {{expecting mutex 'mu' to be held at the end of function}}
5113 
5114   void test4() SHARED_LOCKS_REQUIRED(mu) {     // expected-note {{mutex acquired here}}
5115     ReaderMutexLock slock(&mu, true);
5116     b = a;
5117   }  // expected-warning {{expecting mutex 'mu' to be held at the end of function}}
5118 
5119 };
5120 
5121 }  // end namespace ScopedAdoptTest
5122 
5123 
5124 namespace TestReferenceNoThreadSafetyAnalysis {
5125 
5126 #define TS_UNCHECKED_READ(x) ts_unchecked_read(x)
5127 
5128 // Takes a reference to a guarded data member, and returns an unguarded
5129 // reference.
5130 template <class T>
5131 inline const T& ts_unchecked_read(const T& v) NO_THREAD_SAFETY_ANALYSIS {
5132   return v;
5133 }
5134 
5135 template <class T>
5136 inline T& ts_unchecked_read(T& v) NO_THREAD_SAFETY_ANALYSIS {
5137   return v;
5138 }
5139 
5140 
5141 class Foo {
5142 public:
5143   Foo(): a(0) { }
5144 
5145   int a;
5146 };
5147 
5148 
5149 class Bar {
5150 public:
5151   Bar() : a(0) { }
5152 
5153   Mutex mu;
5154   int a   GUARDED_BY(mu);
5155   Foo foo GUARDED_BY(mu);
5156 };
5157 
5158 
5159 void test() {
5160   Bar bar;
5161   const Bar cbar;
5162 
5163   int a = TS_UNCHECKED_READ(bar.a);       // nowarn
5164   TS_UNCHECKED_READ(bar.a) = 1;           // nowarn
5165 
5166   int b = TS_UNCHECKED_READ(bar.foo).a;   // nowarn
5167   TS_UNCHECKED_READ(bar.foo).a = 1;       // nowarn
5168 
5169   int c = TS_UNCHECKED_READ(cbar.a);      // nowarn
5170 }
5171 
5172 #undef TS_UNCHECKED_READ
5173 
5174 }  // end namespace TestReferenceNoThreadSafetyAnalysis
5175 
5176 
5177 namespace GlobalAcquiredBeforeAfterTest {
5178 
5179 Mutex mu1;
5180 Mutex mu2 ACQUIRED_AFTER(mu1);
5181 
5182 void test3() {
5183   mu2.Lock();
5184   mu1.Lock();  // expected-warning {{mutex 'mu1' must be acquired before 'mu2'}}
5185   mu1.Unlock();
5186   mu2.Unlock();
5187 }
5188 
5189 }  // end namespace  GlobalAcquiredBeforeAfterTest
5190 
5191 
5192 namespace LifetimeExtensionText {
5193 
5194 struct Holder {
5195   virtual ~Holder() throw() {}
5196   int i = 0;
5197 };
5198 
5199 void test() {
5200   // Should not crash.
5201   const auto &value = Holder().i;
5202 }
5203 
5204 } // end namespace LifetimeExtensionTest
5205 
5206 
5207 namespace LockableUnions {
5208 
5209 union LOCKABLE MutexUnion {
5210   int a;
5211   char* b;
5212 
5213   void Lock()   EXCLUSIVE_LOCK_FUNCTION();
5214   void Unlock() UNLOCK_FUNCTION();
5215 };
5216 
5217 MutexUnion muun2;
5218 MutexUnion muun1 ACQUIRED_BEFORE(muun2);
5219 
5220 void test() {
5221   muun2.Lock();
5222   muun1.Lock();  // expected-warning {{mutex 'muun1' must be acquired before 'muun2'}}
5223   muun1.Unlock();
5224   muun2.Unlock();
5225 }
5226 
5227 }  // end namespace LockableUnions
5228 
5229 // This used to crash.
5230 class acquired_before_empty_str {
5231   void WaitUntilSpaceAvailable() {
5232     lock_.ReaderLock(); // expected-note {{acquired here}}
5233   } // expected-warning {{mutex 'lock_' is still held at the end of function}}
5234   Mutex lock_ ACQUIRED_BEFORE("");
5235 };
5236