func printProgram(prog *loader.Program) { // Created packages are the initial packages specified by a call // to CreateFromFilenames or CreateFromFiles. var names []string for _, info := range prog.Created { names = append(names, info.Pkg.Path()) } fmt.Printf("created: %s\n", names) // Imported packages are the initial packages specified by a // call to Import or ImportWithTests. names = nil for _, info := range prog.Imported { names = append(names, info.Pkg.Path()) } sort.Strings(names) fmt.Printf("imported: %s\n", names) // InitialPackages contains the union of created and imported. names = nil for _, info := range prog.InitialPackages() { names = append(names, info.Pkg.Path()) } sort.Strings(names) fmt.Printf("initial: %s\n", names) // AllPackages contains all initial packages and their dependencies. names = nil for pkg := range prog.AllPackages { names = append(names, pkg.Path()) } sort.Strings(names) fmt.Printf("all: %s\n", names) }
// Create a pointer.Config whose scope is the initial packages of lprog // and their dependencies. func setupPTA(prog *ssa.Program, lprog *loader.Program, ptaLog io.Writer, reflection bool) (*pointer.Config, error) { // TODO(adonovan): the body of this function is essentially // duplicated in all go/pointer clients. Refactor. // For each initial package (specified on the command line), // if it has a main function, analyze that, // otherwise analyze its tests, if any. var testPkgs, mains []*ssa.Package for _, info := range lprog.InitialPackages() { initialPkg := prog.Package(info.Pkg) // Add package to the pointer analysis scope. if initialPkg.Func("main") != nil { mains = append(mains, initialPkg) } else { testPkgs = append(testPkgs, initialPkg) } } if testPkgs != nil { if p := prog.CreateTestMainPackage(testPkgs...); p != nil { mains = append(mains, p) } } if mains == nil { return nil, fmt.Errorf("analysis scope has no main and no tests") } return &pointer.Config{ Log: ptaLog, Reflection: reflection, Mains: mains, }, nil }
// FindMains returns the set of all packages loaded into the given // loader.Program which contain main functions func FindMains(p *loader.Program, s *ssa.Program) []*ssa.Package { ips := p.InitialPackages() mains := make([]*ssa.Package, 0, len(ips)) for _, info := range ips { ssaPkg := s.Package(info.Pkg) if ssaPkg.Func("main") != nil { mains = append(mains, ssaPkg) } } return mains }
func newOracle(iprog *loader.Program, ptalog io.Writer, needs int, reflection bool) (*Oracle, error) { o := &Oracle{fset: iprog.Fset} // Retain type info for all ASTs in the program. if needs&needRetainTypeInfo != 0 { o.typeInfo = iprog.AllPackages } // Create SSA package for the initial packages and their dependencies. if needs&needSSA != 0 { var mode ssa.BuilderMode if needs&needSSADebug != 0 { mode |= ssa.GlobalDebug } prog := ssa.Create(iprog, mode) // For each initial package (specified on the command line), // if it has a main function, analyze that, // otherwise analyze its tests, if any. var testPkgs, mains []*ssa.Package for _, info := range iprog.InitialPackages() { initialPkg := prog.Package(info.Pkg) // Add package to the pointer analysis scope. if initialPkg.Func("main") != nil { mains = append(mains, initialPkg) } else { testPkgs = append(testPkgs, initialPkg) } } if testPkgs != nil { if p := prog.CreateTestMainPackage(testPkgs...); p != nil { mains = append(mains, p) } } if mains == nil { return nil, fmt.Errorf("analysis scope has no main and no tests") } o.ptaConfig.Log = ptalog o.ptaConfig.Reflection = reflection o.ptaConfig.Mains = mains o.prog = prog } return o, nil }
// Create a pointer.Config whose scope is the initial packages of lprog // and their dependencies. func setupPTA(prog *ssa.Program, lprog *loader.Program, ptaLog io.Writer, reflection bool) (*pointer.Config, error) { // For each initial package (specified on the command line), // if it has a main function, analyze that, // otherwise analyze its tests, if any. var mains []*ssa.Package for _, info := range lprog.InitialPackages() { p := prog.Package(info.Pkg) // Add package to the pointer analysis scope. if p.Pkg.Name() == "main" && p.Func("main") != nil { mains = append(mains, p) } else if main := prog.CreateTestMainPackage(p); main != nil { mains = append(mains, main) } } if mains == nil { return nil, fmt.Errorf("analysis scope has no main and no tests") } return &pointer.Config{ Log: ptaLog, Reflection: reflection, Mains: mains, }, nil }