typehierarchybuilder.cpp 6.98 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11 12
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
Eike Ziller's avatar
Eike Ziller committed
13 14
** conditions see http://www.qt.io/licensing.  For further information
** use the contact form at http://www.qt.io/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24 25 26
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
30

31
#include "typehierarchybuilder.h"
32 33

#include <cplusplus/FindUsages.h>
34

35
using namespace CppTools;
36 37 38 39 40 41 42 43 44 45 46

namespace {

QString unqualifyName(const QString &qualifiedName)
{
    const int index = qualifiedName.lastIndexOf(QLatin1String("::"));
    if (index == -1)
        return qualifiedName;
    return qualifiedName.right(qualifiedName.length() - index - 2);
}

47
class DerivedHierarchyVisitor : public CPlusPlus::SymbolVisitor
48 49 50 51 52 53 54
{
public:
    DerivedHierarchyVisitor(const QString &qualifiedName)
        : _qualifiedName(qualifiedName)
        , _unqualifiedName(unqualifyName(qualifiedName))
    {}

55
    void execute(const CPlusPlus::Document::Ptr &doc, const CPlusPlus::Snapshot &snapshot);
56

57
    virtual bool visit(CPlusPlus::Class *);
58

59
    const QList<CPlusPlus::Symbol *> &derived() { return _derived; }
60 61 62
    const QStringList otherBases() { return _otherBases; }

private:
63
    CPlusPlus::LookupContext _context;
64 65
    QString _qualifiedName;
    QString _unqualifiedName;
66 67
    CPlusPlus::Overview _overview;
    QHash<CPlusPlus::Symbol *, QString> _actualBases;
68
    QStringList _otherBases;
69
    QList<CPlusPlus::Symbol *> _derived;
70 71
};

72 73
void DerivedHierarchyVisitor::execute(const CPlusPlus::Document::Ptr &doc,
                                      const CPlusPlus::Snapshot &snapshot)
74 75 76
{
    _derived.clear();
    _otherBases.clear();
77
    _context = CPlusPlus::LookupContext(doc, snapshot);
78 79 80 81 82

    for (unsigned i = 0; i < doc->globalSymbolCount(); ++i)
        accept(doc->globalSymbolAt(i));
}

83
bool DerivedHierarchyVisitor::visit(CPlusPlus::Class *symbol)
84 85
{
    for (unsigned i = 0; i < symbol->baseClassCount(); ++i) {
86
        CPlusPlus::BaseClass *baseSymbol = symbol->baseClassAt(i);
87 88 89

        QString baseName = _actualBases.value(baseSymbol);
        if (baseName.isEmpty()) {
90
            QList<CPlusPlus::LookupItem> items = _context.lookup(baseSymbol->name(), symbol->enclosingScope());
91 92 93
            if (items.isEmpty() || !items.first().declaration())
                continue;

94
            CPlusPlus::Symbol *actualBaseSymbol = items.first().declaration();
95
            if (actualBaseSymbol->isTypedef()) {
96
                CPlusPlus::NamedType *namedType = actualBaseSymbol->type()->asNamedType();
97 98 99 100
                if (!namedType) {
                    // Anonymous aggregate such as: typedef struct {} Empty;
                    continue;
                }
101
                const QString &typeName = _overview.prettyName(namedType->name());
102
                if (typeName == _unqualifiedName || typeName == _qualifiedName) {
103 104 105 106 107 108 109
                    items = _context.lookup(namedType->name(), actualBaseSymbol->enclosingScope());
                    if (items.isEmpty() || !items.first().declaration())
                        continue;
                    actualBaseSymbol = items.first().declaration();
                }
            }

110 111
            const QList<const CPlusPlus::Name *> &full
                    = CPlusPlus::LookupContext::fullyQualifiedName(actualBaseSymbol);
112 113 114 115 116 117 118 119 120 121 122 123 124
            baseName = _overview.prettyName(full);
            _actualBases.insert(baseSymbol, baseName);
        }

        if (_qualifiedName == baseName)
            _derived.append(symbol);
        else
            _otherBases.append(baseName);
    }

    return true;
}

125
} // namespace
126 127 128 129

TypeHierarchy::TypeHierarchy() : _symbol(0)
{}

130
TypeHierarchy::TypeHierarchy(CPlusPlus::Symbol *symbol) : _symbol(symbol)
131 132
{}

133
CPlusPlus::Symbol *TypeHierarchy::symbol() const
134 135 136 137 138 139 140 141 142
{
    return _symbol;
}

const QList<TypeHierarchy> &TypeHierarchy::hierarchy() const
{
    return _hierarchy;
}

143
TypeHierarchyBuilder::TypeHierarchyBuilder(CPlusPlus::Symbol *symbol, const CPlusPlus::Snapshot &snapshot)
144 145
    : _symbol(symbol)
    , _snapshot(snapshot)
146
{}
147 148 149 150 151 152 153 154 155 156 157

void TypeHierarchyBuilder::reset()
{
    _visited.clear();
    _candidates.clear();
}

TypeHierarchy TypeHierarchyBuilder::buildDerivedTypeHierarchy()
{
    reset();
    TypeHierarchy hierarchy(_symbol);
158
    buildDerived(&hierarchy, filesDependingOn(_symbol));
159 160 161
    return hierarchy;
}

162 163
void TypeHierarchyBuilder::buildDerived(TypeHierarchy *typeHierarchy,
                                        const QStringList &dependingFiles)
164
{
165
    CPlusPlus::Symbol *symbol = typeHierarchy->_symbol;
166 167 168 169 170
    if (_visited.contains(symbol))
        return;

    _visited.insert(symbol);

171
    const QString &symbolName = _overview.prettyName(CPlusPlus::LookupContext::fullyQualifiedName(symbol));
172 173
    DerivedHierarchyVisitor visitor(symbolName);

174
    foreach (const QString &fileName, dependingFiles) {
175
        CPlusPlus::Document::Ptr doc = _snapshot.document(fileName);
176 177 178 179 180 181 182 183 184 185 186 187
        if ((_candidates.contains(fileName) && !_candidates.value(fileName).contains(symbolName))
                || !doc->control()->findIdentifier(symbol->identifier()->chars(),
                                                   symbol->identifier()->size())) {
            continue;
        }

        visitor.execute(doc, _snapshot);
        _candidates.insert(fileName, QSet<QString>());

        foreach (const QString &candidate, visitor.otherBases())
            _candidates[fileName].insert(candidate);

188
        foreach (CPlusPlus::Symbol *s, visitor.derived()) {
189
            TypeHierarchy derivedHierarchy(s);
190
            buildDerived(&derivedHierarchy, filesDependingOn(s));
191 192 193 194
            typeHierarchy->_hierarchy.append(derivedHierarchy);
        }
    }
}
195 196 197 198 199 200 201

QStringList TypeHierarchyBuilder::filesDependingOn(CPlusPlus::Symbol *symbol) const
{
    if (!symbol)
        return QStringList();

    const QString file = QString::fromUtf8(symbol->fileName(), symbol->fileNameLength());
202
    return QStringList() << file << _snapshot.filesDependingOn(file);
203
}