Search in sources :

Example 61 with JavaExpression

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

the class SameLenAnnotatedTypeFactory method getAnnotatedTypeLhs.

// Handles case "user-written SameLen"
@Override
public AnnotatedTypeMirror getAnnotatedTypeLhs(Tree tree) {
    AnnotatedTypeMirror atm = super.getAnnotatedTypeLhs(tree);
    if (tree.getKind() == Tree.Kind.VARIABLE) {
        AnnotationMirror sameLenAnno = atm.getAnnotation(SameLen.class);
        if (sameLenAnno != null) {
            JavaExpression je = JavaExpression.fromVariableTree((VariableTree) tree);
            String varName = je.toString();
            List<String> exprs = AnnotationUtils.getElementValueArray(sameLenAnno, sameLenValueElement, String.class);
            exprs.remove(varName);
            if (exprs.isEmpty()) {
                atm.replaceAnnotation(UNKNOWN);
            } else {
                atm.replaceAnnotation(createSameLen(exprs));
            }
        }
    }
    return atm;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 62 with JavaExpression

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

the class SameLenTransfer method addInformationFromPreconditions.

/**
 * Overridden to ensure that SameLen annotations on method parameters are symmetric.
 */
@Override
protected void addInformationFromPreconditions(CFStore info, AnnotatedTypeFactory factory, UnderlyingAST.CFGMethod method, MethodTree methodTree, ExecutableElement methodElement) {
    super.addInformationFromPreconditions(info, factory, method, methodTree, methodElement);
    List<? extends VariableTree> paramTrees = methodTree.getParameters();
    int numParams = paramTrees.size();
    List<String> paramNames = new ArrayList<>(numParams);
    List<AnnotatedTypeMirror> params = new ArrayList<>(numParams);
    for (VariableTree tree : paramTrees) {
        paramNames.add(tree.getName().toString());
        params.add(aTypeFactory.getAnnotatedType(tree));
    }
    for (int index = 0; index < numParams; index++) {
        // If the parameter has a samelen annotation, then look for other parameters in that
        // annotation and propagate default the other annotation so that it is symmetric.
        AnnotatedTypeMirror atm = params.get(index);
        AnnotationMirror sameLenAnno = atm.getAnnotation(SameLen.class);
        if (sameLenAnno == null) {
            continue;
        }
        List<String> values = AnnotationUtils.getElementValueArray(sameLenAnno, aTypeFactory.sameLenValueElement, String.class);
        for (String value : values) {
            int otherParamIndex = paramNames.indexOf(value);
            if (otherParamIndex == -1) {
                continue;
            }
            // the SameLen value is in the list of params, so modify the type of
            // that param in the store
            AnnotationMirror newSameLen = aTypeFactory.createSameLen(Collections.singletonList(paramNames.get(index)));
            JavaExpression otherParamRec = JavaExpression.fromVariableTree(paramTrees.get(otherParamIndex));
            info.insertValuePermitNondeterministic(otherParamRec, newSameLen);
        }
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) ArrayList(java.util.ArrayList) VariableTree(com.sun.source.tree.VariableTree) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 63 with JavaExpression

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

the class SameLenVisitor method commonAssignmentCheck.

/**
 * Merges SameLen annotations, then calls super.
 *
 * <p>{@inheritDoc}
 */
@Override
protected void commonAssignmentCheck(AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, Tree valueTree, @CompilerMessageKey String errorKey, Object... extraArgs) {
    if (IndexUtil.isSequenceType(valueType.getUnderlyingType()) && TreeUtils.isExpressionTree(valueTree) && // if both annotations are @PolySameLen, there is nothing to do
    !(valueType.hasAnnotation(PolySameLen.class) && varType.hasAnnotation(PolySameLen.class))) {
        JavaExpression rhs = JavaExpression.fromTree((ExpressionTree) valueTree);
        if (rhs != null && SameLenAnnotatedTypeFactory.mayAppearInSameLen(rhs)) {
            String rhsExpr = rhs.toString();
            AnnotationMirror sameLenAnno = valueType.getAnnotation(SameLen.class);
            Collection<String> exprs;
            if (sameLenAnno == null) {
                exprs = Collections.singletonList(rhsExpr);
            } else {
                exprs = new TreeSet<>(AnnotationUtils.getElementValueArray(sameLenAnno, atypeFactory.sameLenValueElement, String.class));
                exprs.add(rhsExpr);
            }
            AnnotationMirror newSameLen = atypeFactory.createSameLen(exprs);
            valueType.replaceAnnotation(newSameLen);
        }
    }
    super.commonAssignmentCheck(varType, valueType, valueTree, errorKey, extraArgs);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) JavaExpression(org.checkerframework.dataflow.expression.JavaExpression)

Example 64 with JavaExpression

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

the class OffsetEquation method createOffsetFromNode.

/**
 * Updates an offset equation from a Node.
 *
 * @param node the Node from which to create an offset equation
 * @param factory an AnnotationTypeFactory
 * @param eq an OffsetEquation to update
 * @param op '+' or '-'
 */
private static void createOffsetFromNode(Node node, AnnotationProvider factory, OffsetEquation eq, char op) {
    JavaExpression je = JavaExpression.fromNode(node);
    if (je instanceof Unknown || je == null) {
        if (node instanceof NumericalAdditionNode) {
            createOffsetFromNode(((NumericalAdditionNode) node).getLeftOperand(), factory, eq, op);
            createOffsetFromNode(((NumericalAdditionNode) node).getRightOperand(), factory, eq, op);
        } else if (node instanceof NumericalSubtractionNode) {
            createOffsetFromNode(((NumericalSubtractionNode) node).getLeftOperand(), factory, eq, op);
            char other = op == '+' ? '-' : '+';
            createOffsetFromNode(((NumericalSubtractionNode) node).getRightOperand(), factory, eq, other);
        } else {
            eq.error = node.toString();
        }
    } else {
        eq.addTerm(op, je.toString());
    }
}
Also used : JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) NumericalAdditionNode(org.checkerframework.dataflow.cfg.node.NumericalAdditionNode) Unknown(org.checkerframework.dataflow.expression.Unknown) NumericalSubtractionNode(org.checkerframework.dataflow.cfg.node.NumericalSubtractionNode)

Example 65 with JavaExpression

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

the class UpperBoundAnnotatedTypeFactory method fromLessThanOrEqual.

private UBQualifier fromLessThanOrEqual(Tree tree, TreePath treePath, List<String> lessThanExpressions) {
    UBQualifier ubQualifier = null;
    for (String expression : lessThanExpressions) {
        Pair<JavaExpression, String> exprAndOffset;
        try {
            exprAndOffset = getExpressionAndOffsetFromJavaExpressionString(expression, treePath);
        } catch (JavaExpressionParseException e) {
            exprAndOffset = null;
        }
        if (exprAndOffset == null) {
            continue;
        }
        JavaExpression je = exprAndOffset.first;
        String offset = exprAndOffset.second;
        if (!CFAbstractStore.canInsertJavaExpression(je)) {
            continue;
        }
        CFStore store = getStoreBefore(tree);
        CFValue value = store.getValue(je);
        if (value != null && value.getAnnotations().size() == 1) {
            UBQualifier newUBQ = UBQualifier.createUBQualifier(qualHierarchy.findAnnotationInHierarchy(value.getAnnotations(), UNKNOWN), AnnotatedTypeFactory.negateConstant(offset), (IndexChecker) checker);
            if (ubQualifier == null) {
                ubQualifier = newUBQ;
            } else {
                ubQualifier = ubQualifier.glb(newUBQ);
            }
        }
    }
    return ubQualifier;
}
Also used : CFValue(org.checkerframework.framework.flow.CFValue) CFStore(org.checkerframework.framework.flow.CFStore) JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) JavaExpressionParseException(org.checkerframework.framework.util.JavaExpressionParseUtil.JavaExpressionParseException)

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