Skip to content

Commit 7c3e4a1

Browse files
committed
filter: adopt derivates of *AbstractFilter from csgrep
1 parent b78155e commit 7c3e4a1

File tree

3 files changed

+177
-153
lines changed

3 files changed

+177
-153
lines changed

src/csgrep.cc

Lines changed: 0 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include "writer-json.hh"
2929

3030
#include <cstdlib>
31-
#include <fstream>
3231
#include <iomanip>
3332
#include <map>
3433

@@ -336,158 +335,6 @@ class SrcAnnotPredicate: public IPredicate {
336335
}
337336
};
338337

339-
class PathStripper: public GenericAbstractFilter {
340-
public:
341-
PathStripper(AbstractWriter *agent, const std::string &prefix):
342-
GenericAbstractFilter(agent),
343-
prefStr_(prefix),
344-
prefSize_(prefix.size())
345-
{
346-
}
347-
348-
void handleDef(const Defect &defOrig) override {
349-
Defect def(defOrig);
350-
351-
// iterate through all events
352-
for (DefEvent &evt : def.events) {
353-
std::string &path = evt.fileName;
354-
if (path.size() < prefSize_)
355-
continue;
356-
357-
const std::string str(path, /* pos */ 0U, prefSize_);
358-
if (str != prefStr_)
359-
continue;
360-
361-
// strip path prefix in this event
362-
path.erase(/* pos */ 0U, prefSize_);
363-
}
364-
365-
agent_->handleDef(def);
366-
}
367-
368-
private:
369-
const std::string prefStr_;
370-
const size_t prefSize_;
371-
};
372-
373-
class PathPrepender: public GenericAbstractFilter {
374-
public:
375-
PathPrepender(AbstractWriter *agent, const std::string &prefix):
376-
GenericAbstractFilter(agent),
377-
prefix_(prefix)
378-
{
379-
}
380-
381-
void handleDef(const Defect &defOrig) override {
382-
Defect def(defOrig);
383-
384-
// iterate through all events
385-
for (DefEvent &evt : def.events) {
386-
std::string &path = evt.fileName;
387-
if (path.empty() || path[0] == '/')
388-
// not a relative path
389-
continue;
390-
391-
path.insert(0U, prefix_);
392-
}
393-
394-
agent_->handleDef(def);
395-
}
396-
397-
private:
398-
const std::string prefix_;
399-
};
400-
401-
class DropScanProps: public GenericAbstractFilter {
402-
public:
403-
DropScanProps(AbstractWriter *agent):
404-
GenericAbstractFilter(agent)
405-
{
406-
}
407-
408-
/// ignore any given scan properties
409-
void setScanProps(const TScanProps &) override { }
410-
411-
/// always return empty scan properties
412-
const TScanProps& getScanProps() const override {
413-
return emp_;
414-
}
415-
416-
private:
417-
const TScanProps emp_;
418-
};
419-
420-
class ScanPropSetter: public GenericAbstractFilter {
421-
public:
422-
ScanPropSetter(AbstractWriter *agent, const TStringList &propList);
423-
424-
/// override specified scan properties
425-
void setScanProps(const TScanProps &origProps) override;
426-
427-
private:
428-
// key/val pairs are stored in a vector
429-
using TItem = std::pair<std::string, std::string>;
430-
using TList = std::vector<TItem>;
431-
TList itemList_;
432-
};
433-
434-
ScanPropSetter::ScanPropSetter(
435-
AbstractWriter *agent,
436-
const TStringList &propList):
437-
GenericAbstractFilter(agent)
438-
{
439-
// iterate over the given NAME:VALUE strings
440-
for (const std::string &str : propList) {
441-
// split the string by the first occurrence of ':'
442-
size_t ddAt = str.find(':');
443-
if (std::string::npos == ddAt) {
444-
const auto msg = "missing ':' in " + str;
445-
throw std::runtime_error(msg);
446-
}
447-
448-
// store the key/val pair into the vector
449-
itemList_.emplace_back(
450-
/* key */ str.substr(0, ddAt),
451-
/* val */ str.substr(ddAt + 1));
452-
}
453-
}
454-
455-
void ScanPropSetter::setScanProps(const TScanProps &origProps)
456-
{
457-
// we need to copy the given map
458-
TScanProps props = origProps;
459-
460-
// apply specified changes
461-
for (const auto &item : itemList_)
462-
props[item./* key */first] = item./* val */second;
463-
464-
// forward the result
465-
agent_->setScanProps(props);
466-
}
467-
468-
class DuplicateFilter: public AbstractFilter {
469-
public:
470-
DuplicateFilter(AbstractWriter *agent):
471-
AbstractFilter(agent)
472-
{
473-
}
474-
475-
protected:
476-
bool matchDef(const Defect &def) override {
477-
DefEvent evt = def.events[def.keyEventIdx];
478-
479-
// abstract out differences we do not deem important
480-
evt.fileName = MsgFilter::inst().filterPath(evt.fileName);
481-
evt.msg = MsgFilter::inst().filterMsg(evt.msg, def.checker);
482-
483-
return lookup_.insert(evt)./* inserted */second;
484-
}
485-
486-
private:
487-
typedef std::set<DefEvent> TLookup;
488-
TLookup lookup_;
489-
};
490-
491338
class WriterFactory {
492339
private:
493340
typedef std::map<std::string, AbstractWriter* (*)(void)> TTable;

src/lib/filter.cc

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

2020
#include "filter.hh"
2121

22+
#include "msg-filter.hh"
23+
2224
#include <fstream>
2325
#include <iomanip>
26+
#include <set>
2427
#include <sstream>
2528

2629
#include <boost/algorithm/string/replace.hpp>
@@ -136,3 +139,67 @@ void CtxEmbedder::handleDef(const Defect &defOrig)
136139
fstr.close();
137140
agent_->handleDef(def);
138141
}
142+
143+
144+
// /////////////////////////////////////////////////////////////////////////////
145+
// implementation of ScanPropSetter
146+
147+
ScanPropSetter::ScanPropSetter(
148+
AbstractWriter *agent,
149+
const TStringList &propList):
150+
GenericAbstractFilter(agent)
151+
{
152+
// iterate over the given NAME:VALUE strings
153+
for (const std::string &str : propList) {
154+
// split the string by the first occurrence of ':'
155+
size_t ddAt = str.find(':');
156+
if (std::string::npos == ddAt) {
157+
const auto msg = "missing ':' in " + str;
158+
throw std::runtime_error(msg);
159+
}
160+
161+
// store the key/val pair into the vector
162+
itemList_.emplace_back(
163+
/* key */ str.substr(0, ddAt),
164+
/* val */ str.substr(ddAt + 1));
165+
}
166+
}
167+
168+
void ScanPropSetter::setScanProps(const TScanProps &origProps)
169+
{
170+
// we need to copy the given map
171+
TScanProps props = origProps;
172+
173+
// apply specified changes
174+
for (const auto &item : itemList_)
175+
props[item./* key */first] = item./* val */second;
176+
177+
// forward the result
178+
agent_->setScanProps(props);
179+
}
180+
181+
182+
// /////////////////////////////////////////////////////////////////////////////
183+
// implementation of DuplicateFilter
184+
185+
struct DuplicateFilter::Private {
186+
using TLookup = std::set<DefEvent>;
187+
TLookup lookup;
188+
};
189+
190+
DuplicateFilter::DuplicateFilter(AbstractWriter *agent):
191+
AbstractFilter(agent),
192+
d(new Private)
193+
{
194+
}
195+
196+
bool DuplicateFilter::matchDef(const Defect &def)
197+
{
198+
DefEvent evt = def.events[def.keyEventIdx];
199+
200+
// abstract out differences we do not deem important
201+
evt.fileName = MsgFilter::inst().filterPath(evt.fileName);
202+
evt.msg = MsgFilter::inst().filterMsg(evt.msg, def.checker);
203+
204+
return d->lookup.insert(evt)./* inserted */second;
205+
}

src/lib/filter.hh

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,114 @@ class CtxEmbedder: public GenericAbstractFilter {
5252
void handleDef(const Defect &defOrig) override;
5353
};
5454

55+
class PathStripper: public GenericAbstractFilter {
56+
public:
57+
PathStripper(AbstractWriter *agent, const std::string &prefix):
58+
GenericAbstractFilter(agent),
59+
prefStr_(prefix),
60+
prefSize_(prefix.size())
61+
{
62+
}
63+
64+
void handleDef(const Defect &defOrig) override {
65+
Defect def(defOrig);
66+
67+
// iterate through all events
68+
for (DefEvent &evt : def.events) {
69+
std::string &path = evt.fileName;
70+
if (path.size() < prefSize_)
71+
continue;
72+
73+
const std::string str(path, /* pos */ 0U, prefSize_);
74+
if (str != prefStr_)
75+
continue;
76+
77+
// strip path prefix in this event
78+
path.erase(/* pos */ 0U, prefSize_);
79+
}
80+
81+
agent_->handleDef(def);
82+
}
83+
84+
private:
85+
const std::string prefStr_;
86+
const size_t prefSize_;
87+
};
88+
89+
class PathPrepender: public GenericAbstractFilter {
90+
public:
91+
PathPrepender(AbstractWriter *agent, const std::string &prefix):
92+
GenericAbstractFilter(agent),
93+
prefix_(prefix)
94+
{
95+
}
96+
97+
void handleDef(const Defect &defOrig) override {
98+
Defect def(defOrig);
99+
100+
// iterate through all events
101+
for (DefEvent &evt : def.events) {
102+
std::string &path = evt.fileName;
103+
if (path.empty() || path[0] == '/')
104+
// not a relative path
105+
continue;
106+
107+
path.insert(0U, prefix_);
108+
}
109+
110+
agent_->handleDef(def);
111+
}
112+
113+
private:
114+
const std::string prefix_;
115+
};
116+
117+
class DropScanProps: public GenericAbstractFilter {
118+
public:
119+
DropScanProps(AbstractWriter *agent):
120+
GenericAbstractFilter(agent)
121+
{
122+
}
123+
124+
/// ignore any given scan properties
125+
void setScanProps(const TScanProps &) override { }
126+
127+
/// always return empty scan properties
128+
const TScanProps& getScanProps() const override {
129+
return emp_;
130+
}
131+
132+
private:
133+
const TScanProps emp_;
134+
};
135+
136+
class ScanPropSetter: public GenericAbstractFilter {
137+
public:
138+
using TStringList = std::vector<std::string>;
139+
140+
ScanPropSetter(AbstractWriter *agent, const TStringList &propList);
141+
142+
/// override specified scan properties
143+
void setScanProps(const TScanProps &origProps) override;
144+
145+
private:
146+
// key/val pairs are stored in a vector
147+
using TItem = std::pair<std::string, std::string>;
148+
using TList = std::vector<TItem>;
149+
TList itemList_;
150+
};
151+
152+
class DuplicateFilter: public AbstractFilter {
153+
public:
154+
DuplicateFilter(AbstractWriter *agent);
155+
~DuplicateFilter() override = default;
156+
157+
protected:
158+
bool matchDef(const Defect &def) override;
159+
160+
private:
161+
struct Private;
162+
std::unique_ptr<Private> d;
163+
};
164+
55165
#endif /* H_GUARD_FILTER_H */

0 commit comments

Comments
 (0)