Search in sources :

Example 6 with JavaExpression

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

the class ResourceLeakTransfer method updateStoreWithTempVar.

/**
 * This method either creates or looks up the temp var t for node, and then updates the store to
 * give t the same type as node. Temporary variables are supported for expressions throughout this
 * checker (and the Must Call Checker) to enable refinement of their types. See the documentation
 * of {@link MustCallConsistencyAnalyzer} for more details.
 *
 * @param node the node to be assigned to a temporary variable
 * @param result the transfer result containing the store to be modified
 */
public void updateStoreWithTempVar(TransferResult<CFValue, CFStore> result, Node node) {
    // Must-call obligations on primitives are not supported.
    if (!TypesUtils.isPrimitiveOrBoxed(node.getType())) {
        MustCallAnnotatedTypeFactory mcAtf = rlTypeFactory.getTypeFactoryOfSubchecker(MustCallChecker.class);
        LocalVariableNode temp = mcAtf.getTempVar(node);
        if (temp != null) {
            rlTypeFactory.addTempVar(temp, node.getTree());
            JavaExpression localExp = JavaExpression.fromNode(temp);
            AnnotationMirror anm = rlTypeFactory.getAnnotatedType(node.getTree()).getAnnotationInHierarchy(rlTypeFactory.top);
            insertIntoStores(result, localExp, anm == null ? rlTypeFactory.top : anm);
        }
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) MustCallAnnotatedTypeFactory(org.checkerframework.checker.mustcall.MustCallAnnotatedTypeFactory) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode)

Example 7 with JavaExpression

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

the class KeyForTransfer method visitMethodInvocation.

/*
   * Provided that m is of a type that implements interface java.util.Map:
   * <ul>
   * <li>Given a call m.containsKey(k), ensures that k is @KeyFor("m") in the thenStore of the transfer result.
   * <li>Given a call m.put(k, ...), ensures that k is @KeyFor("m") in the thenStore and elseStore of the transfer result.
   * </ul>
   */
@Override
public TransferResult<KeyForValue, KeyForStore> visitMethodInvocation(MethodInvocationNode node, TransferInput<KeyForValue, KeyForStore> in) {
    TransferResult<KeyForValue, KeyForStore> result = super.visitMethodInvocation(node, in);
    KeyForAnnotatedTypeFactory factory = (KeyForAnnotatedTypeFactory) analysis.getTypeFactory();
    if (factory.isMapContainsKey(node) || factory.isMapPut(node)) {
        Node receiver = node.getTarget().getReceiver();
        JavaExpression receiverJe = JavaExpression.fromNode(receiver);
        String mapName = receiverJe.toString();
        JavaExpression keyExpr = JavaExpression.fromNode(node.getArgument(0));
        LinkedHashSet<String> keyForMaps = new LinkedHashSet<>();
        keyForMaps.add(mapName);
        final KeyForValue previousKeyValue = in.getValueOfSubNode(node.getArgument(0));
        if (previousKeyValue != null) {
            for (AnnotationMirror prevAm : previousKeyValue.getAnnotations()) {
                if (prevAm != null && factory.areSameByClass(prevAm, KeyFor.class)) {
                    keyForMaps.addAll(getKeys(prevAm));
                }
            }
        }
        AnnotationMirror am = factory.createKeyForAnnotationMirrorWithValue(keyForMaps);
        if (factory.isMapContainsKey(node)) {
            // method is Map.containsKey
            result.getThenStore().insertValue(keyExpr, am);
        } else {
            // method is Map.put
            result.getThenStore().insertValue(keyExpr, am);
            result.getElseStore().insertValue(keyExpr, am);
        }
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AnnotationMirror(javax.lang.model.element.AnnotationMirror) JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) Node(org.checkerframework.dataflow.cfg.node.Node) KeyFor(org.checkerframework.checker.nullness.qual.KeyFor)

Example 8 with JavaExpression

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

the class MustCallTransfer method visitAssignment.

@Override
public TransferResult<CFValue, CFStore> visitAssignment(AssignmentNode n, TransferInput<CFValue, CFStore> in) {
    TransferResult<CFValue, CFStore> result = super.visitAssignment(n, in);
    // resource variables are closed.
    if (atypeFactory.isResourceVariable(TreeUtils.elementFromTree(n.getTarget().getTree()))) {
        CFStore store = result.getRegularStore();
        JavaExpression expr = JavaExpression.fromNode(n.getTarget());
        CFValue value = store.getValue(expr);
        AnnotationMirror withClose = atypeFactory.getAnnotationByClass(value.getAnnotations(), MustCall.class);
        if (withClose == null) {
            return result;
        }
        AnnotationMirror withoutClose = atypeFactory.withoutClose(withClose);
        insertIntoStores(result, expr, withoutClose);
    }
    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)

Example 9 with JavaExpression

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

the class LockTransfer method makeLockHeld.

/**
 * Sets a given {@link Node} to @LockHeld in the given {@code store}.
 *
 * @param store the store to update
 * @param node the node that should be @LockHeld
 */
protected void makeLockHeld(LockStore store, Node node) {
    JavaExpression internalRepr = JavaExpression.fromNode(node);
    store.insertValue(internalRepr, atypeFactory.LOCKHELD);
}
Also used : JavaExpression(org.checkerframework.dataflow.expression.JavaExpression)

Example 10 with JavaExpression

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

the class NullnessTransfer method makeNonNull.

/**
 * Sets a given {@link Node} to non-null in the given {@code store}. Calls to this method
 * implement case 2.
 *
 * @param store the store to update
 * @param node the node that should be non-null
 */
protected void makeNonNull(NullnessStore store, Node node) {
    JavaExpression internalRepr = JavaExpression.fromNode(node);
    store.insertValue(internalRepr, NONNULL);
}
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