Try to fix gloal tool stuff

This commit is contained in:
Guus Waals 2025-05-28 01:00:52 +08:00
parent c9c9e9c6e6
commit 99aaebba82
1 changed files with 31 additions and 51 deletions

View File

@ -405,6 +405,28 @@ bool processFile(const std::string &filepath, DatabaseManager &db) {
return true;
}
// Helper function to recursively find identifier in any declarator
std::string findIdentifierInDeclarator(TSNode node, const char *source_code) {
const char *type = ts_node_type(node);
// If this is an identifier, return it
if (strcmp(type, "identifier") == 0) {
return extractNodeText(node, source_code);
}
// Recursively search all children
uint32_t child_count = ts_node_child_count(node);
for (uint32_t i = 0; i < child_count; i++) {
TSNode child = ts_node_child(node, i);
std::string result = findIdentifierInDeclarator(child, source_code);
if (!result.empty()) {
return result;
}
}
return "";
}
std::string getGlobalName(TSNode node, const char *source_code) {
uint32_t child_count = ts_node_child_count(node);
@ -412,57 +434,15 @@ std::string getGlobalName(TSNode node, const char *source_code) {
TSNode child = ts_node_child(node, i);
const char *type = ts_node_type(child);
// Handle reference declarators like "undefined& DAT_00000004" (direct
// child)
if (strcmp(type, "reference_declarator") == 0) {
uint32_t ref_children = ts_node_child_count(child);
for (uint32_t k = 0; k < ref_children; k++) {
TSNode ref_child = ts_node_child(child, k);
if (strcmp(ts_node_type(ref_child), "identifier") == 0) {
return extractNodeText(ref_child, source_code);
}
}
}
// Look for declarator in the declaration
else if (strcmp(type, "init_declarator") == 0 ||
strcmp(type, "declarator") == 0) {
uint32_t declarator_children = ts_node_child_count(child);
for (uint32_t j = 0; j < declarator_children; j++) {
TSNode declarator_child = ts_node_child(child, j);
const char *child_type = ts_node_type(declarator_child);
// Handle reference declarators like "undefined& DAT_00000004"
if (strcmp(child_type, "reference_declarator") == 0) {
uint32_t ref_children = ts_node_child_count(declarator_child);
for (uint32_t k = 0; k < ref_children; k++) {
TSNode ref_child = ts_node_child(declarator_child, k);
if (strcmp(ts_node_type(ref_child), "identifier") == 0) {
return extractNodeText(ref_child, source_code);
}
}
}
// Handle array declarators like
// "char(&s_or_press_ESC_to_quit_Rayman_3__005b662c)[32]"
else if (strcmp(child_type, "parenthesized_declarator") == 0) {
uint32_t paren_children = ts_node_child_count(declarator_child);
for (uint32_t k = 0; k < paren_children; k++) {
TSNode paren_child = ts_node_child(declarator_child, k);
if (strcmp(ts_node_type(paren_child), "reference_declarator") ==
0) {
uint32_t ref_children = ts_node_child_count(paren_child);
for (uint32_t l = 0; l < ref_children; l++) {
TSNode ref_child = ts_node_child(paren_child, l);
if (strcmp(ts_node_type(ref_child), "identifier") == 0) {
return extractNodeText(ref_child, source_code);
}
}
}
}
}
// Handle simple identifiers
else if (strcmp(child_type, "identifier") == 0) {
return extractNodeText(declarator_child, source_code);
}
// Look for any kind of declarator and recursively search for identifier
if (strcmp(type, "init_declarator") == 0 ||
strcmp(type, "declarator") == 0 ||
strcmp(type, "reference_declarator") == 0 ||
strcmp(type, "pointer_declarator") == 0 ||
strcmp(type, "parenthesized_declarator") == 0) {
std::string name = findIdentifierInDeclarator(child, source_code);
if (!name.empty()) {
return name;
}
}
// Direct identifier child