searchsymbols.cpp 8.51 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 13 14
** 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
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29 30 31

#include "searchsymbols.h"

32
#include <cplusplus/LookupContext.h>
33

Roberto Raggi's avatar
Roberto Raggi committed
34
#include <QDebug>
35 36

using namespace CPlusPlus;
37
using namespace CppTools;
38

39
SearchSymbols::SymbolTypes SearchSymbols::AllTypes =
40 41 42 43
        SymbolSearcher::Classes
        | SymbolSearcher::Functions
        | SymbolSearcher::Enums
        | SymbolSearcher::Declarations;
44

45 46 47
SearchSymbols::SearchSymbols(Internal::StringTable &stringTable)
    : strings(stringTable)
    , symbolsToSearchFor(SymbolSearcher::Classes | SymbolSearcher::Functions | SymbolSearcher::Enums)
48 49 50
{
}

51
void SearchSymbols::setSymbolsToSearchFor(const SymbolTypes &types)
52
{
53
    symbolsToSearchFor = types;
54 55
}

56
QList<ModelItemInfo> SearchSymbols::operator()(Document::Ptr doc, int sizeHint, const QString &scope)
57 58 59
{
    QString previousScope = switchScope(scope);
    items.clear();
60
    items.reserve(sizeHint);
61 62 63 64
    for (unsigned i = 0; i < doc->globalSymbolCount(); ++i) {
        accept(doc->globalSymbolAt(i));
    }
    (void) switchScope(previousScope);
Roberto Raggi's avatar
Roberto Raggi committed
65
    QList<ModelItemInfo> result = items;
66
    strings.scheduleGC();
Roberto Raggi's avatar
Roberto Raggi committed
67 68 69
    items.clear();
    m_paths.clear();
    return result;
70 71 72 73 74 75 76 77 78 79 80
}

QString SearchSymbols::switchScope(const QString &scope)
{
    QString previousScope = _scope;
    _scope = scope;
    return previousScope;
}

bool SearchSymbols::visit(Enum *symbol)
{
81
    if (!(symbolsToSearchFor & SymbolSearcher::Enums))
82 83
        return false;

84 85
    QString name = overview.prettyName(symbol->name());
    QString scopedName = scopedSymbolName(name, symbol);
86
    QString previousScope = switchScope(scopedName);
87
    appendItem(name, QString(), previousScope, ModelItemInfo::Enum, symbol);
88 89
    for (unsigned i = 0; i < symbol->memberCount(); ++i) {
        accept(symbol->memberAt(i));
90 91 92 93 94 95 96
    }
    (void) switchScope(previousScope);
    return false;
}

bool SearchSymbols::visit(Function *symbol)
{
97
    if (!(symbolsToSearchFor & SymbolSearcher::Functions) || !symbol->name())
98
        return false;
99
    QString name = overview.prettyName(symbol->name());
100
    QString type = overview.prettyType(symbol->type());
101
    appendItem(name, type, _scope, ModelItemInfo::Function, symbol);
102 103 104 105 106
    return false;
}

bool SearchSymbols::visit(Namespace *symbol)
{
Roberto Raggi's avatar
Roberto Raggi committed
107
    QString name = scopedSymbolName(symbol);
108
    QString previousScope = switchScope(name);
109 110
    for (unsigned i = 0; i < symbol->memberCount(); ++i) {
        accept(symbol->memberAt(i));
111 112 113 114 115 116 117
    }
    (void) switchScope(previousScope);
    return false;
}

