Skip to content

Commit 162b91b

Browse files
committed
move guideline/classification functions to errorlogger
1 parent 6f96092 commit 162b91b

9 files changed

+220
-217
lines changed

cli/cmdlineparser.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -1231,24 +1231,24 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
12311231
else if (std::strncmp(argv[i], "--report-type=", 14) == 0) {
12321232
const std::string typeStr = argv[i] + 14;
12331233
if (typeStr == "normal") {
1234-
mSettings.reportType = checkers::ReportType::normal;
1234+
mSettings.reportType = ReportType::normal;
12351235
} else if (typeStr == "autosar") {
1236-
mSettings.reportType = checkers::ReportType::autosar;
1236+
mSettings.reportType = ReportType::autosar;
12371237
} else if (typeStr == "certC") {
1238-
mSettings.reportType = checkers::ReportType::certC;
1238+
mSettings.reportType = ReportType::certC;
12391239
} else if (typeStr == "certCpp") {
1240-
mSettings.reportType = checkers::ReportType::certCpp;
1240+
mSettings.reportType = ReportType::certCpp;
12411241
} else if (typeStr == "misraC") {
1242-
mSettings.reportType = checkers::ReportType::misraC;
1242+
mSettings.reportType = ReportType::misraC;
12431243
} else if (typeStr == "misraCpp2008") {
1244-
mSettings.reportType = checkers::ReportType::misraCpp2008;
1244+
mSettings.reportType = ReportType::misraCpp2008;
12451245
} else if (typeStr == "misraCpp2023") {
1246-
mSettings.reportType = checkers::ReportType::misraCpp2023;
1246+
mSettings.reportType = ReportType::misraCpp2023;
12471247
} else {
12481248
mLogger.printError("Unknown report type \'" + typeStr + "\'");
12491249
return Result::Fail;
12501250
}
1251-
mSettings.guidelineMapping = checkers::createGuidelineMapping(mSettings.reportType);
1251+
mSettings.guidelineMapping = createGuidelineMapping(mSettings.reportType);
12521252
}
12531253

12541254
// Rule given at command line

cli/cppcheckexecutor.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "cppcheckexecutor.h"
2424

2525
#include "analyzerinfo.h"
26-
#include "checkers.h"
2726
#include "checkersreport.h"
2827
#include "cmdlinelogger.h"
2928
#include "cmdlineparser.h"
@@ -628,9 +627,9 @@ void StdLogger::reportErr(const ErrorMessage &msg)
628627
return;
629628

630629
ErrorMessage msgCopy = msg;
631-
msgCopy.guideline = checkers::getGuideline(msgCopy.id, mSettings.reportType,
632-
mSettings.guidelineMapping, msgCopy.severity);
633-
msgCopy.classification = checkers::getClassification(msgCopy.guideline, mSettings.reportType);
630+
msgCopy.guideline = getGuideline(msgCopy.id, mSettings.reportType,
631+
mSettings.guidelineMapping, msgCopy.severity);
632+
msgCopy.classification = getClassification(msgCopy.guideline, mSettings.reportType);
634633

635634
if (mSettings.outputFormat == Settings::OutputFormat::sarif)
636635
mSarifReport.addFinding(msgCopy);

gui/common.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include <QMap>
2525
#include <QString>
2626

27-
#include "checkers.h"
27+
#include "errorlogger.h"
2828

2929
/// @addtogroup GUI
3030
/// @{
@@ -54,7 +54,6 @@
5454

5555
// Report type
5656
#define SETTINGS_REPORT_TYPE "Report type"
57-
using ReportType = checkers::ReportType;
5857

5958
// Show * states
6059
#define SETTINGS_SHOW_STYLE "Show style"

gui/resultstree.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ static constexpr int COLUMN_CWE = 12;
100100

101101
static QString getGuideline(ReportType reportType, const std::map<std::string, std::string> &guidelineMapping,
102102
const QString& errorId, Severity severity) {
103-
return QString::fromStdString(checkers::getGuideline(errorId.toStdString(),
104-
reportType, guidelineMapping,
105-
severity));
103+
return QString::fromStdString(getGuideline(errorId.toStdString(),
104+
reportType, guidelineMapping,
105+
severity));
106106
}
107107

108108
static QString getClassification(ReportType reportType, const QString& guideline) {
109-
return QString::fromStdString(checkers::getClassification(guideline.toStdString(), reportType));
109+
return QString::fromStdString(getClassification(guideline.toStdString(), reportType));
110110
}
111111

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

