Search in sources :

Example 26 with ControlFlowElement

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

the class CFGraph method build.

@Override
public void build(CFGraphProvider provider, Object input) {
    clear();
    nodeMap.clear();
    gProvider = provider;
    Collection<ControlFlowElement> cfes = gProvider.getElements(input);
    for (ControlFlowElement cfe : cfes) {
        if (cfe instanceof Script) {
            continue;
        }
        if (FGUtils.isCFContainer(cfe)) {
            CFNode entryNode = gProvider.getEntryNode(cfe);
            CFNode exitNode = gProvider.getExitNode(cfe);
            nodeMap.put(entryNode, entryNode.getControlFlowElement());
            nodeMap.put(exitNode, exitNode.getControlFlowElement());
            nodes.add(entryNode);
            nodes.add(exitNode);
            List<Edge> succs = gProvider.getConnectedEdges(entryNode, null);
            edges.addAll(succs);
        } else {
            CFNode node = gProvider.getNode(cfe);
            nodes.add(node);
            nodeMap.put(node, cfe);
            List<Edge> succs = gProvider.getConnectedEdges(node, null);
            edges.addAll(succs);
        }
    }
}
Also used : Script(org.eclipse.n4js.n4JS.Script) ControlFlowElement(org.eclipse.n4js.n4JS.ControlFlowElement)

Example 27 with ControlFlowElement

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

the class FlowgraphsXpectMethod method astOrder.

/**
 * This xpect method can evaluate the direct predecessors of a code element. The predecessors 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 = "('of' arg2=OFFSET)?")
@Xpect
public void astOrder(@N4JSCommaSeparatedValuesExpectation IN4JSCommaSeparatedValuesExpectation expectation, IEObjectCoveringRegion offset) {
    EObject context = offset.getEObject();
    Iterator<ControlFlowElement> astIter = new ASTIterator(context);
    int idx = 0;
    List<String> astElements = new LinkedList<>();
    while (astIter.hasNext()) {
        ControlFlowElement cfe = astIter.next();
        String elem = idx + ": " + FGUtils.getSourceText(cfe);
        astElements.add(elem);
        idx++;
    }
    expectation.assertEquals(astElements);
}
Also used : EObject(org.eclipse.emf.ecore.EObject) ASTIterator(org.eclipse.n4js.flowgraphs.ASTIterator) ControlFlowElement(org.eclipse.n4js.n4JS.ControlFlowElement) LinkedList(java.util.LinkedList) Xpect(org.eclipse.xpect.runner.Xpect) ParameterParser(org.eclipse.xpect.parameter.ParameterParser)

Example 28 with ControlFlowElement

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

the class FlowgraphsXpectMethod method path.

/**
 * This xpect method can evaluate if the tested element is a transitive predecessor of the given element.
 */
@ParameterParser(syntax = "'from' arg0=OFFSET ('to' arg1=OFFSET)? ('notTo' arg2=OFFSET)? ('via' arg3=OFFSET)? ('notVia' arg4=OFFSET)? ('pleaseNeverUseThisParameterSinceItExistsOnlyToGetAReferenceOffset' arg5=OFFSET)?")
@Xpect
public void path(IEObjectCoveringRegion fromOffset, IEObjectCoveringRegion toOffset, IEObjectCoveringRegion notToOffset, IEObjectCoveringRegion viaOffset, IEObjectCoveringRegion notViaOffset, IEObjectCoveringRegion referenceOffset) {
    EObjectCoveringRegion toOffsetImpl = (EObjectCoveringRegion) toOffset;
    EObjectCoveringRegion notToOffsetImpl = (EObjectCoveringRegion) notToOffset;
    EObjectCoveringRegion viaOffsetImpl = (EObjectCoveringRegion) viaOffset;
    EObjectCoveringRegion notViaOffsetImpl = (EObjectCoveringRegion) notViaOffset;
    EObjectCoveringRegion referenceOffsetImpl = (EObjectCoveringRegion) referenceOffset;
    ControlFlowElement fromCFE = getCFE(fromOffset);
    ControlFlowElement toCFE = getCFEWithReference(toOffsetImpl, referenceOffsetImpl);
    ControlFlowElement notToCFE = getCFEWithReference(notToOffsetImpl, referenceOffsetImpl);
    ControlFlowElement viaCFE = getCFEWithReference(viaOffsetImpl, referenceOffsetImpl);
    ControlFlowElement notViaCFE = getCFEWithReference(notViaOffsetImpl, referenceOffsetImpl);
    ControlFlowElement targetCFE = (toCFE != null) ? toCFE : notToCFE;
    boolean expectPathExists = toCFE != null;
    if (fromCFE == null) {
        fail("Element 'from' could not be found");
    }
    if (targetCFE == null) {
        fail("Element 'to' or 'notTo' could not be found or before 'from'");
    }
    boolean actualPathExists;
    if (viaCFE != null) {
        actualPathExists = getFlowAnalyzer(fromCFE).isTransitiveSuccessor(fromCFE, viaCFE, notViaCFE);
        actualPathExists &= getFlowAnalyzer(fromCFE).isTransitiveSuccessor(viaCFE, targetCFE, notViaCFE);
    } else {
        actualPathExists = getFlowAnalyzer(fromCFE).isTransitiveSuccessor(fromCFE, targetCFE, notViaCFE);
    }
    if (expectPathExists && !actualPathExists) {
        fail("Path not found");
    }
    if (!expectPathExists && actualPathExists) {
        fail("A path was found");
    }
}
Also used : IEObjectCoveringRegion(org.eclipse.n4js.xpect.common.N4JSOffsetAdapter.IEObjectCoveringRegion) EObjectCoveringRegion(org.eclipse.n4js.xpect.common.N4JSOffsetAdapter.EObjectCoveringRegion) ControlFlowElement(org.eclipse.n4js.n4JS.ControlFlowElement) Xpect(org.eclipse.xpect.runner.Xpect) ParameterParser(org.eclipse.xpect.parameter.ParameterParser)

