Files
qt-creator/src/plugins/cpptools/searchsymbols.cpp

339 lines
9.3 KiB
C++
Raw Normal View History

/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
2010-12-17 16:01:08 +01:00
**
****************************************************************************/
#include "searchsymbols.h"
#include "stringtable.h"
#include <cplusplus/Icons.h>
#include <cplusplus/LookupContext.h>
#include <utils/qtcassert.h>
#include <utils/scopedswap.h>
2010-09-07 14:13:54 +02:00
#include <QDebug>
using namespace CPlusPlus;
namespace CppTools {
typedef Utils::ScopedSwap<IndexItem::Ptr> ScopedIndexItemPtr;
typedef Utils::ScopedSwap<QString> ScopedScope;
SearchSymbols::SymbolTypes SearchSymbols::AllTypes =
SymbolSearcher::Classes
| SymbolSearcher::Functions
| SymbolSearcher::Enums
| SymbolSearcher::Declarations;
SearchSymbols::SearchSymbols()
: symbolsToSearchFor(SymbolSearcher::Classes | SymbolSearcher::Functions | SymbolSearcher::Enums)
{
}
void SearchSymbols::setSymbolsToSearchFor(const SymbolTypes &types)
{
symbolsToSearchFor = types;
}
IndexItem::Ptr SearchSymbols::operator()(Document::Ptr doc, const QString &scope)
{
IndexItem::Ptr root = IndexItem::create(Internal::StringTable::insert(doc->fileName()), 100);
{ // RAII scope
ScopedIndexItemPtr parentRaii(_parent, root);
QString newScope = scope;
ScopedScope scopeRaii(_scope, newScope);
QTC_ASSERT(_parent, return IndexItem::Ptr());
QTC_ASSERT(root, return IndexItem::Ptr());
QTC_ASSERT(_parent->fileName() == Internal::StringTable::insert(doc->fileName()),
return IndexItem::Ptr());
for (unsigned i = 0, ei = doc->globalSymbolCount(); i != ei; ++i)
accept(doc->globalSymbolAt(i));
Internal::StringTable::scheduleGC();
m_paths.clear();
}
root->squeeze();
return root;
}
bool SearchSymbols::visit(Enum *symbol)
{
if (!(symbolsToSearchFor & SymbolSearcher::Enums))
return false;
QString name = overview.prettyName(symbol->name());
IndexItem::Ptr newParent = addChildItem(name, QString(), _scope, IndexItem::Enum, symbol);
if (!newParent)
newParent = _parent;
ScopedIndexItemPtr parentRaii(_parent, newParent);
QString newScope = scopedSymbolName(name, symbol);
ScopedScope scopeRaii(_scope, newScope);
for (unsigned i = 0, ei = symbol->memberCount(); i != ei; ++i)
2010-08-11 12:26:02 +02:00
accept(symbol->memberAt(i));
return false;
}
bool SearchSymbols::visit(Function *symbol)
{
processFunction(symbol);
return false;
}
bool SearchSymbols::visit(Namespace *symbol)
{
2010-09-07 14:13:54 +02:00
QString name = scopedSymbolName(symbol);
QString newScope = name;
ScopedScope raii(_scope, newScope);
2010-08-11 12:26:02 +02:00
for (unsigned i = 0; i < symbol->memberCount(); ++i) {
accept(symbol->memberAt(i));
}
return false;
}
bool SearchSymbols::visit(Declaration *symbol)
{
if (!(symbolsToSearchFor & SymbolSearcher::Declarations)) {
// if we're searching for functions, still allow signal declarations to show up.
if (symbolsToSearchFor & SymbolSearcher::Functions) {
Function *funTy = symbol->type()->asFunctionType();
if (!funTy) {
if (!symbol->type()->asObjCMethodType())
return false;
} else if (!funTy->isSignal()) {
return false;
}
} else {
return false;
}
}
if (symbol->name()) {
QString name = overview.prettyName(symbol->name());
QString type = overview.prettyType(symbol->type());
addChildItem(name, type, _scope,
symbol->type()->asFunctionType() ? IndexItem::Function
: IndexItem::Declaration,
symbol);
}
return false;
}
bool SearchSymbols::visit(Class *symbol)
{
processClass(symbol);
return false;
}
bool SearchSymbols::visit(UsingNamespaceDirective *)
2010-09-08 14:21:19 +02:00
{
return false;
}
bool SearchSymbols::visit(UsingDeclaration *)
2010-09-08 14:21:19 +02:00
{
return false;
}
bool SearchSymbols::visit(NamespaceAlias *)
2010-09-08 14:21:19 +02:00
{
return false;
}
bool SearchSymbols::visit(Argument *)
2010-09-08 14:21:19 +02:00
{
return false;
}
bool SearchSymbols::visit(TypenameArgument *)
2010-09-08 14:21:19 +02:00
{
return false;
}
bool SearchSymbols::visit(BaseClass *)
2010-09-08 14:21:19 +02:00
{
return false;
}
bool SearchSymbols::visit(Template *)
2010-09-08 14:21:19 +02:00
{
return true;
}
bool SearchSymbols::visit(Block *)
2010-09-08 14:21:19 +02:00
{
return false;
}
bool SearchSymbols::visit(ForwardClassDeclaration *)
2010-09-08 14:21:19 +02:00
{
return false;
}
bool SearchSymbols::visit(ObjCBaseClass *)
2010-09-08 14:21:19 +02:00
{
return false;
}
bool SearchSymbols::visit(ObjCBaseProtocol *)
2010-09-08 14:21:19 +02:00
{
return false;
}
bool SearchSymbols::visit(ObjCClass *symbol)
2010-09-08 14:21:19 +02:00
{
processClass(symbol);
2010-09-08 14:21:19 +02:00
return false;
}
bool SearchSymbols::visit(ObjCForwardClassDeclaration *)
2010-09-08 14:21:19 +02:00
{
return false;
}
bool SearchSymbols::visit(ObjCProtocol *symbol)
2010-09-08 14:21:19 +02:00
{
processClass(symbol);
2010-09-08 14:21:19 +02:00
return false;
}
bool SearchSymbols::visit(ObjCForwardProtocolDeclaration *)
2010-09-08 14:21:19 +02:00
{
return false;
}
bool SearchSymbols::visit(ObjCMethod *symbol)
2010-09-08 14:21:19 +02:00
{
processFunction(symbol);
2010-09-08 14:21:19 +02:00
return false;
}
bool SearchSymbols::visit(ObjCPropertyDeclaration *symbol)
2010-09-08 14:21:19 +02:00
{
processFunction(symbol);
2010-09-08 14:21:19 +02:00
return false;
}
QString SearchSymbols::scopedSymbolName(const QString &symbolName, const Symbol *symbol) const
{
QString name = _scope;
if (!name.isEmpty())
name += QLatin1String("::");
name += scopeName(symbolName, symbol);
return name;
}
QString SearchSymbols::scopedSymbolName(const Symbol *symbol) const
{
return scopedSymbolName(overview.prettyName(symbol->name()), symbol);
}
QString SearchSymbols::scopeName(const QString &name, const Symbol *symbol) const
{
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>");
}
}
IndexItem::Ptr SearchSymbols::addChildItem(const QString &symbolName, const QString &symbolType,
const QString &symbolScope, IndexItem::ItemType itemType,
Symbol *symbol)
{
if (!symbol->name() || symbol->isGenerated())
return IndexItem::Ptr();
QString path = m_paths.value(symbol->fileId(), QString());
if (path.isEmpty()) {
path = QString::fromUtf8(symbol->fileName(), symbol->fileNameLength());
m_paths.insert(symbol->fileId(), path);
}
const QIcon icon = Icons::iconForSymbol(symbol);
IndexItem::Ptr newItem = IndexItem::create(Internal::StringTable::insert(symbolName),
Internal::StringTable::insert(symbolType),
Internal::StringTable::insert(symbolScope),
itemType,
Internal::StringTable::insert(path),
symbol->line(),
symbol->column() - 1, // 1-based vs 0-based column
icon);
_parent->addChild(newItem);
return newItem;
}
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);
}
} // namespace CppTools