Commit 947207c8 authored by Nikolai Kosjar's avatar Nikolai Kosjar

C++: Fix dangling IDocument* in CppCompletionAssistProcessor

Steps to reproduce the crash:
 1. Open some long file, e.g. botan.cpp
 2. Trigger completion and close editor immediately

The IDocument pointer was only used to reference the file name. Instead
of passing an IDocument* to the IAssistInterface, pass the file name.

Change-Id: Iafce9b818806a77968a10541114bc9b7c8665f11
Reviewed-by: default avatarErik Verbruggen <erik.verbruggen@digia.com>
Reviewed-by: default avatarDavid Schulz <david.schulz@digia.com>
parent 7f61d113
......@@ -74,7 +74,7 @@ const CppQuickFixAssistInterface *CppQuickFixOperation::assistInterface() const
QString CppQuickFixOperation::fileName() const
{
return m_interface->document()->fileName();
return m_interface->fileName();
}
void CppQuickFixFactory::matchingOperations(const QuickFixInterface &interface, QuickFixOperations &result)
......
......@@ -99,7 +99,8 @@ const IAssistProvider *CppQuickFixAssistProcessor::provider() const
// --------------------------
CppQuickFixAssistInterface::CppQuickFixAssistInterface(CPPEditorWidget *editor,
TextEditor::AssistReason reason)
: DefaultAssistInterface(editor->document(), editor->position(), editor->editorDocument(), reason)
: DefaultAssistInterface(editor->document(), editor->position(),
editor->editorDocument()->fileName(), reason)
, m_editor(editor)
, m_semanticInfo(editor->semanticInfo())
, m_snapshot(CPlusPlus::CppModelManagerInterface::instance()->snapshot())
......
......@@ -77,7 +77,7 @@ static QStringList getCompletions(TestData &data, bool *replaceAccessOperator =
QStringList completions;
CppCompletionAssistInterface *ai = new CppCompletionAssistInterface(data.editor->document(), data.pos,
data.editor->editorDocument(), ExplicitlyInvoked,
data.editor->editorDocument()->fileName(), ExplicitlyInvoked,
data.snapshot, QStringList(), QStringList());
CppCompletionAssistProcessor processor;
IAssistProposal *proposal = processor.perform(ai);
......
......@@ -453,7 +453,7 @@ public:
return new CppTools::Internal::CppCompletionAssistInterface(
document,
position,
editor()->document(),
editor()->document()->fileName(),
reason,
modelManager->snapshot(),
includePaths,
......@@ -975,7 +975,7 @@ int CppCompletionAssistProcessor::startCompletionHelper()
int line = 0, column = 0;
Convenience::convertPosition(m_interface->textDocument(), startOfExpression, &line, &column);
const QString fileName = m_interface->document()->fileName();
const QString fileName = m_interface->fileName();
return startCompletionInternal(fileName, line, column, expression, endOfExpression);
}
......@@ -1000,7 +1000,7 @@ bool CppCompletionAssistProcessor::tryObjCCompletion()
const int startPos = tokens[start].begin() + tokens.startPosition();
const QString expr = m_interface->textAt(startPos, m_interface->position() - startPos);
Document::Ptr thisDocument = m_interface->snapshot().document(m_interface->document()->fileName());
Document::Ptr thisDocument = m_interface->snapshot().document(m_interface->fileName());
if (! thisDocument)
return false;
......@@ -1143,7 +1143,7 @@ bool CppCompletionAssistProcessor::completeInclude(const QTextCursor &cursor)
// Make completion for all relevant includes
QStringList includePaths = m_interface->includePaths();
const QString &currentFilePath = QFileInfo(m_interface->document()->fileName()).path();
const QString &currentFilePath = QFileInfo(m_interface->fileName()).path();
if (!includePaths.contains(currentFilePath))
includePaths.append(currentFilePath);
......@@ -1204,7 +1204,7 @@ bool CppCompletionAssistProcessor::objcKeywordsWanted() const
if (!m_objcEnabled)
return false;
const QString fileName = m_interface->document()->fileName();
const QString fileName = m_interface->fileName();
const Core::MimeDatabase *mdb = Core::ICore::mimeDatabase();
return mdb->findByFile(fileName).type() == QLatin1String(CppTools::Constants::OBJECTIVE_CPP_SOURCE_MIMETYPE);
......
......@@ -167,12 +167,12 @@ class CppCompletionAssistInterface : public TextEditor::DefaultAssistInterface
public:
CppCompletionAssistInterface(QTextDocument *textDocument,
int position,
Core::IDocument *document,
const QString &fileName,
TextEditor::AssistReason reason,
const CPlusPlus::Snapshot &snapshot,
const QStringList &includePaths,
const QStringList &frameworkPaths)
: TextEditor::DefaultAssistInterface(textDocument, position, document, reason)
: TextEditor::DefaultAssistInterface(textDocument, position, fileName, reason)
, m_snapshot(snapshot)
, m_includePaths(includePaths)
, m_frameworkPaths(frameworkPaths)
......
......@@ -463,11 +463,11 @@ void GLSLCompletionAssistProcessor::addCompletion(const QString &text,
// -----------------------------
GLSLCompletionAssistInterface::GLSLCompletionAssistInterface(QTextDocument *textDocument,
int position,
Core::IDocument *document,
const QString &fileName,
TextEditor::AssistReason reason,
const QString &mimeType,
const Document::Ptr &glslDoc)
: DefaultAssistInterface(textDocument, position, document, reason)
: DefaultAssistInterface(textDocument, position, fileName, reason)
, m_mimeType(mimeType)
, m_glslDoc(glslDoc)
{
......
......@@ -96,7 +96,7 @@ class GLSLCompletionAssistInterface : public TextEditor::DefaultAssistInterface
{
public:
GLSLCompletionAssistInterface(QTextDocument *textDocument,
int position, Core::IDocument *document,
int position, const QString &fileName,
TextEditor::AssistReason reason,
const QString &mimeType,
const Document::Ptr &glslDoc);
......
......@@ -416,7 +416,7 @@ TextEditor::IAssistInterface *GLSLTextEditorWidget::createAssistInterface(
if (kind == TextEditor::Completion)
return new GLSLCompletionAssistInterface(document(),
position(),
editor()->document(),
editor()->document()->fileName(),
reason,
mimeType(),
glslDocument());
......
......@@ -530,7 +530,7 @@ IAssistProposal *QmlJSCompletionAssistProcessor::perform(const IAssistInterface
if (assistInterface->reason() == IdleEditor && !acceptsIdleEditor())
return 0;
const QString &fileName = m_interface->document()->fileName();
const QString &fileName = m_interface->fileName();
m_startPosition = assistInterface->position();
while (isIdentifierChar(m_interface->textDocument()->characterAt(m_startPosition - 1), false, false))
......@@ -964,10 +964,10 @@ bool QmlJSCompletionAssistProcessor::completeUrl(const QString &relativeBasePath
// ------------------------------
QmlJSCompletionAssistInterface::QmlJSCompletionAssistInterface(QTextDocument *textDocument,
int position,
Core::IDocument *document,
const QString &fileName,
TextEditor::AssistReason reason,
const SemanticInfo &info)
: DefaultAssistInterface(textDocument, position, document, reason)
: DefaultAssistInterface(textDocument, position, fileName, reason)
, m_semanticInfo(info)
, m_darkBlueIcon(iconForColor(Qt::darkBlue))
, m_darkYellowIcon(iconForColor(Qt::darkYellow))
......
......@@ -120,7 +120,7 @@ class QmlJSCompletionAssistInterface : public TextEditor::DefaultAssistInterface
public:
QmlJSCompletionAssistInterface(QTextDocument *textDocument,
int position,
Core::IDocument *document,
const QString &fileName,
TextEditor::AssistReason reason,
const QmlJSTools::SemanticInfo &info);
const QmlJSTools::SemanticInfo &semanticInfo() const;
......
......@@ -1424,7 +1424,7 @@ TextEditor::IAssistInterface *QmlJSTextEditorWidget::createAssistInterface(
if (assistKind == TextEditor::Completion) {
return new QmlJSCompletionAssistInterface(document(),
position(),
editor()->document(),
editor()->document()->fileName(),
reason,
m_semanticInfo);
} else if (assistKind == TextEditor::QuickFix) {
......
......@@ -45,7 +45,8 @@ using namespace TextEditor;
// -----------------------
QmlJSQuickFixAssistInterface::QmlJSQuickFixAssistInterface(QmlJSTextEditorWidget *editor,
TextEditor::AssistReason reason)
: DefaultAssistInterface(editor->document(), editor->position(), editor->editorDocument(), reason)
: DefaultAssistInterface(editor->document(), editor->position(),
editor->editorDocument()->fileName(), reason)
, m_editor(editor)
, m_semanticInfo(editor->semanticInfo())
, m_currentFile(QmlJSRefactoringChanges::file(m_editor, m_semanticInfo.document))
......
......@@ -6681,7 +6681,7 @@ IAssistInterface *BaseTextEditorWidget::createAssistInterface(AssistKind kind,
AssistReason reason) const
{
Q_UNUSED(kind);
return new DefaultAssistInterface(document(), position(), d->m_document.data(), reason);
return new DefaultAssistInterface(document(), position(), d->m_document->fileName(), reason);
}
QString TextEditor::BaseTextEditorWidget::foldReplacementText(const QTextBlock &) const
......
......@@ -39,12 +39,12 @@ using namespace TextEditor;
DefaultAssistInterface::DefaultAssistInterface(QTextDocument *textDocument,
int position,
Core::IDocument *document,
const QString &fileName,
AssistReason reason)
: m_textDocument(textDocument)
, m_isAsync(false)
, m_position(position)
, m_document(document)
, m_fileName(fileName)
, m_reason(reason)
{}
......
......@@ -39,14 +39,14 @@ class TEXTEDITOR_EXPORT DefaultAssistInterface : public IAssistInterface
public:
DefaultAssistInterface(QTextDocument *textDocument,
int position,
Core::IDocument *document,
const QString &fileName,
AssistReason reason);
virtual ~DefaultAssistInterface();
virtual int position() const { return m_position; }
virtual QChar characterAt(int position) const;
virtual QString textAt(int position, int length) const;
virtual const Core::IDocument *document() const { return m_document; }
virtual QString fileName() const { return m_fileName; }
virtual QTextDocument *textDocument() const { return m_textDocument; }
virtual void prepareForAsyncUse();
virtual void recreateTextDocument();
......@@ -56,7 +56,7 @@ private:
QTextDocument *m_textDocument;
bool m_isAsync;
int m_position;
Core::IDocument *m_document;
QString m_fileName;
AssistReason m_reason;
QString m_text;
};
......
......@@ -56,7 +56,7 @@ public:
virtual int position() const = 0;
virtual QChar characterAt(int position) const = 0;
virtual QString textAt(int position, int length) const = 0;
virtual const Core::IDocument *document() const = 0;
virtual QString fileName() const = 0;
virtual QTextDocument *textDocument() const = 0;
virtual void prepareForAsyncUse() = 0;
virtual void recreateTextDocument() = 0;
......
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