Search in sources :

Example 1 with ControlFlowElement

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

the class DeadCodeValidator method getStatementDescription.

private String getStatementDescription(DeadCodeRegion deadCodeRegion) {
    ControlFlowElement reachablePred = deadCodeRegion.getReachablePredecessor();
    if (reachablePred == null) {
        return null;
    }
    boolean addKeyword = false;
    addKeyword |= reachablePred instanceof ReturnStatement;
    addKeyword |= reachablePred instanceof BreakStatement;
    addKeyword |= reachablePred instanceof ContinueStatement;
    addKeyword |= reachablePred instanceof ThrowStatement;
    if (addKeyword) {
        String keyword = keywordProvider.keyword(reachablePred);
        if (Strings.isNullOrEmpty(keyword)) {
            keyword = reachablePred.eClass().getName();
        }
        return keyword;
    }
    return null;
}
Also used : BreakStatement(org.eclipse.n4js.n4JS.BreakStatement) ReturnStatement(org.eclipse.n4js.n4JS.ReturnStatement) ThrowStatement(org.eclipse.n4js.n4JS.ThrowStatement) ControlFlowElement(org.eclipse.n4js.n4JS.ControlFlowElement) ContinueStatement(org.eclipse.n4js.n4JS.ContinueStatement)

Example 2 with ControlFlowElement

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

the class FlowgraphsXpectMethod method allEdges.

/**
 * This xpect method can evaluate all edges of the containing function.
 */
@ParameterParser(syntax = "('from' arg1=OFFSET)?")
@Xpect
public void allEdges(@N4JSCommaSeparatedValuesExpectation IN4JSCommaSeparatedValuesExpectation expectation, IEObjectCoveringRegion offset) {
    ControlFlowElement cfe = null;
    if (offset != null) {
        cfe = getCFE(offset);
    }
    cfe = FGUtils.getCFContainer(cfe);
    AllNodesAndEdgesPrintVisitor anaepw = new AllNodesAndEdgesPrintVisitor(cfe);
    getFlowAnalyzer(cfe).accept(anaepw);
    List<String> pathStrings = anaepw.getAllEdgeStrings();
    expectation.assertEquals(pathStrings);
}
Also used : AllNodesAndEdgesPrintVisitor(org.eclipse.n4js.flowgraphs.analysers.AllNodesAndEdgesPrintVisitor) ControlFlowElement(org.eclipse.n4js.n4JS.ControlFlowElement) Xpect(org.eclipse.xpect.runner.Xpect) ParameterParser(org.eclipse.xpect.parameter.ParameterParser)

Example 3 with ControlFlowElement

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

the class FlowgraphsXpectMethod method filterByControlFlowType.

private void filterByControlFlowType(ControlFlowElement start, Collection<ControlFlowElement> succList, ControlFlowType cfType) {
    if (cfType == null)
        return;
    for (Iterator<ControlFlowElement> succIt = succList.iterator(); succIt.hasNext(); ) {
        N4JSFlowAnalyser flowAnalyzer = getFlowAnalyzer(start);
        Set<ControlFlowType> currCFTypes = flowAnalyzer.getControlFlowTypeToSuccessors(start, succIt.next());
        if (!currCFTypes.contains(cfType)) {
            succIt.remove();
        }
    }
}
Also used : ControlFlowType(org.eclipse.n4js.flowgraphs.ControlFlowType) N4JSFlowAnalyser(org.eclipse.n4js.flowgraphs.N4JSFlowAnalyser) ControlFlowElement(org.eclipse.n4js.n4JS.ControlFlowElement)

Example 4 with ControlFlowElement

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

the class FlowgraphsXpectMethod method succs.

/**
 * This xpect method can evaluate the direct successors of a code element. The successors can be limited when
 * specifying the edge type.
 * <p>
 * <b>Attention:</b> The type parameter <i>does not</i> work on self loops!
 */
@ParameterParser(syntax = "('type' arg1=STRING)? ('at' arg2=OFFSET)?")
@Xpect
public void succs(@N4JSCommaSeparatedValuesExpectation IN4JSCommaSeparatedValuesExpectation expectation, String type, IEObjectCoveringRegion offset) {
    ControlFlowType cfType = getControlFlowType(type);
    ControlFlowElement cfe = getCFE(offset);
    Set<ControlFlowElement> succs = getFlowAnalyzer(cfe).getSuccessorsSkipInternal(cfe);
    filterByControlFlowType(cfe, succs, cfType);
    List<String> succTexts = new LinkedList<>();
    for (ControlFlowElement succ : succs) {
        String succText = FGUtils.getSourceText(succ);
        succTexts.add(succText);
    }
    expectation.assertEquals(succTexts);
}
Also used : ControlFlowType(org.eclipse.n4js.flowgraphs.ControlFlowType) ControlFlowElement(org.eclipse.n4js.n4JS.ControlFlowElement) LinkedList(java.util.LinkedList) Xpect(org.eclipse.xpect.runner.Xpect) ParameterParser(org.eclipse.xpect.parameter.ParameterParser)

Example 5 with ControlFlowElement

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

the class FlowgraphsXpectMethod method allMergeBranches.

/**
 * This xpect method can evaluate all branches that are merged at the given node name.
 */
@ParameterParser(syntax = "('pleaseNeverUseThisParameterSinceItExistsOnlyToGetAReferenceOffset' arg1=OFFSET)?")
@Xpect
public void allMergeBranches(@N4JSCommaSeparatedValuesExpectation IN4JSCommaSeparatedValuesExpectation expectation, IEObjectCoveringRegion referenceOffset) {
    N4JSFlowAnalyserDataRecorder.setEnabled(true);
    GraphVisitor dfv = new DummyForwardVisitor();
    GraphVisitor dbv = new DummyBackwardVisitor();
    ControlFlowElement referenceCFE = getCFE(referenceOffset);
    getFlowAnalyzer(referenceCFE).accept(dfv, dbv);
    N4JSFlowAnalyserDataRecorder.setEnabled(false);
    performBranchAnalysis(referenceOffset, null, referenceOffset);
    List<String> edgeStrings = new LinkedList<>();
    int groupIdx = 0;
    List<Pair<Node, List<ControlFlowEdge>>> mergedEdges = N4JSFlowAnalyserDataRecorder.getMergedEdges();
    for (Pair<Node, List<ControlFlowEdge>> pair : mergedEdges) {
        Node startNode = pair.getKey();
        List<ControlFlowEdge> edges = pair.getValue();
        for (ControlFlowEdge edge : edges) {
            String c = edge.start == startNode ? "B" : "F";
            edgeStrings.add(c + groupIdx + ": " + edge.toString());
        }
        groupIdx++;
    }
    Collections.sort(edgeStrings);
    expectation.assertEquals(edgeStrings);
}
Also used : DummyBackwardVisitor(org.eclipse.n4js.flowgraphs.analysers.DummyBackwardVisitor) GraphVisitor(org.eclipse.n4js.flowgraphs.analysis.GraphVisitor) Node(org.eclipse.n4js.flowgraphs.model.Node) DummyForwardVisitor(org.eclipse.n4js.flowgraphs.analysers.DummyForwardVisitor) LinkedList(java.util.LinkedList) ControlFlowEdge(org.eclipse.n4js.flowgraphs.model.ControlFlowEdge) LinkedList(java.util.LinkedList) List(java.util.List) ControlFlowElement(org.eclipse.n4js.n4JS.ControlFlowElement) Pair(org.eclipse.xtext.xbase.lib.Pair) Xpect(org.eclipse.xpect.runner.Xpect) ParameterParser(org.eclipse.xpect.parameter.ParameterParser)

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