searchsymbols.cpp 9.59 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 34
#include <utils/qtcassert.h>
#include <utils/scopedswap.h>
35

Roberto Raggi's avatar
Roberto Raggi committed
36
#include <QDebug>
37 38

using namespace CPlusPlus;
39
using namespace CppTools;
40

41
typedef Utils::ScopedSwap<IndexItem::Ptr> ScopedIndexItemPtr;
42 43
typedef Utils::ScopedSwap<QString> ScopedScope;

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

50
SearchSymbols::SearchSymbols(CppTools::Internal::StringTable &stringTable)
51 52
    : strings(stringTable)
    , symbolsToSearchFor(SymbolSearcher::Classes | SymbolSearcher::Functions | SymbolSearcher::Enums)
53 54 55
{
}

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

61
IndexItem::Ptr SearchSymbols::operator()(Document::Ptr doc, const QString &scope)
62
{
63
    IndexItem::Ptr root = IndexItem::create(findOrInsert(doc->fileName()), 100);
64 65

    { // RAII scope
66
        ScopedIndexItemPtr parentRaii(_parent, root);
67 68 69
        QString newScope = scope;
        ScopedScope scopeRaii(_scope, newScope);

70 71
        QTC_ASSERT(_parent, return IndexItem::Ptr());
        QTC_ASSERT(root, return IndexItem::Ptr());
72
        QTC_ASSERT(_parent->fileName() == findOrInsert(doc->fileName()),
73
                   return IndexItem::Ptr());
74 75 76 77 78 79

        for (unsigned i = 0, ei = doc->globalSymbolCount(); i != ei; ++i)
            accept(doc->globalSymbolAt(i));

        strings.scheduleGC();
        m_paths.clear();
80 81
    }

82 83
    root->squeeze();
    return root;
84 85 86 87
}

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

91
    QString name = overview.prettyName(symbol->name());
92
    IndexItem::Ptr newParent = addChildItem(name, QString(), _scope, IndexItem::Enum, symbol);
93 94
    if (!newParent)
        newParent = _parent;
95
    ScopedIndexItemPtr parentRaii(_parent, newParent);
96 97 98 99 100

    QString newScope = scopedSymbolName(name, symbol);
    ScopedScope scopeRaii(_scope, newScope);

    for (unsigned i = 0, ei = symbol->memberCount(); i != ei; ++i)
101
        accept(symbol->memberAt(i));
102

103 104 105 106 107
    return false;
}

bool SearchSymbols::visit(Function *symbol)
{
108
    processFunction(symbol);
109 110 111 112 113
    return false;
}

bool SearchSymbols::visit(Namespace *symbol)
{
Roberto Raggi's avatar
Roberto Raggi committed
114
    QString name = scopedSymbolName(symbol);
115 116
    QString newScope = name;
    ScopedScope raii(_scope, newScope);
117 118
    for (unsigned i = 0; i < symbol->memberCount(); ++i) {
        accept(symbol->memberAt(i));
119 120 121 122 123 124
    }
    return false;
}

bool SearchSymbols::visit(Declaration *symbol)
{
125
    if (!(symbolsToSearchFor & SymbolSearcher::Declarations)) {
126
        // if we're searching for functions, still allow signal declarations to show up.
127
        if (symbolsToSearchFor & SymbolSearcher::Functions) {
128
            Function *funTy = symbol->type()->asFunctionType();
129 130 131 132
            if (!funTy) {
                if (!symbol->type()->asObjCMethodType())
                    return false;
            } else if (!funTy->isSignal()) {
133
                return false;
134
            }
135 136 137 138
        } else {
            return false;
        }
    }
139

140 141 142
    if (symbol->name()) {
        QString name = overview.prettyName(symbol->name());
        QString type = overview.prettyType(symbol->type());
143
        addChildItem(name, type, _scope,
144 145
                     symbol->type()->asFunctionType() ? IndexItem::Function
                                                      : IndexItem::Declaration,
146
                     symbol);
147 148
    }

149 150 151 152 153
    return false;
}

bool SearchSymbols::visit(Class *symbol)
{
154
    processClass(symbol);
155

156 157 158
    return false;
}

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

