|
34 | 34 | #include "filesettings.h" |
35 | 35 | #include "json.h" |
36 | 36 | #include "path.h" |
| 37 | +#include "sarifreport.h" |
37 | 38 | #include "settings.h" |
38 | 39 | #include "singleexecutor.h" |
39 | 40 | #include "suppressions.h" |
|
80 | 81 | #endif |
81 | 82 |
|
82 | 83 | namespace { |
83 | | - class SarifReport { |
84 | | - public: |
85 | | - void addFinding(ErrorMessage msg) { |
86 | | - mFindings.push_back(std::move(msg)); |
87 | | - } |
88 | | - |
89 | | - picojson::array serializeRules() const { |
90 | | - picojson::array ret; |
91 | | - std::set<std::string> ruleIds; |
92 | | - for (const auto& finding : mFindings) { |
93 | | - // github only supports findings with locations |
94 | | - if (finding.callStack.empty()) |
95 | | - continue; |
96 | | - if (ruleIds.insert(finding.id).second) { |
97 | | - picojson::object rule; |
98 | | - rule["id"] = picojson::value(finding.id); |
99 | | - // rule.shortDescription.text |
100 | | - picojson::object shortDescription; |
101 | | - shortDescription["text"] = picojson::value(finding.shortMessage()); |
102 | | - rule["shortDescription"] = picojson::value(shortDescription); |
103 | | - // rule.fullDescription.text |
104 | | - picojson::object fullDescription; |
105 | | - fullDescription["text"] = picojson::value(finding.verboseMessage()); |
106 | | - rule["fullDescription"] = picojson::value(fullDescription); |
107 | | - // rule.help.text |
108 | | - picojson::object help; |
109 | | - help["text"] = picojson::value(finding.verboseMessage()); // FIXME provide proper help text |
110 | | - rule["help"] = picojson::value(help); |
111 | | - // rule.properties.precision, rule.properties.problem.severity |
112 | | - picojson::object properties; |
113 | | - properties["precision"] = picojson::value(sarifPrecision(finding)); |
114 | | - const char* securitySeverity = nullptr; |
115 | | - if (finding.severity == Severity::error && !ErrorLogger::isCriticalErrorId(finding.id)) |
116 | | - securitySeverity = "9.9"; // We see undefined behavior |
117 | | - //else if (finding.severity == Severity::warning) |
118 | | - // securitySeverity = 5.1; // We see potential undefined behavior |
119 | | - if (securitySeverity) { |
120 | | - properties["security-severity"] = picojson::value(securitySeverity); |
121 | | - const picojson::array tags{picojson::value("security")}; |
122 | | - properties["tags"] = picojson::value(tags); |
123 | | - } |
124 | | - rule["properties"] = picojson::value(properties); |
125 | | - // rule.defaultConfiguration.level |
126 | | - picojson::object defaultConfiguration; |
127 | | - defaultConfiguration["level"] = picojson::value(sarifSeverity(finding)); |
128 | | - rule["defaultConfiguration"] = picojson::value(defaultConfiguration); |
129 | | - |
130 | | - ret.emplace_back(rule); |
131 | | - } |
132 | | - } |
133 | | - return ret; |
134 | | - } |
135 | | - |
136 | | - static picojson::array serializeLocations(const ErrorMessage& finding) { |
137 | | - picojson::array ret; |
138 | | - for (const auto& location : finding.callStack) { |
139 | | - picojson::object physicalLocation; |
140 | | - picojson::object artifactLocation; |
141 | | - artifactLocation["uri"] = picojson::value(location.getfile(false)); |
142 | | - physicalLocation["artifactLocation"] = picojson::value(artifactLocation); |
143 | | - picojson::object region; |
144 | | - region["startLine"] = picojson::value(static_cast<int64_t>(location.line < 1 ? 1 : location.line)); |
145 | | - region["startColumn"] = picojson::value(static_cast<int64_t>(location.column < 1 ? 1 : location.column)); |
146 | | - region["endLine"] = region["startLine"]; |
147 | | - region["endColumn"] = region["startColumn"]; |
148 | | - physicalLocation["region"] = picojson::value(region); |
149 | | - picojson::object loc; |
150 | | - loc["physicalLocation"] = picojson::value(physicalLocation); |
151 | | - ret.emplace_back(loc); |
152 | | - } |
153 | | - return ret; |
154 | | - } |
155 | | - |
156 | | - picojson::array serializeResults() const { |
157 | | - picojson::array results; |
158 | | - for (const auto& finding : mFindings) { |
159 | | - // github only supports findings with locations |
160 | | - if (finding.callStack.empty()) |
161 | | - continue; |
162 | | - picojson::object res; |
163 | | - res["level"] = picojson::value(sarifSeverity(finding)); |
164 | | - res["locations"] = picojson::value(serializeLocations(finding)); |
165 | | - picojson::object message; |
166 | | - message["text"] = picojson::value(finding.shortMessage()); |
167 | | - res["message"] = picojson::value(message); |
168 | | - res["ruleId"] = picojson::value(finding.id); |
169 | | - results.emplace_back(res); |
170 | | - } |
171 | | - return results; |
172 | | - } |
173 | | - |
174 | | - picojson::value serializeRuns(const std::string& productName, const std::string& version) const { |
175 | | - picojson::object driver; |
176 | | - driver["name"] = picojson::value(productName); |
177 | | - driver["semanticVersion"] = picojson::value(version); |
178 | | - driver["informationUri"] = picojson::value("https://cppcheck.sourceforge.io"); |
179 | | - driver["rules"] = picojson::value(serializeRules()); |
180 | | - picojson::object tool; |
181 | | - tool["driver"] = picojson::value(driver); |
182 | | - picojson::object run; |
183 | | - run["tool"] = picojson::value(tool); |
184 | | - run["results"] = picojson::value(serializeResults()); |
185 | | - picojson::array runs{picojson::value(run)}; |
186 | | - return picojson::value(runs); |
187 | | - } |
188 | | - |
189 | | - std::string serialize(std::string productName) const { |
190 | | - const auto nameAndVersion = Settings::getNameAndVersion(productName); |
191 | | - productName = nameAndVersion.first.empty() ? "Cppcheck" : nameAndVersion.first; |
192 | | - std::string version = nameAndVersion.first.empty() ? CppCheck::version() : nameAndVersion.second; |
193 | | - if (version.find(' ') != std::string::npos) |
194 | | - version.erase(version.find(' '), std::string::npos); |
195 | | - |
196 | | - picojson::object doc; |
197 | | - doc["version"] = picojson::value("2.1.0"); |
198 | | - doc["$schema"] = picojson::value("https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json"); |
199 | | - doc["runs"] = serializeRuns(productName, version); |
200 | | - |
201 | | - return picojson::value(doc).serialize(true); |
202 | | - } |
203 | | - private: |
204 | | - |
205 | | - static std::string sarifSeverity(const ErrorMessage& errmsg) { |
206 | | - if (ErrorLogger::isCriticalErrorId(errmsg.id)) |
207 | | - return "error"; |
208 | | - switch (errmsg.severity) { |
209 | | - case Severity::error: |
210 | | - case Severity::warning: |
211 | | - case Severity::style: |
212 | | - case Severity::portability: |
213 | | - case Severity::performance: |
214 | | - return "warning"; |
215 | | - case Severity::information: |
216 | | - case Severity::internal: |
217 | | - case Severity::debug: |
218 | | - case Severity::none: |
219 | | - return "note"; |
220 | | - } |
221 | | - return "note"; |
222 | | - } |
223 | | - |
224 | | - static std::string sarifPrecision(const ErrorMessage& errmsg) { |
225 | | - if (errmsg.certainty == Certainty::inconclusive) |
226 | | - return "medium"; |
227 | | - return "high"; |
228 | | - } |
229 | | - |
230 | | - std::vector<ErrorMessage> mFindings; |
231 | | - }; |
232 | | - |
233 | 84 | class CmdLineLoggerStd : public CmdLineLogger |
234 | 85 | { |
235 | 86 | public: |
@@ -717,18 +568,20 @@ void StdLogger::reportErr(const ErrorMessage &msg) |
717 | 568 | msgCopy.classification = getClassification(msgCopy.guideline, mSettings.reportType); |
718 | 569 |
|
719 | 570 | // TODO: there should be no need for verbose and default messages here |
720 | | - const std::string msgStr = msgCopy.toString(mSettings.verbose, mSettings.templateFormat, mSettings.templateLocation); |
| 571 | + const std::string msgStr = |
| 572 | + msgCopy.toString(mSettings.verbose, mSettings.templateFormat, mSettings.templateLocation); |
721 | 573 |
|
722 | 574 | // Alert only about unique errors |
723 | 575 | if (!mSettings.emitDuplicates && !mShownErrors.insert(msgStr).second) |
724 | 576 | return; |
725 | 577 |
|
726 | | - if (mSettings.outputFormat == Settings::OutputFormat::sarif) |
| 578 | + if (mSettings.outputFormat == Settings::OutputFormat::sarif) { |
727 | 579 | mSarifReport.addFinding(std::move(msgCopy)); |
728 | | - else if (mSettings.outputFormat == Settings::OutputFormat::xml) |
| 580 | + } else if (mSettings.outputFormat == Settings::OutputFormat::xml) { |
729 | 581 | reportErr(msgCopy.toXML()); |
730 | | - else |
| 582 | + } else { |
731 | 583 | reportErr(msgStr); |
| 584 | + } |
732 | 585 | } |
733 | 586 |
|
734 | 587 | /** |
|
0 commit comments