如何从CallExpr中查找完整的包导入

问题描述 投票:0回答:1

以下方法从文件的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
}
go abstract-syntax-tree static-analysis
1个回答
2
投票

要获得完全限定名称,必须使用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
© www.soinside.com 2019 - 2024. All rights reserved.