Fix C++ type hierarchy

Dynamic casts between library boundaries tend to fail. Add an "explicit
cast" to CppClass via virtual functions as a quickfix.
This is a recurring issue, e.g. d2769f3003, 3f11ef9216 and
2ffd0e2d0d to name just a few...

This was introduced in 5e861d2be6

Task-number: QTCREATORBUG-20001
Change-Id: Ie5a89a028d587e4e9d1ecec920a7c7d17497dbde
Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
This commit is contained in:
Eike Ziller
2018-03-08 19:25:04 +01:00
parent 32188f448a
commit 711ac6602f
3 changed files with 19 additions and 3 deletions

View File

@@ -154,7 +154,7 @@ void CppTypeHierarchyWidget::perform()
if (evaluator.identifiedCppElement()) { if (evaluator.identifiedCppElement()) {
const QSharedPointer<CppElement> &cppElement = evaluator.cppElement(); const QSharedPointer<CppElement> &cppElement = evaluator.cppElement();
CppElement *element = cppElement.data(); CppElement *element = cppElement.data();
if (CppClass *cppClass = dynamic_cast<CppClass *>(element)) { if (CppClass *cppClass = element->toCppClass()) {
m_inspectedClass->setText(cppClass->name); m_inspectedClass->setText(cppClass->name);
m_inspectedClass->setLink(cppClass->link); m_inspectedClass->setLink(cppClass->link);
QStandardItem *bases = new QStandardItem(tr("Bases")); QStandardItem *bases = new QStandardItem(tr("Bases"));

View File

@@ -63,6 +63,11 @@ CppElement::CppElement() : helpCategory(TextEditor::HelpItem::Unknown)
CppElement::~CppElement() CppElement::~CppElement()
{} {}
CppClass *CppElement::toCppClass()
{
return nullptr;
}
class Unknown : public CppElement class Unknown : public CppElement
{ {
public: public:
@@ -156,6 +161,11 @@ bool CppClass::operator==(const CppClass &other)
return this->declaration == other.declaration; return this->declaration == other.declaration;
} }
CppClass *CppClass::toCppClass()
{
return this;
}
void CppClass::lookupBases(Symbol *declaration, const LookupContext &context) void CppClass::lookupBases(Symbol *declaration, const LookupContext &context)
{ {
typedef QPair<ClassOrNamespace *, CppClass *> Data; typedef QPair<ClassOrNamespace *, CppClass *> Data;

View File

@@ -81,6 +81,8 @@ private:
QString m_diagnosis; QString m_diagnosis;
}; };
class CppClass;
class CPPTOOLS_EXPORT CppElement class CPPTOOLS_EXPORT CppElement
{ {
protected: protected:
@@ -89,6 +91,8 @@ protected:
public: public:
virtual ~CppElement(); virtual ~CppElement();
virtual CppClass *toCppClass();
TextEditor::HelpItem::Category helpCategory; TextEditor::HelpItem::Category helpCategory;
QStringList helpIdCandidates; QStringList helpIdCandidates;
QString helpMark; QString helpMark;
@@ -96,7 +100,7 @@ public:
QString tooltip; QString tooltip;
}; };
class CppDeclarableElement : public CppElement class CPPTOOLS_EXPORT CppDeclarableElement : public CppElement
{ {
public: public:
explicit CppDeclarableElement(CPlusPlus::Symbol *declaration); explicit CppDeclarableElement(CPlusPlus::Symbol *declaration);
@@ -109,7 +113,7 @@ public:
QIcon icon; QIcon icon;
}; };
class CppClass : public CppDeclarableElement class CPPTOOLS_EXPORT CppClass : public CppDeclarableElement
{ {
public: public:
CppClass(); CppClass();
@@ -117,6 +121,8 @@ public:
bool operator==(const CppClass &other); bool operator==(const CppClass &other);
CppClass *toCppClass() final;
void lookupBases(CPlusPlus::Symbol *declaration, const CPlusPlus::LookupContext &context); void lookupBases(CPlusPlus::Symbol *declaration, const CPlusPlus::LookupContext &context);
void lookupDerived(CPlusPlus::Symbol *declaration, const CPlusPlus::Snapshot &snapshot); void lookupDerived(CPlusPlus::Symbol *declaration, const CPlusPlus::Snapshot &snapshot);