Skip to content

Commit 4d4f174

Browse files
committed
group errors by their place
1 parent ebfa96e commit 4d4f174

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

server/src/sarif/FileSarif.cpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,8 @@ namespace {
102102
}
103103

104104
void FileSarif::addCodeFlowWithoutExternal(json &result, const fs::path &projectRoot) {
105-
json resultWithAbs = result;
106-
json &locationsWithAbs = resultWithAbs.at("codeFlows").at(0).at("threadFlows").at(0);
107-
deleteExternalFilesFromResult(locationsWithAbs,
108-
projectRoot, true);
109-
result = resultWithAbs;
110-
json &locations = result.at("codeFlows").at(0).at("threadFlows").at(0);
111-
deleteExternalFilesFromResult(locations,
105+
deleteExternalFilesFromResult(result.at("codeFlows").at(0).at("threadFlows").at(0),
112106
projectRoot, false);
113-
if (locationsWithAbs.at("locations").size() != locations.at("locations").size()) {
114-
result.at("codeFlows").push_back(resultWithAbs.at("codeFlows").at(0));
115-
}
116107
}
117108

118109
void FileSarif::deleteExternalFilesFromResult(json &result, const fs::path &projectRoot, bool leaveFromLib) {
@@ -125,24 +116,33 @@ namespace {
125116
}
126117
auto it = std::remove_if(result.at("locations").begin(), result.at("locations").end(),
127118
[&](json &location) {
128-
if (leaveFromLib) {
129-
auto opt = isRelevant(
130-
(std::string) getUriFromLocation(location.at("location")));
131-
if (!opt.has_value()) {
132-
return true;
133-
}
134-
getUriFromLocation(location.at("location")) = opt.value().string();
135-
return false;
136-
} else {
137-
return StringUtils::startsWith(
138-
(std::string) getUriFromLocation(location.at("location")), "/");
139-
}
119+
return !fs::exists(projectRoot / (std::string)getUriFromLocation(location.at("location")));
140120
});
141121
result.at("locations").erase(it, result.at("locations").end());
142122
}
143123

144-
void FileSarif::addResultToSarif(const json &result) {
145-
sarifJson.at("runs").at(0).at("results").push_back(result);
124+
namespace {
125+
[[nodiscard]] int getErrorLineFromResult(const json& result) {
126+
return result.at("locations").at(0).at("physicalLocation")
127+
.at("region").at("startLine");
128+
}
129+
130+
[[nodiscard]] std::string getErrorFileFromResult(const json& result) {
131+
return result.at("locations").at(0).at("physicalLocation")
132+
.at("artifactLocation").at("uri");
133+
}
134+
}
135+
136+
void FileSarif::addResultToSarif(const json &newResult) {
137+
json &all = sarifJson.at("runs").at(0).at("results");
138+
for (json &result : all) {
139+
if (getErrorFileFromResult(result) == getErrorFileFromResult(newResult) &&
140+
getErrorLineFromResult(result) == getErrorLineFromResult(newResult)) {
141+
result.at("codeFlows").push_back(newResult.at("codeFlows").at(0));
142+
return;
143+
}
144+
}
145+
sarifJson.at("runs").at(0).at("results").push_back(newResult);
146146
}
147147

148148
}

0 commit comments

Comments
 (0)