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 package init functions when algo is rta #199

Merged
merged 2 commits into from
Dec 30, 2024
Merged
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
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
Loading