1From: Friedemann Kleint <Friedemann.Kleint@qt.io>
2Date: Tue, 25 Apr 2023 14:01:45 +0200
3Subject: shiboken2/clang: Fix build with clang 16
4
5clang 16 returns more elaborated types instead of fully qualified type
6names. Qualify elaborated types when retrieving the type name.
7
8[ChangeLog][shiboken6] Support for libclang version 16 has been added.
9
10Task-number: PYSIDE-2288
11Pick-to: 6.5 5.15
12Change-Id: Ibd428280180967f11d82a72159e744c016afc927
13Reviewed-by: Christian Tismer <tismer@stackless.com>
14(cherry picked from commit 44ef1859214c66861a251d4a0faf5c38dc050850)
15---
16 .../ApiExtractor/clangparser/clangbuilder.cpp | 2 +-
17 .../ApiExtractor/clangparser/clangutils.cpp | 23 ++++++++++++++++++++++
18 .../ApiExtractor/clangparser/clangutils.h | 1 +
19 .../shiboken2/ApiExtractor/tests/testtemplates.cpp | 3 +--
20 4 files changed, 26 insertions(+), 3 deletions(-)
21
22diff --git a/sources/shiboken2/ApiExtractor/clangparser/clangbuilder.cpp b/sources/shiboken2/ApiExtractor/clangparser/clangbuilder.cpp
23index 332f1da..ed1e15d 100644
24--- a/sources/shiboken2/ApiExtractor/clangparser/clangbuilder.cpp
25+++ b/sources/shiboken2/ApiExtractor/clangparser/clangbuilder.cpp
26@@ -524,7 +524,7 @@ TypeInfo BuilderPrivate::createTypeInfoHelper(const CXType &type) const
27 typeInfo.setConstant(clang_isConstQualifiedType(nestedType) != 0);
28 typeInfo.setVolatile(clang_isVolatileQualifiedType(nestedType) != 0);
29
30- QString typeName = getTypeName(nestedType);
31+ QString typeName = getResolvedTypeName(nestedType);
32 while (TypeInfo::stripLeadingConst(&typeName)
33 || TypeInfo::stripLeadingVolatile(&typeName)) {
34 }
35diff --git a/sources/shiboken2/ApiExtractor/clangparser/clangutils.cpp b/sources/shiboken2/ApiExtractor/clangparser/clangutils.cpp
36index 57271ef..295ede3 100644
37--- a/sources/shiboken2/ApiExtractor/clangparser/clangutils.cpp
38+++ b/sources/shiboken2/ApiExtractor/clangparser/clangutils.cpp
39@@ -130,6 +130,23 @@ QString getCursorDisplayName(const CXCursor &cursor)
40 return result;
41 }
42
43+static inline bool isBuiltinType(CXTypeKind kind)
44+{
45+ return kind >= CXType_FirstBuiltin && kind <= CXType_LastBuiltin;
46+}
47+
48+// Resolve elaborated types occurring with clang 16
49+static CXType resolveType(const CXType &type)
50+{
51+ if (!isBuiltinType(type.kind)) {
52+ CXCursor decl = clang_getTypeDeclaration(type);
53+ auto resolvedType = clang_getCursorType(decl);
54+ if (resolvedType.kind != CXType_Invalid && resolvedType.kind != type.kind)
55+ return resolvedType;
56+ }
57+ return type;
58+}
59+
60 QString getTypeName(const CXType &type)
61 {
62 CXString typeSpelling = clang_getTypeSpelling(type);
63@@ -138,6 +155,12 @@ QString getTypeName(const CXType &type)
64 return result;
65 }
66
67+// Resolve elaborated types occurring with clang 16
68+QString getResolvedTypeName(const CXType &type)
69+{
70+ return getTypeName(resolveType(type));
71+}
72+
73 Diagnostic::Diagnostic(const QString &m, const CXCursor &c, CXDiagnosticSeverity s)
74 : message(m), source(Other), severity(s)
75 {
76diff --git a/sources/shiboken2/ApiExtractor/clangparser/clangutils.h b/sources/shiboken2/ApiExtractor/clangparser/clangutils.h
77index f7c230a..aacaf63 100644
78--- a/sources/shiboken2/ApiExtractor/clangparser/clangutils.h
79+++ b/sources/shiboken2/ApiExtractor/clangparser/clangutils.h
80@@ -52,6 +52,7 @@ QString getCursorKindName(CXCursorKind cursorKind);
81 QString getCursorSpelling(const CXCursor &cursor);
82 QString getCursorDisplayName(const CXCursor &cursor);
83 QString getTypeName(const CXType &type);
84+QString getResolvedTypeName(const CXType &type);
85 inline QString getCursorTypeName(const CXCursor &cursor)
86 { return getTypeName(clang_getCursorType(cursor)); }
87 inline QString getCursorResultTypeName(const CXCursor &cursor)
88diff --git a/sources/shiboken2/ApiExtractor/tests/testtemplates.cpp b/sources/shiboken2/ApiExtractor/tests/testtemplates.cpp
89index 9f929c4..fbc5c4c 100644
90--- a/sources/shiboken2/ApiExtractor/tests/testtemplates.cpp
91+++ b/sources/shiboken2/ApiExtractor/tests/testtemplates.cpp
92@@ -236,7 +236,6 @@ struct List {
93 const AbstractMetaFunction *erase = list->findFunction(QStringLiteral("erase"));
94 QVERIFY(erase);
95 QCOMPARE(erase->arguments().size(), 1);
96- QEXPECT_FAIL("", "Clang: Some other code changes the parameter type", Abort);
97 QCOMPARE(erase->arguments().at(0)->type()->cppSignature(), QLatin1String("List::Iterator"));
98 }
99
100@@ -389,7 +388,7 @@ typedef BaseTemplateClass<TypeOne> TypeOneClass;
101 const ComplexTypeEntry* oneType = one->typeEntry();
102 const ComplexTypeEntry* baseType = base->typeEntry();
103 QCOMPARE(oneType->baseContainerType(), baseType);
104- QCOMPARE(one->baseClassNames(), QStringList(QLatin1String("BaseTemplateClass<TypeOne>")));
105+ QCOMPARE(one->baseClassNames(), QStringList(QLatin1String("NSpace::BaseTemplateClass<NSpace::TypeOne>")));
106
107 QVERIFY(one->hasTemplateBaseClassInstantiations());
108 AbstractMetaTypeList instantiations = one->templateBaseClassInstantiations();