コード例 #1
0
    def test_cstyle_comment_multi_line_mismatch(self):
        """
        Multi line C style comment start '/*' is in a different line
        from the codechecker review status comment.

        /*
          codechecker_suppress [ my_checker_1 ]
          multi line
          comment
          again
         */
        """

        bug_line = 108
        sc_handler = SourceCodeCommentHandler(self.__tmp_srcfile_1)
        res = sc_handler.has_source_line_comments(bug_line)
        self.assertTrue(res)

        source_line_comments = sc_handler.get_source_line_comments(bug_line)

        for l in source_line_comments:
            print('-======')
            print(l)

        self.assertEqual(len(source_line_comments), 1)

        expected = [{
            'checkers': {'my_checker_1'},
            'message': 'multi line comment again',
            'status': 'false_positive'
        }]

        self.assertDictEqual(expected[0], source_line_comments[0])
コード例 #2
0
    def get_suppressed_reports(reports):
        """
        Returns suppressed reports.
        """
        suppressed_in_code = []
        for rep in reports:
            bughash = rep.report_hash
            source_file = rep.main['location']['file_name']
            bug_line = rep.main['location']['line']
            checker_name = rep.main['check_name']

            sc_handler = SourceCodeCommentHandler(source_file)
            src_comment_data = sc_handler.filter_source_line_comments(
                bug_line,
                checker_name)

            if len(src_comment_data) == 1:
                suppressed_in_code.append(bughash)
                LOG.debug("Bug " + bughash +
                          "is suppressed in code. file:" + source_file +
                          "Line "+str(bug_line))
            elif len(src_comment_data) > 1:
                LOG.warning(
                    "Multiple source code comment can be found "
                    "for '{0}' checker in '{1}' at line {2}. "
                    "This bug will not be suppressed!".format(
                        checker_name, source_file, bug_line))
        return suppressed_in_code
コード例 #3
0
    def test_multiple_checker_name_comments(self):
        """
        Check multiple comment where same checker name are given for multiple
        source code comment.
        """

        bug_line = 43
        sc_handler = SourceCodeCommentHandler(self.__tmp_srcfile_3)
        res = sc_handler.has_source_line_comments(bug_line)
        self.assertTrue(res)

        source_line_comments = sc_handler.get_source_line_comments(
            bug_line)
        self.assertEqual(len(source_line_comments), 2)

        expected = [{
                        'checkers': {'my.checker_1'},
                        'message': 'intentional comment',
                        'status': 'intentional'
                    },
                    {
                        'checkers': {'my.checker_2', 'my.checker_1'},
                        'message': 'some comment',
                        'status': 'false_positive'
                    }]

        self.assertDictEqual(expected[0], source_line_comments[0])
        self.assertDictEqual(expected[1], source_line_comments[1])

        current_line_comments = \
            sc_handler.filter_source_line_comments(bug_line,
                                                   'my.checker_1')
        self.assertEqual(len(current_line_comments), 2)
コード例 #4
0
    def test_cstyle_comment_multi_nomsg(self):
        """
        Multi line C style comment.
        /* codechecker_suppress [ my_checker_1 ]
        */
        """

        bug_line = 89
        sc_handler = SourceCodeCommentHandler(self.__tmp_srcfile_1)
        res = sc_handler.has_source_line_comments(bug_line)
        self.assertTrue(res)

        source_line_comments = sc_handler.get_source_line_comments(bug_line)

        for l in source_line_comments:
            print(l)

        self.assertEqual(len(source_line_comments), 1)

        expected = [{
            'checkers': {'my_checker_1'},
            'message': 'WARNING! source code comment is missing',
            'status': 'false_positive'
        }]

        self.assertDictEqual(expected[0], source_line_comments[0])
コード例 #5
0
    def test_cstyle_comment(self):
        """
        C style comment in one line.
        /* codechecker_suppress [ my_checker_1 ] suppress comment */
        """

        bug_line = 76
        sc_handler = SourceCodeCommentHandler(self.__tmp_srcfile_1)
        res = sc_handler.has_source_line_comments(bug_line)
        self.assertTrue(res)

        source_line_comments = sc_handler.get_source_line_comments(bug_line)

        for l in source_line_comments:
            print(l)

        self.assertEqual(len(source_line_comments), 1)

        expected = [{
            'checkers': {'my_checker_1'},
            'message': 'suppress comment',
            'status': 'false_positive'
        }]

        self.assertDictEqual(expected[0], source_line_comments[0])
