2018-02-06 08:19:42 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2018 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.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "clangoverviewmodel.h"
|
|
|
|
|
|
|
|
|
|
#include "clangeditordocumentprocessor.h"
|
|
|
|
|
#include "clangutils.h"
|
|
|
|
|
|
|
|
|
|
#include <cplusplus/Icons.h>
|
|
|
|
|
|
|
|
|
|
#include <utils/dropsupport.h>
|
|
|
|
|
#include <utils/linecolumn.h>
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
|
|
|
|
using TokenContainer = ClangBackEnd::TokenInfoContainer;
|
|
|
|
|
using TokenContainers = QVector<TokenContainer>;
|
|
|
|
|
|
|
|
|
|
namespace ClangCodeModel {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2018-04-06 14:24:28 +02:00
|
|
|
void addFirstItem(TokenTreeItem *root)
|
2018-02-06 08:19:42 +01:00
|
|
|
{
|
2018-04-06 14:24:28 +02:00
|
|
|
ClangBackEnd::ExtraInfo extraInfo;
|
|
|
|
|
if (!root->childCount()) {
|
|
|
|
|
extraInfo.token = Utf8String::fromString(
|
|
|
|
|
QString(QT_TRANSLATE_NOOP("ClangCodeModel", "<No Symbols>")));
|
|
|
|
|
} else {
|
|
|
|
|
extraInfo.token = Utf8String::fromString(
|
|
|
|
|
QString(QT_TRANSLATE_NOOP("ClangCodeModel", "<Select Symbol>")));
|
|
|
|
|
}
|
|
|
|
|
ClangBackEnd::HighlightingTypes types;
|
|
|
|
|
types.mainHighlightingType = ClangBackEnd::HighlightingType::Invalid;
|
|
|
|
|
TokenContainer firstItem(0, 0, 0, types, extraInfo);
|
|
|
|
|
root->prependChild(new TokenTreeItem(firstItem));
|
2018-02-06 08:19:42 +01:00
|
|
|
}
|
|
|
|
|
|
2018-04-06 14:24:28 +02:00
|
|
|
void buildTree(const TokenContainers &containers,
|
|
|
|
|
TokenTreeItem *root)
|
2018-02-06 08:19:42 +01:00
|
|
|
{
|
2018-04-06 14:24:28 +02:00
|
|
|
// Most of the nodes are not used in this tree at all (all local variables and more)
|
|
|
|
|
// therefore use unordered_map instead of vector.
|
|
|
|
|
std::unordered_map<int, TokenTreeItem *> treeItemCache;
|
|
|
|
|
for (int index = 0; index < containers.size(); ++index) {
|
|
|
|
|
const TokenContainer &container = containers[index];
|
2018-06-05 13:37:02 +02:00
|
|
|
if (!container.isGlobalDeclaration())
|
2018-02-06 08:19:42 +01:00
|
|
|
continue;
|
|
|
|
|
|
2018-04-06 14:24:28 +02:00
|
|
|
const int lexicalParentIndex = container.extraInfo.lexicalParentIndex;
|
|
|
|
|
QTC_ASSERT(lexicalParentIndex < index, return;);
|
2018-02-06 08:19:42 +01:00
|
|
|
|
2018-04-06 14:24:28 +02:00
|
|
|
auto item = std::make_unique<TokenTreeItem>(container);
|
|
|
|
|
treeItemCache[index] = item.get();
|
|
|
|
|
|
|
|
|
|
TokenTreeItem *parent = root;
|
|
|
|
|
if (lexicalParentIndex >= 0 && treeItemCache[lexicalParentIndex])
|
|
|
|
|
parent = treeItemCache[lexicalParentIndex];
|
|
|
|
|
|
2018-06-05 13:20:19 +02:00
|
|
|
ClangBackEnd::HighlightingType parentType = parent->token.types.mainHighlightingType;
|
|
|
|
|
if (parentType == ClangBackEnd::HighlightingType::VirtualFunction
|
|
|
|
|
|| parentType == ClangBackEnd::HighlightingType::Function) {
|
|
|
|
|
// Treat everything inside a function scope as local variables.
|
2018-06-11 11:07:58 +02:00
|
|
|
treeItemCache.erase(index);
|
2018-06-05 13:20:19 +02:00
|
|
|
continue;
|
2018-06-01 09:22:03 +02:00
|
|
|
}
|
|
|
|
|
|
2018-04-06 14:24:28 +02:00
|
|
|
parent->appendChild(item.release());
|
2018-02-06 08:19:42 +01:00
|
|
|
}
|
2018-04-06 14:24:28 +02:00
|
|
|
|
|
|
|
|
addFirstItem(root);
|
2018-02-06 08:19:42 +01:00
|
|
|
}
|
|
|
|
|
|
2018-04-19 14:59:22 +02:00
|
|
|
static QString addType(const QString &name, const ClangBackEnd::ExtraInfo &extraInfo)
|
2018-02-06 08:19:42 +01:00
|
|
|
{
|
|
|
|
|
return name + QLatin1String(" -> ", 4) + extraInfo.typeSpelling.toString();
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-21 08:28:50 +02:00
|
|
|
static QString fullName(const ClangBackEnd::ExtraInfo &extraInfo, TokenTreeItem *parent)
|
|
|
|
|
{
|
|
|
|
|
const QString parentType = parent->token.extraInfo.typeSpelling.toString();
|
|
|
|
|
if (extraInfo.semanticParentTypeSpelling.startsWith(parentType)) {
|
|
|
|
|
const QString parentQualification = parentType.isEmpty()
|
|
|
|
|
? extraInfo.semanticParentTypeSpelling
|
|
|
|
|
: extraInfo.semanticParentTypeSpelling.mid(parentType.length() + 2);
|
|
|
|
|
if (!parentQualification.isEmpty())
|
|
|
|
|
return parentQualification + "::" + extraInfo.token.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return extraInfo.token.toString();
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-06 08:19:42 +01:00
|
|
|
QVariant TokenTreeItem::data(int column, int role) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(column)
|
|
|
|
|
|
2018-04-04 18:25:23 +02:00
|
|
|
if (token.types.mainHighlightingType == ClangBackEnd::HighlightingType::Invalid
|
|
|
|
|
&& token.line == 0 && token.column == 0 && token.length == 0) {
|
2018-02-06 08:19:42 +01:00
|
|
|
if (role == Qt::DisplayRole)
|
2018-04-04 18:25:23 +02:00
|
|
|
return token.extraInfo.token.toString();
|
2018-02-06 08:19:42 +01:00
|
|
|
return QVariant();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (role) {
|
|
|
|
|
case Qt::DisplayRole: {
|
2018-06-21 08:28:50 +02:00
|
|
|
QString name = fullName(token.extraInfo, static_cast<TokenTreeItem *>(parent()));
|
|
|
|
|
|
2018-04-04 18:25:23 +02:00
|
|
|
ClangBackEnd::HighlightingType mainType = token.types.mainHighlightingType;
|
2018-02-06 08:19:42 +01:00
|
|
|
|
|
|
|
|
if (mainType == ClangBackEnd::HighlightingType::VirtualFunction
|
|
|
|
|
|| mainType == ClangBackEnd::HighlightingType::Function) {
|
2018-04-19 14:59:22 +02:00
|
|
|
name = addType(name, token.extraInfo);
|
2018-02-06 08:19:42 +01:00
|
|
|
} else if (mainType == ClangBackEnd::HighlightingType::GlobalVariable
|
|
|
|
|
|| mainType == ClangBackEnd::HighlightingType::Field
|
|
|
|
|
|| mainType == ClangBackEnd::HighlightingType::QtProperty) {
|
2018-04-19 14:59:22 +02:00
|
|
|
name = addType(name, token.extraInfo);
|
2018-04-04 18:25:23 +02:00
|
|
|
if (token.types.mixinHighlightingTypes.contains(
|
2018-02-06 08:19:42 +01:00
|
|
|
ClangBackEnd::HighlightingType::ObjectiveCProperty)) {
|
|
|
|
|
name = QLatin1String("@property ") + name;
|
2018-04-04 18:25:23 +02:00
|
|
|
} else if (token.types.mixinHighlightingTypes.contains(
|
2018-02-06 08:19:42 +01:00
|
|
|
ClangBackEnd::HighlightingType::ObjectiveCMethod)) {
|
2018-04-04 18:25:23 +02:00
|
|
|
if (token.extraInfo.storageClass == ClangBackEnd::StorageClass::Static)
|
2018-02-06 08:19:42 +01:00
|
|
|
name = QLatin1Char('+') + name;
|
|
|
|
|
else
|
|
|
|
|
name = QLatin1Char('-') + name;
|
|
|
|
|
}
|
|
|
|
|
} else if (mainType == ClangBackEnd::HighlightingType::Type) {
|
|
|
|
|
|
2018-04-04 18:25:23 +02:00
|
|
|
if (token.types.mixinHighlightingTypes.contains(
|
2018-02-06 08:19:42 +01:00
|
|
|
ClangBackEnd::HighlightingType::ObjectiveCClass)) {
|
|
|
|
|
name = QLatin1String("@class ") + name;
|
2018-04-04 18:25:23 +02:00
|
|
|
} else if (token.types.mixinHighlightingTypes.contains(
|
2018-02-06 08:19:42 +01:00
|
|
|
ClangBackEnd::HighlightingType::ObjectiveCProtocol)) {
|
|
|
|
|
name = QLatin1String("@protocol ") + name;
|
2018-04-04 18:25:23 +02:00
|
|
|
} else if (token.types.mixinHighlightingTypes.contains(
|
2018-02-06 08:19:42 +01:00
|
|
|
ClangBackEnd::HighlightingType::ObjectiveCInterface)) {
|
|
|
|
|
name = QLatin1String("@interface ") + name;
|
2018-04-04 18:25:23 +02:00
|
|
|
} else if (token.types.mixinHighlightingTypes.contains(
|
2018-02-06 08:19:42 +01:00
|
|
|
ClangBackEnd::HighlightingType::ObjectiveCImplementation)) {
|
|
|
|
|
name = QLatin1String("@implementation ") + name;
|
2018-04-04 18:25:23 +02:00
|
|
|
} else if (token.types.mixinHighlightingTypes.contains(
|
2018-02-06 08:19:42 +01:00
|
|
|
ClangBackEnd::HighlightingType::ObjectiveCCategory)) {
|
|
|
|
|
name = name + " [category]";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case Qt::EditRole: {
|
2018-04-04 18:25:23 +02:00
|
|
|
return token.extraInfo.token.toString();
|
2018-02-06 08:19:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case Qt::DecorationRole: {
|
2018-07-25 10:00:38 +02:00
|
|
|
return ::Utils::CodeModelIcon::iconForType(ClangCodeModel::Utils::iconTypeForToken(token));
|
2018-02-06 08:19:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case CppTools::AbstractOverviewModel::FileNameRole: {
|
2018-04-04 18:25:23 +02:00
|
|
|
return token.extraInfo.cursorRange.start.filePath.toString();
|
2018-02-06 08:19:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case CppTools::AbstractOverviewModel::LineNumberRole: {
|
2018-04-04 18:25:23 +02:00
|
|
|
return token.line;
|
2018-02-06 08:19:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return QVariant();
|
|
|
|
|
} // switch
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool OverviewModel::rebuild(const QString &filePath)
|
|
|
|
|
{
|
|
|
|
|
ClangEditorDocumentProcessor *processor = ClangEditorDocumentProcessor::get(filePath);
|
|
|
|
|
if (!processor)
|
|
|
|
|
return false;
|
2018-03-22 13:05:59 +01:00
|
|
|
if (m_filePath != filePath) {
|
|
|
|
|
if (!m_filePath.isEmpty()) {
|
|
|
|
|
ClangEditorDocumentProcessor *previousProcessor
|
|
|
|
|
= ClangEditorDocumentProcessor::get(m_filePath);
|
|
|
|
|
if (previousProcessor) {
|
|
|
|
|
disconnect(previousProcessor, &ClangEditorDocumentProcessor::tokenInfosUpdated,
|
|
|
|
|
this, &OverviewModel::needsUpdate);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
m_filePath = filePath;
|
|
|
|
|
connect(processor, &ClangEditorDocumentProcessor::tokenInfosUpdated, this,
|
|
|
|
|
&OverviewModel::needsUpdate);
|
|
|
|
|
}
|
2018-02-06 08:19:42 +01:00
|
|
|
|
|
|
|
|
const TokenContainers &tokenContainers = processor->tokenInfos();
|
|
|
|
|
auto *root = new TokenTreeItem;
|
2018-04-06 14:24:28 +02:00
|
|
|
buildTree(tokenContainers, root);
|
2018-02-06 08:19:42 +01:00
|
|
|
setRootItem(root);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool OverviewModel::isGenerated(const QModelIndex &) const
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
::Utils::Link OverviewModel::linkFromIndex(const QModelIndex &sourceIndex) const
|
|
|
|
|
{
|
|
|
|
|
TokenTreeItem *item = static_cast<TokenTreeItem *>(itemForIndex(sourceIndex));
|
|
|
|
|
if (!item)
|
|
|
|
|
return {};
|
2018-04-04 18:25:23 +02:00
|
|
|
return ::Utils::Link(m_filePath, static_cast<int>(item->token.line),
|
|
|
|
|
static_cast<int>(item->token.column) - 1);
|
2018-02-06 08:19:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
::Utils::LineColumn OverviewModel::lineColumnFromIndex(const QModelIndex &sourceIndex) const
|
|
|
|
|
{
|
|
|
|
|
TokenTreeItem *item = static_cast<TokenTreeItem *>(itemForIndex(sourceIndex));
|
|
|
|
|
if (!item)
|
|
|
|
|
return {};
|
2018-06-01 09:18:45 +02:00
|
|
|
return ::Utils::LineColumn(static_cast<int>(item->token.line),
|
|
|
|
|
static_cast<int>(item->token.column));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OverviewModel::Range OverviewModel::rangeFromIndex(const QModelIndex &sourceIndex) const
|
|
|
|
|
{
|
|
|
|
|
TokenTreeItem *item = static_cast<TokenTreeItem *>(itemForIndex(sourceIndex));
|
|
|
|
|
if (!item)
|
|
|
|
|
return {};
|
|
|
|
|
const ClangBackEnd::SourceRangeContainer &range = item->token.extraInfo.cursorRange;
|
|
|
|
|
return std::make_pair(::Utils::LineColumn(static_cast<int>(range.start.line),
|
|
|
|
|
static_cast<int>(range.start.column)),
|
|
|
|
|
::Utils::LineColumn(static_cast<int>(range.end.line),
|
|
|
|
|
static_cast<int>(range.end.column)));
|
2018-02-06 08:19:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace ClangCodeModel
|