Skip to content

Commit 81638f0

Browse files
committed
writer-json-simple: separate module for SimpleTreeEncoder
Related: #115
1 parent c436c71 commit 81638f0

File tree

4 files changed

+139
-86
lines changed

4 files changed

+139
-86
lines changed

Diff for: src/lib/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ add_library(cs STATIC
4545
writer-html.cc
4646
writer-json.cc
4747
writer-json-common.cc
48+
writer-json-simple.cc
4849
)

Diff for: src/lib/writer-json-simple.cc

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright (C) 2011 - 2023 Red Hat, Inc.
3+
*
4+
* This file is part of csdiff.
5+
*
6+
* csdiff is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* any later version.
10+
*
11+
* csdiff is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with csdiff. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#include "writer-json-simple.hh"
21+
22+
#include "writer-json-common.hh"
23+
24+
using namespace boost::json;
25+
26+
void SimpleTreeEncoder::importScanProps(const TScanProps &scanProps)
27+
{
28+
if (scanProps.empty())
29+
return;
30+
31+
root_["scan"] = jsonSerializeScanProps(scanProps);
32+
}
33+
34+
void SimpleTreeEncoder::appendDef(const Defect &def)
35+
{
36+
// go through events
37+
array evtList;
38+
for (const DefEvent &evt : def.events) {
39+
object evtNode;
40+
41+
// describe the location
42+
evtNode["file_name"] = evt.fileName;
43+
evtNode["line"] = evt.line;
44+
if (0 < evt.column)
45+
evtNode["column"] = evt.column;
46+
47+
// describe the event
48+
evtNode["event"] = evt.event;
49+
evtNode["message"] = sanitizeUTF8(evt.msg);
50+
evtNode["verbosity_level"] = evt.verbosityLevel;
51+
52+
// append the event to the list
53+
evtList.push_back(std::move(evtNode));
54+
}
55+
56+
// create a node for a single defect
57+
object defNode;
58+
defNode["checker"] = def.checker;
59+
if (!def.annotation.empty())
60+
defNode["annotation"] = def.annotation;
61+
62+
// write "defect_id", "cwe", etc. if available
63+
if (0 < def.defectId)
64+
defNode["defect_id"] = def.defectId;
65+
if (0 < def.cwe)
66+
defNode["cwe"] = def.cwe;
67+
if (0 < def.imp)
68+
defNode["imp"] = def.imp;
69+
if (!def.function.empty())
70+
defNode["function"] = def.function;
71+
if (!def.language.empty())
72+
defNode["language"] = def.language;
73+
if (!def.tool.empty())
74+
defNode["tool"] = def.tool;
75+
76+
defNode["key_event_idx"] = def.keyEventIdx;
77+
defNode["events"] = std::move(evtList);
78+
79+
// create the node representing the list of defects
80+
if (!pDefects_)
81+
pDefects_ = &root_["defects"].emplace_array();
82+
83+
// append the node to the list
84+
pDefects_->push_back(std::move(defNode));
85+
}
86+
87+
void SimpleTreeEncoder::writeTo(std::ostream &str)
88+
{
89+
if (!pDefects_)
90+
// create an empty "defects" node to keep format detection working
91+
pDefects_ = &root_["defects"].emplace_array();
92+
93+
jsonPrettyPrint(str, root_);
94+
}

Diff for: src/lib/writer-json-simple.hh

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (C) 2011 - 2023 Red Hat, Inc.
3+
*
4+
* This file is part of csdiff.
5+
*
6+
* csdiff is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* any later version.
10+
*
11+
* csdiff is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with csdiff. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifndef H_GUARD_WRITER_JSON_SIMPLE_H
21+
#define H_GUARD_WRITER_JSON_SIMPLE_H
22+
23+
#include "abstract-tree.hh"
24+
25+
#include <boost/json.hpp>
26+
27+
class SimpleTreeEncoder: public AbstractTreeEncoder {
28+
public:
29+
/// import supported scan properties
30+
void importScanProps(const TScanProps &) override;
31+
32+
/// append single defect
33+
void appendDef(const Defect &) override;
34+
35+
/// write everything to the given output stream
36+
void writeTo(std::ostream &) override;
37+
38+
private:
39+
boost::json::object root_;
40+
boost::json::array *pDefects_ = nullptr;
41+
};
42+
43+
#endif /* H_GUARD_WRITER_JSON_SIMPLE_H */

Diff for: src/lib/writer-json.cc

+1-86
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "regex.hh"
2424
#include "version.hh"
2525
#include "writer-json-common.hh"
26+
#include "writer-json-simple.hh"
2627

2728
#include <algorithm>
2829
#include <queue>
@@ -31,92 +32,6 @@
3132

3233
using namespace boost::json;
3334

34-
class SimpleTreeEncoder: public AbstractTreeEncoder {
35-
public:
36-
/// import supported scan properties
37-
void importScanProps(const TScanProps &) override;
38-
39-
/// append single defect
40-
void appendDef(const Defect &) override;
41-
42-
/// write everything to the given output stream
43-
void writeTo(std::ostream &) override;
44-
45-
private:
46-
object root_;
47-
array *pDefects_ = nullptr;
48-
};
49-
50-
void SimpleTreeEncoder::importScanProps(const TScanProps &scanProps)
51-
{
52-
if (scanProps.empty())
53-
return;
54-
55-
root_["scan"] = jsonSerializeScanProps(scanProps);
56-
}
57-
58-
void SimpleTreeEncoder::appendDef(const Defect &def)
59-
{
60-
// go through events
61-
array evtList;
62-
for (const DefEvent &evt : def.events) {
63-
object evtNode;
64-
65-
// describe the location
66-
evtNode["file_name"] = evt.fileName;
67-
evtNode["line"] = evt.line;
68-
if (0 < evt.column)
69-
evtNode["column"] = evt.column;
70-
71-
// describe the event
72-
evtNode["event"] = evt.event;
73-
evtNode["message"] = sanitizeUTF8(evt.msg);
74-
evtNode["verbosity_level"] = evt.verbosityLevel;
75-
76-
// append the event to the list
77-
evtList.push_back(std::move(evtNode));
78-
}
79-
80-
// create a node for a single defect
81-
object defNode;
82-
defNode["checker"] = def.checker;
83-
if (!def.annotation.empty())
84-
defNode["annotation"] = def.annotation;
85-
86-
// write "defect_id", "cwe", etc. if available
87-
if (0 < def.defectId)
88-
defNode["defect_id"] = def.defectId;
89-
if (0 < def.cwe)
90-
defNode["cwe"] = def.cwe;
91-
if (0 < def.imp)
92-
defNode["imp"] = def.imp;
93-
if (!def.function.empty())
94-
defNode["function"] = def.function;
95-
if (!def.language.empty())
96-
defNode["language"] = def.language;
97-
if (!def.tool.empty())
98-
defNode["tool"] = def.tool;
99-
100-
defNode["key_event_idx"] = def.keyEventIdx;
101-
defNode["events"] = std::move(evtList);
102-
103-
// create the node representing the list of defects
104-
if (!pDefects_)
105-
pDefects_ = &root_["defects"].emplace_array();
106-
107-
// append the node to the list
108-
pDefects_->push_back(std::move(defNode));
109-
}
110-
111-
void SimpleTreeEncoder::writeTo(std::ostream &str)
112-
{
113-
if (!pDefects_)
114-
// create an empty "defects" node to keep format detection working
115-
pDefects_ = &root_["defects"].emplace_array();
116-
117-
jsonPrettyPrint(str, root_);
118-
}
119-
12035
// SARIF 2.1.0 is documented at:
12136
// https://docs.github.com/en/code-security/code-scanning/integrating-with-code-scanning/sarif-support-for-code-scanning
12237
// specification: https://docs.oasis-open.org/sarif/sarif/v2.1.0/os/sarif-v2.1.0-os.html

0 commit comments

Comments
 (0)