|
1
|
|
|
from bears.c_languages.codeclone_detection.ClangFunctionDifferenceBear import ( |
|
2
|
|
|
ClangFunctionDifferenceBear) |
|
3
|
|
|
from coalib.bears.GlobalBear import GlobalBear |
|
4
|
|
|
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY |
|
5
|
|
|
from coalib.results.Result import Result |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class ClangCloneDetectionBear(GlobalBear): |
|
9
|
|
|
def run(self, |
|
10
|
|
|
dependency_results: dict, |
|
11
|
|
|
max_clone_difference: float=0.185): |
|
12
|
|
|
''' |
|
13
|
|
|
Checks the given code for similar functions that are probably |
|
14
|
|
|
redundant. |
|
15
|
|
|
|
|
16
|
|
|
:param max_clone_difference: The maximum difference a clone should |
|
17
|
|
|
have. |
|
18
|
|
|
''' |
|
19
|
|
|
differences = dependency_results[ |
|
20
|
|
|
ClangFunctionDifferenceBear.__name__][0].contents |
|
21
|
|
|
count_matrices = dependency_results[ |
|
22
|
|
|
ClangFunctionDifferenceBear.__name__][1].contents |
|
23
|
|
|
|
|
24
|
|
|
self.debug("Creating results...") |
|
25
|
|
|
for function_1, function_2, difference in differences: |
|
26
|
|
|
if difference < max_clone_difference: |
|
27
|
|
|
yield Result.from_values( |
|
28
|
|
|
self, |
|
29
|
|
|
"Code clone found. The other occurrence is at file " |
|
30
|
|
|
"{file}, line {line}, function {function}. The " |
|
31
|
|
|
"difference is {difference}.".format( |
|
32
|
|
|
file=function_2[0], |
|
33
|
|
|
line=function_2[1], |
|
34
|
|
|
function=function_2[2], |
|
35
|
|
|
difference=difference), |
|
36
|
|
|
file=function_1[0], |
|
37
|
|
|
severity=RESULT_SEVERITY.MAJOR, |
|
38
|
|
|
line=function_1[1], |
|
39
|
|
|
debug_msg=[count_matrices[function_1], |
|
40
|
|
|
count_matrices[function_2]]) |
|
41
|
|
|
|
|
42
|
|
|
@staticmethod |
|
43
|
|
|
def get_dependencies(): |
|
44
|
|
|
return [ClangFunctionDifferenceBear] |
|
45
|
|
|
|