Search in sources :

Example 51 with ControlFlowElement

use of org.eclipse.n4js.n4JS.ControlFlowElement in project n4js by eclipse.

the class DeadCodeAnalyser method getReachableContainer.

/**
 * Finds the nearest reachable {@link Block} of the given {@link ControlFlowElement}
 */
private EObject getReachableContainer(Set<ControlFlowElement> unreachableElems, ControlFlowElement unreachableElem, Set<ControlFlowElement> moreUnreachableElems) {
    EObject elemContainer = unreachableElem.eContainer();
    if (elemContainer instanceof ExpressionStatement) {
        moreUnreachableElems.add((ExpressionStatement) elemContainer);
    }
    EObject block = EcoreUtil2.getContainerOfType(unreachableElem, Block.class);
    if (block == null) {
        block = EcoreUtil2.getContainerOfType(unreachableElem, Script.class);
    }
    EObject blockContainer = block.eContainer();
    boolean isDeadContainer = blockContainer instanceof ControlFlowElement;
    isDeadContainer &= isDeadContainer && FGUtils.isControlStatement((ControlFlowElement) blockContainer);
    isDeadContainer &= isDeadContainer && isDeadCode((ControlFlowElement) blockContainer);
    if (isDeadContainer) {
        ControlFlowElement cfe = (ControlFlowElement) blockContainer;
        moreUnreachableElems.add(cfe);
        return getReachableContainer(unreachableElems, cfe, moreUnreachableElems);
    }
    return block;
}
Also used : Script(org.eclipse.n4js.n4JS.Script) EObject(org.eclipse.emf.ecore.EObject) ExpressionStatement(org.eclipse.n4js.n4JS.ExpressionStatement) ControlFlowElement(org.eclipse.n4js.n4JS.ControlFlowElement)

Example 52 with ControlFlowElement

use of org.eclipse.n4js.n4JS.ControlFlowElement in project n4js by eclipse.

the class DeadCodeAnalyser method getDeadCodeRegion.

private DeadCodeRegion getDeadCodeRegion(Set<ControlFlowElement> deadCodeGroup) {
    int startIdx = Integer.MAX_VALUE;
    int endIdx = 0;
    int firstElementOffset = Integer.MAX_VALUE;
    ControlFlowElement firstElement = null;
    for (ControlFlowElement deadCodeElement : deadCodeGroup) {
        ICompositeNode compNode = NodeModelUtils.findActualNodeFor(deadCodeElement);
        int elemStartIdx = compNode.getOffset();
        int elemEndIdx = elemStartIdx + compNode.getLength();
        startIdx = Math.min(startIdx, elemStartIdx);
        endIdx = Math.max(endIdx, elemEndIdx);
        if (elemStartIdx < firstElementOffset) {
            firstElementOffset = elemStartIdx;
            firstElement = deadCodeElement;
        }
    }
    ControlFlowElement containerCFE = flowAnalyzer.getContainer(firstElement);
    ControlFlowElement reachablePredecessor = findPrecedingStatement(firstElement);
    return new DeadCodeRegion(startIdx, endIdx - startIdx, containerCFE, reachablePredecessor);
}
Also used : ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) ControlFlowElement(org.eclipse.n4js.n4JS.ControlFlowElement)

Aggregations

ControlFlowElement (org.eclipse.n4js.n4JS.ControlFlowElement)52 EObject (org.eclipse.emf.ecore.EObject)12 ComplexNode (org.eclipse.n4js.flowgraphs.model.ComplexNode)12 LinkedList (java.util.LinkedList)10 RepresentingNode (org.eclipse.n4js.flowgraphs.model.RepresentingNode)10 ParameterParser (org.eclipse.xpect.parameter.ParameterParser)9 Xpect (org.eclipse.xpect.runner.Xpect)9 Node (org.eclipse.n4js.flowgraphs.model.Node)8 HashSet (java.util.HashSet)7 ControlFlowType (org.eclipse.n4js.flowgraphs.ControlFlowType)5 ControlFlowEdge (org.eclipse.n4js.flowgraphs.model.ControlFlowEdge)4 HashMap (java.util.HashMap)3 FlowEdge (org.eclipse.n4js.flowgraphs.FlowEdge)3 FinallyBlock (org.eclipse.n4js.n4JS.FinallyBlock)3 LinkedHashSet (java.util.LinkedHashSet)2 N4JSFlowAnalyser (org.eclipse.n4js.flowgraphs.N4JSFlowAnalyser)2 Assumption (org.eclipse.n4js.flowgraphs.dataflow.Assumption)2 Symbol (org.eclipse.n4js.flowgraphs.dataflow.symbols.Symbol)2 Block (org.eclipse.n4js.n4JS.Block)2 ExpressionStatement (org.eclipse.n4js.n4JS.ExpressionStatement)2