Example 29 with ControlFlowElement

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

the class FlowgraphsXpectMethod method getCFE.

private ControlFlowElement getCFE(IEObjectCoveringRegion offset) {
    EObject context = offset.getEObject();
    if (!(context instanceof ControlFlowElement)) {
        fail("Element '" + FGUtils.getSourceText(context) + "' is not a control flow element");
    }
    ControlFlowElement cfe = (ControlFlowElement) context;
    return cfe;
}
Also used : EObject(org.eclipse.emf.ecore.EObject) ControlFlowElement(org.eclipse.n4js.n4JS.ControlFlowElement)

Example 30 with ControlFlowElement

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

the class FlowgraphsXpectMethod method instanceofguard.

/**
 * This xpect method can evaluate the control flow container of a given {@link ControlFlowElement}.
 */
@ParameterParser(syntax = "('of' arg1=OFFSET)? (arg2=STRING)?")
@Xpect
public void instanceofguard(@N4JSCommaSeparatedValuesExpectation IN4JSCommaSeparatedValuesExpectation expectation, IEObjectCoveringRegion offset, String holdsName) {
    ControlFlowElement cfe = getCFE(offset);
    GuardAssertion assertion = null;
    if (holdsName != null) {
        assertion = GuardAssertion.valueOf(holdsName);
    }
    InstanceofGuardAnalyser iga = new InstanceofGuardAnalyser();
    N4JSFlowAnalyser flowAnalyzer = getFlowAnalyzer(cfe);
    flowAnalyzer.accept(iga);
    Collection<InstanceofGuard> ioGuards = null;
    if (assertion == GuardAssertion.MayHolds) {
        ioGuards = iga.getMayHoldingTypes(cfe);
    } else if (assertion == GuardAssertion.NeverHolds) {
        ioGuards = iga.getNeverHoldingTypes(cfe);
    } else {
        ioGuards = iga.getAlwaysHoldingTypes(cfe);
    }
    List<String> commonPredStrs = new LinkedList<>();
    for (InstanceofGuard ioGuard : ioGuards) {
        String symbolText = FGUtils.getSourceText(ioGuard.symbolCFE);
        String typeText = FGUtils.getSourceText(ioGuard.typeIdentifier);
        commonPredStrs.add(symbolText + "<:" + typeText);
    }
    expectation.assertEquals(commonPredStrs);
}
Also used : InstanceofGuardAnalyser(org.eclipse.n4js.flowgraphs.analysers.InstanceofGuardAnalyser) InstanceofGuard(org.eclipse.n4js.flowgraphs.dataflow.guards.InstanceofGuard) N4JSFlowAnalyser(org.eclipse.n4js.flowgraphs.N4JSFlowAnalyser) GuardAssertion(org.eclipse.n4js.flowgraphs.dataflow.guards.GuardAssertion) ControlFlowElement(org.eclipse.n4js.n4JS.ControlFlowElement) LinkedList(java.util.LinkedList) 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