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