diff --git a/shell/problem.h b/shell/problem.h --- a/shell/problem.h +++ b/shell/problem.h @@ -28,6 +28,7 @@ #include struct DetectedProblemPrivate; +struct PluginProblemPrivate; namespace KDevelop { @@ -92,6 +93,46 @@ QScopedPointer d; }; +/** + * @brief Represents a problem which detected by some plugin, for example by analyzer plugins + * like cppcheck/clang-tydy/valgrind/etc. + * + * You should have it wrapped in an IProblem::Ptr which is a shared pointer for it. + * + * Usage example: + * @code + * IProblem::Ptr problem(new PluginProblem(i18n("Cppcheck"))); + * problem->setSeverity(IProblem::Error); + * problem->setDescription(QStringLiteral("Cppcheck error message")); + * problem->setExplanation(QStringLiteral("Cppcheck error explanation")); + * + * DocumentRange range; + * range.document = IndexedString("/path/to/source/file"); + * range.setBothLines(1337); + * range.setBothColumns(12); + * problem->setFinalLocation(range); + * @endcode + * + */ +class KDEVPLATFORMSHELL_EXPORT PluginProblem : public DetectedProblem +{ +public: + explicit PluginProblem(const QString& pluginName); + ~PluginProblem() override; + + /// Overloaded method, which always returns IProblem::Source::Plugin. + Source source() const override; + + /// Overloaded method, which does nothing. + void setSource(Source source) override; + + /// Overloaded method, which returns plugin name, defined at problem construction. + QString sourceString() const override; + +private: + QScopedPointer d; +}; + } #endif diff --git a/shell/problem.cpp b/shell/problem.cpp --- a/shell/problem.cpp +++ b/shell/problem.cpp @@ -40,6 +40,16 @@ KDevelop::IProblem::FinalLocationMode m_finalLocationMode; }; +struct PluginProblemPrivate +{ + PluginProblemPrivate(const QString& pluginName) + : m_pluginName(pluginName) + { + } + + QString m_pluginName; +}; + namespace KDevelop { @@ -184,5 +194,29 @@ return {}; } +PluginProblem::PluginProblem(const QString& pluginName) + :d(new PluginProblemPrivate(pluginName)) +{ +} + +PluginProblem::~PluginProblem() +{ +} + +IProblem::Source PluginProblem::source() const +{ + return IProblem::Plugin; +} + +void PluginProblem::setSource(Source source) +{ + Q_UNUSED(source); +} + +QString PluginProblem::sourceString() const +{ + return d->m_pluginName; +} + } diff --git a/shell/tests/CMakeLists.txt b/shell/tests/CMakeLists.txt --- a/shell/tests/CMakeLists.txt +++ b/shell/tests/CMakeLists.txt @@ -35,6 +35,9 @@ ecm_add_test(test_detectedproblem.cpp LINK_LIBRARIES Qt5::Test KDev::Tests KDev::Shell KDev::Serialization) +ecm_add_test(test_pluginproblem.cpp + LINK_LIBRARIES Qt5::Test KDev::Tests KDev::Shell) + ecm_add_test(test_problemmodelset.cpp LINK_LIBRARIES Qt5::Test KDev::Tests KDev::Shell) diff --git a/shell/tests/test_pluginproblem.cpp b/shell/tests/test_pluginproblem.cpp new file mode 100644 --- /dev/null +++ b/shell/tests/test_pluginproblem.cpp @@ -0,0 +1,70 @@ +/* This file is part of KDevelop + * Copyright 2017 Anton Anikin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include +#include + +#include +#include + +using namespace KDevelop; + +class TestPluginProblem : public QObject +{ + Q_OBJECT +private slots: + void initTestCase(); + void cleanupTestCase(); + + void testSource(); +}; + +void TestPluginProblem::initTestCase() +{ + AutoTestShell::init(); + TestCore::initialize(Core::NoUi); +} + +void TestPluginProblem::cleanupTestCase() +{ + TestCore::shutdown(); +} + +void TestPluginProblem::testSource() +{ + PluginProblem p1(QStringLiteral("Plugin1")); + PluginProblem p2(QStringLiteral("Plugin2")); + PluginProblem p3(QStringLiteral("")); + + QCOMPARE(p1.sourceString(), QStringLiteral("Plugin1")); + QCOMPARE(p2.sourceString(), QStringLiteral("Plugin2")); + QCOMPARE(p3.sourceString(), QStringLiteral("")); + + p1.setSource(IProblem::Preprocessor); + p2.setSource(IProblem::Lexer); + p3.setSource(IProblem::Parser); + + QCOMPARE(p1.source(), IProblem::Plugin); + QCOMPARE(p2.source(), IProblem::Plugin); + QCOMPARE(p3.source(), IProblem::Plugin); +} + +QTEST_MAIN(TestPluginProblem) + +#include "test_pluginproblem.moc"