Skip to content

Commit

Permalink
add package init functions when algo is rta (#199)
Browse files Browse the repository at this point in the history
Co-authored-by: Ondrej Fabry <[email protected]>
  • Loading branch information
cnzhujie and ondrajz authored Dec 30, 2024
1 parent 7306502 commit 5341ae2
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ func mainPackages(pkgs []*ssa.Package) ([]*ssa.Package, error) {
return mains, nil
}

// initFuncs returns all package init functions
func initFuncs(pkgs []*ssa.Package) ([]*ssa.Function, error) {
var inits []*ssa.Function
for _, p := range pkgs {
if p == nil {
continue
}
for name, member := range p.Members {
fun, ok := member.(*ssa.Function)
if !ok {
continue
}
if name == "init" || strings.HasPrefix(name, "init#") {
inits = append(inits, fun)
}
}
}
return inits, nil
}

//==[ type def/func: analysis ]===============================================
type analysis struct {
opts *renderOpts
Expand Down Expand Up @@ -119,6 +139,15 @@ func (a *analysis) DoAnalysis(
for _, main := range mains {
roots = append(roots, main.Func("main"))
}

inits, err := initFuncs(prog.AllPackages())
if err != nil {
return err
}
for _, init := range inits {
roots = append(roots, init)
}

graph = rta.Analyze(roots, true).CallGraph
case CallGraphTypePointer:
mains, err := mainPackages(prog.AllPackages())
Expand Down

0 comments on commit 5341ae2

Please sign in to comment.