aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/clangtools/inlinesuppresseddiagnostics.cpp
blob: 312e9213e52dece6629f5379a61b5315d9f2bc2a (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "inlinesuppresseddiagnostics.h"

#include <utils/algorithm.h>
#include <utils/qtcassert.h>

#ifdef WITH_TESTS
#include <QObject>
#include <QtTest>
#endif // WITH_TESTS

namespace ClangTools::Internal {

InlineSuppressedDiagnostics::~InlineSuppressedDiagnostics() = default;

void InlineSuppressedDiagnostics::addDiagnostic(const QString &diag)
{
    if (!m_diagnostics.contains(diag)) {
        m_diagnostics << diag;
        m_diagnostics.sort();
    }
}

void InlineSuppressedDiagnostics::fromString(const QString &input)
{
    try {
        int tokenOffset = -1;
        for (int i = 0; i < input.size(); ++i) {
            const QChar c = input.at(i);
            if (c.isSpace()) {
                tokenOffset = -1;
                continue;
            }
            if (tokenOffset == -1)
                tokenOffset = i;
            const int tokenPos = i - tokenOffset;
            if (tokenPos >= m_startString.size() || m_startString.at(tokenPos) != input.at(i)) {
                tokenOffset = -1;
                continue;
            }
            if (tokenPos == m_startString.size() - 1) {
                m_parsedStartOffset = tokenOffset;
                fromStringImpl(QStringView(input).mid(m_parsedStartOffset + m_startString.size()));
                break;
            }
        }
    } catch (const ParseError &) {
        m_parsedStartOffset = -1;
        m_parsedEndOffset = -1;
        m_parseError = true;
    }
}

QString InlineSuppressedDiagnostics::toString() const
{
    QTC_ASSERT(!m_diagnostics.isEmpty(), return {});
    if (m_parseError)
        return {};
    return toStringImpl();
}

void InlineSuppressedDiagnostics::parseDiagnostic(QStringView input, int &offset, const QString &prefix)
{
    QString diag;
    while (offset < input.size()) {
        const QChar c = input.at(offset);
        if (!c.isLetterOrNumber() && c != '-')
            break;
        diag.append(c);
        ++offset;
    }
    if (diag.isEmpty())
        throw ParseError();
    m_diagnostics << prefix + diag;
}

void InlineSuppressedClangTidyDiagnostics::fromStringImpl(QStringView input)
{
    int offset = 0;
    const auto skipWhiteSpace = [&] {
        for (; offset < input.size() && input.at(offset).isSpace(); ++offset)
            ;
    };
    while (true) {
        skipWhiteSpace();
        parseDiagnostic(input, offset, {});
        skipWhiteSpace();
        if (offset == input.size())
            throw ParseError();
        const QChar c = input.at(offset);
        if (c == ')') {
            setEndOffset(parsedStartOffset() + startString().size() + offset + 1);
            return;
        }
        if (c == ',') {
            ++offset;
            continue;
        }
        throw ParseError();
    }
}

QString InlineSuppressedClangTidyDiagnostics::toStringImpl() const
{
    return "NOLINT(" + diagnostics().join(',') + ')';
}

void InlineSuppressedClazyDiagnostics::fromStringImpl(QStringView input)
{
    int offset = 0;
    while (true) {
        parseDiagnostic(input, offset, "clazy-");
        if (offset == input.size())
            break;
        const QChar c = input.at(offset);
        if (c == ',') {
            ++offset;
            continue;
        }
        if (c.isSpace())
            break;
        throw ParseError();
    }
    setEndOffset(parsedStartOffset() + startString().size() + offset);
}

QString InlineSuppressedClazyDiagnostics::toStringImpl() const
{
    return "clazy:exclude=" + Utils::transform(diagnostics(), [](const QString &diag) {
                                  return diag.mid(6); // Remove "clazy-" prefix
                              }).join(',');
}

#ifdef WITH_TESTS
class InlineSuppressedDiagnosticsTest : public QObject
{
    Q_OBJECT
private:
    template<typename Diags> void testExcludedDiagnostics()
    {
        QFETCH(QString, input);
        QFETCH(QStringList, additionalDiagnostics);
        QFETCH(int, startOffset);
        QFETCH(int, endOffset);
        QFETCH(QString, output);
        QFETCH(bool, error);

        Diags diags;
        diags.fromString(input);
        for (const QString &diag : std::as_const(additionalDiagnostics))
            diags.addDiagnostic(diag);
        QCOMPARE(diags.parsedStartOffset(), startOffset);
        QCOMPARE(diags.parsedEndOffset(), endOffset);
        QCOMPARE(diags.toString(), output);
        QCOMPARE(diags.hasError(), error);
    }

private slots:
    void testClangTidyExcludedDiagnostics_data()
    {
        QTest::addColumn<QString>("input");
        QTest::addColumn<QStringList>("additionalDiagnostics");
        QTest::addColumn<int>("startOffset");
        QTest::addColumn<int>("endOffset");
        QTest::addColumn<QString>("output");
        QTest::addColumn<bool>("error");

        QTest::newRow("no offset, round-trip")
            << QString("NOLINT(google-explicit-constructor, google-runtime-int)")
            << QStringList() << 0 << 55
            << QString("NOLINT(google-explicit-constructor,google-runtime-int)")
            << false;
        QTest::newRow("random other content, add some diags")
            << QString("some prefix NOLINT(google-runtime-int) some suffix")
            << QStringList{"modernize-concat-nested-namespaces", "misc-include-cleaner"} << 12 << 38
            << QString("NOLINT(google-runtime-int,misc-include-cleaner,modernize-concat-nested-namespaces)")
            << false;
        QTest::newRow("create from empty input")
            << QString()
            << QStringList{"misc-include-cleaner"} << -1 << -1
            << QString("NOLINT(misc-include-cleaner)")
            << false;
        QTest::newRow("create from irrelevant input")
            << QString(" random content ")
            << QStringList{"misc-include-cleaner"} << -1 << -1
            << QString("NOLINT(misc-include-cleaner)")
            << false;
        QTest::newRow("missing closing parenthesis")
            << QString("NOLINT(google-explicit-constructor, google-runtime-int")
            << QStringList() << -1 << -1
            << QString()
            << true;
        QTest::newRow("missing comma")
            << QString("NOLINT(google-explicit-constructor google-runtime-int)")
            << QStringList() << -1 << -1
            << QString()
            << true;
        QTest::newRow("no diagnostics")
            << QString("NOLINT()")
            << QStringList() << -1 << -1
            << QString()
            << true;
        QTest::newRow("empty diagnostic (start)")
            << QString("NOLINT(,google-explicit-constructor,google-runtime-int)")
            << QStringList() << -1 << -1
            << QString()
            << true;
        QTest::newRow("empty diagnostic (middle)")
            << QString("NOLINT(google-explicit-constructor,,google-runtime-int)")
            << QStringList() << -1 << -1
            << QString()
            << true;
        QTest::newRow("empty diagnostic (end)")
            << QString("NOLINT(google-explicit-constructor,)")
            << QStringList() << -1 << -1
            << QString()
            << true;
    }

    void testClangTidyExcludedDiagnostics()
    {
        testExcludedDiagnostics<InlineSuppressedClangTidyDiagnostics>();
    }

    void testClazyExcludedDiagnostics_data()
    {
        QTest::addColumn<QString>("input");
        QTest::addColumn<QStringList>("additionalDiagnostics");
        QTest::addColumn<int>("startOffset");
        QTest::addColumn<int>("endOffset");
        QTest::addColumn<QString>("output");
        QTest::addColumn<bool>("error");

        QTest::newRow("no offset, round-trip")
            << QString("clazy:exclude=qstring-allocations,qenums")
            << QStringList() << 0 << 40
            << QString("clazy:exclude=qstring-allocations,qenums")
            << false;
        QTest::newRow("random other content, add some diags")
            << QString("some prefix clazy:exclude=qstring-allocations some suffix")
            << QStringList{"clazy-qgetenv", "clazy-qtmacros"} << 12 << 45
            << QString("clazy:exclude=qgetenv,qstring-allocations,qtmacros")
            << false;
        QTest::newRow("create from empty input")
            << QString()
            << QStringList{"clazy-qgetenv"} << -1 << -1
            << QString("clazy:exclude=qgetenv")
            << false;
        QTest::newRow("create from irrelevant input")
            << QString(" random content ")
            << QStringList{"clazy-qgetenv"} << -1 << -1
            << QString("clazy:exclude=qgetenv")
            << false;
        QTest::newRow("missing comma")
            << QString("clazy:exclude=qstring-allocations qenums")
            << QStringList() << 0 << 33
            << QString("clazy:exclude=qstring-allocations")
            << false;
        QTest::newRow("empty diagnostic (start)")
            << QString("clazy:exclude=,qenums")
            << QStringList() << -1 << -1
            << QString()
            << true;
        QTest::newRow("empty diagnostic (middle)")
            << QString("clazy:exclude=qstring-allocations,,qenums")
            << QStringList() << -1 << -1
            << QString()
            << true;
        QTest::newRow("empty diagnostic (end)")
            << QString("clazy:exclude=qstring-allocations,qenums,")
            << QStringList() << -1 << -1
            << QString()
            << true;
    }

    void testClazyExcludedDiagnostics()
    {
        testExcludedDiagnostics<InlineSuppressedClazyDiagnostics>();
    }
};

QObject *createInlineSuppressedDiagnosticsTest()
{
    return new InlineSuppressedDiagnosticsTest;
}
#endif // WITH_TESTS

} // namespace ClangTools::Internal

#ifdef WITH_TESTS
#include <inlinesuppresseddiagnostics.moc>
#endif