Skip to content

Commit f98d916

Browse files
committed
parser-json-cov: separate module for CovTreeDecoder
1 parent c2ef39c commit f98d916

File tree

4 files changed

+125
-62
lines changed

4 files changed

+125
-62
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ add_library(cs STATIC
7676
parser-cov.cc
7777
parser-gcc.cc
7878
parser-json.cc
79+
parser-json-cov.cc
7980
parser-json-simple.cc
8081
parser-xml.cc
8182
shared-string.cc

src/parser-json-cov.cc

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (C) 2012-2022 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 "parser-json-cov.hh"
21+
22+
#include "parser-cov.hh" // for KeyEventDigger
23+
24+
struct CovTreeDecoder::Private {
25+
KeyEventDigger keDigger;
26+
};
27+
28+
CovTreeDecoder::CovTreeDecoder():
29+
d(new Private)
30+
{
31+
}
32+
33+
CovTreeDecoder::~CovTreeDecoder() = default;
34+
35+
bool CovTreeDecoder::readNode(Defect *def)
36+
{
37+
// move the iterator after we get the current position
38+
const pt::ptree *pNode = this->nextNode();
39+
if (!pNode)
40+
// failed initialization or EOF
41+
return false;
42+
43+
const pt::ptree &defNode = *pNode;
44+
45+
// read per-defect properties
46+
def->checker = defNode.get<std::string>("checkerName");
47+
def->function = valueOf<std::string>(defNode, "functionDisplayName", "");
48+
def->language = valueOf<std::string>(defNode, "code-language", "");
49+
50+
// out of the supported tools, only Coverity produces this data format
51+
def->tool = "coverity";
52+
53+
// read CWE if available
54+
const pt::ptree *checkerProps;
55+
if (findChildOf(&checkerProps, defNode, "checkerProperties"))
56+
def->cwe = valueOf<int>(*checkerProps, "cweCategory", 0);
57+
58+
// count the events and allocate dst array
59+
const pt::ptree &evtList = defNode.get_child("events");
60+
def->events.resize(evtList.size());
61+
62+
// decode events one by one
63+
unsigned idx = 0;
64+
pt::ptree::const_iterator it;
65+
for (it = evtList.begin(); it != evtList.end(); ++it, ++idx) {
66+
const pt::ptree &evtNode = it->second;
67+
DefEvent &evt = def->events[idx];
68+
69+
evt.fileName = valueOf<std::string>(evtNode, "filePathname" , "");
70+
evt.line = valueOf<int> (evtNode, "lineNumber" , 0 );
71+
// TODO: read column?
72+
evt.event = valueOf<std::string>(evtNode, "eventTag" , "");
73+
evt.msg = valueOf<std::string>(evtNode, "eventDescription", "");
74+
75+
if (evtNode.get<bool>("main"))
76+
// this is a key event
77+
// TODO: detect and report re-definitions of key events
78+
def->keyEventIdx = idx;
79+
}
80+
81+
// initialize verbosity level of all events
82+
d->keDigger.initVerbosity(def);
83+
84+
return true;
85+
}
86+

src/parser-json-cov.hh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (C) 2012-2022 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_PARSER_JSON_COV_H
21+
#define H_GUARD_PARSER_JSON_COV_H
22+
23+
#include "abstract-tree.hh"
24+
25+
/// tree decoder of the Coverity JSON format
26+
class CovTreeDecoder: public AbstractTreeDecoder {
27+
public:
28+
CovTreeDecoder();
29+
~CovTreeDecoder() override;
30+
bool readNode(Defect *def) override;
31+
32+
private:
33+
struct Private;
34+
std::unique_ptr<Private> d;
35+
};
36+
37+
#endif /* H_GUARD_PARSER_JSON_COV_H */

src/parser-json.cc

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,14 @@
2121

2222
#include "abstract-tree.hh"
2323
#include "parser-common.hh"
24-
#include "parser-cov.hh" // for KeyEventDigger
2524
#include "parser-gcc.hh" // for GccPostProcessor
25+
#include "parser-json-cov.hh"
2626
#include "parser-json-simple.hh"
2727
#include "regex.hh"
2828

2929
#include <boost/algorithm/string/predicate.hpp>
3030
#include <boost/property_tree/json_parser.hpp>
3131

32-
/// tree decoder of the Coverity JSON format
33-
class CovTreeDecoder: public AbstractTreeDecoder {
34-
public:
35-
bool readNode(Defect *def) override;
36-
37-
private:
38-
KeyEventDigger keDigger;
39-
};
40-
4132
/// tree decoder of the SARIF format
4233
class SarifTreeDecoder: public AbstractTreeDecoder {
4334
public:
@@ -192,58 +183,6 @@ bool JsonParser::getNext(Defect *def)
192183
}
193184
}
194185

195-
bool CovTreeDecoder::readNode(Defect *def)
196-
{
197-
// move the iterator after we get the current position
198-
const pt::ptree *pNode = this->nextNode();
199-
if (!pNode)
200-
// failed initialization or EOF
201-
return false;
202-
203-
const pt::ptree &defNode = *pNode;
204-
205-
// read per-defect properties
206-
def->checker = defNode.get<std::string>("checkerName");
207-
def->function = valueOf<std::string>(defNode, "functionDisplayName", "");
208-
def->language = valueOf<std::string>(defNode, "code-language", "");
209-
210-
// out of the supported tools, only Coverity produces this data format
211-
def->tool = "coverity";
212-
213-
// read CWE if available
214-
const pt::ptree *checkerProps;
215-
if (findChildOf(&checkerProps, defNode, "checkerProperties"))
216-
def->cwe = valueOf<int>(*checkerProps, "cweCategory", 0);
217-
218-
// count the events and allocate dst array
219-
const pt::ptree &evtList = defNode.get_child("events");
220-
def->events.resize(evtList.size());
221-
222-
// decode events one by one
223-
unsigned idx = 0;
224-
pt::ptree::const_iterator it;
225-
for (it = evtList.begin(); it != evtList.end(); ++it, ++idx) {
226-
const pt::ptree &evtNode = it->second;
227-
DefEvent &evt = def->events[idx];
228-
229-
evt.fileName = valueOf<std::string>(evtNode, "filePathname" , "");
230-
evt.line = valueOf<int> (evtNode, "lineNumber" , 0 );
231-
// TODO: read column?
232-
evt.event = valueOf<std::string>(evtNode, "eventTag" , "");
233-
evt.msg = valueOf<std::string>(evtNode, "eventDescription", "");
234-
235-
if (evtNode.get<bool>("main"))
236-
// this is a key event
237-
// TODO: detect and report re-definitions of key events
238-
def->keyEventIdx = idx;
239-
}
240-
241-
// initialize verbosity level of all events
242-
this->keDigger.initVerbosity(def);
243-
244-
return true;
245-
}
246-
247186
void SarifTreeDecoder::updateCweMap(const pt::ptree *driverNode)
248187
{
249188
const pt::ptree *rules;

0 commit comments

Comments
 (0)