コード例 #6
0
    def test_malformed_commment_format(self):
        """Check malformed comment."""
        bug_line = 1
        sc_handler = SourceCodeCommentHandler(self.__tmp_srcfile_2)
        res = sc_handler.has_source_line_comments(bug_line)
        self.assertFalse(res)

        source_line_comments = sc_handler.get_source_line_comments(bug_line)
        self.assertEqual(len(source_line_comments), 0)
コード例 #7
0
    def test_multi_liner_some_checkers(self):
        """There is source code comment above the bug line."""
        bug_line = 50
        sc_handler = SourceCodeCommentHandler(self.__tmp_srcfile_1)
        res = sc_handler.has_source_line_comments(bug_line)
        self.assertFalse(res)

        source_line_comments = sc_handler.get_source_line_comments(bug_line)
        self.assertEqual(len(source_line_comments), 0)
コード例 #8
0
    def test_no_comment(self):
        """There is no comment above the bug line."""
        bug_line = 9
        sc_handler = SourceCodeCommentHandler(self.__tmp_srcfile_1)
        res = sc_handler.has_source_line_comments(bug_line)
        self.assertFalse(res)

        source_line_comments = sc_handler.get_source_line_comments(bug_line)
        self.assertEqual(len(source_line_comments), 0)
コード例 #9
0
    def test_src_comment_first_line(self):
        """Bug is reported for the first line."""
        bug_line = 3
        sc_handler = SourceCodeCommentHandler(self.__tmp_srcfile_1)
        res = sc_handler.has_source_line_comments(bug_line)
        self.assertFalse(res)

        source_line_comments = sc_handler.get_source_line_comments(bug_line)
        self.assertEqual(len(source_line_comments), 0)
コード例 #10
0
    def test_confirmed_comment(self):
        """Check Confirmed comment."""
        bug_line = 17
        sc_handler = SourceCodeCommentHandler(self.__tmp_srcfile_3)
        res = sc_handler.has_source_line_comments(bug_line)
        self.assertTrue(res)

        source_line_comments = sc_handler.get_source_line_comments(bug_line)
        self.assertEqual(len(source_line_comments), 1)

        expected = {'checkers': {'all'},
                    'message': 'some comment',
                    'status': 'confirmed'}
        self.assertDictEqual(expected, source_line_comments[0])
コード例 #11
0
    def test_fancy_comment_characters(self):
        """Check fancy comment."""
        bug_line = 64
        sc_handler = SourceCodeCommentHandler(self.__tmp_srcfile_1)
        res = sc_handler.has_source_line_comments(bug_line)
        self.assertTrue(res)

        source_line_comments = sc_handler.get_source_line_comments(bug_line)
        self.assertEqual(len(source_line_comments), 1)

        expected = {'checkers': {'my_checker_1'},
                    'message': "áúőóüöáé ▬▬▬▬▬▬▬▬▬▬ஜ۩۞۩ஜ▬▬▬▬▬▬▬▬▬▬",
                    'status': 'false_positive'}
        self.assertDictEqual(expected, source_line_comments[0])
コード例 #12
0
    def test_comment_characters(self):
        """Check for different special comment characters."""
        bug_line = 57
        sc_handler = SourceCodeCommentHandler(self.__tmp_srcfile_1)
        res = sc_handler.has_source_line_comments(bug_line)
        self.assertTrue(res)

        source_line_comments = sc_handler.get_source_line_comments(bug_line)
        self.assertEqual(len(source_line_comments), 1)

        expected = {'checkers': {'my.checker_1', 'my.checker_2'},
                    'message': "i/';0 (*&^%$#@!)",
                    'status': 'false_positive'}
        self.assertDictEqual(expected, source_line_comments[0])
コード例 #13
0
    def test_no_fancy_comment(self):
        """Check no fancy comment."""
        bug_line = 70
        sc_handler = SourceCodeCommentHandler(self.__tmp_srcfile_1)
        res = sc_handler.has_source_line_comments(bug_line)
        self.assertTrue(res)

        source_line_comments = sc_handler.get_source_line_comments(bug_line)
        self.assertEqual(len(source_line_comments), 1)

        expected = {'checkers': {'my_checker_1'},
                    'message': 'WARNING! source code comment is missing',
                    'status': 'false_positive'}
        self.assertDictEqual(expected, source_line_comments[0])
