Skip to content

Commit 653f312

Browse files
committed
csgrep: implement --filter-file option
1 parent 036108f commit 653f312

10 files changed

+134
-5
lines changed

src/csgrep.cc

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,8 @@ int main(int argc, char *argv[])
586586

587587
("ignore-case,i", "ignore case when matching regular expressions")
588588
("invert-match,v", "select defects that do not match the selected criteria")
589-
("invert-regex,n", "invert regular expressions in all predicates");
590-
589+
("invert-regex,n", "invert regular expressions in all predicates")
590+
("filter-file,f", po::value<TStringList>(), "read custom filtering rules from a file in JSON format");
591591
addColorOptions(&desc);
592592
desc.add_options()
593593
("quiet,q", "do not report any parsing errors")
@@ -639,6 +639,15 @@ int main(int argc, char *argv[])
639639
return 1;
640640
}
641641

642+
const bool silent = vm.count("quiet");
643+
644+
if (vm.count("filter-file")) {
645+
const TStringList &filterFiles = vm["filter-file"].as<TStringList>();
646+
if (!MsgFilter::inst()->setFilterFiles(filterFiles, silent))
647+
// an error message already printed out
648+
return 1;
649+
}
650+
642651
// create a writer according to the selected mode
643652
WriterFactory factory;
644653
AbstractWriter *eng = factory.create(mode);
@@ -664,14 +673,13 @@ int main(int argc, char *argv[])
664673
if (vm.count("remove-duplicates"))
665674
eng = new DuplicateFilter(eng);
666675

667-
const bool silent = vm.count("quiet");
668-
bool hasError = false;
669-
670676
if (!chainDecorator<EventPrunner>(&eng, vm, "prune-events")
671677
|| !chainDecorator<CtxEmbedder>(&eng, vm, "embed-context"))
672678
// error message already printed, eng already feeed
673679
return 1;
674680

