Search in sources :

Example 1 with IntRange

use of org.checkerframework.common.value.qual.IntRange in project checker-framework by typetools.

the class ValueAnnotatedTypeFactory method convertIntRangeToIntVal.

/**
 * Convert an {@code @IntRange} annotation to an {@code @IntVal} annotation, or to UNKNOWNVAL if
 * the input is too wide to be represented as an {@code @IntVal}.
 */
public AnnotationMirror convertIntRangeToIntVal(AnnotationMirror intRangeAnno) {
    Range range = getRange(intRangeAnno);
    List<Long> values = ValueCheckerUtils.getValuesFromRange(range, Long.class);
    return createIntValAnnotation(values);
}
Also used : IntRange(org.checkerframework.common.value.qual.IntRange) ArrayLenRange(org.checkerframework.common.value.qual.ArrayLenRange) Range(org.checkerframework.common.value.util.Range)

Example 2 with IntRange

use of org.checkerframework.common.value.qual.IntRange 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 3 with IntRange

use of org.checkerframework.common.value.qual.IntRange in project checker-framework by typetools.

the class ValueVisitor method visitTypeCast.

@Override
public Void visitTypeCast(TypeCastTree node, Void p) {
    if (node.getExpression().getKind() == Kind.NULL_LITERAL) {
        return null;
    }
    AnnotatedTypeMirror castType = atypeFactory.getAnnotatedType(node);
    AnnotationMirror castAnno = castType.getAnnotationInHierarchy(atypeFactory.UNKNOWNVAL);
    AnnotationMirror exprAnno = atypeFactory.getAnnotatedType(node.getExpression()).getAnnotationInHierarchy(atypeFactory.UNKNOWNVAL);
    // to a @IntRange(from = Byte.MIN_VALUE, to = Byte.MAX_VALUE byte).
    if (castAnno != null && exprAnno != null && atypeFactory.isIntRange(castAnno) && atypeFactory.isIntRange(exprAnno)) {
        Range castRange = ValueAnnotatedTypeFactory.getRange(castAnno);
        if (castType.getKind() == TypeKind.BYTE && castRange.isByteEverything()) {
            return p;
        }
        if (castType.getKind() == TypeKind.SHORT && castRange.isShortEverything()) {
            return p;
        }
        if (castType.getKind() == TypeKind.INT && castRange.isIntEverything()) {
            return p;
        }
        if (castType.getKind() == TypeKind.LONG && castRange.isLongEverything()) {
            return p;
        }
        if (Range.IGNORE_OVERFLOW) {
            // Range.IGNORE_OVERFLOW is only set if this checker is ignoring overflow.
            // In that case, do not warn if the range of the expression encompasses
            // the whole type being casted to (i.e. the warning is actually about overflow).
            Range exprRange = ValueAnnotatedTypeFactory.getRange(exprAnno);
            switch(castType.getKind()) {
                case BYTE:
                    exprRange = exprRange.byteRange();
                    break;
                case SHORT:
                    exprRange = exprRange.shortRange();
                    break;
                case INT:
                    exprRange = exprRange.intRange();
                    break;
                default:
            }
            if (castRange.equals(exprRange)) {
                return p;
            }
        }
    }
    return super.visitTypeCast(node, p);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) Range(org.checkerframework.common.value.util.Range) IntRange(org.checkerframework.common.value.qual.IntRange) ArrayLenRange(org.checkerframework.common.value.qual.ArrayLenRange) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Aggregations

IntRange (org.checkerframework.common.value.qual.IntRange)3 AnnotationMirror (javax.lang.model.element.AnnotationMirror)2 ArrayLenRange (org.checkerframework.common.value.qual.ArrayLenRange)2 Range (org.checkerframework.common.value.util.Range)2 IntVal (org.checkerframework.common.value.qual.IntVal)1 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)1 FlowExpressionParseException (org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionParseException)1