Class CallGraph
- java.lang.Object
-
- com.google.javascript.jscomp.CallGraph
-
- All Implemented Interfaces:
CompilerPass
public class CallGraph extends java.lang.Object implements CompilerPass
A pass the uses aDefinitionProvider
to compute a call graph for an AST.A
CallGraph
connectsCallGraph.Function
s toCallGraph.Callsite
s and vice versa: each function in the graph links to the callsites it contains and each callsite links to the functions it could call. Similarly, each callsite links to the function that contains it and each function links to the callsites that could call it.The callgraph is not precise. That is, a callsite may indicate it can call a function when in fact it does not do so in the running program.
The callgraph is also not complete: in some cases it may be unable to determine some targets of a callsite. In this case, Callsite.hasUnknownTarget() will return true.
The CallGraph doesn't (currently) have functions for externally defined functions; however, callsites that target externs will have hasExternTarget() return true.
TODO(dcc): Have CallGraph (optionally?) include functions for externs.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
CallGraph.Callsite
An inner class that represents call sites in the call graph.class
CallGraph.Function
An inner class that represents functions in the call graph.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
MAIN_FUNCTION_NAME
The name we give the main function.
-
Constructor Summary
Constructors Constructor Description CallGraph(AbstractCompiler compiler)
Creates a call graph object support both forward and backward lookups.CallGraph(AbstractCompiler compiler, boolean computeForwardGraph, boolean computeBackwardGraph)
Creates a call graph object supporting the specified lookups.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.Collection<CallGraph.Callsite>
getAllCallsites()
Returns a collection of all callsites in the call graph.java.util.Collection<CallGraph.Function>
getAllFunctions()
Returns a collection of all functions (including the main function) in the call graph.DiGraph<CallGraph.Function,CallGraph.Callsite>
getBackwardDirectedGraph()
Constructs and returns a directed graph where the nodes are functions and the edges are callsites connecting callees to callers.CallGraph.Callsite
getCallsiteForAstNode(Node callsiteNode)
Returns the call graph Callsite object corresponding to the provided AST Token.CALL or Token.NEW node, or null if no such object exists.DiGraph<CallGraph.Function,CallGraph.Callsite>
getForwardDirectedGraph()
Constructs and returns a directed graph where the nodes are functions and the edges are callsites connecting callers to callees.CallGraph.Function
getFunctionForAstNode(Node functionNode)
Returns the call graph Function object corresponding to the provided AST Token.FUNCTION node, or null if no such object exists.CallGraph.Function
getMainFunction()
Returns a Function object representing the "main" global function.CallGraph.Function
getUniqueFunctionWithName(java.lang.String desiredName)
Finds a function with the given name.void
process(Node externsRoot, Node jsRoot)
Builds a call graph for the given externsRoot and jsRoot.
-
-
-
Field Detail
-
MAIN_FUNCTION_NAME
public static final java.lang.String MAIN_FUNCTION_NAME
The name we give the main function.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
CallGraph
public CallGraph(AbstractCompiler compiler, boolean computeForwardGraph, boolean computeBackwardGraph)
Creates a call graph object supporting the specified lookups. At least one (and possibly both) of computeForwardGraph and computeBackwardGraph must be true.- Parameters:
compiler
- The compilercomputeForwardGraph
- Should the call graph allow lookup of the target functions a given callsite could call?computeBackwardGraph
- Should the call graph allow lookup of the callsites that could call a given function?
-
CallGraph
public CallGraph(AbstractCompiler compiler)
Creates a call graph object support both forward and backward lookups.
-
-
Method Detail
-
process
public void process(Node externsRoot, Node jsRoot)
Builds a call graph for the given externsRoot and jsRoot. This method must not be called more than once per CallGraph instance.- Specified by:
process
in interfaceCompilerPass
- Parameters:
externsRoot
- Top of external JS treejsRoot
- Top of JS tree
-
getFunctionForAstNode
public CallGraph.Function getFunctionForAstNode(Node functionNode)
Returns the call graph Function object corresponding to the provided AST Token.FUNCTION node, or null if no such object exists.
-
getMainFunction
public CallGraph.Function getMainFunction()
Returns a Function object representing the "main" global function.
-
getAllFunctions
public java.util.Collection<CallGraph.Function> getAllFunctions()
Returns a collection of all functions (including the main function) in the call graph.
-
getUniqueFunctionWithName
public CallGraph.Function getUniqueFunctionWithName(java.lang.String desiredName)
Finds a function with the given name. Throws an exception if there are no functions or multiple functions with the name. This is for testing purposes only.
-
getCallsiteForAstNode
public CallGraph.Callsite getCallsiteForAstNode(Node callsiteNode)
Returns the call graph Callsite object corresponding to the provided AST Token.CALL or Token.NEW node, or null if no such object exists.
-
getAllCallsites
public java.util.Collection<CallGraph.Callsite> getAllCallsites()
Returns a collection of all callsites in the call graph.
-
getForwardDirectedGraph
public DiGraph<CallGraph.Function,CallGraph.Callsite> getForwardDirectedGraph()
Constructs and returns a directed graph where the nodes are functions and the edges are callsites connecting callers to callees. It is safe to call this method on both forward and backwardly constructed CallGraphs.
-
getBackwardDirectedGraph
public DiGraph<CallGraph.Function,CallGraph.Callsite> getBackwardDirectedGraph()
Constructs and returns a directed graph where the nodes are functions and the edges are callsites connecting callees to callers. It is safe to call this method on both forward and backwardly constructed CallGraphs.
-
-