clangdiagnosticfilter-test.cpp 7.69 KB
Newer Older
1 2
/****************************************************************************
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
5 6 7 8 9 10 11
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
15
**
16 17 18 19 20 21 22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 24 25
**
****************************************************************************/

Marco Bubke's avatar
Marco Bubke committed
26 27
#include "googletest.h"

28 29
#include <clangdiagnosticfilter.h>
#include <diagnosticcontainer.h>
30
#include <fixitcontainer.h>
31 32 33 34 35 36 37

namespace {

using ::testing::Contains;
using ::testing::Not;

using ClangBackEnd::DiagnosticContainer;
38 39 40
using ClangBackEnd::FixItContainer;
using ClangBackEnd::SourceLocationContainer;
using ClangBackEnd::SourceRangeContainer;
41 42 43

DiagnosticContainer createDiagnostic(const QString &text,
                                     ClangBackEnd::DiagnosticSeverity severity,
44 45 46
                                     const QString &filePath,
                                     QVector<DiagnosticContainer> children = QVector<DiagnosticContainer>(),
                                     bool addFixItContainer = false)
47
{
48 49 50 51
    QVector<FixItContainer> fixIts;
    if (addFixItContainer)
        fixIts.append(FixItContainer(Utf8StringLiteral("("), {{filePath, 1, 1}, {filePath, 1, 2}}));

52 53 54 55 56 57 58
    return DiagnosticContainer(
            text,
            Utf8StringLiteral(""),
            {},
            severity,
            {filePath, 0u, 0u},
            {},
59 60
            fixIts,
            children
61 62 63
    );
}

64
const QString mainFileHeaderPath = QString::fromUtf8(TESTDATA_DIR "/someHeader.h");
65
const QString mainFilePath = QString::fromUtf8(TESTDATA_DIR "/diagnostic_erroneous_source.cpp");
66
const QString headerFilePath = QString::fromUtf8(TESTDATA_DIR "/diagnostic_erroneous_header.cpp");
67

68
DiagnosticContainer warningFromHeader()
69 70 71 72
{
    return createDiagnostic(
                QStringLiteral("warning: control reaches end of non-void function"),
                ClangBackEnd::DiagnosticSeverity::Warning,
73
                headerFilePath);
74 75
}

76
DiagnosticContainer errorFromHeader()
77 78 79 80
{
    return createDiagnostic(
                QStringLiteral("C++ requires a type specifier for all declarations"),
                ClangBackEnd::DiagnosticSeverity::Error,
81
                headerFilePath);
82 83 84 85 86 87 88 89 90 91
}

DiagnosticContainer warningFromMainFile()
{
    return createDiagnostic(
                QStringLiteral("warning: enumeration value 'Three' not handled in switch"),
                ClangBackEnd::DiagnosticSeverity::Warning,
                mainFilePath);
}

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
DiagnosticContainer pragmaOnceWarningInHeader()
{
    return createDiagnostic(
                QStringLiteral("warning: #pragma once in main file"),
                ClangBackEnd::DiagnosticSeverity::Warning,
                mainFileHeaderPath);
}

DiagnosticContainer includeNextInPrimarySourceFileWarningInHeader()
{
    return createDiagnostic(
                QStringLiteral("warning: #include_next in primary source file"),
                ClangBackEnd::DiagnosticSeverity::Warning,
                mainFileHeaderPath);
}

108 109 110 111 112 113 114 115
DiagnosticContainer errorFromMainFile()
{
    return createDiagnostic(
                QStringLiteral("error: void function 'g' should not return a value"),
                ClangBackEnd::DiagnosticSeverity::Error,
                mainFilePath);
}

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
DiagnosticContainer fixIt1(const QString &filePath)
{
    return createDiagnostic(
                QStringLiteral("note: place parentheses around the assignment to silence this warning"),
                ClangBackEnd::DiagnosticSeverity::Note,
                filePath);
}

DiagnosticContainer fixIt2(const QString &filePath)
{
    return createDiagnostic(
                QStringLiteral("note: use '==' to turn this assignment into an equality comparison"),
                ClangBackEnd::DiagnosticSeverity::Note,
                filePath);
}

DiagnosticContainer createDiagnosticWithFixIt(const QString &filePath)
{
    return createDiagnostic(
                QStringLiteral("warning: using the result of an assignment as a condition without parentheses"),
                ClangBackEnd::DiagnosticSeverity::Warning,
                filePath,
                {fixIt1(filePath), fixIt2(filePath)},
                true);
}

DiagnosticContainer warningFromMainFileWithFixits()
{
    return createDiagnosticWithFixIt(mainFilePath);
}

147
DiagnosticContainer warningFromHeaderFileWithFixits()
148
{
149
    return createDiagnosticWithFixIt(headerFilePath);
150 151
}

152 153 154 155 156 157 158 159 160 161 162 163 164
class ClangDiagnosticFilter : public ::testing::Test
{
protected:
    ClangCodeModel::Internal::ClangDiagnosticFilter clangDiagnosticFilter{mainFilePath};
};

TEST_F(ClangDiagnosticFilter, WarningsFromMainFileAreLetThrough)
{
    clangDiagnosticFilter.filter({warningFromMainFile()});

    ASSERT_THAT(clangDiagnosticFilter.takeWarnings(),Contains(warningFromMainFile()));
}

165
TEST_F(ClangDiagnosticFilter, WarningsFromHeaderAreIgnored)
166
{
167
    clangDiagnosticFilter.filter({warningFromHeader()});
168

169
    ASSERT_THAT(clangDiagnosticFilter.takeWarnings(), Not(Contains(warningFromHeader())));
170 171 172 173 174 175 176 177 178
}

TEST_F(ClangDiagnosticFilter, ErrorsFromMainFileAreLetThrough)
{
    clangDiagnosticFilter.filter({errorFromMainFile()});

    ASSERT_THAT(clangDiagnosticFilter.takeErrors(), Contains(errorFromMainFile()));
}

179
TEST_F(ClangDiagnosticFilter, ErrorsFromHeaderAreIgnored)
180
{
181
    clangDiagnosticFilter.filter({errorFromHeader()});
182

183
    ASSERT_THAT(clangDiagnosticFilter.takeErrors(), Not(Contains(errorFromHeader())));
184 185
}

186 187 188 189 190 191 192
TEST_F(ClangDiagnosticFilter, FixItsFromMainFileAreLetThrough)
{
    clangDiagnosticFilter.filter({warningFromMainFileWithFixits()});

    ASSERT_THAT(clangDiagnosticFilter.takeFixIts(), Contains(warningFromMainFileWithFixits()));
}

193
TEST_F(ClangDiagnosticFilter, FixItsFromHeaderAreIgnored)
194
{
195
    clangDiagnosticFilter.filter({warningFromHeaderFileWithFixits()});
196 197

    ASSERT_THAT(clangDiagnosticFilter.takeFixIts(),
198
                Not(Contains(warningFromHeaderFileWithFixits())));
199 200 201 202 203 204 205 206 207 208 209
}

TEST_F(ClangDiagnosticFilter, WarningsAreEmptyAfterTaking)
{
    clangDiagnosticFilter.filter({warningFromMainFile()});

    clangDiagnosticFilter.takeWarnings();

    ASSERT_TRUE(clangDiagnosticFilter.takeWarnings().isEmpty());
}

210 211 212 213 214 215 216 217 218 219
TEST_F(ClangDiagnosticFilter, IgnoreCertainWarningsInHeaderFiles)
{
    ClangCodeModel::Internal::ClangDiagnosticFilter myClangDiagnosticFilter{mainFileHeaderPath};

    myClangDiagnosticFilter.filter({pragmaOnceWarningInHeader(),
                                    includeNextInPrimarySourceFileWarningInHeader()});

    ASSERT_TRUE(myClangDiagnosticFilter.takeWarnings().isEmpty());
}

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
TEST_F(ClangDiagnosticFilter, ErrorsAreEmptyAfterTaking)
{
    clangDiagnosticFilter.filter({errorFromMainFile()});

    clangDiagnosticFilter.takeErrors();

    ASSERT_TRUE(clangDiagnosticFilter.takeErrors().isEmpty());
}

TEST_F(ClangDiagnosticFilter, FixItssAreEmptyAfterTaking)
{
    clangDiagnosticFilter.filter({warningFromMainFileWithFixits()});

    clangDiagnosticFilter.takeFixIts();

    ASSERT_TRUE(clangDiagnosticFilter.takeFixIts().isEmpty());
}

238
} // anonymous namespace