Search code examples
goabstract-syntax-treestatic-analysisgo-toolchain

How to find the declaration of an Ident using go/analysis?


I use go/analysis to create my own static analysis tool. I still don't know how to find the def information from ast.Ident.

Here is my testdata

package randomcheck
func xxx() {
}
func demo()  {
    xxx()
}

And my own analyzer

import (
    "fmt"
    "go/ast"
    "golang.org/x/tools/go/analysis"
    "golang.org/x/tools/go/analysis/passes/inspect"
)

var name string // -name flag
var Analyzer = &analysis.Analyzer{
    Name:     "fft",
    Requires: []*analysis.Analyzer{inspect.Analyzer},
    Run:      run,
}
//pass.Fset.Position(name.Pos())
func run(pass *analysis.Pass) (interface{}, error) {
    for _, f := range pass.Files {
        ast.Inspect(f, func(node ast.Node) bool {
            name,ok := node.(*ast.Ident)
            if !ok {
                return true
            }
            if name == nil {
                return true
            }
            if pass.TypesInfo.Defs[name] != nil {
                fmt.Println("def: " ,name)
            } else {
                fmt.Println("use: ", name)
            }
            return true
        })
    }

    return nil, nil
}


output:

use:  randomcheck
def:  xxx
def:  demo
use:  xxx

I need to find the def info def:xxx directly from use:xxx, but I can't find useful information in pass.TypesInfo


Solution

  • Are you looking for the ObjectOf method? Here's your version with some modifications:

    func run(pass *analysis.Pass) (interface{}, error) {
        for _, f := range pass.Files {
            ast.Inspect(f, func(node ast.Node) bool {
                name, ok := node.(*ast.Ident)
                if !ok {
                    return true
                }
                if name == nil {
                    return true
                }
    
                fmt.Println("ident:", nodeString(node, pass.Fset))
                obj := pass.TypesInfo.ObjectOf(name)
                fmt.Println(obj)
                if obj != nil {
                    fmt.Println("  pos:", pass.Fset.Position(obj.Pos()))
                }
                return true
            })
        }
    
        return nil, nil
    }
    
    // nodeString formats a syntax tree in the style of gofmt.
    func nodeString(n ast.Node, fset *token.FileSet) string {
        var buf bytes.Buffer
        format.Node(&buf, fset, n)
        return buf.String()
    }
    

    When run on your sample input file, it shows:

    ident: randomcheck
    <nil>
    ident: xxx
    func command-line-arguments.xxx()
      pos: /home/eliben/temp/randomcheck.go:3:6
    ident: demo
    func command-line-arguments.demo()
      pos: /home/eliben/temp/randomcheck.go:5:6
    ident: xxx
    func command-line-arguments.xxx()
      pos: /home/eliben/temp/randomcheck.go:3:6
    

    Note that the last id xxx is found as a reference to the top-level function xxx() with its proper position, etc.