1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
15 #include <unordered_set>
17 #include <clang/AST/CXXInheritance.h>
19 #include "config_clang.h"
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.
30 (*) check if the data being returned is never modified, in which case locking is not necessary
36 class UnnecessaryLocking
: public loplugin::FilteringPlugin
<UnnecessaryLocking
>
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"))
51 virtual void run() override
54 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
57 bool VisitCompoundStmt(const CompoundStmt
*);
58 bool VisitCXXThisExpr(const CXXThisExpr
*);
59 bool VisitCallExpr(const CallExpr
*);
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
))
73 if (compoundStmt
->size() < 1)
76 const Stmt
* firstStmt
= *compoundStmt
->body_begin();
77 bool solarMutex
= isSolarMutexLockGuardStmt(firstStmt
);
78 const CXXThisExpr
* ignoreThisStmt
= nullptr;
80 ignoreThisStmt
= isOtherMutexLockGuardStmt(firstStmt
);
81 if (!solarMutex
&& ignoreThisStmt
== nullptr)
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()));
96 !loplugin::isSamePathname(fn
, SRCDIR
"/include/comphelper/unique_disposing_ptr.hxx")
97 && !loplugin::isSamePathname(fn
, SRCDIR
"/sw/inc/unobaseclass.hxx")
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")
119 // needs to lock around access to methods in vcl
120 && !loplugin::isSamePathname(fn
, SRCDIR
"/basctl/source/basicide/unomodel.cxx")
121 && !loplugin::isSamePathname(fn
, SRCDIR
"/cui/source/dialogs/AdditionsDialog.cxx")
122 && !loplugin::isSamePathname(fn
, SRCDIR
"/cui/source/dialogs/cuigaldlg.cxx")
123 && !loplugin::isSamePathname(fn
, SRCDIR
"/dbaccess/source/ui/browser/unodatbr.cxx")
124 && !loplugin::isSamePathname(fn
, SRCDIR
"/dbaccess/source/ui/uno/dbinteraction.cxx")
125 && !loplugin::isSamePathname(fn
, SRCDIR
"/dbaccess/source/ui/dlg/DbAdminImpl.cxx")
126 && !loplugin::isSamePathname(fn
, SRCDIR
"/dbaccess/source/ui/misc/UITools.cxx")
127 && !loplugin::isSamePathname(fn
, SRCDIR
"/desktop/source/lib/lokclipboard.cxx")
128 && !loplugin::isSamePathname(fn
, SRCDIR
"/editeng/source/misc/unolingu.cxx")
129 && !loplugin::isSamePathname(fn
, SRCDIR
130 "/framework/source/uielement/popuptoolbarcontroller.cxx")
131 && !loplugin::isSamePathname(fn
,
132 SRCDIR
"/framework/source/uielement/newmenucontroller.cxx")
133 && !loplugin::isSamePathname(fn
,
134 SRCDIR
"/framework/source/uielement/menubarwrapper.cxx")
135 && !loplugin::isSamePathname(fn
, SRCDIR
"/framework/source/services/desktop.cxx")
136 && !loplugin::isSamePathname(fn
,
137 SRCDIR
"/framework/source/layoutmanager/layoutmanager.cxx")
138 && !loplugin::isSamePathname(fn
, SRCDIR
139 "/framework/source/layoutmanager/toolbarlayoutmanager.cxx")
140 && !loplugin::isSamePathname(fn
, SRCDIR
141 "/framework/source/fwe/helper/actiontriggerhelper.cxx")
142 && !loplugin::isSamePathname(fn
, SRCDIR
"/sc/source/ui/unoobj/unodoc.cxx")
143 && !loplugin::isSamePathname(fn
, SRCDIR
"/sc/source/ui/unoobj/filtuno.cxx")
144 && !loplugin::isSamePathname(fn
, SRCDIR
"/sc/source/ui/unoobj/funcuno.cxx")
145 && !loplugin::isSamePathname(fn
, SRCDIR
"/sc/source/ui/vba/vbaapplication.cxx")
146 && !loplugin::isSamePathname(fn
, SRCDIR
"/sd/source/ui/unoidl/unodoc.cxx")
147 && !loplugin::isSamePathname(fn
, SRCDIR
"/sd/source/ui/unoidl/unomodule.cxx")
148 && !loplugin::isSamePathname(fn
, SRCDIR
"/sd/source/ui/remotecontrol/Receiver.cxx")
149 && !loplugin::isSamePathname(fn
, SRCDIR
150 "/sd/source/ui/slidesorter/controller/SlsClipboard.cxx")
151 && !loplugin::isSamePathname(fn
, SRCDIR
"/sfx2/source/appl/fwkhelper.cxx")
152 && !loplugin::isSamePathname(fn
, SRCDIR
"/sfx2/source/appl/appinit.cxx")
153 && !loplugin::isSamePathname(fn
, SRCDIR
"/sfx2/source/appl/shutdownicon.cxx")
154 && !loplugin::isSamePathname(fn
, SRCDIR
"/sfx2/source/dialog/dockwin.cxx")
155 && !loplugin::isSamePathname(fn
, SRCDIR
"/sfx2/source/statbar/stbitem.cxx")
156 && !loplugin::isSamePathname(fn
, SRCDIR
"/sfx2/source/toolbox/tbxitem.cxx")
157 && !loplugin::isSamePathname(fn
,
158 SRCDIR
"/svtools/source/java/javainteractionhandler.cxx")
159 && !loplugin::isSamePathname(fn
,
160 SRCDIR
"/svx/source/accessibility/ShapeTypeHandler.cxx")
161 && !loplugin::isSamePathname(fn
,
162 SRCDIR
"/svx/source/tbxctrls/tbunosearchcontrollers.cxx")
163 && !loplugin::isSamePathname(fn
, SRCDIR
"/svx/source/form/fmscriptingenv.cxx")
164 && !loplugin::isSamePathname(fn
, SRCDIR
"/svx/source/fmcomp/fmgridif.cxx")
165 && !loplugin::isSamePathname(fn
, SRCDIR
"/toolkit/source/awt/vclxspinbutton.cxx")
166 && !loplugin::isSamePathname(fn
, SRCDIR
"/toolkit/source/awt/vclxtoolkit.cxx")
167 && !loplugin::isSamePathname(fn
,
168 SRCDIR
"/toolkit/source/controls/tree/treecontrolpeer.cxx")
169 && !loplugin::isSamePathname(fn
, SRCDIR
"/ucb/source/ucp/image/ucpimage.cxx")
170 && !loplugin::hasPathnamePrefix(fn
, SRCDIR
"/vcl/")
173 && !loplugin::isSamePathname(fn
,
174 SRCDIR
"/dbaccess/source/ui/browser/AsynchronousLink.cxx")
175 && !loplugin::isSamePathname(fn
, SRCDIR
"/framework/source/services/autorecovery.cxx")
176 && !loplugin::isSamePathname(fn
, SRCDIR
"/sfx2/source/dialog/filedlghelper.cxx")
177 && !loplugin::isSamePathname(fn
, SRCDIR
"/sfx2/source/appl/appdispatchprovider.cxx")
178 && !loplugin::isSamePathname(fn
, SRCDIR
"/ucb/source/ucp/tdoc/tdoc_storage.cxx")
179 && !loplugin::isSamePathname(fn
, SRCDIR
"/sc/source/core/data/poolhelp.cxx")
181 // touching shared global data
182 && !loplugin::isSamePathname(fn
, SRCDIR
183 "/framework/source/fwi/classes/protocolhandlercache.cxx")
184 && !loplugin::isSamePathname(fn
,
185 SRCDIR
"/basic/source/basmgr/basicmanagerrepository.cxx")
186 && !loplugin::isSamePathname(fn
, SRCDIR
"/basic/source/classes/sb.cxx")
187 && !loplugin::isSamePathname(fn
, SRCDIR
"/sc/source/ui/unoobj/docuno.cxx")
188 && !loplugin::isSamePathname(fn
, SRCDIR
"/sc/source/ui/unoobj/afmtuno.cxx")
189 && !loplugin::isSamePathname(fn
, SRCDIR
"/sc/source/ui/unoobj/appluno.cxx")
190 && !loplugin::isSamePathname(fn
, SRCDIR
"/sc/source/ui/vba/vbaapplication.cxx")
191 && !loplugin::isSamePathname(fn
, SRCDIR
"/sw/source/core/access/accdoc.cxx")
192 && !loplugin::isSamePathname(fn
, SRCDIR
"/sw/source/core/access/acccontext.cxx")
193 && !loplugin::isSamePathname(fn
,
194 SRCDIR
"/sw/source/core/bastyp/proofreadingiterator.cxx")
195 && !loplugin::isSamePathname(fn
, SRCDIR
"/sw/source/core/unocore/unoftn.cxx")
196 && !loplugin::isSamePathname(fn
, SRCDIR
"/sw/source/core/unocore/unolinebreak.cxx")
197 && !loplugin::isSamePathname(fn
, SRCDIR
"/sw/source/core/unocore/unoobj.cxx")
198 && !loplugin::isSamePathname(fn
, SRCDIR
"/sw/source/core/unocore/unorefmk.cxx")
199 && !loplugin::isSamePathname(fn
, SRCDIR
"/sw/source/core/unocore/unotbl.cxx")
200 && !loplugin::isSamePathname(fn
, SRCDIR
"/sw/source/core/unocore/unocontentcontrol.cxx")
201 && !loplugin::isSamePathname(fn
, SRCDIR
"/sw/source/core/unocore/unobkm.cxx")
202 && !loplugin::isSamePathname(fn
, SRCDIR
"/sw/source/core/unocore/unocoll.cxx")
203 && !loplugin::isSamePathname(fn
, SRCDIR
"/sw/source/core/unocore/unostyle.cxx")
204 && !loplugin::isSamePathname(fn
, SRCDIR
"/sw/source/filter/xml/xmltexti.cxx")
205 && !loplugin::isSamePathname(fn
, SRCDIR
"/sw/source/uibase/uno/dlelstnr.cxx")
206 && !loplugin::isSamePathname(fn
, SRCDIR
"/sw/source/uibase/uno/unoatxt.cxx")
207 && !loplugin::isSamePathname(fn
, SRCDIR
"/sw/source/uibase/uno/unodoc.cxx")
208 && !loplugin::isSamePathname(fn
, SRCDIR
"/sw/source/uibase/uno/unomodule.cxx")
209 && !loplugin::isSamePathname(fn
, SRCDIR
"/sw/source/uibase/uno/SwXFilterOptions.cxx")
210 && !loplugin::isSamePathname(fn
, SRCDIR
"/sw/source/uibase/shells/translatehelper.cxx")
211 && !loplugin::isSamePathname(fn
, SRCDIR
"/sw/source/ui/vba/vbaapplication.cxx")
212 && !loplugin::isSamePathname(fn
, SRCDIR
"/unoxml/source/dom/documentbuilder.cxx")
213 && !loplugin::isSamePathname(
214 fn
, SRCDIR
"/sd/source/ui/accessibility/AccessibleDrawDocumentView.cxx")
215 && !loplugin::isSamePathname(fn
, SRCDIR
"/starmath/source/accessibility.cxx")
216 && !loplugin::isSamePathname(fn
,
217 SRCDIR
"/starmath/source/AccessibleSmElementsControl.cxx"))
219 report(DiagnosticsEngine::Warning
, "unnecessary locking", compoundStmt
->getBeginLoc())
220 << compoundStmt
->getSourceRange();
224 m_TouchesThis
.pop_back();
225 m_IgnoreThis
.pop_back();
230 bool UnnecessaryLocking::isSolarMutexLockGuardStmt(const Stmt
* stmt
)
232 auto declStmt
= dyn_cast
<DeclStmt
>(stmt
);
235 if (!declStmt
->isSingleDecl())
237 auto varDecl
= dyn_cast
<VarDecl
>(declStmt
->getSingleDecl());
240 auto tc
= loplugin::TypeCheck(varDecl
->getType());
241 if (!tc
.Class("SolarMutexGuard").GlobalNamespace()
242 && !tc
.Class("SolarMutexClearableGuard").GlobalNamespace()
243 && !tc
.Class("SolarMutexResettableGuard").GlobalNamespace()
244 && !tc
.Class("SolarMutexTryAndBuyGuard").GlobalNamespace())
249 const CXXThisExpr
* UnnecessaryLocking::isOtherMutexLockGuardStmt(const Stmt
* stmt
)
251 auto declStmt
= dyn_cast
<DeclStmt
>(stmt
);
254 if (!declStmt
->isSingleDecl())
256 auto varDecl
= dyn_cast
<VarDecl
>(declStmt
->getSingleDecl());
259 auto tc
= loplugin::TypeCheck(varDecl
->getType());
260 if (!tc
.Class("unique_lock").StdNamespace() && !tc
.Class("scoped_lock").StdNamespace()
261 && !tc
.Class("Guard") && !tc
.Class("ClearableGuard") && !tc
.Class("ResettableGuard"))
263 auto cxxConstructExpr
= dyn_cast
<CXXConstructExpr
>(varDecl
->getInit());
264 if (!cxxConstructExpr
|| cxxConstructExpr
->getNumArgs() < 1)
266 auto arg0
= cxxConstructExpr
->getArg(0);
267 if (auto memberExpr
= dyn_cast
<MemberExpr
>(arg0
))
269 const CXXThisExpr
* thisStmt
270 = dyn_cast
<CXXThisExpr
>(memberExpr
->getBase()->IgnoreImplicit());
273 else if (auto memberCallExpr
= dyn_cast
<CXXMemberCallExpr
>(arg0
))
275 return dyn_cast_or_null
<CXXThisExpr
>(
276 memberCallExpr
->getImplicitObjectArgument()->IgnoreImplicit());
281 bool UnnecessaryLocking::VisitCXXThisExpr(const CXXThisExpr
* cxxThisExpr
)
283 if (ignoreLocation(cxxThisExpr
))
286 if (m_TouchesThis
.empty())
288 // already found something
289 if (m_TouchesThis
.back())
291 if (m_IgnoreThis
.back() && m_IgnoreThis
.back() == cxxThisExpr
)
293 m_TouchesThis
.back() = true;
297 bool UnnecessaryLocking::VisitCallExpr(const CallExpr
* callExpr
)
299 if (ignoreLocation(callExpr
))
302 if (m_TouchesThis
.empty())
304 // already found something
305 if (m_TouchesThis
.back())
307 const CXXMethodDecl
* callee
= dyn_cast_or_null
<CXXMethodDecl
>(callExpr
->getDirectCallee());
310 auto dc
= loplugin::DeclCheck(callee
->getParent());
311 if (dc
.Class("VCLUnoHelper") || dc
.Class("Application"))
312 m_TouchesThis
.back() = true;
316 /** off by default because each warning needs to be carefully inspected */
317 loplugin::Plugin::Registration
<UnnecessaryLocking
> unnecessarylocking("unnecessarylocking", false);
320 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */