Search in sources :

Example 21 with MethodInvocationNode

use of org.checkerframework.dataflow.cfg.node.MethodInvocationNode in project checker-framework by typetools.

the class InitializationTransfer method initializedFieldsAfterCall.

/**
 * Returns the fields that can safely be considered initialized after the method call {@code
 * node}.
 */
protected List<VariableElement> initializedFieldsAfterCall(MethodInvocationNode node, ConditionalTransferResult<V, S> transferResult) {
    List<VariableElement> result = new ArrayList<>();
    MethodInvocationTree tree = node.getTree();
    ExecutableElement method = TreeUtils.elementFromUse(tree);
    boolean isConstructor = method.getSimpleName().contentEquals("<init>");
    Node receiver = node.getTarget().getReceiver();
    String methodString = tree.getMethodSelect().toString();
    // invariant fields are guaranteed to be initialized.
    if (isConstructor && receiver instanceof ThisLiteralNode && methodString.equals("this")) {
        ClassTree clazz = TreeUtils.enclosingClass(analysis.getTypeFactory().getPath(tree));
        TypeElement clazzElem = TreeUtils.elementFromDeclaration(clazz);
        markInvariantFieldsAsInitialized(result, clazzElem);
    }
    // invariant fields of any super class are guaranteed to be initialized.
    if (isConstructor && receiver instanceof ThisLiteralNode && methodString.equals("super")) {
        ClassTree clazz = TreeUtils.enclosingClass(analysis.getTypeFactory().getPath(tree));
        TypeElement clazzElem = TreeUtils.elementFromDeclaration(clazz);
        TypeMirror superClass = clazzElem.getSuperclass();
        while (superClass != null && superClass.getKind() != TypeKind.NONE) {
            clazzElem = (TypeElement) analysis.getTypes().asElement(superClass);
            superClass = clazzElem.getSuperclass();
            markInvariantFieldsAsInitialized(result, clazzElem);
        }
    }
    return result;
}
Also used : AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ThisLiteralNode(org.checkerframework.dataflow.cfg.node.ThisLiteralNode) 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) ThisLiteralNode(org.checkerframework.dataflow.cfg.node.ThisLiteralNode) ArrayList(java.util.ArrayList) ClassTree(com.sun.source.tree.ClassTree) VariableElement(javax.lang.model.element.VariableElement)

Example 22 with MethodInvocationNode

use of org.checkerframework.dataflow.cfg.node.MethodInvocationNode in project checker-framework by typetools.

the class RegexTransfer method handleMatcherGroupCount.

/**
 * See whether possibleMatcher is a call of groupCount on a Matcher and possibleConstant is a
 * constant. If so, annotate the matcher as constant + 1 if !isAlsoEqual constant if isAlsoEqual
 *
 * @param possibleMatcher the Node that might be a call of Matcher.groupCount()
 * @param possibleConstant the Node that might be a constant
 * @param isAlsoEqual whether the comparison operation is strict or reflexive
 * @param in the TransferInput
 * @param resultIn TransferResult
 * @return the possibly refined output TransferResult
 */
private TransferResult<CFValue, CFStore> handleMatcherGroupCount(Node possibleMatcher, Node possibleConstant, boolean isAlsoEqual, TransferInput<CFValue, CFStore> in, TransferResult<CFValue, CFStore> resultIn) {
    if (!(possibleMatcher instanceof MethodInvocationNode)) {
        return resultIn;
    }
    if (!(possibleConstant instanceof IntegerLiteralNode)) {
        return resultIn;
    }
    MethodAccessNode methodAccessNode = ((MethodInvocationNode) possibleMatcher).getTarget();
    ExecutableElement method = methodAccessNode.getMethod();
    Node receiver = methodAccessNode.getReceiver();
    if (!isMatcherGroupCountMethod(method, receiver)) {
        return resultIn;
    }
    Receiver matcherReceiver = FlowExpressions.internalReprOf(analysis.getTypeFactory(), receiver);
    IntegerLiteralNode iln = (IntegerLiteralNode) possibleConstant;
    int groupCount;
    if (isAlsoEqual) {
        groupCount = iln.getValue();
    } else {
        groupCount = iln.getValue() + 1;
    }
    CFStore thenStore = resultIn.getRegularStore();
    CFStore elseStore = thenStore.copy();
    ConditionalTransferResult<CFValue, CFStore> newResult = new ConditionalTransferResult<>(resultIn.getResultValue(), thenStore, elseStore);
    RegexAnnotatedTypeFactory factory = (RegexAnnotatedTypeFactory) analysis.getTypeFactory();
    AnnotationMirror regexAnnotation = factory.createRegexAnnotation(groupCount);
    thenStore.insertValue(matcherReceiver, regexAnnotation);
    return newResult;
}
Also used : CFStore(org.checkerframework.framework.flow.CFStore) MethodAccessNode(org.checkerframework.dataflow.cfg.node.MethodAccessNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) ConditionalTransferResult(org.checkerframework.dataflow.analysis.ConditionalTransferResult) ExecutableElement(javax.lang.model.element.ExecutableElement) MethodAccessNode(org.checkerframework.dataflow.cfg.node.MethodAccessNode) ClassNameNode(org.checkerframework.dataflow.cfg.node.ClassNameNode) LessThanNode(org.checkerframework.dataflow.cfg.node.LessThanNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) IntegerLiteralNode(org.checkerframework.dataflow.cfg.node.IntegerLiteralNode) GreaterThanNode(org.checkerframework.dataflow.cfg.node.GreaterThanNode) LessThanOrEqualNode(org.checkerframework.dataflow.cfg.node.LessThanOrEqualNode) GreaterThanOrEqualNode(org.checkerframework.dataflow.cfg.node.GreaterThanOrEqualNode) Node(org.checkerframework.dataflow.cfg.node.Node) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver) CFValue(org.checkerframework.framework.flow.CFValue) AnnotationMirror(javax.lang.model.element.AnnotationMirror) IntegerLiteralNode(org.checkerframework.dataflow.cfg.node.IntegerLiteralNode)

Example 23 with MethodInvocationNode

use of org.checkerframework.dataflow.cfg.node.MethodInvocationNode in project checker-framework by typetools.

the class CFAbstractTransfer method getValueFromFactory.

/**
 * Returns the abstract value of a non-leaf tree {@code tree}, as computed by the {@link
 * AnnotatedTypeFactory}.
 *
 * @return the abstract value of a non-leaf tree {@code tree}, as computed by the {@link
 *     AnnotatedTypeFactory}
 */
