reman3/tooling/file_processor.cpp

127 lines
3.6 KiB
C++

#include "tool.hpp"
#include <fstream>
#include <sstream>
#include <filesystem>
#include <spdlog/spdlog.h>
#include <tree_sitter/api.h>
// Forward declarations
extern "C" TSLanguage *tree_sitter_cpp();
std::vector<std::string> readFileList(const std::string &list_file) {
std::vector<std::string> files;
std::ifstream file(list_file);
if (!file.is_open()) {
spdlog::error("Could not open list file {}", list_file);
return files;
}
std::string line;
while (std::getline(file, line)) {
if (line.empty() || line[0] == '#')
continue;
if (line.find('*') != std::string::npos) {
spdlog::info("Skipping wildcard pattern: {}", line);
continue;
}
if (std::filesystem::exists(line)) {
files.push_back(line);
} else {
spdlog::warn("File not found: {}", line);
}
}
return files;
}
bool processFile(const std::string &filepath, DatabaseManager &db,
FileType file_type) {
std::ifstream file(filepath);
if (!file.is_open()) {
spdlog::error("Could not open file {}", filepath);
return false;
}
std::string file_content((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
TSParser *parser = ts_parser_new();
ts_parser_set_language(parser, tree_sitter_cpp());
TSTree *tree = ts_parser_parse_string(parser, nullptr, file_content.c_str(),
file_content.length());
TSNode root_node = ts_tree_root_node(tree);
if (ts_node_is_null(root_node)) {
spdlog::error("Failed to parse file {}", filepath);
ts_tree_delete(tree);
ts_parser_delete(parser);
return false;
}
db.clearEntriesForFile(filepath);
std::vector<FunctionInfo> functions;
findFunctions(root_node, file_content.c_str(), file_content.length(),
functions, file_type);
for (auto &func : functions) {
func.filepath = filepath;
db.insertFunction(func);
spdlog::debug("{}: {} @ {} in {} (type: {})",
func.is_import ? "Import" : "Function", func.name,
func.address, filepath, fileTypeToString(func.type));
}
spdlog::info("Processed {} functions/imports from {} (type: {})",
functions.size(), filepath, fileTypeToString(file_type));
ts_tree_delete(tree);
ts_parser_delete(parser);
return true;
}
bool processGlobalsFile(const std::string &filepath, DatabaseManager &db) {
std::ifstream file(filepath);
if (!file.is_open()) {
spdlog::error("Could not open file {}", filepath);
return false;
}
std::string file_content((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
TSParser *parser = ts_parser_new();
ts_parser_set_language(parser, tree_sitter_cpp());
TSTree *tree = ts_parser_parse_string(parser, nullptr, file_content.c_str(),
file_content.length());
TSNode root_node = ts_tree_root_node(tree);
if (ts_node_is_null(root_node)) {
spdlog::error("Failed to parse file {}", filepath);
ts_tree_delete(tree);
ts_parser_delete(parser);
return false;
}
db.clearGlobalsForFile(filepath);
std::vector<GlobalInfo> globals;
findGlobals(root_node, file_content.c_str(), file_content.length(), globals);
for (auto &global : globals) {
global.filepath = filepath;
db.insertGlobal(global);
spdlog::debug("Global: {} @ {} in {}", global.name, global.address,
filepath);
}
spdlog::info("Processed {} globals from {}", globals.size(), filepath);
ts_tree_delete(tree);
ts_parser_delete(parser);
return true;
}