From 23220e9c8413d65886be1a0b992a4b76eb7066c2 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Tue, 12 Nov 2024 16:43:07 +0800 Subject: [PATCH] refactor(goast): improve handling of qualified identifiers in GoFullIdentListener Updated the `GoFullIdentListener` to better manage qualified identifiers by resolving local variable references within a qualified expression. This change ensures that when a qualified identifier is encountered (e.g., `d.Field`), if the first part of the identifier (local variable `d`) is present in the local variables map, it is replaced accordingly (e.g., `Dao.Field`). Additionally, removed a println statement and corrected an assertion in the `GoFullIdentListenerTest` to reflect the expected node name with the updated handling of qualified identifiers. --- .../kotlin/chapi/ast/goast/GoFullIdentListener.kt | 13 +++++++++++++ .../chapi/ast/goast/GoFullIdentListenerTest.kt | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/chapi-ast-go/src/main/kotlin/chapi/ast/goast/GoFullIdentListener.kt b/chapi-ast-go/src/main/kotlin/chapi/ast/goast/GoFullIdentListener.kt index 134b6456..23a51350 100644 --- a/chapi-ast-go/src/main/kotlin/chapi/ast/goast/GoFullIdentListener.kt +++ b/chapi-ast-go/src/main/kotlin/chapi/ast/goast/GoFullIdentListener.kt @@ -366,6 +366,19 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() { private fun handleForPrimary(child: PrimaryExprContext, isForLocalVar: Boolean = false): String? { +// val possibleResult = handleForPrimary(first, isForLocalVar).orEmpty() + if (child.primaryExpr()?.text?.contains(".") == true) { + /// replace first.text to possibleResult + /// d.Field -> Dao.Field + val split = child.primaryExpr().text.split(".") + val withoutFirst = split.drop(1).joinToString(".") + // get first part and try to resolve in local var + val first = split.first() + if (localVars.containsKey(first)) { + return "${localVars[first]}.$withoutFirst" + } + } + val nodeName = when (val first = child.getChild(0)) { is GoParser.OperandContext -> { first.text diff --git a/chapi-ast-go/src/test/kotlin/chapi/ast/goast/GoFullIdentListenerTest.kt b/chapi-ast-go/src/test/kotlin/chapi/ast/goast/GoFullIdentListenerTest.kt index 642b6e75..f0180bae 100644 --- a/chapi-ast-go/src/test/kotlin/chapi/ast/goast/GoFullIdentListenerTest.kt +++ b/chapi-ast-go/src/test/kotlin/chapi/ast/goast/GoFullIdentListenerTest.kt @@ -546,10 +546,10 @@ func (d *Dao) GetLiveAchieve(ctx context.Context, uid int64) (achieve int64, err val functionCalls = codeFile.DataStructures.first().Functions.last().FunctionCalls val getExecFunc = functionCalls[0] - println(getExecFunc) + assertEquals(getExecFunc.FunctionName, "Userstatus") assertEquals(getExecFunc.Parameters.size, 2) - assertEquals(getExecFunc.NodeName, "Dao") + assertEquals(getExecFunc.NodeName, "Dao.rcClient") } }