Search in sources :

Example 1 with FlowExpressionParseException

use of org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionParseException 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 FlowExpressionParseException

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

the class ValueAnnotatedTypeFactory method getMinLenFromString.

/**
 * Returns the minimum length of an array expression or 0 if the min length is unknown.
 *
 * @param sequenceExpression flow expression
 * @param tree expression tree or variable declaration
 * @param currentPath path to local scope
 * @return min length of sequenceExpression or 0
 */
public int getMinLenFromString(String sequenceExpression, Tree tree, TreePath currentPath) {
    AnnotationMirror lengthAnno = null;
    try {
        FlowExpressions.Receiver expressionObj = getReceiverFromJavaExpressionString(sequenceExpression, currentPath);
        if (expressionObj instanceof FlowExpressions.ValueLiteral) {
            FlowExpressions.ValueLiteral sequenceLiteral = (FlowExpressions.ValueLiteral) expressionObj;
            Object sequenceLiteralValue = sequenceLiteral.getValue();
            if (sequenceLiteralValue instanceof String) {
                return ((String) sequenceLiteralValue).length();
            }
        }
        lengthAnno = getAnnotationFromReceiver(expressionObj, tree, ArrayLenRange.class);
        if (lengthAnno == null) {
            lengthAnno = getAnnotationFromReceiver(expressionObj, tree, ArrayLen.class);
        }
        if (lengthAnno == null) {
            lengthAnno = getAnnotationFromReceiver(expressionObj, tree, StringVal.class);
        }
    } catch (FlowExpressionParseException e) {
    // ignore parse errors
    }
    if (lengthAnno == null) {
        // Could not find a more precise type, so return 0;
        return 0;
    }
    return getMinLenValue(lengthAnno);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) ArrayLen(org.checkerframework.common.value.qual.ArrayLen) ArrayLenRange(org.checkerframework.common.value.qual.ArrayLenRange) FlowExpressions(org.checkerframework.dataflow.analysis.FlowExpressions) StringVal(org.checkerframework.common.value.qual.StringVal) FlowExpressionParseException(org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionParseException)

Example 3 with FlowExpressionParseException

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

the class LessThanAnnotatedTypeFactory method getMinValueFromString.

/**
 * Returns the minimum value of {@code expressions} at {@code tree}.
 */
private long getMinValueFromString(String expression, Tree tree, TreePath path) {
    AnnotationMirror intRange = null;
    try {
        intRange = getValueAnnotatedTypeFactory().getAnnotationFromJavaExpressionString(expression, tree, path, IntRange.class);
    } catch (FlowExpressionParseException e) {
    }
    if (intRange != null) {
        return ValueAnnotatedTypeFactory.getRange(intRange).from;
    }
    AnnotationMirror intValue = null;
    try {
        intValue = getValueAnnotatedTypeFactory().getAnnotationFromJavaExpressionString(expression, tree, path, IntVal.class);
    } catch (FlowExpressionParseException e) {
    }
    if (intValue != null) {
        List<Long> possibleValues = ValueAnnotatedTypeFactory.getIntValues(intValue);
        return Collections.min(possibleValues);
    }
    return Long.MIN_VALUE;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) IntVal(org.checkerframework.common.value.qual.IntVal) IntRange(org.checkerframework.common.value.qual.IntRange) FlowExpressionParseException(org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionParseException)

Example 4 with FlowExpressionParseException

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

the class CFAbstractTransfer method processPostconditionsAndConditionalPostconditions.

