@@ -140,7 +140,7 @@ void SimpleTreeEncoder::writeTo(std::ostream &str)
140
140
// validation: https://sarifweb.azurewebsites.net/Validation
141
141
class SarifTreeEncoder : public AbstractTreeEncoder {
142
142
public:
143
- SarifTreeEncoder ();
143
+ SarifTreeEncoder () = default ;
144
144
145
145
// / import supported scan properties
146
146
void importScanProps (const TScanProps &) override ;
@@ -152,6 +152,7 @@ class SarifTreeEncoder: public AbstractTreeEncoder {
152
152
void writeTo (std::ostream &) override ;
153
153
154
154
private:
155
+ void initToolVersion ();
155
156
void serializeCweMap ();
156
157
157
158
typedef std::map<std::string, int > TCweMap;
@@ -161,13 +162,57 @@ class SarifTreeEncoder: public AbstractTreeEncoder {
161
162
PTree results_;
162
163
};
163
164
164
- SarifTreeEncoder::SarifTreeEncoder ()
165
+ void SarifTreeEncoder::initToolVersion ()
165
166
{
166
- // mandatory: tool/driver
167
- driver_.put <std::string>(" name" , " csdiff" );
168
- driver_.put <std::string>(" version" , CS_VERSION);
169
- driver_.put <std::string>(" informationUri" ,
170
- " https://github.com/csutils/csdiff" );
167
+ std::string tool;
168
+ auto it = scanProps_.find (" tool" );
169
+ if (scanProps_.end () != it)
170
+ // read "tool" scan property
171
+ tool = it->second ;
172
+
173
+ std::string ver;
174
+ it = scanProps_.find (" tool-version" );
175
+ if (scanProps_.end () != it) {
176
+ // read "tool-version" scan property
177
+ ver = it->second ;
178
+
179
+ if (tool.empty ()) {
180
+ // try to split the "{tool}-{version}" string by the last '-'
181
+ const size_t lastDashAt = ver.rfind (' -' );
182
+ if (std::string::npos != lastDashAt) {
183
+ // read tool from the "{tool}-{version}" string
184
+ tool = ver.substr (0 , lastDashAt);
185
+
186
+ // remove "{tool}-" from "{tool}-{version}"
187
+ ver.erase (0U , lastDashAt);
188
+ }
189
+ }
190
+ else {
191
+ // try to find "{tool}-" prefix in the "tool-version" scan property
192
+ const std::string prefix = tool + " -" ;
193
+ if (0U == ver.find (prefix))
194
+ ver.erase (0U , prefix.size ());
195
+ }
196
+ }
197
+
198
+ std::string uri;
199
+ if (tool.empty ()) {
200
+ // unable to read tool name --> fallback to csdiff as the tool
201
+ tool = " csdiff" ;
202
+ ver = CS_VERSION;
203
+ uri = " https://github.com/csutils/csdiff" ;
204
+ }
205
+ else if (scanProps_.end () != (it = scanProps_.find (" tool-url" )))
206
+ // read "tool-url" scan property
207
+ uri = it->second ;
208
+
209
+ driver_.put <std::string>(" name" , tool);
210
+
211
+ if (!ver.empty ())
212
+ driver_.put <std::string>(" version" , ver);
213
+
214
+ if (!uri.empty ())
215
+ driver_.put <std::string>(" informationUri" , uri);
171
216
}
172
217
173
218
void SarifTreeEncoder::serializeCweMap ()
@@ -376,6 +421,8 @@ void SarifTreeEncoder::writeTo(std::ostream &str)
376
421
root.put_child (" inlineExternalProperties" , propsList);
377
422
}
378
423
424
+ this ->initToolVersion ();
425
+
379
426
if (!cweMap_.empty ())
380
427
// needs to run before we pick driver_
381
428
this ->serializeCweMap ();
0 commit comments