summaryrefslogtreecommitdiff
path: root/compilerplugins/clang/unusedenumconstants.cxx
blob: 88461c1075fd7de7fb7dc057dfe1657817f608ef (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
/* -*- 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 "plugin.hxx"
#include "compat.hxx"

/**
This looks for unused enum constants

We search for 3 things
(a) constants that are declared but never used
(b) constants only used in a "read" fashion i.e. we compare stuff against them, but we never store a value anywhere
(c) constants only used in a "write" fashion i.e. we store a value, but never check for that value

(a) is fairly reliable but (b) and (c) will need some checking before acting on.

Be warned that it produces around 5G of log file.

The process goes something like this:
  $ make check
  $ make FORCE_COMPILE_ALL=1 COMPILER_PLUGIN_TOOL='unusedenumconstants' check
  $ ./compilerplugins/clang/unusedenumconstants.py

Note that the actual process may involve a fair amount of undoing, hand editing, and general messing around
to get it to work :-)

*/

namespace {

struct MyFieldInfo
{
    std::string parentClass;
    std::string fieldName;
    std::string sourceLocation;
};
bool operator < (const MyFieldInfo &lhs, const MyFieldInfo &rhs)
{
    return std::tie(lhs.parentClass, lhs.fieldName)
         < std::tie(rhs.parentClass, rhs.fieldName);
}


// try to limit the voluminous output a little
static std::set<MyFieldInfo> definitionSet;
static std::set<MyFieldInfo> writeSet;
static std::set<MyFieldInfo> readSet;


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

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

        // dump all our output in one write call - this is to try and limit IO "crosstalk" between multiple processes
        // writing to the same logfile
        std::string output;
        for (const MyFieldInfo & s : definitionSet)
            output += "definition:\t" + s.parentClass + "\t" + s.fieldName + "\t" + s.sourceLocation + "\n";
        for (const MyFieldInfo & s : writeSet)
            output += "write:\t" + s.parentClass + "\t" + s.fieldName + "\n";
        for (const MyFieldInfo & s : readSet)
            output += "read:\t" + s.parentClass + "\t" + s.fieldName + "\n";
        std::ofstream myfile;
        myfile.open( SRCDIR "/loplugin.unusedenumconstants.log", std::ios::app | std::ios::out);
        myfile << output;
        myfile.close();
    }

    bool shouldVisitTemplateInstantiations () const { return true; }
    bool shouldVisitImplicitCode() const { return true; }

    bool VisitEnumConstantDecl( const EnumConstantDecl * );
    bool VisitDeclRefExpr( const DeclRefExpr * );
private:
    MyFieldInfo niceName(const EnumConstantDecl*);
};

MyFieldInfo UnusedEnumConstants::niceName(const EnumConstantDecl* enumConstantDecl)
{
    MyFieldInfo aInfo;

    aInfo.parentClass = enumConstantDecl->getType().getAsString();
    aInfo.fieldName = enumConstantDecl->getNameAsString();
    // sometimes the name (if it's anonymous thing) contains the full path of the build folder, which we don't need
    size_t idx = aInfo.fieldName.find(SRCDIR);
    if (idx != std::string::npos) {
        aInfo.fieldName = aInfo.fieldName.replace(idx, strlen(SRCDIR), "");
    }

    SourceLocation expansionLoc = compiler.getSourceManager().getExpansionLoc( enumConstantDecl->getLocation() );
    StringRef name = compiler.getSourceManager().getFilename(expansionLoc);
    aInfo.sourceLocation = std::string(name.substr(strlen(SRCDIR)+1)) + ":" + std::to_string(compiler.getSourceManager().getSpellingLineNumber(expansionLoc));
    normalizeDotDotInFilePath(aInfo.sourceLocation);

    return aInfo;
}

bool UnusedEnumConstants::VisitEnumConstantDecl( const EnumConstantDecl* enumConstantDecl )
{
    enumConstantDecl = enumConstantDecl->getCanonicalDecl();
    if (ignoreLocation( enumConstantDecl )) {
        return true;
    }
    // ignore stuff that forms part of the stable URE interface
    if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(enumConstantDecl->getLocation()))) {
        return true;
    }

    definitionSet.insert(niceName(enumConstantDecl));
    return true;
}

bool UnusedEnumConstants::VisitDeclRefExpr( const DeclRefExpr* declRefExpr )
{
    auto enumConstantDecl = dyn_cast<EnumConstantDecl>(declRefExpr->getDecl());
    if (!enumConstantDecl) {
        return true;
    }
    enumConstantDecl = enumConstantDecl->getCanonicalDecl();
    if (ignoreLocation(enumConstantDecl)) {
        return true;
    }
    // ignore stuff that forms part of the stable URE interface
    if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(enumConstantDecl->getLocation()))) {
        return true;
    }

    const Stmt * parent = declRefExpr;
try_again:
    parent = getParentStmt(parent);
    bool bWrite = false;
    bool bRead = false;
    bool bDump = false;
    if (!parent)
    {
        // Could probably do better here.
        // Sometimes this is a constructor-initialiser-expression, so just make a pessimistic assumption.
        bWrite = true;
    } else if (isa<CallExpr>(parent) || isa<InitListExpr>(parent) || isa<ArraySubscriptExpr>(parent)
                || isa<ReturnStmt>(parent) || isa<DeclStmt>(parent)
                || isa<CXXConstructExpr>(parent)
                || isa<CXXThrowExpr>(parent))
    {
        bWrite = true;
    } else if (isa<CaseStmt>(parent) || isa<SwitchStmt>(parent))
    {
        bRead = true;
    } else if (const BinaryOperator * binaryOp = dyn_cast<BinaryOperator>(parent))
    {
        if (BinaryOperator::isAssignmentOp(binaryOp->getOpcode())) {
            bWrite = true;
        } else {
            bRead = true;
        }
    } else if (const CXXOperatorCallExpr * operatorCall = dyn_cast<CXXOperatorCallExpr>(parent))
    {
        auto oo = operatorCall->getOperator();
        if (oo == OO_Equal
            || (oo >= OO_PlusEqual && oo <= OO_GreaterGreaterEqual)) {
            bWrite = true;
        } else {
            bRead = true;
        }
    } else if (isa<CastExpr>(parent) || isa<UnaryOperator>(parent)
                || isa<ConditionalOperator>(parent) || isa<ParenExpr>(parent)
                || isa<MaterializeTemporaryExpr>(parent)
                || isa<ExprWithCleanups>(parent))
    {
        goto try_again;
    } else {
        bDump = true;
    }

    // to let me know if I missed something
    if (bDump) {
        parent->dump();
        report( DiagnosticsEngine::Warning,
                "unhandled clang AST node type",
                parent->getLocStart());
    }

    if (bWrite) {
        writeSet.insert(niceName(enumConstantDecl));
    }
    if (bRead) {
        readSet.insert(niceName(enumConstantDecl));
    }
    return true;
}

loplugin::Plugin::Registration< UnusedEnumConstants > X("unusedenumconstants", false);

}

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