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.
11 #ifndef LO_CLANG_SHARED_PLUGINS
18 #include <unordered_set>
23 Look for places where we are using cow_wrapper, but we are calling a const method on the impl object
24 with a non-const pointer, which means we will unnecessarily trigger a copy.
29 class Cow_Wrapper
: public loplugin::FilteringPlugin
<Cow_Wrapper
>
32 explicit Cow_Wrapper(loplugin::InstantiationData
const& data
)
33 : FilteringPlugin(data
)
37 bool shouldVisitTemplateInstantiations() const { return true; }
39 virtual bool preRun() override
{ return true; }
41 virtual void run() override
44 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
47 bool VisitCXXMemberCallExpr(const CXXMemberCallExpr
*);
50 bool Cow_Wrapper::VisitCXXMemberCallExpr(const CXXMemberCallExpr
* memberCallExpr
)
52 if (ignoreLocation(memberCallExpr
))
54 auto methodDecl
= memberCallExpr
->getMethodDecl();
55 if (!methodDecl
|| !methodDecl
->isConst())
58 auto expr
= memberCallExpr
->getImplicitObjectArgument()->IgnoreImplicit()->IgnoreParens();
59 auto operatorCallExpr
= dyn_cast
<CXXOperatorCallExpr
>(expr
);
61 if (operatorCallExpr
&& operatorCallExpr
->getOperator() == OO_Arrow
)
63 auto arrowMethodDecl
= dyn_cast_or_null
<CXXMethodDecl
>(operatorCallExpr
->getDirectCallee());
66 if (arrowMethodDecl
->isConst())
68 auto dc
= loplugin::DeclCheck(arrowMethodDecl
->getParent())
75 else if (operatorCallExpr
)
77 auto methodDecl2
= dyn_cast_or_null
<CXXMethodDecl
>(operatorCallExpr
->getDirectCallee());
80 auto dc
= loplugin::DeclCheck(methodDecl2
->getParent())
87 else if (auto callExpr
= dyn_cast
<CallExpr
>(expr
))
89 if (!isa
<ImplicitCastExpr
>(callExpr
->getCallee())) // std::as_const shows up as this
91 if (callExpr
->getNumArgs() < 1)
93 auto arg0
= dyn_cast
<CXXOperatorCallExpr
>(callExpr
->getArg(0));
96 auto starMethodDecl
= dyn_cast_or_null
<CXXMethodDecl
>(arg0
->getDirectCallee());
99 auto dc
= loplugin::DeclCheck(starMethodDecl
->getParent())
100 .Class("cow_wrapper")
109 report(DiagnosticsEngine::Warning
,
110 "calling const method on o3tl::cow_wrapper impl class via non-const pointer, rather use "
111 "std::as_const to prevent triggering an unnecessary copy",
112 memberCallExpr
->getBeginLoc())
113 << memberCallExpr
->getSourceRange();
117 loplugin::Plugin::Registration
<Cow_Wrapper
> cow_wrapper("cow_wrapper", true);
121 #endif // LO_CLANG_SHARED_PLUGINS
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */