2015-02-04 15:19:30 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-14 10:59:10 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2015-02-04 15:19:30 +01:00
|
|
|
**
|
2016-01-14 10:59:10 +01:00
|
|
|
** This file is part of Qt Creator.
|
2015-02-04 15:19:30 +01:00
|
|
|
**
|
2016-01-14 10:59:10 +01:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
2015-02-04 15:19:30 +01:00
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-14 10:59:10 +01:00
|
|
|
** 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.
|
2015-02-04 15:19:30 +01:00
|
|
|
**
|
2016-01-14 10:59:10 +01:00
|
|
|
** 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.
|
2015-02-04 15:19:30 +01:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2018-04-24 15:30:19 +02:00
|
|
|
#include "clangtoolsunittests.h"
|
2015-02-04 15:19:30 +01:00
|
|
|
|
2019-09-25 15:07:59 +02:00
|
|
|
#include "clangtool.h"
|
2019-07-09 14:54:05 +02:00
|
|
|
#include "clangtoolsdiagnostic.h"
|
|
|
|
|
#include "clangtoolssettings.h"
|
2018-01-17 15:08:30 +01:00
|
|
|
#include "clangtoolsutils.h"
|
2015-02-04 15:19:30 +01:00
|
|
|
|
2018-08-06 11:32:32 +02:00
|
|
|
#include <coreplugin/icore.h>
|
2019-09-25 15:46:15 +02:00
|
|
|
#include <cpptools/clangdiagnosticconfig.h>
|
2015-02-04 15:19:30 +01:00
|
|
|
#include <cpptools/cppmodelmanager.h>
|
2018-04-24 17:34:21 +02:00
|
|
|
#include <cpptools/cpptoolsreuse.h>
|
2019-09-25 15:46:15 +02:00
|
|
|
#include <cpptools/cpptoolstestcase.h>
|
2015-02-04 15:19:30 +01:00
|
|
|
#include <projectexplorer/kitinformation.h>
|
|
|
|
|
#include <projectexplorer/kitmanager.h>
|
|
|
|
|
#include <projectexplorer/projectexplorer.h>
|
|
|
|
|
#include <projectexplorer/toolchain.h>
|
2018-04-24 17:34:21 +02:00
|
|
|
|
|
|
|
|
#include <utils/executeondestruction.h>
|
2015-02-04 15:19:30 +01:00
|
|
|
#include <utils/fileutils.h>
|
|
|
|
|
|
|
|
|
|
#include <QEventLoop>
|
|
|
|
|
#include <QSignalSpy>
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
#include <QtTest>
|
|
|
|
|
|
2019-09-25 15:39:32 +02:00
|
|
|
using namespace CppTools;
|
2015-02-04 15:19:30 +01:00
|
|
|
using namespace ProjectExplorer;
|
|
|
|
|
using namespace Utils;
|
|
|
|
|
|
2019-07-09 14:54:05 +02:00
|
|
|
Q_DECLARE_METATYPE(CppTools::ClangDiagnosticConfig)
|
|
|
|
|
|
2018-03-14 12:58:12 +01:00
|
|
|
namespace ClangTools {
|
2015-02-04 15:19:30 +01:00
|
|
|
namespace Internal {
|
|
|
|
|
|
2018-04-24 15:30:19 +02:00
|
|
|
void ClangToolsUnitTests::initTestCase()
|
2015-02-04 15:19:30 +01:00
|
|
|
{
|
|
|
|
|
const QList<Kit *> allKits = KitManager::kits();
|
|
|
|
|
if (allKits.count() != 1)
|
|
|
|
|
QSKIP("This test requires exactly one kit to be present");
|
2020-02-18 18:25:26 +01:00
|
|
|
const ToolChain *const toolchain = ToolChainKitAspect::cxxToolChain(allKits.first());
|
2015-02-04 15:19:30 +01:00
|
|
|
if (!toolchain)
|
|
|
|
|
QSKIP("This test requires that there is a kit with a toolchain.");
|
2018-04-30 15:26:36 +02:00
|
|
|
|
2018-08-06 11:32:32 +02:00
|
|
|
if (Core::ICore::clangExecutable(CLANG_BINDIR).isEmpty())
|
2015-02-04 15:19:30 +01:00
|
|
|
QSKIP("No clang suitable for analyzing found");
|
|
|
|
|
|
2019-11-28 16:05:38 +01:00
|
|
|
m_tmpDir = new Tests::TemporaryCopiedDir(":/clangtools/unit-tests");
|
2015-05-18 11:32:43 +02:00
|
|
|
QVERIFY(m_tmpDir->isValid());
|
2015-02-04 15:19:30 +01:00
|
|
|
}
|
|
|
|
|
|
2018-04-24 15:30:19 +02:00
|
|
|
void ClangToolsUnitTests::cleanupTestCase()
|
2015-02-04 15:19:30 +01:00
|
|
|
{
|
|
|
|
|
delete m_tmpDir;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-25 15:39:32 +02:00
|
|
|
static ClangDiagnosticConfig configFor(const QString &tidyChecks,
|
2019-07-09 14:54:05 +02:00
|
|
|
const QString &clazyChecks)
|
2018-04-24 17:34:21 +02:00
|
|
|
{
|
2019-09-25 15:39:32 +02:00
|
|
|
ClangDiagnosticConfig config;
|
2019-07-09 14:54:05 +02:00
|
|
|
config.setId("Test.MyTestConfig");
|
2018-04-24 17:34:21 +02:00
|
|
|
config.setDisplayName("Test");
|
|
|
|
|
config.setIsReadOnly(true);
|
|
|
|
|
config.setClangOptions(QStringList{QStringLiteral("-Wno-everything")});
|
2019-10-24 11:31:42 +02:00
|
|
|
config.setClangTidyMode(ClangDiagnosticConfig::TidyMode::UseCustomChecks);
|
2019-09-13 10:49:14 +02:00
|
|
|
const QString theTidyChecks = tidyChecks.isEmpty() ? tidyChecks : "-*," + tidyChecks;
|
|
|
|
|
config.setClangTidyChecks(theTidyChecks);
|
2019-07-09 14:54:05 +02:00
|
|
|
config.setClazyChecks(clazyChecks);
|
2018-04-24 17:34:21 +02:00
|
|
|
return config;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-24 15:30:19 +02:00
|
|
|
void ClangToolsUnitTests::testProject()
|
2015-02-04 15:19:30 +01:00
|
|
|
{
|
|
|
|
|
QFETCH(QString, projectFilePath);
|
|
|
|
|
QFETCH(int, expectedDiagCount);
|
2019-09-25 15:39:32 +02:00
|
|
|
QFETCH(ClangDiagnosticConfig, diagnosticConfig);
|
2017-08-17 09:40:02 +02:00
|
|
|
if (projectFilePath.contains("mingw")) {
|
2020-02-18 18:25:26 +01:00
|
|
|
const auto toolchain = ToolChainKitAspect::cxxToolChain(KitManager::kits().constFirst());
|
2017-08-17 09:40:02 +02:00
|
|
|
if (toolchain->typeId() != ProjectExplorer::Constants::MINGW_TOOLCHAIN_TYPEID)
|
2018-05-02 12:27:35 +02:00
|
|
|
QSKIP("This test is mingw specific, does not run for other toolchains");
|
2017-08-17 09:40:02 +02:00
|
|
|
}
|
2015-02-04 15:19:30 +01:00
|
|
|
|
2019-11-28 16:17:57 +01:00
|
|
|
// Open project
|
2019-09-25 15:39:32 +02:00
|
|
|
Tests::ProjectOpenerAndCloser projectManager;
|
|
|
|
|
const ProjectInfo projectInfo = projectManager.open(projectFilePath, true);
|
2019-11-28 16:17:57 +01:00
|
|
|
const bool isProjectOpen = projectInfo.isValid();
|
|
|
|
|
QVERIFY(isProjectOpen);
|
2018-05-09 09:22:01 +02:00
|
|
|
|
2019-11-28 16:17:57 +01:00
|
|
|
// Run tool
|
2019-09-25 15:46:15 +02:00
|
|
|
ClangTool *tool = ClangTool::instance();
|
|
|
|
|
tool->startTool(ClangTool::FileSelection::AllFiles,
|
|
|
|
|
ClangToolsSettings::instance()->runSettings(),
|
|
|
|
|
diagnosticConfig);
|
2019-11-28 16:17:57 +01:00
|
|
|
QSignalSpy waitForFinishedTool(tool, &ClangTool::finished);
|
|
|
|
|
QVERIFY(waitForFinishedTool.wait(30000));
|
|
|
|
|
|
|
|
|
|
// Check for errors
|
|
|
|
|
const QString errorText = waitForFinishedTool.takeFirst().first().toString();
|
|
|
|
|
const bool finishedSuccessfully = errorText.isEmpty();
|
|
|
|
|
if (!finishedSuccessfully)
|
|
|
|
|
qWarning("Error: %s", qPrintable(errorText));
|
|
|
|
|
QVERIFY(finishedSuccessfully);
|
2017-05-08 09:21:48 +02:00
|
|
|
QCOMPARE(tool->diagnostics().count(), expectedDiagCount);
|
2015-02-04 15:19:30 +01:00
|
|
|
}
|
|
|
|
|
|
2018-04-24 15:30:19 +02:00
|
|
|
void ClangToolsUnitTests::testProject_data()
|
2015-02-04 15:19:30 +01:00
|
|
|
{
|
|
|
|
|
QTest::addColumn<QString>("projectFilePath");
|
|
|
|
|
QTest::addColumn<int>("expectedDiagCount");
|
2019-09-25 15:39:32 +02:00
|
|
|
QTest::addColumn<ClangDiagnosticConfig>("diagnosticConfig");
|
2015-04-15 18:24:58 +02:00
|
|
|
|
2019-07-09 14:54:05 +02:00
|
|
|
// Test simple C++ project.
|
2019-09-25 15:39:32 +02:00
|
|
|
ClangDiagnosticConfig config = configFor("modernize-use-nullptr", QString());
|
2019-07-09 14:54:05 +02:00
|
|
|
addTestRow("simple/simple.qbs", 1, config);
|
|
|
|
|
addTestRow("simple/simple.pro", 1, config);
|
2016-07-08 15:30:48 +02:00
|
|
|
|
2019-07-09 14:54:05 +02:00
|
|
|
// Test simple Qt project.
|
|
|
|
|
config = configFor("readability-static-accessed-through-instance", QString());
|
|
|
|
|
addTestRow("qt-widgets-app/qt-widgets-app.qbs", 1, config);
|
|
|
|
|
addTestRow("qt-widgets-app/qt-widgets-app.pro", 1, config);
|
2016-07-08 15:30:48 +02:00
|
|
|
|
2019-07-09 14:54:05 +02:00
|
|
|
// Test that libraries can be analyzed.
|
|
|
|
|
config = configFor(QString(), QString());
|
|
|
|
|
addTestRow("simple-library/simple-library.qbs", 0, config);
|
|
|
|
|
addTestRow("simple-library/simple-library.pro", 0, config);
|
2016-07-08 15:30:48 +02:00
|
|
|
|
2019-07-09 14:54:05 +02:00
|
|
|
// Test that standard headers can be parsed.
|
|
|
|
|
addTestRow("stdc++11-includes/stdc++11-includes.qbs", 0, config);
|
|
|
|
|
addTestRow("stdc++11-includes/stdc++11-includes.pro", 0, config);
|
2016-07-08 15:30:48 +02:00
|
|
|
|
2019-07-09 14:54:05 +02:00
|
|
|
// Test that qt essential headers can be parsed.
|
|
|
|
|
addTestRow("qt-essential-includes/qt-essential-includes.qbs", 0, config);
|
|
|
|
|
addTestRow("qt-essential-includes/qt-essential-includes.pro", 0, config);
|
2017-08-17 09:40:02 +02:00
|
|
|
|
2019-07-09 14:54:05 +02:00
|
|
|
// Test that mingw includes can be parsed.
|
|
|
|
|
addTestRow("mingw-includes/mingw-includes.qbs", 0, config);
|
|
|
|
|
addTestRow("mingw-includes/mingw-includes.pro", 0, config);
|
2018-04-24 17:34:21 +02:00
|
|
|
|
2019-07-09 14:54:05 +02:00
|
|
|
// Test that tidy and clazy diagnostics are emitted for the same project.
|
2018-05-09 09:22:01 +02:00
|
|
|
addTestRow("clangtidy_clazy/clangtidy_clazy.pro",
|
2019-07-09 14:54:05 +02:00
|
|
|
1 /*tidy*/ + 1 /*clazy*/,
|
2019-11-21 14:19:19 +01:00
|
|
|
configFor("misc-unconventional-assign-operator", "qgetenv"));
|
2016-07-08 15:30:48 +02:00
|
|
|
}
|
|
|
|
|
|
2018-05-09 09:22:01 +02:00
|
|
|
void ClangToolsUnitTests::addTestRow(const QByteArray &relativeFilePath,
|
2019-07-09 14:54:05 +02:00
|
|
|
int expectedDiagCount,
|
2019-09-25 15:39:32 +02:00
|
|
|
const ClangDiagnosticConfig &diagnosticConfig)
|
2016-07-08 15:30:48 +02:00
|
|
|
{
|
|
|
|
|
const QString absoluteFilePath = m_tmpDir->absolutePath(relativeFilePath);
|
|
|
|
|
const QString fileName = QFileInfo(absoluteFilePath).fileName();
|
|
|
|
|
|
2019-07-09 14:54:05 +02:00
|
|
|
QTest::newRow(fileName.toUtf8().constData())
|
|
|
|
|
<< absoluteFilePath << expectedDiagCount << diagnosticConfig;
|
2015-02-04 15:19:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
2018-03-14 12:58:12 +01:00
|
|
|
} // namespace ClangTools
|