Java Code Examples for com.google.javascript.rhino.Node#isTry()

The following examples show how to use com.google.javascript.rhino.Node#isTry() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: 1_ControlFlowAnalysis.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
private void handleContinue(Node node) {
  String label = null;
  if (node.hasChildren()) {
    label = node.getFirstChild().getString();
  }
  Node cur;
  Node previous = null;
  Node lastJump;

  // Similar to handBreak's logic with a few minor variation.
  Node parent = node.getParent();
  for (cur = node, lastJump = node;
      !isContinueTarget(cur, parent, label);
      cur = parent, parent = parent.getParent()) {
    if (cur.isTry() && NodeUtil.hasFinally(cur)
        && cur.getLastChild() != previous) {
      if (lastJump == node) {
        createEdge(lastJump, Branch.UNCOND, cur.getLastChild());
      } else {
        finallyMap.put(lastJump, computeFallThrough(cur.getLastChild()));
      }
      lastJump = cur;
    }
    Preconditions.checkState(parent != null, "Cannot find continue target.");
    previous = cur;
  }
  Node iter = cur;
  if (cur.getChildCount() == 4) {
    iter = cur.getFirstChild().getNext().getNext();
  }

  if (lastJump == node) {
    createEdge(node, Branch.UNCOND, iter);
  } else {
    finallyMap.put(lastJump, iter);
  }
}
 
Example 2
Source File: 1_ControlFlowAnalysis.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
private void handleStmtList(Node node) {
  Node parent = node.getParent();
  // Special case, don't add a block of empty CATCH block to the graph.
  if (node.isBlock() && parent != null &&
      parent.isTry() &&
      NodeUtil.getCatchBlock(parent) == node &&
      !NodeUtil.hasCatchHandler(node)) {
    return;
  }

  // A block transfer control to its first child if it is not empty.
  Node child = node.getFirstChild();

  // Function declarations are skipped since control doesn't go into that
  // function (unless it is called)
  while (child != null && child.isFunction()) {
    child = child.getNext();
  }

  if (child != null) {
    createEdge(node, Branch.UNCOND, computeFallThrough(child));
  } else {
    createEdge(node, Branch.UNCOND, computeFollowNode(node, this));
  }

  // Synthetic blocks
  if (parent != null) {
    switch (parent.getType()) {
      case Token.DEFAULT_CASE:
      case Token.CASE:
      case Token.TRY:
        break;
      default:
        if (node.isBlock() && node.isSyntheticBlock()) {
          createEdge(node, Branch.SYN_BLOCK, computeFollowNode(node, this));
        }
        break;
    }
  }
}
 
Example 3
Source File: Closure_14_ControlFlowAnalysis_t.java    From coming with MIT License 5 votes vote down vote up
private void handleStmtList(Node node) {
  Node parent = node.getParent();
  // Special case, don't add a block of empty CATCH block to the graph.
  if (node.isBlock() && parent != null &&
      parent.isTry() &&
      NodeUtil.getCatchBlock(parent) == node &&
      !NodeUtil.hasCatchHandler(node)) {
    return;
  }

  // A block transfer control to its first child if it is not empty.
  Node child = node.getFirstChild();

  // Function declarations are skipped since control doesn't go into that
  // function (unless it is called)
  while (child != null && child.isFunction()) {
    child = child.getNext();
  }

  if (child != null) {
    createEdge(node, Branch.UNCOND, computeFallThrough(child));
  } else {
    createEdge(node, Branch.UNCOND, computeFollowNode(node, this));
  }

  // Synthetic blocks
  if (parent != null) {
    switch (parent.getType()) {
      case Token.DEFAULT_CASE:
      case Token.CASE:
      case Token.TRY:
        break;
      default:
        if (node.isBlock() && node.isSyntheticBlock()) {
          createEdge(node, Branch.SYN_BLOCK, computeFollowNode(node, this));
        }
        break;
    }
  }
}
 
