Commit 056db856 authored by Orgad Shaneh's avatar Orgad Shaneh Committed by Orgad Shaneh

CppEditor: Accept LookupContext in virtual function lookup

Required for correct resolving of first virtual appearance

Change-Id: I2307027f769fb2f4c0942f4aa4e0d2b5327562b5
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@digia.com>
parent a28005da
......@@ -128,11 +128,13 @@ bool VirtualFunctionHelper::canLookupVirtualFunctionOverrides(Function *function
if (IdExpressionAST *idExpressionAST = m_baseExpressionAST->asIdExpression()) {
NameAST *name = idExpressionAST->name;
const bool nameIsQualified = name && name->asQualifiedName();
result = !nameIsQualified && FunctionHelper::isVirtualFunction(function, m_snapshot);
result = !nameIsQualified && FunctionHelper::isVirtualFunction(
function, LookupContext(m_document, m_snapshot));
} else if (MemberAccessAST *memberAccessAST = m_baseExpressionAST->asMemberAccess()) {
NameAST *name = memberAccessAST->member_name;
const bool nameIsQualified = name && name->asQualifiedName();
if (!nameIsQualified && FunctionHelper::isVirtualFunction(function, m_snapshot)) {
if (!nameIsQualified && FunctionHelper::isVirtualFunction(
function, LookupContext(m_document, m_snapshot))) {
TranslationUnit *unit = m_expressionDocument->translationUnit();
QTC_ASSERT(unit, return false);
m_accessTokenKind = unit->tokenKind(memberAccessAST->access_token);
......
......@@ -210,7 +210,7 @@ IAssistProcessor *VirtualFunctionAssistProvider::createProcessor() const
enum VirtualType { Virtual, PureVirtual };
static bool isVirtualFunction_helper(const Function *function,
const Snapshot &snapshot,
const LookupContext &context,
VirtualType virtualType)
{
if (!function)
......@@ -222,24 +222,20 @@ static bool isVirtualFunction_helper(const Function *function,
if (function->isVirtual())
return true;
const QString filePath = QString::fromUtf8(function->fileName(), function->fileNameLength());
if (Document::Ptr document = snapshot.document(filePath)) {
LookupContext context(document, snapshot);
QList<LookupItem> results = context.lookup(function->name(), function->enclosingScope());
if (!results.isEmpty()) {
const bool isDestructor = function->name()->isDestructorNameId();
foreach (const LookupItem &item, results) {
if (Symbol *symbol = item.declaration()) {
if (Function *functionType = symbol->type()->asFunctionType()) {
if (functionType->name()->isDestructorNameId() != isDestructor)
continue;
if (functionType == function) // already tested
continue;
if (functionType->isFinal())
return false;
if (functionType->isVirtual())
return true;
}
QList<LookupItem> results = context.lookup(function->name(), function->enclosingScope());
if (!results.isEmpty()) {
const bool isDestructor = function->name()->isDestructorNameId();
foreach (const LookupItem &item, results) {
if (Symbol *symbol = item.declaration()) {
if (Function *functionType = symbol->type()->asFunctionType()) {
if (functionType->name()->isDestructorNameId() != isDestructor)
continue;
if (functionType == function) // already tested
continue;
if (functionType->isFinal())
return false;
if (functionType->isVirtual())
return true;
}
}
}
......@@ -248,14 +244,14 @@ static bool isVirtualFunction_helper(const Function *function,
return false;
}
bool FunctionHelper::isVirtualFunction(const Function *function, const Snapshot &snapshot)
bool FunctionHelper::isVirtualFunction(const Function *function, const LookupContext &context)
{
return isVirtualFunction_helper(function, snapshot, Virtual);
return isVirtualFunction_helper(function, context, Virtual);
}
bool FunctionHelper::isPureVirtualFunction(const Function *function, const Snapshot &snapshot)
bool FunctionHelper::isPureVirtualFunction(const Function *function, const LookupContext &context)
{
return isVirtualFunction_helper(function, snapshot, PureVirtual);
return isVirtualFunction_helper(function, context, PureVirtual);
}
QList<Symbol *> FunctionHelper::overrides(Function *function, Class *functionsClass,
......@@ -342,6 +338,7 @@ void CppEditorPlugin::test_functionhelper_virtualFunctions()
// Iterate through Function symbols
Snapshot snapshot;
snapshot.insert(document);
const LookupContext context(document, snapshot);
Control *control = document->translationUnit()->control();
Symbol **end = control->lastSymbol();
for (Symbol **it = control->firstSymbol(); it != end; ++it) {
......@@ -349,8 +346,8 @@ void CppEditorPlugin::test_functionhelper_virtualFunctions()
if (const Function *function = symbol->asFunction()) {
QTC_ASSERT(!virtualityList.isEmpty(), return);
Virtuality virtuality = virtualityList.takeFirst();
if (FunctionHelper::isVirtualFunction(function, snapshot)) {
if (FunctionHelper::isPureVirtualFunction(function, snapshot))
if (FunctionHelper::isVirtualFunction(function, context)) {
if (FunctionHelper::isPureVirtualFunction(function, context))
QCOMPARE(virtuality, PureVirtual);
else
QCOMPARE(virtuality, Virtual);
......
......@@ -74,10 +74,10 @@ class FunctionHelper
{
public:
static bool isVirtualFunction(const CPlusPlus::Function *function,
const CPlusPlus::Snapshot &snapshot);
const CPlusPlus::LookupContext &context);
static bool isPureVirtualFunction(const CPlusPlus::Function *function,
const CPlusPlus::Snapshot &snapshot);
const CPlusPlus::LookupContext &context);
static QList<CPlusPlus::Symbol *> overrides(CPlusPlus::Function *function,
CPlusPlus::Class *functionsClass,
......
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