New tool version

This commit is contained in:
Guus Waals 2025-05-26 23:56:11 +08:00
parent 86fb5c8973
commit 468866b833
1 changed files with 37 additions and 25 deletions

View File

@ -33,18 +33,18 @@ public:
// Create tables if they don't exist // Create tables if they don't exist
const char* create_functions_table = R"( const char* create_functions_table = R"(
CREATE TABLE IF NOT EXISTS Functions ( CREATE TABLE IF NOT EXISTS Functions (
filepath TEXT,
name TEXT, name TEXT,
address TEXT, address TEXT,
filepath TEXT,
PRIMARY KEY (name, filepath) PRIMARY KEY (name, filepath)
) )
)"; )";
const char* create_imports_table = R"( const char* create_imports_table = R"(
CREATE TABLE IF NOT EXISTS Imports ( CREATE TABLE IF NOT EXISTS Imports (
filepath TEXT,
name TEXT, name TEXT,
address TEXT, address TEXT,
filepath TEXT,
PRIMARY KEY (name, filepath) PRIMARY KEY (name, filepath)
) )
)"; )";
@ -81,13 +81,13 @@ public:
void insertFunction(const FunctionInfo& func) { void insertFunction(const FunctionInfo& func) {
const char* table = func.is_import ? "Imports" : "Functions"; const char* table = func.is_import ? "Imports" : "Functions";
std::string sql = "INSERT OR REPLACE INTO " + std::string(table) + std::string sql = "INSERT OR REPLACE INTO " + std::string(table) +
" (name, address, filepath) VALUES (?, ?, ?)"; " (filepath, name, address) VALUES (?, ?, ?)";
sqlite3_stmt* stmt; sqlite3_stmt* stmt;
sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr); sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr);
sqlite3_bind_text(stmt, 1, func.name.c_str(), -1, SQLITE_STATIC); sqlite3_bind_text(stmt, 1, func.filepath.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, func.address.c_str(), -1, SQLITE_STATIC); sqlite3_bind_text(stmt, 2, func.name.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 3, func.filepath.c_str(), -1, SQLITE_STATIC); sqlite3_bind_text(stmt, 3, func.address.c_str(), -1, SQLITE_STATIC);
sqlite3_step(stmt); sqlite3_step(stmt);
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
} }
@ -143,30 +143,42 @@ std::string getCommentBeforeNode(TSNode node, const char* source_code) {
// Get text before the node // Get text before the node
std::string before_text(source_code, start_byte); std::string before_text(source_code, start_byte);
// Find the last occurrence of "//" before this node // Find all "//" comments before this node and look for addresses
size_t last_comment = before_text.rfind("//"); std::regex addr_regex(R"(//\s*([0-9a-fA-F]{8}))");
if (last_comment != std::string::npos) { std::smatch match;
// Find the start of the line containing this comment std::string found_address;
size_t line_start = before_text.rfind('\n', last_comment);
if (line_start == std::string::npos) { // Search backwards through all comment lines
line_start = 0; size_t search_pos = before_text.length();
} else { while (search_pos > 0) {
line_start++; // Move past the newline size_t comment_pos = before_text.rfind("//", search_pos - 1);
if (comment_pos == std::string::npos) {
break;
} }
// Extract the comment line // Find the end of this comment line
std::string comment_line = before_text.substr(line_start, start_byte - line_start); size_t line_end = before_text.find('\n', comment_pos);
if (line_end == std::string::npos) {
// Check if this comment line is close to the function (within a few lines) line_end = before_text.length();
size_t newlines_between = 0;
for (size_t i = last_comment; i < start_byte; i++) {
if (before_text[i] == '\n') newlines_between++;
} }
// If comment is within 3 lines of the function, consider it related // Extract this comment line
if (newlines_between <= 3) { std::string comment_line = before_text.substr(comment_pos, line_end - comment_pos);
return comment_line;
// Check if this comment contains an address
if (std::regex_search(comment_line, match, addr_regex)) {
// Check if this comment is reasonably close to the function (within 20 lines)
size_t newlines_between = 0;
for (size_t i = comment_pos; i < start_byte; i++) {
if (before_text[i] == '\n') newlines_between++;
}
if (newlines_between <= 20) {
return comment_line;
}
} }
search_pos = comment_pos;
} }
return ""; return "";