2016-01-15 14:57:40 +01:00
|
|
|
/****************************************************************************
|
2010-07-16 11:18:30 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 Denis Mingulov
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2010-07-16 11:18:30 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-07-16 11:18:30 +02: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.
|
2010-07-16 11:18:30 +02: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
|
|
|
****************************************************************************/
|
2010-07-16 11:18:30 +02:00
|
|
|
|
|
|
|
|
#include "classviewparser.h"
|
|
|
|
|
#include "classviewconstants.h"
|
|
|
|
|
#include "classviewutils.h"
|
|
|
|
|
|
|
|
|
|
// cplusplus shared library. the same folder (cplusplus)
|
2013-03-27 18:54:03 +01:00
|
|
|
#include <cplusplus/Symbol.h>
|
|
|
|
|
#include <cplusplus/Symbols.h>
|
|
|
|
|
#include <cplusplus/Scope.h>
|
|
|
|
|
#include <cplusplus/Name.h>
|
2010-07-16 11:18:30 +02:00
|
|
|
|
|
|
|
|
// other
|
2014-09-15 00:12:27 +02:00
|
|
|
#include <cpptools/cppmodelmanager.h>
|
2010-07-16 11:18:30 +02:00
|
|
|
#include <cplusplus/Overview.h>
|
|
|
|
|
#include <cplusplus/Icons.h>
|
|
|
|
|
#include <projectexplorer/projectexplorer.h>
|
|
|
|
|
#include <projectexplorer/session.h>
|
|
|
|
|
#include <projectexplorer/project.h>
|
|
|
|
|
#include <projectexplorer/projectnodes.h>
|
2017-12-06 10:27:27 +01:00
|
|
|
|
|
|
|
|
#include <utils/algorithm.h>
|
2010-07-16 11:18:30 +02:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QStandardItem>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QHash>
|
|
|
|
|
#include <QSet>
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
#include <QReadWriteLock>
|
|
|
|
|
#include <QReadLocker>
|
|
|
|
|
#include <QWriteLocker>
|
2019-07-05 13:43:54 +02:00
|
|
|
#include <QElapsedTimer>
|
2010-07-16 11:18:30 +02:00
|
|
|
|
|
|
|
|
enum { debug = false };
|
|
|
|
|
|
2013-09-05 11:46:07 +02:00
|
|
|
using namespace ProjectExplorer;
|
2019-11-15 18:42:11 +01:00
|
|
|
using namespace Utils;
|
2013-09-05 11:46:07 +02:00
|
|
|
|
2010-07-16 11:18:30 +02:00
|
|
|
namespace ClassView {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
// ----------------------------- ParserPrivate ---------------------------------
|
|
|
|
|
|
|
|
|
|
/*!
|
2013-05-24 17:35:14 +02:00
|
|
|
\class ParserPrivate
|
|
|
|
|
\brief The ParserPrivate class defines private class data for the Parser
|
|
|
|
|
class.
|
2010-07-16 11:18:30 +02:00
|
|
|
\sa Parser
|
|
|
|
|
*/
|
2013-05-24 17:35:14 +02:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
\class Parser
|
|
|
|
|
\brief The Parser class parses C++ information. Multithreading is supported.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
\fn void Parser::treeDataUpdate(QSharedPointer<QStandardItem> result)
|
|
|
|
|
|
|
|
|
|
Emits a signal about a tree data update.
|
|
|
|
|
*/
|
|
|
|
|
|
2011-07-06 17:40:54 +02:00
|
|
|
class ParserPrivate
|
2010-07-16 11:18:30 +02:00
|
|
|
{
|
2011-07-06 17:40:54 +02:00
|
|
|
public:
|
2021-02-02 16:16:39 +01:00
|
|
|
// Keep timer as a child of Parser in order to move it together with its parent
|
|
|
|
|
// into another thread.
|
|
|
|
|
ParserPrivate(QObject *parent) : timer(parent) {}
|
2018-11-04 22:40:59 +01:00
|
|
|
using CitDocumentList = QHash<QString, CPlusPlus::Document::Ptr>::const_iterator;
|
2010-07-16 11:18:30 +02:00
|
|
|
|
|
|
|
|
//! Get document from documentList
|
|
|
|
|
CPlusPlus::Document::Ptr document(const QString &fileName) const;
|
|
|
|
|
|
|
|
|
|
CPlusPlus::Overview overview;
|
|
|
|
|
|
|
|
|
|
//! timer
|
2017-04-05 17:46:55 +02:00
|
|
|
QTimer timer;
|
2010-07-16 11:18:30 +02:00
|
|
|
|
|
|
|
|
// documents
|
|
|
|
|
//! Documents read write lock
|
|
|
|
|
QReadWriteLock docLocker;
|
|
|
|
|
|
|
|
|
|
//! Current document list
|
|
|
|
|
QHash<QString, CPlusPlus::Document::Ptr> documentList;
|
|
|
|
|
|
|
|
|
|
//! Parsed documents' revision - to speed up computations
|
|
|
|
|
QHash<QString, unsigned> cachedDocTreesRevision;
|
|
|
|
|
|
|
|
|
|
//! Parsed documents - to speed up computations
|
|
|
|
|
QHash<QString, ParserTreeItem::ConstPtr> cachedDocTrees;
|
|
|
|
|
|
|
|
|
|
// project trees
|
|
|
|
|
//! Projects read write lock
|
|
|
|
|
QReadWriteLock prjLocker;
|
|
|
|
|
|
|
|
|
|
//! Parsed projects' revision - to speed up computations
|
|
|
|
|
QHash<QString, unsigned> cachedPrjTreesRevision;
|
|
|
|
|
|
|
|
|
|
//! Merged trees for projects. Not const - projects might be substracted/added
|
|
|
|
|
QHash<QString, ParserTreeItem::Ptr> cachedPrjTrees;
|
|
|
|
|
|
|
|
|
|
//! Cached file lists for projects (non-flat mode)
|
|
|
|
|
QHash<QString, QStringList> cachedPrjFileLists;
|
|
|
|
|
|
|
|
|
|
// other
|
|
|
|
|
//! List for files which has to be parsed
|
|
|
|
|
QSet<QString> fileList;
|
|
|
|
|
|
|
|
|
|
//! Root item read write lock
|
|
|
|
|
QReadWriteLock rootItemLocker;
|
|
|
|
|
|
|
|
|
|
//! Parsed root item
|
|
|
|
|
ParserTreeItem::ConstPtr rootItem;
|
|
|
|
|
|
|
|
|
|
//! Flat mode
|
2018-11-04 22:40:59 +01:00
|
|
|
bool flatMode = false;
|
2010-07-16 11:18:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CPlusPlus::Document::Ptr ParserPrivate::document(const QString &fileName) const
|
|
|
|
|
{
|
2013-12-04 13:46:12 +01:00
|
|
|
CitDocumentList cit = documentList.find(fileName);
|
|
|
|
|
if (cit == documentList.end())
|
2010-07-16 11:18:30 +02:00
|
|
|
return CPlusPlus::Document::Ptr();
|
2013-12-04 13:46:12 +01:00
|
|
|
return cit.value();
|
2010-07-16 11:18:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------- Parser ---------------------------------
|
|
|
|
|
|
2013-05-24 17:35:14 +02:00
|
|
|
/*!
|
|
|
|
|
Constructs the parser object.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-07-16 11:18:30 +02:00
|
|
|
Parser::Parser(QObject *parent)
|
|
|
|
|
: QObject(parent),
|
2021-02-02 16:16:39 +01:00
|
|
|
d(new ParserPrivate(this))
|
2010-07-16 11:18:30 +02:00
|
|
|
{
|
2017-04-05 17:46:55 +02:00
|
|
|
d->timer.setSingleShot(true);
|
2010-07-16 11:18:30 +02:00
|
|
|
|
|
|
|
|
// timer for emitting changes
|
2021-02-08 12:22:21 +01:00
|
|
|
connect(&d->timer, &QTimer::timeout, this, &Parser::requestCurrentState);
|
2010-07-16 11:18:30 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-24 17:35:14 +02:00
|
|
|
/*!
|
|
|
|
|
Destructs the parser object.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-07-16 11:18:30 +02:00
|
|
|
Parser::~Parser()
|
|
|
|
|
{
|
2011-07-06 17:40:54 +02:00
|
|
|
delete d;
|
2010-07-16 11:18:30 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-24 17:35:14 +02:00
|
|
|
/*!
|
|
|
|
|
Checks \a item for lazy data population of a QStandardItemModel.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-12-04 13:46:12 +01:00
|
|
|
bool Parser::canFetchMore(QStandardItem *item, bool skipRoot) const
|
2010-07-16 11:18:30 +02:00
|
|
|
{
|
2013-12-04 13:46:12 +01:00
|
|
|
ParserTreeItem::ConstPtr ptr = findItemByRoot(item, skipRoot);
|
2010-07-16 11:18:30 +02:00
|
|
|
if (ptr.isNull())
|
|
|
|
|
return false;
|
|
|
|
|
return ptr->canFetchMore(item);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-24 17:35:14 +02:00
|
|
|
/*!
|
|
|
|
|
Checks \a item for lazy data population of a QStandardItemModel.
|
|
|
|
|
\a skipRoot skips the root item.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-07-16 11:18:30 +02:00
|
|
|
void Parser::fetchMore(QStandardItem *item, bool skipRoot) const
|
|
|
|
|
{
|
|
|
|
|
ParserTreeItem::ConstPtr ptr = findItemByRoot(item, skipRoot);
|
|
|
|
|
if (ptr.isNull())
|
|
|
|
|
return;
|
|
|
|
|
ptr->fetchMore(item);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-04 13:46:12 +01:00
|
|
|
bool Parser::hasChildren(QStandardItem *item) const
|
|
|
|
|
{
|
|
|
|
|
ParserTreeItem::ConstPtr ptr = findItemByRoot(item);
|
|
|
|
|
if (ptr.isNull())
|
|
|
|
|
return false;
|
|
|
|
|
return ptr->childCount() != 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-24 17:35:14 +02:00
|
|
|
/*!
|
|
|
|
|
Switches to flat mode (without subprojects) if \a flat returns \c true.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-07-16 11:18:30 +02:00
|
|
|
void Parser::setFlatMode(bool flatMode)
|
|
|
|
|
{
|
2011-07-06 17:40:54 +02:00
|
|
|
if (flatMode == d->flatMode)
|
2010-07-16 11:18:30 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// change internal
|
2011-07-06 17:40:54 +02:00
|
|
|
d->flatMode = flatMode;
|
2010-07-16 11:18:30 +02:00
|
|
|
|
|
|
|
|
// regenerate and resend current tree
|
2021-02-08 12:22:21 +01:00
|
|
|
requestCurrentState();
|
2010-07-16 11:18:30 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-24 17:35:14 +02:00
|
|
|
/*!
|
|
|
|
|
Returns the internal tree item for \a item. \a skipRoot skips the root
|
|
|
|
|
item.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-07-16 11:18:30 +02:00
|
|
|
ParserTreeItem::ConstPtr Parser::findItemByRoot(const QStandardItem *item, bool skipRoot) const
|
|
|
|
|
{
|
|
|
|
|
if (!item)
|
|
|
|
|
return ParserTreeItem::ConstPtr();
|
|
|
|
|
|
|
|
|
|
// go item by item to the root
|
|
|
|
|
QList<const QStandardItem *> uiList;
|
|
|
|
|
const QStandardItem *cur = item;
|
2012-11-28 20:44:03 +02:00
|
|
|
while (cur) {
|
2010-07-16 11:18:30 +02:00
|
|
|
uiList.append(cur);
|
|
|
|
|
cur = cur->parent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (skipRoot && uiList.count() > 0)
|
|
|
|
|
uiList.removeLast();
|
|
|
|
|
|
2011-07-06 17:40:54 +02:00
|
|
|
QReadLocker locker(&d->rootItemLocker);
|
2010-07-16 11:18:30 +02:00
|
|
|
|
|
|
|
|
// using internal root - search correct item
|
2011-07-06 17:40:54 +02:00
|
|
|
ParserTreeItem::ConstPtr internal = d->rootItem;
|
2010-07-16 11:18:30 +02:00
|
|
|
|
2012-11-28 20:44:03 +02:00
|
|
|
while (uiList.count() > 0) {
|
2010-07-16 11:18:30 +02:00
|
|
|
cur = uiList.last();
|
|
|
|
|
uiList.removeLast();
|
2019-11-15 18:42:11 +01:00
|
|
|
const SymbolInformation &inf = Internal::symbolInformationFromItem(cur);
|
2010-07-16 11:18:30 +02:00
|
|
|
internal = internal->child(inf);
|
|
|
|
|
if (internal.isNull())
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return internal;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-24 17:35:14 +02:00
|
|
|
/*!
|
|
|
|
|
Parses the class and produces a new tree.
|
|
|
|
|
|
|
|
|
|
\sa addProject
|
|
|
|
|
*/
|
|
|
|
|
|
2010-07-16 11:18:30 +02:00
|
|
|
ParserTreeItem::ConstPtr Parser::parse()
|
|
|
|
|
{
|
2019-07-05 13:43:54 +02:00
|
|
|
QElapsedTimer time;
|
2010-07-16 11:18:30 +02:00
|
|
|
if (debug)
|
|
|
|
|
time.start();
|
|
|
|
|
|
|
|
|
|
ParserTreeItem::Ptr rootItem(new ParserTreeItem());
|
|
|
|
|
|
|
|
|
|
// check all projects
|
2017-03-01 17:53:15 +01:00
|
|
|
for (const Project *prj : SessionManager::projects()) {
|
2010-07-16 11:18:30 +02:00
|
|
|
ParserTreeItem::Ptr item;
|
|
|
|
|
QString prjName(prj->displayName());
|
2019-08-16 09:57:59 +02:00
|
|
|
QString prjType = prj->projectFilePath().toString();
|
2010-07-16 11:18:30 +02:00
|
|
|
SymbolInformation inf(prjName, prjType);
|
2013-12-04 13:46:12 +01:00
|
|
|
item = ParserTreeItem::Ptr(new ParserTreeItem());
|
2010-07-16 11:18:30 +02:00
|
|
|
|
2013-12-04 13:46:12 +01:00
|
|
|
if (d->flatMode)
|
2017-04-05 17:32:36 +02:00
|
|
|
addFlatTree(item, prj);
|
2013-12-04 13:46:12 +01:00
|
|
|
else
|
2017-04-05 17:32:36 +02:00
|
|
|
addProjectTree(item, prj);
|
|
|
|
|
|
|
|
|
|
item->setIcon(prj->containerNode()->icon());
|
2010-07-16 11:18:30 +02:00
|
|
|
|
|
|
|
|
rootItem->appendChild(item, inf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (debug)
|
|
|
|
|
qDebug() << "Class View:" << QDateTime::currentDateTime().toString()
|
|
|
|
|
<< "Parsed in " << time.elapsed() << "msecs.";
|
|
|
|
|
|
|
|
|
|
return rootItem;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-24 17:35:14 +02:00
|
|
|
/*!
|
|
|
|
|
Parses the project with the \a projectId and adds the documents
|
|
|
|
|
from the \a fileList to the tree item \a item.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-07-16 11:18:30 +02:00
|
|
|
void Parser::addProject(const ParserTreeItem::Ptr &item, const QStringList &fileList,
|
2017-04-06 11:45:16 +02:00
|
|
|
const QString &projectId)
|
2010-07-16 11:18:30 +02:00
|
|
|
{
|
|
|
|
|
// recalculate cache tree if needed
|
|
|
|
|
ParserTreeItem::Ptr prj(getCachedOrParseProjectTree(fileList, projectId));
|
|
|
|
|
if (item.isNull())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// if there is an item - copy project tree to that item
|
|
|
|
|
item->copy(prj);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-24 17:35:14 +02:00
|
|
|
/*!
|
|
|
|
|
Parses \a symbol and adds the results to \a item (as a parent).
|
|
|
|
|
*/
|
|
|
|
|
|
2010-07-16 11:18:30 +02:00
|
|
|
void Parser::addSymbol(const ParserTreeItem::Ptr &item, const CPlusPlus::Symbol *symbol)
|
|
|
|
|
{
|
|
|
|
|
if (item.isNull() || !symbol)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// easy solution - lets add any scoped symbol and
|
|
|
|
|
// any symbol which does not contain :: in the name
|
|
|
|
|
|
|
|
|
|
//! \todo collect statistics and reorder to optimize
|
|
|
|
|
if (symbol->isForwardClassDeclaration()
|
|
|
|
|
|| symbol->isExtern()
|
|
|
|
|
|| symbol->isFriend()
|
|
|
|
|
|| symbol->isGenerated()
|
|
|
|
|
|| symbol->isUsingNamespaceDirective()
|
|
|
|
|
|| symbol->isUsingDeclaration()
|
|
|
|
|
)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const CPlusPlus::Name *symbolName = symbol->name();
|
|
|
|
|
if (symbolName && symbolName->isQualifiedNameId())
|
|
|
|
|
return;
|
|
|
|
|
|
2013-12-04 13:46:12 +01:00
|
|
|
QString name = d->overview.prettyName(symbolName).trimmed();
|
2011-07-06 17:40:54 +02:00
|
|
|
QString type = d->overview.prettyType(symbol->type()).trimmed();
|
2010-07-16 11:18:30 +02:00
|
|
|
int iconType = CPlusPlus::Icons::iconTypeForSymbol(symbol);
|
|
|
|
|
|
|
|
|
|
SymbolInformation information(name, type, iconType);
|
|
|
|
|
|
|
|
|
|
ParserTreeItem::Ptr itemAdd;
|
|
|
|
|
|
|
|
|
|
// If next line will be removed, 5% speed up for the initial parsing.
|
|
|
|
|
// But there might be a problem for some files ???
|
|
|
|
|
// Better to improve qHash timing
|
|
|
|
|
itemAdd = item->child(information);
|
|
|
|
|
|
|
|
|
|
if (itemAdd.isNull())
|
|
|
|
|
itemAdd = ParserTreeItem::Ptr(new ParserTreeItem());
|
|
|
|
|
|
2018-11-02 14:17:12 +01:00
|
|
|
// locations have 1-based column in Symbol, use the same here.
|
2012-01-20 15:13:56 +01:00
|
|
|
SymbolLocation location(QString::fromUtf8(symbol->fileName() , symbol->fileNameLength()),
|
2018-11-02 14:17:12 +01:00
|
|
|
symbol->line(), symbol->column());
|
2010-07-16 11:18:30 +02:00
|
|
|
itemAdd->addSymbolLocation(location);
|
|
|
|
|
|
|
|
|
|
// prevent showing a content of the functions
|
|
|
|
|
if (!symbol->isFunction()) {
|
2013-12-04 13:46:12 +01:00
|
|
|
if (const CPlusPlus::Scope *scope = symbol->asScope()) {
|
2015-01-13 17:29:34 +01:00
|
|
|
CPlusPlus::Scope::iterator cur = scope->memberBegin();
|
|
|
|
|
CPlusPlus::Scope::iterator last = scope->memberEnd();
|
2013-12-04 13:46:12 +01:00
|
|
|
while (cur != last) {
|
2010-08-11 12:26:02 +02:00
|
|
|
const CPlusPlus::Symbol *curSymbol = *cur;
|
|
|
|
|
++cur;
|
|
|
|
|
if (!curSymbol)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
addSymbol(itemAdd, curSymbol);
|
2010-07-16 11:18:30 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if item is empty and has not to be added
|
2013-12-04 13:46:12 +01:00
|
|
|
if (!(symbol->isNamespace() && itemAdd->childCount() == 0))
|
2010-07-16 11:18:30 +02:00
|
|
|
item->appendChild(itemAdd, information);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-24 17:35:14 +02:00
|
|
|
/*!
|
|
|
|
|
Parses the project with the \a projectId and adds the documents from the
|
|
|
|
|
\a fileList to the project. Updates the internal cached tree for this
|
|
|
|
|
project.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-07-16 11:18:30 +02:00
|
|
|
ParserTreeItem::Ptr Parser::getParseProjectTree(const QStringList &fileList,
|
|
|
|
|
const QString &projectId)
|
|
|
|
|
{
|
|
|
|
|
//! \todo Way to optimize - for documentUpdate - use old cached project and subtract
|
|
|
|
|
//! changed files only (old edition), and add curent editions
|
|
|
|
|
ParserTreeItem::Ptr item(new ParserTreeItem());
|
|
|
|
|
unsigned revision = 0;
|
2012-11-28 20:44:03 +02:00
|
|
|
foreach (const QString &file, fileList) {
|
2010-07-16 11:18:30 +02:00
|
|
|
// ? locker for document?..
|
2011-07-06 17:40:54 +02:00
|
|
|
const CPlusPlus::Document::Ptr &doc = d->document(file);
|
2010-07-16 11:18:30 +02:00
|
|
|
if (doc.isNull())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
revision += doc->revision();
|
|
|
|
|
|
|
|
|
|
ParserTreeItem::ConstPtr list = getCachedOrParseDocumentTree(doc);
|
|
|
|
|
if (list.isNull())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
// add list to out document
|
|
|
|
|
item->add(list);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// update the cache
|
|
|
|
|
if (!projectId.isEmpty()) {
|
2011-07-06 17:40:54 +02:00
|
|
|
QWriteLocker locker(&d->prjLocker);
|
2010-07-16 11:18:30 +02:00
|
|
|
|
2011-07-06 17:40:54 +02:00
|
|
|
d->cachedPrjTrees[projectId] = item;
|
|
|
|
|
d->cachedPrjTreesRevision[projectId] = revision;
|
2010-07-16 11:18:30 +02:00
|
|
|
}
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-24 17:35:14 +02:00
|
|
|
/*!
|
|
|
|
|
Gets the project with \a projectId from the cache if it is valid or parses
|
|
|
|
|
the project and adds the documents from the \a fileList to the project.
|
|
|
|
|
Updates the internal cached tree for this project.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-07-16 11:18:30 +02:00
|
|
|
ParserTreeItem::Ptr Parser::getCachedOrParseProjectTree(const QStringList &fileList,
|
|
|
|
|
const QString &projectId)
|
|
|
|
|
{
|
2011-07-06 17:40:54 +02:00
|
|
|
d->prjLocker.lockForRead();
|
2010-07-16 11:18:30 +02:00
|
|
|
|
2013-12-04 13:46:12 +01:00
|
|
|
ParserTreeItem::Ptr item = d->cachedPrjTrees.value(projectId);
|
2010-07-16 11:18:30 +02:00
|
|
|
// calculate current revision
|
2013-12-04 13:46:12 +01:00
|
|
|
if (!projectId.isEmpty() && !item.isNull()) {
|
2010-07-16 11:18:30 +02:00
|
|
|
// calculate project's revision
|
|
|
|
|
unsigned revision = 0;
|
2012-11-28 20:44:03 +02:00
|
|
|
foreach (const QString &file, fileList) {
|
2011-07-06 17:40:54 +02:00
|
|
|
const CPlusPlus::Document::Ptr &doc = d->document(file);
|
2010-07-16 11:18:30 +02:00
|
|
|
if (doc.isNull())
|
|
|
|
|
continue;
|
|
|
|
|
revision += doc->revision();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if even revision is the same, return cached project
|
2011-07-06 17:40:54 +02:00
|
|
|
if (revision == d->cachedPrjTreesRevision[projectId]) {
|
|
|
|
|
d->prjLocker.unlock();
|
2013-12-04 13:46:12 +01:00
|
|
|
return item;
|
2010-07-16 11:18:30 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-06 17:40:54 +02:00
|
|
|
d->prjLocker.unlock();
|
2010-07-16 11:18:30 +02:00
|
|
|
return getParseProjectTree(fileList, projectId);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-24 17:35:14 +02:00
|
|
|
/*!
|
|
|
|
|
Parses the document \a doc if it is in the project files and adds a tree to
|
|
|
|
|
the internal storage. Updates the internal cached tree for this document.
|
|
|
|
|
|
|
|
|
|
\sa parseDocument
|
|
|
|
|
*/
|
|
|
|
|
|
2010-07-16 11:18:30 +02:00
|
|
|
ParserTreeItem::ConstPtr Parser::getParseDocumentTree(const CPlusPlus::Document::Ptr &doc)
|
|
|
|
|
{
|
|
|
|
|
if (doc.isNull())
|
|
|
|
|
return ParserTreeItem::ConstPtr();
|
|
|
|
|
|
|
|
|
|
const QString &fileName = doc->fileName();
|
2011-07-06 17:40:54 +02:00
|
|
|
if (!d->fileList.contains(fileName))
|
2010-07-16 11:18:30 +02:00
|
|
|
return ParserTreeItem::ConstPtr();
|
|
|
|
|
|
|
|
|
|
ParserTreeItem::Ptr itemPtr(new ParserTreeItem());
|
|
|
|
|
|
2013-12-04 13:46:12 +01:00
|
|
|
const unsigned total = doc->globalSymbolCount();
|
2011-04-19 15:42:14 +02:00
|
|
|
for (unsigned i = 0; i < total; ++i)
|
2010-07-16 11:18:30 +02:00
|
|
|
addSymbol(itemPtr, doc->globalSymbolAt(i));
|
|
|
|
|
|
2011-07-06 17:40:54 +02:00
|
|
|
QWriteLocker locker(&d->docLocker);
|
2010-07-16 11:18:30 +02:00
|
|
|
|
2011-07-06 17:40:54 +02:00
|
|
|
d->cachedDocTrees[fileName] = itemPtr;
|
|
|
|
|
d->cachedDocTreesRevision[fileName] = doc->revision();
|
|
|
|
|
d->documentList[fileName] = doc;
|
2010-07-16 11:18:30 +02:00
|
|
|
|
|
|
|
|
return itemPtr;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-24 17:35:14 +02:00
|
|
|
/*!
|
|
|
|
|
Gets the document \a doc from the cache or parses it if it is in the project
|
|
|
|
|
files and adds a tree to the internal storage.
|
|
|
|
|
|
|
|
|
|
\sa parseDocument
|
|
|
|
|
*/
|
|
|
|
|
|
2010-07-16 11:18:30 +02:00
|
|
|
ParserTreeItem::ConstPtr Parser::getCachedOrParseDocumentTree(const CPlusPlus::Document::Ptr &doc)
|
|
|
|
|
{
|
|
|
|
|
if (doc.isNull())
|
|
|
|
|
return ParserTreeItem::ConstPtr();
|
|
|
|
|
|
|
|
|
|
const QString &fileName = doc->fileName();
|
2011-07-06 17:40:54 +02:00
|
|
|
d->docLocker.lockForRead();
|
2013-12-04 13:46:12 +01:00
|
|
|
ParserTreeItem::ConstPtr item = d->cachedDocTrees.value(fileName);
|
2015-04-01 11:19:32 +02:00
|
|
|
CitCachedDocTreeRevision citCachedDocTreeRevision = d->cachedDocTreesRevision.constFind(fileName);
|
2013-12-04 13:46:12 +01:00
|
|
|
if (!item.isNull()
|
2015-04-01 11:19:32 +02:00
|
|
|
&& citCachedDocTreeRevision != d->cachedDocTreesRevision.constEnd()
|
2013-12-04 13:46:12 +01:00
|
|
|
&& citCachedDocTreeRevision.value() == doc->revision()) {
|
2011-07-06 17:40:54 +02:00
|
|
|
d->docLocker.unlock();
|
2013-12-04 13:46:12 +01:00
|
|
|
return item;
|
2010-07-16 11:18:30 +02:00
|
|
|
}
|
2011-09-19 10:02:45 +02:00
|
|
|
d->docLocker.unlock();
|
|
|
|
|
return getParseDocumentTree(doc);
|
2010-07-16 11:18:30 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-24 17:35:14 +02:00
|
|
|
/*!
|
|
|
|
|
Parses the document \a doc if it is in the project files and adds a tree to
|
|
|
|
|
the internal storage.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-07-16 11:18:30 +02:00
|
|
|
void Parser::parseDocument(const CPlusPlus::Document::Ptr &doc)
|
|
|
|
|
{
|
|
|
|
|
if (doc.isNull())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const QString &name = doc->fileName();
|
|
|
|
|
|
|
|
|
|
// if it is external file (not in any of our projects)
|
2011-07-06 17:40:54 +02:00
|
|
|
if (!d->fileList.contains(name))
|
2010-07-16 11:18:30 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
getParseDocumentTree(doc);
|
|
|
|
|
|
2017-04-05 17:46:55 +02:00
|
|
|
if (!d->timer.isActive())
|
|
|
|
|
d->timer.start(400); //! Delay in msecs before an update
|
2010-07-16 11:18:30 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-24 17:35:14 +02:00
|
|
|
/*!
|
|
|
|
|
Requests to clear internal stored data. The data has to be regenerated on
|
|
|
|
|
the next request.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-07-16 11:18:30 +02:00
|
|
|
void Parser::clearCache()
|
|
|
|
|
{
|
2011-07-06 17:40:54 +02:00
|
|
|
QWriteLocker locker(&d->prjLocker);
|
2010-07-16 11:18:30 +02:00
|
|
|
|
|
|
|
|
// remove cached trees
|
2011-07-06 17:40:54 +02:00
|
|
|
d->cachedPrjFileLists.clear();
|
2010-07-16 11:18:30 +02:00
|
|
|
|
|
|
|
|
//! \todo where better to clear project's trees?
|
|
|
|
|
//! When file is add/removed from a particular project?..
|
2011-07-06 17:40:54 +02:00
|
|
|
d->cachedPrjTrees.clear();
|
|
|
|
|
d->cachedPrjTreesRevision.clear();
|
2010-07-16 11:18:30 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-24 17:35:14 +02:00
|
|
|
/*!
|
|
|
|
|
Specifies the files that must be allowed for the parsing as a \a fileList.
|
|
|
|
|
Files outside of this list will not be in any tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-07-16 11:18:30 +02:00
|
|
|
void Parser::setFileList(const QStringList &fileList)
|
|
|
|
|
{
|
2019-11-15 18:42:11 +01:00
|
|
|
d->fileList = Utils::toSet(fileList);
|
2010-07-16 11:18:30 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-24 17:35:14 +02:00
|
|
|
/*!
|
|
|
|
|
Removes the files defined in the \a fileList from the parsing.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-07-16 11:18:30 +02:00
|
|
|
void Parser::removeFiles(const QStringList &fileList)
|
|
|
|
|
{
|
2017-02-21 12:33:35 +01:00
|
|
|
if (fileList.isEmpty())
|
2010-07-16 11:18:30 +02:00
|
|
|
return;
|
|
|
|
|
|
2011-07-06 17:40:54 +02:00
|
|
|
QWriteLocker lockerPrj(&d->prjLocker);
|
|
|
|
|
QWriteLocker lockerDoc(&d->docLocker);
|
2011-09-19 10:02:45 +02:00
|
|
|
foreach (const QString &name, fileList) {
|
2011-07-06 17:40:54 +02:00
|
|
|
d->fileList.remove(name);
|
|
|
|
|
d->cachedDocTrees.remove(name);
|
|
|
|
|
d->cachedDocTreesRevision.remove(name);
|
|
|
|
|
d->documentList.remove(name);
|
|
|
|
|
d->cachedPrjTrees.remove(name);
|
|
|
|
|
d->cachedPrjFileLists.clear();
|
2010-07-16 11:18:30 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-24 17:35:14 +02:00
|
|
|
/*!
|
|
|
|
|
Fully resets the internal state of the code parser to \a snapshot.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-07-16 11:18:30 +02:00
|
|
|
void Parser::resetData(const CPlusPlus::Snapshot &snapshot)
|
|
|
|
|
{
|
|
|
|
|
// clear internal cache
|
|
|
|
|
clearCache();
|
|
|
|
|
|
2011-07-06 17:40:54 +02:00
|
|
|
d->docLocker.lockForWrite();
|
2010-07-16 11:18:30 +02:00
|
|
|
|
|
|
|
|
// copy snapshot's documents
|
|
|
|
|
CPlusPlus::Snapshot::const_iterator cur = snapshot.begin();
|
|
|
|
|
CPlusPlus::Snapshot::const_iterator end = snapshot.end();
|
2014-05-19 23:31:23 +03:00
|
|
|
for (; cur != end; ++cur)
|
2021-02-09 11:54:22 +01:00
|
|
|
d->documentList[cur.key().toString()] = cur.value(); // why we don't clear documentList?
|
2010-07-16 11:18:30 +02:00
|
|
|
|
2011-07-06 17:40:54 +02:00
|
|
|
d->docLocker.unlock();
|
2010-07-16 11:18:30 +02:00
|
|
|
|
|
|
|
|
// recalculate file list
|
2019-12-17 14:07:53 +01:00
|
|
|
FilePaths fileList;
|
2010-07-16 11:18:30 +02:00
|
|
|
|
|
|
|
|
// check all projects
|
2017-03-01 17:53:15 +01:00
|
|
|
for (const Project *prj : SessionManager::projects())
|
|
|
|
|
fileList += prj->files(Project::SourceFiles);
|
2019-11-15 18:42:11 +01:00
|
|
|
setFileList(Utils::transform(fileList, &FilePath::toString));
|
2010-07-16 11:18:30 +02:00
|
|
|
|
2021-02-08 12:22:21 +01:00
|
|
|
requestCurrentState();
|
2010-07-16 11:18:30 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-24 17:35:14 +02:00
|
|
|
/*!
|
|
|
|
|
Fully resets the internal state of the code parser to the current state.
|
|
|
|
|
|
|
|
|
|
\sa resetData
|
|
|
|
|
*/
|
|
|
|
|
|
2010-07-16 11:18:30 +02:00
|
|
|
void Parser::resetDataToCurrentState()
|
|
|
|
|
{
|
|
|
|
|
// get latest data
|
2015-02-15 23:13:28 +02:00
|
|
|
resetData(CppTools::CppModelManager::instance()->snapshot());
|
2010-07-16 11:18:30 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-24 17:35:14 +02:00
|
|
|
/*!
|
|
|
|
|
Requests to emit a signal with the current tree state.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-07-16 11:18:30 +02:00
|
|
|
void Parser::requestCurrentState()
|
|
|
|
|
{
|
2017-04-05 17:46:55 +02:00
|
|
|
d->timer.stop();
|
2010-07-16 11:18:30 +02:00
|
|
|
|
2011-07-06 17:40:54 +02:00
|
|
|
d->rootItemLocker.lockForWrite();
|
|
|
|
|
d->rootItem = parse();
|
|
|
|
|
d->rootItemLocker.unlock();
|
2010-07-16 11:18:30 +02:00
|
|
|
|
|
|
|
|
// convert
|
|
|
|
|
QSharedPointer<QStandardItem> std(new QStandardItem());
|
|
|
|
|
|
2011-07-06 17:40:54 +02:00
|
|
|
d->rootItem->convertTo(std.data());
|
2010-07-16 11:18:30 +02:00
|
|
|
|
|
|
|
|
emit treeDataUpdate(std);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-24 17:35:14 +02:00
|
|
|
/*!
|
|
|
|
|
Generates projects like the Project Explorer.
|
|
|
|
|
\a item specifies the item and \a node specifies the root node.
|
|
|
|
|
|
|
|
|
|
Returns a list of projects which were added to the item.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-04-05 17:32:36 +02:00
|
|
|
QStringList Parser::addProjectTree(const ParserTreeItem::Ptr &item, const Project *project)
|
2010-07-16 11:18:30 +02:00
|
|
|
{
|
|
|
|
|
QStringList projectList;
|
2017-04-05 17:32:36 +02:00
|
|
|
if (!project)
|
2010-07-16 11:18:30 +02:00
|
|
|
return projectList;
|
|
|
|
|
|
2017-04-05 17:32:36 +02:00
|
|
|
const QString projectPath = project->projectFilePath().toString();
|
2010-07-16 11:18:30 +02:00
|
|
|
|
|
|
|
|
// our own files
|
|
|
|
|
QStringList fileList;
|
|
|
|
|
|
2017-04-05 17:32:36 +02:00
|
|
|
CitCachedPrjFileLists cit = d->cachedPrjFileLists.constFind(projectPath);
|
2010-07-16 11:18:30 +02:00
|
|
|
// try to improve parsing speed by internal cache
|
2015-04-01 11:19:32 +02:00
|
|
|
if (cit != d->cachedPrjFileLists.constEnd()) {
|
2013-12-04 13:46:12 +01:00
|
|
|
fileList = cit.value();
|
2010-07-16 11:18:30 +02:00
|
|
|
} else {
|
2019-11-15 18:42:11 +01:00
|
|
|
fileList = Utils::transform(project->files(Project::SourceFiles), &FilePath::toString);
|
2017-04-05 17:32:36 +02:00
|
|
|
d->cachedPrjFileLists[projectPath] = fileList;
|
2010-07-16 11:18:30 +02:00
|
|
|
}
|
|
|
|
|
if (fileList.count() > 0) {
|
2017-04-05 17:32:36 +02:00
|
|
|
addProject(item, fileList, projectPath);
|
|
|
|
|
projectList << projectPath;
|
2010-07-16 11:18:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return projectList;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-05 17:32:36 +02:00
|
|
|
QStringList Parser::getAllFiles(const Project *project)
|
2013-12-04 13:46:12 +01:00
|
|
|
{
|
|
|
|
|
QStringList fileList;
|
|
|
|
|
|
2017-04-05 17:32:36 +02:00
|
|
|
if (!project)
|
2013-12-04 13:46:12 +01:00
|
|
|
return fileList;
|
|
|
|
|
|
2017-04-05 17:32:36 +02:00
|
|
|
const QString nodePath = project->projectFilePath().toString();
|
2013-12-04 13:46:12 +01:00
|
|
|
|
2015-04-01 11:19:32 +02:00
|
|
|
CitCachedPrjFileLists cit = d->cachedPrjFileLists.constFind(nodePath);
|
2013-12-04 13:46:12 +01:00
|
|
|
// try to improve parsing speed by internal cache
|
2015-04-01 11:19:32 +02:00
|
|
|
if (cit != d->cachedPrjFileLists.constEnd()) {
|
2013-12-04 13:46:12 +01:00
|
|
|
fileList = cit.value();
|
|
|
|
|
} else {
|
2019-11-15 18:42:11 +01:00
|
|
|
fileList = Utils::transform(project->files(Project::SourceFiles), &FilePath::toString);
|
2013-12-04 13:46:12 +01:00
|
|
|
d->cachedPrjFileLists[nodePath] = fileList;
|
|
|
|
|
}
|
|
|
|
|
return fileList;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-05 17:32:36 +02:00
|
|
|
void Parser::addFlatTree(const ParserTreeItem::Ptr &item, const Project *project)
|
2013-12-04 13:46:12 +01:00
|
|
|
{
|
2017-04-05 17:32:36 +02:00
|
|
|
if (!project)
|
2013-12-04 13:46:12 +01:00
|
|
|
return;
|
|
|
|
|
|
2017-04-05 17:32:36 +02:00
|
|
|
QStringList fileList = getAllFiles(project);
|
2013-12-04 13:46:12 +01:00
|
|
|
fileList.removeDuplicates();
|
|
|
|
|
|
|
|
|
|
if (fileList.count() > 0) {
|
2017-04-05 17:32:36 +02:00
|
|
|
addProject(item, fileList, project->projectFilePath().toString());
|
2013-12-04 13:46:12 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-16 11:18:30 +02:00
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace ClassView
|