blob: 93c0b6dba665d8936e371fcbca933c01224e0d99 [file] [log] [blame]
Anna Zaksc154f7b2016-09-20 20:28:50 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=alpha.unix.BlockInCriticalSection -std=c++11 -verify %s
2
3void sleep(int x) {}
4
5namespace std {
6struct mutex {
7 void lock() {}
8 void unlock() {}
9};
10}
11
12void 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
19void 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
33void f() {
34 sleep(1000); // expected-warning {{A blocking function %s is called inside a critical section}}
35}
36
37void testBlockInCriticalSectionInterProcedural() {
38 std::mutex m;
39 m.lock();
40 f();
41 m.unlock();
42}
43
44void 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}