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: Nikolai Kosjar <nikolai.kosjar@digia.com>
This commit is contained in:
hjk
2014-09-11 13:15:44 +02:00
parent 0273757c93
commit 9a968b7417
13 changed files with 35 additions and 133 deletions

View File

@@ -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)) {

View File

@@ -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;
};

View File

@@ -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);

View File

@@ -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;
void build(const Snapshot &snapshot);
private:
friend class Snapshot;
void build(const Snapshot &snapshot);
QStringList filesDependingOn(const QString &fileName) const;
QHash<QString, QStringList> includesPerFile;
QVector<QString> files;
QHash<QString, int> fileIndex;