Search in sources :

Example 26 with JavaExpression

use of org.checkerframework.dataflow.expression.JavaExpression in project checker-framework by typetools.

the class UpperBoundTransfer method refineGTE.

/**
 * Case 9: if x ≤ y, and y has a type that is related to the length of an array, then x has the
 * same type.
 */
@Override
protected void refineGTE(Node left, AnnotationMirror leftAnno, Node right, AnnotationMirror rightAnno, CFStore store, TransferInput<CFValue, CFStore> in) {
    UBQualifier leftQualifier = UBQualifier.createUBQualifier(leftAnno, (UpperBoundChecker) atypeFactory.getChecker());
    UBQualifier rightQualifier = UBQualifier.createUBQualifier(rightAnno, (UpperBoundChecker) atypeFactory.getChecker());
    UBQualifier refinedRight = rightQualifier.glb(leftQualifier);
    if (leftQualifier.isLessThanLengthQualifier()) {
        propagateToOperands((LessThanLengthOf) leftQualifier, right, in, store);
    }
    refineSubtrahendWithOffset(left, right, false, in, store);
    JavaExpression rightJe = JavaExpression.fromNode(right);
    store.insertValue(rightJe, atypeFactory.convertUBQualifierToAnnotation(refinedRight));
}
Also used : JavaExpression(org.checkerframework.dataflow.expression.JavaExpression)

Example 27 with JavaExpression

use of org.checkerframework.dataflow.expression.JavaExpression in project checker-framework by typetools.

the class UpperBoundTransfer method visitMethodInvocation.

/**
 * If n is a String.length() method invocation, then the type of s.length() is the glb
 * of @LTEqLengthOf("s") and the value of s.length() in the store. This is case 20.
 */
@Override
public TransferResult<CFValue, CFStore> visitMethodInvocation(MethodInvocationNode n, TransferInput<CFValue, CFStore> in) {
    if (atypeFactory.getMethodIdentifier().isLengthOfMethodInvocation(n)) {
        JavaExpression stringLength = JavaExpression.fromNode(n);
        if (stringLength instanceof MethodCall) {
            JavaExpression receiverJe = ((MethodCall) stringLength).getReceiver();
            Tree receiverTree = n.getTarget().getReceiver().getTree();
            // receiverTree is null when the receiver is implicit "this".
            if (receiverTree != null) {
                TransferResult<CFValue, CFStore> result = visitLengthAccess(n, in, receiverJe, receiverTree);
                if (result != null) {
                    return result;
                }
            }
        }
    }
    return super.visitMethodInvocation(n, in);
}
Also used : CFValue(org.checkerframework.framework.flow.CFValue) CFStore(org.checkerframework.framework.flow.CFStore) JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) Tree(com.sun.source.tree.Tree) MethodCall(org.checkerframework.dataflow.expression.MethodCall)

Example 28 with JavaExpression

use of org.checkerframework.dataflow.expression.JavaExpression in project checker-framework by typetools.

the class UpperBoundTransfer method refineEq.

/**
 * Refines the type of the left and right node to glb of the left and right annotation.
 */
private void refineEq(Node left, AnnotationMirror leftAnno, Node right, AnnotationMirror rightAnno, CFStore store) {
    UBQualifier leftQualifier = UBQualifier.createUBQualifier(leftAnno, (UpperBoundChecker) atypeFactory.getChecker());
    UBQualifier rightQualifier = UBQualifier.createUBQualifier(rightAnno, (UpperBoundChecker) atypeFactory.getChecker());
    UBQualifier glb = rightQualifier.glb(leftQualifier);
    AnnotationMirror glbAnno = atypeFactory.convertUBQualifierToAnnotation(glb);
    List<Node> internalsRight = splitAssignments(right);
    for (Node internal : internalsRight) {
        JavaExpression rightJe = JavaExpression.fromNode(internal);
        store.insertValue(rightJe, glbAnno);
    }
    List<Node> internalsLeft = splitAssignments(left);
    for (Node internal : internalsLeft) {
        JavaExpression leftJe = JavaExpression.fromNode(internal);
        store.insertValue(leftJe, glbAnno);
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) TypeCastNode(org.checkerframework.dataflow.cfg.node.TypeCastNode) NumericalMultiplicationNode(org.checkerframework.dataflow.cfg.node.NumericalMultiplicationNode) ArrayCreationNode(org.checkerframework.dataflow.cfg.node.ArrayCreationNode) AssignmentNode(org.checkerframework.dataflow.cfg.node.AssignmentNode) CaseNode(org.checkerframework.dataflow.cfg.node.CaseNode) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) NumericalAdditionNode(org.checkerframework.dataflow.cfg.node.NumericalAdditionNode) NumericalSubtractionNode(org.checkerframework.dataflow.cfg.node.NumericalSubtractionNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) IntegerLiteralNode(org.checkerframework.dataflow.cfg.node.IntegerLiteralNode) Node(org.checkerframework.dataflow.cfg.node.Node)

Example 29 with JavaExpression

use of org.checkerframework.dataflow.expression.JavaExpression in project checker-framework by typetools.

the class UpperBoundTransfer method visitAssignment.

/**
 * Case 1: Refine the type of expressions used as an array dimension to be less than length of the
 * array to which the new array is assigned. For example, in "int[] array = new int[expr];", the
 * type of expr is @LTEqLength("array").
 */
