ArKanjo 0.5.1
A tool for find code duplicated functions in codebases
counter_duplication_code.cpp
Go to the documentation of this file.
2
4
5std::vector<std::string> CounterDuplicationCode::common_folders(
6 const Path& path1, const Path& path2
7) const {
8 std::vector<std::string> tokens_relative_1;
9 std::vector<std::string> tokens_relative_2;
10
11 for (const auto& part : path1.value())
12 tokens_relative_1.push_back(part.string());
13 for (const auto& part : path2.value())
14 tokens_relative_2.push_back(part.string());
15
16 size_t minimum_size_tokens = std::min(tokens_relative_1.size(), tokens_relative_2.size());
17 std::vector<std::string> common_folders;
18 for (size_t i = 0; i < minimum_size_tokens; i++) {
19 auto token_1 = tokens_relative_1[i];
20 auto token_2 = tokens_relative_2[i];
21 if (token_1 == token_2)
22 common_folders.push_back(token_1);
23 else
24 break;
25 }
26 return common_folders;
27}
28
29void CounterDuplicationCode::mark_path_as_processed(Path path) {
30 processed_paths.insert(path);
31}
32
33bool CounterDuplicationCode::is_path_processed_path(Path path) {
34 return processed_paths.find(path) != processed_paths.end();
35}
36
37int CounterDuplicationCode::get_number_of_lines_in_path(const Path& path) {
38 FunctionLoader loader;
39 auto function = loader.load_metadata(path);
40 return function.scope_location().size();
41}
42
43void CounterDuplicationCode::register_code_duplication(const Path& path1, const Path& path2, int number_of_lines) {
44 std::vector<std::string> common_folder_path = common_folders(path1, path2);
45 counter_duplication_code_trie.add_folder_duplication_code(common_folder_path, number_of_lines);
46}
47
48void CounterDuplicationCode::process_path(const Path& path) {
49 if (is_path_processed_path(path)) {
50 return;
51 }
52 uint32_t number_of_lines = get_number_of_lines_in_path(path);
53 mark_path_as_processed(path);
54 std::vector<Path> similar_paths = similarity_table->get_similar_path_to_the_reference(path);
55 for (const auto& similar_path : similar_paths) {
56 mark_path_as_processed(similar_path);
57 register_code_duplication(path, similar_path, number_of_lines);
58 }
59}
60
61void CounterDuplicationCode::process_every_path_in_similarity_table() {
62 std::vector<Path> paths = similarity_table->get_path_list();
63 for (const auto& path : paths) {
64 process_path(path);
65 }
66}
67
68CounterDuplicationCode::CounterDuplicationCode(Similarity_Table* _similarity_table) {
69 similarity_table = _similarity_table;
70}
71
73 if (options.has("json"))
74 throw CLIError("--json is not supported in this command.");
75
76 return true;
77}
78
79bool CounterDuplicationCode::run([[maybe_unused]] const ParsedOptions& options) {
80 process_every_path_in_similarity_table();
81 counter_duplication_code_trie.print_duplication_code_trie();
82
83 return true;
84}
Base class for CLI-related errors.
Definition: cli_error.hpp:10
const CliOption * options() const final
void add_folder_duplication_code(const std::vector< std::string > &folder_path, int number_of_duplication_lines)
Adds duplication count for a folder path.
void print_duplication_code_trie()
Prints the trie structure with duplication counts.
bool validate(const ParsedOptions &options) override
Validate the arguments already analyzed.
bool run(const ParsedOptions &options) override
Handles duplication analysis command.
Function load_metadata(const Path &path)
Loads function metadata from a path.
const FunctionLocation & scope_location() const
Gets the function location inside the source file.
Definition: function.cpp:17
Represents a logical path to a function in the codebase.
Definition: path.hpp:26
const fs::path & value() const
Gets the underlying filesystem path.
Definition: path.cpp:7
Represents a similarity graph between functions (paths).
const std::vector< Path > & get_path_list() const
Gets list of all known paths.
std::vector< Path > get_similar_path_to_the_reference(const Path &reference)
Gets paths similar to reference path.
Code duplication reporting system.
Provides functionality to load Function objects from external sources.
constexpr uint32_t size() const noexcept
Definition: function.hpp:28