go - 如何从 CallExpr 中找到完整的包导入

标签 go abstract-syntax-tree static-analysis

以下方法从文件的 AST 中提取所有公共(public)方法调用。我需要从 CallExpr 中找出完整的包,例如:ast.Inspect() 是从“go/ast”导入的。我想将 pkgsInclude 字符串列表与导入的包名称进行匹配:

func functionCalls(path string, node *ast.File, pkgsInclude []string) int {
    fCalls := 0
    ast.Inspect(node, func(n ast.Node) bool {
        switch fCall := n.(type) {
        case *ast.CallExpr:
            if fun, ok := fCall.Fun.(*ast.SelectorExpr); ok {
                if fun.Sel.IsExported() {
                    fCalls += 1
                }
            }
        }
        return true
    })
    return fCalls
}

最佳答案

要获得完全限定的名称,必须使用 go/types 包对代码进行类型检查。

The go/types article by Alan Donovan详细介绍了如何正确使用类型检查器,但这里是要点。为了简洁起见,我在 Visit 方法中留下了一些类型断言。在生产代码中,您不应采用特定的节点类型。

package main

import (
    "fmt"
    "go/ast"
    "go/importer"
    "go/parser"
    "go/token"
    "go/types"
    "log"
)

// code to parse. It includes two variants of calling a package function.
var code = `package main

import (
    foo "io/ioutil"
    . "io/ioutil"
)

func main() {
    foo.ReadFile("")
    ReadFile("")
}
`

func main() {
    fset := &token.FileSet{}
    f, err := parser.ParseFile(fset, "", code, 0)
    if err != nil {
        log.Fatal(err)
    }


    // info.Uses allows to lookup import paths for identifiers.
    info := &types.Info{
        Uses: make(map[*ast.Ident]types.Object),
    }

    // Type check the parsed code using the default importer.
    // Use golang.org/x/tools/go/loader to check a program
    // consisting of multiple packages.
    conf := types.Config{Importer: importer.Default()}

    pkg, err := conf.Check("main", fset, []*ast.File{f}, info)
    if err != nil {
        log.Fatal(err)
    }

    // Do something with ast, info, and possibly pkg
    var _ = pkg

    ast.Walk(v{info}, f)
}

type v struct {
    info *types.Info
}

func (v v) Visit(node ast.Node) (w ast.Visitor) {
    switch node := node.(type) {
    case *ast.CallExpr:
        // Get some kind of *ast.Ident for the CallExpr that represents the
        // package. Then we can look it up in v.info. Where exactly it sits in
        // the ast depends on the form of the function call.

        switch node := node.Fun.(type) {
        case *ast.SelectorExpr: // foo.ReadFile
            pkgID := node.X.(*ast.Ident)
            fmt.Println(v.info.Uses[pkgID].(*types.PkgName).Imported().Path())

        case *ast.Ident:        // ReadFile
            pkgID := node
            fmt.Println(v.info.Uses[pkgID].Pkg().Path())
        }
    }

    return v
}

// Output:
// io/ioutil
// io/ioutil

关于go - 如何从 CallExpr 中找到完整的包导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55377694/

相关文章:

python - 为什么 PEP 3107(或 484)不包含注释全局/局部变量的语法?

java - Intellij(或其他)- 可以看到用 Java 进行的所有调用的图形/树吗?

go - Golang中的Web应用程序供离线/浏览器使用

mongodb - 使用 mgo 或 bson 在 Go 中重命名 mongo 集合?

c++ - 让 clang-tidy 发现静态可推导的逻辑错误

java - 我可以在不编译的情况下获得 C/C++/Java 代码的 XML AST 吗?

java - 从java语法antlr创建解析器时出错

go - docker 错误 : Volume specifies nonexistent driver inmemory

go - Golang 结构体名称和类型后面的字符串是什么?

abstract-syntax-tree - 如何获得 Kotlin AST?