reman3/tooling/tool.hpp

108 lines
3.6 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 };
// Add calling convention enum
enum class CallingConvention { Cdecl, Stdcall, Fastcall };
// Data structures
struct FunctionInfo {
std::string name;
std::string address;
std::string filepath;
std::string parameter_names; // Semicolon-separated parameter names
std::string parameter_types; // Semicolon-separated parameter types
std::string return_type; // Function return type
bool is_import;
FileType type;
CallingConvention calling_convention = CallingConvention::Cdecl; // Default to cdecl
};
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);
// Add utility functions for calling convention
CallingConvention stringToCallingConvention(const std::string &conv_str);
std::string callingConventionToString(CallingConvention conv);
// 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);
std::string getParameterNames(TSNode node, const char *source_code);
std::string getParameterTypes(TSNode node, const char *source_code);
std::string getReturnType(TSNode node, const char *source_code);
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);
std::vector<std::string> getAllFiles();
void removeFile(const std::string &filepath);
};
// 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;
}
};