Skip to content

Commit

Permalink
move guideline/classification functions to errorlogger
Browse files Browse the repository at this point in the history
  • Loading branch information
ludviggunne committed Jan 19, 2025
1 parent b955c4f commit b5d0944
Show file tree
Hide file tree
Showing 16 changed files with 322 additions and 328 deletions.
74 changes: 37 additions & 37 deletions Makefile

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1231,24 +1231,24 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
else if (std::strncmp(argv[i], "--report-type=", 14) == 0) {
const std::string typeStr = argv[i] + 14;
if (typeStr == "normal") {
mSettings.reportType = checkers::ReportType::normal;
mSettings.reportType = ReportType::normal;
} else if (typeStr == "autosar") {
mSettings.reportType = checkers::ReportType::autosar;
mSettings.reportType = ReportType::autosar;
} else if (typeStr == "certC") {
mSettings.reportType = checkers::ReportType::certC;
mSettings.reportType = ReportType::certC;
} else if (typeStr == "certCpp") {
mSettings.reportType = checkers::ReportType::certCpp;
mSettings.reportType = ReportType::certCpp;
} else if (typeStr == "misraC") {
mSettings.reportType = checkers::ReportType::misraC;
mSettings.reportType = ReportType::misraC;
} else if (typeStr == "misraCpp2008") {
mSettings.reportType = checkers::ReportType::misraCpp2008;
mSettings.reportType = ReportType::misraCpp2008;
} else if (typeStr == "misraCpp2023") {
mSettings.reportType = checkers::ReportType::misraCpp2023;
mSettings.reportType = ReportType::misraCpp2023;
} else {
mLogger.printError("Unknown report type \'" + typeStr + "\'");
return Result::Fail;
}
mSettings.guidelineMapping = checkers::createGuidelineMapping(mSettings.reportType);
mSettings.guidelineMapping = createGuidelineMapping(mSettings.reportType);
}

// Rule given at command line
Expand Down
7 changes: 3 additions & 4 deletions cli/cppcheckexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "cppcheckexecutor.h"

#include "analyzerinfo.h"
#include "checkers.h"
#include "checkersreport.h"
#include "cmdlinelogger.h"
#include "cmdlineparser.h"
Expand Down Expand Up @@ -628,9 +627,9 @@ void StdLogger::reportErr(const ErrorMessage &msg)
return;

ErrorMessage msgCopy = msg;
msgCopy.guideline = checkers::getGuideline(msgCopy.id, mSettings.reportType,
mSettings.guidelineMapping, msgCopy.severity);
msgCopy.classification = checkers::getClassification(msgCopy.guideline, mSettings.reportType);
msgCopy.guideline = getGuideline(msgCopy.id, mSettings.reportType,
mSettings.guidelineMapping, msgCopy.severity);
msgCopy.classification = getClassification(msgCopy.guideline, mSettings.reportType);

if (mSettings.outputFormat == Settings::OutputFormat::sarif)
mSarifReport.addFinding(msgCopy);
Expand Down
3 changes: 1 addition & 2 deletions gui/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <QMap>
#include <QString>

#include "checkers.h"
#include "errorlogger.h"

/// @addtogroup GUI
/// @{
Expand Down Expand Up @@ -54,7 +54,6 @@

// Report type
#define SETTINGS_REPORT_TYPE "Report type"
using ReportType = checkers::ReportType;

// Show * states
#define SETTINGS_SHOW_STYLE "Show style"
Expand Down
10 changes: 5 additions & 5 deletions gui/resultstree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ static constexpr int COLUMN_CWE = 12;

static QString getGuideline(ReportType reportType, const std::map<std::string, std::string> &guidelineMapping,
const QString& errorId, Severity severity) {
return QString::fromStdString(checkers::getGuideline(errorId.toStdString(),
reportType, guidelineMapping,
severity));
return QString::fromStdString(getGuideline(errorId.toStdString(),
reportType, guidelineMapping,
severity));
}

static QString getClassification(ReportType reportType, const QString& guideline) {
return QString::fromStdString(checkers::getClassification(guideline.toStdString(), reportType));
return QString::fromStdString(getClassification(guideline.toStdString(), reportType));
}

static Severity getSeverityFromClassification(const QString &c) {
Expand Down Expand Up @@ -167,7 +167,7 @@ void ResultsTree::keyPressEvent(QKeyEvent *event)
void ResultsTree::setReportType(ReportType reportType) {
mReportType = reportType;

mGuideline = checkers::createGuidelineMapping(reportType);
mGuideline = createGuidelineMapping(reportType);

for (int i = 0; i < mModel.rowCount(); ++i) {
const QStandardItem *fileItem = mModel.item(i, COLUMN_FILE);
Expand Down
1 change: 0 additions & 1 deletion gui/test/filelist/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ add_executable(test-filelist
${CMAKE_SOURCE_DIR}/lib/pathmatch.cpp
${CMAKE_SOURCE_DIR}/lib/path.cpp
${CMAKE_SOURCE_DIR}/lib/utils.cpp
${CMAKE_SOURCE_DIR}/lib/checkersidmapping.cpp
$<TARGET_OBJECTS:simplecpp_objs>
)
target_include_directories(test-filelist PRIVATE ${CMAKE_SOURCE_DIR}/gui ${CMAKE_SOURCE_DIR}/lib)
Expand Down
20 changes: 17 additions & 3 deletions gui/test/resultstree/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ qt_wrap_cpp(test-resultstree_SRC
${CMAKE_SOURCE_DIR}/gui/threadhandler.h
${CMAKE_SOURCE_DIR}/gui/threadresult.h
)
if(USE_BUNDLED_TINYXML2)
list(APPEND test-resultstree_SRC $<TARGET_OBJECTS:tinyxml2_objs>)
endif()
list(APPEND test-resultstree_SRC $<TARGET_OBJECTS:simplecpp_objs> $<TARGET_OBJECTS:cppcheck-core>)
add_custom_target(build-resultstree-deps SOURCES ${test-resultstree_SRC})
add_dependencies(gui-build-deps build-resultstree-deps)
add_executable(test-resultstree
Expand All @@ -16,13 +20,23 @@ add_executable(test-resultstree
${CMAKE_SOURCE_DIR}/gui/showtypes.cpp
${CMAKE_SOURCE_DIR}/gui/report.cpp
${CMAKE_SOURCE_DIR}/gui/xmlreportv2.cpp
${CMAKE_SOURCE_DIR}/lib/checkers.cpp
${CMAKE_SOURCE_DIR}/lib/checkersidmapping.cpp
${CMAKE_SOURCE_DIR}/lib/utils.cpp
)
target_include_directories(test-resultstree PRIVATE ${CMAKE_SOURCE_DIR}/gui ${CMAKE_SOURCE_DIR}/lib)
target_externals_include_directories(test-resultstree PRIVATE ${CMAKE_SOURCE_DIR}/externals/simplecpp)
if(USE_BUNDLED_TINYXML2)
target_externals_include_directories(test-resultstree PRIVATE ${PROJECT_SOURCE_DIR}/externals/tinyxml2/)
else()
target_include_directories(test-resultstree SYSTEM PRIVATE ${tinyxml2_INCLUDE_DIRS})
endif()
if (HAVE_RULES)
target_link_libraries(test-resultstree ${PCRE_LIBRARY})
target_include_directories(test-resultstree SYSTEM PRIVATE ${PCRE_INCLUDE})
endif()
target_compile_definitions(test-resultstree PRIVATE SRCDIR="${CMAKE_CURRENT_SOURCE_DIR}")
target_link_libraries(test-resultstree ${QT_CORE_LIB} ${QT_GUI_LIB} ${QT_WIDGETS_LIB} ${QT_TEST_LIB})
if(tinyxml2_FOUND AND NOT USE_BUNDLED_TINYXML2)
target_link_libraries(cppcheck ${tinyxml2_LIBRARIES})
endif()

if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# Q_UNUSED() in generated code
Expand Down
21 changes: 0 additions & 21 deletions gui/test/resultstree/testresultstree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ void ProjectFile::setWarningTags(std::size_t /*unused*/, const QString& /*unused
bool ProjectFile::write(const QString & /*unused*/) {
return true;
}
std::string severityToString(Severity severity) {
return std::to_string((int)severity);
}
ApplicationList::ApplicationList(QObject *parent) : QObject(parent) {}
ApplicationList::~ApplicationList() = default;
int ApplicationList::getApplicationCount() const {
Expand Down Expand Up @@ -122,24 +119,6 @@ void ThreadResult::reportErr(const ErrorMessage & /*unused*/) {
throw 1;
}

// Mock LIB...
bool Path::isHeader(std::string const& /*unused*/) {
return false;
}
const std::set<std::string> ErrorLogger::mCriticalErrorIds;
std::string ErrorMessage::FileLocation::getfile(bool /*unused*/) const {
return std::string();
}
const char* CppCheck::version() {
return "1.0";
}
std::pair<std::string, std::string> Settings::getNameAndVersion(const std::string& /*unused*/) {
throw 1;
}
Severity severityFromString(const std::string& severity) {
return (Severity)std::stoi(severity);
}

// Test...

void TestResultsTree::test1() const
Expand Down
180 changes: 0 additions & 180 deletions lib/checkers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2097,183 +2097,3 @@ std::vector<checkers::Info> checkers::certCppInfo{
{"MSC53-CPP", "L2"},
{"MSC54-CPP", "L2"},
};

namespace checkers {
std::string getClassification(const std::string &guideline, ReportType reportType) {
if (guideline.empty())
return "";

const auto getClassification = [](const std::vector<checkers::Info> &info, const std::string &guideline) -> std::string {
const auto it = std::find_if(info.cbegin(), info.cend(), [&](const checkers::Info &i) {
return caseInsensitiveStringCompare(i.guideline, guideline) == 0;
});
if (it == info.cend())
return "";
return it->classification;
};

switch (reportType) {
case ReportType::autosar:
return getClassification(checkers::autosarInfo, guideline);
case ReportType::certC:
return getClassification(checkers::certCInfo, guideline);
case ReportType::certCpp:
return getClassification(checkers::certCppInfo, guideline);
case ReportType::misraC:
{
auto components = splitString<std::vector>(guideline, '.');
if (components.size() != 2)
return "";

const int a = std::stoi(components[0]);
const int b = std::stoi(components[1]);

const std::vector<checkers::MisraInfo> &info = checkers::misraC2012Rules;
const auto it = std::find_if(info.cbegin(), info.cend(), [&](const checkers::MisraInfo &i) {
return i.a == a && i.b == b;
});

if (it == info.cend())
return "";

return it->str;
}
case ReportType::misraCpp2008:
case ReportType::misraCpp2023:
{
char delim;
const std::vector<checkers::MisraCppInfo> *info;
if (reportType == ReportType::misraCpp2008) {
delim = '-';
info = &checkers::misraCpp2008Rules;
} else {
delim = '.';
info = &checkers::misraCpp2023Rules;
}

auto components = splitString<std::vector>(guideline, delim);
if (components.size() != 3)
return "";

const int a = std::stoi(components[0]);
const int b = std::stoi(components[1]);
const int c = std::stoi(components[2]);

const auto it = std::find_if(info->cbegin(), info->cend(), [&](const checkers::MisraCppInfo &i) {
return i.a == a && i.b == b && i.c == c;
});

if (it == info->cend())
return "";

return it->classification;
}
default:
return "";
}
}

std::string getGuideline(const std::string &errId, ReportType reportType,
const std::map<std::string, std::string> &guidelineMapping,
Severity severity)
{
std::string guideline;

switch (reportType) {
case ReportType::autosar:
if (errId.rfind("premium-autosar-", 0) == 0) {
guideline = errId.substr(16);
break;
}
if (errId.rfind("premium-misra-cpp-2008-", 0) == 0)
guideline = "M" + errId.substr(23);
break;
case ReportType::certC:
case ReportType::certCpp:
if (errId.rfind("premium-cert-", 0) == 0) {
guideline = errId.substr(13);
std::transform(guideline.begin(), guideline.end(),
guideline.begin(), static_cast<int (*)(int)>(std::toupper));
}
break;
case ReportType::misraC:
if (errId.rfind("misra-c20", 0) == 0)
guideline = errId.substr(errId.rfind('-') + 1);
break;
case ReportType::misraCpp2008:
if (errId.rfind("misra-cpp-2008-", 0) == 0)
guideline = errId.substr(15);
break;
case ReportType::misraCpp2023:
if (errId.rfind("misra-cpp-2023-", 0) == 0)
guideline = errId.substr(15);
break;
default:
break;
}

if (!guideline.empty())
return guideline;

auto it = guidelineMapping.find(errId);

if (it != guidelineMapping.cend())
return it->second;

if (severity != Severity::error && severity != Severity::warning)
return "";

it = guidelineMapping.find(errId);

if (it != guidelineMapping.cend())
return it->second;

return "";
}

std::map<std::string, std::string> createGuidelineMapping(ReportType reportType) {
std::map<std::string, std::string> guidelineMapping;
const std::vector<IdMapping> *idMapping1 = nullptr;
const std::vector<IdMapping> *idMapping2 = nullptr;
std::string ext1, ext2;

switch (reportType) {
case ReportType::autosar:
idMapping1 = &idMappingAutosar;
break;
case ReportType::certCpp:
idMapping2 = &idMappingCertCpp;
ext2 = "-CPP";
FALLTHROUGH;
case ReportType::certC:
idMapping1 = &idMappingCertC;
ext1 = "-C";
break;
case ReportType::misraC:
idMapping1 = &idMappingMisraC;
break;
case ReportType::misraCpp2008:
idMapping1 = &idMappingMisraCpp2008;
break;
case ReportType::misraCpp2023:
idMapping1 = &idMappingMisraCpp2023;
break;
default:
break;
}

if (idMapping1) {
for (const auto &i : *idMapping1)
for (const std::string &cppcheckId : splitString<std::vector>(i.cppcheckId, ','))
guidelineMapping[cppcheckId] = i.guideline + ext1;
}

if (idMapping2) {
for (const auto &i : *idMapping2)
for (const std::string &cppcheckId : splitString<std::vector>(i.cppcheckId, ','))
guidelineMapping[cppcheckId] = i.guideline + ext2;
}

return guidelineMapping;
}
}
16 changes: 0 additions & 16 deletions lib/checkers.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,6 @@ namespace checkers {
extern CPPCHECKLIB const std::map<std::string, std::string> allCheckers;
extern CPPCHECKLIB const std::map<std::string, std::string> premiumCheckers;

enum class ReportType : std::uint8_t {
normal = 0,
autosar = 1,
certC = 2,
certCpp = 3,
misraC = 4,
misraCpp2008 = 5,
misraCpp2023 = 6,
};

struct CPPCHECKLIB MisraInfo {
int a;
int b;
Expand Down Expand Up @@ -84,12 +74,6 @@ namespace checkers {
extern std::vector<Info> autosarInfo;
extern std::vector<Info> certCInfo;
extern std::vector<Info> certCppInfo;

extern CPPCHECKLIB std::string getClassification(const std::string &guideline, ReportType reportType);
extern CPPCHECKLIB std::string getGuideline(const std::string &errId, ReportType reportType,
const std::map<std::string, std::string> &guidelineMapping,
Severity severity);
extern CPPCHECKLIB std::map<std::string, std::string> createGuidelineMapping(ReportType reportType);
}

#endif
Loading

0 comments on commit b5d0944

Please sign in to comment.