Skip to content

Commit 7aa27f5

Browse files
committed
movec reporttype logic to checkers.*
1 parent 86cee80 commit 7aa27f5

File tree

7 files changed

+141
-147
lines changed

7 files changed

+141
-147
lines changed

cli/cmdlineparser.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,19 +1152,19 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
11521152
else if (std::strncmp(argv[i], "--report-type=", 14) == 0) {
11531153
const std::string typeStr = argv[i] + 14;
11541154
if (typeStr == "normal") {
1155-
mSettings.reportType = Settings::ReportType::Normal;
1155+
mSettings.reportType = checkers::ReportType::normal;
11561156
} else if (typeStr == "autosar") {
1157-
mSettings.reportType = Settings::ReportType::Autosar;
1157+
mSettings.reportType = checkers::ReportType::autosar;
11581158
} else if (typeStr == "certC") {
1159-
mSettings.reportType = Settings::ReportType::CertC;
1159+
mSettings.reportType = checkers::ReportType::certC;
11601160
} else if (typeStr == "certCpp") {
1161-
mSettings.reportType = Settings::ReportType::CertCpp;
1161+
mSettings.reportType = checkers::ReportType::certCpp;
11621162
} else if (typeStr == "misraC") {
1163-
mSettings.reportType = Settings::ReportType::MisraC;
1163+
mSettings.reportType = checkers::ReportType::misraC;
11641164
} else if (typeStr == "misraCpp2008") {
1165-
mSettings.reportType = Settings::ReportType::MisraCpp2008;
1165+
mSettings.reportType = checkers::ReportType::misraCpp2008;
11661166
} else if (typeStr == "misraCpp2023") {
1167-
mSettings.reportType = Settings::ReportType::MisraCpp2023;
1167+
mSettings.reportType = checkers::ReportType::misraCpp2023;
11681168
} else {
11691169
mLogger.printError("Unknown report type \'" + typeStr + "\'");
11701170
return Result::Fail;

gui/resultstree.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "common.h"
2424
#include "showtypes.h"
25+
#include "checkers.h"
2526

2627
#include <cstdint>
2728

@@ -185,7 +186,7 @@ class ResultsTree : public QTreeView {
185186

186187
void keyPressEvent(QKeyEvent *event) override;
187188

188-
void setReportType(ReportType reportType);
189+
void setReportType(checkers::ReportType reportType);
189190

190191
signals:
191192
/**

lib/checkers.cpp

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// python3 tools/get_checkers.py > lib/checkers.cpp
2121

2222
#include "checkers.h"
23+
#include "utils.h"
2324

2425
namespace checkers {
2526
const std::map<std::string, std::string> allCheckers{
@@ -2096,3 +2097,119 @@ std::vector<checkers::Info> checkers::certCppInfo{
20962097
{"MSC53-CPP", "L2"},
20972098
{"MSC54-CPP", "L2"},
20982099
};
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+
std::vector<std::string> components = splitStringVector(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+
std::vector<std::string> components = splitStringVector(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+
{
2178+
switch (reportType) {
2179+
case ReportType::autosar:
2180+
if (errId.rfind("premium-autosar-", 0) == 0) {
2181+
return errId.substr(16);
2182+
}
2183+
if (errId.rfind("premium-misra-cpp-2008-", 0) == 0) {
2184+
return errId.substr(23);
2185+
}
2186+
return "";
2187+
case ReportType::certC:
2188+
case ReportType::certCpp:
2189+
if (errId.rfind("premium-cert-", 0) == 0) {
2190+
std::string guideline = errId.substr(13);
2191+
std::transform(guideline.begin(), guideline.end(),
2192+
guideline.begin(), static_cast<int (*)(int)>(std::toupper));
2193+
return guideline;
2194+
}
2195+
return "";
2196+
case ReportType::misraC:
2197+
if (errId.rfind("misra-c20", 0) == 0) {
2198+
return errId.substr(errId.rfind('-') + 1);
2199+
}
2200+
return "";
2201+
case ReportType::misraCpp2008:
2202+
if (errId.rfind("misra-cpp-2008-", 0) == 0) {
2203+
return errId.substr(15);
2204+
}
2205+
return "";
2206+
case ReportType::misraCpp2023:
2207+
if (errId.rfind("misra-cpp-2023-", 0) == 0) {
2208+
return errId.substr(15);
2209+
}
2210+
return "";
2211+
default:
2212+
return "";
2213+
}
2214+
}
2215+
}

lib/checkers.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ namespace checkers {
2929
extern CPPCHECKLIB const std::map<std::string, std::string> allCheckers;
3030
extern CPPCHECKLIB const std::map<std::string, std::string> premiumCheckers;
3131

32+
enum class ReportType {
33+
normal,
34+
autosar,
35+
certC,
36+
certCpp,
37+
misraC,
38+
misraCpp2008,
39+
misraCpp2023,
40+
};
41+
3242
struct CPPCHECKLIB MisraInfo {
3343
int a;
3444
int b;
@@ -73,6 +83,9 @@ namespace checkers {
7383
extern std::vector<Info> autosarInfo;
7484
extern std::vector<Info> certCInfo;
7585
extern std::vector<Info> certCppInfo;
86+
87+
extern CPPCHECKLIB std::string getClassification(const std::string &guideline, ReportType reportType);
88+
extern CPPCHECKLIB std::string getGuideline(const std::string &errId, ReportType reportType);
7689
}
7790

7891
#endif

lib/cppcheck.cpp

Lines changed: 0 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,120 +1419,6 @@ void CppCheck::executeAddons(const std::string& dumpFile, const FileWithDetails&
14191419
}
14201420
}
14211421

1422-
std::string CppCheck::getClassification(const std::string &guideline, Settings::ReportType reportType) {
1423-
if (guideline.empty())
1424-
return "";
1425-
1426-
const auto getClassification = [](const std::vector<checkers::Info> &info, const std::string &guideline) -> std::string {
1427-
const auto it = std::find_if(info.cbegin(), info.cend(), [&](const checkers::Info &i) {
1428-
return caseInsensitiveStringCompare(i.guideline, guideline) == 0;
1429-
});
1430-
if (it == info.cend())
1431-
return "";
1432-
return it->classification;
1433-
};
1434-
1435-
switch (reportType) {
1436-
case Settings::ReportType::Autosar:
1437-
return getClassification(checkers::autosarInfo, guideline);
1438-
case Settings::ReportType::CertC:
1439-
return getClassification(checkers::certCInfo, guideline);
1440-
case Settings::ReportType::CertCpp:
1441-
return getClassification(checkers::certCppInfo, guideline);
1442-
case Settings::ReportType::MisraC:
1443-
{
1444-
std::vector<std::string> components = split(guideline, ".");
1445-
if (components.size() != 2)
1446-
return "";
1447-
1448-
const int a = std::stoi(components[0]);
1449-
const int b = std::stoi(components[1]);
1450-
1451-
const std::vector<checkers::MisraInfo> &info = checkers::misraC2012Rules;
1452-
const auto it = std::find_if(info.cbegin(), info.cend(), [&](const checkers::MisraInfo &i) {
1453-
return i.a == a && i.b == b;
1454-
});
1455-
1456-
if (it == info.cend())
1457-
return "";
1458-
1459-
return it->str;
1460-
}
1461-
case Settings::ReportType::MisraCpp2008:
1462-
case Settings::ReportType::MisraCpp2023:
1463-
{
1464-
std::string delim;
1465-
const std::vector<checkers::MisraCppInfo> *info;
1466-
if (reportType == Settings::ReportType::MisraCpp2008) {
1467-
delim = "-";
1468-
info = &checkers::misraCpp2008Rules;
1469-
} else {
1470-
delim = ".";
1471-
info = &checkers::misraCpp2023Rules;
1472-
}
1473-
1474-
std::vector<std::string> components = split(guideline, delim);
1475-
if (components.size() != 3)
1476-
return "";
1477-
1478-
const int a = std::stoi(components[0]);
1479-
const int b = std::stoi(components[1]);
1480-
const int c = std::stoi(components[2]);
1481-
1482-
const auto it = std::find_if(info->cbegin(), info->cend(), [&](const checkers::MisraCppInfo &i) {
1483-
return i.a == a && i.b == b && i.c == c;
1484-
});
1485-
1486-
if (it == info->cend())
1487-
return "";
1488-
1489-
return it->classification;
1490-
}
1491-
default:
1492-
return "";
1493-
}
1494-
}
1495-
1496-
std::string CppCheck::getGuideline(const std::string &errId, Settings::ReportType reportType)
1497-
{
1498-
switch (reportType) {
1499-
case Settings::ReportType::Autosar:
1500-
if (errId.rfind("premium-autosar-", 0) == 0) {
1501-
return errId.substr(16);
1502-
}
1503-
if (errId.rfind("premium-misra-cpp-2008-", 0) == 0) {
1504-
return errId.substr(23);
1505-
}
1506-
return "";
1507-
case Settings::ReportType::CertC:
1508-
case Settings::ReportType::CertCpp:
1509-
if (errId.rfind("premium-cert-", 0) == 0) {
1510-
std::string guideline = errId.substr(13);
1511-
std::transform(guideline.begin(), guideline.end(),
1512-
guideline.begin(), static_cast<int (*)(int)>(std::toupper));
1513-
return guideline;
1514-
}
1515-
return "";
1516-
case Settings::ReportType::MisraC:
1517-
if (errId.rfind("misra-c20", 0) == 0) {
1518-
return errId.substr(errId.rfind('-') + 1);
1519-
}
1520-
return "";
1521-
case Settings::ReportType::MisraCpp2008:
1522-
if (errId.rfind("misra-cpp-2008-", 0) == 0) {
1523-
return errId.substr(15);
1524-
}
1525-
return "";
1526-
case Settings::ReportType::MisraCpp2023:
1527-
if (errId.rfind("misra-cpp-2023-", 0) == 0) {
1528-
return errId.substr(15);
1529-
}
1530-
return "";
1531-
default:
1532-
return "";
1533-
}
1534-
}
1535-
15361422
void CppCheck::executeAddons(const std::vector<std::string>& files, const std::string& file0)
15371423
{
15381424
if (mSettings.addons.empty() || files.empty())

lib/cppcheck.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -160,20 +160,6 @@ class CPPCHECKLIB CppCheck : ErrorLogger {
160160

161161
std::string getLibraryDumpData() const;
162162

163-
/**
164-
* @brief Get classification from a guideline
165-
* @param guideline guideline string
166-
* @param reportType report type
167-
*/
168-
static std::string getClassification(const std::string &guideline, Settings::ReportType reportType);
169-
170-
/**
171-
* @brief Get guideline from an error id
172-
* @param errId error id string
173-
* @param reportType report type
174-
*/
175-
static std::string getGuideline(const std::string &errId, Settings::ReportType reportType);
176-
177163
private:
178164
#ifdef HAVE_RULES
179165
/** Are there "simple" rules */

lib/settings.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "platform.h"
2929
#include "standards.h"
3030
#include "suppressions.h"
31+
#include "checkers.h"
3132

3233
#include <algorithm>
3334
#include <atomic>
@@ -110,18 +111,8 @@ class CPPCHECKLIB WARN_UNUSED Settings {
110111

111112
static std::pair<std::string, std::string> getNameAndVersion(const std::string& productName);
112113

113-
enum class ReportType : std::uint8_t {
114-
Normal,
115-
Autosar,
116-
CertC,
117-
CertCpp,
118-
MisraC,
119-
MisraCpp2008,
120-
MisraCpp2023,
121-
};
122-
123114
/** @brief Report type */
124-
ReportType reportType = ReportType::Normal;
115+
checkers::ReportType reportType = checkers::ReportType::normal;
125116

126117
/** @brief addons, either filename of python/json file or json data */
127118
std::unordered_set<std::string> addons;

0 commit comments

Comments
 (0)