diagnosticset-test.cpp 6.23 KB
Newer Older
Marco Bubke's avatar
Marco Bubke committed
1 2
/****************************************************************************
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
Marco Bubke's avatar
Marco Bubke committed
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.
Marco Bubke's avatar
Marco Bubke committed
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.
Marco Bubke's avatar
Marco Bubke committed
23 24 25
**
****************************************************************************/

Marco Bubke's avatar
Marco Bubke committed
26 27
#include "googletest.h"
#include "diagnosticcontainer-matcher.h"
28
#include "testenvironment.h"
Marco Bubke's avatar
Marco Bubke committed
29

30
#include <clangbackendipc_global.h>
31
#include <clangdocument.h>
32
#include <diagnosticcontainer.h>
Marco Bubke's avatar
Marco Bubke committed
33
#include <diagnosticset.h>
34
#include <fixitcontainer.h>
Marco Bubke's avatar
Marco Bubke committed
35
#include <projectpart.h>
36
#include <projects.h>
37 38 39
#include <sourcelocation.h>
#include <sourcelocationcontainer.h>
#include <sourcerangecontainer.h>
40
#include <clangtranslationunit.h>
41
#include <clangdocuments.h>
Marco Bubke's avatar
Marco Bubke committed
42 43 44 45
#include <unsavedfiles.h>

#include <clang-c/Index.h>

46 47 48 49 50
using ::testing::Contains;
using ::testing::Not;
using ::testing::PrintToString;

using ::ClangBackEnd::Diagnostic;
Marco Bubke's avatar
Marco Bubke committed
51
using ::ClangBackEnd::DiagnosticSet;
52 53
using ::ClangBackEnd::DiagnosticContainer;
using ::ClangBackEnd::FixItContainer;
Marco Bubke's avatar
Marco Bubke committed
54
using ::ClangBackEnd::ProjectPart;
55 56
using ::ClangBackEnd::SourceLocation;
using ::ClangBackEnd::SourceLocationContainer;
57
using ::ClangBackEnd::Document;
Marco Bubke's avatar
Marco Bubke committed
58 59 60 61
using ::ClangBackEnd::UnsavedFiles;

