Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions pkg/analysis/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)

/**
Expand Down Expand Up @@ -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
})
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions pkg/analysis/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package analysis

import (
"fmt"

"github.com/goccy/go-graphviz/cgraph"
)

Expand All @@ -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来索引
}
Expand Down