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 <iostream>
8#include <string>
9#include <vector>
10#include <utility>
11#include <algorithm>
12
14
15Utils::COLOR Similarity_Explorer::choose_text_color(){
17 if(processed_results%2 == 0){
19 }
20 return ret;
21}
22
23int Similarity_Explorer::find_number_pairs_show(int number_pair_found){
24 if(limit_on_results == UNLIMITED_RESULTS){
25 return number_pair_found;
26 }
27 return min(limit_on_results,number_pair_found);
28}
29
30string Similarity_Explorer::format_initial_message(int number_pair_found){
31 string ret;
32 ret += INITIAL_TEXT_PRINT_1;
33 ret += to_string(number_pair_found);
34 ret += INITIAL_TEXT_PRINT_2;
35 ret += to_string(find_number_pairs_show(number_pair_found));
36 ret += INITIAL_TEXT_PRINT_3;
37 return ret;
38}
39
40bool Similarity_Explorer::match_pattern(Path path1, Path path2){
41 bool match1 = path1.contains_given_pattern(pattern_to_match);
42 bool match2 = path2.contains_given_pattern(pattern_to_match);
43
44 if(both_path_need_to_match_pattern){
45 return match1 && match2;
46 }
47 return match1 || match2;
48}
49
50string Similarity_Explorer::format_path_message_in_pair(Path path){
51 string ret = path.build_relative_path() + BETWEEN_RELATIVE_AND_FUNCTION_NAME + path.build_function_name();
52 return ret;
53}
54
55int Similarity_Explorer::find_number_lines(Path path1){
56 Function function(path1);
57 return function.number_of_lines();
58}
59
60void Similarity_Explorer::print_similar_path_pair(Path path1, Path path2){
61 string line;
62 line += START_LINE_COMPARATION_PRINT;
63 line += format_path_message_in_pair(path1);
64 line += BETWEEN_TWO_FUNCTION;
65 line += format_path_message_in_pair(path2);
66 line += NUMBER_LINES_MESSAGE;
67 line += to_string(find_number_lines(path1));
68
69 Utils::COLOR color = choose_text_color();
70 cout << Utils::format_colored_message(line,color) << '\n';
71}
72
73void Similarity_Explorer::process_similar_path_pair(Path path1, Path path2){
74 if(!match_pattern(path1,path2)){
75 return;
76 }
77 if(limit_on_results != UNLIMITED_RESULTS && processed_results >= limit_on_results){
78 return;
79 }
80 processed_results++;
81 print_similar_path_pair(path1,path2);
82}
83
84int Similarity_Explorer::find_number_pair_found(vector<pair<Path,Path>> similar_path_pairs){
85 int count = 0;
86 for(auto [path1, path2] : similar_path_pairs){
87 if(match_pattern(path1,path2)){
88 count++;
89 }
90 }
91 return count;
92}
93
94vector<pair<Path,Path>> Similarity_Explorer::build_similar_path_pairs(bool sorted_by_number_of_duplicated_code){
95 vector<pair<Path,Path>> similar_path_pairs;
96 if(sorted_by_number_of_duplicated_code){
97 similar_path_pairs = similarity_table->get_all_similar_path_pairs_sorted_by_line_number();
98 }else{
99 similar_path_pairs = similarity_table->get_all_similar_path_pairs_sorted_by_similarity();
100 }
101 return similar_path_pairs;
102}
103
104void Similarity_Explorer::explorer(bool sorted_by_number_of_duplicated_code){
105 vector<pair<Path,Path>> similar_path_pairs = build_similar_path_pairs(sorted_by_number_of_duplicated_code);
106 string initial_line = format_initial_message(find_number_pair_found(similar_path_pairs));
107
108 cout << initial_line << '\n';
109 cout << Utils::LIMITER_PRINT << '\n';
110
111 for(auto [path1, path2] : similar_path_pairs){
112 process_similar_path_pair(path1,path2);
113 }
114}
115
117 int _limit_on_results,
118 string _pattern_to_match,
119 bool _both_path_need_to_match,
120 bool sorted_by_number_of_duplicated_code){
121 similarity_table = _similarity_table;
122 limit_on_results = _limit_on_results;
123 pattern_to_match = _pattern_to_match;
124 both_path_need_to_match_pattern = _both_path_need_to_match;
125 explorer(sorted_by_number_of_duplicated_code);
126}
Represents a code function with its content and metadata.
Definition function.hpp:33
Path manipulation class for tool-specific directory structure.
Definition path.hpp:27
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.
vector< pair< Path, Path > > get_all_similar_path_pairs_sorted_by_line_number()
Gets all similar path pairs, sorted by line count.
vector< pair< Path, Path > > get_all_similar_path_pairs_sorted_by_similarity()
Gets all similar path pairs, sorted by similarity.
return ret
Definition sum.c:3
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values
Definition json.hpp:25317
COLOR
Enumeration of available colors for formatted messages.
Definition utils.hpp:103
@ GRAY
Gray color.
Definition utils.hpp:111
@ CYAN
Cyan color.
Definition utils.hpp:110
string format_colored_message(string message, COLOR color)
Formats a message with ANSI color codes.
Definition utils.cpp:84
const string LIMITER_PRINT
Constant string used as a visual delimiter/separator in prints.
Definition utils.hpp:33
Duplicate function exploration interface.