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