Beispiel #1
0
    def __set_up_test_dir(self, project_path):
        self.test_dir = project.path(project_path)

        # Get if clang is CTU-capable or not.
        cmd = [self._codechecker_cmd, 'analyze', '-h']
        output, _ = call_command(cmd, cwd=self.test_dir, env=self.env)
        self.ctu_capable = '--ctu-' in output
        print("'analyze' reported CTU-compatibility? " + str(self.ctu_capable))

        self.ctu_has_analyzer_display_ctu_progress = \
            host_check.has_analyzer_feature(self.__getClangSaPath(),
                                            '-analyzer-display-ctu-progress')
        print("Has -analyzer-display-ctu-progress? " +
              str(self.ctu_has_analyzer_display_ctu_progress))

        # Fix the "template" build JSONs to contain a proper directory
        # so the tests work.
        raw_buildlog = os.path.join(self.test_dir, 'buildlog.json')
        with open(raw_buildlog) as log_file:
            build_json = json.load(log_file)
            for command in build_json:
                command['directory'] = self.test_dir

        self.__old_pwd = os.getcwd()
        os.chdir(self.test_workspace)
        self.buildlog = os.path.join(self.test_workspace, 'buildlog.json')
        with open(self.buildlog, 'w') as log_file:
            json.dump(build_json, log_file)
Beispiel #2
0
def __build_clangsa_config_handler(args, context):
    """
    Build the config handler for clang static analyzer.
    Handle config options from the command line and config files.
    """

    config_handler = config_handler_clangsa.ClangSAConfigHandler()
    config_handler.analyzer_plugins_dir = context.checker_plugin
    config_handler.analyzer_binary = context.analyzer_binaries.get(CLANG_SA)
    config_handler.compiler_resource_dir =\
        __get_compiler_resource_dir(context, config_handler.analyzer_binary)

    check_env = analyzer_env.get_check_env(context.path_env_extra,
                                           context.ld_lib_path_extra)

    if 'ctu_phases' in args:
        config_handler.ctu_dir = os.path.join(args.output_path, args.ctu_dir)

        config_handler.ctu_has_analyzer_display_ctu_progress = \
            host_check.has_analyzer_feature(
                context.analyzer_binaries.get(CLANG_SA),
                '-analyzer-display-ctu-progress',
                check_env)
        config_handler.log_file = args.logfile
        config_handler.path_env_extra = context.path_env_extra
        config_handler.ld_lib_path_extra = context.ld_lib_path_extra

    try:
        with open(args.clangsa_args_cfg_file, 'rb') as sa_cfg:
            config_handler.analyzer_extra_arguments = \
                re.sub(r'\$\((.*?)\)',
                       __replace_env_var(args.clangsa_args_cfg_file),
                       sa_cfg.read().strip())
    except IOError as ioerr:
        LOG.debug_analyzer(ioerr)
    except AttributeError as aerr:
        # No clangsa arguments file was given in the command line.
        LOG.debug_analyzer(aerr)

    analyzer = construct_analyzer_type(CLANG_SA, config_handler, None)

    checkers = analyzer.get_analyzer_checkers(config_handler, check_env)

    # Read clang-sa checkers from the config file.
    clang_sa_checkers = context.checker_config.get(CLANG_SA + '_checkers')

    try:
        cmdline_checkers = args.ordered_checkers
    except AttributeError:
        LOG.debug_analyzer('No checkers were defined in '
                           'the command line for ' + CLANG_SA)
        cmdline_checkers = None

    initialize_checkers(config_handler, context.available_profiles,
                        context.package_root, checkers, clang_sa_checkers,
                        cmdline_checkers, 'enable_all' in args
                        and args.enable_all)

    return config_handler
