ArKanjo 0.5.1
A tool for find code duplicated functions in codebases
similar_function_finder.cpp
Go to the documentation of this file.
7
9
10void SimilarFunctionFinder::find_path_that_meets_pattern() {
11 std::vector<Path> paths = similarity_table->get_path_list();
12 FunctionLoader loader;
13 for (const auto& _path : paths) {
14 auto function = loader.load(_path);
15
16 if (function.contains_given_pattern(function_pattern))
17 path = _path;
18 }
19}
20
21void SimilarFunctionFinder::print_empty_path_message() const {
22 fm::write(TEMPLATE_EMPTY_FUNCTION, SimilarFunctionEmptyEntry{
23 function_pattern
24 }, Format::TEXT);
25}
26
27Function SimilarFunctionFinder::get_reference_function(const Path& path) const {
28 FunctionLoader loader;
29 return loader.load(path);
30}
31
32void SimilarFunctionFinder::print_reference_path() {
33 fm::write(REFERENCE_PATH_MESSAGE);
34 auto function = get_reference_function(path);
36}
37
38void SimilarFunctionFinder::print_similar_functions(const std::vector<Path>& similar_paths) {
40 similar_paths.size()
41 }, Format::TEXT);
42 fm::write("");
43 for (const auto& similar_path : similar_paths) {
44 auto function = get_reference_function(similar_path);
48 fm::write("");
49 }
50}
51
52void SimilarFunctionFinder::print_similar_functions() {
53 std::vector<Path> similar_paths = similarity_table->get_similar_path_to_the_reference(path);
54
56 print_reference_path();
58 if (open_folder) {
60 fs::path full_path = params.path / fs::path{path.relative_path()};
61 fs::path dir_path = full_path.parent_path();
62 Utils::open_folder(dir_path);
63 return;
64 }
65 print_similar_functions(similar_paths);
66}
67
68void SimilarFunctionFinder::handle_show_mode() {
69 Function ref = get_reference_function(path);
71 fm::write("");
72 FunctionPrinter::print_code(ref, no_numbers);
73}
74
76 similarity_table = _similarity_table;
77 function_pattern = "";
78}
79
81 open_folder = options.has("open");
82 show_mode = options.has("show");
83 no_numbers = options.has("no-numbers");
84
85 if (options.extra_args.empty())
86 throw CLIError("Similar Function Finder Command expect one parameter, but none was given");
87
88 return true;
89}
90
92 function_pattern = options.extra_args[0];
93
94 find_path_that_meets_pattern();
95
96 if (path.empty()) {
97 print_empty_path_message();
98 return true;
99 }
100
101 if (show_mode) {
102 handle_show_mode();
103 return true;
104 }
105
106 print_similar_functions();
107
108 return true;
109}
Base class for CLI-related errors.
Definition: cli_error.hpp:10
const CliOption * options() const final
static void write(const std::string &template_str, const std::vector< T > &data, enum Format effective=Format::AUTO, RowColorFn color_fn=nullptr, std::ostream &out=std::cout)
Function load(const Path &path)
Loads a function from a path.
static void print_basic_info(const Function &function)
Prints formatted function information to console.
static void print_code(const Function &function, bool no_numbers=false)
Prints the function source code.
Represents a function extracted from a codebase.
Definition: function.hpp:45
Represents a logical path to a function in the codebase.
Definition: path.hpp:26
fs::path relative_path() const
Gets the directory portion of the path.
Definition: path.cpp:15
bool empty() const
Checks if path is empty.
Definition: path.cpp:11
static PreprocessRunParams read_current_run_params()
read preprocessing parameters runs
SimilarFunctionFinder(Similarity_Table *_similarity_table)
Constructs finder and initiates search.
bool validate(const ParsedOptions &options) override
Validate the arguments already analyzed.
bool run(const ParsedOptions &options) override
Handles similar function finding command.
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.
Provides functionality to load Function objects from external sources.
Provides utilities for printing function information and source code.
void open_folder(const std::string &path)
Definition: utils.cpp:196
const std::string LIMITER_PRINT
Constant string used as a visual delimiter/separator in prints.
Definition: utils.hpp:33
Similar function locator interface.