Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for VTA algorithm #207

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Usage of go-callvis:
-tests
Include test code.
-algo string
Use specific algorithm for package analyzer: static, cha or rta (default "static")
Use specific algorithm for package analyzer: static, cha, rta or vta (default "static")
-version
Show version and exit.
```
Expand Down
13 changes: 12 additions & 1 deletion analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"golang.org/x/tools/go/callgraph"
"golang.org/x/tools/go/callgraph/cha"
"golang.org/x/tools/go/callgraph/rta"
"golang.org/x/tools/go/callgraph/vta"
"golang.org/x/tools/go/callgraph/static"

"golang.org/x/tools/go/packages"
Expand All @@ -29,6 +30,7 @@ const (
CallGraphTypeStatic CallGraphType = "static"
CallGraphTypeCha CallGraphType = "cha"
CallGraphTypeRta CallGraphType = "rta"
CallGraphTypeVta CallGraphType = "vta"
)

// ==[ type def/func: analysis ]===============================================
Expand Down Expand Up @@ -134,6 +136,8 @@ func (a *analysis) DoAnalysis(
graph = static.CallGraph(prog)
case CallGraphTypeCha:
graph = cha.CallGraph(prog)
case CallGraphTypeVta:
fallthrough
case CallGraphTypeRta:
mains, err := mainPackages(prog.AllPackages())
if err != nil {
Expand All @@ -152,8 +156,15 @@ func (a *analysis) DoAnalysis(
for _, init := range inits {
roots = append(roots, init)
}

graph = rta.Analyze(roots, true).CallGraph

if algo == CallGraphTypeVta {
funcs := make(map[*ssa.Function]bool)
for fun := range graph.Nodes {
funcs[fun] = true
}
graph = vta.CallGraph(funcs, graph)
}
default:
return fmt.Errorf("invalid call graph type: %s", a.opts.algo)
}
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ var (
outputFile = flag.String("file", "", "output filename - omit to use server mode")
outputFormat = flag.String("format", "svg", "output file format [svg | png | jpg | ...]")
cacheDir = flag.String("cacheDir", "", "Enable caching to avoid unnecessary re-rendering, you can force rendering by adding 'refresh=true' to the URL query or emptying the cache directory")
callgraphAlgo = flag.String("algo", string(CallGraphTypeStatic), fmt.Sprintf("The algorithm used to construct the call graph. Possible values inlcude: %q, %q, %q",
CallGraphTypeStatic, CallGraphTypeCha, CallGraphTypeRta))
callgraphAlgo = flag.String("algo", string(CallGraphTypeStatic), fmt.Sprintf("The algorithm used to construct the call graph. Possible values inlcude: %q, %q, %q, %q",
CallGraphTypeStatic, CallGraphTypeCha, CallGraphTypeRta, CallGraphTypeVta))

debugFlag = flag.Bool("debug", false, "Enable verbose log.")
versionFlag = flag.Bool("version", false, "Show version and exit.")
Expand Down
Loading