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;
}
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);
}
Aggregations