Example 4
Source File: 1_ControlFlowAnalysis.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
private void handleStmtList(Node node) {
  Node parent = node.getParent();
  // Special case, don't add a block of empty CATCH block to the graph.
  if (node.isBlock() && parent != null &&
      parent.isTry() &&
      NodeUtil.getCatchBlock(parent) == node &&
      !NodeUtil.hasCatchHandler(node)) {
    return;
  }

  // A block transfer control to its first child if it is not empty.
  Node child = node.getFirstChild();

  // Function declarations are skipped since control doesn't go into that
  // function (unless it is called)
  while (child != null && child.isFunction()) {
    child = child.getNext();
  }

  if (child != null) {
    createEdge(node, Branch.UNCOND, computeFallThrough(child));
  } else {
    createEdge(node, Branch.UNCOND, computeFollowNode(node, this));
  }

  // Synthetic blocks
  if (parent != null) {
    switch (parent.getType()) {
      case Token.DEFAULT_CASE:
      case Token.CASE:
      case Token.TRY:
        break;
      default:
        if (node.isBlock() && node.isSyntheticBlock()) {
          createEdge(node, Branch.SYN_BLOCK, computeFollowNode(node, this));
        }
        break;
    }
  }
}
 
Example 5
Source File: Closure_14_ControlFlowAnalysis_s.java    From coming with MIT License 5 votes vote down vote up
private void handleContinue(Node node) {
  String label = null;
  if (node.hasChildren()) {
    label = node.getFirstChild().getString();
  }
  Node cur;
  Node previous = null;
  Node lastJump;

  // Similar to handBreak's logic with a few minor variation.
  Node parent = node.getParent();
  for (cur = node, lastJump = node;
      !isContinueTarget(cur, parent, label);
      cur = parent, parent = parent.getParent()) {
    if (cur.isTry() && NodeUtil.hasFinally(cur)
        && cur.getLastChild() != previous) {
      if (lastJump == node) {
        createEdge(lastJump, Branch.UNCOND, cur.getLastChild());
      } else {
        finallyMap.put(lastJump, computeFallThrough(cur.getLastChild()));
      }
      lastJump = cur;
    }
    Preconditions.checkState(parent != null, "Cannot find continue target.");
    previous = cur;
  }
  Node iter = cur;
  if (cur.getChildCount() == 4) {
    iter = cur.getFirstChild().getNext().getNext();
  }

  if (lastJump == node) {
    createEdge(node, Branch.UNCOND, iter);
  } else {
    finallyMap.put(lastJump, iter);
  }
}
 
Example 6
Source File: ControlFlowAnalysis.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private void handleBreak(Node node) {
  String label = null;
  // See if it is a break with label.
  if (node.hasChildren()) {
    label = node.getFirstChild().getString();
  }
  Node cur;
  Node previous = null;
  Node lastJump;
  Node parent = node.getParent();
  /*
   * Continuously look up the ancestor tree for the BREAK target or the target
   * with the corresponding label and connect to it. If along the path we
   * discover a FINALLY, we will connect the BREAK to that FINALLY. From then
   * on, we will just record the control flow changes in the finallyMap. This
   * is due to the fact that we need to connect any node that leaves its own
   * FINALLY block to the outer FINALLY or the BREAK's target but those nodes
   * are not known yet due to the way we traverse the nodes.
   */
  for (cur = node, lastJump = node;
      !isBreakTarget(cur, label);
      cur = parent, parent = parent.getParent()) {
    if (cur.isTry() && NodeUtil.hasFinally(cur)
        && cur.getLastChild() != previous) {
      if (lastJump == node) {
        createEdge(lastJump, Branch.UNCOND, computeFallThrough(
            cur.getLastChild()));
      } else {
        finallyMap.put(lastJump, computeFallThrough(cur.getLastChild()));
      }
      lastJump = cur;
    }
    if (parent == null) {
      if (compiler.isIdeMode()) {
        // In IDE mode, we expect that the data flow graph may
        // not be well-formed.
        return;
      } else {
        throw new IllegalStateException("Cannot find break target.");
      }
    }
    previous = cur;
  }
  if (lastJump == node) {
    createEdge(lastJump, Branch.UNCOND, computeFollowNode(cur, this));
  } else {
    finallyMap.put(lastJump, computeFollowNode(cur, this));
  }
}
 