namespace {

62 63
const Utf8String headerFilePath = Utf8StringLiteral(TESTDATA_DIR"/diagnostic_diagnosticset_header.cpp");

Marco Bubke's avatar
Marco Bubke committed
64 65 66
class DiagnosticSet : public ::testing::Test
{
protected:
67 68
    ProjectPart projectPart{Utf8StringLiteral("projectPartId"),
                TestEnvironment::addPlatformArguments({Utf8StringLiteral("-pedantic")})};
69 70
    ClangBackEnd::ProjectParts projects;
    ClangBackEnd::UnsavedFiles unsavedFiles;
71 72 73 74 75 76 77 78 79
    ClangBackEnd::Documents documents{projects, unsavedFiles};
    Document document{Utf8StringLiteral(TESTDATA_DIR"/diagnostic_diagnosticset.cpp"),
                      projectPart,
                      Utf8StringVector(),
                      documents};
    Document documentMainFile{Utf8StringLiteral(TESTDATA_DIR"/diagnostic_diagnosticset_mainfile.cpp"),
                              projectPart,
                              Utf8StringVector(),
                              documents};
80 81 82 83

protected:
    enum ChildMode { WithChild, WithoutChild };
    DiagnosticContainer expectedDiagnostic(ChildMode childMode) const;
Marco Bubke's avatar
Marco Bubke committed
84 85
};

86 87 88
using DiagnosticSetSlowTest = DiagnosticSet;

TEST_F(DiagnosticSetSlowTest, SetHasContent)
Marco Bubke's avatar
Marco Bubke committed
89
{
90
    document.parse();
91
    const auto set = document.translationUnit().diagnostics();
Marco Bubke's avatar
Marco Bubke committed
92 93 94 95

    ASSERT_THAT(set.size(), 1);
}

96
TEST_F(DiagnosticSetSlowTest, MoveConstructor)
Marco Bubke's avatar
Marco Bubke committed
97
{
98
    document.parse();
99
    auto set = document.translationUnit().diagnostics();
Marco Bubke's avatar
Marco Bubke committed
100 101 102 103 104 105 106

    const auto set2 = std::move(set);

    ASSERT_TRUE(set.isNull());
    ASSERT_FALSE(set2.isNull());
}

107
TEST_F(DiagnosticSetSlowTest, MoveAssigment)
Marco Bubke's avatar
Marco Bubke committed
108
{
109
    document.parse();
110
    auto set = document.translationUnit().diagnostics();
Marco Bubke's avatar
Marco Bubke committed
111 112 113 114 115 116 117 118

    auto set2 = std::move(set);
    set = std::move(set2);

    ASSERT_TRUE(set2.isNull());
    ASSERT_FALSE(set.isNull());
}

119
TEST_F(DiagnosticSetSlowTest, MoveSelfAssigment)
Marco Bubke's avatar
Marco Bubke committed
120
{
121
    document.parse();
122
    auto set = document.translationUnit().diagnostics();
Marco Bubke's avatar
Marco Bubke committed
123 124 125 126 127 128

    set = std::move(set);

    ASSERT_FALSE(set.isNull());
}

129
TEST_F(DiagnosticSetSlowTest, FirstElementEqualBegin)
Marco Bubke's avatar
Marco Bubke committed
130
{
131
    document.parse();
132
    auto set = document.translationUnit().diagnostics();
Marco Bubke's avatar
Marco Bubke committed
133 134 135 136

    ASSERT_TRUE(set.front() == *set.begin());
}

137
TEST_F(DiagnosticSetSlowTest, BeginIsUnequalEnd)
Marco Bubke's avatar
Marco Bubke committed
138
{
139
    document.parse();
140
    auto set = document.translationUnit().diagnostics();
Marco Bubke's avatar
Marco Bubke committed
141 142 143 144

    ASSERT_TRUE(set.begin() != set.end());
}

145
TEST_F(DiagnosticSetSlowTest, BeginPlusOneIsEqualEnd)
Marco Bubke's avatar
Marco Bubke committed
146
{
147
    document.parse();
148
    auto set = document.translationUnit().diagnostics();
Marco Bubke's avatar
Marco Bubke committed
149 150 151 152

    ASSERT_TRUE(++set.begin() == set.end());
}

153
TEST_F(DiagnosticSetSlowTest, ToDiagnosticContainersLetThroughByDefault)
154 155
{
    const auto diagnosticContainerWithoutChild = expectedDiagnostic(WithChild);
156
    documentMainFile.parse();
157

158
    const auto diagnostics = documentMainFile.translationUnit().diagnostics().toDiagnosticContainers();
159 160 161 162

    ASSERT_THAT(diagnostics, Contains(IsDiagnosticContainer(diagnosticContainerWithoutChild)));
}

163
TEST_F(DiagnosticSetSlowTest, ToDiagnosticContainersFiltersOutTopLevelItem)
164
{
165
    documentMainFile.parse();
166
    const ::DiagnosticSet diagnosticSetWithChildren{documentMainFile.translationUnit().diagnostics()};
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    const auto acceptNoDiagnostics = [](const Diagnostic &) { return false; };

    const auto diagnostics = diagnosticSetWithChildren.toDiagnosticContainers(acceptNoDiagnostics);

    ASSERT_TRUE(diagnostics.isEmpty());
}

DiagnosticContainer DiagnosticSet::expectedDiagnostic(DiagnosticSet::ChildMode childMode) const
{
    QVector<DiagnosticContainer> children;
    if (childMode == WithChild) {
        const auto child = DiagnosticContainer(
            Utf8StringLiteral("note: previous definition is here"),
            Utf8StringLiteral("Semantic Issue"),
            {Utf8String(), Utf8String()},
            ClangBackEnd::DiagnosticSeverity::Note,
183
            SourceLocationContainer(headerFilePath, 1, 6),
184 185 186 187 188 189 190 191 192 193 194 195
            {},
            {},
            {}
        );
        children.append(child);
    }

    return DiagnosticContainer(
        Utf8StringLiteral("error: redefinition of 'f'"),
        Utf8StringLiteral("Semantic Issue"),
        {Utf8String(), Utf8String()},
        ClangBackEnd::DiagnosticSeverity::Error,
196
        SourceLocationContainer(documentMainFile.filePath(), 3, 6),
197 198 199 200 201 202
        {},
        {},
        children
    );
}

Marco Bubke's avatar
Marco Bubke committed
203
}