170-
mGuideline = checkers::createGuidelineMapping(reportType);
170+
mGuideline = createGuidelineMapping(reportType);
171171

172172
for (int i = 0; i < mModel.rowCount(); ++i) {
173173
const QStandardItem *fileItem = mModel.item(i, COLUMN_FILE);

lib/checkers.cpp

-180
Original file line numberDiff line numberDiff line change
@@ -2097,183 +2097,3 @@ std::vector<checkers::Info> checkers::certCppInfo{
20972097
{"MSC53-CPP", "L2"},
20982098
{"MSC54-CPP", "L2"},
20992099
};
2100-
2101-
namespace checkers {
2102-
std::string getClassification(const std::string &guideline, ReportType reportType) {
2103-
if (guideline.empty())
2104-
return "";
2105-
2106-
const auto getClassification = [](const std::vector<checkers::Info> &info, const std::string &guideline) -> std::string {
2107-
const auto it = std::find_if(info.cbegin(), info.cend(), [&](const checkers::Info &i) {
2108-
return caseInsensitiveStringCompare(i.guideline, guideline) == 0;
2109-
});
2110-
if (it == info.cend())
2111-
return "";
2112-
return it->classification;
2113-
};
2114-
2115-
switch (reportType) {
2116-
case ReportType::autosar:
2117-
return getClassification(checkers::autosarInfo, guideline);
2118-
case ReportType::certC:
2119-
return getClassification(checkers::certCInfo, guideline);
2120-
case ReportType::certCpp:
2121-
return getClassification(checkers::certCppInfo, guideline);
2122-
case ReportType::misraC:
2123-
{
2124-
auto components = splitString<std::vector>(guideline, '.');
2125-
if (components.size() != 2)
2126-
return "";
2127-
2128-
const int a = std::stoi(components[0]);
2129-
const int b = std::stoi(components[1]);
2130-
2131-
const std::vector<checkers::MisraInfo> &info = checkers::misraC2012Rules;
2132-
const auto it = std::find_if(info.cbegin(), info.cend(), [&](const checkers::MisraInfo &i) {
2133-
return i.a == a && i.b == b;
2134-
});
2135-
2136-
if (it == info.cend())
2137-
return "";
2138-
2139-
return it->str;
2140-
}
2141-
case ReportType::misraCpp2008:
2142-
case ReportType::misraCpp2023:
2143-
{
2144-
char delim;
2145-
const std::vector<checkers::MisraCppInfo> *info;
2146-
if (reportType == ReportType::misraCpp2008) {
2147-
delim = '-';
2148-
info = &checkers::misraCpp2008Rules;
2149-
} else {
2150-
delim = '.';
2151-
info = &checkers::misraCpp2023Rules;
2152-
}
2153-
2154-
auto components = splitString<std::vector>(guideline, delim);
2155-
if (components.size() != 3)
2156-
return "";
2157-
2158-
const int a = std::stoi(components[0]);
2159-
const int b = std::stoi(components[1]);
2160-
const int c = std::stoi(components[2]);
2161-
2162-
const auto it = std::find_if(info->cbegin(), info->cend(), [&](const checkers::MisraCppInfo &i) {
2163-
return i.a == a && i.b == b && i.c == c;
2164-
});
2165-
2166-
if (it == info->cend())
2167-
return "";
2168-
2169-
return it->classification;
2170-
}
2171-
default:
2172-
return "";
2173-
}
2174-
}
2175-
2176-
std::string getGuideline(const std::string &errId, ReportType reportType,
2177-
const std::map<std::string, std::string> &guidelineMapping,
2178-
Severity severity)
2179-
{
2180-
std::string guideline;
2181-
2182-
switch (reportType) {
2183-
case ReportType::autosar:
2184-
if (errId.rfind("premium-autosar-", 0) == 0) {
2185-
guideline = errId.substr(16);
2186-
break;
2187-
}
2188-
if (errId.rfind("premium-misra-cpp-2008-", 0) == 0)
2189-
guideline = "M" + errId.substr(23);
2190-
break;
2191-
case ReportType::certC:
2192-
case ReportType::certCpp:
2193-
if (errId.rfind("premium-cert-", 0) == 0) {
2194-
guideline = errId.substr(13);
2195-
std::transform(guideline.begin(), guideline.end(),
2196-
guideline.begin(), static_cast<int (*)(int)>(std::toupper));
2197-
}
2198-
break;
2199-
case ReportType::misraC:
2200-
if (errId.rfind("misra-c20", 0) == 0)
2201-
guideline = errId.substr(errId.rfind('-') + 1);
2202-
break;
2203-
case ReportType::misraCpp2008:
2204-
if (errId.rfind("misra-cpp-2008-", 0) == 0)
2205-
guideline = errId.substr(15);
2206-
break;
2207-
case ReportType::misraCpp2023:
2208-
if (errId.rfind("misra-cpp-2023-", 0) == 0)
2209-
guideline = errId.substr(15);
2210-
break;
2211-
default:
2212-
break;
2213-
}
2214-
2215-
if (!guideline.empty())
2216-
return guideline;
2217-
2218-
auto it = guidelineMapping.find(errId);
2219-
2220-
if (it != guidelineMapping.cend())
2221-
return it->second;
2222-
2223-
if (severity != Severity::error && severity != Severity::warning)
2224-
return "";
2225-
2226-
it = guidelineMapping.find(errId);
2227-
2228-
if (it != guidelineMapping.cend())
2229-
return it->second;
2230-
2231-
return "";
2232-
}
2233-
2234-
std::map<std::string, std::string> createGuidelineMapping(ReportType reportType) {
2235-
std::map<std::string, std::string> guidelineMapping;
2236-
const std::vector<IdMapping> *idMapping1 = nullptr;
2237-
const std::vector<IdMapping> *idMapping2 = nullptr;
2238-
std::string ext1, ext2;
2239-
2240-
switch (reportType) {
2241-
case ReportType::autosar:
2242-
idMapping1 = &idMappingAutosar;
2243-
break;
2244-
case ReportType::certCpp:
2245-
idMapping2 = &idMappingCertCpp;
2246-
ext2 = "-CPP";
2247-
FALLTHROUGH;
2248-
case ReportType::certC:
2249-
idMapping1 = &idMappingCertC;
2250-
ext1 = "-C";
2251-
break;
2252-
case ReportType::misraC:
2253-
idMapping1 = &idMappingMisraC;
2254-
break;
2255-
case ReportType::misraCpp2008:
2256-
idMapping1 = &idMappingMisraCpp2008;
2257-
break;
2258-
case ReportType::misraCpp2023:
2259-
idMapping1 = &idMappingMisraCpp2023;
2260-
break;
2261-
default:
2262-
break;
2263-
}
2264-
2265-
if (idMapping1) {
2266-
for (const auto &i : *idMapping1)
2267-
for (const std::string &cppcheckId : splitString<std::vector>(i.cppcheckId, ','))
2268-
guidelineMapping[cppcheckId] = i.guideline + ext1;
2269-
}
2270-
2271-
if (idMapping2) {
2272-
for (const auto &i : *idMapping2)
2273-
for (const std::string &cppcheckId : splitString<std::vector>(i.cppcheckId, ','))
2274-
guidelineMapping[cppcheckId] = i.guideline + ext2;
2275-
}
2276-
2277-
return guidelineMapping;
2278-
}
2279-
}

lib/checkers.h

-16
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,6 @@ namespace checkers {
3030
extern CPPCHECKLIB const std::map<std::string, std::string> allCheckers;
3131
extern CPPCHECKLIB const std::map<std::string, std::string> premiumCheckers;
3232

33-
enum class ReportType : std::uint8_t {
34-
normal = 0,
35-
autosar = 1,
36-
certC = 2,
37-
certCpp = 3,
38-
misraC = 4,
39-
misraCpp2008 = 5,
40-
misraCpp2023 = 6,
41-
};
42-
4333
struct CPPCHECKLIB MisraInfo {
4434
int a;
4535
int b;
@@ -84,12 +74,6 @@ namespace checkers {
8474
extern std::vector<Info> autosarInfo;
8575
extern std::vector<Info> certCInfo;
8676
extern std::vector<Info> certCppInfo;
87-
88-
extern CPPCHECKLIB std::string getClassification(const std::string &guideline, ReportType reportType);
89-
extern CPPCHECKLIB std::string getGuideline(const std::string &errId, ReportType reportType,
90-
const std::map<std::string, std::string> &guidelineMapping,
91-
Severity severity);
92-
extern CPPCHECKLIB std::map<std::string, std::string> createGuidelineMapping(ReportType reportType);
9377
}
9478

9579
#endif

0 commit comments

Comments
 (0)