|
| 1 | +/* |
| 2 | + * Cppcheck - A tool for static C/C++ code analysis |
| 3 | + * Copyright (C) 2007-2026 Cppcheck team. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "gitlabcodequalityreport.h" |
| 20 | + |
| 21 | +#include "errorlogger.h" |
| 22 | +#include "errortypes.h" |
| 23 | +#include "utils.h" |
| 24 | + |
| 25 | +#include <cstdint> |
| 26 | +#include <utility> |
| 27 | + |
| 28 | +namespace { |
| 29 | + std::string locationPath(const ErrorMessage::FileLocation& location) |
| 30 | + { |
| 31 | + std::string path = location.getfile(false); |
| 32 | + while (startsWith(path, "./")) |
| 33 | + path.erase(0, 2); |
| 34 | + return path; |
| 35 | + } |
| 36 | + |
| 37 | + picojson::object serializeLocation(const ErrorMessage::FileLocation& location) |
| 38 | + { |
| 39 | + picojson::object lines; |
| 40 | + lines["begin"] = picojson::value(static_cast<int64_t>(location.line < 1 ? 1 : location.line)); |
| 41 | + |
| 42 | + picojson::object result; |
| 43 | + result["path"] = picojson::value(locationPath(location)); |
| 44 | + result["lines"] = picojson::value(lines); |
| 45 | + return result; |
| 46 | + } |
| 47 | + |
| 48 | + std::string fingerprint(const ErrorMessage& finding) |
| 49 | + { |
| 50 | + if (finding.hash != 0) { |
| 51 | + return std::to_string(finding.hash); |
| 52 | + } |
| 53 | + |
| 54 | + // Findings without token information have no warning hash. |
| 55 | + const ErrorMessage::FileLocation& location = finding.callStack.back(); |
| 56 | + return finding.id + ':' + finding.shortMessage() + ':' + locationPath(location) + ':' + |
| 57 | + std::to_string(location.line); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +void GitlabCodeQualityReport::addFinding(ErrorMessage msg) |
| 62 | +{ |
| 63 | + mFindings.push_back(std::move(msg)); |
| 64 | +} |
| 65 | + |
| 66 | +picojson::object GitlabCodeQualityReport::serializeFinding(const ErrorMessage& finding) |
| 67 | +{ |
| 68 | + const ErrorMessage::FileLocation& location = finding.callStack.back(); |
| 69 | + |
| 70 | + picojson::object result; |
| 71 | + result["description"] = picojson::value(finding.shortMessage()); |
| 72 | + result["check_name"] = picojson::value(finding.id); |
| 73 | + result["fingerprint"] = picojson::value(fingerprint(finding)); |
| 74 | + result["severity"] = picojson::value(codeQualitySeverity(finding)); |
| 75 | + result["location"] = picojson::value(serializeLocation(location)); |
| 76 | + return result; |
| 77 | +} |
| 78 | + |
| 79 | +std::string GitlabCodeQualityReport::serialize() const |
| 80 | +{ |
| 81 | + picojson::array findings; |
| 82 | + for (const ErrorMessage& finding : mFindings) { |
| 83 | + // GitLab requires a location for every code quality finding. |
| 84 | + if (finding.callStack.empty()) { |
| 85 | + continue; |
| 86 | + } |
| 87 | + findings.emplace_back(serializeFinding(finding)); |
| 88 | + } |
| 89 | + |
| 90 | + return picojson::value(findings).serialize(true); |
| 91 | +} |
| 92 | + |
| 93 | +std::string GitlabCodeQualityReport::codeQualitySeverity(const ErrorMessage& finding) |
| 94 | +{ |
| 95 | + if (ErrorLogger::isCriticalErrorId(finding.id)) { |
| 96 | + return "critical"; |
| 97 | + } |
| 98 | + |
| 99 | + switch (finding.severity) { |
| 100 | + case Severity::error: |
| 101 | + return "critical"; |
| 102 | + case Severity::warning: |
| 103 | + return "major"; |
| 104 | + case Severity::style: |
| 105 | + case Severity::performance: |
| 106 | + case Severity::portability: |
| 107 | + return "minor"; |
| 108 | + case Severity::information: |
| 109 | + case Severity::internal: |
| 110 | + case Severity::debug: |
| 111 | + case Severity::none: |
| 112 | + return "info"; |
| 113 | + } |
| 114 | + return "info"; |
| 115 | +} |
0 commit comments