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);
}
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;
}
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);
}
Aggregations