forked from csutils/csdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwriter-json-simple.cc
More file actions
118 lines (98 loc) · 3.43 KB
/
writer-json-simple.cc
File metadata and controls
118 lines (98 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Copyright (C) 2011 - 2023 Red Hat, Inc.
*
* This file is part of csdiff.
*
* csdiff is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* csdiff is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with csdiff. If not, see <http://www.gnu.org/licenses/>.
*/
#include "writer-json-simple.hh"
#include "finger-print.hh"
#include "writer-json-common.hh"
using namespace boost::json;
void SimpleTreeEncoder::importScanProps(const TScanProps &scanProps)
{
if (scanProps.empty())
return;
root_["scan"] = jsonSerializeScanProps(scanProps);
}
static array simpleEncodeEvents(const TEvtList &events)
{
array evtList;
// go through events
for (const DefEvent &evt : events) {
object evtNode;
// describe the location from the source code
evtNode["file_name"] = evt.fileName;
evtNode["line"] = evt.line;
if (0 < evt.column)
evtNode["column"] = evt.column;
if (0 < evt.hSize)
evtNode["h_size"] = evt.hSize;
if (0 < evt.vSize)
evtNode["v_size"] = evt.vSize;
// describe the location from the compilation error log/input
if (!evt.inputFile.empty())
evtNode["input_file"] = evt.inputFile;
if (0 < evt.inputLine)
evtNode["input_line"] = evt.inputLine;
// describe the event
evtNode["event"] = evt.event;
evtNode["message"] = sanitizeUTF8(evt.msg);
evtNode["verbosity_level"] = evt.verbosityLevel;
// append the event to the list
evtList.push_back(std::move(evtNode));
}
return evtList;
}
void SimpleTreeEncoder::appendDef(const Defect &def)
{
// create a node for a single defect
object defNode;
defNode["checker"] = def.checker;
// encode optional per-finding properties
if (!def.annotation.empty())
defNode["annotation"] = def.annotation;
if (0 < def.defectId)
defNode["defect_id"] = def.defectId;
if (0 < def.cwe)
defNode["cwe"] = def.cwe;
if (0 < def.imp)
defNode["imp"] = def.imp;
if (!def.function.empty())
defNode["function"] = def.function;
if (!def.language.empty())
defNode["language"] = def.language;
if (!def.tool.empty())
defNode["tool"] = def.tool;
// encode fingerprint if available
const FingerPrinter fp(def);
const std::string hash = fp.getHash(FPV_CSDIFF_WITH_LINE_CONTENT);
if (!hash.empty())
defNode["hash_v1"] = hash;
// encode events
defNode["key_event_idx"] = def.keyEventIdx;
defNode["events"] = simpleEncodeEvents(def.events);
// create the node representing the list of defects
if (!pDefects_)
pDefects_ = &root_["defects"].emplace_array();
// append the node to the list
pDefects_->push_back(std::move(defNode));
}
void SimpleTreeEncoder::writeTo(std::ostream &str)
{
if (!pDefects_)
// create an empty "defects" node to keep format detection working
pDefects_ = &root_["defects"].emplace_array();
jsonPrettyPrint(str, root_);
}