登录
首页 >  Golang >  Go问答

搜索 CallExpr 中的完整包引入

来源:stackoverflow

时间:2024-02-25 16:30:19 363浏览 收藏

Golang不知道大家是否熟悉?今天我将给大家介绍《搜索 CallExpr 中的完整包引入》,这篇文章主要会讲到等等知识点,如果你在看完本篇文章后,有更好的建议或者发现哪里有问题,希望大家都能积极评论指出,谢谢!希望我们能一起加油进步!

问题内容

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

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

以上就是《搜索 CallExpr 中的完整包引入》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>