Search in sources :

Example 6 with FlowExpressionContext

use of org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionContext in project checker-framework by typetools.

the class CFAbstractTransfer method processPostconditionsAndConditionalPostconditions.

private void processPostconditionsAndConditionalPostconditions(MethodInvocationNode n, Tree tree, S thenStore, S elseStore, Set<? extends Contract> postconditions) {
    FlowExpressionContext flowExprContext = null;
    for (Contract p : postconditions) {
        String expression = p.expression;
        AnnotationMirror anno = p.annotation;
        if (flowExprContext == null) {
            flowExprContext = FlowExpressionContext.buildContextForMethodUse(n, analysis.checker.getContext());
        }
        TreePath localScope = analysis.atypeFactory.getPath(tree);
        anno = standardizeAnnotationFromContract(anno, flowExprContext, localScope);
        try {
            FlowExpressions.Receiver r = FlowExpressionParseUtil.parse(expression, flowExprContext, localScope, false);
            if (p.kind == Contract.Kind.CONDITIONALPOSTCONDTION) {
                if (((ConditionalPostcondition) p).annoResult) {
                    thenStore.insertValue(r, anno);
                } else {
                    elseStore.insertValue(r, anno);
                }
            } else {
                thenStore.insertValue(r, anno);
            }
        } catch (FlowExpressionParseException e) {
            Result result;
            if (e.isFlowParseError()) {
                Object[] args = new Object[e.args.length + 1];
                args[0] = ElementUtils.getVerboseName(TreeUtils.elementFromUse(n.getTree()));
                System.arraycopy(e.args, 0, args, 1, e.args.length);
                result = Result.failure("flowexpr.parse.error.postcondition", args);
            } else {
                result = e.getResult();
            }
            // report errors here
            analysis.checker.report(result, tree);
        }
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TreePath(com.sun.source.util.TreePath) FlowExpressionContext(org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionContext) FlowExpressions(org.checkerframework.dataflow.analysis.FlowExpressions) FlowExpressionParseException(org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionParseException) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver) ConditionalPostcondition(org.checkerframework.framework.util.ContractsUtils.ConditionalPostcondition) Contract(org.checkerframework.framework.util.ContractsUtils.Contract) TransferResult(org.checkerframework.dataflow.analysis.TransferResult) Result(org.checkerframework.framework.source.Result) RegularTransferResult(org.checkerframework.dataflow.analysis.RegularTransferResult) ConditionalTransferResult(org.checkerframework.dataflow.analysis.ConditionalTransferResult)

Example 7 with FlowExpressionContext

use of org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionContext in project checker-framework by typetools.

the class LockAnnotatedTypeFactory method createDependentTypesHelper.

@Override
protected DependentTypesHelper createDependentTypesHelper() {
    return new DependentTypesHelper(this) {

        @Override
        protected void reportErrors(Tree errorTree, List<DependentTypesError> errors) {
            // If the error message is NOT_EFFECTIVELY_FINAL, then report lock.expression.not
            // .final instead of an expression.unparsable.type.invalid error.
            List<DependentTypesError> superErrors = new ArrayList<>();
            for (DependentTypesError error : errors) {
                if (error.error.equals(NOT_EFFECTIVELY_FINAL)) {
                    checker.report(Result.failure("lock.expression.not.final", error.expression), errorTree);
                } else {
                    superErrors.add(error);
                }
            }
            super.reportErrors(errorTree, superErrors);
        }

        @Override
        protected String standardizeString(String expression, FlowExpressionContext context, TreePath localScope, boolean useLocalScope) {
            if (DependentTypesError.isExpressionError(expression)) {
                return expression;
            }
            // Adds logic to parse <self> expression, which only the Lock Checker uses.
            if (LockVisitor.selfReceiverPattern.matcher(expression).matches()) {
                return expression;
            }
            try {
                FlowExpressions.Receiver result = FlowExpressionParseUtil.parse(expression, context, localScope, useLocalScope);
                if (result == null) {
                    return new DependentTypesError(expression, " ").toString();
                }
                if (!isExpressionEffectivelyFinal(result)) {
                    // NOT_EFFECTIVELY_FINAL error string.
                    return new DependentTypesError(expression, NOT_EFFECTIVELY_FINAL).toString();
                }
                return result.toString();
            } catch (FlowExpressionParseUtil.FlowExpressionParseException e) {
                return new DependentTypesError(expression, e).toString();
            }
        }
    };
}
Also used : FlowExpressionParseUtil(org.checkerframework.framework.util.FlowExpressionParseUtil) FlowExpressionContext(org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionContext) DependentTypesError(org.checkerframework.framework.util.dependenttypes.DependentTypesError) ArrayList(java.util.ArrayList) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver) TreePath(com.sun.source.util.TreePath) DependentTypesHelper(org.checkerframework.framework.util.dependenttypes.DependentTypesHelper) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) VariableTree(com.sun.source.tree.VariableTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) FlowExpressions(org.checkerframework.dataflow.analysis.FlowExpressions) List(java.util.List) ArrayList(java.util.ArrayList)

