searchsymbols.cpp 9.04 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2013 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
SearchSymbols::SearchSymbols():
46
    symbolsToSearchFor(SymbolSearcher::Classes | SymbolSearcher::Functions | SymbolSearcher::Enums),
47
    separateScope(false)
48 49 50
{
}

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

56 57 58 59 60
void SearchSymbols::setSeparateScope(bool separateScope)
{
    this->separateScope = separateScope;
}

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

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

bool SearchSymbols::visit(Enum *symbol)
{
86
    if (!(symbolsToSearchFor & SymbolSearcher::Enums))
87 88 89
        return false;

    QString name = symbolName(symbol);
90 91 92 93 94
    QString scopedName = scopedSymbolName(name);
    QString previousScope = switchScope(scopedName);
    appendItem(separateScope ? name : scopedName,
               separateScope ? previousScope : QString(),
               ModelItemInfo::Enum, symbol);
95 96
    for (unsigned i = 0; i < symbol->memberCount(); ++i) {
        accept(symbol->memberAt(i));
97 98 99 100 101 102 103
    }
    (void) switchScope(previousScope);
    return false;
}

bool SearchSymbols::visit(Function *symbol)
{
104
    if (!(symbolsToSearchFor & SymbolSearcher::Functions))
105 106
        return false;

107
    QString extraScope;
Roberto Raggi's avatar
Roberto Raggi committed
108
    if (const Name *name = symbol->name()) {
109 110 111
        if (const QualifiedNameId *q = name->asQualifiedNameId()) {
            if (q->base())
                extraScope = overview.prettyName(q->base());
112 113 114 115 116 117
        }
    }
    QString fullScope = _scope;
    if (!_scope.isEmpty() && !extraScope.isEmpty())
        fullScope += QLatin1String("::");
    fullScope += extraScope;
118
    QString name = symbolName(symbol);
119 120
    QString scopedName = scopedSymbolName(name);
    QString type = overview.prettyType(symbol->type(),
121
                                       separateScope ? symbol->unqualifiedName() : 0);
122
    appendItem(separateScope ? type : scopedName,
123
               separateScope ? fullScope : type,
124
               ModelItemInfo::Method, symbol);
125 126 127 128 129
    return false;
}

bool SearchSymbols::visit(Namespace *symbol)
{
Roberto Raggi's avatar
Roberto Raggi committed
130
    QString name = scopedSymbolName(symbol);
131
    QString previousScope = switchScope(name);
132 133
    for (unsigned i = 0; i < symbol->memberCount(); ++i) {
        accept(symbol->memberAt(i));
134 135 136 137 138 139 140
    }
    (void) switchScope(previousScope);
    return false;
}

bool SearchSymbols::visit(Declaration *symbol)
{
141
    if (!(symbolsToSearchFor & SymbolSearcher::Declarations))
142 143 144 145 146
        return false;

    QString name = symbolName(symbol);
    QString scopedName = scopedSymbolName(name);
    QString type = overview.prettyType(symbol->type(),
147
                                       separateScope ? symbol->unqualifiedName() : 0);
148 149 150
    appendItem(separateScope ? type : scopedName,
               separateScope ? _scope : type,
               ModelItemInfo::Declaration, symbol);
151 152 153 154 155 156
    return false;
}

bool SearchSymbols::visit(Class *symbol)
{
    QString name = symbolName(symbol);
157 158
    QString scopedName = scopedSymbolName(name);
    QString previousScope = switchScope(scopedName);
159
    if (symbolsToSearchFor & SymbolSearcher::Classes) {
160 161 162 163
        appendItem(separateScope ? name : scopedName,
                   separateScope ? previousScope : QString(),
                   ModelItemInfo::Class, symbol);
    }
164 165
    for (unsigned i = 0; i < symbol->memberCount(); ++i) {
        accept(symbol->memberAt(i));
166 167 168 169 170
    }
    (void) switchScope(previousScope);
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
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
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;
}

256
QString SearchSymbols::scopedSymbolName(const QString &symbolName) const
257 258
{
    QString name = _scope;
259
    if (!name.isEmpty())
260
        name += QLatin1String("::");
261 262 263 264 265 266 267 268 269 270 271
    name += symbolName;
    return name;
}

QString SearchSymbols::scopedSymbolName(const Symbol *symbol) const
{
    return scopedSymbolName(symbolName(symbol));
}

QString SearchSymbols::symbolName(const Symbol *symbol) const
{
272 273 274 275 276 277 278 279
    QString symbolName = overview.prettyName(symbol->name());
    if (symbolName.isEmpty()) {
        QString type;
        if (symbol->isNamespace()) {
            type = QLatin1String("namespace");
        } else if (symbol->isEnum()) {
            type = QLatin1String("enum");
        } else if (const Class *c = symbol->asClass())  {
280
            if (c->isUnion())
281
                type = QLatin1String("union");
282
            else if (c->isStruct())
283
                type = QLatin1String("struct");
284
            else
285 286 287 288 289 290
                type = QLatin1String("class");
        } else {
            type = QLatin1String("symbol");
        }
        symbolName = QLatin1String("<anonymous ");
        symbolName += type;
291
        symbolName += QLatin1Char('>');
292
    }
293 294 295 296 297 298
    return symbolName;
}

void SearchSymbols::appendItem(const QString &name,
                               const QString &info,
                               ModelItemInfo::ItemType type,
299
                               Symbol *symbol)
300
{
301 302 303
    if (!symbol->name())
        return;

304 305
    QStringList fullyQualifiedName;
    foreach (const Name *name, LookupContext::fullyQualifiedName(symbol))
Roberto Raggi's avatar
Roberto Raggi committed
306
        fullyQualifiedName.append(findOrInsert(overview.prettyName(name)));
307 308 309 310 311 312 313

    QString path = m_paths.value(symbol->fileId(), QString());
    if (path.isEmpty()) {
        path = QString::fromUtf8(symbol->fileName(), symbol->fileNameLength());
        m_paths.insert(symbol->fileId(), path);
    }

314
    const QIcon icon = icons.iconForSymbol(symbol);
Roberto Raggi's avatar
Roberto Raggi committed
315
    items.append(ModelItemInfo(findOrInsert(name), findOrInsert(info), type,
316
                               fullyQualifiedName,
317
                               path,
318
                               symbol->line(),
319
                               symbol->column() - 1, // 1-based vs 0-based column
320
                               icon));
321
}