github.com/jujuyuki/gospal@v1.0.1-0.20210215170718-af79fae13b20/ssa/build/pointer.go (about)

     1  package build
     2  
     3  // Pointer analysis helper functions.
     4  // Most of the functions in this file are modified from golan.org/x/tools/oracle
     5  
     6  import (
     7  	"fmt"
     8  	"io"
     9  
    10  	"golang.org/x/tools/go/loader"
    11  	"golang.org/x/tools/go/pointer"
    12  	"golang.org/x/tools/go/ssa"
    13  )
    14  
    15  // Create a pointer.Config whose scope is the initial packages of lprog
    16  // and their dependencies.
    17  func setupPTA(prog *ssa.Program, lprog *loader.Program, ptaLog io.Writer) (*pointer.Config, error) {
    18  	// TODO(adonovan): the body of this function is essentially
    19  	// duplicated in all go/pointer clients.  Refactor.
    20  
    21  	// For each initial package (specified on the command line),
    22  	// if it has a main function, analyze that,
    23  	// otherwise analyze its tests, if any.
    24  	var testPkgs, mains []*ssa.Package
    25  	for _, info := range lprog.InitialPackages() {
    26  		initialPkg := prog.Package(info.Pkg)
    27  
    28  		// Add package to the pointer analysis scope.
    29  		if initialPkg.Func("main") != nil {
    30  			mains = append(mains, initialPkg)
    31  		} else {
    32  			testPkgs = append(testPkgs, initialPkg)
    33  		}
    34  	}
    35  	if testPkgs != nil {
    36  		for _, testPkg := range testPkgs {
    37  			if p := prog.CreateTestMainPackage(testPkg); p != nil {
    38  				mains = append(mains, p)
    39  			}
    40  		}
    41  	}
    42  	if mains == nil {
    43  		return nil, fmt.Errorf("analysis scope has no main and no tests")
    44  	}
    45  	return &pointer.Config{
    46  		Log:        ptaLog,
    47  		Mains:      mains,
    48  		Reflection: false, // We don't consider reflection in our analysis.
    49  	}, nil
    50  }