Example 8 with FlowExpressionContext

use of org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionContext in project checker-framework by typetools.

the class DependentTypesHelper method standardizeNewClassTree.

public void standardizeNewClassTree(NewClassTree tree, AnnotatedDeclaredType type) {
    if (!hasDependentType(type)) {
        return;
    }
    TreePath path = factory.getPath(tree);
    FlowExpressions.Receiver r = FlowExpressions.internalReprOfImplicitReceiver(TreeUtils.elementFromUse(tree));
    FlowExpressionContext context = new FlowExpressionContext(r, FlowExpressions.getParametersOfEnclosingMethod(factory, path), factory.getContext());
    standardizeUseLocals(context, path, type);
}
Also used : TreePath(com.sun.source.util.TreePath) FlowExpressionContext(org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionContext) FlowExpressions(org.checkerframework.dataflow.analysis.FlowExpressions) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver)

Example 9 with FlowExpressionContext

use of org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionContext in project checker-framework by typetools.

the class DependentTypesHelper method standardizeExpression.

public void standardizeExpression(ExpressionTree tree, AnnotatedTypeMirror annotatedType) {
    if (!hasDependentType(annotatedType)) {
        return;
    }
    TreePath path = factory.getPath(tree);
    if (path == null) {
        return;
    }
    Tree enclosingClass = TreeUtils.enclosingClass(path);
    if (enclosingClass == null) {
        return;
    }
    TypeMirror enclosingType = TreeUtils.typeOf(enclosingClass);
    FlowExpressions.Receiver receiver = FlowExpressions.internalReprOfPseudoReceiver(path, enclosingType);
    FlowExpressionContext localContext = new FlowExpressionContext(receiver, FlowExpressions.getParametersOfEnclosingMethod(factory, path), factory.getContext());
    standardizeUseLocals(localContext, path, annotatedType);
}
Also used : TreePath(com.sun.source.util.TreePath) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) FlowExpressionContext(org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionContext) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) IdentifierTree(com.sun.source.tree.IdentifierTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) ModifiersTree(com.sun.source.tree.ModifiersTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) NewClassTree(com.sun.source.tree.NewClassTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) FlowExpressions(org.checkerframework.dataflow.analysis.FlowExpressions) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver)

Example 10 with FlowExpressionContext

use of org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionContext in project checker-framework by typetools.

the class DependentTypesHelper method standardizeFieldAccess.

public void standardizeFieldAccess(MemberSelectTree node, AnnotatedTypeMirror type) {
    if (!hasDependentType(type)) {
        return;
    }
    if (TreeUtils.isClassLiteral(node)) {
        return;
    }
    Element ele = TreeUtils.elementFromUse(node);
    if (ele.getKind() != ElementKind.FIELD) {
        return;
    }
    FlowExpressions.Receiver receiver = FlowExpressions.internalReprOf(factory, node.getExpression());
    FlowExpressionContext context = new FlowExpressionContext(receiver, null, factory.getContext());
    standardizeDoNotUseLocals(context, factory.getPath(node), type);
}
Also used : FlowExpressionContext(org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionContext) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) FlowExpressions(org.checkerframework.dataflow.analysis.FlowExpressions) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver)

Aggregations

FlowExpressionContext (org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionContext)15 Receiver (org.checkerframework.dataflow.analysis.FlowExpressions.Receiver)13 FlowExpressions (org.checkerframework.dataflow.analysis.FlowExpressions)12 TreePath (com.sun.source.util.TreePath)9 AnnotationMirror (javax.lang.model.element.AnnotationMirror)5 TypeMirror (javax.lang.model.type.TypeMirror)5 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)5 FlowExpressionParseException (org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionParseException)5 ExpressionTree (com.sun.source.tree.ExpressionTree)4 Element (javax.lang.model.element.Element)4 TypeElement (javax.lang.model.element.TypeElement)4 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)3 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)3 MethodTree (com.sun.source.tree.MethodTree)3 Tree (com.sun.source.tree.Tree)3 VariableTree (com.sun.source.tree.VariableTree)3 ArrayList (java.util.ArrayList)3 Contract (org.checkerframework.framework.util.ContractsUtils.Contract)3 AnnotationTree (com.sun.source.tree.AnnotationTree)2 IdentifierTree (com.sun.source.tree.IdentifierTree)2