Arkanjo 0.2
A tool for find code duplicated functions in codebases
Loading...
Searching...
No Matches
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>
23
26
28
35class SimilarityExplorer : public CommandBase<SimilarityExplorer> {
36 public:
37 static constexpr int UNLIMITED_RESULTS = 0;
38 static constexpr const char* EMPTY_PATTERN = "";
39
40 static constexpr CliOption options_[] = {
41 {"limiter", 'l', RequiredArgument, "Limits the number of results shown to the user. By default, all results are shown."},
42 {"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`)."},
43 {"both-match", 'b', NoArgument, "Enable both-pattern matching. By default, the pattern only needs to match one function."},
44 {"sort", 's', NoArgument, "Sort results by number of duplicated lines. By default, results are sorted by the similarity metric."},
45 {"cluster", 'c', NoArgument, "Print results with cluster relationships from the similarity table."},
47 };
49 "Explore duplicated functions detected in the project.")
50
51
55 explicit SimilarityExplorer(Similarity_Table* _similarity_table);
56
57 bool validate(const ParsedOptions& options) override;
58
62 bool run(const ParsedOptions& options) override;
63
64 private:
65 static constexpr const char* TEMPLATE_PROCESSED_RESULTS =
66 "Functions find: {path_a} "
67 "AND {path_b}"
68 ", TOTAL NUMBER LINES IN FUNCTIONS: {duplicated_lines}";
69 static constexpr const char* TEMPLATE_PROCESSED_RESULTS_CLUSTERS =
70 "Function find: {path_a}"
71 ", TOTAL NUMBER LINES IN FUNCTION: {duplicated_lines}";
72 static constexpr const char* TEMPLATE_INITIAL_TEXT =
73 "It was found a total of {found:bold} "
74 "pair of duplicate functions in the codebase. Which the first "
75 "{show:bold} can be found below.";
76
77 int INITIAL_PROCESSED_RESULTS = 0; ///< Initial counter for processed results
78
79 Similarity_Table* similarity_table; ///< Source of similarity data
80 int limit_on_results; ///< Maximum number of results to show
81 std::string pattern_to_match; ///< Pattern to filter results
82 bool both_path_need_to_match_pattern; ///< Whether both paths must match pattern
83 bool sorted_by_number_of_duplicated_code; ///< Whether to sort by line count
84 bool use_clusters; ///< Whether clusters be printed
85 int processed_results = INITIAL_PROCESSED_RESULTS; ///< Counter for processed results
86
92 int find_number_pairs_show(int number_pair_found) const;
93
100 bool match_pattern(const Path& path1, const Path& path2) const;
101
107 static int find_number_lines(const Path& path1);
108
114 SimilarityExplorerEntry process_similar_path_pair(const Path& path1, const Path& path2);
115
121 int find_number_pair_found(const std::vector<std::pair<Path, Path>>& similar_path_pairs) const;
122
127 std::vector<std::pair<Path, Path>> build_similar_path_pairs();
128
132 void explorer_clusters();
133
137 void explorer();
138};
const CliOption * options() const final
Represents a code function with its content and metadata.
Definition function.hpp:30
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 abstraction for temporary codebase.
#define OPTION_END
This marks the end of a long array of options.
@ RequiredArgument
@ NoArgument
Path abstraction for temporary codebase.
Similarity relationships storage and analysis.
Defines utility functions used across all files.