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/.
9 #ifndef LO_CLANG_SHARED_PLUGINS
17 Find code where we are return a const value type from a function.
19 Either we should return by non-const value, or by const ref.
23 const OUString get13() { return mv1; }
26 Specifically, this code pattern will prevent copy/move optimisations.
31 class ReturnConstVal
: public loplugin::FilteringPlugin
<ReturnConstVal
>
34 explicit ReturnConstVal(loplugin::InstantiationData
const& data
)
35 : FilteringPlugin(data
)
39 virtual void run() override
{ TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl()); }
41 bool VisitFunctionDecl(const FunctionDecl
* decl
);
44 bool ReturnConstVal::VisitFunctionDecl(const FunctionDecl
* functionDecl
)
46 if (ignoreLocation(functionDecl
))
48 if (!functionDecl
->hasBody())
50 // ignore stuff that forms part of the stable URE interface
51 if (isInUnoIncludeFile(functionDecl
))
53 QualType t1
{ functionDecl
->getReturnType() };
54 if (!t1
.isConstQualified())
56 if (t1
->isReferenceType())
58 report(DiagnosticsEngine::Warning
, "either return non-const, or by const ref",
59 functionDecl
->getSourceRange().getBegin())
60 << functionDecl
->getSourceRange();
62 // display the location of the class member declaration so I don't have to search for it by hand
63 auto canonicalDecl
= functionDecl
->getCanonicalDecl();
64 if (canonicalDecl
!= functionDecl
)
66 report(DiagnosticsEngine::Note
, "either return non-const, or by const ref",
67 canonicalDecl
->getSourceRange().getBegin())
68 << canonicalDecl
->getSourceRange();
74 loplugin::Plugin::Registration
<ReturnConstVal
> returnconstval("returnconstval");
77 #endif // LO_CLANG_SHARED_PLUGINS
79 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */