aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken6/ApiExtractor/tests/testresolvetype.cpp
blob: a798c4339c0a5c1d1aca018c4a6acf2f29e271a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "testresolvetype.h"
#include "testutil.h"
#include <abstractmetaargument.h>
#include <abstractmetafunction.h>
#include <abstractmetalang.h>
#include <abstractmetatype.h>
#include <complextypeentry.h>
#include <primitivetypeentry.h>
#include <typedatabase.h>

#include <qtcompat.h>

#include <QtTest/QTest>

using namespace Qt::StringLiterals;

void TestResolveType::initTestCase()
{
    // For enum lookup in testFixDefaultArguments()
    AbstractMetaBuilder::setCodeModelTestMode(true);
}

void TestResolveType::testResolveReturnTypeFromParentScope()
{
    const char cppCode[] = "\n\
    namespace A {\n\
        struct B {\n\
            struct C {};\n\
        };\n\
        struct D : public B::C {\n\
            C* foo = 0;\n\
            C* method();\n\
        };\n\
    };";
    const char xmlCode[] = R"XML(
    <typesystem package='Foo'>
        <namespace-type name='A'>
            <value-type name='B'>
                <value-type name='C'/>
            </value-type>
            <value-type name='D'/>
        </namespace-type>
    </typesystem>)XML";
    QScopedPointer<AbstractMetaBuilder> builder(TestUtil::parse(cppCode, xmlCode, false));
    QVERIFY(!builder.isNull());
    AbstractMetaClassList classes = builder->classes();
    const AbstractMetaClass *classD = AbstractMetaClass::findClass(classes, u"A::D");
    QVERIFY(classD);
    const auto meth = classD->findFunction(u"method");
    QVERIFY(!meth.isNull());
    QVERIFY(meth);
}

// Helper classes and functions for testing default value fixing.
// Put the AbstractMetaBuilder into test fixture struct to avoid having
// to re-parse for each data row.

struct DefaultValuesFixture
{
    QSharedPointer<AbstractMetaBuilder> builder;

    AbstractMetaType intType;
    AbstractMetaType stringType;
    AbstractMetaType classType;
    AbstractMetaType listType;
    const AbstractMetaClass *klass{};
};

Q_DECLARE_METATYPE(DefaultValuesFixture)
Q_DECLARE_METATYPE(AbstractMetaType)

static int populateDefaultValuesFixture(DefaultValuesFixture *fixture)
{
    static const char cppCode[] =R"(
#include <string>
#include <list>

namespace Namespace {
class Test
{
public:
    enum Enum { enumValue1, enumValue2 };

    explicit Test(int x = INT_FIELD_1);
    explicit Test(const std::string &t = std::string(CHAR_FIELD_1));

    static void listFunc(std::list<Test> list = std::list<Test>());

    static const int INT_FIELD_1 = 42;
    static const char *CHAR_FIELD_1;
};
} // Namespace
)";
    static const char xmlCode[] = R"(
<typesystem package="Foo">
    <namespace-type name='Namespace'>
        <value-type name='Test'>
            <enum-type name='Enum'/>
        </value-type>
    </namespace-type>
    <container-type name="std::list" type="list"/>
</typesystem>
)";

    fixture->builder.reset(TestUtil::parse(cppCode, xmlCode, false));
    if (fixture->builder.isNull())
        return -1;

    for (const auto &klass : fixture->builder->classes()) {
        if (klass->name() == u"Test") {
            fixture->klass = klass;
            break;
        }
    }
    if (!fixture->klass)
        return -2;

    fixture->classType = AbstractMetaType(fixture->klass->typeEntry());
    fixture->classType.decideUsagePattern();

    for (const auto &f : fixture->klass->findFunctions(u"Test"_s)) {
        if (f->functionType() == AbstractMetaFunction::ConstructorFunction
            && f->arguments().size() == 1) {
            const auto type = f->arguments().constFirst().type();
            if (type.name() == u"int")
                fixture->intType = type;
            else
                fixture->stringType = type;
        }
    }
    if (fixture->intType.isVoid() || fixture->stringType.isVoid())
        return -3;

    auto listFunc = fixture->klass->findFunction(u"listFunc"_s);
    if (listFunc.isNull() || listFunc->arguments().size() != 1)
        return -3;
    fixture->listType = listFunc->arguments().constFirst().type();

    return 0;
}

void TestResolveType::testFixDefaultArguments_data()
{
    DefaultValuesFixture fixture;
    const int setupOk = populateDefaultValuesFixture(&fixture);

    QTest::addColumn<DefaultValuesFixture>("fixture");
    QTest::addColumn<int>("setupOk"); // To verify setup
    QTest::addColumn<AbstractMetaType>("metaType"); // Type and parameters for fixup
    QTest::addColumn<QString>("input");
    QTest::addColumn<QString>("expected");

    QTest::newRow("int") << fixture << setupOk
        << fixture.intType << "1" << "1";
    QTest::newRow("int-macro") << fixture << setupOk
        << fixture.intType << "GL_MACRO" << "GL_MACRO";
    QTest::newRow("int-enum") << fixture << setupOk
        << fixture.intType << "enumValue1" << "Namespace::Test::Enum::enumValue1";

    // Test expansion of container types
    QString expected = u"std::list<Namespace::Test>()"_s;
    QTest::newRow("list")
        << fixture << setupOk << fixture.listType
        << expected << expected;
    QTest::newRow("partially qualified list")
        << fixture << setupOk << fixture.listType
        << "std::list<Test>()" << expected;

    // Test field expansion
    expected = u"Namespace::Test::INT_FIELD_1"_s;
    QTest::newRow("qualified class field")
        << fixture << setupOk << fixture.intType
        << expected << expected;
    QTest::newRow("partially qualified class field")
        << fixture << setupOk << fixture.intType
        << "Test::INT_FIELD_1" << expected;
    QTest::newRow("unqualified class field")
        << fixture << setupOk << fixture.intType
        << "INT_FIELD_1" << expected;

    // Test field expansion when constructing some class
    expected = u"QLatin1String(Namespace::Test::CHAR_FIELD_1)"_s;
    QTest::newRow("class from qualified class field")
        << fixture << setupOk << fixture.classType
        << expected << expected;
    QTest::newRow("class from partially qualified class field")
        << fixture << setupOk << fixture.classType
        << "QLatin1String(Test::CHAR_FIELD_1)" << expected;
    QTest::newRow("class from unqualified class field")
        << fixture << setupOk << fixture.classType
        << "QLatin1String(CHAR_FIELD_1)" << expected;

    // Test field expansion when constructing class itself
    expected = u"Namespace::Test(Namespace::Test::CHAR_FIELD_1)"_s;
    QTest::newRow("self from qualified class field")
        << fixture << setupOk << fixture.classType
        << expected << expected;
    QTest::newRow("self from partially qualified class field")
        << fixture << setupOk << fixture.classType
        << "Test(Test::CHAR_FIELD_1)" << expected;
    QTest::newRow("self from unqualified class field")
        << fixture << setupOk << fixture.classType
        << "Test(CHAR_FIELD_1)" << expected;

    // Test enum expansion when constructing class itself
    expected = u"Namespace::Test(Namespace::Test::Enum::enumValue1)"_s;
    QTest::newRow("self from qualified enum")
        << fixture << setupOk << fixture.classType
        << expected << expected;
    QTest::newRow("self from enum")
        << fixture << setupOk << fixture.classType
        << "Test(enumValue1)" << expected;
}

void TestResolveType::testFixDefaultArguments()
{
    QFETCH(DefaultValuesFixture, fixture);
    QFETCH(int, setupOk);
    QFETCH(AbstractMetaType, metaType);
    QFETCH(QString, input);
    QFETCH(QString, expected);
    QCOMPARE(setupOk, 0);
    const QString actual = fixture.builder->fixDefaultValue(input, metaType, fixture.klass);
    QCOMPARE(actual, expected);
}

// Verify that the typedefs of the C++ 11 integer types (int32_t, ...)
// are seen by the C++ parser, otherwise they are handled as unknown
// primitive types, causing invalid code to be generated.
// (see BuilderPrivate::visitHeader(),
// sources/shiboken6/ApiExtractor/clangparser/clangbuilder.cpp).
void TestResolveType::testCppTypes()
{
    static const char cppCode[] =R"(
#include <cstdint>

class Test
{
public:
    explicit Test(int32_t v);
};
)";
    static const char xmlCode[] = R"(
<typesystem package="Foo">
    <value-type name='Test'/>
    <primitive-type name='int32_t'/>
</typesystem>
)";

    QScopedPointer<AbstractMetaBuilder> builder(TestUtil::parse(cppCode, xmlCode, false));
    QVERIFY(!builder.isNull());
    AbstractMetaClassList classes = builder->classes();
    const AbstractMetaClass *testClass = AbstractMetaClass::findClass(classes, u"Test");
    QVERIFY(testClass);

    auto *tdb = TypeDatabase::instance();
    auto int32TEntry = tdb->findType(u"int32_t"_s);
    QVERIFY2(!int32TEntry.isNull(), "int32_t not found");
    QVERIFY(int32TEntry->isPrimitive());
    auto int32T = qSharedPointerCast<const PrimitiveTypeEntry>(int32TEntry);
    auto basicType = basicReferencedTypeEntry(int32T);
    QVERIFY2(basicType != int32T,
             "Typedef for int32_t not found. Check the system include paths.");
}

QTEST_APPLESS_MAIN(TestResolveType)