bump product version to 6.4.0.3
[LibreOffice.git] / compilerplugins / clang / returnconstval.cxx
blob4f8b0ac34348508ecc3d29bd1be3447b1493fe9d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
9 #ifndef LO_CLANG_SHARED_PLUGINS
11 #include <string>
12 #include <set>
14 #include "plugin.hxx"
16 /**
17 Find code where we are return a const value type from a function.
18 Which makes no sense.
19 Either we should return by non-const value, or by const ref.
20 e.g.
21 struct S2 {
22 OUString mv1;
23 const OUString get13() { return mv1; }
26 Specifically, this code pattern will prevent copy/move optimisations.
29 namespace
31 class ReturnConstVal : public loplugin::FilteringPlugin<ReturnConstVal>
33 public:
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))
47 return true;
48 if (!functionDecl->hasBody())
49 return true;
50 // ignore stuff that forms part of the stable URE interface
51 if (isInUnoIncludeFile(functionDecl))
52 return true;
53 QualType t1{ functionDecl->getReturnType() };
54 if (!t1.isConstQualified())
55 return true;
56 if (t1->isReferenceType())
57 return true;
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();
71 return true;
74 loplugin::Plugin::Registration<ReturnConstVal> returnconstval("returnconstval");
77 #endif // LO_CLANG_SHARED_PLUGINS
79 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */