Repository navigation
Expand file tree
/
Copy pathgitlabcodequalityreport.cpp
More file actions
124 lines (108 loc) · 3.85 KB
/
Copy pathgitlabcodequalityreport.cpp
File metadata and controls
124 lines (108 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2026 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "gitlabcodequalityreport.h"
#include "errorlogger.h"
#include "errortypes.h"
#include "path.h"
#include "utils.h"
#include <cstdint>
#include <utility>
namespace {
std::string locationPath(const ErrorMessage::FileLocation& location)
{
std::string path = location.getfile(false);
while (startsWith(path, "./"))
path.erase(0, 2);
return path;
}
picojson::object serializeLocation(const ErrorMessage::FileLocation& location)
{
picojson::object lines;
lines["begin"] = picojson::value(static_cast<int64_t>(location.line < 1 ? 1 : location.line));
picojson::object result;
result["path"] = picojson::value(locationPath(location));
result["lines"] = picojson::value(lines);
return result;
}
std::string fingerprint(const ErrorMessage& finding)
{
if (finding.hash != 0) {
return std::to_string(finding.hash);
}
// Findings without token information have no warning hash.
const ErrorMessage::FileLocation& location = finding.callStack.back();
return finding.id + ':' + finding.shortMessage() + ':' + locationPath(location) + ':' +
std::to_string(location.line);
}
}
void GitlabCodeQualityReport::addFinding(ErrorMessage msg)
{
if (!msg.callStack.empty() && Path::isAbsolute(locationPath(msg.callStack.back()))) {
mHasAbsolutePath = true;
}
mFindings.push_back(std::move(msg));
}
picojson::object GitlabCodeQualityReport::serializeFinding(const ErrorMessage& finding)
{
const ErrorMessage::FileLocation& location = finding.callStack.back();
std::string description = finding.shortMessage();
if (finding.cwe.id > 0) {
description += " (CWE-" + std::to_string(finding.cwe.id) + ")";
}
picojson::object result;
result["description"] = picojson::value(description);
result["check_name"] = picojson::value(finding.id);
result["fingerprint"] = picojson::value(fingerprint(finding));
result["severity"] = picojson::value(codeQualitySeverity(finding));
result["location"] = picojson::value(serializeLocation(location));
return result;
}
std::string GitlabCodeQualityReport::serialize() const
{
picojson::array findings;
for (const ErrorMessage& finding : mFindings) {
// GitLab requires a location for every code quality finding.
if (finding.callStack.empty()) {
continue;
}
findings.emplace_back(serializeFinding(finding));
}
return picojson::value(findings).serialize(true);
}
std::string GitlabCodeQualityReport::codeQualitySeverity(const ErrorMessage& finding)
{
if (ErrorLogger::isCriticalErrorId(finding.id)) {
return "critical";
}
switch (finding.severity) {
case Severity::error:
return "critical";
case Severity::warning:
return "major";
case Severity::style:
case Severity::performance:
case Severity::portability:
return "minor";
case Severity::information:
case Severity::internal:
case Severity::debug:
case Severity::none:
return "info";
}
return "info";
}