681+
bool hasError = false;
682+
675683
if (!vm.count("input-file")) {
676684
hasError = !eng->handleFile("-", silent);
677685
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--remove-duplicates --filter-file ../../../tests/csgrep/76-filter-file-basic-filter.json
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"msg-filter" : [
3+
{
4+
"checker" : "DIVINE|SYMBIOTIC",
5+
"regexp" : "memory"
6+
},
7+
{
8+
"checker" : "COMPILER_WARNING",
9+
"regexp" : "called on unallocated object",
10+
"replace" : "called correctly, no UB here"
11+
}
12+
]
13+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Error: COMPILER_WARNING:
2+
/home/lukas/aufover-benchmark/tests/single-c/mem-basic-free/0006-free-stack/0006-test.c: scope_hint: In function 'main'
3+
/home/lukas/aufover-benchmark/tests/single-c/mem-basic-free/0006-free-stack/0006-test.c:6:5: warning[-Wfree-nonheap-object]: 'free' called on unallocated object 'a'
4+
# 6 | free(&a); /* invalid free */
5+
# | ^~~~~~~~
6+
/home/lukas/aufover-benchmark/tests/single-c/mem-basic-free/0006-free-stack/0006-test.c:5:9: note: declared here
7+
# 5 | int a;
8+
# | ^
9+
10+
Error: COMPILER_WARNING:
11+
/home/lukas/aufover-benchmark/tests/single-c/mem-basic-free/0006-free-stack/0006-test.c: scope_hint: In function 'main'
12+
/home/lukas/aufover-benchmark/tests/single-c/mem-basic-free/0006-free-stack/0006-test.c:6:5: warning[-Wfree-nonheap-object]: 'free' called correctly, no UB here 'a'
13+
# 6 | free(&a); /* invalid free */
14+
# | ^~~~~~~~
15+
/home/lukas/aufover-benchmark/tests/single-c/mem-basic-free/0006-free-stack/0006-test.c:5:9: note: declared here
16+
# 5 | int a;
17+
# | ^
18+
19+
Error: COMPILER_WARNING:
20+
/home/lukas/aufover-benchmark/tests/single-c/mem-basic-calloc/0004-calloc-plain-leak/0004-test.c: scope_hint: In function 'main'
21+
/home/lukas/aufover-benchmark/tests/single-c/mem-basic-calloc/0004-calloc-plain-leak/0004-test.c:5:5: warning[-Wunused-result]: ignoring return value of 'calloc' declared with attribute 'warn_unused_result'
22+
# 5 | calloc(1, sizeof(char)); /* leak */
23+
# | ^~~~~~~~~~~~~~~~~~~~~~~
24+
25+
Error: SYMBIOTIC_WARNING:
26+
/home/lukas/aufover-benchmark/tests/single-c/mem-basic-malloc/0009-malloc-zerosize-leak/0009-test.c:10: error: memory error: memory not cleaned up
27+
/home/lukas/aufover-benchmark/tests/single-c/mem-basic-malloc/0009-malloc-zerosize-leak/0009-test.c:10: note: call stack: main ()
28+
29+
Error: DIVINE_WARNING:
30+
/home/lukas/tmp/assert-bounds.c: scope_hint: In function 'main':
31+
/home/lukas/tmp/assert-bounds.c:8: error: access of size 1 at [alloca*] is 1 bytes out of bounds
32+
/home/lukas/tmp/assert-bounds.c:8: note: memory error in userspace
33+
/opt/divine/include/dios/sys/fault.hpp:119: note: void __dios::FaultBase::handler<__dios::Context>(_VM_Fault, _VM_Frame*, void (*)())
34+
/home/lukas/tmp/assert-bounds.c:8: note: main
35+
/opt/divine/include/dios/libc/sys/start.cpp:91: note: __dios_start
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Error: COMPILER_WARNING:
2+
/home/lukas/aufover-benchmark/tests/single-c/mem-basic-free/0006-free-stack/0006-test.c: scope_hint: In function 'main'
3+
/home/lukas/aufover-benchmark/tests/single-c/mem-basic-free/0006-free-stack/0006-test.c:6:5: warning[-Wfree-nonheap-object]: 'free' called on unallocated object 'a'
4+
# 6 | free(&a); /* invalid free */
5+
# | ^~~~~~~~
6+
/home/lukas/aufover-benchmark/tests/single-c/mem-basic-free/0006-free-stack/0006-test.c:5:9: note: declared here
7+
# 5 | int a;
8+
# | ^
9+
10+
Error: COMPILER_WARNING:
11+
/home/lukas/aufover-benchmark/tests/single-c/mem-basic-calloc/0004-calloc-plain-leak/0004-test.c: scope_hint: In function 'main'
12+
/home/lukas/aufover-benchmark/tests/single-c/mem-basic-calloc/0004-calloc-plain-leak/0004-test.c:5:5: warning[-Wunused-result]: ignoring return value of 'calloc' declared with attribute 'warn_unused_result'
13+
# 5 | calloc(1, sizeof(char)); /* leak */
14+
# | ^~~~~~~~~~~~~~~~~~~~~~~
15+
16+
Error: SYMBIOTIC_WARNING:
17+
/home/lukas/aufover-benchmark/tests/single-c/mem-basic-malloc/0009-malloc-zerosize-leak/0009-test.c:10: error: memory error: memory not cleaned up
18+
/home/lukas/aufover-benchmark/tests/single-c/mem-basic-malloc/0009-malloc-zerosize-leak/0009-test.c:10: note: call stack: main ()
19+
20+
Error: DIVINE_WARNING:
21+
/home/lukas/tmp/assert-bounds.c: scope_hint: In function 'main':
22+
/home/lukas/tmp/assert-bounds.c:8: error: access of size 1 at [alloca*] is 1 bytes out of bounds
23+
/home/lukas/tmp/assert-bounds.c:8: note: memory error in userspace
24+
/opt/divine/include/dios/sys/fault.hpp:119: note: void __dios::FaultBase::handler<__dios::Context>(_VM_Fault, _VM_Frame*, void (*)())
25+
/home/lukas/tmp/assert-bounds.c:8: note: main
26+
/opt/divine/include/dios/libc/sys/start.cpp:91: note: __dios_start
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--remove-duplicates --filter-file ../../../tests/csgrep/77-filter-file-checker-regex-filter.json
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"msg-filter" : [
3+
{
4+
"checker" : "DIVINE|SYMBIOTIC",
5+
"regexp" : "memory",
6+
"replace" : "heap"
7+
}
8+
]
9+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Error: SYMBIOTIC_WARNING:
2+
/home/lukas/aufover-benchmark/tests/single-c/mem-basic-malloc/0009-malloc-zerosize-leak/0009-test.c:10: error: heap error: memory not cleaned up
3+
/home/lukas/aufover-benchmark/tests/single-c/mem-basic-malloc/0009-malloc-zerosize-leak/0009-test.c:10: note: call stack: main ()
4+
5+
Error: SYMBIOTIC_WARNING:
6+
/home/lukas/aufover-benchmark/tests/single-c/mem-basic-malloc/0009-malloc-zerosize-leak/0009-test.c:10: error: memory error: memory not cleaned up
7+
/home/lukas/aufover-benchmark/tests/single-c/mem-basic-malloc/0009-malloc-zerosize-leak/0009-test.c:10: note: call stack: main ()
8+
9+
Error: DIVINE_WARNING:
10+
/home/lukas/tmp/assert-bounds.c: scope_hint: In function 'main':
11+
/home/lukas/tmp/assert-bounds.c:8: error: access of size 1 at [alloca*] is 1 bytes out of bounds
12+
/home/lukas/tmp/assert-bounds.c:8: note: memory error in userspace
13+
/opt/divine/include/dios/sys/fault.hpp:119: note: void __dios::FaultBase::handler<__dios::Context>(_VM_Fault, _VM_Frame*, void (*)())
14+
/home/lukas/tmp/assert-bounds.c:8: note: main
15+
/opt/divine/include/dios/libc/sys/start.cpp:91: note: __dios_start
16+
17+
Error: DIVINE_WARNING:
18+
/home/lukas/tmp/assert-bounds.c: scope_hint: In function 'main':
19+
/home/lukas/tmp/assert-bounds.c:8: error: access of size 1 at [alloca*] is 1 bytes out of bounds
20+
/home/lukas/tmp/assert-bounds.c:8: note: heap error in userspace
21+
/opt/divine/include/dios/sys/fault.hpp:119: note: void __dios::FaultBase::handler<__dios::Context>(_VM_Fault, _VM_Frame*, void (*)())
22+
/home/lukas/tmp/assert-bounds.c:8: note: main
23+
/opt/divine/include/dios/libc/sys/start.cpp:91: note: __dios_start
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Error: SYMBIOTIC_WARNING:
2+
/home/lukas/aufover-benchmark/tests/single-c/mem-basic-malloc/0009-malloc-zerosize-leak/0009-test.c:10: error: heap error: memory not cleaned up
3+
/home/lukas/aufover-benchmark/tests/single-c/mem-basic-malloc/0009-malloc-zerosize-leak/0009-test.c:10: note: call stack: main ()
4+
5+
Error: DIVINE_WARNING:
6+
/home/lukas/tmp/assert-bounds.c: scope_hint: In function 'main':
7+
/home/lukas/tmp/assert-bounds.c:8: error: access of size 1 at [alloca*] is 1 bytes out of bounds
8+
/home/lukas/tmp/assert-bounds.c:8: note: memory error in userspace
9+
/opt/divine/include/dios/sys/fault.hpp:119: note: void __dios::FaultBase::handler<__dios::Context>(_VM_Fault, _VM_Frame*, void (*)())
10+
/home/lukas/tmp/assert-bounds.c:8: note: main
11+
/opt/divine/include/dios/libc/sys/start.cpp:91: note: __dios_start

tests/csgrep/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,5 @@ test_csgrep("72-abstract-parser-gcc-builtin" )
119119
test_csgrep("73-coverity-misra-checkers" )
120120
test_csgrep("74-coverity-leading-space" )
121121
test_csgrep("75-coverity-from-cshtml" )
122+
test_csgrep("76-filter-file-basic" )
123+
test_csgrep("77-filter-file-checker-regex" )

0 commit comments

Comments
 (0)