Search in sources :

Example 1 with ContractsFromMethod

use of org.checkerframework.framework.util.ContractsFromMethod in project checker-framework by typetools.

the class CFAbstractTransfer method addInformationFromPreconditions.

/**
 * Add the information from all the preconditions of a method to the initial store in the method
 * body.
 *
 * @param initialStore the initial store for the method body
 * @param factory the type factory
 * @param methodAst the AST for a method declaration
 * @param methodDeclTree the declaration of the method; is a field of {@code methodAst}
 * @param methodElement the element for the method
 */
protected void addInformationFromPreconditions(S initialStore, AnnotatedTypeFactory factory, CFGMethod methodAst, MethodTree methodDeclTree, ExecutableElement methodElement) {
    ContractsFromMethod contractsUtils = analysis.atypeFactory.getContractsFromMethod();
    Set<Precondition> preconditions = contractsUtils.getPreconditions(methodElement);
    StringToJavaExpression stringToJavaExpr = stringExpr -> StringToJavaExpression.atMethodBody(stringExpr, methodDeclTree, analysis.checker);
    for (Precondition p : preconditions) {
        String stringExpr = p.expressionString;
        AnnotationMirror annotation = p.viewpointAdaptDependentTypeAnnotation(analysis.atypeFactory, stringToJavaExpr, /*errorTree=*/
        null);
        JavaExpression exprJe;
        try {
            // TODO: currently, these expressions are parsed at the declaration (i.e. here) and for
            // every use. this could be optimized to store the result the first time.
            // (same for other annotations)
            exprJe = StringToJavaExpression.atMethodBody(stringExpr, methodDeclTree, analysis.checker);
        } catch (JavaExpressionParseException e) {
            // Errors are reported by BaseTypeVisitor.checkContractsAtMethodDeclaration().
            continue;
        }
        initialStore.insertValuePermitNondeterministic(exprJe, annotation);
    }
}
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) Precondition(org.checkerframework.framework.util.Contract.Precondition) StringToJavaExpression(org.checkerframework.framework.util.StringToJavaExpression) JavaExpressionParseException(org.checkerframework.framework.util.JavaExpressionParseUtil.JavaExpressionParseException) ContractsFromMethod(org.checkerframework.framework.util.ContractsFromMethod)

Example 2 with ContractsFromMethod

use of org.checkerframework.framework.util.ContractsFromMethod in project checker-framework by typetools.

the class CFAbstractTransfer method processPostconditions.

/**
 * Add information from the postconditions of a method to the store after an invocation.
 *
 * @param invocationNode a method call
 * @param store a store; is side-effected by this method
 * @param methodElement the method being called
 * @param invocationTree the tree for the method call
 */
protected void processPostconditions(MethodInvocationNode invocationNode, S store, ExecutableElement methodElement, Tree invocationTree) {
    ContractsFromMethod contractsUtils = analysis.atypeFactory.getContractsFromMethod();
    Set<Postcondition> postconditions = contractsUtils.getPostconditions(methodElement);
    processPostconditionsAndConditionalPostconditions(invocationNode, invocationTree, store, null, postconditions);
}
Also used : ContractsFromMethod(org.checkerframework.framework.util.ContractsFromMethod) Postcondition(org.checkerframework.framework.util.Contract.Postcondition) ConditionalPostcondition(org.checkerframework.framework.util.Contract.ConditionalPostcondition)

Example 3 with ContractsFromMethod

use of org.checkerframework.framework.util.ContractsFromMethod in project checker-framework by typetools.

the class CFAbstractTransfer method processConditionalPostconditions.

/**
 * Add information from the conditional postconditions of a method to the stores after an
 * invocation.
 *
 * @param invocationNode a method call
 * @param methodElement the method being called
 * @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
 */
protected void processConditionalPostconditions(MethodInvocationNode invocationNode, ExecutableElement methodElement, Tree invocationTree, S thenStore, S elseStore) {
    ContractsFromMethod contractsUtils = analysis.atypeFactory.getContractsFromMethod();
    Set<ConditionalPostcondition> conditionalPostconditions = contractsUtils.getConditionalPostconditions(methodElement);
    processPostconditionsAndConditionalPostconditions(invocationNode, invocationTree, thenStore, elseStore, conditionalPostconditions);
}
Also used : ContractsFromMethod(org.checkerframework.framework.util.ContractsFromMethod) ConditionalPostcondition(org.checkerframework.framework.util.Contract.ConditionalPostcondition)

Aggregations

ConditionalPostcondition (org.checkerframework.framework.util.Contract.ConditionalPostcondition)3 ContractsFromMethod (org.checkerframework.framework.util.ContractsFromMethod)3 Postcondition (org.checkerframework.framework.util.Contract.Postcondition)2 ClassTree (com.sun.source.tree.ClassTree)1 ExpressionTree (com.sun.source.tree.ExpressionTree)1 MethodTree (com.sun.source.tree.MethodTree)1 Tree (com.sun.source.tree.Tree)1 TreePath (com.sun.source.util.TreePath)1 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 Element (javax.lang.model.element.Element)1 ElementKind (javax.lang.model.element.ElementKind)1