searchsymbols.cpp 8.81 KB
Newer Older
1
/**************************************************************************
2 3 4
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
8 9
**
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
30
**
31
**************************************************************************/
32 33 34 35 36

#include "searchsymbols.h"

#include <Literals.h>
#include <Scope.h>
37
#include <Names.h>
38
#include <cplusplus/LookupContext.h>
Roberto Raggi's avatar
Roberto Raggi committed
39
#include <QDebug>
40 41 42 43

using namespace CPlusPlus;
using namespace CppTools::Internal;

44 45 46 47 48 49
SearchSymbols::SymbolTypes SearchSymbols::AllTypes =
        SearchSymbols::Classes
        | SearchSymbols::Functions
        | SearchSymbols::Enums
        | SearchSymbols::Declarations;

50
SearchSymbols::SearchSymbols():
51 52
    symbolsToSearchFor(Classes | Functions | Enums),
    separateScope(false)
53 54 55
{
}

56
void SearchSymbols::setSymbolsToSearchFor(SymbolTypes types)
57
{
58
    symbolsToSearchFor = types;
59 60
}

61 62 63 64 65
void SearchSymbols::setSeparateScope(bool separateScope)
{
    this->separateScope = separateScope;
}

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

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

bool SearchSymbols::visit(Enum *symbol)
{
90
    if (!(symbolsToSearchFor & Enums))
91 92 93
        return false;

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

bool SearchSymbols::visit(Function *symbol)
{
108
    if (!(symbolsToSearchFor & Functions))
109 110
        return false;

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

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

bool SearchSymbols::visit(Declaration *symbol)
{
145 146 147 148 149 150
    if (!(symbolsToSearchFor & Declarations))
        return false;

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

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

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

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

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

QString SearchSymbols::symbolName(const Symbol *symbol) const
{
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    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())  {
            if (c->isUnion()) {
                type = QLatin1String("union");
            } else if (c->isStruct()) {
                type = QLatin1String("struct");
            } else {
                type = QLatin1String("class");
            }
        } else {
            type = QLatin1String("symbol");
        }
        symbolName = QLatin1String("<anonymous ");
        symbolName += type;
296
        symbolName += QLatin1Char('>');
297
    }
298 299 300 301 302 303
    return symbolName;
}

void SearchSymbols::appendItem(const QString &name,
                               const QString &info,
                               ModelItemInfo::ItemType type,
304
                               Symbol *symbol)
305
{
306 307 308
    if (!symbol->name())
        return;

309 310
    QStringList fullyQualifiedName;
    foreach (const Name *name, LookupContext::fullyQualifiedName(symbol))
Roberto Raggi's avatar
Roberto Raggi committed
311
        fullyQualifiedName.append(findOrInsert(overview.prettyName(name)));
312 313 314 315 316 317 318

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

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