private void processPostconditionsAndConditionalPostconditions(MethodInvocationNode n, Tree tree, S thenStore, S elseStore, Set<? extends Contract> postconditions) {
    FlowExpressionContext flowExprContext = null;
    for (Contract p : postconditions) {
        String expression = p.expression;
        AnnotationMirror anno = p.annotation;
        if (flowExprContext == null) {
            flowExprContext = FlowExpressionContext.buildContextForMethodUse(n, analysis.checker.getContext());
        }
        TreePath localScope = analysis.atypeFactory.getPath(tree);
        anno = standardizeAnnotationFromContract(anno, flowExprContext, localScope);
        try {
            FlowExpressions.Receiver r = FlowExpressionParseUtil.parse(expression, flowExprContext, localScope, false);
            if (p.kind == Contract.Kind.CONDITIONALPOSTCONDTION) {
                if (((ConditionalPostcondition) p).annoResult) {
                    thenStore.insertValue(r, anno);
                } else {
                    elseStore.insertValue(r, anno);
                }
            } else {
                thenStore.insertValue(r, anno);
            }
        } catch (FlowExpressionParseException e) {
            Result result;
            if (e.isFlowParseError()) {
                Object[] args = new Object[e.args.length + 1];
                args[0] = ElementUtils.getVerboseName(TreeUtils.elementFromUse(n.getTree()));
                System.arraycopy(e.args, 0, args, 1, e.args.length);
                result = Result.failure("flowexpr.parse.error.postcondition", args);
            } else {
                result = e.getResult();
            }
            // report errors here
            analysis.checker.report(result, tree);
        }
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TreePath(com.sun.source.util.TreePath) FlowExpressionContext(org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionContext) FlowExpressions(org.checkerframework.dataflow.analysis.FlowExpressions) FlowExpressionParseException(org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionParseException) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver) ConditionalPostcondition(org.checkerframework.framework.util.ContractsUtils.ConditionalPostcondition) Contract(org.checkerframework.framework.util.ContractsUtils.Contract) TransferResult(org.checkerframework.dataflow.analysis.TransferResult) Result(org.checkerframework.framework.source.Result) RegularTransferResult(org.checkerframework.dataflow.analysis.RegularTransferResult) ConditionalTransferResult(org.checkerframework.dataflow.analysis.ConditionalTransferResult)

Example 5 with FlowExpressionParseException

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

the class UpperBoundAnnotatedTypeFactory method fromLessThanOrEqual.

private UBQualifier fromLessThanOrEqual(Tree tree, TreePath treePath, List<String> lessThanExpressions) {
    UBQualifier ubQualifier = null;
    for (String expression : lessThanExpressions) {
        FlowExpressions.Receiver receiver;
        try {
            receiver = getReceiverFromJavaExpressionString(expression, treePath);
        } catch (FlowExpressionParseException e) {
            receiver = null;
        }
        if (receiver == null || !CFAbstractStore.canInsertReceiver(receiver)) {
            continue;
        }
        CFStore store = getStoreBefore(tree);
        if (store != null) {
            CFValue value = store.getValue(receiver);
            if (value != null && value.getAnnotations().size() == 1) {
                UBQualifier newUBQ = UBQualifier.createUBQualifier(value.getAnnotations().iterator().next());
                if (ubQualifier == null) {
                    ubQualifier = newUBQ;
                } else {
                    ubQualifier = ubQualifier.glb(newUBQ);
                }
            }
        }
    }
    return ubQualifier;
}
Also used : CFValue(org.checkerframework.framework.flow.CFValue) CFStore(org.checkerframework.framework.flow.CFStore) FlowExpressions(org.checkerframework.dataflow.analysis.FlowExpressions) FlowExpressionParseException(org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionParseException)

Aggregations

FlowExpressionParseException (org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionParseException)10 AnnotationMirror (javax.lang.model.element.AnnotationMirror)8 FlowExpressions (org.checkerframework.dataflow.analysis.FlowExpressions)7 Receiver (org.checkerframework.dataflow.analysis.FlowExpressions.Receiver)6 FlowExpressionContext (org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionContext)5 TreePath (com.sun.source.util.TreePath)3 Contract (org.checkerframework.framework.util.ContractsUtils.Contract)3 Precondition (org.checkerframework.framework.util.ContractsUtils.Precondition)2 MethodTree (com.sun.source.tree.MethodTree)1 VariableTree (com.sun.source.tree.VariableTree)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Matcher (java.util.regex.Matcher)1 CompilerMessageKey (org.checkerframework.checker.compilermsgs.qual.CompilerMessageKey)1 ArrayLen (org.checkerframework.common.value.qual.ArrayLen)1 ArrayLenRange (org.checkerframework.common.value.qual.ArrayLenRange)1 IntRange (org.checkerframework.common.value.qual.IntRange)1 IntVal (org.checkerframework.common.value.qual.IntVal)1 StringVal (org.checkerframework.common.value.qual.StringVal)1 ConditionalTransferResult (org.checkerframework.dataflow.analysis.ConditionalTransferResult)1