Skip to content

Commit b78155e

Browse files
committed
abstract-filter: move CtxEmbedder and EventPrunner to separate module
They use GenericAbstractFilter but do not implement it.
1 parent dd5babe commit b78155e

File tree

6 files changed

+196
-150
lines changed

6 files changed

+196
-150
lines changed

src/csgrep.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
* along with csdiff. If not, see <http://www.gnu.org/licenses/>.
1818
*/
1919

20-
#include "parser.hh"
2120
#include "abstract-filter.hh"
21+
#include "filter.hh"
2222
#include "msg-filter.hh"
23+
#include "parser.hh"
2324
#include "parser-common.hh"
2425
#include "regex.hh"
2526
#include "version.hh"

src/lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ add_library(cs STATIC
2424
cwe-mapper.cc
2525
cwe-name-lookup.cc
2626
deflookup.cc
27+
filter.cc
2728
instream.cc
2829
msg-filter.cc
2930
parser.cc

src/lib/abstract-filter.cc

Lines changed: 0 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,8 @@
1919

2020
#include "abstract-filter.hh"
2121

22-
#include "instream.hh"
23-
24-
#include <fstream>
25-
#include <iomanip>
26-
#include <sstream>
2722
#include <vector>
2823

29-
#include <boost/algorithm/string/replace.hpp>
30-
3124
// /////////////////////////////////////////////////////////////////////////////
3225
// implementation of PredicateFilter
3326

@@ -70,116 +63,3 @@ bool PredicateFilter::matchDef(const Defect &def)
7063

7164
return true;
7265
}
73-
74-
75-
// /////////////////////////////////////////////////////////////////////////////
76-
// implementation of EventPrunner
77-
78-
void EventPrunner::handleDef(const Defect &defOrig)
79-
{
80-
Defect def(defOrig);
81-
def.events.clear();
82-
83-
const unsigned cnt = defOrig.events.size();
84-
for (unsigned i = 0; i < cnt; ++i) {
85-
const DefEvent &evt = defOrig.events[i];
86-
87-
if (evt.verbosityLevel <= thr_)
88-
def.events.push_back(evt);
89-
else if (i < defOrig.keyEventIdx)
90-
def.keyEventIdx--;
91-
}
92-
93-
agent_->handleDef(def);
94-
}
95-
96-
97-
// /////////////////////////////////////////////////////////////////////////////
98-
// implementation of CtxEmbedder
99-
100-
void dropCtxLines(TEvtList *pEvtList)
101-
{
102-
static CtxEventDetector detector;
103-
104-
TEvtList dst;
105-
for (const DefEvent &evt : *pEvtList) {
106-
if (detector.isAnyCtxLine(evt))
107-
continue;
108-
109-
dst.push_back(evt);
110-
}
111-
112-
pEvtList->swap(dst);
113-
}
114-
115-
void appendCtxLines(
116-
TEvtList *pDst,
117-
std::istream &inStr,
118-
const int defLine,
119-
const int ctxLines)
120-
{
121-
if (ctxLines < 0)
122-
return;
123-
124-
const int firstLine = defLine - ctxLines;
125-
const int lastLine = defLine + ctxLines;
126-
127-
int line = 1;
128-
std::string text;
129-
for (; line <= lastLine; ++line) {
130-
if (!std::getline(inStr, text))
131-
// premature end of input
132-
break;
133-
134-
if (line < firstLine)
135-
// skip lines before the context lines
136-
continue;
137-
138-
// quote embedded NULs as they cause problems to some JSON parsers
139-
std::string nul;
140-
nul.push_back('\0');
141-
boost::algorithm::replace_all(text, nul, "[NUL]");
142-
143-
// format a single line of the comment
144-
std::ostringstream str;
145-
str << std::fixed << std::setw(5) << line;
146-
if (defLine == line)
147-
str << "|-> ";
148-
else
149-
str << "| ";
150-
str << text;
151-
152-
// append the comment as a new event
153-
DefEvent evt;
154-
evt.event = "#";
155-
evt.msg = str.str();
156-
evt.verbosityLevel = /* not a key event */ 1;
157-
pDst->push_back(evt);
158-
}
159-
}
160-
161-
void CtxEmbedder::handleDef(const Defect &defOrig)
162-
{
163-
const DefEvent &evt = defOrig.events[defOrig.keyEventIdx];
164-
if (!evt.line) {
165-
// no line number for the key event
166-
agent_->handleDef(defOrig);
167-
return;
168-
}
169-
170-
std::ifstream fstr(evt.fileName);
171-
if (!fstr) {
172-
// failed to open input file
173-
agent_->handleDef(defOrig);
174-
return;
175-
}
176-
177-
// clone defOrig and append the context lines
178-
Defect def(defOrig);
179-
dropCtxLines(&def.events);
180-
appendCtxLines(&def.events, fstr, evt.line, ctxLines_ - 1);
181-
182-
// close the file stream and forward the result
183-
fstr.close();
184-
agent_->handleDef(def);
185-
}

src/lib/abstract-filter.hh

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -58,35 +58,6 @@ class GenericAbstractFilter: public AbstractWriter {
5858
}
5959
};
6060

