summaryrefslogtreecommitdiff
path: root/clang/find-unprefixed-members.cxx
blob: c79647aa12d50a3b6b3608a2a0183ba418e189fd (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
#include <fstream>
#include <iostream>
#include <set>
#include <sstream>

#include <clang/AST/ASTConsumer.h>
#include <clang/AST/ASTContext.h>
#include <clang/AST/RecursiveASTVisitor.h>
#include <clang/Rewrite/Core/Rewriter.h>
#include <clang/Tooling/CommonOptionsParser.h>
#include <clang/Tooling/Tooling.h>

class Context
{
    std::string m_aClassName;
    std::string m_aClassPrefix;

public:
    Context(const std::string& rClassName, const std::string& rClassPrefix)
        : m_aClassName(rClassName),
          m_aClassPrefix(rClassPrefix)
    {
    }

    bool match(const std::string& rName) const
    {
        if (m_aClassName == "")
            return rName.find(m_aClassPrefix) == 0;
        else
            return rName == m_aClassName;
    }
};

class Visitor : public clang::RecursiveASTVisitor<Visitor>
{
    const Context m_rContext;
    /// List of qualified class name -- member name pairs.
    std::vector<std::pair<std::string, std::string>> m_aResults;
    /// List of qualified class names which have member functions.
    std::set<std::string> m_aFunctions;

public:
    Visitor(const Context& rContext)
        : m_rContext(rContext)
    {
    }

    const std::vector<std::pair<std::string, std::string>>& getResults()
    {
        return m_aResults;
    }

    const std::set<std::string>& getFunctions()
    {
        return m_aFunctions;
    }

    /*
     * class C
     * {
     * public:
     *     int nX; <- Handles this declaration.
     * };
     */
    bool VisitFieldDecl(clang::FieldDecl* pDecl)
    {
        clang::RecordDecl* pRecord = pDecl->getParent();

        if (m_rContext.match(pRecord->getQualifiedNameAsString()))
        {
            std::string aName = pDecl->getNameAsString();
            if (aName.find("m") != 0)
            {
                aName.insert(0, "m_");
                std::stringstream ss;
                ss << pDecl->getNameAsString() << "," << aName;
                m_aResults.push_back(std::make_pair(pRecord->getQualifiedNameAsString(), ss.str()));
            }
        }

        return true;
    }

    /*
     * class C
     * {
     * public:
     *     static const int aS[]; <- Handles e.g. this declaration;
     * };
     */
    bool VisitVarDecl(clang::VarDecl* pDecl)
    {
        if (!pDecl->getQualifier())
            return true;

        clang::RecordDecl* pRecord = pDecl->getQualifier()->getAsType()->getAsCXXRecordDecl();

        if (m_rContext.match(pRecord->getQualifiedNameAsString()))
        {
            std::string aName = pDecl->getNameAsString();
            if (aName.find("m") != 0)
            {
                aName.insert(0, "m_");
                std::stringstream ss;
                ss << pDecl->getNameAsString() << "," << aName;
                m_aResults.push_back(std::make_pair(pRecord->getQualifiedNameAsString(), ss.str()));
            }
        }

        return true;
    }

    bool VisitCXXMethodDecl(clang::CXXMethodDecl* pDecl)
    {
        if (clang::isa<clang::CXXConstructorDecl>(pDecl) || clang::isa<clang::CXXDestructorDecl>(pDecl))
            return true;

        clang::CXXRecordDecl* pRecord = pDecl->getParent();
        if (pRecord->getKindName().str() == "struct")
            return true;

        m_aFunctions.insert(pRecord->getQualifiedNameAsString());
        return true;
    }
};

class ASTConsumer : public clang::ASTConsumer
{
    const Context& m_rContext;

public:
    ASTConsumer(const Context& rContext)
        : m_rContext(rContext)
    {
    }

    virtual void HandleTranslationUnit(clang::ASTContext& rContext)
    {
        if (rContext.getDiagnostics().hasErrorOccurred())
            return;

        Visitor aVisitor(m_rContext);
        aVisitor.TraverseDecl(rContext.getTranslationUnitDecl());
        const std::set<std::string>& rFunctions = aVisitor.getFunctions();
        const std::vector<std::pair<std::string, std::string>>& rResults = aVisitor.getResults();
        // Ignore missing prefixes in structs without member functions.
        bool bFound = false;
        for (const std::string& rFunction : rFunctions)
        {
            for (const std::pair<std::string, std::string>& rResult : rResults)
            {
                if (rResult.first == rFunction)
                {
                    std::cerr << rResult.first << "::" << rResult.second << std::endl;
                    bFound = true;
                }
            }
        }
        if (bFound)
            exit(1);
    }
};

class FrontendAction
{
    const Context& m_rContext;

public:
    FrontendAction(const Context& rContext)
        : m_rContext(rContext)
    {
    }

#if (__clang_major__ == 3 && __clang_minor__ >= 6) || __clang_major__ > 3
    std::unique_ptr<clang::ASTConsumer> newASTConsumer()
    {
        return llvm::make_unique<ASTConsumer>(m_rContext);
    }
#else
    clang::ASTConsumer* newASTConsumer()
    {
        return new ASTConsumer(m_rContext);
    }
#endif
};

int main(int argc, const char** argv)
{
    llvm::cl::OptionCategory aCategory("find-unprefixed-members options");
    llvm::cl::opt<std::string> aClassName("class-name",
                                          llvm::cl::desc("Qualified name (namespace::Class)."),
                                          llvm::cl::cat(aCategory));
    llvm::cl::opt<std::string> aClassPrefix("class-prefix",
                                            llvm::cl::desc("Qualified name prefix (e.g. namespace::Cl)."),
                                            llvm::cl::cat(aCategory));
    clang::tooling::CommonOptionsParser aParser(argc, argv, aCategory);

    if (aClassName.empty() && aClassPrefix.empty())
    {
        std::cerr << "-class-name or -class-prefix is required." << std::endl;
        return 1;
    }

    clang::tooling::ClangTool aTool(aParser.getCompilations(), aParser.getSourcePathList());

    Context aContext(aClassName, aClassPrefix);
    FrontendAction aAction(aContext);
    std::unique_ptr<clang::tooling::FrontendActionFactory> pFactory = clang::tooling::newFrontendActionFactory(&aAction);
    return aTool.run(pFactory.get());
}

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