From 14376c3c32d2eba270752ada6f6e6c5825e6dfe1 Mon Sep 17 00:00:00 2001 From: Roberto Raggi <roberto.raggi@nokia.com> Date: Mon, 7 Dec 2009 10:54:27 +0100 Subject: [PATCH] Initial work on the new CPlusPlus::Snapshot. Encapsulate the details. --- src/libs/cplusplus/CppBindings.cpp | 2 +- src/libs/cplusplus/CppDocument.cpp | 47 +++++++++++++++---- src/libs/cplusplus/CppDocument.h | 28 ++++++++--- src/libs/cplusplus/FastPreprocessor.cpp | 4 +- src/libs/cplusplus/LookupContext.cpp | 4 +- src/libs/cplusplus/TypeOfExpression.cpp | 2 +- src/plugins/cppeditor/cppeditor.cpp | 6 +-- src/plugins/cppeditor/cppeditor.h | 2 +- src/plugins/cppeditor/cpphoverhandler.cpp | 2 +- .../cpptools/abstracteditorsupport.cpp | 2 +- src/plugins/cpptools/cppcodecompletion.cpp | 2 +- .../cpptools/cppcurrentdocumentfilter.cpp | 2 +- src/plugins/cpptools/cppfindreferences.cpp | 2 +- src/plugins/cpptools/cppmodelmanager.cpp | 31 ++++++------ src/plugins/debugger/debuggermanager.cpp | 4 +- src/plugins/debugger/watchutils.cpp | 8 ++-- src/plugins/designer/codemodelhelpers.cpp | 2 +- src/plugins/designer/qtcreatorintegration.cpp | 18 +++---- 18 files changed, 108 insertions(+), 60 deletions(-) diff --git a/src/libs/cplusplus/CppBindings.cpp b/src/libs/cplusplus/CppBindings.cpp index d9b0a32eacd..5f5d9a77d0f 100644 --- a/src/libs/cplusplus/CppBindings.cpp +++ b/src/libs/cplusplus/CppBindings.cpp @@ -606,7 +606,7 @@ protected: processed->insert(doc->fileName()); 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); } } diff --git a/src/libs/cplusplus/CppDocument.cpp b/src/libs/cplusplus/CppDocument.cpp index 472e1f30da6..9eeb1311eb4 100644 --- a/src/libs/cplusplus/CppDocument.cpp +++ b/src/libs/cplusplus/CppDocument.cpp @@ -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) { if (doc) - insert(doc->fileName(), doc); + _documents.insert(doc->fileName(), doc); } 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); - if (Document::Ptr thisDocument = value(fileName)) { + if (Document::Ptr thisDocument = document(fileName)) { newDoc->_revision = thisDocument->_revision; newDoc->_lastModified = thisDocument->_lastModified; newDoc->_includes = thisDocument->_includes; @@ -474,9 +504,9 @@ QSharedPointer<NamespaceBinding> Snapshot::globalNamespaceBinding(Document::Ptr 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 @@ -495,7 +525,7 @@ void Snapshot::simplified_helper(Document::Ptr doc, Snapshot *snapshot) const snapshot->insert(doc); foreach (const Document::Include &incl, doc->includes()) { - Document::Ptr includedDoc = value(incl.fileName()); + Document::Ptr includedDoc = document(incl.fileName()); simplified_helper(includedDoc, snapshot); } } @@ -559,15 +589,14 @@ void Snapshot::dependency_helper(QVector<QString> &files, QHash<int, QList<int> > &includes, QVector<QBitArray> &includeMap) const { - QMapIterator<QString, Document::Ptr> it(*this); - for (int i = 0; it.hasNext(); ++i) { - it.next(); + int i = 0; + for (const_iterator it = begin(); it != end(); ++it, ++i) { files[i] = it.key(); fileIndex[it.key()] = 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()); QList<int> directIncludes; diff --git a/src/libs/cplusplus/CppDocument.h b/src/libs/cplusplus/CppDocument.h index b40cb21e6c9..22129ad1870 100644 --- a/src/libs/cplusplus/CppDocument.h +++ b/src/libs/cplusplus/CppDocument.h @@ -321,7 +321,7 @@ private: friend class Snapshot; }; -class CPLUSPLUS_EXPORT Snapshot: public QMap<QString, Document::Ptr> +class CPLUSPLUS_EXPORT Snapshot { typedef QMap<QString, Document::Ptr> _Base; @@ -329,6 +329,24 @@ public: 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; QByteArray preprocessedCode(const QString &source, @@ -342,17 +360,15 @@ public: QStringList filesDependingOn(const QString &fileName) const; QMap<QString, QStringList> dependencyTable() const; - void insert(Document::Ptr doc); - Document::Ptr value(const QString &fileName) const; - - using _Base::insert; - private: void simplified_helper(Document::Ptr doc, Snapshot *snapshot) const; void dependency_helper(QVector<QString> &files, QHash<QString, int> &fileIndex, QHash<int, QList<int> > &includes, QVector<QBitArray> &includeMap) const; + +private: + _Base _documents; }; } // end of namespace CPlusPlus diff --git a/src/libs/cplusplus/FastPreprocessor.cpp b/src/libs/cplusplus/FastPreprocessor.cpp index b61f61cee76..78de8d1b42f 100644 --- a/src/libs/cplusplus/FastPreprocessor.cpp +++ b/src/libs/cplusplus/FastPreprocessor.cpp @@ -43,7 +43,7 @@ QByteArray FastPreprocessor::run(QString fileName, const QString &source) { _preproc.setExpandMacros(false); - if (Document::Ptr doc = _snapshot.value(fileName)) { + if (Document::Ptr doc = _snapshot.document(fileName)) { _merged.insert(fileName); foreach (const Document::Include &i, doc->includes()) @@ -62,7 +62,7 @@ void FastPreprocessor::mergeEnvironment(const QString &fileName) if (! _merged.contains(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()) mergeEnvironment(i.fileName()); diff --git a/src/libs/cplusplus/LookupContext.cpp b/src/libs/cplusplus/LookupContext.cpp index 942c0dc764e..cbd0bce1df0 100644 --- a/src/libs/cplusplus/LookupContext.cpp +++ b/src/libs/cplusplus/LookupContext.cpp @@ -89,7 +89,7 @@ Document::Ptr LookupContext::thisDocument() const { return _thisDocument; } Document::Ptr LookupContext::document(const QString &fileName) const -{ return _snapshot.value(fileName); } +{ return _snapshot.document(fileName); } Snapshot LookupContext::snapshot() const { return _snapshot; } @@ -317,7 +317,7 @@ void LookupContext::buildVisibleScopes_helper(Document::Ptr doc, QList<Scope *> scopes->append(doc->globalSymbols()); foreach (const Document::Include &incl, doc->includes()) { - buildVisibleScopes_helper(_snapshot.value(incl.fileName()), + buildVisibleScopes_helper(_snapshot.document(incl.fileName()), scopes, processed); } } diff --git a/src/libs/cplusplus/TypeOfExpression.cpp b/src/libs/cplusplus/TypeOfExpression.cpp index 8450bac236c..578944dd126 100644 --- a/src/libs/cplusplus/TypeOfExpression.cpp +++ b/src/libs/cplusplus/TypeOfExpression.cpp @@ -123,7 +123,7 @@ void TypeOfExpression::processEnvironment(Snapshot documents, processed->insert(doc->fileName()); foreach (const Document::Include &incl, doc->includes()) { processEnvironment(documents, - documents.value(incl.fileName()), + documents.document(incl.fileName()), env, processed); } foreach (const Macro ¯o, doc->definedMacros()) diff --git a/src/plugins/cppeditor/cppeditor.cpp b/src/plugins/cppeditor/cppeditor.cpp index 3457e4cffa6..4d442258cd2 100644 --- a/src/plugins/cppeditor/cppeditor.cpp +++ b/src/plugins/cppeditor/cppeditor.cpp @@ -1162,7 +1162,7 @@ void CPPEditor::switchDeclarationDefinition() const Snapshot snapshot = m_modelManager->snapshot(); - Document::Ptr doc = snapshot.value(file()->fileName()); + Document::Ptr doc = snapshot.document(file()->fileName()); if (!doc) return; Symbol *lastSymbol = doc->findSymbolAt(line, column); @@ -1215,7 +1215,7 @@ CPPEditor::Link CPPEditor::findLinkAt(const QTextCursor &cursor, const Snapshot snapshot = m_modelManager->snapshot(); int line = 0, column = 0; convertPosition(cursor.position(), &line, &column); - Document::Ptr doc = snapshot.value(file()->fileName()); + Document::Ptr doc = snapshot.document(file()->fileName()); if (!doc) return link; @@ -1391,7 +1391,7 @@ Symbol *CPPEditor::findDefinition(Symbol *symbol) it.next(); // 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()) { // create a lookup context diff --git a/src/plugins/cppeditor/cppeditor.h b/src/plugins/cppeditor/cppeditor.h index 56430c7e62d..3794b4735c4 100644 --- a/src/plugins/cppeditor/cppeditor.h +++ b/src/plugins/cppeditor/cppeditor.h @@ -126,7 +126,7 @@ public: void clear() { - snapshot.clear(); + snapshot = CPlusPlus::Snapshot(); fileName.clear(); code.clear(); line = 0; diff --git a/src/plugins/cppeditor/cpphoverhandler.cpp b/src/plugins/cppeditor/cpphoverhandler.cpp index 3db38f6f716..71f5442a5b4 100644 --- a/src/plugins/cppeditor/cpphoverhandler.cpp +++ b/src/plugins/cppeditor/cpphoverhandler.cpp @@ -268,7 +268,7 @@ void CppHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in const Snapshot documents = m_modelManager->snapshot(); const QString fileName = editor->file()->fileName(); - Document::Ptr doc = documents.value(fileName); + Document::Ptr doc = documents.document(fileName); if (!doc) return; // nothing to do diff --git a/src/plugins/cpptools/abstracteditorsupport.cpp b/src/plugins/cpptools/abstracteditorsupport.cpp index 3c75cbd47a4..4b033fb571e 100644 --- a/src/plugins/cpptools/abstracteditorsupport.cpp +++ b/src/plugins/cpptools/abstracteditorsupport.cpp @@ -62,7 +62,7 @@ QString AbstractEditorSupport::functionAt(const CppModelManagerInterface *modelM int line, int column) { const CPlusPlus::Snapshot snapshot = modelManager->snapshot(); - const CPlusPlus::Document::Ptr document = snapshot.value(fileName); + const CPlusPlus::Document::Ptr document = snapshot.document(fileName); if (!document) return QString(); if (const CPlusPlus::Symbol *symbol = document->findSymbolAt(line, column)) diff --git a/src/plugins/cpptools/cppcodecompletion.cpp b/src/plugins/cpptools/cppcodecompletion.cpp index 5176dd6d307..cab6ed81842 100644 --- a/src/plugins/cpptools/cppcodecompletion.cpp +++ b/src/plugins/cpptools/cppcodecompletion.cpp @@ -793,7 +793,7 @@ int CppCodeCompletion::startCompletion(TextEditor::ITextEditable *editor) 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); typeOfExpression.setSnapshot(m_manager->snapshot()); diff --git a/src/plugins/cpptools/cppcurrentdocumentfilter.cpp b/src/plugins/cpptools/cppcurrentdocumentfilter.cpp index eb6845d9656..933002681aa 100644 --- a/src/plugins/cpptools/cppcurrentdocumentfilter.cpp +++ b/src/plugins/cpptools/cppcurrentdocumentfilter.cpp @@ -74,7 +74,7 @@ QList<Locator::FilterEntry> CppCurrentDocumentFilter::matchesFor(const QString & if (m_itemsOfCurrentDoc.isEmpty()) { Snapshot snapshot = m_modelManager->snapshot(); - Document::Ptr thisDocument = snapshot.value(m_currentFileName); + Document::Ptr thisDocument = snapshot.document(m_currentFileName); if (thisDocument) m_itemsOfCurrentDoc = search(thisDocument); } diff --git a/src/plugins/cpptools/cppfindreferences.cpp b/src/plugins/cpptools/cppfindreferences.cpp index c6cf8642ef8..81d7caadf97 100644 --- a/src/plugins/cpptools/cppfindreferences.cpp +++ b/src/plugins/cpptools/cppfindreferences.cpp @@ -110,7 +110,7 @@ public: QList<Usage> usages; const Identifier *symbolId = symbol->identifier(); - if (Document::Ptr previousDoc = snapshot.value(fileName)) { + if (Document::Ptr previousDoc = snapshot.document(fileName)) { Control *control = previousDoc->control(); if (! control->findIdentifier(symbolId->chars(), symbolId->size())) return usages; // skip this document, it's not using symbolId. diff --git a/src/plugins/cpptools/cppmodelmanager.cpp b/src/plugins/cpptools/cppmodelmanager.cpp index 680a22b238c..76d80b00140 100644 --- a/src/plugins/cpptools/cppmodelmanager.cpp +++ b/src/plugins/cpptools/cppmodelmanager.cpp @@ -502,7 +502,7 @@ void CppPreprocessor::mergeEnvironment(Document::Ptr doc) foreach (const Document::Include &incl, doc->includes()) { QString includedFile = incl.fileName(); - if (Document::Ptr includedDoc = snapshot.value(includedFile)) + if (Document::Ptr includedDoc = snapshot.document(includedFile)) mergeEnvironment(includedDoc); else run(includedFile); @@ -553,7 +553,7 @@ void CppPreprocessor::sourceNeeded(QString &fileName, IncludeType type, //qDebug() << "parse file:" << fileName << "contents:" << contents.size(); - Document::Ptr doc = snapshot.value(fileName); + Document::Ptr doc = snapshot.document(fileName); if (doc) { mergeEnvironment(doc); return; @@ -574,7 +574,7 @@ void CppPreprocessor::sourceNeeded(QString &fileName, IncludeType type, doc->tokenize(); doc->releaseSource(); - snapshot.insert(doc->fileName(), doc); + snapshot.insert(doc); m_todo.remove(fileName); Process process(m_modelManager, snapshot, m_workingCopy); @@ -955,7 +955,7 @@ void CppModelManager::onDocumentUpdated(Document::Ptr doc) 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())) outdated = true; @@ -1351,7 +1351,7 @@ void CppModelManager::parse(QFutureInterface<void> &future, void CppModelManager::GC() { protectSnapshot.lock(); - Snapshot documents = m_snapshot; + Snapshot currentSnapshot = m_snapshot; protectSnapshot.unlock(); QSet<QString> processed; @@ -1366,26 +1366,27 @@ void CppModelManager::GC() processed.insert(fn); - if (Document::Ptr doc = documents.value(fn)) { + if (Document::Ptr doc = currentSnapshot.document(fn)) { todo += doc->includedFiles(); } } QStringList removedFiles; - QMutableMapIterator<QString, Document::Ptr> it(documents); - while (it.hasNext()) { - it.next(); - const QString fn = it.key(); - if (! processed.contains(fn)) { - removedFiles.append(fn); - it.remove(); - } + + Snapshot newSnapshot; + for (Snapshot::const_iterator it = currentSnapshot.begin(); it != currentSnapshot.end(); ++it) { + const QString fileName = it.key(); + + if (processed.contains(fileName)) + newSnapshot.insert(it.value()); + else + removedFiles.append(fileName); } emit aboutToRemoveFiles(removedFiles); protectSnapshot.lock(); - m_snapshot = documents; + m_snapshot = newSnapshot; protectSnapshot.unlock(); } diff --git a/src/plugins/debugger/debuggermanager.cpp b/src/plugins/debugger/debuggermanager.cpp index 43ee5c84fcb..720c50414d2 100644 --- a/src/plugins/debugger/debuggermanager.cpp +++ b/src/plugins/debugger/debuggermanager.cpp @@ -619,7 +619,7 @@ const CPlusPlus::Snapshot &DebuggerManager::cppCodeModelSnapshot() const void DebuggerManager::clearCppCodeModelSnapshot() { - d->m_codeModelSnapshot.clear(); + d->m_codeModelSnapshot = CPlusPlus::Snapshot(); } SourceFilesWindow *DebuggerManager::sourceFileWindow() const @@ -1027,7 +1027,7 @@ void DebuggerManager::exitDebugger() // in turn will handle the cleanup. if (d->m_engine && state() != DebuggerNotReady) d->m_engine->exitDebugger(); - d->m_codeModelSnapshot.clear(); + d->m_codeModelSnapshot = CPlusPlus::Snapshot(); } DebuggerStartParametersPtr DebuggerManager::startParameters() const diff --git a/src/plugins/debugger/watchutils.cpp b/src/plugins/debugger/watchutils.cpp index 1c07ef499e3..5b45d90aff6 100644 --- a/src/plugins/debugger/watchutils.cpp +++ b/src/plugins/debugger/watchutils.cpp @@ -354,10 +354,10 @@ int getUninitializedVariablesI(const CPlusPlus::Snapshot &snapshot, { uninitializedVariables->clear(); // Find document - if (snapshot.empty() || functionName.isEmpty() || file.isEmpty() || line < 1) + if (snapshot.isEmpty() || functionName.isEmpty() || file.isEmpty() || line < 1) return 1; - const CPlusPlus::Snapshot::ConstIterator docIt = snapshot.constFind(file); - if (docIt == snapshot.constEnd()) + const CPlusPlus::Snapshot::const_iterator docIt = snapshot.find(file); + if (docIt == snapshot.end()) return 2; const CPlusPlus::Document::Ptr doc = docIt.value(); // 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 << " '" << uninitializedVariables->join(QString(QLatin1Char(','))) << '\''; if (rc) - str << " of " << snapshot.keys().size() << " documents"; + str << " of " << snapshot.size() << " documents"; qDebug() << msg; } return rc == 0; diff --git a/src/plugins/designer/codemodelhelpers.cpp b/src/plugins/designer/codemodelhelpers.cpp index 5baaeca65c6..b5c2c9ed0c4 100644 --- a/src/plugins/designer/codemodelhelpers.cpp +++ b/src/plugins/designer/codemodelhelpers.cpp @@ -130,7 +130,7 @@ bool navigateToSlot(const QString &uiFileName, return false; } const CPlusPlus::Snapshot snapshot = CppTools::CppModelManagerInterface::instance()->snapshot(); - const DocumentPtr generatedHeaderDoc = snapshot.value(generatedHeaderFile); + const DocumentPtr generatedHeaderDoc = snapshot.document(generatedHeaderFile); 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); return false; diff --git a/src/plugins/designer/qtcreatorintegration.cpp b/src/plugins/designer/qtcreatorintegration.cpp index 3d40f6dd420..3f59839641a 100644 --- a/src/plugins/designer/qtcreatorintegration.cpp +++ b/src/plugins/designer/qtcreatorintegration.cpp @@ -530,8 +530,8 @@ static ClassDocumentPtrPair // Check the includes const unsigned recursionMaxIncludeDepth = maxIncludeDepth - 1u; foreach (const QString &include, doc->includedFiles()) { - const CPlusPlus::Snapshot::const_iterator it = docTable.constFind(include); - if (it != docTable.constEnd()) { + const CPlusPlus::Snapshot::const_iterator it = docTable.find(include); + if (it != docTable.end()) { const Document::Ptr includeDoc = it.value(); const ClassDocumentPtrPair irc = findClassRecursively(docTable, it.value(), className, recursionMaxIncludeDepth, namespaceName); if (irc.first) @@ -589,14 +589,16 @@ bool QtCreatorIntegration::navigateToSlot(const QString &objectName, return false; } 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()); - if (project == uiProject) { - ++it; - } else { - it = docTable.erase(it); - } + if (project == uiProject) + newDocTable.insert(it.value()); } + + docTable = newDocTable; + // 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 -- GitLab