ArKanjo 0.5.1
A tool for find code duplicated functions in codebases
similarity_explorer.hpp
Go to the documentation of this file.
1
13#pragma once
14
15#include <string>
16#include <utility>
17#include <vector>
18
20#include <arkanjo/base/path.hpp>
22
25
27
34class SimilarityExplorer : public CommandBase<SimilarityExplorer> {
35 public:
36 static constexpr int UNLIMITED_RESULTS = 0;
37 static constexpr const char* EMPTY_PATTERN = "";
38
39 static constexpr CliOption options_[] = {
40 {"limiter", 'l', RequiredArgument, "Limits the number of results shown to the user. By default, all results are shown."},
41 {"pattern", 'p', RequiredArgument, "Defines a pattern that function names must match to be included in the results. A function is considered a match if the pattern is a substring of the function's concatenated file path and name (e.g., `path/to/file.c:function_name`)."},
42 {"both-match", 'b', NoArgument, "Enable both-pattern matching. By default, the pattern only needs to match one function."},
43 {"sort", 's', NoArgument, "Sort results by number of duplicated lines. By default, results are sorted by the similarity metric."},
44 {"cluster", 'c', NoArgument, "Print results with cluster relationships from the similarity table."},
45 {"verbose", 0, NoArgument, "Enable verbose output"},
46 {"template", 't', RequiredArgument, "Output format template for each result entry. Run --template-help to see available variables and an example."},
47 {"template-help", 0, NoArgument, "Show available template variables for --template."},
49 };
51 "Explore duplicated functions detected in the project.")
52
53
57 explicit SimilarityExplorer(Similarity_Table* _similarity_table);
58
59 bool validate(const ParsedOptions& options) override;
60
64 bool run(const ParsedOptions& options) override;
65
66 private:
67 static constexpr const char* TEMPLATE_PROCESSED_RESULTS =
68 "Functions find: {path_a} "
69 "AND {path_b}"
70 ", TOTAL NUMBER LINES IN FUNCTIONS: {duplicated_lines}";
71 static constexpr const char* TEMPLATE_PROCESSED_RESULTS_CLUSTERS =
72 "Function find: {path_a}"
73 ", TOTAL NUMBER LINES IN FUNCTION: {duplicated_lines}";
74 static constexpr const char* TEMPLATE_INITIAL_TEXT =
75 "It was found a total of {found:bold} "
76 "pair of duplicate functions in the codebase. Which the first "
77 "{show:bold} can be found below.";
78
79 int INITIAL_PROCESSED_RESULTS = 0; ///< Initial counter for processed results
80
81 Similarity_Table* similarity_table; ///< Source of similarity data
82 int limit_on_results; ///< Maximum number of results to show
83 std::string pattern_to_match; ///< Pattern to filter results
84 bool both_path_need_to_match_pattern; ///< Whether both paths must match pattern
85 bool sorted_by_number_of_duplicated_code; ///< Whether to sort by line count
86 bool use_clusters; ///< Whether clusters be printed
87 int processed_results = INITIAL_PROCESSED_RESULTS; ///< Counter for processed results
88 bool mode_verbose = false; ///< Detailed execution information
89 std::string template_processed_results_output = TEMPLATE_PROCESSED_RESULTS;
90
96 int find_number_pairs_show(int number_pair_found) const;
97
104 bool match_pattern(const Function& function1, const Function& function2) const;
105
111 SimilarityExplorerEntry process_similar_path_pair(const Path& path1, const Path& path2);
112
118 int find_number_pair_found(const std::vector<SimilarPair>& similar_path_pairs) const;
119
124 std::vector<SimilarPair> build_similar_path_pairs();
125
129 static void print_template_variables();
130
134 void explorer_clusters();
135
139 void explorer();
140};
const CliOption * options() const final
Represents a function extracted from a codebase.
Definition: function.hpp:45
Duplicate function explorer and analyzer.
static constexpr int UNLIMITED_RESULTS
Constant for unlimited results display.
static constexpr const char * EMPTY_PATTERN
Constant for empty search pattern.
static constexpr CliOption options_[]
bool validate(const ParsedOptions &options) override
Validate the arguments already analyzed.
COMMAND_DESCRIPTION("Explore duplicated functions detected in the project.") explicit SimilarityExplorer(Similarity_Table *_similarity_table)
Constructs explorer with configuration.
bool run(const ParsedOptions &options) override
Handles code exploration command.
Represents a similarity graph between functions (paths).
Function representation for a codebase.
#define OPTION_END
This marks the end of a long array of options.
@ RequiredArgument
@ NoArgument
Logical path abstraction for source code elements.
Similarity relationships storage and analysis.