Skip to content

Commit 2a4f2c2

Browse files
committed
parser-json-gcc: separate module for GccTreeDecoder
1 parent 158890f commit 2a4f2c2

File tree

4 files changed

+145
-82
lines changed

4 files changed

+145
-82
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ add_library(cs STATIC
7777
parser-gcc.cc
7878
parser-json.cc
7979
parser-json-cov.cc
80+
parser-json-gcc.cc
8081
parser-json-sarif.cc
8182
parser-json-simple.cc
8283
parser-xml.cc

src/parser-json-gcc.cc

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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-gcc.hh"
21+
22+
#include "parser-gcc.hh" // for GccPostProcessor
23+
24+
struct GccTreeDecoder::Private {
25+
GccPostProcessor postProc;
26+
};
27+
28+
GccTreeDecoder::GccTreeDecoder():
29+
d(new Private)
30+
{
31+
}
32+
33+
GccTreeDecoder::~GccTreeDecoder() = default;
34+
35+
static bool gccReadEvent(DefEvent *pEvt, const pt::ptree &evtNode)
36+
{
37+
using std::string;
38+
39+
// read kind (error, warning, note)
40+
string &evtName = pEvt->event;
41+
evtName = valueOf<string>(evtNode, "kind", "");
42+
if (evtName.empty())
43+
return false;
44+
45+
// read location
46+
pEvt->fileName = "<unknown>";
47+
const pt::ptree *locs;
48+
if (findChildOf(&locs, evtNode, "locations") && !locs->empty()) {
49+
const pt::ptree *caret;
50+
if (findChildOf(&caret, locs->begin()->second, "caret")) {
51+
pEvt->fileName = valueOf<string>(*caret, "file", "<unknown>");
52+
pEvt->line = valueOf<int> (*caret, "line", 0);
53+
pEvt->column = valueOf<int> (*caret, "byte-column", 0);
54+
}
55+
}
56+
57+
// read message
58+
pEvt->msg = valueOf<string>(evtNode, "message", "<unknown>");
59+
60+
// read -W... if available
61+
const string option = valueOf<string>(evtNode, "option", "");
62+
if (!option.empty())
63+
pEvt->msg += " [" + option + "]";
64+
65+
return true;
66+
}
67+
68+
bool GccTreeDecoder::readNode(Defect *def)
69+
{
70+
// move the iterator after we get the current position
71+
const pt::ptree *pNode = this->nextNode();
72+
if (!pNode)
73+
// failed initialization or EOF
74+
return false;
75+
76+
const pt::ptree &defNode = *pNode;
77+
78+
*def = Defect("COMPILER_WARNING");
79+
80+
// read key event
81+
def->events.push_back(DefEvent());
82+
if (!gccReadEvent(&def->events.back(), defNode))
83+
return false;
84+
85+
// read other events if available
86+
const pt::ptree *children;
87+
if (findChildOf(&children, defNode, "children")) {
88+
for (const auto &item : *children) {
89+
const pt::ptree &evtNode = item.second;
90+
91+
DefEvent evt;
92+
if (gccReadEvent(&evt, evtNode))
93+
def->events.emplace_back(evt);
94+
}
95+
}
96+
97+
// read CWE ID if available
98+
const pt::ptree *meta;
99+
if (findChildOf(&meta, defNode, "metadata"))
100+
def->cwe = valueOf<int>(*meta, "cwe", 0);
101+
102+
// apply post-processing rules
103+
d->postProc.apply(def);
104+
105+
return true;
106+
}

src/parser-json-gcc.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_GCC_H
21+
#define H_GUARD_PARSER_JSON_GCC_H
22+
23+
#include "abstract-tree.hh"
24+
25+
/// tree decoder of the JSON format produced by GCC
26+
class GccTreeDecoder: public AbstractTreeDecoder {
27+
public:
28+
GccTreeDecoder();
29+
~GccTreeDecoder() 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_GCC_H */

src/parser-json.cc

Lines changed: 1 addition & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,12 @@
2222
#include "abstract-tree.hh"
2323
#include "parser-gcc.hh" // for GccPostProcessor
2424
#include "parser-json-cov.hh"
25+
#include "parser-json-gcc.hh"
2526
#include "parser-json-sarif.hh"
2627
#include "parser-json-simple.hh"
2728

2829
#include <boost/property_tree/json_parser.hpp>
2930

30-
/// tree decoder of the JSON format produced by GCC
31-
class GccTreeDecoder: public AbstractTreeDecoder {
32-
public:
33-
bool readNode(Defect *def) override;
34-
35-
private:
36-
const GccPostProcessor postProc;
37-
};
38-
3931
/// tree decoder of the JSON format produced by ShellCheck
4032
class ShellCheckTreeDecoder: public AbstractTreeDecoder {
4133
public:
@@ -154,79 +146,6 @@ bool JsonParser::getNext(Defect *def)
154146
}
155147
}
156148

157-
static bool gccReadEvent(DefEvent *pEvt, const pt::ptree &evtNode)
158-
{
159-
using std::string;
160-
161-
// read kind (error, warning, note)
162-
string &evtName = pEvt->event;
163-
evtName = valueOf<string>(evtNode, "kind", "");
164-
if (evtName.empty())
165-
return false;
166-
167-
// read location
168-
pEvt->fileName = "<unknown>";
169-
const pt::ptree *locs;
170-
if (findChildOf(&locs, evtNode, "locations") && !locs->empty()) {
171-
const pt::ptree *caret;
172-
if (findChildOf(&caret, locs->begin()->second, "caret")) {
173-
pEvt->fileName = valueOf<string>(*caret, "file", "<unknown>");
174-
pEvt->line = valueOf<int> (*caret, "line", 0);
175-
pEvt->column = valueOf<int> (*caret, "byte-column", 0);
176-
}
177-
}
178-
179-
// read message
180-
pEvt->msg = valueOf<string>(evtNode, "message", "<unknown>");
181-
182-
// read -W... if available
183-
const string option = valueOf<string>(evtNode, "option", "");
184-
if (!option.empty())
185-
pEvt->msg += " [" + option + "]";
186-
187-
return true;
188-
}
189-
190-
bool GccTreeDecoder::readNode(Defect *def)
191-
{
192-
// move the iterator after we get the current position
193-
const pt::ptree *pNode = this->nextNode();
194-
if (!pNode)
195-
// failed initialization or EOF
196-
return false;
197-
198-
const pt::ptree &defNode = *pNode;
199-
200-
*def = Defect("COMPILER_WARNING");
201-
202-
// read key event
203-
def->events.push_back(DefEvent());
204-
if (!gccReadEvent(&def->events.back(), defNode))
205-
return false;
206-
207-
// read other events if available
208-
const pt::ptree *children;
209-
if (findChildOf(&children, defNode, "children")) {
210-
for (const auto &item : *children) {
211-
const pt::ptree &evtNode = item.second;
212-
213-
DefEvent evt;
214-
if (gccReadEvent(&evt, evtNode))
215-
def->events.emplace_back(evt);
216-
}
217-
}
218-
219-
// read CWE ID if available
220-
const pt::ptree *meta;
221-
if (findChildOf(&meta, defNode, "metadata"))
222-
def->cwe = valueOf<int>(*meta, "cwe", 0);
223-
224-
// apply post-processing rules
225-
this->postProc.apply(def);
226-
227-
return true;
228-
}
229-
230149
static bool scReadEvent(DefEvent *pEvt, const pt::ptree &evtNode)
231150
{
232151
using std::string;

0 commit comments

Comments
 (0)