diff --git a/analyzer/codechecker_analyzer/cmd/analyze.py b/analyzer/codechecker_analyzer/cmd/analyze.py index b676fbe9dc..882cbcd291 100644 --- a/analyzer/codechecker_analyzer/cmd/analyze.py +++ b/analyzer/codechecker_analyzer/cmd/analyze.py @@ -9,7 +9,6 @@ Execute analysis over an already existing build.json compilation database. """ - import argparse import collections import json @@ -35,7 +34,6 @@ SkipListHandlers from codechecker_common.util import load_json - LOG = logger.get_logger('system') header_file_extensions = ( @@ -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 @@ -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 "