Commit bab60d14 authored by Roberto Raggi's avatar Roberto Raggi

Introduced CPlusPlus::Snapshot, it contains a snap shot of the indexer's current state.

This change removes a number of wrong usages of the CppModelManager::documents()/document().
parent ce66bc3c
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
#include <QByteArray> #include <QByteArray>
#include <QList> #include <QList>
#include <QSet> #include <QMap>
#include <QSharedPointer> #include <QSharedPointer>
#include <QString> #include <QString>
#include <QStringList> #include <QStringList>
...@@ -236,6 +236,16 @@ private: ...@@ -236,6 +236,16 @@ private:
QList<MacroUse> _macroUses; QList<MacroUse> _macroUses;
}; };
class CPLUSPLUS_EXPORT Snapshot: public QMap<QString, Document::Ptr>
{
public:
Snapshot()
{ }
~Snapshot()
{ }
};
} // end of namespace CPlusPlus } // end of namespace CPlusPlus
#endif // CPPDOCUMENT_H #endif // CPPDOCUMENT_H
...@@ -77,7 +77,7 @@ LookupContext::LookupContext(Control *control) ...@@ -77,7 +77,7 @@ LookupContext::LookupContext(Control *control)
LookupContext::LookupContext(Symbol *symbol, LookupContext::LookupContext(Symbol *symbol,
Document::Ptr expressionDocument, Document::Ptr expressionDocument,
Document::Ptr thisDocument, Document::Ptr thisDocument,
const QMap<QString, Document::Ptr> &documents) const Snapshot &documents)
: _symbol(symbol), : _symbol(symbol),
_expressionDocument(expressionDocument), _expressionDocument(expressionDocument),
_thisDocument(thisDocument), _thisDocument(thisDocument),
......
...@@ -57,7 +57,7 @@ public: ...@@ -57,7 +57,7 @@ public:
LookupContext(Symbol *symbol, LookupContext(Symbol *symbol,
Document::Ptr expressionDocument, Document::Ptr expressionDocument,
Document::Ptr thisDocument, Document::Ptr thisDocument,
const QMap<QString, Document::Ptr> &documents); const Snapshot &documents);
LookupContext(Symbol *symbol, LookupContext(Symbol *symbol,
const LookupContext &context); const LookupContext &context);
...@@ -87,7 +87,7 @@ public: ...@@ -87,7 +87,7 @@ public:
QList<Symbol *> resolveClassOrNamespace(Name *name) const QList<Symbol *> resolveClassOrNamespace(Name *name) const
{ return resolveClassOrNamespace(name, visibleScopes()); } { return resolveClassOrNamespace(name, visibleScopes()); }
QMap<QString, Document::Ptr> documents() const Snapshot snapshot() const
{ return _documents; } { return _documents; }
enum ResolveMode { enum ResolveMode {
...@@ -140,7 +140,7 @@ private: ...@@ -140,7 +140,7 @@ private:
Document::Ptr _thisDocument; Document::Ptr _thisDocument;
// All documents. // All documents.
QMap<QString, Document::Ptr> _documents; Snapshot _documents;
// Visible scopes. // Visible scopes.
QList<Scope *> _visibleScopes; QList<Scope *> _visibleScopes;
......
...@@ -46,9 +46,9 @@ TypeOfExpression::TypeOfExpression(): ...@@ -46,9 +46,9 @@ TypeOfExpression::TypeOfExpression():
{ {
} }
void TypeOfExpression::setDocuments(const QMap<QString, Document::Ptr> &documents) void TypeOfExpression::setSnapshot(const Snapshot &documents)
{ {
m_documents = documents; m_snapshot = documents;
m_lookupContext = LookupContext(); m_lookupContext = LookupContext();
} }
...@@ -59,12 +59,12 @@ QList<TypeOfExpression::Result> TypeOfExpression::operator()(const QString &expr ...@@ -59,12 +59,12 @@ QList<TypeOfExpression::Result> TypeOfExpression::operator()(const QString &expr
{ {
QString code = expression; QString code = expression;
if (mode == Preprocess) if (mode == Preprocess)
code = preprocessedExpression(expression, m_documents, document); code = preprocessedExpression(expression, m_snapshot, document);
Document::Ptr expressionDoc = documentForExpression(code); Document::Ptr expressionDoc = documentForExpression(code);
m_ast = extractExpressionAST(expressionDoc); m_ast = extractExpressionAST(expressionDoc);
m_lookupContext = LookupContext(lastVisibleSymbol, expressionDoc, m_lookupContext = LookupContext(lastVisibleSymbol, expressionDoc,
document, m_documents); document, m_snapshot);
ResolveExpression resolveExpression(m_lookupContext); ResolveExpression resolveExpression(m_lookupContext);
return resolveExpression(m_ast); return resolveExpression(m_ast);
...@@ -103,7 +103,7 @@ Document::Ptr TypeOfExpression::documentForExpression(const QString &expression) ...@@ -103,7 +103,7 @@ Document::Ptr TypeOfExpression::documentForExpression(const QString &expression)
return doc; return doc;
} }
void TypeOfExpression::processEnvironment(QMap<QString, Document::Ptr> documents, void TypeOfExpression::processEnvironment(Snapshot documents,
Document::Ptr doc, Environment *env, Document::Ptr doc, Environment *env,
QSet<QString> *processed) const QSet<QString> *processed) const
{ {
...@@ -122,7 +122,7 @@ void TypeOfExpression::processEnvironment(QMap<QString, Document::Ptr> documents ...@@ -122,7 +122,7 @@ void TypeOfExpression::processEnvironment(QMap<QString, Document::Ptr> documents
} }
QString TypeOfExpression::preprocessedExpression(const QString &expression, QString TypeOfExpression::preprocessedExpression(const QString &expression,
QMap<QString, Document::Ptr> documents, Snapshot documents,
Document::Ptr thisDocument) const Document::Ptr thisDocument) const
{ {
Environment env; Environment env;
......
...@@ -61,7 +61,7 @@ public: ...@@ -61,7 +61,7 @@ public:
* Also clears the lookup context, so can be used to make sure references * Also clears the lookup context, so can be used to make sure references
* to the documents previously used are removed. * to the documents previously used are removed.
*/ */
void setDocuments(const QMap<QString, Document::Ptr> &documents); void setSnapshot(const Snapshot &documents);
enum PreprocessMode { enum PreprocessMode {
NoPreprocess, NoPreprocess,
...@@ -100,15 +100,15 @@ private: ...@@ -100,15 +100,15 @@ private:
ExpressionAST *extractExpressionAST(Document::Ptr doc) const; ExpressionAST *extractExpressionAST(Document::Ptr doc) const;
Document::Ptr documentForExpression(const QString &expression) const; Document::Ptr documentForExpression(const QString &expression) const;
void processEnvironment(QMap<QString, CPlusPlus::Document::Ptr> documents, void processEnvironment(CPlusPlus::Snapshot documents,
CPlusPlus::Document::Ptr doc, CPlusPlus::Environment *env, CPlusPlus::Document::Ptr doc, CPlusPlus::Environment *env,
QSet<QString> *processed) const; QSet<QString> *processed) const;
QString preprocessedExpression(const QString &expression, QString preprocessedExpression(const QString &expression,
QMap<QString, CPlusPlus::Document::Ptr> documents, CPlusPlus::Snapshot documents,
CPlusPlus::Document::Ptr thisDocument) const; CPlusPlus::Document::Ptr thisDocument) const;
QMap<QString, Document::Ptr> m_documents; Snapshot m_snapshot;
ExpressionAST *m_ast; ExpressionAST *m_ast;
LookupContext m_lookupContext; LookupContext m_lookupContext;
}; };
......
...@@ -427,7 +427,9 @@ void CPPEditor::switchDeclarationDefinition() ...@@ -427,7 +427,9 @@ void CPPEditor::switchDeclarationDefinition()
if (!m_modelManager) if (!m_modelManager)
return; return;
Document::Ptr doc = m_modelManager->document(file()->fileName()); const Snapshot snapshot = m_modelManager->snapshot();
Document::Ptr doc = snapshot.value(file()->fileName());
if (!doc) if (!doc)
return; return;
Symbol *lastSymbol = doc->findSymbolAt(line, column); Symbol *lastSymbol = doc->findSymbolAt(line, column);
...@@ -445,7 +447,7 @@ void CPPEditor::switchDeclarationDefinition() ...@@ -445,7 +447,7 @@ void CPPEditor::switchDeclarationDefinition()
if (f) { if (f) {
TypeOfExpression typeOfExpression; TypeOfExpression typeOfExpression;
typeOfExpression.setDocuments(m_modelManager->documents()); typeOfExpression.setSnapshot(m_modelManager->snapshot());
QList<TypeOfExpression::Result> resolvedSymbols = typeOfExpression(QString(), doc, lastSymbol); QList<TypeOfExpression::Result> resolvedSymbols = typeOfExpression(QString(), doc, lastSymbol);
const LookupContext &context = typeOfExpression.lookupContext(); const LookupContext &context = typeOfExpression.lookupContext();
...@@ -474,10 +476,12 @@ void CPPEditor::jumpToDefinition() ...@@ -474,10 +476,12 @@ void CPPEditor::jumpToDefinition()
if (!m_modelManager) if (!m_modelManager)
return; return;
const Snapshot snapshot = m_modelManager->snapshot();
// Find the last symbol up to the cursor position // Find the last symbol up to the cursor position
int line = 0, column = 0; int line = 0, column = 0;
convertPosition(position(), &line, &column); convertPosition(position(), &line, &column);
Document::Ptr doc = m_modelManager->document(file()->fileName()); Document::Ptr doc = snapshot.value(file()->fileName());
if (!doc) if (!doc)
return; return;
...@@ -503,7 +507,7 @@ void CPPEditor::jumpToDefinition() ...@@ -503,7 +507,7 @@ void CPPEditor::jumpToDefinition()
// Evaluate the type of the expression // Evaluate the type of the expression
TypeOfExpression typeOfExpression; TypeOfExpression typeOfExpression;
typeOfExpression.setDocuments(m_modelManager->documents()); typeOfExpression.setSnapshot(m_modelManager->snapshot());
QList<TypeOfExpression::Result> resolvedSymbols = QList<TypeOfExpression::Result> resolvedSymbols =
typeOfExpression(expression, doc, lastSymbol); typeOfExpression(expression, doc, lastSymbol);
...@@ -572,7 +576,7 @@ Symbol *CPPEditor::findDefinition(Symbol *lastSymbol) ...@@ -572,7 +576,7 @@ Symbol *CPPEditor::findDefinition(Symbol *lastSymbol)
QualifiedNameId *q = control.qualifiedNameId(&qualifiedName[0], qualifiedName.size()); QualifiedNameId *q = control.qualifiedNameId(&qualifiedName[0], qualifiedName.size());
LookupContext context(&control); LookupContext context(&control);
const QMap<QString, Document::Ptr> documents = m_modelManager->documents(); const Snapshot documents = m_modelManager->snapshot();
foreach (Document::Ptr doc, documents) { foreach (Document::Ptr doc, documents) {
QList<Scope *> visibleScopes; QList<Scope *> visibleScopes;
visibleScopes.append(doc->globalSymbols()); visibleScopes.append(doc->globalSymbols());
......
...@@ -434,10 +434,12 @@ int CppCodeCompletion::startCompletion(TextEditor::ITextEditable *editor) ...@@ -434,10 +434,12 @@ int CppCodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
//if (! expression.isEmpty()) //if (! expression.isEmpty())
//qDebug() << "***** expression:" << expression; //qDebug() << "***** expression:" << expression;
if (Document::Ptr thisDocument = m_manager->document(fileName)) { const Snapshot snapshot = m_manager->snapshot();
if (Document::Ptr thisDocument = snapshot.value(fileName)) {
Symbol *symbol = thisDocument->findSymbolAt(line, column); Symbol *symbol = thisDocument->findSymbolAt(line, column);
typeOfExpression.setDocuments(m_manager->documents()); typeOfExpression.setSnapshot(m_manager->snapshot());
QList<TypeOfExpression::Result> resolvedTypes = typeOfExpression(expression, thisDocument, symbol, QList<TypeOfExpression::Result> resolvedTypes = typeOfExpression(expression, thisDocument, symbol,
TypeOfExpression::Preprocess); TypeOfExpression::Preprocess);
...@@ -1034,7 +1036,7 @@ void CppCodeCompletion::cleanup() ...@@ -1034,7 +1036,7 @@ void CppCodeCompletion::cleanup()
// Set empty map in order to avoid referencing old versions of the documents // Set empty map in order to avoid referencing old versions of the documents
// until the next completion // until the next completion
typeOfExpression.setDocuments(QMap<QString, Document::Ptr>()); typeOfExpression.setSnapshot(Snapshot());
} }
int CppCodeCompletion::findStartOfName(const TextEditor::ITextEditor *editor) int CppCodeCompletion::findStartOfName(const TextEditor::ITextEditor *editor)
......
...@@ -165,9 +165,11 @@ void CppHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in ...@@ -165,9 +165,11 @@ void CppHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in
QTextCursor tc(edit->document()); QTextCursor tc(edit->document());
tc.setPosition(pos); tc.setPosition(pos);
const Snapshot documents = m_manager->snapshot();
const int lineNumber = tc.block().blockNumber() + 1; const int lineNumber = tc.block().blockNumber() + 1;
const QString fileName = editor->file()->fileName(); const QString fileName = editor->file()->fileName();
Document::Ptr doc = m_manager->document(fileName); Document::Ptr doc = documents.value(fileName);
if (doc) { if (doc) {
foreach (Document::DiagnosticMessage m, doc->diagnosticMessages()) { foreach (Document::DiagnosticMessage m, doc->diagnosticMessages()) {
if (m.line() == lineNumber) { if (m.line() == lineNumber) {
...@@ -212,7 +214,7 @@ void CppHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in ...@@ -212,7 +214,7 @@ void CppHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in
Symbol *lastSymbol = doc->findSymbolAt(line, column); Symbol *lastSymbol = doc->findSymbolAt(line, column);
TypeOfExpression typeOfExpression; TypeOfExpression typeOfExpression;
typeOfExpression.setDocuments(m_manager->documents()); typeOfExpression.setSnapshot(documents);
QList<TypeOfExpression::Result> types = typeOfExpression(expression, doc, lastSymbol); QList<TypeOfExpression::Result> types = typeOfExpression(expression, doc, lastSymbol);
if (!types.isEmpty()) { if (!types.isEmpty()) {
......
...@@ -143,7 +143,7 @@ protected: ...@@ -143,7 +143,7 @@ protected:
private: private:
QPointer<CppModelManager> m_modelManager; QPointer<CppModelManager> m_modelManager;
CppModelManager::DocumentTable m_documents; Snapshot m_snapshot;
Environment env; Environment env;
pp m_proc; pp m_proc;
QStringList m_includePaths; QStringList m_includePaths;
...@@ -160,7 +160,7 @@ private: ...@@ -160,7 +160,7 @@ private:
CppPreprocessor::CppPreprocessor(QPointer<CppModelManager> modelManager) CppPreprocessor::CppPreprocessor(QPointer<CppModelManager> modelManager)
: m_modelManager(modelManager), : m_modelManager(modelManager),
m_documents(modelManager->documents()), m_snapshot(modelManager->snapshot()),
m_proc(this, env) m_proc(this, env)
{ } { }
...@@ -340,7 +340,7 @@ void CppPreprocessor::mergeEnvironment(Document::Ptr doc, QSet<QString> *process ...@@ -340,7 +340,7 @@ void CppPreprocessor::mergeEnvironment(Document::Ptr doc, QSet<QString> *process
processed->insert(fn); processed->insert(fn);
foreach (QString includedFile, doc->includedFiles()) { foreach (QString includedFile, doc->includedFiles()) {
mergeEnvironment(m_documents.value(includedFile), processed); mergeEnvironment(m_snapshot.value(includedFile), processed);
} }
foreach (const Macro macro, doc->definedMacros()) { foreach (const Macro macro, doc->definedMacros()) {
...@@ -386,7 +386,7 @@ void CppPreprocessor::sourceNeeded(QString &fileName, IncludeType type, ...@@ -386,7 +386,7 @@ void CppPreprocessor::sourceNeeded(QString &fileName, IncludeType type,
} }
if (! contents.isEmpty()) { if (! contents.isEmpty()) {
Document::Ptr cachedDoc = m_documents.value(fileName); Document::Ptr cachedDoc = m_snapshot.value(fileName);
if (cachedDoc && m_currentDoc) { if (cachedDoc && m_currentDoc) {
mergeEnvironment(cachedDoc); mergeEnvironment(cachedDoc);
} else { } else {
...@@ -477,11 +477,8 @@ CppModelManager::CppModelManager(QObject *parent) : ...@@ -477,11 +477,8 @@ CppModelManager::CppModelManager(QObject *parent) :
CppModelManager::~CppModelManager() CppModelManager::~CppModelManager()
{ } { }
Document::Ptr CppModelManager::document(const QString &fileName) const Snapshot CppModelManager::snapshot() const
{ return m_documents.value(fileName); } { return m_snapshot; }
CppModelManager::DocumentTable CppModelManager::documents() const
{ return m_documents; }
void CppModelManager::ensureUpdated() void CppModelManager::ensureUpdated()
{ {
...@@ -672,7 +669,7 @@ void CppModelManager::emitDocumentUpdated(Document::Ptr doc) ...@@ -672,7 +669,7 @@ void CppModelManager::emitDocumentUpdated(Document::Ptr doc)
void CppModelManager::onDocumentUpdated(Document::Ptr doc) void CppModelManager::onDocumentUpdated(Document::Ptr doc)
{ {
const QString fileName = doc->fileName(); const QString fileName = doc->fileName();
m_documents[fileName] = doc; m_snapshot[fileName] = doc;
QList<Core::IEditor *> openedEditors = m_core->editorManager()->openedEditors(); QList<Core::IEditor *> openedEditors = m_core->editorManager()->openedEditors();
foreach (Core::IEditor *editor, openedEditors) { foreach (Core::IEditor *editor, openedEditors) {
if (editor->file()->fileName() == fileName) { if (editor->file()->fileName() == fileName) {
...@@ -837,7 +834,7 @@ void CppModelManager::parse(QFutureInterface<void> &future, ...@@ -837,7 +834,7 @@ void CppModelManager::parse(QFutureInterface<void> &future,
void CppModelManager::GC() void CppModelManager::GC()
{ {
DocumentTable documents = m_documents; Snapshot documents = m_snapshot;
QSet<QString> processed; QSet<QString> processed;
QStringList todo = projectFiles(); QStringList todo = projectFiles();
...@@ -868,7 +865,7 @@ void CppModelManager::GC() ...@@ -868,7 +865,7 @@ void CppModelManager::GC()
} }
emit aboutToRemoveFiles(removedFiles); emit aboutToRemoveFiles(removedFiles);
m_documents = documents; m_snapshot = documents;
} }
...@@ -76,8 +76,7 @@ public: ...@@ -76,8 +76,7 @@ public:
virtual ProjectInfo projectInfo(ProjectExplorer::Project *project) const; virtual ProjectInfo projectInfo(ProjectExplorer::Project *project) const;
virtual void updateProjectInfo(const ProjectInfo &pinfo); virtual void updateProjectInfo(const ProjectInfo &pinfo);
virtual CPlusPlus::Document::Ptr document(const QString &fileName) const; virtual CPlusPlus::Snapshot snapshot() const;
virtual DocumentTable documents() const;
virtual void GC(); virtual void GC();
QFuture<void> refreshSourceFiles(const QStringList &sourceFiles); QFuture<void> refreshSourceFiles(const QStringList &sourceFiles);
...@@ -146,7 +145,7 @@ private: ...@@ -146,7 +145,7 @@ private:
Core::ICore *m_core; Core::ICore *m_core;
ProjectExplorer::ProjectExplorerPlugin *m_projectExplorer; ProjectExplorer::ProjectExplorerPlugin *m_projectExplorer;
CppHoverHandler *m_hoverHandler; CppHoverHandler *m_hoverHandler;
DocumentTable m_documents; CPlusPlus::Snapshot m_snapshot;
// cache // cache
bool m_dirty; bool m_dirty;
......
...@@ -46,14 +46,11 @@ namespace ProjectExplorer { ...@@ -46,14 +46,11 @@ namespace ProjectExplorer {
namespace CppTools { namespace CppTools {
class CPPTOOLS_EXPORT CppModelManagerInterface class CPPTOOLS_EXPORT CppModelManagerInterface: public QObject
: public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
typedef QMap<QString, CPlusPlus::Document::Ptr> DocumentTable; // ### remove me
class ProjectInfo class ProjectInfo
{ {
public: public:
...@@ -89,8 +86,7 @@ public: ...@@ -89,8 +86,7 @@ public:
virtual void GC() = 0; virtual void GC() = 0;
virtual void updateSourceFiles(const QStringList &sourceFiles) = 0; virtual void updateSourceFiles(const QStringList &sourceFiles) = 0;
virtual CPlusPlus::Document::Ptr document(const QString &fileName) const = 0; virtual CPlusPlus::Snapshot snapshot() const = 0;
virtual DocumentTable documents() const = 0;
virtual QList<ProjectInfo> projectInfos() const = 0; virtual QList<ProjectInfo> projectInfos() const = 0;
virtual ProjectInfo projectInfo(ProjectExplorer::Project *project) const = 0; virtual ProjectInfo projectInfo(ProjectExplorer::Project *project) const = 0;
......
...@@ -43,6 +43,7 @@ ...@@ -43,6 +43,7 @@
#include <QIcon> #include <QIcon>
#include <QMetaType> #include <QMetaType>
#include <QString> #include <QString>
#include <QSet>
#include <functional> #include <functional>
......
...@@ -95,7 +95,7 @@ QList<Document::Ptr> WorkbenchIntegration::findDocuments(const QString &uiFileNa ...@@ -95,7 +95,7 @@ QList<Document::Ptr> WorkbenchIntegration::findDocuments(const QString &uiFileNa
QList<Document::Ptr> docList; QList<Document::Ptr> docList;
// take all docs // take all docs
CppTools::CppModelManagerInterface::DocumentTable docTable = cppModelManager->documents(); CPlusPlus::Snapshot docTable = cppModelManager->snapshot();
foreach (Document::Ptr doc, docTable) { // we go through all documents foreach (Document::Ptr doc, docTable) { // we go through all documents
QStringList includes = doc->includedFiles(); QStringList includes = doc->includedFiles();
foreach (QString include, includes) { foreach (QString include, includes) {
...@@ -253,7 +253,7 @@ Document::Ptr WorkbenchIntegration::findDefinition(Function *functionDeclaration ...@@ -253,7 +253,7 @@ Document::Ptr WorkbenchIntegration::findDefinition(Function *functionDeclaration
QualifiedNameId *q = control.qualifiedNameId(&qualifiedName[0], qualifiedName.size()); QualifiedNameId *q = control.qualifiedNameId(&qualifiedName[0], qualifiedName.size());
LookupContext context(&control); LookupContext context(&control);
const QMap<QString, Document::Ptr> documents = cppModelManager->documents(); const Snapshot documents = cppModelManager->snapshot();
foreach (Document::Ptr doc, documents) { foreach (Document::Ptr doc, documents) {
QList<Scope *> visibleScopes; QList<Scope *> visibleScopes;
visibleScopes.append(doc->globalSymbols()); visibleScopes.append(doc->globalSymbols());
......
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