Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / test / unnecessarylocking.cxx
bloba2319623140a393785f99ef0ef56aadda2b2e32e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <mutex>
11 #include <osl/mutex.hxx>
13 static std::mutex gSolarMutex;
15 class SolarMutexGuard
17 std::unique_lock<std::mutex> lock;
19 public:
20 SolarMutexGuard()
21 : lock(gSolarMutex)
26 namespace test1
28 struct Foo
30 int m_foo;
31 int bar1()
32 // expected-error@+1 {{unnecessary locking [loplugin:unnecessarylocking]}}
34 SolarMutexGuard guard;
35 return 1;
37 // no warning expected
38 int bar2()
40 SolarMutexGuard guard;
41 return m_foo;
46 namespace test2
48 int free_function() { return 1; }
50 struct Foo
52 std::mutex m_aMutex;
53 osl::Mutex m_aOslMutex;
54 int m_foo;
56 int bar1()
57 // expected-error@+1 {{unnecessary locking [loplugin:unnecessarylocking]}}
59 std::unique_lock guard(m_aMutex);
60 return 1;
62 int bar2()
63 // expected-error@+1 {{unnecessary locking [loplugin:unnecessarylocking]}}
65 std::scoped_lock guard(m_aMutex);
66 return 1;
68 // no warning expected
69 int bar3()
71 std::scoped_lock guard(m_aMutex);
72 return m_foo;
74 int bar4()
75 // expected-error@+1 {{unnecessary locking [loplugin:unnecessarylocking]}}
77 ::osl::Guard<::osl::Mutex> aGuard(m_aOslMutex);
78 return 1;
80 int bar5()
82 // expected-error@+1 {{unnecessary locking [loplugin:unnecessarylocking]}}
84 std::unique_lock guard(m_aMutex);
85 return free_function();
88 osl::Mutex& getOslMutex() { return m_aOslMutex; }
89 int bar6()
90 // expected-error@+1 {{unnecessary locking [loplugin:unnecessarylocking]}}
92 ::osl::Guard<::osl::Mutex> aGuard(getOslMutex());
93 return 1;
98 // Calling anything on VCLUnoHelper means we need the SolarMutex
99 class VCLUnoHelper
101 public:
102 static int CreateToolkit();
104 namespace test4
106 // no warning expected
107 void bar1()
109 SolarMutexGuard guard;
110 VCLUnoHelper::CreateToolkit();
114 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */