Initial work on the new CPlusPlus::Snapshot.

Encapsulate the details.
This commit is contained in:
Roberto Raggi
2009-12-07 10:54:27 +01:00
parent 5628f945fb
commit 14376c3c32
18 changed files with 108 additions and 60 deletions

View File

@@ -606,7 +606,7 @@ protected:
processed->insert(doc->fileName()); processed->insert(doc->fileName());
foreach (const Document::Include &i, doc->includes()) { foreach (const Document::Include &i, doc->includes()) {
if (Document::Ptr includedDoc = _snapshot.value(i.fileName())) { if (Document::Ptr includedDoc = _snapshot.document(i.fileName())) {
/*NamepaceBinding *binding = */ bind(includedDoc, processed); /*NamepaceBinding *binding = */ bind(includedDoc, processed);
} }
} }

View File

@@ -440,10 +440,40 @@ Snapshot::~Snapshot()
{ {
} }
int Snapshot::size() const
{
return _documents.size();
}
bool Snapshot::isEmpty() const
{
return _documents.isEmpty();
}
Document::Ptr Snapshot::operator[](const QString &fileName) const
{
return _documents.value(fileName, Document::Ptr());
}
Snapshot::const_iterator Snapshot::find(const QString &fileName) const
{
return _documents.find(fileName);
}
void Snapshot::remove(const QString &fileName)
{
_documents.remove(fileName);
}
bool Snapshot::contains(const QString &fileName) const
{
return _documents.contains(fileName);
}
void Snapshot::insert(Document::Ptr doc) void Snapshot::insert(Document::Ptr doc)
{ {
if (doc) if (doc)
insert(doc->fileName(), doc); _documents.insert(doc->fileName(), doc);
} }
QByteArray Snapshot::preprocessedCode(const QString &source, const QString &fileName) const QByteArray Snapshot::preprocessedCode(const QString &source, const QString &fileName) const
@@ -457,7 +487,7 @@ Document::Ptr Snapshot::documentFromSource(const QByteArray &preprocessedCode,
{ {
Document::Ptr newDoc = Document::create(fileName); Document::Ptr newDoc = Document::create(fileName);
if (Document::Ptr thisDocument = value(fileName)) { if (Document::Ptr thisDocument = document(fileName)) {
newDoc->_revision = thisDocument->_revision; newDoc->_revision = thisDocument->_revision;
newDoc->_lastModified = thisDocument->_lastModified; newDoc->_lastModified = thisDocument->_lastModified;
newDoc->_includes = thisDocument->_includes; newDoc->_includes = thisDocument->_includes;
@@ -474,9 +504,9 @@ QSharedPointer<NamespaceBinding> Snapshot::globalNamespaceBinding(Document::Ptr
return CPlusPlus::bind(doc, *this); return CPlusPlus::bind(doc, *this);
} }
Document::Ptr Snapshot::value(const QString &fileName) const Document::Ptr Snapshot::document(const QString &fileName) const
{ {
return QMap<QString, Document::Ptr>::value(QDir::cleanPath(fileName)); return _documents.value(QDir::cleanPath(fileName));
} }
Snapshot Snapshot::simplified(Document::Ptr doc) const Snapshot Snapshot::simplified(Document::Ptr doc) const
@@ -495,7 +525,7 @@ void Snapshot::simplified_helper(Document::Ptr doc, Snapshot *snapshot) const
snapshot->insert(doc); snapshot->insert(doc);
foreach (const Document::Include &incl, doc->includes()) { foreach (const Document::Include &incl, doc->includes()) {
Document::Ptr includedDoc = value(incl.fileName()); Document::Ptr includedDoc = document(incl.fileName());
simplified_helper(includedDoc, snapshot); simplified_helper(includedDoc, snapshot);
} }
} }
@@ -559,15 +589,14 @@ void Snapshot::dependency_helper(QVector<QString> &files,
QHash<int, QList<int> > &includes, QHash<int, QList<int> > &includes,
QVector<QBitArray> &includeMap) const QVector<QBitArray> &includeMap) const
{ {
QMapIterator<QString, Document::Ptr> it(*this); int i = 0;
for (int i = 0; it.hasNext(); ++i) { for (const_iterator it = begin(); it != end(); ++it, ++i) {
it.next();
files[i] = it.key(); files[i] = it.key();
fileIndex[it.key()] = i; fileIndex[it.key()] = i;
} }
for (int i = 0; i < files.size(); ++i) { for (int i = 0; i < files.size(); ++i) {
if (Document::Ptr doc = value(files.at(i))) { if (Document::Ptr doc = document(files.at(i))) {
QBitArray bitmap(files.size()); QBitArray bitmap(files.size());
QList<int> directIncludes; QList<int> directIncludes;

View File

@@ -321,7 +321,7 @@ private:
friend class Snapshot; friend class Snapshot;
}; };
class CPLUSPLUS_EXPORT Snapshot: public QMap<QString, Document::Ptr> class CPLUSPLUS_EXPORT Snapshot
{ {
typedef QMap<QString, Document::Ptr> _Base; typedef QMap<QString, Document::Ptr> _Base;
@@ -329,6 +329,24 @@ public:
Snapshot(); Snapshot();
~Snapshot(); ~Snapshot();
typedef _Base::const_iterator iterator;
typedef _Base::const_iterator const_iterator;
int size() const; // ### remove
bool isEmpty() const;
void insert(Document::Ptr doc); // ### remove
void remove(const QString &fileName); // ### remove
const_iterator begin() const { return _documents.begin(); }
const_iterator end() const { return _documents.end(); }
bool contains(const QString &fileName) const;
Document::Ptr document(const QString &fileName) const;
Document::Ptr operator[](const QString &fileName) const;
const_iterator find(const QString &fileName) const;
Snapshot simplified(Document::Ptr doc) const; Snapshot simplified(Document::Ptr doc) const;
QByteArray preprocessedCode(const QString &source, QByteArray preprocessedCode(const QString &source,
@@ -342,17 +360,15 @@ public:
QStringList filesDependingOn(const QString &fileName) const; QStringList filesDependingOn(const QString &fileName) const;
QMap<QString, QStringList> dependencyTable() const; QMap<QString, QStringList> dependencyTable() const;
void insert(Document::Ptr doc);
Document::Ptr value(const QString &fileName) const;
using _Base::insert;
private: private:
void simplified_helper(Document::Ptr doc, Snapshot *snapshot) const; void simplified_helper(Document::Ptr doc, Snapshot *snapshot) const;
void dependency_helper(QVector<QString> &files, void dependency_helper(QVector<QString> &files,
QHash<QString, int> &fileIndex, QHash<QString, int> &fileIndex,
QHash<int, QList<int> > &includes, QHash<int, QList<int> > &includes,
QVector<QBitArray> &includeMap) const; QVector<QBitArray> &includeMap) const;
private:
_Base _documents;
}; };
} // end of namespace CPlusPlus } // end of namespace CPlusPlus

View File

@@ -43,7 +43,7 @@ QByteArray FastPreprocessor::run(QString fileName, const QString &source)
{ {
_preproc.setExpandMacros(false); _preproc.setExpandMacros(false);
if (Document::Ptr doc = _snapshot.value(fileName)) { if (Document::Ptr doc = _snapshot.document(fileName)) {
_merged.insert(fileName); _merged.insert(fileName);
foreach (const Document::Include &i, doc->includes()) foreach (const Document::Include &i, doc->includes())
@@ -62,7 +62,7 @@ void FastPreprocessor::mergeEnvironment(const QString &fileName)
if (! _merged.contains(fileName)) { if (! _merged.contains(fileName)) {
_merged.insert(fileName); _merged.insert(fileName);
if (Document::Ptr doc = _snapshot.value(fileName)) { if (Document::Ptr doc = _snapshot.document(fileName)) {
foreach (const Document::Include &i, doc->includes()) foreach (const Document::Include &i, doc->includes())
mergeEnvironment(i.fileName()); mergeEnvironment(i.fileName());

View File

@@ -89,7 +89,7 @@ Document::Ptr LookupContext::thisDocument() const
{ return _thisDocument; } { return _thisDocument; }
Document::Ptr LookupContext::document(const QString &fileName) const Document::Ptr LookupContext::document(const QString &fileName) const
{ return _snapshot.value(fileName); } { return _snapshot.document(fileName); }
Snapshot LookupContext::snapshot() const Snapshot LookupContext::snapshot() const
{ return _snapshot; } { return _snapshot; }
@@ -317,7 +317,7 @@ void LookupContext::buildVisibleScopes_helper(Document::Ptr doc, QList<Scope *>
scopes->append(doc->globalSymbols()); scopes->append(doc->globalSymbols());
foreach (const Document::Include &incl, doc->includes()) { foreach (const Document::Include &incl, doc->includes()) {
buildVisibleScopes_helper(_snapshot.value(incl.fileName()), buildVisibleScopes_helper(_snapshot.document(incl.fileName()),
scopes, processed); scopes, processed);
} }
} }

View File

@@ -123,7 +123,7 @@ void TypeOfExpression::processEnvironment(Snapshot documents,
processed->insert(doc->fileName()); processed->insert(doc->fileName());
foreach (const Document::Include &incl, doc->includes()) { foreach (const Document::Include &incl, doc->includes()) {
processEnvironment(documents, processEnvironment(documents,
documents.value(incl.fileName()), documents.document(incl.fileName()),
env, processed); env, processed);
} }
foreach (const Macro &macro, doc->definedMacros()) foreach (const Macro &macro, doc->definedMacros())

View File

@@ -1162,7 +1162,7 @@ void CPPEditor::switchDeclarationDefinition()
const Snapshot snapshot = m_modelManager->snapshot(); const Snapshot snapshot = m_modelManager->snapshot();
Document::Ptr doc = snapshot.value(file()->fileName()); Document::Ptr doc = snapshot.document(file()->fileName());
if (!doc) if (!doc)
return; return;
Symbol *lastSymbol = doc->findSymbolAt(line, column); Symbol *lastSymbol = doc->findSymbolAt(line, column);
@@ -1215,7 +1215,7 @@ CPPEditor::Link CPPEditor::findLinkAt(const QTextCursor &cursor,
const Snapshot snapshot = m_modelManager->snapshot(); const Snapshot snapshot = m_modelManager->snapshot();
int line = 0, column = 0; int line = 0, column = 0;
convertPosition(cursor.position(), &line, &column); convertPosition(cursor.position(), &line, &column);
Document::Ptr doc = snapshot.value(file()->fileName()); Document::Ptr doc = snapshot.document(file()->fileName());
if (!doc) if (!doc)
return link; return link;
@@ -1391,7 +1391,7 @@ Symbol *CPPEditor::findDefinition(Symbol *symbol)
it.next(); it.next();
// get the instance of the document. // get the instance of the document.
Document::Ptr thisDocument = snapshot.value(it.key()); Document::Ptr thisDocument = snapshot.document(it.key());
foreach (Function *f, it.value()) { foreach (Function *f, it.value()) {
// create a lookup context // create a lookup context

View File

@@ -126,7 +126,7 @@ public:
void clear() void clear()
{ {
snapshot.clear(); snapshot = CPlusPlus::Snapshot();
fileName.clear(); fileName.clear();
code.clear(); code.clear();
line = 0; line = 0;

View File

@@ -268,7 +268,7 @@ void CppHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in
const Snapshot documents = m_modelManager->snapshot(); const Snapshot documents = m_modelManager->snapshot();
const QString fileName = editor->file()->fileName(); const QString fileName = editor->file()->fileName();
Document::Ptr doc = documents.value(fileName); Document::Ptr doc = documents.document(fileName);
if (!doc) if (!doc)
return; // nothing to do return; // nothing to do

View File

@@ -62,7 +62,7 @@ QString AbstractEditorSupport::functionAt(const CppModelManagerInterface *modelM
int line, int column) int line, int column)
{ {
const CPlusPlus::Snapshot snapshot = modelManager->snapshot(); const CPlusPlus::Snapshot snapshot = modelManager->snapshot();
const CPlusPlus::Document::Ptr document = snapshot.value(fileName); const CPlusPlus::Document::Ptr document = snapshot.document(fileName);
if (!document) if (!document)
return QString(); return QString();
if (const CPlusPlus::Symbol *symbol = document->findSymbolAt(line, column)) if (const CPlusPlus::Symbol *symbol = document->findSymbolAt(line, column))

View File

@@ -793,7 +793,7 @@ int CppCodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
const Snapshot snapshot = m_manager->snapshot(); const Snapshot snapshot = m_manager->snapshot();
if (Document::Ptr thisDocument = snapshot.value(fileName)) { if (Document::Ptr thisDocument = snapshot.document(fileName)) {
Symbol *lastVisibleSymbol = thisDocument->findSymbolAt(line, column); Symbol *lastVisibleSymbol = thisDocument->findSymbolAt(line, column);
typeOfExpression.setSnapshot(m_manager->snapshot()); typeOfExpression.setSnapshot(m_manager->snapshot());

View File

@@ -74,7 +74,7 @@ QList<Locator::FilterEntry> CppCurrentDocumentFilter::matchesFor(const QString &
if (m_itemsOfCurrentDoc.isEmpty()) { if (m_itemsOfCurrentDoc.isEmpty()) {
Snapshot snapshot = m_modelManager->snapshot(); Snapshot snapshot = m_modelManager->snapshot();
Document::Ptr thisDocument = snapshot.value(m_currentFileName); Document::Ptr thisDocument = snapshot.document(m_currentFileName);
if (thisDocument) if (thisDocument)
m_itemsOfCurrentDoc = search(thisDocument); m_itemsOfCurrentDoc = search(thisDocument);
} }

View File

@@ -110,7 +110,7 @@ public:
QList<Usage> usages; QList<Usage> usages;
const Identifier *symbolId = symbol->identifier(); const Identifier *symbolId = symbol->identifier();
if (Document::Ptr previousDoc = snapshot.value(fileName)) { if (Document::Ptr previousDoc = snapshot.document(fileName)) {
Control *control = previousDoc->control(); Control *control = previousDoc->control();
if (! control->findIdentifier(symbolId->chars(), symbolId->size())) if (! control->findIdentifier(symbolId->chars(), symbolId->size()))
return usages; // skip this document, it's not using symbolId. return usages; // skip this document, it's not using symbolId.

View File

@@ -502,7 +502,7 @@ void CppPreprocessor::mergeEnvironment(Document::Ptr doc)
foreach (const Document::Include &incl, doc->includes()) { foreach (const Document::Include &incl, doc->includes()) {
QString includedFile = incl.fileName(); QString includedFile = incl.fileName();
if (Document::Ptr includedDoc = snapshot.value(includedFile)) if (Document::Ptr includedDoc = snapshot.document(includedFile))
mergeEnvironment(includedDoc); mergeEnvironment(includedDoc);
else else
run(includedFile); run(includedFile);
@@ -553,7 +553,7 @@ void CppPreprocessor::sourceNeeded(QString &fileName, IncludeType type,
//qDebug() << "parse file:" << fileName << "contents:" << contents.size(); //qDebug() << "parse file:" << fileName << "contents:" << contents.size();
Document::Ptr doc = snapshot.value(fileName); Document::Ptr doc = snapshot.document(fileName);
if (doc) { if (doc) {
mergeEnvironment(doc); mergeEnvironment(doc);
return; return;
@@ -574,7 +574,7 @@ void CppPreprocessor::sourceNeeded(QString &fileName, IncludeType type,
doc->tokenize(); doc->tokenize();
doc->releaseSource(); doc->releaseSource();
snapshot.insert(doc->fileName(), doc); snapshot.insert(doc);
m_todo.remove(fileName); m_todo.remove(fileName);
Process process(m_modelManager, snapshot, m_workingCopy); Process process(m_modelManager, snapshot, m_workingCopy);
@@ -955,7 +955,7 @@ void CppModelManager::onDocumentUpdated(Document::Ptr doc)
protectSnapshot.lock(); protectSnapshot.lock();
Document::Ptr previous = m_snapshot.value(fileName); Document::Ptr previous = m_snapshot.document(fileName);
if (previous && (doc->revision() != 0 && doc->revision() < previous->revision())) if (previous && (doc->revision() != 0 && doc->revision() < previous->revision()))
outdated = true; outdated = true;
@@ -1351,7 +1351,7 @@ void CppModelManager::parse(QFutureInterface<void> &future,
void CppModelManager::GC() void CppModelManager::GC()
{ {
protectSnapshot.lock(); protectSnapshot.lock();
Snapshot documents = m_snapshot; Snapshot currentSnapshot = m_snapshot;
protectSnapshot.unlock(); protectSnapshot.unlock();
QSet<QString> processed; QSet<QString> processed;
@@ -1366,26 +1366,27 @@ void CppModelManager::GC()
processed.insert(fn); processed.insert(fn);
if (Document::Ptr doc = documents.value(fn)) { if (Document::Ptr doc = currentSnapshot.document(fn)) {
todo += doc->includedFiles(); todo += doc->includedFiles();
} }
} }
QStringList removedFiles; QStringList removedFiles;
QMutableMapIterator<QString, Document::Ptr> it(documents);
while (it.hasNext()) { Snapshot newSnapshot;
it.next(); for (Snapshot::const_iterator it = currentSnapshot.begin(); it != currentSnapshot.end(); ++it) {
const QString fn = it.key(); const QString fileName = it.key();
if (! processed.contains(fn)) {
removedFiles.append(fn); if (processed.contains(fileName))
it.remove(); newSnapshot.insert(it.value());
} else
removedFiles.append(fileName);
} }
emit aboutToRemoveFiles(removedFiles); emit aboutToRemoveFiles(removedFiles);
protectSnapshot.lock(); protectSnapshot.lock();
m_snapshot = documents; m_snapshot = newSnapshot;
protectSnapshot.unlock(); protectSnapshot.unlock();
} }

View File

@@ -619,7 +619,7 @@ const CPlusPlus::Snapshot &DebuggerManager::cppCodeModelSnapshot() const
void DebuggerManager::clearCppCodeModelSnapshot() void DebuggerManager::clearCppCodeModelSnapshot()
{ {
d->m_codeModelSnapshot.clear(); d->m_codeModelSnapshot = CPlusPlus::Snapshot();
} }
SourceFilesWindow *DebuggerManager::sourceFileWindow() const SourceFilesWindow *DebuggerManager::sourceFileWindow() const
@@ -1027,7 +1027,7 @@ void DebuggerManager::exitDebugger()
// in turn will handle the cleanup. // in turn will handle the cleanup.
if (d->m_engine && state() != DebuggerNotReady) if (d->m_engine && state() != DebuggerNotReady)
d->m_engine->exitDebugger(); d->m_engine->exitDebugger();
d->m_codeModelSnapshot.clear(); d->m_codeModelSnapshot = CPlusPlus::Snapshot();
} }
DebuggerStartParametersPtr DebuggerManager::startParameters() const DebuggerStartParametersPtr DebuggerManager::startParameters() const

View File

@@ -354,10 +354,10 @@ int getUninitializedVariablesI(const CPlusPlus::Snapshot &snapshot,
{ {
uninitializedVariables->clear(); uninitializedVariables->clear();
// Find document // Find document
if (snapshot.empty() || functionName.isEmpty() || file.isEmpty() || line < 1) if (snapshot.isEmpty() || functionName.isEmpty() || file.isEmpty() || line < 1)
return 1; return 1;
const CPlusPlus::Snapshot::ConstIterator docIt = snapshot.constFind(file); const CPlusPlus::Snapshot::const_iterator docIt = snapshot.find(file);
if (docIt == snapshot.constEnd()) if (docIt == snapshot.end())
return 2; return 2;
const CPlusPlus::Document::Ptr doc = docIt.value(); const CPlusPlus::Document::Ptr doc = docIt.value();
// Look at symbol at line and find its function. Either it is the // Look at symbol at line and find its function. Either it is the
@@ -416,7 +416,7 @@ bool getUninitializedVariables(const CPlusPlus::Snapshot &snapshot,
<< " returns (int) " << rc << " '" << " returns (int) " << rc << " '"
<< uninitializedVariables->join(QString(QLatin1Char(','))) << '\''; << uninitializedVariables->join(QString(QLatin1Char(','))) << '\'';
if (rc) if (rc)
str << " of " << snapshot.keys().size() << " documents"; str << " of " << snapshot.size() << " documents";
qDebug() << msg; qDebug() << msg;
} }
return rc == 0; return rc == 0;

View File

@@ -130,7 +130,7 @@ bool navigateToSlot(const QString &uiFileName,
return false; return false;
} }
const CPlusPlus::Snapshot snapshot = CppTools::CppModelManagerInterface::instance()->snapshot(); const CPlusPlus::Snapshot snapshot = CppTools::CppModelManagerInterface::instance()->snapshot();
const DocumentPtr generatedHeaderDoc = snapshot.value(generatedHeaderFile); const DocumentPtr generatedHeaderDoc = snapshot.document(generatedHeaderFile);
if (!generatedHeaderDoc) { if (!generatedHeaderDoc) {
*errorMessage = QCoreApplication::translate("Designer", "The generated header '%1' could not be found in the code model.\nRebuilding the project might help.").arg(generatedHeaderFile); *errorMessage = QCoreApplication::translate("Designer", "The generated header '%1' could not be found in the code model.\nRebuilding the project might help.").arg(generatedHeaderFile);
return false; return false;

View File

@@ -530,8 +530,8 @@ static ClassDocumentPtrPair
// Check the includes // Check the includes
const unsigned recursionMaxIncludeDepth = maxIncludeDepth - 1u; const unsigned recursionMaxIncludeDepth = maxIncludeDepth - 1u;
foreach (const QString &include, doc->includedFiles()) { foreach (const QString &include, doc->includedFiles()) {
const CPlusPlus::Snapshot::const_iterator it = docTable.constFind(include); const CPlusPlus::Snapshot::const_iterator it = docTable.find(include);
if (it != docTable.constEnd()) { if (it != docTable.end()) {
const Document::Ptr includeDoc = it.value(); const Document::Ptr includeDoc = it.value();
const ClassDocumentPtrPair irc = findClassRecursively(docTable, it.value(), className, recursionMaxIncludeDepth, namespaceName); const ClassDocumentPtrPair irc = findClassRecursively(docTable, it.value(), className, recursionMaxIncludeDepth, namespaceName);
if (irc.first) if (irc.first)
@@ -589,14 +589,16 @@ bool QtCreatorIntegration::navigateToSlot(const QString &objectName,
return false; return false;
} }
CPlusPlus::Snapshot docTable = cppModelManagerInstance()->snapshot(); CPlusPlus::Snapshot docTable = cppModelManagerInstance()->snapshot();
for (CPlusPlus::Snapshot::iterator it = docTable.begin(); it != docTable.end(); ) { CPlusPlus::Snapshot newDocTable;
for (CPlusPlus::Snapshot::iterator it = docTable.begin(); it != docTable.end(); ++it) {
const ProjectExplorer::Project *project = ProjectExplorer::ProjectExplorerPlugin::instance()->session()->projectForFile(it.key()); const ProjectExplorer::Project *project = ProjectExplorer::ProjectExplorerPlugin::instance()->session()->projectForFile(it.key());
if (project == uiProject) { if (project == uiProject)
++it; newDocTable.insert(it.value());
} else {
it = docTable.erase(it);
}
} }
docTable = newDocTable;
// take all docs, find the ones that include the ui_xx.h. // take all docs, find the ones that include the ui_xx.h.
QList<Document::Ptr> docList = findDocumentsIncluding(docTable, uicedName, true); // change to false when we know the absolute path to generated ui_<>.h file QList<Document::Ptr> docList = findDocumentsIncluding(docTable, uicedName, true); // change to false when we know the absolute path to generated ui_<>.h file