Coverage Report

Created: 2026-07-14 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmSarif.cxx
Line
Count
Source
1
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2
   file LICENSE.rst or https://cmake.org/licensing for details.  */
3
#include "cmSarif.h"
4
5
#include <memory>
6
7
#include <cm3p/json/value.h>
8
#include <cm3p/json/writer.h>
9
10
#include "cmsys/FStream.hxx"
11
12
namespace cmSarif {
13
14
constexpr char const* SpecVersion = "2.1.0";
15
constexpr char const* SpecSchema =
16
  "https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/"
17
  "sarif-schema-2.1.0.json";
18
19
Json::Value GetJson(ResultSeverityLevel level)
20
0
{
21
0
  switch (level) {
22
0
    case ResultSeverityLevel::Warning:
23
0
      return "warning";
24
0
    case ResultSeverityLevel::Error:
25
0
      return "error";
26
0
    case ResultSeverityLevel::Note:
27
0
      return "note";
28
0
    case ResultSeverityLevel::None:
29
0
    default:
30
0
      return "none";
31
0
  }
32
0
}
33
34
Json::Value GetJson(ArtifactLocation const& artifactLocation)
35
0
{
36
0
  Json::Value obj(Json::objectValue);
37
0
  obj["uri"] = artifactLocation.Uri;
38
0
  if (!artifactLocation.UriBaseId.empty()) {
39
0
    obj["uriBaseId"] = artifactLocation.UriBaseId;
40
0
  }
41
0
  return obj;
42
0
}
43
44
Json::Value GetJson(Region region)
45
0
{
46
0
  Json::Value obj(Json::objectValue);
47
0
  obj["startLine"] = Json::Int64(region.StartLine);
48
0
  return obj;
49
0
}
50
51
Json::Value GetJson(PhysicalLocation const& physicalLocation)
52
0
{
53
0
  Json::Value obj(Json::objectValue);
54
0
  obj["artifactLocation"] = cmSarif::GetJson(physicalLocation.Artifact);
55
0
  if (physicalLocation.ArtifactRegion) {
56
0
    obj["region"] = cmSarif::GetJson(*physicalLocation.ArtifactRegion);
57
0
  }
58
0
  return obj;
59
0
}
60
61
Json::Value GetJson(Location const& location)
62
0
{
63
0
  Json::Value obj(Json::objectValue);
64
0
  obj["physicalLocation"] = cmSarif::GetJson(location.Physical);
65
0
  return obj;
66
0
}
67
68
Json::Value GetJson(ReportingDescriptor const& reportingDescriptor)
69
0
{
70
0
  Json::Value rd(Json::objectValue);
71
0
  rd["id"] = reportingDescriptor.Id;
72
0
  if (reportingDescriptor.Name) {
73
0
    rd["name"] = *reportingDescriptor.Name;
74
0
  }
75
0
  return rd;
76
0
}
77
78
Json::Value GetJson(Result const& result)
79
0
{
80
0
  Json::Value resultJson(Json::objectValue);
81
82
0
  if (result.Message) {
83
0
    resultJson["message"]["text"] = *result.Message;
84
0
  }
85
86
0
  if (result.Level) {
87
0
    resultJson["level"] = cmSarif::GetJson(*result.Level);
88
0
  }
89
90
0
  if (result.RuleId) {
91
0
    resultJson["ruleId"] = *result.RuleId;
92
0
  }
93
0
  if (result.RuleIndex) {
94
0
    resultJson["ruleIndex"] = Json::UInt64(*result.RuleIndex);
95
0
  }
96
97
0
  if (result.Location) {
98
0
    resultJson["locations"][0] = cmSarif::GetJson(*result.Location);
99
0
  }
100
101
0
  return resultJson;
102
0
}
103
104
Json::Value GetJson(ToolComponent const& toolComponent)
105
0
{
106
0
  Json::Value component(Json::objectValue);
107
0
  component["name"] = toolComponent.Name;
108
0
  component["version"] = toolComponent.Version;
109
0
  Json::Value rules(Json::arrayValue);
110
0
  for (auto const& rule : toolComponent.Rules) {
111
0
    rules.append(cmSarif::GetJson(rule));
112
0
  }
113
0
  component["rules"] = rules;
114
0
  return component;
115
0
}
116
117
Json::Value GetJson(Tool const& tool)
118
0
{
119
0
  Json::Value toolJson(Json::objectValue);
120
0
  toolJson["driver"] = cmSarif::GetJson(tool.Driver);
121
0
  return toolJson;
122
0
}
123
124
Json::Value GetJson(Run const& run)
125
0
{
126
0
  Json::Value runJson(Json::objectValue);
127
0
  runJson["tool"] = cmSarif::GetJson(run.Tool);
128
0
  Json::Value results(Json::arrayValue);
129
0
  for (auto const& result : run.Results) {
130
0
    results.append(cmSarif::GetJson(result));
131
0
  }
132
0
  runJson["results"] = results;
133
0
  return runJson;
134
0
}
135
136
bool WriteLog(std::string const& path, cmSarif::Run const& run)
137
0
{
138
0
  cmsys::ofstream outputFile(path.c_str());
139
0
  if (!outputFile.good()) {
140
0
    return false;
141
0
  }
142
143
0
  Json::Value root(Json::objectValue);
144
0
  root["version"] = SpecVersion;
145
0
  root["$schema"] = SpecSchema;
146
0
  Json::Value runs(Json::arrayValue);
147
0
  runs.append(cmSarif::GetJson(run));
148
0
  root["runs"] = runs;
149
150
0
  Json::StreamWriterBuilder builder;
151
0
  std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
152
0
  writer->write(root, &outputFile);
153
0
  outputFile.close();
154
155
0
  return true;
156
0
}
157
158
} // namespace cmSarif