xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/warn-thread-safety-negative.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wthread-safety -Wthread-safety-beta -Wthread-safety-negative -fcxx-exceptions %s
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc // FIXME: should also run  %clang_cc1 -fsyntax-only -verify -Wthread-safety -std=c++11 -Wc++98-compat %s
4*0a6a1f1dSLionel Sambuc // FIXME: should also run  %clang_cc1 -fsyntax-only -verify -Wthread-safety %s
5*0a6a1f1dSLionel Sambuc 
6*0a6a1f1dSLionel Sambuc #define LOCKABLE            __attribute__ ((lockable))
7*0a6a1f1dSLionel Sambuc #define SCOPED_LOCKABLE     __attribute__ ((scoped_lockable))
8*0a6a1f1dSLionel Sambuc #define GUARDED_BY(x)       __attribute__ ((guarded_by(x)))
9*0a6a1f1dSLionel Sambuc #define GUARDED_VAR         __attribute__ ((guarded_var))
10*0a6a1f1dSLionel Sambuc #define PT_GUARDED_BY(x)    __attribute__ ((pt_guarded_by(x)))
11*0a6a1f1dSLionel Sambuc #define PT_GUARDED_VAR      __attribute__ ((pt_guarded_var))
12*0a6a1f1dSLionel Sambuc #define ACQUIRED_AFTER(...) __attribute__ ((acquired_after(__VA_ARGS__)))
13*0a6a1f1dSLionel Sambuc #define ACQUIRED_BEFORE(...) __attribute__ ((acquired_before(__VA_ARGS__)))
14*0a6a1f1dSLionel Sambuc #define EXCLUSIVE_LOCK_FUNCTION(...)    __attribute__ ((exclusive_lock_function(__VA_ARGS__)))
15*0a6a1f1dSLionel Sambuc #define SHARED_LOCK_FUNCTION(...)       __attribute__ ((shared_lock_function(__VA_ARGS__)))
16*0a6a1f1dSLionel Sambuc #define ASSERT_EXCLUSIVE_LOCK(...)      __attribute__ ((assert_exclusive_lock(__VA_ARGS__)))
17*0a6a1f1dSLionel Sambuc #define ASSERT_SHARED_LOCK(...)         __attribute__ ((assert_shared_lock(__VA_ARGS__)))
18*0a6a1f1dSLionel Sambuc #define EXCLUSIVE_TRYLOCK_FUNCTION(...) __attribute__ ((exclusive_trylock_function(__VA_ARGS__)))
19*0a6a1f1dSLionel Sambuc #define SHARED_TRYLOCK_FUNCTION(...)    __attribute__ ((shared_trylock_function(__VA_ARGS__)))
20*0a6a1f1dSLionel Sambuc #define UNLOCK_FUNCTION(...)            __attribute__ ((unlock_function(__VA_ARGS__)))
21*0a6a1f1dSLionel Sambuc #define LOCK_RETURNED(x)    __attribute__ ((lock_returned(x)))
22*0a6a1f1dSLionel Sambuc #define LOCKS_EXCLUDED(...) __attribute__ ((locks_excluded(__VA_ARGS__)))
23*0a6a1f1dSLionel Sambuc #define EXCLUSIVE_LOCKS_REQUIRED(...) \
24*0a6a1f1dSLionel Sambuc   __attribute__ ((exclusive_locks_required(__VA_ARGS__)))
25*0a6a1f1dSLionel Sambuc #define SHARED_LOCKS_REQUIRED(...) \
26*0a6a1f1dSLionel Sambuc   __attribute__ ((shared_locks_required(__VA_ARGS__)))
27*0a6a1f1dSLionel Sambuc #define NO_THREAD_SAFETY_ANALYSIS  __attribute__ ((no_thread_safety_analysis))
28*0a6a1f1dSLionel Sambuc 
29*0a6a1f1dSLionel Sambuc 
30*0a6a1f1dSLionel Sambuc class  __attribute__((lockable)) Mutex {
31*0a6a1f1dSLionel Sambuc  public:
32*0a6a1f1dSLionel Sambuc   void Lock() __attribute__((exclusive_lock_function));
33*0a6a1f1dSLionel Sambuc   void ReaderLock() __attribute__((shared_lock_function));
34*0a6a1f1dSLionel Sambuc   void Unlock() __attribute__((unlock_function));
35*0a6a1f1dSLionel Sambuc   bool TryLock() __attribute__((exclusive_trylock_function(true)));
36*0a6a1f1dSLionel Sambuc   bool ReaderTryLock() __attribute__((shared_trylock_function(true)));
37*0a6a1f1dSLionel Sambuc   void LockWhen(const int &cond) __attribute__((exclusive_lock_function));
38*0a6a1f1dSLionel Sambuc 
39*0a6a1f1dSLionel Sambuc   // for negative capabilities
operator !() const40*0a6a1f1dSLionel Sambuc   const Mutex& operator!() const { return *this; }
41*0a6a1f1dSLionel Sambuc 
42*0a6a1f1dSLionel Sambuc   void AssertHeld()       ASSERT_EXCLUSIVE_LOCK();
43*0a6a1f1dSLionel Sambuc   void AssertReaderHeld() ASSERT_SHARED_LOCK();
44*0a6a1f1dSLionel Sambuc };
45*0a6a1f1dSLionel Sambuc 
46*0a6a1f1dSLionel Sambuc 
47*0a6a1f1dSLionel Sambuc namespace SimpleTest {
48*0a6a1f1dSLionel Sambuc 
49*0a6a1f1dSLionel Sambuc class Bar {
50*0a6a1f1dSLionel Sambuc   Mutex mu;
51*0a6a1f1dSLionel Sambuc   int a GUARDED_BY(mu);
52*0a6a1f1dSLionel Sambuc 
53*0a6a1f1dSLionel Sambuc public:
baz()54*0a6a1f1dSLionel Sambuc   void baz() EXCLUSIVE_LOCKS_REQUIRED(!mu) {
55*0a6a1f1dSLionel Sambuc     mu.Lock();
56*0a6a1f1dSLionel Sambuc     a = 0;
57*0a6a1f1dSLionel Sambuc     mu.Unlock();
58*0a6a1f1dSLionel Sambuc   }
59*0a6a1f1dSLionel Sambuc };
60*0a6a1f1dSLionel Sambuc 
61*0a6a1f1dSLionel Sambuc 
62*0a6a1f1dSLionel Sambuc class Foo {
63*0a6a1f1dSLionel Sambuc   Mutex mu;
64*0a6a1f1dSLionel Sambuc   int a GUARDED_BY(mu);
65*0a6a1f1dSLionel Sambuc 
66*0a6a1f1dSLionel Sambuc public:
foo()67*0a6a1f1dSLionel Sambuc   void foo() {
68*0a6a1f1dSLionel Sambuc     mu.Lock();    // expected-warning {{acquiring mutex 'mu' requires negative capability '!mu'}}
69*0a6a1f1dSLionel Sambuc     baz();        // expected-warning {{cannot call function 'baz' while mutex 'mu' is held}}
70*0a6a1f1dSLionel Sambuc     bar();
71*0a6a1f1dSLionel Sambuc     mu.Unlock();
72*0a6a1f1dSLionel Sambuc   }
73*0a6a1f1dSLionel Sambuc 
bar()74*0a6a1f1dSLionel Sambuc   void bar() {
75*0a6a1f1dSLionel Sambuc     baz();        // expected-warning {{calling function 'baz' requires holding  '!mu'}}
76*0a6a1f1dSLionel Sambuc   }
77*0a6a1f1dSLionel Sambuc 
baz()78*0a6a1f1dSLionel Sambuc   void baz() EXCLUSIVE_LOCKS_REQUIRED(!mu) {
79*0a6a1f1dSLionel Sambuc     mu.Lock();
80*0a6a1f1dSLionel Sambuc     a = 0;
81*0a6a1f1dSLionel Sambuc     mu.Unlock();
82*0a6a1f1dSLionel Sambuc   }
83*0a6a1f1dSLionel Sambuc 
test()84*0a6a1f1dSLionel Sambuc   void test() {
85*0a6a1f1dSLionel Sambuc     Bar b;
86*0a6a1f1dSLionel Sambuc     b.baz();     // no warning -- in different class.
87*0a6a1f1dSLionel Sambuc   }
88*0a6a1f1dSLionel Sambuc 
test2()89*0a6a1f1dSLionel Sambuc   void test2() {
90*0a6a1f1dSLionel Sambuc     mu.Lock();   // expected-warning {{acquiring mutex 'mu' requires negative capability '!mu'}}
91*0a6a1f1dSLionel Sambuc     a = 0;
92*0a6a1f1dSLionel Sambuc     mu.Unlock();
93*0a6a1f1dSLionel Sambuc     baz();       // no warning -- !mu in set.
94*0a6a1f1dSLionel Sambuc   }
95*0a6a1f1dSLionel Sambuc 
test3()96*0a6a1f1dSLionel Sambuc   void test3() EXCLUSIVE_LOCKS_REQUIRED(!mu) {
97*0a6a1f1dSLionel Sambuc     mu.Lock();
98*0a6a1f1dSLionel Sambuc     a = 0;
99*0a6a1f1dSLionel Sambuc     mu.Unlock();
100*0a6a1f1dSLionel Sambuc     baz();       // no warning -- !mu in set.
101*0a6a1f1dSLionel Sambuc   }
102*0a6a1f1dSLionel Sambuc };
103*0a6a1f1dSLionel Sambuc 
104*0a6a1f1dSLionel Sambuc }  // end namespace SimpleTest
105*0a6a1f1dSLionel Sambuc 
106*0a6a1f1dSLionel Sambuc namespace DoubleAttribute {
107*0a6a1f1dSLionel Sambuc 
108*0a6a1f1dSLionel Sambuc struct Foo {
109*0a6a1f1dSLionel Sambuc   Mutex &mutex();
110*0a6a1f1dSLionel Sambuc };
111*0a6a1f1dSLionel Sambuc 
112*0a6a1f1dSLionel Sambuc template <typename A>
113*0a6a1f1dSLionel Sambuc class TemplateClass {
114*0a6a1f1dSLionel Sambuc   template <typename B>
Function(Foo * F)115*0a6a1f1dSLionel Sambuc   static void Function(Foo *F)
116*0a6a1f1dSLionel Sambuc       EXCLUSIVE_LOCKS_REQUIRED(F->mutex()) UNLOCK_FUNCTION(F->mutex()) {}
117*0a6a1f1dSLionel Sambuc };
118*0a6a1f1dSLionel Sambuc 
test()119*0a6a1f1dSLionel Sambuc void test() { TemplateClass<int> TC; }
120*0a6a1f1dSLionel Sambuc 
121*0a6a1f1dSLionel Sambuc }  // end namespace DoubleAttribute
122