1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wthread-safety -Wthread-safety-beta -Wthread-safety-negative -fcxx-exceptions -DUSE_CAPABILITY=0 %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wthread-safety -Wthread-safety-beta -Wthread-safety-negative -fcxx-exceptions -DUSE_CAPABILITY=1 %s
4 // FIXME: should also run %clang_cc1 -fsyntax-only -verify -Wthread-safety -std=c++11 -Wc++98-compat %s
5 // FIXME: should also run %clang_cc1 -fsyntax-only -verify -Wthread-safety %s
7 #include "thread-safety-annotations.h"
11 void Lock() EXCLUSIVE_LOCK_FUNCTION();
12 void ReaderLock() SHARED_LOCK_FUNCTION();
13 void Unlock() UNLOCK_FUNCTION();
14 bool TryLock() EXCLUSIVE_TRYLOCK_FUNCTION(true);
15 bool ReaderTryLock() SHARED_TRYLOCK_FUNCTION(true);
17 // for negative capabilities
18 const Mutex
& operator!() const { return *this; }
20 void AssertHeld() ASSERT_EXCLUSIVE_LOCK();
21 void AssertReaderHeld() ASSERT_SHARED_LOCK();
24 class SCOPED_LOCKABLE MutexLock
{
26 MutexLock(Mutex
*mu
) EXCLUSIVE_LOCK_FUNCTION(mu
);
27 MutexLock(Mutex
*mu
, bool adopt
) EXCLUSIVE_LOCKS_REQUIRED(mu
);
28 ~MutexLock() UNLOCK_FUNCTION();
31 namespace SimpleTest
{
38 void baz() EXCLUSIVE_LOCKS_REQUIRED(!mu
) {
52 mu
.Lock(); // expected-warning {{acquiring mutex 'mu' requires negative capability '!mu'}}
53 baz(); // expected-warning {{cannot call function 'baz' while mutex 'mu' is held}}
59 baz(); // expected-warning {{calling function 'baz' requires negative capability '!mu'}}
62 void baz() EXCLUSIVE_LOCKS_REQUIRED(!mu
) {
70 b
.baz(); // no warning -- in different class.
74 mu
.Lock(); // expected-warning {{acquiring mutex 'mu' requires negative capability '!mu'}}
77 baz(); // no warning -- !mu in set.
80 void test3() EXCLUSIVE_LOCKS_REQUIRED(!mu
) {
84 baz(); // no warning -- !mu in set.
88 MutexLock
lock(&mu
); // expected-warning {{acquiring mutex 'mu' requires negative capability '!mu'}}
92 } // end namespace SimpleTest
98 void f() EXCLUSIVE_LOCKS_REQUIRED(!globalMutex
);
99 void fq() EXCLUSIVE_LOCKS_REQUIRED(!::globalMutex
);
103 void f() EXCLUSIVE_LOCKS_REQUIRED(!globalMutex
);
104 void fq() EXCLUSIVE_LOCKS_REQUIRED(!ns::globalMutex
);
107 void testGlobals() EXCLUSIVE_LOCKS_REQUIRED(!ns::globalMutex
) {
108 f(); // expected-warning {{calling function 'f' requires negative capability '!globalMutex'}}
109 fq(); // expected-warning {{calling function 'fq' requires negative capability '!globalMutex'}}
114 void testNamespaceGlobals() EXCLUSIVE_LOCKS_REQUIRED(!globalMutex
) {
117 ns::f(); // expected-warning {{calling function 'f' requires negative capability '!globalMutex'}}
118 ns::fq(); // expected-warning {{calling function 'fq' requires negative capability '!globalMutex'}}
121 class StaticMembers
{
123 void pub() EXCLUSIVE_LOCKS_REQUIRED(!publicMutex
);
124 void prot() EXCLUSIVE_LOCKS_REQUIRED(!protectedMutex
);
125 void priv() EXCLUSIVE_LOCKS_REQUIRED(!privateMutex
);
132 static Mutex publicMutex
;
135 static Mutex protectedMutex
;
138 static Mutex privateMutex
;
141 void testStaticMembers() {
148 } // end namespace ScopeTest
150 namespace DoubleAttribute
{
156 template <typename A
>
157 class TemplateClass
{
158 template <typename B
>
159 static void Function(Foo
*F
)
160 EXCLUSIVE_LOCKS_REQUIRED(F
->mutex()) UNLOCK_FUNCTION(F
->mutex()) {}
163 void test() { TemplateClass
<int> TC
; }
165 } // end namespace DoubleAttribute