コード例 #14
0
    def test_multi_liner_all_2(self):
        """There is source code comment above the bug line."""
        bug_line = 36
        sc_handler = SourceCodeCommentHandler(self.__tmp_srcfile_1)
        res = sc_handler.has_source_line_comments(bug_line)
        self.assertTrue(res)

        source_line_comments = sc_handler.get_source_line_comments(bug_line)
        self.assertEqual(len(source_line_comments), 1)

        expected = {'checkers': {'my.checker_1', 'my.checker_2'},
                    'message': 'some really long comment',
                    'status': 'false_positive'}
        self.assertDictEqual(expected, source_line_comments[0])
コード例 #15
0
    def test_no_src_comment_comment(self):
        """There is no source comment above the bug line."""
        bug_line = 16
        sc_handler = SourceCodeCommentHandler(self.__tmp_srcfile_1)
        res = sc_handler.has_source_line_comments(bug_line)
        self.assertTrue(res)

        source_line_comments = sc_handler.get_source_line_comments(bug_line)
        self.assertEqual(len(source_line_comments), 1)

        expected = {'checkers': {'all'},
                    'message': 'some comment',
                    'status': 'false_positive'}
        self.assertDictEqual(expected, source_line_comments[0])
コード例 #16
0
    def get_diff_report_dir(client, baseids, report_dir, cmp_data):
        filtered_reports = []
        report_dir_results = get_report_dir_results(report_dir)
        new_hashes = {}
        suppressed_in_code = []

        for rep in report_dir_results:
            bughash = rep.main['issue_hash_content_of_line_in_context']
            source_file = rep.main['location']['file_name']
            bug_line = rep.main['location']['line']
            checker_name = rep.main['check_name']

            new_hashes[bughash] = rep
            sc_handler = SourceCodeCommentHandler(source_file)
            src_comment_data = sc_handler.filter_source_line_comments(
                bug_line, checker_name)

            if len(src_comment_data) == 1:
                suppressed_in_code.append(bughash)
                LOG.debug("Bug " + bughash + "is suppressed in code. file:" +
                          source_file + "Line " + str(bug_line))
            elif len(src_comment_data) > 1:
                LOG.warning("Multiple source code comment can be found "
                            "for '{0}' checker in '{1}' at line {2}. "
                            "This bug will not be suppressed!".format(
                                checker_name, source_file, bug_line))

        base_hashes = client.getDiffResultsHash(baseids, new_hashes.keys(),
                                                cmp_data.diffType)

        if cmp_data.diffType == ttypes.DiffType.NEW or \
           cmp_data.diffType == ttypes.DiffType.UNRESOLVED:
            # Shows reports from the report dir which are not present in the
            # baseline (NEW reports) or appear in both side (UNRESOLVED
            # reports) and not suppressed in the code.
            for result in report_dir_results:
                h = result.main['issue_hash_content_of_line_in_context']
                if h in base_hashes and h not in suppressed_in_code:
                    filtered_reports.append(result)
        elif cmp_data.diffType == ttypes.DiffType.RESOLVED:
            # Show bugs in the baseline (server) which are not present in the
            # report dir or suppressed.
            results = get_diff_base_results(client, baseids, base_hashes,
                                            suppressed_in_code)
            for result in results:
                filtered_reports.append(result)

        return filtered_reports
コード例 #17
0
    def test_cstyle_multi_comment_multi_line(self):
        """
        Multi line C style comment with multiple review status comment.

        /* codechecker_false_positive [ my.checker_2, my.checker_1 ] comment
        codechecker_intentional [ my.checker_1 ] intentional comment */

        """

        bug_line = 49
        sc_handler = SourceCodeCommentHandler(self.__tmp_srcfile_3)
        res = sc_handler.has_source_line_comments(bug_line)
        self.assertTrue(res)

        source_line_comments = sc_handler.get_source_line_comments(
            bug_line)

        for l in source_line_comments:
            print(l)

        self.assertEqual(len(source_line_comments), 2)

        expected = [{
                        'checkers': {'my.checker_1'},
                        'message': 'intentional comment',
                        'status': 'intentional'},
                    {
                        'checkers': {'my.checker_1', 'my.checker_2'},
                        'message': 'some comment',
                        'status': 'false_positive'
                    }]

        self.assertDictEqual(expected[0], source_line_comments[0])
        self.assertDictEqual(expected[1], source_line_comments[1])

        current_line_comments = \
            sc_handler.filter_source_line_comments(bug_line, 'my.checker_1')
        self.assertEqual(len(current_line_comments), 2)
        self.assertEqual(current_line_comments[0]['message'],
                         expected[0]['message'])
        self.assertEqual(current_line_comments[0]['status'],
                         expected[0]['status'])
        self.assertEqual(current_line_comments[1]['message'],
                         expected[1]['message'])
        self.assertEqual(current_line_comments[1]['status'],
                         expected[1]['status'])

        current_line_comments = \
            sc_handler.filter_source_line_comments(bug_line, 'my.checker_2')
        self.assertEqual(len(current_line_comments), 1)

        self.assertEqual(current_line_comments[0]['message'],
                         expected[1]['message'])
        self.assertEqual(current_line_comments[0]['status'],
                         expected[1]['status'])

        current_line_comments = \
            sc_handler.filter_source_line_comments(bug_line, 'my.dummy')
        self.assertEqual(len(current_line_comments), 0)
コード例 #18
0
def skip_report(report_hash, source_file, report_line, checker_name,
                src_comment_handler=None):
    """
    Returns True if the report was suppressed in the source code, otherwise
    False.
    """
    bug = {'hash_value': report_hash, 'file_path': source_file}
    if src_comment_handler and src_comment_handler.get_suppressed(bug):
        LOG.debug("Suppressed by suppress file: %s:%s [%s] %s", source_file,
                  report_line, checker_name, report_hash)
        return True

    sc_handler = SourceCodeCommentHandler(source_file)

    # Check for source code comment.
    src_comment_data = sc_handler.filter_source_line_comments(
        report_line,
        checker_name)

    if len(src_comment_data) == 1:
        status = src_comment_data[0]['status']

        LOG.debug("Suppressed by source code comment.")
        if src_comment_handler:
            file_name = os.path.basename(source_file)
            message = src_comment_data[0]['message']
            src_comment_handler.store_suppress_bug_id(
                report_hash,
                file_name,
                message,
                status)

        if skip_suppress_status(status):
            return True

    elif len(src_comment_data) > 1:
        LOG.warning("Multiple source code comment can be found "
                    "for '{0}' checker in '{1}' at line {2}. "
                    "This bug will not be suppressed!".format(
                        checker_name, source_file, report_line))
    return False
コード例 #19
0
    def test_multiple_multi_line_comments(self):
        """Check multi line long line comments."""
        bug_line = 31
        sc_handler = SourceCodeCommentHandler(self.__tmp_srcfile_3)
        res = sc_handler.has_source_line_comments(bug_line)
        self.assertTrue(res)

        source_line_comments = sc_handler.get_source_line_comments(bug_line)
        self.assertEqual(len(source_line_comments), 2)

        expected = [{
                        'checkers': {'my.checker_1'},
                        'message': 'long intentional bug comment',
                        'status': 'intentional'},
                    {
                        'checkers': {'my.checker_2'},
                        'message': 'long confirmed bug comment',
                        'status': 'confirmed'
                    }]

        self.assertDictEqual(expected[0], source_line_comments[0])
        self.assertDictEqual(expected[1], source_line_comments[1])
コード例 #20
0
    def test_multiple_all_comments(self):
        """Check multiple comment."""
        bug_line = 37
        sc_handler = SourceCodeCommentHandler(self.__tmp_srcfile_3)
        res = sc_handler.has_source_line_comments(bug_line)
        self.assertTrue(res)

        source_line_comments = sc_handler.get_source_line_comments(bug_line)
        self.assertEqual(len(source_line_comments), 2)

        expected = [{
                        'checkers': {'my.checker_1'},
                        'message': 'intentional comment',
                        'status': 'intentional'},
                    {
                        'checkers': {'all'},
                        'message': 'some comment',
                        'status': 'false_positive'
                    }]

        self.assertDictEqual(expected[0], source_line_comments[0])
        self.assertDictEqual(expected[1], source_line_comments[1])

        current_line_comments = \
            sc_handler.filter_source_line_comments(bug_line, 'my.checker_1')
        self.assertEqual(len(current_line_comments), 1)

        self.assertEqual(current_line_comments[0]['message'],
                         expected[0]['message'])
        self.assertEqual(current_line_comments[0]['status'],
                         expected[0]['status'])

        current_line_comments = \
            sc_handler.filter_source_line_comments(bug_line, '')
        self.assertEqual(len(current_line_comments), 1)
        self.assertEqual(current_line_comments[0]['message'],
                         expected[1]['message'])
        self.assertEqual(current_line_comments[0]['status'],
                         expected[1]['status'])

        current_line_comments = \
            sc_handler.filter_source_line_comments(bug_line, 'my.dummy')
        self.assertEqual(len(current_line_comments), 1)

        self.assertEqual(len(current_line_comments), 1)
        self.assertEqual(current_line_comments[0]['message'],
                         expected[1]['message'])
        self.assertEqual(current_line_comments[0]['status'],
                         expected[1]['status'])
コード例 #21
0
ファイル: plist_parser.py プロジェクト: emoonoh/codechecker
    def write(self, files, reports, analyzed_source_file, output=sys.stdout):
        """
        Format an already parsed plist report file to a more
        human readable format.
        The formatted text is written to the output.
        During writing the output statistics are collected.

        Write out the bugs to the output and collect report statistics.
        """

        severity_stats = defaultdict(int)
        file_stats = defaultdict(int)
        report_count = defaultdict(int)

        report_num = len(reports)
        if report_num > 0:
            index_format = '    %%%dd, ' % \
                           int(math.floor(math.log10(report_num)) + 1)

        non_suppressed = 0
        for report in reports:
            events = [i for i in report.bug_path if i.get('kind') == 'event']
            f_path = files[events[-1]['location']['file']]
            if self.skiplist_handler and \
                    self.skiplist_handler.should_skip(f_path):
                LOG.debug(report + ' is skipped (in ' + f_path + ")")
                continue
            hash_value = report.main['issue_hash_content_of_line_in_context']
            bug = {'hash_value': hash_value,
                   'file_path': f_path}

            if self.src_comment_handler and \
                    self.src_comment_handler.get_suppressed(bug):
                LOG.debug("Suppressed by suppress file: {0}".format(report))
                continue

            last_report_event = report.bug_path[-1]
            source_file = files[last_report_event['location']['file']]
            report_line = last_report_event['location']['line']
            report_hash = report.main['issue_hash_content_of_line_in_context']
            checker_name = report.main['check_name']
            sc_handler = SourceCodeCommentHandler(source_file)

            # Check for source code comment.
            src_comment_data = sc_handler.filter_source_line_comments(
                report_line,
                checker_name)

            if len(src_comment_data) == 1:
                LOG.debug("Suppressed by source code comment.")
                if self.src_comment_handler:
                    file_name = os.path.basename(source_file)
                    message = src_comment_data[0]['message']
                    status = src_comment_data[0]['status']
                    self.src_comment_handler.store_suppress_bug_id(
                        report_hash,
                        file_name,
                        message,
                        status)
                continue
            elif len(src_comment_data) > 1:
                LOG.warning("Multiple source code comment can be found "
                            "for '{0}' checker in '{1}' at line {2}. "
                            "This bug will not be suppressed!".format(
                                checker_name, source_file, report_line))

            file_stats[f_path] += 1
            severity = self.__severity_map.get(checker_name,
                                               'UNSPECIFIED')
            severity_stats[severity] += 1
            report_count["report_count"] += 1

            output.write(self.__format_bug_event(checker_name,
                                                 severity,
                                                 last_report_event,
                                                 source_file))
            output.write('\n')
            output.write(self.__format_location(last_report_event,
                                                source_file))
            output.write('\n')

            if self.print_steps:
                output.write('  Report hash: ' + report_hash + '\n')
                output.write('  Steps:\n')
                for index, event in enumerate(events):
                    output.write(index_format % (index + 1))
                    source_file = files[event['location']['file']]
                    output.write(self.__format_bug_event(None,
                                                         None,
                                                         event,
                                                         source_file))
                    output.write('\n')
            output.write('\n')

            non_suppressed += 1

        basefile_print = (' ' +
                          os.path.basename(analyzed_source_file)) \
            if analyzed_source_file and \
            len(analyzed_source_file) > 0 else ''

        if non_suppressed == 0:
            output.write('Found no defects while analyzing%s\n' %
                         (basefile_print))
        else:
            output.write(
                'Found %d defect(s) while analyzing%s\n\n' %
                (non_suppressed, basefile_print))

        return {"severity": severity_stats,
                "files": file_stats,
                "reports": report_count}