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