Skip to content

Commit 70c91e1

Browse files
committed
move guideline mapping to library
1 parent d0f565c commit 70c91e1

File tree

7 files changed

+102
-48
lines changed

7 files changed

+102
-48
lines changed

cli/cmdlineparser.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "addoninfo.h"
2222
#include "check.h"
23+
#include "checkers.h"
2324
#include "color.h"
2425
#include "config.h"
2526
#include "cppcheck.h"
@@ -1169,6 +1170,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
11691170
mLogger.printError("Unknown report type \'" + typeStr + "\'");
11701171
return Result::Fail;
11711172
}
1173+
mSettings.guidelineMapping = checkers::createGuidelineMapping(mSettings.reportType);
11721174
}
11731175

11741176
// Rule given at command line

cli/cppcheckexecutor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,8 @@ void StdLogger::reportErr(const ErrorMessage &msg)
623623
return;
624624

625625
ErrorMessage msgCopy = msg;
626-
msgCopy.guideline = checkers::getGuideline(msgCopy.id, mSettings.reportType);
626+
msgCopy.guideline = checkers::getGuideline(msgCopy.id, mSettings.reportType,
627+
mSettings.guidelineMapping, msgCopy.severity);
627628
msgCopy.classification = checkers::getClassification(msgCopy.guideline, mSettings.reportType);
628629

629630
if (mSettings.outputFormat == Settings::OutputFormat::sarif)

gui/resultstree.cpp

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,11 @@ static constexpr int COLUMN_SINCE_DATE = 10;
9898
static constexpr int COLUMN_TAGS = 11;
9999
static constexpr int COLUMN_CWE = 12;
100100

101-
static QString getGuideline(ReportType reportType, const QMap<QString,QString>& guidelines, const QString& errorId, Severity severity) {
102-
auto guideline = QString::fromStdString(checkers::getGuideline(errorId.toStdString(), reportType));
103-
if (!guideline.isEmpty())
104-
return guideline;
105-
106-
guideline = guidelines.value(errorId);
107-
if (!guideline.isEmpty())
108-
return guideline;
109-
if (severity == Severity::error || severity == Severity::warning)
110-
return guidelines.value("error");
111-
return QString();
101+
static QString getGuideline(ReportType reportType, const std::map<std::string, std::string> &guidelineMapping,
102+
const QString& errorId, Severity severity) {
103+
return QString::fromStdString(checkers::getGuideline(errorId.toStdString(),
104+
reportType, guidelineMapping,
105+
severity));
112106
}
113107

114108
static QString getClassification(ReportType reportType, const QString& guideline) {
@@ -173,26 +167,7 @@ void ResultsTree::keyPressEvent(QKeyEvent *event)
173167
void ResultsTree::setReportType(ReportType reportType) {
174168
mReportType = reportType;
175169

176-
auto readIdMapping = [this](const std::vector<checkers::IdMapping>& idMapping, const char* ext = "") {
177-
for (const auto& i: idMapping)
178-
for (const QString& cppcheckId: QString(i.cppcheckId).split(","))
179-
mGuideline[cppcheckId] = QString(i.guideline) + ext;
180-
};
181-
182-
if (reportType == ReportType::autosar)
183-
readIdMapping(checkers::idMappingAutosar);
184-
else if (reportType == ReportType::certC)
185-
readIdMapping(checkers::idMappingCertC, "-C");
186-
else if (reportType == ReportType::certCpp) {
187-
readIdMapping(checkers::idMappingCertC, "-C");
188-
readIdMapping(checkers::idMappingCertCpp, "-CPP");
189-
}
190-
else if (reportType == ReportType::misraC)
191-
readIdMapping(checkers::idMappingMisraC);
192-
else if (reportType == ReportType::misraCpp2008)
193-
readIdMapping(checkers::idMappingMisraCpp2008);
194-
else if (reportType == ReportType::misraCpp2023)
195-
readIdMapping(checkers::idMappingMisraCpp2023);
170+
mGuideline = checkers::createGuidelineMapping(reportType);
196171

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

gui/resultstree.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ protected slots:
540540

541541
ReportType mReportType = ReportType::normal;
542542

543-
QMap<QString,QString> mGuideline;
543+
std::map<std::string, std::string> mGuideline;
544544
};
545545
/// @}
546546
#endif // RESULTSTREE_H

lib/checkers.cpp

Lines changed: 83 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,38 +2173,107 @@ namespace checkers {
21732173
}
21742174
}
21752175

2176-
std::string getGuideline(const std::string &errId, ReportType reportType)
2176+
std::string getGuideline(const std::string &errId, ReportType reportType,
2177+
const std::map<std::string, std::string> &guidelineMapping,
2178+
Severity severity)
21772179
{
2180+
std::string guideline;
2181+
21782182
switch (reportType) {
21792183
case ReportType::autosar:
2180-
if (errId.rfind("premium-autosar-", 0) == 0)
2181-
return errId.substr(16);
2184+
if (errId.rfind("premium-autosar-", 0) == 0) {
2185+
guideline = errId.substr(16);
2186+
break;
2187+
}
21822188
if (errId.rfind("premium-misra-cpp-2008-", 0) == 0)
2183-
return "M" + errId.substr(23);
2184-
return "";
2189+
guideline = "M" + errId.substr(23);
2190+
break;
21852191
case ReportType::certC:
21862192
case ReportType::certCpp:
21872193
if (errId.rfind("premium-cert-", 0) == 0) {
2188-
std::string guideline = errId.substr(13);
2194+
guideline = errId.substr(13);
21892195
std::transform(guideline.begin(), guideline.end(),
21902196
guideline.begin(), static_cast<int (*)(int)>(std::toupper));
2191-
return guideline;
21922197
}
2193-
return "";
2198+
break;
21942199
case ReportType::misraC:
21952200
if (errId.rfind("misra-c20", 0) == 0)
2196-
return errId.substr(errId.rfind('-') + 1);
2197-
return "";
2201+
guideline = errId.substr(errId.rfind('-') + 1);
2202+
break;
21982203
case ReportType::misraCpp2008:
21992204
if (errId.rfind("misra-cpp-2008-", 0) == 0)
2200-
return errId.substr(15);
2201-
return "";
2205+
guideline = errId.substr(15);
2206+
break;
22022207
case ReportType::misraCpp2023:
22032208
if (errId.rfind("misra-cpp-2023-", 0) == 0)
2204-
return errId.substr(15);
2205-
return "";
2209+
guideline = errId.substr(15);
2210+
break;
22062211
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)
22072224
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 : splitStringVector(i.cppcheckId, ','))
2268+
guidelineMapping[cppcheckId] = i.guideline + ext1;
22082269
}
2270+
2271+
if (idMapping2) {
2272+
for (const auto &i : *idMapping2)
2273+
for (const std::string &cppcheckId : splitStringVector(i.cppcheckId, ','))
2274+
guidelineMapping[cppcheckId] = i.guideline + ext2;
2275+
}
2276+
2277+
return guidelineMapping;
22092278
}
22102279
}

lib/checkers.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <vector>
2525

2626
#include "config.h"
27+
#include "errortypes.h"
2728

2829
namespace checkers {
2930
extern CPPCHECKLIB const std::map<std::string, std::string> allCheckers;
@@ -85,7 +86,10 @@ namespace checkers {
8586
extern std::vector<Info> certCppInfo;
8687

8788
extern CPPCHECKLIB std::string getClassification(const std::string &guideline, ReportType reportType);
88-
extern CPPCHECKLIB std::string getGuideline(const std::string &errId, 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);
8993
}
9094

9195
#endif

lib/settings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ class CPPCHECKLIB WARN_UNUSED Settings {
114114
/** @brief Report type */
115115
checkers::ReportType reportType = checkers::ReportType::normal;
116116

117+
/** @brief Maps cppcheck error ids to guidelines */
118+
std::map<std::string, std::string> guidelineMapping;
119+
117120
/** @brief addons, either filename of python/json file or json data */
118121
std::unordered_set<std::string> addons;
119122

0 commit comments

Comments
 (0)