Class CallGraph

java.lang.Object
com.google.javascript.jscomp.CallGraph
All Implemented Interfaces:
CompilerPass

public class CallGraph extends Object implements CompilerPass
A pass the uses a DefinitionProvider to compute a call graph for an AST.

A CallGraph connects CallGraph.Functions to CallGraph.Callsites 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.

  • Field Details

    • MAIN_FUNCTION_NAME

      public static final String MAIN_FUNCTION_NAME
      The name we give the main function.
      See Also:
  • Constructor Details

    • 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 compiler
      computeForwardGraph - 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 Details

    • 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 interface CompilerPass
      Parameters:
      externsRoot - Top of external JS tree
      jsRoot - 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 Collection<CallGraph.Function> getAllFunctions()
      Returns a collection of all functions (including the main function) in the call graph.
    • getUniqueFunctionWithName

      public CallGraph.Function getUniqueFunctionWithName(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 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.