Skip to content

Commit

Permalink
Add functionality to validate analyzer and checker options
Browse files Browse the repository at this point in the history
When specifying invalid checker and analyzer config,
the CodeChecker analyze command used to give a warning
but continued running.
This functionality throws an error and prevents the
analysis from running in this case.
  • Loading branch information
Nora Zinaeddin committed Apr 5, 2024
1 parent 42afcee commit fadef54
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
59 changes: 57 additions & 2 deletions analyzer/codechecker_analyzer/cmd/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
Execute analysis over an already existing build.json compilation database.
"""


import argparse
import collections
import json
Expand All @@ -35,7 +34,6 @@
SkipListHandlers
from codechecker_common.util import load_json


LOG = logger.get_logger('system')

header_file_extensions = (
Expand Down Expand Up @@ -797,6 +795,57 @@ def add_arguments_to_parser(parser):
func=main, func_process_config_file=cmd_config.process_config_file)


def validate_analyzer_parameter(args, analyzer):
"""
Ensure that the analyzer_config parameter is set to a valid value
by verifying if it belongs to the set of allowed values.
"""
if analyzer in analyzer_types.supported_analyzers:
analyzer_class = analyzer_types.supported_analyzers[analyzer]

analyzers = [analyzer[0] for analyzer in
analyzer_class.get_analyzer_config()]

# Make sure "[clang-tidy] Allow to override checker list #3203" works
if analyzer_class.ANALYZER_NAME == 'clang-tidy':
analyzers.append('take-config-from-directory')

if 'analyzer_config' in args and isinstance(args.analyzer_config,
list):
for cfg in args.analyzer_config:
if cfg.analyzer == analyzer_class.ANALYZER_NAME and \
cfg.option not in analyzers:
LOG.error(f"Invalid --analyzer-config parameter: "
f"{analyzer_class.ANALYZER_NAME} has no "
f"analyzer named {cfg.option}. Use the "
f"'CodeChecker analyzers --analyzer-config "
f"{analyzer_class.ANALYZER_NAME}' "
f"command to check available analyzers.")
sys.exit(1)


def validate_checker_parameter(args, analyzer):
"""
Ensure that the checker_config parameter is set to a valid value
by verifying if it belongs to the set of allowed values.
"""
if analyzer in analyzer_types.supported_analyzers:
analyzer_class = analyzer_types.supported_analyzers[analyzer]

checkers = [checker[0] for checker in
analyzer_class.get_analyzer_checkers()]
if 'checker_config' in args and isinstance(args.checker_config, list):
for cfg in args.checker_config:
if cfg.analyzer == analyzer_class.ANALYZER_NAME and \
cfg.checker not in checkers:
LOG.error(f"Invalid --checker-config parameter: "
f"{analyzer_class.ANALYZER_NAME} has no checker "
f"named {cfg.checker}. Use the "
f"'CodeChecker checkers --checker-config' "
f"command to check available checkers.")
sys.exit(1)


def get_affected_file_paths(
file_filters: List[str],
compile_commands: tu_collector.CompilationDB
Expand Down Expand Up @@ -934,6 +983,12 @@ def main(args):
"""
logger.setup_logger(args.verbose if 'verbose' in args else None)

# Validate analyzer and checker config (if any)
[validate_analyzer_parameter(args, an) for an in
analyzer_types.supported_analyzers if 'analyzer_config' in args]
[validate_checker_parameter(args, an) for an in
analyzer_types.supported_analyzers if 'checker_config' in args]

if 'tidy_config' in args:
LOG.warning(
"--tidy-config is deprecated and will be removed in the next "
Expand Down
14 changes: 14 additions & 0 deletions analyzer/tests/functional/cmdline/test_cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,20 @@ def test_analyzer_config(self):
self.assertTrue(desc)
self.assertFalse(desc[0].islower())

def test_analyze_wrong_parameters(self):
"""
This test checks whether the analyze command stops running if a wrong
--analysis-config or --checker-config parameter is specified.
"""

analyzer_cfg_cmd = [env.codechecker_cmd(), 'analyze',
'--analyzer-config', 'clangsa:asd=value']
checker_cfg_cmd = [env.codechecker_cmd(), 'analyze',
'--checker-config', 'clang-tidy:asd=value']

[self.assertEqual(1, run_cmd(cmd)[0])
for cmd in [analyzer_cfg_cmd, checker_cfg_cmd]]

def test_checker_config_format(self):
"""
Test if checker config option is meeting the reqired format.
Expand Down

0 comments on commit fadef54

Please sign in to comment.