Search in sources :

Example 1 with Precondition

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

the class CFAbstractTransfer method addInformationFromPreconditions.

/**
 * Add the information from all the preconditions of the method {@code method} with
 * corresponding tree {@code methodTree} to the store {@code info}.
 */
protected void addInformationFromPreconditions(S info, AnnotatedTypeFactory factory, CFGMethod method, MethodTree methodTree, ExecutableElement methodElement) {
    ContractsUtils contracts = ContractsUtils.getInstance(analysis.atypeFactory);
    FlowExpressionContext flowExprContext = null;
    Set<Precondition> preconditions = contracts.getPreconditions(methodElement);
    for (Precondition p : preconditions) {
        String expression = p.expression;
        AnnotationMirror annotation = p.annotation;
        if (flowExprContext == null) {
            flowExprContext = FlowExpressionContext.buildContextForMethodDeclaration(methodTree, method.getClassTree(), analysis.checker.getContext());
        }
        TreePath localScope = analysis.atypeFactory.getPath(methodTree);
        annotation = standardizeAnnotationFromContract(annotation, flowExprContext, localScope);
        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)
            FlowExpressions.Receiver expr = FlowExpressionParseUtil.parse(expression, flowExprContext, localScope, false);
            info.insertValue(expr, annotation);
        } catch (FlowExpressionParseException e) {
        // Errors are reported by BaseTypeVisitor.checkContractsAtMethodDeclaration()
        }
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TreePath(com.sun.source.util.TreePath) FlowExpressionContext(org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionContext) Precondition(org.checkerframework.framework.util.ContractsUtils.Precondition) FlowExpressions(org.checkerframework.dataflow.analysis.FlowExpressions) FlowExpressionParseException(org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionParseException) ContractsUtils(org.checkerframework.framework.util.ContractsUtils) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver)

Example 2 with Precondition

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

the class BaseTypeVisitor method checkPreconditions.

/**
 * Checks that all the given {@code preconditions} hold true immediately prior to the method
 * invocation or variable access at {@code tree}.
 *
 * @param tree the Tree immediately prior to which the preconditions must hold true
 * @param preconditions the preconditions to be checked
 */
protected void checkPreconditions(MethodInvocationTree tree, Set<Precondition> preconditions) {
    // TODO: Remove this check and investigate the root cause.
    if (preconditions.isEmpty()) {
        return;
    }
    FlowExpressionContext flowExprContext = FlowExpressionContext.buildContextForMethodUse(tree, checker.getContext());
    if (flowExprContext == null) {
        checker.report(Result.failure("flowexpr.parse.context.not.determined", tree), tree);
        return;
    }
    for (Precondition p : preconditions) {
        String expression = p.expression;
        AnnotationMirror anno = p.annotation;
        anno = standardizeAnnotationFromContract(anno, flowExprContext, getCurrentPath());
        try {
            FlowExpressions.Receiver expr = FlowExpressionParseUtil.parse(expression, flowExprContext, getCurrentPath(), false);
            CFAbstractStore<?, ?> store = atypeFactory.getStoreBefore(tree);
            CFAbstractValue<?> value = store.getValue(expr);
            AnnotationMirror inferredAnno = null;
            if (value != null) {
                QualifierHierarchy hierarchy = atypeFactory.getQualifierHierarchy();
                Set<AnnotationMirror> annos = value.getAnnotations();
                inferredAnno = hierarchy.findAnnotationInSameHierarchy(annos, anno);
            }
            if (!checkContract(expr, anno, inferredAnno, store)) {
                checker.report(Result.failure("contracts.precondition.not.satisfied", tree.toString(), expr == null ? expression : expr.toString()), tree);
            }
        } catch (FlowExpressionParseException e) {
            // report errors here
            checker.report(e.getResult(), tree);
        }
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) FlowExpressionContext(org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionContext) Precondition(org.checkerframework.framework.util.ContractsUtils.Precondition) QualifierHierarchy(org.checkerframework.framework.type.QualifierHierarchy) FlowExpressions(org.checkerframework.dataflow.analysis.FlowExpressions) FlowExpressionParseException(org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionParseException) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver)

Aggregations

AnnotationMirror (javax.lang.model.element.AnnotationMirror)2 FlowExpressions (org.checkerframework.dataflow.analysis.FlowExpressions)2 Receiver (org.checkerframework.dataflow.analysis.FlowExpressions.Receiver)2 Precondition (org.checkerframework.framework.util.ContractsUtils.Precondition)2 FlowExpressionContext (org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionContext)2 FlowExpressionParseException (org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionParseException)2 TreePath (com.sun.source.util.TreePath)1 QualifierHierarchy (org.checkerframework.framework.type.QualifierHierarchy)1 ContractsUtils (org.checkerframework.framework.util.ContractsUtils)1