Search in sources :

Example 1 with BooleanLiteralNode

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

the class BaseTypeVisitor method checkConditionalPostcondition.

/**
 * Check that the expression's type is annotated with {@code annotation} at every regular exit
 * that returns {@code result}
 *
 * @param node tree of method with the postcondition
 * @param annotation expression's type must have this annotation
 * @param expression the expression that the postcondition concerns
 * @param result result for which the postcondition is valid
 */
protected void checkConditionalPostcondition(MethodTree node, AnnotationMirror annotation, Receiver expression, boolean result) {
    boolean booleanReturnType = TypesUtils.isBooleanType(TreeUtils.typeOf(node.getReturnType()));
    if (!booleanReturnType) {
        checker.report(Result.failure("contracts.conditional.postcondition.invalid.returntype"), node);
        // annotation is invalid.
        return;
    }
    for (Pair<ReturnNode, ?> pair : atypeFactory.getReturnStatementStores(node)) {
        ReturnNode returnStmt = pair.first;
        Node retValNode = returnStmt.getResult();
        Boolean retVal = retValNode instanceof BooleanLiteralNode ? ((BooleanLiteralNode) retValNode).getValue() : null;
        TransferResult<?, ?> transferResult = (TransferResult<?, ?>) pair.second;
        if (transferResult == null) {
            // Unreachable return statements have no stores, but there is no need to check them.
            continue;
        }
        CFAbstractStore<?, ?> exitStore = (CFAbstractStore<?, ?>) (result ? transferResult.getThenStore() : transferResult.getElseStore());
        CFAbstractValue<?> value = exitStore.getValue(expression);
        // this means the result is a boolean literal
        if (!(retVal == null || retVal == result)) {
            continue;
        }
        AnnotationMirror inferredAnno = null;
        if (value != null) {
            QualifierHierarchy hierarchy = atypeFactory.getQualifierHierarchy();
            Set<AnnotationMirror> annos = value.getAnnotations();
            inferredAnno = hierarchy.findAnnotationInSameHierarchy(annos, annotation);
        }
        if (!checkContract(expression, annotation, inferredAnno, exitStore)) {
            checker.report(Result.failure("contracts.conditional.postcondition.not.satisfied", expression.toString()), returnStmt.getTree());
        }
    }
}
Also used : BooleanLiteralNode(org.checkerframework.dataflow.cfg.node.BooleanLiteralNode) BooleanLiteralNode(org.checkerframework.dataflow.cfg.node.BooleanLiteralNode) ReturnNode(org.checkerframework.dataflow.cfg.node.ReturnNode) Node(org.checkerframework.dataflow.cfg.node.Node) TransferResult(org.checkerframework.dataflow.analysis.TransferResult) ReturnNode(org.checkerframework.dataflow.cfg.node.ReturnNode) AnnotationMirror(javax.lang.model.element.AnnotationMirror) CFAbstractStore(org.checkerframework.framework.flow.CFAbstractStore) QualifierHierarchy(org.checkerframework.framework.type.QualifierHierarchy)

Aggregations

AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 TransferResult (org.checkerframework.dataflow.analysis.TransferResult)1 BooleanLiteralNode (org.checkerframework.dataflow.cfg.node.BooleanLiteralNode)1 Node (org.checkerframework.dataflow.cfg.node.Node)1 ReturnNode (org.checkerframework.dataflow.cfg.node.ReturnNode)1 CFAbstractStore (org.checkerframework.framework.flow.CFAbstractStore)1 QualifierHierarchy (org.checkerframework.framework.type.QualifierHierarchy)1