Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / unnecessarylocking.cxx
blob40b15518571dd2734d254083b7c0293ffd493f84
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <cassert>
11 #include <string>
12 #include <iostream>
13 #include <fstream>
14 #include <set>
15 #include <unordered_set>
17 #include <clang/AST/CXXInheritance.h>
19 #include "config_clang.h"
21 #include "plugin.hxx"
22 #include "check.hxx"
24 /**
25 Look for methods that are taking a lock at the top of the method, but then not
26 touching any object-local state. In which case the method might not need locking.
28 TODO
30 (*) check if the data being returned is never modified, in which case locking is not necessary
34 namespace
36 class UnnecessaryLocking : public loplugin::FilteringPlugin<UnnecessaryLocking>
38 public:
39 explicit UnnecessaryLocking(loplugin::InstantiationData const& data)
40 : FilteringPlugin(data)
44 virtual bool preRun() override
46 // StringRef fn(handler.getMainFileName());
47 // if (loplugin::isSamePathname(fn, WORKDIR "/YaccTarget/unoidl/source/sourceprovider-parser.cxx"))
48 // return false;
49 return true;
51 virtual void run() override
53 if (preRun())
54 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
57 bool VisitCompoundStmt(const CompoundStmt*);
58 bool VisitCXXThisExpr(const CXXThisExpr*);
59 bool VisitCallExpr(const CallExpr*);
61 private:
62 bool isSolarMutexLockGuardStmt(const Stmt*);
63 const CXXThisExpr* isOtherMutexLockGuardStmt(const Stmt*);
64 std::vector<bool> m_TouchesThis;
65 // so we ignore the CxxThisEpxr that references the maMutex in the guard expression
66 std::vector<const CXXThisExpr*> m_IgnoreThis;
69 bool UnnecessaryLocking::VisitCompoundStmt(const CompoundStmt* compoundStmt)
71 if (ignoreLocation(compoundStmt))
72 return true;
73 if (compoundStmt->size() < 1)
74 return true;
76 const Stmt* firstStmt = *compoundStmt->body_begin();
77 bool solarMutex = isSolarMutexLockGuardStmt(firstStmt);
78 const CXXThisExpr* ignoreThisStmt = nullptr;
79 if (!solarMutex)
80 ignoreThisStmt = isOtherMutexLockGuardStmt(firstStmt);
81 if (!solarMutex && ignoreThisStmt == nullptr)
82 return true;
84 m_TouchesThis.push_back(false);
85 m_IgnoreThis.push_back(ignoreThisStmt);
87 for (const Stmt* stmt : compoundStmt->body())
88 FilteringPlugin::TraverseStmt(const_cast<Stmt*>(stmt));
90 if (!m_TouchesThis.back())
92 StringRef fn = getFilenameOfLocation(
93 compiler.getSourceManager().getSpellingLoc(compoundStmt->getBeginLoc()));
94 if (
95 // template magic
96 !loplugin::isSamePathname(fn, SRCDIR "/include/comphelper/unique_disposing_ptr.hxx")
97 && !loplugin::isSamePathname(fn, SRCDIR "/sw/inc/unobaseclass.hxx")
99 // false+
100 && !loplugin::isSamePathname(fn, SRCDIR "/cppuhelper/source/component_context.cxx")
101 && !loplugin::isSamePathname(fn, SRCDIR "/toolkit/source/controls/tree/treecontrol.cxx")
102 && !loplugin::isSamePathname(fn,
103 SRCDIR "/toolkit/source/helper/listenermultiplexer.cxx")
104 && !loplugin::isSamePathname(fn, SRCDIR "/include/toolkit/helper/macros.hxx")
105 && !loplugin::isSamePathname(fn, SRCDIR
106 "/chart2/source/controller/main/CommandDispatch.cxx")
107 && !loplugin::isSamePathname(fn, SRCDIR "/chart2/source/controller/main/ChartView.cxx")
108 && !loplugin::isSamePathname(fn, SRCDIR
109 "/chart2/source/controller/main/SelectionHelper.cxx")
110 && !loplugin::isSamePathname(
111 fn, SRCDIR "/chart2/source/controller/accessibility/AccessibleChartView.cxx")
112 && !loplugin::isSamePathname(fn, SRCDIR "/desktop/source/offacc/acceptor.cxx")
113 && !loplugin::isSamePathname(
114 fn, SRCDIR "/desktop/source/deployment/registry/component/dp_component.cxx")
115 && !loplugin::isSamePathname(fn,
116 SRCDIR "/desktop/source/deployment/gui/dp_gui_dialog2.cxx")
117 && !loplugin::isSamePathname(fn, SRCDIR "/desktop/source/lib/init.cxx")
118 && !loplugin::isSamePathname(
119 fn, SRCDIR "/sd/source/ui/slidesorter/cache/SlsCacheConfiguration.cxx")
121 // needs to lock around access to methods in vcl
122 && !loplugin::isSamePathname(fn, SRCDIR "/basctl/source/basicide/unomodel.cxx")
123 && !loplugin::isSamePathname(fn, SRCDIR "/cui/source/dialogs/AdditionsDialog.cxx")
124 && !loplugin::isSamePathname(fn, SRCDIR "/cui/source/dialogs/cuigaldlg.cxx")
125 && !loplugin::isSamePathname(fn, SRCDIR "/dbaccess/source/ui/browser/unodatbr.cxx")
126 && !loplugin::isSamePathname(fn, SRCDIR "/dbaccess/source/ui/uno/dbinteraction.cxx")
127 && !loplugin::isSamePathname(fn, SRCDIR "/dbaccess/source/ui/dlg/DbAdminImpl.cxx")
128 && !loplugin::isSamePathname(fn, SRCDIR "/dbaccess/source/ui/misc/UITools.cxx")
129 && !loplugin::isSamePathname(fn, SRCDIR "/desktop/source/lib/lokclipboard.cxx")
130 && !loplugin::isSamePathname(fn, SRCDIR "/editeng/source/misc/unolingu.cxx")
131 && !loplugin::isSamePathname(fn, SRCDIR
132 "/framework/source/uielement/popuptoolbarcontroller.cxx")
133 && !loplugin::isSamePathname(fn,
134 SRCDIR "/framework/source/uielement/newmenucontroller.cxx")
135 && !loplugin::isSamePathname(fn,
136 SRCDIR "/framework/source/uielement/menubarwrapper.cxx")
137 && !loplugin::isSamePathname(fn, SRCDIR "/framework/source/services/desktop.cxx")
138 && !loplugin::isSamePathname(fn,
139 SRCDIR "/framework/source/layoutmanager/layoutmanager.cxx")
140 && !loplugin::isSamePathname(fn, SRCDIR
141 "/framework/source/layoutmanager/toolbarlayoutmanager.cxx")
142 && !loplugin::isSamePathname(fn, SRCDIR
143 "/framework/source/fwe/helper/actiontriggerhelper.cxx")
144 && !loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/unoobj/unodoc.cxx")
145 && !loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/unoobj/filtuno.cxx")
146 && !loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/unoobj/funcuno.cxx")
147 && !loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/vba/vbaapplication.cxx")
148 && !loplugin::isSamePathname(fn, SRCDIR "/sd/source/ui/unoidl/unodoc.cxx")
149 && !loplugin::isSamePathname(fn, SRCDIR "/sd/source/ui/unoidl/unomodule.cxx")
150 && !loplugin::isSamePathname(fn, SRCDIR "/sd/source/ui/remotecontrol/Receiver.cxx")
151 && !loplugin::isSamePathname(fn, SRCDIR
152 "/sd/source/ui/slidesorter/controller/SlsClipboard.cxx")
153 && !loplugin::isSamePathname(fn, SRCDIR "/sfx2/source/appl/fwkhelper.cxx")
154 && !loplugin::isSamePathname(fn, SRCDIR "/sfx2/source/appl/appinit.cxx")
155 && !loplugin::isSamePathname(fn, SRCDIR "/sfx2/source/appl/shutdownicon.cxx")
156 && !loplugin::isSamePathname(fn, SRCDIR "/sfx2/source/dialog/dockwin.cxx")
157 && !loplugin::isSamePathname(fn, SRCDIR "/sfx2/source/statbar/stbitem.cxx")
158 && !loplugin::isSamePathname(fn, SRCDIR "/sfx2/source/toolbox/tbxitem.cxx")
159 && !loplugin::isSamePathname(fn,
160 SRCDIR "/svtools/source/java/javainteractionhandler.cxx")
161 && !loplugin::isSamePathname(fn,
162 SRCDIR "/svx/source/accessibility/ShapeTypeHandler.cxx")
163 && !loplugin::isSamePathname(fn,
164 SRCDIR "/svx/source/tbxctrls/tbunosearchcontrollers.cxx")
165 && !loplugin::isSamePathname(fn, SRCDIR "/svx/source/form/fmscriptingenv.cxx")
166 && !loplugin::isSamePathname(fn, SRCDIR "/svx/source/fmcomp/fmgridif.cxx")
167 && !loplugin::isSamePathname(fn, SRCDIR "/toolkit/source/awt/vclxspinbutton.cxx")
168 && !loplugin::isSamePathname(fn, SRCDIR "/toolkit/source/awt/vclxtoolkit.cxx")
169 && !loplugin::isSamePathname(fn,
170 SRCDIR "/toolkit/source/controls/tree/treecontrolpeer.cxx")
171 && !loplugin::isSamePathname(fn, SRCDIR "/ucb/source/ucp/image/ucpimage.cxx")
172 && !loplugin::hasPathnamePrefix(fn, SRCDIR "/vcl/")
174 // not sure
175 && !loplugin::isSamePathname(fn,
176 SRCDIR "/dbaccess/source/ui/browser/AsynchronousLink.cxx")
177 && !loplugin::isSamePathname(fn, SRCDIR "/framework/source/services/autorecovery.cxx")
178 && !loplugin::isSamePathname(fn, SRCDIR "/sfx2/source/dialog/filedlghelper.cxx")
179 && !loplugin::isSamePathname(fn, SRCDIR "/sfx2/source/appl/appdispatchprovider.cxx")
180 && !loplugin::isSamePathname(fn, SRCDIR "/ucb/source/ucp/tdoc/tdoc_storage.cxx")
181 && !loplugin::isSamePathname(fn, SRCDIR "/sc/source/core/data/poolhelp.cxx")
183 // touching shared global data
184 && !loplugin::isSamePathname(fn, SRCDIR
185 "/framework/source/fwi/classes/protocolhandlercache.cxx")
186 && !loplugin::isSamePathname(fn,
187 SRCDIR "/basic/source/basmgr/basicmanagerrepository.cxx")
188 && !loplugin::isSamePathname(fn, SRCDIR "/basic/source/classes/sb.cxx")
189 && !loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/unoobj/docuno.cxx")
190 && !loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/unoobj/afmtuno.cxx")
191 && !loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/unoobj/appluno.cxx")
192 && !loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/vba/vbaapplication.cxx")
193 && !loplugin::isSamePathname(fn, SRCDIR "/sw/source/core/access/accdoc.cxx")
194 && !loplugin::isSamePathname(fn, SRCDIR "/sw/source/core/access/acccontext.cxx")
195 && !loplugin::isSamePathname(fn,
196 SRCDIR "/sw/source/core/bastyp/proofreadingiterator.cxx")
197 && !loplugin::isSamePathname(fn, SRCDIR "/sw/source/core/unocore/unoftn.cxx")
198 && !loplugin::isSamePathname(fn, SRCDIR "/sw/source/core/unocore/unolinebreak.cxx")
199 && !loplugin::isSamePathname(fn, SRCDIR "/sw/source/core/unocore/unoobj.cxx")
200 && !loplugin::isSamePathname(fn, SRCDIR "/sw/source/core/unocore/unorefmk.cxx")
201 && !loplugin::isSamePathname(fn, SRCDIR "/sw/source/core/unocore/unotbl.cxx")
202 && !loplugin::isSamePathname(fn, SRCDIR "/sw/source/core/unocore/unocontentcontrol.cxx")
203 && !loplugin::isSamePathname(fn, SRCDIR "/sw/source/core/unocore/unobkm.cxx")
204 && !loplugin::isSamePathname(fn, SRCDIR "/sw/source/core/unocore/unocoll.cxx")
205 && !loplugin::isSamePathname(fn, SRCDIR "/sw/source/core/unocore/unostyle.cxx")
206 && !loplugin::isSamePathname(fn, SRCDIR "/sw/source/filter/xml/xmltexti.cxx")
207 && !loplugin::isSamePathname(fn, SRCDIR "/sw/source/uibase/uno/dlelstnr.cxx")
208 && !loplugin::isSamePathname(fn, SRCDIR "/sw/source/uibase/uno/unoatxt.cxx")
209 && !loplugin::isSamePathname(fn, SRCDIR "/sw/source/uibase/uno/unodoc.cxx")
210 && !loplugin::isSamePathname(fn, SRCDIR "/sw/source/uibase/uno/unomodule.cxx")
211 && !loplugin::isSamePathname(fn, SRCDIR "/sw/source/uibase/uno/SwXFilterOptions.cxx")
212 && !loplugin::isSamePathname(fn, SRCDIR "/sw/source/uibase/shells/translatehelper.cxx")
213 && !loplugin::isSamePathname(fn, SRCDIR "/sw/source/ui/vba/vbaapplication.cxx")
214 && !loplugin::isSamePathname(fn, SRCDIR "/unoxml/source/dom/documentbuilder.cxx")
215 && !loplugin::isSamePathname(
216 fn, SRCDIR "/sd/source/ui/accessibility/AccessibleDrawDocumentView.cxx")
217 && !loplugin::isSamePathname(fn, SRCDIR "/starmath/source/accessibility.cxx")
218 && !loplugin::isSamePathname(fn,
219 SRCDIR "/starmath/source/AccessibleSmElementsControl.cxx"))
221 report(DiagnosticsEngine::Warning, "unnecessary locking", compoundStmt->getBeginLoc())
222 << compoundStmt->getSourceRange();
226 m_TouchesThis.pop_back();
227 m_IgnoreThis.pop_back();
229 return true;
232 bool UnnecessaryLocking::isSolarMutexLockGuardStmt(const Stmt* stmt)
234 auto declStmt = dyn_cast<DeclStmt>(stmt);
235 if (!declStmt)
236 return false;
237 if (!declStmt->isSingleDecl())
238 return false;
239 auto varDecl = dyn_cast<VarDecl>(declStmt->getSingleDecl());
240 if (!varDecl)
241 return false;
242 auto tc = loplugin::TypeCheck(varDecl->getType());
243 if (!tc.Class("SolarMutexGuard").GlobalNamespace()
244 && !tc.Class("SolarMutexClearableGuard").GlobalNamespace()
245 && !tc.Class("SolarMutexResettableGuard").GlobalNamespace()
246 && !tc.Class("SolarMutexTryAndBuyGuard").GlobalNamespace())
247 return false;
248 return true;
251 const CXXThisExpr* UnnecessaryLocking::isOtherMutexLockGuardStmt(const Stmt* stmt)
253 auto declStmt = dyn_cast<DeclStmt>(stmt);
254 if (!declStmt)
255 return nullptr;
256 if (!declStmt->isSingleDecl())
257 return nullptr;
258 auto varDecl = dyn_cast<VarDecl>(declStmt->getSingleDecl());
259 if (!varDecl)
260 return nullptr;
261 auto tc = loplugin::TypeCheck(varDecl->getType());
262 if (!tc.Class("unique_lock").StdNamespace() && !tc.Class("scoped_lock").StdNamespace()
263 && !tc.Class("Guard") && !tc.Class("ClearableGuard") && !tc.Class("ResettableGuard"))
264 return nullptr;
265 auto cxxConstructExpr = dyn_cast<CXXConstructExpr>(varDecl->getInit());
266 if (!cxxConstructExpr || cxxConstructExpr->getNumArgs() < 1)
267 return nullptr;
268 auto arg0 = cxxConstructExpr->getArg(0);
269 if (auto memberExpr = dyn_cast<MemberExpr>(arg0))
271 const CXXThisExpr* thisStmt
272 = dyn_cast<CXXThisExpr>(memberExpr->getBase()->IgnoreImplicit());
273 return thisStmt;
275 else if (auto memberCallExpr = dyn_cast<CXXMemberCallExpr>(arg0))
277 return dyn_cast_or_null<CXXThisExpr>(
278 memberCallExpr->getImplicitObjectArgument()->IgnoreImplicit());
280 return nullptr;
283 bool UnnecessaryLocking::VisitCXXThisExpr(const CXXThisExpr* cxxThisExpr)
285 if (ignoreLocation(cxxThisExpr))
286 return true;
287 // just in case
288 if (m_TouchesThis.empty())
289 return true;
290 // already found something
291 if (m_TouchesThis.back())
292 return true;
293 if (m_IgnoreThis.back() && m_IgnoreThis.back() == cxxThisExpr)
294 return true;
295 m_TouchesThis.back() = true;
296 return true;
299 bool UnnecessaryLocking::VisitCallExpr(const CallExpr* callExpr)
301 if (ignoreLocation(callExpr))
302 return true;
303 // just in case
304 if (m_TouchesThis.empty())
305 return true;
306 // already found something
307 if (m_TouchesThis.back())
308 return true;
309 const CXXMethodDecl* callee = dyn_cast_or_null<CXXMethodDecl>(callExpr->getDirectCallee());
310 if (!callee)
311 return true;
312 auto dc = loplugin::DeclCheck(callee->getParent());
313 if (dc.Class("VCLUnoHelper") || dc.Class("Application"))
314 m_TouchesThis.back() = true;
315 return true;
318 /** off by default because each warning needs to be carefully inspected */
319 loplugin::Plugin::Registration<UnnecessaryLocking> unnecessarylocking("unnecessarylocking", false);
322 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */