1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
7 * This file is distributed under the University of Illinois Open Source
8 * License. See LICENSE.TXT for details.
16 #include "config_clang.h"
19 #include <unordered_set>
20 #include <unordered_map>
23 Look for local variables that can be std::move'd into parameters.
26 (*) Ideally we would use a proper data-flow analysis, to detect that the var is dead after this point,
27 like the one in clang at include/clang/Analysis/CFG.h
28 (*) we could expand the set of approved/interesting types
33 class MoveIt
: public loplugin::FilteringPlugin
<MoveIt
>
36 explicit MoveIt(loplugin::InstantiationData
const& data
)
37 : FilteringPlugin(data
)
41 virtual bool preRun() override
43 std::string
fn(handler
.getMainFileName());
44 loplugin::normalizeDotDotInFilePath(fn
);
45 // false +, needs to check if the moved-from var is outside a loop
46 if (loplugin::hasPathnamePrefix(
47 fn
, SRCDIR
"/drawinglayer/source/primitive3d/sdrdecompositiontools3d.cxx"))
49 if (loplugin::hasPathnamePrefix(
50 fn
, SRCDIR
"/drawinglayer/source/processor2d/vclmetafileprocessor2d.cxx"))
52 if (loplugin::hasPathnamePrefix(fn
, SRCDIR
"/drawinglayer/source/tools/emfphelperdata.cxx"))
54 if (loplugin::hasPathnamePrefix(fn
, SRCDIR
"/sc/source/core/tool/reftokenhelper.cxx"))
56 if (loplugin::hasPathnamePrefix(fn
,
57 SRCDIR
"/svx/source/svdraw/svdotextpathdecomposition.cxx"))
59 if (loplugin::hasPathnamePrefix(fn
, SRCDIR
"/svx/source/svdraw/svdcrtv.cxx"))
61 if (loplugin::hasPathnamePrefix(fn
, SRCDIR
"/svx/source/table/tablehandles.cxx"))
63 if (loplugin::hasPathnamePrefix(fn
, SRCDIR
"/svx/source/xoutdev/xpool.cxx"))
68 virtual void run() override
71 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
72 for (auto const& pair
: m_possibles
)
74 auto const& possible
= pair
.second
;
75 report(DiagnosticsEngine::Warning
, "can std::move this var into this param",
76 possible
.argExpr
->getBeginLoc());
77 report(DiagnosticsEngine::Note
, "passing to this param",
78 possible
.calleeParmVarDecl
->getBeginLoc());
79 report(DiagnosticsEngine::Note
, "local var declared here",
80 possible
.localVarDecl
->getBeginLoc());
81 report(DiagnosticsEngine::Note
, "type declared here",
82 possible
.recordDecl
->getBeginLoc());
86 bool VisitCXXMemberCallExpr(const CXXMemberCallExpr
*);
87 bool VisitCXXConstructExpr(const CXXConstructExpr
*);
88 bool VisitDeclRefExpr(const DeclRefExpr
*);
91 bool isInterestingType(QualType
);
95 const ParmVarDecl
* calleeParmVarDecl
;
96 const VarDecl
* localVarDecl
;
97 const CXXRecordDecl
* recordDecl
;
98 const DeclRefExpr
* dre
;
100 std::unordered_map
<const VarDecl
*, Possible
> m_possibles
;
101 std::unordered_set
<const VarDecl
*> m_rejected
;
104 bool MoveIt::VisitCXXMemberCallExpr(const CXXMemberCallExpr
* topExpr
)
106 if (ignoreLocation(topExpr
))
108 const CXXMethodDecl
* methodDecl
= topExpr
->getMethodDecl();
112 unsigned len
= std::min(topExpr
->getNumArgs(), methodDecl
->getNumParams());
113 for (unsigned i
= 0; i
< len
; ++i
)
115 // check if the parameter is a moveable type
116 const ParmVarDecl
* parmVarDecl
= methodDecl
->getParamDecl(i
);
117 if (!parmVarDecl
->getType()->isRecordType())
119 const CXXRecordDecl
* recordDecl
120 = dyn_cast
<CXXRecordDecl
>(parmVarDecl
->getType()->getAsRecordDecl());
121 if (!recordDecl
|| !recordDecl
->hasMoveConstructor() || recordDecl
->isTriviallyCopyable())
123 if (!isInterestingType(parmVarDecl
->getType()))
126 // check if (a) we're making a copy to pass to the param and (b) we're making a copy of a local var
127 const Expr
* argExpr
= topExpr
->getArg(i
);
130 const CXXConstructExpr
* argSubExpr
= dyn_cast
<CXXConstructExpr
>(argExpr
->IgnoreImplicit());
131 if (!argSubExpr
|| argSubExpr
->getNumArgs() == 0)
133 const DeclRefExpr
* dre
= dyn_cast
<DeclRefExpr
>(argSubExpr
->getArg(0)->IgnoreImplicit());
136 const VarDecl
* localVarDecl
= dyn_cast
<VarDecl
>(dre
->getDecl());
137 if (!localVarDecl
|| localVarDecl
->getType()->isReferenceType()
138 || localVarDecl
->getType()->isPointerType() || !localVarDecl
->hasLocalStorage())
140 // because sometimes the parameter type is some obscured STL thing
141 if (!isInterestingType(localVarDecl
->getType()))
144 if (m_rejected
.count(localVarDecl
))
147 m_possibles
[localVarDecl
] = Possible
{ argExpr
, parmVarDecl
, localVarDecl
, recordDecl
, dre
};
153 bool MoveIt::VisitCXXConstructExpr(const CXXConstructExpr
* topExpr
)
155 if (ignoreLocation(topExpr
))
157 if (isa
<CXXTemporaryObjectExpr
>(topExpr
))
159 const CXXConstructorDecl
* methodDecl
= topExpr
->getConstructor();
163 unsigned len
= std::min(topExpr
->getNumArgs(), methodDecl
->getNumParams());
164 for (unsigned i
= 0; i
< len
; ++i
)
166 // check if the parameter is a moveable type
167 const ParmVarDecl
* parmVarDecl
= methodDecl
->getParamDecl(i
);
168 if (!parmVarDecl
->getType()->isRecordType())
170 const CXXRecordDecl
* recordDecl
171 = dyn_cast
<CXXRecordDecl
>(parmVarDecl
->getType()->getAsRecordDecl());
172 if (!recordDecl
|| !recordDecl
->hasMoveConstructor() || recordDecl
->isTriviallyCopyable())
174 if (!isInterestingType(parmVarDecl
->getType()))
177 // check if (a) we're making a copy to pass to the param and (b) we're making a copy of a local var
178 const Expr
* argExpr
= topExpr
->getArg(i
);
181 const CXXConstructExpr
* argSubExpr
= dyn_cast
<CXXConstructExpr
>(argExpr
->IgnoreImplicit());
182 if (!argSubExpr
|| argSubExpr
->getNumArgs() == 0)
184 const DeclRefExpr
* dre
= dyn_cast
<DeclRefExpr
>(argSubExpr
->getArg(0)->IgnoreImplicit());
187 const VarDecl
* localVarDecl
= dyn_cast
<VarDecl
>(dre
->getDecl());
188 if (!localVarDecl
|| localVarDecl
->getType()->isReferenceType()
189 || localVarDecl
->getType()->isPointerType() || !localVarDecl
->hasLocalStorage())
191 // because sometimes the parameter type is some obscured STL thing
192 if (!isInterestingType(localVarDecl
->getType()))
195 if (m_rejected
.count(localVarDecl
))
198 m_possibles
[localVarDecl
] = Possible
{ argExpr
, parmVarDecl
, localVarDecl
, recordDecl
, dre
};
204 /// If we have pushed a possibility, and then we see that possibility again,
205 /// then we cannot std::move it, because it is being referenced after being moved.
207 bool MoveIt::VisitDeclRefExpr(const DeclRefExpr
* declRefExpr
)
209 if (ignoreLocation(declRefExpr
))
211 const VarDecl
* localVarDecl
= dyn_cast
<VarDecl
>(declRefExpr
->getDecl());
214 auto it
= m_possibles
.find(localVarDecl
);
215 if (it
== m_possibles
.end())
217 // ignoring the DeclRefExpr* for the expression where we found the Possible
218 if (it
->second
.dre
== declRefExpr
)
220 m_possibles
.erase(it
);
221 m_rejected
.insert(localVarDecl
);
225 /// Exclude boring types, so that we don't generate too many low-value conversions.
226 /// e.g. for now I ignore ref-counted types like Sequence and OUString and css::uno::Reference,
227 /// because that generates too many changes
228 bool MoveIt::isInterestingType(QualType qt
)
230 if (qt
->isEnumeralType())
232 if (!qt
->isRecordType())
235 auto tc
= loplugin::TypeCheck(qt
);
238 return !tc
.ClassOrStruct("iterator")
239 && !tc
.ClassOrStruct("const_iterator")
240 && !tc
.Typedef("iterator")
241 && !tc
.Typedef("const_iterator")
242 && !tc
.Class("_Safe_iterator")
243 && !tc
.Typedef("string")
244 && !tc
.ClassOrStruct("shared_ptr").StdNamespace()
245 && !tc
.ClassOrStruct("shared_ptr").Namespace("boost")
246 && !tc
.Class("B2DHomMatrix").Namespace("basegfx").GlobalNamespace()
247 && !tc
.Class("Pipe").Namespace("osl")
248 && !tc
.Class("Any").Namespace("uno")
249 && !tc
.Class("TypeDescription").Namespace("uno")
250 && !tc
.Class("UnoInterfaceReference").Namespace("uno")
251 && !tc
.Class("ByteSequence").Namespace("rtl").GlobalNamespace()
252 && !tc
.Class("OUString").Namespace("rtl").GlobalNamespace()
253 && !tc
.Class("OString").Namespace("rtl").GlobalNamespace()
254 && !tc
.Class("BinaryAny")
255 && !tc
.Class("Reference")
256 && !tc
.Class("SvRef").Namespace("tools").GlobalNamespace()
257 && !tc
.ClassOrStruct("sk_sp") // skia shared pointer
258 && !tc
.ClassOrStruct("VclPtr")
259 && !tc
.Typedef("IterString") // SalInstanceTreeView::IterString
260 && !tc
.Typedef("svtree_render_args")
261 && !tc
.Typedef("render_args") // weld::ComboBox::render_args
266 /// off by default because each warning needs to be hand checked to ensure it is not a false+
267 loplugin::Plugin::Registration
<MoveIt
> moveit("moveit", false);
271 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */