1 // RUN: %clang_analyze_cc1 -analyzer-checker=alpha.unix.BlockInCriticalSection -std=c++11 -verify %s
2 
3 void sleep(int x) {}
4 
5 namespace std {
6 struct mutex {
7   void lock() {}
8   void unlock() {}
9 };
10 }
11 
12 void testBlockInCriticalSection() {
13   std::mutex m;
14   m.lock();
15   sleep(3); // expected-warning {{A blocking function %s is called inside a critical section}}
16   m.unlock();
17 }
18 
19 void testBlockInCriticalSectionWithNestedMutexes() {
20   std::mutex m, n, k;
21   m.lock();
22   n.lock();
23   k.lock();
24   sleep(3); // expected-warning {{A blocking function %s is called inside a critical section}}
25   k.unlock();
26   sleep(5); // expected-warning {{A blocking function %s is called inside a critical section}}
27   n.unlock();
28   sleep(3); // expected-warning {{A blocking function %s is called inside a critical section}}
29   m.unlock();
30   sleep(3); // no-warning
31 }
32 
33 void f() {
34   sleep(1000); // expected-warning {{A blocking function %s is called inside a critical section}}
35 }
36 
37 void testBlockInCriticalSectionInterProcedural() {
38   std::mutex m;
39   m.lock();
40   f();
41   m.unlock();
42 }
43 
44 void testBlockInCriticalSectionUnexpectedUnlock() {
45   std::mutex m;
46   m.unlock();
47   sleep(1); // no-warning
48   m.lock();
49   sleep(1); // expected-warning {{A blocking function %s is called inside a critical section}}
50 }
51