protected V getValueFromFactory(Tree tree, Node node) {
    GenericAnnotatedTypeFactory<V, S, T, ? extends CFAbstractAnalysis<V, S, T>> factory = analysis.atypeFactory;
    Tree preTree = analysis.getCurrentTree();
    analysis.setCurrentTree(tree);
    AnnotatedTypeMirror at;
    if (node instanceof MethodInvocationNode && ((MethodInvocationNode) node).getIterableExpression() != null) {
        ExpressionTree iter = ((MethodInvocationNode) node).getIterableExpression();
        at = factory.getIterableElementType(iter);
    } else if (node instanceof ArrayAccessNode && ((ArrayAccessNode) node).getArrayExpression() != null) {
        ExpressionTree array = ((ArrayAccessNode) node).getArrayExpression();
        at = factory.getIterableElementType(array);
    } else {
        at = factory.getAnnotatedType(tree);
    }
    analysis.setCurrentTree(preTree);
    return analysis.createAbstractValue(at);
}
Also used : UnderlyingAST(org.checkerframework.dataflow.cfg.UnderlyingAST) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) ArrayAccessNode(org.checkerframework.dataflow.cfg.node.ArrayAccessNode) MethodTree(com.sun.source.tree.MethodTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ExpressionTree(com.sun.source.tree.ExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 24 with MethodInvocationNode

use of org.checkerframework.dataflow.cfg.node.MethodInvocationNode in project checker-framework by typetools.

the class CFAbstractTransfer method processPostconditionsAndConditionalPostconditions.

/**
 * Add information from the postconditions and conditional postconditions of a method to the
 * stores after an invocation.
 *
 * @param invocationNode a method call
 * @param invocationTree the tree for the method call
 * @param thenStore the "then" store; is side-effected by this method
 * @param elseStore the "else" store; is side-effected by this method
 * @param postconditions the postconditions
 */
private void processPostconditionsAndConditionalPostconditions(MethodInvocationNode invocationNode, Tree invocationTree, S thenStore, S elseStore, Set<? extends Contract> postconditions) {
    StringToJavaExpression stringToJavaExpr = stringExpr -> StringToJavaExpression.atMethodInvocation(stringExpr, invocationNode, analysis.checker);
    for (Contract p : postconditions) {
        // Viewpoint-adapt to the method use (the call site).
        AnnotationMirror anno = p.viewpointAdaptDependentTypeAnnotation(analysis.atypeFactory, stringToJavaExpr, /*errorTree=*/
        null);
        String expressionString = p.expressionString;
        try {
            JavaExpression je = stringToJavaExpr.toJavaExpression(expressionString);
            // are removed from the store before this method is called.
            if (p.kind == Contract.Kind.CONDITIONALPOSTCONDITION) {
                if (((ConditionalPostcondition) p).resultValue) {
                    thenStore.insertOrRefinePermitNondeterministic(je, anno);
                } else {
                    elseStore.insertOrRefinePermitNondeterministic(je, anno);
                }
            } else {
                thenStore.insertOrRefinePermitNondeterministic(je, anno);
            }
        } catch (JavaExpressionParseException e) {
            // report errors here
            if (e.isFlowParseError()) {
                Object[] args = new Object[e.args.length + 1];
                args[0] = ElementUtils.getSimpleSignature(TreeUtils.elementFromUse(invocationNode.getTree()));
                System.arraycopy(e.args, 0, args, 1, e.args.length);
                analysis.checker.reportError(invocationTree, "flowexpr.parse.error.postcondition", args);
            } else {
                analysis.checker.report(invocationTree, e.getDiagMessage());
            }
        }
    }
}
Also used : NodeUtils(org.checkerframework.dataflow.util.NodeUtils) Arrays(java.util.Arrays) TransferResult(org.checkerframework.dataflow.analysis.TransferResult) Modifier(javax.lang.model.element.Modifier) ForwardTransferFunction(org.checkerframework.dataflow.analysis.ForwardTransferFunction) TypeElement(javax.lang.model.element.TypeElement) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) StringConversionNode(org.checkerframework.dataflow.cfg.node.StringConversionNode) GenericAnnotatedTypeFactory(org.checkerframework.framework.type.GenericAnnotatedTypeFactory) Map(java.util.Map) WideningConversionNode(org.checkerframework.dataflow.cfg.node.WideningConversionNode) InternedDistinct(org.checkerframework.checker.interning.qual.InternedDistinct) AnnotatedExecutableType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType) ClassNameNode(org.checkerframework.dataflow.cfg.node.ClassNameNode) TreePath(com.sun.source.util.TreePath) ObjectCreationNode(org.checkerframework.dataflow.cfg.node.ObjectCreationNode) UnderlyingAST(org.checkerframework.dataflow.cfg.UnderlyingAST) AbstractNodeVisitor(org.checkerframework.dataflow.cfg.node.AbstractNodeVisitor) Set(java.util.Set) Element(javax.lang.model.element.Element) Contract(org.checkerframework.framework.util.Contract) TreeUtils(org.checkerframework.javacutil.TreeUtils) CFGMethod(org.checkerframework.dataflow.cfg.UnderlyingAST.CFGMethod) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode) NarrowingConversionNode(org.checkerframework.dataflow.cfg.node.NarrowingConversionNode) List(java.util.List) LocalVariable(org.checkerframework.dataflow.expression.LocalVariable) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) EqualToNode(org.checkerframework.dataflow.cfg.node.EqualToNode) AnnotatedTypes(org.checkerframework.framework.util.AnnotatedTypes) CFGLambda(org.checkerframework.dataflow.cfg.UnderlyingAST.CFGLambda) VariableDeclarationNode(org.checkerframework.dataflow.cfg.node.VariableDeclarationNode) RegularTransferResult(org.checkerframework.dataflow.analysis.RegularTransferResult) TernaryExpressionNode(org.checkerframework.dataflow.cfg.node.TernaryExpressionNode) Postcondition(org.checkerframework.framework.util.Contract.Postcondition) MethodTree(com.sun.source.tree.MethodTree) ConditionalPostcondition(org.checkerframework.framework.util.Contract.ConditionalPostcondition) VariableElement(javax.lang.model.element.VariableElement) InstanceOfNode(org.checkerframework.dataflow.cfg.node.InstanceOfNode) HashMap(java.util.HashMap) ThisNode(org.checkerframework.dataflow.cfg.node.ThisNode) ArrayList(java.util.ArrayList) ConditionalTransferResult(org.checkerframework.dataflow.analysis.ConditionalTransferResult) AssignmentNode(org.checkerframework.dataflow.cfg.node.AssignmentNode) CaseNode(org.checkerframework.dataflow.cfg.node.CaseNode) HashSet(java.util.HashSet) LambdaResultExpressionNode(org.checkerframework.dataflow.cfg.node.LambdaResultExpressionNode) Precondition(org.checkerframework.framework.util.Contract.Precondition) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) TreePathUtil(org.checkerframework.javacutil.TreePathUtil) SwitchExpressionNode(org.checkerframework.dataflow.cfg.node.SwitchExpressionNode) NotEqualNode(org.checkerframework.dataflow.cfg.node.NotEqualNode) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) Nullable(org.checkerframework.checker.nullness.qual.Nullable) FieldAccess(org.checkerframework.dataflow.expression.FieldAccess) TransferInput(org.checkerframework.dataflow.analysis.TransferInput) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) ArrayAccessNode(org.checkerframework.dataflow.cfg.node.ArrayAccessNode) ConditionalNotNode(org.checkerframework.dataflow.cfg.node.ConditionalNotNode) JavaExpressionParseException(org.checkerframework.framework.util.JavaExpressionParseUtil.JavaExpressionParseException) ElementKind(javax.lang.model.element.ElementKind) ExpressionTree(com.sun.source.tree.ExpressionTree) ExecutableElement(javax.lang.model.element.ExecutableElement) ReturnNode(org.checkerframework.dataflow.cfg.node.ReturnNode) JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) FieldInitialValue(org.checkerframework.framework.flow.CFAbstractAnalysis.FieldInitialValue) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) AnnotationMirror(javax.lang.model.element.AnnotationMirror) StringToJavaExpression(org.checkerframework.framework.util.StringToJavaExpression) AnnotatedTypeFactory(org.checkerframework.framework.type.AnnotatedTypeFactory) StringConcatenateAssignmentNode(org.checkerframework.dataflow.cfg.node.StringConcatenateAssignmentNode) TypeMirror(javax.lang.model.type.TypeMirror) Collections(java.util.Collections) ElementUtils(org.checkerframework.javacutil.ElementUtils) Node(org.checkerframework.dataflow.cfg.node.Node) ContractsFromMethod(org.checkerframework.framework.util.ContractsFromMethod) AnnotationMirror(javax.lang.model.element.AnnotationMirror) JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) StringToJavaExpression(org.checkerframework.framework.util.StringToJavaExpression) StringToJavaExpression(org.checkerframework.framework.util.StringToJavaExpression) JavaExpressionParseException(org.checkerframework.framework.util.JavaExpressionParseUtil.JavaExpressionParseException) ConditionalPostcondition(org.checkerframework.framework.util.Contract.ConditionalPostcondition) Contract(org.checkerframework.framework.util.Contract)

