blob: a2319623140a393785f99ef0ef56aadda2b2e32e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <mutex>
#include <osl/mutex.hxx>
static std::mutex gSolarMutex;
class SolarMutexGuard
{
std::unique_lock<std::mutex> lock;
public:
SolarMutexGuard()
: lock(gSolarMutex)
{
}
};
namespace test1
{
struct Foo
{
int m_foo;
int bar1()
// expected-error@+1 {{unnecessary locking [loplugin:unnecessarylocking]}}
{
SolarMutexGuard guard;
return 1;
}
// no warning expected
int bar2()
{
SolarMutexGuard guard;
return m_foo;
}
};
}
namespace test2
{
int free_function() { return 1; }
struct Foo
{
std::mutex m_aMutex;
osl::Mutex m_aOslMutex;
int m_foo;
int bar1()
// expected-error@+1 {{unnecessary locking [loplugin:unnecessarylocking]}}
{
std::unique_lock guard(m_aMutex);
return 1;
}
int bar2()
// expected-error@+1 {{unnecessary locking [loplugin:unnecessarylocking]}}
{
std::scoped_lock guard(m_aMutex);
return 1;
}
// no warning expected
int bar3()
{
std::scoped_lock guard(m_aMutex);
return m_foo;
}
int bar4()
// expected-error@+1 {{unnecessary locking [loplugin:unnecessarylocking]}}
{
::osl::Guard<::osl::Mutex> aGuard(m_aOslMutex);
return 1;
}
int bar5()
{
// expected-error@+1 {{unnecessary locking [loplugin:unnecessarylocking]}}
{
std::unique_lock guard(m_aMutex);
return free_function();
}
}
osl::Mutex& getOslMutex() { return m_aOslMutex; }
int bar6()
// expected-error@+1 {{unnecessary locking [loplugin:unnecessarylocking]}}
{
::osl::Guard<::osl::Mutex> aGuard(getOslMutex());
return 1;
}
};
}
// Calling anything on VCLUnoHelper means we need the SolarMutex
class VCLUnoHelper
{
public:
static int CreateToolkit();
};
namespace test4
{
// no warning expected
void bar1()
{
SolarMutexGuard guard;
VCLUnoHelper::CreateToolkit();
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
|