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/.
17 // Find places where various things are passed by value.
18 // It's not very efficient, because we generally end up copying it twice - once into the parameter and
19 // again into the destination.
20 // They should rather be passed by reference.
22 // Generally recommending lambda capture by-ref rather than by-copy is even more
23 // problematic than with function parameters, as a lambda instance can easily
24 // outlive a referenced variable. So once lambdas start to get used in more
25 // sophisticated ways than passing them into standard algorithms, this plugin's
26 // advice, at least for explicit captures, will need to be revisited.
31 public loplugin::FilteringPlugin
<PassStuffByRef
>
34 explicit PassStuffByRef(loplugin::InstantiationData
const & data
): FilteringPlugin(data
), mbInsideFunctionDecl(false), mbFoundReturnValueDisqualifier(false) {}
36 virtual void run() override
{ TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl()); }
38 // When warning about function params of primitive type that could be passed
39 // by value instead of by reference, make sure not to warn if the parameter
40 // is ever bound to a reference; on the one hand, this needs scaffolding in
41 // all Traverse*Decl functions (indirectly) derived from FunctionDecl; and
42 // on the other hand, use a hack of ignoring just the DeclRefExprs nested in
43 // LValueToRValue ImplicitCastExprs when determining whether a param is
44 // bound to a reference:
45 bool TraverseFunctionDecl(FunctionDecl
* decl
);
46 bool TraverseCXXMethodDecl(CXXMethodDecl
* decl
);
47 bool TraverseCXXConstructorDecl(CXXConstructorDecl
* decl
);
48 bool TraverseCXXDestructorDecl(CXXDestructorDecl
* decl
);
49 bool TraverseCXXConversionDecl(CXXConversionDecl
* decl
);
50 bool VisitFunctionDecl(const FunctionDecl
* decl
);
51 bool TraverseImplicitCastExpr(ImplicitCastExpr
* expr
);
52 bool VisitDeclRefExpr(const DeclRefExpr
* expr
);
54 bool VisitReturnStmt(const ReturnStmt
* );
55 bool VisitVarDecl(const VarDecl
* );
58 template<typename T
> bool traverseAnyFunctionDecl(
59 T
* decl
, bool (RecursiveASTVisitor::* fn
)(T
*));
60 void checkParams(const FunctionDecl
* functionDecl
);
61 void checkReturnValue(const FunctionDecl
* functionDecl
, const CXXMethodDecl
* methodDecl
);
62 bool isPrimitiveConstRef(QualType type
);
63 bool isReturnExprDisqualified(const Expr
* expr
);
65 bool mbInsideFunctionDecl
;
66 bool mbFoundReturnValueDisqualifier
;
69 std::set
<ParmVarDecl
const *> parms
;
72 std::vector
<FDecl
> functionDecls_
;
75 bool PassStuffByRef::TraverseFunctionDecl(FunctionDecl
* decl
) {
76 return traverseAnyFunctionDecl(
77 decl
, &RecursiveASTVisitor::TraverseFunctionDecl
);
80 bool PassStuffByRef::TraverseCXXMethodDecl(CXXMethodDecl
* decl
) {
81 return traverseAnyFunctionDecl(
82 decl
, &RecursiveASTVisitor::TraverseCXXMethodDecl
);
85 bool PassStuffByRef::TraverseCXXConstructorDecl(CXXConstructorDecl
* decl
) {
86 return traverseAnyFunctionDecl(
87 decl
, &RecursiveASTVisitor::TraverseCXXConstructorDecl
);
90 bool PassStuffByRef::TraverseCXXDestructorDecl(CXXDestructorDecl
* decl
) {
91 return traverseAnyFunctionDecl(
92 decl
, &RecursiveASTVisitor::TraverseCXXDestructorDecl
);
95 bool PassStuffByRef::TraverseCXXConversionDecl(CXXConversionDecl
* decl
) {
96 return traverseAnyFunctionDecl(
97 decl
, &RecursiveASTVisitor::TraverseCXXConversionDecl
);
100 template<typename T
> bool PassStuffByRef::traverseAnyFunctionDecl(
101 T
* decl
, bool (RecursiveASTVisitor::* fn
)(T
*))
103 if (ignoreLocation(decl
)) {
106 if (decl
->doesThisDeclarationHaveABody()) {
107 functionDecls_
.emplace_back();
109 bool ret
= (this->*fn
)(decl
);
110 if (decl
->doesThisDeclarationHaveABody()) {
111 assert(!functionDecls_
.empty());
112 if (functionDecls_
.back().check
) {
113 for (auto d
: functionDecls_
.back().parms
) {
115 DiagnosticsEngine::Warning
,
116 ("passing primitive type param %0 by const &, rather pass"
119 << d
->getType() << d
->getSourceRange();
120 auto can
= decl
->getCanonicalDecl();
121 if (can
->getLocation() != decl
->getLocation()) {
123 DiagnosticsEngine::Note
, "function is declared here:",
125 << can
->getSourceRange();
129 functionDecls_
.pop_back();
134 bool PassStuffByRef::VisitFunctionDecl(const FunctionDecl
* functionDecl
) {
135 if (ignoreLocation(functionDecl
)) {
138 if (functionDecl
->isDeleted()
139 || functionDecl
->isFunctionTemplateSpecialization())
143 // only consider base declarations, not overridden ones, or we warn on methods that
144 // are overriding stuff from external libraries
145 const CXXMethodDecl
* methodDecl
= dyn_cast
<CXXMethodDecl
>(functionDecl
);
146 if (methodDecl
&& methodDecl
->size_overridden_methods() > 0) {
150 checkParams(functionDecl
);
151 checkReturnValue(functionDecl
, methodDecl
);
155 bool PassStuffByRef::TraverseImplicitCastExpr(ImplicitCastExpr
* expr
) {
156 if (ignoreLocation(expr
)) {
160 (expr
->getCastKind() == CK_LValueToRValue
161 && (dyn_cast
<DeclRefExpr
>(expr
->getSubExpr()->IgnoreParenImpCasts())
163 || RecursiveASTVisitor::TraverseImplicitCastExpr(expr
);
166 bool PassStuffByRef::VisitDeclRefExpr(const DeclRefExpr
* expr
) {
167 if (ignoreLocation(expr
)) {
170 auto d
= dyn_cast
<ParmVarDecl
>(expr
->getDecl());
174 for (auto & fd
: functionDecls_
) {
175 if (fd
.parms
.erase(d
) == 1) {
182 void PassStuffByRef::checkParams(const FunctionDecl
* functionDecl
) {
183 // Only warn on the definition of the function:
184 if (!functionDecl
->doesThisDeclarationHaveABody()) {
187 // ignore stuff that forms part of the stable URE interface
188 if (isInUnoIncludeFile(functionDecl
)) {
191 // these functions are passed as parameters to another function
192 if (loplugin::DeclCheck(functionDecl
).MemberFunction()
193 .Class("ShapeAttributeLayer").Namespace("internal")
194 .Namespace("slideshow").GlobalNamespace())
198 unsigned n
= functionDecl
->getNumParams();
199 assert(!functionDecls_
.empty());
200 functionDecls_
.back().check
= true;
201 for (unsigned i
= 0; i
!= n
; ++i
) {
202 const ParmVarDecl
* pvDecl
= functionDecl
->getParamDecl(i
);
203 auto const t
= pvDecl
->getType();
204 if (isPrimitiveConstRef(t
)) {
205 functionDecls_
.back().parms
.insert(pvDecl
);
210 static bool startswith(const std::string
& rStr
, const char* pSubStr
) {
211 return rStr
.compare(0, strlen(pSubStr
), pSubStr
) == 0;
214 void PassStuffByRef::checkReturnValue(const FunctionDecl
* functionDecl
, const CXXMethodDecl
* methodDecl
) {
215 if (methodDecl
&& (methodDecl
->isVirtual() || methodDecl
->hasAttr
<OverrideAttr
>())) {
218 if( !functionDecl
->doesThisDeclarationHaveABody()
219 || functionDecl
->isLateTemplateParsed())
224 const QualType type
= functionDecl
->getReturnType().getDesugaredType(compiler
.getASTContext());
225 if (type
->isReferenceType() || type
->isIntegralOrEnumerationType() || type
->isPointerType()
226 || type
->isTemplateTypeParmType() || type
->isDependentType() || type
->isBuiltinType()
227 || type
->isScalarType())
232 // not sure if it's possible to modify these
233 if (isa
<CXXConversionDecl
>(functionDecl
))
236 // ignore stuff that forms part of the stable URE interface
237 if (isInUnoIncludeFile(functionDecl
)) {
241 loplugin::DeclCheck
dc(functionDecl
);
242 // function is passed as parameter to another function
243 if (dc
.Function("ImplColMonoFnc").Class("GDIMetaFile").GlobalNamespace()
244 || (dc
.Function("darkColor").Class("SvxBorderLine").Namespace("editeng")
246 || (dc
.MemberFunction().Class("Binding").Namespace("xforms")
248 || (dc
.MemberFunction().Class("Model").Namespace("xforms")
250 || (dc
.MemberFunction().Class("Submission").Namespace("xforms")
252 || (dc
.Function("TopLeft").Class("SwRect").GlobalNamespace())
253 || (dc
.Function("ConvDicList_CreateInstance").GlobalNamespace())
254 || (dc
.Function("Create").Class("OUnoAutoPilot").Namespace("dbp").GlobalNamespace())
255 || (dc
.Function("Size_").Class("SwRect").GlobalNamespace()))
259 // not sure how to exclude this yet, returns copy of one of it's params
260 if (dc
.Function("sameDistColor").GlobalNamespace()
261 || dc
.Function("sameColor").GlobalNamespace()
262 || (dc
.Operator(OO_Call
).Struct("StringIdentity").AnonymousNamespace()
263 .Namespace("pcr").GlobalNamespace())
264 || (dc
.Function("accumulate").Namespace("internal")
265 .Namespace("slideshow").GlobalNamespace())
266 || (dc
.Function("lerp").Namespace("internal").Namespace("slideshow")
269 // depends on a define
270 if (dc
.Function("GetSharedFileURL").Class("SfxObjectShell")
271 .GlobalNamespace()) {
274 // hides a constructor
275 if (dc
.Function("createNonOwningCopy").Class("SortedAutoCompleteStrings").Namespace("editeng")
276 .GlobalNamespace()) {
280 if (dc
.Function("convertItems").Class("ValueParser").Namespace("configmgr").GlobalNamespace()
281 || dc
.Function("parseListValue").AnonymousNamespace().Namespace("configmgr").GlobalNamespace()
282 || dc
.Function("parseSingleValue").AnonymousNamespace().Namespace("configmgr").GlobalNamespace()
283 || dc
.Function("Create").Class("HandlerComponentBase").Namespace("pcr").GlobalNamespace()
284 || dc
.Function("toAny").Struct("Convert").Namespace("detail").Namespace("comphelper").GlobalNamespace()
285 || dc
.Function("makeAny").Namespace("utl").GlobalNamespace()) {
288 if (startswith(type
.getAsString(), "struct o3tl::strong_int")) {
291 auto tc
= loplugin::TypeCheck(functionDecl
->getReturnType());
292 // these functions are passed by function-pointer
293 if (functionDecl
->getIdentifier() && functionDecl
->getName() == "GetRanges"
294 && tc
.Struct("WhichRangesContainer").GlobalNamespace())
296 // extremely simple class, might as well pass by value
297 if (tc
.Class("Color")) {
300 // extremely simple class, might as well pass by value
301 if (tc
.Struct("TranslateId")) {
304 if (tc
.Class("span").Namespace("o3tl")) {
308 // functionDecl->dump();
310 mbInsideFunctionDecl
= true;
311 mbFoundReturnValueDisqualifier
= false;
312 TraverseStmt(functionDecl
->getBody());
313 mbInsideFunctionDecl
= false;
315 if (mbFoundReturnValueDisqualifier
)
318 report( DiagnosticsEngine::Warning
,
319 "rather return %0 by const& than by value, to avoid unnecessary copying",
320 functionDecl
->getSourceRange().getBegin())
321 << type
.getAsString() << functionDecl
->getSourceRange();
323 // display the location of the class member declaration so I don't have to search for it by hand
324 auto canonicalDecl
= functionDecl
->getCanonicalDecl();
325 if (functionDecl
!= canonicalDecl
)
327 report( DiagnosticsEngine::Note
,
329 canonicalDecl
->getSourceRange().getBegin())
330 << canonicalDecl
->getSourceRange();
333 //functionDecl->dump();
336 bool PassStuffByRef::VisitReturnStmt(const ReturnStmt
* returnStmt
)
338 if (!mbInsideFunctionDecl
)
340 const Expr
* expr
= dyn_cast
<Expr
>(*returnStmt
->child_begin())->IgnoreParenImpCasts();
342 if (isReturnExprDisqualified(expr
))
343 mbFoundReturnValueDisqualifier
= true;
349 * Does a return expression disqualify this method from doing return by const & ?
351 bool PassStuffByRef::isReturnExprDisqualified(const Expr
* expr
)
355 expr
= expr
->IgnoreParens();
357 if (auto implicitCast
= dyn_cast
<ImplicitCastExpr
>(expr
)) {
358 expr
= implicitCast
->getSubExpr();
361 if (auto exprWithCleanups
= dyn_cast
<ExprWithCleanups
>(expr
)) {
362 expr
= exprWithCleanups
->getSubExpr();
365 if (auto constructExpr
= dyn_cast
<CXXConstructExpr
>(expr
))
367 if (constructExpr
->getNumArgs()==1
368 && constructExpr
->getConstructor()->isCopyOrMoveConstructor())
370 expr
= constructExpr
->getArg(0);
376 if (isa
<CXXFunctionalCastExpr
>(expr
)) {
379 if (isa
<MaterializeTemporaryExpr
>(expr
)) {
382 if (isa
<CXXBindTemporaryExpr
>(expr
)) {
385 if (isa
<InitListExpr
>(expr
)) {
388 expr
= expr
->IgnoreParenCasts();
389 if (auto childExpr
= dyn_cast
<ArraySubscriptExpr
>(expr
)) {
390 expr
= childExpr
->getLHS();
393 if (auto memberExpr
= dyn_cast
<MemberExpr
>(expr
)) {
394 expr
= memberExpr
->getBase();
397 if (auto declRef
= dyn_cast
<DeclRefExpr
>(expr
)) {
398 // a param might be a temporary
399 if (isa
<ParmVarDecl
>(declRef
->getDecl()))
401 const VarDecl
* varDecl
= dyn_cast
<VarDecl
>(declRef
->getDecl());
403 if (varDecl
->getStorageDuration() == SD_Thread
404 || varDecl
->getStorageDuration() == SD_Static
) {
410 if (auto condOper
= dyn_cast
<ConditionalOperator
>(expr
)) {
411 return isReturnExprDisqualified(condOper
->getTrueExpr())
412 || isReturnExprDisqualified(condOper
->getFalseExpr());
414 if (auto unaryOp
= dyn_cast
<UnaryOperator
>(expr
)) {
415 expr
= unaryOp
->getSubExpr();
418 if (auto operatorCallExpr
= dyn_cast
<CXXOperatorCallExpr
>(expr
)) {
419 // TODO could improve this, but sometimes it means we're returning a copy of a temporary.
420 // Same logic as CXXOperatorCallExpr::isAssignmentOp(), which our supported clang
422 auto Opc
= operatorCallExpr
->getOperator();
423 if (Opc
== OO_Equal
|| Opc
== OO_StarEqual
||
424 Opc
== OO_SlashEqual
|| Opc
== OO_PercentEqual
||
425 Opc
== OO_PlusEqual
|| Opc
== OO_MinusEqual
||
426 Opc
== OO_LessLessEqual
|| Opc
== OO_GreaterGreaterEqual
||
427 Opc
== OO_AmpEqual
|| Opc
== OO_CaretEqual
||
430 if (Opc
== OO_Subscript
)
432 if (isReturnExprDisqualified(operatorCallExpr
->getArg(0)))
434 // otherwise fall through to the checking below
437 if (isReturnExprDisqualified(operatorCallExpr
->getArg(0)))
440 if (auto memberCallExpr
= dyn_cast
<CXXMemberCallExpr
>(expr
)) {
441 if (isReturnExprDisqualified(memberCallExpr
->getImplicitObjectArgument()))
443 // otherwise fall through to the checking in CallExpr
445 if (auto callExpr
= dyn_cast
<CallExpr
>(expr
)) {
446 FunctionDecl
const * calleeFunctionDecl
= callExpr
->getDirectCallee();
447 if (!calleeFunctionDecl
)
449 // TODO anything takes a non-integral param is suspect because it might return the param by ref.
450 // we could tighten this to only reject functions that have a param of the same type
451 // as the return type. Or we could check for such functions and disallow them.
452 // Or we could force such functions to be annotated somehow.
453 for (unsigned i
= 0; i
!= calleeFunctionDecl
->getNumParams(); ++i
) {
454 if (!calleeFunctionDecl
->getParamDecl(i
)->getType()->isIntegralOrEnumerationType())
457 auto tc
= loplugin::TypeCheck(calleeFunctionDecl
->getReturnType());
458 if (!tc
.LvalueReference() && !tc
.Pointer())
465 bool PassStuffByRef::VisitVarDecl(const VarDecl
* varDecl
)
467 if (!mbInsideFunctionDecl
|| mbFoundReturnValueDisqualifier
)
469 // things guarded by locking are probably best left alone
470 loplugin::TypeCheck
dc(varDecl
->getType());
471 if (dc
.Class("Guard").Namespace("osl").GlobalNamespace() ||
472 dc
.Class("ClearableGuard").Namespace("osl").GlobalNamespace() ||
473 dc
.Class("ResettableGuard").Namespace("osl").GlobalNamespace() ||
474 dc
.Class("SolarMutexGuard").GlobalNamespace() ||
475 dc
.Class("SfxModelGuard").GlobalNamespace() ||
476 dc
.Class("ReadWriteGuard").Namespace("utl").GlobalNamespace() ||
477 dc
.Class("unique_lock").StdNamespace() ||
478 dc
.Class("lock_guard").StdNamespace() ||
479 dc
.Class("scoped_lock").StdNamespace())
480 mbFoundReturnValueDisqualifier
= true;
484 bool PassStuffByRef::isPrimitiveConstRef(QualType type
) {
485 if (type
->isIncompleteType()) {
488 const clang::ReferenceType
* referenceType
= type
->getAs
<ReferenceType
>();
489 if (referenceType
== nullptr) {
492 QualType pointeeQT
= referenceType
->getPointeeType();
493 if (!pointeeQT
.isConstQualified()) {
496 if (!pointeeQT
->isFundamentalType()) {
499 // ignore double for now, some of our code seems to believe it is cheaper to pass by ref
500 const BuiltinType
* builtinType
= pointeeQT
->getAs
<BuiltinType
>();
501 return builtinType
->getKind() != BuiltinType::Kind::Double
;
505 loplugin::Plugin::Registration
< PassStuffByRef
> X("passstuffbyref", false);
509 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */