From 3969742700babbbefacc40289a75da326c277d0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Csord=C3=A1s?= Date: Tue, 16 Nov 2021 08:39:22 +0100 Subject: [PATCH] [cli] Add file filter option for CodeChecker parse --- analyzer/codechecker_analyzer/cmd/parse.py | 41 ++++++++++++---- .../test_files/multiple_input_filter.output | 39 +++++++++++++++ .../multiple_input_filter_all.output | 48 +++++++++++++++++++ 3 files changed, 118 insertions(+), 10 deletions(-) create mode 100644 analyzer/tests/functional/analyze_and_parse/test_files/multiple_input_filter.output create mode 100644 analyzer/tests/functional/analyze_and_parse/test_files/multiple_input_filter_all.output diff --git a/analyzer/codechecker_analyzer/cmd/parse.py b/analyzer/codechecker_analyzer/cmd/parse.py index 2c3480b41c..ee33b05cf8 100644 --- a/analyzer/codechecker_analyzer/cmd/parse.py +++ b/analyzer/codechecker_analyzer/cmd/parse.py @@ -192,15 +192,6 @@ def add_arguments_to_parser(parser): help="Print the steps the analyzers took in finding " "the reported defect.") - parser.add_argument('-i', '--ignore', '--skip', - dest="skipfile", - required=False, - default=argparse.SUPPRESS, - help="Path to the Skipfile dictating which project " - "files should be omitted from analysis. Please " - "consult the User guide on how a Skipfile " - "should be laid out.") - parser.add_argument('--trim-path-prefix', type=str, nargs='*', @@ -224,6 +215,31 @@ def add_arguments_to_parser(parser): "values are: {0}".format( ', '.join(REVIEW_STATUS_VALUES))) + group = parser.add_argument_group("file filter arguments") + group = group.add_mutually_exclusive_group() + + group.add_argument('-i', '--ignore', '--skip', + dest="skipfile", + required=False, + default=argparse.SUPPRESS, + help="Path to the Skipfile dictating which project " + "files should be omitted from analysis. Please " + "consult the User guide on how a Skipfile " + "should be laid out.") + + group.add_argument('--file', + nargs='+', + dest="files", + metavar='FILE', + required=False, + default=argparse.SUPPRESS, + help="Filter results by file path. " + "The file path can contain multiple * " + "quantifiers which matches any number of " + "characters (zero or more). So if you have " + "/a/x.cpp and /a/y.cpp then \"/a/*.cpp\" " + "selects both.") + logger.add_verbose_arguments(parser) parser.set_defaults( func=main, func_process_config_file=cmd_config.process_config_file) @@ -358,7 +374,12 @@ def get_output_file_path(default_file_name: str) -> Optional[str]: return os.path.join(output_dir_path, default_file_name) skip_file_content = "" - if 'skipfile' in args: + if 'files' in args: + items = [f"+{file_path}" for file_path in args.files] + items.append("-*") + + skip_file_content = "\n".join(items) + elif 'skipfile' in args: with open(args.skipfile, 'r', encoding='utf-8', errors='ignore') as skip_file: skip_file_content = skip_file.read() diff --git a/analyzer/tests/functional/analyze_and_parse/test_files/multiple_input_filter.output b/analyzer/tests/functional/analyze_and_parse/test_files/multiple_input_filter.output new file mode 100644 index 0000000000..5c94cb1e8e --- /dev/null +++ b/analyzer/tests/functional/analyze_and_parse/test_files/multiple_input_filter.output @@ -0,0 +1,39 @@ +NORMAL#CodeChecker parse $WORKSPACE$/test_files/macros/macros.plist $WORKSPACE$/test_files/notes/notes.plist --file "*macros.cpp" +-------------------------------------------------------------------------------- +[HIGH] macros.cpp:7:8: Dereference of null pointer (loaded from variable 'ptr') [core.NullDereference] + *ptr = 5; // expected-warning{{Dereference of null pointer}} + ^ + +Found 1 defect(s) in macros.cpp + + +----==== Severity Statistics ====---- +---------------------------- +Severity | Number of reports +---------------------------- +HIGH | 1 +---------------------------- +----=================---- + +----==== Checker Statistics ====---- +--------------------------------------------------- +Checker name | Severity | Number of reports +--------------------------------------------------- +core.NullDereference | HIGH | 1 +--------------------------------------------------- +----=================---- + +----==== File Statistics ====---- +------------------------------ +File name | Number of reports +------------------------------ +macros.cpp | 1 +------------------------------ +----=================---- + +----======== Summary ========---- +--------------------------------------------- +Number of processed analyzer result files | 2 +Number of analyzer reports | 1 +--------------------------------------------- +----=================---- diff --git a/analyzer/tests/functional/analyze_and_parse/test_files/multiple_input_filter_all.output b/analyzer/tests/functional/analyze_and_parse/test_files/multiple_input_filter_all.output new file mode 100644 index 0000000000..9b16894b9c --- /dev/null +++ b/analyzer/tests/functional/analyze_and_parse/test_files/multiple_input_filter_all.output @@ -0,0 +1,48 @@ +NORMAL#CodeChecker parse $WORKSPACE$/test_files/macros/macros.plist $WORKSPACE$/test_files/notes/notes.plist --file "*.cpp" +-------------------------------------------------------------------------------- +[HIGH] macros.cpp:7:8: Dereference of null pointer (loaded from variable 'ptr') [core.NullDereference] + *ptr = 5; // expected-warning{{Dereference of null pointer}} + ^ + +Found 1 defect(s) in macros.cpp + +[LOW] notes.cpp:3:23: Duplicate code detected [alpha.clone.CloneChecker] +int max(int a, int b) { // expected-warning{{Duplicate code detected}} + ^ + +Found 1 defect(s) in notes.cpp + + +----==== Severity Statistics ====---- +---------------------------- +Severity | Number of reports +---------------------------- +HIGH | 1 +LOW | 1 +---------------------------- +----=================---- + +----==== Checker Statistics ====---- +------------------------------------------------------- +Checker name | Severity | Number of reports +------------------------------------------------------- +core.NullDereference | HIGH | 1 +alpha.clone.CloneChecker | LOW | 1 +------------------------------------------------------- +----=================---- + +----==== File Statistics ====---- +------------------------------ +File name | Number of reports +------------------------------ +macros.cpp | 1 +notes.cpp | 1 +------------------------------ +----=================---- + +----======== Summary ========---- +--------------------------------------------- +Number of processed analyzer result files | 2 +Number of analyzer reports | 2 +--------------------------------------------- +----=================----