ArKanjo 0.5.1
A tool for find code duplicated functions in codebases
preprocessor_build.hpp
Go to the documentation of this file.
1
13#pragma once
14
19
20#include "function_breaker.hpp"
23#include <optional>
24#include <string>
25#include <tuple>
26#include <filesystem>
27
28namespace fs = std::filesystem;
29
30using MethodFactory = std::function<std::unique_ptr<IMethod>(
31 const std::string&, float, const std::vector<std::string>&
32)>;
33
34struct MethodInfo {
35 size_t id;
36 std::string_view name;
38 std::string_view description;
39};
40
52class PreprocessorBuild : public Preprocess_State, public CommandBase<PreprocessorBuild> {
53 private:
54 static constexpr const char* TEMPLATE_COUNT =
55 "The total number of functions that are "
56 "similar to the found one is {count}. "
57 "More info about them are listed below.";
58 // User interaction messages
59 static constexpr const char* PROJECT_PATH_MESSAGE = "Enter your project path:";
60 static constexpr const char* MINIMUM_SIMILARITY_MESSAGE = "Enter minimum similarity desired on using the tool:";
61
62 // Processing stage messages
63 static constexpr const char* INITIAL_MESSAGE = "Initiating Preprocessing";
64 static constexpr const char* BREAKER_MESSAGE = "Reading codebase... (this may take a while)";
65 static constexpr const char* DUPLICATION_MESSAGE = "Finding duplication in the codebase... (this may take a while)";
66 static constexpr const char* SAVING_MESSAGE = "Saving results...";
67 static constexpr const char* END_MESSAGE = "Finished preprocessing";
68 static constexpr const char* ERROR_PATH_MESSAGE = "Provided path does not exist.";
69
70 // Duplication finder selection messages
71 static constexpr const char* MESSAGE_DUPLICATION_FINDER_TYPE = "Enter the number of the duplication finder technique you want to use:";
72 static constexpr const char* INVALID_CODE_DUPLICATION_FINDER = "Invalid option entered. Stopping Program...";
73
74 bool mode_verbose = false;
75 int minimum_lines = 0;
76
77 const std::vector<MethodInfo> MethodsType = {
78 {
79 1,
80 "tfidf",
81 [](const std::string& base_path, float similarity,
82 const std::vector<std::string>&) {
83 return std::make_unique<TfidfMethod>(base_path, similarity);
84 },
85 "NLP text similarity using gensim"
86 },
87 {
88 2,
89 "diff",
90 [](const std::string& base_path, float similarity,
91 const std::vector<std::string>&) {
92 return std::make_unique<DiffMethod>(base_path, similarity);
93 },
94 "Count proportion of equal lines using diff command"
95 },
96 {
97 3,
98 "ast",
99 [](const std::string& base_path, float similarity,
100 const std::vector<std::string>&) {
101 return std::make_unique<ASTMethod>(base_path, similarity);
102 },
103 "Compare linearized structural sequences extracted from Tree-sitter ASTs"
104 },
105 {
106 4,
107 "embedding",
108 [](const std::string& base_path, float similarity,
109 const std::vector<std::string>& pass_through_args) {
110 return std::make_unique<EmbeddingMethod>(base_path, similarity, pass_through_args);
111 },
112 "Embedding-based similarity using a code language model"
113 }
114 };
115
126 std::tuple<std::string, double, size_t, std::vector<std::string>, Granularity>
127 read_parameters(const ParsedOptions& options);
128
137 void preprocess(const fs::path& path, double similarity, size_t use_duplication_finder_index,
138 const std::vector<std::string>& pass_through_args = {},
139 Granularity granularity = Granularity::Function);
140
141 public:
142 static constexpr CliOption options_[] = {
143 {"verbose", 0, NoArgument, "Enable verbose output"},
144 {"path", 0, RequiredArgument, "Project path to preprocess."},
145 {"minimum-lines", 0, RequiredArgument, "Minimum clone size in original lines."},
146 {"granularity", 0, RequiredArgument,
147 "Comparison granularity: 'function' (default) compares individual "
148 "functions; 'file' keeps each file whole and compares files against "
149 "each other. Applies to every duplication finder method."},
150 {"method", 0, RequiredArgument,
151 "Duplication finder method: 1|tfidf, 2|diff, 3|ast, 4|embedding."},
153 };
154
155 PreprocessorBuild() = default;
156
164 PreprocessorBuild(bool force_preprocess, const fs::path& path, double similarity);
165
166 COMMAND_DESCRIPTION("Creates the foundation for fast query responses during the main operation phase.")
167
168 bool validate(const ParsedOptions& options) override;
169
170 bool run(const ParsedOptions& options) override;
171};
const CliOption * options() const final
Codebase preprocessing orchestrator.
static constexpr CliOption options_[]
bool validate(const ParsedOptions &options) override
Validate the arguments already analyzed.
PreprocessorBuild()=default
bool run(const ParsedOptions &options) override
Executes the main logic of the command.
#define COMMAND_DESCRIPTION(str)
Definition: command_base.hpp:9
Code duplication preprocessing system.
LLM-embedding-based code duplication detection.
Main function extraction interface.
Granularity
Comparison granularity used when breaking the codebase.
#define OPTION_END
This marks the end of a long array of options.
@ RequiredArgument
@ NoArgument
std::function< std::unique_ptr< IMethod >(const std::string &, float, const std::vector< std::string > &)> MethodFactory
std::string_view name
std::string_view description
MethodFactory create
Main duplication detection tool interface.