diff --git a/pkg/analysis/analysis.go b/pkg/analysis/analysis.go index 73183ea..664ef0a 100644 --- a/pkg/analysis/analysis.go +++ b/pkg/analysis/analysis.go @@ -3,11 +3,16 @@ package analysis import ( "encoding/json" "fmt" - "github.com/goccy/go-graphviz" "go/ast" "go/build" "go/parser" "go/token" + "io" + "log" + "os" + "strings" + + "github.com/goccy/go-graphviz" "golang.org/x/tools/go/callgraph" "golang.org/x/tools/go/callgraph/cha" "golang.org/x/tools/go/callgraph/rta" @@ -16,10 +21,6 @@ import ( "golang.org/x/tools/go/packages" "golang.org/x/tools/go/ssa" "golang.org/x/tools/go/ssa/ssautil" - "io" - "log" - "os" - "strings" ) /** @@ -260,6 +261,7 @@ func (p *ProgramAnalysis) SetTree() error { } pNode.Children = append(pNode.Children, qNode.Key) qNode.Parent = append(qNode.Parent, pNode.Key) + qNode.CalledTimes += 1 fmt.Printf("%s to %s \n", caller, callee) return nil }) @@ -326,22 +328,19 @@ func (p *ProgramAnalysis) printPng() { } g.Close() }() - main := p.tree.Nodes[p.tree.MainKey] - p.tree.ChildrenToPng(main, nil, graph) - // init函数的遍历 + for key, value := range p.tree.Nodes { // 排除已遍历程序 if _, ok := p.tree.isVisited[key]; ok { continue } - // 最后以为以init开始时 - keySpl := strings.Split(key, ".") - initKey := keySpl[len(keySpl)-1] - if !strings.HasPrefix(initKey, "init") { - continue + + // 入度为0说明是根节点 + if value.CalledTimes == 0 { + p.tree.ChildrenToPng(value, nil, graph) } - p.tree.ChildrenToPng(value, nil, graph) } + // 1. write to file directly if err := g.RenderFilename(graph, graphviz.PNG, p.outputPath); err != nil { log.Fatal(err) diff --git a/pkg/analysis/tree.go b/pkg/analysis/tree.go index f982b99..c0702a8 100644 --- a/pkg/analysis/tree.go +++ b/pkg/analysis/tree.go @@ -2,6 +2,7 @@ package analysis import ( "fmt" + "github.com/goccy/go-graphviz/cgraph" ) @@ -17,6 +18,8 @@ type FuncNode struct { Pkg string `json:"pkg"` Name string `json:"name"` + CalledTimes int `json:"calledTimes"` // 被调用次数 入度 + Parent []string `json:"parent"` // 通过key来索引 Children []string `json:"children"` // 通过key来索引 }