File: test_files.cpp

package info (click to toggle)
kdevelop 4%3A24.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 71,888 kB
  • sloc: cpp: 290,869; python: 3,626; javascript: 3,518; sh: 1,316; ansic: 703; xml: 401; php: 95; lisp: 66; makefile: 31; sed: 12
file content (217 lines) | stat: -rw-r--r-- 7,360 bytes parent folder | download | duplicates (2)
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
/*
    SPDX-FileCopyrightText: 2013 Milian Wolff <mail@milianw.de>
    SPDX-FileCopyrightText: 2013 Olivier de Gaalon <olivier.jg@gmail.com>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "test_files.h"

#include <language/duchain/duchain.h>
#include <language/duchain/problem.h>
#include <language/codegen/coderepresentation.h>
#include <language/backgroundparser/backgroundparser.h>

#include <tests/testcore.h>
#include <tests/autotestshell.h>
#include <tests/json/declarationvalidator.h>

#include "testfilepaths.h"
#include "testprovider.h"
#include "duchain/clanghelpers.h"

//Include all used json tests, otherwise "Test not found"
#include <tests/json/jsondeclarationtests.h>
#include <tests/json/jsonducontexttests.h>
#include <tests/json/jsontypetests.h>
#include <interfaces/ilanguagecontroller.h>

#include <QDebug>
#include <QTest>
#include <QLoggingCategory>
#include <QProcess>
#include <QRegularExpression>
#include <QVersionNumber>

using namespace KDevelop;

QTEST_MAIN(TestFiles)

namespace {
bool isCudaAvailable()
{
    return QProcess::execute(QStringLiteral("clang"), {QStringLiteral("-xcuda"), QStringLiteral("-fsyntax-only"), QProcess::nullDevice()}) == 0;
}

QVersionNumber clangVersionNumber()
{
    return QVersionNumber::fromString(ClangHelpers::clangVersion());
}

QRegularExpression rangeRegularExpression()
{
    const auto capturedCursorRegexp = QStringLiteral("(\\(\\d+, \\d+\\))");
    const auto rangeRegexp = QStringLiteral("\\[%1, %2\\]").arg(capturedCursorRegexp, capturedCursorRegexp);
    return QRegularExpression{QRegularExpression::anchoredPattern(rangeRegexp)};
}

void expandTestFilesDir(QVariantMap& testData)
{
    const auto idIt = testData.find("identifier");
    if (idIt == testData.end()) {
        return; // nothing to adjust
    }

    static const QLatin1String testFilesDirVariableName("${TEST_FILES_DIR}");

    QCOMPARE(idIt->userType(), QMetaType::QString);
    auto identifier = idIt->toString();
    if (!identifier.contains(testFilesDirVariableName)) {
        return; // nothing to adjust
    }

    if (clangVersionNumber() >= QVersionNumber(13, 0, 0)) {
        *idIt = identifier.replace(testFilesDirVariableName, TEST_FILES_DIR);
        return; // done
    }

    // Older Clang versions return an empty identifier for unnamed struct and anonymous union.
    *idIt = QString();

    // KDevelop's Visitor::createDeclarationCommon() assigns the range's end to its start when
    // the identifier is empty, so the same is done below.
    const auto rangeIt = testData.find("range");
    if (rangeIt == testData.end()) {
        return; // no range => nothing left to adjust
    }

    QCOMPARE(rangeIt->userType(), QMetaType::QString);
    auto range = rangeIt->toString();

    static const auto regexp = rangeRegularExpression();
    QVERIFY(range.contains(regexp));
    *rangeIt = range.replace(regexp, "[\\1, \\1]");
}

/// libclang 16.0.0 and later does not qualify template arguments redundantly
/// and does not spell out default template argument values.
void adjustTemplateArguments(QVariantMap& testData)
{
    if (clangVersionNumber() < QVersionNumber(16, 0, 0)) {
        return; // nothing to adjust
    }

    const auto typeIt = testData.find("type");
    if (typeIt == testData.end()) {
        return; // nothing to adjust
    }

    QCOMPARE(typeIt->userType(), QMetaType::QVariantMap);
    auto typeData = typeIt->toMap();

    const auto toStringIt = typeData.find("toString");
    QVERIFY(toStringIt != typeData.end());
    QCOMPARE(toStringIt->userType(), QMetaType::QString);
    auto typeString = toStringIt->toString();

    const auto replaceInTypeString = [&](const char* before, const char* after, const char* replacementDescription) {
        if (!typeString.contains(QLatin1String{before}))
            return;
        typeString.replace(QLatin1String{before}, QLatin1String{after});
        qDebug() << replacementDescription << toStringIt->toString() << "=>" << typeString;
        *toStringIt = typeString;
        *typeIt = typeData;
    };

    replaceInTypeString("FormattingBug::X", "X", "Removing template argument qualification:");
    replaceInTypeString("SpacedDefaultParam< 20 >", "SpacedDefaultParam<  >",
                        "Unspecifying default template argument:");
}

void adjustTestData(QVariantMap& testData)
{
    expandTestFilesDir(testData);
    adjustTemplateArguments(testData);
}
} // unnamed namespace

void TestFiles::initTestCase()
{
    qputenv("KDEV_CLANG_JSON_TEST_RUN", "1");
    qputenv("KDEV_CLANG_EXTRA_ARGUMENTS", "-Wno-unused-variable -Wno-unused-parameter -Wno-unused-comparison -Wno-unused-value -Wno-unused-private-field -Wno-ignored-attributes");

    QLoggingCategory::setFilterRules(QStringLiteral("*.debug=false\ndefault.debug=true\nkdevelop.plugins.clang.debug=true\n"));
    QVERIFY(qputenv("KDEV_CLANG_DISPLAY_DIAGS", "1"));
    AutoTestShell::init({"kdevclangsupport"});
    TestCore::initialize(Core::NoUi);
    DUChain::self()->disablePersistentStorage();
    Core::self()->languageController()->backgroundParser()->setDelay(0);
    CodeRepresentation::setDiskChangesForbidden(true);

    m_provider = new TestEnvironmentProvider;
    IDefinesAndIncludesManager::manager()->registerBackgroundProvider(m_provider);
}

void TestFiles::cleanupTestCase()
{
    delete m_provider;
    TestCore::shutdown();
}

void TestFiles::cleanup()
{
    m_provider->clear();
}

void TestFiles::testFiles_data()
{
    QTest::addColumn<QString>("fileName");
    const QString testDirPath = TEST_FILES_DIR;
    auto patterns = QStringList{"*.h", "*.cpp", "*.c", "*.cl"};
    if (isCudaAvailable()) {
        patterns.append("*.cu");
    }
    const QStringList files = QDir(testDirPath).entryList(patterns, QDir::Files);
    for (const QString& file : files) {
        QTest::newRow(file.toUtf8().constData()) << QString(testDirPath + '/' + file);
    }
}

void TestFiles::testFiles()
{
    QFETCH(QString, fileName);

    if (QTest::currentDataTag() == QLatin1String("lambdas.cpp")) {
        m_provider->parserArguments += "-std=c++14";
    } else if (QTest::currentDataTag() == QLatin1String("templates.cpp")) {
        m_provider->parserArguments += "-std=c++17";
    } else if (QTest::currentDataTag() == QLatin1String("types.cpp")) {
        if (clangVersionNumber() >= QVersionNumber(18, 0, 0)) {
            // without this flag, the top->problems().isEmpty() check below fails at the line `int arr2[argc];`
            m_provider->parserArguments += "-Wno-vla-cxx-extension";
        }
    }

    const IndexedString indexedFileName(fileName);
    ReferencedTopDUContext top =
        DUChain::self()->waitForUpdate(indexedFileName, TopDUContext::AllDeclarationsContextsAndUses);
    QVERIFY(top);
    DUChainReadLocker lock;
    DeclarationValidator validator(adjustTestData);
    top->visit(validator);

    const auto problems = top->problems();
    for (auto& problem : problems) {
        qDebug() << problem;
    }

    if (clangVersionNumber() < QVersionNumber(9, 0, 0))
        QEXPECT_FAIL("lambdas.cpp", "capture with identifier and initializer aren't visited apparently", Abort);
    QVERIFY(validator.testsPassed());

    if (!QTest::currentDataTag() || strcmp("invalid.cpp", QTest::currentDataTag()) != 0) {
        QVERIFY(top->problems().isEmpty());
    }
}

#include "moc_test_files.cpp"