bool SearchSymbols::visit(Declaration *symbol)
{
118
    if (!(symbolsToSearchFor & SymbolSearcher::Declarations)) {
119
        // if we're searching for functions, still allow signal declarations to show up.
120
        if (symbolsToSearchFor & SymbolSearcher::Functions) {
121 122 123 124
            Function *funTy = symbol->type()->asFunctionType();
            if (!funTy)
                return false;
            if (!funTy->isSignal())
125 126 127 128 129
                return false;
        } else {
            return false;
        }
    }
130

131 132 133 134 135 136 137 138 139
    if (symbol->name()) {
        QString name = overview.prettyName(symbol->name());
        QString type = overview.prettyType(symbol->type());
        appendItem(name, type, _scope,
                   symbol->type()->asFunctionType() ? ModelItemInfo::Function
                                                    : ModelItemInfo::Declaration,
                   symbol);
    }

140 141 142 143 144
    return false;
}

bool SearchSymbols::visit(Class *symbol)
{
145 146
    QString name = overview.prettyName(symbol->name());
    QString scopedName = scopedSymbolName(name, symbol);
147
    QString previousScope = switchScope(scopedName);
Orgad Shaneh's avatar
Orgad Shaneh committed
148
    if (symbolsToSearchFor & SymbolSearcher::Classes)
149
        appendItem(name, QString(), previousScope, ModelItemInfo::Class, symbol);
150 151
    for (unsigned i = 0; i < symbol->memberCount(); ++i) {
        accept(symbol->memberAt(i));
152 153 154 155 156
    }
    (void) switchScope(previousScope);
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
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
bool SearchSymbols::visit(CPlusPlus::UsingNamespaceDirective *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::UsingDeclaration *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::NamespaceAlias *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::Argument *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::TypenameArgument *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::BaseClass *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::Template *)
{
    return true;
}

bool SearchSymbols::visit(CPlusPlus::Block *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::ForwardClassDeclaration *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::ObjCBaseClass *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::ObjCBaseProtocol *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::ObjCClass *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::ObjCForwardClassDeclaration *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::ObjCProtocol *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::ObjCForwardProtocolDeclaration *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::ObjCMethod *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::ObjCPropertyDeclaration *)
{
    return false;
}

242
QString SearchSymbols::scopedSymbolName(const QString &symbolName, const Symbol *symbol) const
243 244
{
    QString name = _scope;
245
    if (!name.isEmpty())
246
        name += QLatin1String("::");
247
    name += scopeName(symbolName, symbol);
248 249 250 251 252
    return name;
}

QString SearchSymbols::scopedSymbolName(const Symbol *symbol) const
{
253
    return scopedSymbolName(overview.prettyName(symbol->name()), symbol);
254 255
}

256
QString SearchSymbols::scopeName(const QString &name, const Symbol *symbol) const
257
{
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    if (!name.isEmpty())
        return name;

    if (symbol->isNamespace()) {
        return QLatin1String("<anonymous namespace>");
    } else if (symbol->isEnum()) {
        return QLatin1String("<anonymous enum>");
    } else if (const Class *c = symbol->asClass())  {
        if (c->isUnion())
            return QLatin1String("<anonymous union>");
        else if (c->isStruct())
            return QLatin1String("<anonymous struct>");
        else
            return QLatin1String("<anonymous class>");
    } else {
        return QLatin1String("<anonymous symbol>");
274
    }
275 276
}

277 278
void SearchSymbols::appendItem(const QString &symbolName, const QString &symbolType,
                               const QString &symbolScope, ModelItemInfo::ItemType itemType,
279
                               Symbol *symbol)
280
{
281
    if (!symbol->name() || symbol->isGenerated())
282 283
        return;

284 285 286 287 288 289
    QString path = m_paths.value(symbol->fileId(), QString());
    if (path.isEmpty()) {
        path = QString::fromUtf8(symbol->fileName(), symbol->fileNameLength());
        m_paths.insert(symbol->fileId(), path);
    }

290
    const QIcon icon = icons.iconForSymbol(symbol);
291 292 293 294
    items.append(ModelItemInfo(findOrInsert(symbolName),
                               findOrInsert(symbolType),
                               findOrInsert(symbolScope),
                               itemType,
295
                               findOrInsert(path),
296
                               symbol->line(),
297
                               symbol->column() - 1, // 1-based vs 0-based column
298
                               icon));
299
}