Example 7
Source File: Closure_10_NodeUtil_s.java    From coming with MIT License 4 votes vote down vote up
/** Whether the node is a CATCH container BLOCK. */
static boolean isTryCatchNodeContainer(Node n) {
  Node parent = n.getParent();
  return parent.isTry()
      && parent.getFirstChild().getNext() == n;
}
 
Example 8
Source File: Cardumen_0087_s.java    From coming with MIT License 4 votes vote down vote up
/** Whether the child node is the FINALLY block of a try. */
static boolean isTryFinallyNode(Node parent, Node child) {
  return parent.isTry() && parent.getChildCount() == 3
      && child == parent.getLastChild();
}
 
Example 9
Source File: NodeUtil.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** Whether the child node is the FINALLY block of a try. */
static boolean isTryFinallyNode(Node parent, Node child) {
  return parent.isTry() && parent.getChildCount() == 3
      && child == parent.getLastChild();
}
 
Example 10
Source File: jMutRepair_003_s.java    From coming with MIT License 4 votes vote down vote up
/** Whether the child node is the FINALLY block of a try. */
static boolean isTryFinallyNode(Node parent, Node child) {
  return parent.isTry() && parent.getChildCount() == 3
      && child == parent.getLastChild();
}
 
Example 11
Source File: Cardumen_0014_t.java    From coming with MIT License 4 votes vote down vote up
/** Whether the child node is the FINALLY block of a try. */
static boolean isTryFinallyNode(Node parent, Node child) {
  return parent.isTry() && parent.getChildCount() == 3
      && child == parent.getLastChild();
}
 
Example 12
Source File: jMutRepair_003_s.java    From coming with MIT License 4 votes vote down vote up
/** Whether the node is a CATCH container BLOCK. */
static boolean isTryCatchNodeContainer(Node n) {
  Node parent = n.getParent();
  return parent.isTry()
      && parent.getFirstChild().getNext() == n;
}
 
Example 13
Source File: Cardumen_0087_t.java    From coming with MIT License 4 votes vote down vote up
/** Whether the node is a CATCH container BLOCK. */
static boolean isTryCatchNodeContainer(Node n) {
  Node parent = n.getParent();
  return parent.isTry()
      && parent.getFirstChild().getNext() == n;
}
 
Example 14
Source File: Cardumen_00200_t.java    From coming with MIT License 4 votes vote down vote up
/** Whether the node is a CATCH container BLOCK. */
static boolean isTryCatchNodeContainer(Node n) {
  Node parent = n.getParent();
  return parent.isTry()
      && parent.getFirstChild().getNext() == n;
}
 
Example 15
Source File: Closure_10_NodeUtil_t.java    From coming with MIT License 4 votes vote down vote up
/** Whether the node is a CATCH container BLOCK. */
static boolean isTryCatchNodeContainer(Node n) {
  Node parent = n.getParent();
  return parent.isTry()
      && parent.getFirstChild().getNext() == n;
}
 
Example 16
Source File: Cardumen_00200_s.java    From coming with MIT License 4 votes vote down vote up
/** Whether the node is a CATCH container BLOCK. */
static boolean isTryCatchNodeContainer(Node n) {
  Node parent = n.getParent();
  return parent.isTry()
      && parent.getFirstChild().getNext() == n;
}
 
Example 17
Source File: jMutRepair_003_t.java    From coming with MIT License 4 votes vote down vote up
/** Whether the child node is the FINALLY block of a try. */
static boolean isTryFinallyNode(Node parent, Node child) {
  return parent.isTry() && parent.getChildCount() == 3
      && child == parent.getLastChild();
}
 
