Skip to content

Commit e483308

Browse files
committed
csgrep --hash-v1: match csdiff/v1 fingerprint prefix
Related: #98
1 parent 83c638d commit e483308

File tree

5 files changed

+113321
-1
lines changed

5 files changed

+113321
-1
lines changed

src/csgrep.cc

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "abstract-filter.hh"
2121
#include "filter.hh"
22+
#include "finger-print.hh"
2223
#include "msg-filter.hh"
2324
#include "parser.hh"
2425
#include "parser-common.hh"
@@ -232,6 +233,36 @@ class ImpLevelFilter: public AbstractFilter {
232233
}
233234
};
234235

236+
class FingerPrintFilter: public AbstractFilter {
237+
private:
238+
const std::string hashPrefix_;
239+
240+
public:
241+
FingerPrintFilter(AbstractWriter *agent, const std::string &hashPrefix):
242+
AbstractFilter(agent),
243+
hashPrefix_(hashPrefix)
244+
{
245+
}
246+
247+
protected:
248+
bool matchDef(const Defect &def) override {
249+
const FingerPrinter fp(def);
250+
std::string hash = fp.getHash(FPV_CSDIFF_WITH_LINE_CONTENT);
251+
if (hash.empty())
252+
// fingerprint not available for this finding
253+
return false;
254+
255+
const size_t prefixLen = hashPrefix_.size();
256+
if (hash.size() < prefixLen)
257+
// the prefix we are looking for is longer than the hash itself
258+
return false;
259+
260+
// make size of the hash equal to size of the prefix and compare
261+
hash.resize(prefixLen);
262+
return (hashPrefix_ == hash);
263+
}
264+
};
265+
235266
class KeyEventPredicate: public IPredicate {
236267
private:
237268
const RE re_;
@@ -543,7 +574,8 @@ bool chainFilters(
543574
return false;
544575
}
545576

546-
return chainDecoratorIntArg<ImpLevelFilter>(pEng, vm, "imp-level");
577+
return chainDecoratorGeneric<FingerPrintFilter>(pEng, vm, "hash-v1")
578+
&& chainDecoratorIntArg<ImpLevelFilter> (pEng, vm, "imp-level");
547579
}
548580

549581
int main(int argc, char *argv[])
@@ -565,6 +597,7 @@ int main(int argc, char *argv[])
565597
("path", po::value<string>(), "defect matches if the path of its key event matches the given regex")
566598
("event", po::value<string>(), "defect matches if its key event matches the given regex (each defect has exactly one key event, which determines its location in the code)")
567599
("error", po::value<string>(), "defect matches if the message of its key event matches the given regex")
600+
("hash-v1", po::value<string>(), "defect matches if its csdiff/v1 fingerprint starts with the given prefix")
568601
("msg", po::value<string>(), "defect matches if any of its messages matches the given regex")
569602
("tool", po::value<string>(), "defect matches if it was detected by tool that matches the given regex")
570603
("annot", po::value<string>(), "defect matches if its annotation matches the given regex")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--mode=json --prune-events=1 --hash-v1 04

0 commit comments

Comments
 (0)