使用ast模块来获取函数中的所有函数调用。

huangapple go评论136阅读模式
英文:

Use ast to get all function calls in a function

问题

我正在尝试使用ast列出函数中的所有函数调用。但是我对如何使用它感到困惑。我已经做到了这一点。

set := token.NewFileSet()
packs, err := parser.ParseFile(set, serviceFile, nil, 0)
if err != nil {
	fmt.Println("Failed to parse package:", err)
	os.Exit(1)
}

funcs := []*ast.FuncDecl{}
for _, d := range packs.Decls {
	if fn, isFn := d.(*ast.FuncDecl); isFn {
		funcs = append(funcs, fn)
	}
}

我已经检查了funcs。我到达了funcs[n1].Body.List[n2]
但是在这之后,我不明白我应该如何读取底层的data.X.Fun.data.Sel.name(从gogland的评估中获得),以获取被调用函数的名称。

英文:

I'm trying to list all function call in a function using ast. But having trouble understanding how it is suppose to be used. I have been able to get this far.

set := token.NewFileSet()
packs, err := parser.ParseFile(set, serviceFile, nil, 0)
if err != nil {
	fmt.Println("Failed to parse package:", err)
	os.Exit(1)
}

funcs := []*ast.FuncDecl{}
for _, d := range packs.Decls {
	if fn, isFn := d.(*ast.FuncDecl); isFn {
		funcs = append(funcs, fn)
	}
}

I have inspected funcs. I get to funcs[n1].Body.List[n2].
But after this i don't understand how i'm suppose to read the underlaying data.X.Fun.data.Sel.name (got it from evaluation in gogland) to get name of the function being called.

答案1

得分: 2

你可以使用ast.Inspect来实现这个功能,并根据节点的类型使用switch语句。

for _, fun := range funcs {
    ast.Inspect(fun, func(node ast.Node) bool {
        switch n := node.(type) {
            case *ast.CallExpr:
                fmt.Println(n) // 打印每个函数调用表达式
        }
        return true
    })
}
英文:

you can use ast.Inspect for that and use a switch on the type of the node.

for _, fun := range funcs {
    ast.Inspect(fun, func(node ast.Node) bool {
        switch n := node.(type) {
            case *ast.CallExpr:
                fmt.Println(n) // prints every func call expression
        }
        return true
    })
}

答案2

得分: 0

好的,以下是翻译好的内容:

好的,我发现你需要进行很多类型转换才能提取数据。

这是一个示例,演示如何在函数中提取函数调用。

for _, function := range funcs {
    extractFuncCallInFunc(function.Body.List)
}

func extractFuncCallInFunc(stmts []ast.Stmt) {
    for _, stmt := range stmts {
        if exprStmt, ok := stmt.(*ast.ExprStmt); ok {
            if call, ok := exprStmt.X.(*ast.CallExpr); ok {
                if fun, ok := call.Fun.(*ast.SelectorExpr); ok {
                    funcName := fun.Sel.Name
                }
            }
        }
    }
}

我还找到了一个有助于找出需要进行类型转换的内容的网站。
http://goast.yuroyoro.net/

英文:

Ok so what i found is that you have to a lot of casting to actually extract the data.

Here is an example on how to do extract the func call in a func.

for _, function := range funcs {
	extractFuncCallInFunc(function.Body.List)
}

func extractFuncCallInFunc(stmts []ast.Stmt) {
    for _, stmt := range funcs {
    	if exprStmt, ok := stmt.(*ast.ExprStmt); ok {
    		if call, ok := exprStmt.X.(*ast.CallExpr); ok {
    			if fun, ok := call.Fun.(*ast.SelectorExpr); ok {
    				funcName := fun.Sel.Name
    			}
    		}
    	}
    }
}

I also found this with kind of help with finding out what you need to cast it to.
http://goast.yuroyoro.net/

huangapple
  • 本文由 发表于 2017年9月8日 19:20:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/46115312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定