Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / automem.cxx
blob92478b9fbb9548882fc811e54927c39b6fa5d39e
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 */
10 #include <memory>
11 #include <cassert>
12 #include <string>
13 #include <iostream>
14 #include <fstream>
15 #include <set>
16 #include "config_clang.h"
17 #include "plugin.hxx"
19 /**
20 Find calls to "delete x" where x is a field on an object.
21 Should rather be using std::unique_ptr
24 namespace {
26 class AutoMem:
27 public loplugin::FilteringPlugin<AutoMem>
29 public:
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* );
39 private:
40 bool mbInsideDestructor;
43 bool AutoMem::TraverseCXXDestructorDecl(CXXDestructorDecl* expr)
45 mbInsideDestructor = true;
46 bool ret = RecursiveASTVisitor::TraverseCXXDestructorDecl(expr);
47 mbInsideDestructor = false;
48 return ret;
51 bool AutoMem::VisitCXXDeleteExpr(const CXXDeleteExpr* expr)
53 if (ignoreLocation( expr ))
54 return true;
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/"))
61 return true;
63 if (mbInsideDestructor)
64 return true;
66 const ImplicitCastExpr* pCastExpr = dyn_cast<ImplicitCastExpr>(expr->getArgument());
67 if (!pCastExpr)
68 return true;
69 const MemberExpr* pMemberExpr = dyn_cast<MemberExpr>(pCastExpr->getSubExpr());
70 if (!pMemberExpr)
71 return true;
72 // ignore union games
73 const FieldDecl* pFieldDecl = dyn_cast<FieldDecl>(pMemberExpr->getMemberDecl());
74 if (!pFieldDecl)
75 return true;
76 TagDecl const * td = dyn_cast<TagDecl>(pFieldDecl->getDeclContext());
77 if (td->isUnion())
78 return true;
80 report(
81 DiagnosticsEngine::Warning,
82 "calling delete on object field, rather use std::unique_ptr or std::scoped_ptr",
83 expr->getBeginLoc())
84 << expr->getSourceRange();
85 return true;
88 loplugin::Plugin::Registration< AutoMem > X("automem", false);
92 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */