Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

csgrep --imp-level=1: filter out important defects #143

Merged
merged 3 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 63 additions & 44 deletions src/csgrep.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,23 @@ class ToolPredicate: public IPredicate {
}
};

class ImpLevelFilter: public AbstractFilter {
private:
const int minLevel_;

public:
ImpLevelFilter(AbstractWriter *agent, const int minLevel):
AbstractFilter(agent),
minLevel_(minLevel)
{
}

protected:
bool matchDef(const Defect &def) override {
return minLevel_ <= def.imp;
}
};

class KeyEventPredicate: public IPredicate {
private:
const RE re_;
Expand Down Expand Up @@ -425,50 +442,6 @@ bool appendPredIfNeeded(
return true;
}

template <typename TFlags>
bool chainFiltersCore(
PredicateFilter *pf,
const po::variables_map &vm,
const TFlags flags)
{
return appendPredIfNeeded<AnnotPredicate> (pf, vm, flags, "annot")
&& appendPredIfNeeded<CheckerPredicate> (pf, vm, flags, "checker")
&& appendPredIfNeeded<ErrorPredicate> (pf, vm, flags, "error")
&& appendPredIfNeeded<KeyEventPredicate> (pf, vm, flags, "event")
&& appendPredIfNeeded<MsgPredicate> (pf, vm, flags, "msg")
&& appendPredIfNeeded<PathPredicate> (pf, vm, flags, "path")
&& appendPredIfNeeded<SrcAnnotPredicate> (pf, vm, flags, "src-annot")
&& appendPredIfNeeded<ToolPredicate> (pf, vm, flags, "tool");
}

bool chainFilters(
AbstractWriter **pEng,
const po::variables_map &vm)
{
// insert a filter predicate into the chain
PredicateFilter *pf = new PredicateFilter(*pEng);
*pEng = pf;

// common matching flags
boost::regex_constants::syntax_option_type flags = 0;
if (vm.count("ignore-case"))
flags |= boost::regex_constants::icase;

if (vm.count("invert-match"))
pf->setInvertMatch();

if (vm.count("invert-regex"))
pf->setInvertEachMatch();

if (chainFiltersCore(pf, vm, flags))
return true;

// failed to create the chain of filters
delete pf;
*pEng = 0;
return false;
}

template <class TDesc, class TStream>
void printUsage(TStream &str, const TDesc &desc)
{
Expand Down Expand Up @@ -528,6 +501,51 @@ bool chainDecoratorIntArg(
return true;
}

template <typename TFlags>
bool chainFiltersCore(
PredicateFilter *pf,
const po::variables_map &vm,
const TFlags flags)
{
return appendPredIfNeeded<AnnotPredicate> (pf, vm, flags, "annot")
&& appendPredIfNeeded<CheckerPredicate> (pf, vm, flags, "checker")
&& appendPredIfNeeded<ErrorPredicate> (pf, vm, flags, "error")
&& appendPredIfNeeded<KeyEventPredicate> (pf, vm, flags, "event")
&& appendPredIfNeeded<MsgPredicate> (pf, vm, flags, "msg")
&& appendPredIfNeeded<PathPredicate> (pf, vm, flags, "path")
&& appendPredIfNeeded<SrcAnnotPredicate> (pf, vm, flags, "src-annot")
&& appendPredIfNeeded<ToolPredicate> (pf, vm, flags, "tool");
}

bool chainFilters(
AbstractWriter **pEng,
const po::variables_map &vm)
{
// insert a filter predicate into the chain
PredicateFilter *pf = new PredicateFilter(*pEng);
*pEng = pf;

// common matching flags
boost::regex_constants::syntax_option_type flags = 0;
if (vm.count("ignore-case"))
flags |= boost::regex_constants::icase;

if (vm.count("invert-match"))
pf->setInvertMatch();

if (vm.count("invert-regex"))
pf->setInvertEachMatch();

if (!chainFiltersCore(pf, vm, flags)) {
// failed to create the chain of filters
delete pf;
*pEng = 0;
return false;
}

return chainDecoratorIntArg<ImpLevelFilter>(pEng, vm, "imp-level");
}

int main(int argc, char *argv[])
{
using std::string;
Expand All @@ -551,6 +569,7 @@ int main(int argc, char *argv[])
("tool", po::value<string>(), "defect matches if it was detected by tool that matches the given regex")
("annot", po::value<string>(), "defect matches if its annotation matches the given regex")
("src-annot", po::value<string>(), "defect matches if an annotation in the _source_ file matches the given regex")
("imp-level", po::value<int>(), "defect matches if the importance level is greater or equal than the given number")

("drop-scan-props", "do not propagate scan properties")
("embed-context,U", po::value<int>(), "embed a number of lines of context from the source file for the key event")
Expand Down
1 change: 1 addition & 0 deletions tests/csgrep/0115-csgrep-imp-filter-args.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--mode=json --imp-level=1
Loading