Example 25 with MethodInvocationNode

use of org.checkerframework.dataflow.cfg.node.MethodInvocationNode in project checker-framework by typetools.

the class CalledMethodsTransfer method visitMethodInvocation.

@Override
public TransferResult<CFValue, CFStore> visitMethodInvocation(final MethodInvocationNode node, final TransferInput<CFValue, CFStore> input) {
    exceptionalStores = makeExceptionalStores(node, input);
    TransferResult<CFValue, CFStore> superResult = super.visitMethodInvocation(node, input);
    handleEnsuresCalledMethodsVarArgs(node, superResult);
    Node receiver = node.getTarget().getReceiver();
    if (receiver != null) {
        String methodName = node.getTarget().getMethod().getSimpleName().toString();
        methodName = ((CalledMethodsAnnotatedTypeFactory) atypeFactory).adjustMethodNameUsingValueChecker(methodName, node.getTree());
        accumulate(receiver, superResult, methodName);
    }
    TransferResult<CFValue, CFStore> finalResult = new ConditionalTransferResult<>(superResult.getResultValue(), superResult.getThenStore(), superResult.getElseStore(), exceptionalStores);
    exceptionalStores = null;
    return finalResult;
}
Also used : CFValue(org.checkerframework.framework.flow.CFValue) CFStore(org.checkerframework.framework.flow.CFStore) ConditionalTransferResult(org.checkerframework.dataflow.analysis.ConditionalTransferResult) ArrayCreationNode(org.checkerframework.dataflow.cfg.node.ArrayCreationNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) Node(org.checkerframework.dataflow.cfg.node.Node)

