首页 > 解决方案 > 如何从 CallExpr 中找到完整的包导入

问题描述

以下方法从文件的 AST 中提取所有公共方法调用。我需要从 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
}

标签: goabstract-syntax-treestatic-analysis

解决方案


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

Alan Donovan 的 go/types 文章详细介绍了如何正确使用类型检查器,但这里是它的要点。为简洁起见,我在 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

推荐阅读