Search in sources :

Example 1 with SameLen

use of org.checkerframework.checker.index.qual.SameLen in project checker-framework by typetools.

the class SameLenTransfer method visitAssignment.

@Override
public TransferResult<CFValue, CFStore> visitAssignment(AssignmentNode node, TransferInput<CFValue, CFStore> in) {
    TransferResult<CFValue, CFStore> result = super.visitAssignment(node, in);
    // Handle b = new T[a.length]
    if (node.getExpression() instanceof ArrayCreationNode) {
        ArrayCreationNode acNode = (ArrayCreationNode) node.getExpression();
        if (acNode.getDimensions().size() == 1) {
            Node lengthNode = acNode.getDimension(0);
            Node lengthNodeReceiver = getLengthReceiver(lengthNode);
            if (lengthNodeReceiver != null) {
                // "new T[a.length]" or "new T[s.length()]" is the right hand side of the assignment.
                // lengthNode is known to be "lengthNodeReceiver.length" or "lengthNodeReceiver.length()"
                // targetRec is the receiver for the left hand side of the assignment.
                JavaExpression targetRec = JavaExpression.fromNode(node.getTarget());
                JavaExpression otherRec = JavaExpression.fromNode(lengthNodeReceiver);
                AnnotationMirror lengthNodeAnnotation = aTypeFactory.getAnnotatedType(lengthNodeReceiver.getTree()).getAnnotationInHierarchy(UNKNOWN);
                AnnotationMirror combinedSameLen = aTypeFactory.createCombinedSameLen(Arrays.asList(targetRec, otherRec), Arrays.asList(UNKNOWN, lengthNodeAnnotation));
                propagateCombinedSameLen(combinedSameLen, node, result.getRegularStore());
                return result;
            }
        }
    }
    AnnotationMirror rightAnno = aTypeFactory.getAnnotatedType(node.getExpression().getTree()).getAnnotationInHierarchy(UNKNOWN);
    // If the left side of the assignment is an array or a string, then have both the right and
    // left side be SameLen of each other.
    JavaExpression targetRec = JavaExpression.fromNode(node.getTarget());
    JavaExpression exprRec = JavaExpression.fromNode(node.getExpression());
    if (IndexUtil.isSequenceType(node.getTarget().getType()) || (rightAnno != null && aTypeFactory.areSameByClass(rightAnno, SameLen.class))) {
        AnnotationMirror rightAnnoOrUnknown = rightAnno == null ? UNKNOWN : rightAnno;
        AnnotationMirror combinedSameLen = aTypeFactory.createCombinedSameLen(Arrays.asList(targetRec, exprRec), Arrays.asList(UNKNOWN, rightAnnoOrUnknown));
        propagateCombinedSameLen(combinedSameLen, node, 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) SameLen(org.checkerframework.checker.index.qual.SameLen) ArrayCreationNode(org.checkerframework.dataflow.cfg.node.ArrayCreationNode) AssignmentNode(org.checkerframework.dataflow.cfg.node.AssignmentNode) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) Node(org.checkerframework.dataflow.cfg.node.Node) ArrayCreationNode(org.checkerframework.dataflow.cfg.node.ArrayCreationNode)

Aggregations

AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 SameLen (org.checkerframework.checker.index.qual.SameLen)1 ArrayCreationNode (org.checkerframework.dataflow.cfg.node.ArrayCreationNode)1 AssignmentNode (org.checkerframework.dataflow.cfg.node.AssignmentNode)1 FieldAccessNode (org.checkerframework.dataflow.cfg.node.FieldAccessNode)1 MethodInvocationNode (org.checkerframework.dataflow.cfg.node.MethodInvocationNode)1 Node (org.checkerframework.dataflow.cfg.node.Node)1 JavaExpression (org.checkerframework.dataflow.expression.JavaExpression)1 CFStore (org.checkerframework.framework.flow.CFStore)1 CFValue (org.checkerframework.framework.flow.CFValue)1