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