Commit 9a968b74 authored by hjk's avatar hjk

CppTools: Move DependencyTable to Snapshot

It logically depends on the Snapshot and has a related lifetime.
Keeping it in the Snapshot avoids some code compelxity.

Change-Id: I24ee4483b44d9b0d7f2e4d494ae7ea624b949f9c
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@digia.com>
parent 0273757c
......@@ -713,6 +713,7 @@ bool Document::DiagnosticMessage::operator!=(const Document::DiagnosticMessage &
}
Snapshot::Snapshot()
: m_deps(new DependencyTable)
{
}
......@@ -747,8 +748,10 @@ bool Snapshot::contains(const QString &fileName) const
void Snapshot::insert(Document::Ptr doc)
{
if (doc)
if (doc) {
_documents.insert(doc->fileName(), doc);
m_deps->files.clear(); // Will trigger re-build when accessed.
}
}
Document::Ptr Snapshot::preprocessedDocument(const QByteArray &source,
......@@ -795,6 +798,18 @@ QSet<QString> Snapshot::allIncludesForDocument(const QString &fileName) const
return result;
}
QStringList Snapshot::filesDependingOn(const QString &fileName) const
{
updateDependencyTable();
return m_deps->filesDependingOn(fileName);
}
void Snapshot::updateDependencyTable() const
{
if (m_deps->files.isEmpty())
m_deps->build(*this);
}
void Snapshot::allIncludesForDocument_helper(const QString &fileName, QSet<QString> &result) const
{
if (Document::Ptr doc = document(fileName)) {
......
......@@ -34,6 +34,7 @@
#include <cplusplus/CPlusPlusForwardDeclarations.h>
#include <cplusplus/PreprocessorClient.h>
#include <cplusplus/DependencyTable.h>
#include <QSharedPointer>
#include <QDateTime>
......@@ -424,10 +425,13 @@ public:
QSet<QString> allIncludesForDocument(const QString &fileName) const;
QStringList filesDependingOn(const QString &fileName) const;
void updateDependencyTable() const;
private:
void allIncludesForDocument_helper(const QString &fileName, QSet<QString> &result) const;
private:
mutable QSharedPointer<DependencyTable> m_deps;
Base _documents;
};
......
......@@ -27,7 +27,6 @@
**
****************************************************************************/
#include "DependencyTable.h"
#include "CppDocument.h"
#include <QDebug>
......@@ -51,50 +50,8 @@ QStringList DependencyTable::filesDependingOn(const QString &fileName) const
return deps;
}
QHash<QString, QStringList> DependencyTable::dependencyTable() const
{
QHash<QString, QStringList> depMap;
for (int index = 0; index < files.size(); ++index) {
QStringList deps;
for (int i = 0; i < files.size(); ++i) {
const QBitArray &bits = includeMap.at(i);
if (bits.testBit(index))
deps.append(files.at(i));
}
depMap[files.at(index)] = deps;
}
return depMap;
}
bool DependencyTable::isValidFor(const Snapshot &snapshot) const
{
const int documentCount = snapshot.size();
if (documentCount != files.size())
return false;
for (Snapshot::const_iterator it = snapshot.begin(); it != snapshot.end(); ++it) {
QHash<QString, QStringList>::const_iterator i = includesPerFile.find(it.key());
if (i == includesPerFile.end())
return false;
if (i.value() != it.value()->includedFiles())
return false;
}
return true;
}
void DependencyTable::build(const Snapshot &snapshot)
{
includesPerFile.clear();
files.clear();
fileIndex.clear();
includes.clear();
includeMap.clear();
const int documentCount = snapshot.size();
files.resize(documentCount);
includeMap.resize(documentCount);
......
......@@ -44,15 +44,11 @@ class Snapshot;
class CPLUSPLUS_EXPORT DependencyTable
{
public:
bool isValidFor(const Snapshot &snapshot) const;
QStringList filesDependingOn(const QString &fileName) const;
QHash<QString, QStringList> dependencyTable() const;
private:
friend class Snapshot;
void build(const Snapshot &snapshot);
QStringList filesDependingOn(const QString &fileName) const;
private:
QHash<QString, QStringList> includesPerFile;
QVector<QString> files;
QHash<QString, int> fileIndex;
......
......@@ -50,7 +50,6 @@
#include <cplusplus/ASTPath.h>
#include <cplusplus/CPlusPlusForwardDeclarations.h>
#include <cplusplus/CppRewriter.h>
#include <cplusplus/DependencyTable.h>
#include <cplusplus/TypeOfExpression.h>
#include <extensionsystem/pluginmanager.h>
......@@ -1579,9 +1578,7 @@ public:
fwdHeaders.insert(doc);
}
DependencyTable dep;
dep.build(fwdHeaders);
QStringList candidates = dep.dependencyTable().value(headerFile);
QStringList candidates = fwdHeaders.filesDependingOn(headerFile);
const QString className = QString::fromUtf8(k->identifier()->chars());
......
......@@ -187,7 +187,7 @@ void BuiltinEditorDocumentParser::update(WorkingCopy workingCopy)
newSnapshot.insert(i.value());
}
m_snapshot = newSnapshot;
m_deps.build(m_snapshot);
m_snapshot.updateDependencyTable();
emit finished(document(), m_snapshot);
}
......@@ -197,7 +197,6 @@ void BuiltinEditorDocumentParser::releaseResources()
{
QMutexLocker locker(&m_mutex);
m_snapshot = Snapshot();
m_deps = DependencyTable();
m_forceSnapshotInvalidation = true;
}
......@@ -237,7 +236,7 @@ void BuiltinEditorDocumentParser::addFileAndDependencies(QSet<QString> *toRemove
{
toRemove->insert(fileName);
if (fileName != filePath()) {
QStringList deps = m_deps.filesDependingOn(fileName);
QStringList deps = m_snapshot.filesDependingOn(fileName);
toRemove->unite(QSet<QString>::fromList(deps));
}
}
......@@ -35,7 +35,6 @@
#include "cppmodelmanager.h"
#include <cplusplus/CppDocument.h>
#include <cplusplus/DependencyTable.h>
#include <utils/qtcoverride.h>
#include <QMutex>
......@@ -76,7 +75,6 @@ private:
QStringList m_precompiledHeaders;
CPlusPlus::Snapshot m_snapshot;
CPlusPlus::DependencyTable m_deps;
bool m_forceSnapshotInvalidation;
bool m_releaseSourceAndAST;
};
......
......@@ -250,7 +250,6 @@ CppFindReferences::CppFindReferences(CppModelManagerInterface *modelManager)
: QObject(modelManager),
m_modelManager(modelManager)
{
connect(modelManager, SIGNAL(globalSnapshotChanged()), this, SLOT(flushDependencyTable()));
}
CppFindReferences::~CppFindReferences()
......@@ -271,7 +270,6 @@ QList<int> CppFindReferences::references(Symbol *symbol, const LookupContext &co
static void find_helper(QFutureInterface<Usage> &future,
const WorkingCopy workingCopy,
const LookupContext context,
CppFindReferences *findRefs,
Symbol *symbol)
{
const Identifier *symbolId = symbol->identifier();
......@@ -297,8 +295,7 @@ static void find_helper(QFutureInterface<Usage> &future,
files.append(doc->fileName());
}
} else {
DependencyTable dependencyTable = findRefs->updateDependencyTable(snapshot);
files += dependencyTable.filesDependingOn(sourceFile);
files += snapshot.filesDependingOn(sourceFile);
}
files.removeDuplicates();
......@@ -370,7 +367,7 @@ void CppFindReferences::findAll_helper(Core::SearchResult *search, CPlusPlus::Sy
Core::SearchResultWindow::instance()->popup(IOutputPane::ModeSwitch | IOutputPane::WithFocus);
const WorkingCopy workingCopy = m_modelManager->workingCopy();
QFuture<Usage> result;
result = QtConcurrent::run(&find_helper, workingCopy, context, this, symbol);
result = QtConcurrent::run(&find_helper, workingCopy, context, symbol);
createWatcher(result, search);
FutureProgress *progress = ProgressManager::addTask(result, tr("Searching for Usages"),
......@@ -614,15 +611,11 @@ restart_search:
static void findMacroUses_helper(QFutureInterface<Usage> &future,
const WorkingCopy workingCopy,
const Snapshot snapshot,
CppFindReferences *findRefs,
const Macro macro)
{
// ensure the dependency table is updated
DependencyTable dependencies = findRefs->updateDependencyTable(snapshot);
const QString& sourceFile = macro.fileName();
QStringList files(sourceFile);
files += dependencies.filesDependingOn(sourceFile);
files += snapshot.filesDependingOn(sourceFile);
files.removeDuplicates();
future.setProgressRange(0, files.size());
......@@ -677,7 +670,7 @@ void CppFindReferences::findMacroUses(const Macro &macro, const QString &replace
}
QFuture<Usage> result;
result = QtConcurrent::run(&findMacroUses_helper, workingCopy, snapshot, this, macro);
result = QtConcurrent::run(&findMacroUses_helper, workingCopy, snapshot, macro);
createWatcher(result, search);
FutureProgress *progress = ProgressManager::addTask(result, tr("Searching for Usages"),
......@@ -691,39 +684,6 @@ void CppFindReferences::renameMacroUses(const Macro &macro, const QString &repla
findMacroUses(macro, textToReplace, true);
}
DependencyTable CppFindReferences::updateDependencyTable(CPlusPlus::Snapshot snapshot)
{
DependencyTable oldDeps = dependencyTable();
if (oldDeps.isValidFor(snapshot))
return oldDeps;
DependencyTable newDeps;
newDeps.build(snapshot);
setDependencyTable(newDeps);
return newDeps;
}
void CppFindReferences::flushDependencyTable()
{
QMutexLocker locker(&m_depsLock);
Q_UNUSED(locker);
m_deps = DependencyTable();
}
DependencyTable CppFindReferences::dependencyTable() const
{
QMutexLocker locker(&m_depsLock);
Q_UNUSED(locker);
return m_deps;
}
void CppFindReferences::setDependencyTable(const CPlusPlus::DependencyTable &newTable)
{
QMutexLocker locker(&m_depsLock);
Q_UNUSED(locker);
m_deps = newTable;
}
void CppFindReferences::createWatcher(const QFuture<Usage> &future, Core::SearchResult *search)
{
QFutureWatcher<Usage> *watcher = new QFutureWatcher<Usage>();
......
......@@ -30,7 +30,6 @@
#ifndef CPPFINDREFERENCES_H
#define CPPFINDREFERENCES_H
#include <cplusplus/DependencyTable.h>
#include <cplusplus/FindUsages.h>
#include <QMutex>
......@@ -76,11 +75,6 @@ public:
void findMacroUses(const CPlusPlus::Macro &macro);
void renameMacroUses(const CPlusPlus::Macro &macro, const QString &replacement = QString());
CPlusPlus::DependencyTable updateDependencyTable(CPlusPlus::Snapshot snapshot);
public slots:
void flushDependencyTable();
private slots:
void displayResults(int first, int last);
void searchFinished();
......@@ -97,8 +91,6 @@ private:
bool replace);
void findAll_helper(Core::SearchResult *search, CPlusPlus::Symbol *symbol,
const CPlusPlus::LookupContext &context);
CPlusPlus::DependencyTable dependencyTable() const;
void setDependencyTable(const CPlusPlus::DependencyTable &newTable);
void createWatcher(const QFuture<CPlusPlus::Usage> &future, Core::SearchResult *search);
CPlusPlus::Symbol *findSymbol(const CppFindReferencesParameters &parameters,
const CPlusPlus::Snapshot &snapshot, CPlusPlus::LookupContext *context);
......@@ -106,9 +98,6 @@ private:
private:
QPointer<CppModelManagerInterface> m_modelManager;
QMap<QFutureWatcher<CPlusPlus::Usage> *, QPointer<Core::SearchResult> > m_watchers;
mutable QMutex m_depsLock;
CPlusPlus::DependencyTable m_deps;
};
} // namespace Internal
......
......@@ -222,10 +222,6 @@ CppModelManager::CppModelManager(QObject *parent)
, m_enableGC(true)
{
qRegisterMetaType<QSet<QString> >();
connect(this, SIGNAL(documentUpdated(CPlusPlus::Document::Ptr)),
this, SIGNAL(globalSnapshotChanged()));
connect(this, SIGNAL(aboutToRemoveFiles(QStringList)),
this, SIGNAL(globalSnapshotChanged()));
connect(this, SIGNAL(sourceFilesRefreshed(QSet<QString>)),
this, SLOT(onSourceFilesRefreshed()));
......@@ -731,9 +727,7 @@ QList<ProjectPart::Ptr> CppModelManager::projectPart(const QString &fileName) co
QList<ProjectPart::Ptr> CppModelManager::projectPartFromDependencies(const QString &fileName) const
{
QSet<ProjectPart::Ptr> parts;
DependencyTable table;
table.build(snapshot());
const QStringList deps = table.filesDependingOn(fileName);
const QStringList deps = snapshot().filesDependingOn(fileName);
foreach (const QString &dep, deps)
parts.unite(QSet<ProjectPart::Ptr>::fromList(m_fileToProjectParts.value(dep)));
......
......@@ -139,8 +139,6 @@ signals:
/// Other classes can use this to get notified when the \c ProjectExplorer has updated the parts.
void projectPartsUpdated(ProjectExplorer::Project *project);
void globalSnapshotChanged();
public slots:
// Documented in source file.
virtual QFuture<void> updateSourceFiles(const QSet<QString> &sourceFiles,
......
......@@ -29,7 +29,6 @@
#include "typehierarchybuilder.h"
#include <cplusplus/DependencyTable.h>
#include <cplusplus/FindUsages.h>
using namespace CppTools;
......@@ -143,9 +142,7 @@ const QList<TypeHierarchy> &TypeHierarchy::hierarchy() const
TypeHierarchyBuilder::TypeHierarchyBuilder(CPlusPlus::Symbol *symbol, const CPlusPlus::Snapshot &snapshot)
: _symbol(symbol)
, _snapshot(snapshot)
{
_dependencyTable.build(_snapshot);
}
{}
void TypeHierarchyBuilder::reset()
{
......@@ -201,5 +198,5 @@ QStringList TypeHierarchyBuilder::filesDependingOn(CPlusPlus::Symbol *symbol) co
return QStringList();
const QString file = QString::fromUtf8(symbol->fileName(), symbol->fileNameLength());
return QStringList() << file << _dependencyTable.filesDependingOn(file);
return QStringList() << file << _snapshot.filesDependingOn(file);
}
......@@ -33,7 +33,6 @@
#include "cpptools_global.h"
#include "cppmodelmanagerinterface.h"
#include <cplusplus/DependencyTable.h>
#include <cplusplus/Overview.h>
#include <QList>
......@@ -78,7 +77,6 @@ private:
QSet<CPlusPlus::Symbol *> _visited;
QHash<QString, QSet<QString> > _candidates;
CPlusPlus::Overview _overview;
CPlusPlus::DependencyTable _dependencyTable;
};
} // CppTools
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment