Arkanjo 0.1
A tool for find code duplicated functions in codebases
Loading...
Searching...
No Matches
similarity_explorer.cpp
Go to the documentation of this file.
1/*
2This file expect the output of parser.cpp
3This code filter the output only printing the files similar to files that contains a given pattern passed as argc
4The code filter every file that has the pattern as a substring, so be carefull with duplications
5*/
6
7#include <bits/stdc++.h>
8
10
11Utils::COLOR Similarity_Explorer::choose_text_color(){
13 if(processed_results%2 == 0){
15 }
16 return ret;
17}
18
19int Similarity_Explorer::find_number_pairs_show(int number_pair_found){
20 if(limit_on_results == UNLIMITED_RESULTS){
21 return number_pair_found;
22 }
23 return min(limit_on_results,number_pair_found);
24}
25
26string Similarity_Explorer::format_initial_message(int number_pair_found){
27 string ret;
28 ret += INITIAL_TEXT_PRINT_1;
29 ret += to_string(number_pair_found);
30 ret += INITIAL_TEXT_PRINT_2;
31 ret += to_string(find_number_pairs_show(number_pair_found));
32 ret += INITIAL_TEXT_PRINT_3;
33 return ret;
34}
35
36bool Similarity_Explorer::match_pattern(Path path1, Path path2){
37 bool match1 = path1.contains_given_pattern(pattern_to_match);
38 bool match2 = path2.contains_given_pattern(pattern_to_match);
39
40 if(both_path_need_to_match_pattern){
41 return match1 && match2;
42 }
43 return match1 || match2;
44}
45
46string Similarity_Explorer::format_path_message_in_pair(Path path){
47 string ret = path.build_relative_path() + BETWEEN_RELATIVE_AND_FUNCTION_NAME + path.build_function_name();
48 return ret;
49}
50
51int Similarity_Explorer::find_number_lines(Path path1){
52 Function function(path1);
53 return function.number_of_lines();
54}
55
56void Similarity_Explorer::print_similar_path_pair(Path path1, Path path2){
57 string line;
58 line += START_LINE_COMPARATION_PRINT;
59 line += format_path_message_in_pair(path1);
60 line += BETWEEN_TWO_FUNCTION;
61 line += format_path_message_in_pair(path2);
62 line += NUMBER_LINES_MESSAGE;
63 line += to_string(find_number_lines(path1));
64
65 Utils::COLOR color = choose_text_color();
66 cout << Utils::format_colored_message(line,color) << '\n';
67}
68
69void Similarity_Explorer::process_similar_path_pair(Path path1, Path path2){
70 if(!match_pattern(path1,path2)){
71 return;
72 }
73 if(limit_on_results != UNLIMITED_RESULTS && processed_results >= limit_on_results){
74 return;
75 }
76 processed_results++;
77 print_similar_path_pair(path1,path2);
78}
79
80int Similarity_Explorer::find_number_pair_found(vector<pair<Path,Path>> similar_path_pairs){
81 int count = 0;
82 for(auto [path1, path2] : similar_path_pairs){
83 if(match_pattern(path1,path2)){
84 count++;
85 }
86 }
87 return count;
88}
89
90vector<pair<Path,Path>> Similarity_Explorer::build_similar_path_pairs(bool sorted_by_number_of_duplicated_code){
91 vector<pair<Path,Path>> similar_path_pairs;
92 if(sorted_by_number_of_duplicated_code){
93 similar_path_pairs = similarity_table->get_all_similar_path_pairs_sorted_by_line_number();
94 }else{
95 similar_path_pairs = similarity_table->get_all_similar_path_pairs_sorted_by_similarity();
96 }
97 return similar_path_pairs;
98}
99
100void Similarity_Explorer::explorer(bool sorted_by_number_of_duplicated_code){
101 vector<pair<Path,Path>> similar_path_pairs = build_similar_path_pairs(sorted_by_number_of_duplicated_code);
102 string initial_line = format_initial_message(find_number_pair_found(similar_path_pairs));
103
104 cout << initial_line << '\n';
105 cout << Utils::LIMITER_PRINT << '\n';
106
107 for(auto [path1, path2] : similar_path_pairs){
108 process_similar_path_pair(path1,path2);
109 }
110}
111
113 int _limit_on_results,
114 string _pattern_to_match,
115 bool _both_path_need_to_match,
116 bool sorted_by_number_of_duplicated_code){
117 similarity_table = _similarity_table;
118 limit_on_results = _limit_on_results;
119 pattern_to_match = _pattern_to_match;
120 both_path_need_to_match_pattern = _both_path_need_to_match;
121 explorer(sorted_by_number_of_duplicated_code);
122}
Path manipulation class for tool-specific directory structure.
Definition path.hpp:24
bool contains_given_pattern(string pattern)
Checks for pattern in path.
Definition path.cpp:151
string build_relative_path()
Builds relative path portion.
Definition path.cpp:112
string build_function_name()
Extracts function name from path.
Definition path.cpp:121
Similarity_Explorer(Similarity_Table *_similarity_table, int _limit_on_results, string _pattern_to_match, bool _both_path_need_to_match, bool sorted_by_number_of_duplicated_code=false)
Constructs explorer with configuration.
int UNLIMITED_RESULTS
Constant for unlimited results display.
Manages and analyzes function similarity relationships.
return ret
Definition sum.c:3
COLOR
Enumeration of available colors for formatted messages.
Definition utils.hpp:94
@ GRAY
Gray color.
Definition utils.hpp:102
@ CYAN
Cyan color.
Definition utils.hpp:101
string format_colored_message(string message, COLOR color)
Formats a message with ANSI color codes.
Definition utils.cpp:85
const string LIMITER_PRINT
Constant string used as a visual delimiter/separator in prints.
Definition utils.hpp:24
Duplicate function exploration interface.