Beispiel #1
0
def __print_guidelines(args: argparse.Namespace, cl: CheckerLabels):
    """
    Print guidelines according to the command line arguments to the standard
    output.
    """
    if args.output_format == 'custom':
        args.output_format = 'rows'

    result = {}

    for guideline in cl.get_description('guideline'):
        result[guideline] = set(cl.occurring_values(guideline))

    header = ['Guideline', 'Rules']
    if args.output_format in ['csv', 'json']:
        header = list(map(__uglify, header))

    if args.output_format == 'json':
        rows = [(g, sorted(list(r))) for g, r in result.items()]
    else:
        rows = [(g, ', '.join(sorted(r))) for g, r in result.items()]

    if args.output_format == 'rows':
        for row in rows:
            print('Guideline: {}'.format(row[0]))
            print('Rules: {}'.format(row[1]))
    else:
        print(twodim.to_str(args.output_format, header, rows))
Beispiel #2
0
    def __init__(self):
        """ Initialize web context. """
        self._lib_dir_path = os.environ.get('CC_LIB_DIR', '')
        self._data_files_dir_path = os.environ.get('CC_DATA_FILES_DIR', '')

        lcfg_dict = self.__get_package_layout()
        self.pckg_layout = lcfg_dict['runtime']

        # Use this environment variable for testing purposes only. This
        # variable helps to configure which labels to use in this context.
        labels_dir = os.path.join(self._data_files_dir_path, 'config',
                                  'labels')
        if 'CC_TEST_LABELS_DIR' in os.environ:
            labels_dir = os.environ['CC_TEST_LABELS_DIR']

        self._checker_labels = CheckerLabels(labels_dir)
        self.__system_comment_map = \
            load_json_or_empty(self.system_comment_map_file, {})
        self.__git_commit_urls = self.__get_git_commit_urls()
        self.__package_version = None
        self.__package_build_date = None
        self.__package_git_hash = None

        # This should be initialized in command line scripts based on the
        # given CLI options.
        self.codechecker_workspace = None

        self.__set_version()
Beispiel #3
0
def __print_severities(args: argparse.Namespace, cl: CheckerLabels):
    """
    Print checker severities according to the command line arguments to the
    standard output.
    """
    if args.output_format == 'custom':
        args.output_format = 'rows'

    if 'details' in args:
        header = ['Severity', 'Description']
        rows = cl.get_description('severity').items()
    else:
        header = ['Severity']
        rows = [(key, ) for key in cl.get_description('severity')]

    if args.output_format in ['csv', 'json']:
        header = list(map(__uglify, header))

    print(twodim.to_str(args.output_format, header, rows))
Beispiel #4
0
def __print_checkers(args: argparse.Namespace, cl: CheckerLabels):
    """
    Print checkers according to the command line arguments to the standard
    output.
    """
    labels = [args.label] if 'label' in args else []

    if 'profile' in args:
        labels.append(f'profile:{args.profile}')

    if 'guideline' in args:
        labels.append(__guideline_to_label(args, cl))

    if 'severity' in args:
        labels.append(f'severity:{args.severity}')

    checker_info = __get_detailed_checker_info(args, cl)

    result = []
    for analyzer in args.analyzers:
        if labels:
            checkers = cl.checkers_by_labels(labels, analyzer)
            result.extend(
                filter(lambda x: x[1] in checkers, checker_info[analyzer]))
        else:
            result.extend(checker_info[analyzer])

    __post_process_result(result)

    if args.output_format == 'custom':
        if result:
            __print_checkers_custom_format(result)
        else:
            LOG.info('No checkers with the given label found.')
        return

    if args.output_format == 'json':
        __print_checkers_json_format(result, 'details' in args)
        return

    if 'details' in args:
        header = ['Status', 'Name', 'Analyzer', 'Description', 'Labels']
        rows = list(map(__format_row, result))
    else:
        header = ['Name']
        rows = [[r[1]] for r in result]

    if args.output_format in ['csv', 'json']:
        header = list(map(__uglify, header))

    if rows:
        print(twodim.to_str(args.output_format, header, rows))
    else:
        LOG.info('No checkers with the given label found.')
    def __check_serverity_of_reports(self, run_name):
        """
        This will check whether reports in the database has the same severity
        levels as in the labels config file.
        """
        run_filter = RunFilter()
        run_filter.names = [run_name]
        run_filter.exactMatch = True

        runs = self._cc_client.getRunData(run_filter, None, 0, None)
        run_id = runs[0].runId

        reports \
            = self._cc_client.getRunResults([run_id], 10, 0, [], None, None,
                                            False)

        checker_labels = CheckerLabels(self.workspace_labels_dir)
        for report in reports:
            severity_id = checker_labels.severity(report.checkerId)
            self.assertEqual(Severity._VALUES_TO_NAMES[report.severity],
                             severity_id)
