From 99aaebba82df908f1b8b086edf4ff791115bb488 Mon Sep 17 00:00:00 2001 From: Guus Waals <_@guusw.nl> Date: Wed, 28 May 2025 01:00:52 +0800 Subject: [PATCH] Try to fix gloal tool stuff --- tooling/tool.cpp | 82 ++++++++++++++++++------------------------------ 1 file changed, 31 insertions(+), 51 deletions(-) diff --git a/tooling/tool.cpp b/tooling/tool.cpp index c68f3073..35255ebe 100644 --- a/tooling/tool.cpp +++ b/tooling/tool.cpp @@ -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