214
bool SearchSymbols::visit(CPlusPlus::ObjCClass *symbol)
Roberto Raggi's avatar
Roberto Raggi committed
215
{
216 217
    processClass(symbol);

Roberto Raggi's avatar
Roberto Raggi committed
218 219 220 221 222 223 224 225
    return false;
}

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

226
bool SearchSymbols::visit(CPlusPlus::ObjCProtocol *symbol)
Roberto Raggi's avatar
Roberto Raggi committed
227
{
228 229
    processClass(symbol);

Roberto Raggi's avatar
Roberto Raggi committed
230 231 232 233 234 235 236 237
    return false;
}

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

238
bool SearchSymbols::visit(CPlusPlus::ObjCMethod *symbol)
Roberto Raggi's avatar
Roberto Raggi committed
239
{
240
    processFunction(symbol);
Roberto Raggi's avatar
Roberto Raggi committed
241 242 243
    return false;
}

244
bool SearchSymbols::visit(CPlusPlus::ObjCPropertyDeclaration *symbol)
Roberto Raggi's avatar
Roberto Raggi committed
245
{
246
    processFunction(symbol);
Roberto Raggi's avatar
Roberto Raggi committed
247 248 249
    return false;
}

250
QString SearchSymbols::scopedSymbolName(const QString &symbolName, const Symbol *symbol) const
251 252
{
    QString name = _scope;
253
    if (!name.isEmpty())
254
        name += QLatin1String("::");
255
    name += scopeName(symbolName, symbol);
256 257 258 259 260
    return name;
}

QString SearchSymbols::scopedSymbolName(const Symbol *symbol) const
{
261
    return scopedSymbolName(overview.prettyName(symbol->name()), symbol);
262 263
}

264
QString SearchSymbols::scopeName(const QString &name, const Symbol *symbol) const
265
{
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    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>");
282
    }
283 284
}

285 286 287
IndexItem::Ptr SearchSymbols::addChildItem(const QString &symbolName, const QString &symbolType,
                                           const QString &symbolScope, IndexItem::ItemType itemType,
                                           Symbol *symbol)
288
{
289
    if (!symbol->name() || symbol->isGenerated())
290
        return IndexItem::Ptr();
291

292 293 294 295 296 297
    QString path = m_paths.value(symbol->fileId(), QString());
    if (path.isEmpty()) {
        path = QString::fromUtf8(symbol->fileName(), symbol->fileNameLength());
        m_paths.insert(symbol->fileId(), path);
    }

298
    const QIcon icon = icons.iconForSymbol(symbol);
299 300 301 302 303 304 305 306
    IndexItem::Ptr newItem = IndexItem::create(findOrInsert(symbolName),
                                               findOrInsert(symbolType),
                                               findOrInsert(symbolScope),
                                               itemType,
                                               findOrInsert(path),
                                               symbol->line(),
                                               symbol->column() - 1, // 1-based vs 0-based column
                                               icon);
307 308 309
    _parent->addChild(newItem);
    return newItem;
}
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337

template<class T>
void SearchSymbols::processClass(T *clazz)
{
    QString name = overview.prettyName(clazz->name());

    IndexItem::Ptr newParent;
    if (symbolsToSearchFor & SymbolSearcher::Classes)
        newParent = addChildItem(name, QString(), _scope, IndexItem::Class, clazz);
    if (!newParent)
        newParent = _parent;
    ScopedIndexItemPtr parentRaii(_parent, newParent);

    QString newScope = scopedSymbolName(name, clazz);
    ScopedScope scopeRaii(_scope, newScope);
    for (unsigned i = 0, ei = clazz->memberCount(); i != ei; ++i)
        accept(clazz->memberAt(i));
}

template<class T>
void SearchSymbols::processFunction(T *func)
{
    if (!(symbolsToSearchFor & SymbolSearcher::Functions) || !func->name())
        return;
    QString name = overview.prettyName(func->name());
    QString type = overview.prettyType(func->type());
    addChildItem(name, type, _scope, IndexItem::Function, func);
}