Skip to content

Commit

Permalink
refactor(ast): update local variable handling in GoFullIdentListener
Browse files Browse the repository at this point in the history
Update the way local variables are added to functions in the GoFullIdentListener. This includes:
- Changing the method of adding local variables to use a new data structure.
- Enhancing the const local variable type detection by including the expression list text when the type is not specified.
- Adding a new test case to verify the identification of const local variables.
  • Loading branch information
phodal committed Nov 9, 2024
1 parent 2c17637 commit fa914fe
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ allprojects {
apply(plugin = "java")

group = "com.phodal.chapi"
version = "2.4.6"
version = "2.4.7"
description =
"Chapi is A common hierarchical abstract parser && information convertor, streamlines code analysis by converting diverse language source code into a unified abstract model, simplifying cross-language development."

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() {
}

override fun exitFunctionDecl(ctx: GoParser.FunctionDeclContext?) {
currentFunction.addVarsFromMap(localVars)
currentFunction.LocalVariables = localVars.map { entry ->
CodeProperty(TypeValue = entry.key, DefaultValue = entry.value, TypeType = entry.value)
}

defaultNode.Functions += currentFunction
currentFunction = CodeFunction()
}


private var blockStack = Stack<CodeFunction>()
private var lastBlock = CodeFunction(Type = FunctionType.Block, Name = "chapi_block")

Expand Down Expand Up @@ -149,7 +151,10 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() {

override fun exitMethodDecl(ctx: GoParser.MethodDeclContext?) {
val receiverName = this.getStructNameFromReceiver(ctx?.receiver()?.parameters())
currentFunction.addVarsFromMap(localVars)
currentFunction.LocalVariables = localVars.map { entry ->
CodeProperty(TypeValue = entry.key, DefaultValue = entry.value, TypeType = entry.value)
}

this.addReceiverToStruct(receiverName, currentFunction)
currentFunction = CodeFunction()
}
Expand Down Expand Up @@ -432,7 +437,7 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() {
override fun enterConstDecl(ctx: GoParser.ConstDeclContext?) {
ctx?.constSpec()?.forEach { constSpecContext ->
constSpecContext.identifierList().IDENTIFIER().forEach { terminalNode ->
localVars[terminalNode.text] = constSpecContext.type_()?.text ?: ""
localVars[terminalNode.text] = constSpecContext.type_()?.text ?: constSpecContext.expressionList().text
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func ConstDecls() {
"""
val codeFile = GoAnalyser().analysis(code, "")
assertEquals(codeFile.DataStructures[0].Functions[0].LocalVariables.size, 4)
assertEquals(codeFile.DataStructures[0].Functions[0].LocalVariables[3].TypeType, "")
assertEquals(codeFile.DataStructures[0].Functions[0].LocalVariables[3].TypeType, "-1")
assertEquals(codeFile.DataStructures[0].Functions[0].LocalVariables[3].TypeValue, "eof")
}

Expand Down Expand Up @@ -331,6 +331,54 @@ func (d *Dao) QueryBuglyProjectList() (projectList []string, err error) {
assertEquals(functionCalls[1].FunctionName, "Rows")
}

@Test
internal fun shouldIdentifyConstLocalVars() {
@Language("Go")
val code= """
package dao
const (
_taskSQL = "SELECT id,business_id,flow_id,rid,admin_id,uid,state,weight,utime,gtime,mid,fans,`group`,reason,ctime,mtime from task WHERE id=?"
_listCheckSQL = "SELECT id FROM task WHERE id IN (%s)"
_dispatchByIDSQL = "UPDATE task SET gtime=? WHERE id=? AND state=? AND uid=? AND gtime=0"
_queryGtimeSQL = "SELECT gtime FROM task WHERE id=? AND state=? AND uid=?"
_dispatchSQL = "UPDATE task SET gtime=? WHERE state=? AND uid=? AND gtime='0000-00-00 00:00:00' ORDER BY weight LIMIT ?"
_releaseSQL = "UPDATE task SET admin_id=0,uid=0,state=0,gtime='0000-00-00 00:00:00' WHERE business_id=? AND flow_id=? AND uid=? AND (state=? OR (state=0 AND admin_id>0))"
_resetGtimeSQL = "UPDATE task SET gtime='0000-00-00 00:00:00' WHERE state=? AND business_id=? AND flow_id=? AND uid=?"
_seizeSQL = "UPDATE task SET state=?,uid=? WHERE id=? AND state=?"
_submitSQL = "UPDATE task SET state=?,uid=?,utime=? WHERE id=? AND state=? AND uid=?"
_delaySQL = "UPDATE task SET state=?,uid=?,reason=?,gtime='0000-00-00 00:00:00' WHERE id=? AND state=? AND uid=?"
_consumerSQL = "INSERT INTO task_consumer (business_id,flow_id,uid,state) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE state=?"
_onlinesSQL = "SELECT uid,mtime FROM task_consumer WHERE business_id=? AND flow_id=? AND state=?"
_isconsumerOnSQL = "SELECT state FROM task_consumer WHERE business_id=? AND flow_id=? AND uid=?"
_queryTaskSQL = "SELECT id,business_id,flow_id,uid,weight FROM task WHERE state=? AND mtime<=? AND id>? ORDER BY id LIMIT ?"
_countPersonalSQL = "SELECT count(*) FROM task WHERE state=? AND business_id=? AND flow_id=? AND uid=?"
_queryForSeizeSQL = "SELECT id FROM task WHERE state=? AND business_id=? AND flow_id=? AND uid IN (0,?) ORDER BY weight DESC LIMIT ?"
_listTasksSQL = "SELECT `id`,`business_id`,`flow_id`,`rid`,`admin_id`,`uid`,`state`,`weight`,`utime`,`gtime`,`mid`,`fans`,`group`,`reason`,`ctime`,`mtime` FROM task %s ORDER BY weight DESC LIMIT ?,?"
)
func (d *Dao) CountPersonal(c context.Context, opt *common.BaseOptions) (count int64, err error) {
if err = d.db.QueryRow(c, _countPersonalSQL, modtask.TaskStateDispatch, opt.BusinessID, opt.FlowID, opt.UID).Scan(&count); err != nil {
log.Error("QueryRow error(%v)", errors.WithStack(err))
return
}
return
}
"""

val codeFile = GoAnalyser().analysis(code, "")
val vars = codeFile.DataStructures[0].Functions[0].LocalVariables

assertEquals(vars.size, 19)
// get first var
assertEquals(vars[0].TypeValue, "_taskSQL")
assertEquals(vars[0].TypeType, "\"SELECT id,business_id,flow_id,rid,admin_id,uid,state,weight,utime,gtime,mid,fans,`group`,reason,ctime,mtime from task WHERE id=?\"")
}

@Test
internal fun shouldIdentifyLocalVarWithText() {
@Language("Go")
Expand Down

0 comments on commit fa914fe

Please sign in to comment.