[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / SemaCXX / warn-thread-safety-verbose.cpp
blobe8b229f3373bee5c3ef8ff251367c440d609994e
1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wthread-safety -Wthread-safety-beta -Wthread-safety-verbose -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 -Wthread-safety-verbose -Wno-thread-safety-negative -fcxx-exceptions -DUSE_CAPABILITY=1 %s
4 #include "thread-safety-annotations.h"
6 class LOCKABLE Mutex {
7 public:
8 void Lock() EXCLUSIVE_LOCK_FUNCTION();
9 void ReaderLock() SHARED_LOCK_FUNCTION();
10 void Unlock() UNLOCK_FUNCTION();
11 bool TryLock() EXCLUSIVE_TRYLOCK_FUNCTION(true);
12 bool ReaderTryLock() SHARED_TRYLOCK_FUNCTION(true);
14 // for negative capabilities
15 const Mutex& operator!() const { return *this; }
17 void AssertHeld() ASSERT_EXCLUSIVE_LOCK();
18 void AssertReaderHeld() ASSERT_SHARED_LOCK();
22 class Test {
23 Mutex mu;
24 int a GUARDED_BY(mu); // expected-note3 {{guarded_by declared here}}
26 void foo1() EXCLUSIVE_LOCKS_REQUIRED(mu);
27 void foo2() SHARED_LOCKS_REQUIRED(mu);
28 void foo3() LOCKS_EXCLUDED(mu);
30 void test1() { // expected-note {{thread warning in function 'test1'}}
31 a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}}
34 void test2() { // expected-note {{thread warning in function 'test2'}}
35 int b = a; // expected-warning {{reading variable 'a' requires holding mutex 'mu'}}
38 void test3() { // expected-note {{thread warning in function 'test3'}}
39 foo1(); // expected-warning {{calling function 'foo1' requires holding mutex 'mu' exclusively}}
42 void test4() { // expected-note {{thread warning in function 'test4'}}
43 foo2(); // expected-warning {{calling function 'foo2' requires holding mutex 'mu'}}
46 void test5() { // expected-note {{thread warning in function 'test5'}}
47 mu.ReaderLock();
48 foo1(); // expected-warning {{calling function 'foo1' requires holding mutex 'mu' exclusively}}
49 mu.Unlock();
52 void test6() { // expected-note {{thread warning in function 'test6'}}
53 mu.ReaderLock();
54 a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}}
55 mu.Unlock();
58 void test7() { // expected-note {{thread warning in function 'test7'}}
59 mu.Lock();
60 foo3(); // expected-warning {{cannot call function 'foo3' while mutex 'mu' is held}}
61 mu.Unlock();