reman3/tooling/tool.hpp

92 lines
2.8 KiB
C++

#pragma once
#include <string>
#include <vector>
#include <memory>
#include <regex>
#include <sqlite3.h>
#include <spdlog/spdlog.h>
#include <tree_sitter/api.h>
// Global constants
extern const std::regex ADDRESS_REGEX;
// Enums
enum class FileType { Auto, Fix, Stub, Ref };
// Data structures
struct FunctionInfo {
std::string name;
std::string address;
std::string filepath;
bool is_import;
FileType type;
};
struct GlobalInfo {
std::string name;
std::string address;
std::string filepath;
};
// Utility functions
FileType stringToFileType(const std::string &type_str);
std::string fileTypeToString(FileType type);
bool hasAddressPattern(const std::string &comment);
std::string extractAddress(const std::string &comment);
// Tree-sitter parsing functions
std::string extractNodeText(TSNode node, const char *source_code);
std::string findIdentifierInNode(TSNode node, const char *source_code);
std::string findIdentifierInDeclarator(TSNode node, const char *source_code);
std::string getFunctionName(TSNode node, const char *source_code);
std::string getGlobalName(TSNode node, const char *source_code);
std::string getComment(TSNode node, const char *source_code, uint32_t source_length, bool search_before);
bool hasFunctionBody(TSNode node);
void findFunctions(TSNode node, const char *source_code, uint32_t source_length,
std::vector<FunctionInfo> &functions, FileType file_type);
void findGlobals(TSNode node, const char *source_code, uint32_t source_length,
std::vector<GlobalInfo> &globals);
void dumpTreeSitterAST(TSNode node, const char *source_code, int depth = 0);
struct PreparedStatements;
class DatabaseManager {
private:
sqlite3 *db;
std::shared_ptr<PreparedStatements> prepared_stmts;
public:
DatabaseManager(const std::string &db_path);
~DatabaseManager();
void clearEntriesForFile(const std::string &filepath);
void insertFunction(const FunctionInfo &func);
void clearGlobalsForFile(const std::string &filepath);
void insertGlobal(const GlobalInfo &global);
void beginTransaction();
void commitTransaction();
void rollbackTransaction();
bool checkDuplicateAddresses();
bool checkDuplicateNames();
std::vector<FunctionInfo> getFunctionsByType(FileType type);
};
// File processing functions
std::vector<std::string> readFileList(const std::string &list_file);
bool processFile(const std::string &filepath, DatabaseManager &db, FileType file_type);
bool processGlobalsFile(const std::string &filepath, DatabaseManager &db);
bool dumpTreeFile(const std::string &filepath);
bool processDuplicates(DatabaseManager &db);
struct Options {
std::string db_path = "gh.db";
std::string mode = "functions";
std::string log_file = "";
bool verbose = false;
static Options &get() {
static Options options;
return options;
}
};