2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-11-28 15:36:54 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2008-11-28 15:36:54 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2008-11-28 15:36:54 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2008-11-28 15:36:54 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2008-11-28 15:36:54 +01:00
|
|
|
|
|
|
|
|
#include "searchsymbols.h"
|
2018-02-07 13:18:48 +01:00
|
|
|
#include "stringtable.h"
|
2008-11-28 15:36:54 +01:00
|
|
|
|
2016-04-06 10:08:01 +02:00
|
|
|
#include <cplusplus/Icons.h>
|
2010-07-19 14:46:53 +02:00
|
|
|
#include <cplusplus/LookupContext.h>
|
2014-03-13 16:53:17 +01:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
#include <utils/scopedswap.h>
|
2013-03-27 18:54:03 +01:00
|
|
|
|
2010-09-07 14:13:54 +02:00
|
|
|
#include <QDebug>
|
2008-11-28 15:36:54 +01:00
|
|
|
|
|
|
|
|
using namespace CPlusPlus;
|
2015-02-04 17:01:07 +02:00
|
|
|
|
|
|
|
|
namespace CppTools {
|
2008-11-28 15:36:54 +01:00
|
|
|
|
2014-04-14 16:52:01 +02:00
|
|
|
typedef Utils::ScopedSwap<IndexItem::Ptr> ScopedIndexItemPtr;
|
2014-03-13 16:53:17 +01:00
|
|
|
typedef Utils::ScopedSwap<QString> ScopedScope;
|
|
|
|
|
|
2010-07-19 14:46:53 +02:00
|
|
|
SearchSymbols::SymbolTypes SearchSymbols::AllTypes =
|
2012-11-23 11:47:39 +01:00
|
|
|
SymbolSearcher::Classes
|
|
|
|
|
| SymbolSearcher::Functions
|
|
|
|
|
| SymbolSearcher::Enums
|
|
|
|
|
| SymbolSearcher::Declarations;
|
2010-07-19 14:46:53 +02:00
|
|
|
|
2018-02-07 13:18:48 +01:00
|
|
|
SearchSymbols::SearchSymbols()
|
|
|
|
|
: symbolsToSearchFor(SymbolSearcher::Classes | SymbolSearcher::Functions | SymbolSearcher::Enums)
|
2008-11-28 15:36:54 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-19 18:32:38 +03:00
|
|
|
void SearchSymbols::setSymbolsToSearchFor(const SymbolTypes &types)
|
2008-11-28 15:36:54 +01:00
|
|
|
{
|
2008-12-05 09:02:08 +01:00
|
|
|
symbolsToSearchFor = types;
|
2008-11-28 15:36:54 +01:00
|
|
|
}
|
|
|
|
|
|
2014-04-14 16:52:01 +02:00
|
|
|
IndexItem::Ptr SearchSymbols::operator()(Document::Ptr doc, const QString &scope)
|
2008-11-28 15:36:54 +01:00
|
|
|
{
|
2018-02-07 13:18:48 +01:00
|
|
|
IndexItem::Ptr root = IndexItem::create(Internal::StringTable::insert(doc->fileName()), 100);
|
2014-03-13 16:53:17 +01:00
|
|
|
|
|
|
|
|
{ // RAII scope
|
2014-04-14 16:52:01 +02:00
|
|
|
ScopedIndexItemPtr parentRaii(_parent, root);
|
2014-03-13 16:53:17 +01:00
|
|
|
QString newScope = scope;
|
|
|
|
|
ScopedScope scopeRaii(_scope, newScope);
|
|
|
|
|
|
2014-04-14 16:52:01 +02:00
|
|
|
QTC_ASSERT(_parent, return IndexItem::Ptr());
|
|
|
|
|
QTC_ASSERT(root, return IndexItem::Ptr());
|
2018-02-07 13:18:48 +01:00
|
|
|
QTC_ASSERT(_parent->fileName() == Internal::StringTable::insert(doc->fileName()),
|
2014-04-14 16:52:01 +02:00
|
|
|
return IndexItem::Ptr());
|
2014-03-13 16:53:17 +01:00
|
|
|
|
|
|
|
|
for (unsigned i = 0, ei = doc->globalSymbolCount(); i != ei; ++i)
|
|
|
|
|
accept(doc->globalSymbolAt(i));
|
|
|
|
|
|
2018-02-07 13:18:48 +01:00
|
|
|
Internal::StringTable::scheduleGC();
|
2014-03-13 16:53:17 +01:00
|
|
|
m_paths.clear();
|
2008-11-28 15:36:54 +01:00
|
|
|
}
|
|
|
|
|
|
2014-03-13 16:53:17 +01:00
|
|
|
root->squeeze();
|
|
|
|
|
return root;
|
2008-11-28 15:36:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SearchSymbols::visit(Enum *symbol)
|
|
|
|
|
{
|
2012-11-23 11:47:39 +01:00
|
|
|
if (!(symbolsToSearchFor & SymbolSearcher::Enums))
|
2008-11-28 15:36:54 +01:00
|
|
|
return false;
|
|
|
|
|
|
2014-02-25 16:57:18 +01:00
|
|
|
QString name = overview.prettyName(symbol->name());
|
2014-04-14 16:52:01 +02:00
|
|
|
IndexItem::Ptr newParent = addChildItem(name, QString(), _scope, IndexItem::Enum, symbol);
|
2014-03-13 16:53:17 +01:00
|
|
|
if (!newParent)
|
|
|
|
|
newParent = _parent;
|
2014-04-14 16:52:01 +02:00
|
|
|
ScopedIndexItemPtr parentRaii(_parent, newParent);
|
2014-03-13 16:53:17 +01:00
|
|
|
|
|
|
|
|
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));
|
2014-03-13 16:53:17 +01:00
|
|
|
|
2008-11-28 15:36:54 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SearchSymbols::visit(Function *symbol)
|
|
|
|
|
{
|
2014-07-16 16:35:03 +02:00
|
|
|
processFunction(symbol);
|
2008-11-28 15:36:54 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SearchSymbols::visit(Namespace *symbol)
|
|
|
|
|
{
|
2010-09-07 14:13:54 +02:00
|
|
|
QString name = scopedSymbolName(symbol);
|
2014-03-13 16:53:17 +01:00
|
|
|
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));
|
2008-11-28 15:36:54 +01:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SearchSymbols::visit(Declaration *symbol)
|
|
|
|
|
{
|
2013-05-15 11:20:17 +02:00
|
|
|
if (!(symbolsToSearchFor & SymbolSearcher::Declarations)) {
|
2013-06-18 11:01:56 +02:00
|
|
|
// if we're searching for functions, still allow signal declarations to show up.
|
2013-05-15 11:20:17 +02:00
|
|
|
if (symbolsToSearchFor & SymbolSearcher::Functions) {
|
2013-06-18 11:01:56 +02:00
|
|
|
Function *funTy = symbol->type()->asFunctionType();
|
2014-07-16 16:35:03 +02:00
|
|
|
if (!funTy) {
|
|
|
|
|
if (!symbol->type()->asObjCMethodType())
|
|
|
|
|
return false;
|
|
|
|
|
} else if (!funTy->isSignal()) {
|
2013-05-15 11:20:17 +02:00
|
|
|
return false;
|
2014-07-16 16:35:03 +02:00
|
|
|
}
|
2013-05-15 11:20:17 +02:00
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-05-14 21:08:08 +02:00
|
|
|
|
2014-02-25 16:57:18 +01:00
|
|
|
if (symbol->name()) {
|
|
|
|
|
QString name = overview.prettyName(symbol->name());
|
|
|
|
|
QString type = overview.prettyType(symbol->type());
|
2014-03-13 16:53:17 +01:00
|
|
|
addChildItem(name, type, _scope,
|
2014-04-14 16:52:01 +02:00
|
|
|
symbol->type()->asFunctionType() ? IndexItem::Function
|
|
|
|
|
: IndexItem::Declaration,
|
2014-03-13 16:53:17 +01:00
|
|
|
symbol);
|
2014-02-25 16:57:18 +01:00
|
|
|
}
|
|
|
|
|
|
2008-11-28 15:36:54 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SearchSymbols::visit(Class *symbol)
|
|
|
|
|
{
|
2014-07-16 16:35:03 +02:00
|
|
|
processClass(symbol);
|
2014-03-13 16:53:17 +01:00
|
|
|
|
2008-11-28 15:36:54 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-04 17:01:07 +02:00
|
|
|
bool SearchSymbols::visit(UsingNamespaceDirective *)
|
2010-09-08 14:21:19 +02:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-04 17:01:07 +02:00
|
|
|
bool SearchSymbols::visit(UsingDeclaration *)
|
2010-09-08 14:21:19 +02:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-04 17:01:07 +02:00
|
|
|
bool SearchSymbols::visit(NamespaceAlias *)
|
2010-09-08 14:21:19 +02:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-04 17:01:07 +02:00
|
|
|
bool SearchSymbols::visit(Argument *)
|
2010-09-08 14:21:19 +02:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-04 17:01:07 +02:00
|
|
|
bool SearchSymbols::visit(TypenameArgument *)
|
2010-09-08 14:21:19 +02:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-04 17:01:07 +02:00
|
|
|
bool SearchSymbols::visit(BaseClass *)
|
2010-09-08 14:21:19 +02:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-04 17:01:07 +02:00
|
|
|
bool SearchSymbols::visit(Template *)
|
2010-09-08 14:21:19 +02:00
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-04 17:01:07 +02:00
|
|
|
bool SearchSymbols::visit(Block *)
|
2010-09-08 14:21:19 +02:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-04 17:01:07 +02:00
|
|
|
bool SearchSymbols::visit(ForwardClassDeclaration *)
|
2010-09-08 14:21:19 +02:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-04 17:01:07 +02:00
|
|
|
bool SearchSymbols::visit(ObjCBaseClass *)
|
2010-09-08 14:21:19 +02:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-04 17:01:07 +02:00
|
|
|
bool SearchSymbols::visit(ObjCBaseProtocol *)
|
2010-09-08 14:21:19 +02:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-04 17:01:07 +02:00
|
|
|
bool SearchSymbols::visit(ObjCClass *symbol)
|
2010-09-08 14:21:19 +02:00
|
|
|
{
|
2014-07-16 16:35:03 +02:00
|
|
|
processClass(symbol);
|
|
|
|
|
|
2010-09-08 14:21:19 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-04 17:01:07 +02:00
|
|
|
bool SearchSymbols::visit(ObjCForwardClassDeclaration *)
|
2010-09-08 14:21:19 +02:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-04 17:01:07 +02:00
|
|
|
bool SearchSymbols::visit(ObjCProtocol *symbol)
|
2010-09-08 14:21:19 +02:00
|
|
|
{
|
2014-07-16 16:35:03 +02:00
|
|
|
processClass(symbol);
|
|
|
|
|
|
2010-09-08 14:21:19 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-04 17:01:07 +02:00
|
|
|
bool SearchSymbols::visit(ObjCForwardProtocolDeclaration *)
|
2010-09-08 14:21:19 +02:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-04 17:01:07 +02:00
|
|
|
bool SearchSymbols::visit(ObjCMethod *symbol)
|
2010-09-08 14:21:19 +02:00
|
|
|
{
|
2014-07-16 16:35:03 +02:00
|
|
|
processFunction(symbol);
|
2010-09-08 14:21:19 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-04 17:01:07 +02:00
|
|
|
bool SearchSymbols::visit(ObjCPropertyDeclaration *symbol)
|
2010-09-08 14:21:19 +02:00
|
|
|
{
|
2014-07-16 16:35:03 +02:00
|
|
|
processFunction(symbol);
|
2010-09-08 14:21:19 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-25 16:57:18 +01:00
|
|
|
QString SearchSymbols::scopedSymbolName(const QString &symbolName, const Symbol *symbol) const
|
2008-11-28 15:36:54 +01:00
|
|
|
{
|
|
|
|
|
QString name = _scope;
|
2008-12-09 16:30:47 +01:00
|
|
|
if (!name.isEmpty())
|
2008-11-28 15:36:54 +01:00
|
|
|
name += QLatin1String("::");
|
2014-02-25 16:57:18 +01:00
|
|
|
name += scopeName(symbolName, symbol);
|
2008-12-05 12:21:18 +01:00
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString SearchSymbols::scopedSymbolName(const Symbol *symbol) const
|
|
|
|
|
{
|
2014-02-25 16:57:18 +01:00
|
|
|
return scopedSymbolName(overview.prettyName(symbol->name()), symbol);
|
2008-12-05 12:21:18 +01:00
|
|
|
}
|
|
|
|
|
|
2014-02-25 16:57:18 +01:00
|
|
|
QString SearchSymbols::scopeName(const QString &name, const Symbol *symbol) const
|
2008-12-05 12:21:18 +01:00
|
|
|
{
|
2014-02-25 16:57:18 +01:00
|
|
|
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>");
|
2008-11-28 15:36:54 +01:00
|
|
|
}
|
2008-12-05 12:21:18 +01:00
|
|
|
}
|
|
|
|
|
|
2014-04-14 16:52:01 +02:00
|
|
|
IndexItem::Ptr SearchSymbols::addChildItem(const QString &symbolName, const QString &symbolType,
|
|
|
|
|
const QString &symbolScope, IndexItem::ItemType itemType,
|
|
|
|
|
Symbol *symbol)
|
2008-12-05 12:21:18 +01:00
|
|
|
{
|
2013-08-16 12:09:50 +02:00
|
|
|
if (!symbol->name() || symbol->isGenerated())
|
2014-04-14 16:52:01 +02:00
|
|
|
return IndexItem::Ptr();
|
2008-12-09 16:30:47 +01:00
|
|
|
|
2010-09-06 16:46:23 +02:00
|
|
|
QString path = m_paths.value(symbol->fileId(), QString());
|
|
|
|
|
if (path.isEmpty()) {
|
|
|
|
|
path = QString::fromUtf8(symbol->fileName(), symbol->fileNameLength());
|
|
|
|
|
m_paths.insert(symbol->fileId(), path);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-06 10:08:01 +02:00
|
|
|
const QIcon icon = Icons::iconForSymbol(symbol);
|
2018-02-07 13:18:48 +01:00
|
|
|
|
|
|
|
|
IndexItem::Ptr newItem = IndexItem::create(Internal::StringTable::insert(symbolName),
|
|
|
|
|
Internal::StringTable::insert(symbolType),
|
|
|
|
|
Internal::StringTable::insert(symbolScope),
|
2014-04-14 16:52:01 +02:00
|
|
|
itemType,
|
2018-02-07 13:18:48 +01:00
|
|
|
Internal::StringTable::insert(path),
|
2014-04-14 16:52:01 +02:00
|
|
|
symbol->line(),
|
|
|
|
|
symbol->column() - 1, // 1-based vs 0-based column
|
|
|
|
|
icon);
|
2014-03-13 16:53:17 +01:00
|
|
|
_parent->addChild(newItem);
|
|
|
|
|
return newItem;
|
|
|
|
|
}
|
2014-07-16 16:35:03 +02:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2015-02-04 17:01:07 +02:00
|
|
|
|
|
|
|
|
} // namespace CppTools
|