From 831b22fcd6eb9b45c9e7834799a91698ec00a6a4 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Mon, 23 Jul 2018 15:53:03 +0200 Subject: shiboken: Add support for type aliases (using) QAbstractRayCaster is the first class in Qt to use type aliases: using Hits = QVector; Treat them as typedefs if a canonical type can be obtained for them. This requires adding some simplification of the canonical types obtained for standard containers. Task-number: PYSIDE-751 Change-Id: I521a8b02d3c8cb89e4f72a817fbacc5955041570 Reviewed-by: Qt CI Bot Reviewed-by: Christian Tismer --- .../ApiExtractor/clangparser/clangbuilder.cpp | 30 +++++++++++++------- .../shiboken2/ApiExtractor/parser/codemodel.cpp | 32 ++++++++++++++++++++++ sources/shiboken2/ApiExtractor/parser/codemodel.h | 4 +++ sources/shiboken2/tests/libsample/objecttype.h | 2 +- 4 files changed, 57 insertions(+), 11 deletions(-) (limited to 'sources') diff --git a/sources/shiboken2/ApiExtractor/clangparser/clangbuilder.cpp b/sources/shiboken2/ApiExtractor/clangparser/clangbuilder.cpp index e3d7d0273..e3e52cdd3 100644 --- a/sources/shiboken2/ApiExtractor/clangparser/clangbuilder.cpp +++ b/sources/shiboken2/ApiExtractor/clangparser/clangbuilder.cpp @@ -190,6 +190,8 @@ public: TypeInfo *t) const; bool addTemplateInstantiationsRecursion(const CXType &type, TypeInfo *t) const; + void addTypeDef(const CXCursor &cursor, const TypeInfo &ti); + TemplateParameterModelItem createTemplateParameter(const CXCursor &cursor) const; TemplateParameterModelItem createNonTypeTemplateParameter(const CXCursor &cursor) const; void addField(const CXCursor &cursor); @@ -511,6 +513,7 @@ TypeInfo BuilderPrivate::createTypeInfoHelper(const CXType &type) const typeInfo.setQualifiedName(qualifiedName(typeName)); // 3320:CINDEX_LINKAGE int clang_getNumArgTypes(CXType T); function ptr types? + typeInfo.simplifyStdType(); return typeInfo; } @@ -522,6 +525,16 @@ TypeInfo BuilderPrivate::createTypeInfo(const CXType &type) const return it.value(); } +void BuilderPrivate::addTypeDef(const CXCursor &cursor, const TypeInfo &ti) +{ + TypeDefModelItem item(new _TypeDefModelItem(m_model, getCursorSpelling(cursor))); + setFileName(cursor, item.data()); + item->setType(ti); + item->setScope(m_scope); + m_scopeStack.back()->addTypeDef(item); + m_cursorTypedefHash.insert(cursor, item); +} + // extract an expression from the cursor via source // CXCursor_EnumConstantDecl, ParmDecl (a = Flag1 | Flag2) QString BuilderPrivate::cursorValueExpression(BaseVisitor *bv, const CXCursor &cursor) const @@ -912,17 +925,14 @@ BaseVisitor::StartTokenResult Builder::startToken(const CXCursor &cursor) } break; case CXCursor_TypeAliasDecl: - case CXCursor_TypeAliasTemplateDecl: // May contain nested CXCursor_TemplateTypeParameter - return Skip; - case CXCursor_TypedefDecl: { - const QString name = getCursorSpelling(cursor); - TypeDefModelItem item(new _TypeDefModelItem(d->m_model, name)); - setFileName(cursor, item.data()); - item->setType(d->createTypeInfo(clang_getTypedefDeclUnderlyingType(cursor))); - item->setScope(d->m_scope); - d->m_scopeStack.back()->addTypeDef(item); - d->m_cursorTypedefHash.insert(cursor, item); + case CXCursor_TypeAliasTemplateDecl: { // May contain nested CXCursor_TemplateTypeParameter + const CXType type = clang_getCanonicalType(clang_getCursorType(cursor)); + if (type.kind > CXType_Unexposed) + d->addTypeDef(cursor, d->createTypeInfo(type)); } + return Skip; + case CXCursor_TypedefDecl: + d->addTypeDef(cursor, d->createTypeInfo(clang_getTypedefDeclUnderlyingType(cursor))); break; case CXCursor_TypeRef: if (!d->m_currentFunction.isNull()) { diff --git a/sources/shiboken2/ApiExtractor/parser/codemodel.cpp b/sources/shiboken2/ApiExtractor/parser/codemodel.cpp index 485222bb0..c9861b510 100644 --- a/sources/shiboken2/ApiExtractor/parser/codemodel.cpp +++ b/sources/shiboken2/ApiExtractor/parser/codemodel.cpp @@ -355,6 +355,38 @@ bool TypeInfo::stripLeadingQualifier(const QString &qualifier, QString *s) return true; } +// Helper functionality to simplify a raw standard type as returned by +// clang_getCanonicalType() for g++ standard containers from +// "std::__cxx11::list >" or +// "std::__1::list >" -> "std::list". + +bool TypeInfo::isStdType() const +{ + return m_qualifiedName.size() > 1 + && m_qualifiedName.constFirst() == QLatin1String("std"); +} + +static inline bool discardStdType(const QString &name) +{ + return name == QLatin1String("allocator") || name == QLatin1String("less"); +} + +void TypeInfo::simplifyStdType() +{ + if (isStdType()) { + if (m_qualifiedName.at(1).startsWith(QLatin1String("__"))) + m_qualifiedName.removeAt(1); + for (int t = m_instantiations.size() - 1; t >= 0; --t) { + if (m_instantiations.at(t).isStdType()) { + if (discardStdType(m_instantiations.at(t).m_qualifiedName.constLast())) + m_instantiations.removeAt(t); + else + m_instantiations[t].simplifyStdType(); + } + } + } +} + #ifndef QT_NO_DEBUG_STREAM template void formatSequence(QDebug &d, It i1, It i2, const char *separator=", ") diff --git a/sources/shiboken2/ApiExtractor/parser/codemodel.h b/sources/shiboken2/ApiExtractor/parser/codemodel.h index eef2bf281..aad1a8eaa 100644 --- a/sources/shiboken2/ApiExtractor/parser/codemodel.h +++ b/sources/shiboken2/ApiExtractor/parser/codemodel.h @@ -186,6 +186,8 @@ public: void addInstantiation(const TypeInfo &i) { m_instantiations.append(i); } void clearInstantiations() { m_instantiations.clear(); } + bool isStdType() const; + QPair parseTemplateArgumentList(const QString &l, int from = 0); bool operator==(const TypeInfo &other) const; @@ -212,6 +214,8 @@ public: static bool stripLeadingVolatile(QString *s); static bool stripLeadingQualifier(const QString &qualifier, QString *s); + void simplifyStdType(); + private: friend class TypeInfoTemplateArgumentHandler; diff --git a/sources/shiboken2/tests/libsample/objecttype.h b/sources/shiboken2/tests/libsample/objecttype.h index bcb4f3332..ecd67b684 100644 --- a/sources/shiboken2/tests/libsample/objecttype.h +++ b/sources/shiboken2/tests/libsample/objecttype.h @@ -63,7 +63,7 @@ private: class ObjectTypeLayout; class ObjectType; -typedef std::list ObjectTypeList; +using ObjectTypeList = std::list; class LIBSAMPLE_API ObjectType { -- cgit v1.2.3