summaryrefslogtreecommitdiffstats
path: root/compilerplugins/clang/store/manualrefcount.cxx
blob: c8085904fede8f888d9b2a9f44bac85abba19fd5 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* -*- 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/.
 */

#include <cassert>
#include <string>
#include <iostream>
#include <fstream>
#include <set>

#include <clang/AST/CXXInheritance.h>
#include "compat.hxx"
#include "plugin.hxx"
#include "check.hxx"

/**
 Look for calls to the ref-counting methods acquire()/release(), which should only be called by classes like rtl::Reference.
*/

namespace {

class ManualRefCount:
    public RecursiveASTVisitor<ManualRefCount>, public loplugin::Plugin
{
public:
    explicit ManualRefCount(InstantiationData const & data): Plugin(data) {}

    virtual void run() override
    {
        StringRef fn( compiler.getSourceManager().getFileEntryForID(
                          compiler.getSourceManager().getMainFileID())->getName() );

       // old code, no point in updating
        if (loplugin::isSamePathname(fn, SRCDIR "/store/source/store.cxx"))
             return;

// TODO -----------------------------
        if (loplugin::isSamePathname(fn, SRCDIR "/registry/source/registry.cxx"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/registry/source/regimpl.cxx"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/registry/source/reflread.cxx"))
             return;
        // TODO MenuAttributes::CreateAttribute
        if (loplugin::isSamePathname(fn, SRCDIR "/framework/source/fwe/xml/menuconfiguration.cxx"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/sw/source/uibase/app/apphdl.cxx"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/dbaccess/source/core/dataaccess/ModelImpl.cxx"))
             return;
        // need a better replacement for vcl::EventPoster
        if (loplugin::isSamePathname(fn, SRCDIR "/svtools/source/misc/acceleratorexecute.cxx"))
             return;
        // PostUserEvent stuff
        if (loplugin::isSamePathname(fn, SRCDIR "/toolkit/source/awt/vclxwindow.cxx"))
             return;
        // playing games with pointers passed into combobox entries
        if (loplugin::isSamePathname(fn, SRCDIR "/cui/source/customize/cfgutil.cxx"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/cui/source/customize/cfg.cxx"))
             return;
// END TODO -----------------------------

        // can't fix these without breaking stable ABI
        if (fn.startswith(SRCDIR "/sal/"))
             return;
        if (fn.startswith(SRCDIR "/salhelper/"))
             return;
        if (fn.startswith(SRCDIR "/cppu/"))
             return;
        if (fn.startswith(SRCDIR "/cppuhelper/"))
             return;
        if (fn.startswith(SRCDIR "/bridges/"))
             return;

        // lots of magic here
        if (fn.startswith(SRCDIR "/stoc/"))
             return;
        if (fn.startswith(SRCDIR "/testtools/"))
             return;

        // mutex games
        if (loplugin::isSamePathname(fn, SRCDIR "/vcl/source/app/scheduler.cxx"))
             return;
        // opengl games
        if (loplugin::isSamePathname(fn, SRCDIR "/vcl/source/app/svdata.cxx"))
             return;

        // passing the pointer through PostUserEvent
        if (loplugin::isSamePathname(fn, SRCDIR "/avmedia/source/gstreamer/gstplayer.cxx"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/svx/source/form/fmscriptingenv.cxx"))
             return;

        // thread games
        if (loplugin::isSamePathname(fn, SRCDIR "/io/source/stm/opump.cxx"))
             return;

        // ??? no idea what this code is up to
        if (loplugin::isSamePathname(fn, SRCDIR "/extensions/source/scanner/scanunx.cxx"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/stoc/source/invocation_adapterfactory/iafactory.cxx"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/fpicker/source/office/asyncfilepicker.cxx"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/forms/source/component/FormComponent.cxx"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/ucb/source/ucp/file/bc.cxx"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/ucb/source/ucp/file/filprp.cxx"))
             return;
        // calling release() ?
        if (loplugin::isSamePathname(fn, SRCDIR "/toolkit/source/helper/accessibilityclient.cxx"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/svtools/source/misc/svtaccessiblefactory.cxx"))
             return;

        // implementing css::uno::XInterface
        if (loplugin::isSamePathname(fn, SRCDIR "/sd/source/ui/animations/motionpathtag.cxx"))
             return;
        // UNO factory methods
        if (fn.startswith(SRCDIR "/comphelper/"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/linguistic/source/convdiclist.cxx"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/linguistic/source/dlistimp.cxx"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/linguistic/source/gciterator.cxx"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/linguistic/source/lngsvcmgr.cxx"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/linguistic/source/lngopt.cxx"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/vcl/unx/generic/gdi/gcach_xpeer.cxx"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/dbaccess/source/ui/dlg/dbwizsetup.cxx"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/dbaccess/source/ui/dlg/dbwizsetup.cxx"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/lingucomponent/source/hyphenator/hyphen/hyphenimp.cxx"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/lingucomponent/source/spellcheck/spell/sspellimp.cxx"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/lingucomponent/source/thesaurus/libnth/nthesimp.cxx"))
             return;


        // some kind of complicated listener nonsense
        if (loplugin::isSamePathname(fn, SRCDIR "/sd/source/ui/framework/tools/FrameworkHelper.cxx"))
             return;
        // more listener nonsense
        if (loplugin::isSamePathname(fn, SRCDIR "/sw/source/uibase/uno/unomailmerge.cxx"))
             return;
        // playing games with it's listener list
        if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/unoobj/cellsuno.cxx"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/unoobj/chart2uno.cxx"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/unoobj/dapiuno.cxx"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/unoobj/datauno.cxx"))
             return;
        if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/unoobj/linkuno.cxx"))
             return;
        // PostUserEvent
        if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/vba/vbaeventshelper.cxx"))
             return;
        // thread holding itself
        if (loplugin::isSamePathname(fn, SRCDIR "/forms/source/component/EventThread.cxx"))
             return;


        TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
    }

    bool shouldVisitTemplateInstantiations () const { return true; }

    bool VisitCXXMemberCallExpr(const CXXMemberCallExpr *);
    bool TraverseCXXRecordDecl(CXXRecordDecl *);
    bool TraverseCXXMethodDecl(CXXMethodDecl *);
    bool TraverseFunctionDecl(FunctionDecl *);
    bool TraverseCXXConstructorDecl(CXXConstructorDecl *);
    bool TraverseCXXDestructorDecl(CXXDestructorDecl *);
    bool TraverseCXXConversionDecl(CXXConversionDecl *);
    bool TraverseClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl *);
    bool TraverseLinkageSpecDecl(LinkageSpecDecl *);
private:
    bool ignoreCallerClass(CXXRecordDecl*);
};

bool ManualRefCount::TraverseCXXMethodDecl(CXXMethodDecl* cxxMethodDecl)
{
    if (ignoreCallerClass(cxxMethodDecl->getParent()))
        return true;
    // disambiguating forwarding methods for XInterface subclasses
    if (cxxMethodDecl->getIdentifier() && (cxxMethodDecl->getName() == "acquire" || cxxMethodDecl->getName() == "release"))
        return true;
    return RecursiveASTVisitor::TraverseCXXMethodDecl(cxxMethodDecl);
}

bool ManualRefCount::TraverseFunctionDecl(FunctionDecl* functionDecl)
{
    auto tc = loplugin::DeclCheck(functionDecl);
    if (tc.Function("make_shared_from_UNO").Namespace("comphelper").GlobalNamespace())
        return true;
    return RecursiveASTVisitor::TraverseFunctionDecl(functionDecl);
}

bool ManualRefCount::TraverseCXXConstructorDecl(CXXConstructorDecl* cxxMethodDecl)
{
    if (ignoreCallerClass(cxxMethodDecl->getParent()))
        return true;
    return RecursiveASTVisitor::TraverseCXXMethodDecl(cxxMethodDecl);
}

bool ManualRefCount::TraverseCXXDestructorDecl(CXXDestructorDecl*)
{
    // just ignore destructors, tons of places like to call acquire() on themselves in their destructor
    // supposedly to prevent recursively calling the destructor
    return true;
}
bool ManualRefCount::TraverseCXXConversionDecl(CXXConversionDecl* cxxMethodDecl)
{
    if (ignoreCallerClass(cxxMethodDecl->getParent()))
        return true;
    return RecursiveASTVisitor::TraverseCXXMethodDecl(cxxMethodDecl);
}
bool ManualRefCount::TraverseCXXRecordDecl(CXXRecordDecl* cxxRecordDecl)
{
    if (ignoreCallerClass(cxxRecordDecl))
        return true;
    return RecursiveASTVisitor::TraverseCXXRecordDecl(cxxRecordDecl);
}

bool ManualRefCount::TraverseClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl* templateDecl)
{
    if (ignoreCallerClass(templateDecl))
        return true;
    return RecursiveASTVisitor::TraverseClassTemplateSpecializationDecl(templateDecl);
}

bool ManualRefCount::TraverseLinkageSpecDecl(LinkageSpecDecl *)
{
    // ignore methods inside "extern ""C""" blocks, these are normally UNO constructors, which
    // are required to raise the reference count before returning
    return true;
}

bool ManualRefCount::ignoreCallerClass(CXXRecordDecl* cxxRecordDecl)
{
    auto tc = loplugin::TypeCheck(cxxRecordDecl);
    return
           tc.Class("Reference").Namespace("rtl").GlobalNamespace()
        || tc.Class("cow_wrapper").Namespace("o3tl").GlobalNamespace()
        || tc.Class("Reference").Namespace("uno").Namespace("star").Namespace("sun").Namespace("com").GlobalNamespace()
        || tc.Class("ShareGuard").Namespace("framework").GlobalNamespace()
        || tc.Class("ControlModelLock").Namespace("frm").GlobalNamespace()
        || tc.Struct("ReleaseFunc").Namespace("detail").Namespace("comphelper").GlobalNamespace()
        // TODO no idea what this is up to
        || tc.Class("SfxModelSubComponent").GlobalNamespace()
        || tc.Class("OSubComponent").Namespace("mysqlc").Namespace("connectivity").GlobalNamespace()
        || tc.Class("OSubComponent").Namespace("connectivity").GlobalNamespace()
        // TODO do we really need this?
        || tc.Class("ShareableMutex").Namespace("framework").GlobalNamespace()
        || tc.Class("ObservableThread").GlobalNamespace()
        ;
}

bool ManualRefCount::VisitCXXMemberCallExpr(const CXXMemberCallExpr* cxxMemberCallExpr)
{
    if (ignoreLocation(cxxMemberCallExpr))
        return true;
    if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(cxxMemberCallExpr->getLocStart())))
        return true;

    // first, use some heuristics to find the right kind of acquire()/release() calls
    CXXMethodDecl const * calleeMethodDecl = cxxMemberCallExpr->getMethodDecl();
    if (!calleeMethodDecl || !calleeMethodDecl->getIdentifier())
        return true;
    if (calleeMethodDecl->getName() != "acquire" && calleeMethodDecl->getName() != "release")
        return true;
    if (calleeMethodDecl->getNumParams() != 0)
        return true;
    // std::unique_ptr::release() and similar methods
    if (calleeMethodDecl->getName() == "release" && loplugin::TypeCheck(calleeMethodDecl->getReturnType()).Pointer())
        return true;

    // these are OK
    auto calleeRecordTC = loplugin::TypeCheck(calleeMethodDecl->getParent());
    if (calleeRecordTC.Struct("ResourceHolder").Namespace("store").GlobalNamespace())
        return true;
    if (calleeRecordTC.Class("Module").Namespace("osl").GlobalNamespace())
        return true;
    if (calleeRecordTC.Class("Mutex").Namespace("osl").GlobalNamespace())
        return true;
    if (calleeRecordTC.Class("multi_type_vector").Namespace("mdds").GlobalNamespace())
        return true;

//    while (calleeMethodDecl->size_overridden_methods() > 0)
//        calleeMethodDecl = *calleeMethodDecl->begin_overridden_methods();
//    auto tc2 = loplugin::TypeCheck(calleeMethodDecl->getParent());
//    if (tc2.Class("XInterface").Namespace("uno").Namespace("star").Namespace("sun").Namespace("com").GlobalNamespace())
//        return true;

std::cout << calleeMethodDecl->getParent()->getQualifiedNameAsString() << std::endl;
    report(
        DiagnosticsEngine::Warning, "call to acquire/release",
        cxxMemberCallExpr->getLocStart())
        << cxxMemberCallExpr->getSourceRange();
    return true;
}


loplugin::Plugin::Registration< ManualRefCount > X("manualrefcount", true);

}

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