1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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
10 
11 #include <string>
12 #include <iostream>
13 
14 #include "plugin.hxx"
15 #include "compat.hxx"
16 #include "clang/AST/CXXInheritance.h"
17 
18 
19 // Check for calls to virtual methods from destructors. These are dangerous because intention might be to call
20 // a method on a subclass, while in actual fact, it only calls the method on the current or super class.
21 //
22 
23 namespace {
24 
25 class FragileDestructor:
26     public loplugin::FilteringPlugin<FragileDestructor>
27 {
28 public:
FragileDestructor(loplugin::InstantiationData const & data)29     explicit FragileDestructor(loplugin::InstantiationData const & data):
30         FilteringPlugin(data) {}
31 
preRun()32     virtual bool preRun() override
33     {
34         StringRef fn(handler.getMainFileName());
35 
36         // TODO, these all need fixing
37 
38         if (loplugin::isSamePathname(fn, SRCDIR "/comphelper/source/misc/proxyaggregation.cxx"))
39              return false;
40         if (loplugin::isSamePathname(fn, SRCDIR "/svx/source/svdraw/svdpntv.cxx")) // ~SdrPaintView calling ClearPageView
41              return false;
42         if (loplugin::isSamePathname(fn, SRCDIR "/svx/source/svdraw/svdobj.cxx")) // ~SdrObject calling GetLastBoundRect
43              return false;
44         if (loplugin::isSamePathname(fn, SRCDIR "/svx/source/svdraw/svdedxv.cxx")) // ~SdrObjEditView calling SdrEndTextEdit
45              return false;
46         if (loplugin::isSamePathname(fn, SRCDIR "/connectivity/source/drivers/file/FStatement.cxx")) // ~OStatement_Base calling disposing
47              return false;
48         if (loplugin::isSamePathname(fn, SRCDIR "/sd/source/core/CustomAnimationEffect.cxx")) // ~EffectSequenceHelper calling reset
49              return false;
50         if (loplugin::isSamePathname(fn, SRCDIR "/sd/source/ui/view/sdview.cxx")) // ~View calling DeleteWindowFromPaintView
51              return false;
52         if (loplugin::isSamePathname(fn, SRCDIR "/sw/source/core/layout/ssfrm.cxx")) // ~SwFrame calling IsDeleteForbidden
53              return false;
54         if (loplugin::isSamePathname(fn, SRCDIR "/chart2/source/model/template/CandleStickChartType.cxx")) // to ignore <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100797> "[10/11/12 Regression] using declaration causing virtual call with wrongly adjusted this pointer" workaround
55              return false;
56 
57         return true;
58     }
59 
run()60     virtual void run() override
61     {
62         if (preRun())
63             TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
64     }
65 
66     bool PreTraverseCXXDestructorDecl(CXXDestructorDecl*);
67     bool PostTraverseCXXDestructorDecl(CXXDestructorDecl*, bool);
68     bool TraverseCXXDestructorDecl(CXXDestructorDecl*);
69     bool VisitCXXMemberCallExpr(const CXXMemberCallExpr *);
70 
71 private:
72     std::vector<CXXDestructorDecl*>  m_vDestructors;
73 };
74 
PreTraverseCXXDestructorDecl(CXXDestructorDecl * cxxDestructorDecl)75 bool FragileDestructor::PreTraverseCXXDestructorDecl(CXXDestructorDecl* cxxDestructorDecl)
76 {
77     if (ignoreLocation(cxxDestructorDecl))
78         return true;
79     if (!cxxDestructorDecl->isThisDeclarationADefinition())
80         return true;
81     if (cxxDestructorDecl->getParent()->hasAttr<FinalAttr>())
82         return true;
83     m_vDestructors.push_back(cxxDestructorDecl);
84     return true;
85 }
86 
PostTraverseCXXDestructorDecl(CXXDestructorDecl * cxxDestructorDecl,bool)87 bool FragileDestructor::PostTraverseCXXDestructorDecl(CXXDestructorDecl* cxxDestructorDecl, bool)
88 {
89     if (!m_vDestructors.empty() && m_vDestructors.back() == cxxDestructorDecl)
90         m_vDestructors.pop_back();
91     return true;
92 }
93 
TraverseCXXDestructorDecl(CXXDestructorDecl * cxxDestructorDecl)94 bool FragileDestructor::TraverseCXXDestructorDecl(CXXDestructorDecl* cxxDestructorDecl)
95 {
96     PreTraverseCXXDestructorDecl(cxxDestructorDecl);
97     auto ret = FilteringPlugin::TraverseCXXDestructorDecl(cxxDestructorDecl);
98     PostTraverseCXXDestructorDecl(cxxDestructorDecl, ret);
99     return ret;
100 }
101 
VisitCXXMemberCallExpr(const CXXMemberCallExpr * callExpr)102 bool FragileDestructor::VisitCXXMemberCallExpr(const CXXMemberCallExpr* callExpr)
103 {
104     if (m_vDestructors.empty() || ignoreLocation(callExpr))
105         return true;
106     const CXXMethodDecl* methodDecl = callExpr->getMethodDecl();
107     if (!methodDecl->isVirtual() || methodDecl->hasAttr<FinalAttr>())
108         return true;
109     const CXXRecordDecl* parentRecordDecl = methodDecl->getParent();
110     if (parentRecordDecl->hasAttr<FinalAttr>())
111         return true;
112     if (!callExpr->getImplicitObjectArgument()->IgnoreImpCasts()->isImplicitCXXThis())
113         return true;
114 
115     // if we see an explicit call to its own method, that's OK
116     auto s1 = compiler.getSourceManager().getCharacterData(compat::getBeginLoc(callExpr));
117     auto s2 = compiler.getSourceManager().getCharacterData(compat::getEndLoc(callExpr));
118     std::string tok(s1, s2-s1);
119     if (tok.find("::") != std::string::npos)
120         return true;
121 
122     // Very common pattern that we call acquire/dispose in destructors of UNO objects
123     // to make sure they are cleaned up.
124     if (methodDecl->getName() == "acquire" || methodDecl->getName() == "dispose")
125         return true;
126 
127     report(
128         DiagnosticsEngine::Warning,
129         "calling virtual method from destructor, either make the virtual method final, or make this class final",
130         compat::getBeginLoc(callExpr))
131       << callExpr->getSourceRange();
132     report(
133         DiagnosticsEngine::Note,
134         "callee method here",
135         compat::getBeginLoc(methodDecl))
136       << methodDecl->getSourceRange();
137     return true;
138 }
139 
140 
141 loplugin::Plugin::Registration<FragileDestructor> fragiledestructor("fragiledestructor");
142 
143 }
144 
145 #endif // LO_CLANG_SHARED_PLUGINS
146 
147 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
148