@Override
public TransferResult<CFValue, CFStore> visitAssignment(AssignmentNode node, TransferInput<CFValue, CFStore> in) {
    TransferResult<CFValue, CFStore> result = super.visitAssignment(node, in);
    Node expNode = node.getExpression();
    // strip off typecast if any
    Node expNodeSansCast = (expNode instanceof TypeCastNode) ? ((TypeCastNode) expNode).getOperand() : expNode;
    // null if right-hand-side is not an array creation expression
    ArrayCreationNode acNode = (expNodeSansCast instanceof ArrayCreationNode) ? acNode = (ArrayCreationNode) expNodeSansCast : null;
    if (acNode != null) {
        // Right-hand side of assignment is an array creation expression
        List<Node> nodeList = acNode.getDimensions();
        if (nodeList.size() < 1) {
            return result;
        }
        Node dim = acNode.getDimension(0);
        UBQualifier previousQualifier = getUBQualifier(dim, in);
        JavaExpression arrayExpr = JavaExpression.fromNode(node.getTarget());
        String arrayString = arrayExpr.toString();
        LessThanLengthOf newInfo = (LessThanLengthOf) UBQualifier.createUBQualifier(arrayString, "-1");
        UBQualifier combined = previousQualifier.glb(newInfo);
        AnnotationMirror newAnno = atypeFactory.convertUBQualifierToAnnotation(combined);
        JavaExpression dimExpr = JavaExpression.fromNode(dim);
        result.getRegularStore().insertValue(dimExpr, newAnno);
        propagateToOperands(newInfo, dim, in, result.getRegularStore());
    }
    return result;
}
Also used : CFValue(org.checkerframework.framework.flow.CFValue) AnnotationMirror(javax.lang.model.element.AnnotationMirror) CFStore(org.checkerframework.framework.flow.CFStore) JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) TypeCastNode(org.checkerframework.dataflow.cfg.node.TypeCastNode) NumericalMultiplicationNode(org.checkerframework.dataflow.cfg.node.NumericalMultiplicationNode) ArrayCreationNode(org.checkerframework.dataflow.cfg.node.ArrayCreationNode) AssignmentNode(org.checkerframework.dataflow.cfg.node.AssignmentNode) CaseNode(org.checkerframework.dataflow.cfg.node.CaseNode) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) NumericalAdditionNode(org.checkerframework.dataflow.cfg.node.NumericalAdditionNode) NumericalSubtractionNode(org.checkerframework.dataflow.cfg.node.NumericalSubtractionNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) IntegerLiteralNode(org.checkerframework.dataflow.cfg.node.IntegerLiteralNode) Node(org.checkerframework.dataflow.cfg.node.Node) LessThanLengthOf(org.checkerframework.checker.index.upperbound.UBQualifier.LessThanLengthOf) ArrayCreationNode(org.checkerframework.dataflow.cfg.node.ArrayCreationNode) TypeCastNode(org.checkerframework.dataflow.cfg.node.TypeCastNode)

Example 30 with JavaExpression

use of org.checkerframework.dataflow.expression.JavaExpression in project checker-framework by typetools.

the class UpperBoundTransfer method propagateToMultiplicationOperand.

/**
 * {@code other} times {@code node} is known to be {@code typeOfMultiplication}.
 *
 * <p>This implies that if {@code other} is positive, then {@code node} is {@code
 * typeOfMultiplication}. If {@code other} is greater than 1, then {@code node} is {@code
 * typeOfMultiplication} plus 1. These are cases 2 and 3, respectively.
 */
private void propagateToMultiplicationOperand(LessThanLengthOf typeOfMultiplication, Node node, Node other, TransferInput<CFValue, CFStore> in, CFStore store) {
    if (atypeFactory.hasLowerBoundTypeByClass(other, Positive.class)) {
        Long minValue = ValueCheckerUtils.getMinValue(other.getTree(), atypeFactory.getValueAnnotatedTypeFactory());
        if (minValue != null && minValue > 1) {
            typeOfMultiplication = (LessThanLengthOf) typeOfMultiplication.plusOffset(1);
        }
        UBQualifier qual = getUBQualifier(node, in);
        UBQualifier newQual = qual.glb(typeOfMultiplication);
        JavaExpression je = JavaExpression.fromNode(node);
        store.insertValue(je, atypeFactory.convertUBQualifierToAnnotation(newQual));
    }
}
Also used : JavaExpression(org.checkerframework.dataflow.expression.JavaExpression)

Aggregations

JavaExpression (org.checkerframework.dataflow.expression.JavaExpression)87 AnnotationMirror (javax.lang.model.element.AnnotationMirror)39 Node (org.checkerframework.dataflow.cfg.node.Node)23 StringToJavaExpression (org.checkerframework.framework.util.StringToJavaExpression)21 CFValue (org.checkerframework.framework.flow.CFValue)20 ExecutableElement (javax.lang.model.element.ExecutableElement)19 MethodInvocationNode (org.checkerframework.dataflow.cfg.node.MethodInvocationNode)19 ArrayList (java.util.ArrayList)15 CFStore (org.checkerframework.framework.flow.CFStore)15 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)13 JavaExpressionParseException (org.checkerframework.framework.util.JavaExpressionParseUtil.JavaExpressionParseException)13 Tree (com.sun.source.tree.Tree)12 List (java.util.List)11 FieldAccess (org.checkerframework.dataflow.expression.FieldAccess)11 MethodTree (com.sun.source.tree.MethodTree)10 TreePath (com.sun.source.util.TreePath)10 HashMap (java.util.HashMap)10 Map (java.util.Map)9 Element (javax.lang.model.element.Element)9 VariableElement (javax.lang.model.element.VariableElement)9