callgraph

package
v0.0.0-...-c7609e9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 9, 2021 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name:             "callgraph",
	Doc:              "computes an approximate callgraph based on function arity, name, and nothing else",
	Run:              run,
	RunDespiteErrors: true,
	Requires:         []*analysis.Analyzer{inspect.Analyzer},
	ResultType:       reflect.TypeOf((*Result)(nil)),
}

Analyzer provides an approximate callgraph based on function name and arity. Edges in the callgraph are only present if both functions are declared in the source and both take pointer arguments.

View Source
var ErrSignatureMissing error = errors.New("requested root signature does not appear in callgraph")

ErrSignatureMissing is returned when a request signature could not be found.

Functions

This section is empty.

Types

type Call

type Call struct {
	Signature
	// Caller is the signature and position of the function which makes this call.
	Caller DeclaredSignature
	// Pos is the source position of the call.
	Pos token.Pos
	// ArgDeclPos contains the source positions of the declaration of each call's arguments in order.
	ArgDeclPos []token.Pos
}

Call captures the signature of function calls along with the signature of the function from which they are called and position of their argument declarations.

type CallGraph

type CallGraph struct {
	// contains filtered or unexported fields
}

CallGraph represents an approximate call-graph, relying only on the name and arity of each function. A call-graph has a node for each function, and edges between two nodes a and b if function a calls function b. The call-graph this package computes is approximate in the sense that two functions with the same name and arity are considered equivalent. The resulting graph is a coarsening of the actual call-graph in the sense that two functions with matching signatures may refer to the same node in the resulting call graph. When this happens, the 'calls' relation described by the edges of the graph is preserved. In more complicated terms, when the approximate call-graph and the actual call-graph are viewed as categories, they are related by a forgetful functor which discards everything other than the function name and arity.

The CallGraph also makes it easy to access the called-by graph, which is the callgraph with all edges reversed.

func NewCallGraph

func NewCallGraph() CallGraph

NewCallGraph creates an empty callgraph.

func (*CallGraph) AddCall

func (cg *CallGraph) AddCall(callerID, callID int)

AddCall adds an edge in the callgraph from the callerID to the callID; no attempt is made to check if the two signatures IDs exist in the graph. Therefore, users can corrupt this data structure if they are not careful.

func (*CallGraph) AddSignature

func (cg *CallGraph) AddSignature(sig Signature) int

AddSignature adds a new signature to the callgraph and returns an ID that can later be used to refer to it.

func (*CallGraph) BFSWithStack

func (cg *CallGraph) BFSWithStack(root Signature, visit func(sig Signature, stack []Signature)) error

BFSWithStack performs a breadth-first search of the callgraph, starting from the provided root node. The provided visit function is called once for every node visited during the search. Each node in the graph is visited once for every path from the provided root node

func (*CallGraph) CalledByBFS

func (cg *CallGraph) CalledByBFS(roots []Signature, visit func(sig Signature))

CalledByBFS performs a breadth-first search of the called-by graph, starting from the set of roots provided. The provided visit function is called for every node visited during the search. Each node in the graph is visited at most once.

func (*CallGraph) CalledByRoots

func (cg *CallGraph) CalledByRoots() []Signature

CalledByRoots returns a list of the nodes in the called-by graph which do not have any incoming edges (i.e. the signatures of functions which do not call any functions).

type DeclaredSignature

type DeclaredSignature struct {
	Signature
	Pos token.Pos
}

DeclaredSignature is a signature along with the position of its declaration.

type Result

type Result struct {
	// PtrSignatures contains a record for each declared function signature found to contain a pointer
	// during analysis.
	PtrSignatures []DeclaredSignature
	// PtrCalls contains a record for each function call to a function with a pointer signature found
	// during analysis.
	PtrCalls []Call
	// ApproxCallGraph contains the approximate call graph computed by the analyzer. An edge is present
	// in the callgraph between each pair of functions whose declarations are found in the source material
	// and which accept a pointer variable in their signature.
	ApproxCallGraph *CallGraph
}

Result is the result of the callgraph analyzer.

type Signature

type Signature struct {
	Name  string
	Arity int
}

Signature is only an approximation of the information needed to make a function call. It captures only name and arity.

func SignatureFromCallExpr

func SignatureFromCallExpr(call *ast.CallExpr) Signature

SignatureFromCallExpr retrieves the signature of a call expression.

func SignatureFromFuncDecl

func SignatureFromFuncDecl(fdec *ast.FuncDecl) Signature

SignatureFromFuncDecl retrieves a signature from the provided FuncDecl.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL