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/.
16 #include "config_clang.h"
20 Find calls to "delete x" where x is a field on an object.
21 Should rather be using std::unique_ptr
27 public loplugin::FilteringPlugin
<AutoMem
>
30 explicit AutoMem(loplugin::InstantiationData
const & data
): FilteringPlugin(data
), mbInsideDestructor(false) {}
32 virtual void run() override
34 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
37 bool TraverseCXXDestructorDecl(CXXDestructorDecl
* );
38 bool VisitCXXDeleteExpr(const CXXDeleteExpr
* );
40 bool mbInsideDestructor
;
43 bool AutoMem::TraverseCXXDestructorDecl(CXXDestructorDecl
* expr
)
45 mbInsideDestructor
= true;
46 bool ret
= RecursiveASTVisitor::TraverseCXXDestructorDecl(expr
);
47 mbInsideDestructor
= false;
51 bool AutoMem::VisitCXXDeleteExpr(const CXXDeleteExpr
* expr
)
53 if (ignoreLocation( expr
))
55 StringRef aFileName
= getFilenameOfLocation(compiler
.getSourceManager().getSpellingLoc(expr
->getBeginLoc()));
56 if (loplugin::hasPathnamePrefix(aFileName
, SRCDIR
"/include/salhelper/")
57 || loplugin::hasPathnamePrefix(aFileName
, SRCDIR
"/include/osl/")
58 || loplugin::hasPathnamePrefix(aFileName
, SRCDIR
"/salhelper/")
59 || loplugin::hasPathnamePrefix(aFileName
, SRCDIR
"/store/")
60 || loplugin::hasPathnamePrefix(aFileName
, SRCDIR
"/sal/"))
63 if (mbInsideDestructor
)
66 const ImplicitCastExpr
* pCastExpr
= dyn_cast
<ImplicitCastExpr
>(expr
->getArgument());
69 const MemberExpr
* pMemberExpr
= dyn_cast
<MemberExpr
>(pCastExpr
->getSubExpr());
73 const FieldDecl
* pFieldDecl
= dyn_cast
<FieldDecl
>(pMemberExpr
->getMemberDecl());
76 TagDecl
const * td
= dyn_cast
<TagDecl
>(pFieldDecl
->getDeclContext());
81 DiagnosticsEngine::Warning
,
82 "calling delete on object field, rather use std::unique_ptr or std::scoped_ptr",
84 << expr
->getSourceRange();
88 loplugin::Plugin::Registration
< AutoMem
> X("automem", false);
92 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */