forked from csutils/csdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser-json-simple.cc
192 lines (159 loc) · 5.76 KB
/
parser-json-simple.cc
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* Copyright (C) 2012-2022 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 "parser-json-simple.hh"
#include "abstract-tree.hh" // for findChildOf()
#include "parser-cov.hh" // for KeyEventDigger
#include <set>
struct SimpleTreeDecoder::Private {
enum ENodeKind {
NK_DEFECT,
NK_EVENT,
NK_LAST
};
void reportUnknownNodes(ENodeKind, const pt::ptree &) const;
typedef std::set<std::string> TNodeSet;
typedef std::vector<TNodeSet> TNodeStore;
InStream &input;
TNodeStore nodeStore;
KeyEventDigger keDigger;
Private(InStream &input);
};
SimpleTreeDecoder::Private::Private(InStream &input):
input(input)
{
if (input.silent())
// skip initialization of nodeStore_ because no lookup will ever happen
return;
this->nodeStore.resize(NK_LAST);
// known per-defect subnodes
this->nodeStore[NK_DEFECT] = {
"annotation",
"checker",
"cwe",
"defect_id",
"events",
"function",
"hash_v1",
"imp",
"key_event_idx",
"language",
"tool",
};
// known per-event subnodes
this->nodeStore[NK_EVENT] = {
"column",
"event",
"file_name",
"h_size",
"line",
"message",
"v_size",
"verbosity_level",
};
}
void SimpleTreeDecoder::Private::reportUnknownNodes(
const ENodeKind nk,
const pt::ptree &node)
const
{
if (this->input.silent())
return;
const TNodeSet &nodeSet = this->nodeStore[nk];
for (const pt::ptree::value_type &item : node) {
const std::string &name = item.first;
if (nodeSet.end() == nodeSet.find(name))
std::cerr << this->input.fileName()
<< ": warning: unknown JSON node: " << name
<< std::endl;
}
}
SimpleTreeDecoder::SimpleTreeDecoder(InStream &input):
d(new Private(input))
{
}
SimpleTreeDecoder::~SimpleTreeDecoder() = default;
void SimpleTreeDecoder::readScanProps(
TScanProps *pDst,
const pt::ptree *root)
{
const pt::ptree *scanNode;
if (findChildOf(&scanNode, *root, "scan"))
for (const pt::ptree::value_type &item : *scanNode)
(*pDst)[item.first] = item.second.data();
}
bool SimpleTreeDecoder::readNode(Defect *def)
{
// move the iterator after we get the current position
const pt::ptree *pNode = this->nextNode();
if (!pNode)
// failed initialization or EOF
return false;
const pt::ptree &defNode = *pNode;
d->reportUnknownNodes(Private::NK_DEFECT, defNode);
// the checker field is mandatory
def->checker = defNode.get<std::string>("checker");
bool verbosityLevelNeedsInit = false;
// read the events
TEvtList &evtListDst = def->events;
const pt::ptree &evtListSrc = defNode.get_child("events");
for (const pt::ptree::value_type &evtItem : evtListSrc) {
using TNumDiff = DefEvent::TNumDiff;
const pt::ptree &evtNode = evtItem.second;
d->reportUnknownNodes(Private::NK_EVENT, evtNode);
DefEvent evt;
evt.fileName = valueOf<std::string >(evtNode, "file_name");
evt.line = valueOf<int >(evtNode, "line");
evt.column = valueOf<int >(evtNode, "column");
evt.hSize = valueOf<TNumDiff >(evtNode, "h_size");
evt.vSize = valueOf<TNumDiff >(evtNode, "v_size");
evt.event = valueOf<std::string >(evtNode, "event");
evt.msg = valueOf<std::string >(evtNode, "message");
evt.verbosityLevel = valueOf<int>(evtNode, "verbosity_level", -1);
if (-1 == evt.verbosityLevel)
verbosityLevelNeedsInit = true;
evtListDst.push_back(std::move(evt));
}
// read "defect_id", "cwe", and "function" if available
def->defectId = valueOf<int> (defNode, "defect_id");
def->cwe = valueOf<int> (defNode, "cwe");
def->imp = valueOf<int> (defNode, "imp");
def->function = valueOf<std::string>(defNode, "function");
def->language = valueOf<std::string>(defNode, "language");
def->tool = valueOf<std::string>(defNode, "tool");
if (defNode.not_found() == defNode.find("key_event_idx")) {
// key event not specified, try to guess it
if (!d->keDigger.guessKeyEvent(def))
throw pt::ptree_error("failed to guess key event");
}
else {
// use the provided key_event_idx unless it is out of range
const int cntEvents = evtListDst.size();
const int defKeyEvent = defNode.get<int>("key_event_idx");
if (0 <= defKeyEvent && defKeyEvent < cntEvents)
def->keyEventIdx = defKeyEvent;
else
throw pt::ptree_error("key event out of range");
}
if (verbosityLevelNeedsInit)
// missing or incomplete verbosity_level, initialize it over
d->keDigger.initVerbosity(def);
// read annotation if available
def->annotation = valueOf<std::string>(defNode, "annotation");
return true;
}