Beispiel #6
0
def __print_labels(args: argparse.Namespace, cl: CheckerLabels):
    """
    Print labels according to the command line arguments to the standard
    output.
    """
    if args.output_format == 'custom':
        args.output_format = 'rows'

    header = ['Label']
    if args.output_format in ['csv', 'json']:
        header = list(map(__uglify, header))

    rows = list(map(lambda x: (x, ), cl.labels()))

    print(twodim.to_str(args.output_format, header, rows))
Beispiel #7
0
def __guideline_to_label(args: argparse.Namespace, cl: CheckerLabels) -> str:
    """
    Transforms --guideline parameter as if they were given through --label.
    For example "--guideline sei-cert" is equivalent with
    "--label guideline:sei-cert" and "--guideline sei-cert:str38-c" is the same
    as "--label sei-cert:str38-c".
    """
    guidelines = []
    for analyzer in args.analyzers:
        guidelines.extend(cl.occurring_values('guideline', analyzer))

    if args.guideline in guidelines:
        return f'guideline:{args.guideline}'
    else:
        if args.guideline.find(':') == -1:
            LOG.error('--guideline parameter is either <guideline> or '
                      '<guideline>:<rule>')
            sys.exit(1)
        return args.guideline
Beispiel #8
0
    def __init__(self):
        """ Initialize analyzer context. """
        self._bin_dir_path = os.environ.get('CC_BIN_DIR', '')
        self._lib_dir_path = os.environ.get('CC_LIB_DIR', '')
        self._data_files_dir_path = os.environ.get('CC_DATA_FILES_DIR', '')

        # Use this environment variable for testing purposes only. This
        # variable helps to configure which labels to use in this context.
        labels_dir = os.path.join(self._data_files_dir_path, 'config',
                                  'labels')
        if 'CC_TEST_LABELS_DIR' in os.environ:
            labels_dir = os.environ['CC_TEST_LABELS_DIR']

        cfg_dict = self.__get_package_config()
        self.env_vars = cfg_dict['environment_variables']

        lcfg_dict = self.__get_package_layout()
        self.pckg_layout = lcfg_dict['runtime']

        self._checker_labels = CheckerLabels(labels_dir)
        self.__package_version = None
        self.__package_build_date = None
        self.__package_git_hash = None
        self.__analyzers = {}

        self.logger_lib_dir_path = os.path.join(self._data_files_dir_path,
                                                'ld_logger', 'lib')

        if not os.path.exists(self.logger_lib_dir_path):
            self.logger_lib_dir_path = os.path.join(self._lib_dir_path,
                                                    'codechecker_analyzer',
                                                    'ld_logger', 'lib')

        self.logger_bin = None
        self.logger_file = None
        self.logger_compilers = None

        # Init package specific environment variables.
        self.__init_env()

        self.__set_version()
        self.__populate_analyzers()
        self.__populate_replacer()
Beispiel #9
0
def __print_label_values(args: argparse.Namespace, cl: CheckerLabels):
    """
    If --label flag is given an argument which doesn't contain a colon (:) then
    we assume that the user intends to see the available values of that label:
    CodeChecker checkers --label severity
    """
    if args.output_format == 'custom':
        args.output_format = 'rows'

    header = ['Value']
    if args.output_format == 'custom':
        args.output_format = 'rows'

    rows = list(map(lambda x: (x, ), cl.occurring_values(args.label)))

    if rows:
        print(twodim.to_str(args.output_format, header, rows))
    else:
        LOG.info(
            'Label "%s" doesn\'t exist. Use "CodeChecker checkers --label" '
            'command to list available labels.', args.label)
    def test_checker_labels(self):
        cl = CheckerLabels(self.labels_dir.name)

        self.assertEqual(
            sorted(cl.checkers_by_labels(['profile:extreme'])),
            sorted([
                'core.builtin.NoReturnFunctions',
                'bugprone-undelegated-constructor',
                'google-objc-global-variable-declaration', 'cert-err34-c'
            ]))

        self.assertEqual(
            sorted(cl.checkers_by_labels(['profile:extreme',
                                          'severity:HIGH'])),
            sorted([
                'globalChecker', 'globalChecker', 'core.DivideZero',
                'core.NonNullParamChecker', 'core.builtin.NoReturnFunctions',
                'bugprone-undelegated-constructor',
                'google-objc-global-variable-declaration', 'cert-err34-c'
            ]))

        self.assertEqual(
            sorted(
                cl.checkers_by_labels(['profile:extreme', 'severity:HIGH'],
                                      'clangsa')),
            sorted([
                'globalChecker', 'core.DivideZero', 'core.NonNullParamChecker',
                'core.builtin.NoReturnFunctions'
            ]))

        self.assertEqual(cl.label_of_checker('globalChecker', 'severity'),
                         'HIGH')

        self.assertEqual(cl.label_of_checker('globalChecker', 'profile'),
                         ['security'])

        self.assertEqual(
            cl.label_of_checker('bugprone-undelegated-constructor', 'severity',
                                'clang-tidy'), 'MEDIUM')

        self.assertEqual(
            cl.label_of_checker('bugprone-undelegated-constructor', 'severity',
                                'clangsa'), 'UNSPECIFIED')

        self.assertEqual(
            sorted(cl.labels_of_checker('globalChecker')),
            sorted([('profile', 'security'), ('severity', 'HIGH')]))

        self.assertEqual(
            sorted(cl.labels()),
            sorted(['guideline', 'profile', 'sei-cert', 'severity']))

        self.assertEqual(
            sorted(cl.occurring_values('profile')),
            sorted(['default', 'extreme', 'security', 'sensitive']))

        self.assertEqual(cl.severity('bugprone-undelegated-constructor'),
                         'MEDIUM')

        self.assertEqual(
            cl.severity('bugprone-undelegated-constructor', 'clang-tidy'),
            'MEDIUM')

        self.assertEqual(
            cl.severity('bugprone-undelegated-constructor', 'clangsa'),
            'UNSPECIFIED')

        self.assertEqual(
            cl.get_description('profile'), {
                'default': 'Default documentation',
                'sensitive': 'Sensitive documentation',
                'extreme': 'Extreme documentation'
            })

        self.assertEqual(
            sorted(cl.checkers()),
            sorted([
                'globalChecker', 'core.DivideZero', 'core.NonNullParamChecker',
                'core.builtin.NoReturnFunctions', 'globalChecker',
                'bugprone-undelegated-constructor',
                'google-objc-global-variable-declaration', 'cert-err34-c'
            ]))

        self.assertEqual(
            sorted(cl.checkers('clang-tidy')),
            sorted([
                'globalChecker', 'bugprone-undelegated-constructor',
                'google-objc-global-variable-declaration', 'cert-err34-c'
            ]))
Beispiel #11
0
def __get_detailed_checker_info(args: argparse.Namespace,
                                cl: CheckerLabels) -> Dict[str, list]:
    """
    Returns a dictionary which maps analyzer names to the collection of their
    supported checkers. Checker information is described with tuples of this
    information: (status, checker name, analyzer name, description, labels).
    """
    context = analyzer_context.get_context()

    working_analyzers, _ = analyzer_types.check_supported_analyzers(
        analyzer_types.supported_analyzers, context)

    analyzer_config_map = analyzer_types.build_config_handlers(
        args, context, working_analyzers)

    analyzer_environment = env.extend(context.path_env_extra,
                                      context.ld_lib_path_extra)

    checker_info = defaultdict(list)

    for analyzer in working_analyzers:
        config_handler = analyzer_config_map.get(analyzer)
        analyzer_class = analyzer_types.supported_analyzers[analyzer]

        checkers = analyzer_class.get_analyzer_checkers(
            config_handler, analyzer_environment)

        profile_checkers = []
        if 'profile' in args:
            available_profiles = cl.get_description('profile')

            if args.profile not in available_profiles:
                LOG.error("Checker profile '%s' does not exist!", args.profile)
                LOG.error("To list available profiles, use '--profile list'.")
                sys.exit(1)

            profile_checkers.append((f'profile:{args.profile}', True))

        if 'label' in args:
            profile_checkers.extend((label, True) for label in args.label)

        if 'severity' in args:
            profile_checkers.append((f'severity:{args.severity}', True))

        if 'guideline' in args:
            profile_checkers.append((__guideline_to_label(args, cl), True))

        config_handler.initialize_checkers(context, checkers, profile_checkers)

        for checker, (state, description) in config_handler.checks().items():
            # severity = cl.severity(checker)
            # guideline = guideline_rules_for_checker(checker, context)
            # checker_info[analyzer].append(
            #     (state, checker, analyzer, severity, guideline, description))
            checker_info[analyzer].append(
                (state, checker, analyzer, description,
                 sorted(cl.labels_of_checker(checker, analyzer))))

    if 'show_warnings' in args:
        for warning in get_warnings(analyzer_environment):
            warning = 'clang-diagnostic-' + warning
            # guideline = guideline_rules_for_checker(warning, context)
            # checker_info[ClangTidy.ANALYZER_NAME].append(
            #     (CheckerState.default, warning, ClangTidy.ANALYZER_NAME,
            #      'MEDIUM', guideline, ''))
            checker_info[ClangTidy.ANALYZER_NAME].append(
                (CheckerState.default, warning, ClangTidy.ANALYZER_NAME, '',
                 sorted(cl.labels_of_checker(warning,
                                             ClangTidy.ANALYZER_NAME))))

    return checker_info