summaryrefslogtreecommitdiffstats
path: root/compilerplugins/clang/fragiledestructor.cxx
blob: f8ec76a561f8480df6a039f6dc9de0f23cf6ae3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
#ifndef LO_CLANG_SHARED_PLUGINS

#include <string>
#include <iostream>

#include "plugin.hxx"
#include "config_clang.h"
#include "clang/AST/CXXInheritance.h"


// Check for calls to virtual methods from destructors. These are dangerous because intention might be to call
// a method on a subclass, while in actual fact, it only calls the method on the current or super class.
//

namespace {

class FragileDestructor:
    public loplugin::FilteringPlugin<FragileDestructor>
{
public:
    explicit FragileDestructor(loplugin::InstantiationData const & data):
        FilteringPlugin(data) {}

    virtual bool preRun() override
    {
        StringRef fn(handler.getMainFileName());

        // TODO, these all need fixing

        if (loplugin::isSamePathname(fn, SRCDIR "/comphelper/source/misc/proxyaggregation.cxx"))
             return false;
        if (loplugin::isSamePathname(fn, SRCDIR "/svx/source/svdraw/svdpntv.cxx")) // ~SdrPaintView calling ClearPageView
             return false;
        if (loplugin::isSamePathname(fn, SRCDIR "/svx/source/svdraw/svdobj.cxx")) // ~SdrObject calling GetLastBoundRect
             return false;
        if (loplugin::isSamePathname(fn, SRCDIR "/svx/source/svdraw/svdedxv.cxx")) // ~SdrObjEditView calling SdrEndTextEdit
             return false;
        if (loplugin::isSamePathname(fn, SRCDIR "/connectivity/source/drivers/file/FStatement.cxx")) // ~OStatement_Base calling disposing
             return false;
        if (loplugin::isSamePathname(fn, SRCDIR "/sd/source/core/CustomAnimationEffect.cxx")) // ~EffectSequenceHelper calling reset
             return false;
        if (loplugin::isSamePathname(fn, SRCDIR "/sd/source/ui/view/sdview.cxx")) // ~View calling DeleteWindowFromPaintView
             return false;
        if (loplugin::isSamePathname(fn, SRCDIR "/sw/source/core/layout/ssfrm.cxx")) // ~SwFrame calling IsDeleteForbidden
             return false;
        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
             return false;

        return true;
    }

    virtual void run() override
    {
        if (preRun())
            TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
    }

    bool PreTraverseCXXDestructorDecl(CXXDestructorDecl*);
    bool PostTraverseCXXDestructorDecl(CXXDestructorDecl*, bool);
    bool TraverseCXXDestructorDecl(CXXDestructorDecl*);
    bool VisitCXXMemberCallExpr(const CXXMemberCallExpr *);

private:
    std::vector<CXXDestructorDecl*>  m_vDestructors;
};

bool FragileDestructor::PreTraverseCXXDestructorDecl(CXXDestructorDecl* cxxDestructorDecl)
{
    if (ignoreLocation(cxxDestructorDecl))
        return true;
    if (!cxxDestructorDecl->isThisDeclarationADefinition())
        return true;
    if (cxxDestructorDecl->getParent()->hasAttr<FinalAttr>())
        return true;
    m_vDestructors.push_back(cxxDestructorDecl);
    return true;
}

bool FragileDestructor::PostTraverseCXXDestructorDecl(CXXDestructorDecl* cxxDestructorDecl, bool)
{
    if (!m_vDestructors.empty() && m_vDestructors.back() == cxxDestructorDecl)
        m_vDestructors.pop_back();
    return true;
}

bool FragileDestructor::TraverseCXXDestructorDecl(CXXDestructorDecl* cxxDestructorDecl)
{
    PreTraverseCXXDestructorDecl(cxxDestructorDecl);
    auto ret = FilteringPlugin::TraverseCXXDestructorDecl(cxxDestructorDecl);
    PostTraverseCXXDestructorDecl(cxxDestructorDecl, ret);
    return ret;
}

bool FragileDestructor::VisitCXXMemberCallExpr(const CXXMemberCallExpr* callExpr)
{
    if (m_vDestructors.empty() || ignoreLocation(callExpr))
        return true;
    const CXXMethodDecl* methodDecl = callExpr->getMethodDecl();
    if (!methodDecl->isVirtual() || methodDecl->hasAttr<FinalAttr>())
        return true;
    const CXXRecordDecl* parentRecordDecl = methodDecl->getParent();
    if (parentRecordDecl->hasAttr<FinalAttr>())
        return true;
    if (!callExpr->getImplicitObjectArgument()->IgnoreImpCasts()->isImplicitCXXThis())
        return true;

    // if we see an explicit call to its own method, that's OK
    auto s1 = compiler.getSourceManager().getCharacterData(callExpr->getBeginLoc());
    auto s2 = compiler.getSourceManager().getCharacterData(callExpr->getEndLoc());
    std::string tok(s1, s2-s1);
    if (tok.find("::") != std::string::npos)
        return true;

    // Very common pattern that we call acquire/dispose in destructors of UNO objects
    // to make sure they are cleaned up.
    if (methodDecl->getName() == "acquire" || methodDecl->getName() == "dispose")
        return true;

    report(
        DiagnosticsEngine::Warning,
        "calling virtual method from destructor, either make the virtual method final, or make this class final",
        callExpr->getBeginLoc())
      << callExpr->getSourceRange();
    report(
        DiagnosticsEngine::Note,
        "callee method here",
        methodDecl->getBeginLoc())
      << methodDecl->getSourceRange();
    return true;
}


loplugin::Plugin::Registration<FragileDestructor> fragiledestructor("fragiledestructor");

}

#endif // LO_CLANG_SHARED_PLUGINS

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */