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)
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'])
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)
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
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
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
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}