Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / compilerplugins / clang / fragiledestructor.cxx
blobf8a0ea50660c9a2cf3ff47ed610d572a433bb407
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 <string>
11 #include <iostream>
13 #include "plugin.hxx"
14 #include "compat.hxx"
15 #include "clang/AST/CXXInheritance.h"
18 // Check for calls to virtual methods from destructors. These are dangerous because intention might be to call
19 // a method on a subclass, while in actual fact, it only calls the method on the current or super class.
22 namespace {
24 class FragileDestructor:
25 public RecursiveASTVisitor<FragileDestructor>, public loplugin::Plugin
27 public:
28 explicit FragileDestructor(loplugin::InstantiationData const & data):
29 Plugin(data) {}
31 virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
33 bool TraverseCXXDestructorDecl(CXXDestructorDecl *);
35 bool VisitCXXMemberCallExpr(const CXXMemberCallExpr *);
37 private:
38 bool mbChecking = false;
41 bool FragileDestructor::TraverseCXXDestructorDecl(CXXDestructorDecl* pCXXDestructorDecl)
43 if (ignoreLocation(pCXXDestructorDecl)) {
44 return RecursiveASTVisitor::TraverseCXXDestructorDecl(pCXXDestructorDecl);
46 if (!pCXXDestructorDecl->isThisDeclarationADefinition()) {
47 return RecursiveASTVisitor::TraverseCXXDestructorDecl(pCXXDestructorDecl);
49 // ignore this for now, too tricky for me to work out
50 StringRef aFileName = compiler.getSourceManager().getFilename(
51 compiler.getSourceManager().getSpellingLoc(pCXXDestructorDecl->getLocStart()));
52 if (loplugin::hasPathnamePrefix(aFileName, SRCDIR "/include/comphelper/")
53 || loplugin::hasPathnamePrefix(aFileName, SRCDIR "/include/cppuhelper/")
54 || loplugin::hasPathnamePrefix(aFileName, SRCDIR "/cppuhelper/")
55 || loplugin::hasPathnamePrefix(aFileName, SRCDIR "/comphelper/")
56 // don't know how to detect this in clang - it is making an explicit call to its own method, so presumably OK
57 || loplugin::isSamePathname(aFileName, SRCDIR "/basic/source/sbx/sbxvalue.cxx")
59 return RecursiveASTVisitor::TraverseCXXDestructorDecl(pCXXDestructorDecl);
60 mbChecking = true;
61 bool ret = RecursiveASTVisitor::TraverseCXXDestructorDecl(pCXXDestructorDecl);
62 mbChecking = false;
63 return ret;
66 bool FragileDestructor::VisitCXXMemberCallExpr(const CXXMemberCallExpr* callExpr)
68 if (!mbChecking || ignoreLocation(callExpr)) {
69 return true;
71 const CXXMethodDecl* methodDecl = callExpr->getMethodDecl();
72 if (!methodDecl->isVirtual() || methodDecl->hasAttr<FinalAttr>()) {
73 return true;
75 const CXXRecordDecl* parentRecordDecl = methodDecl->getParent();
76 if (parentRecordDecl->hasAttr<FinalAttr>()) {
77 return true;
79 if (!callExpr->getImplicitObjectArgument()->IgnoreImpCasts()->isImplicitCXXThis()) {
80 return true;
82 // if we see an explicit call to its own method, that's OK
83 auto s1 = compiler.getSourceManager().getCharacterData(callExpr->getLocStart());
84 auto s2 = compiler.getSourceManager().getCharacterData(callExpr->getLocEnd());
85 std::string tok(s1, s2-s1);
86 if (tok.find("::") != std::string::npos) {
87 return true;
89 // e.g. osl/thread.hxx and cppuhelper/compbase.hxx
90 StringRef aFileName = compiler.getSourceManager().getFilename(compiler.getSourceManager().getSpellingLoc(methodDecl->getLocStart()));
91 if (loplugin::hasPathnamePrefix(aFileName, SRCDIR "/include/osl/")
92 || loplugin::hasPathnamePrefix(aFileName, SRCDIR "/include/comphelper/")
93 || loplugin::hasPathnamePrefix(aFileName, SRCDIR "/include/cppuhelper/"))
94 return true;
95 report(
96 DiagnosticsEngine::Warning,
97 "calling virtual method from destructor, either make the virtual method SAL_FINAL, or make this class SAL_FINAL",
98 callExpr->getLocStart())
99 << callExpr->getSourceRange();
100 report(
101 DiagnosticsEngine::Note,
102 "callee method here",
103 methodDecl->getLocStart())
104 << methodDecl->getSourceRange();
105 return true;
109 loplugin::Plugin::Registration< FragileDestructor > X("fragiledestructor", false);
113 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */