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/.
10 // Find places where a std::unique_ptr is release()'ed and returned as a raw
11 // pointer. Some occurrences of that might better be rewritten to return the
12 // unique_ptr is returned directly. (But other occurrences might be fine the
13 // way they are, hence place this plugin into store/).
21 public loplugin::FilteringPlugin
<ReturnUnique
>
24 explicit ReturnUnique(InstantiationData
const & data
): FilteringPlugin(data
) {}
27 if (compiler
.getLangOpts().CPlusPlus
) {
28 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
32 bool VisitReturnStmt(ReturnStmt
const * stmt
);
35 bool ReturnUnique::VisitReturnStmt(ReturnStmt
const * stmt
) {
36 if (ignoreLocation(stmt
)) {
39 auto const e1
= stmt
->getRetValue();
43 auto const e2
= dyn_cast
<CXXMemberCallExpr
>(e1
->IgnoreParenImpCasts());
47 auto const d1
= e2
->getMethodDecl();
48 if (d1
== nullptr) { // call via ptr to member
51 auto const d2
= d1
->getParent();
52 assert(d2
!= nullptr);
53 assert(d2
->getParent() != nullptr);
54 auto const d3
= dyn_cast
<NamespaceDecl
>(d2
->getParent());
56 /* || dyn_cast<TranslationUnitDecl>(d3->getParent()) == nullptr */)
60 auto const id3
= d3
->getIdentifier();
61 if (id3
== nullptr /* || id3->getName() != "std" */) {
64 auto const id2
= d2
->getIdentifier();
65 if (id2
== nullptr || id2
->getName() != "unique_ptr") {
68 auto const id1
= d1
->getIdentifier();
69 if (id1
== nullptr || id1
->getName() != "release") {
73 DiagnosticsEngine::Warning
, "return std::unique_ptr::release",
75 << stmt
->getSourceRange();
79 loplugin::Plugin::Registration
<ReturnUnique
> X("returnunique");
83 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */