brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.7 KiB · 0caf6d6 Raw
198 lines · cpp
1// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wthread-safety -Wthread-safety-beta -Wthread-safety-negative -fcxx-exceptions -DUSE_CAPABILITY=0 %s2// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wthread-safety -Wthread-safety-beta -Wthread-safety-negative -fcxx-exceptions -DUSE_CAPABILITY=1 %s3 4// FIXME: should also run  %clang_cc1 -fsyntax-only -verify -Wthread-safety -std=c++11 -Wc++98-compat %s5// FIXME: should also run  %clang_cc1 -fsyntax-only -verify -Wthread-safety %s6 7#include "thread-safety-annotations.h"8 9class LOCKABLE Mutex {10 public: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);16 17  // for negative capabilities18  const Mutex& operator!() const { return *this; }19 20  void AssertHeld()       ASSERT_EXCLUSIVE_LOCK();21  void AssertReaderHeld() ASSERT_SHARED_LOCK();22};23 24class LOCKABLE REENTRANT_CAPABILITY ReentrantMutex {25public:26  void Lock() EXCLUSIVE_LOCK_FUNCTION();27  void Unlock() UNLOCK_FUNCTION();28 29  // for negative capabilities30  const ReentrantMutex& operator!() const { return *this; }31};32 33class SCOPED_LOCKABLE MutexLock {34public:35  MutexLock(Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu);36  MutexLock(Mutex *mu, bool adopt) EXCLUSIVE_LOCKS_REQUIRED(mu);37  ~MutexLock() UNLOCK_FUNCTION();38};39 40namespace SimpleTest {41 42class Bar {43  Mutex mu;44  int a GUARDED_BY(mu);45 46public:47  void baz() EXCLUSIVE_LOCKS_REQUIRED(!mu) {48    mu.Lock();49    a = 0;50    mu.Unlock();51  }52};53 54 55class Foo {56  Mutex mu;57  int a GUARDED_BY(mu);58 59public:60  void foo() {61    mu.Lock();    // expected-warning {{acquiring mutex 'mu' requires negative capability '!mu'}}62    baz();        // expected-warning {{cannot call function 'baz' while mutex 'mu' is held}}63    bar();64    mu.Unlock();65  }66 67  void bar() {68    baz();        // expected-warning {{calling function 'baz' requires negative capability '!mu'}}69  }70 71  void baz() EXCLUSIVE_LOCKS_REQUIRED(!mu) {72    mu.Lock();73    a = 0;74    mu.Unlock();75  }76 77  void test() {78    Bar b;79    b.baz();     // no warning -- in different class.80  }81 82  void test2() {83    mu.Lock();   // expected-warning {{acquiring mutex 'mu' requires negative capability '!mu'}}84    a = 0;85    mu.Unlock();86    baz();       // no warning -- !mu in set.87  }88 89  void test3() EXCLUSIVE_LOCKS_REQUIRED(!mu) {90    mu.Lock();91    a = 0;92    mu.Unlock();93    baz();       // no warning -- !mu in set.94  }95 96  void test4() {97    MutexLock lock(&mu); // expected-warning {{acquiring mutex 'mu' requires negative capability '!mu'}}98  }99};100 101class Reentrant {102  ReentrantMutex mu;103 104public:105  void acquire() {106    mu.Lock();   // no warning -- reentrant mutex107    mu.Unlock();108  }109 110  void requireNegative() EXCLUSIVE_LOCKS_REQUIRED(!mu) { // warning?111    mu.Lock();112    mu.Unlock();113  }114 115  void callRequireNegative() {116    requireNegative(); // expected-warning{{calling function 'requireNegative' requires negative capability '!mu'}}117  }118 119  void callHaveNegative() EXCLUSIVE_LOCKS_REQUIRED(!mu) {120    requireNegative();121  }122};123 124}  // end namespace SimpleTest125 126Mutex globalMutex;127 128namespace ScopeTest {129 130void f() EXCLUSIVE_LOCKS_REQUIRED(!globalMutex);131void fq() EXCLUSIVE_LOCKS_REQUIRED(!::globalMutex);132 133namespace ns {134  Mutex globalMutex;135  void f() EXCLUSIVE_LOCKS_REQUIRED(!globalMutex);136  void fq() EXCLUSIVE_LOCKS_REQUIRED(!ns::globalMutex);137}138 139void testGlobals() EXCLUSIVE_LOCKS_REQUIRED(!ns::globalMutex) {140  f();     // expected-warning {{calling function 'f' requires negative capability '!globalMutex'}}141  fq();    // expected-warning {{calling function 'fq' requires negative capability '!globalMutex'}}142  ns::f();143  ns::fq();144}145 146void testNamespaceGlobals() EXCLUSIVE_LOCKS_REQUIRED(!globalMutex) {147  f();148  fq();149  ns::f();  // expected-warning {{calling function 'f' requires negative capability '!globalMutex'}}150  ns::fq(); // expected-warning {{calling function 'fq' requires negative capability '!globalMutex'}}151}152 153class StaticMembers {154public:155  void pub() EXCLUSIVE_LOCKS_REQUIRED(!publicMutex);156  void prot() EXCLUSIVE_LOCKS_REQUIRED(!protectedMutex);157  void priv() EXCLUSIVE_LOCKS_REQUIRED(!privateMutex);158  void test() {159    pub();160    prot();161    priv();162  }163 164  static Mutex publicMutex;165 166protected:167  static Mutex protectedMutex;168 169private:170  static Mutex privateMutex;171};172 173void testStaticMembers() {174  StaticMembers x;175  x.pub();176  x.prot();177  x.priv();178}179 180}  // end namespace ScopeTest181 182namespace DoubleAttribute {183 184struct Foo {185  Mutex &mutex();186};187 188template <typename A>189class TemplateClass {190  template <typename B>191  static void Function(Foo *F)192      EXCLUSIVE_LOCKS_REQUIRED(F->mutex()) UNLOCK_FUNCTION(F->mutex()) {}193};194 195void test() { TemplateClass<int> TC; }196 197}  // end namespace DoubleAttribute198