annotate clang/test/SemaCXX/warn-thread-safety-negative.cpp @ 150:1d019706d866

LLVM10
author anatofuz
date Thu, 13 Feb 2020 15:10:13 +0900
parents
children 2e18cbf3894f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wthread-safety -Wthread-safety-beta -Wthread-safety-negative -fcxx-exceptions -DUSE_CAPABILITY=0 %s
anatofuz
parents:
diff changeset
2 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wthread-safety -Wthread-safety-beta -Wthread-safety-negative -fcxx-exceptions -DUSE_CAPABILITY=1 %s
anatofuz
parents:
diff changeset
3
anatofuz
parents:
diff changeset
4 // FIXME: should also run %clang_cc1 -fsyntax-only -verify -Wthread-safety -std=c++11 -Wc++98-compat %s
anatofuz
parents:
diff changeset
5 // FIXME: should also run %clang_cc1 -fsyntax-only -verify -Wthread-safety %s
anatofuz
parents:
diff changeset
6
anatofuz
parents:
diff changeset
7 #include "thread-safety-annotations.h"
anatofuz
parents:
diff changeset
8
anatofuz
parents:
diff changeset
9 class LOCKABLE Mutex {
anatofuz
parents:
diff changeset
10 public:
anatofuz
parents:
diff changeset
11 void Lock() EXCLUSIVE_LOCK_FUNCTION();
anatofuz
parents:
diff changeset
12 void ReaderLock() SHARED_LOCK_FUNCTION();
anatofuz
parents:
diff changeset
13 void Unlock() UNLOCK_FUNCTION();
anatofuz
parents:
diff changeset
14 bool TryLock() EXCLUSIVE_TRYLOCK_FUNCTION(true);
anatofuz
parents:
diff changeset
15 bool ReaderTryLock() SHARED_TRYLOCK_FUNCTION(true);
anatofuz
parents:
diff changeset
16
anatofuz
parents:
diff changeset
17 // for negative capabilities
anatofuz
parents:
diff changeset
18 const Mutex& operator!() const { return *this; }
anatofuz
parents:
diff changeset
19
anatofuz
parents:
diff changeset
20 void AssertHeld() ASSERT_EXCLUSIVE_LOCK();
anatofuz
parents:
diff changeset
21 void AssertReaderHeld() ASSERT_SHARED_LOCK();
anatofuz
parents:
diff changeset
22 };
anatofuz
parents:
diff changeset
23
anatofuz
parents:
diff changeset
24
anatofuz
parents:
diff changeset
25 namespace SimpleTest {
anatofuz
parents:
diff changeset
26
anatofuz
parents:
diff changeset
27 class Bar {
anatofuz
parents:
diff changeset
28 Mutex mu;
anatofuz
parents:
diff changeset
29 int a GUARDED_BY(mu);
anatofuz
parents:
diff changeset
30
anatofuz
parents:
diff changeset
31 public:
anatofuz
parents:
diff changeset
32 void baz() EXCLUSIVE_LOCKS_REQUIRED(!mu) {
anatofuz
parents:
diff changeset
33 mu.Lock();
anatofuz
parents:
diff changeset
34 a = 0;
anatofuz
parents:
diff changeset
35 mu.Unlock();
anatofuz
parents:
diff changeset
36 }
anatofuz
parents:
diff changeset
37 };
anatofuz
parents:
diff changeset
38
anatofuz
parents:
diff changeset
39
anatofuz
parents:
diff changeset
40 class Foo {
anatofuz
parents:
diff changeset
41 Mutex mu;
anatofuz
parents:
diff changeset
42 int a GUARDED_BY(mu);
anatofuz
parents:
diff changeset
43
anatofuz
parents:
diff changeset
44 public:
anatofuz
parents:
diff changeset
45 void foo() {
anatofuz
parents:
diff changeset
46 mu.Lock(); // expected-warning {{acquiring mutex 'mu' requires negative capability '!mu'}}
anatofuz
parents:
diff changeset
47 baz(); // expected-warning {{cannot call function 'baz' while mutex 'mu' is held}}
anatofuz
parents:
diff changeset
48 bar();
anatofuz
parents:
diff changeset
49 mu.Unlock();
anatofuz
parents:
diff changeset
50 }
anatofuz
parents:
diff changeset
51
anatofuz
parents:
diff changeset
52 void bar() {
anatofuz
parents:
diff changeset
53 baz(); // expected-warning {{calling function 'baz' requires holding '!mu'}}
anatofuz
parents:
diff changeset
54 }
anatofuz
parents:
diff changeset
55
anatofuz
parents:
diff changeset
56 void baz() EXCLUSIVE_LOCKS_REQUIRED(!mu) {
anatofuz
parents:
diff changeset
57 mu.Lock();
anatofuz
parents:
diff changeset
58 a = 0;
anatofuz
parents:
diff changeset
59 mu.Unlock();
anatofuz
parents:
diff changeset
60 }
anatofuz
parents:
diff changeset
61
anatofuz
parents:
diff changeset
62 void test() {
anatofuz
parents:
diff changeset
63 Bar b;
anatofuz
parents:
diff changeset
64 b.baz(); // no warning -- in different class.
anatofuz
parents:
diff changeset
65 }
anatofuz
parents:
diff changeset
66
anatofuz
parents:
diff changeset
67 void test2() {
anatofuz
parents:
diff changeset
68 mu.Lock(); // expected-warning {{acquiring mutex 'mu' requires negative capability '!mu'}}
anatofuz
parents:
diff changeset
69 a = 0;
anatofuz
parents:
diff changeset
70 mu.Unlock();
anatofuz
parents:
diff changeset
71 baz(); // no warning -- !mu in set.
anatofuz
parents:
diff changeset
72 }
anatofuz
parents:
diff changeset
73
anatofuz
parents:
diff changeset
74 void test3() EXCLUSIVE_LOCKS_REQUIRED(!mu) {
anatofuz
parents:
diff changeset
75 mu.Lock();
anatofuz
parents:
diff changeset
76 a = 0;
anatofuz
parents:
diff changeset
77 mu.Unlock();
anatofuz
parents:
diff changeset
78 baz(); // no warning -- !mu in set.
anatofuz
parents:
diff changeset
79 }
anatofuz
parents:
diff changeset
80 };
anatofuz
parents:
diff changeset
81
anatofuz
parents:
diff changeset
82 } // end namespace SimpleTest
anatofuz
parents:
diff changeset
83
anatofuz
parents:
diff changeset
84 namespace DoubleAttribute {
anatofuz
parents:
diff changeset
85
anatofuz
parents:
diff changeset
86 struct Foo {
anatofuz
parents:
diff changeset
87 Mutex &mutex();
anatofuz
parents:
diff changeset
88 };
anatofuz
parents:
diff changeset
89
anatofuz
parents:
diff changeset
90 template <typename A>
anatofuz
parents:
diff changeset
91 class TemplateClass {
anatofuz
parents:
diff changeset
92 template <typename B>
anatofuz
parents:
diff changeset
93 static void Function(Foo *F)
anatofuz
parents:
diff changeset
94 EXCLUSIVE_LOCKS_REQUIRED(F->mutex()) UNLOCK_FUNCTION(F->mutex()) {}
anatofuz
parents:
diff changeset
95 };
anatofuz
parents:
diff changeset
96
anatofuz
parents:
diff changeset
97 void test() { TemplateClass<int> TC; }
anatofuz
parents:
diff changeset
98
anatofuz
parents:
diff changeset
99 } // end namespace DoubleAttribute