Search in sources :

Example 1 with N4JSFlowAnalyser

use of org.eclipse.n4js.flowgraphs.N4JSFlowAnalyser in project n4js by eclipse.

the class N4JSFlowgraphValidator method checkFlowGraphs.

/**
 * Triggers all flow graph related validations
 */
@Check
public void checkFlowGraphs(Script script) {
    // Note: The Flow Graph is NOT stored in the meta info cache. Hence, it is created here at use site.
    // In case the its creation is moved to the N4JSPostProcessor, care about an increase in memory consumption.
    N4JSFlowAnalyser flowAnalyzer = new N4JSFlowAnalyser(this::checkCancelled);
    FlowValidator[] fValidators = { new DeadCodeValidator(keywordProvider), new UsedBeforeDeclaredValidator(), new NullUndefinedValidator(n4jsCore, findReferenceHelper), new MissingReturnOrThrowValidator(typeSystemHelper, jsVariantHelper) };
    FlowAnalyser[] fAnalysers = new FlowAnalyser[fValidators.length];
    for (int i = 0; i < fValidators.length; i++) {
        fAnalysers[i] = fValidators[i].getFlowAnalyser();
    }
    flowAnalyzer.createGraphs(script);
    flowAnalyzer.accept(fAnalysers);
    String uriString = script.eResource().getURI().toString();
    try (ClosableMeasurement m1 = dcFlowGraphs.getClosableMeasurement("flowGraphs_" + uriString);
        ClosableMeasurement m2 = dcPostprocessing.getClosableMeasurement("createGraph_" + uriString)) {
        for (FlowValidator fValidator : fValidators) {
            fValidator.checkResults(this);
        }
    }
}
Also used : ClosableMeasurement(org.eclipse.n4js.smith.ClosableMeasurement) NullUndefinedValidator(org.eclipse.n4js.validation.validators.flowgraphs.NullUndefinedValidator) MissingReturnOrThrowValidator(org.eclipse.n4js.validation.validators.flowgraphs.MissingReturnOrThrowValidator) N4JSFlowAnalyser(org.eclipse.n4js.flowgraphs.N4JSFlowAnalyser) UsedBeforeDeclaredValidator(org.eclipse.n4js.validation.validators.flowgraphs.UsedBeforeDeclaredValidator) FlowValidator(org.eclipse.n4js.validation.validators.flowgraphs.FlowValidator) FlowAnalyser(org.eclipse.n4js.flowgraphs.FlowAnalyser) N4JSFlowAnalyser(org.eclipse.n4js.flowgraphs.N4JSFlowAnalyser) DeadCodeValidator(org.eclipse.n4js.validation.validators.flowgraphs.DeadCodeValidator) Check(org.eclipse.xtext.validation.Check)

Example 2 with N4JSFlowAnalyser

use of org.eclipse.n4js.flowgraphs.N4JSFlowAnalyser 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 3 with N4JSFlowAnalyser

use of org.eclipse.n4js.flowgraphs.N4JSFlowAnalyser in project n4js by eclipse.

the class FlowgraphsXpectMethod method getFlowAnalyzer.

N4JSFlowAnalyser getFlowAnalyzer(EObject eo) {
    Script script = EcoreUtil2.getContainerOfType(eo, Script.class);
    N4JSFlowAnalyser flowAnalyzer = new N4JSFlowAnalyser();
    flowAnalyzer.createGraphs(script);
    return flowAnalyzer;
}
Also used : Script(org.eclipse.n4js.n4JS.Script) N4JSFlowAnalyser(org.eclipse.n4js.flowgraphs.N4JSFlowAnalyser)

Example 4 with N4JSFlowAnalyser

use of org.eclipse.n4js.flowgraphs.N4JSFlowAnalyser 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

N4JSFlowAnalyser (org.eclipse.n4js.flowgraphs.N4JSFlowAnalyser)4 ControlFlowElement (org.eclipse.n4js.n4JS.ControlFlowElement)2 LinkedList (java.util.LinkedList)1 ControlFlowType (org.eclipse.n4js.flowgraphs.ControlFlowType)1 FlowAnalyser (org.eclipse.n4js.flowgraphs.FlowAnalyser)1 InstanceofGuardAnalyser (org.eclipse.n4js.flowgraphs.analysers.InstanceofGuardAnalyser)1 GuardAssertion (org.eclipse.n4js.flowgraphs.dataflow.guards.GuardAssertion)1 InstanceofGuard (org.eclipse.n4js.flowgraphs.dataflow.guards.InstanceofGuard)1 Script (org.eclipse.n4js.n4JS.Script)1 ClosableMeasurement (org.eclipse.n4js.smith.ClosableMeasurement)1 DeadCodeValidator (org.eclipse.n4js.validation.validators.flowgraphs.DeadCodeValidator)1 FlowValidator (org.eclipse.n4js.validation.validators.flowgraphs.FlowValidator)1 MissingReturnOrThrowValidator (org.eclipse.n4js.validation.validators.flowgraphs.MissingReturnOrThrowValidator)1 NullUndefinedValidator (org.eclipse.n4js.validation.validators.flowgraphs.NullUndefinedValidator)1 UsedBeforeDeclaredValidator (org.eclipse.n4js.validation.validators.flowgraphs.UsedBeforeDeclaredValidator)1 ParameterParser (org.eclipse.xpect.parameter.ParameterParser)1 Xpect (org.eclipse.xpect.runner.Xpect)1 Check (org.eclipse.xtext.validation.Check)1