Search in sources :

Example 6 with AllVarsDeclaredInFunction

use of com.google.javascript.jscomp.NodeUtil.AllVarsDeclaredInFunction in project closure-compiler by google.

the class ReachingUseDefTester method computeReachingUses.

/**
 * Runs `MaybeReachingVariableUse` pass to compute and store the may-be-reaching uses for all
 * definitions of each variable in the test source.
 */
void computeReachingUses(String src, boolean async) {
    Node script = parseScript(src, async);
    root = script.getFirstChild();
    Scope funcBlockScope = computeFunctionBlockScope(script, root);
    ControlFlowGraph<Node> cfg = computeCfg(root);
    HashSet<Var> escaped = new HashSet<>();
    AllVarsDeclaredInFunction allVarsDeclaredInFunction = NodeUtil.getAllVarsDeclaredInFunction(compiler, scopeCreator, funcBlockScope.getParent());
    Map<String, Var> allVarsInFn = allVarsDeclaredInFunction.getAllVariables();
    DataFlowAnalysis.computeEscaped(funcBlockScope.getParent(), escaped, compiler, scopeCreator, allVarsInFn);
    reachingUse = new MaybeReachingVariableUse(cfg, escaped, allVarsInFn);
    reachingUse.analyze();
}
Also used : AllVarsDeclaredInFunction(com.google.javascript.jscomp.NodeUtil.AllVarsDeclaredInFunction) Node(com.google.javascript.rhino.Node) HashSet(java.util.HashSet)

Example 7 with AllVarsDeclaredInFunction

use of com.google.javascript.jscomp.NodeUtil.AllVarsDeclaredInFunction in project closure-compiler by google.

the class LiveVariablesAnalysisTest method computeLiveness.

private static LiveVariablesAnalysis computeLiveness(String src, boolean async) {
    // Set up compiler
    Compiler compiler = new Compiler();
    CompilerOptions options = new CompilerOptions();
    options.setLanguage(LanguageMode.UNSUPPORTED);
    options.setCodingConvention(new GoogleCodingConvention());
    compiler.initOptions(options);
    compiler.setLifeCycleStage(LifeCycleStage.NORMALIZED);
    // Set up test case
    src = (async ? "async " : "") + "function _FUNCTION(param1, param2 = 1, ...param3){" + src + "}";
    Node n = compiler.parseTestCode(src).removeFirstChild();
    checkState(n.isFunction(), n);
    Node script = new Node(Token.SCRIPT, n);
    script.setInputId(new InputId("test"));
    assertThat(compiler.getErrors()).isEmpty();
    // Create scopes
    SyntacticScopeCreator scopeCreator = new SyntacticScopeCreator(compiler);
    Scope scope = scopeCreator.createScope(n, Scope.createGlobalScope(script));
    Scope childScope = scopeCreator.createScope(NodeUtil.getFunctionBody(n), scope);
    // Control flow graph
    ControlFlowAnalysis cfa = new ControlFlowAnalysis(compiler, false, true);
    cfa.process(null, n);
    ControlFlowGraph<Node> cfg = cfa.getCfg();
    // All variables declared in function
    AllVarsDeclaredInFunction allVarsDeclaredInFunction = NodeUtil.getAllVarsDeclaredInFunction(compiler, scopeCreator, scope);
    // Compute liveness of variables
    LiveVariablesAnalysis analysis = new LiveVariablesAnalysis(cfg, scope, childScope, compiler, scopeCreator, allVarsDeclaredInFunction);
    analysis.analyze();
    return analysis;
}
Also used : AllVarsDeclaredInFunction(com.google.javascript.jscomp.NodeUtil.AllVarsDeclaredInFunction) Node(com.google.javascript.rhino.Node) InputId(com.google.javascript.rhino.InputId)

Aggregations

AllVarsDeclaredInFunction (com.google.javascript.jscomp.NodeUtil.AllVarsDeclaredInFunction)7 Node (com.google.javascript.rhino.Node)7 DiGraphNode (com.google.javascript.jscomp.graph.DiGraph.DiGraphNode)3 HashSet (java.util.HashSet)3 GraphNode (com.google.javascript.jscomp.graph.GraphNode)2 InputId (com.google.javascript.rhino.InputId)2 GreedyGraphColoring (com.google.javascript.jscomp.graph.GraphColoring.GreedyGraphColoring)1 LinkedHashSet (java.util.LinkedHashSet)1