summaryrefslogtreecommitdiffstats
path: root/compilerplugins/clang/unusedvariablemore.cxx
blob: 908edf33a465ab1fce5421398ceb58d31fcc301e (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * Based on LLVM/Clang.
 *
 * This file is distributed under the University of Illinois Open Source
 * License. See LICENSE.TXT for details.
 *
 */

#include "config_clang.h"
#include <config_global.h>
#include "plugin.hxx"
#include "compat.hxx"
#include "check.hxx"
#include <unordered_set>
#include <unordered_map>

#include "clang/AST/ParentMapContext.h"

namespace loplugin
{
/*
  This is a compile check. The results of this plugin need to be checked by hand, since it is a collection of heuristics.

  Check for unused variable where
  (*) we never call methods that return information from the variable.
  (*) we never pass the variable to anything else

  Classes which are safe to be warned about need to be marked using
  SAL_WARN_UNUSED (see e.g. OUString). For external classes such as std::vector
  that cannot be edited there is a manual list.

  This is an expensive plugin, since it walks up the parent tree,
  so it is off by default.
*/

class UnusedVariableMore : public loplugin::FilteringPlugin<UnusedVariableMore>
{
public:
    explicit UnusedVariableMore(const InstantiationData& data);
    virtual void run() override;
    bool VisitVarDecl(VarDecl const*);
    bool VisitDeclRefExpr(DeclRefExpr const*);
    bool VisitFunctionDecl(FunctionDecl const*);

private:
    bool checkifUnused(Stmt const*, VarDecl const*);
    bool isOkForParameter(const QualType&);

    std::unordered_set<VarDecl const*> interestingSet;
    // used to dump the last place that said the usage was unused, for debug purposes
    std::unordered_map<VarDecl const*, Stmt const*> interestingDebugMap;
};

UnusedVariableMore::UnusedVariableMore(const InstantiationData& data)
    : FilteringPlugin(data)
{
}

void UnusedVariableMore::run()
{
    std::string fn(handler.getMainFileName());
    loplugin::normalizeDotDotInFilePath(fn);

    // ignore QA folders
    if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sal/qa/"))
        return;
    if (loplugin::hasPathnamePrefix(fn, SRCDIR "/i18npool/qa/"))
        return;
    if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sc/qa/"))
        return;

    // vector of shared_ptr used to delay destruction
    if (fn == SRCDIR "/cppuhelper/source/servicemanager.cxx")
        return;
    if (fn == SRCDIR "/i18nlangtag/source/languagetag/languagetag.cxx")
        return;
    // unordered_set of Reference to delay destruction
    if (fn == SRCDIR "/stoc/source/servicemanager/servicemanager.cxx")
        return;
    // TODO "operator >>" fooling me here
    if (fn == SRCDIR "/editeng/source/accessibility/AccessibleEditableTextPara.cxx")
        return;
    // some weird stuff
    if (fn == SRCDIR "/sfx2/source/dialog/dinfdlg.cxx")
        return;
    // std::vector< Reference< XInterface > > keep alive
    if (fn == SRCDIR "/dbaccess/source/core/dataaccess/databasedocument.cxx")
        return;
    // template magic
    if (fn == SRCDIR "/sc/source/core/tool/scmatrix.cxx")
        return;
    // storing local copy of Link<>
    if (fn == SRCDIR "/sc/source/ui/miscdlgs/simpref.cxx")
        return;
    // Using an SwPaM to do stuff
    if (fn == SRCDIR "/sw/source/core/crsr/bookmark.cxx")
        return;
    // index variable in for loop?
    if (fn == SRCDIR "/sw/source/uibase/docvw/edtwin.cxx")
        return;
    // TODO "operator >>" fooling me here
    if (fn == SRCDIR "/sw/source/filter/ww8/ww8par.cxx")
        return;
    // TODO "operator >>" fooling me here
    if (fn == SRCDIR "/sc/source/filter/excel/xistream.cxx")
        return;

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

    for (VarDecl const* varDecl : interestingSet)
    {
        report(DiagnosticsEngine::Warning, "unused variable %0", varDecl->getLocation())
            << varDecl->getDeclName();
        //auto it = interestingDebugMap.find(varDecl);
        //if (it != interestingDebugMap.end())
        //    it->second->dump();
    }
}

bool isWarnUnusedType(QualType type)
{
    if (auto const t = type->getAs<TypedefType>())
    {
        if (t->getDecl()->hasAttr<WarnUnusedAttr>())
        {
            return true;
        }
    }
    if (auto const t = type->getAs<RecordType>())
    {
        if (t->getDecl()->hasAttr<WarnUnusedAttr>())
        {
            return true;
        }
    }
    return loplugin::isExtraWarnUnusedType(type);
}

bool UnusedVariableMore::VisitVarDecl(VarDecl const* var)
{
    if (ignoreLocation(var))
        return true;
    if (var->isDefinedOutsideFunctionOrMethod())
        return true;
    if (isa<ParmVarDecl>(var))
        return true;
    if (!isWarnUnusedType(var->getType()))
        return true;

    // some false +
    auto dc = loplugin::TypeCheck(var->getType());
    if (dc.Class("ZCodec").GlobalNamespace())
        return true;
    if (dc.Class("ScopedVclPtrInstance").GlobalNamespace())
        return true;
    if (dc.Class("VclPtrInstance").GlobalNamespace())
        return true;
    if (dc.Class("Config").GlobalNamespace())
        return true;
    // I think these classes modify global state somehow
    if (dc.Class("SvtHistoryOptions").GlobalNamespace())
        return true;
    if (dc.Class("SvtSecurityOptions").GlobalNamespace())
        return true;
    if (dc.Class("SvtViewOptions").GlobalNamespace())
        return true;
    if (dc.Class("SvtUserOptions").GlobalNamespace())
        return true;
    if (dc.Class("SvtMenuOptions").GlobalNamespace())
        return true;
    if (dc.Class("SvtPathOptions").GlobalNamespace())
        return true;
    if (dc.Class("SvtSysLocaleOptions").GlobalNamespace())
        return true;

    interestingSet.insert(var);
    return true;
}

bool UnusedVariableMore::VisitDeclRefExpr(DeclRefExpr const* declRefExpr)
{
    if (ignoreLocation(declRefExpr))
        return true;
    auto varDecl = dyn_cast<VarDecl>(declRefExpr->getDecl());
    if (!varDecl)
        return true;
    if (interestingSet.find(varDecl) == interestingSet.end())
        return true;
    if (!checkifUnused(declRefExpr, varDecl))
        interestingSet.erase(varDecl);
    return true;
}

// Walk up from a statement that contains a DeclRefExpr, checking if the usage means that the
// related VarDecl is unused.
bool UnusedVariableMore::checkifUnused(Stmt const* stmt, VarDecl const* varDecl)
{
    const Stmt* parent = getParentStmt(stmt);
    if (!parent)
    {
        // check if we're inside a CXXCtorInitializer
        auto parentsRange = compiler.getASTContext().getParents(*stmt);
        if (parentsRange.begin() != parentsRange.end())
        {
            auto parentDecl = parentsRange.begin()->get<Decl>();
            if (parentDecl && (isa<CXXConstructorDecl>(parentDecl) || isa<VarDecl>(parentDecl)))
                return false;
        }
        interestingDebugMap[varDecl] = stmt;
        return true;
    }

    if (isa<ReturnStmt>(parent))
        return false;
    if (isa<IfStmt>(parent))
        return false;
    if (isa<SwitchStmt>(parent))
        return false;
    if (isa<InitListExpr>(parent))
        return false;
    if (isa<CXXConstructExpr>(parent))
        return false;
    if (isa<BinaryOperator>(parent))
        return false;
    if (isa<UnaryOperator>(parent))
        return false;
    if (isa<ConditionalOperator>(parent))
        return false;
    if (isa<ArraySubscriptExpr>(parent))
        return false;
    if (isa<CXXBindTemporaryExpr>(parent))
        return checkifUnused(parent, varDecl);
    if (isa<MaterializeTemporaryExpr>(parent))
        return checkifUnused(parent, varDecl);

    if (isa<CompoundStmt>(parent))
    {
        interestingDebugMap[varDecl] = parent;
        return true;
    }

    // check for cast to void
    if (auto explicitCastExpr = dyn_cast<ExplicitCastExpr>(parent))
    {
        if (loplugin::TypeCheck(explicitCastExpr->getTypeAsWritten()).Void())
            return false;
    }

    if (isa<MemberExpr>(parent))
        return checkifUnused(parent, varDecl);
    if (isa<ExprWithCleanups>(parent))
        return checkifUnused(parent, varDecl);
    if (isa<CastExpr>(parent))
        return checkifUnused(parent, varDecl);
    if (isa<ParenExpr>(parent))
        return checkifUnused(parent, varDecl);

    if (auto operatorCallExpr = dyn_cast<CXXOperatorCallExpr>(parent))
    {
        const CXXMethodDecl* calleeMethodDecl
            = dyn_cast_or_null<CXXMethodDecl>(operatorCallExpr->getDirectCallee());
        if (calleeMethodDecl)
        {
            if (calleeMethodDecl->getNumParams() == 0)
                return checkifUnused(parent, varDecl);
            if (operatorCallExpr->getArg(0) == stmt)
                return checkifUnused(parent, varDecl);
        }
    }
    if (auto memberCallExpr = dyn_cast<CXXMemberCallExpr>(parent))
    {
        const MemberExpr* memberExpr = dyn_cast<MemberExpr>(stmt);
        if (memberExpr && memberCallExpr->getImplicitObjectArgument() == memberExpr->getBase())
        {
            // if we are calling a method on the varDecl, walk up
            if (!checkifUnused(parent, varDecl))
                return false;
            // check if we are passing something to the var by non-const ref, in which case it is updating something else for us
            const FunctionDecl* calleeFunctionDecl = memberCallExpr->getDirectCallee();
            if (calleeFunctionDecl)
            {
                if (calleeFunctionDecl->isVariadic())
                    return false;
                for (unsigned i = 0; i < memberCallExpr->getNumArgs(); ++i)
                {
                    if (i >= calleeFunctionDecl->getNumParams()) // can happen in template code
                    {
                        interestingDebugMap[varDecl] = parent;
                        return true;
                    }
                    if (!isOkForParameter(calleeFunctionDecl->getParamDecl(i)->getType()))
                        return false;
                }
            }
            interestingDebugMap[varDecl] = parent;
            return true;
        }
    }
    if (isa<CallExpr>(parent))
        return false;

    interestingDebugMap[varDecl] = parent;
    return true;
}

bool UnusedVariableMore::isOkForParameter(const QualType& qt)
{
    if (qt->isIntegralOrEnumerationType())
        return true;
    auto const type = loplugin::TypeCheck(qt);
    if (type.Pointer())
    {
        return bool(type.Pointer().Const());
    }
    else if (type.LvalueReference())
    {
        return bool(type.LvalueReference().Const());
    }
    return false;
}

bool UnusedVariableMore::VisitFunctionDecl(FunctionDecl const* functionDecl)
{
    if (ignoreLocation(functionDecl))
        return true;
    //functionDecl->dump();
    return true;
}

static Plugin::Registration<UnusedVariableMore> X("unusedvariablemore", false);

} // namespace

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