61-
/// decorator
62-
class EventPrunner: public GenericAbstractFilter {
63-
private:
64-
int thr_;
65-
66-
public:
67-
EventPrunner(AbstractWriter *agent, int thr):
68-
GenericAbstractFilter(agent),
69-
thr_(thr)
70-
{
71-
}
72-
73-
void handleDef(const Defect &defOrig) override;
74-
};
75-
76-
/// decorator
77-
class CtxEmbedder: public GenericAbstractFilter {
78-
private:
79-
int ctxLines_;
80-
81-
public:
82-
CtxEmbedder(AbstractWriter *agent, const int ctxLines):
83-
GenericAbstractFilter(agent),
84-
ctxLines_(ctxLines)
85-
{
86-
}
87-
88-
void handleDef(const Defect &defOrig) override;
89-
};
9061

9162
/// decorator
9263
class AbstractFilter: public GenericAbstractFilter {

src/lib/filter.cc

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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 "filter.hh"
21+
22+
#include <fstream>
23+
#include <iomanip>
24+
#include <sstream>
25+
26+
#include <boost/algorithm/string/replace.hpp>
27+
28+
// /////////////////////////////////////////////////////////////////////////////
29+
// implementation of EventPrunner
30+
31+
void EventPrunner::handleDef(const Defect &defOrig)
32+
{
33+
Defect def(defOrig);
34+
def.events.clear();
35+
36+
const unsigned cnt = defOrig.events.size();
37+
for (unsigned i = 0; i < cnt; ++i) {
38+
const DefEvent &evt = defOrig.events[i];
39+
40+
if (evt.verbosityLevel <= thr_)
41+
def.events.push_back(evt);
42+
else if (i < defOrig.keyEventIdx)
43+
def.keyEventIdx--;
44+
}
45+
46+
agent_->handleDef(def);
47+
}
48+
49+
50+
// /////////////////////////////////////////////////////////////////////////////
51+
// implementation of CtxEmbedder
52+
53+
void dropCtxLines(TEvtList *pEvtList)
54+
{
55+
static CtxEventDetector detector;
56+
57+
TEvtList dst;
58+
for (const DefEvent &evt : *pEvtList) {
59+
if (detector.isAnyCtxLine(evt))
60+
continue;
61+
62+
dst.push_back(evt);
63+
}
64+
65+
pEvtList->swap(dst);
66+
}
67+
68+
void appendCtxLines(
69+
TEvtList *pDst,
70+
std::istream &inStr,
71+
const int defLine,
72+
const int ctxLines)
73+
{
74+
if (ctxLines < 0)
75+
return;
76+
77+
const int firstLine = defLine - ctxLines;
78+
const int lastLine = defLine + ctxLines;
79+
80+
int line = 1;
81+
std::string text;
82+
for (; line <= lastLine; ++line) {
83+
if (!std::getline(inStr, text))
84+
// premature end of input
85+
break;
86+
87+
if (line < firstLine)
88+
// skip lines before the context lines
89+
continue;
90+
91+
// quote embedded NULs as they cause problems to some JSON parsers
92+
std::string nul;
93+
nul.push_back('\0');
94+
boost::algorithm::replace_all(text, nul, "[NUL]");
95+
96+
// format a single line of the comment
97+
std::ostringstream str;
98+
str << std::fixed << std::setw(5) << line;
99+
if (defLine == line)
100+
str << "|-> ";
101+
else
102+
str << "| ";
103+
str << text;
104+
105+
// append the comment as a new event
106+
DefEvent evt;
107+
evt.event = "#";
108+
evt.msg = str.str();
109+
evt.verbosityLevel = /* not a key event */ 1;
110+
pDst->push_back(evt);
111+
}
112+
}
113+
114+
void CtxEmbedder::handleDef(const Defect &defOrig)
115+
{
116+
const DefEvent &evt = defOrig.events[defOrig.keyEventIdx];
117+
if (!evt.line) {
118+
// no line number for the key event
119+
agent_->handleDef(defOrig);
120+
return;
121+
}
122+
123+
std::ifstream fstr(evt.fileName);
124+
if (!fstr) {
125+
// failed to open input file
126+
agent_->handleDef(defOrig);
127+
return;
128+
}
129+
130+
// clone defOrig and append the context lines
131+
Defect def(defOrig);
132+
dropCtxLines(&def.events);
133+
appendCtxLines(&def.events, fstr, evt.line, ctxLines_ - 1);
134+
135+
// close the file stream and forward the result
136+
fstr.close();
137+
agent_->handleDef(def);
138+
}

src/lib/filter.hh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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_FILTER_H
21+
#define H_GUARD_FILTER_H
22+
23+
#include "abstract-filter.hh"
24+
25+
/// decorator
26+
class EventPrunner: public GenericAbstractFilter {
27+
private:
28+
int thr_;
29+
30+
public:
31+
EventPrunner(AbstractWriter *agent, int thr):
32+
GenericAbstractFilter(agent),
33+
thr_(thr)
34+
{
35+
}
36+
37+
void handleDef(const Defect &defOrig) override;
38+
};
39+
40+
/// decorator
41+
class CtxEmbedder: public GenericAbstractFilter {
42+
private:
43+
int ctxLines_;
44+
45+
public:
46+
CtxEmbedder(AbstractWriter *agent, const int ctxLines):
47+
GenericAbstractFilter(agent),
48+
ctxLines_(ctxLines)
49+
{
50+
}
51+
52+
void handleDef(const Defect &defOrig) override;
53+
};
54+
55+
#endif /* H_GUARD_FILTER_H */

0 commit comments

Comments
 (0)