2013-07-04 20:11:10 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
|
|
|
|
** Contact: http://www.qt-project.org/legal
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "cppvirtualfunctionassistprovider.h"
|
|
|
|
|
|
2013-12-10 10:47:41 +01:00
|
|
|
#include "cppeditor.h"
|
2013-07-04 20:11:10 +02:00
|
|
|
#include "cppeditorconstants.h"
|
2013-10-19 15:57:12 +02:00
|
|
|
#include "cppvirtualfunctionproposalitem.h"
|
2013-07-04 20:11:10 +02:00
|
|
|
|
|
|
|
|
#include <cplusplus/Icons.h>
|
|
|
|
|
#include <cplusplus/Overview.h>
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
|
|
|
|
#include <coreplugin/actionmanager/command.h>
|
|
|
|
|
|
2013-10-21 13:39:48 +02:00
|
|
|
#include <cpptools/symbolfinder.h>
|
2013-12-10 10:47:41 +01:00
|
|
|
#include <cpptools/typehierarchybuilder.h>
|
2013-10-21 13:39:48 +02:00
|
|
|
|
2013-07-04 20:11:10 +02:00
|
|
|
#include <texteditor/codeassist/basicproposalitemlistmodel.h>
|
|
|
|
|
#include <texteditor/codeassist/genericproposal.h>
|
|
|
|
|
#include <texteditor/codeassist/genericproposalwidget.h>
|
|
|
|
|
#include <texteditor/codeassist/iassistinterface.h>
|
|
|
|
|
#include <texteditor/codeassist/iassistprocessor.h>
|
|
|
|
|
#include <texteditor/codeassist/iassistproposal.h>
|
2013-12-10 10:47:41 +01:00
|
|
|
#include <texteditor/texteditorconstants.h>
|
2013-07-04 20:11:10 +02:00
|
|
|
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
|
|
|
|
using namespace CPlusPlus;
|
|
|
|
|
using namespace CppEditor::Internal;
|
|
|
|
|
using namespace TextEditor;
|
|
|
|
|
|
|
|
|
|
/// Activate current item with the same shortcut that is configured for Follow Symbol Under Cursor.
|
|
|
|
|
/// This is limited to single-key shortcuts without modifiers.
|
|
|
|
|
class VirtualFunctionProposalWidget : public GenericProposalWidget
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
VirtualFunctionProposalWidget(bool openInSplit)
|
|
|
|
|
{
|
|
|
|
|
const char *id = openInSplit
|
|
|
|
|
? TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR_IN_NEXT_SPLIT
|
|
|
|
|
: TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR;
|
|
|
|
|
if (Core::Command *command = Core::ActionManager::command(id))
|
|
|
|
|
m_sequence = command->keySequence();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
bool eventFilter(QObject *o, QEvent *e)
|
|
|
|
|
{
|
|
|
|
|
if (e->type() == QEvent::ShortcutOverride && m_sequence.count() == 1) {
|
|
|
|
|
QKeyEvent *ke = static_cast<QKeyEvent *>(e);
|
|
|
|
|
const QKeySequence seq(ke->key());
|
|
|
|
|
if (seq == m_sequence) {
|
|
|
|
|
activateCurrentProposalItem();
|
|
|
|
|
e->accept();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return GenericProposalWidget::eventFilter(o, e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
QKeySequence m_sequence;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class VirtualFunctionProposal : public GenericProposal
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
VirtualFunctionProposal(int cursorPos, IGenericProposalModel *model, bool openInSplit)
|
|
|
|
|
: GenericProposal(cursorPos, model)
|
|
|
|
|
, m_openInSplit(openInSplit)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
bool isFragile() const
|
|
|
|
|
{ return true; }
|
|
|
|
|
|
|
|
|
|
IAssistProposalWidget *createWidget() const
|
|
|
|
|
{ return new VirtualFunctionProposalWidget(m_openInSplit); }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool m_openInSplit;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class VirtualFunctionsAssistProcessor : public IAssistProcessor
|
|
|
|
|
{
|
|
|
|
|
public:
|
2013-10-23 15:14:28 +02:00
|
|
|
VirtualFunctionsAssistProcessor(const VirtualFunctionAssistProvider::Parameters ¶ms)
|
|
|
|
|
: m_params(params)
|
2013-07-04 20:11:10 +02:00
|
|
|
{}
|
|
|
|
|
|
2013-10-23 16:02:18 +02:00
|
|
|
IAssistProposal *immediateProposal(const TextEditor::IAssistInterface *)
|
2013-07-04 20:11:10 +02:00
|
|
|
{
|
2013-10-23 15:14:28 +02:00
|
|
|
QTC_ASSERT(m_params.function, return 0);
|
2013-07-04 20:11:10 +02:00
|
|
|
|
|
|
|
|
BasicProposalItem *hintItem = new VirtualFunctionProposalItem(CPPEditorWidget::Link());
|
|
|
|
|
hintItem->setText(QCoreApplication::translate("VirtualFunctionsAssistProcessor",
|
|
|
|
|
"...searching overrides"));
|
|
|
|
|
hintItem->setOrder(-1000);
|
|
|
|
|
|
|
|
|
|
QList<BasicProposalItem *> items;
|
2013-10-23 15:14:28 +02:00
|
|
|
items << itemFromSymbol(maybeDefinitionFor(m_params.function));
|
2013-07-04 20:11:10 +02:00
|
|
|
items << hintItem;
|
2013-10-23 16:02:18 +02:00
|
|
|
return new VirtualFunctionProposal(m_params.cursorPosition,
|
2013-07-04 20:11:10 +02:00
|
|
|
new BasicProposalItemListModel(items),
|
2013-10-23 15:14:28 +02:00
|
|
|
m_params.openInNextSplit);
|
2013-07-04 20:11:10 +02:00
|
|
|
}
|
|
|
|
|
|
2013-10-23 16:02:18 +02:00
|
|
|
IAssistProposal *perform(const IAssistInterface *)
|
2013-07-04 20:11:10 +02:00
|
|
|
{
|
2013-10-23 15:14:28 +02:00
|
|
|
QTC_ASSERT(m_params.function, return 0);
|
2013-10-28 15:12:18 +01:00
|
|
|
QTC_ASSERT(m_params.staticClass, return 0);
|
2013-10-23 15:14:28 +02:00
|
|
|
QTC_ASSERT(!m_params.snapshot.isEmpty(), return 0);
|
2013-07-04 20:11:10 +02:00
|
|
|
|
2013-10-25 15:04:51 +02:00
|
|
|
Class *functionsClass = m_finder.findMatchingClassDeclaration(m_params.function,
|
|
|
|
|
m_params.snapshot);
|
|
|
|
|
if (!functionsClass)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2013-10-28 15:12:18 +01:00
|
|
|
const QList<Symbol *> overrides = FunctionHelper::overrides(
|
|
|
|
|
m_params.function, functionsClass, m_params.staticClass, m_params.snapshot);
|
2013-10-21 13:39:48 +02:00
|
|
|
if (overrides.isEmpty())
|
|
|
|
|
return 0;
|
|
|
|
|
|
2013-07-04 20:11:10 +02:00
|
|
|
QList<BasicProposalItem *> items;
|
|
|
|
|
foreach (Symbol *symbol, overrides)
|
2013-10-21 13:39:48 +02:00
|
|
|
items << itemFromSymbol(maybeDefinitionFor(symbol));
|
|
|
|
|
items.first()->setOrder(1000); // Ensure top position for function of static type
|
2013-07-04 20:11:10 +02:00
|
|
|
|
2013-10-23 16:02:18 +02:00
|
|
|
return new VirtualFunctionProposal(m_params.cursorPosition,
|
2013-07-04 20:11:10 +02:00
|
|
|
new BasicProposalItemListModel(items),
|
2013-10-23 15:14:28 +02:00
|
|
|
m_params.openInNextSplit);
|
2013-07-04 20:11:10 +02:00
|
|
|
}
|
|
|
|
|
|
2013-10-21 13:39:48 +02:00
|
|
|
private:
|
|
|
|
|
Symbol *maybeDefinitionFor(Symbol *symbol)
|
|
|
|
|
{
|
2013-10-23 15:14:28 +02:00
|
|
|
if (Function *definition = m_finder.findMatchingDefinition(symbol, m_params.snapshot))
|
2013-10-21 13:39:48 +02:00
|
|
|
return definition;
|
|
|
|
|
return symbol;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BasicProposalItem *itemFromSymbol(Symbol *symbol) const
|
2013-07-04 20:11:10 +02:00
|
|
|
{
|
|
|
|
|
const QString text = m_overview.prettyName(LookupContext::fullyQualifiedName(symbol));
|
|
|
|
|
const CPPEditorWidget::Link link = CPPEditorWidget::linkToSymbol(symbol);
|
|
|
|
|
|
2013-10-23 15:14:28 +02:00
|
|
|
BasicProposalItem *item = new VirtualFunctionProposalItem(link, m_params.openInNextSplit);
|
2013-07-04 20:11:10 +02:00
|
|
|
item->setText(text);
|
|
|
|
|
item->setIcon(m_icons.iconForSymbol(symbol));
|
|
|
|
|
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 15:14:28 +02:00
|
|
|
VirtualFunctionAssistProvider::Parameters m_params;
|
2013-07-04 20:11:10 +02:00
|
|
|
Overview m_overview;
|
|
|
|
|
Icons m_icons;
|
2013-10-21 13:39:48 +02:00
|
|
|
CppTools::SymbolFinder m_finder;
|
2013-07-04 20:11:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
VirtualFunctionAssistProvider::VirtualFunctionAssistProvider()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 15:14:28 +02:00
|
|
|
bool VirtualFunctionAssistProvider::configure(const Parameters ¶meters)
|
2013-07-04 20:11:10 +02:00
|
|
|
{
|
2013-10-23 15:14:28 +02:00
|
|
|
m_params = parameters;
|
2013-07-04 20:11:10 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool VirtualFunctionAssistProvider::isAsynchronous() const
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool VirtualFunctionAssistProvider::supportsEditor(const Core::Id &editorId) const
|
|
|
|
|
{
|
|
|
|
|
return editorId == CppEditor::Constants::CPPEDITOR_ID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IAssistProcessor *VirtualFunctionAssistProvider::createProcessor() const
|
|
|
|
|
{
|
2013-10-23 15:14:28 +02:00
|
|
|
return new VirtualFunctionsAssistProcessor(m_params);
|
2013-07-04 20:11:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum VirtualType { Virtual, PureVirtual };
|
|
|
|
|
|
2013-10-08 21:41:21 +03:00
|
|
|
static bool isVirtualFunction_helper(const Function *function,
|
2013-11-10 23:14:17 +02:00
|
|
|
const LookupContext &context,
|
2013-10-31 20:50:49 +02:00
|
|
|
VirtualType virtualType,
|
|
|
|
|
const Function **firstVirtual)
|
2013-07-04 20:11:10 +02:00
|
|
|
{
|
2013-10-31 20:50:49 +02:00
|
|
|
enum { Unknown, False, True } res = Unknown;
|
|
|
|
|
|
|
|
|
|
if (firstVirtual)
|
|
|
|
|
*firstVirtual = 0;
|
|
|
|
|
|
2013-07-04 20:11:10 +02:00
|
|
|
if (!function)
|
|
|
|
|
return false;
|
|
|
|
|
|
2013-10-10 23:00:55 +03:00
|
|
|
if (virtualType == PureVirtual)
|
2013-10-31 20:50:49 +02:00
|
|
|
res = function->isPureVirtual() ? True : False;
|
|
|
|
|
|
|
|
|
|
if (function->isVirtual()) {
|
|
|
|
|
if (firstVirtual)
|
|
|
|
|
*firstVirtual = function;
|
|
|
|
|
if (res == Unknown)
|
|
|
|
|
res = True;
|
|
|
|
|
}
|
2013-10-10 23:00:55 +03:00
|
|
|
|
2013-10-31 20:50:49 +02:00
|
|
|
if (!firstVirtual && res != Unknown)
|
|
|
|
|
return res == True;
|
2013-07-04 20:11:10 +02:00
|
|
|
|
2013-11-10 23:14:17 +02:00
|
|
|
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())
|
2013-10-31 20:50:49 +02:00
|
|
|
return res == True;
|
|
|
|
|
if (functionType->isVirtual()) {
|
|
|
|
|
if (!firstVirtual)
|
|
|
|
|
return true;
|
|
|
|
|
if (res == Unknown)
|
|
|
|
|
res = True;
|
|
|
|
|
*firstVirtual = functionType;
|
|
|
|
|
}
|
2013-07-04 20:11:10 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-31 20:50:49 +02:00
|
|
|
return res == True;
|
2013-07-04 20:11:10 +02:00
|
|
|
}
|
|
|
|
|
|
2013-10-31 20:50:49 +02:00
|
|
|
bool FunctionHelper::isVirtualFunction(const Function *function,
|
|
|
|
|
const LookupContext &context,
|
|
|
|
|
const Function **firstVirtual)
|
2013-07-04 20:11:10 +02:00
|
|
|
{
|
2013-10-31 20:50:49 +02:00
|
|
|
return isVirtualFunction_helper(function, context, Virtual, firstVirtual);
|
2013-07-04 20:11:10 +02:00
|
|
|
}
|
|
|
|
|
|
2013-10-31 20:50:49 +02:00
|
|
|
bool FunctionHelper::isPureVirtualFunction(const Function *function,
|
|
|
|
|
const LookupContext &context,
|
|
|
|
|
const Function **firstVirtual)
|
2013-07-04 20:11:10 +02:00
|
|
|
{
|
2013-10-31 20:50:49 +02:00
|
|
|
return isVirtualFunction_helper(function, context, PureVirtual, firstVirtual);
|
2013-07-04 20:11:10 +02:00
|
|
|
}
|
|
|
|
|
|
2013-10-25 15:04:51 +02:00
|
|
|
QList<Symbol *> FunctionHelper::overrides(Function *function, Class *functionsClass,
|
2013-10-28 15:12:18 +01:00
|
|
|
Class *staticClass, const Snapshot &snapshot)
|
2013-07-04 20:11:10 +02:00
|
|
|
{
|
|
|
|
|
QList<Symbol *> result;
|
2013-10-28 15:12:18 +01:00
|
|
|
QTC_ASSERT(function && functionsClass && staticClass, return result);
|
2013-07-04 20:11:10 +02:00
|
|
|
|
|
|
|
|
FullySpecifiedType referenceType = function->type();
|
|
|
|
|
const Name *referenceName = function->name();
|
2013-10-08 09:17:41 +02:00
|
|
|
QTC_ASSERT(referenceName && referenceType.isValid(), return result);
|
2013-07-04 20:11:10 +02:00
|
|
|
|
|
|
|
|
// Find overrides
|
2013-12-10 10:47:41 +01:00
|
|
|
CppTools::TypeHierarchyBuilder builder(staticClass, snapshot);
|
|
|
|
|
const CppTools::TypeHierarchy &staticClassHierarchy = builder.buildDerivedTypeHierarchy();
|
2013-07-04 20:11:10 +02:00
|
|
|
|
2013-12-10 10:47:41 +01:00
|
|
|
QList<CppTools::TypeHierarchy> l;
|
|
|
|
|
l.append(CppTools::TypeHierarchy(functionsClass));
|
|
|
|
|
l.append(staticClassHierarchy);
|
2013-07-04 20:11:10 +02:00
|
|
|
|
|
|
|
|
while (!l.isEmpty()) {
|
|
|
|
|
// Add derived
|
2013-12-10 10:47:41 +01:00
|
|
|
const CppTools::TypeHierarchy hierarchy = l.takeFirst();
|
|
|
|
|
QTC_ASSERT(hierarchy.symbol(), continue);
|
|
|
|
|
Class *c = hierarchy.symbol()->asClass();
|
2013-10-28 15:12:18 +01:00
|
|
|
QTC_ASSERT(c, continue);
|
|
|
|
|
|
2013-12-10 10:47:41 +01:00
|
|
|
foreach (const CppTools::TypeHierarchy &t, hierarchy.hierarchy()) {
|
|
|
|
|
if (!l.contains(t))
|
|
|
|
|
l << t;
|
2013-07-04 20:11:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check member functions
|
|
|
|
|
for (int i = 0, total = c->memberCount(); i < total; ++i) {
|
|
|
|
|
Symbol *candidate = c->memberAt(i);
|
|
|
|
|
const Name *candidateName = candidate->name();
|
2013-10-08 09:17:41 +02:00
|
|
|
const FullySpecifiedType candidateType = candidate->type();
|
|
|
|
|
if (!candidateName || !candidateType.isValid())
|
|
|
|
|
continue;
|
2013-07-04 20:11:10 +02:00
|
|
|
if (candidateName->isEqualTo(referenceName) && candidateType.isEqualTo(referenceType))
|
|
|
|
|
result << candidate;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2013-10-10 23:00:55 +03:00
|
|
|
|
|
|
|
|
#ifdef WITH_TESTS
|
|
|
|
|
#include "cppeditorplugin.h"
|
|
|
|
|
|
|
|
|
|
#include <QList>
|
|
|
|
|
#include <QTest>
|
|
|
|
|
|
|
|
|
|
namespace CppEditor {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
enum Virtuality
|
|
|
|
|
{
|
|
|
|
|
NotVirtual,
|
|
|
|
|
Virtual,
|
|
|
|
|
PureVirtual
|
|
|
|
|
};
|
|
|
|
|
typedef QList<Virtuality> VirtualityList;
|
2013-10-15 13:11:11 +02:00
|
|
|
} // Internal namespace
|
|
|
|
|
} // CppEditor namespace
|
|
|
|
|
|
|
|
|
|
Q_DECLARE_METATYPE(CppEditor::Internal::Virtuality)
|
|
|
|
|
Q_DECLARE_METATYPE(CppEditor::Internal::VirtualityList)
|
2013-10-31 20:50:49 +02:00
|
|
|
Q_DECLARE_METATYPE(QList<int>)
|
2013-10-15 13:11:11 +02:00
|
|
|
|
|
|
|
|
namespace CppEditor {
|
|
|
|
|
namespace Internal {
|
2013-10-10 23:00:55 +03:00
|
|
|
|
|
|
|
|
void CppEditorPlugin::test_functionhelper_virtualFunctions()
|
|
|
|
|
{
|
|
|
|
|
// Create and parse document
|
|
|
|
|
QFETCH(QByteArray, source);
|
|
|
|
|
QFETCH(VirtualityList, virtualityList);
|
2013-10-31 20:50:49 +02:00
|
|
|
QFETCH(QList<int>, firstVirtualList);
|
2013-10-10 23:00:55 +03:00
|
|
|
Document::Ptr document = Document::create(QLatin1String("virtuals"));
|
|
|
|
|
document->setUtf8Source(source);
|
|
|
|
|
document->check(); // calls parse();
|
|
|
|
|
QCOMPARE(document->diagnosticMessages().size(), 0);
|
|
|
|
|
QVERIFY(document->translationUnit()->ast());
|
2013-10-31 20:50:49 +02:00
|
|
|
QList<const Function *> allFunctions;
|
|
|
|
|
const Function *firstVirtual = 0;
|
2013-10-10 23:00:55 +03:00
|
|
|
|
|
|
|
|
// Iterate through Function symbols
|
|
|
|
|
Snapshot snapshot;
|
|
|
|
|
snapshot.insert(document);
|
2013-11-10 23:14:17 +02:00
|
|
|
const LookupContext context(document, snapshot);
|
2013-10-10 23:00:55 +03:00
|
|
|
Control *control = document->translationUnit()->control();
|
|
|
|
|
Symbol **end = control->lastSymbol();
|
|
|
|
|
for (Symbol **it = control->firstSymbol(); it != end; ++it) {
|
2013-10-31 20:50:49 +02:00
|
|
|
if (const Function *function = (*it)->asFunction()) {
|
|
|
|
|
allFunctions.append(function);
|
2013-10-10 23:00:55 +03:00
|
|
|
QTC_ASSERT(!virtualityList.isEmpty(), return);
|
|
|
|
|
Virtuality virtuality = virtualityList.takeFirst();
|
2013-10-31 20:50:49 +02:00
|
|
|
QTC_ASSERT(!firstVirtualList.isEmpty(), return);
|
|
|
|
|
int firstVirtualIndex = firstVirtualList.takeFirst();
|
|
|
|
|
bool isVirtual = FunctionHelper::isVirtualFunction(function, context, &firstVirtual);
|
|
|
|
|
bool isPureVirtual = FunctionHelper::isPureVirtualFunction(function, context,
|
|
|
|
|
&firstVirtual);
|
|
|
|
|
|
|
|
|
|
// Test for regressions introduced by firstVirtual
|
|
|
|
|
QCOMPARE(FunctionHelper::isVirtualFunction(function, context), isVirtual);
|
|
|
|
|
QCOMPARE(FunctionHelper::isPureVirtualFunction(function, context), isPureVirtual);
|
|
|
|
|
if (isVirtual) {
|
|
|
|
|
if (isPureVirtual)
|
2013-10-10 23:00:55 +03:00
|
|
|
QCOMPARE(virtuality, PureVirtual);
|
|
|
|
|
else
|
|
|
|
|
QCOMPARE(virtuality, Virtual);
|
|
|
|
|
} else {
|
2013-11-11 22:38:03 +02:00
|
|
|
QEXPECT_FAIL("virtual-dtor-dtor", "Not implemented", Abort);
|
|
|
|
|
if (allFunctions.size() == 3)
|
|
|
|
|
QEXPECT_FAIL("dtor-virtual-dtor-dtor", "Not implemented", Abort);
|
2013-10-10 23:00:55 +03:00
|
|
|
QCOMPARE(virtuality, NotVirtual);
|
|
|
|
|
}
|
2013-10-31 20:50:49 +02:00
|
|
|
if (firstVirtualIndex == -1)
|
|
|
|
|
QVERIFY(!firstVirtual);
|
|
|
|
|
else
|
|
|
|
|
QCOMPARE(firstVirtual, allFunctions.at(firstVirtualIndex));
|
2013-10-10 23:00:55 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
QVERIFY(virtualityList.isEmpty());
|
2013-10-31 20:50:49 +02:00
|
|
|
QVERIFY(firstVirtualList.isEmpty());
|
2013-10-10 23:00:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppEditorPlugin::test_functionhelper_virtualFunctions_data()
|
|
|
|
|
{
|
|
|
|
|
typedef QByteArray _;
|
|
|
|
|
QTest::addColumn<QByteArray>("source");
|
|
|
|
|
QTest::addColumn<VirtualityList>("virtualityList");
|
2013-10-31 20:50:49 +02:00
|
|
|
QTest::addColumn<QList<int> >("firstVirtualList");
|
2013-10-10 23:00:55 +03:00
|
|
|
|
|
|
|
|
QTest::newRow("none")
|
|
|
|
|
<< _("struct None { void foo() {} };\n")
|
2013-10-31 20:50:49 +02:00
|
|
|
<< (VirtualityList() << NotVirtual)
|
|
|
|
|
<< (QList<int>() << -1);
|
2013-10-10 23:00:55 +03:00
|
|
|
|
|
|
|
|
QTest::newRow("single-virtual")
|
|
|
|
|
<< _("struct V { virtual void foo() {} };\n")
|
2013-10-31 20:50:49 +02:00
|
|
|
<< (VirtualityList() << Virtual)
|
|
|
|
|
<< (QList<int>() << 0);
|
2013-10-10 23:00:55 +03:00
|
|
|
|
|
|
|
|
QTest::newRow("single-pure-virtual")
|
|
|
|
|
<< _("struct PV { virtual void foo() = 0; };\n")
|
2013-10-31 20:50:49 +02:00
|
|
|
<< (VirtualityList() << PureVirtual)
|
|
|
|
|
<< (QList<int>() << 0);
|
2013-10-10 23:00:55 +03:00
|
|
|
|
|
|
|
|
QTest::newRow("virtual-derived-with-specifier")
|
|
|
|
|
<< _("struct Base { virtual void foo() {} };\n"
|
|
|
|
|
"struct Derived : Base { virtual void foo() {} };\n")
|
2013-10-31 20:50:49 +02:00
|
|
|
<< (VirtualityList() << Virtual << Virtual)
|
|
|
|
|
<< (QList<int>() << 0 << 0);
|
2013-10-10 23:00:55 +03:00
|
|
|
|
|
|
|
|
QTest::newRow("virtual-derived-implicit")
|
|
|
|
|
<< _("struct Base { virtual void foo() {} };\n"
|
|
|
|
|
"struct Derived : Base { void foo() {} };\n")
|
2013-10-31 20:50:49 +02:00
|
|
|
<< (VirtualityList() << Virtual << Virtual)
|
|
|
|
|
<< (QList<int>() << 0 << 0);
|
2013-10-10 23:00:55 +03:00
|
|
|
|
|
|
|
|
QTest::newRow("not-virtual-then-virtual")
|
|
|
|
|
<< _("struct Base { void foo() {} };\n"
|
|
|
|
|
"struct Derived : Base { virtual void foo() {} };\n")
|
2013-10-31 20:50:49 +02:00
|
|
|
<< (VirtualityList() << NotVirtual << Virtual)
|
|
|
|
|
<< (QList<int>() << -1 << 1);
|
2013-10-10 23:00:55 +03:00
|
|
|
|
|
|
|
|
QTest::newRow("virtual-final-not-virtual")
|
|
|
|
|
<< _("struct Base { virtual void foo() {} };\n"
|
|
|
|
|
"struct Derived : Base { void foo() final {} };\n"
|
|
|
|
|
"struct Derived2 : Derived { void foo() {} };")
|
2013-10-31 20:50:49 +02:00
|
|
|
<< (VirtualityList() << Virtual << Virtual << NotVirtual)
|
|
|
|
|
<< (QList<int>() << 0 << 0 << -1);
|
2013-10-10 23:00:55 +03:00
|
|
|
|
|
|
|
|
QTest::newRow("virtual-then-pure")
|
|
|
|
|
<< _("struct Base { virtual void foo() {} };\n"
|
|
|
|
|
"struct Derived : Base { virtual void foo() = 0; };\n"
|
|
|
|
|
"struct Derived2 : Derived { void foo() {} };")
|
2013-10-31 20:50:49 +02:00
|
|
|
<< (VirtualityList() << Virtual << PureVirtual << Virtual)
|
|
|
|
|
<< (QList<int>() << 0 << 0 << 0);
|
2013-10-10 23:00:55 +03:00
|
|
|
|
|
|
|
|
QTest::newRow("virtual-virtual-final-not-virtual")
|
|
|
|
|
<< _("struct Base { virtual void foo() {} };\n"
|
|
|
|
|
"struct Derived : Base { virtual void foo() final {} };\n"
|
|
|
|
|
"struct Derived2 : Derived { void foo() {} };")
|
2013-10-31 20:50:49 +02:00
|
|
|
<< (VirtualityList() << Virtual << Virtual << NotVirtual)
|
|
|
|
|
<< (QList<int>() << 0 << 0 << -1);
|
2013-11-09 20:50:03 +02:00
|
|
|
|
|
|
|
|
QTest::newRow("ctor-virtual-dtor")
|
|
|
|
|
<< _("struct Base { Base() {} virtual ~Base() {} };\n")
|
2013-10-31 20:50:49 +02:00
|
|
|
<< (VirtualityList() << NotVirtual << Virtual)
|
|
|
|
|
<< (QList<int>() << -1 << 1);
|
2013-11-11 22:38:03 +02:00
|
|
|
|
|
|
|
|
QTest::newRow("virtual-dtor-dtor")
|
|
|
|
|
<< _("struct Base { virtual ~Base() {} };\n"
|
|
|
|
|
"struct Derived : Base { ~Derived() {} };\n")
|
|
|
|
|
<< (VirtualityList() << Virtual << Virtual)
|
|
|
|
|
<< (QList<int>() << 0 << 0);
|
|
|
|
|
|
|
|
|
|
QTest::newRow("dtor-virtual-dtor-dtor")
|
|
|
|
|
<< _("struct Base { ~Base() {} };\n"
|
|
|
|
|
"struct Derived : Base { virtual ~Derived() {} };\n"
|
|
|
|
|
"struct Derived2 : Derived { ~Derived2() {} };\n")
|
|
|
|
|
<< (VirtualityList() << NotVirtual << Virtual << Virtual)
|
|
|
|
|
<< (QList<int>() << -1 << 1 << 1);
|
2013-10-10 23:00:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace CppEditor
|
|
|
|
|
|
|
|
|
|
#endif
|