annotate clang/test/SemaCXX/warn-thread-safety-negative.cpp @ 222:81f6424ef0e3 llvm-original

LLVM original branch
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 18 Jul 2021 22:10:01 +0900
parents 79ff65ed7e25
children
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
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
24 class SCOPED_LOCKABLE MutexLock {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
25 public:
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
26 MutexLock(Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
27 MutexLock(Mutex *mu, bool adopt) EXCLUSIVE_LOCKS_REQUIRED(mu);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
28 ~MutexLock() UNLOCK_FUNCTION();
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
29 };
150
anatofuz
parents:
diff changeset
30
anatofuz
parents:
diff changeset
31 namespace SimpleTest {
anatofuz
parents:
diff changeset
32
anatofuz
parents:
diff changeset
33 class Bar {
anatofuz
parents:
diff changeset
34 Mutex mu;
anatofuz
parents:
diff changeset
35 int a GUARDED_BY(mu);
anatofuz
parents:
diff changeset
36
anatofuz
parents:
diff changeset
37 public:
anatofuz
parents:
diff changeset
38 void baz() EXCLUSIVE_LOCKS_REQUIRED(!mu) {
anatofuz
parents:
diff changeset
39 mu.Lock();
anatofuz
parents:
diff changeset
40 a = 0;
anatofuz
parents:
diff changeset
41 mu.Unlock();
anatofuz
parents:
diff changeset
42 }
anatofuz
parents:
diff changeset
43 };
anatofuz
parents:
diff changeset
44
anatofuz
parents:
diff changeset
45
anatofuz
parents:
diff changeset
46 class Foo {
anatofuz
parents:
diff changeset
47 Mutex mu;
anatofuz
parents:
diff changeset
48 int a GUARDED_BY(mu);
anatofuz
parents:
diff changeset
49
anatofuz
parents:
diff changeset
50 public:
anatofuz
parents:
diff changeset
51 void foo() {
anatofuz
parents:
diff changeset
52 mu.Lock(); // expected-warning {{acquiring mutex 'mu' requires negative capability '!mu'}}
anatofuz
parents:
diff changeset
53 baz(); // expected-warning {{cannot call function 'baz' while mutex 'mu' is held}}
anatofuz
parents:
diff changeset
54 bar();
anatofuz
parents:
diff changeset
55 mu.Unlock();
anatofuz
parents:
diff changeset
56 }
anatofuz
parents:
diff changeset
57
anatofuz
parents:
diff changeset
58 void bar() {
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
59 baz(); // expected-warning {{calling function 'baz' requires negative capability '!mu'}}
150
anatofuz
parents:
diff changeset
60 }
anatofuz
parents:
diff changeset
61
anatofuz
parents:
diff changeset
62 void baz() EXCLUSIVE_LOCKS_REQUIRED(!mu) {
anatofuz
parents:
diff changeset
63 mu.Lock();
anatofuz
parents:
diff changeset
64 a = 0;
anatofuz
parents:
diff changeset
65 mu.Unlock();
anatofuz
parents:
diff changeset
66 }
anatofuz
parents:
diff changeset
67
anatofuz
parents:
diff changeset
68 void test() {
anatofuz
parents:
diff changeset
69 Bar b;
anatofuz
parents:
diff changeset
70 b.baz(); // no warning -- in different class.
anatofuz
parents:
diff changeset
71 }
anatofuz
parents:
diff changeset
72
anatofuz
parents:
diff changeset
73 void test2() {
anatofuz
parents:
diff changeset
74 mu.Lock(); // expected-warning {{acquiring mutex 'mu' requires negative capability '!mu'}}
anatofuz
parents:
diff changeset
75 a = 0;
anatofuz
parents:
diff changeset
76 mu.Unlock();
anatofuz
parents:
diff changeset
77 baz(); // no warning -- !mu in set.
anatofuz
parents:
diff changeset
78 }
anatofuz
parents:
diff changeset
79
anatofuz
parents:
diff changeset
80 void test3() EXCLUSIVE_LOCKS_REQUIRED(!mu) {
anatofuz
parents:
diff changeset
81 mu.Lock();
anatofuz
parents:
diff changeset
82 a = 0;
anatofuz
parents:
diff changeset
83 mu.Unlock();
anatofuz
parents:
diff changeset
84 baz(); // no warning -- !mu in set.
anatofuz
parents:
diff changeset
85 }
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
86
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
87 void test4() {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
88 MutexLock lock(&mu); // expected-warning {{acquiring mutex 'mu' requires negative capability '!mu'}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
89 }
150
anatofuz
parents:
diff changeset
90 };
anatofuz
parents:
diff changeset
91
anatofuz
parents:
diff changeset
92 } // end namespace SimpleTest
anatofuz
parents:
diff changeset
93
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
94 Mutex globalMutex;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
95
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
96 namespace ScopeTest {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
97
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
98 void f() EXCLUSIVE_LOCKS_REQUIRED(!globalMutex);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
99 void fq() EXCLUSIVE_LOCKS_REQUIRED(!::globalMutex);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
100
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
101 namespace ns {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
102 Mutex globalMutex;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
103 void f() EXCLUSIVE_LOCKS_REQUIRED(!globalMutex);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
104 void fq() EXCLUSIVE_LOCKS_REQUIRED(!ns::globalMutex);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
105 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
106
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
107 void testGlobals() EXCLUSIVE_LOCKS_REQUIRED(!ns::globalMutex) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
108 f(); // expected-warning {{calling function 'f' requires negative capability '!globalMutex'}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
109 fq(); // expected-warning {{calling function 'fq' requires negative capability '!globalMutex'}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
110 ns::f();
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
111 ns::fq();
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
112 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
113
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
114 void testNamespaceGlobals() EXCLUSIVE_LOCKS_REQUIRED(!globalMutex) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
115 f();
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
116 fq();
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
117 ns::f(); // expected-warning {{calling function 'f' requires negative capability '!globalMutex'}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
118 ns::fq(); // expected-warning {{calling function 'fq' requires negative capability '!globalMutex'}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
119 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
120
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
121 class StaticMembers {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
122 public:
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
123 void pub() EXCLUSIVE_LOCKS_REQUIRED(!publicMutex);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
124 void prot() EXCLUSIVE_LOCKS_REQUIRED(!protectedMutex);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
125 void priv() EXCLUSIVE_LOCKS_REQUIRED(!privateMutex);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
126 void test() {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
127 pub();
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
128 prot();
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
129 priv();
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
130 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
131
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
132 static Mutex publicMutex;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
133
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
134 protected:
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
135 static Mutex protectedMutex;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
136
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
137 private:
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
138 static Mutex privateMutex;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
139 };
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
140
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
141 void testStaticMembers() {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
142 StaticMembers x;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
143 x.pub();
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
144 x.prot();
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
145 x.priv();
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
146 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
147
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
148 } // end namespace ScopeTest
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
149
150
anatofuz
parents:
diff changeset
150 namespace DoubleAttribute {
anatofuz
parents:
diff changeset
151
anatofuz
parents:
diff changeset
152 struct Foo {
anatofuz
parents:
diff changeset
153 Mutex &mutex();
anatofuz
parents:
diff changeset
154 };
anatofuz
parents:
diff changeset
155
anatofuz
parents:
diff changeset
156 template <typename A>
anatofuz
parents:
diff changeset
157 class TemplateClass {
anatofuz
parents:
diff changeset
158 template <typename B>
anatofuz
parents:
diff changeset
159 static void Function(Foo *F)
anatofuz
parents:
diff changeset
160 EXCLUSIVE_LOCKS_REQUIRED(F->mutex()) UNLOCK_FUNCTION(F->mutex()) {}
anatofuz
parents:
diff changeset
161 };
anatofuz
parents:
diff changeset
162
anatofuz
parents:
diff changeset
163 void test() { TemplateClass<int> TC; }
anatofuz
parents:
diff changeset
164
anatofuz
parents:
diff changeset
165 } // end namespace DoubleAttribute