Aggregations

MethodInvocationNode (org.checkerframework.dataflow.cfg.node.MethodInvocationNode)42 Node (org.checkerframework.dataflow.cfg.node.Node)38 FieldAccessNode (org.checkerframework.dataflow.cfg.node.FieldAccessNode)21 ObjectCreationNode (org.checkerframework.dataflow.cfg.node.ObjectCreationNode)16 ExecutableElement (javax.lang.model.element.ExecutableElement)15 AssignmentNode (org.checkerframework.dataflow.cfg.node.AssignmentNode)15 MethodAccessNode (org.checkerframework.dataflow.cfg.node.MethodAccessNode)14 LocalVariableNode (org.checkerframework.dataflow.cfg.node.LocalVariableNode)13 ClassNameNode (org.checkerframework.dataflow.cfg.node.ClassNameNode)12 ReturnNode (org.checkerframework.dataflow.cfg.node.ReturnNode)12 ArrayCreationNode (org.checkerframework.dataflow.cfg.node.ArrayCreationNode)11 GreaterThanNode (org.checkerframework.dataflow.cfg.node.GreaterThanNode)11 GreaterThanOrEqualNode (org.checkerframework.dataflow.cfg.node.GreaterThanOrEqualNode)11 LessThanNode (org.checkerframework.dataflow.cfg.node.LessThanNode)11 LessThanOrEqualNode (org.checkerframework.dataflow.cfg.node.LessThanOrEqualNode)11 StringConversionNode (org.checkerframework.dataflow.cfg.node.StringConversionNode)11 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)10 ArrayAccessNode (org.checkerframework.dataflow.cfg.node.ArrayAccessNode)10 CFStore (org.checkerframework.framework.flow.CFStore)10 CFValue (org.checkerframework.framework.flow.CFValue)10