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