ArKanjo 0.5.1
A tool for find code duplicated functions in codebases
tree_sitter_parser.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <filesystem>
5#include <functional>
6
7#include <tree_sitter/api.h>
8
11
12namespace fs = std::filesystem;
13
14struct SkipStats {
15 size_t errors;
16};
17
19 static bool is_function_empty(TSNode body);
20
21 static std::string detect_language(const fs::path& path);
22
23 static std::string get_full_signature(TSNode func_node, const std::string& source);
24
25 // Extracts the function name from a Tree-sitter function node across multiple languages.
26 //
27 // Supports different AST structures depending on the language:
28 // - C/C++: uses "declarator" field (function_definition)
29 // - Rust: uses "name" field (function_item)
30 // - Fallback: recursively searches for an identifier-like node
31 //
32 // This abstraction allows the same FunctionBreaker pipeline to work across
33 // multiple Tree-sitter grammars without hardcoding per-language parsers.
34 //
35 static std::string get_function_name(TSNode func_node, const std::string& source);
36
37 static TSNode get_body(TSNode node);
38
49 static bool should_ignore(TSNode node);
50
51 static void collect_functions(
52 TSNode node, const std::string& source, const fs::path& relative_path,
53 const std::shared_ptr<TSTree>& tree,
54 std::function<void(const FunctionData&)> callback,
55 SkipStats& stats);
56
57 static std::shared_ptr<TSTree> parse_source(
58 const fs::path& file_path, const std::string& source_code);
59
60 public:
61 static void process_file(
62 const fs::path& file_path, const fs::path& relative_path, const std::string& source_code,
63 std::function<void(const FunctionData&)> callback,
64 SkipStats& stats);
65
66 static void process_file_as_unit(
67 const fs::path& file_path, const fs::path& relative_path, const std::string& source_code,
68 std::function<void(const FunctionData&)> callback,
69 SkipStats& stats);
70
71 explicit TreeSitterParser() = default;
72};
static void process_file_as_unit(const fs::path &file_path, const fs::path &relative_path, const std::string &source_code, std::function< void(const FunctionData &)> callback, SkipStats &stats)
TreeSitterParser()=default
static void process_file(const fs::path &file_path, const fs::path &relative_path, const std::string &source_code, std::function< void(const FunctionData &)> callback, SkipStats &stats)