Beispiel #3
0
    def construct_config_handler(cls, args, context):
        handler = config_handler_clangsa.ClangSAConfigHandler()
        handler.analyzer_plugins_dir = context.checker_plugin
        handler.analyzer_binary = context.analyzer_binaries.get(
            cls.ANALYZER_NAME)
        handler.compiler_resource_dir = \
            host_check.get_resource_dir(handler.analyzer_binary, context)

        handler.report_hash = args.report_hash \
            if 'report_hash' in args else None

        check_env = analyzer_env.get_check_env(context.path_env_extra,
                                               context.ld_lib_path_extra)

        if 'ctu_phases' in args:
            handler.ctu_dir = os.path.join(args.output_path, args.ctu_dir)

            handler.ctu_has_analyzer_display_ctu_progress = \
                host_check.has_analyzer_feature(
                    context.analyzer_binaries.get(cls.ANALYZER_NAME),
                    '-analyzer-display-ctu-progress',
                    check_env)
            handler.log_file = args.logfile
            handler.path_env_extra = context.path_env_extra
            handler.ld_lib_path_extra = context.ld_lib_path_extra

        try:
            with open(args.clangsa_args_cfg_file, 'rb') as sa_cfg:
                handler.analyzer_extra_arguments = \
                    re.sub(r'\$\((.*?)\)',
                           replace_env_var(args.clangsa_args_cfg_file),
                           sa_cfg.read().strip())
        except IOError as ioerr:
            LOG.debug_analyzer(ioerr)
        except AttributeError as aerr:
            # No clangsa arguments file was given in the command line.
            LOG.debug_analyzer(aerr)

        checkers = ClangSA.get_analyzer_checkers(handler, check_env)

        # Read clang-sa checkers from the config file.
        clang_sa_checkers = context.checker_config.get(cls.ANALYZER_NAME +
                                                       '_checkers')

        try:
            cmdline_checkers = args.ordered_checkers
        except AttributeError:
            LOG.debug_analyzer('No checkers were defined in '
                               'the command line for ' + cls.ANALYZER_NAME)
            cmdline_checkers = None

        handler.initialize_checkers(context.available_profiles,
                                    context.package_root, checkers,
                                    clang_sa_checkers, cmdline_checkers,
                                    'enable_all' in args and args.enable_all)

        return handler
Beispiel #4
0
    def setUp(self):
        """ Set up workspace."""

        # TEST_WORKSPACE is automatically set by test package __init__.py .
        self.test_workspace = os.environ['TEST_WORKSPACE']

        test_class = self.__class__.__name__
        print('Running ' + test_class + ' tests in ' + self.test_workspace)

        # Get the CodeChecker cmd if needed for the tests.
        self._codechecker_cmd = env.codechecker_cmd()
        self.env = env.codechecker_env()
        self.report_dir = os.path.join(self.test_workspace, 'reports')
        os.makedirs(self.report_dir)

        self.test_dir = project.path('ctu_failure')

        # Get if clang is CTU-capable or not.
        cmd = [self._codechecker_cmd, 'analyze', '-h']
        output = subprocess.check_output(cmd, stderr=subprocess.STDOUT,
                                         cwd=self.test_dir, env=self.env)
        self.ctu_capable = '--ctu-' in output
        print("'analyze' reported CTU-compatibility? " + str(self.ctu_capable))

        self.ctu_has_analyzer_display_ctu_progress = \
            host_check.has_analyzer_feature(self.__getClangSaPath(),
                                            '-analyzer-display-ctu-progress')
        print("Has -analyzer-display-ctu-progress? " + str(self.ctu_capable))

        # Fix the "template" build JSONs to contain a proper directory
        # so the tests work.
        raw_buildlog = os.path.join(self.test_dir, 'buildlog.json')
        with open(raw_buildlog) as log_file:
            build_json = json.load(log_file)
            for command in build_json:
                command['directory'] = self.test_dir

        os.chdir(self.test_workspace)
        self.buildlog = os.path.join(self.test_workspace, 'buildlog.json')
        with open(self.buildlog, 'w') as log_file:
            json.dump(build_json, log_file)
Beispiel #5
0
 def test_existing_feature(self):
     self.assertEqual(
         hc.has_analyzer_feature("clang",
                                 "-analyzer-display-progress"),
         True)
Beispiel #6
0
 def test_non_existent_binary_throws(self):
     with self.assertRaises(OSError):
         hc.has_analyzer_feature("non-existent-binary-Yg4pEna5P7",
                                 "")
Beispiel #7
0
 def test_non_existing_feature(self):
     self.assertEqual(
         hc.has_analyzer_feature("clang",
                                 "-non-existent-feature"),
         False)