Example 18
Source File: Closure_127_UnreachableCodeElimination_t.java    From coming with MIT License 4 votes vote down vote up
private void removeDeadExprStatementSafely(Node n) {
  Node parent = n.getParent();
  if (n.isEmpty() || (n.isBlock() && !n.hasChildren())) {
    // Not always trivial to remove, let FoldConstants work its magic later.
    return;
  }

  // TODO(user): This is a problem with removeNoOpStatements.
  // Every expression in a FOR-IN header looks side effect free on its own.
  if (NodeUtil.isForIn(parent)) {
    return;
  }

  switch (n.getType()) {
    // Removing an unreachable DO node is messy b/c it means we still have
    // to execute one iteration. If the DO's body has breaks in the middle,
    // it can get even more tricky and code size might actually increase.
    case Token.DO:
      return;

    case Token.BLOCK:
      // BLOCKs are used in several ways including wrapping CATCH
      // blocks in TRYs
      if (parent.isTry() && NodeUtil.isTryCatchNodeContainer(n)) {
        return;
      }
      break;

    case Token.CATCH:
      Node tryNode = parent.getParent();
      NodeUtil.maybeAddFinally(tryNode);
      break;
  }

  if (n.isVar() && !n.getFirstChild().hasChildren()) {
    // Very unlikely case, Consider this:
    // File 1: {throw 1}
    // File 2: {var x}
    // The node var x is unreachable in the global scope.
    // Before we remove the node, redeclareVarsInsideBranch
    // would basically move var x to the beginning of File 2,
    // which resulted in zero changes to the AST but triggered
    // reportCodeChange().
    // Instead, we should just ignore dead variable declarations.
    return;
  }

  removeNode(n);
}
 
Example 19
Source File: 1_ControlFlowAnalysis.java    From SimFix with GNU General Public License v2.0 4 votes vote down vote up
private void handleBreak(Node node) {
  String label = null;
  // See if it is a break with label.
  if (node.hasChildren()) {
    label = node.getFirstChild().getString();
  }
  Node cur;
  Node previous = null;
  Node lastJump;
  Node parent = node.getParent();
  /*
   * Continuously look up the ancestor tree for the BREAK target or the target
   * with the corresponding label and connect to it. If along the path we
   * discover a FINALLY, we will connect the BREAK to that FINALLY. From then
   * on, we will just record the control flow changes in the finallyMap. This
   * is due to the fact that we need to connect any node that leaves its own
   * FINALLY block to the outer FINALLY or the BREAK's target but those nodes
   * are not known yet due to the way we traverse the nodes.
   */
  for (cur = node, lastJump = node;
      !isBreakTarget(cur, label);
      cur = parent, parent = parent.getParent()) {
    if (cur.isTry() && NodeUtil.hasFinally(cur)
        && cur.getLastChild() != previous) {
      if (lastJump == node) {
        createEdge(lastJump, Branch.UNCOND, computeFallThrough(
            cur.getLastChild()));
      } else {
        finallyMap.put(lastJump, computeFallThrough(cur.getLastChild()));
      }
      lastJump = cur;
    }
    if (parent == null) {
      if (compiler.isIdeMode()) {
        // In IDE mode, we expect that the data flow graph may
        // not be well-formed.
        return;
      } else {
        throw new IllegalStateException("Cannot find break target.");
      }
    }
    previous = cur;
  }
  if (lastJump == node) {
    createEdge(lastJump, Branch.UNCOND, computeFollowNode(cur, this));
  } else {
    finallyMap.put(lastJump, computeFollowNode(cur, this));
  }
}
 
Example 20
Source File: jMutRepair_003_t.java    From coming with MIT License 4 votes vote down vote up
/** Whether the node is a CATCH container BLOCK. */
static boolean isTryCatchNodeContainer(Node n) {
  Node parent = n.getParent();
  return parent.isTry()
      && parent.getFirstChild().getNext() == n;
}